mirror of
https://github.com/oliverschmidt/contiki.git
synced 2024-12-22 10:30:13 +00:00
68 lines
1.9 KiB
C
68 lines
1.9 KiB
C
#include "contiki.h"
|
|
#include "lib/random.h"
|
|
#include "sys/ctimer.h"
|
|
#include "sys/etimer.h"
|
|
#include "net/uip.h"
|
|
#include "net/uip-ds6.h"
|
|
|
|
#include "simple-udp.h"
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define UDP_PORT 1234
|
|
|
|
#define SEND_INTERVAL (10 * CLOCK_SECOND)
|
|
#define SEND_TIME (random_rand() % (SEND_INTERVAL))
|
|
|
|
static struct simple_udp_connection broadcast_connection;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
PROCESS(broadcast_example_process, "UDP broadcast example process");
|
|
AUTOSTART_PROCESSES(&broadcast_example_process);
|
|
/*---------------------------------------------------------------------------*/
|
|
static void
|
|
receiver(struct simple_udp_connection *c,
|
|
const uip_ipaddr_t *sender_addr,
|
|
uint16_t sender_port,
|
|
const uip_ipaddr_t *receiver_addr,
|
|
uint16_t receiver_port,
|
|
const uint8_t *data,
|
|
uint16_t datalen)
|
|
{
|
|
printf("Data received on port %d from port %d with length %d\n",
|
|
receiver_port, sender_port, datalen);
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
PROCESS_THREAD(broadcast_example_process, ev, data)
|
|
{
|
|
static struct etimer periodic_timer;
|
|
static struct etimer send_timer;
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
simple_udp_register(&broadcast_connection, UDP_PORT,
|
|
NULL, UDP_PORT,
|
|
receiver);
|
|
|
|
etimer_set(&periodic_timer, SEND_INTERVAL);
|
|
while(1) {
|
|
|
|
PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_TIMER);
|
|
if(data == &periodic_timer) {
|
|
etimer_reset(&periodic_timer);
|
|
etimer_set(&send_timer, SEND_TIME);
|
|
}
|
|
if(data == &send_timer) {
|
|
uip_ipaddr_t addr;
|
|
printf("Sending broadcast\n");
|
|
uip_create_linklocal_allnodes_mcast(&addr);
|
|
simple_udp_sendto(&broadcast_connection, "hej\n", 4, &addr);
|
|
}
|
|
}
|
|
|
|
PROCESS_END();
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|