35 #include "net/mac/ctdma_mac.h"
50 #define DLEDS_ON(x) leds_on(x)
51 #define DLEDS_OFF(x) leds_off(x)
52 #define DLEDS_TOGGLE(x) leds_toggle(x)
53 #define PRINTF(...) printf(__VA_ARGS__)
57 #define DLEDS_TOGGLE(x)
63 #define SLOT_LENGTH (CLOCK_SECOND/3)
64 #define GUARD_PERIOD (CLOCK_SECOND/12)
66 #define MY_SLOT (node_id % NR_SLOTS)
67 #define PERIOD_LENGTH CLOCK_SECOND
71 uint8_t lastqueued = 0;
74 struct queuebuf* data[NUM_PACKETS];
77 static struct ctimer ctimer;
81 static void (* receiver_callback)(
const struct mac_driver *);
82 static int id_counter = 0;
83 static int sent_counter = 0;
87 transmitter(
void *ptr)
89 clock_time_t now, rest, period_start, slot_start;
93 rest = now % PERIOD_LENGTH;
94 period_start = now - rest;
95 slot_start = period_start + MY_SLOT*SLOT_LENGTH;
98 if (now < slot_start ||
99 now > slot_start + SLOT_LENGTH - GUARD_PERIOD)
101 PRINTF(
"TIMER We are outside our slot: %u != [%u,%u]\n", now, slot_start, slot_start + SLOT_LENGTH);
102 while (now > slot_start + SLOT_LENGTH - GUARD_PERIOD)
104 slot_start += PERIOD_LENGTH;
107 PRINTF(
"TIMER Rescheduling until %u\n", slot_start);
113 while (nextsend != freeslot)
115 PRINTF(
"RADIO Transmitting packet #%i\n",
id[nextsend]);
117 queuebuf_dataptr(data[nextsend]),
118 queuebuf_datalen(data[nextsend])))
121 PRINTF(
"RADIO Transmit OK for #%i, total=%i\n",
id[nextsend], sent_counter);
122 DLEDS_TOGGLE(LEDS_GREEN);
126 PRINTF(
"RADIO Transmit failed for #%i, total=%i\n",
id[nextsend], sent_counter);
127 DLEDS_TOGGLE(LEDS_RED);
130 nextsend = (nextsend + 1) % NUM_PACKETS;
133 if (
clock_time() > slot_start + SLOT_LENGTH - GUARD_PERIOD)
135 PRINTF(
"TIMER No more time to transmit\n");
141 slot_start += PERIOD_LENGTH;
142 PRINTF(
"TIMER Rescheduling until %u\n", slot_start);
152 while (lastqueued != nextsend)
154 PRINTF(
"BUFFER Cleaning up packet #%i\n",
id[lastqueued]);
155 queuebuf_free(data[lastqueued]);
156 data[lastqueued] =
NULL;
158 lastqueued = (lastqueued + 1) % NUM_PACKETS;
161 if ((freeslot + 1) % NUM_PACKETS == lastqueued)
163 PRINTF(
"BUFFER Buffer full, dropping packet #%i\n", (id_counter+1));
168 data[freeslot] = queuebuf_new_from_packetbuf();
169 id[freeslot] = id_counter;
170 if (data[freeslot] ==
NULL)
172 PRINTF(
"BUFFER Queuebuffer full, dropping packet #%i\n",
id[freeslot]);
175 PRINTF(
"BUFFER Wrote packet #%i to buffer \n",
id[freeslot]);
177 freeslot = (freeslot + 1) % NUM_PACKETS;
181 PRINTF(
"TIMER Starting timer\n");
192 receiver_callback(&ctdma_mac_driver);
206 set_receive_function(
void (* recv)(
const struct mac_driver *))
208 receiver_callback = recv;
218 off(
int keep_radio_on)
231 for (i=0; i < NUM_PACKETS; i++)
237 radio->set_receive_function(
input);
239 return &ctdma_mac_driver;
247 set_receive_function,