38namespace attribute_store
46template<
typename T,
typename =
void>
struct is_iterable : std::false_type {};
49 std::void_t<decltype(std::declval<T>().begin()),
50 decltype(std::declval<T>().end()),
51 decltype(std::declval<T>().push_back(
52 *(std::declval<T>().begin())))>> :
78 attribute parent()
const
102 if (type() == parent_type) {
105 return first_parent(parent_type);
135 if (
get(value_type, (uint8_t *)buffer, &size)) {
136 if constexpr(std::is_same<T,std::string>::value) {
137 return std::string(
reinterpret_cast<char*
>(buffer));
138 }
else if constexpr (is_iterable<T>::value) {
139 typedef typename T::value_type v_t;
141 static_assert(std::is_trivially_copyable<v_t>::value);
143 v_t *start =
reinterpret_cast<v_t *
>(buffer);
144 v_t *end =
reinterpret_cast<v_t *
>(buffer + size);
145 std::copy(start, end, std::back_insert_iterator(v));
147 }
else if constexpr (std::is_integral<T>::value) {
149 return *(
reinterpret_cast<uint8_t *
>(buffer));
150 }
else if (size == 2) {
151 return *(
reinterpret_cast<uint16_t *
>(buffer));
154 static_assert(std::is_trivially_copyable<T>::value);
157 if (size ==
sizeof(T)) {
158 return *(
reinterpret_cast<T *
>(buffer));
160 throw std::invalid_argument(
161 "Unexpected attribute size for node " + std::to_string(_n)
162 +
".Expected: " + std::to_string(
sizeof(T))
163 +
", actual: " + std::to_string(size)
164 +
" value state: " + std::to_string(value_type) +
".");
167 throw std::invalid_argument(
"Unable to read attribute "
168 + std::to_string(_n));
173 const uint8_t *value,
181 throw std::invalid_argument(
"Unable to write attribute "
182 + std::to_string(_n));
203 if constexpr(std::is_same<T,char const*>::value) {
205 return set(value_type,
reinterpret_cast<const uint8_t *
>(value), std::strlen(value)+1);
206 }
else if constexpr(std::is_same<T,std::string>::value) {
208 return set(value_type, value.c_str());
209 }
else if constexpr (is_iterable<T>::value) {
210 typedef typename T::value_type v_t;
212 static_assert(std::is_trivially_copyable<v_t>::value);
216 std::copy(value.begin(), value.end(), std::back_insert_iterator(v));
217 return set(value_type,
218 reinterpret_cast<const uint8_t *
>(v.data()),
219 v.size() *
sizeof(v_t));
222 static_assert(std::is_trivially_copyable<T>::value);
223 return set(value_type,
reinterpret_cast<const uint8_t *
>(&value),
sizeof(T));
232 throw std::invalid_argument(
"Unable to write attribute");
239 template<
typename T> T reported()
const
246 template<
typename T> T desired()
const
254 bool desired_exists()
263 bool reported_exists()
268 template<
typename T> T desired_or_reported()
273 void clear_reported()
286 template<
typename T> attribute &set_reported(T v)
294 template<
typename T> attribute &set_desired(T v)
304 size_t child_count()
const
315 attribute child(
size_t index)
const
337 std::vector<attribute> children()
const
339 std::vector<attribute> _children;
340 size_t n_childs = child_count();
341 for (
size_t i = 0; i < n_childs; i++) {
342 _children.push_back(child(i));
356 std::vector<attribute> _children;
357 for (
size_t i = 0; i < child_count(); i++) {
358 if (child(i).type() == type) {
359 _children.push_back(child(i));
379 for (
auto c: children(type)) {
380 if (c.reported_exists() && c.reported<T>() == value) {
393 bool is_valid()
const
413 static attribute root()
436 attribute a = child_by_type_and_value(type, val);
440 return add_node(type).set_reported(val);
453 attribute a = child_by_type(type);
457 return add_node(type);
483 const std::function<
sl_status_t(attribute_store::attribute &,
int)> &func,
488 for (
auto c: children()) {
489 status = c.visit(func, depth + 1);
uint32_t attribute_store_type_t
Definition: attribute_store.h:50
#define ATTRIBUTE_STORE_MAXIMUM_VALUE_LENGTH
Maximum length for a value associated to an attribute.
Definition: attribute_store.h:104
bool attribute_store_node_exists(attribute_store_node_t node)
Verify whether a node is in the Attribute Store.
attribute_store_node_t attribute_store_get_node_child(attribute_store_node_t node, uint32_t child_index)
Get the node handle of a child of a node.
sl_status_t attribute_store_get_node_attribute_value(attribute_store_node_t node, attribute_store_node_value_state_t value_state, uint8_t *value, uint8_t *value_size)
Get the attribute value of a node.
attribute_store_node_value_state_t
This is the value state of a value.
Definition: attribute_store.h:121
attribute_store_node_t attribute_store_get_node_parent(attribute_store_node_t node)
Get the node handle of the parent of a node.
attribute_store_node_t attribute_store_get_root()
Retrieve the root node of the tree.
attribute_store_node_t attribute_store_add_node(attribute_store_type_t type, attribute_store_node_t parent_node)
Add a new node in the current attribute store.
attribute_store_node_t attribute_store_get_node_child_by_type(attribute_store_node_t node, attribute_store_type_t child_type, uint32_t child_index)
Get the node handle of a child of a node with a certain type.
datastore_attribute_id_t attribute_store_node_t
Handle to identify attribute store nodes.
Definition: attribute_store.h:101
attribute_store_node_t attribute_store_get_first_parent_with_type(attribute_store_node_t node, attribute_store_type_t parent_type)
Traverse up the tree from a node and finds the first parent with a given type.
attribute_store_type_t attribute_store_get_node_type(attribute_store_node_t node)
Get the type of a node.
size_t attribute_store_get_node_child_count(attribute_store_node_t node)
Get the number of children of the given node.
sl_status_t attribute_store_delete_node(attribute_store_node_t node)
Delete a node and all its children from the attribute store.
sl_status_t attribute_store_set_node_attribute_value(attribute_store_node_t node, attribute_store_node_value_state_t value_state, const uint8_t *value, uint8_t value_size)
Set the attribute value of a node in the current attribute store tree.
#define ATTRIBUTE_STORE_INVALID_NODE
Special attribute_store_node_t value indicating that it does not exist.
Definition: attribute_store.h:106
@ DESIRED_OR_REPORTED_ATTRIBUTE
Retrieve the desired value if it exists if not then provide the reported attribute only used for read...
Definition: attribute_store.h:124
@ REPORTED_ATTRIBUTE
This is the value reported by the node or actual value.
Definition: attribute_store.h:122
@ DESIRED_ATTRIBUTE
This is the value to be applied to the node.
Definition: attribute_store.h:123
bool attribute_store_is_value_defined(attribute_store_node_t node, attribute_store_node_value_state_t value_state)
Indicate whether a value is defined in the attribute store.
Definition: attribute_store_helper.c:30
#define SL_STATUS_OK
No error.
Definition: sl_status.h:49
uint32_t sl_status_t
Definition: sl_status.h:139
#define SL_STATUS_ABORT
Operation aborted.
Definition: sl_status.h:57
bool operator==(const nil &a, const nil &b)
void get(const std::string &image_key, const std::string &subscribe_topic, const std::string &publish_topic, const uic_ota::image_ready_funct_t &image_ready_cb, uint32_t timeout_s)
Function for downloading images.