Contiki-NG
nbr-table.h
1 /*
2  * Copyright (c) 2013, Swedish Institute of Computer Science
3  * Copyright (c) 2010, Vrije Universiteit Brussel
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *
31  * Authors: Simon Duquennoy <simonduq@sics.se>
32  * Joris Borms <joris.borms@vub.ac.be>
33  */
34 
35 #ifndef NBR_TABLE_H_
36 #define NBR_TABLE_H_
37 
38 #include "contiki.h"
39 #include "net/linkaddr.h"
40 #include "net/netstack.h"
41 
42 /* Neighbor table size */
43 #ifdef NBR_TABLE_CONF_MAX_NEIGHBORS
44 #define NBR_TABLE_MAX_NEIGHBORS NBR_TABLE_CONF_MAX_NEIGHBORS
45 #else /* NBR_TABLE_CONF_MAX_NEIGHBORS */
46 #define NBR_TABLE_MAX_NEIGHBORS 8
47 #endif /* NBR_TABLE_CONF_MAX_NEIGHBORS */
48 
49 /* An item in a neighbor table */
50 typedef void nbr_table_item_t;
51 
52 /* Callback function, called when removing an item from a table */
53 typedef void(nbr_table_callback)(nbr_table_item_t *item);
54 
55 /* A neighbor table */
56 typedef struct nbr_table {
57  int index;
58  int item_size;
59  nbr_table_callback *callback;
60  nbr_table_item_t *data;
61 } nbr_table_t;
62 
63 /** \brief A static neighbor table. To be initialized through nbr_table_register(name) */
64 #define NBR_TABLE(type, name) \
65  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
66  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
67  static nbr_table_t *name = &name##_struct \
68 
69 /** \brief A non-static neighbor table. To be initialized through nbr_table_register(name) */
70 #define NBR_TABLE_GLOBAL(type, name) \
71  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
72  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
73  nbr_table_t *name = &name##_struct \
74 
75 /** \brief Declaration of non-static neighbor tables */
76 #define NBR_TABLE_DECLARE(name) extern nbr_table_t *name
77 
78 typedef enum {
79  NBR_TABLE_REASON_UNDEFINED,
80  NBR_TABLE_REASON_RPL_DIO,
81  NBR_TABLE_REASON_RPL_DAO,
82  NBR_TABLE_REASON_RPL_DIS,
83  NBR_TABLE_REASON_ROUTE,
84  NBR_TABLE_REASON_IPV6_ND,
85  NBR_TABLE_REASON_IPV6_ND_AUTOFILL,
86  NBR_TABLE_REASON_MAC,
87  NBR_TABLE_REASON_LLSEC,
88  NBR_TABLE_REASON_LINK_STATS,
89  NBR_TABLE_REASON_SIXTOP,
90 } nbr_table_reason_t;
91 
92 /** \name Neighbor tables: register and loop through table elements */
93 /** @{ */
94 int nbr_table_register(nbr_table_t *table, nbr_table_callback *callback);
95 int nbr_table_is_registered(nbr_table_t *table);
96 nbr_table_item_t *nbr_table_head(nbr_table_t *table);
97 nbr_table_item_t *nbr_table_next(nbr_table_t *table, nbr_table_item_t *item);
98 /** @} */
99 
100 /** \name Neighbor tables: add and get data */
101 /** @{ */
102 nbr_table_item_t *nbr_table_add_lladdr(nbr_table_t *table, const linkaddr_t *lladdr, nbr_table_reason_t reason, void *data);
103 nbr_table_item_t *nbr_table_get_from_lladdr(nbr_table_t *table, const linkaddr_t *lladdr);
104 /** @} */
105 
106 /** \name Neighbor tables: set flags (unused, locked, unlocked) */
107 /** @{ */
108 int nbr_table_remove(nbr_table_t *table, nbr_table_item_t *item);
109 int nbr_table_lock(nbr_table_t *table, nbr_table_item_t *item);
110 int nbr_table_unlock(nbr_table_t *table, nbr_table_item_t *item);
111 /** @} */
112 
113 /** \name Neighbor tables: address manipulation */
114 /** @{ */
115 linkaddr_t *nbr_table_get_lladdr(nbr_table_t *table, const nbr_table_item_t *item);
116 /** @} */
117 
118 #endif /* NBR_TABLE_H_ */
Header file for the link-layer address representation
Include file for the Contiki low-layer network stack (NETSTACK)