2011-07-14 21:40:03 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "nrf24l01p.h"
|
|
|
|
#include "filetransfer.h"
|
|
|
|
#include "rftransfer.h"
|
|
|
|
#include "basic/xxtea.h"
|
|
|
|
#include "filesystem/ff.h"
|
|
|
|
|
2011-07-16 01:38:44 +00:00
|
|
|
|
|
|
|
//TODO: use a proper MAC to sign the message
|
2011-07-14 21:40:03 +00:00
|
|
|
int filetransfer_send(uint8_t *filename, uint16_t size,
|
|
|
|
uint8_t *mac, uint32_t const k[4])
|
|
|
|
{
|
|
|
|
uint8_t buf[MAXSIZE];
|
|
|
|
FIL file;
|
|
|
|
FRESULT res;
|
|
|
|
UINT readbytes;
|
|
|
|
|
|
|
|
if( size > MAXSIZE )
|
|
|
|
return 1; //File to big
|
|
|
|
|
|
|
|
res=f_open(&file, (const char*)filename, FA_OPEN_EXISTING|FA_READ);
|
|
|
|
if( res )
|
|
|
|
return res;
|
|
|
|
|
2011-07-16 01:38:44 +00:00
|
|
|
//res = f_read(&file, (char *)buf, size, &readbytes);
|
|
|
|
res = f_read(&file, (char *)buf, MAXSIZE, &readbytes);
|
|
|
|
size = readbytes;
|
|
|
|
|
2011-07-14 21:40:03 +00:00
|
|
|
if( res )
|
|
|
|
return res;
|
|
|
|
if( size != readbytes)
|
|
|
|
return 1; //Error while reading
|
|
|
|
|
|
|
|
uint16_t wordcount = (size+3)/4;
|
|
|
|
uint8_t macbuf[5];
|
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
uint8_t metadata[32];
|
2011-07-14 21:40:03 +00:00
|
|
|
if( strlen((char*)filename) < 20 )
|
|
|
|
strcpy((char*)metadata, (char*)filename);
|
|
|
|
else
|
|
|
|
return 1; //File name too long
|
|
|
|
metadata[20] = size >> 8;
|
|
|
|
metadata[21] = size & 0xFF;
|
|
|
|
|
|
|
|
//nrf_get_tx_max(5,macbuf);
|
2011-07-16 01:38:44 +00:00
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
//nrf_set_tx_mac(5, mac);
|
|
|
|
nrf_snd_pkt_crc_encr(32, metadata, k);
|
|
|
|
delayms(20);
|
2011-07-14 21:40:03 +00:00
|
|
|
xxtea_encode_words((uint32_t *)buf, wordcount, k);
|
|
|
|
rftransfer_send(wordcount*4, buf);
|
2011-07-17 10:46:33 +00:00
|
|
|
//nrf_set_tx_mac(5, macbuf);
|
2011-07-14 21:40:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-14 21:40:03 +00:00
|
|
|
int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
|
|
|
|
{
|
|
|
|
uint8_t buf[MAXSIZE+1];
|
|
|
|
uint16_t size;
|
|
|
|
|
|
|
|
UINT written = 0;
|
|
|
|
FIL file;
|
|
|
|
FRESULT res;
|
|
|
|
uint8_t macbuf[5];
|
2011-07-16 01:38:44 +00:00
|
|
|
//nrf_get_rx_max(0,5,macbuf);
|
2011-07-14 21:40:03 +00:00
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
uint8_t metadata[32];
|
2011-07-16 01:38:44 +00:00
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
//nrf_set_rx_mac(0, 32, 5, mac);
|
|
|
|
nrf_rcv_pkt_time_encr(3000, 32, metadata, k);
|
2011-07-16 18:17:46 +00:00
|
|
|
//nrf_set_rx_mac(0, 32, 5, macbuf);
|
2011-07-17 10:46:33 +00:00
|
|
|
//lcdPrintln("got meta"); lcdRefresh();
|
2011-07-14 21:40:03 +00:00
|
|
|
metadata[19] = 0; //enforce termination
|
|
|
|
size = (metadata[20] << 8) | metadata[21];
|
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
if( size > MAXSIZE ) {lcdPrintln("too big"); lcdRefresh(); while(1);}
|
2011-07-14 21:40:03 +00:00
|
|
|
if( size > MAXSIZE ) return 1; //file to big
|
|
|
|
//if(fileexists(metadata)) return 1; //file already exists
|
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
lcdPrint("open"); lcdPrintln(metadata); lcdRefresh();
|
2011-07-14 21:40:03 +00:00
|
|
|
res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE);
|
2011-07-17 10:46:33 +00:00
|
|
|
|
|
|
|
//lcdPrintln("file opened"); lcdRefresh();
|
|
|
|
if( res ) {lcdPrintln("res"); put_rc(res); lcdRefresh(); while(1);}
|
2011-07-14 21:40:03 +00:00
|
|
|
if( res )
|
|
|
|
return res;
|
|
|
|
|
|
|
|
uint16_t wordcount = (size+3)/4;
|
2011-07-16 01:38:44 +00:00
|
|
|
|
2011-07-17 10:46:33 +00:00
|
|
|
//nrf_set_rx_mac(0, 32, 5, mac);
|
|
|
|
lcdPrintln("get file"); lcdRefresh();
|
2011-07-14 21:40:03 +00:00
|
|
|
rftransfer_receive(buf, wordcount*4, 1000);
|
2011-07-17 10:46:33 +00:00
|
|
|
lcdPrintln("got file"); lcdRefresh();
|
2011-07-16 18:17:46 +00:00
|
|
|
//nrf_set_rx_mac(0, 32, 5, macbuf);
|
2011-07-14 21:40:03 +00:00
|
|
|
|
|
|
|
xxtea_decode_words((uint32_t *)buf, wordcount, k);
|
|
|
|
|
|
|
|
res = f_write(&file, buf, size, &written);
|
2011-07-17 10:46:33 +00:00
|
|
|
f_close(&file);
|
2011-07-14 21:40:03 +00:00
|
|
|
if( res )
|
|
|
|
return res;
|
|
|
|
if( written != size )
|
|
|
|
return 1; //error while writing
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|