vcard: moved to new rf interface

This commit is contained in:
schneider 2011-07-17 12:46:33 +02:00
parent 46db3203d7
commit 13637cafb9
5 changed files with 78 additions and 87 deletions

View File

@ -24,46 +24,46 @@ char *Priv ="0e10e787036941e6c78daf8a0e8e1dbfac68e26d2";
void sendPublicKey(char *px, char *py)
{
uint8_t exp[2 + 4*NUMWORDS];
uint8_t exp[2 + 4*NUMWORDS + 2];
exp[0] = 'P';
bitstr_parse_export((char*)exp+2, px);
exp[1] = 'X';
nrf_snd_pkt_crc(30, exp);
nrf_snd_pkt_crc(32, exp);
delayms(10);
exp[1] = 'Y';
bitstr_parse_export((char*)exp+2, py);
nrf_snd_pkt_crc(30, exp);
nrf_snd_pkt_crc(32, exp);
delayms(10);
}
void sendR(uint8_t *rx, uint8_t *ry)
{
uint8_t exp[2 + 4*NUMWORDS];
uint8_t exp[2 + 4*NUMWORDS + 2];
exp[0] = 'R';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = rx[i];
exp[1] = 'X';
nrf_snd_pkt_crc(30, exp);
nrf_snd_pkt_crc(32, exp);
delayms(10);
exp[1] = 'Y';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = ry[i];
nrf_snd_pkt_crc(30, exp);
nrf_snd_pkt_crc(32, exp);
delayms(10);
}
int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
{
uint8_t buf[30];
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(1000, 30, buf);
n = nrf_rcv_pkt_time(1000, 32, buf);
lcdPrint("pkt:"); lcdPrintInt(n);lcdPrintln(""); lcdRefresh();
if( n == 30 && buf[0] == type && buf[1] == 'X' ){
if( n == 32 && buf[0] == type && buf[1] == 'X' ){
for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2];
n = nrf_rcv_pkt_time(100, 30, buf);
if( n == 30 && buf[0] ==type && buf[1] == 'Y' ){
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] ==type && buf[1] == 'Y' ){
for(int i=0; i<NUMWORDS*4; i++)
y[i] = buf[i+2];
return 0;
@ -84,7 +84,7 @@ int receiveR(uint8_t *rx, uint8_t *ry)
void sendMac(void)
{
uint8_t buf[7];
uint8_t buf[32];
buf[0] = 'M';
buf[1] = 'C';
buf[2] = mac[0];
@ -92,17 +92,17 @@ void sendMac(void)
buf[4] = mac[2];
buf[5] = mac[3];
buf[6] = mac[4];
nrf_snd_pkt_crc(30, buf);
nrf_snd_pkt_crc(32, buf);
delayms(10);
}
int receiveMac(uint8_t *mac)
{
uint8_t buf[30];
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(100, 30, buf);
if( n == 30 && buf[0] == 'M' && buf[1] == 'C' ){
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] == 'M' && buf[1] == 'C' ){
for(int i=0; i<5; i++)
mac[i] = buf[i+2];
return 0;
@ -340,24 +340,6 @@ void main_vcard(void) {
lcdPrintln("Done");
lcdRefresh();
}else if(key==BTN_RIGHT){
//uint8_t *data1 = "123456789012345678901234567890";
uint8_t data2[30] = "123456789012345678901234567890";
uint32_t key[4] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
lcdClear();
lcdPrintln("encode"); lcdRefresh();
xxtea_encode(data2, 30, key);
//xxtea_encode_words(data2, 7 , key);
lcdPrintln("decode"); lcdRefresh();
xxtea_decode(data2, 30, key);
//xxtea_decode_words(data2, 7 , key);
lcdClear();
for(int i=0; i<30; i++){
lcdPrintCharHex(data2[i]);
if((i+1)%5==0)
lcdPrintln("");
}
lcdRefresh();
}
//encryption_decryption_demo("This is encrypted",

View File

@ -34,7 +34,7 @@ int filetransfer_send(uint8_t *filename, uint16_t size,
uint16_t wordcount = (size+3)/4;
uint8_t macbuf[5];
uint8_t metadata[30];
uint8_t metadata[32];
if( strlen((char*)filename) < 20 )
strcpy((char*)metadata, (char*)filename);
else
@ -44,14 +44,34 @@ int filetransfer_send(uint8_t *filename, uint16_t size,
//nrf_get_tx_max(5,macbuf);
nrf_set_tx_mac(5, mac);
nrf_snd_pkt_xxtea(30, metadata, k);
//nrf_set_tx_mac(5, mac);
nrf_snd_pkt_crc_encr(32, metadata, k);
delayms(20);
xxtea_encode_words((uint32_t *)buf, wordcount, k);
rftransfer_send(wordcount*4, buf);
nrf_set_tx_mac(5, macbuf);
//nrf_set_tx_mac(5, macbuf);
return 0;
}
void put_rc_y (FRESULT rc, int y) {
const TCHAR *p =
_T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
_T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
_T("NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
_T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0");
FRESULT i;
for (i = 0; i != rc && *p; i++) {
while(*p++) ;
}
DoString(0,y,p);
}
void put_rc (FRESULT rc){
put_rc_y(rc,0);
};
int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
{
uint8_t buf[MAXSIZE+1];
@ -63,31 +83,39 @@ int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
uint8_t macbuf[5];
//nrf_get_rx_max(0,5,macbuf);
uint8_t metadata[30];
uint8_t metadata[32];
nrf_set_rx_mac(0, 32, 5, mac);
nrf_rcv_pkt_time_xxtea(1000, 30, metadata, k);
//nrf_set_rx_mac(0, 32, 5, mac);
nrf_rcv_pkt_time_encr(3000, 32, metadata, k);
//nrf_set_rx_mac(0, 32, 5, macbuf);
//lcdPrintln("got meta"); lcdRefresh();
metadata[19] = 0; //enforce termination
size = (metadata[20] << 8) | metadata[21];
if( size > MAXSIZE ) {lcdPrintln("too big"); lcdRefresh(); while(1);}
if( size > MAXSIZE ) return 1; //file to big
//if(fileexists(metadata)) return 1; //file already exists
lcdPrint("open"); lcdPrintln(metadata); lcdRefresh();
res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE);
//lcdPrintln("file opened"); lcdRefresh();
if( res ) {lcdPrintln("res"); put_rc(res); lcdRefresh(); while(1);}
if( res )
return res;
uint16_t wordcount = (size+3)/4;
nrf_set_rx_mac(0, 32, 5, mac);
//nrf_set_rx_mac(0, 32, 5, mac);
lcdPrintln("get file"); lcdRefresh();
rftransfer_receive(buf, wordcount*4, 1000);
lcdPrintln("got file"); lcdRefresh();
//nrf_set_rx_mac(0, 32, 5, macbuf);
xxtea_decode_words((uint32_t *)buf, wordcount, k);
res = f_write(&file, buf, size, &written);
f_close(&file);
if( res )
return res;
if( written != size )

View File

@ -92,24 +92,6 @@ void nrf_write_long(const uint8_t cmd, int len, uint8_t* data){
nrf_write_long(C_W_REGISTER|(reg), len, data)
// High-Level:
int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
uint8_t * pkt, uint32_t const k[4])
{
int n = nrf_rcv_pkt_time(maxtime, maxsize, pkt);
if( n ){
if( n < 2 )
return -4;
xxtea_decode(pkt, n, k);
uint16_t crc=crc16(pkt,n-2);
if( pkt[n-2] == ((crc>>8)&0xFF) && pkt[n-1] == (crc&0xFF) ){
return n;
}else{
return -5;
}
}
return n;
}
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){
uint8_t len;
uint8_t status=0;
@ -203,12 +185,6 @@ char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){
return nrf_cmd_status(C_NOP);
};
char nrf_snd_pkt_xxtea(int size, uint8_t * pkt, uint32_t const k[4])
{
xxtea_encode(pkt, size, k);
return nrf_snd_pkt_crc(size, pkt);
}
void nrf_set_rx_mac(int pipe, int rxlen, int maclen, uint8_t * mac){
#ifdef SAFE
assert(maclen>=1 || maclen<=5);

View File

@ -137,10 +137,6 @@ int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t cons
nrf_snd_pkt_crc_encr(size, pkt, NULL)
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const k[4]);
// Schneider-stuff?
int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
uint8_t * pkt, uint32_t const k[4]);
char nrf_snd_pkt_xxtea(int size, uint8_t * pkt, uint32_t const k[4]);
void nrf_init() ;
void nrf_cmd(uint8_t cmd);

View File

@ -3,7 +3,7 @@
#include <basic/basic.h>
#include <core/systick/systick.h>
#define MAXPACKET 30
#define MAXPACKET 32
void rftransfer_send(uint16_t size, uint8_t *data)
{
uint8_t buf[MAXPACKET];
@ -16,8 +16,8 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[4] = rand & 0xFF;
//nrf_snd_pkt_crc(5,buf); //setup packet
nrf_snd_pkt_crc(30,buf); //setup packet
delayms(10);
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
uint16_t index = 0;
uint8_t i;
uint16_t crc = crc16(data,size);
@ -32,9 +32,8 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[i] = *data++;
}
index++;
//nrf_snd_pkt_crc(i,buf); //data packet
nrf_snd_pkt_crc(30,buf); //setup packet
delayms(10);
nrf_snd_pkt_crc(32,buf); //data packet
delayms(20);
}
buf[0] = 'C';
@ -43,8 +42,8 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
//nrf_snd_pkt_crc(5,buf); //crc packet
nrf_snd_pkt_crc(30,buf); //setup packet
delayms(10);
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
}
uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
@ -57,36 +56,46 @@ uint16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
unsigned int startTick = currentTick;
while(currentTick < (startTick+timeout) ){//this fails if either overflows
n = nrf_rcv_pkt_time(10, MAXPACKET, buf);
n = nrf_rcv_pkt_time(1000, MAXPACKET, buf);
switch(state){
case 0:
if( n == 5 && buf[0] == 'L' ){
if( n == 32 && buf[0] == 'L' ){
size = (buf[1] << 8) | buf[2];
rand = (buf[3] << 8) | buf[4];
seq = 0;
pos = 0;
if( size <= maxlen )
if( size <= maxlen ){
lcdClear();
lcdPrintln("got l"); lcdRefresh();
state = 1;
}
}
break;
case 1:
if( n > 5 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){
if( n == 32 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){
lcdPrint("got d"); lcdRefresh();
if( seq == ((buf[1]<<8)|buf[2]) ){
if( (pos + n - 5)<maxlen ){
for(i=5; i<n; i++,pos++){
lcdPrintln(" in seq"); lcdRefresh();
//if( (pos + n - 5)<maxlen ){
//for(i=5; i<n; i++,pos++){
for(i=5; i<n && pos<size; i++,pos++){
buffer[pos] = buf[i];
}
}
seq++;
//}
}
}
if( pos == size ){
lcdPrintln("got all"); lcdRefresh();
crc = crc16(buffer, size);
state = 2;
}
break;
case 2:
if( n == 5 && buf[0] == 'C' && ((buf[3]<<8)|buf[4])==rand){
if( n == 32 && buf[0] == 'C' && ((buf[3]<<8)|buf[4])==rand){
lcdPrint("got crc"); lcdRefresh();
if( crc == ((buf[1]<<8)|buf[2]) ){
lcdPrintln(" ok"); lcdRefresh();
return size;
}else{
state = 0;