2010-11-03 16:36:38 +01: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"
|
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-11-03 16:36:38 +01:00
|
|
|
#include "net/rpl/rpl.h"
|
|
|
|
#include "dev/serial-line.h"
|
|
|
|
#if CONTIKI_TARGET_Z1
|
|
|
|
#include "dev/uart0.h"
|
|
|
|
#else
|
|
|
|
#include "dev/uart1.h"
|
|
|
|
#endif
|
|
|
|
#include "collect-common.h"
|
|
|
|
#include "collect-view.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define UDP_CLIENT_PORT 8775
|
|
|
|
#define UDP_SERVER_PORT 5688
|
|
|
|
|
|
|
|
#define DEBUG DEBUG_PRINT
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-11-03 16:36:38 +01:00
|
|
|
|
|
|
|
static struct uip_udp_conn *client_conn;
|
|
|
|
static uip_ipaddr_t server_ipaddr;
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS(udp_client_process, "UDP client process");
|
|
|
|
AUTOSTART_PROCESSES(&udp_client_process, &collect_common_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
collect_common_set_sink(void)
|
|
|
|
{
|
|
|
|
/* A udp client can never become sink */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void
|
|
|
|
collect_common_net_print(void)
|
|
|
|
{
|
|
|
|
rpl_dag_t *dag;
|
2012-11-26 21:30:01 +01:00
|
|
|
uip_ds6_route_t *r;
|
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
/* Let's suppose we have only one instance */
|
|
|
|
dag = rpl_get_any_dag();
|
2010-11-03 16:36:38 +01:00
|
|
|
if(dag->preferred_parent != NULL) {
|
|
|
|
PRINTF("Preferred parent: ");
|
2013-07-03 20:26:54 +02:00
|
|
|
PRINT6ADDR(rpl_get_parent_ipaddr(dag->preferred_parent));
|
2010-11-03 16:36:38 +01:00
|
|
|
PRINTF("\n");
|
|
|
|
}
|
2013-07-03 20:26:54 +02:00
|
|
|
for(r = uip_ds6_route_head();
|
2012-11-26 21:30:01 +01:00
|
|
|
r != NULL;
|
2013-07-03 20:26:54 +02:00
|
|
|
r = uip_ds6_route_next(r)) {
|
2012-11-26 21:30:01 +01:00
|
|
|
PRINT6ADDR(&r->ipaddr);
|
2010-11-03 16:36:38 +01:00
|
|
|
}
|
|
|
|
PRINTF("---\n");
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
tcpip_handler(void)
|
|
|
|
{
|
|
|
|
if(uip_newdata()) {
|
|
|
|
/* Ignore incoming data */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
collect_common_send(void)
|
|
|
|
{
|
|
|
|
static uint8_t seqno;
|
|
|
|
struct {
|
|
|
|
uint8_t seqno;
|
|
|
|
uint8_t for_alignment;
|
|
|
|
struct collect_view_data_msg msg;
|
|
|
|
} msg;
|
|
|
|
/* struct collect_neighbor *n; */
|
|
|
|
uint16_t parent_etx;
|
|
|
|
uint16_t rtmetric;
|
|
|
|
uint16_t num_neighbors;
|
|
|
|
uint16_t beacon_interval;
|
|
|
|
rpl_parent_t *preferred_parent;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t parent;
|
2010-11-03 16:36:38 +01:00
|
|
|
rpl_dag_t *dag;
|
|
|
|
|
|
|
|
if(client_conn == NULL) {
|
|
|
|
/* Not setup yet */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
seqno++;
|
|
|
|
if(seqno == 0) {
|
|
|
|
/* Wrap to 128 to identify restarts */
|
|
|
|
seqno = 128;
|
|
|
|
}
|
|
|
|
msg.seqno = seqno;
|
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&parent, &linkaddr_null);
|
2010-11-03 16:36:38 +01:00
|
|
|
parent_etx = 0;
|
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
/* Let's suppose we have only one instance */
|
|
|
|
dag = rpl_get_any_dag();
|
2010-11-03 16:36:38 +01:00
|
|
|
if(dag != NULL) {
|
|
|
|
preferred_parent = dag->preferred_parent;
|
|
|
|
if(preferred_parent != NULL) {
|
|
|
|
uip_ds6_nbr_t *nbr;
|
2013-07-03 20:26:54 +02:00
|
|
|
nbr = uip_ds6_nbr_lookup(rpl_get_parent_ipaddr(preferred_parent));
|
2010-11-03 16:36:38 +01:00
|
|
|
if(nbr != NULL) {
|
2011-07-29 20:26:57 +02:00
|
|
|
/* Use parts of the IPv6 address as the parent address, in reversed byte order. */
|
2013-12-12 23:58:52 +01:00
|
|
|
parent.u8[LINKADDR_SIZE - 1] = nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 2];
|
|
|
|
parent.u8[LINKADDR_SIZE - 2] = nbr->ipaddr.u8[sizeof(uip_ipaddr_t) - 1];
|
2015-05-06 16:56:19 +02:00
|
|
|
parent_etx = rpl_get_parent_rank((uip_lladdr_t *) uip_ds6_nbr_get_ll(nbr)) / 2;
|
2010-11-03 16:36:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rtmetric = dag->rank;
|
2011-12-09 17:15:33 +01:00
|
|
|
beacon_interval = (uint16_t) ((2L << dag->instance->dio_intcurrent) / 1000);
|
2013-11-20 15:14:34 +01:00
|
|
|
num_neighbors = uip_ds6_nbr_num();
|
2010-11-03 16:36:38 +01:00
|
|
|
} else {
|
|
|
|
rtmetric = 0;
|
|
|
|
beacon_interval = 0;
|
|
|
|
num_neighbors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* num_neighbors = collect_neighbor_list_num(&tc.neighbor_list); */
|
|
|
|
collect_view_construct_message(&msg.msg, &parent,
|
|
|
|
parent_etx, rtmetric,
|
|
|
|
num_neighbors, beacon_interval);
|
|
|
|
|
|
|
|
uip_udp_packet_sendto(client_conn, &msg, sizeof(msg),
|
|
|
|
&server_ipaddr, UIP_HTONS(UDP_SERVER_PORT));
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
collect_common_net_init(void)
|
|
|
|
{
|
|
|
|
#if CONTIKI_TARGET_Z1
|
|
|
|
uart0_set_input(serial_line_input_byte);
|
|
|
|
#else
|
|
|
|
uart1_set_input(serial_line_input_byte);
|
|
|
|
#endif
|
|
|
|
serial_line_init();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
|
|
|
|
|
|
|
|
/* set server address */
|
|
|
|
uip_ip6addr(&server_ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(udp_client_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
PROCESS_PAUSE();
|
|
|
|
|
|
|
|
set_global_address();
|
|
|
|
|
|
|
|
PRINTF("UDP client process started\n");
|
|
|
|
|
|
|
|
print_local_addresses();
|
|
|
|
|
|
|
|
/* new connection with remote host */
|
|
|
|
client_conn = udp_new(NULL, UIP_HTONS(UDP_SERVER_PORT), NULL);
|
|
|
|
udp_bind(client_conn, UIP_HTONS(UDP_CLIENT_PORT));
|
|
|
|
|
|
|
|
PRINTF("Created a connection with the server ");
|
|
|
|
PRINT6ADDR(&client_conn->ripaddr);
|
|
|
|
PRINTF(" local/remote port %u/%u\n",
|
|
|
|
UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PROCESS_YIELD();
|
|
|
|
if(ev == tcpip_event) {
|
|
|
|
tcpip_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|