Revamp receive function. It now checks everything and returns only

valid packets or 0 (timeout)
This commit is contained in:
Stefan `Sec` Zehl 2011-07-16 20:25:15 +02:00
parent 830ecd93d4
commit afd01eada1
1 changed files with 23 additions and 22 deletions

View File

@ -107,8 +107,7 @@ int nrf_rcv_pkt_time_xxtea(int maxtime, int maxsize,
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
uint8_t buf; uint8_t len;
int len;
uint8_t status=0; uint8_t status=0;
uint8_t crc[2]; uint8_t crc[2];
uint16_t cmpcrc; uint16_t cmpcrc;
@ -137,34 +136,36 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
delayms(1); delayms(1);
nrf_write_reg(R_STATUS,0); nrf_write_reg(R_STATUS,0);
continue; continue;
}else{ }else{ // Get/Check packet...
nrf_read_long(C_R_RX_PL_WID,1,&len);
if(len>32 || len==0){
continue;
return -2; // no packet error
};
len-=2; // crc is not part of the length
if(len>maxsize){
continue;
return -1; // packet too large
};
nrf_read_pkt_crc(len,pkt,crc);
cmpcrc=crc16(pkt,len);
if(cmpcrc != (crc[0] <<8 | crc[1])) {
continue;
return -3; // CRC failed
};
break; break;
}; };
}; };
}; };
CE_LOW(); CE_LOW();
CS_HIGH();
if(maxtime<LOOPY) if(maxtime<LOOPY)
return 0; // timeout return 0; // timeout
len=1;
nrf_read_long(C_R_RX_PL_WID,len,&buf);
len=buf;
if(len>32 || len==0){
return -2; // no packet error
};
len-=2; // skip crc
if(len>maxsize){
return -1; // packet too large
};
nrf_read_pkt_crc(len,pkt,crc);
cmpcrc=crc16(pkt,len);
if(cmpcrc != (crc[0] <<8 | crc[1])) {
return -3; // CRC failed
};
CS_HIGH();
return len; return len;
}; };