Rewrote the collect module to use packet attributes instead of explicit headers
This commit is contained in:
parent
6bbb99438e
commit
020a0c30aa
2 changed files with 61 additions and 56 deletions
|
@ -36,7 +36,7 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* $Id: collect.c,v 1.10 2008/06/27 16:44:58 adamdunkels Exp $
|
* $Id: collect.c,v 1.11 2008/06/30 09:15:22 adamdunkels Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,13 +63,11 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
struct hdr {
|
static const struct rimebuf_attrlist attributes[] =
|
||||||
rimeaddr_t originator;
|
{
|
||||||
uint8_t originator_seqno;
|
COLLECT_ATTRIBUTES
|
||||||
uint8_t hops;
|
RIMEBUF_ATTR_LAST
|
||||||
uint8_t hoplim;
|
};
|
||||||
uint8_t rexmits;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define NUM_RECENT_PACKETS 4
|
#define NUM_RECENT_PACKETS 4
|
||||||
|
|
||||||
|
@ -150,7 +148,6 @@ node_packet_received(struct ruc_conn *c, rimeaddr_t *from, uint8_t seqno)
|
||||||
{
|
{
|
||||||
struct collect_conn *tc = (struct collect_conn *)
|
struct collect_conn *tc = (struct collect_conn *)
|
||||||
((char *)c - offsetof(struct collect_conn, ruc_conn));
|
((char *)c - offsetof(struct collect_conn, ruc_conn));
|
||||||
struct hdr *hdr = rimebuf_dataptr();
|
|
||||||
struct neighbor *n;
|
struct neighbor *n;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -159,44 +156,48 @@ node_packet_received(struct ruc_conn *c, rimeaddr_t *from, uint8_t seqno)
|
||||||
packet exists in the list, we drop the packet. */
|
packet exists in the list, we drop the packet. */
|
||||||
|
|
||||||
for(i = 0; i < NUM_RECENT_PACKETS; i++) {
|
for(i = 0; i < NUM_RECENT_PACKETS; i++) {
|
||||||
if(recent_packets[i].seqno == hdr->originator_seqno &&
|
if(recent_packets[i].seqno == rimebuf_attr(RIMEBUF_ATTR_EPACKET_ID)&&
|
||||||
rimeaddr_cmp(&recent_packets[i].originator, &hdr->originator)) {
|
rimeaddr_cmp(&recent_packets[i].originator,
|
||||||
|
rimebuf_addr(RIMEBUF_ADDR_ESENDER))) {
|
||||||
/* Drop the packet. */
|
/* Drop the packet. */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
recent_packets[recent_packet_ptr].seqno = hdr->originator_seqno;
|
recent_packets[recent_packet_ptr].seqno = rimebuf_attr(RIMEBUF_ATTR_EPACKET_ID);
|
||||||
rimeaddr_copy(&recent_packets[recent_packet_ptr].originator, &hdr->originator);
|
rimeaddr_copy(&recent_packets[recent_packet_ptr].originator,
|
||||||
|
rimebuf_addr(RIMEBUF_ADDR_ESENDER));
|
||||||
recent_packet_ptr = (recent_packet_ptr + 1) % NUM_RECENT_PACKETS;
|
recent_packet_ptr = (recent_packet_ptr + 1) % NUM_RECENT_PACKETS;
|
||||||
|
|
||||||
if(tc->rtmetric == SINK) {
|
if(tc->rtmetric == SINK) {
|
||||||
|
|
||||||
/* If we are the sink, we call the receive function. */
|
/* 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\n",
|
||||||
|
|
||||||
PRINTF("%d.%d: sink received packet from %d.%d via %d.%d with rtmetric %d\n",
|
|
||||||
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
||||||
hdr->originator.u8[0], hdr->originator.u8[1],
|
rimebuf_addr(RIMEBUF_ADDR_ESENDER)->u8[0],
|
||||||
from->u8[0], from->u8[1], hdr->rtmetric);
|
rimebuf_addr(RIMEBUF_ADDR_ESENDER)->u8[1],
|
||||||
|
from->u8[0], from->u8[1]);
|
||||||
|
|
||||||
if(tc->cb->recv != NULL) {
|
if(tc->cb->recv != NULL) {
|
||||||
tc->cb->recv(&hdr->originator, hdr->originator_seqno,
|
tc->cb->recv(rimebuf_addr(RIMEBUF_ADDR_ESENDER),
|
||||||
hdr->hops);
|
rimebuf_attr(RIMEBUF_ATTR_EPACKET_ID),
|
||||||
|
rimebuf_attr(RIMEBUF_ATTR_HOPS));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if(hdr->hoplim > 1 && tc->rtmetric != RTMETRIC_MAX) {
|
} else if(rimebuf_attr(RIMEBUF_ATTR_TTL) > 1 &&
|
||||||
|
tc->rtmetric != RTMETRIC_MAX) {
|
||||||
|
|
||||||
/* If we are not the sink, we forward the packet to the best
|
/* If we are not the sink, we forward the packet to the best
|
||||||
neighbor. */
|
neighbor. */
|
||||||
hdr->hops++;
|
rimebuf_set_attr(RIMEBUF_ATTR_HOPS, rimebuf_attr(RIMEBUF_ATTR_HOPS) + 1);
|
||||||
hdr->hoplim--;
|
rimebuf_set_attr(RIMEBUF_ATTR_TTL, rimebuf_attr(RIMEBUF_ATTR_TTL) - 1);
|
||||||
|
|
||||||
|
|
||||||
PRINTF("%d.%d: packet received from %d.%d via %d.%d with rtmetric %d, best neighbor %p, forwarding %d\n",
|
PRINTF("%d.%d: packet received from %d.%d via %d.%d, best neighbor %p, forwarding %d\n",
|
||||||
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
||||||
hdr->originator.u8[0], hdr->originator.u8[1],
|
rimebuf_addr(RIMEBUF_ADDR_ESENDER)->u8[0],
|
||||||
from->u8[0], from->u8[1], hdr->rtmetric,
|
rimebuf_addr(RIMEBUF_ADDR_ESENDER)->u8[1],
|
||||||
|
from->u8[0], from->u8[1],
|
||||||
neighbor_best(), tc->forwarding);
|
neighbor_best(), tc->forwarding);
|
||||||
|
|
||||||
if(!tc->forwarding) {
|
if(!tc->forwarding) {
|
||||||
|
@ -206,7 +207,7 @@ node_packet_received(struct ruc_conn *c, rimeaddr_t *from, uint8_t seqno)
|
||||||
#if NETSIM
|
#if NETSIM
|
||||||
ether_set_line(n->addr.u8[0], n->addr.u8[1]);
|
ether_set_line(n->addr.u8[0], n->addr.u8[1]);
|
||||||
#endif /* NETSIM */
|
#endif /* NETSIM */
|
||||||
ruc_send(c, &n->addr, hdr->rexmits);
|
ruc_send(c, &n->addr, rimebuf_attr(RIMEBUF_ATTR_MAX_REXMIT));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
@ -279,6 +280,7 @@ collect_open(struct collect_conn *tc, uint16_t channels,
|
||||||
CLOCK_SECOND * 60,
|
CLOCK_SECOND * 60,
|
||||||
&neighbor_discovery_callbacks);
|
&neighbor_discovery_callbacks);
|
||||||
ruc_open(&tc->ruc_conn, channels + 1, &ruc_callbacks);
|
ruc_open(&tc->ruc_conn, channels + 1, &ruc_callbacks);
|
||||||
|
channel_set_attributes(channels + 1, attributes);
|
||||||
tc->rtmetric = RTMETRIC_MAX;
|
tc->rtmetric = RTMETRIC_MAX;
|
||||||
tc->cb = cb;
|
tc->cb = cb;
|
||||||
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
||||||
|
@ -307,36 +309,32 @@ int
|
||||||
collect_send(struct collect_conn *tc, int rexmits)
|
collect_send(struct collect_conn *tc, int rexmits)
|
||||||
{
|
{
|
||||||
struct neighbor *n;
|
struct neighbor *n;
|
||||||
struct hdr *hdr;
|
|
||||||
|
|
||||||
|
rimebuf_set_attr(RIMEBUF_ATTR_EPACKET_ID, tc->seqno++);
|
||||||
|
rimebuf_set_addr(RIMEBUF_ADDR_ESENDER, &rimeaddr_node_addr);
|
||||||
|
rimebuf_set_attr(RIMEBUF_ATTR_HOPS, 1);
|
||||||
|
rimebuf_set_attr(RIMEBUF_ATTR_TTL, MAX_HOPLIM);
|
||||||
|
rimebuf_set_attr(RIMEBUF_ATTR_MAX_REXMIT, rexmits);
|
||||||
|
|
||||||
if(rimebuf_hdralloc(sizeof(struct hdr))) {
|
if(tc->rtmetric == 0) {
|
||||||
hdr = rimebuf_hdrptr();
|
rimebuf_set_attr(RIMEBUF_ATTR_HOPS, 0);
|
||||||
hdr->originator_seqno = tc->seqno++;
|
if(tc->cb->recv != NULL) {
|
||||||
rimeaddr_copy(&hdr->originator, &rimeaddr_node_addr);
|
tc->cb->recv(rimebuf_addr(RIMEBUF_ADDR_ESENDER),
|
||||||
hdr->hops = 1;
|
rimebuf_attr(RIMEBUF_ATTR_EPACKET_ID),
|
||||||
hdr->hoplim = MAX_HOPLIM;
|
rimebuf_attr(RIMEBUF_ATTR_HOPS));
|
||||||
hdr->rexmits = rexmits;
|
}
|
||||||
if(tc->rtmetric == 0) {
|
return 1;
|
||||||
hdr->hops = 0;
|
} else {
|
||||||
if(tc->cb->recv != NULL) {
|
n = neighbor_best();
|
||||||
tc->cb->recv(&hdr->originator, hdr->originator_seqno,
|
if(n != NULL) {
|
||||||
hdr->hops);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
n = neighbor_best();
|
|
||||||
if(n != NULL) {
|
|
||||||
/* printf("Sending to best neighbor\n");*/
|
|
||||||
#if NETSIM
|
#if NETSIM
|
||||||
ether_set_line(n->addr.u8[0], n->addr.u8[1]);
|
ether_set_line(n->addr.u8[0], n->addr.u8[1]);
|
||||||
#endif /* NETSIM */
|
#endif /* NETSIM */
|
||||||
return ruc_send(&tc->ruc_conn, &n->addr, rexmits);
|
return ruc_send(&tc->ruc_conn, &n->addr, rexmits);
|
||||||
} else {
|
} else {
|
||||||
/* printf("Didn't find any neighbor\n");*/
|
/* printf("Didn't find any neighbor\n");*/
|
||||||
PRINTF("%d.%d: did not find any neighbor to send to\n",
|
PRINTF("%d.%d: did not find any neighbor to send to\n",
|
||||||
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -47,7 +47,7 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* $Id: collect.h,v 1.5 2008/06/26 11:38:59 nifi Exp $
|
* $Id: collect.h,v 1.6 2008/06/30 09:15:22 adamdunkels Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,6 +64,13 @@
|
||||||
#include "net/rime/ruc.h"
|
#include "net/rime/ruc.h"
|
||||||
#include "net/rime/neighbor-discovery.h"
|
#include "net/rime/neighbor-discovery.h"
|
||||||
|
|
||||||
|
#define COLLECT_ATTRIBUTES { RIMEBUF_ADDR_ESENDER, RIMEBUF_ADDRSIZE }, \
|
||||||
|
{ RIMEBUF_ATTR_EPACKET_ID, RIMEBUF_ATTR_BIT * 2 }, \
|
||||||
|
{ RIMEBUF_ATTR_TTL, RIMEBUF_ATTR_BIT * 4 }, \
|
||||||
|
{ RIMEBUF_ATTR_HOPS, RIMEBUF_ATTR_BIT * 4 }, \
|
||||||
|
{ RIMEBUF_ATTR_MAX_REXMIT, RIMEBUF_ATTR_BIT * 3 }, \
|
||||||
|
RUC_ATTRIBUTES
|
||||||
|
|
||||||
struct collect_callbacks {
|
struct collect_callbacks {
|
||||||
void (* recv)(rimeaddr_t *originator, uint8_t seqno,
|
void (* recv)(rimeaddr_t *originator, uint8_t seqno,
|
||||||
uint8_t hops);
|
uint8_t hops);
|
||||||
|
|
Loading…
Add table
Reference in a new issue