Merge pull request #9336 from jia200x/openthread_ftd

pkg/openthread: rework of FTD and MTD support
This commit is contained in:
Alexandre Abadie 2018-10-16 17:23:55 +02:00 committed by GitHub
commit 77d97a6fa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 539 additions and 354 deletions

View File

@ -649,11 +649,6 @@ ifneq (,$(filter random,$(USEMODULE)))
USEMODULE += luid USEMODULE += luid
endif endif
ifneq (,$(filter openthread_contrib,$(USEMODULE)))
USEMODULE += openthread_contrib_netdev
FEATURES_REQUIRED += cpp
endif
ifneq (,$(filter asymcute,$(USEMODULE))) ifneq (,$(filter asymcute,$(USEMODULE)))
USEMODULE += sock_udp USEMODULE += sock_udp
USEMODULE += sock_util USEMODULE += sock_util

View File

@ -0,0 +1,52 @@
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:
RIOTBASE ?= $(CURDIR)/../..
# Change this to 0 show compiler invocation lines by default:
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
else
# ftd: A Full Thread Device has router functionality compiled in
USEMODULE += openthread-ftd
USEMODULE += openthread-cli-ftd
endif
#Define PANID, CHANNEL and UART_BAUDRATE used by default
OPENTHREAD_PANID ?= 0xbeef
OPENTHREAD_CHANNEL ?= 26
CFLAGS += -DOPENTHREAD_PANID=$(OPENTHREAD_PANID)
CFLAGS += -DOPENTHREAD_CHANNEL=$(OPENTHREAD_CHANNEL)
ifneq (,$(filter samr21-xpro,$(BOARD)))
DRIVER := at86rf233
endif
ifneq (,$(filter iotlab-m3 fox iotlab-a8-m3,$(BOARD)))
DRIVER := at86rf231
endif
USEMODULE += $(DRIVER)
USEMODULE += random
USEMODULE += ps
#required for C++ compiling
CXXEXFLAGS += -fno-rtti
USEMODULE += cpp11-compat
include $(RIOTBASE)/Makefile.include

View File

@ -0,0 +1,70 @@
## OpenThread on RIOT
This example 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
With RIOT port, a node is auto-setup and ready to communicate with
this configuration:
```
OPENTHREAD_PANID=0xbeef
OPENTHREAD_CHANNEL=26
```
You can pass the panid/channel independently when building the firmware:
```
make BOARD=<target> OPENTHREAD_PANID=0xaaaa OPENTHREAD_TYPE=ftd flash term
```
```
make BOARD=<target> OPENTHREAD_CHANNEL=20 OPENTHREAD_TYPE=ftd flash term
```
To try OpenThread in RIOT, you can do the following:
1. Flash nodes with MTD or FTD functionality:
```
make BOARD=<target> clean all flash OPENTHREAD_TYPE=mtd
```
```
make BOARD=<target> 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
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`
## OpenThread port on RIOT status
OpenThread port on RIOT is stable. In case of any bug, please report via
[GitHub issue](https://github.com/RIOT-OS/RIOT/issues/new?template=bug_report.md&title=Bug).

View File

@ -10,25 +10,21 @@
* @file * @file
* @brief OpenThread test application * @brief OpenThread test application
* *
* @author Baptiste Clenet <baptiste.clenet@xsoen.com> * @author Baptiste Clenet <bapclenet@gmail.com>
*/ */
#include <stdio.h> #include <stdio.h>
#include "net/ipv6/addr.h"
#include "openthread/ip6.h"
#include "openthread/thread.h"
#include "openthread/udp.h"
#include "ot.h" #include "ot.h"
#include "shell.h"
#include "shell_commands.h"
int main(void) 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; uint16_t panid = 0;
uint8_t res = ot_call_command("panid", NULL, (void*)&panid); uint8_t res = ot_call_command("panid", NULL, (void*)&panid);
printf("Current panid: 0x%x (res:%x)\n", panid, res); printf("Current panid: 0x%x (res:%x)\n", panid, res);
openthread_uart_run();
return 0; return 0;
} }

View File

@ -1,24 +1,31 @@
PKG_NAME=openthread PKG_NAME=openthread
PKG_URL=https://github.com/openthread/openthread.git PKG_URL=https://github.com/openthread/openthread.git
PKG_VERSION=fbfd76a990b81f007957e1bd774e51bce742e53e PKG_VERSION=thread-reference-20170716
PKG_BUILDDIR ?= $(PKGDIRBASE)/$(PKG_NAME) PKG_BUILDDIR ?= $(PKGDIRBASE)/$(PKG_NAME)
ifneq (,$(filter openthread-cli-ftd,$(USEMODULE)))
$(info Compile OpenThread for FTD device) $(info Compile OpenThread for FTD device)
OPENTHREAD_ARGS+= --enable-cli-app=ftd --enable-application-coap OPENTHREAD_ARGS += --enable-cli-app=ftd
endif
$(info $$OPENTHREAD_ARGS is [$(OPENTHREAD_ARGS)]) 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 .PHONY: all
OPENTHREAD_COMMON_FLAGS = -fdata-sections -ffunction-sections -Os 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 all: git-download
cd $(PKG_BUILDDIR) && PREFIX="/" ./bootstrap cd $(PKG_BUILDDIR) && PREFIX="/" ./bootstrap
cd $(PKG_BUILDDIR) && CPP="$(CPP)" CC="$(CC)" CXX="$(CXX)"\ cd $(PKG_BUILDDIR) && CPP="$(CPP)" CC="$(CC)" CXX="$(CXX)"\
OBJC="" OBJCXX="" AR="$(AR)" RANLIB="$(RANLIB)" NM="$(NM)" \ OBJC="" OBJCXX="" AR="$(AR)" RANLIB="$(RANLIB)" NM="$(NM)" \
STRIP="$(STRIP)" \ STRIP="$(STRIP)" \
CPPFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) " \ CPPFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -D$(CONFIG_FILE)" \
CFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) " \ CFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) " \
CXXFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -fno-exceptions -fno-rtti " \ CXXFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -fno-exceptions -fno-rtti " \
LDFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -nostartfiles -specs=nano.specs \ LDFLAGS="$(OPENTHREAD_COMMON_FLAGS) $(CFLAGS_CPU) -nostartfiles -specs=nano.specs \
@ -27,9 +34,14 @@ all: git-download
--prefix=/ --enable-default-logging $(OPENTHREAD_ARGS) --prefix=/ --enable-default-logging $(OPENTHREAD_ARGS)
cd $(PKG_BUILDDIR) && DESTDIR=$(PKG_BUILDDIR)/output PREFIX=/ make -j4 --no-print-directory install 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/libmbedcrypto.a ${BINDIR}/mbedcrypto.a
cp $(PKG_BUILDDIR)/output/lib/libopenthread-ftd.a $(BINDIR)/libopenthread.a ifneq (,$(filter openthread-cli-ftd,$(USEMODULE)))
cp $(PKG_BUILDDIR)/output/lib/libopenthread-cli-ftd.a $(BINDIR)/libopenthread-cli.a cp $(PKG_BUILDDIR)/output/lib/libopenthread-ftd.a ${BINDIR}/openthread-ftd.a
sed -ie 's/BASE/_BASE/g' $(PKG_BUILDDIR)/output/include/openthread/types.h 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
include $(RIOTBASE)/pkg/pkg.mk include $(RIOTBASE)/pkg/pkg.mk

View File

@ -0,0 +1,10 @@
ifneq (,$(filter openthread,$(USEPKG)))
USEMODULE += openthread_contrib
USEMODULE += mbedcrypto
endif
ifneq (,$(filter openthread_contrib,$(USEMODULE)))
USEMODULE += openthread_contrib_netdev
USEMODULE += xtimer
FEATURES_REQUIRED += cpp
endif

View File

@ -1,8 +1,7 @@
OPENTHREAD_DIR = $(RIOTBASE)/pkg/openthread OPENTHREAD_DIR = $(RIOTBASE)/pkg/openthread
INCLUDES += -I$(OPENTHREAD_DIR)/include \ INCLUDES += -I$(OPENTHREAD_DIR)/include \
-I$(PKGDIRBASE)/openthread/output/include \ -I$(PKGDIRBASE)/openthread/include
-I$(PKGDIRBASE)/openthread/include/openthread \
ifneq (,$(filter openthread_contrib,$(USEMODULE))) ifneq (,$(filter openthread_contrib,$(USEMODULE)))
DIRS += $(OPENTHREAD_DIR)/contrib DIRS += $(OPENTHREAD_DIR)/contrib

42
pkg/openthread/contrib/netdev/openthread_netdev.c Normal file → Executable file
View File

@ -13,6 +13,7 @@
* @brief Netdev adoption for OpenThread * @brief Netdev adoption for OpenThread
* *
* @author Jose Ignacio Alamos <jialamos@uc.cl> * @author Jose Ignacio Alamos <jialamos@uc.cl>
* @author Baptiste Clenet <bapclenet@gmail.com>
* @} * @}
*/ */
@ -23,7 +24,7 @@
#include "openthread/cli.h" #include "openthread/cli.h"
#include "openthread/instance.h" #include "openthread/instance.h"
#include "openthread/ip6.h" #include "openthread/ip6.h"
#include "openthread/platform/alarm.h" #include "openthread/platform/alarm-milli.h"
#include "openthread/platform/uart.h" #include "openthread/platform/uart.h"
#include "openthread/tasklet.h" #include "openthread/tasklet.h"
#include "openthread/thread.h" #include "openthread/thread.h"
@ -39,18 +40,6 @@ static msg_t _queue[OPENTHREAD_QUEUE_LEN];
static kernel_pid_t _pid; static kernel_pid_t _pid;
static otInstance *sInstance; 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) { uint8_t ot_call_command(char* command, void *arg, void* answer) {
ot_job_t job; 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. */ /* OpenThread will call this when switching state from empty tasklet to non-empty tasklet. */
void otTaskletsSignalPending(otInstance *aInstance) { void otTaskletsSignalPending(otInstance *aInstance) {
otTaskletsProcess(aInstance); (void) aInstance;
} }
static void *_openthread_event_loop(void *arg) { static void *_openthread_event_loop(void *arg) {
@ -78,18 +67,14 @@ static void *_openthread_event_loop(void *arg) {
otPlatUartEnable(); otPlatUartEnable();
/* init OpenThread */ /* init OpenThread */
sInstance = otInstanceInit(); sInstance = otInstanceInitSingle();
msg_init_queue(_queue, OPENTHREAD_QUEUE_LEN); msg_init_queue(_queue, OPENTHREAD_QUEUE_LEN);
netdev_t *dev; netdev_t *dev;
msg_t msg, reply; msg_t msg, reply;
#if defined(MODULE_OPENTHREAD_CLI_FTD) || defined(MODULE_OPENTHREAD_CLI_MTD)
otCliUartInit(sInstance); otCliUartInit(sInstance);
#if OPENTHREAD_ENABLE_DIAG
diagInit(sInstance);
#endif
/* Init default parameters */ /* Init default parameters */
otPanId panid = OPENTHREAD_PANID; otPanId panid = OPENTHREAD_PANID;
uint8_t channel = OPENTHREAD_CHANNEL; uint8_t channel = OPENTHREAD_CHANNEL;
@ -99,15 +84,22 @@ static void *_openthread_event_loop(void *arg) {
otIp6SetEnabled(sInstance, true); otIp6SetEnabled(sInstance, true);
/* Start Thread protocol operation */ /* Start Thread protocol operation */
otThreadSetEnabled(sInstance, true); otThreadSetEnabled(sInstance, true);
#endif
#if OPENTHREAD_ENABLE_DIAG
diagInit(sInstance);
#endif
uint8_t *buf;
ot_job_t *job; ot_job_t *job;
serial_msg_t* serialBuffer;
while (1) { while (1) {
otTaskletsProcess(sInstance);
if (otTaskletsArePending(sInstance) == false) {
msg_receive(&msg); msg_receive(&msg);
switch (msg.type) { switch (msg.type) {
case OPENTHREAD_XTIMER_MSG_TYPE_EVENT: case OPENTHREAD_XTIMER_MSG_TYPE_EVENT:
/* Tell OpenThread a time event was received */ /* Tell OpenThread a time event was received */
otPlatAlarmFired(sInstance); otPlatAlarmMilliFired(sInstance);
break; break;
case OPENTHREAD_NETDEV_MSG_TYPE_EVENT: case OPENTHREAD_NETDEV_MSG_TYPE_EVENT:
/* Received an event from driver */ /* Received an event from driver */
@ -116,8 +108,9 @@ static void *_openthread_event_loop(void *arg) {
break; break;
case OPENTHREAD_SERIAL_MSG_TYPE_EVENT: case OPENTHREAD_SERIAL_MSG_TYPE_EVENT:
/* Tell OpenThread about the reception of a CLI command */ /* Tell OpenThread about the reception of a CLI command */
buf = msg.content.ptr; serialBuffer = (serial_msg_t*)msg.content.ptr;
otPlatUartReceived(buf, strlen((char *) buf)); otPlatUartReceived((uint8_t*) serialBuffer->buf,serialBuffer->length);
serialBuffer->serial_buffer_status = OPENTHREAD_SERIAL_BUFFER_STATUS_FREE;
break; break;
case OPENTHREAD_JOB_MSG_TYPE_EVENT: case OPENTHREAD_JOB_MSG_TYPE_EVENT:
job = msg.content.ptr; job = msg.content.ptr;
@ -126,6 +119,7 @@ static void *_openthread_event_loop(void *arg) {
break; break;
} }
} }
}
return NULL; return NULL;
} }

View File

@ -18,7 +18,7 @@
#include <assert.h> #include <assert.h>
#include "openthread/platform/alarm.h" #include "openthread/platform/alarm-milli.h"
#include "openthread/platform/uart.h" #include "openthread/platform/uart.h"
#include "ot.h" #include "ot.h"
#include "random.h" #include "random.h"
@ -41,29 +41,10 @@
static at86rf2xx_t at86rf2xx_dev; static at86rf2xx_t at86rf2xx_dev;
#endif #endif
#define OPENTHREAD_NETDEV_BUFLEN (ETHERNET_MAX_LEN)
static uint8_t rx_buf[OPENTHREAD_NETDEV_BUFLEN]; static uint8_t rx_buf[OPENTHREAD_NETDEV_BUFLEN];
static uint8_t tx_buf[OPENTHREAD_NETDEV_BUFLEN]; static uint8_t tx_buf[OPENTHREAD_NETDEV_BUFLEN];
static char ot_thread_stack[2 * THREAD_STACKSIZE_MAIN]; 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) void openthread_bootstrap(void)
{ {
/* init random */ /* init random */

View File

@ -19,7 +19,7 @@
#include <stdint.h> #include <stdint.h>
#include "msg.h" #include "msg.h"
#include "openthread/platform/alarm.h" #include "openthread/platform/alarm-milli.h"
#include "ot.h" #include "ot.h"
#include "thread.h" #include "thread.h"
#include "xtimer.h" #include "xtimer.h"
@ -38,7 +38,7 @@ static msg_t ot_alarm_msg;
* @param[in] aT0 The reference time. * @param[in] aT0 The reference time.
* @param[in] aDt The time delay in milliseconds from @p aT0. * @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)aInstance;
(void)aT0; (void)aT0;
@ -56,7 +56,7 @@ void otPlatAlarmStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt)
} }
/* OpenThread will call this to stop alarms */ /* OpenThread will call this to stop alarms */
void otPlatAlarmStop(otInstance *aInstance) void otPlatAlarmMilliStop(otInstance *aInstance)
{ {
(void)aInstance; (void)aInstance;
DEBUG("openthread: otPlatAlarmStop\n"); DEBUG("openthread: otPlatAlarmStop\n");
@ -64,7 +64,7 @@ void otPlatAlarmStop(otInstance *aInstance)
} }
/* OpenThread will call this for getting running time in millisecs */ /* 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; uint32_t now = xtimer_now_usec() / US_PER_MS;
DEBUG("openthread: otPlatAlarmGetNow: %" PRIu32 "\n", now); DEBUG("openthread: otPlatAlarmGetNow: %" PRIu32 "\n", now);

View File

@ -264,26 +264,23 @@ OT_COMMAND ot_state(otInstance* ot_instance, void* arg, void* answer) {
(void)arg; (void)arg;
if (answer != NULL) { if (answer != NULL) {
uint8_t state = otThreadGetDeviceRole(ot_instance); otDeviceRole state = otThreadGetDeviceRole(ot_instance);
*((uint8_t *) answer) = state; *((otDeviceRole *) answer) = state;
DEBUG("state: "); DEBUG("state: ");
switch (state) { switch (state) {
case kDeviceRoleOffline: case OT_DEVICE_ROLE_DISABLED:
puts("offline");
break;
case kDeviceRoleDisabled:
puts("disabled"); puts("disabled");
break; break;
case kDeviceRoleDetached: case OT_DEVICE_ROLE_DETACHED:
puts("detached"); puts("detached");
break; break;
case kDeviceRoleChild: case OT_DEVICE_ROLE_CHILD:
puts("child"); puts("child");
break; break;
case kDeviceRoleRouter: case OT_DEVICE_ROLE_ROUTER:
puts("router"); puts("router");
break; break;
case kDeviceRoleLeader: case OT_DEVICE_ROLE_LEADER:
puts("leader"); puts("leader");
break; break;
default: default:

View File

@ -13,6 +13,7 @@
* @brief Implementation of OpenThread logging platform abstraction * @brief Implementation of OpenThread logging platform abstraction
* *
* @author Jose Ignacio Alamos <jialamos@uc.cl> * @author Jose Ignacio Alamos <jialamos@uc.cl>
* @author Baptiste Clenet <bapclenet@gmail.com>
* @} * @}
*/ */
@ -20,10 +21,9 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "openthread/config.h"
#include "openthread/platform/logging.h" #include "openthread/platform/logging.h"
/* adapted from OpenThread posix example: /* adapted from OpenThread posix example:
@ -31,68 +31,11 @@
__attribute__((__format__ (__printf__, 3, 4))) __attribute__((__format__ (__printf__, 3, 4)))
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
{ {
(void) aLogLevel;
(void) aLogRegion;
va_list args; 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); va_start(args, aFormat);
vfprintf(stderr, aFormat, args); vfprintf(stderr, aFormat, args);
fprintf(stderr, "\r"); fprintf(stderr, "\n");
va_end(args); va_end(args);
} }

View File

@ -21,10 +21,13 @@
#include "openthread/platform/misc.h" #include "openthread/platform/misc.h"
#include "periph/pm.h" #include "periph/pm.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
void otPlatReset(otInstance *aInstance) void otPlatReset(otInstance *aInstance)
{ {
(void)aInstance; (void)aInstance;
printf("reboot...\n"); DEBUG("reboot...\n");
pm_reboot(); pm_reboot();
} }
@ -32,5 +35,10 @@ otPlatResetReason otPlatGetResetReason(otInstance *aInstance)
{ {
(void)aInstance; (void)aInstance;
/* TODO: Write me! */ /* 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. */
} }

135
pkg/openthread/contrib/platform_radio.c Normal file → Executable file
View File

@ -26,6 +26,9 @@
#include "net/ethertype.h" #include "net/ethertype.h"
#include "net/ieee802154.h" #include "net/ieee802154.h"
#include "net/netdev/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 "openthread/platform/radio.h"
#include "ot.h" #include "ot.h"
@ -34,14 +37,12 @@
#define RADIO_IEEE802154_FCS_LEN (2U) #define RADIO_IEEE802154_FCS_LEN (2U)
static RadioPacket sTransmitFrame; static otRadioFrame sTransmitFrame;
static RadioPacket sReceiveFrame; static otRadioFrame sReceiveFrame;
static int8_t Rssi; static int8_t Rssi;
static netdev_t *_dev; static netdev_t *_dev;
static bool sDisabled;
/* set 15.4 channel */ /* set 15.4 channel */
static int _set_channel(uint16_t 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)); 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 */ /* set IEEE802.15.4 PAN ID */
static int _set_panid(uint16_t panid) static int _set_panid(uint16_t panid)
{ {
@ -101,6 +109,11 @@ static netopt_state_t _get_state(void)
return state; return state;
} }
static void _set_off(void)
{
_set_state(NETOPT_STATE_OFF);
}
/* sets device state to SLEEP */ /* sets device state to SLEEP */
static void _set_sleep(void) static void _set_sleep(void)
{ {
@ -134,7 +147,7 @@ void recv_pkt(otInstance *aInstance, netdev_t *dev)
/* very unlikely */ /* very unlikely */
if ((len < 0) || ((uint32_t)len > UINT16_MAX)) { if ((len < 0) || ((uint32_t)len > UINT16_MAX)) {
DEBUG("Invalid len: %d\n", len); DEBUG("Invalid len: %d\n", len);
otPlatRadioReceiveDone(aInstance, NULL, kThreadError_Abort); otPlatRadioReceiveDone(aInstance, NULL, OT_ERROR_ABORT);
return; return;
} }
@ -148,16 +161,16 @@ void recv_pkt(otInstance *aInstance, netdev_t *dev)
/* Get RSSI from a radio driver. RSSI should be in [dBm] */ /* Get RSSI from a radio driver. RSSI should be in [dBm] */
Rssi = (int8_t)rx_info.rssi; Rssi = (int8_t)rx_info.rssi;
sReceiveFrame.mPower = Rssi; if (ENABLE_DEBUG) {
DEBUG("Received message: len %d\n", (int) sReceiveFrame.mLength); DEBUG("Received message: len %d\n", (int) sReceiveFrame.mLength);
for (int i = 0; i < sReceiveFrame.mLength; ++i) { for (int i = 0; i < sReceiveFrame.mLength; ++i) {
DEBUG("%x ", sReceiveFrame.mPsdu[i]); DEBUG("%x ", sReceiveFrame.mPsdu[i]);
} }
DEBUG("\n"); DEBUG("\n");
}
/* Tell OpenThread that receive has finished */ /* 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 */ /* Called upon TX event */
@ -169,19 +182,19 @@ void send_pkt(otInstance *aInstance, netdev_t *dev, netdev_event_t event)
switch (event) { switch (event) {
case NETDEV_EVENT_TX_COMPLETE: case NETDEV_EVENT_TX_COMPLETE:
DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE\n"); DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE\n");
otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, kThreadError_None); otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_NONE);
break; break;
case NETDEV_EVENT_TX_COMPLETE_DATA_PENDING: case NETDEV_EVENT_TX_COMPLETE_DATA_PENDING:
DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE_DATA_PENDING\n"); DEBUG("openthread: NETDEV_EVENT_TX_COMPLETE_DATA_PENDING\n");
otPlatRadioTransmitDone(aInstance, &sTransmitFrame, true, kThreadError_None); otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_NONE);
break; break;
case NETDEV_EVENT_TX_NOACK: case NETDEV_EVENT_TX_NOACK:
DEBUG("openthread: NETDEV_EVENT_TX_NOACK\n"); DEBUG("openthread: NETDEV_EVENT_TX_NOACK\n");
otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, kThreadError_NoAck); otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_NO_ACK);
break; break;
case NETDEV_EVENT_TX_MEDIUM_BUSY: case NETDEV_EVENT_TX_MEDIUM_BUSY:
DEBUG("openthread: NETDEV_EVENT_TX_MEDIUM_BUSY\n"); DEBUG("openthread: NETDEV_EVENT_TX_MEDIUM_BUSY\n");
otPlatRadioTransmitDone(aInstance, &sTransmitFrame, false, kThreadError_ChannelAccessFailure); otPlatRadioTxDone(aInstance, &sTransmitFrame, NULL, OT_ERROR_CHANNEL_ACCESS_FAILURE);
break; break;
default: default:
break; break;
@ -201,11 +214,17 @@ void otPlatRadioSetExtendedAddress(otInstance *aInstance, uint8_t *aExtendedAddr
{ {
(void)aInstance; (void)aInstance;
DEBUG("openthread: otPlatRadioSetExtendedAddress\n"); 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++) { 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];
} }
_set_long_addr(reversed_addr); 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((uint8_t*) reversed_addr);
} }
/* OpenThread will call this for setting short address */ /* OpenThread will call this for setting short address */
@ -217,31 +236,29 @@ void otPlatRadioSetShortAddress(otInstance *aInstance, uint16_t aShortAddress)
} }
/* OpenThread will call this for enabling the radio */ /* OpenThread will call this for enabling the radio */
ThreadError otPlatRadioEnable(otInstance *aInstance) otError otPlatRadioEnable(otInstance *aInstance)
{ {
DEBUG("openthread: otPlatRadioEnable\n"); DEBUG("openthread: otPlatRadioEnable\n");
(void)aInstance; (void)aInstance;
if (sDisabled) { if (!otPlatRadioIsEnabled(aInstance)) {
sDisabled = false; _set_sleep();
_set_idle();
} }
return kThreadError_None; return OT_ERROR_NONE;
} }
/* OpenThread will call this for disabling the radio */ /* OpenThread will call this for disabling the radio */
ThreadError otPlatRadioDisable(otInstance *aInstance) otError otPlatRadioDisable(otInstance *aInstance)
{ {
DEBUG("openthread: otPlatRadioDisable\n"); DEBUG("openthread: otPlatRadioDisable\n");
(void)aInstance; (void)aInstance;
if (!sDisabled) { if (otPlatRadioIsEnabled(aInstance)) {
sDisabled = true; _set_off();
_set_sleep();
} }
return kThreadError_None; return OT_ERROR_NONE;
} }
bool otPlatRadioIsEnabled(otInstance *aInstance) bool otPlatRadioIsEnabled(otInstance *aInstance)
@ -249,7 +266,7 @@ bool otPlatRadioIsEnabled(otInstance *aInstance)
DEBUG("otPlatRadioIsEnabled\n"); DEBUG("otPlatRadioIsEnabled\n");
(void)aInstance; (void)aInstance;
netopt_state_t state = _get_state(); netopt_state_t state = _get_state();
if (state == NETOPT_STATE_OFF || state == NETOPT_STATE_SLEEP) { if (state == NETOPT_STATE_OFF) {
return false; return false;
} else { } else {
return true; return true;
@ -257,28 +274,29 @@ bool otPlatRadioIsEnabled(otInstance *aInstance)
} }
/* OpenThread will call this for setting device state to SLEEP */ /* OpenThread will call this for setting device state to SLEEP */
ThreadError otPlatRadioSleep(otInstance *aInstance) otError otPlatRadioSleep(otInstance *aInstance)
{ {
DEBUG("otPlatRadioSleep\n"); DEBUG("otPlatRadioSleep\n");
(void)aInstance; (void)aInstance;
_set_sleep(); _set_sleep();
return kThreadError_None; return OT_ERROR_NONE;
} }
/*OpenThread will call this for waiting the reception of a packet */ /*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); DEBUG("openthread: otPlatRadioReceive. Channel: %i\n", aChannel);
(void)aInstance; (void)aInstance;
_set_idle(); _set_idle();
_set_channel(aChannel); _set_channel(aChannel);
return kThreadError_None; sReceiveFrame.mChannel = aChannel;
return OT_ERROR_NONE;
} }
/* OpenThread will call this function to get the transmit buffer */ /* 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"); DEBUG("openthread: otPlatRadioGetTransmitBuffer\n");
@ -293,8 +311,28 @@ void otPlatRadioSetDefaultTxPower(otInstance *aInstance, int8_t aPower)
_set_power(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*/ /* OpenThread will call this for transmitting a packet*/
ThreadError otPlatRadioTransmit(otInstance *aInstance, RadioPacket *aPacket) otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket)
{ {
(void) aInstance; (void) aInstance;
@ -308,18 +346,21 @@ ThreadError otPlatRadioTransmit(otInstance *aInstance, RadioPacket *aPacket)
}; };
/*Set channel and power based on transmit frame */ /*Set channel and power based on transmit frame */
DEBUG("otPlatRadioTransmit->channel: %i, length %d\n", (int) aPacket->mChannel, (int)aPacket->mLength); if (ENABLE_DEBUG) {
for (int i = 0; i < aPacket->mLength; ++i) { 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("%x ", aPacket->mPsdu[i]);
} }
DEBUG("\n"); DEBUG("\n");
}
_set_channel(aPacket->mChannel); _set_channel(aPacket->mChannel);
_set_power(aPacket->mPower);
/* send packet though netdev */ /* send packet though netdev */
_dev->driver->send(_dev, &iolist); _dev->driver->send(_dev, &iolist);
otPlatRadioTxStarted(aInstance, aPacket);
return kThreadError_None; return OT_ERROR_NONE;
} }
/* OpenThread will call this for getting the radio caps */ /* OpenThread will call this for getting the radio caps */
@ -328,7 +369,7 @@ otRadioCaps otPlatRadioGetCaps(otInstance *aInstance)
(void)aInstance; (void)aInstance;
DEBUG("openthread: otPlatRadioGetCaps\n"); DEBUG("openthread: otPlatRadioGetCaps\n");
/* all drivers should handle ACK, including call of NETDEV_EVENT_TX_NOACK */ /* 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 */ /* OpenThread will call this for getting the state of promiscuous mode */
@ -361,36 +402,36 @@ void otPlatRadioEnableSrcMatch(otInstance *aInstance, bool aEnable)
(void)aEnable; (void)aEnable;
} }
ThreadError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress) otError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, const uint16_t aShortAddress)
{ {
DEBUG("otPlatRadioAddSrcMatchShortEntry\n"); DEBUG("otPlatRadioAddSrcMatchShortEntry\n");
(void)aInstance; (void)aInstance;
(void)aShortAddress; (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"); DEBUG("otPlatRadioAddSrcMatchExtEntry\n");
(void)aInstance; (void)aInstance;
(void)aExtAddress; (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"); DEBUG("otPlatRadioClearSrcMatchShortEntry\n");
(void)aInstance; (void)aInstance;
(void)aShortAddress; (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"); DEBUG("otPlatRadioClearSrcMatchExtEntry\n");
(void)aInstance; (void)aInstance;
(void)aExtAddress; (void)aExtAddress;
return kThreadError_None; return OT_ERROR_NONE;
} }
void otPlatRadioClearSrcMatchShortEntries(otInstance *aInstance) void otPlatRadioClearSrcMatchShortEntries(otInstance *aInstance)
@ -405,13 +446,13 @@ void otPlatRadioClearSrcMatchExtEntries(otInstance *aInstance)
(void)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"); DEBUG("otPlatRadioEnergyScan\n");
(void)aInstance; (void)aInstance;
(void)aScanChannel; (void)aScanChannel;
(void)aScanDuration; (void)aScanDuration;
return kThreadError_NotImplemented; return OT_ERROR_NOT_IMPLEMENTED;
} }
void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeee64Eui64) void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeee64Eui64)

View File

@ -49,3 +49,13 @@ uint32_t otPlatRandomGet(void)
DEBUG("otPlatRandomGet: %i\n", (int) rand_val); DEBUG("otPlatRandomGet: %i\n", (int) rand_val);
return 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;
}

View File

@ -27,26 +27,26 @@ void otPlatSettingsInit(otInstance *aInstance)
(void)aInstance; (void)aInstance;
} }
ThreadError otPlatSettingsBeginChange(otInstance *aInstance) otError otPlatSettingsBeginChange(otInstance *aInstance)
{ {
(void)aInstance; (void)aInstance;
return kThreadError_None; return OT_ERROR_NONE;
} }
ThreadError otPlatSettingsCommitChange(otInstance *aInstance) otError otPlatSettingsCommitChange(otInstance *aInstance)
{ {
DEBUG("openthread: otPlatSettingsCommitChange\n"); DEBUG("openthread: otPlatSettingsCommitChange\n");
(void)aInstance; (void)aInstance;
return kThreadError_None; return OT_ERROR_NONE;
} }
ThreadError otPlatSettingsAbandonChange(otInstance *aInstance) otError otPlatSettingsAbandonChange(otInstance *aInstance)
{ {
(void)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)aInstance;
(void)aKey; (void)aKey;
@ -55,36 +55,33 @@ ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex,
DEBUG("openthread: otPlatSettingsGet\n"); DEBUG("openthread: otPlatSettingsGet\n");
*aValueLength = 0; *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)aInstance;
(void)aKey; (void)aKey;
(void)aValue; (void)aValue;
(void)aValueLength; (void)aValueLength;
return OT_ERROR_NONE;
return kThreadError_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)aInstance;
(void)aKey; (void)aKey;
(void)aValue; (void)aValue;
(void)aValueLength; (void)aValueLength;
return OT_ERROR_NONE;
return kThreadError_None;
} }
ThreadError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex)
{ {
(void)aInstance; (void)aInstance;
(void)aKey; (void)aKey;
(void)aIndex; (void)aIndex;
return OT_ERROR_NONE;
return kThreadError_None;
} }
void otPlatSettingsWipe(otInstance *aInstance) void otPlatSettingsWipe(otInstance *aInstance)

62
pkg/openthread/contrib/platform_uart.c Normal file → Executable file
View File

@ -13,34 +13,82 @@
* @brief Implementation of OpenThread UART platform abstraction * @brief Implementation of OpenThread UART platform abstraction
* *
* @author Jose Ignacio Alamos <jialamos@uc.cl> * @author Jose Ignacio Alamos <jialamos@uc.cl>
* @author Baptiste Clenet <bapclenet@gmail.com>
* @} * @}
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "stdio_uart.h"
#include "periph/uart.h" #include "periph/uart.h"
#include "openthread/types.h"
#include "openthread/platform/uart.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)*/ /* 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(STDIO_UART_DEV, STDIO_UART_BAUDRATE, (uart_rx_cb_t) uart_handler, NULL);
return OT_ERROR_NONE;
} }
/* OpenThread will call this for disabling UART */ /* OpenThread will call this for disabling UART */
ThreadError otPlatUartDisable(void) otError otPlatUartDisable(void)
{ {
return kThreadError_None; uart_poweroff(STDIO_UART_DEV);
return OT_ERROR_NONE;
} }
/* OpenThread will call this for sending data through UART */ /* 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(STDIO_UART_DEV, aBuf, aBufLength);
/* Tell OpenThread the sending of UART is done */ /* Tell OpenThread the sending of UART is done */
otPlatUartSendDone(); otPlatUartSendDone();
return kThreadError_None; return OT_ERROR_NONE;
} }

59
pkg/openthread/include/ot.h Normal file → Executable file
View File

@ -17,7 +17,8 @@
* *
* @file * @file
* *
* @author José Ignacio Alamos <jialamos@uc.cl> * @author Jose Ignacio Alamos <jialamos@uc.cl>
* @author Baptiste Clenet <bapclenet@gmail.com>
*/ */
#ifndef OT_H #ifndef OT_H
@ -34,18 +35,55 @@ extern "C" {
#include "thread.h" #include "thread.h"
#include "openthread/types.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 */ * @name Openthread message types
#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 */ /** @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 * @brief Struct containing a serial message
*/ */
typedef struct { typedef struct {
void *buf; /**< buffer containing the message */ uint16_t length; /**< length of the message */
size_t len; /**< 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; } serial_msg_t;
/** /**
@ -115,11 +153,6 @@ kernel_pid_t openthread_get_pid(void);
*/ */
void ot_random_init(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 * @brief Execute OpenThread command. Call this function only in OpenThread thread
* *

View File

@ -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 */

View File

@ -0,0 +1,27 @@
From 0da72cf690b0d0f08fbd7df07b03a9b45e0e50a3 Mon Sep 17 00:00:00 2001
From: Jose Alamos <jose.alamos@haw-hamburg.de>
Date: Fri, 5 Oct 2018 15:56:03 +0200
Subject: [PATCH] fix macro conflict
---
include/openthread/types.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/openthread/types.h b/include/openthread/types.h
index f84a7d5e..87afe7eb 100644
--- a/include/openthread/types.h
+++ b/include/openthread/types.h
@@ -61,8 +61,8 @@ extern "C" {
#define CONTAINING_RECORD(address, type, field) \
((type *)((uint8_t*)(address) - offsetof(type, field)))
#pragma GCC diagnostic pop*/
-#define BASE 0x1
-#define myoffsetof(s,m) (((size_t)&(((s*)BASE)->m))-BASE)
+#define _BASE 0x1
+#define myoffsetof(s,m) (((size_t)&(((s*)_BASE)->m))-_BASE)
#define CONTAINING_RECORD(address, type, field) \
((type *)((uint8_t*)(address) - myoffsetof(type, field)))
#endif /* CONTAINING_RECORD */
--
2.18.0

View File

@ -1,43 +0,0 @@
APPLICATION = openthread
# If no BOARD is found in the environment, use this default:
BOARD ?= samr21-xpro
BOARD_WHITELIST := samr21-xpro iotlab-m3 fox iotlab-a8-m3
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..
# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1
USEPKG += openthread
USEMODULE += openthread_contrib
USEMODULE += libmbedcrypto
USEMODULE += libopenthread
USEMODULE += libopenthread-cli
USEMODULE += xtimer
ifneq (,$(filter samr21-xpro,$(BOARD)))
DRIVER := at86rf233
endif
ifneq (,$(filter iotlab-m3 fox iotlab-a8-m3,$(BOARD)))
DRIVER := at86rf231
endif
USEMODULE += $(DRIVER)
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

View File

@ -1,36 +0,0 @@
## 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.
## Quick usage
To test OpenThread on RIOT, you can do the following:
1. Flash nodes with `make BOARD=<target> 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`.
```
ipaddr
fdde:ad00:beef::ff:fe00:8000
fe80::ff:fe00:8000
fdde:ad00:beef:0:946a:c722:a5d9:8481
fe80::3984:f4eb:d182:5dae
```
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
See the [OpenThread CLI Reference](https://github.com/openthread/openthread/blob/master/src/cli/README.md) for more information about OpenThread CLI commands