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_composition.hpp
Go to the documentation of this file.
1#ifndef __MESHX_COMPOSITION_HPP__
2#define __MESHX_COMPOSITION_HPP__
3
4#include <meshx_fwd_decl.hpp>
5#include <vector>
6#include <memory>
7
8/**
9 * @class meshXComposition
10 * @brief Singleton manager for the dynamic BLE Mesh composition.
11 * @details This class manages the collection of elements and flattens them into
12 * the contiguous C-arrays required by the platform's BLE Mesh stack.
13 */
15public:
16 /**
17 * @brief Get the singleton instance
18 * @return Reference to the meshXComposition instance
19 */
21
22 /**
23 * @brief Add an element to the composition
24 * @param element Unique pointer to the element to add
25 */
26 void add_element(std::unique_ptr<meshXElementIF> element) {
27 elements.push_back(std::move(element));
28 }
29
30 /**
31 * @brief Get the list of elements
32 */
33 std::vector<std::unique_ptr<meshXElementIF>>& get_elements() { return elements; }
34
35 /**
36 * @brief Check if any element in the composition contains a specific model.
37 * @param model_id The model ID to search for.
38 * @return true if found, false otherwise.
39 */
40 bool has_model(uint16_t model_id) const {
41 for (const auto& el : elements) {
42 if (el && el->has_model(model_id)) return true;
43 }
44 return false;
45 }
46
47 /**
48 * @brief Check if there are any elements
49 */
50 bool has_elements() const { return !elements.empty(); }
51
52 /**
53 * @brief Clear all elements and reset state
54 */
56 elements.clear();
57 is_baked = false;
58 baked_elements.clear();
61 }
62
63 /**
64 * @brief Set the device structure pointer
65 * @param pdev Pointer to the device structure
66 */
67 void set_device_struct(dev_struct_t *pdev) { this->pdev = pdev; }
68
69 /**
70 * @brief "Bake" the dynamic composition into a contiguous C-array
71 * @details This flattens the C++ object tree into the format required by the BLE stack.
72 * @param cid Company ID.
73 * @param pid Product ID.
74 * @param vid Version ID.
75 * @return MESHX_SUCCESS on success, error code otherwise
76 */
77 meshx_err_t bake(uint16_t cid, uint16_t pid, uint16_t vid);
78
79 /**
80 * @brief Check if the composition is baked
81 * @return true if baked, false otherwise
82 */
83 bool is_composition_baked() const { return is_baked; }
84
85 /**
86 * @brief Get the baked elements array
87 */
88 MESHX_ELEMENT* get_baked_elements() {
89 return reinterpret_cast<MESHX_ELEMENT*>(baked_elements.data());
90 }
91
92 /**
93 * @brief Get the baked composition structure
94 */
95 MESHX_COMPOSITION* get_baked_composition() { return baked_comp_ptr; }
96
97private:
98 meshXComposition() : pdev(nullptr), is_baked(false), baked_comp_ptr(nullptr) {}
99 ~meshXComposition() = default;
100
101 // Delete copy/move to enforce singleton
104
107
108 // C++ object collection
109 std::vector<std::unique_ptr<meshXElementIF>> elements;
110
111 // Dynamic storage using byte vectors to bypass const-member assignment issues
112 std::vector<uint8_t> baked_elements;
113
114 // Contiguous model arrays for each element
115 // Index 0 is for root element, 1+ for dynamic elements
116 // Using vector<uint8_t> to avoid C++ constructor/copy issues with platform structs
117 std::vector<std::vector<uint8_t>> baked_sig_model_arrays;
118 std::vector<std::vector<uint8_t>> baked_ven_model_arrays;
119
120 MESHX_COMPOSITION baked_comp;
121 MESHX_COMPOSITION* baked_comp_ptr;
122};
123
124#endif /* __MESHX_COMPOSITION_HPP__ */
std::vector< uint8_t > baked_elements
Definition meshx_composition.hpp:112
MESHX_COMPOSITION * baked_comp_ptr
Definition meshx_composition.hpp:121
meshXComposition()
Definition meshx_composition.hpp:98
void add_element(std::unique_ptr< meshXElementIF > element)
Add an element to the composition.
Definition meshx_composition.hpp:26
MESHX_COMPOSITION * get_baked_composition()
Get the baked composition structure.
Definition meshx_composition.hpp:95
meshx_err_t bake(uint16_t cid, uint16_t pid, uint16_t vid)
"Bake" the dynamic composition into a contiguous C-array
Definition meshx_composition.cpp:34
std::vector< std::vector< uint8_t > > baked_ven_model_arrays
Definition meshx_composition.hpp:118
std::vector< std::unique_ptr< meshXElementIF > > & get_elements()
Get the list of elements.
Definition meshx_composition.hpp:33
bool is_composition_baked() const
Check if the composition is baked.
Definition meshx_composition.hpp:83
meshXComposition & operator=(const meshXComposition &)=delete
dev_struct_t * pdev
Definition meshx_composition.hpp:105
void clear_elements()
Clear all elements and reset state.
Definition meshx_composition.hpp:55
void set_device_struct(dev_struct_t *pdev)
Set the device structure pointer.
Definition meshx_composition.hpp:67
static meshXComposition & get_instance()
Get the singleton instance.
Definition meshx_composition.cpp:29
bool is_baked
Definition meshx_composition.hpp:106
meshXComposition(const meshXComposition &)=delete
MESHX_COMPOSITION baked_comp
Definition meshx_composition.hpp:120
std::vector< std::vector< uint8_t > > baked_sig_model_arrays
Definition meshx_composition.hpp:117
std::vector< std::unique_ptr< meshXElementIF > > elements
Definition meshx_composition.hpp:109
~meshXComposition()=default
bool has_elements() const
Check if there are any elements.
Definition meshx_composition.hpp:50
MESHX_ELEMENT * get_baked_elements()
Get the baked elements array.
Definition meshx_composition.hpp:88
bool has_model(uint16_t model_id) const
Check if any element in the composition contains a specific model.
Definition meshx_composition.hpp:40
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
Forward declaration of MeshX classes.