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_gen_light_cli.c
Go to the documentation of this file.
1
15#include "stdlib.h"
16#include "meshx_txcm.h"
17#include "meshx_gen_light_cli.h"
18
19#define MESHX_CLIENT_INIT_MAGIC_NO 0x4309
20
21#if CONFIG_ENABLE_LIGHT_CLIENT
22
34
61
73
80static struct
81{
85
106
133
144{
145 switch (model_id)
146 {
152 return MESHX_SUCCESS;
153 default:
154 return MESHX_FAIL;
155 }
156}
157
194
210{
211 if(params == NULL || msg_param_len != sizeof(meshx_gen_light_client_msg_ctx_t))
212 {
213 return MESHX_INVALID_ARG;
214 }
215 bool is_get_opcode = (meshx_is_gen_light_cli_get_opcode(params->opcode) == MESHX_SUCCESS);
216
218 params->model,
219 &params->state,
220 params->opcode,
221 params->addr,
222 params->net_idx,
223 params->app_idx,
224 is_get_opcode
225 );
226}
227
243 dev_struct_t *pdev,
246)
247{
248 MESHX_UNUSED(evt);
250 meshx_gen_light_cli_cb_reg_t const * reg_cb = NULL;
252
253 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "TXCM Timeout for model 0x%x", param->model_id);
254
255 while (node)
256 {
257 if (param->model_id == node->reg.model_id)
258 {
259 reg_cb = &node->reg;
262 err = reg_cb->cb(pdev, param->model_id, &param->param);
263 }
264 node = node->next;
265 }
266
267 if(reg_cb == NULL)
268 {
269 return MESHX_SUCCESS;
270 }
271
272 return err;
273}
274
286{
288 err = meshx_txcm_request_send(MESHX_TXCM_SIG_ACK, src_addr, NULL, 0, NULL);
289 return err;
290}
291
301{
303 .model_id = model_id
304 };
305
306 memcpy(&ctx.param, param, sizeof(meshx_gen_light_cli_cb_param_t));
307
311 &ctx,
313 NULL);
314}
315
329 dev_struct_t *pdev,
330 control_task_msg_evt_t model_id,
332)
333{
334 if(!param || !pdev)
335 {
336 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid parameters");
337 return MESHX_INVALID_ARG;
338 }
339 meshx_gen_light_cli_cb_reg_t const * reg_cb = NULL;
341
343
344 while (node)
345 {
346 if (model_id == node->reg.model_id)
347 {
348 MESHX_LOGD(MODULE_ID_MODEL_CLIENT, "op|src|dst:%04" PRIx32 "|%04x|%04x",
349 param->ctx.opcode, param->ctx.src_addr, param->ctx.dst_addr);
350 reg_cb = &node->reg;
351 if (param->evt == MESHX_GEN_LIGHT_CLI_TIMEOUT || param->err_code != MESHX_SUCCESS)
352 {
353 MESHX_LOGD(MODULE_ID_MODEL_CLIENT, "Retrying to send the message");
354 err = meshx_gen_light_cli_handle_resend((uint16_t)model_id, param);
355 if(err != MESHX_SUCCESS)
356 {
357 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Resend failed: %d", err);
358 }
359 else
360 {
361 /* As the retry would callback shall be triggered by the TXCM */
363 }
364 }
365 else
366 {
367 /* We need to notify if the ack is from the same source as that of dest */
369 if(err != MESHX_SUCCESS)
370 {
371 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Ack failed: %d", err);
372 }
373 err = reg_cb->cb(pdev, model_id, param);
374 }
375 }
376 node = node->next;
377 }
378
379 if(reg_cb == NULL)
380 {
381 return MESHX_SUCCESS;
382 }
383
384 return err;
385}
386
409
410
424{
425 if (!params || !params->model || !params->state)
426 {
427 return MESHX_INVALID_ARG;
428 }
429
431 bool is_unack = meshx_is_unack_opcode(params->opcode) == MESHX_SUCCESS;
432 /* Broadcast / Multicast will not be sending an ACK. Hence, it is not required to queue */
433 meshx_txcm_sig_t req_type = (is_unack || (MESHX_ADDR_IS_UNICAST(params->addr) == false)) ?
435
437 {
438 .addr = params->addr,
439 .model = params->model,
440 .opcode = params->opcode,
441 .app_idx = params->app_idx,
442 .net_idx = params->net_idx,
443 };
444 memcpy(&send_msg.state, params->state, sizeof(send_msg.state));
445
447 req_type,
448 send_msg.addr,
449 &send_msg,
450 sizeof(send_msg),
452 );
453 if(err)
454 {
455 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Failed to send message: %p", (meshx_ptr_t) err);
456 }
457 return err;
458}
459
473{
474 if (!cb || meshx_is_gen_light_cli_model(model_id) != MESHX_SUCCESS)
475 {
476 return MESHX_INVALID_ARG;
477 }
478
480 meshx_gen_light_cli_cb_reg_t reg = { .model_id = model_id, .cb = cb };
481
483 if (err != MESHX_SUCCESS)
484 {
485 return err;
486 }
487
490 model_id,
492 );
493}
494
495#endif /* CONFIG_ENABLE_LIGHT_CLIENT */
#define MESHX_MODEL_ID_LIGHT_CTL_CLI
#define MESHX_MODEL_OP_LIGHT_XYL_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_LC_PROPERTY_GET
#define MESHX_MODEL_OP_LIGHT_LC_OM_GET
#define MESHX_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK
void * meshx_ptr_t
#define MESHX_MODEL_ID_LIGHT_XYL_CLI
#define MESHX_MODEL_ID_LIGHT_LC_CLI
#define MESHX_MODEL_ID_LIGHT_HSL_CLI
#define MESHX_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_LC_MODE_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_LC_MODE_GET
#define MESHX_ADDR_UNASSIGNED
#define MESHX_MODEL_OP_LIGHT_HSL_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_HSL_GET
#define MESHX_MODEL_OP_LIGHT_XYL_GET
#define MESHX_ADDR_IS_UNICAST(_addr)
#define MESHX_MODEL_OP_LIGHT_CTL_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK
#define MESHX_MODEL_ID_LIGHT_LIGHTNESS_CLI
#define MESHX_MODEL_OP_LIGHT_CTL_GET
#define MESHX_MODEL_OP_LIGHT_LC_OM_SET_UNACK
#define MESHX_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET
#define MESHX_MODEL_OP_LIGHT_LIGHTNESS_GET
meshx_err_t meshx_plat_light_client_send_msg(meshx_ptr_t p_model, meshx_light_client_set_state_t *p_set, uint16_t opcode, uint16_t addr, uint16_t net_idx, uint16_t app_idx, bool is_get_opcode)
Sends a Light Client message over BLE Mesh.
struct meshx_gen_light_cli_cb_param meshx_gen_light_cli_cb_param_t
Callback parameters for Generic Light Client Model events. This structure is used to pass information...
control_task_msg_handle_t meshx_gen_light_client_cb_t
@ MESHX_GEN_LIGHT_CLI_TIMEOUT
meshx_err_t meshx_plat_gen_light_client_init(void)
Initialize the Generic Light Client Model. This function sets up the necessary parameters and resourc...
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
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.
@ CONTROL_TASK_MSG_CODE_FRM_BLE
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.
#define MESHX_UNUSED(x)
Definition meshx_err.h:15
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_FAIL
Definition meshx_err.h:41
@ MESHX_NO_MEM
Definition meshx_err.h:44
@ MESHX_TIMEOUT
Definition meshx_err.h:48
#define MESHX_DO_NOTHING
Definition meshx_err.h:17
#define MESHX_MALLOC
Definition meshx_err.h:24
#define MESHX_CLIENT_INIT_MAGIC_NO
meshx_gen_cli_cb_reg_node_t * cli_cb_reg_head
static meshx_err_t meshx_handle_txcm_msg(dev_struct_t *pdev, control_task_msg_evt_t evt, meshx_gen_cli_resend_ctx *param)
Handles a control task message for a generic client model.
static meshx_err_t meshx_gen_client_txcm_fn_model_send(meshx_gen_client_msg_ctx_t *msg_param, size_t msg_param_len)
Transmit callback handler for sending Generic Client model messages.
meshx_err_t meshx_gen_light_send_msg(const meshx_gen_light_client_send_params_t *params)
Send a message using the generic client model.
static struct @222321174163026111123260040104160030310301145143 g_meshx_gen_light_client_ctrl
meshx_err_t meshx_gen_light_client_from_ble_reg_cb(uint16_t model_id, meshx_gen_light_client_cb_t cb)
Registers a callback function for getting Generic Light Client messages from BLE.
static meshx_err_t meshx_is_gen_light_cli_model(uint32_t model_id)
Checks if the given model ID corresponds to a Generic Light Client model.
static meshx_err_t meshx_gen_light_cli_handle_ack(uint16_t src_addr)
Handles ack request for generic light client messages.
struct meshx_gen_light_cli_cb_reg meshx_gen_light_cli_cb_reg_t
struct meshx_gen_light_cli_cb_reg_node meshx_gen_light_cli_cb_reg_node_t
static meshx_err_t meshx_is_gen_light_cli_get_opcode(uint32_t opcode)
Checks if the given model ID corresponds to a Generic Light Client GET opcode.
static meshx_err_t meshx_gen_client_txcm_fn_model_send(meshx_gen_light_client_msg_ctx_t *params, size_t msg_param_len)
Send a message using the generic client model.
static meshx_err_t meshx_handle_txcm_msg(dev_struct_t *pdev, control_task_msg_evt_t evt, meshx_gen_light_cli_resend_ctx *param)
Handles TXCM message for generic light client.
uint16_t meshx_client_init
static meshx_err_t meshx_is_unack_opcode(uint32_t opcode)
Checks if the given opcode corresponds to an unacknowledged (unack) message in the Generic Light Clie...
static meshx_err_t meshx_gen_light_cli_cb_reg_add(meshx_gen_light_cli_cb_reg_t reg)
Adds a new callback registration to the linked list of registered callbacks.
static meshx_err_t meshx_gen_light_cli_handle_resend(uint16_t model_id, const meshx_gen_light_cli_cb_param_t *param)
Handles resend request for generic light client messages.
static meshx_err_t meshx_handle_gen_light_msg(dev_struct_t *pdev, control_task_msg_evt_t model_id, meshx_gen_light_cli_cb_param_t *param)
Handle the Generic Light Client messages.
struct meshx_gen_light_client_msg_ctx meshx_gen_light_client_msg_ctx_t
meshx_err_t meshx_gen_light_cli_init(void)
Initialize the meshxuction generic client.
Implementation of the MeshX generic light client model for BLE mesh nodes. This file contains functio...
struct meshx_gen_light_client_send_params meshx_gen_light_client_send_params_t
Generic Light Client Model send parameters. This structure is used to pass parameters to the Generic ...
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
MeshX Tx Control Module This header file contains the definitions and function prototypes for the Mes...
meshx_err_t meshx_txcm_request_send(meshx_txcm_sig_t request_type, uint16_t dest_addr, meshx_cptr_t msg_param, uint16_t msg_param_len, meshx_txcm_fn_model_send_t send_fn)
Sends a request to the Tx Control module.
control_task_msg_handle_t meshx_txcm_cb_t
Definition meshx_txcm.h:49
meshx_txcm_sig_t
Enumeration of signal types for the Tx Control Module.
Definition meshx_txcm.h:57
@ MESHX_TXCM_SIG_ENQ_SEND
Definition meshx_txcm.h:58
@ MESHX_TXCM_SIG_DIRECT_SEND
Definition meshx_txcm.h:59
@ MESHX_TXCM_SIG_ACK
Definition meshx_txcm.h:61
@ MESHX_TXCM_SIG_RESEND
Definition meshx_txcm.h:60
meshx_err_t(* meshx_txcm_fn_model_send_t)(meshx_cptr_t msg_param, size_t msg_param_len)
Function pointer the Model client layer needs to provide for the msg to be sent for both MESHX_TXCM_S...
Definition meshx_txcm.h:94
meshx_err_t meshx_txcm_event_cb_reg(meshx_txcm_cb_t event_cb)
Registers a callback function for handling Tx Control module events.
@ MODULE_ID_MODEL_CLIENT
Definition module_id.h:31
uint16_t src_addr
uint16_t dst_addr
Structure containing a node in the linked list of registered callbacks.
struct meshx_gen_light_cli_cb_reg_node * next
meshx_gen_light_cli_cb_reg_t reg
Structure containing the model ID and callback function for generic client model registrations.
meshx_gen_light_client_cb_t cb
Structure containing the model ID and parameter for generic client model message re-sending.
meshx_gen_light_cli_cb_param_t param
Structure containing the message context for generic client model messages.
meshx_light_client_set_state_t state
meshx_light_client_set_state_t * state
Lighting Client Model set message union.