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_relay_element.cpp File Reference

Implementation of MeshX Relay Element. This file contains the implementation of the MeshX Relay Element class, which represents a relay element in the MeshX BLE mesh network. Key Features: More...

Macros

#define CONFIG_SERVER_CB_MASK
#define RELAY_SRV_TO_BLE_EVT_MASK   CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF_SRV
#define RELAY_CLI_TO_BLE_EVT_MASK   CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF

Functions

meshx_err_t relay_cli_ut_handler (int cmd_id, int argc, char **argv)
meshx_err_t relay_srv_ut_handler (int cmd_id, int argc, char **argv)
meshx_err_t create_relay_server_elements_cpp (dev_struct_t *pdev, uint16_t element_cnt)
 C++ factory for Relay Server elements.
meshx_err_t create_relay_client_elements_cpp (dev_struct_t *pdev, uint16_t element_cnt)
 C++ factory for Relay Client elements.
meshx_err_t meshx_relay_el_set_state (uint16_t el_id, bool ack)
meshx_err_t meshx_relay_el_get_state (uint16_t el_id)

Detailed Description

Implementation of MeshX Relay Element. This file contains the implementation of the MeshX Relay Element class, which represents a relay element in the MeshX BLE mesh network. Key Features:

  • Implements relay element functionality
  • Inherits from meshXElementServer / meshXElementClient
  • Automatically initializes required SIG models for relay elements
  • Maintains state context for NVS persistence (mirrors C el_ctx)
  • Config server callback for app-key bind and publication address
  • Provisioning event handling (server: re-publish; client: send GET)
  • TO_BLE client send path for app-driven OnOff commands
Author
Pranjal Chanda
Date
2024-2025

Macro Definition Documentation

◆ CONFIG_SERVER_CB_MASK

#define CONFIG_SERVER_CB_MASK
Value:
CONTROL_TASK_MSG_EVT_PUB_ADD
Definition meshx_control_task.h:146
CONTROL_TASK_MSG_EVT_APP_KEY_BIND
Definition meshx_control_task.h:142
CONTROL_TASK_MSG_EVT_PUB_DEL
Definition meshx_control_task.h:147
35#define CONFIG_SERVER_CB_MASK \
36 CONTROL_TASK_MSG_EVT_PUB_ADD \
37 | CONTROL_TASK_MSG_EVT_PUB_DEL \
38 | CONTROL_TASK_MSG_EVT_APP_KEY_BIND

◆ RELAY_CLI_TO_BLE_EVT_MASK

#define RELAY_CLI_TO_BLE_EVT_MASK   CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF

◆ RELAY_SRV_TO_BLE_EVT_MASK

#define RELAY_SRV_TO_BLE_EVT_MASK   CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF_SRV

Function Documentation

◆ create_relay_client_elements_cpp()

meshx_err_t create_relay_client_elements_cpp ( dev_struct_t * pdev,
uint16_t element_cnt )

C++ factory for Relay Client elements.

Constructs CONFIG_RELAY_CLIENT_COUNT meshXRelayClientElement objects and registers each with the platform BLE Mesh composition.

Parameters
pdevPointer to the device structure.
element_cntNumber of relay client elements to create. Capped by CONFIG_RELAY_CLIENT_COUNT.
Returns
MESHX_SUCCESS on success, error code otherwise.
407{
409 pdev,
410 element_cnt,
411 CONFIG_RELAY_CLIENT_COUNT,
413 "Relay Cli"
414 );
415}
meshx_err_t meshx_element_factory_helper(dev_struct_t *pdev, uint16_t requested_cnt, uint16_t max_cnt, module_id_t module_id, const char *log_name)
Helper function to create and register mesh elements.
Definition meshx_element_factory.hpp:33
@ MODULE_ID_ELEMENT_SWITCH_RELAY_CLIENT
Definition module_id.h:28

◆ create_relay_server_elements_cpp()

meshx_err_t create_relay_server_elements_cpp ( dev_struct_t * pdev,
uint16_t element_cnt )

C++ factory for Relay Server elements.

Constructs CONFIG_RELAY_SERVER_COUNT meshXRelayServerElement objects and registers each with the platform BLE Mesh composition.

Parameters
pdevPointer to the device structure.
element_cntNumber of relay server elements to create. Capped by CONFIG_RELAY_SERVER_COUNT.
Returns
MESHX_SUCCESS on success, error code otherwise.
383{
385 pdev,
386 element_cnt,
387 CONFIG_RELAY_SERVER_COUNT,
389 "Relay Srv"
390 );
391}
@ MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER
Definition module_id.h:32

◆ meshx_relay_el_get_state()

meshx_err_t meshx_relay_el_get_state ( uint16_t el_id)
448{
452 msg.reserved = 0;
453 msg.element_id = el_id;
454
458 &msg,
459 sizeof(msg));
460}
#define MESHX_GEN_ON_OFF_CLI_MSG_ACK
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:28
#define MESHX_GEN_ON_OFF_CLI_MSG_GET
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:27
struct meshx_gen_on_off_cli_msg meshx_gen_on_off_cli_msg_t
Standard Relay Client Message structure (shared between C element and C++ model).
@ CONTROL_TASK_MSG_CODE_TO_BLE
Definition meshx_control_task.h:67
CONTROL_TASK_MSG_EVT_TO_BLE_SET_ON_OFF
Definition meshx_control_task.h:109
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.
Definition meshx_control_task.c:84
uint8_t set_get
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:21
uint16_t element_id
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:23
uint8_t reserved
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:22
uint8_t ack
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:20

◆ meshx_relay_el_set_state()

meshx_err_t meshx_relay_el_set_state ( uint16_t el_id,
bool ack )
425{
427 el_id, MESHX_ELEMENT_TYPE_RELAY_CLIENT);
428 if (!el) return MESHX_NOT_FOUND;
429
431 msg.ack = (uint8_t)(ack ? 1 : 0);
433 msg.reserved = 0;
434 msg.element_id = el_id;
435
436 // We use the model's next_on_off state internally as the target for the SET operation.
437 // This allows the UT command to set the target state in the context before triggering.
438 msg.on_off = el->get_ctx().gen_on_off_state.next_on_off;
439
443 &msg,
444 sizeof(msg));
445}
#define MESHX_GEN_ON_OFF_CLI_MSG_SET
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:26
T * find_and_cast(uint16_t element_idx, meshx_element_type_t expected_variant)
Find and cast an element instance.
Definition meshx_element_registry.hpp:84
static meshXElementRegistry & get_instance()
Definition meshx_element_registry.hpp:33
Definition meshx_relay_element.hpp:129
@ MESHX_NOT_FOUND
Definition meshx_err.h:50
uint8_t on_off
Definition ble_mesh/elements/inc/variants/meshx_relay_msg_defs.h:22

◆ relay_cli_ut_handler()

meshx_err_t relay_cli_ut_handler ( int cmd_id,
int argc,
char ** argv )
463{
464 MESHX_LOGI(MODULE_ID_ELEMENT_SWITCH_RELAY_CLIENT, "Entering C++ Relay Client UT Handler: cmd_id=%d, argc=%d", cmd_id, argc);
465 if (argc < 1) return MESHX_INVALID_ARG;
466 uint16_t el_id = UT_GET_ARG(0, uint16_t, argv);
467
468 if (cmd_id == 0x01) { // SET
469 return meshx_relay_el_set_state(el_id, true);
470 } else if (cmd_id == 0x02) { // SET UNACK
471 return meshx_relay_el_set_state(el_id, false);
472 } else { // GET
473 return meshx_relay_el_get_state(el_id);
474 }
475}
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
meshx_err_t meshx_relay_el_get_state(uint16_t el_id)
Definition meshx_relay_element.cpp:447
meshx_err_t meshx_relay_el_set_state(uint16_t el_id, bool ack)
Definition meshx_relay_element.cpp:424
#define UT_GET_ARG(_x, _type, _argv)
Macro to extract an argument from the argument list.
Definition unit_test.h:31

◆ relay_srv_ut_handler()

meshx_err_t relay_srv_ut_handler ( int cmd_id,
int argc,
char ** argv )
478{
479 MESHX_LOGI(MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER, "Entering C++ Relay Server UT Handler: cmd_id=%d, argc=%d", cmd_id, argc);
480 // Server UT can be used to query local state
481 if (argc < 1) return MESHX_INVALID_ARG;
482 uint16_t el_id = UT_GET_ARG(0, uint16_t, argv);
483
485 el_id, MESHX_ELEMENT_TYPE_RELAY_SERVER);
486 if (!el) return MESHX_NOT_FOUND;
487
488 if (cmd_id == 0x01) {
489 uint8_t expected = UT_GET_ARG(1, uint8_t, argv);
490 uint8_t actual = el->get_state();
491 MESHX_LOGI(MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER, "Relay Server [%d] state: %d, expected: %d", el_id, actual, expected);
492 return (actual == expected) ? MESHX_SUCCESS : MESHX_FAIL;
493 }
494
495 MESHX_LOGI(MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER, "Relay Server [%d] state: %d", el_id, el->get_state());
496 return MESHX_SUCCESS;
497}
Definition meshx_relay_element.hpp:52
@ MESHX_FAIL
Definition meshx_err.h:45
**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