Commit 41eae2a472df07f792a81a1cd50066a00dbfe4c7

Authored by vrobic
1 parent 929398b3

ajout serveur sntp sur noeud maitre

RIOT/examples/real_time_app/Makefile 0 → 100644
... ... @@ -0,0 +1,67 @@
  1 +# name of your application
  2 +APPLICATION = real_time_app
  3 +
  4 +# If no BOARD is found in the environment, use this default:
  5 +BOARD ?= native
  6 +
  7 +# This has to be the absolute path to the RIOT base directory:
  8 +RIOTBASE ?= $(CURDIR)/../..
  9 +
  10 +BOARD_INSUFFICIENT_MEMORY := airfy-beacon chronos msb-430 msb-430h nrf51dongle \
  11 + nrf6310 nucleo-f103 nucleo-f334 pca10000 pca10005 spark-core \
  12 + stm32f0discovery telosb weio wsn430-v1_3b wsn430-v1_4 \
  13 + yunjia-nrf51822 z1 nucleo-f072 nucleo-f030 nucleo-f070 \
  14 + microbit calliope-mini
  15 +
  16 +# Include packages that pull up and auto-init the link layer.
  17 +USEMODULE += gnrc_sock_udp
  18 +USEMODULE += sntp
  19 +# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
  20 +USEMODULE += gnrc_netdev_default
  21 +USEMODULE += auto_init_gnrc_netif
  22 +# Specify the mandatory networking modules for IPv6 and UDP
  23 +USEMODULE += gnrc_ipv6_router_default
  24 +USEMODULE += gnrc_udp
  25 +# Add a routing protocol
  26 +USEMODULE += gnrc_rpl
  27 +#USEMODULE += auto_init_gnrc_rpl
  28 +# This application dumps received packets to STDIO using the pktdump module
  29 +#USEMODULE += gnrc_pktdump
  30 +# Additional networking modules that can be dropped if not needed
  31 +USEMODULE += gnrc_icmpv6_echo
  32 +# Add also the shell, some shell commands
  33 +USEMODULE += shell
  34 +USEMODULE += shell_commands
  35 +USEMODULE += ps
  36 +USEMODULE += netstats_l2
  37 +USEMODULE += netstats_ipv6
  38 +USEMODULE += netstats_rpl
  39 +#include our RF module
  40 +USEMODULE += at86rf231
  41 +# Comment this out to disable code in RIOT that does safety checking
  42 +# which is not needed in a production environment but helps in the
  43 +# development process:
  44 +CFLAGS += -DDEVELHELP
  45 +
  46 +# Comment this out to join RPL DODAGs even if DIOs do not contain
  47 +# DODAG Configuration Options (see the doc for more info)
  48 +# CFLAGS += -DGNRC_RPL_DODAG_CONF_OPTIONAL_ON_JOIN
  49 +
  50 +# Change this to 0 show compiler invocation lines by default:
  51 +QUIET ?= 1
  52 +
  53 +include $(RIOTBASE)/Makefile.include
  54 +
  55 +# Set a custom channel if needed
  56 +ifneq (,$(filter cc110x,$(USEMODULE))) # radio is cc110x sub-GHz
  57 + DEFAULT_CHANNEL ?= 0
  58 + CFLAGS += -DCC110X_DEFAULT_CHANNEL=$(DEFAULT_CHANNEL)
  59 +else
  60 + ifneq (,$(filter at86rf212b,$(USEMODULE))) # radio is IEEE 802.15.4 sub-GHz
  61 + DEFAULT_CHANNEL ?= 5
  62 + FLAGS += -DIEEE802154_DEFAULT_SUBGHZ_CHANNEL=$(DEFAULT_CHANNEL)
  63 + else # radio is IEEE 802.15.4 2.4 GHz
  64 + DEFAULT_CHANNEL ?= 26
  65 + CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=$(DEFAULT_CHANNEL)
  66 + endif
  67 +endif
... ...
RIOT/examples/real_time_app/main.c 0 → 100644
... ... @@ -0,0 +1,363 @@
  1 +/*
  2 + * Copyright (C) 2015 Freie Universität Berlin
  3 + *
  4 + * This file is subject to the terms and conditions of the GNU Lesser
  5 + * General Public License v2.1. See the file LICENSE in the top level
  6 + * directory for more details.
  7 + */
  8 +
  9 +/**
  10 + * @ingroup examples
  11 + * @{
  12 + *
  13 + * @file
  14 + * @brief Example application for demonstrating the RIOT network stack
  15 + *
  16 + * @author Hauke Petersen <hauke.petersen@fu-berlin.de>
  17 + *
  18 + * @}
  19 + */
  20 +#include <stdbool.h>
  21 +#include <stdint.h>
  22 +#include <stdio.h>
  23 +#include <stdlib.h>
  24 +#include <string.h>
  25 +#include <inttypes.h>
  26 +
  27 +#include <arpa/inet.h>
  28 +#include "../../boards/stm32f4discovery/include/board.h"
  29 +#include "../../boards/stm32f4discovery/include/periph_conf.h"
  30 +#include "net/sock/udp.h"
  31 +#include "net/gnrc/ipv6.h"
  32 +#include "net/af.h"
  33 +#include "net/sixlowpan.h"
  34 +#include "net/gnrc/pktdump.h"
  35 +#include "shell.h"
  36 +#include "shell_commands.h"
  37 +#include "msg.h"
  38 +#include "thread.h"
  39 +#include "sched.h"
  40 +#include "thread.h"
  41 +#include "kernel_types.h"
  42 +#include "net/netstats.h"
  43 +#include "net/ipv6/addr.h"
  44 +#include "periph/timer.h"
  45 +#include "net/gnrc/ipv6/netif.h"
  46 +#include "net/gnrc/netif.h"
  47 +#include "net/gnrc/netapi.h"
  48 +#include "net/netopt.h"
  49 +#include "net/gnrc/pkt.h"
  50 +#include "net/gnrc/pktbuf.h"
  51 +#include "net/gnrc/netif/hdr.h"
  52 +#include "net/gnrc/sixlowpan/netif.h"
  53 +#include "net/fib.h"
  54 +#include "net/gnrc/udp.h"
  55 +#include "periph/pwm.h"
  56 +#include "od.h"
  57 +#include "net/sntp.h"
  58 +#include "net/ntp_packet.h"
  59 +#ifdef MODULE_SCHEDSTATISTICS
  60 +#include "xtimer.h"
  61 +#endif
  62 +
  63 +#ifdef MODULE_TLSF
  64 +#include "tlsf.h"
  65 +#endif
  66 +
  67 +#define MAIN_QUEUE_SIZE (8)
  68 +/**
  69 + * @brief The maximal expected link layer address length in byte
  70 + */
  71 +#define MAX_ADDR_LEN (8U)
  72 +
  73 +/**
  74 + * @brief The default IPv6 prefix length if not specified.
  75 + */
  76 +#define SC_NETIF_IPV6_DEFAULT_PREFIX_LEN (64)
  77 +
  78 +#define _STACKSIZE (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
  79 +#define MSG_TYPE_ISR (0x3456)
  80 +
  81 +#define PWM_FREQ 100
  82 +#define PWM_RES 100
  83 +
  84 +// addr ipv6 link local node 1: fe80::3734:510e:3317:3402
  85 +uint8_t node1[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x37,0x34,0x51,0x0e,0x33,0x17,0x34,0x02};
  86 +// addr ipv6 link local node 2: fe80::3634:5110:3473:3762
  87 +uint8_t node2[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x34,0x51,0x10,0x34,0x73,0x37,0x62};
  88 +//addr ipv6 link local node 3: fe80::3634:5110:3471:3766
  89 +uint8_t node3[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x34,0x51,0x10,0x34,0x71,0x37,0x66};
  90 +
  91 +//static char _stack_server[GNRC_PKTDUMP_STACKSIZE];
  92 +char pwm_stack[THREAD_STACKSIZE_MAIN];
  93 +char sock_server_stack[THREAD_STACKSIZE_MAIN];
  94 +char sock_client_stack[THREAD_STACKSIZE_MAIN];
  95 +char sock_time_server_stack[THREAD_STACKSIZE_MAIN];
  96 +// static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
  97 +// KERNEL_PID_UNDEF);
  98 +//
  99 +// kernel_pid_t gnrc_server_pid = KERNEL_PID_UNDEF;
  100 +//
  101 +kernel_pid_t server, client, time_server;
  102 +int ordre = 0;
  103 +uint8_t buf[128];
  104 +sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
  105 +sock_udp_t sock;
  106 +sock_udp_ep_t local_ntp = SOCK_IPV6_EP_ANY;
  107 +sock_udp_t sock_ntp;
  108 +static ntp_packet_t sntp_packet;
  109 +
  110 +
  111 +void *sock_time_server_thread(void *arg)
  112 +{
  113 + (void) arg;
  114 + local_ntp.port = NTP_PORT;
  115 +
  116 + if (sock_udp_create(&sock_ntp, &local_ntp, NULL, 0) < 0) {
  117 + puts("Error creating UDP sock");
  118 + return NULL;
  119 + }
  120 +
  121 + while (1) {
  122 + sock_udp_ep_t remote;
  123 + ssize_t res;
  124 +
  125 + if ((res = sock_udp_recv(&sock_ntp,&sntp_packet, sizeof(sntp_packet), SOCK_NO_TIMEOUT,
  126 + &remote)) >= 0) {
  127 + puts("Received a message");
  128 + printf("TT: %lu\n", byteorder_ntohl(sntp_packet.transmit.seconds));
  129 +
  130 + // printf("%c\n",remote.addr.ipv6[15]);
  131 + //xtimer_ticks64_t now = xtimer_now64();
  132 + // heure actuelle du serveur
  133 + sntp_packet.receive.seconds=byteorder_htonl( xtimer_now_usec());
  134 + sntp_packet.origin.seconds=sntp_packet.transmit.seconds;
  135 + sntp_packet.transmit.seconds=byteorder_htonl( xtimer_now_usec());
  136 + printf("heure actuelle : %lu\n",xtimer_now_usec());
  137 + printf("TT2: %lu\n", byteorder_ntohl(sntp_packet.transmit.seconds));
  138 + //memset(&sntp_packet, 0, sizeof(sntp_packet));
  139 + //ntp_packet_set_vn(&sntp_packet);
  140 + //ntp_packet_set_mode(&sntp_packet, NTP_MODE_SERVER);
  141 + if (sock_udp_send(&sock_ntp, &sntp_packet, sizeof(sntp_packet), &remote) < 0) {
  142 + puts("Error sending reply");
  143 + }
  144 + }
  145 + }
  146 + return NULL;
  147 +}
  148 +void *pwm_thread(void *arg)
  149 +{
  150 + (void) arg;
  151 + int tourne = 0;
  152 + while(1)
  153 + {
  154 +
  155 + if(ordre==1)
  156 + {
  157 + pwm_set(PWM_DEV(0),1,45);
  158 + tourne = 1;
  159 + }
  160 + else if(ordre==0)
  161 + {
  162 + pwm_set(PWM_DEV(0),1,0);
  163 + tourne = 0;
  164 + }
  165 + else if(ordre==2 && tourne == 1)
  166 + pwm_set(PWM_DEV(0),1,31);
  167 +
  168 + }
  169 + return NULL;
  170 +}
  171 +
  172 +void *sock_server_thread(void *arg)
  173 +{
  174 + (void) arg;
  175 + local.port = 1234;
  176 +
  177 + if (sock_udp_create(&sock, &local, NULL, 0) < 0) {
  178 + puts("Error creating UDP sock");
  179 + return NULL;
  180 + }
  181 +
  182 + //sntp_sync(&local,5000);
  183 + //printf("temps : %i\n",(int)sntp_get_offset());
  184 +
  185 + while (1) {
  186 + sock_udp_ep_t remote;
  187 + ssize_t res;
  188 +
  189 + if ((res = sock_udp_recv(&sock, buf, sizeof(buf), SOCK_NO_TIMEOUT,
  190 + &remote)) >= 0) {
  191 + // puts("Received a message");
  192 + //printf("%s\n",buf);
  193 + if (sock_udp_send(&sock, buf, res, &remote) < 0) {
  194 + puts("Error sending reply");
  195 + }
  196 + if(strcmp((char *)buf,"go")==0)
  197 + {
  198 + ordre = 1;
  199 + timer_set(XTIMER_DEV,0,8400);
  200 + timer_clear(TIMER_DEV(1),0);
  201 + }
  202 + else
  203 + {
  204 + ordre = 0;
  205 + }
  206 + }
  207 + }
  208 + return NULL;
  209 +}
  210 +
  211 +void *sock_client_thread(void *arg)
  212 +{
  213 + (void) arg;
  214 + uint_8t paquet[];
  215 + sock_udp_ep_t remote = { .family = AF_INET6 };
  216 +
  217 + remote.port = 1234;
  218 + remote.addr.ipv6[0] = 0xde;
  219 + remote.addr.ipv6[1] = 0xad;
  220 + remote.addr.ipv6[2] = 0xbe;
  221 + remote.addr.ipv6[3] = 0xef;
  222 + remote.addr.ipv6[14] = 0x37;
  223 + remote.addr.ipv6[15] = 0x66;
  224 +// memcpy(remote.addr.ipv6,addr.u8,IPV6_ADDR_BIT_LEN);
  225 + while (1) {
  226 +// //ipv6_addr_set_all_nodes_multicast((ipv6_addr_t *)&remote.addr.ipv6,
  227 +// // IPV6_ADDR_MCAST_SCP_LINK_LOCAL);
  228 + if (sock_udp_send(NULL, "go", sizeof("go"), &remote) < 0) {
  229 + puts("Error sending message");
  230 + }
  231 + xtimer_sleep(1);
  232 + }
  233 + return NULL;
  234 +}
  235 +
  236 +static void arret_urgence(void *arg,int channel)
  237 +{
  238 + //pwm_set(PWM_DEV(0),1,0);
  239 + ordre=0;
  240 + printf("Arret d'urgence\n");
  241 +}
  242 +
  243 +static void degradation(void *arg,int channel)
  244 +{
  245 + ordre=2;
  246 + //pwm_set(PWM_DEV(0),1,0);
  247 + printf("Ralentissement\n");
  248 + timer_set(TIMER_DEV(1),0,25200);
  249 +}
  250 +
  251 +static void _init_timer(void)
  252 +{
  253 + printf("ok timer\n");
  254 + timer_init(XTIMER_DEV, CLOCK_CORECLOCK/2 ,&degradation,NULL);
  255 + timer_set(XTIMER_DEV, 0, 8400);
  256 + timer_irq_enable(XTIMER_DEV);
  257 + timer_init(TIMER_DEV(1), CLOCK_CORECLOCK/2 ,&arret_urgence,NULL);
  258 + timer_irq_enable(TIMER_DEV(1));
  259 +}
  260 +
  261 +static void _init_pwm(void)
  262 +{
  263 + pwm_init(PWM_DEV(0), PWM_LEFT, PWM_FREQ, PWM_RES);
  264 + pwm_set(PWM_DEV(0),1,0);
  265 +
  266 + thread_create(pwm_stack,sizeof(pwm_stack),8,THREAD_CREATE_STACKTEST,pwm_thread,NULL,"pwm_thread");
  267 +}
  268 +
  269 +
  270 +
  271 +
  272 +static void _init_interface(void)
  273 +{
  274 + kernel_pid_t ifs[GNRC_NETIF_NUMOF];
  275 + ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED;
  276 + ipv6_addr_t tmp_addr= IPV6_ADDR_UNSPECIFIED;
  277 + uint8_t hwaddr[MAX_ADDR_LEN];
  278 + int res;
  279 +
  280 + gnrc_netif_get(ifs);
  281 +
  282 + //addresses gobales
  283 + addr.u8[0] = 0xde;
  284 + addr.u8[1] = 0xad;
  285 + addr.u8[2] = 0xbe;
  286 + addr.u8[3] = 0xef;
  287 +
  288 + res = gnrc_netapi_get(ifs[0], NETOPT_ADDRESS, 0, hwaddr, sizeof(hwaddr));
  289 +
  290 + if (res >= 0) {
  291 + addr.u8[14] = *hwaddr;
  292 + addr.u8[15] = *(hwaddr+1);
  293 + }
  294 + memcpy(tmp_addr.u8,addr.u8,IPV6_ADDR_BIT_LEN);
  295 +
  296 + gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST);
  297 + /* model ipv6 addr: dead:beef::Hwaddr */
  298 + if((addr.u8[14]==0x34)&&(addr.u8[15]==0x02)){
  299 +
  300 + tmp_addr.u8[14] = 0x37;
  301 + tmp_addr.u8[15] = 0x66;
  302 + //fibroute dest: dead:beef::3766 via fe80::3634:5110:3473:3762
  303 + fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE);
  304 + tmp_addr.u8[14] = 0x37;
  305 + tmp_addr.u8[15] = 0x62;
  306 + //fibroute dest: dead:beef::3762 via fe80::3634:5110:3473:3762
  307 + fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE);
  308 + client=thread_create(sock_client_stack,sizeof(sock_client_stack),8,THREAD_CREATE_WOUT_YIELD |THREAD_CREATE_STACKTEST,sock_client_thread,NULL,"sock_client_thread");
  309 + xtimer_usleep(10000);
  310 + time_server=thread_create(sock_time_server_stack,sizeof(sock_time_server_stack),8, THREAD_CREATE_WOUT_YIELD |THREAD_CREATE_STACKTEST,sock_time_server_thread,NULL,"sock_time_server_thread");
  311 + xtimer_usleep(200);
  312 + }else if((addr.u8[14]==0x37)&&(addr.u8[15]==0x62)){
  313 + tmp_addr.u8[14] = 0x37;
  314 + tmp_addr.u8[15] = 0x66;
  315 + //fibroute dest: dead:beef::3766 via fe80::3634:5110:3471:3766
  316 + fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node3, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE);
  317 + tmp_addr.u8[14] = 0x34;
  318 + tmp_addr.u8[15] = 0x02;
  319 + //fibroute dest: dead:beef::3402 via fe80::3734:510e:3317:3402
  320 + fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node1, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE);
  321 + //sntp_sync(&local,5000);
  322 + //printf("temps : %i\n",(int)sntp_get_offset());
  323 + }else if((addr.u8[14]==0x37)&&(addr.u8[15]==0x66)){
  324 + tmp_addr.u8[14] = 0x34;
  325 + tmp_addr.u8[15] = 0x02;
  326 + //fibroute dest: dead:beef::3402 via fe80::3634:5110:3473:3762
  327 + 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] = 0x37;
  328 + tmp_addr.u8[15] = 0x62;
  329 + //fibroute dest: dead:beef::3762 via fe80::3634:5110:3473:3762
  330 + fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE);
  331 + //start_server("1234");
  332 + server=thread_create(sock_server_stack,sizeof(sock_server_stack),6,THREAD_CREATE_WOUT_YIELD |THREAD_CREATE_STACKTEST,sock_server_thread,NULL,"sock_server_thread");
  333 + _init_pwm();
  334 + _init_timer();
  335 + }else{
  336 + puts("new node ?");
  337 + }
  338 +}
  339 +static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
  340 +
  341 +extern int udp_cmd(int argc, char **argv);
  342 +
  343 +static const shell_command_t shell_commands[] = {
  344 + { "udp", "send data over UDP and listen on UDP ports", udp_cmd },
  345 + { NULL, NULL, NULL }
  346 +};
  347 +
  348 +
  349 +int main(void)
  350 +{
  351 +
  352 + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
  353 + puts("RIOT network stack example application");
  354 + _init_interface();
  355 +
  356 + /* start shell */
  357 + puts("All up, running the shell now");
  358 + char line_buf[SHELL_DEFAULT_BUFSIZE];
  359 + shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  360 +
  361 + /* should be never reached */
  362 + return 0;
  363 +}
... ...
RIOT/examples/real_time_app/udp.c 0 → 100644
... ... @@ -0,0 +1,174 @@
  1 +/*
  2 + * Copyright (C) 2015 Freie Universität Berlin
  3 + *
  4 + * This file is subject to the terms and conditions of the GNU Lesser
  5 + * General Public License v2.1. See the file LICENSE in the top level
  6 + * directory for more details.
  7 + */
  8 +
  9 +/**
  10 + * @ingroup examples
  11 + * @{
  12 + *
  13 + * @file
  14 + * @brief Demonstrating the sending and receiving of UDP data
  15 + *
  16 + * @author Hauke Petersen <hauke.petersen@fu-berlin.de>
  17 + *
  18 + * @}
  19 + */
  20 +
  21 +#include <stdio.h>
  22 +#include <inttypes.h>
  23 +
  24 +#include "net/gnrc.h"
  25 +#include "net/gnrc/ipv6.h"
  26 +#include "net/gnrc/udp.h"
  27 +#include "net/gnrc/pktdump.h"
  28 +#include "timex.h"
  29 +#include "xtimer.h"
  30 +
  31 +//static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
  32 + // KERNEL_PID_UNDEF);
  33 +
  34 +
  35 +static void send(char *addr_str, char *port_str, char *data, unsigned int num,
  36 + unsigned int delay)
  37 +{
  38 + uint16_t port;
  39 + ipv6_addr_t addr;
  40 +
  41 + /* parse destination address */
  42 + if (ipv6_addr_from_str(&addr, addr_str) == NULL) {
  43 + puts("Error: unable to parse destination address");
  44 + return;
  45 + }
  46 + /* parse port */
  47 + port = (uint16_t)atoi(port_str);
  48 + if (port == 0) {
  49 + puts("Error: unable to parse destination port");
  50 + return;
  51 + }
  52 +
  53 + for (unsigned int i = 0; i < num; i++) {
  54 + gnrc_pktsnip_t *payload, *udp, *ip;
  55 + unsigned payload_size;
  56 + /* allocate payload */
  57 + payload = gnrc_pktbuf_add(NULL, data, strlen(data), GNRC_NETTYPE_UNDEF);
  58 + if (payload == NULL) {
  59 + puts("Error: unable to copy data to packet buffer");
  60 + return;
  61 + }
  62 + /* store size for output */
  63 + payload_size = (unsigned)payload->size;
  64 + /* allocate UDP header, set source port := destination port */
  65 + udp = gnrc_udp_hdr_build(payload, port, port);
  66 + if (udp == NULL) {
  67 + puts("Error: unable to allocate UDP header");
  68 + gnrc_pktbuf_release(payload);
  69 + return;
  70 + }
  71 + /* allocate IPv6 header */
  72 + ip = gnrc_ipv6_hdr_build(udp, NULL, &addr);
  73 + if (ip == NULL) {
  74 + puts("Error: unable to allocate IPv6 header");
  75 + gnrc_pktbuf_release(udp);
  76 + return;
  77 + }
  78 + /* send packet */
  79 + if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) {
  80 + puts("Error: unable to locate UDP thread");
  81 + gnrc_pktbuf_release(ip);
  82 + return;
  83 + }
  84 + /* access to `payload` was implicitly given up with the send operation above
  85 + * => use temporary variable for output */
  86 + printf("Success: send %u byte to [%s]:%u\n", payload_size, addr_str,
  87 + port);
  88 + xtimer_usleep(delay);
  89 + }
  90 +}
  91 +
  92 +static void start_server(char *port_str)
  93 +{
  94 +// uint16_t port;
  95 +//
  96 +// /* check if server is already running */
  97 +// if (server.target.pid != KERNEL_PID_UNDEF) {
  98 +// printf("Error: server already running on port %" PRIu32 "\n",
  99 +// server.demux_ctx);
  100 +// return;
  101 +// }
  102 +// /* parse port */
  103 +// port = (uint16_t)atoi(port_str);
  104 +// if (port == 0) {
  105 +// puts("Error: invalid port specified");
  106 +// return;
  107 +// }
  108 +// /* start server (which means registering pktdump for the chosen port) */
  109 +// server.target.pid = gnrc_pktdump_pid;
  110 +// server.demux_ctx = (uint32_t)port;
  111 +// gnrc_netreg_register(GNRC_NETTYPE_UDP, &server);
  112 +// printf("Success: started UDP server on port %" PRIu16 "\n", port);
  113 +}
  114 +
  115 +static void stop_server(void)
  116 +{
  117 +// /* check if server is running at all */
  118 +// if (server.target.pid == KERNEL_PID_UNDEF) {
  119 +// printf("Error: server was not running\n");
  120 +// return;
  121 +// }
  122 +// /* stop server */
  123 +// gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &server);
  124 +// server.target.pid = KERNEL_PID_UNDEF;
  125 +// puts("Success: stopped UDP server");
  126 +}
  127 +
  128 +int udp_cmd(int argc, char **argv)
  129 +{
  130 + if (argc < 2) {
  131 + printf("usage: %s [send|server]\n", argv[0]);
  132 + return 1;
  133 + }
  134 +
  135 + if (strcmp(argv[1], "send") == 0) {
  136 + uint32_t num = 1;
  137 + uint32_t delay = 1000000;
  138 + if (argc < 5) {
  139 + printf("usage: %s send <addr> <port> <data> [<num> [<delay in us>]]\n",
  140 + argv[0]);
  141 + return 1;
  142 + }
  143 + if (argc > 5) {
  144 + num = (uint32_t)atoi(argv[5]);
  145 + }
  146 + if (argc > 6) {
  147 + delay = (uint32_t)atoi(argv[6]);
  148 + }
  149 + send(argv[2], argv[3], argv[4], num, delay);
  150 + }
  151 + else if (strcmp(argv[1], "server") == 0) {
  152 + if (argc < 3) {
  153 + printf("usage: %s server [start|stop]\n", argv[0]);
  154 + return 1;
  155 + }
  156 + if (strcmp(argv[2], "start") == 0) {
  157 + if (argc < 4) {
  158 + printf("usage %s server start <port>\n", argv[0]);
  159 + return 1;
  160 + }
  161 + start_server(argv[3]);
  162 + }
  163 + else if (strcmp(argv[2], "stop") == 0) {
  164 + stop_server();
  165 + }
  166 + else {
  167 + puts("error: invalid command");
  168 + }
  169 + }
  170 + else {
  171 + puts("error: invalid command");
  172 + }
  173 + return 0;
  174 +}
... ...
RIOT/examples/sntp_app/main.c
... ... @@ -110,7 +110,7 @@ int main(void)
110 110 msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
111 111 puts("RIOT network stack example application");
112 112  
113   -//(void) thread_create(sock_time_server_stack,sizeof(sock_time_server_stack),THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,sock_time_server_thread,NULL,"sock_time_server_thread");
  113 +(void) thread_create(sock_time_server_stack,sizeof(sock_time_server_stack),THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,sock_time_server_thread,NULL,"sock_time_server_thread");
114 114 // xtimer_sleep(1);
115 115 printf("Offset: %i\n", (int)sntp_get_offset());
116 116 /* start shell */
... ...