diff --git a/boards/native/include/eui_provider_params.h b/boards/native/include/eui_provider_params.h new file mode 100644 index 0000000000..0af230a2c9 --- /dev/null +++ b/boards/native/include/eui_provider_params.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2020 ML!PA Consulting GmbH + * + * 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 boards_native + * @{ + * + * @file + * @brief EUI providers found on the board + * + * @author Benjamin Valentin + */ +#ifndef EUI_PROVIDER_PARAMS_H +#define EUI_PROVIDER_PARAMS_H + +#include "native_cli_eui_provider.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name EUI sources on the board + * EUI-64 can be provided with the -Z command line argument + * @{ + */ +#define EUI64_PROVIDER_FUNC native_cli_get_eui64 +#define EUI64_PROVIDER_TYPE NETDEV_ANY +#define EUI64_PROVIDER_INDEX NETDEV_INDEX_ANY +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* EUI_PROVIDER_PARAMS_H */ +/** @} */ diff --git a/cpu/native/Makefile b/cpu/native/Makefile index 552c3f8b25..9f279f954a 100644 --- a/cpu/native/Makefile +++ b/cpu/native/Makefile @@ -30,6 +30,10 @@ ifneq (,$(filter backtrace,$(USEMODULE))) DIRS += backtrace endif +ifneq (,$(filter native_cli_eui_provider,$(USEMODULE))) + DIRS += cli_eui_provider +endif + include $(RIOTBASE)/Makefile.base INCLUDES = $(NATIVEINCLUDES) diff --git a/cpu/native/Makefile.dep b/cpu/native/Makefile.dep index 19ddb2ea39..01620b3bad 100644 --- a/cpu/native/Makefile.dep +++ b/cpu/native/Makefile.dep @@ -21,6 +21,14 @@ ifneq (,$(filter periph_rtc,$(USEMODULE))) USEMODULE += xtimer endif +ifneq (,$(filter eui_provider,$(USEMODULE))) + USEMODULE += native_cli_eui_provider +endif + +ifneq (,$(filter native_cli_eui_provider,$(USEMODULE))) + USEMODULE += l2util +endif + USEMODULE += periph # UART is needed by startup.c diff --git a/cpu/native/cli_eui_provider/Makefile b/cpu/native/cli_eui_provider/Makefile new file mode 100644 index 0000000000..cafbfc6040 --- /dev/null +++ b/cpu/native/cli_eui_provider/Makefile @@ -0,0 +1,3 @@ +MODULE := native_cli_eui_provider + +include $(RIOTBASE)/Makefile.base diff --git a/cpu/native/cli_eui_provider/eui_provider.c b/cpu/native/cli_eui_provider/eui_provider.c new file mode 100644 index 0000000000..b187b19743 --- /dev/null +++ b/cpu/native/cli_eui_provider/eui_provider.c @@ -0,0 +1,65 @@ +/** + * Native CPU EUI provider + * + * Copyright (C) 2020 Benjamin Valentin + * + * 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 cpu_native + * @{ + * @file + * @author Benjamin Valentin + * @} + */ + +#include +#include + +#include "net/l2util.h" +#include "native_cli_eui_provider.h" +#include "list.h" + +#include "native_internal.h" + +/* list of user supplied EUI-64s */ +typedef struct { + list_node_t node; + eui64_t addr; +} native_eui64_list_t; + +static list_node_t head; + +/* parse EUI-64 from command line */ +void native_cli_add_eui64(const char *s) +{ + _native_syscall_enter(); + native_eui64_list_t *e = real_malloc(sizeof(native_eui64_list_t)); + _native_syscall_leave(); + + size_t res = l2util_addr_from_str(s, e->addr.uint8); + assert(res <= sizeof(eui64_t)); + + /* if the provided address exceeds eui64_t, l2util_addr_from_str() + * *will* corrupt memory. */ + if (res > sizeof(eui64_t)) { + exit(-1); + } + + list_add(&head, &e->node); +} + +/* callback for EUI provider */ +int native_cli_get_eui64(uint8_t index, eui64_t *addr) +{ + uint8_t cnt = 0; + for (list_node_t *e = head.next; e != NULL; e = e->next) { + if (cnt++ == index) { + *addr = container_of(e, native_eui64_list_t, node)->addr; + return 0; + } + } + + return -1; +} diff --git a/cpu/native/include/native_cli_eui_provider.h b/cpu/native/include/native_cli_eui_provider.h new file mode 100644 index 0000000000..0ed57a7a22 --- /dev/null +++ b/cpu/native/include/native_cli_eui_provider.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 Benjamin Valentin + * + * 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 cpu_native + * @{ + * + * @file + * @brief Command-line EUI provider for native + * + * @author Benjamin Valentin + */ + +#ifndef NATIVE_CLI_EUI_PROVIDER_H +#define NATIVE_CLI_EUI_PROVIDER_H + +#include "net/eui64.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name parse a string as an EUI-64 and add it to the list of EUI-64s + * + * @param s[in] EUI-64 as hexadecimal string representation + */ +void native_cli_add_eui64(const char *s); + +/** + * @name Get a command-line provided EUI-64 + * + * @param index index of ZEP device + * @param addr[out] user supplied EUI-64 + * + * @return 0 on success, negatvie if no more EUIs are available. + */ +int native_cli_get_eui64(uint8_t index, eui64_t *addr); + +#ifdef __cplusplus +} +#endif + +#endif /* NATIVE_CLI_EUI_PROVIDER_H */ +/** @} */ diff --git a/cpu/native/include/native_internal.h b/cpu/native/include/native_internal.h index d61689eb26..b997b0a0ae 100644 --- a/cpu/native/include/native_internal.h +++ b/cpu/native/include/native_internal.h @@ -181,8 +181,6 @@ int register_interrupt(int sig, _native_callback_t handler); */ int unregister_interrupt(int sig); -//#include - #ifdef __cplusplus } #endif diff --git a/cpu/native/startup.c b/cpu/native/startup.c index ec5d8ac77e..9009487290 100644 --- a/cpu/native/startup.c +++ b/cpu/native/startup.c @@ -84,6 +84,9 @@ netdev_tap_params_t netdev_tap_params[NETDEV_TAP_MAX]; #ifdef MODULE_PERIPH_GPIO_LINUX #include "gpiodev_linux.h" #endif +#ifdef MODULE_NATIVE_CLI_EUI_PROVIDER +#include "native_cli_eui_provider.h" +#endif #ifdef MODULE_SOCKET_ZEP #include "socket_zep_params.h" @@ -107,6 +110,9 @@ static const char short_opts[] = ":hi:s:deEoc:" #ifdef MODULE_SOCKET_ZEP "z:" #endif +#ifdef MODULE_NATIVE_CLI_EUI_PROVIDER + "U:" +#endif #ifdef MODULE_PERIPH_SPIDEV_LINUX "p:" #endif @@ -133,6 +139,9 @@ static const struct option long_opts[] = { #ifdef MODULE_SOCKET_ZEP { "zep", required_argument, NULL, 'z' }, #endif +#ifdef MODULE_NATIVE_CLI_EUI_PROVIDER + { "eui64", required_argument, NULL, 'U' }, +#endif #ifdef MODULE_PERIPH_SPIDEV_LINUX { "spi", required_argument, NULL, 'p' }, #endif @@ -281,6 +290,9 @@ void usage_exit(int status) real_printf(" -z :,:"); } #endif +#ifdef MODULE_NATIVE_CLI_EUI_PROVIDER + real_printf(" [--eui64 …]"); +#endif #ifdef MODULE_PERIPH_SPIDEV_LINUX real_printf(" [-p ::]"); #endif @@ -323,6 +335,11 @@ void usage_exit(int status) " The ZEP interface connects to the remote address and may listen\n" " on a local address.\n" " Required to be provided SOCKET_ZEP_MAX times\n" +#endif +#ifdef MODULE_NATIVE_CLI_EUI_PROVIDER +" -U , --eui64=\n" +" provide a ZEP interface with EUI-64 (MAC address)\n" +" This argument can be provided multiple times\n" #endif ); #ifdef MODULE_MTD_NATIVE @@ -414,6 +431,7 @@ static void _zep_params_setup(char *zep_str, int zep) &socket_zep_params[zep].remote_port); } } + #endif /** @brief Initialization function pointer type */ @@ -531,6 +549,11 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e _zep_params_setup(optarg, zeps++); break; #endif +#ifdef MODULE_NATIVE_CLI_EUI_PROVIDER + case 'U': + native_cli_add_eui64(optarg); + break; +#endif #ifdef MODULE_PERIPH_SPIDEV_LINUX case 'p': { long bus = strtol(optarg, &optarg, 10);