Merge pull request #10464 from haukepetersen/opt_cordepsim_addrhandling

examples/cord_epsim: change default RD server addr handling
This commit is contained in:
Martine Lenders 2019-03-27 18:57:54 +01:00 committed by GitHub
commit 78247d9e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 130 additions and 157 deletions

View File

@ -792,11 +792,6 @@ ifneq (,$(filter bluetil_addr,$(USEMODULE)))
USEMODULE += fmt
endif
ifneq (,$(filter cord_epsim_standalone,$(USEMODULE)))
USEMODULE += cord_epsim
USEMODULE += xtimer
endif
ifneq (,$(filter cord_epsim,$(USEMODULE)))
USEMODULE += cord_common
USEMODULE += gcoap

View File

@ -20,12 +20,10 @@ USEMODULE += auto_init_gnrc_netif
USEMODULE += gnrc_ipv6_default
# Run the simple CoRE resource directory
USEMODULE += cord_epsim_standalone
USEMODULE += cord_epsim
# Include the shell for testing purposes
USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps
# We also use the xtimer for periodic RD entry updates in this example
USEMODULE += xtimer
# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
@ -35,13 +33,17 @@ CFLAGS += -DDEVELHELP
# For debugging and demonstration purposes, we limit the lifetime to the minimal
# allowed value of 60s (see draft-ietf-core-resource-directory-11, Table 2)
RD_LT ?= 60
# Override this variable to set the RD server address (default is the all nodes
# multicast address)
RD_ADDR ?= \"ff02::1\"
RD_PORT ?= 5683
CFLAGS += -DCORD_LT=$(RD_LT)
CFLAGS += -DCORD_SERVER_ADDR=$(RD_ADDR)
CFLAGS += -DCORD_SERVER_PORT=$(RD_PORT)
# If nothing else is defined, we use CoAP default port
RD_PORT ?= COAP_PORT
CFLAGS += -DRD_PORT=$(RD_PORT)
# The RD server's address must be defined by the build environment by setting
# the RD_ADDR environment variable. Per default, this value is set to the
# loopback address for enabling the build tests to successfully build this
# example.
RD_ADDR ?= \"affe::1\"
CFLAGS += -DRD_ADDR=$(RD_ADDR)
include $(RIOTBASE)/Makefile.include

View File

@ -5,20 +5,23 @@ This example shows how a node can register with a CoRE resource directory using
the simple registration procedure as described in
draft-ietf-core-resource-directory-15, section 5.3.1.
The registration process needs an endpoint name as well as a lifetime for the
registry entry. These are statically defined during compile time and you can
edit these values by overriding `CORD_EP` and `CORD_LT`:
```
CFLAGS="-DCORD_LT=\"7200\" -DCORD_EP=\"MyNode\"" make all
```
Per default, the node is looking for the CoRE RD at the all nodes link-local
multicast address [FF02::1]:5683. To change the target address or port, simply
override the `RD_ADDR` and `RD_PORT` variables, e.g.:
```
RD_ADDR=\\\"::1\\\" RD_PORT=12345 make all
```
or
When running this example, you **must** define the RD server's IPv6 address
statically, using the `RD_ADDR` environment variable:
```
RD_ADDR=\\\"abc:0815::123\\\" make all
```
Per default, this is set to some random global address (`affe::1`) for compile
test reasons. So change it!
Additionally, you can change the RD server's port by overriding the `RD_PORT`
variable:
```
RD_ADDR=\\\"affe::bee\\\" RD_PORT=12345 make all
```
The registration lifetime is set for demonstration purposes to a rather short
time of 60s in this example. You can easily override that value using the
`RD_LT` variable:
```
RD_ADDR=\\\"abc::def\\\" RD_LT=3600 make all ...
```

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Freie Universität Berlin
* Copyright (C) 2017-2019 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
@ -12,7 +12,7 @@
*
* @file
* @brief Test application demonstrating the simple registration
* process to a CoRE RD using gcoap
* process to a CoRE RD using gcoap and cord_epsim
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
@ -21,11 +21,12 @@
#include <stdio.h>
#include "shell.h"
#include "net/gcoap.h"
#include "net/cord/epsim.h"
#include "net/cord/common.h"
#define BUFSIZE (64U)
#define STARTUP_DELAY (3U) /* wait 3s before sending first request*/
static char riot_info[BUFSIZE];
@ -66,25 +67,62 @@ static gcoap_listener_t listener = {
int main(void)
{
puts("CoAP simplified RD registration example!\n");
puts("Simplified CoRE RD registration example\n");
/* fill riot info */
sprintf(riot_info, "{\"ep\":\"%s\",\"lt\":%u}",
cord_common_get_ep(), CORD_LT);
/* parse RD address information */
sock_udp_ep_t rd_ep = {
.family = AF_INET6,
.netif = SOCK_ADDR_ANY_NETIF,
.port = RD_PORT,
};
/* parse RD server address */
if (ipv6_addr_from_str((ipv6_addr_t *)&rd_ep.addr.ipv6, RD_ADDR) == NULL) {
puts("error: unable to parse RD address from RD_ADDR variable");
return 1;
}
/* register resource handlers with gcoap */
gcoap_register_listener(&listener);
/* print RD client information */
puts("RD client information:");
printf(" RD addr: %s\n", CORD_SERVER_ADDR);
printf(" RD port: %u\n", (unsigned)CORD_SERVER_PORT);
printf(" ep: %s\n", cord_common_get_ep());
printf(" lt: %is\n", (int)CORD_LT);
puts("epsim configuration:");
printf(" ep: %s\n", cord_common_get_ep());
printf(" lt: %is\n", (int)CORD_LT);
printf(" RD address: [%s]:%u\n\n", RD_ADDR, (unsigned)RD_PORT);
/* run the shell */
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
xtimer_sleep(STARTUP_DELAY);
while (1) {
int res = cord_epsim_state();
switch (res) {
case CORD_EPSIM_OK:
puts("state: registration active");
break;
case CORD_EPSIM_BUSY:
puts("state: registration in progress");
break;
case CORD_EPSIM_ERROR:
default:
puts("state: not registered");
break;
}
printf("updating registration with RD [%s]:%u\n", RD_ADDR,
(unsigned)RD_PORT);
res = cord_epsim_register(&rd_ep);
if (res == CORD_EPSIM_BUSY) {
puts("warning: registration already in progress");
}
else if (res == CORD_EPSIM_ERROR) {
puts("error: unable to trigger simple registration process");
}
xtimer_sleep(CORD_UPDATE_INTERVAL);
}
return 0;
}

View File

@ -6,7 +6,6 @@ PSEUDOMODULES += can_raw
PSEUDOMODULES += ccn-lite-utils
PSEUDOMODULES += conn_can_isotp_multi
PSEUDOMODULES += cord_ep_standalone
PSEUDOMODULES += cord_epsim_standalone
PSEUDOMODULES += core_%
PSEUDOMODULES += cortexm_fpu
PSEUDOMODULES += ecc_%

View File

@ -168,11 +168,6 @@ void auto_init(void)
extern void cord_ep_standalone_run(void);
cord_ep_standalone_run();
#endif
#ifdef MODULE_CORD_EPSIM_STANDALONE
DEBUG("Auto init cord_epsim module\n");
extern void cord_epsim_run(void);
cord_epsim_run();
#endif
#ifdef MODULE_ASYMCUTE
DEBUG("Auto init Asymcute\n");
asymcute_handler_run();

View File

@ -75,21 +75,6 @@ extern "C" {
#endif
/** @} */
/**
* @brief Use ALL_NODES multicast address as default address when looking for
* a RD server
*/
#ifndef CORD_SERVER_ADDR
#define CORD_SERVER_ADDR "ff02::1"
#endif
/**
* @brief Default Port to use when looking for RDs
*/
#ifndef CORD_SERVER_PORT
#define CORD_SERVER_PORT COAP_PORT
#endif
#ifdef __cplusplus
}
#endif

View File

@ -11,6 +11,12 @@
* @ingroup net_cord
* @brief CoRE Resource Directory endpoint using the simple registration
* procedure
*
* This module is designed to provide nodes with the possibility to register
* with resource directories without having to allocate a lot of resources. All
* the user has to do, is to call the cord_epsim_register() function in periodic
* intervals, depending on the value of the `CORD_LT` variable.
*
* @{
*
* @file
@ -22,6 +28,8 @@
#ifndef NET_CORD_EPSIM_H
#define NET_CORD_EPSIM_H
#include "net/sock/udp.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -31,34 +39,33 @@ extern "C" {
*/
enum {
CORD_EPSIM_OK = 0, /**< all good */
CORD_EPSIM_NOADDR = -1, /**< on address conversion errors */
CORD_EPSIM_ERROR = -2, /**< on other errors */
CORD_EPSIM_ERROR = -1, /**< on failing to send POST request */
CORD_EPSIM_BUSY = -2, /**< endpoint registration is in progress */
};
/**
* @brief Initiate the node registration by sending an empty CoAP POST message
* to the RD server's /.well-known/core resource
*
* @pre remote != NULL
*
* @param[in] remote address and port of the target resource directory
*
* @return CORD_EPSIM_OK on success
* @return CORD_EPSIM_NOADDR if conversion of RD address fails
* @return CORD_EPSIM_BUSY if registration is already in progress
* @return CORD_EPSIM_ERROR if something goes wrong preparing or sending the
* initial request
* registration request
*/
int cord_epsim_register(void);
int cord_epsim_register(const sock_udp_ep_t *remote);
#if defined(MODULE_CORD_EPSIM_STANDALONE) || defined(DOXYGEN)
/**
* @brief Spawn a new thread that registers the node and updates the
* registration with all responding RDs using the simple registration
* process
* @brief Get the status of the latest registration procedure
*
* @note Only available with the `cord_epsim_standalone` module compiled in
*
* @warning This function must only be called once (typically during system
* initialization)
* @return CORD_EPSIM_OK if last registration was triggered successfully
* @return CORD_EPSIM_BUSY if a registration is currently in progress
* @return CORD_EPSIM_ERROR if latest registration attempt failed
*/
void cord_epsim_run(void);
#endif
int cord_epsim_state(void);
#ifdef __cplusplus
}

View File

@ -2,8 +2,4 @@ MODULE = cord_epsim
SRC = cord_epsim.c
ifneq (,$(filter cord_epsim_standalone,$(USEMODULE)))
SRC += cord_epsim_standalone.c
endif
include $(RIOTBASE)/Makefile.base

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2018 Freie Universität Berlin
* Copyright (C) 2017-2019 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
@ -20,6 +20,7 @@
#include <string.h>
#include "assert.h"
#include "net/gcoap.h"
#include "net/cord/epsim.h"
#include "net/cord/config.h"
@ -33,18 +34,23 @@
static coap_pkt_t pkt;
static uint8_t buf[BUFSIZE];
int cord_epsim_register(void)
{
sock_udp_ep_t remote = {
.family = AF_INET6,
.netif = SOCK_ADDR_ANY_NETIF,
.port = CORD_SERVER_PORT,
};
/* keep state of the latest registration attempt */
static int _state = CORD_EPSIM_ERROR;
/* parse RD server address */
if (ipv6_addr_from_str((ipv6_addr_t *)&remote.addr.ipv6,
CORD_SERVER_ADDR) == NULL) {
return CORD_EPSIM_NOADDR;
static void _req_handler(unsigned req_state, coap_pkt_t* pdu,
sock_udp_ep_t *remote)
{
(void)remote;
(void)pdu;
_state = (req_state == GCOAP_MEMO_RESP) ? CORD_EPSIM_OK : CORD_EPSIM_ERROR;
}
int cord_epsim_register(const sock_udp_ep_t *rd_ep)
{
assert(rd_ep);
if (_state == CORD_EPSIM_BUSY) {
return CORD_EPSIM_BUSY;
}
/* build the initial CON packet */
@ -59,10 +65,16 @@ int cord_epsim_register(void)
return CORD_EPSIM_ERROR;
}
/* finish, we don't have any payload */
ssize_t len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE);
if (gcoap_req_send2(buf, len, &remote, NULL) == 0) {
ssize_t len = gcoap_finish(&pkt, 0, COAP_FORMAT_NONE);
_state = CORD_EPSIM_BUSY;
if (gcoap_req_send2(buf, len, rd_ep, _req_handler) == 0) {
return CORD_EPSIM_ERROR;
}
return CORD_EPSIM_OK;
}
int cord_epsim_state(void)
{
return _state;
}

View File

@ -1,59 +0,0 @@
/*
* Copyright (C) 2017 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 net_cord_epsim
* @{
*
* @file
* @brief Standalone extension for the simple RD registration endpoint
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include "log.h"
#include "thread.h"
#include "xtimer.h"
#include "net/cord/epsim.h"
#include "net/cord/config.h"
#define STACKSIZE (THREAD_STACKSIZE_DEFAULT)
#define PRIO (THREAD_PRIORITY_MAIN - 1)
#define TNAME "cord_epsim"
static char _stack[STACKSIZE];
static void *reg_runner(void *arg)
{
(void)arg;
/* wait some seconds to give the address configuration some time to settle */
xtimer_sleep(CORD_STARTUP_DELAY);
while (1) {
if (cord_epsim_register() != CORD_EPSIM_OK) {
/* if this fails once, it will always fail, so we might as well
* quit now */
LOG_ERROR("[cord_epsim] error: unable to send registration\n");
break;
}
xtimer_sleep(CORD_UPDATE_INTERVAL);
}
return NULL;
}
#ifdef MODULE_CORD_EPSIM_STANDALONE
void cord_epsim_run(void)
{
thread_create(_stack, sizeof(_stack), PRIO, THREAD_CREATE_STACKTEST,
reg_runner, NULL, TNAME);
}
#endif

View File

@ -35,7 +35,7 @@ static int make_sock_ep(sock_udp_ep_t *ep, const char *addr)
ep->family = AF_INET6;
ep->netif = SOCK_ADDR_ANY_NETIF;
if (ep->port == 0) {
ep->port = CORD_SERVER_PORT;
ep->port = COAP_PORT;
}
return 0;
}