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_element_factory.hpp File Reference

Template-based factory helper for creating MeshX elements. More...

#include <meshx_element_class.hpp>
#include <new>

Go to the source code of this file.

Functions

template<typename ElementType, typename ContextType>
meshx_err_t meshx_element_factory_helper (dev_struct_t *pdev, uint16_t requested_cnt, uint16_t max_cnt, module_id_t module_id, const char *log_name)
 Helper function to create and register mesh elements.

Detailed Description

Template-based factory helper for creating MeshX elements.

This header provides a generic factory function to reduce boilerplate code when creating and registering different types of mesh elements.

Author
Pranjal Chanda
Date
2024-2025

Function Documentation

◆ meshx_element_factory_helper()

template<typename ElementType, typename ContextType>
meshx_err_t meshx_element_factory_helper ( dev_struct_t * pdev,
uint16_t requested_cnt,
uint16_t max_cnt,
module_id_t module_id,
const char * log_name )

Helper function to create and register mesh elements.

Template Parameters
ElementTypeThe concrete class of the element to create.
ContextTypeThe type of the element's NVS context structure.
Parameters
pdevPointer to the device structure.
requested_cntNumber of elements requested to be created.
max_cntCompile-time maximum number of elements supported.
module_idModule identifier for logging.
log_nameHuman-readable name of the element type for logging.
Returns
meshx_err_t MESHX_SUCCESS on success, error code otherwise.
39{
41
42 if (requested_cnt > max_cnt)
43 {
44 MESHX_LOGW(module_id,
45 "%s: requested count %d exceeds compile-time max %d. Capping.",
46 log_name, requested_cnt, max_cnt);
47 requested_cnt = max_cnt;
48 }
49
50 for (uint16_t i = 0; i < requested_cnt; i++)
51 {
52 uint16_t abs_idx = (uint16_t)(pdev->element_idx);
53
54 /* Construct element */
55 auto *el = new (std::nothrow) ElementType(abs_idx);
56 if (!el)
57 {
58 MESHX_LOGE(module_id, "%s [%d]: allocation failed", log_name, abs_idx);
59 return MESHX_NO_MEM;
60 }
61
62 /* Initialize element (lists models, allocates platform arrays, adds models) */
63 err = el->initialize();
64 if (err != MESHX_SUCCESS)
65 {
66 MESHX_LOGE(module_id, "%s [%d]: initialization failed: 0x%x", log_name, abs_idx, err);
67 return err;
68 }
69
70 /* Restore NVS context if available (initialize already calls this, but we can keep it explicit if preferred or rely on initialize) */
71 /* For consistency, we rely on initialize() to handle the full setup including NVS restore */
72
73 /* Add element to composition */
74 MESHX_LOGI(module_id, "%s [%d]: Registering Element with SIG=%d, VEN=%d models",
75 log_name, abs_idx, el->get_no_of_sig_models(), el->get_no_of_ven_models());
76
78 abs_idx,
79 pdev->elements,
80 el->get_sig_plat_model_array(),
81 el->get_ven_plat_model_array(),
82 el->get_no_of_sig_models(),
83 el->get_no_of_ven_models());
84
85 if (err != MESHX_SUCCESS)
86 {
87 MESHX_LOGE(module_id, "%s [%d]: composition registration failed: 0x%x",
88 log_name, abs_idx, err);
89 return err;
90 }
91
92 /* Increment device element index */
93 pdev->element_idx++;
94 }
95
96 return MESHX_SUCCESS;
97}
meshx_err_t meshx_plat_add_element_to_composition(uint16_t index, meshx_ptr_t p_element_list, meshx_ptr_t p_sig_models, meshx_ptr_t p_ven_models, uint8_t sig_cnt, uint8_t ven_cnt)
Adds an element to the BLE Mesh composition.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_NO_MEM
Definition meshx_err.h:48
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:120
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
#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
size_t element_idx
Definition meshx_common.h:71
MESHX_ELEMENT * elements
Definition meshx_common.h:73