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

#include <meshx_model_config.hpp>

Inheritance diagram for meshXConfigModel:
meshXServerModel< meshXBaseConfigServerModel, meshx_config_send_params_t > meshXModel< meshXBaseConfigServerModel, meshx_config_send_params_t > meshXModelIF

Public Member Functions

meshx_err_t model_send (meshx_config_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 Config Server model.
meshx_err_t prepare_element_msg (meshx_ptr_t *msg_ptr, size_t *msg_size) override
 Prepare message for element notification.
 meshXConfigModel (meshXElementIF *parent_element, meshx_ptr_t parent_element_state, uint16_t model_func_id)
 Constructor for meshXConfigModel class.
 ~meshXConfigModel () override=default
Public Member Functions inherited from meshXServerModel< meshXBaseConfigServerModel, meshx_config_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< meshXBaseConfigServerModel, meshx_config_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.
meshXBaseConfigServerModelget_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 config server model instance.
meshx_err_t plat_model_delete (void) override
 Deletes the Config Server model and its associated resources.
meshx_err_t element_state_change_handle (void) override
 Handle state change request from element.

Private Attributes

meshx_config_model_state_t model_state
meshx_config_srv_el_msg_t element_msg

Additional Inherited Members

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

Constructor & Destructor Documentation

◆ meshXConfigModel()

meshXConfigModel::meshXConfigModel ( meshXElementIF * parent_element,
meshx_ptr_t parent_element_state,
uint16_t model_func_id )
explicit

Constructor for meshXConfigModel class.

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.
275{
276}
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_CONFIG_SRV
Definition meshx_ble_mesh_cmn_def.h:46

◆ ~meshXConfigModel()

meshXConfigModel::~meshXConfigModel ( )
overridedefault

Member Function Documentation

◆ element_state_change_handle()

meshx_err_t meshXConfigModel::element_state_change_handle ( void )
inlineoverrideprivatevirtual

Handle state change request from element.

Pure virtual function that derived classes must implement to handle state change requests from the parent element. The model validates the request, updates its internal state, and returns the result.

Returns
MESHX_SUCCESS on success, error code otherwise

Implements meshXModelIF.

75 {
76 /* Nothing to do here */
77 return MESHX_SUCCESS;
78 };
**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

◆ model_from_ble_cb()

meshx_err_t meshXConfigModel::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 Config Server model.

This function is triggered whenever a Config Server message is received from the MeshX stack. It handles the Config Server 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

< Config Model Publication Set

< Config Model App Bind

< Config Model App Unbind

< Config Model Publication Set

< Config AppKey Add

< Config NetKey Add

< Config Model Subscription Add

< Config Model Publication Set

< Config Model App Bind

< Config NetKey Delete

< Config AppKey Delete

< Config Model Subscription Delete

< Config Model App Unbind

Implements meshXModel< meshXBaseConfigServerModel, meshx_config_send_params_t >.

132{
133 if(!params || !p_dev)
134 {
135 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid parameters");
136 return MESHX_INVALID_ARG;
137 }
139 {
140 /* Callback triggered not for this model */
141 return MESHX_SUCCESS;
142 }
143
144 auto const *param = static_cast<meshx_config_srv_cb_param_t *>(params);
145
146 MESHX_LOGI(MODULE_ID_MODEL_SERVER, "Config Event: op=0x%04x, src=0x%04x, dst=0x%04x",
147 param->ctx.opcode, param->ctx.src_addr, param->ctx.dst_addr);
148
149 /* Sync publication address and app key index to the targeted element's context */
150 uint16_t target_addr = 0;
151 uint16_t new_pub_addr = MESHX_ADDR_UNASSIGNED;
152 uint16_t new_app_idx = 0;
153 bool sync_required = false;
154
155 if (param->ctx.opcode == MESHX_MODEL_OP_MODEL_PUB_SET)
156 {
157 target_addr = param->state_change.mod_pub_set.element_addr;
158 new_pub_addr = param->state_change.mod_pub_set.pub_addr;
159 new_app_idx = param->state_change.mod_pub_set.app_idx;
160 sync_required = true;
161 MESHX_LOGI(MODULE_ID_MODEL_SERVER, " Syncing PubAddr: el_addr=0x%04x -> pub=0x%04x, app_idx=%d",
162 target_addr, new_pub_addr, new_app_idx);
163 }
164 else if (param->ctx.opcode == MESHX_MODEL_OP_MODEL_APP_BIND)
165 {
166 target_addr = param->state_change.mod_app_bind.element_addr;
167 new_app_idx = param->state_change.mod_app_bind.app_idx;
168 sync_required = true;
169 MESHX_LOGI(MODULE_ID_MODEL_SERVER, " Syncing AppBind: el_addr=0x%04x -> app_idx=%d",
170 target_addr, new_app_idx);
171 }
172 else if (param->ctx.opcode == MESHX_MODEL_OP_MODEL_APP_UNBIND)
173 {
174 target_addr = param->state_change.mod_app_unbind.element_addr;
175 new_app_idx = param->state_change.mod_app_unbind.app_idx;
176 sync_required = true;
177 MESHX_LOGI(MODULE_ID_MODEL_SERVER, " Syncing AppUnbind: el_addr=0x%04x -> app_idx=%d",
178 target_addr, new_app_idx);
179 }
180
181 if (sync_required)
182 {
183 uint16_t el_id = target_addr - p_dev->meshx_store.node_addr;
185 if (el && el->get_element_ctx())
186 {
187 meshx_element_type_t variant = el->get_element_variant();
188 // All element contexts in MeshX start with meshx_element_common_ctx_t (app_id and pub_addr)
189 auto *ctx = static_cast<meshx_element_common_ctx_t*>(el->get_element_ctx());
190 if (param->ctx.opcode == MESHX_MODEL_OP_MODEL_PUB_SET) {
191 ctx->pub_addr = new_pub_addr;
192 }
193 ctx->app_id = new_app_idx;
194
195 meshx_nvs_element_ctx_set(el_id, variant, ctx, el->get_element_ctx_size());
196 meshx_nvs_commit(); // Force immediate commit for critical config changes
197 MESHX_LOGI(MODULE_ID_MODEL_SERVER, " Element [%d] context updated and committed to NVS (variant: %d)", el_id, variant);
198 }
199 }
200
201 // Store the message in member variable for later use by prepare_element_msg
202 element_msg = {
203 .header = {
204 .model = param->model,
205 .element_state_change = MESHX_SUCCESS,
206 },
207 .state = { .state_change = param->state_change }
208 };
209
210 /* Map opcode to legacy config event for publishing */
212 switch (param->ctx.opcode)
213 {
223 default: break;
224 }
225
226 if (config_evt != CONTROL_TASK_MSG_EVT_CONFIG_ALL)
227 {
230 config_evt,
231 param,
233 }
234
235 return MESHX_SUCCESS;
236}
meshx_config_srv_el_msg_t element_msg
Definition meshx_model_config.hpp:70
meshXElementIF * find_element(uint16_t element_idx)
Find an element instance by its index.
Definition meshx_element_registry.hpp:65
static meshXElementRegistry & get_instance()
Definition meshx_element_registry.hpp:33
uint16_t model_id
Definition meshx_model_class.hpp:180
#define MESHX_MODEL_OP_MODEL_APP_BIND
Definition meshx_ble_mesh_cmn_def.h:158
#define MESHX_MODEL_OP_NET_KEY_DELETE
Definition meshx_ble_mesh_cmn_def.h:164
#define MESHX_MODEL_OP_MODEL_SUB_ADD
Definition meshx_ble_mesh_cmn_def.h:150
#define MESHX_ADDR_UNASSIGNED
Definition meshx_ble_mesh_cmn_def.h:30
#define MESHX_MODEL_OP_MODEL_PUB_SET
Definition meshx_ble_mesh_cmn_def.h:149
#define MESHX_MODEL_OP_MODEL_SUB_DELETE
Definition meshx_ble_mesh_cmn_def.h:152
#define MESHX_MODEL_OP_APP_KEY_ADD
Definition meshx_ble_mesh_cmn_def.h:157
#define MESHX_MODEL_OP_MODEL_APP_UNBIND
Definition meshx_ble_mesh_cmn_def.h:171
#define MESHX_MODEL_OP_NET_KEY_ADD
Definition meshx_ble_mesh_cmn_def.h:156
#define MESHX_MODEL_OP_APP_KEY_DELETE
Definition meshx_ble_mesh_cmn_def.h:166
struct meshx_config_srv_cb_param meshx_config_srv_cb_param_t
control_task_msg_evt_config_t config_evt_t
Definition meshx_ble_mesh_config_srv.h:21
CONTROL_TASK_MSG_EVT_NET_KEY_DEL
Definition meshx_control_task.h:149
CONTROL_TASK_MSG_EVT_APP_KEY_DEL
Definition meshx_control_task.h:141
CONTROL_TASK_MSG_EVT_SUB_DEL
Definition meshx_control_task.h:145
CONTROL_TASK_MSG_EVT_APP_KEY_UNBIND
Definition meshx_control_task.h:143
@ CONTROL_TASK_MSG_CODE_CONFIG
Definition meshx_control_task.h:69
CONTROL_TASK_MSG_EVT_APP_KEY_ADD
Definition meshx_control_task.h:140
CONTROL_TASK_MSG_EVT_PUB_ADD
Definition meshx_control_task.h:146
CONTROL_TASK_MSG_EVT_APP_KEY_BIND
Definition meshx_control_task.h:142
CONTROL_TASK_MSG_EVT_SUB_ADD
Definition meshx_control_task.h:144
meshx_err_t control_task_msg_publish(control_task_msg_code_t msg_code, control_task_msg_evt_t msg_evt, const void *msg_evt_params, size_t sizeof_msg_evt_params)
Publish a control task message.
Definition meshx_control_task.c:84
CONTROL_TASK_MSG_EVT_CONFIG_ALL
Definition meshx_control_task.h:150
CONTROL_TASK_MSG_EVT_NET_KEY_ADD
Definition meshx_control_task.h:148
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114
meshx_err_t meshx_nvs_element_ctx_set(uint16_t element_id, meshx_element_type_t element_type, const void *blob, size_t blob_size)
Store the context of a specific element to NVS.
Definition meshx_nvs.c:466
meshx_err_t meshx_nvs_commit(void)
Commit changes to the NVS.
Definition meshx_nvs.c:277
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:34
meshx_app_store_t meshx_store
Definition meshx_common.h:75
uint16_t node_addr
Definition meshx_common.h:62

◆ model_send()

meshx_err_t meshXConfigModel::model_send ( meshx_config_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_config_send_params_t structure as input and sends the corresponding packet to the MeshX stack for processing. For config server, this is typically used for sending status responses.

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

Implements meshXModel< meshXBaseConfigServerModel, meshx_config_send_params_t >.

88{
89 if (!params || !params->model || !params->ctx)
90 {
91 return MESHX_INVALID_ARG;
92 }
93
94 // For config server, we use the base model's send functionality
95 // The config server send params structure is minimal (just a stub)
97 .p_model = params->model,
98 .p_ctx = params->ctx,
99 .p_data = &params->stub, // Use stub field as data
100 .data_len = sizeof(params->stub)
101 };
102
103 return this->get_base_model()->plat_send_msg(&send_params);
104}
meshx_err_t plat_send_msg(meshx_config_server_send_params_t *params) override
Send message through the config server model.
Definition meshx_base_model_common.cpp:83
meshXBaseConfigServerModel * get_base_model(void) const
Definition meshx_model_class.hpp:315
struct meshx_config_server_send_params { meshx_model_t *p_model; meshx_ctx_t *p_ctx; uint8_t *p_data; size_t data_len; } meshx_config_server_send_params_t
Definition meshx_base_model_common.hpp:46
meshx_model_t * model
Definition meshx_model_config.hpp:43
uint8_t stub
Definition meshx_model_config.hpp:45
meshx_ctx_t * ctx
Definition meshx_model_config.hpp:44

◆ plat_model_create()

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

Creates and initializes a config server model instance.

This function handles the platform-specific model creation process for config server models. It initializes config 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

Implements meshXModelIF.

23{
25
26 if (p_plat_model_ptr) {
27 this->set_plat_model(p_plat_model_ptr);
28 }
29
30 MESHX_MODEL* p_use = (MESHX_MODEL*)this->get_plat_model();
31 if (!p_use)
32 {
33 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "No platform model pointer available");
35 }
36
37 // For config server, we use the platform's get_config_srv_model function
38 // to get the model instance since there's no specific create function
40 if (err)
41 {
42 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to get Config Server Model");
43 return err;
44 }
45
46 // Config server doesn't use pub/gen structures like other models
47 // Set them to nullptr to indicate they're not used
48 this->set_pub_struct(nullptr);
49 this->set_gen_struct(nullptr);
50
51 return MESHX_SUCCESS;
52}
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
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
meshx_err_t meshx_plat_get_config_srv_model(meshx_ptr_t p_model)
Retrieves the configuration server model for the BLE Mesh.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_INVALID_STATE
Definition meshx_err.h:49

◆ plat_model_delete()

meshx_err_t meshXConfigModel::plat_model_delete ( void )
overrideprivatevirtual

Deletes the Config Server model and its associated resources.

This function frees the memory allocated for the Config Server and sets the pointer to NULL. For config server, this is mainly a cleanup operation since the model is managed by the platform.

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

Implements meshXModelIF.

67{
68 // Config server is managed by the platform, so we don't need to
69 // explicitly delete it. Just clean up our references.
70 this->set_pub_struct(nullptr);
71 this->set_gen_struct(nullptr);
72
73 return MESHX_SUCCESS;
74}

◆ prepare_element_msg()

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

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.

250{
251 if (!msg_ptr || !msg_size)
252 {
253 return MESHX_INVALID_ARG;
254 }
255
256 *msg_ptr = &element_msg;
257 *msg_size = sizeof(element_msg);
258
259 return MESHX_SUCCESS;
260}

Field Documentation

◆ element_msg

meshx_config_srv_el_msg_t meshXConfigModel::element_msg
private

◆ model_state

meshx_config_model_state_t meshXConfigModel::model_state
private

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