47 #include "lib/random.h"
49 #if CONTIKI_TARGET_NETSIM
53 #define INTERVAL_MIN 1
54 #define INTERVAL_MAX 4
56 #define DUPLICATE_THRESHOLD 1
58 #define SEQNO_LT(a, b) ((signed char)((a) - (b)) < 0)
60 static const struct packetbuf_attrlist attributes[] =
62 TRICKLE_ATTRIBUTES PACKETBUF_ATTR_LAST
69 #define PRINTF(...) printf(__VA_ARGS__)
74 static int run_trickle(
struct trickle_conn *c);
79 struct trickle_conn *c = ptr;
82 queuebuf_to_packetbuf(c->q);
85 PRINTF(
"%d.%d: trickle send but c->q == NULL\n",
91 timer_callback(
void *ptr)
93 struct trickle_conn *c = ptr;
98 reset_interval(
struct trickle_conn *c)
105 set_timer(
struct trickle_conn *c,
struct ctimer *t, clock_time_t i)
111 run_trickle(
struct trickle_conn *c)
113 clock_time_t interval;
117 interval = c->interval << c->interval_scaling;
118 set_timer(c, &c->interval_timer, interval);
119 set_timer(c, &c->t, interval / 2 + (random_rand() % (interval / 2)));
123 if(c->duplicates < DUPLICATE_THRESHOLD) {
127 if(c->interval_scaling < INTERVAL_MAX) {
128 c->interval_scaling++;
136 recv(
struct broadcast_conn *bc,
const rimeaddr_t *from)
138 struct trickle_conn *c = (
struct trickle_conn *)bc;
139 uint16_t seqno = packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);
141 PRINTF(
"%d.%d: trickle recv seqno %d from %d.%d our %d data len %d channel %d\n",
144 from->u8[0], from->u8[1],
147 packetbuf_attr(PACKETBUF_ATTR_CHANNEL));
149 if(seqno == c->seqno) {
152 }
else if(SEQNO_LT(seqno, c->seqno)) {
153 c->interval_scaling = 0;
156 #if CONTIKI_TARGET_NETSIM
164 c->q = queuebuf_new_from_packetbuf();
165 c->interval_scaling = 0;
167 ctimer_set(&c->first_transmission_timer, random_rand() % c->interval,
176 trickle_open(
struct trickle_conn *c, clock_time_t interval,
177 uint16_t channel,
const struct trickle_callbacks *cb)
182 c->interval = interval;
183 c->interval_scaling = 0;
184 channel_set_attributes(channel, attributes);
188 trickle_close(
struct trickle_conn *c)
196 trickle_send(
struct trickle_conn *c)
202 packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, c->seqno);
203 c->q = queuebuf_new_from_packetbuf();
204 PRINTF(
"%d.%d: trickle send seqno %d\n",