2010-03-12 13:19:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Vrije Universiteit Brussel
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Author: Joris Borms <joris.borms@vub.ac.be>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
|
|
|
|
#include "lib/memb.h"
|
|
|
|
#include "lib/list.h"
|
2010-03-12 15:23:57 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2010-03-12 13:19:45 +00:00
|
|
|
|
2010-03-12 13:23:50 +00:00
|
|
|
#include "net/neighbor-attr.h"
|
2010-03-12 13:19:45 +00:00
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static uint16_t timeout = 0;
|
|
|
|
|
2011-02-13 20:34:59 +00:00
|
|
|
MEMB(neighbor_addr_mem, struct neighbor_addr, NEIGHBOR_ATTR_MAX_NEIGHBORS);
|
2010-03-12 13:19:45 +00:00
|
|
|
|
|
|
|
LIST(neighbor_addrs);
|
|
|
|
LIST(neighbor_attrs);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static struct neighbor_addr *
|
|
|
|
neighbor_addr_get(const rimeaddr_t *addr)
|
|
|
|
{
|
2010-06-11 12:32:48 +00:00
|
|
|
struct neighbor_addr *item;
|
|
|
|
|
2010-03-12 13:19:45 +00:00
|
|
|
/* check if addr is derived from table, inside memb */
|
2010-06-11 12:32:48 +00:00
|
|
|
if(memb_inmemb(&neighbor_addr_mem, (char *)addr)) {
|
2010-03-12 13:19:45 +00:00
|
|
|
return (struct neighbor_addr *)
|
2010-06-11 12:32:48 +00:00
|
|
|
(((char *)addr) - offsetof(struct neighbor_addr, addr));
|
2010-03-12 13:19:45 +00:00
|
|
|
}
|
|
|
|
|
2010-06-11 12:32:48 +00:00
|
|
|
item = list_head(neighbor_addrs);
|
2010-03-12 13:19:45 +00:00
|
|
|
while(item != NULL) {
|
|
|
|
if(rimeaddr_cmp(addr, &item->addr)) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct neighbor_addr *
|
|
|
|
neighbor_attr_list_neighbors(void)
|
|
|
|
{
|
|
|
|
return list_head(neighbor_addrs);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-06-11 12:32:48 +00:00
|
|
|
static void
|
|
|
|
set_attr(struct neighbor_attr *attr, uint16_t index)
|
|
|
|
{
|
|
|
|
if(attr->default_value != NULL) {
|
|
|
|
memcpy((char *)attr->data + index * attr->size,
|
|
|
|
attr->default_value, attr->size);
|
|
|
|
} else {
|
|
|
|
/* fill with zeroes */
|
|
|
|
memset((char *)attr->data + index * attr->size, 0, attr->size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-03-12 13:19:45 +00:00
|
|
|
int
|
|
|
|
neighbor_attr_register(struct neighbor_attr *def)
|
|
|
|
{
|
2010-06-11 12:32:48 +00:00
|
|
|
struct neighbor_addr *addr;
|
|
|
|
|
2010-03-12 13:19:45 +00:00
|
|
|
list_push(neighbor_attrs, def);
|
2010-06-11 12:32:48 +00:00
|
|
|
|
2010-03-12 13:19:45 +00:00
|
|
|
/* set default values for already existing neighbors */
|
2010-06-11 12:32:48 +00:00
|
|
|
for(addr = list_head(neighbor_addrs); addr != NULL; addr = addr->next) {
|
|
|
|
set_attr(def, addr->index);
|
2010-03-12 13:19:45 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
neighbor_attr_has_neighbor(const rimeaddr_t *addr)
|
|
|
|
{
|
|
|
|
return neighbor_addr_get(addr) != NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
neighbor_attr_add_neighbor(const rimeaddr_t *addr)
|
|
|
|
{
|
2010-06-11 12:32:48 +00:00
|
|
|
struct neighbor_attr *def;
|
|
|
|
struct neighbor_addr *item;
|
|
|
|
struct neighbor_addr *ptr;
|
|
|
|
uint16_t i;
|
|
|
|
|
2010-03-12 13:19:45 +00:00
|
|
|
if(neighbor_attr_has_neighbor(addr)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-11 12:32:48 +00:00
|
|
|
item = memb_alloc(&neighbor_addr_mem);
|
2010-03-12 13:19:45 +00:00
|
|
|
if(item == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_push(neighbor_addrs, item);
|
|
|
|
|
|
|
|
item->time = 0;
|
|
|
|
rimeaddr_copy(&item->addr, addr);
|
|
|
|
|
|
|
|
/* look up index and set default values */
|
2010-06-11 12:32:48 +00:00
|
|
|
ptr = neighbor_addr_mem.mem;
|
2010-03-12 13:19:45 +00:00
|
|
|
for(i = 0; i < neighbor_addr_mem.num; ++i) {
|
|
|
|
if(&ptr[i] == item) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item->index = i;
|
|
|
|
|
2010-06-11 12:32:48 +00:00
|
|
|
for(def = list_head(neighbor_attrs); def != NULL; def = def->next) {
|
|
|
|
set_attr(def, i);
|
2010-03-12 13:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-03-17 15:00:52 +00:00
|
|
|
int
|
|
|
|
neighbor_attr_remove_neighbor(const rimeaddr_t *addr)
|
|
|
|
{
|
2010-06-11 12:32:48 +00:00
|
|
|
struct neighbor_addr *item = neighbor_addr_get(addr);
|
2010-03-17 15:00:52 +00:00
|
|
|
|
2010-06-11 12:32:48 +00:00
|
|
|
if(item != NULL) {
|
|
|
|
list_remove(neighbor_addrs, item);
|
|
|
|
memb_free(&neighbor_addr_mem, item);
|
|
|
|
return 0;
|
2010-03-17 15:00:52 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-03-12 13:19:45 +00:00
|
|
|
void *
|
|
|
|
neighbor_attr_get_data(struct neighbor_attr *def, const rimeaddr_t *addr)
|
|
|
|
{
|
|
|
|
struct neighbor_addr *attr = neighbor_addr_get(addr);
|
|
|
|
|
|
|
|
if(attr != NULL) {
|
2010-06-11 12:32:48 +00:00
|
|
|
return (char *)def->data + attr->index * def->size;
|
2010-03-12 13:19:45 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
neighbor_attr_set_data(struct neighbor_attr *def, const rimeaddr_t *addr,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct neighbor_addr *attr = neighbor_addr_get(addr);
|
|
|
|
|
|
|
|
if(attr == NULL) {
|
|
|
|
if(neighbor_attr_add_neighbor(addr)) {
|
|
|
|
attr = neighbor_addr_get(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(attr != NULL) {
|
|
|
|
attr->time = 0;
|
2010-06-11 12:32:48 +00:00
|
|
|
memcpy((char *)def->data + attr->index * def->size, data, def->size);
|
2010-03-12 13:19:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_attr_tick(const rimeaddr_t * addr)
|
|
|
|
{
|
|
|
|
struct neighbor_addr *attr = neighbor_addr_get(addr);
|
|
|
|
|
|
|
|
if(attr != NULL) {
|
|
|
|
attr->time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uint16_t
|
|
|
|
neighbor_attr_get_timeout(void)
|
|
|
|
{
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static struct ctimer ct;
|
|
|
|
|
|
|
|
#define TIMEOUT_SECONDS 5
|
|
|
|
static void
|
|
|
|
timeout_check(void *ptr)
|
|
|
|
{
|
|
|
|
if(timeout > 0) {
|
|
|
|
struct neighbor_addr *item = neighbor_attr_list_neighbors();
|
|
|
|
|
|
|
|
while(item != NULL) {
|
|
|
|
item->time += TIMEOUT_SECONDS;
|
|
|
|
if(item->time >= timeout) {
|
|
|
|
struct neighbor_addr *next_item = item->next;
|
|
|
|
|
|
|
|
list_remove(neighbor_addrs, item);
|
2010-06-11 12:32:48 +00:00
|
|
|
memb_free(&neighbor_addr_mem, item);
|
2010-03-12 13:19:45 +00:00
|
|
|
item = next_item;
|
|
|
|
} else {
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctimer_set(&ct, TIMEOUT_SECONDS * CLOCK_SECOND, timeout_check, ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_attr_set_timeout(uint16_t time)
|
|
|
|
{
|
|
|
|
if(timeout == 0 && time > 0) {
|
|
|
|
ctimer_set(&ct, TIMEOUT_SECONDS * CLOCK_SECOND, timeout_check, NULL);
|
|
|
|
} else if(timeout > 0 && time == 0) {
|
|
|
|
ctimer_stop(&ct);
|
|
|
|
}
|
|
|
|
timeout = time;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|