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,23 +136,15 @@ 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...
break; nrf_read_long(C_R_RX_PL_WID,1,&len);
};
};
};
CE_LOW();
if(maxtime<LOOPY)
return 0; // timeout
len=1;
nrf_read_long(C_R_RX_PL_WID,len,&buf);
len=buf;
if(len>32 || len==0){ if(len>32 || len==0){
continue;
return -2; // no packet error return -2; // no packet error
}; };
len-=2; // skip crc len-=2; // crc is not part of the length
if(len>maxsize){ if(len>maxsize){
continue;
return -1; // packet too large return -1; // packet too large
}; };
@ -161,10 +152,20 @@ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
cmpcrc=crc16(pkt,len); cmpcrc=crc16(pkt,len);
if(cmpcrc != (crc[0] <<8 | crc[1])) { if(cmpcrc != (crc[0] <<8 | crc[1])) {
continue;
return -3; // CRC failed return -3; // CRC failed
}; };
break;
};
};
};
CE_LOW();
CS_HIGH(); CS_HIGH();
if(maxtime<LOOPY)
return 0; // timeout
return len; return len;
}; };