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",