Contiki 3.x
mac-sequence.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
6  * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * This file is part of the Contiki operating system.
34  *
35  */
36 
37 /**
38  * \file
39  * MAC sequence numbers management
40  * \author
41  * Adam Dunkels <adam@sics.se>
42  * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
43  */
44 
45 #include <string.h>
46 
47 #include "contiki-net.h"
48 #include "net/mac/mac-sequence.h"
49 #include "net/packetbuf.h"
50 #include "net/rime/rime.h"
51 
52 struct seqno {
53  linkaddr_t sender;
54  uint8_t seqno;
55 };
56 
57 #ifdef NETSTACK_CONF_MAC_SEQNO_HISTORY
58 #define MAX_SEQNOS NETSTACK_CONF_MAC_SEQNO_HISTORY
59 #else /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
60 #define MAX_SEQNOS 16
61 #endif /* NETSTACK_CONF_MAC_SEQNO_HISTORY */
62 static struct seqno received_seqnos[MAX_SEQNOS];
63 
64 /*---------------------------------------------------------------------------*/
65 int
67 {
68  int i;
69 
70  /*
71  * Check for duplicate packet by comparing the sequence number of the incoming
72  * packet with the last few ones we saw.
73  */
74  for(i = 0; i < MAX_SEQNOS; ++i) {
75  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
76  &received_seqnos[i].sender)) {
77  if(packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == received_seqnos[i].seqno) {
78  /* Duplicate packet. */
79  return 1;
80  }
81  break;
82  }
83  }
84  return 0;
85 }
86 /*---------------------------------------------------------------------------*/
87 void
89 {
90  int i, j;
91 
92  /* Locate possible previous sequence number for this address. */
93  for(i = 0; i < MAX_SEQNOS; ++i) {
94  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
95  &received_seqnos[i].sender)) {
96  i++;
97  break;
98  }
99  }
100 
101  /* Keep the last sequence number for each address as per 802.15.4e. */
102  for(j = i - 1; j > 0; --j) {
103  memcpy(&received_seqnos[j], &received_seqnos[j - 1], sizeof(struct seqno));
104  }
105  received_seqnos[0].seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
106  linkaddr_copy(&received_seqnos[0].sender,
107  packetbuf_addr(PACKETBUF_ADDR_SENDER));
108 }
109 /*---------------------------------------------------------------------------*/
int mac_sequence_is_duplicate(void)
Tell whether the packetbuf is a duplicate packet.
Definition: mac-sequence.c:66
Header file for the Rime buffer (packetbuf) management
Header file for MAC sequence numbers management
Header file for the Rime stack
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
void mac_sequence_register_seqno(void)
Register the sequence number of the packetbuf.
Definition: mac-sequence.c:88
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66