diff --git a/firmware/funk/Makefile b/firmware/funk/Makefile index e44105f..dd2a593 100644 --- a/firmware/funk/Makefile +++ b/firmware/funk/Makefile @@ -6,6 +6,7 @@ OBJS = OBJS += nrf24l01p.o OBJS += rftransfer.o +OBJS += filetransfer.o LIBNAME=funk diff --git a/firmware/funk/filetransfer.c b/firmware/funk/filetransfer.c new file mode 100644 index 0000000..5bae5d2 --- /dev/null +++ b/firmware/funk/filetransfer.c @@ -0,0 +1,89 @@ +#include +#include "nrf24l01p.h" +#include "filetransfer.h" +#include "rftransfer.h" +#include "basic/xxtea.h" +#include "filesystem/ff.h" + +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; + + res = f_read(&file, (char *)buf, size, &readbytes); + if( res ) + return res; + if( size != readbytes) + return 1; //Error while reading + + uint16_t wordcount = (size+3)/4; + uint8_t macbuf[5]; + + uint8_t metadata[30]; + 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); + nrf_set_tx_mac(5, mac); + + nrf_snd_pkt_xxtea(30, metadata, k); + xxtea_encode_words((uint32_t *)buf, wordcount, k); + rftransfer_send(wordcount*4, buf); + nrf_set_tx_mac(5, macbuf); + return 0; +} + +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]; + //nrf_get_tx_max(5,macbuf); + nrf_set_tx_mac(5, mac); + + uint8_t metadata[30]; + nrf_rcv_pkt_time_xxtea(1000, 30, metadata, k); + metadata[19] = 0; //enforce termination + size = (metadata[20] << 8) | metadata[21]; + + if( size > MAXSIZE ) return 1; //file to big + //if(fileexists(metadata)) return 1; //file already exists + + res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE); + if( res ) + return res; + + uint16_t wordcount = (size+3)/4; + rftransfer_receive(buf, wordcount*4, 1000); + + nrf_set_tx_mac(5, macbuf); + + xxtea_decode_words((uint32_t *)buf, wordcount, k); + + res = f_write(&file, buf, size, &written); + if( res ) + return res; + if( written != size ) + return 1; //error while writing + + return 0; +} + diff --git a/firmware/funk/filetransfer.h b/firmware/funk/filetransfer.h new file mode 100644 index 0000000..d155bfc --- /dev/null +++ b/firmware/funk/filetransfer.h @@ -0,0 +1,10 @@ +#ifndef _FILETRANSFER_H_ +#define _FILETRANSFER_H_ +#include + +#define MAXSIZE 1024 + +int filetransfer_send(uint8_t *filename, uint16_t size, + uint8_t *mac, uint32_t const k[4]); +int filetransfer_receive(uint8_t *mac, uint32_t const k[4]); +#endif diff --git a/firmware/funk/nrf24l01p.h b/firmware/funk/nrf24l01p.h index ada0fbd..c78c590 100644 --- a/firmware/funk/nrf24l01p.h +++ b/firmware/funk/nrf24l01p.h @@ -1,5 +1,6 @@ #ifndef _NRF24L01P_H #define _NRF24L01P_H 1 +#include #define MAX_PKT (32-2) // 2 bytes are our CRC @@ -109,7 +110,10 @@ /* exported functions */ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt); +int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize, + uint8_t * pkt, uint32_t const k[4]); char nrf_snd_pkt_crc(int size, uint8_t * pkt); +char nrf_snd_pkt_xxtea(int size, uint8_t * pkt, uint32_t const k[4]); void nrf_init() ; void nrf_cmd(uint8_t cmd);