2007-03-15 20:04:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, Swedish Institute of Computer Science.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2007-03-25 21:44:06 +00:00
|
|
|
* $Id: tree.c,v 1.9 2007/03/25 21:44:06 adamdunkels Exp $
|
2007-03-15 20:04:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* A brief description of what this file is.
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
|
|
|
|
#include "net/rime.h"
|
|
|
|
#include "net/rime/neighbor.h"
|
2007-03-25 12:06:39 +00:00
|
|
|
#include "net/rime/nf.h"
|
2007-03-21 23:22:42 +00:00
|
|
|
#include "net/rime/tree.h"
|
2007-03-15 20:04:30 +00:00
|
|
|
|
|
|
|
#include "dev/radio-sensor.h"
|
|
|
|
|
|
|
|
#if NETSIM
|
|
|
|
#include "ether.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2007-03-22 18:54:22 +00:00
|
|
|
#include <stddef.h>
|
2007-03-15 20:04:30 +00:00
|
|
|
|
2007-03-22 18:54:22 +00:00
|
|
|
struct adv_msg {
|
|
|
|
u8_t hopcount;
|
|
|
|
u8_t pad;
|
|
|
|
};
|
2007-03-15 20:04:30 +00:00
|
|
|
|
|
|
|
struct hdr {
|
|
|
|
rimeaddr_t originator;
|
|
|
|
u8_t originator_seqno;
|
|
|
|
u8_t hopcount;
|
|
|
|
u8_t hoplim;
|
|
|
|
u8_t retransmissions;
|
|
|
|
u8_t datalen;
|
|
|
|
u8_t data[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SINK 0
|
|
|
|
#define HOPCOUNT_MAX TREE_MAX_DEPTH
|
|
|
|
|
|
|
|
#define MAX_HOPLIM 10
|
|
|
|
|
2007-03-22 18:54:22 +00:00
|
|
|
#define MAX_INTERVAL CLOCK_SECOND * 4
|
2007-03-15 20:04:30 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2007-03-22 18:54:22 +00:00
|
|
|
update_hopcount(struct tree_conn *tc)
|
2007-03-15 20:04:30 +00:00
|
|
|
{
|
|
|
|
struct neighbor *n;
|
|
|
|
|
2007-03-22 18:54:22 +00:00
|
|
|
if(tc->hops_from_sink != SINK) {
|
2007-03-15 20:04:30 +00:00
|
|
|
n = neighbor_best();
|
|
|
|
if(n == NULL) {
|
|
|
|
/* if(hopcount != HOPCOUNT_MAX) {
|
|
|
|
printf("%d: didn't find a best neighbor, setting hopcount to max\n", node_id);
|
|
|
|
}*/
|
2007-03-22 18:54:22 +00:00
|
|
|
tc->hops_from_sink = HOPCOUNT_MAX;
|
2007-03-15 20:04:30 +00:00
|
|
|
} else {
|
2007-03-22 18:54:22 +00:00
|
|
|
if(n->hopcount + 1 != tc->hops_from_sink) {
|
|
|
|
tc->hops_from_sink = n->hopcount + 1;
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DEBUG_PRINTF("%d: new hopcount %d\n", node_id, hopcount);*/
|
|
|
|
#if NETSIM
|
|
|
|
{
|
|
|
|
char buf[8];
|
2007-03-22 18:54:22 +00:00
|
|
|
if(tc->hops_from_sink == HOPCOUNT_MAX) {
|
2007-03-15 20:04:30 +00:00
|
|
|
strcpy(buf, " ");
|
|
|
|
} else {
|
2007-03-22 18:54:22 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%d", tc->hops_from_sink);
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
ether_set_text(buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2007-03-22 18:54:22 +00:00
|
|
|
send_adv(struct tree_conn *tc)
|
2007-03-15 20:04:30 +00:00
|
|
|
{
|
2007-03-22 18:54:22 +00:00
|
|
|
struct adv_msg *hdr;
|
|
|
|
|
|
|
|
rimebuf_clear();
|
|
|
|
rimebuf_set_datalen(sizeof(struct adv_msg));
|
|
|
|
hdr = rimebuf_dataptr();
|
|
|
|
hdr->hopcount = tc->hops_from_sink;
|
|
|
|
uibc_send(&tc->uibc_conn, MAX_INTERVAL);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
adv_packet_received(struct uibc_conn *c, rimeaddr_t *from)
|
|
|
|
{
|
|
|
|
struct tree_conn *tc = (struct tree_conn *)
|
|
|
|
((char *)c - offsetof(struct tree_conn, uibc_conn));
|
|
|
|
struct adv_msg *msg = rimebuf_dataptr();
|
2007-03-15 20:04:30 +00:00
|
|
|
struct neighbor *n;
|
|
|
|
|
|
|
|
/* printf("%d: neighbor_packet_received from %d with hopcount %d\n",
|
|
|
|
node_id, from, hdr->hopcount
|
|
|
|
);*/
|
|
|
|
|
|
|
|
n = neighbor_find(from);
|
|
|
|
|
|
|
|
if(n == NULL) {
|
2007-03-22 18:54:22 +00:00
|
|
|
neighbor_add(from, msg->hopcount, radio_sensor.value(1));
|
2007-03-15 20:04:30 +00:00
|
|
|
} else {
|
2007-03-22 18:54:22 +00:00
|
|
|
neighbor_update(n, msg->hopcount, radio_sensor.value(1));
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
|
2007-03-22 18:54:22 +00:00
|
|
|
update_hopcount(tc);
|
2007-03-15 20:04:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-22 18:54:22 +00:00
|
|
|
static void
|
|
|
|
adv_packet_sent(struct uibc_conn *c)
|
|
|
|
{
|
|
|
|
struct tree_conn *tc = (struct tree_conn *)
|
|
|
|
((char *)c - offsetof(struct tree_conn, uibc_conn));
|
|
|
|
send_adv(tc);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
adv_packet_dropped(struct uibc_conn *c)
|
|
|
|
{
|
|
|
|
struct tree_conn *tc = (struct tree_conn *)
|
|
|
|
((char *)c - offsetof(struct tree_conn, uibc_conn));
|
|
|
|
send_adv(tc);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-15 20:04:30 +00:00
|
|
|
static int
|
|
|
|
node_packet_received(struct ruc_conn *c, rimeaddr_t *from, u8_t seqno)
|
|
|
|
{
|
2007-03-22 18:54:22 +00:00
|
|
|
struct tree_conn *tc = (struct tree_conn *)
|
|
|
|
((char *)c - offsetof(struct tree_conn, ruc_conn));
|
2007-03-15 20:04:30 +00:00
|
|
|
struct hdr *hdr = rimebuf_dataptr();
|
|
|
|
struct neighbor *n;
|
|
|
|
|
2007-03-22 18:54:22 +00:00
|
|
|
if(tc->hops_from_sink == SINK) {
|
|
|
|
|
2007-03-25 21:44:06 +00:00
|
|
|
rimebuf_hdrreduce(sizeof(struct hdr));
|
2007-03-22 18:54:22 +00:00
|
|
|
if(tc->cb->recv != NULL) {
|
|
|
|
tc->cb->recv(&hdr->originator, hdr->originator_seqno,
|
|
|
|
hdr->hopcount, hdr->retransmissions);
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2007-03-22 18:54:22 +00:00
|
|
|
} else if(hdr->hoplim > 1 && tc->hops_from_sink != HOPCOUNT_MAX) {
|
2007-03-21 23:22:42 +00:00
|
|
|
hdr->hoplim--;
|
2007-03-22 18:54:22 +00:00
|
|
|
/* printf("%d: packet received from %d, forwarding %d, best neighbor %p\n",
|
|
|
|
rimeaddr_node_addr.u16[0], from->u16[0], tc->forwarding, neighbor_best());*/
|
|
|
|
if(!tc->forwarding) {
|
|
|
|
tc->forwarding = 1;
|
2007-03-15 20:04:30 +00:00
|
|
|
n = neighbor_best();
|
|
|
|
if(n != NULL) {
|
|
|
|
ruc_send(c, &n->addr);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
|
2007-03-22 18:54:22 +00:00
|
|
|
/* printf("%d: still forwarding another packet, not sending ACK\n",
|
|
|
|
rimeaddr_node_addr.u16[0]);*/
|
2007-03-15 20:04:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
node_packet_sent(struct ruc_conn *c)
|
|
|
|
{
|
2007-03-22 18:54:22 +00:00
|
|
|
struct tree_conn *tc = (struct tree_conn *)
|
|
|
|
((char *)c - offsetof(struct tree_conn, ruc_conn));
|
|
|
|
|
|
|
|
tc->forwarding = 0;
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-22 18:54:22 +00:00
|
|
|
static const struct uibc_callbacks uibc_callbacks =
|
|
|
|
{adv_packet_received, adv_packet_sent, adv_packet_dropped};
|
2007-03-15 20:04:30 +00:00
|
|
|
static const struct ruc_callbacks ruc_callbacks = {node_packet_received,
|
|
|
|
node_packet_sent};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-22 18:54:22 +00:00
|
|
|
tree_open(struct tree_conn *tc, u16_t channels,
|
|
|
|
const struct tree_callbacks *cb)
|
2007-03-15 20:04:30 +00:00
|
|
|
{
|
2007-03-22 18:54:22 +00:00
|
|
|
uibc_open(&tc->uibc_conn, channels, &uibc_callbacks);
|
|
|
|
ruc_open(&tc->ruc_conn, channels + 1, &ruc_callbacks);
|
|
|
|
tc->hops_from_sink = HOPCOUNT_MAX;
|
|
|
|
/* rimebuf_clear();
|
2007-03-15 20:04:30 +00:00
|
|
|
rimebuf_reference(&tc.hello, sizeof(tc.hello));
|
2007-03-22 18:54:22 +00:00
|
|
|
sibc_send_stubborn(&sibc_conn, CLOCK_SECOND * 8);*/
|
|
|
|
tc->cb = cb;
|
|
|
|
send_adv(tc);
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-19 22:10:16 +00:00
|
|
|
void
|
2007-03-22 18:54:22 +00:00
|
|
|
tree_close(struct tree_conn *tc)
|
2007-03-19 22:10:16 +00:00
|
|
|
{
|
2007-03-22 18:54:22 +00:00
|
|
|
uibc_close(&tc->uibc_conn);
|
|
|
|
ruc_close(&tc->ruc_conn);
|
2007-03-19 22:10:16 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-15 20:04:30 +00:00
|
|
|
void
|
2007-03-22 18:54:22 +00:00
|
|
|
tree_set_sink(struct tree_conn *tc, int should_be_sink)
|
2007-03-15 20:04:30 +00:00
|
|
|
{
|
|
|
|
if(should_be_sink) {
|
2007-03-22 18:54:22 +00:00
|
|
|
tc->hops_from_sink = SINK;
|
2007-03-15 20:04:30 +00:00
|
|
|
} else {
|
2007-03-22 18:54:22 +00:00
|
|
|
tc->hops_from_sink = HOPCOUNT_MAX;
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
2007-03-22 18:54:22 +00:00
|
|
|
update_hopcount(tc);
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-22 18:54:22 +00:00
|
|
|
tree_send(struct tree_conn *tc)
|
2007-03-15 20:04:30 +00:00
|
|
|
{
|
|
|
|
struct neighbor *n;
|
|
|
|
struct hdr *hdr;
|
|
|
|
|
2007-03-23 10:46:35 +00:00
|
|
|
if(rimebuf_hdralloc(sizeof(struct hdr))) {
|
2007-03-15 20:04:30 +00:00
|
|
|
hdr = rimebuf_hdrptr();
|
2007-03-22 18:54:22 +00:00
|
|
|
hdr->originator_seqno = tc->seqno++;
|
2007-03-15 20:04:30 +00:00
|
|
|
rimeaddr_copy(&hdr->originator, &rimeaddr_node_addr);
|
2007-03-22 18:54:22 +00:00
|
|
|
hdr->hopcount = tc->hops_from_sink;
|
2007-03-15 20:04:30 +00:00
|
|
|
hdr->hoplim = MAX_HOPLIM;
|
|
|
|
hdr->retransmissions = 0;
|
|
|
|
hdr->datalen = 0;
|
|
|
|
n = neighbor_best();
|
|
|
|
if(n != NULL) {
|
2007-03-22 18:54:22 +00:00
|
|
|
/* printf("Sending to best neighbor\n");*/
|
|
|
|
ruc_send(&tc->ruc_conn, &n->addr);
|
2007-03-15 20:04:30 +00:00
|
|
|
} else {
|
2007-03-22 18:54:22 +00:00
|
|
|
/* printf("Didn't find any neighbor\n");*/
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2007-03-22 18:54:22 +00:00
|
|
|
tree_depth(struct tree_conn *tc)
|
2007-03-15 20:04:30 +00:00
|
|
|
{
|
2007-03-22 18:54:22 +00:00
|
|
|
return tc->hops_from_sink;
|
2007-03-15 20:04:30 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|