Skip to content

Commit 1dbe7fa

Browse files
ArekBalysNordicfabiobaltieri
authored andcommitted
openthread: Fix OpenThread independence from Zephyr network.
If CONFIG_OPENTHREAD_SYS_INIT is enabled, OpenThread initialisation should also consider running OpenThread automatically if CONFIG_OPENTHREAD_MANUAL_START is disabled. Removed also dependency on the `net_bytes_from_str` functions from the openthread.h file. Now, in the OpenThread module, there is an additional `openthread_utils.c/.h` file that can implement useful utilities for the OpenThread platform. Currently, it implements the `bytes_from_str` function to use it instead of `net_bytes_from_str`. Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
1 parent 703e2c5 commit 1dbe7fa

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

modules/openthread/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ endif()
277277
zephyr_library_named(openthread_utils)
278278
zephyr_library_sources(
279279
openthread.c
280+
openthread_utils.c
280281
)
281282
zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_SHELL shell.c)
282283
zephyr_include_directories(include)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_MODULES_OPENTHREAD_OPENTHREAD_UTILS_H_
8+
#define ZEPHYR_MODULES_OPENTHREAD_OPENTHREAD_UTILS_H_
9+
10+
#include <stdint.h>
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
/**
17+
* @brief Convert a string representation of bytes into a buffer.
18+
*
19+
* This function parses a string containing hexadecimal byte values and fills
20+
* the provided buffer with the corresponding bytes. The string may contain
21+
* optional delimiters (such as spaces or colons) between byte values.
22+
*
23+
* @param buf Pointer to the buffer where the parsed bytes will be stored.
24+
* @param buf_len Length of the buffer in bytes.
25+
* @param src Null-terminated string containing the hexadecimal byte values.
26+
*
27+
* @return 0 on success, or a negative value on error.
28+
*/
29+
int bytes_from_str(uint8_t *buf, int buf_len, const char *src);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
35+
#endif /* ZEPHYR_MODULES_OPENTHREAD_OPENTHREAD_UTILS_H_ */

modules/openthread/openthread.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LOG_MODULE_REGISTER(net_openthread_platform, CONFIG_OPENTHREAD_PLATFORM_LOG_LEVE
2121
#include "platform/platform-zephyr.h"
2222

2323
#include <openthread.h>
24+
#include <openthread_utils.h>
2425

2526
#include <openthread/child_supervision.h>
2627
#include <openthread/cli.h>
@@ -182,15 +183,22 @@ static bool ot_setup_default_configuration(void)
182183
return false;
183184
}
184185

185-
net_bytes_from_str(xpanid.m8, 8, (char *)OT_XPANID);
186+
if (bytes_from_str(xpanid.m8, 8, (char *)OT_XPANID) != 0) {
187+
LOG_ERR("Failed to parse extended PAN ID");
188+
return false;
189+
}
186190
error = otThreadSetExtendedPanId(openthread_instance, &xpanid);
187191
if (error != OT_ERROR_NONE) {
188192
LOG_ERR("Failed to set %s [%d]", "ext PAN ID", error);
189193
return false;
190194
}
191195

192196
if (strlen(OT_NETWORKKEY)) {
193-
net_bytes_from_str(networkKey.m8, OT_NETWORK_KEY_SIZE, (char *)OT_NETWORKKEY);
197+
if (bytes_from_str(networkKey.m8, OT_NETWORK_KEY_SIZE, (char *)OT_NETWORKKEY) !=
198+
0) {
199+
LOG_ERR("Failed to parse network key");
200+
return false;
201+
}
194202
error = otThreadSetNetworkKey(openthread_instance, &networkKey);
195203
if (error != OT_ERROR_NONE) {
196204
LOG_ERR("Failed to set %s [%d]", "network key", error);
@@ -485,5 +493,18 @@ void openthread_mutex_unlock(void)
485493
}
486494

487495
#ifdef CONFIG_OPENTHREAD_SYS_INIT
488-
SYS_INIT(openthread_init, POST_KERNEL, CONFIG_OPENTHREAD_SYS_INIT_PRIORITY);
496+
static int openthread_sys_init(void)
497+
{
498+
int error = openthread_init();
499+
500+
if (error == 0) {
501+
#ifndef CONFIG_OPENTHREAD_MANUAL_START
502+
error = openthread_run();
503+
#endif
504+
}
505+
506+
return error;
507+
}
508+
509+
SYS_INIT(openthread_sys_init, POST_KERNEL, CONFIG_OPENTHREAD_SYS_INIT_PRIORITY);
489510
#endif /* CONFIG_OPENTHREAD_SYS_INIT */

modules/openthread/openthread_utils.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* This file provides utility functions for the OpenThread module.
10+
*
11+
*/
12+
13+
#include <stddef.h>
14+
#include <string.h>
15+
#include <ctype.h>
16+
#include <stdlib.h>
17+
#include <errno.h>
18+
19+
#include <openthread_utils.h>
20+
21+
int bytes_from_str(uint8_t *buf, int buf_len, const char *src)
22+
{
23+
if (!buf || !src) {
24+
return -EINVAL;
25+
}
26+
27+
size_t i;
28+
size_t src_len = strlen(src);
29+
char *endptr;
30+
31+
for (i = 0U; i < src_len; i++) {
32+
if (!isxdigit((unsigned char)src[i]) && src[i] != ':') {
33+
return -EINVAL;
34+
}
35+
}
36+
37+
(void)memset(buf, 0, buf_len);
38+
39+
for (i = 0U; i < (size_t)buf_len; i++) {
40+
buf[i] = (uint8_t)strtol(src, &endptr, 16);
41+
src = ++endptr;
42+
}
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)