2010-04-30 17:52:48 +02: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"
|
|
|
|
#include "contiki-lib.h"
|
|
|
|
#include "contiki-net.h"
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip.h"
|
2010-04-30 17:52:48 +02:00
|
|
|
#include "net/rpl/rpl.h"
|
|
|
|
|
|
|
|
#include "net/netstack.h"
|
|
|
|
#include "dev/button-sensor.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#define DEBUG DEBUG_PRINT
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-04-30 17:52:48 +02:00
|
|
|
|
|
|
|
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
|
|
|
|
|
|
|
#define UDP_CLIENT_PORT 8765
|
|
|
|
#define UDP_SERVER_PORT 5678
|
|
|
|
|
2010-06-15 21:02:00 +02:00
|
|
|
#define UDP_EXAMPLE_ID 190
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
static struct uip_udp_conn *server_conn;
|
|
|
|
|
|
|
|
PROCESS(udp_server_process, "UDP server process");
|
|
|
|
AUTOSTART_PROCESSES(&udp_server_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
tcpip_handler(void)
|
|
|
|
{
|
|
|
|
char *appdata;
|
|
|
|
|
|
|
|
if(uip_newdata()) {
|
|
|
|
appdata = (char *)uip_appdata;
|
|
|
|
appdata[uip_datalen()] = 0;
|
|
|
|
PRINTF("DATA recv '%s' from ", appdata);
|
2011-06-28 17:44:35 +02:00
|
|
|
PRINTF("%d",
|
|
|
|
UIP_IP_BUF->srcipaddr.u8[sizeof(UIP_IP_BUF->srcipaddr.u8) - 1]);
|
2010-04-30 17:52:48 +02:00
|
|
|
PRINTF("\n");
|
2010-05-21 15:45:50 +02:00
|
|
|
#if SERVER_REPLY
|
2011-08-09 07:02:16 +02:00
|
|
|
PRINTF("DATA sending reply\n");
|
2010-05-21 15:45:50 +02:00
|
|
|
uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr);
|
|
|
|
uip_udp_packet_send(server_conn, "Reply", sizeof("Reply"));
|
2010-05-24 17:43:18 +02:00
|
|
|
uip_create_unspecified(&server_conn->ripaddr);
|
2010-05-21 15:45:50 +02:00
|
|
|
#endif
|
2010-04-30 17:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
print_local_addresses(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
PRINTF("Server IPv6 addresses: ");
|
|
|
|
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
|
|
|
|
state = uip_ds6_if.addr_list[i].state;
|
|
|
|
if(state == ADDR_TENTATIVE || state == ADDR_PREFERRED) {
|
|
|
|
PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
|
|
|
|
PRINTF("\n");
|
|
|
|
/* hack to make address "final" */
|
|
|
|
if (state == ADDR_TENTATIVE) {
|
|
|
|
uip_ds6_if.addr_list[i].state = ADDR_PREFERRED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(udp_server_process, ev, data)
|
|
|
|
{
|
|
|
|
uip_ipaddr_t ipaddr;
|
|
|
|
struct uip_ds6_addr *root_if;
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
PROCESS_PAUSE();
|
|
|
|
|
|
|
|
SENSORS_ACTIVATE(button_sensor);
|
|
|
|
|
2015-08-20 17:13:46 +02:00
|
|
|
PRINTF("UDP server started. nbr:%d routes:%d\n",
|
|
|
|
NBR_TABLE_CONF_MAX_NEIGHBORS, UIP_CONF_MAX_ROUTES);
|
2010-04-30 17:52:48 +02:00
|
|
|
|
|
|
|
#if UIP_CONF_ROUTER
|
2011-03-08 20:15:55 +01:00
|
|
|
/* The choice of server address determines its 6LoPAN header compression.
|
|
|
|
* Obviously the choice made here must also be selected in udp-client.c.
|
|
|
|
*
|
|
|
|
* For correct Wireshark decoding using a sniffer, add the /64 prefix to the 6LowPAN protocol preferences,
|
2015-09-30 16:06:58 +02:00
|
|
|
* e.g. set Context 0 to fd00::. At present Wireshark copies Context/128 and then overwrites it.
|
|
|
|
* (Setting Context 0 to fd00::1111:2222:3333:4444 will report a 16 bit compressed address of fd00::1111:22ff:fe33:xxxx)
|
2011-03-08 20:15:55 +01:00
|
|
|
* Note Wireshark's IPCMV6 checksum verification depends on the correct uncompressed addresses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Mode 1 - 64 bits inline */
|
2015-09-30 16:06:58 +02:00
|
|
|
uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 1);
|
2011-03-08 20:15:55 +01:00
|
|
|
#elif 1
|
|
|
|
/* Mode 2 - 16 bits inline */
|
2015-09-30 16:06:58 +02:00
|
|
|
uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0x00ff, 0xfe00, 1);
|
2011-03-08 20:15:55 +01:00
|
|
|
#else
|
|
|
|
/* Mode 3 - derived from link local (MAC) address */
|
2015-09-30 16:06:58 +02:00
|
|
|
uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
|
2011-03-08 20:15:55 +01:00
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
#endif
|
|
|
|
|
2010-10-18 15:43:41 +02:00
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL);
|
2010-04-30 17:52:48 +02:00
|
|
|
root_if = uip_ds6_addr_lookup(&ipaddr);
|
|
|
|
if(root_if != NULL) {
|
2010-05-25 22:19:16 +02:00
|
|
|
rpl_dag_t *dag;
|
2011-07-11 15:50:51 +02:00
|
|
|
dag = rpl_set_root(RPL_DEFAULT_INSTANCE,(uip_ip6addr_t *)&ipaddr);
|
2015-09-30 16:06:58 +02:00
|
|
|
uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
|
2010-05-25 22:19:16 +02:00
|
|
|
rpl_set_prefix(dag, &ipaddr, 64);
|
2010-04-30 17:52:48 +02:00
|
|
|
PRINTF("created a new RPL dag\n");
|
|
|
|
} else {
|
|
|
|
PRINTF("failed to create a new RPL DAG\n");
|
|
|
|
}
|
|
|
|
#endif /* UIP_CONF_ROUTER */
|
|
|
|
|
|
|
|
print_local_addresses();
|
|
|
|
|
|
|
|
/* The data sink runs with a 100% duty cycle in order to ensure high
|
|
|
|
packet reception rates. */
|
|
|
|
NETSTACK_MAC.off(1);
|
|
|
|
|
2010-10-19 20:29:03 +02:00
|
|
|
server_conn = udp_new(NULL, UIP_HTONS(UDP_CLIENT_PORT), NULL);
|
2011-12-01 14:40:00 +01:00
|
|
|
if(server_conn == NULL) {
|
|
|
|
PRINTF("No UDP connection available, exiting the process!\n");
|
|
|
|
PROCESS_EXIT();
|
|
|
|
}
|
2010-10-19 20:29:03 +02:00
|
|
|
udp_bind(server_conn, UIP_HTONS(UDP_SERVER_PORT));
|
2010-04-30 17:52:48 +02:00
|
|
|
|
|
|
|
PRINTF("Created a server connection with remote address ");
|
|
|
|
PRINT6ADDR(&server_conn->ripaddr);
|
2010-10-19 20:29:03 +02:00
|
|
|
PRINTF(" local/remote port %u/%u\n", UIP_HTONS(server_conn->lport),
|
|
|
|
UIP_HTONS(server_conn->rport));
|
2010-04-30 17:52:48 +02:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PROCESS_YIELD();
|
|
|
|
if(ev == tcpip_event) {
|
|
|
|
tcpip_handler();
|
|
|
|
} else if (ev == sensors_event && data == &button_sensor) {
|
|
|
|
PRINTF("Initiaing global repair\n");
|
2011-07-11 15:50:51 +02:00
|
|
|
rpl_repair_root(RPL_DEFAULT_INSTANCE);
|
2010-04-30 17:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|