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_client.c
Go to the documentation of this file.
1
15#include "stdlib.h"
16#include "meshx_txcm.h"
17#include "meshx_gen_client.h"
18
19#if CONFIG_ENABLE_GEN_CLIENT
20#define MESHX_CLIENT_INIT_MAGIC_NO 0x1121
32
59
71
78static struct{
79 uint16_t client_init;
82
92{
94 if (!node)
95 return MESHX_NO_MEM;
96
97 node->reg = reg;
98 node->next = g_meshx_client_control.cli_cb_reg_head;
99 g_meshx_client_control.cli_cb_reg_head = node;
100
101 return MESHX_SUCCESS;
102}
103
136
145static meshx_err_t meshx_is_gen_cli_model(uint32_t model_id)
146{
147 switch (model_id)
148 {
155 return MESHX_SUCCESS;
156 default:
157 return MESHX_FAIL;
158 }
159}
160
198
217{
218 if(msg_param == NULL || msg_param_len != sizeof(meshx_gen_client_msg_ctx_t))
219 {
220 return MESHX_INVALID_ARG;
221 }
222
224 msg_param->model,
225 &msg_param->state,
226 msg_param->opcode,
227 msg_param->addr,
228 msg_param->net_idx,
229 msg_param->app_idx,
231 );
232}
233
251 dev_struct_t *pdev,
254)
255{
256 MESHX_UNUSED(evt);
258 meshx_gen_cli_cb_reg_t const * reg_cb = NULL;
259 const meshx_gen_cli_cb_reg_node_t *node = g_meshx_client_control.cli_cb_reg_head;
260
261 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "TXCM Timeout for model 0x%x", param->model_id);
262
263 while (node)
264 {
265 if (param->model_id == node->reg.model_id)
266 {
267 reg_cb = &node->reg;
270 err = reg_cb->cb(pdev, param->model_id, &param->param);
271 }
272 node = node->next;
273 }
274
275 if(reg_cb == NULL)
276 {
277 return MESHX_SUCCESS;
278 }
279
280 return err;
281}
282
293static meshx_err_t meshx_gen_cli_handle_ack(uint16_t src_addr)
294{
296 err = meshx_txcm_request_send(MESHX_TXCM_SIG_ACK, src_addr, NULL, 0, NULL);
297 return err;
298}
299
309{
311 .model_id = model_id
312 };
313 memcpy(&ctx.param, param, sizeof(meshx_gen_cli_cb_param_t));
314
318 &ctx,
320 NULL);
321}
322
336 dev_struct_t *pdev,
337 control_task_msg_evt_t model_id,
339)
340{
341 if(!param || !pdev)
342 {
343 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid parameters");
344 return MESHX_INVALID_ARG;
345 }
346 meshx_gen_cli_cb_reg_t const * reg_cb = NULL;
348
350
351 while (node)
352 {
353 if (model_id == node->reg.model_id)
354 {
355 MESHX_LOGD(MODULE_ID_MODEL_CLIENT, "op|src|dst:%04" PRIx32 "|%04x|%04x",
356 param->ctx.opcode, param->ctx.src_addr, param->ctx.dst_addr);
357 reg_cb = &node->reg;
358 if (param->evt == MESHX_GEN_CLI_TIMEOUT || param->err_code != MESHX_SUCCESS)
359 {
360 MESHX_LOGD(MODULE_ID_MODEL_CLIENT, "Retrying to send the message");
361 err = meshx_gen_cli_handle_resend((uint16_t)model_id, param);
362 if(err != MESHX_SUCCESS)
363 {
364 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Resend failed: %d", err);
365 }
366 else
367 {
368 /* As the retry would callback shall be triggered by the TXCM */
370 }
371 }
372 else
373 {
374 /* We need to notify if the ack is from the same source as that of dest */
376 if(err != MESHX_SUCCESS)
377 {
378 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Ack failed: %d", err);
379 }
380 err = reg_cb->cb(pdev, model_id, param);
381 }
382 }
383 node = node->next;
384 }
385
386 if(reg_cb == NULL)
387 {
388 return MESHX_SUCCESS;
389 }
390
391 return err;
392}
393
415
427{
428 if (!params || !params->model || !params->state)
429 {
430 return MESHX_INVALID_ARG;
431 }
432
434 bool is_unack = meshx_is_unack_opcode(params->opcode) == MESHX_SUCCESS;
435 /* Broadcast / Multicast will not be sending an ACK. Hence, it is not required to queue */
436 meshx_txcm_sig_t req_type = (is_unack || (MESHX_ADDR_IS_UNICAST(params->addr) == false)) ?
438
440 {
441 .addr = params->addr,
442 .model = params->model,
443 .opcode = params->opcode,
444 .app_idx = params->app_idx,
445 .net_idx = params->net_idx,
446 };
447 memcpy(&send_msg.state, params->state, sizeof(send_msg.state));
448
450 req_type,
451 send_msg.addr,
452 &send_msg,
453 sizeof(send_msg),
455 );
456 if(err)
457 {
458 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Failed to send message: %p", (meshx_ptr_t) err);
459 }
460 return err;
461}
462
476{
477 if (!cb || meshx_is_gen_cli_model(model_id) != MESHX_SUCCESS)
478 {
479 return MESHX_INVALID_ARG;
480 }
481
483 meshx_gen_cli_cb_reg_t reg = { .model_id = (uint16_t)model_id, .cb = cb };
484
486 if (err != MESHX_SUCCESS)
487 {
488 return err;
489 }
490
493 model_id,
495 );
496}
497
498#endif /* CONFIG_ENABLE_GEN_CLIENT */
#define MESHX_MODEL_OP_GEN_USER_PROPERTIES_GET
#define MESHX_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_GET
#define MESHX_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK
#define MESHX_MODEL_ID_GEN_POWER_ONOFF_CLI
#define MESHX_MODEL_OP_GEN_BATTERY_GET
#define MESHX_MODEL_OP_GEN_CLIENT_PROPERTIES_GET
void * meshx_ptr_t
#define MESHX_MODEL_ID_GEN_BATTERY_CLI
#define MESHX_MODEL_OP_GEN_LOC_LOCAL_GET
#define MESHX_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK
#define MESHX_MODEL_ID_GEN_ONOFF_CLI
#define MESHX_MODEL_ID_GEN_LEVEL_CLI
#define MESHX_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET
#define MESHX_MODEL_OP_GEN_ONOFF_SET_UNACK
#define MESHX_MODEL_ID_GEN_POWER_LEVEL_CLI
#define MESHX_MODEL_OP_GEN_POWER_LEVEL_GET
#define MESHX_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET_UNACK
#define MESHX_MODEL_OP_GEN_LEVEL_SET_UNACK
#define MESHX_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK
#define MESHX_MODEL_OP_GEN_LEVEL_GET
#define MESHX_MODEL_OP_GEN_USER_PROPERTY_GET
#define MESHX_ADDR_UNASSIGNED
#define MESHX_MODEL_OP_GEN_ADMIN_PROPERTIES_GET
#define MESHX_MODEL_OP_GEN_ONOFF_GET
#define MESHX_MODEL_OP_GEN_ADMIN_PROPERTY_GET
#define MESHX_MODEL_OP_GEN_LOC_GLOBAL_GET
#define MESHX_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK
#define MESHX_MODEL_OP_GEN_ONPOWERUP_GET
#define MESHX_MODEL_ID_GEN_LOCATION_CLI
#define MESHX_ADDR_IS_UNICAST(_addr)
#define MESHX_MODEL_OP_GEN_ONPOWERUP_SET_UNACK
#define MESHX_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK
struct meshx_gen_cli_cb_param meshx_gen_cli_cb_param_t
Callback parameters for Generic Client Model events. This structure is used to pass information about...
control_task_msg_handle_t meshx_gen_client_cb_t
@ MESHX_GEN_CLI_TIMEOUT
meshx_err_t meshx_plat_gen_cli_send_msg(meshx_ptr_t p_model, meshx_gen_cli_set_t *p_set, uint16_t opcode, uint16_t addr, uint16_t net_idx, uint16_t app_idx, bool is_get_opcode)
Sends a Generic Client message over BLE Mesh.
meshx_err_t meshx_plat_gen_cli_init(void)
Initialize the meshxuction generic client.
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
meshx_err_t meshx_gen_client_from_ble_reg_cb(uint32_t model_id, meshx_gen_client_cb_t cb)
Registers a callback function for a specific generic server model.
struct meshx_gen_cli_cb_reg meshx_gen_cli_cb_reg_t
static meshx_err_t meshx_gen_cli_handle_resend(uint16_t model_id, const meshx_gen_cli_cb_param_t *param)
Handles resend request for generic client messages.
struct meshx_gen_client_msg_ctx meshx_gen_client_msg_ctx_t
static meshx_err_t meshx_gen_cli_handle_ack(uint16_t src_addr)
Handles ack request for generic client messages.
#define MESHX_CLIENT_INIT_MAGIC_NO
meshx_err_t meshx_gen_cli_send_msg(meshx_gen_client_send_params_t *params)
Sends a Generic Client message over BLE Mesh.
static meshx_err_t meshx_is_gen_cli_model(uint32_t model_id)
Checks if the given model ID corresponds to a Generic Client model.
uint16_t client_init
static meshx_err_t meshx_handle_gen_onoff_msg(dev_struct_t *pdev, control_task_msg_evt_t model_id, meshx_gen_cli_cb_param_t *param)
Handle the Generic OnOff Client messages.
meshx_gen_cli_cb_reg_node_t * cli_cb_reg_head
static meshx_err_t meshx_is_unack_opcode(uint32_t opcode)
Checks if the given opcode corresponds to an unacknowledged (unack) message.
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.
struct meshx_gen_cli_cb_reg_node meshx_gen_cli_cb_reg_node_t
static meshx_err_t meshx_is_gen_cli_get_opcode(uint16_t opcode)
Checks if the given opcode corresponds to a GET request in the Generic Client group.
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_client_init(void)
Initialize the mesh fuction generic client.
static meshx_err_t meshx_gen_cli_cb_reg_add(meshx_gen_cli_cb_reg_t reg)
Adds a new callback registration to the linked list of registered callbacks.
Implementation of the MeshX generic client model for BLE mesh nodes. This file contains functions for...
struct meshx_gen_client_send_params meshx_gen_client_send_params_t
Generic Client Model send parameters. This structure is used to pass parameters to the Generic Client...
#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
Structure containing control variables for the generic client model.
uint16_t src_addr
uint16_t dst_addr
Structure containing a node in the linked list of registered callbacks.
struct meshx_gen_cli_cb_reg_node * next
meshx_gen_cli_cb_reg_t reg
Structure containing the model ID and callback function for generic client model registrations.
meshx_gen_client_cb_t cb
Structure containing the model ID and parameter for generic client model message re-sending.
meshx_gen_cli_cb_param_t param
Structure containing the message context for generic client model messages.
meshx_gen_cli_set_t state
meshx_gen_cli_set_t * state
Generic Client Model set message union.