Add a CRC checksum to all collect packets to ensure that they are valid, even if they are running on top of a radio link with bit errors

This commit is contained in:
adamdunkels 2008-07-07 23:22:59 +00:00
parent 144927c12b
commit 9f08d7c6de

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: shell-rime.c,v 1.6 2008/07/04 08:23:00 adamdunkels Exp $
* $Id: shell-rime.c,v 1.7 2008/07/07 23:22:59 adamdunkels Exp $
*/
/**
@ -44,6 +44,7 @@
#include "dev/leds.h"
#include "lib/crc16.h"
#include "lib/random.h"
#include "net/rime.h"
@ -74,9 +75,10 @@ struct trickle_msg {
uint8_t type;
};
#define COLLECT_MSG_HDRSIZE 2
#define COLLECT_MSG_HDRSIZE 4
struct collect_msg {
uint16_t timestamp;
uint16_t crc;
uint8_t data[1];
};
@ -376,6 +378,7 @@ PROCESS_THREAD(shell_send_process, ev, data)
#else
msg->timestamp = 0;
#endif
msg->crc = crc16_data(msg->data, len, 0);
/* printf("Sending %d bytes\n", len);*/
collect_send(&collect, COLLECT_REXMITS);
}
@ -388,6 +391,7 @@ recv_collect(const rimeaddr_t *originator, u8_t seqno, u8_t hops)
{
struct collect_msg *collect_msg;
rtimer_clock_t latency;
int len;
collect_msg = rimebuf_dataptr();
@ -406,17 +410,22 @@ recv_collect(const rimeaddr_t *originator, u8_t seqno, u8_t hops)
uint16_t latency;
} msg;
msg.len = 5 + (rimebuf_datalen() - COLLECT_MSG_HDRSIZE) / 2;
rimeaddr_copy((rimeaddr_t *)&msg.originator, originator);
msg.seqno = seqno;
msg.hops = hops;
msg.latency = latency;
/* printf("recv_collect datalen %d\n", rimebuf_datalen());*/
shell_output(&collect_command,
&msg, sizeof(msg),
collect_msg->data, rimebuf_datalen() - COLLECT_MSG_HDRSIZE);
if(rimebuf_datalen() >= COLLECT_MSG_HDRSIZE) {
len = rimebuf_datalen() - COLLECT_MSG_HDRSIZE;
if(collect_msg->crc == crc16_data(collect_msg->data, len, 0)) {
msg.len = 5 + (rimebuf_datalen() - COLLECT_MSG_HDRSIZE) / 2;
rimeaddr_copy((rimeaddr_t *)&msg.originator, originator);
msg.seqno = seqno;
msg.hops = hops;
msg.latency = latency;
/* printf("recv_collect datalen %d\n", rimebuf_datalen());*/
shell_output(&collect_command,
&msg, sizeof(msg),
collect_msg->data, rimebuf_datalen() - COLLECT_MSG_HDRSIZE);
}
}
} else if(waiting_for_nodes) {
char buf[40];
snprintf(buf, sizeof(buf), "%d.%d, %d hops, latency %lu ms",