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
meshXRelayServerElement Class Reference

#include <meshx_relay_element.hpp>

Inheritance diagram for meshXRelayServerElement:
meshXElementServer meshXElement< meshx_srv_model_send_param_header_t > meshXElementIF

Public Member Functions

 meshXRelayServerElement (uint16_t element_idx)
 Constructs a new meshXRelayServerElement instance. The meshXRelayServerElement represents a relay element in the MeshX BLE mesh network. It automatically initializes and configures all required SIG models for the relay element.
 meshXRelayServerElement (void)=delete
uint8_t get_state () const
 Get the current OnOff state of the relay server.
Public Member Functions inherited from meshXElementServer
 meshXElementServer ()=default
 meshXElementServer (uint16_t element_idx, uint8_t no_of_sig_models=0, uint8_t no_of_ven_models=0)
Public Member Functions inherited from meshXElement< meshx_srv_model_send_param_header_t >
void set_element_type (meshxElementType_t type)
meshxElementType_t get_element_type (void) const final
 Get the element type.
void set_element_variant (meshx_element_type_t variant)
meshx_element_type_t get_element_variant (void) const final
 Get the element variant.
void set_no_of_sig_models (uint8_t cnt)
uint8_t get_no_of_sig_models (void) const final
 Get the number of SIG models supported by the element.
void set_no_of_ven_models (uint8_t cnt)
uint8_t get_no_of_ven_models (void) const final
 Get the number of Vendor models supported by the element.
std::vector< std::unique_ptr< meshXModelIF > > & get_sig_models (void) final
 Get the SIG models vector.
std::vector< std::unique_ptr< meshXModelIF > > & get_ven_models (void) final
 Get the Vendor models vector.
void register_element_ctx (meshx_ptr_t ctx, size_t ctx_size)
 Register element context structure.
meshx_ptr_t get_element_ctx (void) const override
 Get the element context structure.
size_t get_element_ctx_size (void) const override
 Get the size of the element context structure.
meshx_err_t sig_plat_model_array_allocate (void)
 Allocate memory for SIG model platform array.
meshx_err_t ven_plat_model_array_allocate (void)
 Allocate memory for Vendor model platform array.
std::vector< uint8_t > & get_sig_model_array (void)
 Get the SIG model array.
std::vector< uint8_t > & get_ven_model_array (void)
 Get the Vendor model array.
meshx_err_t add_sig_model (ConstructorsArgs &&... args)
 Add a SIG model to the element.
meshx_err_t add_ven_model (ConstructorsArgs &&... args)
 Add a Vendor model to the element.
uint8_t get_sig_model_count (void) const
 Get the number of SIG models currently added to the element.
uint8_t get_ven_model_count (void) const
 Get the number of Vendor models currently added to the element.
meshx_err_t add_sig_models (void)
 Add multiple SIG models to the element.
meshx_err_t add_ven_models (void)
 Add multiple Vendor models to the element.
meshx_err_t add_models (void)
 Add both SIG and Vendor models to the element.
MESHX_MODEL * get_sig_plat_model_array (void)
 Get the platform model array for SIG models.
MESHX_MODEL * get_ven_plat_model_array (void)
 Get the platform model array for Vendor models.
 meshXElement (void)
 Default constructor for meshXElement.
meshx_err_t initialize (void) override
 Initialize the element.
meshx_err_t reset (void) override
 Reset the element.
bool is_initialized (void) const override
 Check if the element is initialized.
meshx_err_t restore_nvs_context (void) override
 Restore the element's context from NVS.
bool has_model (uint16_t model_id) const override
 Check if the element contains a specific model by ID.
void on_baked (uint16_t index) override
 Called when the composition is baked to update the element's index.
 ~meshXElement () override
Public Member Functions inherited from meshXElementIF
void set_element_idx (uint16_t idx)
uint16_t get_element_idx (void) const
 meshXElementIF ()=delete
 meshXElementIF (uint16_t element_idx)
virtual ~meshXElementIF ()=default

Private Member Functions

uint8_t list_sig_models (void) override
 Lists and initializes SIG models for Relay Server Element.
uint8_t list_ven_models (void) override
 Lists Vendor models for Relay Server Element (none required).
const char * get_element_name (void) const override
 Get the name of the element.
meshx_err_t element_state_change_notify (meshx_ptr_t param, size_t param_size) override
 Notify element of state change from child model.
void sync (control_task_msg_evt_t evt) override
 Synchronize element state (Status broadcast).
void handle_config (control_task_msg_evt_t evt, const meshx_config_srv_cb_param_t *params) override
 Handle configuration server events.

Static Private Member Functions

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

Private Attributes

meshx_relay_srv_el_ctx_t element_ctx

Additional Inherited Members

Static Public Member Functions inherited from meshXElement< meshx_srv_model_send_param_header_t >
static meshx_err_t static_prov_srv_cb (const dev_struct_t *pdev, control_task_msg_evt_t evt, const void *params)
 Global static provisioning callback for Server elements.
static meshx_err_t static_prov_cli_cb (const dev_struct_t *pdev, control_task_msg_evt_t evt, const void *params)
 Global static provisioning callback for Client elements.
static meshx_err_t static_config_cb (const dev_struct_t *pdev, control_task_msg_evt_t evt, const meshx_config_srv_cb_param_t *params)
 Global static configuration callback.
static void register_global_callbacks (void)
 Register the global callbacks with the provisioning server.

Constructor & Destructor Documentation

◆ meshXRelayServerElement() [1/2]

meshXRelayServerElement::meshXRelayServerElement ( uint16_t element_idx)
explicit

Constructs a new meshXRelayServerElement instance. The meshXRelayServerElement represents a relay element in the MeshX BLE mesh network. It automatically initializes and configures all required SIG models for the relay element.

Register class-level callbacks (called exactly once via once_flag).

Parameters
element_idxThe index of the element within the node.

Constructs a new meshXRelayServerElement instance.

66{
67 memset(&element_ctx, 0, sizeof(meshx_relay_srv_el_ctx_t));
69 element_ctx.gen_on_off_state.on_off = 0;
70 element_ctx.gen_on_off_state.next_on_off = 1;
71
72 /* Restore from NVS if available */
73 meshx_nvs_element_ctx_get(element_idx, MESHX_ELEMENT_TYPE_RELAY_SERVER, &element_ctx, sizeof(meshx_relay_srv_el_ctx_t));
74
76 this->set_element_variant(MESHX_ELEMENT_TYPE_RELAY_SERVER);
77
82
83#if CONFIG_ENABLE_UNIT_TEST
85#endif
86}
uint16_t element_idx
Definition meshx_fwd_decl.hpp:84
meshXElementServer()=default
void set_element_variant(meshx_element_type_t variant)
Definition meshx_element_class.hpp:69
void register_element_ctx(meshx_ptr_t ctx, size_t ctx_size)
Definition meshx_element_class.hpp:132
static meshx_err_t s_to_ble_cb(const dev_struct_t *pdev, control_task_msg_evt_t evt, const void *params)
Definition meshx_relay_element.cpp:175
meshx_relay_srv_el_ctx_t element_ctx
Definition meshx_relay_element.hpp:54
#define MESHX_ADDR_UNASSIGNED
Definition meshx_ble_mesh_cmn_def.h:30
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.
Definition meshx_control_task.h:188
@ CONTROL_TASK_MSG_CODE_TO_BLE
Definition meshx_control_task.h:67
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.
Definition meshx_control_task.c:142
meshx_err_t meshx_nvs_element_ctx_get(uint16_t element_id, meshx_element_type_t element_type, void *blob, size_t blob_size)
Retrieve the context of a specific element from NVS.
Definition meshx_nvs.c:447
meshx_err_t relay_srv_ut_handler(int cmd_id, int argc, char **argv)
Definition meshx_relay_element.cpp:477
#define RELAY_SRV_TO_BLE_EVT_MASK
Definition meshx_relay_element.cpp:47
struct meshx_relay_srv_el_ctx_t meshx_relay_srv_el_ctx_t
Definition meshx_relay_element.hpp:38
@ MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER
Definition module_id.h:32
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:137

◆ meshXRelayServerElement() [2/2]

meshXRelayServerElement::meshXRelayServerElement ( void )
delete

Member Function Documentation

◆ element_state_change_notify()

meshx_err_t meshXRelayServerElement::element_state_change_notify ( meshx_ptr_t param,
size_t param_size )
overrideprivatevirtual

Notify element of state change from child model.

Overrides base class stub. Updates element_ctx, saves to NVS, and dispatches app notification via meshx_send_msg_to_app().

Reimplemented from meshXElement< meshx_srv_model_send_param_header_t >.

122{
123 if (!param)
124 return MESHX_INVALID_ARG;
125
126 auto *msg = static_cast<meshx_on_off_srv_el_msg_t *>(param);
127 uint16_t element_id = msg->header.model.el_id;
128
129 /* Update local context */
130 element_ctx.gen_on_off_state = msg->state;
131
132 /* Task B — NVS persistence */
134 element_id, get_element_variant(), &element_ctx, sizeof(element_ctx));
135 if (err)
136 {
138 "Relay Srv [%d]: NVS save failed: %d", element_id, err);
139 }
140
141 /* Task B — App notification */
143 .on_off = element_ctx.gen_on_off_state.on_off
144 };
145 MESHX_LOGD(MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER, "Relay state changed to: %d", app_evt.on_off);
147 element_id,
148 MESHX_ELEMENT_TYPE_RELAY_SERVER,
150 sizeof(app_evt),
151 &app_evt);
152}
meshx_element_type_t get_element_variant(void) const final
Definition meshx_element_class.hpp:70
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:128
#define MESHX_ELEMENT_FUNC_ID_RELAY_SERVER_ONN_OFF
Definition meshx_api.h:28
struct meshx_api_relay_server_evt meshx_api_relay_server_evt_t
Structure defines the payload for MESHX_ELEMENT_TYPE_RELAY_SERVER.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:120
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
struct meshx_on_off_srv_el_msg meshx_on_off_srv_el_msg_t
Definition meshx_model_onoff.hpp:114
meshx_err_t meshx_nvs_element_ctx_set(uint16_t element_id, meshx_element_type_t element_type, const void *blob, size_t blob_size)
Store the context of a specific element to NVS.
Definition meshx_nvs.c:466
uint8_t on_off
Definition meshx_api.h:60
uint16_t el_id
Definition meshx_ble_mesh_cmn.h:36
meshx_srv_model_send_param_header_t header
Definition meshx_model_onoff.hpp:110
meshx_model_t model
Definition meshx_model_class.hpp:350

◆ get_element_name()

const char * meshXRelayServerElement::get_element_name ( void ) const
overrideprivatevirtual

Get the name of the element.

Returns
String literal representing the element name.

Reimplemented from meshXElement< meshx_srv_model_send_param_header_t >.

115{
116 return "Relay Server";
117}

◆ get_state()

uint8_t meshXRelayServerElement::get_state ( ) const
inline

Get the current OnOff state of the relay server.

Returns
uint8_t 0 for OFF, 1 for ON.
98{ return element_ctx.gen_on_off_state.on_off; }

◆ handle_config()

void meshXRelayServerElement::handle_config ( control_task_msg_evt_t evt,
const meshx_config_srv_cb_param_t * params )
overrideprivatevirtual

Handle configuration server events.

Implements meshXElementIF.

155{
156 MESHX_LOGD(MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER, "Relay Server [%d] Config Evt: %d", get_element_idx(), evt);
157}
uint16_t get_element_idx(void) const
Definition meshx_fwd_decl.hpp:99

◆ list_sig_models()

uint8_t meshXRelayServerElement::list_sig_models ( void )
overrideprivatevirtual

Lists and initializes SIG models for Relay Server Element.

Reimplemented from meshXElement< meshx_srv_model_send_param_header_t >.

93{
94 auto relay_model = std::make_unique<meshXGenericOnOffServerModel>(
95 this,
96 &element_ctx.gen_on_off_state.on_off,
97 (uint16_t)static_cast<int>(
99 );
100 this->get_sig_models().push_back(std::move(relay_model));
101 return (uint8_t)this->get_sig_models().size();
102}
std::vector< std::unique_ptr< meshXModelIF > > & get_sig_models(void) final
Definition meshx_element_class.hpp:78
@ MESHX_RELAY_SERVER_ELEMENT_COMP_GENERIC_ONOFF_SERVER
Definition meshx_relay_element.hpp:46

◆ list_ven_models()

uint8_t meshXRelayServerElement::list_ven_models ( void )
overrideprivatevirtual

Lists Vendor models for Relay Server Element (none required).

Reimplemented from meshXElement< meshx_srv_model_send_param_header_t >.

109{
110 return 0; /* No vendor models for relay server */
111}

◆ s_to_ble_cb()

meshx_err_t meshXRelayServerElement::s_to_ble_cb ( const dev_struct_t * pdev,
control_task_msg_evt_t evt,
const void * params )
staticprivate
179{
180 /* Server-side TO_BLE: re-publish current state on demand.
181 The payload is meshx_gen_srv_send_msg_t with the element_id. */
182 MESHX_UNUSED(params);
183 MESHX_UNUSED(pdev);
184 MESHX_UNUSED(evt);
185 /* No-op for server elements in relay; the app-driven send goes via client elements */
186 return MESHX_SUCCESS;
187}
#define MESHX_UNUSED(x)
Definition meshx_err.h:19
**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

◆ sync()

void meshXRelayServerElement::sync ( control_task_msg_evt_t evt)
overrideprivatevirtual

Synchronize element state (Status broadcast).

Implements meshXElementIF.

160{
161 MESHX_LOGD(MODULE_ID_ELEMENT_SWITCH_RELAY_SERVER, "Relay Server [%d] sync event: %d", get_element_idx(), evt);
163 {
164 if (!meshx_prov_srv_is_provisioned()) return;
165 if (element_ctx.pub_addr == MESHX_ADDR_UNASSIGNED) return;
166
167 auto &models = this->get_sig_models();
168 auto *onoff = static_cast<meshXGenericOnOffServerModel *>(models[0].get());
169 onoff->request_status();
170 }
171}
CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY
Definition meshx_control_task.h:168
bool meshx_prov_srv_is_provisioned(void)
Checks if the device is provisioned.
Definition meshx_prov_srv.cpp:316

Field Documentation

◆ element_ctx

meshx_relay_srv_el_ctx_t meshXRelayServerElement::element_ctx
private

The documentation for this class was generated from the following files: