diff --git a/Makefile.dep b/Makefile.dep index e083323e1b0e67d82efda6c7a4107840c9560b60..d6ec40a171adb89e22a6cdbaf0116b969cc45478 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -731,6 +731,11 @@ ifneq (,$(filter gcoap,$(USEMODULE))) USEMODULE += sock_util 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 77da3995d942fe37f05773d6098778541eed233e..aa2bcf94801f024411b47ea1791fe4a4da5e1b1e 100644 --- a/pkg/openthread/Makefile +++ b/pkg/openthread/Makefile @@ -1,24 +1,31 @@ PKG_NAME=openthread PKG_URL=https://github.com/openthread/openthread.git -PKG_VERSION=fbfd76a990b81f007957e1bd774e51bce742e53e +PKG_VERSION=thread-reference-20170716 PKG_BUILDDIR ?= $(PKGDIRBASE)/$(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 +OPENTHREAD_ARGS += --enable-application-coap +CONFIG_FILE = OPENTHREAD_PROJECT_CORE_CONFIG_FILE='\"platform_config.h\"' +$(info $$OPENTHREAD_ARGS is [${OPENTHREAD_ARGS}]) .PHONY: all OPENTHREAD_COMMON_FLAGS = -fdata-sections -ffunction-sections -Os -OPENTHREAD_COMMON_FLAGS += -Wno-implicit-fallthrough +OPENTHREAD_COMMON_FLAGS += -Wno-implicit-fallthrough -Wno-unused-parameter all: git-download cd $(PKG_BUILDDIR) && PREFIX="/" ./bootstrap 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 +34,15 @@ 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 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 e2efe807b10c39e171c0b2484773012429ca2680..d399a8f90253d2e172879230a5c8d5fa22598556 100644 --- a/pkg/openthread/Makefile.include +++ b/pkg/openthread/Makefile.include @@ -1,8 +1,7 @@ OPENTHREAD_DIR = $(RIOTBASE)/pkg/openthread INCLUDES += -I$(OPENTHREAD_DIR)/include \ - -I$(PKGDIRBASE)/openthread/output/include \ - -I$(PKGDIRBASE)/openthread/include/openthread \ + -I$(PKGDIRBASE)/openthread/include 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 446076cf586cd2d21bb1c3496a85855b76ebcae3..862290a98136a0fcc8cfc615fee210aab2e3094c --- 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 <jialamos@uc.cl> + * @author Baptiste Clenet <bapclenet@gmail.com> * @} */ @@ -23,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" @@ -39,18 +40,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 +56,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) { @@ -78,18 +67,14 @@ static void *_openthread_event_loop(void *arg) { otPlatUartEnable(); /* init OpenThread */ - sInstance = otInstanceInit(); + sInstance = otInstanceInitSingle(); msg_init_queue(_queue, OPENTHREAD_QUEUE_LEN); 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 +84,40 @@ static void *_openthread_event_loop(void *arg) { otIp6SetEnabled(sInstance, true); /* Start Thread protocol operation */ otThreadSetEnabled(sInstance, true); +#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 */ + otPlatAlarmMilliFired(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 882bb9bd7ae69a4a059cfc02084e200079fc2a15..d9584b3a112535e813b02a75a7d29d84437b3a4d 100644 --- a/pkg/openthread/contrib/openthread.c +++ b/pkg/openthread/contrib/openthread.c @@ -18,7 +18,7 @@ #include <assert.h> -#include "openthread/platform/alarm.h" +#include "openthread/platform/alarm-milli.h" #include "openthread/platform/uart.h" #include "ot.h" #include "random.h" @@ -41,29 +41,10 @@ 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]; -/* 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 */ diff --git a/pkg/openthread/contrib/platform_alarm.c b/pkg/openthread/contrib/platform_alarm.c index 52b8d48caa66632dd0c33f8586d73d93c619fe9a..f7d50b0d4f84747be15dbdc79ac1b4681ed840c4 100644 --- a/pkg/openthread/contrib/platform_alarm.c +++ b/pkg/openthread/contrib/platform_alarm.c @@ -19,7 +19,7 @@ #include <stdint.h> #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); diff --git a/pkg/openthread/contrib/platform_functions_wrapper.c b/pkg/openthread/contrib/platform_functions_wrapper.c index 832765b4f2c293274b884670a853f64b119a2249..8693f70d17b0779dc6f86904a2a15612ca9b9446 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 a7deade082bc957757e3674cd7988a0902579838..4917354519646f79112ce421693566a1b27b3779 100644 --- a/pkg/openthread/contrib/platform_logging.c +++ b/pkg/openthread/contrib/platform_logging.c @@ -13,6 +13,7 @@ * @brief Implementation of OpenThread logging platform abstraction * * @author Jose Ignacio Alamos <jialamos@uc.cl> + * @author Baptiste Clenet <bapclenet@gmail.com> * @} */ @@ -20,10 +21,9 @@ #include <inttypes.h> #include <stdarg.h> #include <stdio.h> -#include <stdint.h> #include <string.h> -#include <time.h> +#include "openthread/config.h" #include "openthread/platform/logging.h" /* adapted from OpenThread posix example: @@ -31,68 +31,11 @@ __attribute__((__format__ (__printf__, 3, 4))) void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { + (void) aLogLevel; + (void) aLogRegion; 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"); + fprintf(stderr, "\n"); va_end(args); } diff --git a/pkg/openthread/contrib/platform_misc.c b/pkg/openthread/contrib/platform_misc.c index d8bc9af2b3c60c0fe2aa795ecc587f67cc90f544..2d6e97a56bb4ffaf601d5d0c019d4fad56fcf3e7 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 8095bb658089dabfe1375442dc4d023ad8c0a6f2..992a6f97069ec54edaa2fad8367cdb78f7d7a891 --- a/pkg/openthread/contrib/platform_radio.c +++ b/pkg/openthread/contrib/platform_radio.c @@ -26,6 +26,9 @@ #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/radio.h" #include "ot.h" @@ -34,14 +37,12 @@ #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; -static bool sDisabled; - /* set 15.4 channel */ static int _set_channel(uint16_t channel) { @@ -54,6 +55,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) { @@ -101,6 +109,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) { @@ -134,7 +147,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; } @@ -148,16 +161,16 @@ 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 ? kThreadError_None : kThreadError_Abort); + otPlatRadioReceiveDone(aInstance, res > 0 ? &sReceiveFrame : NULL, res > 0 ? OT_ERROR_NONE : OT_ERROR_ABORT); } /* Called upon TX event */ @@ -169,19 +182,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); + 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, kThreadError_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, kThreadError_NoAck); + 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, kThreadError_ChannelAccessFailure); + otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_CHANNEL_ACCESS_FAILURE); break; default: break; @@ -201,55 +214,59 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, uint8_t *aExtendedAddr { (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)); } /* OpenThread will call this for enabling the radio */ -ThreadError otPlatRadioEnable(otInstance *aInstance) +otError otPlatRadioEnable(otInstance *aInstance) { DEBUG("openthread: otPlatRadioEnable\n"); - (void) aInstance; + (void)aInstance; - if (sDisabled) { - sDisabled = false; - _set_idle(); + if (!otPlatRadioIsEnabled(aInstance)) { + _set_sleep(); } - 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; + (void)aInstance; - if (!sDisabled) { - sDisabled = true; - _set_sleep(); + if (otPlatRadioIsEnabled(aInstance)) { + _set_off(); } - return kThreadError_None; + return OT_ERROR_NONE; } 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; @@ -257,30 +274,31 @@ 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; + (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; + (void)aInstance; _set_idle(); _set_channel(aChannel); - return kThreadError_None; + sReceiveFrame.mChannel = aChannel; + return OT_ERROR_NONE; } /* OpenThread will call this function to get the transmit buffer */ -RadioPacket *otPlatRadioGetTransmitBuffer(otInstance *aInstance) +otRadioFrame *otPlatRadioGetTransmitBuffer(otInstance *aInstance) { - (void) aInstance; + (void)aInstance; DEBUG("openthread: otPlatRadioGetTransmitBuffer\n"); return &sTransmitFrame; } @@ -293,8 +311,28 @@ 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*/ -ThreadError otPlatRadioTransmit(otInstance *aInstance, RadioPacket *aPacket) +otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) { (void) aInstance; @@ -308,33 +346,36 @@ ThreadError otPlatRadioTransmit(otInstance *aInstance, RadioPacket *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 < aPacket->mLength; ++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); + otPlatRadioTxStarted(aInstance, aPacket); - return kThreadError_None; + return OT_ERROR_NONE; } /* 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 kRadioCapsNone; + 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(); } @@ -342,7 +383,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); } @@ -350,7 +391,7 @@ void otPlatRadioSetPromiscuous(otInstance *aInstance, bool aEnable) int8_t otPlatRadioGetRssi(otInstance *aInstance) { DEBUG("otPlatRadioGetRssi\n"); - (void) aInstance; + (void)aInstance; return Rssi; } @@ -361,36 +402,36 @@ 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; (void)aExtAddress; - return kThreadError_None; + return OT_ERROR_NONE; } void otPlatRadioClearSrcMatchShortEntries(otInstance *aInstance) @@ -405,13 +446,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 c82bb43ebc7451775caeb68d36ed5c6df7ad8fcc..ae8eb27023acdc5031dbcce4d86730254c625953 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 e95cea34eedb6782bd8fc06b261cf2fb70e0c38a..b8ae084037b2c96b7511657f02cea443e0a8b1ab 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 d3aff67af3fc18216f255a0397bf004a3e230faf..36a48a762fe7aa7648bf93e911b892275c93b8d2 --- a/pkg/openthread/contrib/platform_uart.c +++ b/pkg/openthread/contrib/platform_uart.c @@ -13,34 +13,82 @@ * @brief Implementation of OpenThread UART platform abstraction * * @author Jose Ignacio Alamos <jialamos@uc.cl> + * @author Baptiste Clenet <bapclenet@gmail.com> * @} */ #include <stdint.h> #include <stdio.h> +#include <string.h> +#include "uart_stdio.h" #include "periph/uart.h" +#include "openthread/types.h" #include "openthread/platform/uart.h" +#include "ot.h" + +#define ENABLE_DEBUG (0) +#include "debug.h" + +#define OPENTHREAD_SPINEL_FRAME_MARKER (0x7e) + +static serial_msg_t gSerialMessage[OPENTHREAD_NUMBER_OF_SERIAL_BUFFER]; +static uint16_t frameLength = 0; + +static void uart_handler(void* arg, char c) { + (void)arg; + + 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; + 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; + } +} /* 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_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE; + } + + 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 */ -ThreadError otPlatUartDisable(void) +otError otPlatUartDisable(void) { - return kThreadError_None; + uart_poweroff(UART_STDIO_DEV); + 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_STDIO_DEV, 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 6741ceadbc95b8274faf466b4607089a63a4d3c3..9a9c66485fd4d2df19187aa4572f58f4adb95b52 --- a/pkg/openthread/include/ot.h +++ b/pkg/openthread/include/ot.h @@ -17,7 +17,8 @@ * * @file * - * @author José Ignacio Alamos <jialamos@uc.cl> + * @author Jose Ignacio Alamos <jialamos@uc.cl> + * @author Baptiste Clenet <bapclenet@gmail.com> */ #ifndef OT_H @@ -34,18 +35,55 @@ 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 */ +/** + * @name Openthread message types + * @{ + */ +/** @brief xtimer message receiver event */ +#define OPENTHREAD_XTIMER_MSG_TYPE_EVENT (0x2235) +/** @brief message received from driver */ +#define OPENTHREAD_NETDEV_MSG_TYPE_EVENT (0x2236) +/** @brief event indicating a serial (UART) message was sent to OpenThread */ +#define OPENTHREAD_SERIAL_MSG_TYPE_EVENT (0x2237) +/** @brief event for frame reception */ +#define OPENTHREAD_MSG_TYPE_RECV (0x2238) +/** @brief event indicating an OT_JOB message */ +#define OPENTHREAD_JOB_MSG_TYPE_EVENT (0x2240) +/** @} */ + +/** + * @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) +/** @brief sizeof the serial buffer */ +#define OPENTHREAD_SERIAL_BUFFER_SIZE OPENTHREAD_SIZEOF_LENGTH_AND_FREEBUFF + 100 +/** @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 */ 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 +153,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 0000000000000000000000000000000000000000..46527a4e72fa6192df76b0f6c9cc48d003f2621c --- /dev/null +++ b/pkg/openthread/include/platform_config.h @@ -0,0 +1,51 @@ +/* + * 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 <bapclenet@gmail.com> + * @} + */ + +#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 + */ +#if OPENTHREAD_MTD +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS (20U) +#else +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS (10U) +#endif + + +/** + * @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 +} +#endif + +#endif /* PLATFORM_CONFIG_H */