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

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

#include <meshx_model_onoff.hpp>

Inheritance diagram for meshXGenericOnOffClientModel:
meshXClientModel< meshXBaseGenericClientModel, meshx_gen_onoff_send_params_t > meshXModel< meshXBaseGenericClientModel, 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
 Creates a meshXGenericOnOffClientModel instance based on a BLE device.
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_onoff (const meshx_gen_on_off_cli_msg_t *msg)
 Request to change or get the Generic OnOff state.
 meshXGenericOnOffClientModel (meshXElementIF *parent_element=nullptr, meshx_ptr_t parent_element_state=nullptr, uint16_t model_func_id=0)
 A template class for creating Generic OnOff Client models.
 ~meshXGenericOnOffClientModel () override=default
Public Member Functions inherited from meshXClientModel< meshXBaseGenericClientModel, meshx_gen_onoff_send_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.
Public Member Functions inherited from meshXModel< meshXBaseGenericClientModel, 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.
meshXBaseGenericClientModelget_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 meshx_state_change_notify (const meshx_gen_cli_cb_param_t *param, uint8_t status)
 Handle Generic OnOff state change notifications from the MeshX stack.
meshx_err_t element_state_change_handle (void) override
 Handle state change request from element.

Private Attributes

meshx_gen_onoff_model_state_t model_state
meshx_on_off_cli_el_msg_t element_msg

Additional Inherited Members

Protected Member Functions inherited from meshXClientModel< meshXBaseGenericClientModel, 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 client message header.

Detailed Description

A template class for creating Generic OnOff Client models.

This class is derived from meshXClientModel and provides a convenient interface for creating Generic OnOff Client 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

◆ meshXGenericOnOffClientModel()

meshXGenericOnOffClientModel::meshXGenericOnOffClientModel ( 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 Client models.

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

Template Parameters
meshxBaseClientModel_tThe type of the meshXBaseClientModel 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.
319 model_state({0, 1, 0})
320{
321 /* Used only for initialization of Parent Class */
322}
meshx_gen_onoff_model_state_t model_state
Definition meshx_model_onoff.hpp:80
meshXElementIF * parent_element
Definition meshx_model_class.hpp:42
uint16_t model_func_id
Definition meshx_model_class.hpp:181
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)
Definition meshx_model_class.cpp:216
#define MESHX_MODEL_ID_GEN_ONOFF_CLI
Definition meshx_ble_mesh_cmn_def.h:71

◆ ~meshXGenericOnOffClientModel()

meshXGenericOnOffClientModel::~meshXGenericOnOffClientModel ( )
overridedefault

Member Function Documentation

◆ element_state_change_handle()

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

Implements meshXModelIF.

76{
77 auto *el_state =
79
80 if (!el_state)
81 {
82 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid parameter in element_state_change_handle");
83 return MESHX_INVALID_ARG;
84 }
85
86 if (el_state->on_off != model_state.on_off || el_state->next_on_off != model_state.next_on_off)
87 {
88 *el_state = model_state;
89 return MESHX_SUCCESS;
90 }
91 else
92 {
94 }
95}
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_CLIENT
Definition module_id.h:35

◆ meshx_state_change_notify()

meshx_err_t meshXGenericOnOffClientModel::meshx_state_change_notify ( const meshx_gen_cli_cb_param_t * param,
uint8_t status )
private

Handle Generic OnOff state change notifications from the MeshX stack.

This function is responsible for handling Generic OnOff state change notifications from the MeshX stack. It is called when the MeshX stack receives a state change event from the Generic OnOff server model. The function publishes the state change event to the control task framework, which in turn notifies the application about the state change.

Parameters
[in]paramPointer to the Generic OnOff client callback parameter structure.
[in]statusStatus of the state change event (success or timeout).
Returns
  • MESHX_SUCCESS: Successfully handled the state change notification.
  • MESHX_INVALID_ARG: One or more arguments are invalid.
39{
40 if (!param){
41 return MESHX_INVALID_ARG;
42 }
43
45 model_state.next_on_off = !model_state.on_off;
46
47 // Prepare the message (store in member variable)
48 element_msg = {
49 .header = {
50 .err_code = status,
51 .model = param->model,
52 .ctx = param->ctx,
53 .element_state_change = MESHX_SUCCESS, // Will be set by base layer
54 },
55 .state = model_state,
56 };
57
58 return MESHX_SUCCESS;
59}
meshx_on_off_cli_el_msg_t element_msg
Definition meshx_model_onoff.hpp:83
meshx_err_t status
Definition meshx_model_class.hpp:179
meshx_gen_client_status_cb_t status
Definition meshx_ble_mesh_gen_cli.h:289
meshx_model_t model
Definition meshx_ble_mesh_gen_cli.h:287
meshx_ctx_t ctx
Definition meshx_ble_mesh_gen_cli.h:286
uint8_t present_onoff
Definition meshx_ble_mesh_gen_cli.h:133
meshx_gen_onoff_status_cb_t onoff_status
Definition meshx_ble_mesh_gen_cli.h:253

◆ model_from_ble_cb()

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

Creates a meshXGenericOnOffClientModel instance based on a BLE device.

This function is used to create a meshXGenericOnOffClientModel instance based on a BLE device. It takes the device structure, model ID, and parameters as input and returns a pointer to the created instance.

Parameters
[in]p_devPointer to the device structure
[in]model_idModel ID associated with the device
[in]paramsPointer to the parameters associated with the device
Returns
Pointer to the created meshXGenericOnOffClientModel instance

Implements meshXModel< meshXBaseGenericClientModel, meshx_gen_onoff_send_params_t >.

115{
116 if(!params || !p_dev)
117 {
118 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid parameters");
119 return MESHX_INVALID_ARG;
120 }
121 if(model_id != this->get_model_id())
122 {
123 /* Callback triggered not for this model */
124 return MESHX_SUCCESS;
125 }
126
127 auto *param = static_cast<meshx_gen_cli_cb_param_t *>(params);
128
129 return static_cast<int>(param->evt) == static_cast<int>(meshx_base_cli_evt::MESHX_BASE_CLI_TIMEOUT) ?
132}
meshx_err_t meshx_state_change_notify(const meshx_gen_cli_cb_param_t *param, uint8_t status)
Handle Generic OnOff state change notifications from the MeshX stack.
Definition meshx_model_onoff.cpp:38
uint16_t get_model_id(void) const override
Definition meshx_model_class.hpp:292
@ MESHX_BASE_CLI_TIMEOUT
Definition meshx_base_model_class.hpp:233
struct meshx_gen_cli_cb_param meshx_gen_cli_cb_param_t
Callback parameters for Generic Client Model events. This structure is used to pass information about...
@ MESHX_TIMEOUT
Definition meshx_err.h:52

◆ model_send()

meshx_err_t meshXGenericOnOffClientModel::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< meshXBaseGenericClientModel, meshx_gen_onoff_send_params_t >.

237{
238 meshx_err_t err;
239 meshx_gen_cli_set_t set;
240 if (!params || !params->model || !params->model->p_model)
241 {
242 return MESHX_INVALID_ARG;
243 }
244
246
247 send_params.state = &set;
248 send_params.opcode = static_cast<uint16_t>(params->ctx->opcode);
249 send_params.net_idx = params->ctx->net_idx;
250 send_params.app_idx = params->ctx->app_idx;
251 send_params.addr = params->model->pub_addr;
252 send_params.model = params->model->p_model;
253
255 {
256 err = this->get_base_model()->plat_send_msg(&send_params);
257 }
258
259 else if (params->ctx->opcode == MESHX_MODEL_OP_GEN_ONOFF_SET ||
261 {
262 set.onoff_set.tid = params->tid;
263 set.onoff_set.onoff = params->state.on_off;
264 set.onoff_set.op_en = false;
265
266 MESHX_LOGD(MODULE_ID_MODEL_CLIENT, "[EL %d] Sending OnOff: %d (TID: %d)",
267 params->model->el_id, set.onoff_set.onoff, set.onoff_set.tid);
268
269 err = this->get_base_model()->plat_send_msg(&send_params);
270 if(!err && (params->ctx->opcode == MESHX_MODEL_OP_GEN_ONOFF_SET_UNACK ||
272 {
273 model_state.on_off = params->state.on_off;
274 model_state.next_on_off = !model_state.on_off;
275
276 /* Notify parent element of optimistic state change */
277 element_msg.header.err_code = MESHX_SUCCESS;
278 element_msg.header.model.el_id = params->model->el_id;
279 element_msg.header.model.model_id = params->model->model_id;
280 element_msg.header.ctx = *params->ctx;
281 element_msg.state = model_state;
283 }
284 if(err)
285 {
286 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Failed to send Generic OnOff Set message");
287 }
288 }
289 else
290 {
291 err = MESHX_INVALID_ARG;
292 MESHX_LOGE(MODULE_ID_MODEL_CLIENT, "Invalid opcode for Generic OnOff Client: %04x", params->ctx->opcode);
293 }
294 return err;
295}
meshx_err_t plat_send_msg(meshx_gen_client_send_params_t *params) override
Send a message using the generic client model.
Definition meshx_base_model_generic.cpp:263
meshXBaseGenericClientModel * get_base_model(void) const
Definition meshx_model_class.hpp:315
meshx_err_t send_to_parent_element(meshx_ptr_t msg_ptr, size_t msg_size)
Definition meshx_model_class.cpp:119
#define MESHX_MODEL_OP_GEN_ONOFF_SET_UNACK
Definition meshx_ble_mesh_cmn_def.h:243
#define MESHX_ADDR_GROUP_START
Definition meshx_ble_mesh_cmn_def.h:29
#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_client_send_params meshx_gen_client_send_params_t
Generic Client Model send parameters.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
uint16_t app_idx
Definition meshx_ble_mesh_cmn.h:47
uint32_t opcode
Definition meshx_ble_mesh_cmn.h:49
uint16_t net_idx
Definition meshx_ble_mesh_cmn.h:48
uint16_t dst_addr
Definition meshx_ble_mesh_cmn.h:51
uint16_t addr
Definition meshx_ble_mesh_gen_cli.h:299
uint16_t net_idx
Definition meshx_ble_mesh_gen_cli.h:300
meshx_gen_cli_set_t * state
Definition meshx_ble_mesh_gen_cli.h:302
meshx_ptr_t model
Definition meshx_ble_mesh_gen_cli.h:297
uint16_t opcode
Definition meshx_ble_mesh_gen_cli.h:298
uint16_t app_idx
Definition meshx_ble_mesh_gen_cli.h:301
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
uint8_t tid
Definition meshx_model_onoff.hpp:48
uint8_t tid
Definition meshx_ble_mesh_gen_cli.h:35
bool op_en
Definition meshx_ble_mesh_gen_cli.h:33
uint8_t onoff
Definition meshx_ble_mesh_gen_cli.h:34
meshx_ptr_t p_model
Definition meshx_ble_mesh_cmn.h:39
uint16_t pub_addr
Definition meshx_ble_mesh_cmn.h:38
uint16_t el_id
Definition meshx_ble_mesh_cmn.h:36
uint16_t model_id
Definition meshx_ble_mesh_cmn.h:37
meshx_gen_onoff_set_t onoff_set
Definition meshx_ble_mesh_gen_cli.h:117

◆ prepare_element_msg()

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

Prepare message for element notification.

This function prepares a message structure that will be sent to the parent element. The message must persist after this function returns, so it is stored as a member variable.

Parameters
[out]msg_ptrPointer to message structure (output parameter)
[out]msg_sizeSize of the message structure (output parameter)
Returns
  • MESHX_SUCCESS: Message prepared successfully
  • MESHX_INVALID_ARG: Invalid parameter

Implements meshXModelIF.

213{
214 if (!msg_ptr || !msg_size)
215 {
216 return MESHX_INVALID_ARG;
217 }
218
219 *msg_ptr = &element_msg;
220 *msg_size = sizeof(element_msg);
221
222 return MESHX_SUCCESS;
223}

◆ request_onoff()

meshx_err_t meshXGenericOnOffClientModel::request_onoff ( const meshx_gen_on_off_cli_msg_t * msg)

Request to change or get the Generic OnOff state.

This function is used to request a change or get the Generic OnOff state from the device.

Parameters
[in]msgPointer to the Generic OnOff request message structure
Returns
  • MESHX_SUCCESS: Successfully sent the request
  • MESHX_INVALID_ARG: One or more arguments are invalid
  • MESHX_INVALID_STATE: One or more states are invalid
149{
150 if (!msg) return MESHX_INVALID_ARG;
151
152 auto *el_state = static_cast<meshx_gen_onoff_model_state_t*>(this->get_parent_element_state());
153 auto *el = this->get_parent_element();
154 if (!el || !el_state) return MESHX_INVALID_STATE;
155
156 auto *common_ctx = static_cast<meshx_element_common_ctx_t*>(el->get_element_ctx());
157 if (!common_ctx) return MESHX_INVALID_STATE;
158
159 uint16_t opcode = (msg->set_get == MESHX_GEN_ON_OFF_CLI_MSG_GET)
162
163 meshx_model_t model_ref = {
164 .el_id = msg->element_id,
165 .model_id = (uint16_t)this->get_model_id(),
166 .pub_addr = common_ctx->pub_addr,
167 .p_model = (MESHX_MODEL*)this->get_plat_model()
168 };
169
170 meshx_ctx_t ctx = {
171 .app_idx = common_ctx->app_id,
172 .net_idx = meshx_get_net_key_id(),
173 .opcode = opcode,
174 .src_addr = 0,
175 .dst_addr = common_ctx->pub_addr,
176 .p_ctx = nullptr
177 };
178
180 .model = &model_ref,
181 .ctx = &ctx,
182 .state = { 0, 0, 0 },
183 .tid = 0
184 };
185
187 {
188 sp.state.on_off = msg->on_off;
189 sp.tid = ++el_state->tid;
190
191 /* Update local model state as well for consistency */
192 model_state.tid = el_state->tid;
193 }
194
195 return this->model_send(&sp);
196}
#define MESHX_GEN_ON_OFF_CLI_MSG_GET
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:27
MESHX_MODEL * get_plat_model(void) const
Get the platform-specific model instance.
Definition meshx_model_class.hpp:107
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
struct meshx_ctx meshx_ctx_t
Structure to hold context information for BLE Mesh operations.
struct meshx_gen_onoff_send_params meshx_gen_onoff_send_params_t
Definition meshx_model_onoff.hpp:51
uint8_t set_get
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:21
uint16_t element_id
Definition ble_mesh/elements/inc/meshx_relay_msg_defs.h:23

Field Documentation

◆ element_msg

meshx_on_off_cli_el_msg_t meshXGenericOnOffClientModel::element_msg
private

◆ model_state

meshx_gen_onoff_model_state_t meshXGenericOnOffClientModel::model_state
private

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