From a345a9dfa670a193b219b5fab9fa598d8af852da Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Mon, 14 Sep 2015 17:00:23 +0200 Subject: [PATCH] examples: add POSIX socket example --- examples/posix_sockets/Makefile | 36 ++++++ examples/posix_sockets/README.md | 92 +++++++++++++++ examples/posix_sockets/main.c | 44 ++++++++ examples/posix_sockets/udp.c | 187 +++++++++++++++++++++++++++++++ 4 files changed, 359 insertions(+) create mode 100644 examples/posix_sockets/Makefile create mode 100644 examples/posix_sockets/README.md create mode 100644 examples/posix_sockets/main.c create mode 100644 examples/posix_sockets/udp.c diff --git a/examples/posix_sockets/Makefile b/examples/posix_sockets/Makefile new file mode 100644 index 0000000000..4b0946a527 --- /dev/null +++ b/examples/posix_sockets/Makefile @@ -0,0 +1,36 @@ +# name of your application +APPLICATION = posix_sockets + +# 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-f334 pca10000 pca10005 stm32f0discovery telosb wsn430-v1_3b \ + wsn430-v1_4 yunjia-nrf51822 z1 + +# 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_netif_default +USEMODULE += auto_init_gnrc_netif +# Specify the mandatory networking modules for socket communication via UDP +USEMODULE += gnrc_ipv6_default +USEMODULE += gnrc_udp +USEMODULE += gnrc_conn_udp +USEMODULE += posix_sockets +# Add also the shell, some shell commands +USEMODULE += shell +USEMODULE += shell_commands +USEMODULE += ps + +# 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 + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +include $(RIOTBASE)/Makefile.include diff --git a/examples/posix_sockets/README.md b/examples/posix_sockets/README.md new file mode 100644 index 0000000000..75305fe9b3 --- /dev/null +++ b/examples/posix_sockets/README.md @@ -0,0 +1,92 @@ +examples/posix_sockets +====================== +This application is a showcase for RIOT's POSIX socket support. To +keep things simple this application has only one-hop support and +no routing capabilities. + +Usage +===== + +Build, flash and start the application: +``` +export BOARD=your_board +make +make flash +make term +``` + +The `term` make target starts a terminal emulator for your board. It +connects to a default port so you can interact with the shell, usually +that is `/dev/ttyUSB0`. If your port is named differently, the +`PORT=/dev/yourport` (not to be confused with the UDP port) variable can +be used to override this. + + +Example output +============== + +The shell commands come with online help. Call `help` to see which commands +exist and what they do. + + +udp send fe80::1 1337 uiaeue +2015-09-22 14:55:30,686 - INFO # > udp send fe80::1 1337 uiaeue +2015-09-22 14:55:30,690 - INFO # Success: send 6 byte to fe80::1:1337 + +Running the `help` command on an iotlab-m3: +``` +2015-09-22 14:54:54,442 - INFO # help +2015-09-22 14:54:54,443 - INFO # Command Description +2015-09-22 14:54:54,444 - INFO # --------------------------------------- +2015-09-22 14:54:54,446 - INFO # udp send data over UDP and listen on UDP ports +2015-09-22 14:54:54,447 - INFO # reboot Reboot the node +2015-09-22 14:54:54,449 - INFO # ps Prints information about running threads. +2015-09-22 14:54:54,451 - INFO # mersenne_init initializes the PRNG +2015-09-22 14:54:54,453 - INFO # mersenne_get returns 32 bit of pseudo randomness +2015-09-22 14:54:54,454 - INFO # ifconfig Configure network interfaces +2015-09-22 14:54:54,455 - INFO # txtsnd send raw data +2015-09-22 14:54:54,457 - INFO # ncache manage neighbor cache by hand +2015-09-22 14:54:54,459 - INFO # routers IPv6 default router list +``` + +Running the `ps` command on an iotlab-m3: + +``` +2015-09-22 14:54:57,134 - INFO # > ps +2015-09-22 14:54:57,139 - INFO # pid | name | state Q | pri | stack ( used) | location +2015-09-22 14:54:57,143 - INFO # 1 | idle | pending Q | 15 | 256 ( 136) | 0x200001cc +2015-09-22 14:54:57,157 - INFO # 2 | main | pending Q | 7 | 1536 ( 620) | 0x200002cc +2015-09-22 14:54:57,164 - INFO # 3 | 6lo | bl rx _ | 3 | 1024 ( 404) | 0x20003ef8 +2015-09-22 14:54:57,169 - INFO # 4 | ipv6 | bl rx _ | 4 | 1024 ( 436) | 0x20001cc0 +2015-09-22 14:54:57,172 - INFO # 5 | udp | bl rx _ | 5 | 1024 ( 268) | 0x20004660 +2015-09-22 14:54:57,177 - INFO # 6 | at86rfxx | bl rx _ | 3 | 1024 ( 320) | 0x20001888 +2015-09-22 14:54:57,183 - INFO # | SUM | | | 5888 ( 2184) +``` + +Start a UDP server with `udp server start `: + +``` +2015-09-22 14:55:09,563 - INFO # > udp server start 1337 +2015-09-22 14:55:09,564 - INFO # Success: started UDP server on port 1337 +``` + +Send a UDP package with `udp send `: + +``` +2015-09-22 14:55:30,686 - INFO # > udp send fe80::3432:4833:46d4:9e06 1337 test +2015-09-22 14:55:30,690 - INFO # Success: send 4 byte to [fe80::3432:4833:46d4:9e06]:1337 +``` + +You can get the IPv6 address of the destination by using the `ifconfig` command on the receiver: + +``` +2015-09-22 14:58:10,394 - INFO # ifconfig +2015-09-22 14:58:10,397 - INFO # Iface 6 HWaddr: 9e:06 Channel: 26 NID: 0x23 TX-Power: 0dBm State: IDLE CSMA Retries: 4 +2015-09-22 14:58:10,399 - INFO # Long HWaddr: 36:32:48:33:46:d4:9e:06 +2015-09-22 14:58:10,400 - INFO # AUTOACK CSMA MTU:1280 6LO IPHC +2015-09-22 14:58:10,402 - INFO # Source address length: 8 +2015-09-22 14:58:10,404 - INFO # Link type: wireless +2015-09-22 14:58:10,407 - INFO # inet6 addr: ff02::1/128 scope: local [multicast] +2015-09-22 14:58:10,415 - INFO # inet6 addr: fe80::3432:4833:46d4:9e06/64 scope: local +2015-09-22 14:58:10,416 - INFO # +``` diff --git a/examples/posix_sockets/main.c b/examples/posix_sockets/main.c new file mode 100644 index 0000000000..995019ad8c --- /dev/null +++ b/examples/posix_sockets/main.c @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 Martine Lenders + * + * 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's POSIX sockets + * + * @author Martine Lenders + * + * @} + */ + +#include + +#include "shell.h" + +#define MAIN_MSG_QUEUE_SIZE (1) + +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) +{ + puts("RIOT socket example application"); + /* 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/examples/posix_sockets/udp.c b/examples/posix_sockets/udp.c new file mode 100644 index 0000000000..d29d560e84 --- /dev/null +++ b/examples/posix_sockets/udp.c @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2015 Martine Lenders + * + * 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 over POSIX sockets. + * + * @author Martine Lenders + * + * @} + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "thread.h" + +#define SERVER_MSG_QUEUE_SIZE (8) +#define SERVER_BUFFER_SIZE (64) + +static int server_socket = -1; +static char server_buffer[SERVER_BUFFER_SIZE]; +static char server_stack[THREAD_STACKSIZE_DEFAULT]; +static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE]; + +static void *_server_thread(void *args) +{ + struct sockaddr_in6 server_addr; + uint16_t port; + msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE); + server_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + /* parse port */ + port = (uint16_t)atoi((char *)args); + if (port == 0) { + puts("Error: invalid port specified"); + return NULL; + } + server_addr.sin6_family = AF_INET6; + memset(&server_addr.sin6_addr, 0, sizeof(server_addr.sin6_addr)); + server_addr.sin6_port = htons(port); + if (server_socket < 0) { + puts("error initializing socket"); + server_socket = 0; + return NULL; + } + if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + server_socket = -1; + puts("error binding socket"); + return NULL; + } + printf("Success: started UDP server on port %" PRIu16 "\n", port); + while (1) { + int res; + struct sockaddr_in6 src; + socklen_t src_len = sizeof(struct sockaddr_in6); + if ((res = recvfrom(server_socket, server_buffer, sizeof(server_buffer), 0, + (struct sockaddr *)&src, &src_len)) < 0) { + puts("Error on receive"); + } + else if (res == 0) { + puts("Peer did shut down"); + } + else { + printf("Received data: "); + puts(server_buffer); + } + } + return NULL; +} + +static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num, + unsigned int delay) +{ + struct sockaddr_in6 src, dst; + size_t data_len = strlen(data); + uint16_t port; + int s; + src.sin6_family = AF_INET6; + dst.sin6_family = AF_INET6; + memset(&src.sin6_addr, 0, sizeof(src.sin6_addr)); + /* parse destination address */ + if (inet_pton(AF_INET6, addr_str, &dst.sin6_addr) != 1) { + puts("Error: unable to parse destination address"); + return 1; + } + /* parse port */ + port = (uint16_t)atoi(port_str); + dst.sin6_port = htons(port); + src.sin6_port = htons(port); + s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + if (s < 0) { + puts("error initializing socket"); + return 1; + } + for (unsigned int i = 0; i < num; i++) { + if (sendto(s, data, data_len, 0, (struct sockaddr *)&dst, sizeof(dst)) < 0) { + puts("could not send"); + } + else { + printf("Success: send %u byte to %s:%u\n", (unsigned)data_len, addr_str, port); + } + + usleep(delay); + } + close(s); + return 0; +} + +static int udp_start_server(char *port_str) +{ + /* check if server is already running */ + if (server_socket >= 0) { + puts("Error: server already running"); + return 1; + } + /* start server (which means registering pktdump for the chosen port) */ + if (thread_create(server_stack, sizeof(server_stack), THREAD_PRIORITY_MAIN - 1, + CREATE_STACKTEST, _server_thread, port_str, "UDP server") <= KERNEL_PID_UNDEF) { + server_socket = -1; + puts("error initializing thread"); + return 1; + } + return 0; +} + +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]); + } + return udp_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; + } + return udp_start_server(argv[3]); + } + else { + puts("error: invalid command"); + return 1; + } + } + else { + puts("error: invalid command"); + return 1; + } +} + +/** @} */