Example code for the simple UDP API
This commit is contained in:
parent
8faaaa2c83
commit
b3f3163661
4 changed files with 289 additions and 0 deletions
67
examples/ipv6/simple-udp-rpl/broadcast-example.c
Normal file
67
examples/ipv6/simple-udp-rpl/broadcast-example.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#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();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
Loading…
Add table
Add a link
Reference in a new issue