Skip to content

Commit a98cb40

Browse files
[nrf fromlist] openthread: Move OpenThread implementation from net to modules
Move OpenThread-related code from zephyr/subsys/net/l2/openthread/openthread.c to zephyr/modules/openthread/platform/openthread.c. The primary goal of this refactor is to enable the use of OpenThread as an independent module, without the necessity of Zephyr's networking layer. This change is particularly beneficial for simple applications that have their own implementation of the IEEE802.15.4 driver and do not require a networking layer. These applications can now disable Zephyr's L2 and IEEE802.15.4 shim layers and directly use the OpenThread module, saving valuable kilobytes of memory. In this approach if the CONFIG_NET_L2_OPENTHREAD Kconfig option is set, Zephyr's L2 and IEEE802.15.4 layers will be used, and everything will function as before. The main difference is the Zephyr's L2 layer now uses the OpenThread module, no longer implementing it. While most of the functions in include/net/openthread.h have been deprecated, they are still available for use to maintain backwards compatibility. Upstream PR #: 89090 Signed-off-by: Arkadiusz Balys <[email protected]>
1 parent 48d1164 commit a98cb40

File tree

7 files changed

+759
-467
lines changed

7 files changed

+759
-467
lines changed

include/zephyr/net/openthread.h

+171-21
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@
55
*/
66

77
/** @file
8-
* @brief OpenThread L2 stack public header
8+
* @brief OpenThread stack public header
99
*/
1010

1111
#ifndef ZEPHYR_INCLUDE_NET_OPENTHREAD_H_
1212
#define ZEPHYR_INCLUDE_NET_OPENTHREAD_H_
1313

1414
/**
15-
* @brief OpenThread Layer 2 abstraction layer
16-
* @defgroup openthread OpenThread L2 abstraction layer
15+
* @brief OpenThread stack public header
16+
* @defgroup openthread OpenThread stack
1717
* @since 1.11
1818
* @version 0.8.0
1919
* @ingroup ieee802154
2020
* @{
2121
*/
2222

2323
#include <zephyr/kernel.h>
24-
2524
#include <zephyr/net/net_if.h>
25+
#include <zephyr/kernel/thread.h>
2626

2727
#include <openthread/instance.h>
28+
#include <openthread/message.h>
2829

2930
#ifdef __cplusplus
3031
extern "C" {
@@ -44,8 +45,10 @@ struct pkt_list_elem {
4445
* @brief OpenThread l2 private data.
4546
*/
4647
struct openthread_context {
47-
/** Pointer to OpenThread stack instance */
48-
otInstance *instance;
48+
/** @deprecated Pointer to OpenThread stack instance. This is deprecated and will be removed
49+
* in a future release. This field must not be used anymore.
50+
*/
51+
__deprecated otInstance *instance;
4952

5053
/** Pointer to OpenThread network interface */
5154
struct net_if *iface;
@@ -62,25 +65,74 @@ struct openthread_context {
6265
/** Array for storing net_pkt for OpenThread internal usage */
6366
struct pkt_list_elem pkt_list[CONFIG_OPENTHREAD_PKT_LIST_SIZE];
6467

65-
/** A mutex to protect API calls from being preempted. */
66-
struct k_mutex api_lock;
68+
/** @deprecated A mutex to protect API calls from being preempted. This is deprecated and
69+
* will be removed in a future release. This field must not be used anymore.
70+
*/
71+
__deprecated struct k_mutex api_lock;
6772

68-
/** A work queue for all OpenThread activity */
69-
struct k_work_q work_q;
73+
/** @deprecated A work queue for all OpenThread activity. This is deprecated and will be
74+
* removed in a future release. This field must not be used anymore.
75+
*/
76+
__deprecated struct k_work_q work_q;
7077

71-
/** Work object for OpenThread internal usage */
72-
struct k_work api_work;
78+
/** @deprecated Work object for OpenThread internal usage. This is deprecated and will be
79+
* removed in a future release. This field must not be used anymore.
80+
*/
81+
__deprecated struct k_work api_work;
7382

74-
/** A list for state change callbacks */
83+
/** @deprecated A list for state change callbacks. This is deprecated and will be removed in
84+
* a future release.
85+
*/
7586
sys_slist_t state_change_cbs;
7687
};
7788
/**
7889
* INTERNAL_HIDDEN @endcond
7990
*/
8091

92+
/**
93+
* @brief The common callback type for receiving IPv4 (translated by NAT64) and IPv6 datagrams.
94+
*
95+
* This callback is called when a datagram is received.
96+
*
97+
* @param message The message to receive.
98+
* @param context The context to pass to the callback.
99+
*/
100+
typedef void (*openthread_receive_cb)(otMessage *message, void *context);
101+
81102
/** OpenThread state change callback */
82103

83104
/**
105+
* @brief OpenThread state change callback structure
106+
*
107+
* Used to register a callback in the callback list. As many
108+
* callbacks as needed can be added as long as each of them
109+
* are unique pointers of struct openthread_state_changed_cb.
110+
*
111+
* @note You may destroy the object only after it is unregistered from the callback list.
112+
*/
113+
struct openthread_state_changed_callback {
114+
/**
115+
* @brief Callback for notifying configuration or state changes.
116+
*
117+
* @param otCallback OpenThread callback to register.
118+
* See https://openthread.io/reference/group/api-instance#otstatechangedcallback for
119+
* details.
120+
*/
121+
otStateChangedCallback otCallback;
122+
123+
/** User data if required */
124+
void *user_data;
125+
126+
/**
127+
* Internally used field for list handling
128+
* - user must not directly modify
129+
*/
130+
sys_snode_t node;
131+
};
132+
133+
/**
134+
* @deprecated use @ref openthread_state_changed_callback instead.
135+
*
84136
* @brief OpenThread state change callback structure
85137
*
86138
* Used to register a callback in the callback list. As many
@@ -111,23 +163,42 @@ struct openthread_state_changed_cb {
111163
};
112164

113165
/**
166+
* @brief Register callbacks that will be called when a certain configuration
167+
* or state changes occur within OpenThread.
168+
*
169+
* @param cb Callback struct to register.
170+
*/
171+
int openthread_state_changed_callback_register(struct openthread_state_changed_callback *cb);
172+
173+
/**
174+
* @brief Unregister OpenThread configuration or state changed callbacks.
175+
*
176+
* @param cb Callback struct to unregister.
177+
*/
178+
int openthread_state_changed_callback_unregister(struct openthread_state_changed_callback *cb);
179+
180+
/**
181+
* @deprecated use @ref openthread_state_changed_callback_register instead.
182+
*
114183
* @brief Registers callbacks which will be called when certain configuration
115184
* or state changes occur within OpenThread.
116185
*
117186
* @param ot_context the OpenThread context to register the callback with.
118187
* @param cb callback struct to register.
119188
*/
120-
int openthread_state_changed_cb_register(struct openthread_context *ot_context,
121-
struct openthread_state_changed_cb *cb);
189+
__deprecated int openthread_state_changed_cb_register(struct openthread_context *ot_context,
190+
struct openthread_state_changed_cb *cb);
122191

123192
/**
193+
* @deprecated use @ref openthread_state_changed_callback_unregister instead.
194+
*
124195
* @brief Unregisters OpenThread configuration or state changed callbacks.
125196
*
126197
* @param ot_context the OpenThread context to unregister the callback from.
127198
* @param cb callback struct to unregister.
128199
*/
129-
int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
130-
struct openthread_state_changed_cb *cb);
200+
__deprecated int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
201+
struct openthread_state_changed_cb *cb);
131202

132203
/**
133204
* @brief Get OpenThread thread identification.
@@ -151,6 +222,44 @@ struct openthread_context *openthread_get_default_context(void);
151222
struct otInstance *openthread_get_default_instance(void);
152223

153224
/**
225+
* @brief Initialize the OpenThread module.
226+
*
227+
* This function:
228+
* - Initializes the OpenThread module.
229+
* - Creates an OpenThread single instance.
230+
* - Starts the shell.
231+
* - Enables the UART and NCP HDLC for coprocessor purposes.
232+
* - Initializes the NAT64 translator.
233+
* - Creates a work queue for the OpenThread module.
234+
*
235+
* @note This function is automatically called by Zephyr's networking layer.
236+
* If you want to initialize the OpenThread independently, call this function
237+
* in your application init code.
238+
*
239+
* @retval 0 On success.
240+
* @retval -EIO On failure.
241+
*/
242+
int openthread_init(void);
243+
244+
/**
245+
* @brief Run the OpenThread network.
246+
*
247+
* @details Prepares the OpenThread network and enables it.
248+
* Depends on active settings: it uses the stored network configuration,
249+
* starts the joining procedure or uses the default network configuration.
250+
* Additionally, when the device is MTD, it sets the SED mode to properly
251+
* attach the network.
252+
*/
253+
int openthread_run(void);
254+
255+
/**
256+
* @brief Disable the OpenThread network.
257+
*/
258+
int openthread_stop(void);
259+
260+
/**
261+
* @deprecated use @ref openthread_run instead.
262+
*
154263
* @brief Starts the OpenThread network.
155264
*
156265
* @details Depends on active settings: it uses stored network configuration,
@@ -159,9 +268,46 @@ struct otInstance *openthread_get_default_instance(void);
159268
*
160269
* @param ot_context
161270
*/
162-
int openthread_start(struct openthread_context *ot_context);
271+
__deprecated int openthread_start(struct openthread_context *ot_context);
163272

164273
/**
274+
* @brief Set the additional callback for receiving packets.
275+
*
276+
* @details This callback is called once a packet is received and can be
277+
* used to inject packets into the Zephyr networking stack.
278+
* Setting this callback is optional.
279+
*
280+
* @param cb Callback to set.
281+
* @param context Context to pass to the callback.
282+
*/
283+
void openthread_set_receive_cb(openthread_receive_cb cb, void *context);
284+
285+
/**
286+
* @brief Lock internal mutex before accessing OpenThread API.
287+
*
288+
* @details OpenThread API is not thread-safe. Therefore, before accessing any
289+
* API function, you need to lock the internal mutex, to prevent the
290+
* OpenThread thread from pre-empting the API call.
291+
*/
292+
void openthread_mutex_lock(void);
293+
294+
/**
295+
* @brief Try to lock internal mutex before accessing OpenThread API.
296+
*
297+
* @details This function behaves like openthread_mutex_lock(), provided that
298+
* the internal mutex is unlocked. Otherwise, it returns a negative value without
299+
* waiting.
300+
*/
301+
int openthread_mutex_try_lock(void);
302+
303+
/**
304+
* @brief Unlock internal mutex after accessing OpenThread API.
305+
*/
306+
void openthread_mutex_unlock(void);
307+
308+
/**
309+
* @deprecated use @ref openthread_mutex_lock.
310+
*
165311
* @brief Lock internal mutex before accessing OT API.
166312
*
167313
* @details OpenThread API is not thread-safe, therefore before accessing any
@@ -170,9 +316,11 @@ int openthread_start(struct openthread_context *ot_context);
170316
*
171317
* @param ot_context Context to lock.
172318
*/
173-
void openthread_api_mutex_lock(struct openthread_context *ot_context);
319+
__deprecated void openthread_api_mutex_lock(struct openthread_context *ot_context);
174320

175321
/**
322+
* @deprecated use @ref openthread_mutex_try_lock instead.
323+
*
176324
* @brief Try to lock internal mutex before accessing OT API.
177325
*
178326
* @details This function behaves like openthread_api_mutex_lock() provided that
@@ -183,14 +331,16 @@ void openthread_api_mutex_lock(struct openthread_context *ot_context);
183331
* @retval 0 On success.
184332
* @retval <0 On failure.
185333
*/
186-
int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
334+
__deprecated int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
187335

188336
/**
337+
* @deprecated use @ref openthread_mutex_unlock instead.
338+
*
189339
* @brief Unlock internal mutex after accessing OT API.
190340
*
191341
* @param ot_context Context to unlock.
192342
*/
193-
void openthread_api_mutex_unlock(struct openthread_context *ot_context);
343+
__deprecated void openthread_api_mutex_unlock(struct openthread_context *ot_context);
194344

195345
/** @cond INTERNAL_HIDDEN */
196346

modules/openthread/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ zephyr_link_libraries(${ot_libs})
273273

274274
endif()
275275

276+
# Create a library for the OpenThread Zephyr utils
277+
zephyr_library_named(openthread_utils)
278+
zephyr_library_sources(
279+
openthread.c
280+
)
281+
zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_SHELL shell.c)
282+
276283
add_subdirectory(platform)
277284

278285
endif()

0 commit comments

Comments
 (0)