Commit d3acc3ce746708b1e43b7aa4691edbc5cd1b61cb
1 parent
0ce5a8d8
ajout gestion led arret d'urgence
Showing
5 changed files
with
465 additions
and
23 deletions
Show diff stats
RIOT/examples/real_time_app/main.c
... | ... | @@ -104,6 +104,7 @@ int ordre = 0; |
104 | 104 | int64_t offset = 0; |
105 | 105 | int timer_run = 0; |
106 | 106 | int tourne = 0; |
107 | +int arret = 0; | |
107 | 108 | sock_udp_ep_t local = SOCK_IPV6_EP_ANY; |
108 | 109 | sock_udp_t sock; |
109 | 110 | sock_udp_ep_t local_ntp = SOCK_IPV6_EP_ANY; |
... | ... | @@ -196,8 +197,10 @@ void *sock_server_thread(void *arg) |
196 | 197 | } |
197 | 198 | if(deadline >= DEADLINE && compteur >=5) { |
198 | 199 | compteur = 0; |
199 | - if (sock_udp_send(&sock, "d", sizeof("d"), &remote) < 0) { | |
200 | - puts("Error sending reply"); | |
200 | + if(arret == 0) { | |
201 | + if (sock_udp_send(&sock, "d", sizeof("d"), &remote) < 0) { | |
202 | + puts("Error sending reply"); | |
203 | + } | |
201 | 204 | } |
202 | 205 | if(tourne == 1) |
203 | 206 | { |
... | ... | @@ -205,16 +208,17 @@ void *sock_server_thread(void *arg) |
205 | 208 | pwm_set(PWM_DEV(1),1,960); |
206 | 209 | } |
207 | 210 | if(timer_run == 0) { |
211 | + puts("Degradation"); | |
208 | 212 | gpio_clear(LED1_PIN); |
209 | 213 | gpio_clear(LED2_PIN); |
210 | 214 | gpio_set(LED0_PIN); |
211 | 215 | timer_set(TIMER_DEV(1),0,25200); |
212 | - /* if (sntp_sync(&server, SOCK_NO_TIMEOUT) < 0) { | |
213 | - puts("Error in synchronization"); | |
214 | - return NULL; | |
215 | - } | |
216 | - offset = sntp_get_offset(); | |
217 | - printf("offset : %i\n",(int)offset);*/ | |
216 | + /* if (sntp_sync(&server, SOCK_NO_TIMEOUT) < 0) { | |
217 | + * puts("Error in synchronization"); | |
218 | + * return NULL; | |
219 | + } | |
220 | + offset = sntp_get_offset(); | |
221 | + printf("offset : %i\n",(int)offset);*/ | |
218 | 222 | timer_run = 1; |
219 | 223 | } |
220 | 224 | } |
... | ... | @@ -233,6 +237,7 @@ void *sock_server_thread(void *arg) |
233 | 237 | if (sock_udp_send(&sock, "n", sizeof("n"), &remote) < 0) { |
234 | 238 | puts("Error sending reply"); |
235 | 239 | } |
240 | + arret = 0; | |
236 | 241 | gpio_clear(LED2_PIN); |
237 | 242 | gpio_clear(LED0_PIN); |
238 | 243 | gpio_set(LED1_PIN); |
... | ... | @@ -242,6 +247,11 @@ void *sock_server_thread(void *arg) |
242 | 247 | } |
243 | 248 | printf("compteur : %d\n",compteur); |
244 | 249 | memset(&buf,0,sizeof(buf)); |
250 | + if(arret == 1) { | |
251 | + if (sock_udp_send(&sock, "a", sizeof("a"), &remote) < 0) { | |
252 | + puts("Error sending reply"); | |
253 | + } | |
254 | + } | |
245 | 255 | } |
246 | 256 | } |
247 | 257 | else { |
... | ... | @@ -285,15 +295,6 @@ void *sock_client_thread(void *arg) |
285 | 295 | remote.addr.ipv6[15] = 0x1e; |
286 | 296 | |
287 | 297 | while (1) { |
288 | - if(vitesse==2) { | |
289 | - gpio_clear(LED0_PIN); | |
290 | - gpio_toggle(LED1_PIN); | |
291 | - } | |
292 | - else { | |
293 | - gpio_clear(LED1_PIN); | |
294 | - gpio_toggle(LED0_PIN); | |
295 | - } | |
296 | - | |
297 | 298 | data.heure_actuelle = xtimer_now_usec(); |
298 | 299 | if (sock_udp_send(&sock, &data, sizeof(data), &remote) < 0) { |
299 | 300 | puts("Error sending message"); |
... | ... | @@ -307,14 +308,22 @@ void *sock_client_thread(void *arg) |
307 | 308 | } |
308 | 309 | } |
309 | 310 | else { |
310 | - printf("Received message: \""); | |
311 | - for (int i = 0; i < res; i++) { | |
312 | - printf("%c", buf[i]); | |
313 | - } | |
314 | 311 | if(buf[0] == 'n') |
315 | 312 | vitesse = 2; |
313 | + else if(buf[0] == 'a') { | |
314 | + vitesse = 1; | |
315 | + gpio_set(LED2_PIN); | |
316 | + } | |
316 | 317 | else vitesse = 1; |
317 | - printf("\"\n"); | |
318 | + } | |
319 | + if(vitesse==2) { | |
320 | + gpio_clear(LED0_PIN); | |
321 | + gpio_clear(LED2_PIN); | |
322 | + gpio_toggle(LED1_PIN); | |
323 | + } | |
324 | + else { | |
325 | + gpio_clear(LED1_PIN); | |
326 | + gpio_toggle(LED0_PIN); | |
318 | 327 | } |
319 | 328 | xtimer_sleep(vitesse); |
320 | 329 | } |
... | ... | @@ -327,10 +336,11 @@ static void arret_urgence(void *arg,int channel) |
327 | 336 | pwm_set(PWM_DEV(1),1,0); |
328 | 337 | tourne = 0; |
329 | 338 | timer_run = 0; |
339 | + arret = 1; | |
330 | 340 | gpio_clear(LED0_PIN); |
331 | 341 | gpio_clear(LED1_PIN); |
332 | 342 | gpio_set(LED2_PIN); |
333 | - printf("Arret d'urgence\n"); | |
343 | + puts("Arret d'urgence"); | |
334 | 344 | } |
335 | 345 | |
336 | 346 | static void _init_timer(void) | ... | ... |
... | ... | @@ -0,0 +1,64 @@ |
1 | +# name of your application | |
2 | +APPLICATION = gnrc_networking | |
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 | +DISABLE_MODULE += net_gnrc_sixlowpan_iphc | |
16 | +# Include packages that pull up and auto-init the link layer. | |
17 | +# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present | |
18 | +USEMODULE += gnrc_netdev_default | |
19 | +USEMODULE += auto_init_gnrc_netif | |
20 | +# Specify the mandatory networking modules for IPv6 and UDP | |
21 | +USEMODULE += gnrc_ipv6_router_default | |
22 | +USEMODULE += gnrc_udp | |
23 | +# Add a routing protocol | |
24 | +USEMODULE += gnrc_rpl | |
25 | +USEMODULE += auto_init_gnrc_rpl | |
26 | +# This application dumps received packets to STDIO using the pktdump module | |
27 | +USEMODULE += gnrc_pktdump | |
28 | +# Additional networking modules that can be dropped if not needed | |
29 | +USEMODULE += gnrc_icmpv6_echo | |
30 | +# Add also the shell, some shell commands | |
31 | +USEMODULE += shell | |
32 | +USEMODULE += shell_commands | |
33 | +USEMODULE += ps | |
34 | +USEMODULE += netstats_l2 | |
35 | +USEMODULE += netstats_ipv6 | |
36 | +USEMODULE += netstats_rpl | |
37 | +USEMODULE += at86rf231 | |
38 | +# Comment this out to disable code in RIOT that does safety checking | |
39 | +# which is not needed in a production environment but helps in the | |
40 | +# development process: | |
41 | +CFLAGS += -DDEVELHELP | |
42 | + | |
43 | +# Comment this out to join RPL DODAGs even if DIOs do not contain | |
44 | +# DODAG Configuration Options (see the doc for more info) | |
45 | +# CFLAGS += -DGNRC_RPL_DODAG_CONF_OPTIONAL_ON_JOIN | |
46 | + | |
47 | +# Change this to 0 show compiler invocation lines by default: | |
48 | +QUIET ?= 1 | |
49 | + | |
50 | +include $(RIOTBASE)/Makefile.include | |
51 | + | |
52 | +# Set a custom channel if needed | |
53 | +ifneq (,$(filter cc110x,$(USEMODULE))) # radio is cc110x sub-GHz | |
54 | + DEFAULT_CHANNEL ?= 0 | |
55 | + CFLAGS += -DCC110X_DEFAULT_CHANNEL=$(DEFAULT_CHANNEL) | |
56 | +else | |
57 | + ifneq (,$(filter at86rf212b,$(USEMODULE))) # radio is IEEE 802.15.4 sub-GHz | |
58 | + DEFAULT_CHANNEL ?= 5 | |
59 | + FLAGS += -DIEEE802154_DEFAULT_SUBGHZ_CHANNEL=$(DEFAULT_CHANNEL) | |
60 | + else # radio is IEEE 802.15.4 2.4 GHz | |
61 | + DEFAULT_CHANNEL ?= 26 | |
62 | + CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=$(DEFAULT_CHANNEL) | |
63 | + endif | |
64 | +endif | ... | ... |
... | ... | @@ -0,0 +1,142 @@ |
1 | +# gnrc_networking example | |
2 | + | |
3 | +This example shows you how to try out the code in two different ways: Either by communicating | |
4 | +between the RIOT machine and its Linux host, or by communicating between two RIOT instances. | |
5 | +Note that the former only works with native, i.e. if you run RIOT on your Linux machine. | |
6 | + | |
7 | +## Connecting RIOT native and the Linux host | |
8 | + | |
9 | +> **Note:** RIOT does not support IPv4, so you need to stick to IPv6 anytime. To | |
10 | +establish a connection between RIOT and the Linux host, you will need `netcat` | |
11 | +(with IPv6 support). Ubuntu 14.04 comes with netcat IPv6 support pre-installed. | |
12 | +On Debian it's available in the package `netcat-openbsd`. Be aware that many | |
13 | +programs require you to add an option such as -6 to tell them to use IPv6, otherwise they | |
14 | +will fail. If you're using a _Raspberry Pi_, run `sudo modprobe ipv6` before trying | |
15 | +this example, because raspbian does not load the IPv6 module automatically. | |
16 | +On some systems (openSUSE for example), the _firewall_ may interfere, and prevent | |
17 | +some packets to arrive at the application (they will however show up in Wireshark, | |
18 | +which can be confusing). So be sure to adjust your firewall rules, or turn it off | |
19 | +(who needs security anyway). | |
20 | + | |
21 | +First, create a tap interface: | |
22 | + | |
23 | + sudo ip tuntap add tap0 mode tap user ${USER} | |
24 | + sudo ip link set tap0 up | |
25 | + | |
26 | +Now you can start the `gnrc_networking` example by invoking `make term`. This should | |
27 | +automatically connect to the `tap0` interface. If this doesn't work for any reason, | |
28 | +run make term with the tap0 interface as the PORT environment variable: | |
29 | + | |
30 | + PORT=tap0 make term | |
31 | + | |
32 | +To verify that there is connectivity between RIOT and Linux, go to the RIOT console and run `ifconfig`: | |
33 | + | |
34 | + > ifconfig | |
35 | + Iface 7 HWaddr: ce:f5:e1:c5:f7:5a | |
36 | + inet6 addr: ff02::1/128 scope: local [multicast] | |
37 | + inet6 addr: fe80::ccf5:e1ff:fec5:f75a/64 scope: local | |
38 | + inet6 addr: ff02::1:ffc5:f75a/128 scope: local [multicast] | |
39 | + | |
40 | +Copy the [link-local address](https://en.wikipedia.org/wiki/Link-local_address) | |
41 | +of the RIOT node (prefixed with `fe80`) and try to ping it **from the Linux node**: | |
42 | + | |
43 | + ping6 fe80::ccf5:e1ff:fec5:f75a%tap0 | |
44 | + | |
45 | +Note that the interface on which to send the ping needs to be appended to the IPv6 | |
46 | +address, `%tap0` in the above example. When talking to the RIOT node, you always want | |
47 | +to send to/receive from the `tap0` interface. | |
48 | + | |
49 | +If the pings succeed you can go on to send UDP packets. To do that, first start a | |
50 | +UDP server on the RIOT node: | |
51 | + | |
52 | + > udp server start 8808 | |
53 | + Success: started UDP server on port 8808 | |
54 | + | |
55 | +Now, on the Linux host, you can run netcat to connect with RIOT's UDP server: | |
56 | + | |
57 | + nc -6uv fe80::ccf5:e1ff:fec5:f75a%tap0 8808 | |
58 | + | |
59 | +The `-6` option is necessary to tell netcat to use IPv6 only, the `-u` option tells | |
60 | +it to use UDP only, and the `-v` option makes it give more verbose output (this one is optional). | |
61 | + | |
62 | +You should now see that UDP messages are received on the RIOT side. Opening a UDP | |
63 | +server on the Linux side is also possible. To do that, write down the IP address | |
64 | +of the host (run on Linux): | |
65 | + | |
66 | + ifconfig tap0 | |
67 | + tap0 Link encap:Ethernet HWaddr ce:f5:e1:c5:f7:59 | |
68 | + inet6 addr: fe80::4049:5fff:fe17:b3ae/64 Scope:Link | |
69 | + UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 | |
70 | + RX packets:6 errors:0 dropped:0 overruns:0 frame:0 | |
71 | + TX packets:36 errors:0 dropped:0 overruns:0 carrier:0 | |
72 | + collisions:0 txqueuelen:0 | |
73 | + RX bytes:488 (488.0 B) TX bytes:3517 (3.5 KB) | |
74 | + | |
75 | +Then open a UDP server on Linux (the `-l` option makes netcat listen for incoming connections): | |
76 | + | |
77 | + nc -6ul 8808 | |
78 | + | |
79 | +Now, on the RIOT side, send a UDP packet using: | |
80 | + | |
81 | + udp send fe80::4049:5fff:fe17:b3ae 8808 testmessage | |
82 | + | |
83 | +You should see `testmessage` appear in netcat. Instead of using netcat, you can of | |
84 | +course write your own software, but you may have to bind the socket to a specific | |
85 | +interface (tap0 in this case). For an example that shows how to do so, see | |
86 | +[here](https://gist.github.com/backenklee/dad5e80b764b3b3d0d3e). | |
87 | + | |
88 | +## Connecting two RIOT instances | |
89 | + | |
90 | +When using native (i.e. when you're trying this on your Linux machine), you first | |
91 | +need to set up two tap devices and a bridge that connects them. This constitutes a | |
92 | +virtual network that the RIOT instances can use to communicate. | |
93 | + | |
94 | + ./../../dist/tools/tapsetup/tapsetup --create 2 | |
95 | + | |
96 | +Then, make sure you've compiled the application by calling `make` and start the | |
97 | +first RIOT instance by invoking `make term`. In the RIOT shell, get to know the | |
98 | +IP address of this node: | |
99 | + | |
100 | + > ifconfig | |
101 | + Iface 7 HWaddr: ce:f5:e1:c5:f7:5a | |
102 | + inet6 addr: ff02::1/128 scope: local [multicast] | |
103 | + inet6 addr: fe80::ccf5:e1ff:fec5:f75a/64 scope: local | |
104 | + inet6 addr: ff02::1:ffc5:f75a/128 scope: local [multicast] | |
105 | + | |
106 | +and start a UDP server. | |
107 | + | |
108 | + > udp server start 8808 | |
109 | + | |
110 | +This node is now ready to receive data on port `8808`. | |
111 | + | |
112 | +In a second terminal, start a second RIOT instance, this time listening on `tap1`: | |
113 | + | |
114 | + PORT=tap1 make term | |
115 | + | |
116 | +In the RIOT shell, you can now send a message to the first RIOT instance: | |
117 | + | |
118 | + > udp send fe80::ccf5:e1ff:fec5:f75 8808 testmessage | |
119 | + | |
120 | +*(Make sure to copy the actual | |
121 | +[link-local address](https://en.wikipedia.org/wiki/Link-local_address) of your first | |
122 | +RIOT instance into the above command)* | |
123 | + | |
124 | +In your first terminal, you should now see output that looks like this. | |
125 | + | |
126 | + > PKTDUMP: data received: | |
127 | + ~~ SNIP 0 - size: 11 byte, type: NETTYPE_UNDEF (0) | |
128 | + 000000 74 65 73 74 6d 65 73 73 61 67 65 | |
129 | + ~~ SNIP 1 - size: 8 byte, type: NETTYPE_UDP (3) | |
130 | + src-port: 8808 dst-port: 8808 | |
131 | + length: 19 cksum: 0x4d95f | |
132 | + ~~ SNIP 2 - size: 40 byte, type: NETTYPE_IPV6 (1) | |
133 | + traffic class: 0x00 (ECN: 0x0, DSCP: 0x00) | |
134 | + flow label: 0x00000 | |
135 | + length: 19 next header: 17 hop limit: 64 | |
136 | + source address: fe80::a08a:84ff:fe68:544f | |
137 | + destination address: fe80::60fc:3cff:fe5e:40df | |
138 | + ~~ SNIP 3 - size: 20 byte, type: NETTYPE_NETIF (-1) | |
139 | + if_pid: 6 rssi: 0 lqi: 0 | |
140 | + src_l2addr: a2:8a:84:68:54:4f | |
141 | + dst_l2addr: 62:fc:3c:5e:40:df | |
142 | + ~~ PKT - 4 snips, total size: 79 byte | ... | ... |
... | ... | @@ -0,0 +1,52 @@ |
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 | + | |
21 | +#include <stdio.h> | |
22 | + | |
23 | +#include "shell.h" | |
24 | +#include "msg.h" | |
25 | +#include "board.h" | |
26 | +#include "periph/gpio.h" | |
27 | + | |
28 | +#define MAIN_QUEUE_SIZE (8) | |
29 | +static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; | |
30 | + | |
31 | +extern int udp_cmd(int argc, char **argv); | |
32 | + | |
33 | +static const shell_command_t shell_commands[] = { | |
34 | + { "udp", "send data over UDP and listen on UDP ports", udp_cmd }, | |
35 | + { NULL, NULL, NULL } | |
36 | +}; | |
37 | + | |
38 | +int main(void) | |
39 | +{ | |
40 | + /* we need a message queue for the thread running the shell in order to | |
41 | + * receive potentially fast incoming networking packets */ | |
42 | + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); | |
43 | + puts("RIOT network stack example application"); | |
44 | + gpio_set(LED0_PIN); | |
45 | + /* start shell */ | |
46 | + puts("All up, running the shell now"); | |
47 | + char line_buf[SHELL_DEFAULT_BUFSIZE]; | |
48 | + shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); | |
49 | + | |
50 | + /* should be never reached */ | |
51 | + return 0; | |
52 | +} | ... | ... |
... | ... | @@ -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 | +} | ... | ... |