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_element_class.hpp
Go to the documentation of this file.
1/**
2 * @file meshx_element_class.hpp
3 * @brief MeshX Element class and interface declaration
4 * This file contains the meshXElement class and its interface meshXElementIF.
5 * The meshXElement class represents an element in the MeshX BLE mesh network,
6 * while the meshXElementIF interface defines the callback function for model events.
7 *
8 * @author Pranjal Chanda
9 * @date 2024-2025
10 * @copyright Copyright 2024 - 2025 MeshX
11 */
12
13#ifndef __MESHX_ELEMENT_CLASS__
14#define __MESHX_ELEMENT_CLASS__
15
16#include <meshx_fwd_decl.hpp>
17#include <meshx_model_class.hpp>
18#include <memory>
19#include <vector>
20
21#define MESHX_ELEMENT_ADD_MODEL_TEMPLATE_PROTO template <typename meshXModelT, typename... ConstructorsArgs>
22#define MESHX_ELEMENT_ADD_MODEL_TEMPLATE_PARAMS <meshXModelT, typename... ConstructorsArgs>
23
24/*********************************************************************************
25 * meshXElement
26 *********************************************************************************/
27/**
28 * @class meshXElement
29 * @brief Base class for MeshX elements
30 * @details This is a base class for elements.
31 */
34{
35private:
38
39 std::vector<uint8_t> sig_model_array;
40 std::vector<uint8_t> ven_model_array;
41
42 std::vector<std::unique_ptr<meshXModelIF>> sig_models;
43 std::vector<std::unique_ptr<meshXModelIF>> ven_models;
45 meshx_element_type_t element_variant;
46
47 meshx_ptr_t element_ctx; /**< Pointer to element context structure */
48 size_t element_ctx_size; /**< Size of the element context structure */
49
50 /**
51 *
52 * @brief Handle model callback from child models.
53 *
54 * This function is called by child models when a state change occurs.
55 * It handles the state change by storing in element context, saving to NVS,
56 * and notifying the application.
57 * @param[in] param Pointer to the model callback parameter
58 * @param[in] param_size Size of the parameter structure
59 * @return
60 * - MESHX_SUCCESS: State change handled successfully
61 * - MESHX_INVALID_ARG: Invalid parameter
62 */
63 meshx_err_t on_model_cb(meshx_ptr_t param, size_t param_size) final;
64public:
65
67 meshxElementType_t get_element_type(void) const final { return element_type; }
68
69 void set_element_variant(meshx_element_type_t variant) { element_variant = variant; }
70 meshx_element_type_t get_element_variant(void) const final { return element_variant; }
71
72 void set_no_of_sig_models(uint8_t cnt) { no_of_sig_models = cnt; }
73 uint8_t get_no_of_sig_models(void) const final { return no_of_sig_models; }
74
75 void set_no_of_ven_models(uint8_t cnt) { no_of_ven_models = cnt; }
76 uint8_t get_no_of_ven_models(void) const final { return no_of_ven_models; }
77
78 std::vector<std::unique_ptr<meshXModelIF>>& get_sig_models(void) final { return sig_models; }
79 std::vector<std::unique_ptr<meshXModelIF>>& get_ven_models(void) final { return ven_models; }
80
81 const char* get_element_name(void) const override;
82
83 /**
84 * @brief Lists and creates all required SIG models for the element.
85 *
86 * This function is responsible for creating and populating the sig_models vector
87 * with all essential SIG models that the element must have.
88 *
89 * The function can be extended to include additional models based on configuration
90 * flags or specific requirements.
91 *
92 * @return uint8_t The total number of SIG models created and added to the root_sig_models vector
93 */
94 virtual uint8_t list_sig_models(void) { return 0; };
95 /**
96 * @brief Lists and creates all required Vendor models for the element.
97 *
98 * This function is responsible for creating and populating the ven_models vector
99 * with all essential Vendor models that the element must have.
100 *
101 * The function can be extended to include additional models based on configuration
102 * flags or specific requirements.
103 *
104 * @return uint8_t The total number of Vendor models created and added to the root_sig_models vector
105 */
106 virtual uint8_t list_ven_models(void) { return 0; };
107
108 /**
109 * @brief Notify element about state change
110 * @note This function shall be derived by the specific element class to handle
111 * state change notifications from child models (if required).
112 *
113 * @param[in] param Pointer to the state change parameter
114 * @param[in] param_size Size of the parameter structure
115 *
116 * @return MESHX_SUCCESS on success, error code otherwise
117 */
118 virtual meshx_err_t element_state_change_notify(meshx_ptr_t param, size_t param_size)
119 {
120 /* If not derived, return success */
121 return MESHX_SUCCESS;
122 }
123
124 /**
125 * @brief Register element context structure
126 * @details This function registers the element context structure used to
127 * maintain state information for the element.
128 *
129 * @param[in] ctx Pointer to the element context structure
130 * @param[in] ctx_size Size of the context structure
131 */
132 void register_element_ctx(meshx_ptr_t ctx, size_t ctx_size)
133 {
134 element_ctx = ctx;
135 element_ctx_size = ctx_size;
136 }
137 /**
138 * @brief Get the element context structure
139 * @return Pointer to the element context structure
140 */
141 meshx_ptr_t get_element_ctx(void) const override { return element_ctx; }
142 /**
143 * @brief Get the size of the element context structure
144 * @return Size of the element context structure
145 */
146 size_t get_element_ctx_size(void) const override { return element_ctx_size; }
147
148 /**
149 * @brief Allocate memory for SIG model platform array
150 * @details This function allocates memory for the SIG model platform array
151 * based on the number of SIG models supported by the element.
152 * It reserves capacity for the specified number of models.
153 *
154 * @return MESHX_SUCCESS on success, MESHX_NOT_SUPPORTED if no SIG models supported
155 */
157
158 /**
159 * @brief Allocate memory for Vendor model platform array
160 * @details This function allocates memory for the Vendor model platform array
161 * based on the number of Vendor models supported by the element.
162 * It reserves capacity for the specified number of models.
163 *
164 * @return MESHX_SUCCESS on success, MESHX_NOT_SUPPORTED if no Vendor models supported
165 */
167
168 /**
169 * @brief Get the SIG model array
170 * @return Reference to the SIG model array
171 */
172 std::vector<uint8_t>& get_sig_model_array(void) { return sig_model_array; }
173
174 /**
175 * @brief Get the Vendor model array
176 * @return Reference to the Vendor model array
177 */
178 std::vector<uint8_t>& get_ven_model_array(void) { return ven_model_array; }
179
180 /**
181 * @brief Add a SIG model to the element
182 * @tparam meshXModelT Type of the model to add
183 * @tparam ConstructorsArgs Variadic template parameter pack for constructor arguments
184 *
185 * This function adds a SIG model to the element using perfect forwarding.
186 * It validates the model type, checks capacity constraints, and transfers ownership.
187 *
188 * @param[in] args Constructor arguments for the model
189 * @return MESHX_SUCCESS on success, error code otherwise
190 */
192 meshx_err_t add_sig_model(ConstructorsArgs&&... args);
193
194 /**
195 * @brief Add a Vendor model to the element
196 * @tparam meshXModelT Type of the model to add
197 * @tparam ConstructorsArgs Variadic template parameter pack for constructor arguments
198 *
199 * This function adds a Vendor model to the element using perfect forwarding.
200 * It validates the model type, checks capacity constraints, and transfers ownership.
201 *
202 * @param[in] args Constructor arguments for the model
203 * @return MESHX_SUCCESS on success, error code otherwise
204 */
206 meshx_err_t add_ven_model(ConstructorsArgs&&... args);
207
208 /**
209 * @brief Get the number of SIG models currently added to the element
210 * @return Number of SIG models in the element
211 */
212 uint8_t get_sig_model_count(void) const { return sig_models.size(); }
213
214 /**
215 * @brief Get the number of Vendor models currently added to the element
216 * @return Number of Vendor models in the element
217 */
218 uint8_t get_ven_model_count(void) const { return ven_models.size(); }
219
220 /**
221 * @brief Add multiple SIG models to the element
222 * @details This function allows adding multiple SIG models at once to the element.
223 * It accepts a vector of unique_ptr to meshXModelIF objects.
224 * The function validates input parameters, checks capacity constraints,
225 * sets parent element for each model, and transfers ownership.
226 *
227 * @return MESHX_SUCCESS on success, MESHX_NOT_SUPPORTED if no SIG models supported,
228 * MESHX_INVALID_ARG if input vector is empty or contains null pointers,
229 * MESHX_NO_MEM if adding models would exceed capacity
230 */
232
233 /**
234 * @brief Add multiple Vendor models to the element
235 * @details This function allows adding multiple Vendor models at once to the element.
236 * It accepts a vector of unique_ptr to meshXModelIF objects.
237 * The function validates input parameters, checks capacity constraints,
238 * sets parent element for each model, and transfers ownership.
239 *
240 * @return MESHX_SUCCESS on success, MESHX_NOT_SUPPORTED if no Vendor models supported,
241 * MESHX_INVALID_ARG if input vector is empty or contains null pointers,
242 * MESHX_NO_MEM if adding models would exceed capacity
243 */
245
246 /**
247 * @brief Add both SIG and Vendor models to the element
248 * @details This function combines the functionality of add_sig_models() and add_ven_models()
249 * to add all available models (both SIG and Vendor) to the element in a single call.
250 * It validates input parameters, checks capacity constraints for both model types,
251 * sets parent element for each model, and transfers ownership.
252 *
253 * @return MESHX_SUCCESS on success, error code otherwise (MESHX_NOT_SUPPORTED, MESHX_INVALID_ARG, MESHX_NO_MEM)
254 */
256
257 /**
258 * @brief Get the platform model array for SIG models
259 * @return Pointer to the SIG model array
260 */
261 MESHX_MODEL* get_sig_plat_model_array(void);
262
263 /**
264 * @brief Get the platform model array for Vendor models
265 * @return Pointer to the Vendor model array
266 */
267 MESHX_MODEL* get_ven_plat_model_array(void);
268
269 /**
270 * @brief Default constructor for meshXElement
271 * @details Creates an element with default parameters (index 0, no models)
272 */
274
275 /**
276 * @brief Constructor for meshXElement with element index
277 * @param[in] element_idx Index of the element in the mesh network
278 */
279 explicit meshXElement(uint16_t element_idx);
280
281 /**
282 * @brief Constructor for meshXElement with element index, type, and model counts
283 * @param[in] element_idx Index of the element in the mesh network
284 * @param[in] type Type of the element (server or client)
285 * @param[in] no_of_sig_models Number of SIG models supported by the element
286 * @param[in] no_of_ven_models Number of vendor models supported by the element
287 */
289
290 meshx_err_t initialize(void) override;
291 meshx_err_t reset(void) override;
292 bool is_initialized(void) const override;
293 meshx_err_t restore_nvs_context(void) override;
294
295
296 /**
297 * @brief Global static provisioning callback for Server elements
298 */
299 static meshx_err_t static_prov_srv_cb(const dev_struct_t *pdev, control_task_msg_evt_t evt, const void *params);
300
301 /**
302 * @brief Global static provisioning callback for Client elements
303 */
304 static meshx_err_t static_prov_cli_cb(const dev_struct_t *pdev, control_task_msg_evt_t evt, const void *params);
305
306 /**
307 * @brief Global static configuration callback
308 */
310
311 /**
312 * @brief Register the global callbacks with the provisioning server.
313 */
314 static void register_global_callbacks(void);
315
316 bool has_model(uint16_t model_id) const override;
317
318 void on_baked(uint16_t index) override {
319 this->set_element_idx(index);
320 for (auto& m : sig_models) m->on_baked();
321 for (auto& m : ven_models) m->on_baked();
322 }
323
324 ~meshXElement() override;
325};
326
327/***********************************************************************************************************
328 * meshXElementServer and meshXElementClient Classes
329 ***********************************************************************************************************/
330/**
331 * @class meshXElementServer
332 * @brief Derived class for server elements
333 */
335class meshXElementServer : public meshXElement <meshx_srv_model_send_param_header_t>
336{
337public:
341};
342
343/**
344 * @class meshXElementClient
345 * @brief Derived class for client elements
346 */
348class meshXElementClient : public meshXElement <meshx_cli_model_send_param_header_t>
349{
350public:
354};
355
356#endif /* __MESHX_ELEMENT_CLASS__ */
meshXElementClient(uint16_t element_idx, uint8_t no_of_sig_models=0, uint8_t no_of_ven_models=0)
Definition meshx_element_class.hpp:352
meshXElementClient()=default
meshXElementIF()=delete
void set_element_idx(uint16_t idx)
Definition meshx_fwd_decl.hpp:98
uint16_t element_idx
Definition meshx_fwd_decl.hpp:84
meshXElementServer(uint16_t element_idx, uint8_t no_of_sig_models=0, uint8_t no_of_ven_models=0)
Definition meshx_element_class.hpp:339
meshXElementServer()=default
void set_element_type(meshxElementType_t type)
Definition meshx_element_class.hpp:66
meshx_err_t restore_nvs_context(void) override
Restore the element's context from NVS.
Definition meshx_element_class.cpp:365
size_t get_element_ctx_size(void) const override
Get the size of the element context structure.
Definition meshx_element_class.hpp:146
void on_baked(uint16_t index) override
Called when the composition is baked to update the element's index.
Definition meshx_element_class.hpp:318
size_t element_ctx_size
Definition meshx_element_class.hpp:48
MESHX_MODEL * get_ven_plat_model_array(void)
Get the platform model array for Vendor models.
Definition meshx_element_class.cpp:91
bool has_model(uint16_t model_id) const override
Check if the element contains a specific model by ID.
Definition meshx_element_class.cpp:384
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.
Definition meshx_element_class.cpp:406
virtual uint8_t list_sig_models(void)
Lists and creates all required SIG models for the element.
Definition meshx_element_class.hpp:94
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.
Definition meshx_element_class.cpp:442
void set_no_of_sig_models(uint8_t cnt)
Definition meshx_element_class.hpp:72
meshxElementType_t get_element_type(void) const final
Get the element type.
Definition meshx_element_class.hpp:67
std::vector< std::unique_ptr< meshXModelIF > > ven_models
Definition meshx_element_class.hpp:43
meshx_err_t on_model_cb(meshx_ptr_t param, size_t param_size) final
Handle model callback from child models.
Definition meshx_element_class.cpp:265
void set_element_variant(meshx_element_type_t variant)
Definition meshx_element_class.hpp:69
static void register_global_callbacks(void)
Register the global callbacks with the provisioning server.
Definition meshx_element_class.cpp:502
virtual uint8_t list_ven_models(void)
Lists and creates all required Vendor models for the element.
Definition meshx_element_class.hpp:106
uint8_t get_no_of_ven_models(void) const final
Get the number of Vendor models supported by the element.
Definition meshx_element_class.hpp:76
uint8_t get_sig_model_count(void) const
Get the number of SIG models currently added to the element.
Definition meshx_element_class.hpp:212
meshx_err_t ven_plat_model_array_allocate(void)
Allocate memory for Vendor model platform array.
Definition meshx_element_class.cpp:72
meshx_ptr_t element_ctx
Definition meshx_element_class.hpp:47
meshx_err_t reset(void) override
Reset the element.
Definition meshx_element_class.cpp:349
meshx_err_t add_ven_models(void)
Add multiple Vendor models to the element.
Definition meshx_element_class.cpp:191
std::vector< uint8_t > ven_model_array
Definition meshx_element_class.hpp:40
uint8_t no_of_sig_models
Definition meshx_element_class.hpp:36
MESHX_MODEL * get_sig_plat_model_array(void)
Get the platform model array for SIG models.
Definition meshx_element_class.cpp:84
std::vector< std::unique_ptr< meshXModelIF > > & get_sig_models(void) final
Get the SIG models vector.
Definition meshx_element_class.hpp:78
void register_element_ctx(meshx_ptr_t ctx, size_t ctx_size)
Register element context structure.
Definition meshx_element_class.hpp:132
virtual meshx_err_t element_state_change_notify(meshx_ptr_t param, size_t param_size)
Notify element about state change.
Definition meshx_element_class.hpp:118
std::vector< uint8_t > & get_sig_model_array(void)
Get the SIG model array.
Definition meshx_element_class.hpp:172
std::vector< uint8_t > sig_model_array
Definition meshx_element_class.hpp:39
bool is_initialized(void) const override
Check if the element is initialized.
Definition meshx_element_class.cpp:357
uint8_t get_ven_model_count(void) const
Get the number of Vendor models currently added to the element.
Definition meshx_element_class.hpp:218
meshXElement(void)
Default constructor for meshXElement.
std::vector< uint8_t > & get_ven_model_array(void)
Get the Vendor model array.
Definition meshx_element_class.hpp:178
std::vector< std::unique_ptr< meshXModelIF > > & get_ven_models(void) final
Get the Vendor models vector.
Definition meshx_element_class.hpp:79
void set_no_of_ven_models(uint8_t cnt)
Definition meshx_element_class.hpp:75
meshx_ptr_t get_element_ctx(void) const override
Get the element context structure.
Definition meshx_element_class.hpp:141
meshx_err_t add_models(void)
Add both SIG and Vendor models to the element.
Definition meshx_element_class.cpp:234
uint8_t no_of_ven_models
Definition meshx_element_class.hpp:37
uint8_t get_no_of_sig_models(void) const final
Get the number of SIG models supported by the element.
Definition meshx_element_class.hpp:73
const char * get_element_name(void) const override
Get the name of the element.
Definition meshx_element_class.cpp:98
meshx_element_type_t element_variant
Definition meshx_element_class.hpp:45
meshxElementType_t element_type
Definition meshx_element_class.hpp:44
~meshXElement() override
Definition meshx_element_class.cpp:377
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.
Definition meshx_element_class.cpp:424
meshx_element_type_t get_element_variant(void) const final
Get the element variant.
Definition meshx_element_class.hpp:70
meshx_err_t add_sig_model(ConstructorsArgs &&... args)
Add a SIG model to the element.
Definition meshx_element_class.cpp:109
meshx_err_t add_sig_models(void)
Add multiple SIG models to the element.
Definition meshx_element_class.cpp:128
std::vector< std::unique_ptr< meshXModelIF > > sig_models
Definition meshx_element_class.hpp:42
meshx_err_t add_ven_model(ConstructorsArgs &&... args)
Add a Vendor model to the element.
Definition meshx_element_class.cpp:172
meshx_err_t sig_plat_model_array_allocate(void)
Allocate memory for SIG model platform array.
Definition meshx_element_class.cpp:60
meshx_err_t initialize(void) override
Initialize the element.
Definition meshx_element_class.cpp:314
void * meshx_ptr_t
Definition meshx_ble_mesh_cmn_def.h:636
struct meshx_config_srv_cb_param meshx_config_srv_cb_param_t
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.
Definition meshx_control_task.h:81
#define MESHX_ELEMENT_ADD_MODEL_TEMPLATE_PROTO
Definition meshx_element_class.hpp:21
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
Forward declaration of MeshX classes.
#define MESHX_SERVER_ELEMENT_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:38
#define MESHX_ELEMENT_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:36
@ MESHX_ELEMENT_TYPE_CLIENT
Definition meshx_fwd_decl.hpp:62
@ MESHX_ELEMENT_TYPE_SERVER
Definition meshx_fwd_decl.hpp:61
#define MESHX_CLIENT_ELEMENT_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:40
enum meshxElementType meshxElementType_t
Definition meshx_fwd_decl.hpp:71
Template declarations for MeshX model wrapper classes.
**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