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

Implementation of the relay client model for BLE Mesh. More...

#include "meshx_common.h"
#include "meshx_control_task.h"
#include "meshx_nvs.h"
#include "meshx_api.h"
#include "meshx_relay_client_element.h"
#include "meshx_config_server.h"
Include dependency graph for meshx_relay_client.c:

Go to the source code of this file.

Macros

#define CONFIG_SERVER_CB_MASK
 
#define CONTROL_TASK_MSG_CODE_EVT_MASK   CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF
 
#define MOD_SRC   MODULE_ID_ELEMENT_SWITCH_RELAY_CLIENT
 
#define RELAY_CLI_EL_STATE_CH_EVT_MASK   CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_ON_OFF
 
#define GET_RELATIVE_EL_IDX(_element_id)
 
#define IS_EL_IN_RANGE(_element_id)
 
#define RELAY_CLI_EL(_rel_el_id)
 

Enumerations

enum  relay_cli_cmd_t {
  RELAY_CLI_CMD_GET = 0x00 ,
  RELAY_CLI_CMD_SET = 0x01 ,
  RELAY_CLI_CMD_SET_UNACK = 0x02 ,
  RELAY_CLI_MAX_CMD
}
 

Functions

static meshx_err_t meshx_element_struct_init (uint16_t n_max)
 Initializes the mesh element structure.
 
static meshx_err_t meshx_element_struct_deinit (void)
 Deinitializes the mesh element structure.
 
static meshx_err_t meshx_dev_create_relay_model_space (dev_struct_t const *pdev, uint16_t n_max)
 Create Dynamic Relay Model Elements.
 
static meshx_err_t meshx_add_relay_cli_model_to_element_list (dev_struct_t *pdev, uint16_t *start_idx, uint16_t n_max)
 Add relay client models to the element list.
 
static meshx_err_t relay_client_config_cli_cb (const dev_struct_t *pdev, control_task_msg_evt_t evt, const meshx_config_srv_cb_param_t *params)
 Callback function for configuration client events.
 
static meshx_err_t relay_cli_prov_srv_msg_handle (const dev_struct_t *pdev, control_task_msg_evt_t evt, const void *params)
 Relay Client Provisioning Server Control Task Message Handler.
 
static meshx_err_t meshx_relay_cli_send_onoff_msg (const dev_struct_t *pdev, uint16_t element_id, uint8_t set_get, uint8_t ack)
 Sends a relay message over BLE mesh.
 
static meshx_err_t meshx_relay_client_element_state_change_handler (const dev_struct_t *pdev, control_task_msg_evt_t evt, const meshx_on_off_cli_el_msg_t *param)
 Handles the relay client element message.
 
static meshx_err_t meshx_relay_client_to_ble_handler (const dev_struct_t *pdev, control_task_msg_evt_t evt, const meshx_gen_on_off_cli_msg_t *msg)
 Relay Client Element Application Request Handler.
 
static meshx_err_t relay_cli_unit_test_cb_handler (int cmd_id, int argc, char **argv)
 Callback handler for the Relay client unit test command.
 
static meshx_err_t meshx_relay_cli_reg_app_req_cb ()
 Registers a callback handler for relay application requests.
 
static meshx_err_t meshx_relay_cli_el_state_change_reg_cb ()
 Registers a callback for relay element state change events.
 
meshx_err_t meshx_relay_el_set_state (uint16_t el_id, bool ack)
 Set the On/Off state for a specific element in the BLE mesh network.
 
meshx_err_t meshx_relay_el_get_state (uint16_t el_id)
 Retrieves the current state of the relay element.
 
meshx_err_t create_relay_client_elements (dev_struct_t *pdev, uint16_t element_cnt)
 Create Dynamic Relay Model Elements.
 
 REG_MESHX_ELEMENT_FN (relay_cli_el, MESHX_ELEMENT_TYPE_RELAY_CLIENT, create_relay_client_elements)
 

Variables

static relay_client_element_ctrl_t relay_element_init_ctrl
 

Detailed Description

Implementation of the relay client model for BLE Mesh.

Copyright (c) 2024 - 2025 MeshX

This file contains the implementation of the relay client model for BLE Mesh, including initialization, configuration, and event handling.

Author
Pranjal Chanda

Definition in file meshx_relay_client.c.

Macro Definition Documentation

◆ CONFIG_SERVER_CB_MASK

#define CONFIG_SERVER_CB_MASK
Value:

Definition at line 24 of file meshx_relay_client.c.

24#define CONFIG_SERVER_CB_MASK \
25 CONTROL_TASK_MSG_EVT_PUB_ADD \
26 | CONTROL_TASK_MSG_EVT_SUB_ADD \
27 | CONTROL_TASK_MSG_EVT_APP_KEY_BIND

◆ CONTROL_TASK_MSG_CODE_EVT_MASK

#define CONTROL_TASK_MSG_CODE_EVT_MASK   CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF

Definition at line 28 of file meshx_relay_client.c.

◆ GET_RELATIVE_EL_IDX

#define GET_RELATIVE_EL_IDX ( _element_id)
Value:
_element_id - relay_element_init_ctrl.element_id_start
static relay_client_element_ctrl_t relay_element_init_ctrl

Definition at line 34 of file meshx_relay_client.c.

◆ IS_EL_IN_RANGE

#define IS_EL_IN_RANGE ( _element_id)
Value:
(_element_id >= relay_element_init_ctrl.element_id_start && _element_id < relay_element_init_ctrl.element_id_end)

Definition at line 35 of file meshx_relay_client.c.

◆ MOD_SRC

Definition at line 31 of file meshx_relay_client.c.

◆ RELAY_CLI_EL

#define RELAY_CLI_EL ( _rel_el_id)
Value:
relay_element_init_ctrl.el_list[_rel_el_id]

Definition at line 36 of file meshx_relay_client.c.

◆ RELAY_CLI_EL_STATE_CH_EVT_MASK

#define RELAY_CLI_EL_STATE_CH_EVT_MASK   CONTROL_TASK_MSG_EVT_EL_STATE_CH_SET_ON_OFF

Definition at line 32 of file meshx_relay_client.c.

Enumeration Type Documentation

◆ relay_cli_cmd_t

Enumerator
RELAY_CLI_CMD_GET 0x00 
RELAY_CLI_CMD_SET 0x01 
RELAY_CLI_CMD_SET_UNACK 0x02 
RELAY_CLI_MAX_CMD  

Definition at line 491 of file meshx_relay_client.c.

492{
493 RELAY_CLI_CMD_GET = 0x00,
494 RELAY_CLI_CMD_SET = 0x01,
relay_cli_cmd_t
@ RELAY_CLI_CMD_GET
@ RELAY_CLI_CMD_SET
@ RELAY_CLI_CMD_SET_UNACK
@ RELAY_CLI_MAX_CMD

Function Documentation

◆ create_relay_client_elements()

meshx_err_t create_relay_client_elements ( dev_struct_t * pdev,
uint16_t element_cnt )

Create Dynamic Relay Model Elements.

Parameters
[in]pdevPointer to device structure
[in]element_cntMaximum number of relay models
Returns
meshx_err_t

Definition at line 635 of file meshx_relay_client.c.

636{
637 meshx_err_t err;
638
639 err = meshx_dev_create_relay_model_space(pdev, element_cnt);
640 if (err)
641 {
642 MESHX_LOGE(MOD_SRC, "Relay Model space create failed: (%d)", err);
643 return err;
644 }
645
646 err = meshx_add_relay_cli_model_to_element_list(pdev, (uint16_t *)&pdev->element_idx, element_cnt);
647 if (err)
648 {
649 MESHX_LOGE(MOD_SRC, "Relay Model add to element create failed: (%d)", err);
650 return err;
651 }
652
653#if CONFIG_ENABLE_CONFIG_SERVER
657 if (err)
658 {
659 MESHX_LOGE(MOD_SRC, "Relay Model config client callback reg failed: (%d)", err);
660 return err;
661 }
662#endif /* CONFIG_ENABLE_CONFIG_SERVER */
664 if (err)
665 {
666 MESHX_LOGE(MOD_SRC, "Relay Client app req callback reg failed: (%d)", err);
667 return err;
668 }
671 if (err)
672 {
673 MESHX_LOGE(MOD_SRC, "Relay Client freshboot callback reg failed: (%d)", err);
674 return err;
675 }
677 if (err)
678 {
679 MESHX_LOGE(MOD_SRC, "Relay Client element state change callback reg failed: (%d)", err);
680 return err;
681 }
682#if CONFIG_ENABLE_UNIT_TEST
684 if (err)
685 {
686 MESHX_LOGE(MOD_SRC, "unit_test reg failed: (%d)", err);
687 return err;
688 }
689#endif /* CONFIG_ENABLE_UNIT_TEST */
690
691 err = meshx_on_off_client_init();
692 if (err)
693 {
694 MESHX_LOGE(MOD_SRC, "meshx_onoff_client_init failed: (%d)", err);
695 return err;
696 }
697
698 return MESHX_SUCCESS;
699}
control_task_msg_handle_t config_srv_cb_t
control_task_msg_handle_t prov_srv_cb_t
meshx_err_t meshx_config_server_cb_reg(config_srv_cb_t cb, uint32_t config_evt_bmap)
Registers a configuration server callback for specific events.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
#define CONFIG_SERVER_CB_MASK
Configuration server callback event mask for cwww server.
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
meshx_err_t meshx_prov_srv_reg_el_client_cb(prov_srv_cb_t cb)
Register a callback function for provisioning events.
#define MOD_SRC
static meshx_err_t meshx_add_relay_cli_model_to_element_list(dev_struct_t *pdev, uint16_t *start_idx, uint16_t n_max)
Add relay client models to the element list.
static meshx_err_t relay_cli_unit_test_cb_handler(int cmd_id, int argc, char **argv)
Callback handler for the Relay client unit test command.
static meshx_err_t meshx_relay_cli_reg_app_req_cb()
Registers a callback handler for relay application requests.
static meshx_err_t relay_cli_prov_srv_msg_handle(const dev_struct_t *pdev, control_task_msg_evt_t evt, const void *params)
Relay Client Provisioning Server Control Task Message Handler.
static meshx_err_t relay_client_config_cli_cb(const dev_struct_t *pdev, control_task_msg_evt_t evt, const meshx_config_srv_cb_param_t *params)
Callback function for configuration client events.
static meshx_err_t meshx_relay_cli_el_state_change_reg_cb()
Registers a callback for relay element state change events.
static meshx_err_t meshx_dev_create_relay_model_space(dev_struct_t const *pdev, uint16_t n_max)
Create Dynamic Relay Model Elements.
size_t element_idx
meshx_err_t register_unit_test(module_id_t module_id, module_callback_t callback)
Register a unit test for a specific module.
Definition unit_test.c:149
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_add_relay_cli_model_to_element_list()

static meshx_err_t meshx_add_relay_cli_model_to_element_list ( dev_struct_t * pdev,
uint16_t * start_idx,
uint16_t n_max )
static

Add relay client models to the element list.

Registers the relay client models to the BLE Mesh element list.

Parameters
[in]pdevPointer to the device structure.
[in,out]start_idxPointer to the start index of elements.
[in]n_maxMaximum number of elements to add.
Returns
MESHX_SUCCESS on success, error code otherwise.

Definition at line 172 of file meshx_relay_client.c.

174{
175 if (!pdev || !start_idx || !n_max)
176 return MESHX_INVALID_STATE;
177
178 if ((n_max + *start_idx) > CONFIG_MAX_ELEMENT_COUNT)
179 {
180 MESHX_LOGE(MOD_SRC, "No of elements limit reached");
181 return MESHX_NO_MEM;
182 }
183
185 relay_element_init_ctrl.element_id_start = *start_idx;
186
187 for (uint16_t i = *start_idx; i < (n_max + *start_idx); i++)
188 {
189 if (i == 0)
190 continue;
192 i,
193 pdev->elements,
194 RELAY_CLI_EL(i - *start_idx).relay_cli_sig_model_list,
195 NULL,
198 if (err)
199 {
200 MESHX_LOGE(MOD_SRC, "Failed to add element to composition: (%d)", err);
201 return err;
202 }
204 i,
205 RELAY_CLI_EL(i - *start_idx).cli_ctx,
207 if (err != MESHX_SUCCESS)
208 {
209 MESHX_LOGW(MOD_SRC, "Failed to get relay cli element context: (0x%x)", err);
210 }
211 }
212 /* Increment the index for further registrations */
213 relay_element_init_ctrl.element_id_end = *start_idx += n_max;
214 return MESHX_SUCCESS;
215}
meshx_err_t meshx_plat_add_element_to_composition(uint16_t index, meshx_ptr_t p_element_list, meshx_ptr_t p_sig_models, meshx_ptr_t p_ven_models, uint8_t sig_cnt, uint8_t ven_cnt)
Adds an element to the BLE Mesh composition.
#define CONFIG_MAX_ELEMENT_COUNT
Total Element Count in the Composition.
@ MESHX_INVALID_STATE
Definition meshx_err.h:45
@ MESHX_NO_MEM
Definition meshx_err.h:44
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:87
meshx_err_t meshx_nvs_element_ctx_get(uint16_t element_id, void *blob, size_t blob_size)
Retrieve the context of a specific element from NVS.
Definition meshx_nvs.c:424
#define RELAY_CLI_EL(_rel_el_id)
#define RELAY_CLI_MODEL_VEN_CNT
Number of Vendor models in a relay model element.
struct meshx_relay_client_model_ctx meshx_relay_client_model_ctx_t
Structure to hold the context of the relay client.
#define RELAY_CLI_MODEL_SIG_CNT
Number of SIG models in a relay model element.
MESHX_ELEMENT elements[MAX_ELE_CNT]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_dev_create_relay_model_space()

static meshx_err_t meshx_dev_create_relay_model_space ( dev_struct_t const * pdev,
uint16_t n_max )
static

Create Dynamic Relay Model Elements.

Parameters
[in]pdevPointer to device structure
[in]n_maxMaximum number of relay models
Returns
meshx_err_t

Definition at line 145 of file meshx_relay_client.c.

146{
147 if (!pdev)
148 return MESHX_INVALID_STATE;
149
150 /* Assign Spaces for Model List, Publish List and onoff gen list */
152 if (err)
153 {
154 MESHX_LOGE(MOD_SRC, "Failed to initialize relay element structures: (%d)", err);
156 return err;
157 }
158 return MESHX_SUCCESS;
159}
static meshx_err_t meshx_element_struct_deinit(void)
Deinitializes the mesh element structure.
static meshx_err_t meshx_element_struct_init(uint16_t n_max)
Initializes the mesh element structure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_element_struct_deinit()

static meshx_err_t meshx_element_struct_deinit ( void )
static

Deinitializes the mesh element structure.

This function deinitializes the mesh element structure by freeing the allocated memory.

Returns
meshx_err_t
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_STATE: Invalid state
  • MESHX_ERR: Error occurred during deinitialization

Definition at line 106 of file meshx_relay_client.c.

107{
108 if (!relay_element_init_ctrl.element_cnt || !relay_element_init_ctrl.el_list)
109 {
110 MESHX_LOGE(MOD_SRC, "Relay element list not initialized");
111 return MESHX_INVALID_STATE;
112 }
113
114 meshx_err_t err;
115
116 for (size_t i = 0; i < relay_element_init_ctrl.element_cnt; i++)
117 {
118 if (RELAY_CLI_EL(i).cli_ctx)
119 {
120 MESHX_FREE(RELAY_CLI_EL(i).cli_ctx);
121 RELAY_CLI_EL(i).cli_ctx = NULL;
122 }
123 err = meshx_on_off_client_delete(&RELAY_CLI_EL(i).onoff_cli_model);
124 if (err)
125 MESHX_LOGE(MOD_SRC, "Meshx On Off Server delete failed (Err : 0x%x)", err);
126 }
127
128 if (relay_element_init_ctrl.el_list)
129 {
131 relay_element_init_ctrl.el_list = NULL;
132 }
133
134 return MESHX_SUCCESS;
135}
#define MESHX_FREE
Definition meshx_err.h:32
Here is the caller graph for this function:

◆ meshx_element_struct_init()

static meshx_err_t meshx_element_struct_init ( uint16_t n_max)
static

Initializes the mesh element structure.

This function initializes the mesh element structure with the specified maximum number of elements.

Parameters
n_maxThe maximum number of elements to initialize.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid argument

Definition at line 51 of file meshx_relay_client.c.

52{
53 if (!n_max)
54 return MESHX_INVALID_ARG;
55
56 if (relay_element_init_ctrl.el_list)
57 {
58 MESHX_LOGW(MOD_SRC, "Relay element list already initialized");
60 }
61
63
64 relay_element_init_ctrl.element_cnt = n_max;
65 relay_element_init_ctrl.element_id_end = 0;
66 relay_element_init_ctrl.element_id_start = 0;
67
70
71 if (!relay_element_init_ctrl.el_list)
72 return MESHX_NO_MEM;
73
74 for (size_t i = 0; i < relay_element_init_ctrl.element_cnt; i++)
75 {
76 RELAY_CLI_EL(i).cli_ctx =
78
79 if (!RELAY_CLI_EL(i).cli_ctx)
80 return MESHX_NO_MEM;
81
82 err = meshx_on_off_client_create(&RELAY_CLI_EL(i).onoff_cli_model,
83 &RELAY_CLI_EL(i).relay_cli_sig_model_list[RELAY_CLI_SIG_ONOFF_MODEL_ID]);
84 if (err)
85 {
86 MESHX_LOGE(MOD_SRC, "Meshx On Off Client create failed (Err : 0x%x)", err);
87 return err;
88 }
89 RELAY_CLI_EL(i).onoff_cli_model->meshx_sig
90 = &RELAY_CLI_EL(i).relay_cli_sig_model_list[RELAY_CLI_SIG_ONOFF_MODEL_ID];
91 }
92
93 return MESHX_SUCCESS;
94}
#define MESHX_CALOC
Definition meshx_err.h:28
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ RELAY_CLI_SIG_ONOFF_MODEL_ID
struct relay_client_element relay_client_elements_t
Structure to hold the context and configuration for the relay client element.
Here is the caller graph for this function:

◆ meshx_relay_cli_el_state_change_reg_cb()

static meshx_err_t meshx_relay_cli_el_state_change_reg_cb ( )
static

Registers a callback for relay element state change events.

This function subscribes the provided callback to control task messages related to relay element state changes. It ensures the callback is valid before subscribing.

Returns

Definition at line 567 of file meshx_relay_client.c.

568{
573 );
574}
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_EL_STATE_CH
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 meshx_relay_client_element_state_change_handler(const dev_struct_t *pdev, control_task_msg_evt_t evt, const meshx_on_off_cli_el_msg_t *param)
Handles the relay client element message.
#define RELAY_CLI_EL_STATE_CH_EVT_MASK
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_relay_cli_reg_app_req_cb()

static meshx_err_t meshx_relay_cli_reg_app_req_cb ( )
static

Registers a callback handler for relay application requests.

This function subscribes the provided callback to control task messages related to BLE events. It ensures the callback is valid before subscribing.

Returns

Definition at line 549 of file meshx_relay_client.c.

550{
555 );
556}
@ CONTROL_TASK_MSG_CODE_TO_BLE
#define CONTROL_TASK_MSG_CODE_EVT_MASK
static meshx_err_t meshx_relay_client_to_ble_handler(const dev_struct_t *pdev, control_task_msg_evt_t evt, const meshx_gen_on_off_cli_msg_t *msg)
Relay Client Element Application Request Handler.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_relay_cli_send_onoff_msg()

static meshx_err_t meshx_relay_cli_send_onoff_msg ( const dev_struct_t * pdev,
uint16_t element_id,
uint8_t set_get,
uint8_t ack )
static

Sends a relay message over BLE mesh.

This function sends a relay message to a specified element in the BLE mesh network.

Parameters
[in]pdevPointer to the device structure.
[in]element_idThe ID of the element to which the message is sent.
[in]set_getIndicates whether the message is a set (0) or get (1) operation.
[in]ackIndicates whether an acknowledgment is required (1) or not (0).
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid argument
  • MESHX_FAIL: Sending message failed

Definition at line 338 of file meshx_relay_client.c.

343{
344 if (!pdev || !IS_EL_IN_RANGE(element_id))
345 return MESHX_INVALID_ARG;
346
348 size_t rel_el_id = GET_RELATIVE_EL_IDX(element_id);
349 meshx_onoff_client_model_t *model = RELAY_CLI_EL(rel_el_id).onoff_cli_model;
350
351 meshx_relay_client_model_ctx_t *el_ctx = RELAY_CLI_EL(rel_el_id).cli_ctx;
352 uint16_t opcode = MESHX_MODEL_OP_GEN_ONOFF_GET;
353
354 if (MESHX_GEN_ON_OFF_CLI_MSG_SET == set_get)
355 {
357 }
358
359 MESHX_LOGD(MOD_SRC, "OPCODE: %p", (void *)(uint32_t)opcode);
360
361 meshx_gen_onoff_send_params_t params = {
362 .element_id = element_id,
363 .model = model,
364 .opcode = opcode,
365 .addr = el_ctx->pub_addr,
366 .net_idx = pdev->meshx_store.net_key_id,
367 .app_idx = el_ctx->app_id,
368 .state = el_ctx->state.on_off,
369 .tid = el_ctx->tid
370 };
371 /* Send message to the relay client */
372 err = meshx_onoff_client_send_msg(&params);
373 if (err)
374 {
375 MESHX_LOGE(MOD_SRC, "Relay Client Send Message failed: (%d)", err);
376 }
377 else
378 {
379 el_ctx->tid++;
381 {
382 el_ctx->prev_state.on_off = el_ctx->state.on_off;
383 el_ctx->state.on_off = !el_ctx->state.on_off;
384 }
385 }
386 return err;
387}
#define MESHX_MODEL_OP_GEN_ONOFF_SET_UNACK
#define MESHX_MODEL_OP_GEN_ONOFF_GET
#define MESHX_MODEL_OP_GEN_ONOFF_SET
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
#define GET_RELATIVE_EL_IDX(_element_id)
#define IS_EL_IN_RANGE(_element_id)
meshx_app_store_t meshx_store
uint16_t net_key_id
Here is the caller graph for this function:

◆ meshx_relay_client_element_state_change_handler()

static meshx_err_t meshx_relay_client_element_state_change_handler ( const dev_struct_t * pdev,
control_task_msg_evt_t evt,
const meshx_on_off_cli_el_msg_t * param )
static

Handles the relay client element message.

Note
CONTROL_TASK_MSG_CODE_EL_STATE_CH

This function processes the relay client element message and updates the state of the relay client model accordingly.

Parameters
[in]pdevPointer to the device structure.
[in]evtEvent type of the control task message.
[in]paramPointer to the parameters of the control task message.
Returns
meshx_err_t

Definition at line 401 of file meshx_relay_client.c.

406{
407 MESHX_UNUSED(pdev);
408 MESHX_UNUSED(evt);
409 uint16_t element_id = param->model.el_id;
410 if (!IS_EL_IN_RANGE(element_id))
411 {
412 return MESHX_SUCCESS;
413 }
414
415 size_t rel_el_id = GET_RELATIVE_EL_IDX(element_id);
416 meshx_relay_client_model_ctx_t *el_ctx = RELAY_CLI_EL(rel_el_id).cli_ctx;
419 bool nvs_save = false;
420 err = meshx_gen_on_off_state_change_handle(param, &el_ctx->prev_state, &el_ctx->state);
421 if(err == MESHX_SUCCESS)
422 {
423 app_notify.err_code = param->err_code;
424 app_notify.on_off = el_ctx->prev_state.on_off;
425
426 err = meshx_send_msg_to_app(element_id,
430 &app_notify);
431 if (err != MESHX_SUCCESS)
432 {
433 MESHX_LOGE(MOD_SRC, "Failed to send Relay state change message: (%d)", err);
434 }
435 }
436 if (nvs_save)
437 {
438 err = meshx_nvs_element_ctx_set(element_id, el_ctx, sizeof(meshx_relay_client_model_ctx_t));
439 if (err != MESHX_SUCCESS)
440 {
441 MESHX_LOGE(MOD_SRC, "Failed to set Relay element context: (%d)", err);
442 }
443 }
444 return MESHX_SUCCESS;
445}
#define MESHX_ELEMENT_FUNC_ID_RELAY_SERVER_ONN_OFF
Definition meshx_api.h:21
meshx_err_t meshx_send_msg_to_app(uint16_t element_id, uint16_t element_type, uint16_t func_id, uint16_t msg_len, const void *msg)
Sends a message to the BLE Mesh application.
Definition meshx_api.c:96
struct meshx_api_relay_client_state meshx_api_relay_client_evt_t
Structure defines the payload for MESHX_ELEMENT_TYPE_RELAY_CLIENT.
@ MESHX_ELEMENT_TYPE_RELAY_CLIENT
Definition meshx_api.h:45
#define MESHX_UNUSED(x)
Definition meshx_err.h:15
meshx_err_t meshx_nvs_element_ctx_set(uint16_t element_id, const void *blob, size_t blob_size)
Store the context of a specific element to NVS.
Definition meshx_nvs.c:444
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_relay_client_to_ble_handler()

static meshx_err_t meshx_relay_client_to_ble_handler ( const dev_struct_t * pdev,
control_task_msg_evt_t evt,
const meshx_gen_on_off_cli_msg_t * msg )
static

Relay Client Element Application Request Handler.

This function handles the relay client application requests for setting or getting the On/Off state of the relay element.

Parameters
[in]pdevPointer to the device structure.
[in]evtEvent type of the control task message.
[in]msgPointer to the parameters of the control task message.
Returns
meshx_err_t

Definition at line 458 of file meshx_relay_client.c.

463{
465
466 if (!pdev || !msg || !IS_EL_IN_RANGE(msg->element_id))
467 return MESHX_INVALID_ARG;
468
470 {
471 MESHX_LOGW(MOD_SRC, "No publish address set for element: %d", msg->element_id);
472 return MESHX_INVALID_STATE;
473 }
475 {
477 msg->element_id,
478 msg->set_get,
479 msg->ack);
480 if (err)
481 {
483 MOD_SRC,
484 "Relay Client Control Task: Set OnOff failed (%p)", (void *)err);
485 }
486 }
487 return err;
488}
#define MESHX_ADDR_UNASSIGNED
CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF
static meshx_err_t meshx_relay_cli_send_onoff_msg(const dev_struct_t *pdev, uint16_t element_id, uint8_t set_get, uint8_t ack)
Sends a relay message over BLE mesh.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_relay_el_get_state()

meshx_err_t meshx_relay_el_get_state ( uint16_t el_id)

Retrieves the current state of the relay element.

This function constructs a generic On/Off client message to request the current state of a relay element identified by the given element ID. It then publishes this message to the control task for BLE communication.

Parameters
[in]el_idThe element ID of the relay whose state is to be retrieved.
Returns
meshx_err_t Returns the result of the message publish operation.

Definition at line 613 of file meshx_relay_client.c.

614{
616 .ack = MESHX_GEN_ON_OFF_CLI_MSG_ACK,
617 .set_get = MESHX_GEN_ON_OFF_CLI_MSG_GET,
618 .element_id = el_id
619 };
623 &msg,
624 sizeof(msg));
625}
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.
struct meshx_gen_on_off_cli_msg meshx_gen_on_off_cli_msg_t
Structure to hold the relay client message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_relay_el_set_state()

meshx_err_t meshx_relay_el_set_state ( uint16_t el_id,
bool ack )

Set the On/Off state for a specific element in the BLE mesh network.

This function sets the On/Off state of the specified element, optionally waiting for an acknowledgment.

Parameters
[in]el_idThe ID of the element for which to set the On/Off state.
[in]ackWhether to wait for an acknowledgment (true) or not (false).
Returns
  • MESHX_SUCCESS: Successfully set the state.
  • MESHX_INVALID_ARG: Invalid element ID or parameters.
  • MESHX_FAIL: Failed to set the state.

Definition at line 589 of file meshx_relay_client.c.

590{
592 .ack = ack == MESHX_GEN_ON_OFF_CLI_MSG_ACK,
593 .set_get = MESHX_GEN_ON_OFF_CLI_MSG_SET,
594 .element_id = el_id
595 };
599 &msg,
600 sizeof(msg));
601}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ REG_MESHX_ELEMENT_FN()

REG_MESHX_ELEMENT_FN ( relay_cli_el ,
MESHX_ELEMENT_TYPE_RELAY_CLIENT ,
create_relay_client_elements  )
Here is the call graph for this function:

◆ relay_cli_prov_srv_msg_handle()

static meshx_err_t relay_cli_prov_srv_msg_handle ( const dev_struct_t * pdev,
control_task_msg_evt_t evt,
const void * params )
static

Relay Client Provisioning Server Control Task Message Handler.

This function handles the CW-WW client control task messages.

Parameters
[in]pdevPointer to the device structure.
[in]evtEvent type of the control task message.
[in]paramsPointer to the parameters of the control task message.
Returns
meshx_err_t

Definition at line 294 of file meshx_relay_client.c.

295{
296 if(!pdev)
297 return MESHX_INVALID_ARG;
298
299 MESHX_UNUSED(params);
300 size_t rel_el_id = 0;
301 switch (evt)
302 {
304 for (size_t i = relay_element_init_ctrl.element_id_start; i < relay_element_init_ctrl.element_id_end; i++)
305 {
306 rel_el_id = GET_RELATIVE_EL_IDX(i);
307 if(false == (RELAY_CLI_EL(rel_el_id).element_model_init
309 {
310 MESHX_LOGD(MOD_SRC, "Sending GET for model: 0");
311 return meshx_relay_el_get_state((uint16_t) i);
312 }
313 }
314 break;
315 default:
316 MESHX_LOGW(MOD_SRC, "Unhandled event: %p", (void *)evt);
317 break;
318 }
319
320 return MESHX_SUCCESS;
321}
#define MESHX_BIT(nr)
CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT
meshx_err_t meshx_relay_el_get_state(uint16_t el_id)
Retrieves the current state of the relay element.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ relay_cli_unit_test_cb_handler()

static meshx_err_t relay_cli_unit_test_cb_handler ( int cmd_id,
int argc,
char ** argv )
static

Callback handler for the Relay client unit test command.

This function handles the Relay client unit test command by processing the provided command ID and arguments.

Parameters
[in]cmd_idThe command ID to be processed.
[in]argcThe number of arguments provided.
[in]argvThe array of arguments.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid arguments
  • Other error codes depending on the implementation

Definition at line 514 of file meshx_relay_client.c.

515{
517 MESHX_LOGD(MOD_SRC, "argc|cmd_id: %d|%d", argc, cmd_id);
518 if (argc < 1 || cmd_id >= RELAY_CLI_MAX_CMD)
519 {
521 MOD_SRC, "Relay Client Unit Test: Invalid number of arguments");
522 return MESHX_INVALID_ARG;
523 }
524
525 relay_cli_cmd_t cmd = (relay_cli_cmd_t)cmd_id;
526 uint8_t set_get = (cmd == RELAY_CLI_CMD_GET) ? MESHX_GEN_ON_OFF_CLI_MSG_GET : MESHX_GEN_ON_OFF_CLI_MSG_SET;
527 uint16_t el_id = UT_GET_ARG(0, uint16_t, argv);
528
529 err = !set_get ? meshx_relay_el_set_state(el_id, (cmd == RELAY_CLI_CMD_SET)) :
531 if (err)
532 {
533 MESHX_LOGE(MOD_SRC, "Relay Client Unit Test: Command %d failed", cmd);
534 }
535 return err;
536}
meshx_err_t meshx_relay_el_set_state(uint16_t el_id, bool ack)
Set the On/Off state for a specific element in the BLE mesh network.
meshx_err_t meshx_relay_el_get_state(uint16_t el_id)
Retrieves the current state of the relay element.
#define UT_GET_ARG(_x, _type, _argv)
Macro to extract an argument from the argument list.
Definition unit_test.h:27
Here is the call graph for this function:
Here is the caller graph for this function:

◆ relay_client_config_cli_cb()

static meshx_err_t relay_client_config_cli_cb ( const dev_struct_t * pdev,
control_task_msg_evt_t evt,
const meshx_config_srv_cb_param_t * params )
static

Callback function for configuration client events.

This function handles events from the configuration client, such as model publication and application binding events.

Parameters
[in]pdevPointer to the device structure.
[in]evtEvent type of the control task message.
[in]paramsPointer to the parameters of the control task message.
Returns
meshx_err_t

Definition at line 231 of file meshx_relay_client.c.

235{
236 MESHX_UNUSED(pdev);
237 meshx_relay_client_model_ctx_t *el_ctx = NULL;
238 size_t rel_el_id = 0;
239 uint16_t element_id = 0;
240 uint16_t base_el_id = 0;
241 bool nvs_save = false;
242 meshx_get_base_element_id(&base_el_id);
243
244 MESHX_LOGD(MODULE_ID_ELEMENT_SWITCH_RELAY_CLIENT, "EVT: %p", (void *)evt);
245 switch (evt)
246 {
248 element_id = params->state_change.mod_app_bind.element_addr - base_el_id;
249 if (!IS_EL_IN_RANGE(element_id))
250 break;
251 rel_el_id = GET_RELATIVE_EL_IDX(element_id);
252 el_ctx = RELAY_CLI_EL(rel_el_id).cli_ctx;
253 el_ctx->app_id = params->state_change.mod_app_bind.app_idx;
254 nvs_save = true;
255 break;
258 element_id = params->state_change.mod_pub_set.element_addr - base_el_id;
259 if (!IS_EL_IN_RANGE(element_id))
260 break;
261 rel_el_id = GET_RELATIVE_EL_IDX(element_id);
262 el_ctx = RELAY_CLI_EL(rel_el_id).cli_ctx;
265 el_ctx->app_id = params->state_change.mod_pub_set.app_idx;
266 nvs_save = true;
267 MESHX_LOGI(MOD_SRC, "PUB_ADD: %d, %d, 0x%X, 0x%X", element_id, rel_el_id, el_ctx->pub_addr, el_ctx->app_id);
268 break;
269 default:
270 break;
271 }
272 if (nvs_save)
273 {
275 if (err != MESHX_SUCCESS)
276 {
277 MESHX_LOGE(MOD_SRC, "Failed to set relay element context: (%d)", err);
278 }
279 }
280 return MESHX_SUCCESS;
281}
meshx_err_t meshx_get_base_element_id(uint16_t *base_el_id)
Retrieves the base element ID for the BLE Mesh platform.
CONTROL_TASK_MSG_EVT_PUB_DEL
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:100
@ MODULE_ID_ELEMENT_SWITCH_RELAY_CLIENT
Definition module_id.h:24
meshx_cfg_srv_state_change_t state_change
meshx_state_change_cfg_model_app_bind_t mod_app_bind
meshx_state_change_cfg_mod_pub_set_t mod_pub_set
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ relay_element_init_ctrl

relay_client_element_ctrl_t relay_element_init_ctrl
static

Definition at line 38 of file meshx_relay_client.c.