Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: openthread: add pskd join loop #25589

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/net/openthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ k_tid_t openthread_thread_id_get(void);

struct otInstance *openthread_get_default_instance(void);

void openthread_get_pskd(const char **pskd);

#define OPENTHREAD_L2_CTX_TYPE struct openthread_context

#ifdef __cplusplus
Expand Down
26 changes: 26 additions & 0 deletions subsys/net/l2/openthread/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,32 @@ config OPENTHREAD_JOINER_PSKD
help
Pre Shared Key for the Device to start joiner

config OPENTHREAD_JOINER_CONTINUOUS
bool "Retries joining till it is successful"
depends on OPENTHREAD_JOINER
help
Retries joining till it is successful.

config OPENTHREAD_JOINER_ADD_DELAY_MS
int "Added joining delay [ms] till next try"
default 0
help
Added joining delay [ms] till next try.

config OPENTHREAD_JOINER_TRIALS_MAX_DELAY
int "Number of joining trials till the maximum delay is reached"
default 0
help
Number of joining trials till the maximum delay is reached. If
it is configured with 0, there is no maxmium delay.

config OPENTHREAD_JOINER_REBOOT
int "Reboots the device after a specified number of failed joining trials"
default 0
help
Reboots the device after a specified number of failed joining trials. If
it is configured with 0, it never reboots.

config OPENTHREAD_DHCP6_CLIENT
bool "DHCPv6 client support"
help
Expand Down
74 changes: 71 additions & 3 deletions subsys/net/l2/openthread/openthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <logging/log.h>
#include <logging/log_ctrl.h>
LOG_MODULE_REGISTER(net_l2_openthread, CONFIG_OPENTHREAD_L2_LOG_LEVEL);

#include <net/net_core.h>
Expand Down Expand Up @@ -101,6 +102,16 @@ static struct net_linkaddr *ll_addr;

static struct net_mgmt_event_callback ip6_addr_cb;

/* for rebooting after a period if join not successful */
#include <power/reboot.h>

static const char *joiner_pskd = OT_JOINER_PSKD;

__weak void openthread_get_pskd(const char **pskd)
{
*pskd = joiner_pskd;
}

k_tid_t openthread_thread_id_get(void)
{
return ot_tid;
Expand Down Expand Up @@ -237,17 +248,74 @@ void ot_receive_handler(otMessage *aMessage, void *context)
otMessageFree(aMessage);
}

static struct openthread_context *ot_context_for_restarter;

void ot_joiner_start_handler(otError error, void *context);

static void ot_joiner_restarter(struct k_work *dummy)
{
otError error = otJoinerStart(ot_context_for_restarter->instance,
joiner_pskd, NULL,
PACKAGE_NAME, OT_PLATFORM_INFO,
PACKAGE_VERSION, NULL,
&ot_joiner_start_handler,
ot_context_for_restarter);

if (error != OT_ERROR_NONE) {
NET_ERR("Failed to start joiner [%d]", error);
}
}

K_WORK_DEFINE(joiner_restart, ot_joiner_restarter);

static void ot_joiner_restart_timer(struct k_timer *dummy)
{
k_work_submit(&joiner_restart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simply use k_delayed_work_submit instead of timer + work combo.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have tried that but that did not work with our zephyr devices.

}

K_TIMER_DEFINE(joiner_restart_timer, ot_joiner_restart_timer, NULL);

void ot_joiner_start_handler(otError error, void *context)
{
struct openthread_context *ot_context = context;

static uint32_t restart_delay;
static uint32_t join_attempts;
switch (error) {
case OT_ERROR_NONE:
NET_INFO("Join success");
otThreadSetEnabled(ot_context->instance, true);
break;
default:
NET_ERR("Join failed [%d]", error);
if (IS_ENABLED(CONFIG_OPENTHREAD_JOINER_CONTINUOUS)) {
join_attempts++;

if (CONFIG_OPENTHREAD_JOINER_REBOOT != 0 &&
join_attempts >= CONFIG_OPENTHREAD_JOINER_REBOOT) {
log_panic();
NET_WARN("Joining failed %d times..."
"REBOOTING now!\n\n",
join_attempts);
sys_reboot(SYS_REBOOT_COLD);
}

if (CONFIG_OPENTHREAD_JOINER_TRIALS_MAX_DELAY != 0) {
if (join_attempts <=
CONFIG_OPENTHREAD_JOINER_TRIALS_MAX_DELAY) {
restart_delay +=
CONFIG_OPENTHREAD_JOINER_ADD_DELAY_MS;
}
} else {
restart_delay +=
CONFIG_OPENTHREAD_JOINER_ADD_DELAY_MS;
}

NET_WARN("Restarting joiner after %d seconds",
restart_delay/1000);

ot_context_for_restarter = context;
k_timer_start(&joiner_restart_timer, K_MSEC(restart_delay), K_NO_WAIT);
}
break;
}
}
Expand All @@ -262,7 +330,6 @@ static void openthread_process(void *context, void *arg2, void *arg3)
}

otSysProcessDrivers(ot_context->instance);

k_sem_take(&ot_sem, K_FOREVER);
Philipp-Wohlgenannt-Tridonic-com marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down Expand Up @@ -371,7 +438,8 @@ static void openthread_start(struct openthread_context *ot_context)
/* No dataset - initiate network join procedure. */
NET_DBG("Starting OpenThread join procedure.");

error = otJoinerStart(ot_instance, OT_JOINER_PSKD, NULL,
openthread_get_pskd(&joiner_pskd);
error = otJoinerStart(ot_instance, joiner_pskd, NULL,
PACKAGE_NAME, OT_PLATFORM_INFO,
PACKAGE_VERSION, NULL,
&ot_joiner_start_handler, ot_context);
Expand Down