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

#include <meshx_rgb_element.hpp>

Inheritance diagram for meshXRGBServerElement:
meshXElementServer meshXElement< meshx_srv_model_send_param_header_t > meshXElementIF

Public Member Functions

 meshXRGBServerElement (uint16_t element_idx)
 Constructs a new meshXRGBServerElement instance.
 meshXRGBServerElement (void)=delete
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 creates all required SIG models for the element.
uint8_t list_ven_models (void) override
 Lists and creates all required Vendor models for the element.
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_rgb_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

◆ meshXRGBServerElement() [1/2]

meshXRGBServerElement::meshXRGBServerElement ( uint16_t element_idx)
explicit

Constructs a new meshXRGBServerElement instance.

Parameters
element_idxThe index of the element within the node.
30{
31 memset(&element_ctx, 0, sizeof(element_ctx));
33
34 /* Restore from NVS if available */
35 meshx_nvs_element_ctx_get(element_idx, MESHX_ELEMENT_TYPE_LIGHT_HSL_SERVER, &element_ctx, sizeof(element_ctx));
36
38 set_element_variant(MESHX_ELEMENT_TYPE_LIGHT_HSL_SERVER);
39
40 /* Add models:
41 1. Generic OnOff Server (mapped to HSL OnOff)
42 2. Light HSL Server
43 */
44#if CONFIG_ENABLE_GEN_ONOFF_SERVER
45 auto onoff_srv = std::make_unique<meshXGenericOnOffServerModel>(
46 this,
47 &element_ctx.gen_on_off_state,
49 );
50 this->get_sig_models().push_back(std::move(onoff_srv));
51#endif
52#if CONFIG_ENABLE_LIGHT_HSL_SERVER
53 auto hsl_srv = std::make_unique<meshXLightHSLServerModel>(
54 this,
55 &element_ctx.light_hsl_state,
57 );
58 this->get_sig_models().push_back(std::move(hsl_srv));
59#endif
60
63
66}
uint16_t element_idx
Definition meshx_fwd_decl.hpp:84
meshXElementServer()=default
void set_no_of_sig_models(uint8_t cnt)
Definition meshx_element_class.hpp:72
void set_element_variant(meshx_element_type_t variant)
Definition meshx_element_class.hpp:69
uint8_t get_sig_model_count(void) const
Definition meshx_element_class.hpp:212
meshx_err_t ven_plat_model_array_allocate(void)
Definition meshx_element_class.cpp:72
std::vector< std::unique_ptr< meshXModelIF > > & get_sig_models(void) final
Definition meshx_element_class.hpp:78
void register_element_ctx(meshx_ptr_t ctx, size_t ctx_size)
Definition meshx_element_class.hpp:132
uint8_t get_ven_model_count(void) const
Definition meshx_element_class.hpp:218
void set_no_of_ven_models(uint8_t cnt)
Definition meshx_element_class.hpp:75
meshx_err_t sig_plat_model_array_allocate(void)
Definition meshx_element_class.cpp:60
meshx_rgb_srv_el_ctx_t element_ctx
Definition meshx_rgb_element.hpp:54
#define MESHX_ELEMENT_FUNC_ID_LIGHT_HSL_SERVER_HSL
Definition meshx_api.h:36
#define MESHX_ELEMENT_FUNC_ID_LIGHT_HSL_SERVER_ONN_OFF
Definition meshx_api.h:35
#define MESHX_ADDR_UNASSIGNED
Definition meshx_ble_mesh_cmn_def.h:30
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

◆ meshXRGBServerElement() [2/2]

meshXRGBServerElement::meshXRGBServerElement ( void )
delete

Member Function Documentation

◆ element_state_change_notify()

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

Notify element of state change from child model.

Reimplemented from meshXElement< meshx_srv_model_send_param_header_t >.

84{
85 MESHX_UNUSED(param_size);
86 if (!param) return MESHX_INVALID_ARG;
87
88 auto *header = static_cast<meshx_srv_model_send_param_header_t *>(param);
89 uint16_t element_id = get_element_idx();
90 uint32_t model_id = header->model.model_id;
91
93
94 if (model_id == 0x1000) /* Generic OnOff Server */
95 {
96#if CONFIG_ENABLE_GEN_ONOFF_SERVER
97 auto *msg = static_cast<meshx_on_off_srv_el_msg_t *>(param);
98 element_ctx.gen_on_off_state.on_off = msg->state.on_off;
99
100 /* App notification */
102 app_evt.state_change.on_off.state = element_ctx.gen_on_off_state.on_off;
103
105 element_id,
106 MESHX_ELEMENT_TYPE_LIGHT_HSL_SERVER,
108 sizeof(app_evt),
109 &app_evt);
110#endif
111 }
112 else if (model_id == 0x1307) /* Light HSL Server */
113 {
114 auto *msg = static_cast<meshx_light_hsl_srv_el_msg_t *>(param);
115 element_ctx.light_hsl_state = msg->state;
116
117 /* App notification */
119 app_evt.state_change.hsl.lightness = element_ctx.light_hsl_state.lightness;
120 app_evt.state_change.hsl.hue = element_ctx.light_hsl_state.hue;
121 app_evt.state_change.hsl.saturation = element_ctx.light_hsl_state.saturation;
122
124 element_id,
125 MESHX_ELEMENT_TYPE_LIGHT_HSL_SERVER,
127 sizeof(app_evt),
128 &app_evt);
129 }
130
131 /* NVS persistence */
133
134 return err;
135}
uint16_t get_element_idx(void) const
Definition meshx_fwd_decl.hpp:99
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
struct meshx_api_light_hsl_server_evt meshx_api_light_hsl_server_evt_t
Structure defines the payload for MESHX_ELEMENT_TYPE_LIGHT_HSL_SERVER.
#define MESHX_UNUSED(x)
Definition meshx_err.h:19
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
struct meshx_srv_model_send_param_header meshx_srv_model_send_param_header_t
Definition meshx_model_class.hpp:354
struct meshx_light_hsl_srv_el_msg meshx_light_hsl_srv_el_msg_t
Definition meshx_model_hsl.hpp:125
**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
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
uint16_t lightness
Definition meshx_api.h:98
union meshx_api_light_hsl_server_evt::@347235105244230326232000270243372246073133053316 state_change
uint8_t state
Definition meshx_api.h:94
struct meshx_api_light_hsl_server_evt::@347235105244230326232000270243372246073133053316::@202375373323027037070222212050326072162342057303 on_off
uint16_t saturation
Definition meshx_api.h:100
uint16_t hue
Definition meshx_api.h:99
struct meshx_api_light_hsl_server_evt::@347235105244230326232000270243372246073133053316::@153162234125000074275266213156212277206313354032 hsl

◆ get_element_name()

const char * meshXRGBServerElement::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 >.

79{
80 return "HSL Server";
81}

◆ handle_config()

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

Handle configuration server events.

Implements meshXElementIF.

232{
233 MESHX_LOGD(MODULE_ID_ELEMENT_LIGHT_HSL_SERVER, "RGB Server [%d] Config Evt: %d", get_element_idx(), evt);
234}
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
@ MODULE_ID_ELEMENT_LIGHT_HSL_SERVER
Definition module_id.h:42

◆ list_sig_models()

uint8_t meshXRGBServerElement::list_sig_models ( void )
overrideprivatevirtual

Lists and creates all required SIG models for the element.

This function is responsible for creating and populating the sig_models vector with all essential SIG models that the element must have.

The function can be extended to include additional models based on configuration flags or specific requirements.

Returns
uint8_t The total number of SIG models created and added to the root_sig_models vector

Reimplemented from meshXElement< meshx_srv_model_send_param_header_t >.

69{
70 return get_no_of_sig_models();
71}
uint8_t get_no_of_sig_models(void) const final
Definition meshx_element_class.hpp:73

◆ list_ven_models()

uint8_t meshXRGBServerElement::list_ven_models ( void )
overrideprivatevirtual

Lists and creates all required Vendor models for the element.

This function is responsible for creating and populating the ven_models vector with all essential Vendor models that the element must have.

The function can be extended to include additional models based on configuration flags or specific requirements.

Returns
uint8_t The total number of Vendor models created and added to the root_sig_models vector

Reimplemented from meshXElement< meshx_srv_model_send_param_header_t >.

74{
75 return 0;
76}

◆ s_to_ble_cb()

meshx_err_t meshXRGBServerElement::s_to_ble_cb ( const dev_struct_t * pdev,
control_task_msg_evt_t evt,
const void * params )
staticprivate
141{
142 MESHX_UNUSED(pdev);
143 MESHX_UNUSED(evt);
144 if (!params) return MESHX_INVALID_ARG;
145
146 auto* app_msg = static_cast<const meshx_app_element_msg_header_t*>(params);
147 auto* app_payload = reinterpret_cast<const meshx_data_payload_t*>(app_msg + 1);
148
149 /* Look up the instance from the registry using the element_id */
151 app_msg->element_id, MESHX_ELEMENT_TYPE_LIGHT_HSL_SERVER);
152
153 if (!el) return MESHX_NOT_FOUND;
154
155 if (app_msg->func_id == MESHX_ELEMENT_FUNC_ID_LIGHT_HSL_SERVER_HSL)
156 {
157 el->element_ctx.light_hsl_state.hue = app_payload->light_hsl_server_evt.state_change.hsl.hue;
158 el->element_ctx.light_hsl_state.saturation = app_payload->light_hsl_server_evt.state_change.hsl.saturation;
159 el->element_ctx.light_hsl_state.lightness = app_payload->light_hsl_server_evt.state_change.hsl.lightness;
160
161 /* Publish the new state */
162 for (auto& m : el->get_sig_models())
163 {
164 if (m->get_model_id() == MESHX_MODEL_ID_LIGHT_HSL_SRV)
165 {
166#if CONFIG_ENABLE_LIGHT_HSL_SERVER
167 auto* hsl_srv = static_cast<meshXLightHSLServerModel*>(m.get());
168 meshx_model_t model_ref = {
169 .el_id = el->get_element_idx(),
170 .model_id = (uint16_t)hsl_srv->get_model_id(),
171 .pub_addr = el->element_ctx.pub_addr,
172 .p_model = hsl_srv->get_plat_model()
173 };
174
175 meshx_ctx_t ctx = {
176 .app_idx = el->element_ctx.app_id,
177 .net_idx = pdev->meshx_store.net_key_id,
179 .src_addr = 0,
180 .dst_addr = el->element_ctx.pub_addr,
181 .p_ctx = hsl_srv->get_pub_struct()
182 };
183
184 meshx_light_hsl_send_params_t send_params = {
185 .model = &model_ref,
186 .ctx = &ctx,
187 .tid = 0,
188 .state = el->element_ctx.light_hsl_state
189 };
190 hsl_srv->model_send(&send_params);
191#endif
192 }
193 }
194
195 /* Persist to NVS */
196 meshx_nvs_element_ctx_set(el->get_element_idx(), MESHX_ELEMENT_TYPE_LIGHT_HSL_SERVER,
197 &el->element_ctx, sizeof(el->element_ctx));
198 }
199
200 return MESHX_SUCCESS;
201}
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
meshXRGBServerElement(uint16_t element_idx)
Constructs a new meshXRGBServerElement instance.
Definition meshx_rgb_element.cpp:28
union meshx_data_payload meshx_data_payload_t
Structure for the BLE Mesh application control message.
struct meshx_app_element_msg_header meshx_app_element_msg_header_t
Structure for the BLE Mesh application element message header.
struct meshx_model meshx_model_t
struct meshx_ctx meshx_ctx_t
Structure to hold context information for BLE Mesh operations.
#define MESHX_MODEL_OP_LIGHT_HSL_STATUS
Definition meshx_ble_mesh_cmn_def.h:446
#define MESHX_MODEL_ID_LIGHT_HSL_SRV
Definition meshx_ble_mesh_cmn_def.h:111
@ MESHX_NOT_FOUND
Definition meshx_err.h:50
struct meshx_light_hsl_send_params meshx_light_hsl_send_params_t
Definition meshx_model_hsl.hpp:61
meshx_app_store_t meshx_store
Definition meshx_common.h:75
uint16_t net_key_id
Definition meshx_common.h:61

◆ sync()

void meshXRGBServerElement::sync ( control_task_msg_evt_t evt)
overrideprivatevirtual

Synchronize element state (Status broadcast).

Implements meshXElementIF.

203{
204 MESHX_LOGD(MODULE_ID_ELEMENT_LIGHT_HSL_SERVER, "RGB Server [%d] sync event: %d", get_element_idx(), evt);
206 {
207 if (!meshx_prov_srv_is_provisioned()) return;
208 if (element_ctx.pub_addr == MESHX_ADDR_UNASSIGNED) return;
209
210 for (auto& m : this->get_sig_models())
211 {
212 (void)m;
213#if CONFIG_ENABLE_LIGHT_HSL_SERVER
214 if (m->get_model_id() == MESHX_MODEL_ID_LIGHT_HSL_SRV)
215 {
216 auto* hsl_srv = static_cast<meshXLightHSLServerModel*>(m.get());
217 hsl_srv->request_status();
218 }
219#endif
220#if CONFIG_ENABLE_GEN_ONOFF_SERVER
221 if (m->get_model_id() == MESHX_MODEL_ID_GEN_ONOFF_SRV)
222 {
223 auto* onoff = static_cast<meshXGenericOnOffServerModel*>(m.get());
224 onoff->request_status();
225 }
226#endif
227 }
228 }
229}
#define MESHX_MODEL_ID_GEN_ONOFF_SRV
Definition meshx_ble_mesh_cmn_def.h:70
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_rgb_srv_el_ctx_t meshXRGBServerElement::element_ctx
private

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