43 #include "sys/clock.h"
47 #include "dev/watchdog.h"
50 struct phase_queueitem {
52 mac_callback_t mac_callback;
53 void *mac_callback_ptr;
57 #define PHASE_DEFER_THRESHOLD 1
58 #define PHASE_QUEUESIZE 8
62 #define MAX_NOACKS_TIME CLOCK_SECOND * 30
64 MEMB(queued_packets_memb,
struct phase_queueitem, PHASE_QUEUESIZE);
69 #define PRINTF(...) printf(__VA_ARGS__)
70 #define PRINTDEBUG(...) printf(__VA_ARGS__)
73 #define PRINTDEBUG(...)
77 find_neighbor(
const struct phase_list *list,
const rimeaddr_t *addr)
89 phase_remove(
const struct phase_list *list,
const rimeaddr_t *neighbor)
92 e = find_neighbor(list, neighbor);
100 phase_update(
const struct phase_list *list,
101 const rimeaddr_t *neighbor, rtimer_clock_t time,
107 e = find_neighbor(list, neighbor);
116 PRINTF(
"phase noacks %d to %d.%d\n", e->noacks, neighbor->u8[0], neighbor->u8[1]);
119 timer_set(&e->noacks_timer, MAX_NOACKS_TIME);
121 if(e->noacks >= MAX_NOACKS ||
timer_expired(&e->noacks_timer)) {
122 PRINTF(
"drop %d\n", neighbor->u8[0]);
135 PRINTF(
"phase alloc NULL\n");
149 send_packet(
void *ptr)
151 struct phase_queueitem *p = ptr;
153 queuebuf_to_packetbuf(p->q);
156 NETSTACK_RDC.send(p->mac_callback, p->mac_callback_ptr);
160 phase_wait(
struct phase_list *list,
161 const rimeaddr_t *neighbor, rtimer_clock_t cycle_time,
162 rtimer_clock_t guard_time,
163 mac_callback_t mac_callback,
void *mac_callback_ptr)
171 e = find_neighbor(list, neighbor);
173 rtimer_clock_t wait, now, expected;
174 clock_time_t ctimewait;
191 wait = (rtimer_clock_t)((e->time - now) &
193 if(wait < guard_time) {
197 ctimewait = (
CLOCK_SECOND * (wait - guard_time)) / RTIMER_ARCH_SECOND;
199 if(ctimewait > PHASE_DEFER_THRESHOLD) {
200 struct phase_queueitem *p;
204 p->q = queuebuf_new_from_packetbuf();
206 p->mac_callback = mac_callback;
207 p->mac_callback_ptr = mac_callback_ptr;
208 ctimer_set(&p->timer, ctimewait, send_packet, p);
209 return PHASE_DEFERRED;
216 expected = now + wait - guard_time;
217 if(!RTIMER_CLOCK_LT(expected, now)) {
219 while(RTIMER_CLOCK_LT(
RTIMER_NOW(), expected));
221 return PHASE_SEND_NOW;
223 return PHASE_UNKNOWN;
227 phase_init(
struct phase_list *list)