2010-04-30 15:52:48 +00:00
|
|
|
/*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
2017-12-09 08:53:48 +00:00
|
|
|
#include "net/routing/routing.h"
|
2010-04-30 15:52:48 +00:00
|
|
|
#include "net/netstack.h"
|
2017-10-30 20:46:45 +00:00
|
|
|
#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-04-30 15:52:48 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
#define WITH_SERVER_REPLY 1
|
2010-04-30 15:52:48 +00:00
|
|
|
#define UDP_CLIENT_PORT 8765
|
|
|
|
#define UDP_SERVER_PORT 5678
|
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
static struct simple_udp_connection udp_conn;
|
2010-04-30 15:52:48 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
PROCESS(udp_server_process, "UDP server");
|
2010-04-30 15:52:48 +00:00
|
|
|
AUTOSTART_PROCESSES(&udp_server_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-23 05:50:15 +00:00
|
|
|
LOG_INFO("Received request '%.*s' from ", datalen, (char *) data);
|
2017-10-30 20:46:45 +00:00
|
|
|
LOG_INFO_6ADDR(sender_addr);
|
|
|
|
LOG_INFO_("\n");
|
|
|
|
#if WITH_SERVER_REPLY
|
2018-10-23 05:50:15 +00:00
|
|
|
/* send back the same string to the client as an echo reply */
|
|
|
|
LOG_INFO("Sending response.\n");
|
|
|
|
simple_udp_sendto(&udp_conn, data, datalen, sender_addr);
|
2017-10-30 20:46:45 +00:00
|
|
|
#endif /* WITH_SERVER_REPLY */
|
2010-04-30 15:52:48 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(udp_server_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
/* Initialize DAG root */
|
2017-12-09 13:23:35 +00:00
|
|
|
NETSTACK_ROUTING.root_start();
|
2010-04-30 15:52:48 +00:00
|
|
|
|
2017-10-30 20:46:45 +00:00
|
|
|
/* Initialize UDP connection */
|
|
|
|
simple_udp_register(&udp_conn, UDP_SERVER_PORT, NULL,
|
|
|
|
UDP_CLIENT_PORT, udp_rx_callback);
|
2010-04-30 15:52:48 +00:00
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|