Skip to content

Commit d781fe2

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 layer 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 63c239c commit d781fe2

File tree

7 files changed

+687
-463
lines changed

7 files changed

+687
-463
lines changed

include/zephyr/net/openthread.h

Lines changed: 165 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
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 outside anymore.
50+
*/
51+
__deprecated otInstance *instance;
4952

5053
/** Pointer to OpenThread network interface */
5154
struct net_if *iface;
@@ -62,22 +65,40 @@ 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 outside 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 outside 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 outside 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 IPv4 (translated by NAT64) and IPv6 datagrams.
94+
*
95+
* This callback is called when a datagram is received.
96+
*
97+
* @param aMessage The message to receive.
98+
* @param aContext The context to pass to the callback.
99+
*/
100+
typedef void (*openthread_receive_cb)(otMessage *aMessage, void *aContext);
101+
81102
/** OpenThread state change callback */
82103

83104
/**
@@ -88,6 +109,38 @@ struct openthread_context {
88109
* are unique pointers of struct openthread_state_changed_cb.
89110
* Beware such structure should not be allocated on stack.
90111
*/
112+
struct openthread_state_changed_callback {
113+
/**
114+
* @brief Callback for notifying configuration or state changes.
115+
*
116+
* @param flags as per OpenThread otStateChangedCallback() aFlags parameter.
117+
* See https://openthread.io/reference/group/api-instance#otstatechangedcallback
118+
* @param instance the OpenThread instance the callback is registered with.
119+
* @param user_data Data to pass to the callback.
120+
*/
121+
void (*openthread_state_changed_cb)(otChangedFlags flags, struct otInstance *instance,
122+
void *user_data);
123+
124+
/** User data if required */
125+
void *user_data;
126+
127+
/**
128+
* Internally used field for list handling
129+
* - user must not directly modify
130+
*/
131+
sys_snode_t node;
132+
};
133+
134+
/**
135+
* @deprecated use @ref openthread_state_changed_callback instead.
136+
*
137+
* @brief OpenThread state change callback structure
138+
*
139+
* Used to register a callback in the callback list. As many
140+
* callbacks as needed can be added as long as each of them
141+
* are unique pointers of struct openthread_state_changed_cb.
142+
* Beware such structure should not be allocated on stack.
143+
*/
91144
struct openthread_state_changed_cb {
92145
/**
93146
* @brief Callback for notifying configuration or state changes.
@@ -111,23 +164,44 @@ struct openthread_state_changed_cb {
111164
};
112165

113166
/**
167+
* @brief Registers callbacks which will be called when certain configuration
168+
* or state changes occur within OpenThread.
169+
*
170+
* @param cb callback struct to register.
171+
*/
172+
int openthread_state_change_callback_register(struct openthread_state_changed_callback *cb);
173+
174+
/**
175+
* @brief Unregisters OpenThread configuration or state changed callbacks.
176+
*
177+
* @param cb callback struct to unregister.
178+
*/
179+
int openthread_state_change_callback_unregister(struct openthread_state_changed_callback *cb);
180+
181+
/**
182+
* @deprecated use @ref openthread_platform_state_changed_cb_register from modules/openthread
183+
* instead.
184+
*
114185
* @brief Registers callbacks which will be called when certain configuration
115186
* or state changes occur within OpenThread.
116187
*
117188
* @param ot_context the OpenThread context to register the callback with.
118189
* @param cb callback struct to register.
119190
*/
120-
int openthread_state_changed_cb_register(struct openthread_context *ot_context,
121-
struct openthread_state_changed_cb *cb);
191+
__deprecated int openthread_state_changed_cb_register(struct openthread_context *ot_context,
192+
struct openthread_state_changed_cb *cb);
122193

123194
/**
195+
* @deprecated use @ref openthread_platform_state_changed_cb_unregister from modules/openthread
196+
* instead.
197+
*
124198
* @brief Unregisters OpenThread configuration or state changed callbacks.
125199
*
126200
* @param ot_context the OpenThread context to unregister the callback from.
127201
* @param cb callback struct to unregister.
128202
*/
129-
int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
130-
struct openthread_state_changed_cb *cb);
203+
__deprecated int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
204+
struct openthread_state_changed_cb *cb);
131205

132206
/**
133207
* @brief Get OpenThread thread identification.
@@ -151,6 +225,46 @@ struct openthread_context *openthread_get_default_context(void);
151225
struct otInstance *openthread_get_default_instance(void);
152226

153227
/**
228+
* @brief Initialize the OpenThread module.
229+
*
230+
* This function:
231+
* - Initializes the OpenThread module.
232+
* - Creates an OpenThread single instance.
233+
* - Starts the shell.
234+
* - Enables the UART and NCP HDLC for coprocessor purposes.
235+
* - Initializes the NAT64 translator.
236+
* - Creates a work queue for the OpenThread module.
237+
* - Initializes the state change callback list.
238+
*
239+
* @note This function is automatically called by Zephyr's networking layer.
240+
* If you want to initialize the OpenThread independently, call this function
241+
* in your application init code and then call openthread_run() to prepare and run
242+
* the OpenThread network.
243+
*
244+
* @param rx_handler The receive callback for the OpenThread module.
245+
* @param context The context to pass to the callback.
246+
* @return true if initialization succeeded, false otherwise.
247+
*/
248+
bool openthread_init(openthread_receive_cb rx_handler, void *context);
249+
250+
/**
251+
* @brief Runs the OpenThread network.
252+
*
253+
* @details Prepares the OpenThread network and enables it.
254+
* Depends on active settings: it uses stored network configuration,
255+
* start joining procedure or uses default network configuration. Additionally
256+
* when the device is MTD, it sets the SED mode to properly attach the network.
257+
*/
258+
int openthread_run(void);
259+
260+
/**
261+
* @brief Disables the OpenThread network.
262+
*/
263+
int openthread_stop(void);
264+
265+
/**
266+
* @deprecated use @ref openthread_run instead.
267+
*
154268
* @brief Starts the OpenThread network.
155269
*
156270
* @details Depends on active settings: it uses stored network configuration,
@@ -159,7 +273,7 @@ struct otInstance *openthread_get_default_instance(void);
159273
*
160274
* @param ot_context
161275
*/
162-
int openthread_start(struct openthread_context *ot_context);
276+
__deprecated int openthread_start(struct openthread_context *ot_context);
163277

164278
/**
165279
* @brief Lock internal mutex before accessing OT API.
@@ -170,9 +284,40 @@ int openthread_start(struct openthread_context *ot_context);
170284
*
171285
* @param ot_context Context to lock.
172286
*/
173-
void openthread_api_mutex_lock(struct openthread_context *ot_context);
287+
void openthread_mutex_lock(void);
174288

175289
/**
290+
* @brief Try to lock internal mutex before accessing OT API.
291+
*
292+
* @details This function behaves like openthread_mutex_lock() provided that
293+
* the internal mutex is unlocked. Otherwise, it exists immediately and returns
294+
* a negative value.
295+
*/
296+
int openthread_mutex_try_lock(void);
297+
298+
/**
299+
* @brief Unlock internal mutex after accessing OT API.
300+
*
301+
* @param ot_context Context to unlock.
302+
*/
303+
void openthread_mutex_unlock(void);
304+
305+
/**
306+
* @deprecated use @ref openthread_mutex_lock.
307+
*
308+
* @brief Lock internal mutex before accessing OT API.
309+
*
310+
* @details OpenThread API is not thread-safe, therefore before accessing any
311+
* API function, it's needed to lock the internal mutex, to prevent the
312+
* OpenThread thread from preempting the API call.
313+
*
314+
* @param ot_context Context to lock.
315+
*/
316+
__deprecated void openthread_api_mutex_lock(struct openthread_context *ot_context);
317+
318+
/**
319+
* @deprecated use @ref openthread_mutex_try_lock instead.
320+
*
176321
* @brief Try to lock internal mutex before accessing OT API.
177322
*
178323
* @details This function behaves like openthread_api_mutex_lock() provided that
@@ -183,14 +328,16 @@ void openthread_api_mutex_lock(struct openthread_context *ot_context);
183328
* @retval 0 On success.
184329
* @retval <0 On failure.
185330
*/
186-
int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
331+
__deprecated int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
187332

188333
/**
334+
* @deprecated use @ref openthread_mutex_unlock instead.
335+
*
189336
* @brief Unlock internal mutex after accessing OT API.
190337
*
191338
* @param ot_context Context to unlock.
192339
*/
193-
void openthread_api_mutex_unlock(struct openthread_context *ot_context);
340+
__deprecated void openthread_api_mutex_unlock(struct openthread_context *ot_context);
194341

195342
/** @cond INTERNAL_HIDDEN */
196343

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)