From 14db3382af50cefc02069f9c2e178284ae9257da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Th=C3=A9baudeau?= Date: Fri, 15 Nov 2013 19:05:52 +0100 Subject: [PATCH] csma: Initialize sequence number with random value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to IEEE 802.15.4 (§5.1.6.1, §6.4.2), the MAC sequence numbers should be initialized to random values. This was already the case in framer-802154.c:create(), but not in csma.c:send_packet(), sometimes causing false detections of duplicate MAC-layer packets in other devices when a device was restarted too quickly. This patch decreases the probability of such an event. Signed-off-by: Benoît Thébaudeau --- core/net/mac/csma.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/net/mac/csma.c b/core/net/mac/csma.c index 66528e542..b75a812ca 100644 --- a/core/net/mac/csma.c +++ b/core/net/mac/csma.c @@ -306,9 +306,16 @@ send_packet(mac_callback_t sent, void *ptr) { struct rdc_buf_list *q; struct neighbor_queue *n; + static uint8_t initialized = 0; static uint16_t seqno; const rimeaddr_t *addr = packetbuf_addr(PACKETBUF_ADDR_RECEIVER); + if(!initialized) { + initialized = 1; + /* Initialize the sequence number to a random value as per 802.15.4. */ + seqno = random_rand(); + } + if(seqno == 0) { /* PACKETBUF_ATTR_MAC_SEQNO cannot be zero, due to a pecuilarity in framer-802154.c. */