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
meshx_base_model_class.hpp
Go to the documentation of this file.
1/**
2 * Copyright © 2024 - 2025 MeshX
3 *
4 * @file meshx_base_model_class.hpp
5 * @brief This file declares the meshXBaseModel class and its derived Client and Server classes.
6 *
7 * This file contains the meshXBaseModel class and its derived Client and Server classes.
8 * The meshXBaseModel class is used for the meshXBaseModel.
9 *
10 * @author Pranjal Chanda
11 */
12#ifndef _MESHX_BASE_MODEL_CLASS_H_
13#define _MESHX_BASE_MODEL_CLASS_H_
14
15#include <meshx_fwd_decl.hpp>
16#include <functional>
17#include <forward_list>
18#include <mutex>
19
20/**************************************************************************************************************************************************************
21 * meshXBaseModel
22 * @brief This class is used for the meshXBaseModel.
23 **************************************************************************************************************************************************************/
29
31
33
34/**
35 * @class meshXBaseModel
36 * @brief Base class for all mesh models
37 *
38 * This class serves as the foundation for all MeshX models, providing common
39 * functionality and interfaces for both client and server models.
40 *
41 * @tparam meshXBaseT Template parameter for base model customization.
42 * This should be a derived class that implements platform-specific behavior.
43 * @tparam ble_mesh_send_msg_params_t BLE mesh message parameters type.
44 * This type defines the structure used for sending BLE mesh messages.
45 *
46 * @note This is an abstract base class that cannot be instantiated directly.
47 * Use meshXBaseServerModel or meshXBaseClientModel instead.
48 */
51protected:
52 uint32_t model_id;
56public:
57 /**
58 * @brief Register BLE message callback for this model
59 * @details Automatically called during model initialization to register a callback
60 * function that handles BLE messages for this model's ID.
61 *
62 * @return MESHX_SUCCESS on success, error code otherwise
63 */
64 meshx_err_t from_ble_reg_cb(void) const;
65
66private:
67 struct reg_info { uint32_t id; int count; };
68 static std::forward_list<reg_info> registrations;
69
70protected:
71 /**
72 * @brief Deregister BLE message callback for this model
73 * @details Automatically called during model destruction to remove the callback
74 * function that handles BLE messages for this model's ID.
75 * @return MESHX_SUCCESS if deregistration successful, error code otherwise
76 */
77 virtual meshx_err_t from_ble_dereg_cb(void) const;
78
79public:
80 /**
81 * @brief Initialize platform model
82 * @details Pure virtual function to be implemented by derived classes for platform-specific
83 * model initialization. This function is called during model construction.
84 *
85 * @tparam meshXBaseT Derived model type
86 * @tparam ble_mesh_send_msg_params_t Type for BLE mesh send message parameters
87 *
88 * @return MESHX_SUCCESS on success, error code otherwise
89 * @retval MESHX_SUCCESS Initialization successful
90 * @retval MESHX_ERR_* Error code indicating reason for failure
91 */
92 virtual meshx_err_t plat_model_init(void) = 0;
93
94 /**
95 * @brief Send message through the model
96 * @details Pure virtual function to be implemented by derived classes for sending messages
97 * through the BLE mesh network. The implementation should handle the actual
98 * transmission of the message to the BLE stack.
99 *
100 * @tparam meshXBaseT Derived model type
101 * @tparam ble_mesh_send_msg_params_t Type for BLE mesh send message parameters
102 *
103 * @param[in] params Pointer to message parameters structure containing:
104 * - opcode: Message opcode
105 * - msg: Pointer to message data
106 * - len: Length of message data
107 * - dst: Destination address
108 * - app_idx: Application key index
109 * - ttl: Time to live value
110 *
111 * @return MESHX_SUCCESS on success, error code otherwise
112 * @retval MESHX_SUCCESS Message sent successfully
113 * @retval MESHX_ERR_INVALID_ARG Invalid parameters
114 * @retval MESHX_ERR_NO_MEM Insufficient memory
115 * @retval MESHX_ERR_INTERNAL Internal error
116 */
117 virtual meshx_err_t plat_send_msg(ble_mesh_send_msg_params_t *params) = 0;
118
119 /**
120 * @brief Get the current status of the model
121 * @return Current status code
122 */
123 meshx_err_t get_status(void) const { return status; }
124
125 /**
126 * @brief Get the model identifier
127 * @return Model ID value
128 */
129 uint32_t get_model_id(void) const { return model_id; }
130
131 /**
132 * @brief Get the BLE message callback function
133 * @return Callback function for handling BLE messages
134 */
136
137 /**
138 * @brief Get the model type (server/client)
139 * @return Model type enumeration value
140 */
142
143 /**
144 * @brief Set the model status
145 * @param[in] err Status code to set
146 */
147 void set_status(meshx_err_t err) { status = err; }
148
149 /**
150 * @brief Set the model identifier
151 * @param[in] id Model ID to set
152 */
153 void set_model_id(uint32_t id) { model_id = id; }
154
155 /**
156 * @brief Set the BLE message callback function
157 * @param[in] cb Callback function for handling BLE messages
158 */
160
161 /**
162 * @brief Set the model type
163 * @param[in] type Model type (server/client) to set
164 */
166
167 /**
168 * @brief Construct a new meshXBaseModel object
169 * @param[in] model_id Model identifier
170 * @param[in] from_ble_cb Callback for handling BLE messages
171 * @param[in] model_type Type of the model (server/client)
172 */
174 meshXBaseModel() = delete;
175
176 /**
177 * @brief Virtual destructor for meshXBaseModel
178 */
179 virtual ~meshXBaseModel();
180};
181
182/**************************************************************************************************************************************************************
183 * meshXBaseServerModel
184 * @brief This class is used for the meshXBaseServerModel.
185 **************************************************************************************************************************************************************/
186
187
189 class meshXBaseServerModel : public meshXBaseModel<ble_mesh_send_msg_params_t> {
190public:
191 using meshXBaseModel<ble_mesh_send_msg_params_t>::from_ble_reg_cb;
192private:
193 using base_server_model_cb_reg_t = struct base_server_model_cb_reg
194 {
195 uint16_t model_id; /**< Model ID associated with the registration. */
196 meshx_ptr_t p_plat_model; /**< Platform model pointer for instance verification. */
197 control_msg_cb cb; /**< Callback function associated with the registration. */
198 meshXBaseServerModel *instance; /**< Instance pointer for registration management. */
199 };
200
201protected:
203 static std::once_flag plat_server_init_flag;
204 static std::forward_list<base_server_model_cb_reg_t> base_server_model_cb_list;
206
207 /* Model validation function - to be implemented by derived classes */
208 virtual meshx_err_t validate_server_status_opcode(uint16_t opcode) = 0;
209public:
210 virtual meshx_err_t server_state_restore(ble_mesh_plat_restore_params_t* param) = 0;
212 virtual ~meshXBaseServerModel() = default;
214 meshx_err_t from_ble_dereg_cb(void) const override;
215
216 /**
217 * @brief Set the platform model pointer and update the registration list.
218 * @param[in] p_model Pointer to the platform model instance.
219 */
220 void set_plat_model_ptr(meshx_ptr_t p_model);
221};
222
223/**************************************************************************************************************************************************************
224 * meshXBaseClientModel
225 * @brief This class is used for the meshXBaseClientModel.
226 *************************************************************************************************************************************************************/
227
240
242
244 class meshXBaseClientModel : public meshXBaseModel<ble_mesh_send_msg_params_t> {
245public:
246 using meshXBaseModel<ble_mesh_send_msg_params_t>::from_ble_reg_cb;
247private:
248 using base_client_model_cb_reg_t = struct base_client_model_cb_reg
249 {
250 uint16_t model_id; /**< Model ID associated with the registration. */
251 meshx_ptr_t p_plat_model; /**< Platform model pointer for instance verification. */
252 control_msg_cb cb; /**< Callback function associated with the registration. */
253 meshXBaseClientModel *instance; /**< Instance pointer for registration management. */
254 };
255 using base_client_model_resend_ctx_t = struct base_client_model_resend_ctx
256 {
257 uint16_t model_id; /**< Model ID associated with the re-sending. */
258 ble_mesh_plat_model_cb_params_t param; /**< Params received from Platform callback */
259 };
260protected:
262 /* Re-initialization protection by multiple client objects */
263 static std::once_flag plat_client_init_flag;
264 static std::forward_list<base_client_model_cb_reg_t> base_client_model_cb_list;
265
266 /* Template type identification for debugging (RTTI-free) */
267 static constexpr const char* get_client_type_name() {
268 return __PRETTY_FUNCTION__;
269 }
270
271 /* Model validation function - to be implemented by derived classes */
273
274 /* Per instance template based static functions */
275 static meshx_err_t base_txcm_handle_ack (uint16_t src_addr);
276 static meshx_err_t base_txcm_handle_resend (uint16_t model_id, const ble_mesh_plat_model_cb_params_t *param);
279public:
280
284 meshx_err_t from_ble_dereg_cb(void) const override;
285
286 /**
287 * @brief Set the platform model pointer and update the registration list.
288 * @param[in] p_model Pointer to the platform model instance.
289 */
290 void set_plat_model_ptr(meshx_ptr_t p_model);
291};
292
293#endif /* _MESHX_BASE_MODEL_CLASS_H_ */
static meshx_err_t base_from_ble_msg_handle(dev_struct_t *pdev, control_task_msg_evt_t evt, meshx_ptr_t params)
Template-based static message handler for BLE Mesh Generic Client models.
Definition meshx_base_model_class.cpp:364
meshXBaseClientModel()=delete
static std::once_flag plat_client_init_flag
Definition meshx_base_model_class.hpp:263
struct base_client_model_cb_reg { uint16_t model_id; meshx_ptr_t p_plat_model; control_msg_cb cb; meshXBaseClientModel *instance; } base_client_model_cb_reg_t
Definition meshx_base_model_class.hpp:248
static meshx_err_t base_txcm_handle_resend(uint16_t model_id, const ble_mesh_plat_model_cb_params_t *param)
Resend a message for the given model ID and parameter.
Definition meshx_base_model_class.cpp:305
static meshx_err_t base_handle_txcm_msg(dev_struct_t *pdev, control_task_msg_evt_t evt, base_client_model_resend_ctx_t *param)
Template-based TXCM (Transmission Control Module) message handler for timeout scenarios.
Definition meshx_base_model_class.cpp:469
static constexpr const char * get_client_type_name()
Definition meshx_base_model_class.hpp:267
static std::forward_list< base_client_model_cb_reg_t > base_client_model_cb_list
Definition meshx_base_model_class.hpp:264
static meshx_err_t base_txcm_handle_ack(uint16_t src_addr)
Resend an acknowledgement message for the given source address.
Definition meshx_base_model_class.cpp:288
void set_plat_model_ptr(meshx_ptr_t p_model)
Set the platform model pointer and update the registration list.
Definition meshx_base_model_class.cpp:520
meshx_ptr_t p_plat_model
Definition meshx_base_model_class.hpp:261
virtual meshx_err_t validate_client_model_id(uint32_t model_id)=0
struct base_client_model_resend_ctx { uint16_t model_id; ble_mesh_plat_model_cb_params_t param; } base_client_model_resend_ctx_t
Definition meshx_base_model_class.hpp:255
meshx_err_t from_ble_dereg_cb(void) const override
Deregister BLE message callback for this model.
Definition meshx_base_model_class.cpp:506
~meshXBaseClientModel()=default
meshx_err_t from_ble_reg_cb(void) const
Register BLE message callback for this model.
Definition meshx_base_model_class.cpp:82
void set_from_ble_cb(control_task_msg_handle_t cb)
Set the BLE message callback function.
Definition meshx_base_model_class.hpp:159
uint32_t get_model_id(void) const
Get the model identifier.
Definition meshx_base_model_class.hpp:129
meshx_err_t status
Definition meshx_base_model_class.hpp:55
uint32_t model_id
Definition meshx_base_model_class.hpp:52
meshXBaseModel(uint32_t model_id, control_task_msg_handle_t from_ble_cb, meshXBaseModelType_t model_type)
Construct a new meshXBaseModel object.
Definition meshx_base_model_class.cpp:44
meshXBaseModelType_t model_type
Definition meshx_base_model_class.hpp:53
meshXBaseModelType_t get_model_type(void) const
Get the model type (server/client).
Definition meshx_base_model_class.hpp:141
control_task_msg_handle_t from_ble_cb
Definition meshx_base_model_class.hpp:54
meshx_err_t get_status(void) const
Get the current status of the model.
Definition meshx_base_model_class.hpp:123
virtual meshx_err_t from_ble_dereg_cb(void) const
Deregister BLE message callback for this model.
Definition meshx_base_model_class.cpp:115
void set_model_type(meshXBaseModelType_t type)
Set the model type.
Definition meshx_base_model_class.hpp:165
meshXBaseModel()=delete
static std::forward_list< reg_info > registrations
Definition meshx_base_model_class.hpp:68
virtual meshx_err_t plat_send_msg(ble_mesh_send_msg_params_t *params)=0
Send message through the model.
virtual ~meshXBaseModel()
Virtual destructor for meshXBaseModel.
Definition meshx_base_model_class.cpp:63
virtual meshx_err_t plat_model_init(void)=0
Initialize platform model.
control_task_msg_handle_t get_from_ble_cb(void) const
Get the BLE message callback function.
Definition meshx_base_model_class.hpp:135
void set_model_id(uint32_t id)
Set the model identifier.
Definition meshx_base_model_class.hpp:153
void set_status(meshx_err_t err)
Set the model status.
Definition meshx_base_model_class.hpp:147
static std::forward_list< base_server_model_cb_reg_t > base_server_model_cb_list
Definition meshx_base_model_class.hpp:204
virtual meshx_err_t server_state_restore(ble_mesh_plat_restore_params_t *param)=0
virtual meshx_err_t validate_server_status_opcode(uint16_t opcode)=0
static meshx_err_t base_from_ble_msg_handle(dev_struct_t *pdev, control_task_msg_evt_t evt, meshx_ptr_t params)
Sends a BLE message from a server model.
Definition meshx_base_model_class.cpp:176
meshx_err_t from_ble_dereg_cb(void) const override
Deregister BLE message callback for this model.
Definition meshx_base_model_class.cpp:213
meshx_ptr_t p_plat_model
Definition meshx_base_model_class.hpp:202
meshXBaseServerModel()=delete
void set_plat_model_ptr(meshx_ptr_t p_model)
Set the platform model pointer and update the registration list.
Definition meshx_base_model_class.cpp:227
struct base_server_model_cb_reg { uint16_t model_id; meshx_ptr_t p_plat_model; control_msg_cb cb; meshXBaseServerModel *instance; } base_server_model_cb_reg_t
Definition meshx_base_model_class.hpp:193
static std::once_flag plat_server_init_flag
Definition meshx_base_model_class.hpp:203
virtual ~meshXBaseServerModel()=default
meshx_base_cli_evt
Definition meshx_base_model_class.hpp:229
@ MESHX_BASE_CLI_EVT_SET
Definition meshx_base_model_class.hpp:231
@ MESHX_BASE_CLI_EVT_ALL
Definition meshx_base_model_class.hpp:234
@ MESHX_BASE_CLI_TIMEOUT
Definition meshx_base_model_class.hpp:233
@ MESHX_BASE_CLI_PUBLISH
Definition meshx_base_model_class.hpp:232
@ MESHX_BASE_CLI_EVT_GET
Definition meshx_base_model_class.hpp:230
enum meshXBaseModelType meshXBaseModelType_t
Definition meshx_base_model_class.hpp:30
meshXBaseModelType
Definition meshx_base_model_class.hpp:25
@ MESHX_BASE_MODEL_TYPE_CLIENT
Definition meshx_base_model_class.hpp:27
@ MESHX_BASE_MODEL_TYPE_SERVER
Definition meshx_base_model_class.hpp:26
enum meshx_base_cli_evt meshx_base_cli_evt_t
Definition meshx_base_model_class.hpp:241
std::function< meshx_err_t(dev_struct_t *, control_task_msg_evt_t, meshx_ptr_t)> control_msg_cb
Definition meshx_base_model_class.hpp:32
void * meshx_ptr_t
Definition meshx_ble_mesh_cmn_def.h:636
#define MESHX_BIT(nr)
Definition meshx_ble_mesh_cmn_def.h:22
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
uint32_t control_task_msg_evt_t
Type definition for control task message event.
Definition meshx_control_task.h:81
meshx_err_t(* control_task_msg_handle_t)(dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Function pointer type for control task message handler.
Definition meshx_control_task.h:188
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
Forward declaration of MeshX classes.
#define MESHX_BASE_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:22
#define MESHX_BASE_CLIENT_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:24
#define MESHX_BASE_SERVER_TEMPLATE_PROTO
Definition meshx_fwd_decl.hpp:26
**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
Definition meshx_base_model_class.hpp:67
uint32_t id
Definition meshx_base_model_class.hpp:67
int count
Definition meshx_base_model_class.hpp:67