Added support for multiple simulteaneous tree connections, removed sibc connection in favour of a uibc connection

This commit is contained in:
adamdunkels 2007-03-22 18:54:22 +00:00
parent 3134789a94
commit 095952f97d
2 changed files with 110 additions and 66 deletions

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: tree.c,v 1.5 2007/03/21 23:22:42 adamdunkels Exp $
* $Id: tree.c,v 1.6 2007/03/22 18:54:22 adamdunkels Exp $
*/
/**
@ -52,9 +52,12 @@
#include <string.h>
#include <stdio.h>
#include <stddef.h>
static struct ruc_conn ruc_conn;
static struct sibc_conn sibc_conn;
struct adv_msg {
u8_t hopcount;
u8_t pad;
};
struct hdr {
rimeaddr_t originator;
@ -71,31 +74,24 @@ struct hdr {
#define MAX_HOPLIM 10
struct tree_conn {
int forwarding;
struct hdr hello;
u8_t seqno;
const struct tree_callbacks *cb;
};
static struct tree_conn tc;
#define MAX_INTERVAL CLOCK_SECOND * 4
/*---------------------------------------------------------------------------*/
static void
update_hopcount(void)
update_hopcount(struct tree_conn *tc)
{
struct neighbor *n;
if(tc.hello.hopcount != SINK) {
if(tc->hops_from_sink != SINK) {
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);
}*/
tc.hello.hopcount = HOPCOUNT_MAX;
tc->hops_from_sink = HOPCOUNT_MAX;
} else {
if(n->hopcount + 1 != tc.hello.hopcount) {
tc.hello.hopcount = n->hopcount + 1;
if(n->hopcount + 1 != tc->hops_from_sink) {
tc->hops_from_sink = n->hopcount + 1;
}
}
}
@ -104,10 +100,10 @@ update_hopcount(void)
#if NETSIM
{
char buf[8];
if(tc.hello.hopcount == HOPCOUNT_MAX) {
if(tc->hops_from_sink == HOPCOUNT_MAX) {
strcpy(buf, " ");
} else {
snprintf(buf, sizeof(buf), "%d", tc.hello.hopcount);
snprintf(buf, sizeof(buf), "%d", tc->hops_from_sink);
}
ether_set_text(buf);
}
@ -115,9 +111,23 @@ update_hopcount(void)
}
/*---------------------------------------------------------------------------*/
static void
neighbor_packet_received(struct sibc_conn *c, rimeaddr_t *from)
send_adv(struct tree_conn *tc)
{
struct hdr *hdr = rimebuf_dataptr();
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();
struct neighbor *n;
/* printf("%d: neighbor_packet_received from %d with hopcount %d\n",
@ -127,36 +137,52 @@ neighbor_packet_received(struct sibc_conn *c, rimeaddr_t *from)
n = neighbor_find(from);
if(n == NULL) {
neighbor_add(from, hdr->hopcount, radio_sensor.value(1));
neighbor_add(from, msg->hopcount, radio_sensor.value(1));
} else {
neighbor_update(n, hdr->hopcount, radio_sensor.value(1));
neighbor_update(n, msg->hopcount, radio_sensor.value(1));
}
update_hopcount();
update_hopcount(tc);
}
/*---------------------------------------------------------------------------*/
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);
}
/*---------------------------------------------------------------------------*/
static int
node_packet_received(struct ruc_conn *c, rimeaddr_t *from, u8_t seqno)
{
struct tree_conn *tc = (struct tree_conn *)
((char *)c - offsetof(struct tree_conn, ruc_conn));
struct hdr *hdr = rimebuf_dataptr();
struct neighbor *n;
if(tc.hello.hopcount == SINK) {
printf("Sink packet received\n");
if(tc.cb->recv != NULL) {
/* receiver(hdr->data, hdr->datalen,
&hdr->originator, hdr->originator_seqno,
from, MAX_HOPLIM - hdr->hoplim,
hdr->retransmissions);*/
if(tc->hops_from_sink == SINK) {
if(tc->cb->recv != NULL) {
tc->cb->recv(&hdr->originator, hdr->originator_seqno,
hdr->hopcount, hdr->retransmissions);
}
return 1;
} else if(hdr->hoplim > 1 && tc.hello.hopcount != HOPCOUNT_MAX) {
} else if(hdr->hoplim > 1 && tc->hops_from_sink != HOPCOUNT_MAX) {
hdr->hoplim--;
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;
/* 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;
n = neighbor_best();
if(n != NULL) {
ruc_send(c, &n->addr);
@ -164,8 +190,8 @@ node_packet_received(struct ruc_conn *c, rimeaddr_t *from, u8_t seqno)
return 1;
} else {
printf("%d: still forwarding another packet, not sending ACK\n",
rimeaddr_node_addr.u16[0]);
/* printf("%d: still forwarding another packet, not sending ACK\n",
rimeaddr_node_addr.u16[0]);*/
return 0;
}
}
@ -175,69 +201,76 @@ node_packet_received(struct ruc_conn *c, rimeaddr_t *from, u8_t seqno)
static void
node_packet_sent(struct ruc_conn *c)
{
tc.forwarding = 0;
struct tree_conn *tc = (struct tree_conn *)
((char *)c - offsetof(struct tree_conn, ruc_conn));
tc->forwarding = 0;
}
/*---------------------------------------------------------------------------*/
static const struct sibc_callbacks sibc_callbacks = {neighbor_packet_received, NULL};
static const struct uibc_callbacks uibc_callbacks =
{adv_packet_received, adv_packet_sent, adv_packet_dropped};
static const struct ruc_callbacks ruc_callbacks = {node_packet_received,
node_packet_sent};
/*---------------------------------------------------------------------------*/
void
tree_open(const struct tree_callbacks *cb)
tree_open(struct tree_conn *tc, u16_t channels,
const struct tree_callbacks *cb)
{
sibc_open(&sibc_conn, CHANNEL_TREE_META, &sibc_callbacks);
ruc_open(&ruc_conn, CHANNEL_TREE_DATA, &ruc_callbacks);
tc.hello.hopcount = HOPCOUNT_MAX;
rimebuf_clear();
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();
rimebuf_reference(&tc.hello, sizeof(tc.hello));
sibc_send_stubborn(&sibc_conn, CLOCK_SECOND * 8);
tc.cb = cb;
sibc_send_stubborn(&sibc_conn, CLOCK_SECOND * 8);*/
tc->cb = cb;
send_adv(tc);
}
/*---------------------------------------------------------------------------*/
void
tree_close(void)
tree_close(struct tree_conn *tc)
{
sibc_close(&sibc_conn);
ruc_close(&ruc_conn);
uibc_close(&tc->uibc_conn);
ruc_close(&tc->ruc_conn);
}
/*---------------------------------------------------------------------------*/
void
tree_set_sink(int should_be_sink)
tree_set_sink(struct tree_conn *tc, int should_be_sink)
{
if(should_be_sink) {
tc.hello.hopcount = SINK;
tc->hops_from_sink = SINK;
} else {
tc.hello.hopcount = HOPCOUNT_MAX;
tc->hops_from_sink = HOPCOUNT_MAX;
}
update_hopcount(tc);
}
/*---------------------------------------------------------------------------*/
void
tree_send(void)
tree_send(struct tree_conn *tc)
{
struct neighbor *n;
struct hdr *hdr;
if(rimebuf_hdrextend(sizeof(struct hdr))) {
hdr = rimebuf_hdrptr();
hdr->originator_seqno = tc.seqno++;
hdr->originator_seqno = tc->seqno++;
rimeaddr_copy(&hdr->originator, &rimeaddr_node_addr);
hdr->hopcount = tc.hello.hopcount;
hdr->hopcount = tc->hops_from_sink;
hdr->hoplim = MAX_HOPLIM;
hdr->retransmissions = 0;
hdr->datalen = 0;
n = neighbor_best();
if(n != NULL) {
printf("Sending to best neighbor\n");
ruc_send(&ruc_conn, &n->addr);
/* printf("Sending to best neighbor\n");*/
ruc_send(&tc->ruc_conn, &n->addr);
} else {
printf("Didn't find any neighbor\n");
/* printf("Didn't find any neighbor\n");*/
}
}
}
/*---------------------------------------------------------------------------*/
int
tree_depth(void)
tree_depth(struct tree_conn *tc)
{
return tc.hello.hopcount;
return tc->hops_from_sink;
}
/*---------------------------------------------------------------------------*/

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: tree.h,v 1.3 2007/03/19 22:10:17 adamdunkels Exp $
* $Id: tree.h,v 1.4 2007/03/22 18:54:22 adamdunkels Exp $
*/
/**
@ -46,13 +46,24 @@ struct tree_callbacks {
u8_t hops, u8_t retransmissions);
};
void tree_open(const struct tree_callbacks *callbacks);
void tree_close(void);
struct tree_conn {
struct uibc_conn uibc_conn;
struct ruc_conn ruc_conn;
u8_t forwarding;
u8_t hops_from_sink;
u8_t seqno;
const struct tree_callbacks *cb;
};
void tree_send(void);
void tree_open(struct tree_conn *c, u16_t channels,
const struct tree_callbacks *callbacks);
void tree_close(struct tree_conn *c);
void tree_set_sink(int should_be_sink);
int tree_depth(void);
void tree_send(struct tree_conn *c);
void tree_set_sink(struct tree_conn *c, int should_be_sink);
int tree_depth(struct tree_conn *c);
#define TREE_MAX_DEPTH 63