MeshX 0.3
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
Go to the documentation of this file.
1
13
14#include <meshx_control_task.h>
15
16static void control_task_handler(void *args);
17
22{
23 .max_msg_length = CONFIG_CONTROL_TASK_QUEUE_LEN,
24 .max_msg_depth = sizeof(control_task_msg_t)
25};
26
31
41{
42
43 meshx_task_t task_handle = {
44 .arg = pdev,
45 .task_cb = control_task_handler,
46 .priority = CONFIG_CONTROL_TASK_PRIO,
47 .task_name = CONFIG_CONTROL_TASK_NAME,
49 };
50
51 return meshx_task_create(&task_handle);
52}
53
54/* @brief Publish a control task message.
55 *
56 * This function allows you to publish a control task message with the given
57 * message code, event, and event parameters.
58 * The message will be sent to the control task for processing.
59 *
60 * @param[in] msg_code The message code to publish.
61 * @param[in] msg_evt The event associated with the message.
62 * @param[in] msg_evt_params Pointer to the event parameters.
63 * @param[in] sizeof_msg_evt_params Size of the event parameters.
64 * @return MESHX_SUCCESS on success, or an error code on failure.
65 */
68 const void *msg_evt_params,
69 size_t sizeof_msg_evt_params)
70{
71 control_task_msg_t send_msg;
72 if (msg_code >= CONTROL_TASK_MSG_CODE_MAX)
73 {
74 MESHX_LOGE(MODULE_ID_COMMON, "Invalid message code or event");
75 return MESHX_INVALID_ARG;
76 }
77#if CONFIG_MESHX_DEFAULT_LOG_LEVEL <= MESHX_LOG_DEBUG
78 void *caller_addr0 = __builtin_return_address(0);
79#endif /* CONFIG_MESHX_DEFAULT_LOG_LEVEL <= MESHX_LOG_DEBUG */
80 MESHX_LOGD(MODULE_ID_COMMON, "fn_address|msg|evt: %p|%p|%p", caller_addr0, (void *)msg_code, (void *)msg_evt);
81
82 if (sizeof_msg_evt_params != 0)
83 {
84 meshx_err_t err = meshx_rtos_malloc(&send_msg.msg_evt_params, sizeof_msg_evt_params);
85 if (err)
86 return err;
87 /* Copy the params to allocated space */
88 memcpy(send_msg.msg_evt_params, msg_evt_params, sizeof_msg_evt_params);
89 }
90 else
91 {
92 send_msg.msg_evt_params = NULL;
93 }
94
95 send_msg.msg_code = msg_code;
96 send_msg.msg_evt = msg_evt;
97
98 meshx_err_t send_err = meshx_msg_q_send(&control_task_queue, &send_msg, sizeof(send_msg), UINT32_MAX);
99 if (send_err != MESHX_SUCCESS && send_msg.msg_evt_params)
100 {
102 }
103 return send_err;
104}
105
123 control_task_msg_evt_t evt_bmap,
125{
126 if (callback == NULL || msg_code >= CONTROL_TASK_MSG_CODE_MAX)
127 return MESHX_INVALID_ARG; // Invalid arguments
128
129 control_task_evt_cb_reg_t *new_node = NULL;
130 meshx_err_t err = meshx_rtos_malloc((void**)&new_node, sizeof(control_task_evt_cb_reg_t));
131 if (err || !new_node)
132 return err; // Memory allocation failed
133
134 new_node->cb = callback;
135 new_node->msg_evt_bmap = evt_bmap;
136 new_node->next = control_task_msg_code_list_heads[msg_code];
137 control_task_msg_code_list_heads[msg_code] = new_node;
138
139 return MESHX_SUCCESS;
140}
141
153 control_task_msg_evt_t evt_bmap,
155{
156 if (callback == NULL || msg_code >= CONTROL_TASK_MSG_CODE_MAX)
157 return MESHX_INVALID_ARG; // Invalid arguments
158
159 control_task_evt_cb_reg_t *prev = NULL;
161
162 while (curr)
163 {
164 if (curr->cb == callback && curr->msg_evt_bmap == evt_bmap)
165 {
166 if (prev == NULL)
167 control_task_msg_code_list_heads[msg_code] = curr->next;
168 else
169 prev->next = curr->next;
170
171 return meshx_rtos_free((void**)&curr);
172 }
173 prev = curr;
174 curr = curr->next;
175 }
176
177 return MESHX_NOT_FOUND;
178}
179
193 dev_struct_t *pdev,
196 void *params)
197{
198 if (!pdev)
199 return MESHX_INVALID_ARG;
200
202 bool evt_handled = false;
203
204 if (ptr == NULL)
205 {
206 MESHX_LOGW(MODULE_ID_COMMON, "No control task msg callback registered for msg: %p", (void *)msg_code);
207 return MESHX_INVALID_STATE;
208 }
209
210 MESHX_LOGD(MODULE_ID_COMMON, "msg|evt: %p|%p", (void *)msg_code, (void *)evt);
211
212 while (ptr)
213 {
214 if (((evt == 0x00) || (evt & ptr->msg_evt_bmap)) && (ptr->cb != NULL))
215 {
216 ptr->cb(pdev, evt, params); // Call the registered callback
217 evt_handled = true;
218 }
219 ptr = ptr->next; // Move to the next registration
220 }
221 if (!evt_handled)
222 MESHX_LOGD(MODULE_ID_COMMON, "No handler reg for EVT %p", (void *)evt);
223
224 return MESHX_SUCCESS;
225}
226
238
247static void control_task_handler(void *args)
248{
249 meshx_err_t err;
250 static control_task_msg_t recv_msg;
251 dev_struct_t *pdev = (dev_struct_t *)args;
252
253 MESHX_LOGI(MODULE_ID_COMMON, "Control Task Initialised");
255 if (err)
256 MESHX_LOGE(MODULE_ID_COMMON, "Failed to initialise Control Task Msg Q Err: 0x%x", err);
257
258 while (true)
259 {
260 if (meshx_msg_q_recv(&control_task_queue, &recv_msg, UINT32_MAX) == MESHX_SUCCESS)
261 {
262 err = control_task_msg_dispatch(pdev, recv_msg.msg_code, recv_msg.msg_evt, recv_msg.msg_evt_params);
263 if (err)
264 MESHX_LOGE(MODULE_ID_COMMON, "Err: 0x%x", err);
265 if (recv_msg.msg_evt_params)
266 {
267 /* If Params were passed Free the allocated memory */
269 }
270 }
271 }
272}
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.
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, void *params)
Dispatch a message to the registered callbacks.
meshx_err_t create_control_task(dev_struct_t *pdev)
Create the control task.
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 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.
static void control_task_handler(void *args)
Task handler function for processing control task messages.
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.
static meshx_err_t create_control_task_msg_q(void)
Create the control task message queue.
Header file for the control task in the BLE mesh node application.
enum control_task_msg_code control_task_msg_code_t
Enumeration for control task message codes.
uint32_t control_task_msg_evt_t
Type definition for control task message event.
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.
struct control_task_evt_cb_reg control_task_evt_cb_reg_t
Structure for control task event callback registration.
@ CONTROL_TASK_MSG_CODE_MAX
struct control_task_msg control_task_msg_t
Structure for control task message.
#define CONFIG_CONTROL_TASK_NAME
Control task name configuration.
#define CONFIG_CONTROL_TASK_STACK_SIZE
Control task stack size configuration.
#define CONFIG_CONTROL_TASK_PRIO
Control task priority configuration.
#define CONFIG_CONTROL_TASK_QUEUE_LEN
Control task queue length configuration.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_INVALID_STATE
Definition meshx_err.h:45
@ MESHX_NOT_FOUND
Definition meshx_err.h:46
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:87
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:100
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
meshx_err_t meshx_msg_q_create(meshx_msg_q_t *msg_q_handle)
Create a MeshX Message Queue.
Definition meshx_msg_q.c:26
struct meshx_msg_q meshx_msg_q_t
MeshX Message Queue Structure.
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.
Definition meshx_msg_q.c:80
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.
meshx_err_t meshx_rtos_malloc(void **ptr, size_t size)
Allocates memory dynamically in a thread-safe manner using FreeRTOS.
Definition meshx_utils.c:65
struct meshx_task meshx_task_t
MeshX Task Structure.
meshx_err_t meshx_task_create(meshx_task_t *task_handle)
Create a MeshX Task.
Definition meshx_task.c:28
@ MODULE_ID_COMMON
Definition module_id.h:32
control_task_msg_handle_t cb
struct control_task_evt_cb_reg * next
control_task_msg_evt_t msg_evt
control_task_msg_code_t msg_code