Skip to content

Commit 201d928

Browse files
adigiekartben
authored andcommitted
openthread: Add header for OpenThread module
* Add header with public API of OpenThread module. * Add module include directory to Zephyr include directories. * Use new header in platform implementation. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 463f518 commit 201d928

File tree

5 files changed

+171
-6
lines changed

5 files changed

+171
-6
lines changed

modules/openthread/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ zephyr_library_sources(
279279
openthread.c
280280
)
281281
zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_SHELL shell.c)
282+
zephyr_include_directories(include)
282283

283284
add_subdirectory(platform)
284285

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_MODULES_OPENTHREAD_OPENTHREAD_H_
8+
#define ZEPHYR_MODULES_OPENTHREAD_OPENTHREAD_H_
9+
10+
#include <zephyr/kernel.h>
11+
12+
#include <openthread/instance.h>
13+
#include <openthread/message.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
/**
20+
* @brief The common callback type for receiving IPv4 (translated by NAT64) and IPv6 datagrams.
21+
*
22+
* This callback is called when a datagram is received.
23+
*
24+
* @param message The message to receive.
25+
* @param context The context to pass to the callback.
26+
*/
27+
typedef void (*openthread_receive_cb)(struct otMessage *message, void *context);
28+
29+
/** OpenThread state change callback */
30+
31+
/**
32+
* @brief OpenThread state change callback structure
33+
*
34+
* Used to register a callback in the callback list. As many
35+
* callbacks as needed can be added as long as each of them
36+
* are unique pointers of struct openthread_state_changed_callback.
37+
*
38+
* @note You may destroy the object only after it is unregistered from the callback list.
39+
*/
40+
struct openthread_state_changed_callback {
41+
/**
42+
* @brief Callback for notifying configuration or state changes.
43+
*
44+
* @param otCallback OpenThread callback to register.
45+
* See https://openthread.io/reference/group/api-instance#otstatechangedcallback for
46+
* details.
47+
*/
48+
otStateChangedCallback otCallback;
49+
50+
/** User data if required */
51+
void *user_data;
52+
53+
/**
54+
* Internally used field for list handling
55+
* - user must not directly modify
56+
*/
57+
sys_snode_t node;
58+
};
59+
60+
/**
61+
* @brief Register callbacks that will be called when a certain configuration
62+
* or state changes occur within OpenThread.
63+
*
64+
* @param cb Callback struct to register.
65+
*/
66+
int openthread_state_changed_callback_register(struct openthread_state_changed_callback *cb);
67+
68+
/**
69+
* @brief Unregister OpenThread configuration or state changed callbacks.
70+
*
71+
* @param cb Callback struct to unregister.
72+
*/
73+
int openthread_state_changed_callback_unregister(struct openthread_state_changed_callback *cb);
74+
75+
/**
76+
* @brief Get OpenThread thread identification.
77+
*/
78+
k_tid_t openthread_thread_id_get(void);
79+
80+
/**
81+
* @brief Get pointer to default OpenThread instance.
82+
*
83+
* @retval !NULL On success.
84+
* @retval NULL On failure.
85+
*/
86+
struct otInstance *openthread_get_default_instance(void);
87+
88+
/**
89+
* @brief Initialize the OpenThread module.
90+
*
91+
* This function:
92+
* - Initializes the OpenThread module.
93+
* - Creates an OpenThread single instance.
94+
* - Starts the shell.
95+
* - Enables the UART and NCP HDLC for coprocessor purposes.
96+
* - Initializes the NAT64 translator.
97+
* - Creates a work queue for the OpenThread module.
98+
*
99+
* @note This function is automatically called by Zephyr's networking layer.
100+
* If you want to initialize the OpenThread independently, call this function
101+
* in your application init code.
102+
*
103+
* @retval 0 On success.
104+
* @retval -EIO On failure.
105+
*/
106+
int openthread_init(void);
107+
108+
/**
109+
* @brief Run the OpenThread network.
110+
*
111+
* @details Prepares the OpenThread network and enables it.
112+
* Depends on active settings: it uses the stored network configuration,
113+
* starts the joining procedure or uses the default network configuration.
114+
* Additionally, when the device is MTD, it sets the SED mode to properly
115+
* attach the network.
116+
*/
117+
int openthread_run(void);
118+
119+
/**
120+
* @brief Disable the OpenThread network.
121+
*/
122+
int openthread_stop(void);
123+
124+
/**
125+
* @brief Set the additional callback for receiving packets.
126+
*
127+
* @details This callback is called once a packet is received and can be
128+
* used to inject packets into the Zephyr networking stack.
129+
* Setting this callback is optional.
130+
*
131+
* @param cb Callback to set.
132+
* @param context Context to pass to the callback.
133+
*/
134+
void openthread_set_receive_cb(openthread_receive_cb cb, void *context);
135+
136+
/**
137+
* @brief Lock internal mutex before accessing OpenThread API.
138+
*
139+
* @details OpenThread API is not thread-safe. Therefore, before accessing any
140+
* API function, you need to lock the internal mutex, to prevent the
141+
* OpenThread thread from pre-empting the API call.
142+
*/
143+
void openthread_mutex_lock(void);
144+
145+
/**
146+
* @brief Try to lock internal mutex before accessing OpenThread API.
147+
*
148+
* @details This function behaves like openthread_mutex_lock(), provided that
149+
* the internal mutex is unlocked. Otherwise, it returns a negative value without
150+
* waiting.
151+
*/
152+
int openthread_mutex_try_lock(void);
153+
154+
/**
155+
* @brief Unlock internal mutex after accessing OpenThread API.
156+
*/
157+
void openthread_mutex_unlock(void);
158+
159+
#ifdef __cplusplus
160+
}
161+
#endif
162+
163+
#endif /* ZEPHYR_MODULES_OPENTHREAD_OPENTHREAD_H_ */

modules/openthread/openthread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ LOG_MODULE_REGISTER(net_openthread_platform, CONFIG_OPENTHREAD_PLATFORM_LOG_LEVE
1717
#include <zephyr/init.h>
1818
#include <zephyr/version.h>
1919
#include <zephyr/sys/check.h>
20-
#include <zephyr/net/openthread.h>
2120

2221
#include "platform/platform-zephyr.h"
2322

23+
#include <openthread.h>
24+
2425
#include <openthread/child_supervision.h>
2526
#include <openthread/cli.h>
2627
#include <openthread/ip6.h>

modules/openthread/platform/ble.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
#include <zephyr/bluetooth/uuid.h>
2020
#include <zephyr/bluetooth/gatt.h>
2121

22-
/* Zephyr OpenThread integration Library */
23-
#include <zephyr/net/openthread.h>
22+
#include <openthread.h>
2423

2524
/* OpenThread BLE driver API */
2625
#include <openthread/error.h>
@@ -163,7 +162,7 @@ static void ot_plat_ble_thread(void *unused1, void *unused2, void *unused3)
163162
ring_buf_get(&ot_plat_ble_ring_buf, ot_plat_ble_msg_buf, len);
164163
}
165164

166-
openthread_api_mutex_lock(openthread_get_default_context());
165+
openthread_mutex_lock();
167166

168167
if (len <= PLAT_BLE_MSG_DATA_MAX) {
169168
/* The packet parameter in otPlatBleGattServerOnWriteRequest is not const.
@@ -178,7 +177,7 @@ static void ot_plat_ble_thread(void *unused1, void *unused2, void *unused3)
178177
} else if (len == PLAT_BLE_MSG_DISCONNECT) {
179178
otPlatBleGapOnDisconnected(ble_openthread_instance, 0);
180179
}
181-
openthread_api_mutex_unlock(openthread_get_default_context());
180+
openthread_mutex_unlock();
182181
}
183182
}
184183

modules/openthread/shell.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
#include <zephyr/kernel.h>
88
#include <stdio.h>
9-
#include <zephyr/net/openthread.h>
109
#include <zephyr/sys/printk.h>
1110
#include <zephyr/shell/shell.h>
1211
#include <zephyr/shell/shell_uart.h>
1312

13+
#include <openthread.h>
14+
1415
#include <openthread/cli.h>
1516
#include <openthread/instance.h>
1617

0 commit comments

Comments
 (0)