#include __USING_SYS #define N 4 #define T 300 #define K 0 #define RESYNCH_THRESH 20 typedef struct PDU { unsigned short ib[N]; unsigned short ialvo; }; PDU* pdu_sent; PDU* pdu_rcvd; int round = N * T; int prev; int timer; int src; void get_src() { for (int i = 0; i < N; i++) if (pdu_rcvd->ib[i] == 0xFFFF) { src = i; return; } } void receive() { int rssi; nic.receive(..., received, &rssi, &timer); get_src(); if (src >= 0) pdu_sent->ib[src] = rssi; else { pdu_sent->ialvo = rssi; led_alvo_on(); } } void send() { nic.send(..., pdu_sent); led_alvo_off(); led_send_turn(); } int main() { pdu_sent = (PDU*) malloc(sizeof(PDU)); pdu_sent->ib[K] = 0xFFFF; pdu_rcvd = (PDU*) malloc(sizeof(PDU)); prev = K - 1; if (prev < 0) // set the previous beacon prev += N; if (K > 0) { synch(); // wait for the previous beacon to send timer = T; } else { send(); // beacon 0 doesn't wait timer = round; } while(1) { // entering permanent state receive(); if (timer == 0) { // timeout send(); timer = round; } else if (src == prev && abs(timer - T) > RESYNCH_THRESH) resynch(); } }