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 "lib/random.h"
|
2010-06-14 20:58:45 +02:00
|
|
|
#include "sys/ctimer.h"
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip.h"
|
|
|
|
#include "net/ipv6/uip-ds6.h"
|
|
|
|
#include "net/ip/uip-udp-packet.h"
|
2010-06-14 18:19:45 +02:00
|
|
|
#include "sys/ctimer.h"
|
2011-10-13 15:09:40 +02:00
|
|
|
#ifdef WITH_COMPOWER
|
|
|
|
#include "powertrace.h"
|
|
|
|
#endif
|
2010-04-30 17:52:48 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-08-20 17:13:46 +02:00
|
|
|
/* Only for TMOTE Sky? */
|
|
|
|
#include "dev/serial-line.h"
|
|
|
|
#include "dev/uart1.h"
|
|
|
|
#include "net/ipv6/uip-ds6-route.h"
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
#define UDP_CLIENT_PORT 8765
|
|
|
|
#define UDP_SERVER_PORT 5678
|
|
|
|
|
2010-06-15 21:02:00 +02:00
|
|
|
#define UDP_EXAMPLE_ID 190
|
|
|
|
|
2015-08-20 17:13:46 +02:00
|
|
|
#define DEBUG DEBUG_FULL
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-04-30 17:52:48 +02:00
|
|
|
|
2011-08-09 07:02:16 +02:00
|
|
|
#ifndef PERIOD
|
|
|
|
#define PERIOD 60
|
|
|
|
#endif
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
#define START_INTERVAL (15 * CLOCK_SECOND)
|
2011-08-09 07:02:16 +02:00
|
|
|
#define SEND_INTERVAL (PERIOD * CLOCK_SECOND)
|
2010-04-30 17:52:48 +02:00
|
|
|
#define SEND_TIME (random_rand() % (SEND_INTERVAL))
|
|
|
|
#define MAX_PAYLOAD_LEN 30
|
|
|
|
|
|
|
|
static struct uip_udp_conn *client_conn;
|
2010-10-18 15:43:41 +02:00
|
|
|
static uip_ipaddr_t server_ipaddr;
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS(udp_client_process, "UDP client process");
|
|
|
|
AUTOSTART_PROCESSES(&udp_client_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-08-20 17:13:46 +02:00
|
|
|
static int seq_id;
|
|
|
|
static int reply;
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
static void
|
|
|
|
tcpip_handler(void)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if(uip_newdata()) {
|
|
|
|
str = uip_appdata;
|
|
|
|
str[uip_datalen()] = '\0';
|
2015-08-20 17:13:46 +02:00
|
|
|
reply++;
|
|
|
|
printf("DATA recv '%s' (s:%d, r:%d)\n", str, seq_id, reply);
|
2010-04-30 17:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
send_packet(void *ptr)
|
|
|
|
{
|
|
|
|
char buf[MAX_PAYLOAD_LEN];
|
2010-10-18 15:43:41 +02:00
|
|
|
|
2015-08-20 17:13:46 +02:00
|
|
|
#ifdef SERVER_REPLY
|
|
|
|
uint8_t num_used = 0;
|
|
|
|
uip_ds6_nbr_t *nbr;
|
|
|
|
|
|
|
|
nbr = nbr_table_head(ds6_neighbors);
|
|
|
|
while(nbr != NULL) {
|
|
|
|
nbr = nbr_table_next(ds6_neighbors, nbr);
|
|
|
|
num_used++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(seq_id > 0) {
|
|
|
|
ANNOTATE("#A r=%d/%d,color=%s,n=%d %d\n", reply, seq_id,
|
|
|
|
reply == seq_id ? "GREEN" : "RED", uip_ds6_route_num_routes(), num_used);
|
|
|
|
}
|
|
|
|
#endif /* SERVER_REPLY */
|
|
|
|
|
2010-10-18 15:43:41 +02:00
|
|
|
seq_id++;
|
|
|
|
PRINTF("DATA send to %d 'Hello %d'\n",
|
2011-06-28 17:44:35 +02:00
|
|
|
server_ipaddr.u8[sizeof(server_ipaddr.u8) - 1], seq_id);
|
2010-10-18 15:43:41 +02:00
|
|
|
sprintf(buf, "Hello %d from the client", seq_id);
|
|
|
|
uip_udp_packet_sendto(client_conn, buf, strlen(buf),
|
2010-10-19 20:29:03 +02:00
|
|
|
&server_ipaddr, UIP_HTONS(UDP_SERVER_PORT));
|
2010-04-30 17:52:48 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
print_local_addresses(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
PRINTF("Client IPv6 addresses: ");
|
|
|
|
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
|
|
|
|
state = uip_ds6_if.addr_list[i].state;
|
|
|
|
if(uip_ds6_if.addr_list[i].isused &&
|
|
|
|
(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set_global_address(void)
|
|
|
|
{
|
|
|
|
uip_ipaddr_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-04-30 17:52:48 +02:00
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
|
2010-10-18 15:43:41 +02:00
|
|
|
|
2011-03-08 20:15:55 +01:00
|
|
|
/* The choice of server address determines its 6LoPAN header compression.
|
|
|
|
* (Our address will be compressed Mode 3 since it is derived from our link-local address)
|
|
|
|
* Obviously the choice made here must also be selected in udp-server.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 the 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(&server_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(&server_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 server link-local (MAC) address */
|
2015-09-30 16:06:58 +02:00
|
|
|
uip_ip6addr(&server_ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0x0250, 0xc2ff, 0xfea8, 0xcd1a); //redbee-econotag
|
2011-03-08 20:15:55 +01:00
|
|
|
#endif
|
2010-04-30 17:52:48 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(udp_client_process, ev, data)
|
|
|
|
{
|
|
|
|
static struct etimer periodic;
|
|
|
|
static struct ctimer backoff_timer;
|
2012-03-14 15:20:02 +01:00
|
|
|
#if WITH_COMPOWER
|
2011-10-13 15:09:40 +02:00
|
|
|
static int print = 0;
|
2012-03-14 15:20:02 +01:00
|
|
|
#endif
|
2010-04-30 17:52:48 +02:00
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
PROCESS_PAUSE();
|
|
|
|
|
2010-06-15 21:02:00 +02:00
|
|
|
set_global_address();
|
2015-08-20 17:13:46 +02:00
|
|
|
|
|
|
|
PRINTF("UDP client process started nbr:%d routes:%d\n",
|
|
|
|
NBR_TABLE_CONF_MAX_NEIGHBORS, UIP_CONF_MAX_ROUTES);
|
2010-04-30 17:52:48 +02:00
|
|
|
|
|
|
|
print_local_addresses();
|
|
|
|
|
|
|
|
/* new connection with remote host */
|
2010-10-19 20:29:03 +02:00
|
|
|
client_conn = udp_new(NULL, UIP_HTONS(UDP_SERVER_PORT), NULL);
|
2011-12-01 14:40:00 +01:00
|
|
|
if(client_conn == NULL) {
|
|
|
|
PRINTF("No UDP connection available, exiting the process!\n");
|
|
|
|
PROCESS_EXIT();
|
|
|
|
}
|
2010-10-19 20:29:03 +02:00
|
|
|
udp_bind(client_conn, UIP_HTONS(UDP_CLIENT_PORT));
|
2010-04-30 17:52:48 +02:00
|
|
|
|
|
|
|
PRINTF("Created a connection with the server ");
|
|
|
|
PRINT6ADDR(&client_conn->ripaddr);
|
|
|
|
PRINTF(" local/remote port %u/%u\n",
|
2010-10-19 20:29:03 +02:00
|
|
|
UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
|
2010-04-30 17:52:48 +02:00
|
|
|
|
2015-08-20 17:13:46 +02:00
|
|
|
/* initialize serial line */
|
|
|
|
uart1_set_input(serial_line_input_byte);
|
|
|
|
serial_line_init();
|
|
|
|
|
|
|
|
|
2011-10-13 15:09:40 +02:00
|
|
|
#if WITH_COMPOWER
|
|
|
|
powertrace_sniff(POWERTRACE_ON);
|
|
|
|
#endif
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
etimer_set(&periodic, SEND_INTERVAL);
|
|
|
|
while(1) {
|
|
|
|
PROCESS_YIELD();
|
|
|
|
if(ev == tcpip_event) {
|
|
|
|
tcpip_handler();
|
|
|
|
}
|
2015-08-20 17:13:46 +02:00
|
|
|
|
|
|
|
if(ev == serial_line_event_message && data != NULL) {
|
|
|
|
char *str;
|
|
|
|
str = data;
|
|
|
|
if(str[0] == 'r') {
|
|
|
|
uip_ds6_route_t *r;
|
|
|
|
uip_ipaddr_t *nexthop;
|
|
|
|
uip_ds6_defrt_t *defrt;
|
|
|
|
uip_ipaddr_t *ipaddr;
|
|
|
|
defrt = NULL;
|
|
|
|
if((ipaddr = uip_ds6_defrt_choose()) != NULL) {
|
|
|
|
defrt = uip_ds6_defrt_lookup(ipaddr);
|
|
|
|
}
|
|
|
|
if(defrt != NULL) {
|
|
|
|
PRINTF("DefRT: :: -> %02d", defrt->ipaddr.u8[15]);
|
|
|
|
PRINTF(" lt:%lu inf:%d\n", stimer_remaining(&defrt->lifetime),
|
|
|
|
defrt->isinfinite);
|
|
|
|
} else {
|
|
|
|
PRINTF("DefRT: :: -> NULL\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for(r = uip_ds6_route_head();
|
|
|
|
r != NULL;
|
|
|
|
r = uip_ds6_route_next(r)) {
|
|
|
|
nexthop = uip_ds6_route_nexthop(r);
|
|
|
|
PRINTF("Route: %02d -> %02d", r->ipaddr.u8[15], nexthop->u8[15]);
|
|
|
|
/* PRINT6ADDR(&r->ipaddr); */
|
|
|
|
/* PRINTF(" -> "); */
|
|
|
|
/* PRINT6ADDR(nexthop); */
|
|
|
|
PRINTF(" lt:%lu\n", r->state.lifetime);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
if(etimer_expired(&periodic)) {
|
|
|
|
etimer_reset(&periodic);
|
|
|
|
ctimer_set(&backoff_timer, SEND_TIME, send_packet, NULL);
|
2011-10-13 15:09:40 +02:00
|
|
|
|
|
|
|
#if WITH_COMPOWER
|
|
|
|
if (print == 0) {
|
|
|
|
powertrace_print("#P");
|
|
|
|
}
|
|
|
|
if (++print == 3) {
|
|
|
|
print = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-30 17:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|