diff --git a/core/net/rime/nbh.h b/core/net/rime/nbh.h index ad6c06d5f..a43263d38 100644 --- a/core/net/rime/nbh.h +++ b/core/net/rime/nbh.h @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: nbh.h,v 1.1 2007/11/13 21:01:54 adamdunkels Exp $ + * $Id: nbh.h,v 1.2 2007/11/17 10:32:54 adamdunkels Exp $ */ /** @@ -46,7 +46,7 @@ struct nbh_conn; struct nbh_callbacks { - int (* recv)(struct nbh_conn *c, rimeaddr_t *from, uint16_t val); + void (* recv)(struct nbh_conn *c, rimeaddr_t *from, uint16_t val); void (* sent)(struct nbh_conn *c); }; diff --git a/core/net/rime/neighbor.c b/core/net/rime/neighbor.c index 4281843a7..846930033 100644 --- a/core/net/rime/neighbor.c +++ b/core/net/rime/neighbor.c @@ -33,7 +33,7 @@ * * This file is part of the Contiki operating system. * - * $Id: neighbor.c,v 1.10 2007/11/13 20:39:29 adamdunkels Exp $ + * $Id: neighbor.c,v 1.11 2007/11/17 10:32:54 adamdunkels Exp $ */ /** @@ -108,12 +108,11 @@ neighbor_find(rimeaddr_t *addr) } /*---------------------------------------------------------------------------*/ void -neighbor_update(struct neighbor *n, u8_t rtmetric, u8_t etx) +neighbor_update(struct neighbor *n, u8_t rtmetric) { if(n != NULL) { n->rtmetric = rtmetric; n->time = 0; - neighbor_update_etx(n, etx); } } /*---------------------------------------------------------------------------*/ diff --git a/core/net/rime/neighbor.h b/core/net/rime/neighbor.h index bbf6c2ea5..602012392 100644 --- a/core/net/rime/neighbor.h +++ b/core/net/rime/neighbor.h @@ -38,7 +38,7 @@ * * This file is part of the Contiki operating system. * - * $Id: neighbor.h,v 1.6 2007/11/13 20:39:29 adamdunkels Exp $ + * $Id: neighbor.h,v 1.7 2007/11/17 10:32:54 adamdunkels Exp $ */ /** @@ -68,7 +68,7 @@ void neighbor_init(void); /*void neighbor_periodic(int max_time);*/ void neighbor_add(rimeaddr_t *addr, u8_t rtmetric, u8_t etx); -void neighbor_update(struct neighbor *n, u8_t rtmetric, u8_t etx); +void neighbor_update(struct neighbor *n, u8_t rtmetric); void neighbor_update_etx(struct neighbor *n, u8_t etx); void neighbor_timedout_etx(struct neighbor *n, u8_t etx); void neighbor_remove(rimeaddr_t *addr); diff --git a/core/net/rime/tree.c b/core/net/rime/tree.c index b053b150a..4c62464ed 100644 --- a/core/net/rime/tree.c +++ b/core/net/rime/tree.c @@ -33,7 +33,7 @@ * * This file is part of the Contiki operating system. * - * $Id: tree.c,v 1.13 2007/11/13 20:39:29 adamdunkels Exp $ + * $Id: tree.c,v 1.14 2007/11/17 10:32:54 adamdunkels Exp $ */ /** @@ -60,21 +60,24 @@ #include #include -/* -struct adv_msg { - u8_t rtmetric; - u8_t pad; -}; -*/ - struct hdr { rimeaddr_t originator; u8_t originator_seqno; - u8_t rtmetric; + u8_t hops; u8_t hoplim; u8_t rexmits; }; +#define NUM_RECENT_PACKETS 4 + +struct recent_packet { + rimeaddr_t originator; + uint8_t seqno; +}; + +static struct recent_packet recent_packets[NUM_RECENT_PACKETS]; +static uint8_t recent_packet_ptr; + #define SINK 0 #define RTMETRIC_MAX TREE_MAX_DEPTH @@ -142,73 +145,6 @@ update_rtmetric(struct tree_conn *tc) } #endif } -#if 0 -/*---------------------------------------------------------------------------*/ -static void -send_adv(struct tree_conn *tc, clock_time_t interval) -{ - struct adv_msg *hdr; - - rimebuf_clear(); - rimebuf_set_datalen(sizeof(struct adv_msg)); - hdr = rimebuf_dataptr(); - hdr->rtmetric = tc->local_rtmetric; - ipolite_send(&tc->ipolite_conn, interval, rimebuf_totlen()); -} -/*---------------------------------------------------------------------------*/ -static void -adv_packet_received(struct ipolite_conn *c, rimeaddr_t *from) -{ - struct tree_conn *tc = (struct tree_conn *) - ((char *)c - offsetof(struct tree_conn, ipolite_conn)); - struct adv_msg *msg = rimebuf_dataptr(); - struct neighbor *n; - - /* PRINTF("%d.%d: adv_packet_received from %d.%d with rtmetric %d\n", - rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], - from->u8[0], from->u8[1], msg->rtmetric);*/ - - n = neighbor_find(from); - - if(n == NULL) { - neighbor_add(from, msg->rtmetric, radio_sensor.value(1)); - } else { - neighbor_update(n, msg->rtmetric, radio_sensor.value(1)); - PRINTF("%d.%d: updating neighbor %d.%d, radio sensor %d, hops %d\n", - rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], - n->addr.u8[0], n->addr.u8[1], - radio_sensor.value(1), msg->rtmetric); - } - - update_rtmetric(tc); - -} -/*---------------------------------------------------------------------------*/ -static void -adv_packet_sent(struct ipolite_conn *c) -{ - struct tree_conn *tc = (struct tree_conn *) - ((char *)c - offsetof(struct tree_conn, ipolite_conn)); - /* send_adv(tc, MAX_INTERVAL);*/ -} -/*---------------------------------------------------------------------------*/ -static void -adv_packet_dropped(struct ipolite_conn *c) -{ - struct tree_conn *tc = (struct tree_conn *) - ((char *)c - offsetof(struct tree_conn, ipolite_conn)); - /* send_adv(tc, MAX_INTERVAL);*/ -} -/*---------------------------------------------------------------------------*/ -static void -send_timer(void *ptr) -{ - struct tree_conn *tc = ptr; - - send_adv(tc, MAX_INTERVAL / 2); - ctimer_set(&tc->t, MAX_INTERVAL, send_timer, tc); -} -#endif /* 0 */ /*---------------------------------------------------------------------------*/ static int node_packet_received(struct ruc_conn *c, rimeaddr_t *from, u8_t seqno) @@ -217,9 +153,27 @@ node_packet_received(struct ruc_conn *c, rimeaddr_t *from, u8_t seqno) ((char *)c - offsetof(struct tree_conn, ruc_conn)); struct hdr *hdr = rimebuf_dataptr(); struct neighbor *n; + int i; + /* To protect against forwarding duplicate packets, we keep a list + of recently forwarded packet seqnos. If the seqno of the current + packet exists in the list, we drop the packet. */ + + for(i = 0; i < NUM_RECENT_PACKETS; i++) { + if(recent_packets[i].seqno == hdr->originator_seqno && + rimeaddr_cmp(&recent_packets[i].originator, &hdr->originator)) { + /* Drop the packet. */ + return 1; + } + } + recent_packets[recent_packet_ptr].seqno = hdr->originator_seqno; + rimeaddr_copy(&recent_packets[recent_packet_ptr].originator, &hdr->originator); + recent_packet_ptr = (recent_packet_ptr + 1) % NUM_RECENT_PACKETS; + if(tc->local_rtmetric == SINK) { + /* If we are the sink, we call the receive function. */ + rimebuf_hdrreduce(sizeof(struct hdr)); PRINTF("%d.%d: sink received packet from %d.%d via %d.%d with rtmetric %d\n", @@ -229,10 +183,14 @@ node_packet_received(struct ruc_conn *c, rimeaddr_t *from, u8_t seqno) if(tc->cb->recv != NULL) { tc->cb->recv(&hdr->originator, hdr->originator_seqno, - hdr->rtmetric); + hdr->hops); } return 1; } else if(hdr->hoplim > 1 && tc->local_rtmetric != RTMETRIC_MAX) { + + /* If we are not the sink, we forward the packet to the best + neighbor. */ + hdr->hops++; hdr->hoplim--; @@ -267,6 +225,7 @@ node_packet_sent(struct ruc_conn *c, rimeaddr_t *to, u8_t retransmissions) tc->forwarding = 0; neighbor_update_etx(neighbor_find(to), retransmissions); + update_rtmetric(tc); } /*---------------------------------------------------------------------------*/ static void @@ -277,6 +236,7 @@ node_packet_timedout(struct ruc_conn *c, rimeaddr_t *to, u8_t retransmissions) tc->forwarding = 0; neighbor_timedout_etx(neighbor_find(to), retransmissions); + update_rtmetric(tc); } /*---------------------------------------------------------------------------*/ static void @@ -293,7 +253,7 @@ adv_received(struct nbh_conn *c, rimeaddr_t *from, uint16_t rtmetric) neighbor_add(from, rtmetric, 1); } } else { - neighbor_update(n, rtmetric, 1); + neighbor_update(n, rtmetric); PRINTF("%d.%d: updating neighbor %d.%d, etx %d, hops %d\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], n->addr.u8[0], n->addr.u8[1], @@ -303,8 +263,6 @@ adv_received(struct nbh_conn *c, rimeaddr_t *from, uint16_t rtmetric) update_rtmetric(tc); } /*---------------------------------------------------------------------------*/ -/*static const struct ipolite_callbacks ipolite_callbacks = - {adv_packet_received, adv_packet_sent, adv_packet_dropped};*/ static const struct ruc_callbacks ruc_callbacks = {node_packet_received, node_packet_sent, node_packet_timedout}; @@ -315,19 +273,15 @@ void tree_open(struct tree_conn *tc, u16_t channels, const struct tree_callbacks *cb) { - /* ipolite_open(&tc->ipolite_conn, channels, &ipolite_callbacks);*/ nbh_open(&tc->nbh_conn, channels, &nbh_callbacks); ruc_open(&tc->ruc_conn, channels + 1, &ruc_callbacks); tc->local_rtmetric = RTMETRIC_MAX; tc->cb = cb; - /* send_adv(tc, MAX_INTERVAL); - ctimer_set(&tc->t, MAX_INTERVAL, send_timer, tc);*/ } /*---------------------------------------------------------------------------*/ void tree_close(struct tree_conn *tc) { - /* ipolite_close(&tc->ipolite_conn);*/ nbh_close(&tc->nbh_conn); ruc_close(&tc->ruc_conn); } @@ -337,7 +291,6 @@ tree_set_sink(struct tree_conn *tc, int should_be_sink) { if(should_be_sink) { tc->local_rtmetric = SINK; - /* send_adv(tc, MIN_INTERVAL);*/ nbh_start(&tc->nbh_conn, tc->local_rtmetric); } else { tc->local_rtmetric = RTMETRIC_MAX; @@ -356,13 +309,13 @@ tree_send(struct tree_conn *tc, int rexmits) hdr = rimebuf_hdrptr(); hdr->originator_seqno = tc->seqno++; rimeaddr_copy(&hdr->originator, &rimeaddr_node_addr); - hdr->rtmetric = tc->local_rtmetric; + hdr->hops = 0; hdr->hoplim = MAX_HOPLIM; hdr->rexmits = rexmits; if(tc->local_rtmetric == 0) { if(tc->cb->recv != NULL) { tc->cb->recv(&hdr->originator, hdr->originator_seqno, - hdr->rtmetric); + hdr->hops); } } else { n = neighbor_best();