Contiki 2.5
neighbor-attr.h
1 /*
2  * Copyright (c) 2010, Vrije Universiteit Brussel
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *
30  * Author: Joris Borms <joris.borms@vub.ac.be>
31  *
32  */
33 #ifndef NEIGHBORATTR_H_
34 #define NEIGHBORATTR_H_
35 
36 #include "net/rime.h"
37 
38 /**
39  * define how many neighbors you can store
40  */
41 #ifdef NEIGHBOR_CONF_MAX_NEIGHBORS
42 #define NEIGHBOR_ATTR_MAX_NEIGHBORS NEIGHBOR_CONF_MAX_NEIGHBORS
43 #else /* NEIGHBOR_CONF_MAX_NEIGHBORS */
44 #define NEIGHBOR_ATTR_MAX_NEIGHBORS 12
45 #endif /* NEIGHBOR_CONF_MAX_NEIGHBORS */
46 
47 /**
48  * \brief properties of a single neighbor
49  */
50 struct neighbor_addr {
51  struct neighbor_addr *next;
52  rimeaddr_t addr;
53  uint16_t time;
54  uint16_t index;
55 };
56 
57 /**
58  * \brief properties that define a neighbor attribute
59  */
60 struct neighbor_attr {
61  struct neighbor_attr *next;
62  uint16_t size;
63  void *default_value;
64  void *data;
65 };
66 
67 /**
68  * \brief Define space for additional parameters in neighbor table entries.
69  * \param type The type of the attribute.
70  * \param name The name of the attribute.
71  * \param def A ptr to the default value for this attribute. If NULL, attribute will
72  * be filled with zeroes by default.
73  *
74  * The attribute 'name' should be registered with 'neighbor_attr_register'
75  * during initialization.
76  */
77 #define NEIGHBOR_ATTRIBUTE(type, name, default_value_ptr) \
78  static type _##name##_mem[NEIGHBOR_ATTR_MAX_NEIGHBORS]; \
79  static struct neighbor_attr name = \
80  {NULL, sizeof(type), default_value_ptr, (void*)_##name##_mem} ; \
81 
82 /** Same as NEIGHBOR_ATTRIBUTE, only the attr is not declared static
83  * this way you can say <tt>extern struct neighbor_attr name</tt> in header to declare
84  * a global neighbor attribute
85  */
86 #define NEIGHBOR_ATTRIBUTE_NONSTATIC(type, name, default_value_ptr) \
87  static type _##name##_mem[MAX_NEIGHBORS]; \
88  struct neighbor_attr name = \
89  {NULL, sizeof(type), default_value_ptr, (void*)_##name##_mem} ; \
90 
91 /**
92  * \brief register a neighbor attribute
93  * \retval non-zero if successful, zero if not
94  */
95 int neighbor_attr_register(struct neighbor_attr *);
96 
97 /**
98  * \retval head of neighbor list, useful for iterating over all neighbors
99  */
100 struct neighbor_addr *neighbor_attr_list_neighbors(void);
101 
102 /**
103  * \brief Check if a neighbor is already added to the neighbor table
104  * \retval non-zero if present, zero if not
105  */
106 int neighbor_attr_has_neighbor(const rimeaddr_t * addr);
107 
108 /**
109  * \brief Add a neighbor entry to neighbor table
110  * \retval -1 if unsuccessful, 0 if the neighbor was already
111  * in the table, and 1 if successful
112  */
113 int neighbor_attr_add_neighbor(const rimeaddr_t * addr);
114 
115 /**
116  * \brief Remove a neighbor entry to neighbor table
117  * \retval -1 if unsuccessful, 0 if the neighbor was removed
118  */
119 int neighbor_attr_remove_neighbor(const rimeaddr_t * addr);
120 
121 /**
122  * \brief Get pointer to neighbor table data specified by id
123  * \param requested attribute
124  * \param addr requested neighbor
125  * \retval pointer to data, NULL if neighbor was not found
126  *
127  * Searches neighbor table for addr and returns pointer to data section
128  * specified by attribute type and addr.
129  * This pointer should not be saved, as it may point to data from another
130  * neighbor in the future if neighbors get removed/added over time.
131  */
132 void *neighbor_attr_get_data(struct neighbor_attr *, const rimeaddr_t * addr);
133 
134 /**
135  * \brief Copy data to neighbor table
136  * \retval non-zero if successful, zero if not
137  *
138  * Copies data to specific part of the neighbor table, specified by
139  * neighbor and attribute type, and resets timeout for that neighbor.
140  * If neighbor was not found, this will add a new neighbor to the table.
141  */
142 int neighbor_attr_set_data(struct neighbor_attr *, const rimeaddr_t * addr,
143  void *data);
144 
145 /**
146  * \brief Set global lifetime of neighbor entries.
147  * \param Lifetime in seconds. If 0, entries will not time out
148  */
149 void neighbor_attr_set_timeout(uint16_t);
150 
151 /**
152  * \brief get global lifetime of neighbor entries. If 0, entries will not time out
153  */
154 uint16_t neighbor_attr_get_timeout(void);
155 
156 /**
157  * \brief reset timeout of a neighbor to prevent it from being removed
158  */
159 void neighbor_attr_tick(const rimeaddr_t *);
160 
161 #endif /* NEIGHBORATTR_H_ */