From 1981eef6748bbfcfe7eaa338cb8516609c241df0 Mon Sep 17 00:00:00 2001 From: Takumi Ando Date: Fri, 1 Mar 2019 17:39:43 +0900 Subject: [PATCH] net/l2: openthread: Add support for automatic joiner start We sometimes want to join a device to OpenThread mesh automatically. This commit adds supports to do by Kconfig. The default of CONFIG_OPENTHREAD_JOINER_PSKD is based on this page: https://codelabs.developers.google.com/codelabs/openthread-hardware/ Signed-off-by: Takumi Ando --- subsys/net/l2/openthread/Kconfig | 19 ++++++++++ subsys/net/l2/openthread/openthread.c | 51 ++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/subsys/net/l2/openthread/Kconfig b/subsys/net/l2/openthread/Kconfig index 8a27ef72eb4..7ed855fa255 100644 --- a/subsys/net/l2/openthread/Kconfig +++ b/subsys/net/l2/openthread/Kconfig @@ -150,6 +150,25 @@ config OPENTHREAD_JOINER help Enable joiner capability in OpenThread stack +config OPENTHREAD_JOINER_AUTOSTART + bool "Support for automatic joiner start" + depends on OPENTHREAD_JOINER + help + Enable automatic joiner start + +config OPENTHREAD_JOINER_PSKD + string "Default Pre Shared key for the Device to start joiner" + depends on OPENTHREAD_JOINER_AUTOSTART + default "J01NME" + help + Pre Shared Key for the Device to start joiner + +config OPENTHREAD_PLATFORM_INFO + string "Platform information for OpenThread" + default "ZEPHYR" + help + Platform information for OpenThread + config OPENTHREAD_JAM_DETECTION bool "Jam detection support" help diff --git a/subsys/net/l2/openthread/openthread.c b/subsys/net/l2/openthread/openthread.c index ed15caf0697..98e1e1c9258 100644 --- a/subsys/net/l2/openthread/openthread.c +++ b/subsys/net/l2/openthread/openthread.c @@ -24,7 +24,10 @@ LOG_MODULE_REGISTER(net_l2_openthread, CONFIG_OPENTHREAD_L2_LOG_LEVEL); #include #include #include +#include +#include #include +#include #include @@ -166,6 +169,21 @@ out: otMessageFree(aMessage); } +void ot_joiner_start_handler(otError error, void *context) +{ + struct openthread_context *ot_context = context; + + switch (error) { + case OT_ERROR_NONE: + NET_INFO("Join success"); + otThreadSetEnabled(ot_context->instance, true); + break; + default: + NET_ERR("Join failed [%d]", error); + break; + } +} + static void openthread_process(void *context, void *arg2, void *arg3) { struct openthread_context *ot_context = context; @@ -279,6 +297,28 @@ enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface, return NET_CONTINUE; } +#if defined(CONFIG_OPENTHREAD_JOINER_AUTOSTART) +void openthread_joiner_autostart(struct net_if *iface) +{ + struct openthread_context *ot_context = net_if_l2_data(iface); + otError error; + + if (!otDatasetIsCommissioned(ot_context->instance)) { + error = otJoinerStart(ot_context->instance, + CONFIG_OPENTHREAD_JOINER_PSKD, NULL, + PACKAGE_NAME, CONFIG_OPENTHREAD_PLATFORM_INFO, + PACKAGE_VERSION, NULL, + &ot_joiner_start_handler, ot_context); + + if (error != OT_ERROR_NONE) { + NET_ERR("Failed to start joiner [%d]", error); + } + } else { + otThreadSetEnabled(ot_context->instance, true); + } +} +#endif + static int openthread_init(struct net_if *iface) { struct openthread_context *ot_context = net_if_l2_data(iface); @@ -302,6 +342,7 @@ static int openthread_init(struct net_if *iface) NET_INFO("OpenThread version: %s", otGetVersionString()); +#if !defined(CONFIG_OPENTHREAD_JOINER_AUTOSTART) otThreadSetNetworkName(ot_context->instance, CONFIG_OPENTHREAD_NETWORK_NAME); NET_INFO("Network name: %s", log_strdup(otThreadGetNetworkName(ot_context->instance))); @@ -309,8 +350,10 @@ static int openthread_init(struct net_if *iface) otLinkSetChannel(ot_context->instance, CONFIG_OPENTHREAD_CHANNEL); otLinkSetPanId(ot_context->instance, CONFIG_OPENTHREAD_PANID); otThreadSetExtendedPanId(ot_context->instance, &xpanid); +#endif + otIp6SetEnabled(ot_context->instance, true); - otThreadSetEnabled(ot_context->instance, true); + otIp6SetReceiveFilterEnabled(ot_context->instance, true); otIp6SetReceiveCallback(ot_context->instance, ot_receive_handler, ot_context); @@ -331,6 +374,12 @@ static int openthread_init(struct net_if *iface) OT_PRIORITY, 0, K_NO_WAIT); k_thread_name_set(&ot_thread_data, "openthread"); +#if !defined(CONFIG_OPENTHREAD_JOINER_AUTOSTART) + otThreadSetEnabled(ot_context->instance, true); +#else + openthread_joiner_autostart(iface); +#endif + return 0; }