46 #include "lib/random.h"
48 #if CONTIKI_TARGET_NETSIM
52 #define INTERVAL_MIN 1
53 #define INTERVAL_MAX 4
55 #define DUPLICATE_THRESHOLD 1
57 #define SEQNO_LT(a, b) ((signed char)((a) - (b)) < 0)
59 static const struct packetbuf_attrlist attributes[] =
61 TRICKLE_ATTRIBUTES PACKETBUF_ATTR_LAST
68 #define PRINTF(...) printf(__VA_ARGS__)
73 static int run_trickle(
struct trickle_conn *c);
78 struct trickle_conn *c = ptr;
81 queuebuf_to_packetbuf(c->q);
84 PRINTF(
"%d.%d: trickle send but c->q == NULL\n",
90 timer_callback(
void *ptr)
92 struct trickle_conn *c = ptr;
97 reset_interval(
struct trickle_conn *c)
104 set_timer(
struct trickle_conn *c,
struct ctimer *t, clock_time_t i)
110 run_trickle(
struct trickle_conn *c)
112 clock_time_t interval;
116 interval = c->interval << c->interval_scaling;
117 set_timer(c, &c->interval_timer, interval);
118 set_timer(c, &c->t, interval / 2 + (
random_rand() % (interval / 2)));
122 if(c->duplicates < DUPLICATE_THRESHOLD) {
126 if(c->interval_scaling < INTERVAL_MAX) {
127 c->interval_scaling++;
135 recv(
struct broadcast_conn *bc,
const linkaddr_t *from)
137 struct trickle_conn *c = (
struct trickle_conn *)bc;
138 uint16_t seqno = packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);
140 PRINTF(
"%d.%d: trickle recv seqno %d from %d.%d our %d data len %d channel %d\n",
143 from->u8[0], from->u8[1],
146 packetbuf_attr(PACKETBUF_ATTR_CHANNEL));
148 if(seqno == c->seqno) {
151 }
else if(SEQNO_LT(seqno, c->seqno)) {
152 c->interval_scaling = 0;
155 #if CONTIKI_TARGET_NETSIM
163 c->q = queuebuf_new_from_packetbuf();
164 c->interval_scaling = 0;
175 trickle_open(
struct trickle_conn *c, clock_time_t interval,
176 uint16_t channel,
const struct trickle_callbacks *cb)
181 c->interval = interval;
182 c->interval_scaling = 0;
183 channel_set_attributes(channel, attributes);
187 trickle_close(
struct trickle_conn *c)
195 trickle_send(
struct trickle_conn *c)
201 packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, c->seqno);
202 c->q = queuebuf_new_from_packetbuf();
203 PRINTF(
"%d.%d: trickle send seqno %d\n",
linkaddr_t linkaddr_node_addr
The Rime address of the node.
#define CC_CONST_FUNCTION
Configure if the C compiler have problems with const function pointers.
void broadcast_close(struct broadcast_conn *c)
Close a broadcast connection.
void(* recv)(struct broadcast_conn *ptr, const linkaddr_t *sender)
Called when a packet has been received by the broadcast module.
#define PT_YIELD(pt)
Yield from the current protothread.
#define NULL
The null pointer.
#define PT_INIT(pt)
Initialize a protothread.
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Callback structure for broadcast.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
int broadcast_send(struct broadcast_conn *c)
Send an identified best-effort broadcast packet.
#define PT_END(pt)
Declare the end of a protothread.
Header file for Trickle (reliable single source flooding) for Rime
void broadcast_open(struct broadcast_conn *c, uint16_t channel, const struct broadcast_callbacks *u)
Set up an identified best-effort broadcast connection.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
unsigned short random_rand(void)
Generate the next state and return the upper part of it.