example-runicast.c
#include <stdio.h>
#include "contiki.h"
#include "net/rime.h"
#include "lib/list.h"
#include "lib/memb.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#define MAX_RETRANSMISSIONS 4
#define NUM_HISTORY_ENTRIES 4
PROCESS(test_runicast_process, "runicast test");
AUTOSTART_PROCESSES(&test_runicast_process);
struct history_entry {
struct history_entry *next;
rimeaddr_t addr;
uint8_t seq;
};
LIST(history_table);
MEMB(history_mem, struct history_entry, NUM_HISTORY_ENTRIES);
static void
recv_runicast(struct runicast_conn *c, const rimeaddr_t *from, uint8_t seqno)
{
struct history_entry *e = NULL;
for(e = list_head(history_table); e != NULL; e = e->next) {
if(rimeaddr_cmp(&e->addr, from)) {
break;
}
}
if(e == NULL) {
e = memb_alloc(&history_mem);
if(e == NULL) {
e = list_chop(history_table);
}
rimeaddr_copy(&e->addr, from);
e->seq = seqno;
list_push(history_table, e);
} else {
if(e->seq == seqno) {
printf("runicast message received from %d.%d, seqno %d (DUPLICATE)\n",
from->u8[0], from->u8[1], seqno);
return;
}
e->seq = seqno;
}
printf("runicast message received from %d.%d, seqno %d\n",
from->u8[0], from->u8[1], seqno);
}
static void
sent_runicast(struct runicast_conn *c, const rimeaddr_t *to, uint8_t retransmissions)
{
printf("runicast message sent to %d.%d, retransmissions %d\n",
to->u8[0], to->u8[1], retransmissions);
}
static void
timedout_runicast(struct runicast_conn *c, const rimeaddr_t *to, uint8_t retransmissions)
{
printf("runicast message timed out when sending to %d.%d, retransmissions %d\n",
to->u8[0], to->u8[1], retransmissions);
}
static const struct runicast_callbacks runicast_callbacks = {recv_runicast,
sent_runicast,
timedout_runicast};
static struct runicast_conn runicast;
PROCESS_THREAD(test_runicast_process, ev, data)
{
PROCESS_EXITHANDLER(runicast_close(&runicast);)
PROCESS_BEGIN();
runicast_open(&runicast, 144, &runicast_callbacks);
list_init(history_table);
memb_init(&history_mem);
if(rimeaddr_node_addr.u8[0] == 1 &&
rimeaddr_node_addr.u8[1] == 0) {
PROCESS_WAIT_EVENT_UNTIL(0);
}
while(1) {
static struct etimer et;
etimer_set(&et, 10*CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
if(!runicast_is_transmitting(&runicast)) {
rimeaddr_t recv;
packetbuf_copyfrom("Hello", 5);
recv.u8[0] = 1;
recv.u8[1] = 0;
printf("%u.%u: sending runicast to address %u.%u\n",
rimeaddr_node_addr.u8[0],
rimeaddr_node_addr.u8[1],
recv.u8[0],
recv.u8[1]);
runicast_send(&runicast, &recv, MAX_RETRANSMISSIONS);
}
}
PROCESS_END();
}