43 #include "contiki-conf.h"
47 #include "lib/random.h"
53 #define PRINTF(...) printf(__VA_ARGS__)
62 #if TRICKLE_TIMER_WIDE_RAND
63 #define tt_rand() wide_rand()
65 #define tt_rand() random_rand()
71 static clock_time_t loc_clock;
73 static void fire(
void *ptr);
74 static void double_interval(
void *ptr);
78 #if TRICKLE_TIMER_WIDE_RAND
95 #if TRICKLE_TIMER_ERROR_CHECKING
97 max_imax(clock_time_t value)
100 #if (TRICKLE_TIMER_MAX_IMAX_WIDTH==TRICKLE_TIMER_MAX_IMAX_GENERIC)
102 clock_time_t mask = 0xFFFF;
106 for(i =
sizeof(clock_time_t) << 2; i > 0; i >>= 1) {
107 if((value & (mask <<= i)) == 0) {
113 #elif (TRICKLE_TIMER_MAX_IMAX_WIDTH==TRICKLE_TIMER_MAX_IMAX_16_BIT)
114 if((value & 0xFF00) == 0) {
118 if((value & 0xF000) == 0) {
122 if((value & 0xC000) == 0) {
126 if((value & 0x8000) == 0) {
129 #elif (TRICKLE_TIMER_MAX_IMAX_WIDTH==TRICKLE_TIMER_MAX_IMAX_32_BIT)
130 if((value & 0xFFFF0000) == 0) {
134 if((value & 0xFF000000) == 0) {
138 if((value & 0xF0000000) == 0) {
142 if((value & 0xC0000000) == 0) {
146 if((value & 0x80000000) == 0) {
157 get_t(clock_time_t
i_cur)
161 PRINTF(
"trickle_timer get t: [%lu, %lu)\n", (
unsigned long)i_cur,
162 (
unsigned long)(i_cur << 1));
175 PRINTF(
"trickle_timer sched for end: at %lu, end in %ld\n",
176 (
unsigned long)
clock_time(), (
signed long)loc_clock);
182 PRINTF(
"trickle_timer doubling: Was in the past. Compensating\n");
191 double_interval(
void *ptr)
193 clock_time_t last_end;
200 PRINTF(
"trickle_timer doubling: at %lu, (was for %lu), ",
211 PRINTF(
"I << 1 = %lu\n", (
unsigned long)loctt->
i_cur);
216 PRINTF(
"I = Imax = %lu\n", (
unsigned long)loctt->
i_cur);
220 loc_clock = get_t(loctt->
i_cur);
222 PRINTF(
"trickle_timer doubling: t=%lu\n", (
unsigned long)loc_clock);
224 #if TRICKLE_TIMER_COMPENSATE_DRIFT
227 loc_clock = (last_end + loc_clock) -
clock_time();
228 PRINTF(
"trickle_timer doubling: at %lu, in %ld ticks\n",
229 (
unsigned long)
clock_time(), (
signed long)loc_clock);
233 PRINTF(
"trickle_timer doubling: Was in the past. Compensating\n");
245 loctt->
i_start = loctt->
ct.etimer.timer.start;
248 PRINTF(
"trickle_timer doubling: Last end %lu, new end %lu, for %lu, I=%lu\n",
249 (
unsigned long)last_end,
251 (
unsigned long)(loctt->
ct.etimer.timer.start +
252 loctt->
ct.etimer.timer.interval),
253 (
unsigned long)(loctt->
i_cur));
264 PRINTF(
"trickle_timer fire: at %lu (was for %lu)\n",
266 (
unsigned long)(loctt->
ct.etimer.timer.start +
267 loctt->
ct.etimer.timer.interval));
274 PRINTF(
"trickle_timer fire: Suppression Status %u (%u < %u)\n",
280 schedule_for_end(loctt);
292 loc_clock = get_t(tt->
i_cur);
298 PRINTF(
"trickle_timer new interval: at %lu, ends %lu, ",
301 PRINTF(
"t=%lu, I=%lu\n", (
unsigned long)loc_clock, (
unsigned long)tt->
i_cur);
312 PRINTF(
"trickle_timer consistency: c=%u\n", tt->
c);
321 PRINTF(
"trickle_timer inconsistency\n");
332 #if TRICKLE_TIMER_ERROR_CHECKING
339 PRINTF(
"trickle_timer config: Bad Imin value\n");
340 return TRICKLE_TIMER_ERROR;
343 if(tt ==
NULL || i_max == 0 || k == 0) {
344 PRINTF(
"trickle_timer config: Bad arguments\n");
345 return TRICKLE_TIMER_ERROR;
355 PRINTF(
"trickle_timer config: %lu << %u would exceed clock boundaries. ",
356 (
unsigned long)i_min, i_max);
359 i_max = max_imax(i_min);
360 PRINTF(
"trickle_timer config: Using Imax=%u\n", i_max);
369 PRINTF(
"trickle_timer config: Imin=%lu, Imax=%u, k=%u\n",
372 return TRICKLE_TIMER_SUCCESS;
379 #if TRICKLE_TIMER_ERROR_CHECKING
381 if(tt ==
NULL || proto_cb ==
NULL) {
382 PRINTF(
"trickle_timer set: Bad arguments\n");
383 return TRICKLE_TIMER_ERROR;
394 PRINTF(
"trickle_timer set: I=%lu in [%lu , %lu]\n", (
unsigned long)tt->
i_cur,
395 (
unsigned long)tt->
i_min,
400 PRINTF(
"trickle_timer set: at %lu, ends %lu, t=%lu in [%lu , %lu)\n",
403 (
unsigned long)tt->
ct.etimer.timer.interval,
404 (
unsigned long)tt->
i_cur >> 1, (
unsigned long)tt->
i_cur);
406 return TRICKLE_TIMER_SUCCESS;