49template<
typename T,
typename =
void>
struct is_iterable : std::false_type {};
52 std::void_t<decltype(std::declval<T>().begin()),
53 decltype(std::declval<T>().end()),
54 decltype(std::declval<T>().push_back(
55 *(std::declval<T>().begin())))>> :
81 attribute parent()
const
105 if (type() == parent_type) {
108 return first_parent(parent_type);
138 if (
get(value_type, (uint8_t *)buffer, &size)) {
139 if constexpr (std::is_same<T, std::string>::value) {
140 return std::string(
reinterpret_cast<char *
>(buffer));
141 }
else if constexpr (
142 is_iterable<T>::value) {
143 typedef typename T::value_type v_t;
145 static_assert(std::is_trivially_copyable<v_t>::value);
147 v_t *start =
reinterpret_cast<v_t *
>(buffer);
148 v_t *end =
reinterpret_cast<v_t *
>(buffer + size);
149 std::copy(start, end, std::back_insert_iterator(v));
151 }
else if constexpr (std::is_integral<T>::value) {
153 return *(
reinterpret_cast<uint8_t *
>(buffer));
154 }
else if (size == 2) {
155 return *(
reinterpret_cast<uint16_t *
>(buffer));
158 static_assert(std::is_trivially_copyable<T>::value);
161 if (size ==
sizeof(T)) {
162 return *(
reinterpret_cast<T *
>(buffer));
164 throw std::invalid_argument(
165 "Unexpected attribute size for node " + name_and_id() +
".Expected: "
166 + std::to_string(
sizeof(T)) +
", actual: " + std::to_string(size)
167 +
" value state: " + std::to_string(value_type) +
".");
170 throw std::invalid_argument(
"Unable to read attribute " + name_and_id());
175 const uint8_t *value,
183 throw std::invalid_argument(
"Unable to write attribute " + name_and_id());
201 template<
typename T>
inline attribute &
204 if constexpr (std::is_same<T, char const *>::value) {
206 return set(value_type, std::string(value));
207 }
else if constexpr (std::is_same<T, std::string>::value) {
209 return set(value_type,
210 reinterpret_cast<const uint8_t *
>(value.c_str()),
212 }
else if constexpr (is_iterable<T>::value) {
213 typedef typename T::value_type v_t;
215 static_assert(std::is_trivially_copyable<v_t>::value);
219 std::copy(value.begin(), value.end(), std::back_insert_iterator(v));
220 return set(value_type,
221 reinterpret_cast<const uint8_t *
>(v.data()),
222 v.size() *
sizeof(v_t));
224 static_assert(std::is_trivially_copyable<T>::value);
225 return set(value_type,
226 reinterpret_cast<const uint8_t *
>(&value),
235 throw std::invalid_argument(
"Unable to write attribute");
242 template<
typename T> T reported()
const
249 template<
typename T> T desired()
const
262 bool desired_exists()
const
271 bool reported_exists()
const
276 template<
typename T> T desired_or_reported()
281 void clear_reported()
294 template<
typename T> attribute &set_reported(T v)
302 template<
typename T> attribute &set_desired(T v)
312 size_t child_count()
const
323 attribute child(
size_t index)
const
345 std::vector<attribute> children()
const
347 std::vector<attribute> _children;
348 size_t n_childs = child_count();
349 for (
size_t i = 0; i < n_childs; i++) {
350 _children.push_back(child(i));
364 std::vector<attribute> _children;
365 for (
size_t i = 0; i < child_count(); i++) {
366 if (child(i).type() == type) {
367 _children.push_back(child(i));
387 for (
auto c: children(type)) {
388 if (c.reported_exists() && c.reported<T>() == value) {
407 template<
typename T> attribute
412 for (
auto c: children(type)) {
413 if (c.exists(value_type) && c.get<T>(value_type) == value) {
426 bool is_valid()
const
446 static attribute root()
472 attribute a = child_by_type_and_value(type, val, value_state);
476 return add_node(type).set(value_state, val);
489 attribute a = child_by_type(type);
493 return add_node(type);
519 const std::function<
sl_status_t(attribute_store::attribute &,
int)> &func,
524 for (
auto c: children()) {
525 status = c.visit(func, depth + 1);
534 std::string name()
const
539 std::string name_and_id()
const
541 std::ostringstream out;
542 out << name() <<
" (Node " << _n <<
")";
547 std::string value_to_string()
const
550 std::string str_value;
557 return "Not displayable value for " + name_and_id();
562 str_value = _get_str_value<uint64_t>();
568 str_value = _get_str_value<int64_t>();
572 str_value = _get_str_value<double>();
575 str_value = _get_str_value<std::string>();
578 str_value = _get_str_value<std::vector<uint8_t>>();
581 return "Unkown value type for " + name_and_id();
584 return "Can't get values of " + name_and_id();
587 return name_and_id() +
" values : " + str_value;
610 std::string _get_str_value()
const {
614 return reported_value +
" (" + desired_value +
")";
619 if (!exists(state)) {
623 std::string str_value =
"";
625 if constexpr (std::is_same<T, std::vector<uint8_t>>::value) {
627 for (uint8_t
byte:
get<std::vector<uint8_t>>(state)) {
628 str_value += std::to_string(
byte) +
",";
630 str_value.pop_back();
632 }
else if constexpr (std::is_same<T, int64_t>::value
633 || std::is_same<T, uint64_t>::value
634 || std::is_same<T, double>::value) {
635 str_value = std::to_string(get<T>(state));
636 }
else if constexpr (std::is_same<T, std::string>::value) {
637 str_value = get<std::string>(state);
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
attribute_store_storage_type_t attribute_store_get_storage_type(attribute_store_type_t type)
Returns the storage type of a given attribute.
const char * attribute_store_get_type_name(attribute_store_type_t type)
Returns the registered name of an attribute.
@ I32_STORAGE_TYPE
Definition: attribute_store_type_registration.h:59
@ INVALID_STORAGE_TYPE
Definition: attribute_store_type_registration.h:73
@ U32_STORAGE_TYPE
Definition: attribute_store_type_registration.h:51
@ I16_STORAGE_TYPE
Definition: attribute_store_type_registration.h:57
@ U64_STORAGE_TYPE
Definition: attribute_store_type_registration.h:53
@ C_STRING_STORAGE_TYPE
Definition: attribute_store_type_registration.h:67
@ EMPTY_STORAGE_TYPE
Definition: attribute_store_type_registration.h:45
@ U16_STORAGE_TYPE
Definition: attribute_store_type_registration.h:49
@ BYTE_ARRAY_STORAGE_TYPE
Definition: attribute_store_type_registration.h:69
@ I8_STORAGE_TYPE
Definition: attribute_store_type_registration.h:55
@ I64_STORAGE_TYPE
Definition: attribute_store_type_registration.h:61
@ UNKNOWN_STORAGE_TYPE
Definition: attribute_store_type_registration.h:42
@ DOUBLE_STORAGE_TYPE
Definition: attribute_store_type_registration.h:65
@ U8_STORAGE_TYPE
Definition: attribute_store_type_registration.h:47
@ FIXED_SIZE_STRUCT_STORAGE_TYPE
Definition: attribute_store_type_registration.h:71
@ FLOAT_STORAGE_TYPE
Definition: attribute_store_type_registration.h:63
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)
Definition: attribute_callbacks.hpp:23
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.