Skip to content

Commit e801d9b

Browse files
committed
[nrf fromlist] 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`. Upstream PR #: 90861 Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
1 parent 26c6ea3 commit e801d9b

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-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 The number of bytes written to the buffer, 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: 19 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,15 @@ static bool ot_setup_default_configuration(void)
182183
return false;
183184
}
184185

185-
net_bytes_from_str(xpanid.m8, 8, (char *)OT_XPANID);
186+
bytes_from_str(xpanid.m8, 8, (char *)OT_XPANID);
186187
error = otThreadSetExtendedPanId(openthread_instance, &xpanid);
187188
if (error != OT_ERROR_NONE) {
188189
LOG_ERR("Failed to set %s [%d]", "ext PAN ID", error);
189190
return false;
190191
}
191192

192193
if (strlen(OT_NETWORKKEY)) {
193-
net_bytes_from_str(networkKey.m8, OT_NETWORK_KEY_SIZE, (char *)OT_NETWORKKEY);
194+
bytes_from_str(networkKey.m8, OT_NETWORK_KEY_SIZE, (char *)OT_NETWORKKEY);
194195
error = otThreadSetNetworkKey(openthread_instance, &networkKey);
195196
if (error != OT_ERROR_NONE) {
196197
LOG_ERR("Failed to set %s [%d]", "network key", error);
@@ -485,5 +486,20 @@ void openthread_mutex_unlock(void)
485486
}
486487

487488
#ifdef CONFIG_OPENTHREAD_SYS_INIT
488-
SYS_INIT(openthread_init, POST_KERNEL, CONFIG_OPENTHREAD_SYS_INIT_PRIORITY);
489+
static int openthread_sys_init(void)
490+
{
491+
int error;
492+
493+
error = openthread_init();
494+
495+
if (error == 0) {
496+
#ifndef CONFIG_OPENTHREAD_MANUAL_START
497+
error = openthread_run();
498+
#endif
499+
}
500+
501+
return error;
502+
}
503+
504+
SYS_INIT(openthread_sys_init, POST_KERNEL, CONFIG_OPENTHREAD_SYS_INIT_PRIORITY);
489505
#endif /* CONFIG_OPENTHREAD_SYS_INIT */

modules/openthread/openthread_utils.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
size_t i;
24+
size_t src_len = strlen(src);
25+
char *endptr;
26+
27+
for (i = 0U; i < src_len; i++) {
28+
if (!isxdigit((unsigned char)src[i]) && src[i] != ':') {
29+
return -EINVAL;
30+
}
31+
}
32+
33+
(void)memset(buf, 0, buf_len);
34+
35+
for (i = 0U; i < (size_t)buf_len; i++) {
36+
buf[i] = (uint8_t)strtol(src, &endptr, 16);
37+
src = ++endptr;
38+
}
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)