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

A template class for creating Light CTL Server models. More...

#include <meshx_model_ctl.hpp>

Inheritance diagram for meshXLightCTLServerModel:
meshXServerModel< meshXBaseLightServerModel, meshx_light_ctl_send_params_t > meshXModel< meshXBaseLightServerModel, meshx_light_ctl_send_params_t > meshXModelIF

Public Member Functions

meshx_err_t model_send (meshx_light_ctl_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 Light CTL 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 request_status (uint16_t dst_addr=0)
 meshXLightCTLServerModel (meshXElementIF *parent_element=nullptr, meshx_ptr_t parent_element_state=nullptr, uint16_t model_func_id=0)
 Constructor for Light CTL Server Model.
Public Member Functions inherited from meshXServerModel< meshXBaseLightServerModel, meshx_light_ctl_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< meshXBaseLightServerModel, meshx_light_ctl_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.
meshXBaseLightServerModelget_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 Light CTL Server model and its associated resources.
meshx_err_t element_state_change_handle (void) override
 Handle state change request from element.

Private Attributes

meshx_light_ctl_model_state_t model_state
meshx_light_ctl_srv_el_msg_t element_msg
bool element_msg_prepared

Additional Inherited Members

Protected Member Functions inherited from meshXServerModel< meshXBaseLightServerModel, meshx_light_ctl_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 Light CTL Server models.

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

Constructor & Destructor Documentation

◆ meshXLightCTLServerModel()

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

Constructor for Light CTL Server Model.

Parameters
[in]parent_elementPointer to the parent element (meshXElementIF)
[in]parent_element_statePointer to the parent element state
[in]model_func_idFunctional ID for the model instance.
589 model_state{},
591{
592 /* Used only for initialization of Parent Class */
593}
meshx_light_ctl_model_state_t model_state
Definition meshx_model_ctl.hpp:139
bool element_msg_prepared
Definition meshx_model_ctl.hpp:145
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_LIGHT_CTL_SRV
Definition meshx_ble_mesh_cmn_def.h:107

Member Function Documentation

◆ element_state_change_handle()

meshx_err_t meshXLightCTLServerModel::element_state_change_handle ( void )
overrideprivatevirtual

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.

610{
611 auto *msg =
613
614 if (!msg) {
615 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid parameter in element_state_change_handle");
616 return MESHX_INVALID_ARG;
617 }
618
619 if(memcmp(&model_state, msg, sizeof(meshx_light_ctl_model_state_t)) != 0)
620 {
622 "CTL state change request: L=%d T=%d UV=%d",
623 model_state.lightness, model_state.temperature, model_state.delta_uv);
624 // Update the model state
625 *msg = model_state;
626 }
627 else
628 {
629 return MESHX_INVALID_STATE;
630 }
631 return MESHX_SUCCESS;
632}
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
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
struct meshx_light_ctl_model_state meshx_light_ctl_model_state_t
Definition meshx_model_ctl.hpp:42
**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 meshXLightCTLServerModel::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 Light CTL Server model.

This function is triggered whenever a Light CTL message is received from the MeshX stack. It handles the Light CTL 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< meshXBaseLightServerModel, meshx_light_ctl_send_params_t >.

447{
448 if(!params || !p_dev)
449 {
450 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid parameters");
451 return MESHX_INVALID_ARG;
452 }
454 {
455 /* Callback triggered not for this model */
456 return MESHX_SUCCESS;
457 }
458
459 auto *param = static_cast<meshx_lighting_server_cb_param_t *>(params);
460
461 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "op|src|dst:%04" PRIx32 "|%04x|%04x",
462 param->ctx.opcode, param->ctx.src_addr, param->ctx.dst_addr);
463
464 model_state = {
465 .lightness = param->state_change.ctl_set.lightness,
466 .temperature = param->state_change.ctl_set.temperature,
467 .delta_uv = param->state_change.ctl_set.delta_uv,
468 .temp_range_min = 0, // Range values come from separate range status message
469 .temp_range_max = 0
470 };
471
472 // Initialize flag - message not prepared yet
473 element_msg_prepared = false;
474
475 bool send_reply = (param->ctx.opcode != MESHX_MODEL_OP_LIGHT_CTL_SET_UNACK);
476 switch (param->ctx.opcode)
477 {
479 // GET opcode - no state change, no element notification needed
480 // Return error so base layer doesn't send uninitialized message
481 return MESHX_NOT_SUPPORTED;
484 {
485 if (MESHX_ADDR_IS_UNICAST(param->ctx.dst_addr)
486 || (MESHX_ADDR_BROADCAST(param->ctx.dst_addr))
487 || (MESHX_ADDR_IS_GROUP(param->ctx.dst_addr)
488 && (MESHX_SUCCESS == meshx_is_group_subscribed(&param->model, param->ctx.dst_addr))))
489 {
490 // Prepare the message (store in member variable)
491 element_msg = {
492 .header = {
493 .model = param->model,
494 .element_state_change = MESHX_SUCCESS, // Will be set by base layer
495 },
496 .state = model_state,
497 };
499 }
500 break;
501 }
503 // GET opcode - no state change, no element notification needed
504 // Return error so base layer doesn't send uninitialized message
505 return MESHX_NOT_SUPPORTED;
508 // Handle temperature range operations
509 break;
510 default:
511 // Unknown opcode - return error
512 return MESHX_NOT_SUPPORTED;
513 }
514
515 if (send_reply
516 /* This is meant to notify the respective publish client */
517 || param->ctx.src_addr != param->model.pub_addr)
518 {
519 /* Here the message was received from unregistered source and mention the state to the respective client */
520 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "PUB: src|pub %x|%x", param->ctx.src_addr, param->model.pub_addr);
521 param->ctx.dst_addr = param->model.pub_addr;
522
523 // Create a parameter structure for sending the response
524 meshx_light_ctl_send_params_t send_params = {
525 .model = &param->model,
526 .ctx = &param->ctx,
527 .tid = 0, // TID not used in server response
528 .state = model_state
529 };
530
531 return this->model_send(&send_params);
532 }
533
534 // If we reach here, message was not prepared for element notification
535 // Return error so base layer doesn't send uninitialized message
537 {
538 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "No element notification needed for opcode: %04x", param->ctx.opcode);
539 return MESHX_NOT_SUPPORTED;
540 }
541
542 return MESHX_SUCCESS;
543}
meshx_light_ctl_srv_el_msg_t element_msg
Definition meshx_model_ctl.hpp:142
meshx_err_t model_send(meshx_light_ctl_send_params_t *params) override
Send a packet to the MeshX stack based on the given parameters.
Definition meshx_model_ctl.cpp:402
meshx_err_t meshx_is_group_subscribed(meshx_model_t *p_model, uint16_t addr)
Checks if a model is subscribed to a specific group address.
#define MESHX_ADDR_IS_GROUP(_addr)
Definition meshx_ble_mesh_cmn_def.h:27
#define MESHX_MODEL_OP_LIGHT_CTL_SET
Definition meshx_ble_mesh_cmn_def.h:416
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK
Definition meshx_ble_mesh_cmn_def.h:432
#define MESHX_ADDR_IS_UNICAST(_addr)
Definition meshx_ble_mesh_cmn_def.h:25
#define MESHX_ADDR_BROADCAST(_addr)
Definition meshx_ble_mesh_cmn_def.h:26
#define MESHX_MODEL_OP_LIGHT_CTL_SET_UNACK
Definition meshx_ble_mesh_cmn_def.h:417
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET
Definition meshx_ble_mesh_cmn_def.h:420
#define MESHX_MODEL_OP_LIGHT_CTL_GET
Definition meshx_ble_mesh_cmn_def.h:415
#define MESHX_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET
Definition meshx_ble_mesh_cmn_def.h:431
struct meshx_lighting_server_cb_param meshx_lighting_server_cb_param_t
@ MESHX_NOT_SUPPORTED
Definition meshx_err.h:51
struct meshx_light_ctl_send_params meshx_light_ctl_send_params_t
Definition meshx_model_ctl.hpp:55

◆ model_send()

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

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

Implements meshXModel< meshXBaseLightServerModel, meshx_light_ctl_send_params_t >.

403{
404 if (!params || !params->model || !params->ctx)
405 {
406 return MESHX_INVALID_ARG;
407 }
409 meshx_lighting_server_state_change_t state_change = {
410 .ctl_set = {
411 .lightness = params->state.lightness,
412 .temperature = params->state.temperature,
413 .delta_uv = params->state.delta_uv
414 }
415 };
417 .p_model = params->model,
418 .p_ctx = params->ctx,
419 .state_change = &state_change
420 };
421 return this->get_base_model()->plat_send_msg(&send_params);
422}
meshx_err_t plat_send_msg(meshx_light_server_send_params_t *params) override
Sends a status message for the Light Server model.
Definition meshx_base_model_light.cpp:375
meshXBaseLightServerModel * get_base_model(void) const
Definition meshx_model_class.hpp:315
#define MESHX_MODEL_OP_LIGHT_CTL_STATUS
Definition meshx_ble_mesh_cmn_def.h:418
struct meshx_light_server_send_params meshx_light_server_send_params_t
Light Server send parameters. Used by meshXBaseLightServerModel::plat_send_msg to forward state chang...
uint32_t opcode
Definition meshx_ble_mesh_cmn.h:49
int16_t delta_uv
Definition meshx_model_ctl.hpp:38
uint16_t temperature
Definition meshx_model_ctl.hpp:37
uint16_t lightness
Definition meshx_model_ctl.hpp:36
meshx_light_ctl_model_state_t state
Definition meshx_model_ctl.hpp:52
meshx_ctx_t * ctx
Definition meshx_model_ctl.hpp:50
meshx_model_t * model
Definition meshx_model_ctl.hpp:49

◆ plat_model_create()

meshx_err_t meshXLightCTLServerModel::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.

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.

322{
323 if (p_plat_model_ptr) {
324 this->set_plat_model(p_plat_model_ptr);
325 }
326
327 meshx_ptr_t p_pub = this->get_pub_struct();
328 meshx_ptr_t p_gen = this->get_gen_struct();
330
331 // If already created, skip platform creation to avoid memory leaks
332 if (p_pub != nullptr || p_gen != nullptr)
333 {
334 MESHX_MODEL* p_use = this->get_plat_model();
335 if (p_use) {
337 memcpy((void*)&p_use->model_id, &model_id, sizeof(model_id));
338 memcpy((void*)&p_use->pub, &p_pub, sizeof(p_pub));
339 memcpy((void*)&p_use->user_data, &p_gen, sizeof(p_gen));
340 }
341 return MESHX_SUCCESS;
342 }
343
344 err = meshx_plat_light_ctl_srv_create( this->get_plat_model(), &p_pub, &p_gen );
345 if(err)
346 {
347 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to create Light CTL Server Model");
348 }
349 else
350 {
351 /* Set the publication and generic structures */
352 this->set_pub_struct(p_pub);
353 this->set_gen_struct(p_gen);
354 }
355 return err;
356}
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_light_ctl_srv_create(meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_ctl_srv)
Create a Light CTL Server instance.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43

◆ plat_model_delete()

meshx_err_t meshXLightCTLServerModel::plat_model_delete ( void )
overrideprivatevirtual

Deletes the Light CTL Server model and its associated resources.

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

372{
373 meshx_ptr_t p_pub = this->get_pub_struct();
374 meshx_ptr_t p_gen = this->get_gen_struct();
375
376 meshx_err_t err = meshx_plat_light_srv_delete( &p_pub, &p_gen );
377 if (err)
378 {
379 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to delete Light CTL Server Model");
380 }
381 else
382 {
383 /* Set the publication and generic structures to NULL */
384 this->set_pub_struct(nullptr);
385 this->set_gen_struct(nullptr);
386 }
387
388 return err;
389}
meshx_err_t meshx_plat_light_srv_delete(meshx_ptr_t *p_pub, meshx_ptr_t *p_ctl_srv)
Delete a Light CTL Server instance.

◆ prepare_element_msg()

meshx_err_t meshXLightCTLServerModel::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.

556{
557 if (!msg_ptr || !msg_size)
558 {
559 return MESHX_INVALID_ARG;
560 }
561
563 {
564 // Message was not prepared, return error
565 return MESHX_NOT_SUPPORTED;
566 }
567
568 *msg_ptr = &element_msg;
569 *msg_size = sizeof(element_msg);
570
571 return MESHX_SUCCESS;
572}

◆ request_status()

meshx_err_t meshXLightCTLServerModel::request_status ( uint16_t dst_addr = 0)
692{
693 auto *el_state = static_cast<meshx_light_ctl_model_state_t*>(this->get_parent_element_state());
694 auto *el = this->get_parent_element();
695 if (!el || !el_state) return MESHX_INVALID_STATE;
696
697 auto *common_ctx = static_cast<meshx_element_common_ctx_t*>(el->get_element_ctx());
698 if (!common_ctx) return MESHX_INVALID_STATE;
699
700 meshx_model_t model_ref = {
701 .el_id = el->get_element_idx(),
702 .model_id = (uint16_t)this->get_model_id(),
703 .pub_addr = common_ctx->pub_addr,
704 .p_model = (MESHX_MODEL*)this->get_plat_model()
705 };
706
707 meshx_ctx_t ctx = {
708 .app_idx = common_ctx->app_id,
709 .net_idx = meshx_get_net_key_id(),
711 .src_addr = 0,
712 .dst_addr = (dst_addr == 0) ? common_ctx->pub_addr : dst_addr,
713 .p_ctx = nullptr
714 };
715
717 .model = &model_ref,
718 .ctx = &ctx,
719 .tid = 0,
720 .state = *el_state
721 };
722
723 return this->model_send(&sp);
724}
meshXElementIF * get_parent_element(void) const
Get the parent element of this model.
Definition meshx_model_class.hpp:143
uint16_t get_model_id(void) const override
Definition meshx_model_class.hpp:292
uint16_t meshx_get_net_key_id(void)
Get the network key identifier.
Definition meshx.c:316
struct meshx_model meshx_model_t
struct meshx_ctx meshx_ctx_t
Structure to hold context information for BLE Mesh operations.

Field Documentation

◆ element_msg

meshx_light_ctl_srv_el_msg_t meshXLightCTLServerModel::element_msg
private

◆ element_msg_prepared

bool meshXLightCTLServerModel::element_msg_prepared
private

◆ model_state

meshx_light_ctl_model_state_t meshXLightCTLServerModel::model_state
private

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