From d3acc3ce746708b1e43b7aa4691edbc5cd1b61cb Mon Sep 17 00:00:00 2001 From: root Date: Fri, 17 Feb 2017 16:29:17 +0000 Subject: [PATCH] ajout gestion led arret d'urgence --- RIOT/examples/real_time_app/main.c | 56 +++++++++++++++++++++++++++++++++----------------------- RIOT/examples/test_led/Makefile | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ RIOT/examples/test_led/README.md | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ RIOT/examples/test_led/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ RIOT/examples/test_led/udp.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 465 insertions(+), 23 deletions(-) create mode 100755 RIOT/examples/test_led/Makefile create mode 100755 RIOT/examples/test_led/README.md create mode 100755 RIOT/examples/test_led/main.c create mode 100755 RIOT/examples/test_led/udp.c diff --git a/RIOT/examples/real_time_app/main.c b/RIOT/examples/real_time_app/main.c index f2c6786..7d0b6f2 100755 --- a/RIOT/examples/real_time_app/main.c +++ b/RIOT/examples/real_time_app/main.c @@ -104,6 +104,7 @@ int ordre = 0; int64_t offset = 0; int timer_run = 0; int tourne = 0; +int arret = 0; sock_udp_ep_t local = SOCK_IPV6_EP_ANY; sock_udp_t sock; sock_udp_ep_t local_ntp = SOCK_IPV6_EP_ANY; @@ -196,8 +197,10 @@ void *sock_server_thread(void *arg) } if(deadline >= DEADLINE && compteur >=5) { compteur = 0; - if (sock_udp_send(&sock, "d", sizeof("d"), &remote) < 0) { - puts("Error sending reply"); + if(arret == 0) { + if (sock_udp_send(&sock, "d", sizeof("d"), &remote) < 0) { + puts("Error sending reply"); + } } if(tourne == 1) { @@ -205,16 +208,17 @@ void *sock_server_thread(void *arg) pwm_set(PWM_DEV(1),1,960); } if(timer_run == 0) { + puts("Degradation"); gpio_clear(LED1_PIN); gpio_clear(LED2_PIN); gpio_set(LED0_PIN); timer_set(TIMER_DEV(1),0,25200); - /* if (sntp_sync(&server, SOCK_NO_TIMEOUT) < 0) { - puts("Error in synchronization"); - return NULL; - } - offset = sntp_get_offset(); - printf("offset : %i\n",(int)offset);*/ + /* if (sntp_sync(&server, SOCK_NO_TIMEOUT) < 0) { + * puts("Error in synchronization"); + * return NULL; + } + offset = sntp_get_offset(); + printf("offset : %i\n",(int)offset);*/ timer_run = 1; } } @@ -233,6 +237,7 @@ void *sock_server_thread(void *arg) if (sock_udp_send(&sock, "n", sizeof("n"), &remote) < 0) { puts("Error sending reply"); } + arret = 0; gpio_clear(LED2_PIN); gpio_clear(LED0_PIN); gpio_set(LED1_PIN); @@ -242,6 +247,11 @@ void *sock_server_thread(void *arg) } printf("compteur : %d\n",compteur); memset(&buf,0,sizeof(buf)); + if(arret == 1) { + if (sock_udp_send(&sock, "a", sizeof("a"), &remote) < 0) { + puts("Error sending reply"); + } + } } } else { @@ -285,15 +295,6 @@ void *sock_client_thread(void *arg) remote.addr.ipv6[15] = 0x1e; while (1) { - if(vitesse==2) { - gpio_clear(LED0_PIN); - gpio_toggle(LED1_PIN); - } - else { - gpio_clear(LED1_PIN); - gpio_toggle(LED0_PIN); - } - data.heure_actuelle = xtimer_now_usec(); if (sock_udp_send(&sock, &data, sizeof(data), &remote) < 0) { puts("Error sending message"); @@ -307,14 +308,22 @@ void *sock_client_thread(void *arg) } } else { - printf("Received message: \""); - for (int i = 0; i < res; i++) { - printf("%c", buf[i]); - } if(buf[0] == 'n') vitesse = 2; + else if(buf[0] == 'a') { + vitesse = 1; + gpio_set(LED2_PIN); + } else vitesse = 1; - printf("\"\n"); + } + if(vitesse==2) { + gpio_clear(LED0_PIN); + gpio_clear(LED2_PIN); + gpio_toggle(LED1_PIN); + } + else { + gpio_clear(LED1_PIN); + gpio_toggle(LED0_PIN); } xtimer_sleep(vitesse); } @@ -327,10 +336,11 @@ static void arret_urgence(void *arg,int channel) pwm_set(PWM_DEV(1),1,0); tourne = 0; timer_run = 0; + arret = 1; gpio_clear(LED0_PIN); gpio_clear(LED1_PIN); gpio_set(LED2_PIN); - printf("Arret d'urgence\n"); + puts("Arret d'urgence"); } static void _init_timer(void) diff --git a/RIOT/examples/test_led/Makefile b/RIOT/examples/test_led/Makefile new file mode 100755 index 0000000..fb2498a --- /dev/null +++ b/RIOT/examples/test_led/Makefile @@ -0,0 +1,64 @@ +# name of your application +APPLICATION = gnrc_networking + +# If no BOARD is found in the environment, use this default: +BOARD ?= native + +# This has to be the absolute path to the RIOT base directory: +RIOTBASE ?= $(CURDIR)/../.. + +BOARD_INSUFFICIENT_MEMORY := airfy-beacon chronos msb-430 msb-430h nrf51dongle \ + nrf6310 nucleo-f103 nucleo-f334 pca10000 pca10005 spark-core \ + stm32f0discovery telosb weio wsn430-v1_3b wsn430-v1_4 \ + yunjia-nrf51822 z1 nucleo-f072 nucleo-f030 nucleo-f070 \ + microbit calliope-mini +DISABLE_MODULE += net_gnrc_sixlowpan_iphc +# Include packages that pull up and auto-init the link layer. +# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +# Specify the mandatory networking modules for IPv6 and UDP +USEMODULE += gnrc_ipv6_router_default +USEMODULE += gnrc_udp +# Add a routing protocol +USEMODULE += gnrc_rpl +USEMODULE += auto_init_gnrc_rpl +# This application dumps received packets to STDIO using the pktdump module +USEMODULE += gnrc_pktdump +# Additional networking modules that can be dropped if not needed +USEMODULE += gnrc_icmpv6_echo +# Add also the shell, some shell commands +USEMODULE += shell +USEMODULE += shell_commands +USEMODULE += ps +USEMODULE += netstats_l2 +USEMODULE += netstats_ipv6 +USEMODULE += netstats_rpl +USEMODULE += at86rf231 +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +CFLAGS += -DDEVELHELP + +# Comment this out to join RPL DODAGs even if DIOs do not contain +# DODAG Configuration Options (see the doc for more info) +# CFLAGS += -DGNRC_RPL_DODAG_CONF_OPTIONAL_ON_JOIN + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +include $(RIOTBASE)/Makefile.include + +# Set a custom channel if needed +ifneq (,$(filter cc110x,$(USEMODULE))) # radio is cc110x sub-GHz + DEFAULT_CHANNEL ?= 0 + CFLAGS += -DCC110X_DEFAULT_CHANNEL=$(DEFAULT_CHANNEL) +else + ifneq (,$(filter at86rf212b,$(USEMODULE))) # radio is IEEE 802.15.4 sub-GHz + DEFAULT_CHANNEL ?= 5 + FLAGS += -DIEEE802154_DEFAULT_SUBGHZ_CHANNEL=$(DEFAULT_CHANNEL) + else # radio is IEEE 802.15.4 2.4 GHz + DEFAULT_CHANNEL ?= 26 + CFLAGS += -DIEEE802154_DEFAULT_CHANNEL=$(DEFAULT_CHANNEL) + endif +endif diff --git a/RIOT/examples/test_led/README.md b/RIOT/examples/test_led/README.md new file mode 100755 index 0000000..fd32831 --- /dev/null +++ b/RIOT/examples/test_led/README.md @@ -0,0 +1,142 @@ +# gnrc_networking example + +This example shows you how to try out the code in two different ways: Either by communicating +between the RIOT machine and its Linux host, or by communicating between two RIOT instances. +Note that the former only works with native, i.e. if you run RIOT on your Linux machine. + +## Connecting RIOT native and the Linux host + +> **Note:** RIOT does not support IPv4, so you need to stick to IPv6 anytime. To +establish a connection between RIOT and the Linux host, you will need `netcat` +(with IPv6 support). Ubuntu 14.04 comes with netcat IPv6 support pre-installed. +On Debian it's available in the package `netcat-openbsd`. Be aware that many +programs require you to add an option such as -6 to tell them to use IPv6, otherwise they +will fail. If you're using a _Raspberry Pi_, run `sudo modprobe ipv6` before trying +this example, because raspbian does not load the IPv6 module automatically. +On some systems (openSUSE for example), the _firewall_ may interfere, and prevent +some packets to arrive at the application (they will however show up in Wireshark, +which can be confusing). So be sure to adjust your firewall rules, or turn it off +(who needs security anyway). + +First, create a tap interface: + + sudo ip tuntap add tap0 mode tap user ${USER} + sudo ip link set tap0 up + +Now you can start the `gnrc_networking` example by invoking `make term`. This should +automatically connect to the `tap0` interface. If this doesn't work for any reason, +run make term with the tap0 interface as the PORT environment variable: + + PORT=tap0 make term + +To verify that there is connectivity between RIOT and Linux, go to the RIOT console and run `ifconfig`: + + > ifconfig + Iface 7 HWaddr: ce:f5:e1:c5:f7:5a + inet6 addr: ff02::1/128 scope: local [multicast] + inet6 addr: fe80::ccf5:e1ff:fec5:f75a/64 scope: local + inet6 addr: ff02::1:ffc5:f75a/128 scope: local [multicast] + +Copy the [link-local address](https://en.wikipedia.org/wiki/Link-local_address) +of the RIOT node (prefixed with `fe80`) and try to ping it **from the Linux node**: + + ping6 fe80::ccf5:e1ff:fec5:f75a%tap0 + +Note that the interface on which to send the ping needs to be appended to the IPv6 +address, `%tap0` in the above example. When talking to the RIOT node, you always want +to send to/receive from the `tap0` interface. + +If the pings succeed you can go on to send UDP packets. To do that, first start a +UDP server on the RIOT node: + + > udp server start 8808 + Success: started UDP server on port 8808 + +Now, on the Linux host, you can run netcat to connect with RIOT's UDP server: + + nc -6uv fe80::ccf5:e1ff:fec5:f75a%tap0 8808 + +The `-6` option is necessary to tell netcat to use IPv6 only, the `-u` option tells +it to use UDP only, and the `-v` option makes it give more verbose output (this one is optional). + +You should now see that UDP messages are received on the RIOT side. Opening a UDP +server on the Linux side is also possible. To do that, write down the IP address +of the host (run on Linux): + + ifconfig tap0 + tap0 Link encap:Ethernet HWaddr ce:f5:e1:c5:f7:59 + inet6 addr: fe80::4049:5fff:fe17:b3ae/64 Scope:Link + UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 + RX packets:6 errors:0 dropped:0 overruns:0 frame:0 + TX packets:36 errors:0 dropped:0 overruns:0 carrier:0 + collisions:0 txqueuelen:0 + RX bytes:488 (488.0 B) TX bytes:3517 (3.5 KB) + +Then open a UDP server on Linux (the `-l` option makes netcat listen for incoming connections): + + nc -6ul 8808 + +Now, on the RIOT side, send a UDP packet using: + + udp send fe80::4049:5fff:fe17:b3ae 8808 testmessage + +You should see `testmessage` appear in netcat. Instead of using netcat, you can of +course write your own software, but you may have to bind the socket to a specific +interface (tap0 in this case). For an example that shows how to do so, see +[here](https://gist.github.com/backenklee/dad5e80b764b3b3d0d3e). + +## Connecting two RIOT instances + +When using native (i.e. when you're trying this on your Linux machine), you first +need to set up two tap devices and a bridge that connects them. This constitutes a +virtual network that the RIOT instances can use to communicate. + + ./../../dist/tools/tapsetup/tapsetup --create 2 + +Then, make sure you've compiled the application by calling `make` and start the +first RIOT instance by invoking `make term`. In the RIOT shell, get to know the +IP address of this node: + + > ifconfig + Iface 7 HWaddr: ce:f5:e1:c5:f7:5a + inet6 addr: ff02::1/128 scope: local [multicast] + inet6 addr: fe80::ccf5:e1ff:fec5:f75a/64 scope: local + inet6 addr: ff02::1:ffc5:f75a/128 scope: local [multicast] + +and start a UDP server. + + > udp server start 8808 + +This node is now ready to receive data on port `8808`. + +In a second terminal, start a second RIOT instance, this time listening on `tap1`: + + PORT=tap1 make term + +In the RIOT shell, you can now send a message to the first RIOT instance: + + > udp send fe80::ccf5:e1ff:fec5:f75 8808 testmessage + +*(Make sure to copy the actual +[link-local address](https://en.wikipedia.org/wiki/Link-local_address) of your first +RIOT instance into the above command)* + +In your first terminal, you should now see output that looks like this. + + > PKTDUMP: data received: + ~~ SNIP 0 - size: 11 byte, type: NETTYPE_UNDEF (0) + 000000 74 65 73 74 6d 65 73 73 61 67 65 + ~~ SNIP 1 - size: 8 byte, type: NETTYPE_UDP (3) + src-port: 8808 dst-port: 8808 + length: 19 cksum: 0x4d95f + ~~ SNIP 2 - size: 40 byte, type: NETTYPE_IPV6 (1) + traffic class: 0x00 (ECN: 0x0, DSCP: 0x00) + flow label: 0x00000 + length: 19 next header: 17 hop limit: 64 + source address: fe80::a08a:84ff:fe68:544f + destination address: fe80::60fc:3cff:fe5e:40df + ~~ SNIP 3 - size: 20 byte, type: NETTYPE_NETIF (-1) + if_pid: 6 rssi: 0 lqi: 0 + src_l2addr: a2:8a:84:68:54:4f + dst_l2addr: 62:fc:3c:5e:40:df + ~~ PKT - 4 snips, total size: 79 byte diff --git a/RIOT/examples/test_led/main.c b/RIOT/examples/test_led/main.c new file mode 100755 index 0000000..b5fa8af --- /dev/null +++ b/RIOT/examples/test_led/main.c @@ -0,0 +1,52 @@ +/* + * 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 "shell.h" +#include "msg.h" +#include "board.h" +#include "periph/gpio.h" + +#define MAIN_QUEUE_SIZE (8) +static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; + +extern int udp_cmd(int argc, char **argv); + +static const shell_command_t shell_commands[] = { + { "udp", "send data over UDP and listen on UDP ports", udp_cmd }, + { NULL, NULL, NULL } +}; + +int main(void) +{ + /* we need a message queue for the thread running the shell in order to + * receive potentially fast incoming networking packets */ + msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); + puts("RIOT network stack example application"); + gpio_set(LED0_PIN); + /* 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; +} diff --git a/RIOT/examples/test_led/udp.c b/RIOT/examples/test_led/udp.c new file mode 100755 index 0000000..f7619a8 --- /dev/null +++ b/RIOT/examples/test_led/udp.c @@ -0,0 +1,174 @@ +/* + * 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 Demonstrating the sending and receiving of UDP data + * + * @author Hauke Petersen + * + * @} + */ + +#include +#include + +#include "net/gnrc.h" +#include "net/gnrc/ipv6.h" +#include "net/gnrc/udp.h" +#include "net/gnrc/pktdump.h" +#include "timex.h" +#include "xtimer.h" + +static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, + KERNEL_PID_UNDEF); + + +static void send(char *addr_str, char *port_str, char *data, unsigned int num, + unsigned int delay) +{ + uint16_t port; + ipv6_addr_t addr; + + /* parse destination address */ + if (ipv6_addr_from_str(&addr, addr_str) == NULL) { + puts("Error: unable to parse destination address"); + return; + } + /* parse port */ + port = (uint16_t)atoi(port_str); + if (port == 0) { + puts("Error: unable to parse destination port"); + return; + } + + for (unsigned int i = 0; i < num; i++) { + gnrc_pktsnip_t *payload, *udp, *ip; + unsigned payload_size; + /* allocate payload */ + payload = gnrc_pktbuf_add(NULL, data, strlen(data), GNRC_NETTYPE_UNDEF); + if (payload == NULL) { + puts("Error: unable to copy data to packet buffer"); + return; + } + /* store size for output */ + payload_size = (unsigned)payload->size; + /* allocate UDP header, set source port := destination port */ + udp = gnrc_udp_hdr_build(payload, port, port); + if (udp == NULL) { + puts("Error: unable to allocate UDP header"); + gnrc_pktbuf_release(payload); + return; + } + /* allocate IPv6 header */ + ip = gnrc_ipv6_hdr_build(udp, NULL, &addr); + if (ip == NULL) { + puts("Error: unable to allocate IPv6 header"); + gnrc_pktbuf_release(udp); + return; + } + /* send packet */ + if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) { + puts("Error: unable to locate UDP thread"); + gnrc_pktbuf_release(ip); + return; + } + /* access to `payload` was implicitly given up with the send operation above + * => use temporary variable for output */ + printf("Success: send %u byte to [%s]:%u\n", payload_size, addr_str, + port); + xtimer_usleep(delay); + } +} + +static void start_server(char *port_str) +{ + uint16_t port; + + /* check if server is already running */ + if (server.target.pid != KERNEL_PID_UNDEF) { + printf("Error: server already running on port %" PRIu32 "\n", + server.demux_ctx); + return; + } + /* parse port */ + port = (uint16_t)atoi(port_str); + if (port == 0) { + puts("Error: invalid port specified"); + return; + } + /* start server (which means registering pktdump for the chosen port) */ + server.target.pid = gnrc_pktdump_pid; + server.demux_ctx = (uint32_t)port; + gnrc_netreg_register(GNRC_NETTYPE_UDP, &server); + printf("Success: started UDP server on port %" PRIu16 "\n", port); +} + +static void stop_server(void) +{ + /* check if server is running at all */ + if (server.target.pid == KERNEL_PID_UNDEF) { + printf("Error: server was not running\n"); + return; + } + /* stop server */ + gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &server); + server.target.pid = KERNEL_PID_UNDEF; + puts("Success: stopped UDP server"); +} + +int udp_cmd(int argc, char **argv) +{ + if (argc < 2) { + printf("usage: %s [send|server]\n", argv[0]); + return 1; + } + + if (strcmp(argv[1], "send") == 0) { + uint32_t num = 1; + uint32_t delay = 1000000; + if (argc < 5) { + printf("usage: %s send [ []]\n", + argv[0]); + return 1; + } + if (argc > 5) { + num = (uint32_t)atoi(argv[5]); + } + if (argc > 6) { + delay = (uint32_t)atoi(argv[6]); + } + send(argv[2], argv[3], argv[4], num, delay); + } + else if (strcmp(argv[1], "server") == 0) { + if (argc < 3) { + printf("usage: %s server [start|stop]\n", argv[0]); + return 1; + } + if (strcmp(argv[2], "start") == 0) { + if (argc < 4) { + printf("usage %s server start \n", argv[0]); + return 1; + } + start_server(argv[3]); + } + else if (strcmp(argv[2], "stop") == 0) { + stop_server(); + } + else { + puts("error: invalid command"); + } + } + else { + puts("error: invalid command"); + } + return 0; +} -- libgit2 0.21.2