Skip to content

Commit 6194a74

Browse files
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. Signed-off-by: Arkadiusz Balys <[email protected]>
1 parent 48d1164 commit 6194a74

File tree

6 files changed

+749
-468
lines changed

6 files changed

+749
-468
lines changed

include/zephyr/net/openthread.h

Lines changed: 175 additions & 21 deletions
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,76 @@ 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 Such structure should not be allocated on stack.
112+
*/
113+
struct openthread_state_changed_callback {
114+
/**
115+
* @brief Callback for notifying configuration or state changes.
116+
*
117+
* @param flags As per OpenThread otStateChangedCallback() aFlags parameter.
118+
* See https://openthread.io/reference/group/api-instance#otstatechangedcallback
119+
* @param instance The OpenThread instance the callback is registered with.
120+
* @param user_data Data to pass to the callback.
121+
*/
122+
void (*openthread_state_changed_cb)(otChangedFlags flags, struct otInstance *instance,
123+
void *user_data);
124+
125+
/** User data if required */
126+
void *user_data;
127+
128+
/**
129+
* Internally used field for list handling
130+
* - user must not directly modify
131+
*/
132+
sys_snode_t node;
133+
};
134+
135+
/**
136+
* @deprecated use @ref openthread_state_changed_callback instead.
137+
*
84138
* @brief OpenThread state change callback structure
85139
*
86140
* Used to register a callback in the callback list. As many
@@ -111,23 +165,44 @@ struct openthread_state_changed_cb {
111165
};
112166

113167
/**
168+
* @brief Register callbacks that will be called when a certain configuration
169+
* or state changes occur within OpenThread.
170+
*
171+
* @param cb Callback struct to register.
172+
*/
173+
int openthread_state_change_callback_register(struct openthread_state_changed_callback *cb);
174+
175+
/**
176+
* @brief Unregister OpenThread configuration or state changed callbacks.
177+
*
178+
* @note This function must be called after openthread_init()
179+
*
180+
* @param cb Callback struct to unregister.
181+
*/
182+
int openthread_state_change_callback_unregister(struct openthread_state_changed_callback *cb);
183+
184+
/**
185+
* @deprecated use @ref openthread_state_change_callback_register instead.
186+
*
114187
* @brief Registers callbacks which will be called when certain configuration
115188
* or state changes occur within OpenThread.
116189
*
117190
* @param ot_context the OpenThread context to register the callback with.
118191
* @param cb callback struct to register.
119192
*/
120-
int openthread_state_changed_cb_register(struct openthread_context *ot_context,
121-
struct openthread_state_changed_cb *cb);
193+
__deprecated int openthread_state_changed_cb_register(struct openthread_context *ot_context,
194+
struct openthread_state_changed_cb *cb);
122195

123196
/**
197+
* @deprecated use @ref openthread_state_change_callback_unregister instead.
198+
*
124199
* @brief Unregisters OpenThread configuration or state changed callbacks.
125200
*
126201
* @param ot_context the OpenThread context to unregister the callback from.
127202
* @param cb callback struct to unregister.
128203
*/
129-
int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
130-
struct openthread_state_changed_cb *cb);
204+
__deprecated int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
205+
struct openthread_state_changed_cb *cb);
131206

132207
/**
133208
* @brief Get OpenThread thread identification.
@@ -151,6 +226,44 @@ struct openthread_context *openthread_get_default_context(void);
151226
struct otInstance *openthread_get_default_instance(void);
152227

153228
/**
229+
* @brief Initialize the OpenThread module.
230+
*
231+
* This function:
232+
* - Initializes the OpenThread module.
233+
* - Creates an OpenThread single instance.
234+
* - Starts the shell.
235+
* - Enables the UART and NCP HDLC for coprocessor purposes.
236+
* - Initializes the NAT64 translator.
237+
* - Creates a work queue for the OpenThread module.
238+
* - Initializes the state change callback list.
239+
*
240+
* @note This function is automatically called by Zephyr's networking layer.
241+
* If you want to initialize the OpenThread independently, call this function
242+
* in your application init code.
243+
*
244+
* @return true if initialization succeeded, false otherwise.
245+
*/
246+
bool openthread_init(void);
247+
248+
/**
249+
* @brief Run the OpenThread network.
250+
*
251+
* @details Prepares the OpenThread network and enables it.
252+
* Depends on active settings: it uses the stored network configuration,
253+
* starts the joining procedure or uses the default network configuration.
254+
* Additionally, when the device is MTD, it sets the SED mode to properly
255+
* attach the network.
256+
*/
257+
int openthread_run(void);
258+
259+
/**
260+
* @brief Disable the OpenThread network.
261+
*/
262+
int openthread_stop(void);
263+
264+
/**
265+
* @deprecated use @ref openthread_run instead.
266+
*
154267
* @brief Starts the OpenThread network.
155268
*
156269
* @details Depends on active settings: it uses stored network configuration,
@@ -159,9 +272,46 @@ struct otInstance *openthread_get_default_instance(void);
159272
*
160273
* @param ot_context
161274
*/
162-
int openthread_start(struct openthread_context *ot_context);
275+
__deprecated int openthread_start(struct openthread_context *ot_context);
276+
277+
/**
278+
* @brief Set the additional callback for receiving packets.
279+
*
280+
* @details This callback is called once a packet is received and can be
281+
* used to inject packets into the OpenThread stack.
282+
* Setting this callback is optional.
283+
*
284+
* @param cb Callback to set.
285+
* @param context Context to pass to the callback.
286+
*/
287+
void openthread_set_receive_cb(openthread_receive_cb cb, void *context);
288+
289+
/**
290+
* @brief Lock internal mutex before accessing OpenThread API.
291+
*
292+
* @details OpenThread API is not thread-safe. Therefore, before accessing any
293+
* API function, you need to lock the internal mutex, to prevent the
294+
* OpenThread thread from pre-empting the API call.
295+
*/
296+
void openthread_mutex_lock(void);
163297

164298
/**
299+
* @brief Try to lock internal mutex before accessing OpenThread API.
300+
*
301+
* @details This function behaves like openthread_mutex_lock(), provided that
302+
* the internal mutex is unlocked. Otherwise, it returns a negative value without
303+
* waiting.
304+
*/
305+
int openthread_mutex_try_lock(void);
306+
307+
/**
308+
* @brief Unlock internal mutex after accessingOpenThread API.
309+
*/
310+
void openthread_mutex_unlock(void);
311+
312+
/**
313+
* @deprecated use @ref openthread_mutex_lock.
314+
*
165315
* @brief Lock internal mutex before accessing OT API.
166316
*
167317
* @details OpenThread API is not thread-safe, therefore before accessing any
@@ -170,9 +320,11 @@ int openthread_start(struct openthread_context *ot_context);
170320
*
171321
* @param ot_context Context to lock.
172322
*/
173-
void openthread_api_mutex_lock(struct openthread_context *ot_context);
323+
__deprecated void openthread_api_mutex_lock(struct openthread_context *ot_context);
174324

175325
/**
326+
* @deprecated use @ref openthread_mutex_try_lock instead.
327+
*
176328
* @brief Try to lock internal mutex before accessing OT API.
177329
*
178330
* @details This function behaves like openthread_api_mutex_lock() provided that
@@ -183,14 +335,16 @@ void openthread_api_mutex_lock(struct openthread_context *ot_context);
183335
* @retval 0 On success.
184336
* @retval <0 On failure.
185337
*/
186-
int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
338+
__deprecated int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
187339

188340
/**
341+
* @deprecated use @ref openthread_mutex_unlock instead.
342+
*
189343
* @brief Unlock internal mutex after accessing OT API.
190344
*
191345
* @param ot_context Context to unlock.
192346
*/
193-
void openthread_api_mutex_unlock(struct openthread_context *ot_context);
347+
__deprecated void openthread_api_mutex_unlock(struct openthread_context *ot_context);
194348

195349
/** @cond INTERNAL_HIDDEN */
196350

modules/openthread/platform/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ zephyr_library_sources(
66
entropy.c
77
misc.c
88
platform.c
9+
openthread.c
910
)
1011

1112
zephyr_library_sources_ifndef(CONFIG_HDLC_RCP_IF

0 commit comments

Comments
 (0)