Skip to content

Commit 783a713

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

File tree

6 files changed

+677
-465
lines changed

6 files changed

+677
-465
lines changed

include/zephyr/net/openthread.h

Lines changed: 161 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 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 receiving 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,42 @@ 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_state_change_callback_register instead.
183+
*
114184
* @brief Registers callbacks which will be called when certain configuration
115185
* or state changes occur within OpenThread.
116186
*
117187
* @param ot_context the OpenThread context to register the callback with.
118188
* @param cb callback struct to register.
119189
*/
120-
int openthread_state_changed_cb_register(struct openthread_context *ot_context,
121-
struct openthread_state_changed_cb *cb);
190+
__deprecated int openthread_state_changed_cb_register(struct openthread_context *ot_context,
191+
struct openthread_state_changed_cb *cb);
122192

123193
/**
194+
* @deprecated use @ref openthread_state_change_callback_unregister instead.
195+
*
124196
* @brief Unregisters OpenThread configuration or state changed callbacks.
125197
*
126198
* @param ot_context the OpenThread context to unregister the callback from.
127199
* @param cb callback struct to unregister.
128200
*/
129-
int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
130-
struct openthread_state_changed_cb *cb);
201+
__deprecated int openthread_state_changed_cb_unregister(struct openthread_context *ot_context,
202+
struct openthread_state_changed_cb *cb);
131203

132204
/**
133205
* @brief Get OpenThread thread identification.
@@ -151,6 +223,45 @@ struct openthread_context *openthread_get_default_context(void);
151223
struct otInstance *openthread_get_default_instance(void);
152224

153225
/**
226+
* @brief Initialize the OpenThread module.
227+
*
228+
* This function:
229+
* - Initializes the OpenThread module.
230+
* - Creates an OpenThread single instance.
231+
* - Starts the shell.
232+
* - Enables the UART and NCP HDLC for coprocessor purposes.
233+
* - Initializes the NAT64 translator.
234+
* - Creates a work queue for the OpenThread module.
235+
* - Initializes the state change callback list.
236+
*
237+
* @note This function is automatically called by Zephyr's networking layer.
238+
* If you want to initialize the OpenThread independently, call this function
239+
* in your application init code.
240+
*
241+
* @param rx_handler The receive callback for the OpenThread module.
242+
* @param context The context to pass to the callback.
243+
* @return true if initialization succeeded, false otherwise.
244+
*/
245+
bool openthread_init(openthread_receive_cb rx_handler, void *context);
246+
247+
/**
248+
* @brief Runs the OpenThread network.
249+
*
250+
* @details Prepares the OpenThread network and enables it.
251+
* Depends on active settings: it uses stored network configuration,
252+
* start joining procedure or uses default network configuration. Additionally
253+
* when the device is MTD, it sets the SED mode to properly attach the network.
254+
*/
255+
int openthread_run(void);
256+
257+
/**
258+
* @brief Disables the OpenThread network.
259+
*/
260+
int openthread_stop(void);
261+
262+
/**
263+
* @deprecated use @ref openthread_run instead.
264+
*
154265
* @brief Starts the OpenThread network.
155266
*
156267
* @details Depends on active settings: it uses stored network configuration,
@@ -159,9 +270,34 @@ struct otInstance *openthread_get_default_instance(void);
159270
*
160271
* @param ot_context
161272
*/
162-
int openthread_start(struct openthread_context *ot_context);
273+
__deprecated int openthread_start(struct openthread_context *ot_context);
274+
275+
/**
276+
* @brief Lock internal mutex before accessing OpenThread API.
277+
*
278+
* @details OpenThread API is not thread-safe, therefore before accessing any
279+
* API function, it's needed to lock the internal mutex, to prevent the
280+
* OpenThread thread from preempting the API call.
281+
*/
282+
void openthread_mutex_lock(void);
283+
284+
/**
285+
* @brief Try to lock internal mutex before accessing OpenThread API.
286+
*
287+
* @details This function behaves like openthread_mutex_lock() provided that
288+
* the internal mutex is unlocked. Otherwise, it exists immediately and returns
289+
* a negative value.
290+
*/
291+
int openthread_mutex_try_lock(void);
163292

164293
/**
294+
* @brief Unlock internal mutex after accessingOpenThread API.
295+
*/
296+
void openthread_mutex_unlock(void);
297+
298+
/**
299+
* @deprecated use @ref openthread_mutex_lock.
300+
*
165301
* @brief Lock internal mutex before accessing OT API.
166302
*
167303
* @details OpenThread API is not thread-safe, therefore before accessing any
@@ -170,9 +306,11 @@ int openthread_start(struct openthread_context *ot_context);
170306
*
171307
* @param ot_context Context to lock.
172308
*/
173-
void openthread_api_mutex_lock(struct openthread_context *ot_context);
309+
__deprecated void openthread_api_mutex_lock(struct openthread_context *ot_context);
174310

175311
/**
312+
* @deprecated use @ref openthread_mutex_try_lock instead.
313+
*
176314
* @brief Try to lock internal mutex before accessing OT API.
177315
*
178316
* @details This function behaves like openthread_api_mutex_lock() provided that
@@ -183,14 +321,16 @@ void openthread_api_mutex_lock(struct openthread_context *ot_context);
183321
* @retval 0 On success.
184322
* @retval <0 On failure.
185323
*/
186-
int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
324+
__deprecated int openthread_api_mutex_try_lock(struct openthread_context *ot_context);
187325

188326
/**
327+
* @deprecated use @ref openthread_mutex_unlock instead.
328+
*
189329
* @brief Unlock internal mutex after accessing OT API.
190330
*
191331
* @param ot_context Context to unlock.
192332
*/
193-
void openthread_api_mutex_unlock(struct openthread_context *ot_context);
333+
__deprecated void openthread_api_mutex_unlock(struct openthread_context *ot_context);
194334

195335
/** @cond INTERNAL_HIDDEN */
196336

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)