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
Go to the documentation of this file.
1/**
2 * @file meshx_element_factory.hpp
3 * @brief Template-based factory helper for creating MeshX elements.
4 *
5 * This header provides a generic factory function to reduce boilerplate code
6 * when creating and registering different types of mesh elements.
7 *
8 * @author Pranjal Chanda
9 * @date 2024-2025
10 * @copyright Copyright 2024 - 2025 MeshX
11 */
12
13#ifndef __MESHX_ELEMENT_FACTORY_HPP__
14#define __MESHX_ELEMENT_FACTORY_HPP__
15
17#include <new>
18
19/**
20 * @brief Helper function to create and register mesh elements.
21 *
22 * @tparam ElementType The concrete class of the element to create.
23 * @tparam ContextType The type of the element's NVS context structure.
24 *
25 * @param pdev Pointer to the device structure.
26 * @param requested_cnt Number of elements requested to be created.
27 * @param max_cnt Compile-time maximum number of elements supported.
28 * @param module_id Module identifier for logging.
29 * @param log_name Human-readable name of the element type for logging.
30 * @return meshx_err_t MESHX_SUCCESS on success, error code otherwise.
31 */
32template <typename ElementType, typename ContextType>
34 dev_struct_t *pdev,
35 uint16_t requested_cnt,
36 uint16_t max_cnt,
37 module_id_t module_id,
38 const char *log_name)
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}
98
99#endif /* __MESHX_ELEMENT_FACTORY_HPP__ */
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.
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
MeshX Element class and interface declaration This file contains the meshXElement class and its inter...
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.
Definition meshx_element_factory.hpp:33
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
module_id_t
Enumeration of module IDs.
Definition module_id.h:27
size_t element_idx
Definition meshx_common.h:71
MESHX_ELEMENT * elements
Definition meshx_common.h:73