From c9c7bf8d41df2fb91f304cef418d980d7ae8e2a0 Mon Sep 17 00:00:00 2001 From: Hauke Petersen <hauke.petersen@fu-berlin.de> Date: Tue, 11 Sep 2018 15:09:22 +0200 Subject: [PATCH] examples: add example for rdcli --- examples/rdcli/Makefile | 33 ++++++++++++ examples/rdcli/README.md | 20 ++++++++ examples/rdcli/main.c | 108 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 examples/rdcli/Makefile create mode 100644 examples/rdcli/README.md create mode 100644 examples/rdcli/main.c diff --git a/examples/rdcli/Makefile b/examples/rdcli/Makefile new file mode 100644 index 0000000000..3d79768331 --- /dev/null +++ b/examples/rdcli/Makefile @@ -0,0 +1,33 @@ +# name of your application +APPLICATION = rdcli + +# 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)/../.. + +USEMODULE += gnrc_netdev_default +USEMODULE += auto_init_gnrc_netif +USEMODULE += gnrc_ipv6_default +USEMODULE += gnrc_icmpv6_echo + +USEMODULE += rdcli_standalone + +USEMODULE += shell +USEMODULE += shell_commands +USEMODULE += ps +USEMODULE += fmt + +# 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 + +# For debugging and demonstration purposes, we limit the lifetime to 60s +CFLAGS += -DRDCLI_LT=60 + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +include $(RIOTBASE)/Makefile.include diff --git a/examples/rdcli/README.md b/examples/rdcli/README.md new file mode 100644 index 0000000000..315e6087e6 --- /dev/null +++ b/examples/rdcli/README.md @@ -0,0 +1,20 @@ +CoRE Resource Directory Client Example +====================================== +This example application demonstrates the usage of RIOT's Resource Directory +(RD) client module, called `rdcli`. This module supports the registration, +update, and removal processes as defined in the +[Resource Directory Draft](https://tools.ietf.org/html/draft-ietf-core-resource-directory-14). + +Usage +===== +The examples includes a shell command that you can use to interact with a given +RD server, called `rdcli`. Simply use that shell command without parameters for +more information on its usage. + +Some connection parameters are configured statically during compile time, +namely the lifetime (`RDCLI_LT`) and the node's endpoint name (`RDCLI_EP`). You +can change these values during command line by overriding these values in the +application's Makefile, e.g. add +``` +CFLAGS += "-DRDCLI_EP=\"MyNewEpName\"" +``` diff --git a/examples/rdcli/main.c b/examples/rdcli/main.c new file mode 100644 index 0000000000..f60f794a96 --- /dev/null +++ b/examples/rdcli/main.c @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2017-2018 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 CoRE Resource Directory client (rdcli) example application + * + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> + * + * @} + */ + +#include <stdio.h> + +#include "fmt.h" +#include "shell.h" +#include "net/ipv6/addr.h" +#include "net/gcoap.h" +#include "net/rdcli_common.h" +#include "net/rdcli_standalone.h" + +#define MAIN_QUEUE_SIZE (8) +static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; + +/* we will use a custom event handler for dumping rdcli_standalone events */ +static void _on_rdcli_event(rdcli_standalone_event_t event) +{ + switch (event) { + case RDCLI_REGISTERED: + puts("rdcli event: now registered with a RD"); + break; + case RDCLI_DEREGISTERED: + puts("rdcli event: dropped client registration"); + break; + case RDCLI_UPDATED: + puts("rdcli event: successfully updated client registration"); + break; + } +} + +/* define some dummy CoAP resources */ +static ssize_t _handler_dummy(coap_pkt_t *pdu, + uint8_t *buf, size_t len, void *ctx) +{ + (void)ctx; + + /* get random data */ + int16_t val = 23; + + gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); + size_t plen = fmt_s16_dec((char *)pdu->payload, val); + return gcoap_finish(pdu, plen, COAP_FORMAT_TEXT); +} + +static ssize_t _handler_info(coap_pkt_t *pdu, + uint8_t *buf, size_t len, void *ctx) +{ + (void)ctx; + + gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); + size_t slen = sizeof("SOME NODE INFOMRATION"); + memcpy(pdu->payload, "SOME NODE INFOMRATION", slen); + return gcoap_finish(pdu, slen, COAP_FORMAT_TEXT); +} + +static const coap_resource_t _resources[] = { + { "/node/info", COAP_GET, _handler_info, NULL }, + { "/sense/hum", COAP_GET, _handler_dummy, NULL }, + { "/sense/temp", COAP_GET, _handler_dummy, NULL } +}; + +static gcoap_listener_t _listener = { + .resources = (coap_resource_t *)&_resources[0], + .resources_len = sizeof(_resources) / sizeof(_resources[0]), + .next = 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("CoRE RD client example!\n"); + + /* setup CoAP resources */ + gcoap_register_listener(&_listener); + + /* register event callback with rdcli_standalone */ + rdcli_standalone_reg_cb(_on_rdcli_event); + + puts("Client information:"); + printf(" ep: %s\n", rdcli_common_get_ep()); + printf(" lt: %is\n", (int)RDCLI_LT); + + char line_buf[SHELL_DEFAULT_BUFSIZE]; + shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE); + + return 0; +} -- GitLab