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

A template class for creating Generic Battery Server models. More...

#include <meshx_model_battery.hpp>

Inheritance diagram for meshXGenericBatteryServerModel:
meshXServerModel< meshXBaseGenericServerModel, meshx_gen_battery_send_params_t > meshXModel< meshXBaseGenericServerModel, meshx_gen_battery_send_params_t > meshXModelIF

Public Member Functions

meshx_err_t model_send (meshx_gen_battery_send_params_t *params) override
 Send a packet to the MeshX stack based on the given parameters.
meshx_err_t model_from_ble_cb (dev_struct_t *, control_task_msg_evt_t, meshx_ptr_t) override
 Callback function for handling BLE mesh events for the Generic Battery Server Model.
meshx_err_t prepare_element_msg (meshx_ptr_t *msg_ptr, size_t *msg_size) override
 Prepare message for element notification.
meshx_err_t element_state_change_handle (void) override
 Handle state change request from element.
 meshXGenericBatteryServerModel (meshXElementIF *parent_element=nullptr, meshx_ptr_t parent_element_state=nullptr, uint16_t model_func_id=0)
 Constructor for the Generic Battery Server Model.
 ~meshXGenericBatteryServerModel ()=default
Public Member Functions inherited from meshXServerModel< meshXBaseGenericServerModel, meshx_gen_battery_send_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.
Public Member Functions inherited from meshXModel< meshXBaseGenericServerModel, meshx_gen_battery_send_params_t >
void set_plat_model (MESHX_MODEL *p_model) override
 Set the platform-specific model instance.
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.
void on_baked (void) override
 Called when the model's parent element is baked.
virtual ~meshXModel ()
 Destructor for meshXModel.
meshx_err_t send_to_parent_element (meshx_ptr_t msg_ptr, size_t msg_size)
 Send message to parent element.
uint16_t get_model_func_id (void) const
 Get the model function identifier.
void set_model_func_id (uint16_t func_id)
 Set the model function identifier.
uint16_t get_model_id (void) const override
 Get the model identifier.
bool is_initialized (void) const override
 Check if the model is successfully initialized.
void set_model_id (uint16_t id)
 Set the model identifier.
meshx_err_t get_init_status (void) const
 Get the model initialization status.
meshXBaseGenericServerModelget_base_model (void) const
 Get the base model instance.
 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.
Public Member Functions inherited from meshXModelIF
 meshXModelIF ()
 meshXModelIF (MESHX_MODEL *p_plat_model)
virtual ~meshXModelIF ()=default
MESHX_MODEL * get_plat_model (void) const
 Get the platform-specific model instance.
meshx_ptr_t get_pub_struct (void) const
 Get the publication structures.
meshx_ptr_t get_gen_struct (void) const
 Get the generic structures.
void set_pub_struct (meshx_ptr_t pub)
 Set the publication structures.
void set_gen_struct (meshx_ptr_t gen)
 Set the generic structures.
void set_parent_element (meshXElementIF *parent)
 Set the parent element for this model.
meshXElementIFget_parent_element (void) const
 Get the parent element of this model.
void set_parent_element_state (meshx_ptr_t state)
 Set the parent element state pointer.
meshx_ptr_t get_parent_element_state (void) const
 Get the parent element state pointer.

Private Member Functions

meshx_err_t plat_model_create (MESHX_MODEL *p_plat_model_ptr=nullptr) override
 Creates and initializes a server model instance for the Generic Battery Server.
meshx_err_t plat_model_delete (void) override
 Deletes the Generic Battery Server model and its associated resources.

Private Attributes

meshx_gen_battery_model_state_t model_state
meshx_battery_srv_el_msg_t element_msg
bool element_msg_prepared = false

Additional Inherited Members

Protected Member Functions inherited from meshXServerModel< meshXBaseGenericServerModel, meshx_gen_battery_send_params_t >
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.

Detailed Description

A template class for creating Generic Battery Server models.

This class is derived from meshXServerModel and provides a convenient interface for creating Generic Battery Server models. It handles the Generic Battery state change notifications from the MeshX stack and publishes the state change event to the element layer.

Constructor & Destructor Documentation

◆ meshXGenericBatteryServerModel()

meshXGenericBatteryServerModel::meshXGenericBatteryServerModel ( meshXElementIF * parent_element = nullptr,
meshx_ptr_t parent_element_state = nullptr,
uint16_t model_func_id = 0 )

Constructor for the Generic Battery Server Model.

Parameters
[in]parent_elementPointer to the parent element (meshXElementIF)
[in]parent_element_statePointer to the parent element's state structure.
[in]model_func_idFunctional ID for the model instance.
392{
393 /* Note: plat_model_create() is now called explicitly during composition bake */
394}
meshXElementIF * parent_element
Definition meshx_model_class.hpp:42
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)
Definition meshx_model_class.cpp:200
#define MESHX_MODEL_ID_GEN_BATTERY_SRV
Definition meshx_ble_mesh_cmn_def.h:82

◆ ~meshXGenericBatteryServerModel()

meshXGenericBatteryServerModel::~meshXGenericBatteryServerModel ( )
default

Member Function Documentation

◆ element_state_change_handle()

meshx_err_t meshXGenericBatteryServerModel::element_state_change_handle ( void )
overridevirtual

Handle state change request from element.

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. Note: The actual state is maintained in the element layer, not the model layer.

Returns
  • MESHX_SUCCESS: State change handled successfully
  • MESHX_INVALID_ARG: Invalid parameter
  • MESHX_INVALID_STATE: No state change detected

Implements meshXModelIF.

353{
354 auto *el_state =
356
357 if (!el_state) {
358 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid parameter in element_state_change_handle");
359 return MESHX_INVALID_ARG;
360 }
361
362 if(memcmp(&model_state, el_state, sizeof(meshx_gen_battery_model_state_t)) != 0)
363 {
365 "Battery state change request: level=%d discharge=%d charge=%d",
366 el_state->battery_level, el_state->time_to_discharge, el_state->time_to_charge);
367 // Update the model state
368 *el_state = model_state;
369 }
370 else
371 {
372 return MESHX_INVALID_STATE;
373 }
374 return MESHX_SUCCESS;
375}
meshx_gen_battery_model_state_t model_state
Definition meshx_model_battery.hpp:129
meshx_ptr_t get_parent_element_state(void) const
Get the parent element state pointer.
Definition meshx_model_class.hpp:154
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
@ MESHX_INVALID_STATE
Definition meshx_err.h:49
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114
struct meshx_gen_battery_model_state meshx_gen_battery_model_state_t
Definition meshx_model_battery.hpp:43
**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
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:34

◆ model_from_ble_cb()

meshx_err_t meshXGenericBatteryServerModel::model_from_ble_cb ( dev_struct_t * p_dev,
control_task_msg_evt_t model_id,
meshx_ptr_t params )
overridevirtual

Callback function for handling BLE mesh events for the Generic Battery Server Model.

Parameters
[in]p_devPointer to the device structure
[in]model_idModel ID associated with the event
[in]paramsPointer to the event parameters
Returns
MESHX_SUCCESS if successful, error code otherwise

Implements meshXModel< meshXBaseGenericServerModel, meshx_gen_battery_send_params_t >.

276{
277 if(!params || !p_dev)
278 {
279 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid parameters");
280 return MESHX_INVALID_ARG;
281 }
283 {
284 /* Callback triggered not for this model */
285 return MESHX_SUCCESS;
286 }
287
288 auto *param = static_cast<meshx_gen_srv_cb_param_t *>(params);
289
290 // Store the message in member variable for later use by prepare_element_msg
291 element_msg = {
292 .header = {
293 .model = param->model,
294 .element_state_change = MESHX_SUCCESS,
295 },
296 .state = {
297 .battery_level = param->state_change.battery_status.battery_level,
298 .time_to_discharge = param->state_change.battery_status.time_to_discharge,
299 .time_to_charge = param->state_change.battery_status.time_to_charge,
300 .presence = param->state_change.battery_status.presence,
301 .charge_level = param->state_change.battery_status.charge_level,
302 .charge_type = param->state_change.battery_status.charge_type,
303 }
304 };
305
307 return MESHX_SUCCESS;
308}
bool element_msg_prepared
Definition meshx_model_battery.hpp:135
meshx_battery_srv_el_msg_t element_msg
Definition meshx_model_battery.hpp:132
struct meshx_gen_srv_cb_param meshx_gen_srv_cb_param_t

◆ model_send()

meshx_err_t meshXGenericBatteryServerModel::model_send ( meshx_gen_battery_send_params_t * params)
overridevirtual

Send a packet to the MeshX stack based on the given parameters.

This function takes a pointer to a meshx_gen_battery_send_params_t structure as input and sends the corresponding packet to the MeshX stack for processing.

Parameters
[in]paramsPointer to the meshx_gen_battery_send_params_t structure containing the parameters
Returns
MESHX_SUCCESS on success, or an error code on failure

Implements meshXModel< meshXBaseGenericServerModel, meshx_gen_battery_send_params_t >.

245{
246 if (!params|| !params->model || !params->ctx)
247 {
248 return MESHX_INVALID_ARG;
249 }
251 // Battery server doesn't modify state directly - just responds with current status
252 meshx_gen_srv_state_change_t state_change = {0}; // Empty state change for battery status
253 meshx_gen_server_send_params_t send_params = {
254 .p_model = params->model,
255 .p_ctx = params->ctx,
256 .state_change = state_change,
257 .data_len = 0 // No state data for battery status response
258 };
259 return this->get_base_model()->plat_send_msg(&send_params);
260}
meshx_err_t plat_send_msg(meshx_gen_server_send_params_t *params) override
Sends a status message for the Generic Server model.
Definition meshx_base_model_generic.cpp:401
meshXBaseGenericServerModel * get_base_model(void) const
Definition meshx_model_class.hpp:315
struct meshx_gen_server_send_params { meshx_model_t *p_model; meshx_ctx_t *p_ctx; meshx_gen_srv_state_change_t state_change; size_t data_len; } meshx_gen_server_send_params_t
Definition meshx_base_model_generic.hpp:135
#define MESHX_MODEL_OP_GEN_BATTERY_STATUS
Definition meshx_ble_mesh_cmn_def.h:290
uint32_t opcode
Definition meshx_ble_mesh_cmn.h:49
meshx_ctx_t * ctx
Definition meshx_model_battery.hpp:51
meshx_model_t * model
Definition meshx_model_battery.hpp:50

◆ plat_model_create()

meshx_err_t meshXGenericBatteryServerModel::plat_model_create ( MESHX_MODEL * p_plat_model_ptr = nullptr)
overrideprivatevirtual

Creates and initializes a server model instance for the Generic Battery Server.

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

Parameters
[in]p_plat_model_ptrOptional pointer to a pre-allocated platform model structure.
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.

414{
415 if (p_plat_model_ptr) {
416 this->set_plat_model(p_plat_model_ptr);
417 }
418
419 meshx_ptr_t p_pub = this->get_pub_struct();
420 meshx_ptr_t p_gen = this->get_gen_struct();
422
423 // If already created, skip platform creation to avoid memory leaks
424 if (p_pub != nullptr || p_gen != nullptr)
425 {
426 MESHX_MODEL* p_use = this->get_plat_model();
427 if (p_use) {
429 memcpy((void*)&p_use->model_id, &model_id, sizeof(model_id));
430 memcpy((void*)&p_use->pub, &p_pub, sizeof(p_pub));
431 memcpy((void*)&p_use->user_data, &p_gen, sizeof(p_gen));
432 }
433 return MESHX_SUCCESS;
434 }
435
436 err = meshx_plat_battery_gen_srv_create(this->get_plat_model(), &p_pub, &p_gen);
437 if(err)
438 {
439 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to create Generic Battery Server Model");
440 }
441 else
442 {
443 /* Set the publication and generic structures */
444 this->set_pub_struct(p_pub);
445 this->set_gen_struct(p_gen);
446 }
447 return err;
448}
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
void set_plat_model(MESHX_MODEL *p_model) override
Definition meshx_model_class.cpp:92
void * meshx_ptr_t
Definition meshx_ble_mesh_cmn_def.h:636
meshx_err_t meshx_plat_battery_gen_srv_create(void *p_model, void **p_pub, void **p_battery_srv)
Creates a Generic Battery Server model and its publication context.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43

◆ plat_model_delete()

meshx_err_t meshXGenericBatteryServerModel::plat_model_delete ( void )
overrideprivatevirtual

Deletes the Generic Battery Server model and its associated resources.

This function frees the memory allocated for the Generic Battery Server and sets the pointer to NULL. It also deletes the model publication resources associated with the server.

Returns
  • MESHX_SUCCESS: Model and publication deleted successfully.
  • MESHX_FAIL: Failed to delete the model or publication.

Implements meshXModelIF.

464{
465 meshx_ptr_t p_pub = this->get_pub_struct();
466 meshx_ptr_t p_gen = this->get_gen_struct();
467
468 meshx_err_t err = meshx_plat_gen_srv_delete(&p_pub, &p_gen);
469 if (err)
470 {
471 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to delete Generic Battery Server Model");
472 }
473 else
474 {
475 this->set_pub_struct(nullptr);
476 this->set_gen_struct(nullptr);
477 }
478 return err;
479}
meshx_err_t meshx_plat_gen_srv_delete(void **p_pub, void **p_srv)
Deletes the Generic OnOff Server model and its associated resources.

◆ prepare_element_msg()

meshx_err_t meshXGenericBatteryServerModel::prepare_element_msg ( meshx_ptr_t * msg_ptr,
size_t * msg_size )
overridevirtual

Prepare message for element notification.

Returns pointer to the stored element message if it has been prepared

Parameters
[out]msg_ptrPointer to message structure (output parameter)
[out]msg_sizeSize of the message structure (output parameter)
Returns
MESHX_SUCCESS if message prepared successfully, error code otherwise

Implements meshXModelIF.

321{
322 if (!msg_ptr || !msg_size)
323 {
324 return MESHX_INVALID_ARG;
325 }
326
328 {
329 return MESHX_NOT_SUPPORTED;
330 }
331
332 *msg_ptr = &element_msg;
333 *msg_size = sizeof(element_msg);
334
335 return MESHX_SUCCESS;
336}
@ MESHX_NOT_SUPPORTED
Definition meshx_err.h:51

Field Documentation

◆ element_msg

meshx_battery_srv_el_msg_t meshXGenericBatteryServerModel::element_msg
private

◆ element_msg_prepared

bool meshXGenericBatteryServerModel::element_msg_prepared = false
private

◆ model_state

meshx_gen_battery_model_state_t meshXGenericBatteryServerModel::model_state
private

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