Skip to content

Commit 3449e22

Browse files
rluboskartben
authored andcommitted
net: openthread: Add missing error checks
Some OpenThread functions were called without verifying the return value, which not only is not the best practice, but also could lead to build warnings with llvm. This commit fixes it. Signed-off-by: Robert Lubos <[email protected]>
1 parent c6fe84c commit 3449e22

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

subsys/net/l2/openthread/openthread.c

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,11 @@ static void ot_joiner_start_handler(otError error, void *context)
338338
switch (error) {
339339
case OT_ERROR_NONE:
340340
NET_INFO("Join success");
341-
otThreadSetEnabled(ot_context->instance, true);
341+
error = otThreadSetEnabled(ot_context->instance, true);
342+
if (error != OT_ERROR_NONE) {
343+
NET_ERR("Failed to start the OpenThread network [%d]", error);
344+
}
345+
342346
break;
343347
default:
344348
NET_ERR("Join failed [%d]", error);
@@ -439,7 +443,11 @@ int openthread_start(struct openthread_context *ot_context)
439443
goto exit;
440444
}
441445

442-
otIp6SetEnabled(ot_context->instance, true);
446+
error = otIp6SetEnabled(ot_context->instance, true);
447+
if (error != OT_ERROR_NONE) {
448+
NET_ERR("Failed to set %s [%d]", "IPv6 support", error);
449+
goto exit;
450+
}
443451

444452
/* Sleepy End Device specific configuration. */
445453
if (IS_ENABLED(CONFIG_OPENTHREAD_MTD_SED)) {
@@ -450,8 +458,17 @@ int openthread_start(struct openthread_context *ot_context)
450458
*/
451459
ot_mode.mRxOnWhenIdle = false;
452460

453-
otThreadSetLinkMode(ot_context->instance, ot_mode);
454-
otLinkSetPollPeriod(ot_context->instance, OT_POLL_PERIOD);
461+
error = otThreadSetLinkMode(ot_context->instance, ot_mode);
462+
if (error != OT_ERROR_NONE) {
463+
NET_ERR("Failed to set %s [%d]", "link mode", error);
464+
goto exit;
465+
}
466+
467+
error = otLinkSetPollPeriod(ot_context->instance, OT_POLL_PERIOD);
468+
if (error != OT_ERROR_NONE) {
469+
NET_ERR("Failed to set %s [%d]", "poll period", error);
470+
goto exit;
471+
}
455472
}
456473

457474
/* Configure Child Supervision and MLE Child timeouts. */
@@ -486,16 +503,39 @@ int openthread_start(struct openthread_context *ot_context)
486503
otExtendedPanId xpanid;
487504
otNetworkKey networkKey;
488505

489-
otThreadSetNetworkName(ot_instance, OT_NETWORK_NAME);
490-
otLinkSetChannel(ot_instance, OT_CHANNEL);
491-
otLinkSetPanId(ot_instance, OT_PANID);
506+
error = otThreadSetNetworkName(ot_instance, OT_NETWORK_NAME);
507+
if (error != OT_ERROR_NONE) {
508+
NET_ERR("Failed to set %s [%d]", "network name", error);
509+
goto exit;
510+
}
511+
512+
error = otLinkSetChannel(ot_instance, OT_CHANNEL);
513+
if (error != OT_ERROR_NONE) {
514+
NET_ERR("Failed to set %s [%d]", "channel", error);
515+
goto exit;
516+
}
517+
518+
error = otLinkSetPanId(ot_instance, OT_PANID);
519+
if (error != OT_ERROR_NONE) {
520+
NET_ERR("Failed to set %s [%d]", "PAN ID", error);
521+
goto exit;
522+
}
523+
492524
net_bytes_from_str(xpanid.m8, 8, (char *)OT_XPANID);
493-
otThreadSetExtendedPanId(ot_instance, &xpanid);
525+
error = otThreadSetExtendedPanId(ot_instance, &xpanid);
526+
if (error != OT_ERROR_NONE) {
527+
NET_ERR("Failed to set %s [%d]", "ext PAN ID", error);
528+
goto exit;
529+
}
494530

495531
if (strlen(OT_NETWORKKEY)) {
496532
net_bytes_from_str(networkKey.m8, OT_NETWORK_KEY_SIZE,
497533
(char *)OT_NETWORKKEY);
498-
otThreadSetNetworkKey(ot_instance, &networkKey);
534+
error = otThreadSetNetworkKey(ot_instance, &networkKey);
535+
if (error != OT_ERROR_NONE) {
536+
NET_ERR("Failed to set %s [%d]", "network key", error);
537+
goto exit;
538+
}
499539
}
500540
}
501541

@@ -542,7 +582,7 @@ static int openthread_init(struct net_if *iface)
542582
.name = "openthread",
543583
.no_yield = true,
544584
};
545-
otError err;
585+
otError err = OT_ERROR_NONE;
546586

547587
NET_DBG("openthread_init");
548588

@@ -565,7 +605,11 @@ static int openthread_init(struct net_if *iface)
565605
}
566606

567607
if (IS_ENABLED(CONFIG_OPENTHREAD_COPROCESSOR)) {
568-
otPlatUartEnable();
608+
err = otPlatUartEnable();
609+
if (err != OT_ERROR_NONE) {
610+
NET_ERR("Failed to enable UART: [%d]", err);
611+
}
612+
569613
otNcpHdlcInit(ot_context->instance, ncp_hdlc_send);
570614
} else {
571615
otIp6SetReceiveFilterEnabled(ot_context->instance, true);
@@ -613,7 +657,7 @@ static int openthread_init(struct net_if *iface)
613657

614658
(void)k_work_submit_to_queue(&ot_context->work_q, &ot_context->api_work);
615659

616-
return 0;
660+
return (err == OT_ERROR_NONE) ? 0 : -EIO;
617661
}
618662

619663
void ieee802154_init(struct net_if *iface)

0 commit comments

Comments
 (0)