diff --git a/Makefile.dep b/Makefile.dep index 0e999f9411b9b587bc81101605e63952eab96310..6a93d54740e255e7c44503a8f4037d450e9ce324 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -707,6 +707,12 @@ ifneq (,$(filter skald,$(USEMODULE))) USEMODULE += xtimer USEMODULE += random endif + +ifneq (,$(filter rdcli_common,$(USEMODULE))) + USEMODULE += luid + USEMODULE += fmt +endif + # always select gpio (until explicit dependencies are sorted out) FEATURES_OPTIONAL += periph_gpio diff --git a/sys/Makefile b/sys/Makefile index cc6d2ab074bcc643ea4ec4a2649237a1c804287d..3c48acd69d12bca5bfd4b625b17669f39ff2ac8a 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -127,6 +127,9 @@ endif ifneq (,$(filter skald,$(USEMODULE))) DIRS += net/skald endif +ifneq (,$(filter rdcli_common,$(USEMODULE))) + DIRS += net/application_layer/rdcli_common +endif DIRS += $(dir $(wildcard $(addsuffix /Makefile, $(USEMODULE)))) diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index fcff85bec8a39388994da000c78c9f322f83bdba..4896751c2644a32bb230acc07f7cd2b4dc64ad8a 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -159,6 +159,11 @@ void auto_init(void) DEBUG("Auto init Skald\n"); skald_init(); #endif +#ifdef MODULE_RDCLI_COMMON + DEBUG("Auto init rdcli_common module\n"); + extern void rdcli_common_init(void); + rdcli_common_init(); +#endif /* initialize network devices */ #ifdef MODULE_AUTO_INIT_GNRC_NETIF diff --git a/sys/include/net/rdcli_common.h b/sys/include/net/rdcli_common.h new file mode 100644 index 0000000000000000000000000000000000000000..005928b219801648fb1a3593f6333ea3a8ea2a87 --- /dev/null +++ b/sys/include/net/rdcli_common.h @@ -0,0 +1,54 @@ +/* + * 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. + */ + +/** + * @defgroup net_rdcli_common Shared Functions for CoRE RD Clients + * @ingroup net_rdcli + * @{ + * + * @file + * @brief Shared CoRE RD client functions + * + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> + */ + +#ifndef NET_RDCLI_COMMON_H +#define NET_RDCLI_COMMON_H + +#include "net/rdcli_config.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Export the local endpoint identifier + * + * @note Use rdcli_common_get_ep() for accessing the endpoint identifier + */ +extern char rdcli_ep[]; + +/** + * @brief Generate unique endpoint identifier (ep) + */ +void rdcli_common_init(void); + +/** + * @brief Get the local endpoint identifier + */ +static inline const char *rdcli_common_get_ep(void) +{ + return (const char *)rdcli_ep; +} + +#ifdef __cplusplus +} +#endif + +#endif /* NET_RDCLI_COMMON_H */ +/** @} */ diff --git a/sys/include/net/rdcli_config.h b/sys/include/net/rdcli_config.h new file mode 100644 index 0000000000000000000000000000000000000000..906d37fc66752c29518ee7a91095c975afaf0aa7 --- /dev/null +++ b/sys/include/net/rdcli_config.h @@ -0,0 +1,60 @@ +/* + * 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. + */ + +/** + * @defgroup net_rdcli_config CoAP Resource Directory Client Configuration + * @ingroup net_rdcli + * @{ + * + * @file + * @brief + * + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> + */ + +#ifndef NET_RDCLI_CONFIG_H +#define NET_RDCLI_CONFIG_H + +#include "net/ipv6/addr.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Endpoint ID definition + * + * Per default, the endpoint ID (ep) is generated by concatenation of a user + * defined prefix (RDCLI_EP_PREFIX) and a locally unique ID (luid) encoded in + * hexadecimal formatting with the given length of characters + * (RDCLI_EP_SUFFIX_LEN). + * + * Alternatively, the endpoint ID value can be defined at compile time by + * assigning a string value to the RDCLI_ED macro. + * + * @{ + */ +#ifndef RDCLI_EP +/** + * @brief Number of generated hexadecimal characters added to the ep + */ +#define RDCLI_EP_SUFFIX_LEN (16) + +/** + * @brief Default static prefix used for the generated ep + */ +#define RDCLI_EP_PREFIX "RIOT-" +#endif +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* NET_RDCLI_CONFIG_H */ +/** @} */ diff --git a/sys/net/application_layer/rdcli_common/Makefile b/sys/net/application_layer/rdcli_common/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..48422e909a47d7cd428d10fa73825060ccc8d8c2 --- /dev/null +++ b/sys/net/application_layer/rdcli_common/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/sys/net/application_layer/rdcli_common/rdcli_common.c b/sys/net/application_layer/rdcli_common/rdcli_common.c new file mode 100644 index 0000000000000000000000000000000000000000..d8298930c48b3f8422f54bc7c0c672dc75c84c41 --- /dev/null +++ b/sys/net/application_layer/rdcli_common/rdcli_common.c @@ -0,0 +1,60 @@ +/* + * 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_rdcli_common + * @{ + * + * @file + * @brief Implementation of common functions for CoRE RD clients + * + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> + * + * @} + */ + +#include "fmt.h" +#include "luid.h" + +#include "net/rdcli_common.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + + +#ifdef RDCLI_EP +#define EP_LEN (sizeof(RDCLI_EP)) +#define BUFSIZE (EP_LEN + 1) +#else +#define PREFIX_LEN (sizeof(RDCLI_EP_PREFIX)) +#define BUFSIZE (PREFIX_LEN + RDCLI_EP_SUFFIX_LEN + 1) +#endif + +char rdcli_ep[BUFSIZE]; + +void rdcli_common_init(void) +{ + size_t pos = 0; + +#ifdef RDCLI_EP + memcpy(rdcli_ep, RDCLI_EP, EP_LEN); + pos += EP_LEN; +#else + uint8_t luid[RDCLI_EP_SUFFIX_LEN / 2]; + + if (PREFIX_LEN) { + memcpy(rdcli_ep, RDCLI_EP_PREFIX, PREFIX_LEN); + pos += PREFIX_LEN - 1; + } + + luid_get(luid, sizeof(luid)); + fmt_bytes_hex(&rdcli_ep[pos], luid, sizeof(luid)); +#endif + + rdcli_ep[pos] = '\0'; +}