From 1704217546c32a27abebcb63b7a1b95ab44b4aa2 Mon Sep 17 00:00:00 2001 From: biboc Date: Wed, 7 Jun 2017 14:58:16 +0200 Subject: [PATCH 01/16] pkg/openthread: add FTD and NCP support --- Makefile.dep | 5 + pkg/openthread/Makefile | 39 ++++-- pkg/openthread/Makefile.include | 2 + .../contrib/netdev/openthread_netdev.c | 82 ++++++------ pkg/openthread/contrib/openthread.c | 19 +-- .../contrib/platform_functions_wrapper.c | 17 +-- pkg/openthread/contrib/platform_logging.c | 80 ++--------- pkg/openthread/contrib/platform_misc.c | 12 +- pkg/openthread/contrib/platform_radio.c | 62 +++++---- pkg/openthread/contrib/platform_random.c | 10 ++ pkg/openthread/contrib/platform_settings.c | 31 ++--- pkg/openthread/contrib/platform_uart.c | 125 +++++++++++++++++- pkg/openthread/include/ot.h | 50 +++++-- pkg/openthread/include/platform_config.h | 49 +++++++ tests/openthread/Makefile | 30 +++-- tests/openthread/README.md | 50 ++++--- tests/openthread/main.c | 14 +- 17 files changed, 426 insertions(+), 251 deletions(-) mode change 100644 => 100755 pkg/openthread/contrib/netdev/openthread_netdev.c mode change 100644 => 100755 pkg/openthread/contrib/platform_radio.c mode change 100644 => 100755 pkg/openthread/contrib/platform_uart.c mode change 100644 => 100755 pkg/openthread/include/ot.h create mode 100644 pkg/openthread/include/platform_config.h diff --git a/Makefile.dep b/Makefile.dep index c0b24c5f550c..7d1436854acd 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -677,6 +677,11 @@ ifneq (,$(filter gcoap,$(USEMODULE))) USEMODULE += gnrc_sock_udp endif +ifneq (,$(filter openthread,$(USEPKG))) + USEMODULE += openthread_contrib + USEMODULE += mbedcrypto +endif + ifneq (,$(filter luid,$(USEMODULE))) FEATURES_OPTIONAL += periph_cpuid endif diff --git a/pkg/openthread/Makefile b/pkg/openthread/Makefile index 849a16cb1f64..c799b77bdf5b 100644 --- a/pkg/openthread/Makefile +++ b/pkg/openthread/Makefile @@ -1,12 +1,23 @@ PKG_NAME=openthread PKG_URL=https://github.com/openthread/openthread.git -PKG_VERSION=fbfd76a990b81f007957e1bd774e51bce742e53e +PKG_VERSION=8dc31d6e279d4f04fa64b5357d96e1ed20f4967a PKG_BUILDDIR ?= $(BINDIRBASE)/pkg/$(BOARD)/$(PKG_NAME) -$(info Compile OpenThread for FTD device) -OPENTHREAD_ARGS+= --enable-cli-app=ftd --enable-application-coap - -$(info $$OPENTHREAD_ARGS is [$(OPENTHREAD_ARGS)]) +ifneq (,$(filter openthread-cli-ftd,$(USEMODULE))) + $(info Compile OpenThread for FTD device) + OPENTHREAD_ARGS += --enable-cli-app=ftd +endif +ifneq (,$(filter openthread-cli-mtd,$(USEMODULE))) + $(info Compile OpenThread for MTD device) + OPENTHREAD_ARGS += --enable-cli-app=mtd --enable-joiner +endif +ifneq (,$(filter openthread-ncp-ftd,$(USEMODULE))) + $(info Compile OpenThread with NCP) + OPENTHREAD_ARGS += --enable-ncp-app=ftd --with-ncp-bus=uart --enable-commissioner --enable-border-router +endif +OPENTHREAD_ARGS += --enable-application-coap +CONFIG_FILE = OPENTHREAD_PROJECT_CORE_CONFIG_FILE='\"platform_config.h\"' +$(info $$OPENTHREAD_ARGS is [${OPENTHREAD_ARGS}]) .PHONY: all @@ -18,7 +29,7 @@ all: git-download cd $(PKG_BUILDDIR) && CPP="$(CPP)" CC="$(CC)" CXX="$(CXX)"\ OBJC="" OBJCXX="" AR="$(AR)" RANLIB="$(RANLIB)" NM="$(NM)" \ STRIP="$(STRIP)" \ - CPPFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) " \ + CPPFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -D$(CONFIG_FILE)" \ CFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) " \ CXXFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -fno-exceptions -fno-rtti " \ LDFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -nostartfiles -specs=nano.specs \ @@ -27,9 +38,19 @@ all: git-download --prefix=/ --enable-default-logging $(OPENTHREAD_ARGS) cd $(PKG_BUILDDIR) && DESTDIR=$(PKG_BUILDDIR)/output PREFIX=/ make -j4 --no-print-directory install - cp $(PKG_BUILDDIR)/output/lib/libmbedcrypto.a $(BINDIR)/libmbedcrypto.a - cp $(PKG_BUILDDIR)/output/lib/libopenthread-ftd.a $(BINDIR)/libopenthread.a - cp $(PKG_BUILDDIR)/output/lib/libopenthread-cli-ftd.a $(BINDIR)/libopenthread-cli.a + cp $(PKG_BUILDDIR)/output/lib/libmbedcrypto.a ${BINDIR}/mbedcrypto.a +ifneq (,$(filter openthread-cli-ftd,$(USEMODULE))) + cp $(PKG_BUILDDIR)/output/lib/libopenthread-ftd.a ${BINDIR}/openthread-ftd.a + cp $(PKG_BUILDDIR)/output/lib/libopenthread-cli-ftd.a ${BINDIR}/openthread-cli-ftd.a +endif +ifneq (,$(filter openthread-cli-mtd,$(USEMODULE))) + cp $(PKG_BUILDDIR)/output/lib/libopenthread-mtd.a ${BINDIR}/openthread-mtd.a + cp $(PKG_BUILDDIR)/output/lib/libopenthread-cli-mtd.a ${BINDIR}/openthread-cli-mtd.a +endif +ifneq (,$(filter openthread-ncp-ftd,$(USEMODULE))) + cp $(PKG_BUILDDIR)/output/lib/libopenthread-ftd.a ${BINDIR}/openthread-ftd.a + cp $(PKG_BUILDDIR)/output/lib/libopenthread-ncp-ftd.a ${BINDIR}/openthread-ncp-ftd.a +endif sed -ie 's/BASE/_BASE/g' $(PKG_BUILDDIR)/output/include/openthread/types.h include $(RIOTBASE)/pkg/pkg.mk diff --git a/pkg/openthread/Makefile.include b/pkg/openthread/Makefile.include index b4eebf43654d..d0d08f64a6fb 100644 --- a/pkg/openthread/Makefile.include +++ b/pkg/openthread/Makefile.include @@ -3,7 +3,9 @@ OPENTHREAD_DIR = $(RIOTBASE)/pkg/openthread INCLUDES += -I$(OPENTHREAD_DIR)/include \ -I$(OPENTHREAD_DIR)/include/openthread \ -I$(BINDIRBASE)/pkg/$(BOARD)/openthread/output/include \ + -I$(BINDIRBASE)/pkg/$(BOARD)/openthread/include \ -I$(BINDIRBASE)/pkg/$(BOARD)/openthread/include/openthread \ + -I$(BINDIRBASE)/pkg/$(BOARD)/openthread/include/openthread/platform \ ifneq (,$(filter openthread_contrib,$(USEMODULE))) DIRS += $(OPENTHREAD_DIR)/contrib diff --git a/pkg/openthread/contrib/netdev/openthread_netdev.c b/pkg/openthread/contrib/netdev/openthread_netdev.c old mode 100644 new mode 100755 index 348da5ac9caf..bf2f56e9afff --- a/pkg/openthread/contrib/netdev/openthread_netdev.c +++ b/pkg/openthread/contrib/netdev/openthread_netdev.c @@ -13,6 +13,7 @@ * @brief Netdev adoption for OpenThread * * @author Jose Ignacio Alamos + * @author Baptiste Clenet * @} */ @@ -30,6 +31,10 @@ #include "random.h" #include "ot.h" +#ifdef MODULE_OPENTHREAD_NCP_FTD +#include "openthread/ncp.h" +#endif + #define ENABLE_DEBUG (0) #include "debug.h" @@ -39,18 +44,6 @@ static msg_t _queue[OPENTHREAD_QUEUE_LEN]; static kernel_pid_t _pid; static otInstance *sInstance; -/** - * @name Default configuration for OpenThread network - * @{ - */ -#ifndef OPENTHREAD_PANID -#define OPENTHREAD_PANID 0x1234 -#endif -#ifndef OPENTHREAD_CHANNEL -#define OPENTHREAD_CHANNEL (26U) -#endif -/** @} */ - uint8_t ot_call_command(char* command, void *arg, void* answer) { ot_job_t job; @@ -67,7 +60,7 @@ uint8_t ot_call_command(char* command, void *arg, void* answer) { /* OpenThread will call this when switching state from empty tasklet to non-empty tasklet. */ void otTaskletsSignalPending(otInstance *aInstance) { - otTaskletsProcess(aInstance); + (void) aInstance; } static void *_openthread_event_loop(void *arg) { @@ -84,12 +77,8 @@ static void *_openthread_event_loop(void *arg) { netdev_t *dev; msg_t msg, reply; +#if defined(MODULE_OPENTHREAD_CLI_FTD) || defined(MODULE_OPENTHREAD_CLI_MTD) otCliUartInit(sInstance); - -#if OPENTHREAD_ENABLE_DIAG - diagInit(sInstance); -#endif - /* Init default parameters */ otPanId panid = OPENTHREAD_PANID; uint8_t channel = OPENTHREAD_CHANNEL; @@ -99,31 +88,44 @@ static void *_openthread_event_loop(void *arg) { otIp6SetEnabled(sInstance, true); /* Start Thread protocol operation */ otThreadSetEnabled(sInstance, true); +#endif + +#ifdef MODULE_OPENTHREAD_NCP_FTD + otNcpInit(sInstance); +#endif + +#if OPENTHREAD_ENABLE_DIAG + diagInit(sInstance); +#endif - uint8_t *buf; ot_job_t *job; + serial_msg_t* serialBuffer; while (1) { - msg_receive(&msg); - switch (msg.type) { - case OPENTHREAD_XTIMER_MSG_TYPE_EVENT: - /* Tell OpenThread a time event was received */ - otPlatAlarmFired(sInstance); - break; - case OPENTHREAD_NETDEV_MSG_TYPE_EVENT: - /* Received an event from driver */ - dev = msg.content.ptr; - dev->driver->isr(dev); - break; - case OPENTHREAD_SERIAL_MSG_TYPE_EVENT: - /* Tell OpenThread about the reception of a CLI command */ - buf = msg.content.ptr; - otPlatUartReceived(buf, strlen((char *) buf)); - break; - case OPENTHREAD_JOB_MSG_TYPE_EVENT: - job = msg.content.ptr; - reply.content.value = ot_exec_command(sInstance, job->command, job->arg, job->answer); - msg_reply(&msg, &reply); - break; + otTaskletsProcess(sInstance); + if (otTaskletsArePending(sInstance) == false) { + msg_receive(&msg); + switch (msg.type) { + case OPENTHREAD_XTIMER_MSG_TYPE_EVENT: + /* Tell OpenThread a time event was received */ + otPlatAlarmFired(sInstance); + break; + case OPENTHREAD_NETDEV_MSG_TYPE_EVENT: + /* Received an event from driver */ + dev = msg.content.ptr; + dev->driver->isr(dev); + break; + case OPENTHREAD_SERIAL_MSG_TYPE_EVENT: + /* Tell OpenThread about the reception of a CLI command */ + serialBuffer = (serial_msg_t*)msg.content.ptr; + otPlatUartReceived((uint8_t*) serialBuffer->buf,serialBuffer->length); + serialBuffer->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; + break; + case OPENTHREAD_JOB_MSG_TYPE_EVENT: + job = msg.content.ptr; + reply.content.value = ot_exec_command(sInstance, job->command, job->arg, job->answer); + msg_reply(&msg, &reply); + break; + } } } diff --git a/pkg/openthread/contrib/openthread.c b/pkg/openthread/contrib/openthread.c index 882bb9bd7ae6..5c48dfb312ee 100644 --- a/pkg/openthread/contrib/openthread.c +++ b/pkg/openthread/contrib/openthread.c @@ -47,23 +47,6 @@ static uint8_t rx_buf[OPENTHREAD_NETDEV_BUFLEN]; static uint8_t tx_buf[OPENTHREAD_NETDEV_BUFLEN]; static char ot_thread_stack[2 * THREAD_STACKSIZE_MAIN]; -/* init and run OpeanThread's UART simulation (stdio) */ -void openthread_uart_run(void) -{ - char buf[256]; - msg_t msg; - - msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; - msg.content.ptr = buf; - - buf[1] = 0; - while (1) { - char c = getchar(); - buf[0] = c; - msg_send(&msg, openthread_get_pid()); - } -} - void openthread_bootstrap(void) { /* init random */ @@ -76,5 +59,5 @@ void openthread_bootstrap(void) #endif openthread_radio_init(netdev, tx_buf, rx_buf); - openthread_netdev_init(ot_thread_stack, sizeof(ot_thread_stack), THREAD_PRIORITY_MAIN - 5, "openthread", netdev); + openthread_netdev_init(ot_thread_stack, sizeof(ot_thread_stack), THREAD_PRIORITY_MAIN + 1, "openthread", netdev); } diff --git a/pkg/openthread/contrib/platform_functions_wrapper.c b/pkg/openthread/contrib/platform_functions_wrapper.c index 832765b4f2c2..8693f70d17b0 100644 --- a/pkg/openthread/contrib/platform_functions_wrapper.c +++ b/pkg/openthread/contrib/platform_functions_wrapper.c @@ -264,26 +264,23 @@ OT_COMMAND ot_state(otInstance* ot_instance, void* arg, void* answer) { (void)arg; if (answer != NULL) { - uint8_t state = otThreadGetDeviceRole(ot_instance); - *((uint8_t *) answer) = state; + otDeviceRole state = otThreadGetDeviceRole(ot_instance); + *((otDeviceRole *) answer) = state; DEBUG("state: "); switch (state) { - case kDeviceRoleOffline: - puts("offline"); - break; - case kDeviceRoleDisabled: + case OT_DEVICE_ROLE_DISABLED: puts("disabled"); break; - case kDeviceRoleDetached: + case OT_DEVICE_ROLE_DETACHED: puts("detached"); break; - case kDeviceRoleChild: + case OT_DEVICE_ROLE_CHILD: puts("child"); break; - case kDeviceRoleRouter: + case OT_DEVICE_ROLE_ROUTER: puts("router"); break; - case kDeviceRoleLeader: + case OT_DEVICE_ROLE_LEADER: puts("leader"); break; default: diff --git a/pkg/openthread/contrib/platform_logging.c b/pkg/openthread/contrib/platform_logging.c index a3aed1db7d04..5674a3909fc1 100644 --- a/pkg/openthread/contrib/platform_logging.c +++ b/pkg/openthread/contrib/platform_logging.c @@ -13,85 +13,33 @@ * @brief Implementation of OpenThread logging platform abstraction * * @author Jose Ignacio Alamos + * @author Baptiste Clenet * @} */ +#include "openthread/config.h" +#include "openthread/platform/logging.h" +#if OPENTHREAD_ENABLE_CLI_LOGGING #include #include #include #include -#include #include -#include -#include "openthread/platform/logging.h" +#include "utils/code_utils.h" +#include "cli/cli-uart.h" +#endif -/* adapted from OpenThread posix example: - * See: https://github.com/openthread/openthread/blob/master/examples/platforms/posix/logging.c */ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { +#if OPENTHREAD_ENABLE_CLI_LOGGING va_list args; - - switch (aLogLevel) { - case kLogLevelNone: - fprintf(stderr, "NONE "); - break; - - case kLogLevelCrit: - fprintf(stderr, "CRIT "); - break; - - case kLogLevelWarn: - fprintf(stderr, "WARN "); - break; - - case kLogLevelInfo: - fprintf(stderr, "INFO "); - break; - - case kLogLevelDebg: - fprintf(stderr, "DEBG "); - break; - } - - switch (aLogRegion) { - case kLogRegionApi: - fprintf(stderr, "API "); - break; - - case kLogRegionMle: - fprintf(stderr, "MLE "); - break; - - case kLogRegionArp: - fprintf(stderr, "ARP "); - break; - - case kLogRegionNetData: - fprintf(stderr, "NETD "); - break; - - case kLogRegionIp6: - fprintf(stderr, "IPV6 "); - break; - - case kLogRegionIcmp: - fprintf(stderr, "ICMP "); - break; - - case kLogRegionMac: - fprintf(stderr, "MAC "); - break; - - case kLogRegionMem: - fprintf(stderr, "MEM "); - break; - default: - break; - } - va_start(args, aFormat); - vfprintf(stderr, aFormat, args); - fprintf(stderr, "\r"); + otCliLog(aLogLevel, aLogRegion, aFormat, args); va_end(args); +#else + (void)aLogLevel; + (void)aLogRegion; + (void)aFormat; +#endif /* OPENTHREAD_ENABLE_CLI_LOGGING */ } diff --git a/pkg/openthread/contrib/platform_misc.c b/pkg/openthread/contrib/platform_misc.c index d8bc9af2b3c6..2d6e97a56bb4 100644 --- a/pkg/openthread/contrib/platform_misc.c +++ b/pkg/openthread/contrib/platform_misc.c @@ -21,10 +21,13 @@ #include "openthread/platform/misc.h" #include "periph/pm.h" +#define ENABLE_DEBUG (0) +#include "debug.h" + void otPlatReset(otInstance *aInstance) { (void)aInstance; - printf("reboot...\n"); + DEBUG("reboot...\n"); pm_reboot(); } @@ -32,5 +35,10 @@ otPlatResetReason otPlatGetResetReason(otInstance *aInstance) { (void)aInstance; /* TODO: Write me! */ - return kPlatResetReason_PowerOn; + return OT_PLAT_RESET_REASON_POWER_ON; +} + +void otPlatWakeHost(void) +{ + /* TODO: implement an operation to wake the host from sleep state. */ } diff --git a/pkg/openthread/contrib/platform_radio.c b/pkg/openthread/contrib/platform_radio.c old mode 100644 new mode 100755 index 8095bb658089..0d47989b3da9 --- a/pkg/openthread/contrib/platform_radio.c +++ b/pkg/openthread/contrib/platform_radio.c @@ -26,6 +26,10 @@ #include "net/ethertype.h" #include "net/ieee802154.h" #include "net/netdev/ieee802154.h" +#include "openthread/config.h" +#include "openthread/openthread.h" +#include "openthread/platform/diag.h" +#include "openthread/platform/platform.h" #include "openthread/platform/radio.h" #include "ot.h" @@ -34,8 +38,8 @@ #define RADIO_IEEE802154_FCS_LEN (2U) -static RadioPacket sTransmitFrame; -static RadioPacket sReceiveFrame; +static otRadioFrame sTransmitFrame; +static otRadioFrame sReceiveFrame; static int8_t Rssi; static netdev_t *_dev; @@ -134,7 +138,7 @@ void recv_pkt(otInstance *aInstance, netdev_t *dev) /* very unlikely */ if ((len < 0) || ((uint32_t)len > UINT16_MAX)) { DEBUG("Invalid len: %d\n", len); - otPlatRadioReceiveDone(aInstance, NULL, kThreadError_Abort); + otPlatRadioReceiveDone(aInstance, NULL, OT_ERROR_ABORT); return; } @@ -157,7 +161,7 @@ void recv_pkt(otInstance *aInstance, netdev_t *dev) DEBUG("\n"); /* Tell OpenThread that receive has finished */ - otPlatRadioReceiveDone(aInstance, res > 0 ? &sReceiveFrame : NULL, res > 0 ? kThreadError_None : kThreadError_Abort); + otPlatRadioReceiveDone(aInstance, res > 0 ? &sReceiveFrame : NULL, res > 0 ? OT_ERROR_NONE : OT_ERROR_ABORT); } /* Called upon TX event */ @@ -169,19 +173,19 @@ void send_pkt(otInstance *aInstance, netdev_t *dev, netdev_event_t event) switch (event) { case NETDEV_EVENT_TX_COMPLETE: DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, kThreadError_None); + otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, OT_ERROR_NONE); break; case NETDEV_EVENT_TX_COMPLETE_DATA_PENDING: DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE_DATA_PENDING\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, true, kThreadError_None); + otPlatRadioTransmitDone(aInstance, &sTransmitFrame, true, OT_ERROR_NONE); break; case NETDEV_EVENT_TX_NOACK: DEBUG("openthread: NETDEV_EVENT_TX_NOACK\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, kThreadError_NoAck); + otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, OT_ERROR_NO_ACK); break; case NETDEV_EVENT_TX_MEDIUM_BUSY: DEBUG("openthread: NETDEV_EVENT_TX_MEDIUM_BUSY\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, kThreadError_ChannelAccessFailure); + otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, OT_ERROR_CHANNEL_ACCESS_FAILURE); break; default: break; @@ -217,7 +221,7 @@ void otPlatRadioSetShortAddress(otInstance *aInstance, uint16_t aShortAddress) } /* OpenThread will call this for enabling the radio */ -ThreadError otPlatRadioEnable(otInstance *aInstance) +otError otPlatRadioEnable(otInstance *aInstance) { DEBUG("openthread: otPlatRadioEnable\n"); (void) aInstance; @@ -227,11 +231,11 @@ ThreadError otPlatRadioEnable(otInstance *aInstance) _set_idle(); } - return kThreadError_None; + return OT_ERROR_NONE; } /* OpenThread will call this for disabling the radio */ -ThreadError otPlatRadioDisable(otInstance *aInstance) +otError otPlatRadioDisable(otInstance *aInstance) { DEBUG("openthread: otPlatRadioDisable\n"); (void) aInstance; @@ -241,7 +245,7 @@ ThreadError otPlatRadioDisable(otInstance *aInstance) _set_sleep(); } - return kThreadError_None; + return OT_ERROR_NONE; } bool otPlatRadioIsEnabled(otInstance *aInstance) @@ -257,28 +261,28 @@ bool otPlatRadioIsEnabled(otInstance *aInstance) } /* OpenThread will call this for setting device state to SLEEP */ -ThreadError otPlatRadioSleep(otInstance *aInstance) +otError otPlatRadioSleep(otInstance *aInstance) { DEBUG("otPlatRadioSleep\n"); (void) aInstance; _set_sleep(); - return kThreadError_None; + return OT_ERROR_NONE; } /*OpenThread will call this for waiting the reception of a packet */ -ThreadError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel) +otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel) { DEBUG("openthread: otPlatRadioReceive. Channel: %i\n", aChannel); (void) aInstance; _set_idle(); _set_channel(aChannel); - return kThreadError_None; + return OT_ERROR_NONE; } /* OpenThread will call this function to get the transmit buffer */ -RadioPacket *otPlatRadioGetTransmitBuffer(otInstance *aInstance) +otRadioFrame *otPlatRadioGetTransmitBuffer(otInstance *aInstance) { (void) aInstance; DEBUG("openthread: otPlatRadioGetTransmitBuffer\n"); @@ -294,7 +298,7 @@ void otPlatRadioSetDefaultTxPower(otInstance *aInstance, int8_t aPower) } /* OpenThread will call this for transmitting a packet*/ -ThreadError otPlatRadioTransmit(otInstance *aInstance, RadioPacket *aPacket) +otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) { (void) aInstance; @@ -319,7 +323,7 @@ ThreadError otPlatRadioTransmit(otInstance *aInstance, RadioPacket *aPacket) /* send packet though netdev */ _dev->driver->send(_dev, &iolist); - return kThreadError_None; + return OT_ERROR_NONE; } /* OpenThread will call this for getting the radio caps */ @@ -328,7 +332,7 @@ otRadioCaps otPlatRadioGetCaps(otInstance *aInstance) (void) aInstance; DEBUG("openthread: otPlatRadioGetCaps\n"); /* all drivers should handle ACK, including call of NETDEV_EVENT_TX_NOACK */ - return kRadioCapsNone; + return OT_RADIO_CAPS_NONE; } /* OpenThread will call this for getting the state of promiscuous mode */ @@ -361,31 +365,31 @@ void otPlatRadioEnableSrcMatch(otInstance *aInstance, bool aEnable) (void)aEnable; } -ThreadError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress) +otError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress) { DEBUG("otPlatRadioAddSrcMatchShortEntry\n"); (void)aInstance; (void)aShortAddress; - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const uint8_t *aExtAddress) +otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const uint8_t *aExtAddress) { DEBUG("otPlatRadioAddSrcMatchExtEntry\n"); (void)aInstance; (void)aExtAddress; - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatRadioClearSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress) +otError otPlatRadioClearSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress) { DEBUG("otPlatRadioClearSrcMatchShortEntry\n"); (void)aInstance; (void)aShortAddress; - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const uint8_t *aExtAddress) +otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const uint8_t *aExtAddress) { DEBUG("otPlatRadioClearSrcMatchExtEntry\n"); (void)aInstance; @@ -405,13 +409,13 @@ void otPlatRadioClearSrcMatchExtEntries(otInstance *aInstance) (void)aInstance; } -ThreadError otPlatRadioEnergyScan(otInstance *aInstance, uint8_t aScanChannel, uint16_t aScanDuration) +otError otPlatRadioEnergyScan(otInstance *aInstance, uint8_t aScanChannel, uint16_t aScanDuration) { DEBUG("otPlatRadioEnergyScan\n"); (void)aInstance; (void)aScanChannel; (void)aScanDuration; - return kThreadError_NotImplemented; + return OT_ERROR_NOT_IMPLEMENTED; } void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeee64Eui64) diff --git a/pkg/openthread/contrib/platform_random.c b/pkg/openthread/contrib/platform_random.c index c82bb43ebc74..ae8eb27023ac 100644 --- a/pkg/openthread/contrib/platform_random.c +++ b/pkg/openthread/contrib/platform_random.c @@ -49,3 +49,13 @@ uint32_t otPlatRandomGet(void) DEBUG("otPlatRandomGet: %i\n", (int) rand_val); return rand_val; } + +otError otPlatRandomGetTrue(uint8_t *aOutput, uint16_t aOutputLength) +{ + for (uint16_t index = 0; index < aOutputLength; index++) { + aOutput[index] = 0; + uint32_t rand_val = random_uint32(); + aOutput[index] = (uint8_t) rand_val; + } + return OT_ERROR_NONE; +} diff --git a/pkg/openthread/contrib/platform_settings.c b/pkg/openthread/contrib/platform_settings.c index e95cea34eedb..b8ae084037b2 100644 --- a/pkg/openthread/contrib/platform_settings.c +++ b/pkg/openthread/contrib/platform_settings.c @@ -27,26 +27,26 @@ void otPlatSettingsInit(otInstance *aInstance) (void)aInstance; } -ThreadError otPlatSettingsBeginChange(otInstance *aInstance) +otError otPlatSettingsBeginChange(otInstance *aInstance) { (void)aInstance; - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatSettingsCommitChange(otInstance *aInstance) +otError otPlatSettingsCommitChange(otInstance *aInstance) { DEBUG("openthread: otPlatSettingsCommitChange\n"); (void)aInstance; - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatSettingsAbandonChange(otInstance *aInstance) +otError otPlatSettingsAbandonChange(otInstance *aInstance) { (void)aInstance; - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) +otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) { (void)aInstance; (void)aKey; @@ -55,36 +55,33 @@ ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, DEBUG("openthread: otPlatSettingsGet\n"); *aValueLength = 0; - return kThreadError_NotImplemented; + return OT_ERROR_NOT_IMPLEMENTED; } -ThreadError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength) +otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength) { (void)aInstance; (void)aKey; (void)aValue; (void)aValueLength; - - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength) +otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength) { (void)aInstance; (void)aKey; (void)aValue; (void)aValueLength; - - return kThreadError_None; + return OT_ERROR_NONE; } -ThreadError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) +otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) { (void)aInstance; (void)aKey; (void)aIndex; - - return kThreadError_None; + return OT_ERROR_NONE; } void otPlatSettingsWipe(otInstance *aInstance) diff --git a/pkg/openthread/contrib/platform_uart.c b/pkg/openthread/contrib/platform_uart.c old mode 100644 new mode 100755 index d3aff67af3fc..debdd75e518a --- a/pkg/openthread/contrib/platform_uart.c +++ b/pkg/openthread/contrib/platform_uart.c @@ -13,6 +13,7 @@ * @brief Implementation of OpenThread UART platform abstraction * * @author Jose Ignacio Alamos + * @author Baptiste Clenet * @} */ @@ -20,27 +21,137 @@ #include #include "periph/uart.h" +#include "openthread/types.h" #include "openthread/platform/uart.h" +#include "ot.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +#define UART_OPENTHREAD_BAUDRATE (115200U) +#define UART_OPENTHREAD UART_DEV(0) +#define OPENTHREAD_SPINEL_FRAME_MARKER (0x7e) + +serial_msg_t * gSerialMessage[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER]; +uint8_t gSerialBuff[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER][OPENTHREAD_SERIAL_BUFFER_SIZE]; + +#ifdef MODULE_OPENTHREAD_NCP_FTD +int8_t getFirstEmptySerialBuffer(void) { + uint8_t i = 0; + for (i = 0; i < OPENTHREAD_NUMBER_OF_SERIAL_BUFFER; i++) { + if ( OPENTHREAD_SERIAL_BUFFER_STATUS_FREE == gSerialMessage[i]->serial_buffer_status ) { + break; + } + } + + if ( i >= OPENTHREAD_NUMBER_OF_SERIAL_BUFFER ) { + return OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER; + } else { + return i; + } +} + +/* OpenThread will call this for enabling UART (required for OpenThread's NCP)*/ +void uart_handler(void* arg, char c) { + + static int16_t currentSerialBufferNumber = 0; + static uint8_t frameLength = 0; + static uint8_t gOnGoingSpinelReception = 0; + + msg_t msg; + msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; + + if ((c == OPENTHREAD_SPINEL_FRAME_MARKER) && (gOnGoingSpinelReception == false)) { /* Start of Spinel Frame */ + currentSerialBufferNumber = getFirstEmptySerialBuffer(); + if (OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER == currentSerialBufferNumber) { + DEBUG("SERIAL: ERROR => OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER found\n"); + return; + } + frameLength = 0; + + gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = c; + + gOnGoingSpinelReception = true; + } + else if ((c == OPENTHREAD_SPINEL_FRAME_MARKER) && (gOnGoingSpinelReception == true)) { /* End of Spinel Frame */ + if ( OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER == currentSerialBufferNumber ) { + return; + } + if (frameLength == 1) { /* It means that we handle the Start of a Spinel frame instead of the end */ + + frameLength--; + return; + } + if( OPENTHREAD_SERIAL_BUFFER_STATUS_FULL != gSerialMessage[currentSerialBufferNumber]->serial_buffer_status) { + gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = (uint8_t) c; + gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_READY_TO_PROCESS; + gSerialMessage[currentSerialBufferNumber]->length = frameLength + 1; + msg.content.ptr = gSerialMessage[currentSerialBufferNumber]; + msg_send_int(&msg, openthread_get_pid()); + } + else { + gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; + } + gOnGoingSpinelReception = false; + frameLength = 0; + } + else if (gOnGoingSpinelReception == true) { /* Payload of Spinel Frame */ + if ( OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER == currentSerialBufferNumber ) { + return; + } + if ( OPENTHREAD_SERIAL_BUFFER_STATUS_FULL != gSerialMessage[currentSerialBufferNumber]->serial_buffer_status) { + gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = (uint8_t) c; + } + } + + if (gOnGoingSpinelReception == true) { + frameLength++; + if ( frameLength >= OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE) { + DEBUG("SERIAL: ERROR => OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE overflowed\n"); + gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FULL; + } + } +} + +#else + +void uart_handler(void* arg, char c) { + msg_t msg; + msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; + gSerialMessage[0]->buf[0] = (uint8_t) c; + gSerialMessage[0]->length = 1; + msg.content.ptr = gSerialMessage[0]; + msg_send_int(&msg, openthread_get_pid()); +} + +#endif /* MODULE_OPENTHREAD_NCP_FTD */ /* OpenThread will call this for enabling UART (required for OpenThread's CLI)*/ -ThreadError otPlatUartEnable(void) +otError otPlatUartEnable(void) { - return kThreadError_None; + for (uint8_t i = 0; i < OPENTHREAD_NUMBER_OF_SERIAL_BUFFER; i++) { + gSerialMessage[i] = (serial_msg_t*) &gSerialBuff[i]; + gSerialMessage[i]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; + } + + uart_init(UART_OPENTHREAD, UART_OPENTHREAD_BAUDRATE, (uart_rx_cb_t) uart_handler, NULL); + return OT_ERROR_NONE; } /* OpenThread will call this for disabling UART */ -ThreadError otPlatUartDisable(void) +otError otPlatUartDisable(void) { - return kThreadError_None; + uart_poweroff(UART_OPENTHREAD); + return OT_ERROR_NONE; } /* OpenThread will call this for sending data through UART */ -ThreadError otPlatUartSend(const uint8_t *aBuf, uint16_t aBufLength) +otError otPlatUartSend(const uint8_t *aBuf, uint16_t aBufLength) { - uart_write(UART_DEV(0), aBuf, aBufLength); + uart_write(UART_OPENTHREAD, aBuf, aBufLength); /* Tell OpenThread the sending of UART is done */ otPlatUartSendDone(); - return kThreadError_None; + return OT_ERROR_NONE; } diff --git a/pkg/openthread/include/ot.h b/pkg/openthread/include/ot.h old mode 100644 new mode 100755 index 9535338526cf..9be33e70262f --- a/pkg/openthread/include/ot.h +++ b/pkg/openthread/include/ot.h @@ -17,7 +17,8 @@ * * @file * - * @author José Ignacio Alamos + * @author Jose Ignacio Alamos + * @author Baptiste Clenet */ #ifndef OT_H @@ -34,18 +35,46 @@ extern "C" { #include "thread.h" #include "openthread/types.h" -#define OPENTHREAD_XTIMER_MSG_TYPE_EVENT (0x2235) /**< xtimer message receiver event*/ -#define OPENTHREAD_NETDEV_MSG_TYPE_EVENT (0x2236) /**< message received from driver */ -#define OPENTHREAD_SERIAL_MSG_TYPE_EVENT (0x2237) /**< event indicating a serial (UART) message was sent to OpenThread */ -#define OPENTHREAD_MSG_TYPE_RECV (0x2238) /**< event for frame reception */ -#define OPENTHREAD_JOB_MSG_TYPE_EVENT (0x2240) /**< event indicating an OT_JOB message */ +/**< xtimer message receiver event*/ +#define OPENTHREAD_XTIMER_MSG_TYPE_EVENT (0x2235) +/**< message received from driver */ +#define OPENTHREAD_NETDEV_MSG_TYPE_EVENT (0x2236) +/**< event indicating a serial (UART) message was sent to OpenThread */ +#define OPENTHREAD_SERIAL_MSG_TYPE_EVENT (0x2237) +/**< event for frame reception */ +#define OPENTHREAD_MSG_TYPE_RECV (0x2238) +/**< event indicating an OT_JOB message */ +#define OPENTHREAD_JOB_MSG_TYPE_EVENT (0x2240) +/**< number of serial reception buffer*/ +#define OPENTHREAD_NUMBER_OF_SERIAL_BUFFER (1U) +/**< sizeof in bytes the two first members of she serial structure*/ +#define OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF (4U) +#ifdef MODULE_OPENTHREAD_NCP_FTD +/**< sizeof the serial buffer*/ +#define OPENTHREAD_SERIAL_BUFFER_SIZE OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF + 200 +#else +/**< sizeof the serial buffer*/ +#define OPENTHREAD_SERIAL_BUFFER_SIZE OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF + 1 +#endif +/**< sizeof the spinel payload data*/ +#define OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE OPENTHREAD_SERIAL_BUFFER_SIZE - OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF +/**< error when no more buffer available*/ +#define OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER -1 +/**< serial buffer ready to use*/ +#define OPENTHREAD_SERIAL_BUFFER_STATUS_FREE (0x0001) +/**< serial buffer ready for processsing*/ +#define OPENTHREAD_SERIAL_BUFFER_STATUS_READY_TO_PROCESS (0x0002) +/**< serial buffer payload full*/ +#define OPENTHREAD_SERIAL_BUFFER_STATUS_FULL (0x0004) + /** * @brief Struct containing a serial message */ typedef struct { - void *buf; /**< buffer containing the message */ - size_t len; /**< length of the message */ + uint16_t length; /**< length of the message */ + uint16_t serial_buffer_status; /**< status of the buffer */ + uint8_t buf[OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE]; /**< buffer containing the message */ } serial_msg_t; /** @@ -115,11 +144,6 @@ kernel_pid_t openthread_get_pid(void); */ void ot_random_init(void); -/** - * @brief Run OpenThread UART simulator (stdio) - */ -void openthread_uart_run(void); - /** * @brief Execute OpenThread command. Call this function only in OpenThread thread * diff --git a/pkg/openthread/include/platform_config.h b/pkg/openthread/include/platform_config.h new file mode 100644 index 000000000000..b07a806ff789 --- /dev/null +++ b/pkg/openthread/include/platform_config.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) Baptiste Clenet + * + * 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 + * @file + * @brief Implementation of OpenThread platform config + * + * @author Baptiste Clenet + * @} + */ + +#ifndef PLATFORM_CONFIG_H +#define PLATFORM_CONFIG_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @def OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS + * + * The number of message buffers in buffer pool + */ +#ifdef OPENTHREAD_ENABLE_NCP_UART +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 5 +#elif defined(OPENTHREAD_FTD) || defined(OPENTHREAD_MTD) +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 5 +#endif + +/** + * @def OPENTHREAD_CONFIG_LEGACY_TRANSMIT_DONE + * + * Define to 1 if you want use legacy transmit done. + * + */ +#define OPENTHREAD_CONFIG_LEGACY_TRANSMIT_DONE 1 + +#ifdef __cplusplus +} +#endif + +#endif /* PLATFORM_CONFIG_H */ diff --git a/tests/openthread/Makefile b/tests/openthread/Makefile index 6056a33365a7..edfc37e2b79b 100644 --- a/tests/openthread/Makefile +++ b/tests/openthread/Makefile @@ -3,6 +3,7 @@ APPLICATION = openthread # If no BOARD is found in the environment, use this default: BOARD ?= samr21-xpro +# These are the boards that OpenThread stack has been tested on BOARD_WHITELIST := samr21-xpro iotlab-m3 fox iotlab-a8-m3 # This has to be the absolute path to the RIOT base directory: @@ -12,11 +13,23 @@ RIOTBASE ?= $(CURDIR)/../.. QUIET ?= 1 USEPKG += openthread -USEMODULE += openthread_contrib -USEMODULE += libmbedcrypto -USEMODULE += libopenthread -USEMODULE += libopenthread-cli -USEMODULE += xtimer +OPENTHREAD_TYPE ?= ftd +ifeq ($(OPENTHREAD_TYPE),mtd) + # MTD: A Minimal Thread Device does not have router functionality + # compiled in. As a result, it is not necessary to configure the + # routerrole on an MTD. At the same time, an MTD may or may not be sleepy. + USEMODULE += openthread-mtd + USEMODULE += openthread-cli-mtd +else + # ftd: A Full Thread Device has router functionality compiled in + USEMODULE += openthread-ftd + USEMODULE += openthread-cli-ftd +endif + +#Define PANID and CHANNEL used by default +OPENTHREAD_PANID ?= 0xbeef +OPENTHREAD_CHANNEL ?= 26 +CFLAGS += -DOPENTHREAD_PANID=${OPENTHREAD_PANID} -DOPENTHREAD_CHANNEL=${OPENTHREAD_CHANNEL} ifneq (,$(filter samr21-xpro,$(BOARD))) DRIVER := at86rf233 @@ -32,17 +45,12 @@ endif USEMODULE += $(DRIVER) +USEMODULE += xtimer USEMODULE += random -USEMODULE += shell -USEMODULE += shell_commands USEMODULE += ps -USEMODULE += ipv6_addr #required for C++ compiling CXXEXFLAGS += -fno-rtti USEMODULE += cpp11-compat -#Define PANID and CHANNEL used by default -#CFLAGS += -DOPENTHREAD_PANID=0xbeef -DOPENTHREAD_CHANNEL=11 - include $(RIOTBASE)/Makefile.include diff --git a/tests/openthread/README.md b/tests/openthread/README.md index 25a79ef83c80..0bbdc31cfd71 100644 --- a/tests/openthread/README.md +++ b/tests/openthread/README.md @@ -1,36 +1,46 @@ ## OpenThread on RIOT -This test demonstrates the [OpenThread](https://github.com/openthread/openthread) stack running on RIOT. When flashed, -it will initialize the OpenThread Command Line Interface for interacting with the stack. +This test demonstrates how to use the [OpenThread](https://github.com/openthread/openthread) +open source implementation of [Thread](https://threadgroup.org/) on RIOT. + The [Command Line Interface](https://github.com/openthread/openthread/blob/master/examples/apps/cli/README.md) of +OpenThread was ported. Please check the [full documentation] +(https://github.com/openthread/openthread/blob/master/src/cli/README.md) of the CLI for usage information. + +You can either build a FTD or MTD firmware: +- MTD: A Minimal Thread Device does not have router functionality compiled in. An MTD may or may not be sleepy. +- FTD: A Full Thread Device has router functionality compiled in. ## Quick usage -To test OpenThread on RIOT, you can do the following: +With RIOT port, a node is auto-setup and ready to communicate with this configuration: +OPENTHREAD_PANID: 0xbeef +OPENTHREAD_CHANNEL: 26 -1. Flash nodes with `make BOARD= clean all flash` -2. Write `panid 0x1234`, `ifconfig up` then `thread start` on one node. -3. Check the state of the node with `state`. In the beggining should be `detached`, but after some seconds it should - become `leader` -4. Write `panid 0x1234`, `ifconfig up` then `thread start` on another node. -The second node should become `child` or `router` if there's a leader. -5. Get the mesh IP address of a node with `ipaddr`. +You can pass the panid/channel independently when building the firmware: +`make BOARD= OPENTHREAD_PANID=0xaaaa OPENTHREAD_TYPE=ftd flash term` +`make BOARD= OPENTHREAD_CHANNEL=20 OPENTHREAD_TYPE=ftd flash term` +To test OpenThread in RIOT, you can do the following: + +1. Flash nodes with MTD or FTD functionality: +`make BOARD= clean all flash OPENTHREAD_TYPE=mtd` +`make BOARD= clean all flash OPENTHREAD_TYPE=ftd` +2. Check the state of the node with `state`. In the beginning, it should be `detached`, but after some seconds it should + become `leader` +3. Start another node and check that it becomes `router`. There is only one leader in a Thread network. +4. Get the mesh IP address of a node with `ipaddr`. ``` - ipaddr +ipaddr fdde:ad00:beef::ff:fe00:8000 fe80::ff:fe00:8000 fdde:ad00:beef:0:946a:c722:a5d9:8481 fe80::3984:f4eb:d182:5dae ``` +5. Ping from another node with `ping fdde:ad00:beef:0:946a:c722:a5d9:848`. +6. You can try IEEE802.15.4 scan with `scan` command +7. You can also check other commands with `help` - Addresses starting with `fd` are mesh-local, and addresses starting with `fe80` are link-local. - Mesh-local address types that contain `ff:fe00` are classified as Router Locator (RLOC). Mesh-local address types - that don't contain `ff:fe00` are Endpoint Identifies (EID). -6. Ping from another node to a mesh-local address with `ping fdde:ad00:beef:0:946a:c722:a5d9:8481`. -7. You can try IEEE802.15.4 scan with `scan` command -8. You can also check other commands with `help` -9. Enjoy! -## Note +## OpenThread port on RIOT status -See the [OpenThread CLI Reference](https://github.com/openthread/openthread/blob/master/src/cli/README.md) for more information about OpenThread CLI commands +OpenThread port on RIOT is stable. In case of any bug, please report via GitHub issue. diff --git a/tests/openthread/main.c b/tests/openthread/main.c index 591abb3cef78..facd03fb6189 100644 --- a/tests/openthread/main.c +++ b/tests/openthread/main.c @@ -10,25 +10,21 @@ * @file * @brief OpenThread test application * - * @author Baptiste Clenet + * @author Baptiste Clenet */ #include -#include "net/ipv6/addr.h" -#include "openthread/ip6.h" -#include "openthread/thread.h" -#include "openthread/udp.h" #include "ot.h" -#include "shell.h" -#include "shell_commands.h" + int main(void) { - printf("Get PANID\n"); + puts("This a test for OpenThread"); + /* Example of how to call OpenThread stack functions */ + puts("Get PANID "); uint16_t panid = 0; uint8_t res = ot_call_command("panid", NULL, (void*)&panid); printf("Current panid: 0x%x (res:%x)\n", panid, res); - openthread_uart_run(); return 0; } From 0a2b5fdbe906e364225cf9395a853369fa59f477 Mon Sep 17 00:00:00 2001 From: biboc Date: Wed, 21 Jun 2017 16:44:19 +0200 Subject: [PATCH 02/16] tests: add openthread NCP application --- pkg/openthread/include/platform_config.h | 8 ++-- tests/openthread/Makefile | 16 ++++---- tests/openthread_ncp/Makefile | 51 ++++++++++++++++++++++++ tests/openthread_ncp/README.md | 43 ++++++++++++++++++++ tests/openthread_ncp/main.c | 24 +++++++++++ 5 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 tests/openthread_ncp/Makefile create mode 100644 tests/openthread_ncp/README.md create mode 100644 tests/openthread_ncp/main.c diff --git a/pkg/openthread/include/platform_config.h b/pkg/openthread/include/platform_config.h index b07a806ff789..66d309a671fb 100644 --- a/pkg/openthread/include/platform_config.h +++ b/pkg/openthread/include/platform_config.h @@ -28,10 +28,12 @@ extern "C" { * * The number of message buffers in buffer pool */ -#ifdef OPENTHREAD_ENABLE_NCP_UART -#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 5 -#elif defined(OPENTHREAD_FTD) || defined(OPENTHREAD_MTD) +#if OPENTHREAD_ENABLE_NCP_UART #define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 5 +#elif OPENTHREAD_MTD +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 20 +#elif OPENTHREAD_FTD +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 10 #endif /** diff --git a/tests/openthread/Makefile b/tests/openthread/Makefile index edfc37e2b79b..564c70ac3243 100644 --- a/tests/openthread/Makefile +++ b/tests/openthread/Makefile @@ -15,15 +15,15 @@ QUIET ?= 1 USEPKG += openthread OPENTHREAD_TYPE ?= ftd ifeq ($(OPENTHREAD_TYPE),mtd) - # MTD: A Minimal Thread Device does not have router functionality - # compiled in. As a result, it is not necessary to configure the - # routerrole on an MTD. At the same time, an MTD may or may not be sleepy. - USEMODULE += openthread-mtd - USEMODULE += openthread-cli-mtd + # MTD: A Minimal Thread Device does not have router functionality + # compiled in. As a result, it is not necessary to configure the + # routerrole on an MTD. At the same time, an MTD may or may not be sleepy. + USEMODULE += openthread-mtd + USEMODULE += openthread-cli-mtd else - # ftd: A Full Thread Device has router functionality compiled in - USEMODULE += openthread-ftd - USEMODULE += openthread-cli-ftd + # ftd: A Full Thread Device has router functionality compiled in + USEMODULE += openthread-ftd + USEMODULE += openthread-cli-ftd endif #Define PANID and CHANNEL used by default diff --git a/tests/openthread_ncp/Makefile b/tests/openthread_ncp/Makefile new file mode 100644 index 000000000000..5381ceb720cb --- /dev/null +++ b/tests/openthread_ncp/Makefile @@ -0,0 +1,51 @@ +APPLICATION = openthread_ncp + +# If no BOARD is found in the environment, use this default: +BOARD ?= samr21-xpro + +# These are the boards that OpenThread stack has been tested on +BOARD_WHITELIST := samr21-xpro iotlab-m3 fox iotlab-a8-m3 + +# This has to be the absolute path to the RIOT base directory: +RIOTBASE ?= $(CURDIR)/../.. + +# 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 -Wall + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +# NCP: A Network Co-Processor is used with wpantund software. wpantund is a user-space network +# interface driver/daemon that provides a native IPv6 network interface to a low-power wireless Network Co-Processor +# FTD (see openthread_test_ftd): NCP needs Full Thread Device to act as a router in the network +USEPKG += openthread +USEMODULE += openthread-ftd +USEMODULE += openthread-ncp-ftd + +#Define PANID and CHANNEL used by default +#CFLAGS += -DOPENTHREAD_PANID=0xbeef -DOPENTHREAD_CHANNEL=11 + +ifneq (,$(filter samr21-xpro,$(BOARD))) + DRIVER := at86rf233 +endif +ifneq (,$(filter iotlab-m3 fox iotlab-a8-m3,$(BOARD))) + DRIVER := at86rf231 +endif + +ifneq (,$(filter at86rf2%,$(DRIVER))) + FEATURES_REQUIRED += periph_spi periph_gpio +endif + +USEMODULE += $(DRIVER) + +USEMODULE += xtimer +USEMODULE += random +USEMODULE += ps + +#required for C++ compiling +CXXEXFLAGS += -fno-rtti +USEMODULE += cpp11-compat + +include $(RIOTBASE)/Makefile.include diff --git a/tests/openthread_ncp/README.md b/tests/openthread_ncp/README.md new file mode 100644 index 000000000000..940c35d6e576 --- /dev/null +++ b/tests/openthread_ncp/README.md @@ -0,0 +1,43 @@ +## OpenThread on RIOT + +This test demonstrates how to use the [OpenThread](https://github.com/openthread/openthread) +open source implementation of [Thread](https://threadgroup.org/) on RIOT. + +This test shows how to use a Network Co-Processor (NCP). A Network Co-Processor is used with wpantund software. +Wpantund is a user-space network interface driver/daemon that provides a native IPv6 network interface to a low-power +wireless Network Co-Processor. NCP and wpantund communicates by UART (UART(0) is used here) + +## Quick usage + +To test OpenThread NCP in RIOT, you can do the following: + +1. Flash one node with openthread_ncp example with `make BOARD= clean all flash -C tests/openthread_ncp` +2. Install wpantund: On Ubuntu 16.04, run +``` +sudo apt-get -y install build-essential subversion libncurses5-dev libssl-dev zlib1g-dev gawk gcc-multilib flex git-core gettext gcc binutils bzip2 python perl make unzip libz-dev tftp git shtool autogen automake libtool autotools-dev libdbus-1-dev +git clone https://github.com/openthread/wpantund.git +cd wpantund +./bootstrap.sh +./configure +make +sudo make install +``` + +3. Start wpantund on your host. Be sure to pass flags appropriate to connect wpantund to your NCP connection type. +You can also name the network interface at this time or let wpantund automatically assign a name (wpan0): + +``` +sudo /usr/local/sbin/wpantund -s /dev/ttyACM0 -I wpan0 -o SyslogMask all +``` + +4. Start wpanctl to access the command line management interface for controlling Thread features on your device. +This is similar to the Thread cli interface conceptually but with slightly different command syntax: + +`sudo /usr/local/bin/wpanctl -I wpan0` + +5. With wpanctl CLI, you can use "status" cmd to check status of NCP, "scan" cmd scan for WPAN network around or "form" to create new WPAN network +6. Confirm your network interface has started and is operational by running standard IP commands such `ifconfig` and `ping6`. + +## OpenThread port on RIOT status + +OpenThread port on RIOT is stable. In case of any bug, please report via GitHub issue. diff --git a/tests/openthread_ncp/main.c b/tests/openthread_ncp/main.c new file mode 100644 index 000000000000..33d85a13591c --- /dev/null +++ b/tests/openthread_ncp/main.c @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2017 Baptiste CLENET + * + * 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. + */ + +/** + * @file + * @brief OpenThread test application + * + * @author Baptiste Clenet + */ + +#include + +#include "ot.h" + +int main(void) +{ + /* Run wpantund to interact with NCP */ + return 0; +} From ca18b0b59ac74e8ad9dc327f9cbff8a07198e816 Mon Sep 17 00:00:00 2001 From: biboc Date: Thu, 29 Jun 2017 14:24:12 +0200 Subject: [PATCH 03/16] pkg/openthread: improve CLI uart handler --- pkg/openthread/Makefile | 13 ++--- pkg/openthread/contrib/openthread.c | 2 - pkg/openthread/contrib/platform_radio.c | 2 +- pkg/openthread/contrib/platform_uart.c | 47 +++++++++++++----- pkg/openthread/include/ot.h | 62 +++++++++++++++--------- pkg/openthread/include/platform_config.h | 8 +-- tests/openthread/Makefile | 6 ++- tests/openthread_ncp/Makefile | 3 -- 8 files changed, 89 insertions(+), 54 deletions(-) diff --git a/pkg/openthread/Makefile b/pkg/openthread/Makefile index c799b77bdf5b..5f08f29083fa 100644 --- a/pkg/openthread/Makefile +++ b/pkg/openthread/Makefile @@ -4,16 +4,17 @@ PKG_VERSION=8dc31d6e279d4f04fa64b5357d96e1ed20f4967a PKG_BUILDDIR ?= $(BINDIRBASE)/pkg/$(BOARD)/$(PKG_NAME) ifneq (,$(filter openthread-cli-ftd,$(USEMODULE))) - $(info Compile OpenThread for FTD device) - OPENTHREAD_ARGS += --enable-cli-app=ftd + $(info Compile OpenThread for FTD device) + OPENTHREAD_ARGS += --enable-cli-app=ftd endif ifneq (,$(filter openthread-cli-mtd,$(USEMODULE))) - $(info Compile OpenThread for MTD device) - OPENTHREAD_ARGS += --enable-cli-app=mtd --enable-joiner + $(info Compile OpenThread for MTD device) + OPENTHREAD_ARGS += --enable-cli-app=mtd --enable-joiner endif ifneq (,$(filter openthread-ncp-ftd,$(USEMODULE))) - $(info Compile OpenThread with NCP) - OPENTHREAD_ARGS += --enable-ncp-app=ftd --with-ncp-bus=uart --enable-commissioner --enable-border-router + $(info Compile OpenThread with NCP) + OPENTHREAD_ARGS += --enable-ncp-app=ftd --with-ncp-bus=uart \ + --enable-commissioner --enable-border-router endif OPENTHREAD_ARGS += --enable-application-coap CONFIG_FILE = OPENTHREAD_PROJECT_CORE_CONFIG_FILE='\"platform_config.h\"' diff --git a/pkg/openthread/contrib/openthread.c b/pkg/openthread/contrib/openthread.c index 5c48dfb312ee..02339307940b 100644 --- a/pkg/openthread/contrib/openthread.c +++ b/pkg/openthread/contrib/openthread.c @@ -41,8 +41,6 @@ static at86rf2xx_t at86rf2xx_dev; #endif -#define OPENTHREAD_NETDEV_BUFLEN (ETHERNET_MAX_LEN) - static uint8_t rx_buf[OPENTHREAD_NETDEV_BUFLEN]; static uint8_t tx_buf[OPENTHREAD_NETDEV_BUFLEN]; static char ot_thread_stack[2 * THREAD_STACKSIZE_MAIN]; diff --git a/pkg/openthread/contrib/platform_radio.c b/pkg/openthread/contrib/platform_radio.c index 0d47989b3da9..bccf78ff7134 100755 --- a/pkg/openthread/contrib/platform_radio.c +++ b/pkg/openthread/contrib/platform_radio.c @@ -394,7 +394,7 @@ otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const uint8_t *a DEBUG("otPlatRadioClearSrcMatchExtEntry\n"); (void)aInstance; (void)aExtAddress; - return kThreadError_None; + return OT_ERROR_NONE; } void otPlatRadioClearSrcMatchShortEntries(otInstance *aInstance) diff --git a/pkg/openthread/contrib/platform_uart.c b/pkg/openthread/contrib/platform_uart.c index debdd75e518a..f2be654a0daa 100755 --- a/pkg/openthread/contrib/platform_uart.c +++ b/pkg/openthread/contrib/platform_uart.c @@ -19,7 +19,9 @@ #include #include +#include +#include "uart_stdio.h" #include "periph/uart.h" #include "openthread/types.h" #include "openthread/platform/uart.h" @@ -28,8 +30,6 @@ #define ENABLE_DEBUG (0) #include "debug.h" -#define UART_OPENTHREAD_BAUDRATE (115200U) -#define UART_OPENTHREAD UART_DEV(0) #define OPENTHREAD_SPINEL_FRAME_MARKER (0x7e) serial_msg_t * gSerialMessage[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER]; @@ -54,6 +54,7 @@ int8_t getFirstEmptySerialBuffer(void) { /* OpenThread will call this for enabling UART (required for OpenThread's NCP)*/ void uart_handler(void* arg, char c) { + (void)arg; static int16_t currentSerialBufferNumber = 0; static uint8_t frameLength = 0; static uint8_t gOnGoingSpinelReception = 0; @@ -115,13 +116,35 @@ void uart_handler(void* arg, char c) { #else -void uart_handler(void* arg, char c) { - msg_t msg; - msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; - gSerialMessage[0]->buf[0] = (uint8_t) c; - gSerialMessage[0]->length = 1; - msg.content.ptr = gSerialMessage[0]; - msg_send_int(&msg, openthread_get_pid()); +void uart_handler(void* arg, char c) +{ + (void)arg; + + static uint16_t frameLength = 0; + if (frameLength == 0 && gSerialMessage != NULL) { + memset(gSerialMessage[0], 0, sizeof(serial_msg_t)); + } + switch (c) { + case '\r': + case '\n': + if (frameLength > 0) { + gSerialMessage[0]->buf[frameLength] = c; + frameLength++; + gSerialMessage[0]->length = frameLength; + msg_t msg; + msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; + msg.content.ptr = gSerialMessage[0]; + msg_send_int(&msg, openthread_get_pid()); + frameLength = 0; + } + break; + default: + if (frameLength < OPENTHREAD_SERIAL_BUFFER_SIZE) { + gSerialMessage[0]->buf[frameLength] = c; + frameLength++; + } + break; + } } #endif /* MODULE_OPENTHREAD_NCP_FTD */ @@ -134,21 +157,21 @@ otError otPlatUartEnable(void) gSerialMessage[i]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; } - uart_init(UART_OPENTHREAD, UART_OPENTHREAD_BAUDRATE, (uart_rx_cb_t) uart_handler, NULL); + uart_init(UART_STDIO_DEV, UART_STDIO_BAUDRATE, (uart_rx_cb_t) uart_handler, NULL); return OT_ERROR_NONE; } /* OpenThread will call this for disabling UART */ otError otPlatUartDisable(void) { - uart_poweroff(UART_OPENTHREAD); + uart_poweroff(UART_STDIO_DEV); return OT_ERROR_NONE; } /* OpenThread will call this for sending data through UART */ otError otPlatUartSend(const uint8_t *aBuf, uint16_t aBufLength) { - uart_write(UART_OPENTHREAD, aBuf, aBufLength); + uart_write(UART_STDIO_DEV, aBuf, aBufLength); /* Tell OpenThread the sending of UART is done */ otPlatUartSendDone(); diff --git a/pkg/openthread/include/ot.h b/pkg/openthread/include/ot.h index 9be33e70262f..c1de87d7e64d 100755 --- a/pkg/openthread/include/ot.h +++ b/pkg/openthread/include/ot.h @@ -35,38 +35,52 @@ extern "C" { #include "thread.h" #include "openthread/types.h" -/**< xtimer message receiver event*/ +/** + * @name Openthread message types + * @{ + */ +/** @brief xtimer message receiver event */ #define OPENTHREAD_XTIMER_MSG_TYPE_EVENT (0x2235) -/**< message received from driver */ +/** @brief message received from driver */ #define OPENTHREAD_NETDEV_MSG_TYPE_EVENT (0x2236) -/**< event indicating a serial (UART) message was sent to OpenThread */ +/** @brief event indicating a serial (UART) message was sent to OpenThread */ #define OPENTHREAD_SERIAL_MSG_TYPE_EVENT (0x2237) -/**< event for frame reception */ +/** @brief event for frame reception */ #define OPENTHREAD_MSG_TYPE_RECV (0x2238) -/**< event indicating an OT_JOB message */ +/** @brief event indicating an OT_JOB message */ #define OPENTHREAD_JOB_MSG_TYPE_EVENT (0x2240) -/**< number of serial reception buffer*/ -#define OPENTHREAD_NUMBER_OF_SERIAL_BUFFER (1U) -/**< sizeof in bytes the two first members of she serial structure*/ -#define OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF (4U) +/** @} */ + +/** + * @name Openthread constants + * @{ + */ +/** @brief number of serial reception buffer */ +#define OPENTHREAD_NUMBER_OF_SERIAL_BUFFER (1U) +/** @brief sizeof in bytes the two first members of she serial structure */ +#define OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF (4U) #ifdef MODULE_OPENTHREAD_NCP_FTD -/**< sizeof the serial buffer*/ -#define OPENTHREAD_SERIAL_BUFFER_SIZE OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF + 200 +/** @brief sizeof the serial buffer */ +#define OPENTHREAD_SERIAL_BUFFER_SIZE OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF + 200 #else -/**< sizeof the serial buffer*/ -#define OPENTHREAD_SERIAL_BUFFER_SIZE OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF + 1 +/** @brief sizeof the serial buffer */ +#define OPENTHREAD_SERIAL_BUFFER_SIZE OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF + 100 #endif -/**< sizeof the spinel payload data*/ -#define OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE OPENTHREAD_SERIAL_BUFFER_SIZE - OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF -/**< error when no more buffer available*/ -#define OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER -1 -/**< serial buffer ready to use*/ -#define OPENTHREAD_SERIAL_BUFFER_STATUS_FREE (0x0001) -/**< serial buffer ready for processsing*/ -#define OPENTHREAD_SERIAL_BUFFER_STATUS_READY_TO_PROCESS (0x0002) -/**< serial buffer payload full*/ -#define OPENTHREAD_SERIAL_BUFFER_STATUS_FULL (0x0004) - +/** @brief sizeof the spinel payload data */ +#define OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE OPENTHREAD_SERIAL_BUFFER_SIZE - OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF +/** @brief error when no more buffer available */ +#define OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER -1 +/** @brief serial buffer ready to use */ +#define OPENTHREAD_SERIAL_BUFFER_STATUS_FREE (0x0001) +/** @brief serial buffer ready for processsing */ +#define OPENTHREAD_SERIAL_BUFFER_STATUS_READY_TO_PROCESS (0x0002) +/** @brief serial buffer payload full */ +#define OPENTHREAD_SERIAL_BUFFER_STATUS_FULL (0x0004) +/** @brief Max length for IEEE802154 frame */ +#define IEEE802154_MAX_LENGTH (127U) +/** @brief Max length for a netdev buffer */ +#define OPENTHREAD_NETDEV_BUFLEN (IEEE802154_MAX_LENGTH) +/** @} */ /** * @brief Struct containing a serial message diff --git a/pkg/openthread/include/platform_config.h b/pkg/openthread/include/platform_config.h index 66d309a671fb..4747669072a6 100644 --- a/pkg/openthread/include/platform_config.h +++ b/pkg/openthread/include/platform_config.h @@ -29,11 +29,11 @@ extern "C" { * The number of message buffers in buffer pool */ #if OPENTHREAD_ENABLE_NCP_UART -#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 5 +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS (30U) #elif OPENTHREAD_MTD -#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 20 +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS (20U) #elif OPENTHREAD_FTD -#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 10 +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS (10U) #endif /** @@ -42,7 +42,7 @@ extern "C" { * Define to 1 if you want use legacy transmit done. * */ -#define OPENTHREAD_CONFIG_LEGACY_TRANSMIT_DONE 1 +#define OPENTHREAD_CONFIG_LEGACY_TRANSMIT_DONE (1U) #ifdef __cplusplus } diff --git a/tests/openthread/Makefile b/tests/openthread/Makefile index 564c70ac3243..81c593538ba3 100644 --- a/tests/openthread/Makefile +++ b/tests/openthread/Makefile @@ -26,10 +26,12 @@ else USEMODULE += openthread-cli-ftd endif -#Define PANID and CHANNEL used by default +#Define PANID, CHANNEL and UART_BAUDRATE used by default OPENTHREAD_PANID ?= 0xbeef OPENTHREAD_CHANNEL ?= 26 -CFLAGS += -DOPENTHREAD_PANID=${OPENTHREAD_PANID} -DOPENTHREAD_CHANNEL=${OPENTHREAD_CHANNEL} + +CFLAGS += -DOPENTHREAD_PANID=${OPENTHREAD_PANID} +CFLAGS += -DOPENTHREAD_CHANNEL=${OPENTHREAD_CHANNEL} ifneq (,$(filter samr21-xpro,$(BOARD))) DRIVER := at86rf233 diff --git a/tests/openthread_ncp/Makefile b/tests/openthread_ncp/Makefile index 5381ceb720cb..e9866784ecc0 100644 --- a/tests/openthread_ncp/Makefile +++ b/tests/openthread_ncp/Makefile @@ -24,9 +24,6 @@ USEPKG += openthread USEMODULE += openthread-ftd USEMODULE += openthread-ncp-ftd -#Define PANID and CHANNEL used by default -#CFLAGS += -DOPENTHREAD_PANID=0xbeef -DOPENTHREAD_CHANNEL=11 - ifneq (,$(filter samr21-xpro,$(BOARD))) DRIVER := at86rf233 endif From 0540432c6729f357ee06b1ca579daee1b0b492cf Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Fri, 5 Jan 2018 16:53:15 +0100 Subject: [PATCH 04/16] pkg/openthread: change logging functions --- pkg/openthread/contrib/platform_logging.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/pkg/openthread/contrib/platform_logging.c b/pkg/openthread/contrib/platform_logging.c index 5674a3909fc1..1c0d4a628e07 100644 --- a/pkg/openthread/contrib/platform_logging.c +++ b/pkg/openthread/contrib/platform_logging.c @@ -17,29 +17,22 @@ * @} */ -#include "openthread/config.h" -#include "openthread/platform/logging.h" -#if OPENTHREAD_ENABLE_CLI_LOGGING #include #include #include #include #include -#include "utils/code_utils.h" -#include "cli/cli-uart.h" -#endif +#include "openthread/config.h" +#include "openthread/platform/logging.h" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { -#if OPENTHREAD_ENABLE_CLI_LOGGING + (void) aLogLevel; + (void) aLogRegion; va_list args; va_start(args, aFormat); - otCliLog(aLogLevel, aLogRegion, aFormat, args); + vfprintf(stderr, aFormat, args); + fprintf(stderr, "\n"); va_end(args); -#else - (void)aLogLevel; - (void)aLogRegion; - (void)aFormat; -#endif /* OPENTHREAD_ENABLE_CLI_LOGGING */ } From c7d98c1fe77494b84cca8eff31633c6a7fcf70a4 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 8 Jan 2018 10:03:07 +0100 Subject: [PATCH 05/16] pkg/openthread: fix receive channel in mReceiveFrame --- pkg/openthread/contrib/platform_radio.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/openthread/contrib/platform_radio.c b/pkg/openthread/contrib/platform_radio.c index bccf78ff7134..c956a3e902a9 100755 --- a/pkg/openthread/contrib/platform_radio.c +++ b/pkg/openthread/contrib/platform_radio.c @@ -278,6 +278,7 @@ otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel) _set_idle(); _set_channel(aChannel); + sReceiveFrame.mChannel = aChannel; return OT_ERROR_NONE; } From 31890f93efb2413146866d044c4317afad8ce53a Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Tue, 9 Jan 2018 11:38:38 +0100 Subject: [PATCH 06/16] pkg/openthread: fix thread priority --- pkg/openthread/contrib/openthread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/openthread/contrib/openthread.c b/pkg/openthread/contrib/openthread.c index 02339307940b..938107cba6b0 100644 --- a/pkg/openthread/contrib/openthread.c +++ b/pkg/openthread/contrib/openthread.c @@ -57,5 +57,5 @@ void openthread_bootstrap(void) #endif openthread_radio_init(netdev, tx_buf, rx_buf); - openthread_netdev_init(ot_thread_stack, sizeof(ot_thread_stack), THREAD_PRIORITY_MAIN + 1, "openthread", netdev); + openthread_netdev_init(ot_thread_stack, sizeof(ot_thread_stack), THREAD_PRIORITY_MAIN - 5, "openthread", netdev); } From 5aa7ef9f53191da1d83fc4f0a27de5ff0f018e00 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Wed, 10 Jan 2018 15:47:45 +0100 Subject: [PATCH 07/16] pkg/openthread: update pkg --- pkg/openthread/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/openthread/Makefile b/pkg/openthread/Makefile index 5f08f29083fa..292dd984d870 100644 --- a/pkg/openthread/Makefile +++ b/pkg/openthread/Makefile @@ -1,6 +1,6 @@ PKG_NAME=openthread PKG_URL=https://github.com/openthread/openthread.git -PKG_VERSION=8dc31d6e279d4f04fa64b5357d96e1ed20f4967a +PKG_VERSION=ca493393a444f00611cef32fbff2da9aa653de52 PKG_BUILDDIR ?= $(BINDIRBASE)/pkg/$(BOARD)/$(PKG_NAME) ifneq (,$(filter openthread-cli-ftd,$(USEMODULE))) From 1277df0fa2669260169399c7aee9ead2df91071c Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Wed, 10 Jan 2018 15:48:00 +0100 Subject: [PATCH 08/16] pkg/openthread: update platform radio --- pkg/openthread/contrib/platform_radio.c | 124 ++++++++++++++++-------- 1 file changed, 81 insertions(+), 43 deletions(-) diff --git a/pkg/openthread/contrib/platform_radio.c b/pkg/openthread/contrib/platform_radio.c index c956a3e902a9..4685890e69d1 100755 --- a/pkg/openthread/contrib/platform_radio.c +++ b/pkg/openthread/contrib/platform_radio.c @@ -44,8 +44,6 @@ static int8_t Rssi; static netdev_t *_dev; -static bool sDisabled; - /* set 15.4 channel */ static int _set_channel(uint16_t channel) { @@ -58,6 +56,13 @@ static int _set_power(int16_t power) return _dev->driver->set(_dev, NETOPT_TX_POWER, &power, sizeof(int16_t)); } +static int _get_power(void) +{ + int16_t power; + _dev->driver->get(_dev, NETOPT_TX_POWER, &power, sizeof(int16_t)); + return power; +} + /* set IEEE802.15.4 PAN ID */ static int _set_panid(uint16_t panid) { @@ -105,6 +110,11 @@ static netopt_state_t _get_state(void) return state; } +static void _set_off(void) +{ + _set_state(NETOPT_STATE_OFF); +} + /* sets device state to SLEEP */ static void _set_sleep(void) { @@ -152,13 +162,13 @@ void recv_pkt(otInstance *aInstance, netdev_t *dev) /* Get RSSI from a radio driver. RSSI should be in [dBm] */ Rssi = (int8_t)rx_info.rssi; - sReceiveFrame.mPower = Rssi; - - DEBUG("Received message: len %d\n", (int) sReceiveFrame.mLength); - for (int i = 0; i < sReceiveFrame.mLength; ++i) { - DEBUG("%x ", sReceiveFrame.mPsdu[i]); + if (ENABLE_DEBUG) { + DEBUG("Received message: len %d\n", (int) sReceiveFrame.mLength); + for (int i = 0; i < sReceiveFrame.mLength; ++i) { + DEBUG("%x ", sReceiveFrame.mPsdu[i]); + } + DEBUG("\n"); } - DEBUG("\n"); /* Tell OpenThread that receive has finished */ otPlatRadioReceiveDone(aInstance, res > 0 ? &sReceiveFrame : NULL, res > 0 ? OT_ERROR_NONE : OT_ERROR_ABORT); @@ -173,19 +183,19 @@ void send_pkt(otInstance *aInstance, netdev_t *dev, netdev_event_t event) switch (event) { case NETDEV_EVENT_TX_COMPLETE: DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, OT_ERROR_NONE); + otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_NONE); break; case NETDEV_EVENT_TX_COMPLETE_DATA_PENDING: DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE_DATA_PENDING\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, true, OT_ERROR_NONE); + otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_NONE); break; case NETDEV_EVENT_TX_NOACK: DEBUG("openthread: NETDEV_EVENT_TX_NOACK\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, OT_ERROR_NO_ACK); + otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_NO_ACK); break; case NETDEV_EVENT_TX_MEDIUM_BUSY: DEBUG("openthread: NETDEV_EVENT_TX_MEDIUM_BUSY\n"); - otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, OT_ERROR_CHANNEL_ACCESS_FAILURE); + otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_CHANNEL_ACCESS_FAILURE); break; default: break; @@ -201,21 +211,27 @@ void otPlatRadioSetPanId(otInstance *aInstance, uint16_t panid) } /* OpenThread will call this for setting extended address */ -void otPlatRadioSetExtendedAddress(otInstance *aInstance, uint8_t *aExtendedAddress) +void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aExtendedAddress) { (void)aInstance; DEBUG("openthread: otPlatRadioSetExtendedAddress\n"); - uint8_t reversed_addr[IEEE802154_LONG_ADDRESS_LEN]; + char reversed_addr[IEEE802154_LONG_ADDRESS_LEN]; for (unsigned i = 0; i < IEEE802154_LONG_ADDRESS_LEN; i++) { - reversed_addr[i] = aExtendedAddress[IEEE802154_LONG_ADDRESS_LEN - 1 - i]; + reversed_addr[i] = (uint8_t) ((uint8_t *)aExtendedAddress)[IEEE802154_LONG_ADDRESS_LEN - 1 - i]; + } + if (ENABLE_DEBUG) { + for (unsigned i = 0; i < IEEE802154_LONG_ADDRESS_LEN; ++i) { + DEBUG("%x ", (uint8_t) ((uint8_t *)reversed_addr)[i]); + } + DEBUG("\n"); } - _set_long_addr(reversed_addr); + _set_long_addr((uint8_t*) reversed_addr); } /* OpenThread will call this for setting short address */ void otPlatRadioSetShortAddress(otInstance *aInstance, uint16_t aShortAddress) { - (void) aInstance; + (void)aInstance; DEBUG("openthread: otPlatRadioSetShortAddress: setting address to %04x\n", aShortAddress); _set_addr(((aShortAddress & 0xff) << 8) | ((aShortAddress >> 8) & 0xff)); } @@ -224,11 +240,10 @@ void otPlatRadioSetShortAddress(otInstance *aInstance, uint16_t aShortAddress) otError otPlatRadioEnable(otInstance *aInstance) { DEBUG("openthread: otPlatRadioEnable\n"); - (void) aInstance; + (void)aInstance; - if (sDisabled) { - sDisabled = false; - _set_idle(); + if (!otPlatRadioIsEnabled(aInstance)) { + _set_sleep(); } return OT_ERROR_NONE; @@ -238,11 +253,10 @@ otError otPlatRadioEnable(otInstance *aInstance) otError otPlatRadioDisable(otInstance *aInstance) { DEBUG("openthread: otPlatRadioDisable\n"); - (void) aInstance; + (void)aInstance; - if (!sDisabled) { - sDisabled = true; - _set_sleep(); + if (otPlatRadioIsEnabled(aInstance)) { + _set_off(); } return OT_ERROR_NONE; @@ -251,9 +265,9 @@ otError otPlatRadioDisable(otInstance *aInstance) bool otPlatRadioIsEnabled(otInstance *aInstance) { DEBUG("otPlatRadioIsEnabled\n"); - (void) aInstance; + (void)aInstance; netopt_state_t state = _get_state(); - if (state == NETOPT_STATE_OFF || state == NETOPT_STATE_SLEEP) { + if (state == NETOPT_STATE_OFF) { return false; } else { return true; @@ -264,7 +278,7 @@ bool otPlatRadioIsEnabled(otInstance *aInstance) otError otPlatRadioSleep(otInstance *aInstance) { DEBUG("otPlatRadioSleep\n"); - (void) aInstance; + (void)aInstance; _set_sleep(); return OT_ERROR_NONE; @@ -274,7 +288,7 @@ otError otPlatRadioSleep(otInstance *aInstance) otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel) { DEBUG("openthread: otPlatRadioReceive. Channel: %i\n", aChannel); - (void) aInstance; + (void)aInstance; _set_idle(); _set_channel(aChannel); @@ -285,7 +299,7 @@ otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel) /* OpenThread will call this function to get the transmit buffer */ otRadioFrame *otPlatRadioGetTransmitBuffer(otInstance *aInstance) { - (void) aInstance; + (void)aInstance; DEBUG("openthread: otPlatRadioGetTransmitBuffer\n"); return &sTransmitFrame; } @@ -298,10 +312,31 @@ void otPlatRadioSetDefaultTxPower(otInstance *aInstance, int8_t aPower) _set_power(aPower); } +otError otPlatRadioGetTransmitPower(otInstance *aInstance, int8_t *aPower) +{ + (void)aInstance; + if (aPower == NULL) { + return OT_ERROR_INVALID_ARGS; + } + + *aPower = _get_power(); + + return OT_ERROR_NONE; +} + +otError otPlatRadioSetTransmitPower(otInstance *aInstance, int8_t aPower) +{ + (void)aInstance; + _set_power(aPower); + + return OT_ERROR_NONE; +} + /* OpenThread will call this for transmitting a packet*/ otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) { (void) aInstance; + struct iovec pkt; /* Populate iolist with transmit data * Unlike RIOT, OpenThread includes two bytes FCS (0x00 0x00) so @@ -313,16 +348,19 @@ otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) }; /*Set channel and power based on transmit frame */ - DEBUG("otPlatRadioTransmit->channel: %i, length %d\n", (int) aPacket->mChannel, (int)aPacket->mLength); - for (int i = 0; i < aPacket->mLength; ++i) { - DEBUG("%x ", aPacket->mPsdu[i]); + if (ENABLE_DEBUG) { + DEBUG("otPlatRadioTransmit->channel: %i, length %d\n", + (int) aPacket->mChannel, (int)aPacket->mLength); + for (size_t i = 0; i < pkt.iov_len; ++i) { + DEBUG("%x ", aPacket->mPsdu[i]); + } + DEBUG("\n"); } - DEBUG("\n"); _set_channel(aPacket->mChannel); - _set_power(aPacket->mPower); /* send packet though netdev */ - _dev->driver->send(_dev, &iolist); + _dev->driver->send(_dev, &pkt, 1); + otPlatRadioTxStarted(aInstance, aPacket); return OT_ERROR_NONE; } @@ -330,16 +368,16 @@ otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) /* OpenThread will call this for getting the radio caps */ otRadioCaps otPlatRadioGetCaps(otInstance *aInstance) { - (void) aInstance; + (void)aInstance; DEBUG("openthread: otPlatRadioGetCaps\n"); /* all drivers should handle ACK, including call of NETDEV_EVENT_TX_NOACK */ - return OT_RADIO_CAPS_NONE; + return OT_RADIO_CAPS_TRANSMIT_RETRIES | OT_RADIO_CAPS_ACK_TIMEOUT; } /* OpenThread will call this for getting the state of promiscuous mode */ bool otPlatRadioGetPromiscuous(otInstance *aInstance) { - (void) aInstance; + (void)aInstance; DEBUG("openthread: otPlatRadioGetPromiscuous\n"); return _is_promiscuous(); } @@ -347,7 +385,7 @@ bool otPlatRadioGetPromiscuous(otInstance *aInstance) /* OpenThread will call this for setting the state of promiscuous mode */ void otPlatRadioSetPromiscuous(otInstance *aInstance, bool aEnable) { - (void) aInstance; + (void)aInstance; DEBUG("openthread: otPlatRadioSetPromiscuous\n"); _set_promiscuous((aEnable) ? NETOPT_ENABLE : NETOPT_DISABLE); } @@ -355,7 +393,7 @@ void otPlatRadioSetPromiscuous(otInstance *aInstance, bool aEnable) int8_t otPlatRadioGetRssi(otInstance *aInstance) { DEBUG("otPlatRadioGetRssi\n"); - (void) aInstance; + (void)aInstance; return Rssi; } @@ -374,7 +412,7 @@ otError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, const uint16_t a return OT_ERROR_NONE; } -otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const uint8_t *aExtAddress) +otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) { DEBUG("otPlatRadioAddSrcMatchExtEntry\n"); (void)aInstance; @@ -390,7 +428,7 @@ otError otPlatRadioClearSrcMatchShortEntry(otInstance *aInstance, const uint16_t return OT_ERROR_NONE; } -otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const uint8_t *aExtAddress) +otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress) { DEBUG("otPlatRadioClearSrcMatchExtEntry\n"); (void)aInstance; From b9aaf7f11e27e2b8ee6b7eaedae9d4fc2b0e31bc Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Wed, 10 Jan 2018 15:48:18 +0100 Subject: [PATCH 09/16] pkg/openthread: fix change of alarm header --- pkg/openthread/contrib/netdev/openthread_netdev.c | 6 +++--- pkg/openthread/contrib/openthread.c | 2 +- pkg/openthread/contrib/platform_alarm.c | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/openthread/contrib/netdev/openthread_netdev.c b/pkg/openthread/contrib/netdev/openthread_netdev.c index bf2f56e9afff..8569b1cc2294 100755 --- a/pkg/openthread/contrib/netdev/openthread_netdev.c +++ b/pkg/openthread/contrib/netdev/openthread_netdev.c @@ -24,7 +24,7 @@ #include "openthread/cli.h" #include "openthread/instance.h" #include "openthread/ip6.h" -#include "openthread/platform/alarm.h" +#include "openthread/platform/alarm-milli.h" #include "openthread/platform/uart.h" #include "openthread/tasklet.h" #include "openthread/thread.h" @@ -71,7 +71,7 @@ static void *_openthread_event_loop(void *arg) { otPlatUartEnable(); /* init OpenThread */ - sInstance = otInstanceInit(); + sInstance = otInstanceInitSingle(); msg_init_queue(_queue, OPENTHREAD_QUEUE_LEN); netdev_t *dev; @@ -107,7 +107,7 @@ static void *_openthread_event_loop(void *arg) { switch (msg.type) { case OPENTHREAD_XTIMER_MSG_TYPE_EVENT: /* Tell OpenThread a time event was received */ - otPlatAlarmFired(sInstance); + otPlatAlarmMilliFired(sInstance); break; case OPENTHREAD_NETDEV_MSG_TYPE_EVENT: /* Received an event from driver */ diff --git a/pkg/openthread/contrib/openthread.c b/pkg/openthread/contrib/openthread.c index 938107cba6b0..d9584b3a1125 100644 --- a/pkg/openthread/contrib/openthread.c +++ b/pkg/openthread/contrib/openthread.c @@ -18,7 +18,7 @@ #include -#include "openthread/platform/alarm.h" +#include "openthread/platform/alarm-milli.h" #include "openthread/platform/uart.h" #include "ot.h" #include "random.h" diff --git a/pkg/openthread/contrib/platform_alarm.c b/pkg/openthread/contrib/platform_alarm.c index 52b8d48caa66..f7d50b0d4f84 100644 --- a/pkg/openthread/contrib/platform_alarm.c +++ b/pkg/openthread/contrib/platform_alarm.c @@ -19,7 +19,7 @@ #include #include "msg.h" -#include "openthread/platform/alarm.h" +#include "openthread/platform/alarm-milli.h" #include "ot.h" #include "thread.h" #include "xtimer.h" @@ -38,7 +38,7 @@ static msg_t ot_alarm_msg; * @param[in] aT0 The reference time. * @param[in] aDt The time delay in milliseconds from @p aT0. */ -void otPlatAlarmStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) +void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) { (void)aInstance; (void)aT0; @@ -56,7 +56,7 @@ void otPlatAlarmStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) } /* OpenThread will call this to stop alarms */ -void otPlatAlarmStop(otInstance *aInstance) +void otPlatAlarmMilliStop(otInstance *aInstance) { (void)aInstance; DEBUG("openthread: otPlatAlarmStop\n"); @@ -64,7 +64,7 @@ void otPlatAlarmStop(otInstance *aInstance) } /* OpenThread will call this for getting running time in millisecs */ -uint32_t otPlatAlarmGetNow(void) +uint32_t otPlatAlarmMilliGetNow(void) { uint32_t now = xtimer_now_usec() / US_PER_MS; DEBUG("openthread: otPlatAlarmGetNow: %" PRIu32 "\n", now); From 9ae7d4dcbb334a8cc523eb53e6219be52c57bb56 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 15 Jan 2018 18:54:46 +0100 Subject: [PATCH 10/16] pkg/openthread: update platform config --- pkg/openthread/include/platform_config.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/openthread/include/platform_config.h b/pkg/openthread/include/platform_config.h index 4747669072a6..251fbc9cad89 100644 --- a/pkg/openthread/include/platform_config.h +++ b/pkg/openthread/include/platform_config.h @@ -36,13 +36,15 @@ extern "C" { #define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS (10U) #endif + /** - * @def OPENTHREAD_CONFIG_LEGACY_TRANSMIT_DONE - * - * Define to 1 if you want use legacy transmit done. - * - */ -#define OPENTHREAD_CONFIG_LEGACY_TRANSMIT_DONE (1U) + * @def OPENTHREAD_CONFIG_LOG_LEVEL + * + * Set OpenThread log level + * + * @see https://openthread.io/releases/thread-reference-20170716/group/plat-logging + */ +#define OPENTHREAD_CONFIG_LOG_LEVEL OT_LOG_LEVEL_NONE #ifdef __cplusplus } From c99631e694b356223b64c018165675351c6824af Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Tue, 27 Mar 2018 18:42:34 +0200 Subject: [PATCH 11/16] pkg/openthread: fix old iovec code --- pkg/openthread/contrib/platform_radio.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/openthread/contrib/platform_radio.c b/pkg/openthread/contrib/platform_radio.c index 4685890e69d1..04cfc703d291 100755 --- a/pkg/openthread/contrib/platform_radio.c +++ b/pkg/openthread/contrib/platform_radio.c @@ -336,7 +336,6 @@ otError otPlatRadioSetTransmitPower(otInstance *aInstance, int8_t aPower) otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) { (void) aInstance; - struct iovec pkt; /* Populate iolist with transmit data * Unlike RIOT, OpenThread includes two bytes FCS (0x00 0x00) so @@ -351,7 +350,7 @@ otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) if (ENABLE_DEBUG) { DEBUG("otPlatRadioTransmit->channel: %i, length %d\n", (int) aPacket->mChannel, (int)aPacket->mLength); - for (size_t i = 0; i < pkt.iov_len; ++i) { + for (size_t i = 0; i < aPacket->mLength; ++i) { DEBUG("%x ", aPacket->mPsdu[i]); } DEBUG("\n"); @@ -359,7 +358,7 @@ otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) _set_channel(aPacket->mChannel); /* send packet though netdev */ - _dev->driver->send(_dev, &pkt, 1); + _dev->driver->send(_dev, &iolist); otPlatRadioTxStarted(aInstance, aPacket); return OT_ERROR_NONE; From a08201c18f5bcdacbbf411ec5fa111910a4a8f9c Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Tue, 27 Mar 2018 19:20:34 +0200 Subject: [PATCH 12/16] pkg/openthread: update pkg version --- pkg/openthread/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/openthread/Makefile b/pkg/openthread/Makefile index 292dd984d870..72475668086d 100644 --- a/pkg/openthread/Makefile +++ b/pkg/openthread/Makefile @@ -1,6 +1,6 @@ PKG_NAME=openthread PKG_URL=https://github.com/openthread/openthread.git -PKG_VERSION=ca493393a444f00611cef32fbff2da9aa653de52 +PKG_VERSION=976f7a4078e126d7200f128ded84cb81c8761844 PKG_BUILDDIR ?= $(BINDIRBASE)/pkg/$(BOARD)/$(PKG_NAME) ifneq (,$(filter openthread-cli-ftd,$(USEMODULE))) From 0ea7baea2dc1a3f065e78c1b4c4fda63c9ffcc2f Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Tue, 27 Mar 2018 19:33:32 +0200 Subject: [PATCH 13/16] pkg/openthread: remove old header file --- pkg/openthread/contrib/platform_radio.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/openthread/contrib/platform_radio.c b/pkg/openthread/contrib/platform_radio.c index 04cfc703d291..71f74a2002ea 100755 --- a/pkg/openthread/contrib/platform_radio.c +++ b/pkg/openthread/contrib/platform_radio.c @@ -29,7 +29,6 @@ #include "openthread/config.h" #include "openthread/openthread.h" #include "openthread/platform/diag.h" -#include "openthread/platform/platform.h" #include "openthread/platform/radio.h" #include "ot.h" From 252738ccadbe3e9481e709374ab36586e44d09c4 Mon Sep 17 00:00:00 2001 From: Hyungsin Date: Wed, 18 Apr 2018 15:43:52 -0700 Subject: [PATCH 14/16] pkg/openthread: fix uart --- pkg/openthread/contrib/platform_uart.c | 43 ++++++++++++-------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/pkg/openthread/contrib/platform_uart.c b/pkg/openthread/contrib/platform_uart.c index f2be654a0daa..e1073222c20f 100755 --- a/pkg/openthread/contrib/platform_uart.c +++ b/pkg/openthread/contrib/platform_uart.c @@ -32,35 +32,32 @@ #define OPENTHREAD_SPINEL_FRAME_MARKER (0x7e) -serial_msg_t * gSerialMessage[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER]; -uint8_t gSerialBuff[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER][OPENTHREAD_SERIAL_BUFFER_SIZE]; +static serial_msg_t * gSerialMessage[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER]; +static uint8_t gSerialBuff[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER][OPENTHREAD_SERIAL_BUFFER_SIZE]; +static uint16_t frameLength = 0; #ifdef MODULE_OPENTHREAD_NCP_FTD +static int8_t currentSerialBufferNumber = 0; +static bool gOnGoingSpinelReception = false; + int8_t getFirstEmptySerialBuffer(void) { - uint8_t i = 0; + int8_t i = 0; for (i = 0; i < OPENTHREAD_NUMBER_OF_SERIAL_BUFFER; i++) { - if ( OPENTHREAD_SERIAL_BUFFER_STATUS_FREE == gSerialMessage[i]->serial_buffer_status ) { + if (gSerialMessage[i]->serial_buffer_status == OPENTHREAD_SERIAL_BUFFER_STATUS_FREE) { break; } } - if ( i >= OPENTHREAD_NUMBER_OF_SERIAL_BUFFER ) { + if (i >= OPENTHREAD_NUMBER_OF_SERIAL_BUFFER) { return OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER; } else { return i; } } -/* OpenThread will call this for enabling UART (required for OpenThread's NCP)*/ -void uart_handler(void* arg, char c) { - - (void)arg; - static int16_t currentSerialBufferNumber = 0; - static uint8_t frameLength = 0; - static uint8_t gOnGoingSpinelReception = 0; - - msg_t msg; - msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; +/* UART interrupt handler (required for OpenThread's NCP)*/ +static void uart_handler(void* arg, char c) { + (void) arg; if ((c == OPENTHREAD_SPINEL_FRAME_MARKER) && (gOnGoingSpinelReception == false)) { /* Start of Spinel Frame */ currentSerialBufferNumber = getFirstEmptySerialBuffer(); @@ -75,7 +72,7 @@ void uart_handler(void* arg, char c) { gOnGoingSpinelReception = true; } else if ((c == OPENTHREAD_SPINEL_FRAME_MARKER) && (gOnGoingSpinelReception == true)) { /* End of Spinel Frame */ - if ( OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER == currentSerialBufferNumber ) { + if (currentSerialBufferNumber == OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER) { return; } if (frameLength == 1) { /* It means that we handle the Start of a Spinel frame instead of the end */ @@ -83,10 +80,12 @@ void uart_handler(void* arg, char c) { frameLength--; return; } - if( OPENTHREAD_SERIAL_BUFFER_STATUS_FULL != gSerialMessage[currentSerialBufferNumber]->serial_buffer_status) { + if(gSerialMessage[currentSerialBufferNumber]->serial_buffer_status != OPENTHREAD_SERIAL_BUFFER_STATUS_FULL) { gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = (uint8_t) c; gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_READY_TO_PROCESS; gSerialMessage[currentSerialBufferNumber]->length = frameLength + 1; + msg_t msg; + msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; msg.content.ptr = gSerialMessage[currentSerialBufferNumber]; msg_send_int(&msg, openthread_get_pid()); } @@ -97,17 +96,17 @@ void uart_handler(void* arg, char c) { frameLength = 0; } else if (gOnGoingSpinelReception == true) { /* Payload of Spinel Frame */ - if ( OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER == currentSerialBufferNumber ) { + if (currentSerialBufferNumber == OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER) { return; } - if ( OPENTHREAD_SERIAL_BUFFER_STATUS_FULL != gSerialMessage[currentSerialBufferNumber]->serial_buffer_status) { + if (gSerialMessage[currentSerialBufferNumber]->serial_buffer_status != OPENTHREAD_SERIAL_BUFFER_STATUS_FULL) { gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = (uint8_t) c; } } if (gOnGoingSpinelReception == true) { frameLength++; - if ( frameLength >= OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE) { + if (frameLength >= OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE) { DEBUG("SERIAL: ERROR => OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE overflowed\n"); gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FULL; } @@ -116,11 +115,9 @@ void uart_handler(void* arg, char c) { #else -void uart_handler(void* arg, char c) -{ +static void uart_handler(void* arg, char c) { (void)arg; - static uint16_t frameLength = 0; if (frameLength == 0 && gSerialMessage != NULL) { memset(gSerialMessage[0], 0, sizeof(serial_msg_t)); } From 8619e021d1687e5ce478cd4ff7ec6859d6cbfa8b Mon Sep 17 00:00:00 2001 From: Hyungsin Date: Mon, 30 Apr 2018 22:07:38 -0700 Subject: [PATCH 15/16] pkg/openthread: fix bit alignment error of uart driver --- pkg/openthread/contrib/platform_uart.c | 40 ++++++++++++-------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/pkg/openthread/contrib/platform_uart.c b/pkg/openthread/contrib/platform_uart.c index e1073222c20f..c403c8cce0c6 100755 --- a/pkg/openthread/contrib/platform_uart.c +++ b/pkg/openthread/contrib/platform_uart.c @@ -32,8 +32,7 @@ #define OPENTHREAD_SPINEL_FRAME_MARKER (0x7e) -static serial_msg_t * gSerialMessage[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER]; -static uint8_t gSerialBuff[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER][OPENTHREAD_SERIAL_BUFFER_SIZE]; +static serial_msg_t gSerialMessage[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER]; static uint16_t frameLength = 0; #ifdef MODULE_OPENTHREAD_NCP_FTD @@ -43,7 +42,7 @@ static bool gOnGoingSpinelReception = false; int8_t getFirstEmptySerialBuffer(void) { int8_t i = 0; for (i = 0; i < OPENTHREAD_NUMBER_OF_SERIAL_BUFFER; i++) { - if (gSerialMessage[i]->serial_buffer_status == OPENTHREAD_SERIAL_BUFFER_STATUS_FREE) { + if (gSerialMessage[i].serial_buffer_status == OPENTHREAD_SERIAL_BUFFER_STATUS_FREE) { break; } } @@ -67,7 +66,7 @@ static void uart_handler(void* arg, char c) { } frameLength = 0; - gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = c; + gSerialMessage[currentSerialBufferNumber].buf[frameLength] = c; gOnGoingSpinelReception = true; } @@ -80,17 +79,17 @@ static void uart_handler(void* arg, char c) { frameLength--; return; } - if(gSerialMessage[currentSerialBufferNumber]->serial_buffer_status != OPENTHREAD_SERIAL_BUFFER_STATUS_FULL) { - gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = (uint8_t) c; - gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_READY_TO_PROCESS; - gSerialMessage[currentSerialBufferNumber]->length = frameLength + 1; + if(gSerialMessage[currentSerialBufferNumber].serial_buffer_status != OPENTHREAD_SERIAL_BUFFER_STATUS_FULL) { + gSerialMessage[currentSerialBufferNumber].buf[frameLength] = (uint8_t) c; + gSerialMessage[currentSerialBufferNumber].serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_READY_TO_PROCESS; + gSerialMessage[currentSerialBufferNumber].length = frameLength + 1; msg_t msg; msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; - msg.content.ptr = gSerialMessage[currentSerialBufferNumber]; + msg.content.ptr = &gSerialMessage[currentSerialBufferNumber]; msg_send_int(&msg, openthread_get_pid()); } else { - gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; + gSerialMessage[currentSerialBufferNumber].serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; } gOnGoingSpinelReception = false; frameLength = 0; @@ -99,8 +98,8 @@ static void uart_handler(void* arg, char c) { if (currentSerialBufferNumber == OPENTHREAD_ERROR_NO_EMPTY_SERIAL_BUFFER) { return; } - if (gSerialMessage[currentSerialBufferNumber]->serial_buffer_status != OPENTHREAD_SERIAL_BUFFER_STATUS_FULL) { - gSerialMessage[currentSerialBufferNumber]->buf[frameLength] = (uint8_t) c; + if (gSerialMessage[currentSerialBufferNumber].serial_buffer_status != OPENTHREAD_SERIAL_BUFFER_STATUS_FULL) { + gSerialMessage[currentSerialBufferNumber].buf[frameLength] = (uint8_t) c; } } @@ -108,7 +107,7 @@ static void uart_handler(void* arg, char c) { frameLength++; if (frameLength >= OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE) { DEBUG("SERIAL: ERROR => OPENTHREAD_SERIAL_BUFFER__PAYLOAD_SIZE overflowed\n"); - gSerialMessage[currentSerialBufferNumber]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FULL; + gSerialMessage[currentSerialBufferNumber].serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FULL; } } } @@ -118,26 +117,26 @@ static void uart_handler(void* arg, char c) { static void uart_handler(void* arg, char c) { (void)arg; - if (frameLength == 0 && gSerialMessage != NULL) { - memset(gSerialMessage[0], 0, sizeof(serial_msg_t)); + if (frameLength == 0) { + memset(&gSerialMessage[0], 0, sizeof(serial_msg_t)); } switch (c) { case '\r': case '\n': if (frameLength > 0) { - gSerialMessage[0]->buf[frameLength] = c; + gSerialMessage[0].buf[frameLength] = c; frameLength++; - gSerialMessage[0]->length = frameLength; + gSerialMessage[0].length = frameLength; msg_t msg; msg.type = OPENTHREAD_SERIAL_MSG_TYPE_EVENT; - msg.content.ptr = gSerialMessage[0]; + msg.content.ptr = &gSerialMessage[0]; msg_send_int(&msg, openthread_get_pid()); frameLength = 0; } break; default: if (frameLength < OPENTHREAD_SERIAL_BUFFER_SIZE) { - gSerialMessage[0]->buf[frameLength] = c; + gSerialMessage[0].buf[frameLength] = c; frameLength++; } break; @@ -150,8 +149,7 @@ static void uart_handler(void* arg, char c) { otError otPlatUartEnable(void) { for (uint8_t i = 0; i < OPENTHREAD_NUMBER_OF_SERIAL_BUFFER; i++) { - gSerialMessage[i] = (serial_msg_t*) &gSerialBuff[i]; - gSerialMessage[i]->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; + gSerialMessage[i].serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; } uart_init(UART_STDIO_DEV, UART_STDIO_BAUDRATE, (uart_rx_cb_t) uart_handler, NULL); From 9b5b0bd9387606c4dbfcb1d09e68afbdd5d82632 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Thu, 3 May 2018 13:18:29 +0200 Subject: [PATCH 16/16] pkg/openthread: fix build issue --- pkg/openthread/contrib/platform_uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/openthread/contrib/platform_uart.c b/pkg/openthread/contrib/platform_uart.c index c403c8cce0c6..d7473016dd66 100755 --- a/pkg/openthread/contrib/platform_uart.c +++ b/pkg/openthread/contrib/platform_uart.c @@ -40,7 +40,7 @@ static int8_t currentSerialBufferNumber = 0; static bool gOnGoingSpinelReception = false; int8_t getFirstEmptySerialBuffer(void) { - int8_t i = 0; + uint8_t i = 0; for (i = 0; i < OPENTHREAD_NUMBER_OF_SERIAL_BUFFER; i++) { if (gSerialMessage[i].serial_buffer_status == OPENTHREAD_SERIAL_BUFFER_STATUS_FREE) { break;