Schneider will probably hate me, but change back to require packet buffer to include space for the crc16.

This commit is contained in:
Stefan `Sec` Zehl 2011-07-17 00:50:54 +02:00
parent 486d52b624
commit 2ee9846d1c
2 changed files with 31 additions and 14 deletions

View File

@ -66,6 +66,13 @@ void nrf_read_long(const uint8_t cmd, int len, uint8_t* data){
CS_HIGH(); CS_HIGH();
}; };
void nrf_read_pkt(int len, uint8_t* data){
CS_LOW();
xmit_spi(C_R_RX_PAYLOAD);
sspReceive(0,data,len);
CS_HIGH();
};
void nrf_read_pkt_crc(int len, uint8_t* data, uint8_t* crc){ void nrf_read_pkt_crc(int len, uint8_t* data, uint8_t* crc){
CS_LOW(); CS_LOW();
xmit_spi(C_R_RX_PAYLOAD); xmit_spi(C_R_RX_PAYLOAD);
@ -103,11 +110,9 @@ int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
return n; return n;
} }
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
uint8_t len; uint8_t len;
uint8_t status=0; uint8_t status=0;
uint8_t crc[2];
uint16_t cmpcrc; uint16_t cmpcrc;
nrf_write_reg(R_CONFIG, nrf_write_reg(R_CONFIG,
@ -140,16 +145,18 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
continue; continue;
return -2; // no packet error return -2; // no packet error
}; };
len-=2; // crc is not part of the length
if(len>maxsize){ if(len>maxsize){
continue; continue;
return -1; // packet too large return -1; // packet too large
}; };
nrf_read_pkt_crc(len,pkt,crc); nrf_read_pkt(len,pkt);
cmpcrc=crc16(pkt,len); cmpcrc=crc16(pkt,len-2);
if(key != NULL)
xxtea_decode_words((uint32_t*)pkt,len/4,key);
if(cmpcrc != (crc[0] <<8 | crc[1])) { if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
continue; continue;
return -3; // CRC failed return -3; // CRC failed
}; };
@ -167,7 +174,7 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
return len; return len;
}; };
char nrf_snd_pkt_crc(int size, uint8_t * pkt){ char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){
if(size > MAX_PKT) if(size > MAX_PKT)
size=MAX_PKT; size=MAX_PKT;
@ -178,12 +185,15 @@ char nrf_snd_pkt_crc(int size, uint8_t * pkt){
); );
// nrf_write_long(C_W_TX_PAYLOAD,size,pkt); // nrf_write_long(C_W_TX_PAYLOAD,size,pkt);
uint16_t crc=crc16(pkt,size); uint16_t crc=crc16(pkt,size-2);
pkt[size-2]=(crc >>8) & 0xff;
pkt[size-1]=crc & 0xff;
if(key !=NULL)
xxtea_encode_words((uint32_t*)pkt,size/4,key);
CS_LOW(); CS_LOW();
xmit_spi(C_W_TX_PAYLOAD); xmit_spi(C_W_TX_PAYLOAD);
sspSend(0,pkt,size); sspSend(0,pkt,size);
xmit_spi((crc >>8) & 0xff);
xmit_spi(crc & 0xff);
CS_HIGH(); CS_HIGH();
CE_HIGH(); CE_HIGH();
@ -321,6 +331,5 @@ void nrf_init() {
// Set speed / strength // Set speed / strength
nrf_write_reg(R_RF_SETUP,DEFAULT_SPEED|R_RF_SETUP_RF_PWR_3); nrf_write_reg(R_RF_SETUP,DEFAULT_SPEED|R_RF_SETUP_RF_PWR_3);
}; };

View File

@ -128,10 +128,18 @@ typedef struct NRF_CFG * nrfconfig;
/* exported functions */ /* exported functions */
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt); #define nrf_rcv_pkt_time(maxtime, maxsize, pkt) \
nrf_rcv_pkt_time_encr(maxtime, maxsize, pkt, NULL)
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const k[4]);
#define nrf_snd_pkt_crc(size, pkt) \
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, int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
uint8_t * pkt, uint32_t const k[4]); 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]); char nrf_snd_pkt_xxtea(int size, uint8_t * pkt, uint32_t const k[4]);
void nrf_init() ; void nrf_init() ;