Added an example of using UDP over an RPL-enabled IPv6 network
This commit is contained in:
parent
d529132002
commit
d751238747
4 changed files with 989 additions and 0 deletions
176
examples/ipv6/rpl-udp/udp-client.c
Normal file
176
examples/ipv6/rpl-udp/udp-client.c
Normal file
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* 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"
|
||||
#include "net/rime/ctimer.h"
|
||||
#include "net/uip.h"
|
||||
#include "net/uip-ds6.h"
|
||||
#include "net/uip-udp-packet.h"
|
||||
#include "node-id.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef SERVER_ID
|
||||
#define SERVER_ID 1
|
||||
#endif
|
||||
|
||||
#define UDP_CLIENT_PORT 8765
|
||||
#define UDP_SERVER_PORT 5678
|
||||
|
||||
#define DEBUG DEBUG_PRINT
|
||||
#include "net/uip-debug.h"
|
||||
|
||||
#define START_INTERVAL (15 * CLOCK_SECOND)
|
||||
#define SEND_INTERVAL (60 * CLOCK_SECOND)
|
||||
#define SEND_TIME (random_rand() % (SEND_INTERVAL))
|
||||
#define MAX_PAYLOAD_LEN 30
|
||||
|
||||
static struct uip_udp_conn *client_conn;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(udp_client_process, "UDP client process");
|
||||
AUTOSTART_PROCESSES(&udp_client_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
tcpip_handler(void)
|
||||
{
|
||||
char *str;
|
||||
|
||||
if(uip_newdata()) {
|
||||
str = uip_appdata;
|
||||
str[uip_datalen()] = '\0';
|
||||
printf("DATA recv '%s'\n", str);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
send_packet(void *ptr)
|
||||
{
|
||||
static int seq_id;
|
||||
char buf[MAX_PAYLOAD_LEN];
|
||||
|
||||
seq_id++;
|
||||
PRINTF("DATA send to %d 'Hello %d'\n",
|
||||
client_conn->ripaddr.u8[15], seq_id);
|
||||
sprintf(buf, "Hello %d from the client", seq_id);
|
||||
uip_udp_packet_send(client_conn, buf, strlen(buf));
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
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);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
set_connection_address(uip_ipaddr_t *ipaddr)
|
||||
{
|
||||
#ifdef IEEE_802154_MAC_ADDRESS
|
||||
/* Node 1 in the testbed scripts */
|
||||
uint8_t ieee[] = IEEE_802154_MAC_ADDRESS;
|
||||
ieee[7] = SERVER_ID;
|
||||
uip_ip6addr(ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
|
||||
uip_ds6_set_addr_iid(ipaddr, (uip_lladdr_t *) ieee);
|
||||
#else
|
||||
uip_ip6addr(ipaddr,0xaaaa,0,0,0,0x0212,0x7400 + SERVER_ID,
|
||||
SERVER_ID,SERVER_ID << 8 | SERVER_ID);
|
||||
#endif
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(udp_client_process, ev, data)
|
||||
{
|
||||
static struct etimer periodic;
|
||||
static struct ctimer backoff_timer;
|
||||
uip_ipaddr_t ipaddr;
|
||||
|
||||
PROCESS_BEGIN();
|
||||
|
||||
PROCESS_PAUSE();
|
||||
|
||||
PRINTF("UDP client process started\n");
|
||||
|
||||
#if UIP_CONF_ROUTER
|
||||
set_global_address();
|
||||
#endif
|
||||
|
||||
print_local_addresses();
|
||||
|
||||
set_connection_address(&ipaddr);
|
||||
|
||||
/* new connection with remote host */
|
||||
client_conn = udp_new(&ipaddr, HTONS(UDP_SERVER_PORT), NULL);
|
||||
udp_bind(client_conn, HTONS(UDP_CLIENT_PORT));
|
||||
|
||||
PRINTF("Created a connection with the server ");
|
||||
PRINT6ADDR(&client_conn->ripaddr);
|
||||
PRINTF(" local/remote port %u/%u\n",
|
||||
HTONS(client_conn->lport), HTONS(client_conn->rport));
|
||||
|
||||
etimer_set(&periodic, SEND_INTERVAL);
|
||||
while(1) {
|
||||
PROCESS_YIELD();
|
||||
if(ev == tcpip_event) {
|
||||
tcpip_handler();
|
||||
}
|
||||
|
||||
if(etimer_expired(&periodic)) {
|
||||
etimer_reset(&periodic);
|
||||
ctimer_set(&backoff_timer, SEND_TIME, send_packet, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
Loading…
Add table
Add a link
Reference in a new issue