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

Functions

 meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::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.
void meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::set_plat_model (MESHX_MODEL *p_model) override
 Set the platform-specific model instance.
virtual meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::~meshXModel ()
 Destructor for meshXModel.
meshx_err_t meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::send_to_parent_element (meshx_ptr_t msg_ptr, size_t msg_size)
 Send message to parent element.
meshx_err_t meshXModel< meshxBaseModel_t, meshx_send_packet_params_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.
 meshXServerModel< meshxBaseServerModel_t, meshx_send_packet_params_t >::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.
 meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::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.
meshx_err_t meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::plat_model_create (MESHX_MODEL *p_plat_model_ptr=nullptr) final
 Create platform-specific client model instance.
meshx_err_t meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::plat_model_delete (void) final
 Delete platform-specific client model instance.
void meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::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.
void meshXServerModel< meshxBaseServerModel_t, meshx_send_packet_params_t >::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.

Detailed Description

Function Documentation

◆ meshXClientModel()

template<typename meshxBaseClientModel_t, typename meshx_send_packet_params_t>
meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::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.

Constructs a new meshXClientModel instance.

Parameters
[in]p_plat_modelPlatform model instance
[in]model_idModel identifier
[in]parent_elementParent element interface (optional)
[in]parent_element_stateParent element state pointer (optional)
[in]model_func_idModel function ID within the element (optional)
Note
This constructor delegates to the base meshXModel constructor. Derived client models should use this constructor to ensure proper initialization.
218{
220}
Base class for all client models in the mesh network.
Definition meshx_model_class.hpp:429
meshXElementIF * parent_element
Definition meshx_model_class.hpp:42
MESHX_MODEL * p_plat_model
Definition meshx_model_class.hpp:39
void set_parent_element_state(meshx_ptr_t state)
Set the parent element state pointer.
Definition meshx_model_class.hpp:149
uint16_t model_func_id
Definition meshx_model_class.hpp:181
uint16_t model_id
Definition meshx_model_class.hpp:180
meshXModel(MESHX_MODEL *p_plat_model, uint32_t model_id, meshXElementIF *parent_element=nullptr, uint16_t model_func_id=0)
Definition meshx_model_class.cpp:65
#define MESHX_CLIENT_MODEL_TEMPLATE_PARAMS
Definition meshx_fwd_decl.hpp:34

◆ meshXModel()

template<typename meshxBaseModel_t, typename meshx_send_packet_params_t>
meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::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.

This constructor initializes a meshXModel object with the given platform model, model ID, and optional parent element. It sets up the base model and model interface for BLE mesh communication.

Parameters
[in]p_plat_modelPointer to the platform model instance
[in]model_idUnique identifier for this model
[in]parent_elementOptional pointer to the parent element
[in]model_func_idOptional model function ID within the element
Note
The constructor allocates memory for the base model and model interface. If memory allocation fails, the status will be set to MESHX_NO_MEM.
72{
75 this->model_id = (uint16_t)model_id;
76 this->status = MESHX_SUCCESS;
77
78 /* base_model needs to be used logically by the element composition */
81 return this->model_handle_from_ble_cb(dev, evt, param);
82 });
83
85
86 /* Create logical model instance is now handled by derived classes or post-construction */
88}
void set_parent_element(meshXElementIF *parent)
Set the parent element for this model.
Definition meshx_model_class.hpp:137
meshXModelIF()
Definition meshx_model_class.hpp:34
meshXModel class
Definition meshx_model_class.hpp:175
meshx_err_t status
Definition meshx_model_class.hpp:179
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
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

◆ meshXServerModel()

template<typename meshxBaseServerModel_t, typename meshx_send_packet_params_t>
meshXServerModel< meshxBaseServerModel_t, meshx_send_packet_params_t >::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.

Constructs a new meshXServerModel instance.

Parameters
[in]p_plat_modelPlatform-specific model instance
[in]model_idUnique identifier for this model
[in]parent_elementParent element interface (optional)
[in]parent_element_stateParent element state pointer (optional)
[in]model_func_idModel function ID within the element (optional)

Initializes a server model with platform-specific implementation and associates it with an optional parent element

Note
This constructor delegates to the base meshXModel constructor.
202{
204}
Base class for all server models in MeshX.
Definition meshx_model_class.hpp:366
#define MESHX_SERVER_MODEL_TEMPLATE_PARAMS
Definition meshx_fwd_decl.hpp:32

◆ model_handle_from_ble_cb()

template<typename meshxBaseModel_t, typename meshx_send_packet_params_t>
meshx_err_t meshXModel< meshxBaseModel_t, meshx_send_packet_params_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.

Callback function invoked when a BLE event is received for the model.

Static callback function that routes messages and events coming from the BLE Mesh network to the appropriate model instance. This function extracts the model instance from the device structure and invokes the instance's model_from_ble_cb method.

Parameters
[in]p_devDevice structure containing sender information
[in]evt_model_idEvent type indicating the nature of the message
[in]paramsEvent-specific data payload
Returns
MESHX_SUCCESS if event handled successfully, error code otherwise
165{
167
168 // Call derived class implementation
170 if(err)
171 {
172 MESHX_LOGE(MODULE_ID_COMMON, "Error in model_from_ble_cb: %d", err);
173 return err;
174 }
175
176 // Get message from derived class
177 meshx_ptr_t msg_ptr = nullptr;
178 size_t msg_size = 0;
180 if(err)
181 {
182 MESHX_LOGE(MODULE_ID_COMMON, "Error in prepare_element_msg: %d", err);
183 return err;
184 }
185
186 // Send to parent element using common implementation
188}
virtual meshx_err_t prepare_element_msg(meshx_ptr_t *msg_ptr, size_t *msg_size)=0
Prepare message for element notification.
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.
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
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114

◆ plat_model_create()

template<typename meshxBaseClientModel_t, typename meshx_send_packet_params_t>
meshx_err_t meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::plat_model_create ( MESHX_MODEL * p_plat_model_ptr = nullptr)
finalprivatevirtual

Create platform-specific client model instance.

Creates and initializes a client model instance.

Final implementation of the model creation process for client models. This function handles the initialization of client-specific features and cannot be overridden by derived classes.

Returns
MESHX_SUCCESS on successful model creation and initialization, error code otherwise

This function handles the platform-specific model creation process for client models. It initializes client-specific features and cannot be overridden by derived classes.

Template Parameters
MESHX_MODELPlatform-specific model type
meshxBaseModel_tBase model implementation type
meshx_send_packet_params_tType for send packet parameters
Returns
meshx_err_t Returns an error code indicating the result of the operation.
  • MESHX_SUCCESS on successful model creation and initialization
  • MESHX_ERR_NO_MEM if memory allocation fails
  • Other error codes for platform-specific failures
Note
This is a final function and cannot be overridden by derived classes.

Implements meshXModelIF.

242{
244
247
248 if (p_plat_model_ptr) {
250 }
251
253 if (!p_use)
254 {
255 MESHX_LOGE(MODULE_ID_COMMON, "No platform model pointer available");
256 return MESHX_INVALID_STATE;
257 }
258
260 if (err)
261 {
262 MESHX_LOGE(MODULE_ID_COMMON, "Failed to create client model");
264 if (err)
265 {
266 MESHX_LOGE(MODULE_ID_COMMON, "Failed to delete client model");
267 }
268 }
269 else
270 {
271 /* Set the publication and generic structures */
272 this->set_pub_struct(p_pub);
273 this->set_gen_struct(p_gen);
274 }
275 return err;
276}
meshx_ptr_t get_pub_struct(void) const
Get the publication structures.
Definition meshx_model_class.hpp:113
MESHX_MODEL * get_plat_model(void) const
Get the platform-specific model instance.
Definition meshx_model_class.hpp:107
void set_gen_struct(meshx_ptr_t gen)
Set the generic structures.
Definition meshx_model_class.hpp:131
meshx_ptr_t get_gen_struct(void) const
Get the generic structures.
Definition meshx_model_class.hpp:119
void set_pub_struct(meshx_ptr_t pub)
Set the publication structures.
Definition meshx_model_class.hpp:125
uint16_t get_model_id(void) const override
Definition meshx_model_class.hpp:292
meshx_err_t plat_model_delete(void) final
Delete platform-specific client model instance.
Definition meshx_model_class.cpp:292
meshx_err_t meshx_plat_client_create(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_cli, uint16_t model_id)
Creates and initializes a generic client model for BLE Mesh.

◆ plat_model_delete()

template<typename meshxBaseClientModel_t, typename meshx_send_packet_params_t>
meshx_err_t meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::plat_model_delete ( void )
finalprivatevirtual

Delete platform-specific client model instance.

Deletes the client model instance.

Final implementation of the model deletion process for client models. This function handles the cleanup of client-specific resources and cannot be overridden by derived classes.

This function is responsible for deleting the client model instance and releasing any associated resources.

Returns
meshx_err_t Returns an error code indicating the result of the operation.
  • MESHX_SUCCESS on successful deletion
  • Other error codes for platform-specific failures
Note
This is a final function and cannot be overridden by derived classes.

Implements meshXModelIF.

293{
296
298 this->get_plat_model(),
299 &p_pub,
300 &p_gen);
301
302 if (err == MESHX_SUCCESS)
303 {
304 this->set_pub_struct(p_pub);
305 this->set_gen_struct(p_gen);
306 }
307
308 return err;
309}
meshx_err_t meshx_plat_client_delete(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_cli)
Deletes a generic client model from BLE Mesh.

◆ send_to_parent_element()

template<typename meshxBaseModel_t, typename meshx_send_packet_params_t>
meshx_err_t meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::send_to_parent_element ( meshx_ptr_t msg_ptr,
size_t msg_size )

Send message to parent element.

Common implementation for sending messages to the parent element. This handles the common pattern of checking parent element, calling element_state_change_handle(), and calling on_model_cb(). Type-specific header casting is delegated to update_element_state_change_header().

Parameters
[in]msg_ptrPointer to the message structure
[in]msg_sizeSize of the message structure
Returns
MESHX_SUCCESS if message sent successfully, error code otherwise

Common implementation to send a message to the parent element.

120{
121 if (!msg_ptr || msg_size == 0)
122 {
123 MESHX_LOGE(MODULE_ID_COMMON, "Invalid message parameters");
124 return MESHX_INVALID_ARG;
125 }
126
127 /* Send the state change event to the respective Element */
128 if (this->get_parent_element())
129 {
131
133 {
135 }
136 else
137 {
138 MESHX_LOGE(MODULE_ID_COMMON, "Parent element state is null for model_id: %04x", this->get_model_id());
140 }
141
142 // Delegate type-specific header casting to derived class
144
146 }
147 else
148 {
149 MESHX_LOGE(MODULE_ID_COMMON, "Parent element is null");
150 }
151
152 return MESHX_INVALID_STATE;
153}
virtual meshx_err_t on_model_cb(meshx_ptr_t param, size_t param_size)=0
Handle model callback from child models.
meshXElementIF * get_parent_element(void) const
Get the parent element of this model.
Definition meshx_model_class.hpp:143
meshx_ptr_t get_parent_element_state(void) const
Get the parent element state pointer.
Definition meshx_model_class.hpp:154
virtual meshx_err_t element_state_change_handle(void)=0
Handle state change request from element.
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.

◆ set_plat_model()

template<typename meshxBaseModel_t, typename meshx_send_packet_params_t>
void meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::set_plat_model ( MESHX_MODEL * p_model)
overridevirtual

Set the platform-specific model instance.

Parameters
[in]p_modelPointer to the platform model instance

Reimplemented from meshXModelIF.

93{
95 if (base_model)
96 {
97 base_model->set_plat_model_ptr((meshx_ptr_t)p_model);
98 }
99}
virtual void set_plat_model(MESHX_MODEL *p_model)
Set the platform-specific model instance.
Definition meshx_model_class.hpp:101

◆ update_element_state_change_header() [1/2]

template<typename meshxBaseClientModel_t, typename meshx_send_packet_params_t>
void meshXClientModel< meshxBaseClientModel_t, meshx_send_packet_params_t >::update_element_state_change_header ( meshx_err_t element_state_change,
meshx_ptr_t msg_ptr )
overrideprotectedvirtual

Update element_state_change field in client message header.

Overrides base class implementation to handle client-specific header structure (meshx_cli_model_send_param_header_t) which includes err_code and ctx fields.

Parameters
[in]element_state_changeResult from element_state_change_handle()
[in]msg_ptrPointer to the message structure

Implements meshXModel< meshxBaseClientModel_t, meshx_send_packet_params_t >.

314{
315 // Cast to client header - safe because this is a client model
317 cli_header->element_state_change = element_state_change;
318}

◆ update_element_state_change_header() [2/2]

template<typename meshxBaseServerModel_t, typename meshx_send_packet_params_t>
void meshXServerModel< meshxBaseServerModel_t, meshx_send_packet_params_t >::update_element_state_change_header ( meshx_err_t element_state_change,
meshx_ptr_t msg_ptr )
overrideprotectedvirtual

Update element_state_change field in server message header.

Overrides base class implementation to handle server-specific header structure (meshx_srv_model_send_param_header_t) which doesn't include err_code and ctx.

Parameters
[in]element_state_changeResult from element_state_change_handle()
[in]msg_ptrPointer to the message structure

Implements meshXModel< meshxBaseServerModel_t, meshx_send_packet_params_t >.

335{
336 // Cast to server header - safe because this is a server model
338 srv_header->element_state_change = element_state_change;
339}

◆ ~meshXModel()

template<typename meshxBaseModel_t, typename meshx_send_packet_params_t>
meshXModel< meshxBaseModel_t, meshx_send_packet_params_t >::~meshXModel ( )
virtual

Destructor for meshXModel.

Virtual destructor to ensure proper cleanup of derived classes and the base_model member.

104{
105 /* Delete logical model instance is now handled by derived classes */
106 if (base_model)
107 {
108 delete base_model;
109 base_model = nullptr;
110 }
111}