Skip to content

Commit 51a9e6f

Browse files
rlubosjukkar
authored andcommitted
net: openthread: Do not overwrite stored dataset with defaults
This commit prevents a situation when stored and possibly modified commissioner dataset is overwritten with default configuration during OpenThread initialization. It introduces a new function, openthread_start, which verifies if the dataset is already stored, and if not, depending on configuration, preloads the default configuration or initiates the join procedure. Signed-off-by: Robert Lubos <[email protected]>
1 parent ee442c2 commit 51a9e6f

File tree

1 file changed

+55
-31
lines changed

1 file changed

+55
-31
lines changed

subsys/net/l2/openthread/openthread.c

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ LOG_MODULE_REGISTER(net_l2_openthread, CONFIG_OPENTHREAD_L2_LOG_LEVEL);
3636
#define OT_STACK_SIZE (CONFIG_OPENTHREAD_THREAD_STACK_SIZE)
3737
#define OT_PRIORITY K_PRIO_COOP(CONFIG_OPENTHREAD_THREAD_PRIORITY)
3838

39+
#define OT_NETWORK_NAME CONFIG_OPENTHREAD_NETWORK_NAME
40+
#define OT_CHANNEL CONFIG_OPENTHREAD_CHANNEL
41+
#define OT_PANID CONFIG_OPENTHREAD_PANID
42+
#define OT_XPANID CONFIG_OPENTHREAD_XPANID
43+
44+
#if defined(CONFIG_OPENTHREAD_JOINER_PSKD)
45+
#define OT_JOINER_PSKD CONFIG_OPENTHREAD_JOINER_PSKD
46+
#else
47+
#define OT_JOINER_PSKD ""
48+
#endif
49+
50+
#if defined(CONFIG_OPENTHREAD_PLATFORM_INFO)
51+
#define OT_PLATFORM_INFO CONFIG_OPENTHREAD_PLATFORM_INFO
52+
#else
53+
#define OT_PLATFORM_INFO ""
54+
#endif
55+
3956
extern void platformShellInit(otInstance *aInstance);
4057

4158
K_SEM_DEFINE(ot_sem, 0, 1);
@@ -297,34 +314,57 @@ enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface,
297314
return NET_CONTINUE;
298315
}
299316

300-
#if defined(CONFIG_OPENTHREAD_JOINER_AUTOSTART)
301-
void openthread_joiner_autostart(struct net_if *iface)
317+
static void openthread_start(struct openthread_context *ot_context)
302318
{
303-
struct openthread_context *ot_context = net_if_l2_data(iface);
319+
otInstance *ot_instance = ot_context->instance;
304320
otError error;
305321

306-
if (!otDatasetIsCommissioned(ot_context->instance)) {
307-
error = otJoinerStart(ot_context->instance,
308-
CONFIG_OPENTHREAD_JOINER_PSKD, NULL,
309-
PACKAGE_NAME, CONFIG_OPENTHREAD_PLATFORM_INFO,
310-
PACKAGE_VERSION, NULL,
311-
&ot_joiner_start_handler, ot_context);
322+
if (otDatasetIsCommissioned(ot_instance)) {
323+
/* OpenThread already has dataset stored - skip the
324+
* configuration.
325+
*/
326+
NET_DBG("OpenThread already commissioned.");
327+
} else if (IS_ENABLED(CONFIG_OPENTHREAD_JOINER_AUTOSTART)) {
328+
/* No dataset - initiate network join procedure. */
329+
NET_DBG("Starting OpenThread join procedure.");
330+
331+
error = otJoinerStart(ot_instance, OT_JOINER_PSKD, NULL,
332+
PACKAGE_NAME, OT_PLATFORM_INFO,
333+
PACKAGE_VERSION, NULL,
334+
&ot_joiner_start_handler, ot_context);
312335

313336
if (error != OT_ERROR_NONE) {
314337
NET_ERR("Failed to start joiner [%d]", error);
315338
}
339+
340+
return;
316341
} else {
317-
otThreadSetEnabled(ot_context->instance, true);
342+
/* No dataset - load the default configuration. */
343+
NET_DBG("Loading OpenThread default configuration.");
344+
345+
otExtendedPanId xpanid;
346+
347+
otThreadSetNetworkName(ot_instance, OT_NETWORK_NAME);
348+
otLinkSetChannel(ot_instance, OT_CHANNEL);
349+
otLinkSetPanId(ot_instance, OT_PANID);
350+
net_bytes_from_str(xpanid.m8, 8, (char *)OT_XPANID);
351+
otThreadSetExtendedPanId(ot_instance, &xpanid);
352+
}
353+
354+
NET_INFO("OpenThread version: %s", otGetVersionString());
355+
NET_INFO("Network name: %s",
356+
log_strdup(otThreadGetNetworkName(ot_instance)));
357+
358+
/* Start the network. */
359+
error = otThreadSetEnabled(ot_instance, true);
360+
if (error != OT_ERROR_NONE) {
361+
NET_ERR("Failed to start the OpenThread network [%d]", error);
318362
}
319363
}
320-
#endif
321364

322365
static int openthread_init(struct net_if *iface)
323366
{
324367
struct openthread_context *ot_context = net_if_l2_data(iface);
325-
otExtendedPanId xpanid;
326-
327-
net_bytes_from_str(xpanid.m8, 8, (char *)CONFIG_OPENTHREAD_XPANID);
328368

329369
NET_DBG("openthread_init");
330370

@@ -339,18 +379,6 @@ static int openthread_init(struct net_if *iface)
339379
platformShellInit(ot_context->instance);
340380
#endif
341381

342-
NET_INFO("OpenThread version: %s",
343-
otGetVersionString());
344-
345-
#if !defined(CONFIG_OPENTHREAD_JOINER_AUTOSTART)
346-
otThreadSetNetworkName(ot_context->instance, CONFIG_OPENTHREAD_NETWORK_NAME);
347-
NET_INFO("Network name: %s",
348-
log_strdup(otThreadGetNetworkName(ot_context->instance)));
349-
350-
otLinkSetChannel(ot_context->instance, CONFIG_OPENTHREAD_CHANNEL);
351-
otLinkSetPanId(ot_context->instance, CONFIG_OPENTHREAD_PANID);
352-
otThreadSetExtendedPanId(ot_context->instance, &xpanid);
353-
#endif
354382

355383
otIp6SetEnabled(ot_context->instance, true);
356384

@@ -374,11 +402,7 @@ static int openthread_init(struct net_if *iface)
374402
OT_PRIORITY, 0, K_NO_WAIT);
375403
k_thread_name_set(&ot_thread_data, "openthread");
376404

377-
#if !defined(CONFIG_OPENTHREAD_JOINER_AUTOSTART)
378-
otThreadSetEnabled(ot_context->instance, true);
379-
#else
380-
openthread_joiner_autostart(iface);
381-
#endif
405+
openthread_start(ot_context);
382406

383407
return 0;
384408
}

0 commit comments

Comments
 (0)