Contiki-Inga 3.x
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 
32 /**
33  * \file Neighbor Table
34  *
35  * \author Simon Duquennoy <simonduq@sics.se>
36  * \author Joris Borms <joris.borms@vub.ac.be>
37  */
38 
39 #ifndef NBR_TABLE_H_
40 #define NBR_TABLE_H_
41 
42 #include "contiki.h"
43 #include "net/linkaddr.h"
44 #include "net/netstack.h"
45 
46 /* Neighbor table size */
47 #ifdef NBR_TABLE_CONF_MAX_NEIGHBORS
48 #define NBR_TABLE_MAX_NEIGHBORS NBR_TABLE_CONF_MAX_NEIGHBORS
49 #else /* NBR_TABLE_CONF_MAX_NEIGHBORS */
50 #define NBR_TABLE_MAX_NEIGHBORS 8
51 #endif /* NBR_TABLE_CONF_MAX_NEIGHBORS */
52 
53 /* An item in a neighbor table */
54 typedef void nbr_table_item_t;
55 
56 /* Callback function, called when removing an item from a table */
57 typedef void(nbr_table_callback)(nbr_table_item_t *item);
58 
59 /* A neighbor table */
60 typedef struct nbr_table {
61  int index;
62  int item_size;
63  nbr_table_callback *callback;
64  nbr_table_item_t *data;
65 } nbr_table_t;
66 
67 /** \brief A static neighbor table. To be initialized through nbr_table_register(name) */
68 #define NBR_TABLE(type, name) \
69  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
70  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
71  static nbr_table_t *name = &name##_struct \
72 
73 /** \brief A non-static neighbor table. To be initialized through nbr_table_register(name) */
74 #define NBR_TABLE_GLOBAL(type, name) \
75  static type _##name##_mem[NBR_TABLE_MAX_NEIGHBORS]; \
76  static nbr_table_t name##_struct = { 0, sizeof(type), NULL, (nbr_table_item_t *)_##name##_mem }; \
77  nbr_table_t *name = &name##_struct \
78 
79 /** \brief Declaration of non-static neighbor tables */
80 #define NBR_TABLE_DECLARE(name) extern nbr_table_t *name
81 
82 /** \name Neighbor tables: register and loop through table elements */
83 /** @{ */
84 int nbr_table_register(nbr_table_t *table, nbr_table_callback *callback);
85 nbr_table_item_t *nbr_table_head(nbr_table_t *table);
86 nbr_table_item_t *nbr_table_next(nbr_table_t *table, nbr_table_item_t *item);
87 /** @} */
88 
89 /** \name Neighbor tables: add and get data */
90 /** @{ */
91 nbr_table_item_t *nbr_table_add_lladdr(nbr_table_t *table, const linkaddr_t *lladdr);
92 nbr_table_item_t *nbr_table_get_from_lladdr(nbr_table_t *table, const linkaddr_t *lladdr);
93 /** @} */
94 
95 /** \name Neighbor tables: set flags (unused, locked, unlocked) */
96 /** @{ */
97 int nbr_table_remove(nbr_table_t *table, nbr_table_item_t *item);
98 int nbr_table_lock(nbr_table_t *table, nbr_table_item_t *item);
99 int nbr_table_unlock(nbr_table_t *table, nbr_table_item_t *item);
100 /** @} */
101 
102 /** \name Neighbor tables: address manipulation */
103 /** @{ */
104 linkaddr_t *nbr_table_get_lladdr(nbr_table_t *table, const nbr_table_item_t *item);
105 /** @} */
106 
107 #endif /* NBR_TABLE_H_ */