Rewrote the neighbor discovery code so that messages are suppressed if a lower rtmetric is received. This reduces the overall number of broadcast packets.

This commit is contained in:
adamdunkels 2008-07-09 09:33:58 +00:00
parent a40791e6c4
commit 9a4e5dfabf
2 changed files with 29 additions and 29 deletions

View File

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: neighbor-discovery.c,v 1.8 2008/07/03 22:02:10 adamdunkels Exp $ * $Id: neighbor-discovery.c,v 1.9 2008/07/09 09:33:58 adamdunkels Exp $
*/ */
/** /**
@ -84,16 +84,16 @@ struct adv_msg {
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
send_adv(struct neighbor_discovery_conn *c, clock_time_t interval) send_adv(void *ptr)
{ {
struct neighbor_discovery_conn *c = ptr;
struct adv_msg *hdr; struct adv_msg *hdr;
rimebuf_clear(); rimebuf_clear();
rimebuf_set_datalen(sizeof(struct adv_msg)); rimebuf_set_datalen(sizeof(struct adv_msg));
hdr = rimebuf_dataptr(); hdr = rimebuf_dataptr();
hdr->val = c->val; hdr->val = c->val;
/* ibc_send(&c->c);*/ broadcast_send(&c->c);
ipolite_send(&c->c, interval, sizeof(struct adv_msg));
if(c->u->sent) { if(c->u->sent) {
c->u->sent(c); c->u->sent(c);
} }
@ -103,17 +103,21 @@ send_adv(struct neighbor_discovery_conn *c, clock_time_t interval)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
/*adv_packet_received(struct ibc_conn *ibc, rimeaddr_t *from)*/ adv_packet_received(struct broadcast_conn *ibc, rimeaddr_t *from)
adv_packet_received(struct ipolite_conn *ibc, rimeaddr_t *from)
{ {
struct neighbor_discovery_conn *c = (struct neighbor_discovery_conn *)ibc; struct neighbor_discovery_conn *c = (struct neighbor_discovery_conn *)ibc;
struct adv_msg *msg = rimebuf_dataptr(); struct adv_msg *msg = rimebuf_dataptr();
/* struct neighbor *n; */
PRINTF("%d.%d: adv_packet_received from %d.%d with val %d\n", PRINTF("%d.%d: adv_packet_received from %d.%d with val %d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
from->u8[0], from->u8[1], msg->val); from->u8[0], from->u8[1], msg->val);
/* If we receive an announcement with a lower value than ours, we
cancel our own announcement. */
if(msg->val < c->val) {
ctimer_stop(&c->t);
}
if(c->u->recv) { if(c->u->recv) {
c->u->recv(c, from, msg->val); c->u->recv(c, from, msg->val);
} }
@ -124,18 +128,16 @@ send_timer(void *ptr)
{ {
struct neighbor_discovery_conn *tc = ptr; struct neighbor_discovery_conn *tc = ptr;
send_adv(tc, tc->max_interval); /* send_adv(tc, tc->max_interval);*/
/* ctimer_set(&tc->t,
MIN_INTERVAL + random_rand() % (MAX_INTERVAL - MIN_INTERVAL),
send_timer, tc);*/
ctimer_set(&tc->t, ctimer_set(&tc->t,
tc->max_interval / 2 + random_rand() % (tc->max_interval / 2),
send_timer, tc);
ctimer_set(&tc->interval_timer,
tc->max_interval, tc->max_interval,
send_timer, tc); send_timer, tc);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/*static const struct ibc_callbacks ibc_callbacks = static CC_CONST_FUNCTION struct broadcast_callbacks broadcast_callbacks =
{adv_packet_received};*/
CC_CONST_FUNCTION static struct ipolite_callbacks ipolite_callbacks =
{adv_packet_received}; {adv_packet_received};
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
@ -145,34 +147,34 @@ neighbor_discovery_open(struct neighbor_discovery_conn *c, uint16_t channel,
clock_time_t max, clock_time_t max,
const struct neighbor_discovery_callbacks *cb) const struct neighbor_discovery_callbacks *cb)
{ {
/* ibc_open(&c->c, channel, &ibc_callbacks);*/ broadcast_open(&c->c, channel, &broadcast_callbacks);
ipolite_open(&c->c, channel, &ipolite_callbacks);
c->u = cb; c->u = cb;
c->initial_interval = initial; c->initial_interval = initial;
c->min_interval = min; c->min_interval = min;
c->max_interval = max; c->max_interval = max;
ctimer_set(&c->interval_timer, max, send_timer, c);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
neighbor_discovery_close(struct neighbor_discovery_conn *c) neighbor_discovery_close(struct neighbor_discovery_conn *c)
{ {
/* ibc_close(&c->c);*/ broadcast_close(&c->c);
ctimer_stop(&c->t); ctimer_stop(&c->t);
ipolite_close(&c->c);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
neighbor_discovery_start(struct neighbor_discovery_conn *c, uint16_t val) neighbor_discovery_start(struct neighbor_discovery_conn *c, uint16_t val)
{ {
clock_time_t interval;
if(val < c->val) { if(val < c->val) {
c->val = val; interval = c->initial_interval;
send_adv(c, c->initial_interval);
ctimer_set(&c->t, c->initial_interval, send_timer, c);
} else { } else {
c->val = val; interval = c->min_interval;
send_adv(c, c->min_interval);
ctimer_set(&c->t, c->min_interval, send_timer, c);
} }
c->val = val;
ctimer_set(&c->t, interval / 2 + random_rand() % (interval / 2),
send_adv, c);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/** @} */ /** @} */

View File

@ -47,7 +47,7 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* $Id: neighbor-discovery.h,v 1.5 2008/06/26 11:19:22 adamdunkels Exp $ * $Id: neighbor-discovery.h,v 1.6 2008/07/09 09:33:58 adamdunkels Exp $
*/ */
/** /**
@ -61,7 +61,6 @@
#define __NEIGHBOR_DISCOVERY_H__ #define __NEIGHBOR_DISCOVERY_H__
#include "net/rime/broadcast.h" #include "net/rime/broadcast.h"
#include "net/rime/ipolite.h"
struct neighbor_discovery_conn; struct neighbor_discovery_conn;
@ -72,10 +71,9 @@ struct neighbor_discovery_callbacks {
}; };
struct neighbor_discovery_conn { struct neighbor_discovery_conn {
/* struct broadcast_conn c;*/ struct broadcast_conn c;
struct ipolite_conn c;
const struct neighbor_discovery_callbacks *u; const struct neighbor_discovery_callbacks *u;
struct ctimer t; struct ctimer t, interval_timer;
clock_time_t initial_interval, min_interval, max_interval; clock_time_t initial_interval, min_interval, max_interval;
uint16_t val; uint16_t val;
}; };