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.c File Reference

Implementation of control task for event handling and messaging. More...

Macros

#define CONFIG_CT_DEBUG_LOG   0

Functions

static void control_task_handler (void *args)
 Task handler function for processing control task messages.
static meshx_err_t create_control_task_msg_q (void)
 Create the control task message queue.
meshx_err_t control_task_init (void)
 Initialize the control task messaging system.
meshx_err_t create_control_task (dev_struct_t *pdev)
 Create the control task.
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.
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.
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.
static meshx_err_t control_task_msg_dispatch (dev_struct_t *pdev, control_task_msg_code_t msg_code, control_task_msg_evt_t evt, const void *params)
 Dispatch a message to the registered callbacks.

Variables

static meshx_msg_q_t control_task_queue
 Queue handle for control task messages.
static control_task_evt_cb_reg_tcontrol_task_msg_code_list_heads [CONTROL_TASK_MSG_CODE_MAX]
 Linked list heads for registered callbacks per message code.

Detailed Description

Implementation of control task for event handling and messaging.

Copyright © 2024 - 2025 MeshX

This file contains the implementation of a control task, including functions for creating the task, sending messages, registering message handlers, and handling events. The control task uses FreeRTOS for inter-task communication and event-driven architecture.

Author
Pranjal Chanda

Macro Definition Documentation

◆ CONFIG_CT_DEBUG_LOG

#define CONFIG_CT_DEBUG_LOG   0

Function Documentation

◆ control_task_handler()

void control_task_handler ( void * args)
static

Task handler function for processing control task messages.

This function runs in a loop, receiving messages from the queue and dispatching them to registered handlers. Allocated memory for message parameters is freed after processing.

Parameters
[in]argsPointer to the device structure (dev_struct_t) passed during task creation.
283{
284 meshx_err_t err;
285 static control_task_msg_t recv_msg;
286 dev_struct_t *pdev = (dev_struct_t *)args;
287
288 MESHX_LOGD(MODULE_ID_COMMON, "Control Task Initialised");
289 MESHX_UNUSED(err);
290
291 while (true)
292 {
293 if (meshx_msg_q_recv(&control_task_queue, &recv_msg, UINT32_MAX) == MESHX_SUCCESS)
294 {
295 err = control_task_msg_dispatch(pdev, recv_msg.msg_code, recv_msg.msg_evt, recv_msg.msg_evt_params);
296 if (err)
297 MESHX_LOGE(MODULE_ID_COMMON, "Err: 0x%x", err);
298 if (recv_msg.msg_evt_params)
299 {
300 /* If Params were passed Free the allocated memory */
302 }
303 }
304 }
305}
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
static meshx_msg_q_t control_task_queue
Queue handle for control task messages.
Definition meshx_control_task.c:26
static meshx_err_t control_task_msg_dispatch(dev_struct_t *pdev, control_task_msg_code_t msg_code, control_task_msg_evt_t evt, const void *params)
Dispatch a message to the registered callbacks.
Definition meshx_control_task.c:212
struct control_task_msg control_task_msg_t
Structure for control task message.
#define MESHX_UNUSED(x)
Definition meshx_err.h:19
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
**This function is called by the parent element when a state change request *is received It validates the request and returns a result to the element not the model layer **return * MESHX_SUCCESS
Definition meshx_model_level.cpp:385
meshx_err_t meshx_msg_q_recv(meshx_msg_q_t *msg_q_handle, void *msg, uint32_t delay_ms)
Receive a Message from a MeshX Message Queue.
meshx_err_t meshx_rtos_free(void **ptr)
Frees memory allocated to a pointer and sets it to NULL.
@ MODULE_ID_COMMON
Definition module_id.h:36
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

◆ control_task_init()

meshx_err_t control_task_init ( void )

Initialize the control task messaging system.

This function initializes the message queue used by the control task. It must be called before any component attempts to publish messages.

Returns
MESHX_SUCCESS on success, or an error code on failure.
46{
48}
static meshx_err_t create_control_task_msg_q(void)
Create the control task message queue.
Definition meshx_control_task.c:269

◆ control_task_msg_dispatch()

meshx_err_t control_task_msg_dispatch ( dev_struct_t * pdev,
control_task_msg_code_t msg_code,
control_task_msg_evt_t evt,
const void * params )
static

Dispatch a message to the registered callbacks.

This function dispatches a message to the registered handlers based on the message code and event type.

Parameters
[in]pdevPointer to the device structure (dev_struct_t).
[in]msg_codeThe message code of the received message.
[in]evtThe event type of the received message.
[in]paramsPointer to the message parameters.
Returns
MESHX_SUCCESS on success, or an error code on failure.
218{
219 if (!pdev || msg_code >= CONTROL_TASK_MSG_CODE_MAX)
220 return MESHX_INVALID_ARG;
221
223 bool evt_handled = false;
224
225 if (ptr == NULL)
226 {
227 MESHX_LOGW(MODULE_ID_COMMON, "No control task msg callback registered for msg: %p", (void *)msg_code);
228 return MESHX_INVALID_STATE;
229 }
230
231#if CONFIG_CT_DEBUG_LOG
232 MESHX_LOGD(MODULE_ID_COMMON, "msg|evt: %p|%p", (void *)msg_code, (void *)evt);
233#endif /* CONFIG_CT_DEBUG_LOG */
234
235 while (ptr)
236 {
237 bool is_match = false;
238 if (msg_code == CONTROL_TASK_MSG_CODE_FRM_BLE)
239 {
240 /* For messages from BLE, the event is the Model ID (value), not a bitmap */
241 is_match = (evt == ptr->msg_evt_bmap);
242 }
243 else
244 {
245 /* Other codes use bitmaps or 0x00 as wildcard */
246 is_match = ((evt == 0x00) || (evt & ptr->msg_evt_bmap));
247 }
248
249 if (is_match && (ptr->cb != NULL))
250 {
251 ptr->cb(pdev, evt, (void*)params); // Call the registered callback
252 evt_handled = true;
253 }
254 ptr = ptr->next;
255 }
256 if (!evt_handled)
257 MESHX_LOGW(MODULE_ID_COMMON, "No handler reg for EVT %p", (void *)evt);
258
259 return MESHX_SUCCESS;
260}
static control_task_evt_cb_reg_t * control_task_msg_code_list_heads[CONTROL_TASK_MSG_CODE_MAX]
Linked list heads for registered callbacks per message code.
Definition meshx_control_task.c:35
struct control_task_evt_cb_reg control_task_evt_cb_reg_t
Structure for control task event callback registration.
@ CONTROL_TASK_MSG_CODE_FRM_BLE
Definition meshx_control_task.h:68
@ CONTROL_TASK_MSG_CODE_MAX
Definition meshx_control_task.h:75
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
@ MESHX_INVALID_STATE
Definition meshx_err.h:49
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:120
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

◆ control_task_msg_publish()

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.

This function allows you to publish a control task message with the given message code, event, and event parameters. The message will be sent to the control task for processing.

Parameters
[in]msg_codeThe message code to publish.
[in]msg_evtThe event associated with the message.
[in]msg_evt_paramsPointer to the event parameters.
[in]sizeof_msg_evt_paramsSize of the event parameters.
Returns
MESHX_SUCCESS on success, or an error code on failure.
88{
89 control_task_msg_t send_msg;
90 if (msg_code >= CONTROL_TASK_MSG_CODE_MAX)
91 {
92 MESHX_LOGE(MODULE_ID_COMMON, "Invalid message code or event");
93 return MESHX_INVALID_ARG;
94 }
95#if CONFIG_CT_DEBUG_LOG
96#if CONFIG_MESHX_DEFAULT_LOG_LEVEL <= MESHX_LOG_DEBUG
97 void *caller_addr0 = __builtin_return_address(0);
98#endif /* CONFIG_MESHX_DEFAULT_LOG_LEVEL <= MESHX_LOG_DEBUG */
99 MESHX_LOGD(MODULE_ID_COMMON, "fn_address|msg|evt: %p|%p|%p", caller_addr0, (void *)msg_code, (void *)msg_evt);
100#endif /* CONFIG_CT_DEBUG_LOG */
101
102 if (sizeof_msg_evt_params != 0)
103 {
104 meshx_err_t err = meshx_rtos_malloc(&send_msg.msg_evt_params, sizeof_msg_evt_params);
105 if (err)
106 return err;
107 /* Copy the params to allocated space */
108 memcpy(send_msg.msg_evt_params, msg_evt_params, sizeof_msg_evt_params);
109 }
110 else
111 {
112 send_msg.msg_evt_params = NULL;
113 }
114
115 send_msg.msg_code = msg_code;
116 send_msg.msg_evt = msg_evt;
117
118 meshx_err_t send_err = meshx_msg_q_send(&control_task_queue, &send_msg, sizeof(send_msg), UINT32_MAX);
119 if (send_err != MESHX_SUCCESS && send_msg.msg_evt_params)
120 {
122 }
123 return send_err;
124}
meshx_err_t meshx_msg_q_send(meshx_msg_q_t *msg_q_handle, void const *msg, size_t msg_len, uint32_t delay_ms)
Send a Message to a MeshX Message Queue Back.
meshx_err_t meshx_rtos_malloc(void **ptr, size_t size)
Allocates memory dynamically in a thread-safe manner using FreeRTOS.

◆ control_task_msg_subscribe()

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.

This function allows you to subscribe to a specific control task message identified by the given message code. When the message is received, the specified callback function will be invoked.

Parameters
[in]msg_codeThe message code to subscribe to.
[in]evt_bmapThe event bitmap associated with the message.
[in]callbackThe callback function to be called when the message is received.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid argument
  • MESHX_FAIL: Other failures
145{
146 if (callback == NULL || msg_code >= CONTROL_TASK_MSG_CODE_MAX)
147 return MESHX_INVALID_ARG; // Invalid arguments
148
149 control_task_evt_cb_reg_t *new_node = NULL;
150 meshx_err_t err = meshx_rtos_malloc((void**)&new_node, sizeof(control_task_evt_cb_reg_t));
151 if (err || !new_node)
152 return err; // Memory allocation failed
153
154 new_node->cb = callback;
155 new_node->msg_evt_bmap = evt_bmap;
156 new_node->next = control_task_msg_code_list_heads[msg_code];
157 control_task_msg_code_list_heads[msg_code] = new_node;
158
159 return MESHX_SUCCESS;
160}

◆ control_task_msg_unsubscribe()

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.

This function allows deregistering a callback handler for a specific message code and event type.

Parameters
[in]msg_codeThe message code to deregister the handler for.
[in]evt_bmapBitmap of events to deregister for.
[in]callbackCallback function to deregister.
Returns
MESHX_SUCCESS on success, or an error code on failure.
175{
176 if (callback == NULL || msg_code >= CONTROL_TASK_MSG_CODE_MAX)
177 return MESHX_INVALID_ARG; // Invalid arguments
178
179 control_task_evt_cb_reg_t *prev = NULL;
181
182 while (curr)
183 {
184 if (curr->cb == callback && curr->msg_evt_bmap == evt_bmap)
185 {
186 if (prev == NULL)
187 control_task_msg_code_list_heads[msg_code] = curr->next;
188 else
189 prev->next = curr->next;
190
191 return meshx_rtos_free((void**)&curr);
192 }
193 prev = curr;
194 curr = curr->next;
195 }
196
197 return MESHX_NOT_FOUND;
198}
@ MESHX_NOT_FOUND
Definition meshx_err.h:50

◆ create_control_task()

meshx_err_t create_control_task ( dev_struct_t * pdev)

Create the control task.

This function creates a FreeRTOS task to handle control events.

Parameters
[in]pdevPointer to the device structure (dev_struct_t).
Returns
MESHX_SUCCESS on success, or an error code on failure.
59{
60
61 meshx_task_t task_handle = {
62 .arg = pdev,
63 .task_cb = control_task_handler,
64 .priority = CONFIG_CONTROL_TASK_PRIO,
65 .task_name = CONFIG_CONTROL_TASK_NAME,
67 };
68
69 return meshx_task_create(&task_handle);
70}
static void control_task_handler(void *args)
Task handler function for processing control task messages.
Definition meshx_control_task.c:282
#define CONFIG_CONTROL_TASK_NAME
Control task name configuration.
Definition meshx_control_task.h:30
#define CONFIG_CONTROL_TASK_STACK_SIZE
Control task stack size configuration.
Definition meshx_control_task.h:50
#define CONFIG_CONTROL_TASK_PRIO
Control task priority configuration.
Definition meshx_control_task.h:36
struct meshx_task meshx_task_t
MeshX Task Structure.
meshx_err_t meshx_task_create(meshx_task_t *task_handle)
Create a MeshX Task.

◆ create_control_task_msg_q()

meshx_err_t create_control_task_msg_q ( void )
static

Create the control task message queue.

This function initializes the FreeRTOS queue for handling control task messages.

Returns
MESHX_SUCCESS on success, or an error code on failure.
270{
272}
meshx_err_t meshx_msg_q_create(meshx_msg_q_t *msg_q_handle)
Create a MeshX Message Queue.

Variable Documentation

◆ control_task_msg_code_list_heads

control_task_evt_cb_reg_t* control_task_msg_code_list_heads[CONTROL_TASK_MSG_CODE_MAX]
static

Linked list heads for registered callbacks per message code.

◆ control_task_queue

meshx_msg_q_t control_task_queue
static
Initial value:
=
{
.max_msg_length = 10 ,
.max_msg_depth = sizeof(control_task_msg_t)
}

Queue handle for control task messages.

26 {
27 .max_msg_length = CONFIG_CONTROL_TASK_QUEUE_LEN,
28 .max_msg_depth = sizeof(control_task_msg_t)
29};
#define CONFIG_CONTROL_TASK_QUEUE_LEN
Control task queue length configuration.
Definition meshx_control_task.h:57