/* * Copyright (C) 2015 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level * directory for more details. */ /** * @ingroup examples * @{ * * @file * @brief Example application for demonstrating the RIOT network stack * * @author Hauke Petersen * * @} */ #include #include #include #include #include #include #include #include "board.h" #include "periph_conf.h" #include "periph/gpio.h" #include "net/sock/udp.h" #include "net/gnrc/ipv6.h" #include "net/af.h" #include "net/sixlowpan.h" #include "net/gnrc/pktdump.h" #include "shell.h" #include "shell_commands.h" #include "msg.h" #include "thread.h" #include "sched.h" #include "kernel_types.h" #include "net/netstats.h" #include "net/ipv6/addr.h" #include "periph/timer.h" #include "net/gnrc/ipv6/netif.h" #include "net/gnrc/netif.h" #include "net/gnrc/netapi.h" #include "net/netopt.h" #include "net/gnrc/pkt.h" #include "net/gnrc/pktbuf.h" #include "net/gnrc/netif/hdr.h" #include "net/gnrc/sixlowpan/netif.h" #include "net/fib.h" #include "net/gnrc/udp.h" #include "periph/pwm.h" #include "od.h" #include "net/sntp.h" #include "net/ntp_packet.h" #ifdef MODULE_SCHEDSTATISTICS #include "xtimer.h" #endif #ifdef MODULE_TLSF #include "tlsf.h" #endif #define MAIN_QUEUE_SIZE (8) /** * @brief The maximal expected link layer address length in byte */ #define MAX_ADDR_LEN (8U) /** * @brief The default IPv6 prefix length if not specified. */ #define SC_NETIF_IPV6_DEFAULT_PREFIX_LEN (64) #define _STACKSIZE (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF) #define MSG_TYPE_ISR (0x3456) #define MAXPAC 100 #define MAXLEN 4096 #define ECHOLEN 2048 // addr ipv6 link local node 1: fe80::1210:642c:1432:1702 uint8_t node1[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x10,0x64,0x2c,0x14,0x32,0x17,0x02}; // addr ipv6 link local node 2: fe80::1210:6432:140f:1732 uint8_t node2[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x10,0x64,0x32,0x14,0x0f,0x17,0x32}; //addr ipv6 link local node 3: fe80::1210:642d:1439:1736 uint8_t node3[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x10,0x64,0x2d,0x14,0x39,0x17,0x36}; char sock_server_stack[THREAD_STACKSIZE_MAIN]; char sock_client_stack[THREAD_STACKSIZE_MAIN]; kernel_pid_t server, client; sock_udp_ep_t local = SOCK_IPV6_EP_ANY; sock_udp_t sock; uint32_t start, end; char rbuf[MAXLEN], sbuf[MAXLEN], ack[MAXLEN]; // calculate the time diff between start and end uint32_t delay(uint32_t t1, uint32_t t2) { uint32_t d; d = (t2 - t1); return(d/1000); //temps in ms } // calculate the min/max/average value from an array void rttd(uint32_t t[], int size) { uint32_t avg; uint32_t min = t[0]; uint32_t max = 0; uint32_t sum=0; int i; for(i=0; i< size; i++){ sum += t[i]; if(t[i] > max) {max = t[i];} if(t[i] < min) {min = t[i];} } avg = sum/size; if(size!=0)printf("Round-trip delay: min/avg/max = %lu/%lu/%lu ms.\n",min,avg,max); } void *sock_server_thread(void *arg) { (void) arg; local.port = 1234; if (sock_udp_create(&sock, &local, NULL, 0) < 0) { puts("Error creating UDP sock"); return NULL; } while (1) { sock_udp_ep_t remote; size_t res; if ((res = sock_udp_recv(&sock,ack, sizeof(ack),SOCK_NO_TIMEOUT ,&remote)) < 0) { puts("Can't receive datagram\n"); } puts("Got a UDP datagram\n"); //send ack if (sock_udp_send(&sock,ack,res, &remote) != res) { puts("Error sending reply"); } } return NULL; } void *sock_client_thread(void *arg) { (void) arg; ssize_t res; int i,j,k=0, num_packet; uint32_t timebuf[MAXLEN]; sock_udp_ep_t remote = { .family = AF_INET6 }; remote.port = 1234; remote.addr.ipv6[0] = 0xba; remote.addr.ipv6[1] = 0xad; remote.addr.ipv6[2] = 0xa5; remote.addr.ipv6[3] = 0x55; remote.addr.ipv6[14] = 0x17; remote.addr.ipv6[15] = 0x36; for (num_packet=0; num_packet < MAXPAC; num_packet++) { /* fill up the echo message */ for (i = 0; i < ECHOLEN; i++) { j = (i < 26) ? i : i % 26; sbuf[i] = 'a' + j; } start = xtimer_now_usec(); if (sock_udp_send(&sock,sbuf,ECHOLEN, &remote) < 0) { puts("Error sending message"); } if ((res = sock_udp_recv(&sock,rbuf,MAXLEN,1 * US_PER_SEC,NULL)) < 0) { if (res == -ETIMEDOUT) { puts("Timedout"); } else { puts("Error receiving message"); } } else{ end = xtimer_now_usec(); timebuf[k] = delay(start, end); k++; } //xtimer_sleep(1); } printf("\n"); printf("Packets: Size: %d bytes, Sent = %d, Received = %d, Lost = %d\n", ECHOLEN, MAXPAC, k, (MAXPAC-k)); rttd(timebuf,k); return NULL; } static void _init_interface(void) { kernel_pid_t ifs[GNRC_NETIF_NUMOF]; ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED; ipv6_addr_t tmp_addr= IPV6_ADDR_UNSPECIFIED; uint8_t hwaddr[MAX_ADDR_LEN]; int res; gnrc_netif_get(ifs); //addresses gobales addr.u8[0] = 0xba; addr.u8[1] = 0xad; addr.u8[2] = 0xa5; addr.u8[3] = 0x55; res = gnrc_netapi_get(ifs[0], NETOPT_ADDRESS, 0, hwaddr, sizeof(hwaddr)); if (res >= 0) { addr.u8[14] = *hwaddr; addr.u8[15] = *(hwaddr+1); } memcpy(tmp_addr.u8,addr.u8,IPV6_ADDR_BIT_LEN); gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST); /* model ipv6 addr: dead:beef::Hwaddr */ if((addr.u8[14]==0x17)&&(addr.u8[15]==0x02)){ tmp_addr.u8[14] = 0x17; tmp_addr.u8[15] = 0x36; //fibroute dest: baad:a555::1736 via fe80::1210:6432:140f:1732 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); tmp_addr.u8[14] = 0x17; tmp_addr.u8[15] = 0x32; //fibroute dest: baad:a555::1732 via fe80::1210:6432:140f:1732 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); client=thread_create(sock_client_stack,sizeof(sock_client_stack),8,THREAD_CREATE_STACKTEST,sock_client_thread,NULL,"sock_client_thread"); }else if((addr.u8[14]==0x17)&&(addr.u8[15]==0x32)){ tmp_addr.u8[14] = 0x17; tmp_addr.u8[15] = 0x36; //fibroute dest: baad:a555::1732 via fe80::1210:642d:1439:1736 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node3, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); tmp_addr.u8[14] = 0x17; tmp_addr.u8[15] = 0x02; //fibroute dest: baad:a555::1702 via fe80::1210:642c:1432:1702 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node1, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); }else if((addr.u8[14]==0x17)&&(addr.u8[15]==0x36)){ tmp_addr.u8[14] = 0x17; tmp_addr.u8[15] = 0x02; //fibroute dest: baad:a555::1702 via fe80::1210:6432:140f:1732 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); tmp_addr.u8[14] = 0x17; tmp_addr.u8[15] = 0x32; //fibroute dest: baad:a555::1732 via fe80::1210:6432:140f:1732 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); server=thread_create(sock_server_stack,sizeof(sock_server_stack),6,THREAD_CREATE_STACKTEST,sock_server_thread,NULL,"sock_server_thread"); }else{ puts("new node ?"); } } static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; static const shell_command_t shell_commands[] = { { NULL, NULL, NULL } }; int main(void) { msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); puts("RIOT network stack example application"); _init_interface(); /* start shell */ puts("All up, running the shell now"); char line_buf[SHELL_DEFAULT_BUFSIZE]; shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); /* should be never reached */ return 0; }