2 * Copyright (c) 2010, Vrije Universiteit Brussel
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * Author: Joris Borms <joris.borms@vub.ac.be>
41 #include "net/neighbor-attr.h"
46 #define PRINTF(...) printf(__VA_ARGS__)
51 static uint16_t timeout
= 0;
53 MEMB(neighbor_addr_mem
, struct neighbor_addr
, MAX_NEIGHBORS
);
57 /*---------------------------------------------------------------------------*/
58 static struct neighbor_addr
*
59 neighbor_addr_get(const rimeaddr_t
*addr
)
61 struct neighbor_addr
*item
;
63 /* check if addr is derived from table, inside memb */
64 if(memb_inmemb(&neighbor_addr_mem
, (char *)addr
)) {
65 return (struct neighbor_addr
*)
66 (((char *)addr
) - offsetof(struct neighbor_addr
, addr
));
69 item
= list_head(neighbor_addrs
);
71 if(rimeaddr_cmp(addr
, &item
->addr
)) {
78 /*---------------------------------------------------------------------------*/
79 struct neighbor_addr
*
80 neighbor_attr_list_neighbors(void)
82 return list_head(neighbor_addrs
);
84 /*---------------------------------------------------------------------------*/
86 set_attr(struct neighbor_attr
*attr
, uint16_t index
)
88 if(attr
->default_value
!= NULL
) {
89 memcpy((char *)attr
->data
+ index
* attr
->size
,
90 attr
->default_value
, attr
->size
);
92 /* fill with zeroes */
93 memset((char *)attr
->data
+ index
* attr
->size
, 0, attr
->size
);
96 /*---------------------------------------------------------------------------*/
98 neighbor_attr_register(struct neighbor_attr
*def
)
100 struct neighbor_addr
*addr
;
102 list_push(neighbor_attrs
, def
);
104 /* set default values for already existing neighbors */
105 for(addr
= list_head(neighbor_addrs
); addr
!= NULL
; addr
= addr
->next
) {
106 set_attr(def
, addr
->index
);
110 /*---------------------------------------------------------------------------*/
112 neighbor_attr_has_neighbor(const rimeaddr_t
*addr
)
114 return neighbor_addr_get(addr
) != NULL
;
116 /*---------------------------------------------------------------------------*/
118 neighbor_attr_add_neighbor(const rimeaddr_t
*addr
)
120 struct neighbor_attr
*def
;
121 struct neighbor_addr
*item
;
122 struct neighbor_addr
*ptr
;
125 if(neighbor_attr_has_neighbor(addr
)) {
129 item
= memb_alloc(&neighbor_addr_mem
);
134 list_push(neighbor_addrs
, item
);
137 rimeaddr_copy(&item
->addr
, addr
);
139 /* look up index and set default values */
140 ptr
= neighbor_addr_mem
.mem
;
141 for(i
= 0; i
< neighbor_addr_mem
.num
; ++i
) {
142 if(&ptr
[i
] == item
) {
149 for(def
= list_head(neighbor_attrs
); def
!= NULL
; def
= def
->next
) {
155 /*---------------------------------------------------------------------------*/
157 neighbor_attr_remove_neighbor(const rimeaddr_t
*addr
)
159 struct neighbor_addr
*item
= neighbor_addr_get(addr
);
162 list_remove(neighbor_addrs
, item
);
163 memb_free(&neighbor_addr_mem
, item
);
168 /*---------------------------------------------------------------------------*/
170 neighbor_attr_get_data(struct neighbor_attr
*def
, const rimeaddr_t
*addr
)
172 struct neighbor_addr
*attr
= neighbor_addr_get(addr
);
175 return (char *)def
->data
+ attr
->index
* def
->size
;
179 /*---------------------------------------------------------------------------*/
181 neighbor_attr_set_data(struct neighbor_attr
*def
, const rimeaddr_t
*addr
,
184 struct neighbor_addr
*attr
= neighbor_addr_get(addr
);
187 if(neighbor_attr_add_neighbor(addr
)) {
188 attr
= neighbor_addr_get(addr
);
193 memcpy((char *)def
->data
+ attr
->index
* def
->size
, data
, def
->size
);
198 /*---------------------------------------------------------------------------*/
200 neighbor_attr_tick(const rimeaddr_t
* addr
)
202 struct neighbor_addr
*attr
= neighbor_addr_get(addr
);
208 /*---------------------------------------------------------------------------*/
210 neighbor_attr_get_timeout(void)
214 /*---------------------------------------------------------------------------*/
215 static struct ctimer ct
;
217 #define TIMEOUT_SECONDS 5
219 timeout_check(void *ptr
)
222 struct neighbor_addr
*item
= neighbor_attr_list_neighbors();
224 while(item
!= NULL
) {
225 item
->time
+= TIMEOUT_SECONDS
;
226 if(item
->time
>= timeout
) {
227 struct neighbor_addr
*next_item
= item
->next
;
229 list_remove(neighbor_addrs
, item
);
230 memb_free(&neighbor_addr_mem
, item
);
236 ctimer_set(&ct
, TIMEOUT_SECONDS
* CLOCK_SECOND
, timeout_check
, ptr
);
239 /*---------------------------------------------------------------------------*/
241 neighbor_attr_set_timeout(uint16_t time
)
243 if(timeout
== 0 && time
> 0) {
244 ctimer_set(&ct
, TIMEOUT_SECONDS
* CLOCK_SECOND
, timeout_check
, NULL
);
245 } else if(timeout
> 0 && time
== 0) {
250 /*---------------------------------------------------------------------------*/