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_model_class.hpp
Go to the documentation of this file.
1/**
2 * @file meshx_model_class.hpp
3 * @brief Template declarations for MeshX model wrapper classes
4 *
5 * This file contains the template declarations for the wrapper classes that
6 * provide a convenient interface around the MeshX base model classes. It includes
7 * the base wrapper (meshXModel) and specialized wrappers for server and client models.
8 *
9 * Key Features:
10 * - Template-based wrapper architecture
11 * - Unified interface for both client and server models
12 * - Type-safe model creation and management
13 * - Simplified integration with platform-specific implementations
14 *
15 * @author Pranjal Chanda
16 * @date 2024-2025
17 * @copyright Copyright 2024 - 2025 MeshX
18 */
19
20#ifndef _MESHX_MODEL_CLASS_HPP_
21#define _MESHX_MODEL_CLASS_HPP_
22
24
25/*********************************************************************************
26 * meshXModel
27 *********************************************************************************/
28
30
32{
33public:
34 meshXModelIF() : p_plat_model(nullptr), p_plat_pub(nullptr), p_plat_gen(nullptr), parent_element(nullptr), p_parent_element_state(nullptr) { }
35 explicit meshXModelIF(MESHX_MODEL *p_plat_model) : p_plat_model(p_plat_model), p_plat_pub(nullptr), p_plat_gen(nullptr), parent_element(nullptr), p_parent_element_state(nullptr) { }
36 virtual ~meshXModelIF() = default;
37
38private:
39 MESHX_MODEL *p_plat_model; /**< Pointer to the platform model */
40 meshx_ptr_t p_plat_pub; /**< publication structures */
41 meshx_ptr_t p_plat_gen; /**< generic structures */
42 meshXElementIF *parent_element; /**< Pointer to the parent element interface */
43 meshx_ptr_t p_parent_element_state; /**< Pointer to the parent element state */
44public:
45
46 /***********************************************************
47 * Virtual Functions
48 ***********************************************************/
49 /**
50 * @brief Create logical model instance
51 * @details Pure virtual function that derived classes must implement to create
52 * a logical model instance on the platform. This is called during
53 * model initialization to set up the model's runtime state.
54 *
55 * @return MESHX_SUCCESS if model created successfully, error code otherwise
56 */
57 virtual meshx_err_t plat_model_create(MESHX_MODEL* p_plat_model_ptr = nullptr) = 0;
58
59 /**
60 * @brief Delete logical model instance
61 * @details Pure virtual function that derived classes must implement to delete
62 * the logical model instance from the platform. This is called during
63 * model cleanup to release resources associated with the model.
64 */
65 virtual meshx_err_t plat_model_delete(void) = 0;
66
67 /**
68 * @brief Handle state change request from element
69 * @details Pure virtual function that derived classes must implement to handle
70 * state change requests from the parent element. The model validates
71 * the request, updates its internal state, and returns the result.
72 *
73 * @return MESHX_SUCCESS on success, error code otherwise
74 */
76
77 /**
78 * @brief Prepare message for element notification
79 * @details Pure virtual function that derived classes must implement to prepare
80 * a message structure that will be sent to the parent element.
81 * The message must persist after this function returns.
82 *
83 * @param[out] msg_ptr Pointer to message structure (output parameter)
84 * @param[out] msg_size Size of the message structure (output parameter)
85 * @return MESHX_SUCCESS if message prepared successfully, error code otherwise
86 */
87 virtual meshx_err_t prepare_element_msg(meshx_ptr_t *msg_ptr, size_t *msg_size) = 0;
88
89 /**
90 * @brief Called when the model's parent element is baked.
91 */
92 virtual void on_baked(void) {}
93
94 /***********************************************************
95 * Accessor Functions
96 ***********************************************************/
97 /**
98 * @brief Set the platform-specific model instance
99 * @param[in] p_model Pointer to the platform model instance
100 */
101 virtual void set_plat_model(MESHX_MODEL *p_model) { p_plat_model = p_model; }
102
103 /**
104 * @brief Get the platform-specific model instance
105 * @return Pointer to the platform model instance
106 */
107 MESHX_MODEL * get_plat_model(void) const { return p_plat_model; }
108
109 /**
110 * @brief Get the publication structures
111 * @return Pointer to the publication structures
112 */
113 meshx_ptr_t get_pub_struct(void) const { return p_plat_pub; }
114
115 /**
116 * @brief Get the generic structures
117 * @return Pointer to the generic structures
118 */
119 meshx_ptr_t get_gen_struct(void) const { return p_plat_gen; }
120
121 /**
122 * @brief Set the publication structures
123 * @param[in] pub Pointer to the publication structures
124 */
126
127 /**
128 * @brief Set the generic structures
129 * @param[in] gen Pointer to the generic structures
130 */
132
133 /**
134 * @brief Set the parent element for this model
135 * @param[in] parent Pointer to the parent element interface
136 */
138
139 /**
140 * @brief Get the parent element of this model
141 * @return Pointer to the parent element interface
142 */
144
145 /**
146 * @brief Set the parent element state pointer
147 * @param[in] state Pointer to the parent element state
148 */
150 /**
151 * @brief Get the parent element state pointer
152 * @return Pointer to the parent element state
153 */
155
156 /**
157 * @brief Get the model identifier
158 * @return Model ID value
159 */
160 virtual uint16_t get_model_id(void) const = 0;
161
162 /**
163 * @brief Check if the model is successfully initialized.
164 * @return true if initialized, false otherwise.
165 */
166 virtual bool is_initialized(void) const = 0;
167};
168
169/**
170 * @brief meshXModel class
171 * @details This is a base class for both client and server models.
172 */
175{
176private:
177 /* private members */
178 meshxBaseModel_t *base_model = nullptr; /*<! Pointer to the base model */
179 meshx_err_t status; /*<! Status of the model */
180 uint16_t model_id; /*<! Model identifier */
181 uint16_t model_func_id; /*<! Model function identifier. This is the function ID of the model within an element */
182
183public:
184 void set_plat_model(MESHX_MODEL *p_model) override;
185
186 /**
187 * @brief Handle upstream BLE Mesh events
188 * @details Static callback function that routes messages and events coming from the BLE Mesh network
189 * to the appropriate model instance. This function extracts the model instance from the
190 * device structure and invokes the instance's model_from_ble_cb method.
191 * @param[in] p_dev Device structure containing sender information
192 * @param[in] evt_model_id Event type indicating the nature of the message
193 * @param[in] params Event-specific data payload
194 * @return MESHX_SUCCESS if event handled successfully, error code otherwise
195 */
197 dev_struct_t *p_dev,
198 evt_model_id_t evt_model_id,
199 meshx_ptr_t params);
200
201protected:
202 /**
203 * @brief Update element_state_change field in message header
204 * @details Protected virtual method that derived classes (meshXClientModel, meshXServerModel)
205 * must implement to handle type-specific header casting. This method is called by
206 * send_to_parent_element to update the element_state_change field.
207 *
208 * @param[in] element_state_change Result from element_state_change_handle()
209 * @param[in] msg_ptr Pointer to the message structure
210 */
211 virtual void update_element_state_change_header(meshx_err_t element_state_change, meshx_ptr_t msg_ptr) = 0;
212public:
213 /***********************************************************
214 * Virtual Functions
215 ***********************************************************/
216 /**
217 * @brief Handle upstream BLE Mesh events
218 * @details Pure virtual function that derived classes must implement to process
219 * messages and events coming from the BLE Mesh network. The implementation
220 * will be automatically registered with base_model->base_client_model_cb_list.
221 *
222 * @param[in] dev Device structure containing sender information
223 * @param[in] evt Event type indicating the nature of the message
224 * @param[in] data Event-specific data payload
225 * @return MESHX_SUCCESS if event handled successfully, error code otherwise
226 */
228 dev_struct_t *dev,
230 meshx_ptr_t data) = 0;
231
232 /**
233 * @brief Send message through the model
234 * @details Pure virtual function that derived classes must implement to send
235 * messages through the model to the BLE Mesh network.
236 *
237 * @param[in] params Message parameters including destination, opcode, and data
238 * @return MESHX_SUCCESS if message sent successfully, error code otherwise
239 */
240 virtual meshx_err_t model_send(meshx_send_packet_params_t *params) = 0;
241
242 /**
243 * @brief Called when the model's parent element is baked.
244 * @details Overridden to perform deferred registration of callbacks.
245 */
246 void on_baked(void) override
247 {
248 if (base_model)
249 {
250 base_model->from_ble_reg_cb();
251 }
252 }
253
254 /**
255 * @brief Destructor for meshXModel
256 * @details Virtual destructor to ensure proper cleanup of derived classes
257 * and the base_model member.
258 */
259 virtual ~meshXModel();
260
261 /**
262 * @brief Send message to parent element
263 * @details Common implementation for sending messages to the parent element.
264 * This handles the common pattern of checking parent element,
265 * calling element_state_change_handle(), and calling on_model_cb().
266 * Type-specific header casting is delegated to update_element_state_change_header().
267 *
268 * @param[in] msg_ptr Pointer to the message structure
269 * @param[in] msg_size Size of the message structure
270 * @return MESHX_SUCCESS if message sent successfully, error code otherwise
271 */
272 meshx_err_t send_to_parent_element(meshx_ptr_t msg_ptr, size_t msg_size);
273
274 /***********************************************************
275 * Accessor Functions
276 ***********************************************************/
277 /**
278 * @brief Get the model function identifier
279 * @return Model function ID value
280 */
281 uint16_t get_model_func_id(void) const { return model_func_id; }
282
283 /**
284 * @brief Set the model function identifier
285 * @param[in] func_id Model function ID to set
286 */
287 void set_model_func_id(uint16_t func_id) { model_func_id = func_id; }
288 /**
289 * @brief Get the model identifier
290 * @return Model ID value
291 */
292 uint16_t get_model_id(void) const override { return model_id; }
293
294 /**
295 * @brief Check if the model is successfully initialized.
296 * @return true if initialized, false otherwise.
297 */
298 bool is_initialized(void) const override { return status == MESHX_SUCCESS; }
299
300 /**
301 * @brief Set the model identifier
302 * @param[in] id Model ID to set
303 */
304 void set_model_id(uint16_t id) { model_id = id; }
305 /**
306 * @brief Get the model initialization status
307 * @return Status code indicating success or failure of initialization
308 */
309 meshx_err_t get_init_status(void) const { return status; }
310
311 /**
312 * @brief Get the base model instance
313 * @return Pointer to the base model implementation
314 */
315 meshxBaseModel_t * get_base_model(void) const { return base_model; }
316
317 /**
318 * @brief Constructs a new meshXModel instance.
319 *
320 * This constructor initializes a meshXModel object with the given platform model,
321 * model ID, and optional parent element. It sets up the base model and model interface
322 * for BLE mesh communication.
323 *
324 * @param[in] p_plat_model Pointer to the platform model instance
325 * @param[in] model_id Unique identifier for this model
326 * @param[in] parent_element Optional pointer to the parent element
327 * @param[in] model_func_id Optional model function ID within the element
328 *
329 * @note The constructor allocates memory for the base model and model interface.
330 * If memory allocation fails, the status will be set to MESHX_NO_MEM.
331 */
333 MESHX_MODEL *p_plat_model,
334 uint32_t model_id,
336 uint16_t model_func_id = 0
337 );
338
339};
340
341/*********************************************************************************
342 * meshXServerModel
343 *********************************************************************************/
344
345/**
346 * @brief Structure for server model send parameters
347 */
349{
350 meshx_model_t model; /**< Server model Pointer */
351 meshx_err_t element_state_change; /**< Return value from element_state_change_handle */
352};
353
355
356/**
357 * @class meshXServerModel
358 * @brief Base class for all server models in MeshX
359 * @tparam MESHX_MODEL Platform-specific model type
360 * @tparam meshxBaseModel_t Base model implementation type
361 * @tparam meshx_send_packet_params_t Type for send packet parameters
362 * @details Server model implementation providing core server functionality
363 */
366{
367protected:
368 /**
369 * @brief Update element_state_change field in server message header
370 * @details Overrides base class implementation to handle server-specific header structure
371 * (meshx_srv_model_send_param_header_t) which doesn't include err_code and ctx.
372 *
373 * @param[in] element_state_change Result from element_state_change_handle()
374 * @param[in] msg_ptr Pointer to the message structure
375 */
376 void update_element_state_change_header(meshx_err_t element_state_change, meshx_ptr_t msg_ptr) override;
377
378public:
379 /**
380 * @brief Construct a new Server Model
381 * @param[in] p_plat_model Platform-specific model instance
382 * @param[in] model_id Unique identifier for this model
383 * @param[in] parent_element Parent element interface (optional)
384 * @param[in] parent_element_state Parent element state pointer (optional)
385 * @param[in] model_func_id Model function ID within the element (optional)
386 *
387 * @details Initializes a server model with platform-specific implementation
388 * and associates it with an optional parent element
389 */
391 MESHX_MODEL *p_plat_model,
392 uint32_t model_id,
394 meshx_ptr_t parent_element_state = nullptr,
395 uint16_t model_func_id = 0
396 );
397
398 /**
399 * @brief Deleted default constructor
400 * @details Server models must be initialized with a platform model and ID
401 */
403};
404
405/*********************************************************************************
406 * meshXClientModel
407 *********************************************************************************/
408
410{
411 uint8_t err_code; /**< Error code */
412 meshx_model_t model; /**< Generic OnOff Server model */
413 meshx_ctx_t ctx; /**< Context of the message */
414 meshx_err_t element_state_change; /**< Return value from element_state_change_handle */
415};
416
418
419/**
420 * @class meshXClientModel
421 * @brief Base class for all client models in the mesh network
422 *
423 * @tparam MESHX_MODEL Platform-specific client model implementation type
424 * @details Implements core client model functionality including model creation,
425 * message sending, and client-specific operations
426 */
429{
430private:
431 /**
432 * @brief Create platform-specific client model instance
433 * @details Final implementation of the model creation process for client models.
434 * This function handles the initialization of client-specific features
435 * and cannot be overridden by derived classes.
436 *
437 * @return MESHX_SUCCESS on successful model creation and initialization,
438 * error code otherwise
439 */
440 meshx_err_t plat_model_create(MESHX_MODEL* p_plat_model_ptr = nullptr) final;
441
442 /**
443 * @brief Delete platform-specific client model instance
444 * @details Final implementation of the model deletion process for client models.
445 * This function handles the cleanup of client-specific resources
446 * and cannot be overridden by derived classes.
447 */
448 meshx_err_t plat_model_delete(void) final;
449
450protected:
451 /**
452 * @brief Update element_state_change field in client message header
453 * @details Overrides base class implementation to handle client-specific header structure
454 * (meshx_cli_model_send_param_header_t) which includes err_code and ctx fields.
455 *
456 * @param[in] element_state_change Result from element_state_change_handle()
457 * @param[in] msg_ptr Pointer to the message structure
458 */
459 void update_element_state_change_header(meshx_err_t element_state_change, meshx_ptr_t msg_ptr) override;
460
461public:
462
463 /**
464 * @brief Construct a new meshXClientModel
465 * @param[in] p_plat_model Platform model instance
466 * @param[in] model_id Model identifier
467 * @param[in] parent_element Parent element interface (optional)
468 * @param[in] parent_element_state Parent element state pointer (optional)
469 * @param[in] model_func_id Model function ID within the element (optional)
470 */
472 MESHX_MODEL *p_plat_model,
473 uint32_t model_id,
475 meshx_ptr_t parent_element_state = nullptr,
476 uint16_t model_func_id = 0
477 );
478
480};
481
482#endif /* _MESHX_MODEL_CLASS_HPP_ */
meshXClientModel()=delete
Interface class for MeshX elements.
Definition meshx_fwd_decl.hpp:82
meshXElementIF * parent_element
Definition meshx_model_class.hpp:42
void set_parent_element(meshXElementIF *parent)
Set the parent element for this model.
Definition meshx_model_class.hpp:137
virtual void on_baked(void)
Called when the model's parent element is baked.
Definition meshx_model_class.hpp:92
MESHX_MODEL * p_plat_model
Definition meshx_model_class.hpp:39
meshx_ptr_t p_parent_element_state
Definition meshx_model_class.hpp:43
meshx_ptr_t get_pub_struct(void) const
Get the publication structures.
Definition meshx_model_class.hpp:113
virtual uint16_t get_model_id(void) const =0
Get the model identifier.
MESHX_MODEL * get_plat_model(void) const
Get the platform-specific model instance.
Definition meshx_model_class.hpp:107
meshXModelIF()
Definition meshx_model_class.hpp:34
meshXElementIF * get_parent_element(void) const
Get the parent element of this model.
Definition meshx_model_class.hpp:143
virtual meshx_err_t prepare_element_msg(meshx_ptr_t *msg_ptr, size_t *msg_size)=0
Prepare message for element notification.
meshXModelIF(MESHX_MODEL *p_plat_model)
Definition meshx_model_class.hpp:35
meshx_ptr_t p_plat_pub
Definition meshx_model_class.hpp:40
virtual bool is_initialized(void) const =0
Check if the model is successfully initialized.
virtual meshx_err_t plat_model_create(MESHX_MODEL *p_plat_model_ptr=nullptr)=0
Create logical model instance.
meshx_ptr_t p_plat_gen
Definition meshx_model_class.hpp:41
meshx_ptr_t get_parent_element_state(void) const
Get the parent element state pointer.
Definition meshx_model_class.hpp:154
virtual void set_plat_model(MESHX_MODEL *p_model)
Set the platform-specific model instance.
Definition meshx_model_class.hpp:101
void set_gen_struct(meshx_ptr_t gen)
Set the generic structures.
Definition meshx_model_class.hpp:131
void set_parent_element_state(meshx_ptr_t state)
Set the parent element state pointer.
Definition meshx_model_class.hpp:149
meshx_ptr_t get_gen_struct(void) const
Get the generic structures.
Definition meshx_model_class.hpp:119
virtual meshx_err_t plat_model_delete(void)=0
Delete logical model instance.
virtual ~meshXModelIF()=default
virtual meshx_err_t element_state_change_handle(void)=0
Handle state change request from element.
void set_pub_struct(meshx_ptr_t pub)
Set the publication structures.
Definition meshx_model_class.hpp:125
meshx_err_t status
Definition meshx_model_class.hpp:179
void set_model_id(uint16_t id)
Set the model identifier.
Definition meshx_model_class.hpp:304
virtual meshx_err_t model_from_ble_cb(dev_struct_t *dev, control_task_msg_evt_t evt, meshx_ptr_t data)=0
Handle upstream BLE Mesh events.
meshxBaseModel_t * get_base_model(void) const
Get the base model instance.
Definition meshx_model_class.hpp:315
meshx_err_t get_init_status(void) const
Get the model initialization status.
Definition meshx_model_class.hpp:309
virtual void update_element_state_change_header(meshx_err_t element_state_change, meshx_ptr_t msg_ptr)=0
Update element_state_change field in message header.
uint16_t model_func_id
Definition meshx_model_class.hpp:181
uint16_t model_id
Definition meshx_model_class.hpp:180
uint16_t get_model_id(void) const override
Get the model identifier.
Definition meshx_model_class.hpp:292
meshxBaseModel_t * base_model
Definition meshx_model_class.hpp:178
void set_model_func_id(uint16_t func_id)
Set the model function identifier.
Definition meshx_model_class.hpp:287
void on_baked(void) override
Called when the model's parent element is baked.
Definition meshx_model_class.hpp:246
bool is_initialized(void) const override
Check if the model is successfully initialized.
Definition meshx_model_class.hpp:298
uint16_t get_model_func_id(void) const
Get the model function identifier.
Definition meshx_model_class.hpp:281
virtual meshx_err_t model_send(meshx_send_packet_params_t *params)=0
Send message through the model.
meshXServerModel()=delete
Deleted default constructor.
meshx_err_t send_to_parent_element(meshx_ptr_t msg_ptr, size_t msg_size)
Send message to parent element.
Definition meshx_model_class.cpp:119
meshXClientModel(MESHX_MODEL *p_plat_model, uint32_t model_id, meshXElementIF *parent_element=nullptr, meshx_ptr_t parent_element_state=nullptr, uint16_t model_func_id=0)
Construct a new meshXClientModel.
Definition meshx_model_class.cpp:216
meshx_err_t plat_model_create(MESHX_MODEL *p_plat_model_ptr=nullptr) final
Create platform-specific client model instance.
Definition meshx_model_class.cpp:241
void update_element_state_change_header(meshx_err_t element_state_change, meshx_ptr_t msg_ptr) override
Update element_state_change field in client message header.
Definition meshx_model_class.cpp:313
meshx_err_t plat_model_delete(void) final
Delete platform-specific client model instance.
Definition meshx_model_class.cpp:292
meshx_err_t model_handle_from_ble_cb(dev_struct_t *p_dev, evt_model_id_t evt_model_id, meshx_ptr_t params)
Handle upstream BLE Mesh events.
Definition meshx_model_class.cpp:160
void set_plat_model(MESHX_MODEL *p_model) override
Set the platform-specific model instance.
Definition meshx_model_class.cpp:92
virtual ~meshXModel()
Destructor for meshXModel.
Definition meshx_model_class.cpp:103
meshXModel(MESHX_MODEL *p_plat_model, uint32_t model_id, meshXElementIF *parent_element=nullptr, uint16_t model_func_id=0)
Constructs a new meshXModel instance.
Definition meshx_model_class.cpp:65
void update_element_state_change_header(meshx_err_t element_state_change, meshx_ptr_t msg_ptr) override
Update element_state_change field in server message header.
Definition meshx_model_class.cpp:334
meshXServerModel(MESHX_MODEL *p_plat_model, uint32_t model_id, meshXElementIF *parent_element=nullptr, meshx_ptr_t parent_element_state=nullptr, uint16_t model_func_id=0)
Construct a new Server Model.
Definition meshx_model_class.cpp:200
This file declares the meshXBaseModel class and its derived Client and Server classes.
struct meshx_model meshx_model_t
struct meshx_ctx meshx_ctx_t
Structure to hold context information for BLE Mesh operations.
void * meshx_ptr_t
Definition meshx_ble_mesh_cmn_def.h:636
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
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
#define MESHX_SERVER_MODEL_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:31
#define MESHX_CLIENT_MODEL_TEMPLATE_PARAMS
Definition meshx_fwd_decl.hpp:34
#define MESHX_MODEL_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:29
#define MESHX_SERVER_MODEL_TEMPLATE_PARAMS
Definition meshx_fwd_decl.hpp:32
#define MESHX_CLIENT_MODEL_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:33
control_task_msg_evt_t evt_model_id_t
Definition meshx_model_class.hpp:29
struct meshx_cli_model_send_param_header meshx_cli_model_send_param_header_t
Definition meshx_model_class.hpp:417
struct meshx_srv_model_send_param_header meshx_srv_model_send_param_header_t
Definition meshx_model_class.hpp:354
**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
uint8_t state
Definition meshx_serial.c:39
Definition meshx_model_class.hpp:410
meshx_ctx_t ctx
Definition meshx_model_class.hpp:413
uint8_t err_code
Definition meshx_model_class.hpp:411
meshx_model_t model
Definition meshx_model_class.hpp:412
meshx_err_t element_state_change
Definition meshx_model_class.hpp:414
Structure for server model send parameters.
Definition meshx_model_class.hpp:349
meshx_model_t model
Definition meshx_model_class.hpp:350
meshx_err_t element_state_change
Definition meshx_model_class.hpp:351