MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_control_task.h
Go to the documentation of this file.
1/**
2 * Copyright © 2024 - 2025 MeshX
3 *
4 * @file meshx_control_task.h
5 * @brief Header file for the control task in the BLE mesh node application.
6 *
7 * This file contains the definitions and function prototypes for the control task,
8 * which handles various control messages and events in the BLE mesh node application.
9 *
10 * @author Pranjal Chanda
11 */
12
13#ifndef __MESHX_CONTROL_TASK__
14#define __MESHX_CONTROL_TASK__
15
16#include "stdio.h"
17#include "string.h"
18#include "meshx_common.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/**
28 * @brief Control task name configuration.
29 */
30#define CONFIG_CONTROL_TASK_NAME "meshx_control_task"
31
32/**
33 * @brief Control task priority configuration.
34 */
35#ifndef CONFIG_CONTROL_TASK_PRIO
36#define CONFIG_CONTROL_TASK_PRIO configTIMER_TASK_PRIORITY + 2
37#endif
38
39/**
40 * @brief Logging task priority configuration.
41 */
42#ifndef CONFIG_MESHX_LOG_TASK_PRIO
43#define CONFIG_MESHX_LOG_TASK_PRIO configTIMER_TASK_PRIORITY + 1
44#endif
45
46/**
47 * @brief Control task stack size configuration.
48 */
49#ifndef CONFIG_CONTROL_TASK_STACK_SIZE
50#define CONFIG_CONTROL_TASK_STACK_SIZE 8192
51#endif
52
53/**
54 * @brief Control task queue length configuration.
55 */
56#ifndef CONFIG_CONTROL_TASK_QUEUE_LEN
57#define CONFIG_CONTROL_TASK_QUEUE_LEN 10
58#endif
59
60/**
61 * @brief Enumeration for control task message codes.
62 */
64{
65 CONTROL_TASK_MSG_CODE_EL_STATE_CH, /**< Message code for Element state change */
66 CONTROL_TASK_MSG_CODE_SYSTEM, /**< Message code for system events. */
67 CONTROL_TASK_MSG_CODE_TO_BLE, /**< Message code to BLE layer. */
68 CONTROL_TASK_MSG_CODE_FRM_BLE, /**< Message code from BLE layer. */
69 CONTROL_TASK_MSG_CODE_CONFIG, /**< Message code for Config Server events */
70 CONTROL_TASK_MSG_CODE_PROVISION, /**< Message code for provisioning events. */
71 CONTROL_TASK_MSG_CODE_PROV_PLAT, /**< Internal message code for platform provisioning events. */
72 CONTROL_TASK_MSG_CODE_TO_APP, /**< Message code for application events. */
73 CONTROL_TASK_MSG_CODE_TO_MESHX, /**< Message code for meshX events from app */
74 CONTROL_TASK_MSG_CODE_TXCM, /**< Message code for TXCM events */
75 CONTROL_TASK_MSG_CODE_MAX, /**< Maximum message code value. */
77
78/**
79 * @brief Type definition for control task message event.
80 */
81typedef uint32_t control_task_msg_evt_t;
82
83/**
84 * @brief Enumeration for control task message events to application.
85 */
87{
88 CONTROL_TASK_MSG_EVT_DATA = MESHX_BIT(0), /**< Data message */
89 CONTROL_TASK_MSG_EVT_CTRL = MESHX_BIT(1), /**< Control message */
90 CONTROL_TASK_MSG_EVT_MAX, /**< Maximum event value */
92
93/**
94 * @brief Enumeration for control task message events to HAL.
95 */
97{
98 CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_ON_OFF = MESHX_BIT(0), /**< Event to set on/off state. */
99 CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_CTL = MESHX_BIT(1), /**< Event to set CTL state. */
100 CONTROL_TASK_MSG_EVT_EL_STATE_CH_MAX, /**< Maximum HAL event value. */
102
103/**
104 * @brief Enumeration for control task message events to BLE.
105 */
106typedef enum __packed control_task_msg_evt_to_ble
107{
108 /* To Client related events */
109 CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF = MESHX_BIT(0), /**< Event to set on/off state. */
110 CONTROL_TASK_MSG_EVT_TO_BLE_SET_CTL = MESHX_BIT(1), /**< Event to set CTL state. */
111 CONTROL_TASK_MSG_EVT_TO_BLE_SET_LIGHTNESS = MESHX_BIT(2), /**< Event to set lightness state. */
112
113 /* To server events */
114 CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF_SRV = MESHX_BIT(16),/**< Event to send out on/off status */
115 CONTROL_TASK_MSG_EVT_TO_BLE_SET_CTL_SRV = MESHX_BIT(17),/**< Event to send out CTL status */
116 CONTROL_TASK_MSG_EVT_TO_BLE_SENSOR_SRV = MESHX_BIT(18),/**< Event to send out Sensor status */
117 CONTROL_TASK_MSG_EVT_TO_BLE_MAX /**< Maximum BLE event value. */
119
120/**
121 * @brief Enumeration for control task system events.
122 */
123typedef enum __packed control_task_msg_evt_system
124{
125 CONTROL_TASK_MSG_EVT_SYSTEM_RESTART = MESHX_BIT(0), /**< Event to restart the system. */
126 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_ARM = MESHX_BIT(1), /**< Event to arm an OS Timer */
127 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_REARM = MESHX_BIT(2), /**< Event to re-arm an OS Timer */
128 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_DISARM = MESHX_BIT(3), /**< Event to stop an OS Timer */
129 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_FIRE = MESHX_BIT(4), /**< Event to fire timedout OS Timer */
130 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_PERIOD = MESHX_BIT(5), /**< Event to set timedout OS Timer */
131 CONTROL_TASK_MSG_EVT_SYSTEM_NVS_COMMIT = MESHX_BIT(6), /**< Event for NVS commit operation */
132 CONTROL_TASK_MSG_EVT_SYSTEM_MAX, /**< Maximum system event value. */
134
135/**
136 * @brief Enumeration for control task config srv events
137 */
138typedef enum __packed control_task_msg_evt_config
139{
140 CONTROL_TASK_MSG_EVT_APP_KEY_ADD = MESHX_BIT(0), /**< Event for adding an application key. */
141 CONTROL_TASK_MSG_EVT_APP_KEY_DEL = MESHX_BIT(1), /**< Event for deleting an application key. */
142 CONTROL_TASK_MSG_EVT_APP_KEY_BIND = MESHX_BIT(2), /**< Event for binding an application key. */
143 CONTROL_TASK_MSG_EVT_APP_KEY_UNBIND = MESHX_BIT(3), /**< Event for unbinding an application key. */
144 CONTROL_TASK_MSG_EVT_SUB_ADD = MESHX_BIT(4), /**< Event for adding a subscription. */
145 CONTROL_TASK_MSG_EVT_SUB_DEL = MESHX_BIT(5), /**< Event for deleting a subscription. */
146 CONTROL_TASK_MSG_EVT_PUB_ADD = MESHX_BIT(6), /**< Event for adding a publication. */
147 CONTROL_TASK_MSG_EVT_PUB_DEL = MESHX_BIT(7), /**< Event for deleting a publication. */
148 CONTROL_TASK_MSG_EVT_NET_KEY_ADD = MESHX_BIT(8), /**< Event for adding a network key. */
149 CONTROL_TASK_MSG_EVT_NET_KEY_DEL = MESHX_BIT(9), /**< Event for deleting a network key. */
150 CONTROL_TASK_MSG_EVT_CONFIG_ALL = 0xFF, /**< Event for all configuration events. */
152
153/**
154 * @brief Enumeration for control task provisioning events.
155 */
157{
158 CONTROL_TASK_MSG_EVT_PROV_REG_COMP = MESHX_BIT(0), /**< ESP_BLE_MESH_PROV_REGISTER_COMP_EVT */
159 CONTROL_TASK_MSG_EVT_SET_NAME_COMP = MESHX_BIT(1), /**< ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT */
160 CONTROL_TASK_MSG_EVT_PROVISION_STOP = MESHX_BIT(2), /**< ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT */
161 CONTROL_TASK_MSG_EVT_IDENTIFY_START = MESHX_BIT(3), /**< EESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT */
162 CONTROL_TASK_MSG_EVT_IDENTIFY_STOP = MESHX_BIT(4), /**< ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT */
163 CONTROL_TASK_MSG_EVT_NODE_RESET = MESHX_BIT(5), /**< CONTROL_TASK_MSG_EVT_NODE_RESET */
164 CONTROL_TASK_MSG_EVT_PROXY_CONNECT = MESHX_BIT(6), /**< ESP_BLE_MESH_PROXY_SERVER_CONNECTED_EVT */
165 CONTROL_TASK_MSG_EVT_PROXY_DISCONN = MESHX_BIT(7), /**< ESP_BLE_MESH_PROXY_SERVER_DISCONNECTED_EVT */
166 CONTROL_TASK_MSG_EVT_EN_NODE_PROV = MESHX_BIT(8), /**< ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT */
167 CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT = MESHX_BIT(9), /**< Event to indicate fresh boot */
168 CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY = MESHX_BIT(10),/**< Event to indicate stack is ready for TX */
169 CONTROL_TASK_MSG_EVT_PROVISION_ALL = 0xFFFF, /**< Maximum provisioning event value. */
171
172/**
173 * @brief Enumeration for control task TXCM events.
174 */
175typedef enum __packed control_task_msg_evt_txcm
176{
177 CONTROL_TASK_MSG_EVT_TXCM_MSG_TIMEOUT = MESHX_BIT(1), /**< Event for TXCM message timeout */
178 CONTROL_TASK_MSG_EVT_TXCM_MAX
180/**
181 * @brief Function pointer type for control task message handler.
182 *
183 * @param pdev Pointer to the device structure.
184 * @param evt Event code.
185 * @param params Pointer to the event parameters.
186 * @return MESHX_SUCCESS on success, or an error code on failure.
187 */
189
190/**
191 * @brief Structure for control task message.
192 */
193typedef struct control_task_msg
194{
195 control_task_msg_code_t msg_code; /**< Message code. */
196 control_task_msg_evt_t msg_evt; /**< Message event. */
197 void *msg_evt_params; /**< Pointer to message event parameters. */
199
200/**
201 * @brief Structure for control task event callback registration.
202 */
204{
205 uint32_t msg_evt_bmap; /**< Bitmap of message events. */
206 control_task_msg_handle_t cb; /**< Callback function pointer. */
207 struct control_task_evt_cb_reg * next; /**< Pointer to the next callback registration. */
209
210/**
211 * @brief Initialize the control task messaging system.
212 *
213 * This function initializes the message queue used by the control task.
214 * It must be called before any component attempts to publish messages.
215 *
216 * @return MESHX_SUCCESS on success, or an error code on failure.
217 */
219
220/**
221 * @brief Create the control task.
222 *
223 * This function creates a FreeRTOS task to handle control events.
224 *
225 * @param[in] pdev Pointer to the device structure (dev_struct_t).
226 * @return MESHX_SUCCESS on success, or an error code on failure.
227 */
229
230/**
231 * @brief Subscribe to a control task message.
232 *
233 * This function allows you to subscribe to a specific control task message
234 * identified by the given message code. When the message is received, the
235 * specified callback function will be invoked.
236 *
237 * @param[in] msg_code The message code to subscribe to.
238 * @param[in] evt_bmap The event bitmap associated with the message.
239 * @param[in] callback The callback function to be called when the message is received.
240 *
241 * @return
242 * - MESHX_SUCCESS: Success
243 * - MESHX_INVALID_ARG: Invalid argument
244 * - MESHX_FAIL: Other failures
245 */
247
248/**
249 * @brief Deregister a callback for a specific message code and event bitmap.
250 *
251 * This function allows deregistering a callback handler for a specific message code and event type.
252 *
253 * @param[in] msg_code The message code to deregister the handler for.
254 * @param[in] evt_bmap Bitmap of events to deregister for.
255 * @param[in] callback Callback function to deregister.
256 * @return MESHX_SUCCESS on success, or an error code on failure.
257 */
259
260/**
261 * @brief Publish a control task message.
262 *
263 * This function allows you to publish a control task message with the given
264 * message code, event, and event parameters.
265 * The message will be sent to the control task for processing.
266 *
267 * @param[in] msg_code The message code to publish.
268 * @param[in] msg_evt The event associated with the message.
269 * @param[in] msg_evt_params Pointer to the event parameters.
270 * @param[in] sizeof_msg_evt_params Size of the event parameters.
271 * @return MESHX_SUCCESS on success, or an error code on failure.
272 */
273meshx_err_t control_task_msg_publish(control_task_msg_code_t msg_code, control_task_msg_evt_t msg_evt, const void *msg_evt_params, size_t sizeof_msg_evt_params);
274
275#ifdef __cplusplus
276}
277#endif
278
279#endif /* __MESHX_CONTROL_TASK__ */
#define MESHX_BIT(nr)
Definition meshx_ble_mesh_cmn_def.h:22
Common application definitions and includes for BLE Mesh Node.
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT
Definition meshx_control_task.h:167
meshx_err_t control_task_init(void)
Initialize the control task messaging system.
Definition meshx_control_task.c:45
CONTROL_TASK_MSG_EVT_IDENTIFY_START
Definition meshx_control_task.h:161
enum __packed control_task_msg_evt_config control_task_msg_evt_config_t
Enumeration for control task config srv events.
CONTROL_TASK_MSG_EVT_TO_BLE_SET_CTL_SRV
Definition meshx_control_task.h:115
CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF_SRV
Definition meshx_control_task.h:114
CONTROL_TASK_MSG_EVT_PROVISION_STOP
Definition meshx_control_task.h:160
enum __packed control_task_msg_evt_to_ble control_task_msg_evt_to_ble_t
Enumeration for control task message events to BLE.
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_FIRE
Definition meshx_control_task.h:129
enum __packed control_task_msg_evt_config
Enumeration for control task config srv events.
Definition meshx_control_task.h:139
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_PERIOD
Definition meshx_control_task.h:130
CONTROL_TASK_MSG_EVT_SYSTEM_MAX
Definition meshx_control_task.h:132
CONTROL_TASK_MSG_EVT_EL_STATE_CH_MAX
Definition meshx_control_task.h:100
CONTROL_TASK_MSG_EVT_TO_BLE_SET_CTL
Definition meshx_control_task.h:110
CONTROL_TASK_MSG_EVT_SYSTEM_NVS_COMMIT
Definition meshx_control_task.h:131
enum control_task_msg_code control_task_msg_code_t
Enumeration for control task message codes.
enum __packed control_task_msg_evt_el_state_ch control_task_msg_evt_el_state_ch_t
Enumeration for control task message events to HAL.
uint32_t control_task_msg_evt_t
Type definition for control task message event.
Definition meshx_control_task.h:81
CONTROL_TASK_MSG_EVT_TO_BLE_SENSOR_SRV
Definition meshx_control_task.h:116
enum __packed control_task_msg_evt_system control_task_msg_evt_system_t
Enumeration for control task system events.
CONTROL_TASK_MSG_EVT_NET_KEY_DEL
Definition meshx_control_task.h:149
CONTROL_TASK_MSG_EVT_APP_KEY_DEL
Definition meshx_control_task.h:141
CONTROL_TASK_MSG_EVT_PROXY_CONNECT
Definition meshx_control_task.h:164
CONTROL_TASK_MSG_EVT_SUB_DEL
Definition meshx_control_task.h:145
meshx_err_t create_control_task(dev_struct_t *pdev)
Create the control task.
Definition meshx_control_task.c:58
enum __packed control_task_msg_evt_el_state_ch
Enumeration for control task message events to HAL.
Definition meshx_control_task.h:97
meshx_err_t(* control_task_msg_handle_t)(dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Function pointer type for control task message handler.
Definition meshx_control_task.h:188
struct control_task_evt_cb_reg control_task_evt_cb_reg_t
Structure for control task event callback registration.
CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_ON_OFF
Definition meshx_control_task.h:98
CONTROL_TASK_MSG_EVT_SYSTEM_RESTART
Definition meshx_control_task.h:125
meshx_err_t control_task_msg_unsubscribe(control_task_msg_code_t msg_code, control_task_msg_evt_t evt_bmap, control_task_msg_handle_t callback)
Deregister a callback for a specific message code and event bitmap.
Definition meshx_control_task.c:172
CONTROL_TASK_MSG_EVT_TXCM_MSG_TIMEOUT
Definition meshx_control_task.h:177
CONTROL_TASK_MSG_EVT_APP_KEY_UNBIND
Definition meshx_control_task.h:143
CONTROL_TASK_MSG_EVT_PROV_REG_COMP
Definition meshx_control_task.h:158
control_task_msg_code
Enumeration for control task message codes.
Definition meshx_control_task.h:64
@ CONTROL_TASK_MSG_CODE_FRM_BLE
Definition meshx_control_task.h:68
@ CONTROL_TASK_MSG_CODE_TXCM
Definition meshx_control_task.h:74
@ CONTROL_TASK_MSG_CODE_PROV_PLAT
Definition meshx_control_task.h:71
@ CONTROL_TASK_MSG_CODE_SYSTEM
Definition meshx_control_task.h:66
@ CONTROL_TASK_MSG_CODE_CONFIG
Definition meshx_control_task.h:69
@ CONTROL_TASK_MSG_CODE_MAX
Definition meshx_control_task.h:75
@ CONTROL_TASK_MSG_CODE_TO_APP
Definition meshx_control_task.h:72
@ CONTROL_TASK_MSG_CODE_TO_MESHX
Definition meshx_control_task.h:73
@ CONTROL_TASK_MSG_CODE_EL_STATE_CH
Definition meshx_control_task.h:65
@ CONTROL_TASK_MSG_CODE_PROVISION
Definition meshx_control_task.h:70
@ CONTROL_TASK_MSG_CODE_TO_BLE
Definition meshx_control_task.h:67
enum __packed control_task_msg_evt_to_ble
Enumeration for control task message events to BLE.
Definition meshx_control_task.h:107
CONTROL_TASK_MSG_EVT_APP_KEY_ADD
Definition meshx_control_task.h:140
struct control_task_msg control_task_msg_t
Structure for control task message.
enum __packed control_task_msg_evt_system
Enumeration for control task system events.
Definition meshx_control_task.h:124
CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY
Definition meshx_control_task.h:168
CONTROL_TASK_MSG_EVT_MAX
Definition meshx_control_task.h:90
CONTROL_TASK_MSG_EVT_IDENTIFY_STOP
Definition meshx_control_task.h:162
enum __packed control_task_msg_evt_to_app_meshx control_task_msg_evt_to_app_meshx_t
Enumeration for control task message events to application.
CONTROL_TASK_MSG_EVT_NODE_RESET
Definition meshx_control_task.h:163
CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_CTL
Definition meshx_control_task.h:99
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_DISARM
Definition meshx_control_task.h:128
CONTROL_TASK_MSG_EVT_PUB_ADD
Definition meshx_control_task.h:146
enum __packed control_task_msg_evt_txcm control_task_msg_evt_txcm_t
Enumeration for control task TXCM events.
CONTROL_TASK_MSG_EVT_EN_NODE_PROV
Definition meshx_control_task.h:166
CONTROL_TASK_MSG_EVT_APP_KEY_BIND
Definition meshx_control_task.h:142
CONTROL_TASK_MSG_EVT_SET_NAME_COMP
Definition meshx_control_task.h:159
CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF
Definition meshx_control_task.h:109
CONTROL_TASK_MSG_EVT_SUB_ADD
Definition meshx_control_task.h:144
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_ARM
Definition meshx_control_task.h:126
meshx_err_t control_task_msg_publish(control_task_msg_code_t msg_code, control_task_msg_evt_t msg_evt, const void *msg_evt_params, size_t sizeof_msg_evt_params)
Publish a control task message.
Definition meshx_control_task.c:84
enum __packed control_task_msg_evt_to_app_meshx
Enumeration for control task message events to application.
Definition meshx_control_task.h:87
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_REARM
Definition meshx_control_task.h:127
CONTROL_TASK_MSG_EVT_CTRL
Definition meshx_control_task.h:89
CONTROL_TASK_MSG_EVT_PROVISION_ALL
Definition meshx_control_task.h:169
CONTROL_TASK_MSG_EVT_DATA
Definition meshx_control_task.h:88
enum __packed control_task_msg_evt_provision
Enumeration for control task provisioning events.
Definition meshx_control_task.h:157
CONTROL_TASK_MSG_EVT_PUB_DEL
Definition meshx_control_task.h:147
enum __packed control_task_msg_evt_provision control_task_msg_evt_provision_t
Enumeration for control task provisioning events.
CONTROL_TASK_MSG_EVT_CONFIG_ALL
Definition meshx_control_task.h:150
CONTROL_TASK_MSG_EVT_PROXY_DISCONN
Definition meshx_control_task.h:165
CONTROL_TASK_MSG_EVT_NET_KEY_ADD
Definition meshx_control_task.h:148
meshx_err_t control_task_msg_subscribe(control_task_msg_code_t msg_code, control_task_msg_evt_t evt_bmap, control_task_msg_handle_t callback)
Subscribe to a control task message.
Definition meshx_control_task.c:142
enum __packed control_task_msg_evt_txcm
Enumeration for control task TXCM events.
Definition meshx_control_task.h:176
CONTROL_TASK_MSG_EVT_TO_BLE_SET_LIGHTNESS
Definition meshx_control_task.h:111
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
MeshX Message Queue Interface.
Utility functions for RTOS operations in the MeshX framework.
MeshX Task Interface.
Structure for control task event callback registration.
Definition meshx_control_task.h:204
control_task_msg_handle_t cb
Definition meshx_control_task.h:206
struct control_task_evt_cb_reg * next
Definition meshx_control_task.h:207
uint32_t msg_evt_bmap
Definition meshx_control_task.h:205
Structure for control task message.
Definition meshx_control_task.h:194
void * msg_evt_params
Definition meshx_control_task.h:197
control_task_msg_evt_t msg_evt
Definition meshx_control_task.h:196
control_task_msg_code_t msg_code
Definition meshx_control_task.h:195