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

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

#include <meshx_model_onoff.hpp>

Inheritance diagram for meshXGenericOnOffServerModel:
meshXServerModel< meshXBaseGenericServerModel, meshx_gen_onoff_send_params_t > meshXModel< meshXBaseGenericServerModel, meshx_gen_onoff_send_params_t > meshXModelIF

Public Member Functions

meshx_err_t model_send (meshx_gen_onoff_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
 This function is the callback for the Generic OnOff Server model.
meshx_err_t prepare_element_msg (meshx_ptr_t *msg_ptr, size_t *msg_size) override
 Prepare the element message for notification.
meshx_err_t request_status (uint16_t dst_addr=0)
 Request to send the Generic OnOff status to a specific address.
meshx_err_t element_state_change_handle (void) override
 Handle state change request from element.
 meshXGenericOnOffServerModel (meshXElementIF *parent_element=nullptr, meshx_ptr_t parent_element_state=nullptr, uint16_t model_func_id=0)
 A template class for creating Generic OnOff Server models.
 ~meshXGenericOnOffServerModel () override=default
Public Member Functions inherited from meshXServerModel< meshXBaseGenericServerModel, meshx_gen_onoff_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_onoff_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.
meshx_err_t plat_model_delete (void) override
 Deletes the Generic OnOff Server model and its associated resources.

Private Attributes

meshx_gen_onoff_model_state_t model_state
meshx_on_off_srv_el_msg_t element_msg
bool element_msg_prepared

Additional Inherited Members

Protected Member Functions inherited from meshXServerModel< meshXBaseGenericServerModel, meshx_gen_onoff_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 OnOff Server models.

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

Constructor & Destructor Documentation

◆ meshXGenericOnOffServerModel()

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

A template class for creating Generic OnOff Server models.

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

Template Parameters
meshxBaseServerModel_tThe type of the meshXBaseServerModel class to be used.
meshx_send_packet_params_tThe type of the meshXSendPacketParams structure used for sending packets.
Parameters
[in]parent_elementA pointer 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.
352 model_state({0, 1, 0}),
354{
355}
meshx_gen_onoff_model_state_t model_state
Definition meshx_model_onoff.hpp:129
bool element_msg_prepared
Definition meshx_model_onoff.hpp:135
meshXElementIF * parent_element
Definition meshx_model_class.hpp:42
uint16_t model_func_id
Definition meshx_model_class.hpp:181
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_ONOFF_SRV
Definition meshx_ble_mesh_cmn_def.h:70

◆ ~meshXGenericOnOffServerModel()

meshXGenericOnOffServerModel::~meshXGenericOnOffServerModel ( )
overridedefault

Member Function Documentation

◆ element_state_change_handle()

meshx_err_t meshXGenericOnOffServerModel::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

Implements meshXModelIF.

462{
463
464 auto *el_state = static_cast<meshx_gen_onoff_model_state_t*>(this->get_parent_element_state());
465
466 if (!el_state) {
467 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid parameter in element_state_change_handle");
468 return MESHX_INVALID_ARG;
469 }
470
471 if(el_state->on_off != model_state.on_off)
472 {
473 *el_state = model_state;
474 }
475 else
476 {
477 return MESHX_INVALID_STATE;
478 }
479 return MESHX_SUCCESS;
480}
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_LOGE(module_id, format,...)
Definition meshx_log.h:114
**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_gen_onoff_model_state meshx_gen_onoff_model_state_t
Definition meshx_model_onoff.hpp:38
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:34

◆ model_from_ble_cb()

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

This function is the callback for the Generic OnOff Server model.

This function is triggered whenever a Generic OnOff message is received from the MeshX stack. It handles Generic OnOff state change notifications from the MeshX stack and publishes the state change event to the element layer.

Parameters
[in]p_devA pointer to the device structure.
[in]model_idThe unique identifier of the BLE mesh model.
[in]paramsA pointer to the callback parameter structure containing the details of the received message.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid argument
  • MESHX_FAIL: Other failures

Implements meshXModel< meshXBaseGenericServerModel, meshx_gen_onoff_send_params_t >.

536{
537 if(!params || !p_dev)
538 {
539 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid parameters");
540 return MESHX_INVALID_ARG;
541 }
542 if(model_id != this->get_model_id())
543 {
544 /* Callback triggered not for this model */
545 return MESHX_SUCCESS;
546 }
547
548 auto *param = static_cast<meshx_gen_srv_cb_param_t *>(params);
549
550 // Initialize flag - message not prepared yet
551 element_msg_prepared = false;
552
553 bool send_reply = (param->ctx.opcode != MESHX_MODEL_OP_GEN_ONOFF_SET_UNACK);
554 switch (param->ctx.opcode)
555 {
557 // GET opcode - no state change, no element notification needed
558 break;
561 {
562 model_state.on_off = param->state_change.onoff_set.onoff;
563 model_state.next_on_off = !model_state.on_off;
564
565 // Prepare the message (store in member variable)
566 element_msg.header.model = param->model;
567 element_msg.state = model_state;
569 break;
570 }
572 // Servers may receive their own status messages in some platform event flows; ignore silently
573 return MESHX_SUCCESS;
574 default:
575 // Unknown opcode - return error
576 return MESHX_NOT_SUPPORTED;
577 }
578
579 meshx_err_t send_err = MESHX_SUCCESS;
580 if (send_reply)
581 {
582 /* Response to the requester */
583 meshx_ctx_t reply_ctx = param->ctx;
584 reply_ctx.dst_addr = param->ctx.src_addr;
585
586 meshx_gen_onoff_send_params_t send_params = {
587 .model = &param->model,
588 .ctx = &reply_ctx,
589 .state = model_state,
590 .tid = 0
591 };
592 send_err = this->model_send(&send_params);
593 }
594
595 /* Publish to the configured publication address if present */
596 if (param->model.pub_addr != MESHX_ADDR_UNASSIGNED && param->ctx.src_addr != param->model.pub_addr)
597 {
598 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "PUB: src|pub %x|%x", param->ctx.src_addr, param->model.pub_addr);
599 meshx_ctx_t pub_ctx = param->ctx;
600 pub_ctx.dst_addr = param->model.pub_addr;
601
602 meshx_gen_onoff_send_params_t send_params = {
603 .model = &param->model,
604 .ctx = &pub_ctx,
605 .state = model_state,
606 .tid = 0
607 };
608 meshx_err_t pub_err = this->model_send(&send_params);
609 if (send_err == MESHX_SUCCESS) send_err = pub_err;
610 }
611
612 return send_err;
613
614 // If we reach here, message was not prepared for element notification
615 // Return error so base layer doesn't send uninitialized message
617 {
618 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "No element notification needed for opcode: %04x", param->ctx.opcode);
619 return MESHX_NOT_SUPPORTED;
620 }
621
622 return MESHX_SUCCESS;
623}
meshx_err_t model_send(meshx_gen_onoff_send_params_t *params) override
Send a packet to the MeshX stack based on the given parameters.
Definition meshx_model_onoff.cpp:493
meshx_on_off_srv_el_msg_t element_msg
Definition meshx_model_onoff.hpp:132
uint16_t get_model_id(void) const override
Definition meshx_model_class.hpp:292
struct meshx_ctx meshx_ctx_t
Structure to hold context information for BLE Mesh operations.
#define MESHX_MODEL_OP_GEN_ONOFF_STATUS
Definition meshx_ble_mesh_cmn_def.h:244
#define MESHX_MODEL_OP_GEN_ONOFF_SET_UNACK
Definition meshx_ble_mesh_cmn_def.h:243
#define MESHX_ADDR_UNASSIGNED
Definition meshx_ble_mesh_cmn_def.h:30
#define MESHX_MODEL_OP_GEN_ONOFF_GET
Definition meshx_ble_mesh_cmn_def.h:241
#define MESHX_MODEL_OP_GEN_ONOFF_SET
Definition meshx_ble_mesh_cmn_def.h:242
struct meshx_gen_srv_cb_param meshx_gen_srv_cb_param_t
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_NOT_SUPPORTED
Definition meshx_err.h:51
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
struct meshx_gen_onoff_send_params meshx_gen_onoff_send_params_t
Definition meshx_model_onoff.hpp:51
uint16_t dst_addr
Definition meshx_ble_mesh_cmn.h:51

◆ model_send()

meshx_err_t meshXGenericOnOffServerModel::model_send ( meshx_gen_onoff_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_onoff_send_params_t structure as input and sends the corresponding packet to the MeshX stack for processing.

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

Implements meshXModel< meshXBaseGenericServerModel, meshx_gen_onoff_send_params_t >.

494{
495 if (!params || !params->model || !params->ctx)
496 {
497 return MESHX_INVALID_ARG;
498 }
500 meshx_gen_srv_state_change_t state_change = {
501 .onoff_set = {
502 .onoff = params->state.on_off
503 }
504 };
505 meshx_gen_server_send_params_t send_params = {
506 .p_model = params->model,
507 .p_ctx = params->ctx,
508 .state_change = state_change,
509 .data_len = sizeof(meshx_state_change_gen_onoff_set_t)
510 };
511 return this->get_base_model()->plat_send_msg(&send_params);
512}
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
uint32_t opcode
Definition meshx_ble_mesh_cmn.h:49
uint8_t on_off
Definition meshx_model_onoff.hpp:33
meshx_ctx_t * ctx
Definition meshx_model_onoff.hpp:46
meshx_gen_onoff_model_state_t state
Definition meshx_model_onoff.hpp:47
meshx_model_t * model
Definition meshx_model_onoff.hpp:45

◆ plat_model_create()

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

Creates and initializes a server model instance.

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

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.

373{
374 if (p_plat_model_ptr) {
375 this->set_plat_model(p_plat_model_ptr);
376 }
377
378 meshx_ptr_t p_pub = this->get_pub_struct();
379 meshx_ptr_t p_gen = this->get_gen_struct();
381
382 MESHX_MODEL* p_use = (MESHX_MODEL*)this->get_plat_model();
383 if (!p_use)
384 {
385 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "No platform model pointer available");
386 return MESHX_INVALID_STATE;
387 }
388
389 // If already created, skip platform creation to avoid memory leaks
390 if (p_pub != nullptr || p_gen != nullptr)
391 {
392 // Still need to initialize the platform struct fields in the new slot
394 memcpy((void*)&p_use->model_id, &model_id, sizeof(model_id));
395 memcpy((void*)&p_use->pub, &p_pub, sizeof(p_pub));
396 memcpy((void*)&p_use->user_data, &p_gen, sizeof(p_gen));
397 return MESHX_SUCCESS;
398 }
399
400 err = meshx_plat_on_off_gen_srv_create( p_use, &p_pub, &p_gen );
401 if(err)
402 {
403 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to create Generic OnOff Server Model");
404 }
405 else
406 {
407 /* Set the publication and generic structures */
408 this->set_pub_struct(p_pub);
409 this->set_gen_struct(p_gen);
410 }
411 return err;
412}
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_on_off_gen_srv_create(void *p_model, void **p_pub, void **p_onoff_srv)
Creates a Generic OnOff Server model and its publication context.

◆ plat_model_delete()

meshx_err_t meshXGenericOnOffServerModel::plat_model_delete ( void )
overrideprivatevirtual

Deletes the Generic OnOff Server model and its associated resources.

This function frees the memory allocated for the Generic OnOff 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.

428{
429 meshx_ptr_t p_pub = this->get_pub_struct();
430 meshx_ptr_t p_gen = this->get_gen_struct();
431
432 meshx_err_t err = meshx_plat_gen_srv_delete( &p_pub, &p_gen );
433 if (err)
434 {
435 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to delete Generic OnOff Server Model");
436 }
437 else
438 {
439 /* Set the publication and generic structures to NULL */
440 this->set_pub_struct(nullptr);
441 this->set_gen_struct(nullptr);
442 }
443
444 return err;
445}
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 meshXGenericOnOffServerModel::prepare_element_msg ( meshx_ptr_t * msg_ptr,
size_t * msg_size )
overridevirtual

Prepare the element message for notification.

This function prepares the element message for notification by setting the message pointer and size. It checks if the message was prepared for element notification and returns an error if not.

Parameters
[out]msg_ptrPointer to store the message pointer.
[out]msg_sizePointer to store the message size.
Returns
  • MESHX_SUCCESS: Message prepared successfully.
  • MESHX_INVALID_ARG: Invalid argument.
  • MESHX_NOT_SUPPORTED: Message was not prepared for element notification.

Implements meshXModelIF.

643{
644 if (!msg_ptr || !msg_size)
645 {
646 return MESHX_INVALID_ARG;
647 }
648
650 {
651 // Message was not prepared, return error
652 return MESHX_NOT_SUPPORTED;
653 }
654
655 *msg_ptr = &element_msg;
656 *msg_size = sizeof(element_msg);
657
658 return MESHX_SUCCESS;
659}

◆ request_status()

meshx_err_t meshXGenericOnOffServerModel::request_status ( uint16_t dst_addr = 0)

Request to send the Generic OnOff status to a specific address.

This function is used to request the Generic OnOff status to be sent to a specific address. It uses the parent element's context for the message.

Parameters
[in]dst_addrThe destination address to send the status to.
Returns
  • MESHX_SUCCESS: Request sent successfully.
  • MESHX_INVALID_STATE: One of the model states is invalid.
676{
677 auto *el_state = static_cast<meshx_gen_onoff_model_state_t*>(this->get_parent_element_state());
678 auto *el = this->get_parent_element();
679 if (!el || !el_state) return MESHX_INVALID_STATE;
680
681 auto *common_ctx = static_cast<meshx_element_common_ctx_t*>(el->get_element_ctx());
682 if (!common_ctx) return MESHX_INVALID_STATE;
683
684 meshx_model_t model_ref = {
685 .el_id = el->get_element_idx(),
686 .model_id = (uint16_t)this->get_model_id(),
687 .pub_addr = common_ctx->pub_addr,
688 .p_model = (MESHX_MODEL*)this->get_plat_model()
689 };
690
691 meshx_ctx_t ctx = {
692 .app_idx = common_ctx->app_id,
693 .net_idx = meshx_get_net_key_id(),
695 .src_addr = 0,
696 .dst_addr = (dst_addr == 0) ? common_ctx->pub_addr : dst_addr,
697 .p_ctx = nullptr
698 };
699
701 .model = &model_ref,
702 .ctx = &ctx,
703 .state = *el_state,
704 .tid = 0
705 };
706
707 return this->model_send(&sp);
708}
meshXElementIF * get_parent_element(void) const
Get the parent element of this model.
Definition meshx_model_class.hpp:143
uint16_t meshx_get_net_key_id(void)
Get the network key identifier.
Definition meshx.c:316
struct meshx_model meshx_model_t

Field Documentation

◆ element_msg

meshx_on_off_srv_el_msg_t meshXGenericOnOffServerModel::element_msg
private

◆ element_msg_prepared

bool meshXGenericOnOffServerModel::element_msg_prepared
private

◆ model_state

meshx_gen_onoff_model_state_t meshXGenericOnOffServerModel::model_state
private

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