
#include "contiki.h"
#include "net/rime.h"
#include "random.h"
#include "dev/button-sensor.h"
#include <stdlib.h>
#include "dev/leds.h"
#include <string.h>
#include <stdio.h>

PROCESS(example_broadcast_process, "[Exemple2] Exemple de diffusion");
AUTOSTART_PROCESSES(&example_broadcast_process);

static void broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from)
{
  printf("[Exemple2] Message recu depuis %d.%d: ’%s’\n",
         from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
}
static const struct broadcast_callbacks broadcast_call = {broadcast_recv};
static struct broadcast_conn broadcast;

PROCESS_THREAD(example_broadcast_process, ev, data)
{
  static struct etimer et;
  static int cpt = 0;
  PROCESS_EXITHANDLER(broadcast_close(&broadcast);)
  PROCESS_BEGIN();

  broadcast_open(&broadcast, 129, &broadcast_call);
  while (1)
  {
    cpt++;
    /* Delay 2-4 seconds */
    etimer_set(&et, CLOCK_SECOND * 8 + random_rand() % (CLOCK_SECOND * 8));
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
    packetbuf_copyfrom("Bonjour", 7);
    broadcast_send(&broadcast);
    printf("[Exemple2] Message envoye en diffusion %d\n", cpt);
  }
  PROCESS_END();
}
