From 91ff8574ad48be321ea6e7f73418708d47138b08 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Thu, 8 Apr 2010 09:32:56 +0000 Subject: [PATCH] Improved handling of duplicate packets --- core/net/mac/contikimac.c | 41 ++++++++++++++++++++++++------------ core/net/mac/csma.c | 9 +++++--- core/net/mac/framer-802154.c | 9 ++++++-- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/core/net/mac/contikimac.c b/core/net/mac/contikimac.c index 1718edc1e..779731b54 100644 --- a/core/net/mac/contikimac.c +++ b/core/net/mac/contikimac.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: contikimac.c,v 1.30 2010/04/06 11:57:43 adamdunkels Exp $ + * $Id: contikimac.c,v 1.31 2010/04/08 09:32:56 adamdunkels Exp $ */ /** @@ -192,8 +192,13 @@ static volatile rtimer_clock_t stream_until; #define MIN(a, b) ((a) < (b)? (a) : (b)) #endif /* MIN */ -static int last_received_seqno; -static rimeaddr_t last_received_sender; +struct seqno { + rimeaddr_t sender; + uint8_t seqno; +}; + +#define MAX_SEQNOS 8 +static struct seqno received_seqnos[MAX_SEQNOS]; #if CONTIKIMAC_CONF_BROADCAST_RATE_LIMIT static struct timer broadcast_rate_timer; @@ -896,19 +901,27 @@ input_packet(void) #endif /* WITH_PHASE_OPTIMIZATION */ /* Check for duplicate packet by comparing the sequence number - of the incoming packet with the last one we saw. */ - if(packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == last_received_seqno && - rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER), - &last_received_sender)) { - /* Drop the packet. */ - /* printf("Drop duplicate ContikiMAC layer packet\n");*/ - return; + of the incoming packet with the last few ones we saw. */ + { + int i; + for(i = 0; i < MAX_SEQNOS; ++i) { + if(packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == received_seqnos[i].seqno && + rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER), + &received_seqnos[i].sender)) { + /* Drop the packet. */ + /* printf("Drop duplicate ContikiMAC layer packet\n");*/ + return; + } + } + for(i = MAX_SEQNOS - 1; i > 0; --i) { + memcpy(&received_seqnos[i], &received_seqnos[i - 1], + sizeof(struct seqno)); + } + received_seqnos[0].seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); + rimeaddr_copy(&received_seqnos[0].sender, + packetbuf_addr(PACKETBUF_ADDR_SENDER)); } - - last_received_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); - rimeaddr_copy(&last_received_sender, packetbuf_addr(PACKETBUF_ADDR_SENDER)); - #if CONTIKIMAC_CONF_COMPOWER /* Accumulate the power consumption for the packet reception. */ compower_accumulate(¤t_packet); diff --git a/core/net/mac/csma.c b/core/net/mac/csma.c index 62d4e3714..76e059146 100644 --- a/core/net/mac/csma.c +++ b/core/net/mac/csma.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: csma.c,v 1.15 2010/03/26 12:29:29 nifi Exp $ + * $Id: csma.c,v 1.16 2010/04/08 09:33:37 adamdunkels Exp $ */ /** @@ -164,7 +164,7 @@ packet_sent(void *ptr, int status, int num_transmissions) /* The retransmission time uses a linear backoff so that the interval between the transmissions increase with each retransmit. */ - time = time + (random_rand() % ((q->collisions + q->transmissions) * 3 * time)); + time = time + (random_rand() % ((q->transmissions + 1) * 3 * time)); if(q->transmissions < q->max_transmissions) { PRINTF("csma: retransmitting with time %lu\n", time); @@ -186,7 +186,10 @@ static void send_packet(mac_callback_t sent, void *ptr) { struct queued_packet *q; - + static uint16_t seqno; + + packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, seqno++); + /* Remember packet for later. */ q = memb_alloc(&packet_memb); if(q != NULL) { diff --git a/core/net/mac/framer-802154.c b/core/net/mac/framer-802154.c index 72dbc347a..686f2e2e4 100644 --- a/core/net/mac/framer-802154.c +++ b/core/net/mac/framer-802154.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: framer-802154.c,v 1.9 2010/04/05 19:28:07 adamdunkels Exp $ + * $Id: framer-802154.c,v 1.10 2010/04/08 09:32:56 adamdunkels Exp $ */ /** @@ -101,7 +101,12 @@ create(void) params.fcf.frame_version = FRAME802154_IEEE802154_2003; /* Increment and set the data sequence number. */ - params.seq = mac_dsn++; + if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) { + params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO); + } else { + params.seq = mac_dsn++; + packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq); + } /* params.seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); */ /* Complete the addressing fields. */