2010-04-30 15:52:48 +00:00
|
|
|
#include "contiki.h"
|
2017-12-09 08:53:48 +00:00
|
|
|
#include "net/routing/routing.h"
|
2017-10-30 20:46:45 +00:00
|
|
|
#include "random.h"
|
|
|
|
#include "net/netstack.h"
|
|
|
|
#include "net/ipv6/simple-udp.h"
|
2010-04-30 15:52:48 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
#include "sys/log.h"
|
|
|
|
#define LOG_MODULE "App"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_INFO
|
2010-06-15 19:02:00 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
#define WITH_SERVER_REPLY 1
|
|
|
|
#define UDP_CLIENT_PORT 8765
|
|
|
|
#define UDP_SERVER_PORT 5678
|
2010-04-30 15:52:48 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
#define SEND_INTERVAL (60 * CLOCK_SECOND)
|
2010-04-30 15:52:48 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
static struct simple_udp_connection udp_conn;
|
2010-10-18 13:43:41 +00:00
|
|
|
|
2010-04-30 15:52:48 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-30 20:46:45 +00:00
|
|
|
PROCESS(udp_client_process, "UDP client");
|
2010-04-30 15:52:48 +00:00
|
|
|
AUTOSTART_PROCESSES(&udp_client_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2017-10-30 20:46:45 +00:00
|
|
|
udp_rx_callback(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)
|
2010-04-30 15:52:48 +00:00
|
|
|
{
|
2018-10-30 15:58:45 +00:00
|
|
|
|
2018-10-23 05:50:15 +00:00
|
|
|
LOG_INFO("Received response '%.*s' from ", datalen, (char *) data);
|
2017-10-30 20:46:45 +00:00
|
|
|
LOG_INFO_6ADDR(sender_addr);
|
2018-10-30 15:58:45 +00:00
|
|
|
#if LLSEC802154_CONF_ENABLED
|
|
|
|
LOG_INFO_(" LLSEC LV:%d", uipbuf_get_attr(UIPBUF_ATTR_LLSEC_LEVEL));
|
|
|
|
#endif
|
2017-10-30 20:46:45 +00:00
|
|
|
LOG_INFO_("\n");
|
2018-10-30 15:58:45 +00:00
|
|
|
|
2010-04-30 15:52:48 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(udp_client_process, ev, data)
|
|
|
|
{
|
2017-10-30 20:46:45 +00:00
|
|
|
static struct etimer periodic_timer;
|
|
|
|
static unsigned count;
|
2018-10-20 20:27:02 +00:00
|
|
|
static char str[32];
|
2017-12-10 20:17:42 +00:00
|
|
|
uip_ipaddr_t dest_ipaddr;
|
2010-04-30 15:52:48 +00:00
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
/* Initialize UDP connection */
|
|
|
|
simple_udp_register(&udp_conn, UDP_CLIENT_PORT, NULL,
|
|
|
|
UDP_SERVER_PORT, udp_rx_callback);
|
2015-08-20 15:13:46 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
etimer_set(&periodic_timer, random_rand() % SEND_INTERVAL);
|
2010-04-30 15:52:48 +00:00
|
|
|
while(1) {
|
2017-10-30 20:46:45 +00:00
|
|
|
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
|
|
|
|
|
2017-12-10 20:17:42 +00:00
|
|
|
if(NETSTACK_ROUTING.node_is_reachable() && NETSTACK_ROUTING.get_root_ipaddr(&dest_ipaddr)) {
|
2017-10-31 09:00:45 +00:00
|
|
|
/* Send to DAG root */
|
2017-12-10 20:17:42 +00:00
|
|
|
LOG_INFO("Sending request %u to ", count);
|
|
|
|
LOG_INFO_6ADDR(&dest_ipaddr);
|
|
|
|
LOG_INFO_("\n");
|
2018-10-30 15:58:45 +00:00
|
|
|
snprintf(str, sizeof(str), "hello %d", count);
|
2018-10-20 20:27:02 +00:00
|
|
|
simple_udp_sendto(&udp_conn, str, strlen(str), &dest_ipaddr);
|
2017-12-10 20:17:42 +00:00
|
|
|
count++;
|
2017-10-30 20:46:45 +00:00
|
|
|
} else {
|
2017-12-10 20:17:42 +00:00
|
|
|
LOG_INFO("Not reachable yet\n");
|
2010-04-30 15:52:48 +00:00
|
|
|
}
|
2015-08-20 15:13:46 +00:00
|
|
|
|
2017-10-31 09:00:45 +00:00
|
|
|
/* Add some jitter */
|
|
|
|
etimer_set(&periodic_timer, SEND_INTERVAL
|
|
|
|
- CLOCK_SECOND + (random_rand() % (2 * CLOCK_SECOND)));
|
2010-04-30 15:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|