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
1 changed files with 22 additions and 13 deletions

View File

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