72 : mpParentObject(nullptr),
75 mErr.err = CN_CBOR_NO_ERROR;
111 return mErr.err != CN_CBOR_NO_ERROR;
121 return cn_cbor_mapget_string(mpMapNode, key) !=
nullptr;
132 auto existing = cn_cbor_mapget_int(mpMapNode, key);
133 if (existing && existing->type == CN_CBOR_ARRAY) {
134 return Array(
this, existing);
136 auto newArray = cn_cbor_array_create(_errback());
138 if (cn_cbor_mapput_int(mpMapNode, key, newArray, _errback())) {
140 return Array(
this, newArray);
142 cn_cbor_free(newArray);
146 return Array(
this,
nullptr);
157 auto existing = cn_cbor_mapget_string(mpMapNode, key);
158 if (existing && existing->type == CN_CBOR_ARRAY) {
159 return Array(
this, existing);
161 auto newArray = cn_cbor_array_create(_errback());
163 if (cn_cbor_mapput_string(mpMapNode, key, newArray, _errback())) {
165 return Array(
this, newArray);
167 cn_cbor_free(newArray);
171 return Array(
this,
nullptr);
181 return put(key,
static_cast<int64_t
>(value));
191 return put(key,
static_cast<int64_t
>(value));
202 bool updated =
false;
203 auto existing = cn_cbor_mapget_int(mpMapNode, key);
205 updated = cn_cbor_int_update(existing, value);
207 updated = cn_cbor_mapput_int(mpMapNode, key, cn_cbor_int_create(value, _errback()), _errback());
209 _markAsDirty(updated);
221 bool updated =
false;
222 auto existing = cn_cbor_mapget_int(mpMapNode, key);
224 updated = cn_cbor_string_update(existing, value);
226 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%d'", key);
229 updated = cn_cbor_mapput_int(mpMapNode, key, cn_cbor_string_create(value, _errback()), _errback());
231 _markAsDirty(updated);
244 bool updated =
false;
245 auto existing = cn_cbor_mapget_int(mpMapNode, key);
247 updated = cn_cbor_data_update(existing, value, size);
249 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%d'", key);
252 updated = cn_cbor_mapput_int(mpMapNode, key, cn_cbor_data_create(value, size, _errback()), _errback());
254 _markAsDirty(updated);
265 return put(key,
static_cast<int64_t
>(value));
275 return put(key,
static_cast<int64_t
>(value));
286 bool updated =
false;
287 auto existing = cn_cbor_mapget_string(mpMapNode, key);
289 updated = cn_cbor_int_update(existing, value);
291 updated = cn_cbor_mapput_string(mpMapNode, key, cn_cbor_int_create(value, _errback()), _errback());
293 _markAsDirty(updated);
305 bool updated =
false;
306 auto existing = cn_cbor_mapget_string(mpMapNode, key);
308 updated = cn_cbor_string_update(existing, value);
310 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%s'", key);
313 updated = cn_cbor_mapput_string(mpMapNode, key, cn_cbor_string_create(value, _errback()), _errback());
315 _markAsDirty(updated);
328 bool updated =
false;
329 auto existing = cn_cbor_mapget_string(mpMapNode, key);
331 updated = cn_cbor_data_update(existing, value, size);
333 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%s'", key);
336 updated = cn_cbor_mapput_string(mpMapNode, key, cn_cbor_data_create(value, size, _errback()), _errback());
338 _markAsDirty(updated);
349 auto existing = cn_cbor_mapget_string(mpMapNode, key);
351 return _getMap(existing);
354 auto newMap = cn_cbor_map_create(_errback());
355 auto success = cn_cbor_mapput_string(mpMapNode, key, newMap, _errback());
370 return _getMap(cn_cbor_mapget_int(mpMapNode, key));
380 return _getMap(cn_cbor_mapget_string(mpMapNode, key));
390 return _getBool(cn_cbor_mapget_int(mpMapNode, key));
400 return _getBool(cn_cbor_mapget_string(mpMapNode, key));
410 return _getInt(cn_cbor_mapget_int(mpMapNode, key));
420 return _getInt(cn_cbor_mapget_string(mpMapNode, key));
430 return _getString(cn_cbor_mapget_int(mpMapNode, key));
440 return _getString(cn_cbor_mapget_string(mpMapNode, key));
451 return _getValueAsString(cn_cbor_mapget_int(mpMapNode, key));
462 return _getValueAsString(cn_cbor_mapget_string(mpMapNode, key));
472 return _getBytes(cn_cbor_mapget_int(mpMapNode, key));
482 return _getBytes(cn_cbor_mapget_string(mpMapNode, key));
491 if (mpParentObject) {
492 UNIOT_LOG_WARN(
"the parent node is not null, the object is not read");
498 mpMapNode = cn_cbor_decode(mBuf.raw(), mBuf.size(), _errback());
510 auto visitSiblings = mpParentObject ==
nullptr;
511 return _build(mpMapNode, visitSiblings);
520 return mpParentObject !=
nullptr;
565 : mpContext(other.mpContext),
566 mpArrayNode(other.mpArrayNode) {
575 if (
this != &other) {
576 mpContext = other.mpContext;
577 mpArrayNode = other.mpArrayNode;
587 mpArrayNode =
nullptr;
595 return mpContext->mErr;
605 auto updated = cn_cbor_array_append(mpArrayNode, cn_cbor_int_create(value, mpContext->_errback()), mpContext->_errback());
606 mpContext->_markAsDirty(updated);
618 auto updated = cn_cbor_array_append(mpArrayNode, cn_cbor_string_create(value, mpContext->_errback()), mpContext->_errback());
619 mpContext->_markAsDirty(updated);
631 template <
typename T>
633 static_assert(std::is_integral<T>::value,
"only integral types are allowed");
635 for (
size_t i = 0; i < size; i++) {
636 append(
static_cast<int>(value[i]));
646 auto newArray = cn_cbor_array_create(mpContext->_errback());
648 auto updated = cn_cbor_array_append(mpArrayNode, newArray, mpContext->_errback());
649 mpContext->_markAsDirty(updated);
651 return Array(mpContext, newArray);
653 cn_cbor_free(newArray);
656 return Array(mpContext,
nullptr);
666 : mpContext(context), mpArrayNode(arrayNode) {}
669 cn_cbor *mpArrayNode;
679 mErr.err = CN_CBOR_NO_ERROR;
682 mpParentObject = parent;
690 mErr.err = CN_CBOR_NO_ERROR;
693 mpMapNode = cn_cbor_map_create(_errback());
694 mpParentObject =
nullptr;
702 if (!mpParentObject) {
704 cn_cbor_free(mpMapNode);
708 mpParentObject =
nullptr;
710 mErr.err = CN_CBOR_NO_ERROR;
721 Bytes _build(cn_cbor *cb,
bool visitSiblings =
true)
const {
722 auto calculated = cn_cbor_encoder_write(NULL, 0, 0, cb, visitSiblings);
723 Bytes bytes(
nullptr, calculated);
724 auto written = bytes.fill([&](uint8_t *buf,
size_t size) {
725 auto actual = cn_cbor_encoder_write(buf, 0, size, cb, visitSiblings);
727 UNIOT_LOG_ERROR(
"%s",
"CBORObject build failed, buffer size too small");
733 bytes.prune(written);
745 if (cb && CN_CBOR_MAP == cb->type) {
759 long _getBool(cn_cbor *cb)
const {
762 if (CN_CBOR_TRUE == cb->type) {
765 if (CN_CBOR_FALSE == cb->type) {
778 long _getInt(cn_cbor *cb)
const {
780 if (cb && CN_CBOR_INT == cb->type) {
782 }
else if (cb && CN_CBOR_UINT == cb->type) {
794 String _getString(cn_cbor *cb)
const {
796 if (cb && CN_CBOR_TEXT == cb->type) {
797 auto bytes = Bytes(cb->v.bytes, cb->length);
799 return String(bytes.c_str());
810 Bytes _getBytes(cn_cbor *cb)
const {
812 if (cb && CN_CBOR_BYTES == cb->type) {
813 return Bytes(cb->v.bytes, cb->length);
824 String _getValueAsString(cn_cbor *cb)
const {
827 if (CN_CBOR_TEXT == cb->type) {
828 auto bytes = Bytes(cb->v.bytes, cb->length);
830 return String(bytes.c_str());
832 if (CN_CBOR_INT == cb->type) {
833 return String(cb->v.sint);
835 if (CN_CBOR_UINT == cb->type) {
836 return String(cb->v.uint);
838 if (CN_CBOR_FLOAT == cb->type) {
839 return String(cb->v.f);
841 if (CN_CBOR_DOUBLE == cb->type) {
842 return String(cb->v.dbl);
844 if (CN_CBOR_TRUE == cb->type) {
847 if (CN_CBOR_FALSE == cb->type) {
861 bool _isPtrEqual(cn_cbor *cb,
const void *ptr)
const {
862 return ptr == (
const void *)cb->v.bytes;
870 void _markAsDirty(
bool updated) {
873 if (mpParentObject) {
874 mpParentObject->_markAsDirty(
true);
884 cn_cbor_errback *_errback() {
887 mErr.err = CN_CBOR_NO_ERROR;
895 cn_cbor_errback mErr;
Helper class for working with CBOR arrays.
Definition CBORObject.h:556
~Array()
Destructor.
Definition CBORObject.h:585
cn_cbor_errback getLastError()
Get the last error that occurred during array operations.
Definition CBORObject.h:594
friend class CBORObject
Definition CBORObject.h:557
Array(const Array &other)
Copy constructor.
Definition CBORObject.h:564
Array & append(const char *value)
Append a string to the array.
Definition CBORObject.h:616
Array & operator=(const Array &other)
Copy assignment operator.
Definition CBORObject.h:574
Array & append(int value)
Append an integer to the array.
Definition CBORObject.h:603
Array appendArray()
Append a new array as an element.
Definition CBORObject.h:645
Array & append(size_t size, const T *value)
Append multiple values from an array to the CBOR array.
Definition CBORObject.h:632
Definition CBORObject.h:40
CBORObject getMap(const char *key)
Get a map at a specific string key.
Definition CBORObject.h:379
friend class COSEMessage
Definition CBORObject.h:41
bool hasKey(const char *key) const
Check if the map contains a specific string key.
Definition CBORObject.h:120
bool dirty() const
Check if the object has been modified since creation or last read.
Definition CBORObject.h:528
long getInt(int key) const
Get an integer value at a specific integer key.
Definition CBORObject.h:409
CBORObject(const CBORObject &)
Copy constructor (not implemented)
Definition CBORObject.h:51
Bytes getBytes(int key) const
Get binary data at a specific integer key.
Definition CBORObject.h:471
long getInt(const char *key) const
Get an integer value at a specific string key.
Definition CBORObject.h:419
Array putArray(int key)
Create or get an array at a specific integer key.
Definition CBORObject.h:131
CBORObject & operator=(const CBORObject &)
Copy assignment operator (not implemented)
Definition CBORObject.h:62
CBORObject & put(int key, int64_t value)
Put a 64-bit integer value at a specific integer key.
Definition CBORObject.h:201
CBORObject(Bytes buf)
Construct a CBORObject from binary CBOR data.
Definition CBORObject.h:71
CBORObject putMap(const char *key)
Put a new map at a specific string key, or get the existing one.
Definition CBORObject.h:348
String getValueAsString(const char *key) const
Get a value as a string at a specific string key.
Definition CBORObject.h:461
CBORObject()
Construct an empty CBORObject.
Definition CBORObject.h:90
CBORObject & put(const char *key, int value)
Put an integer value at a specific string key.
Definition CBORObject.h:264
String getString(const char *key) const
Get a string value at a specific string key.
Definition CBORObject.h:439
CBORObject & put(int key, const uint8_t *value, int size)
Put binary data at a specific integer key.
Definition CBORObject.h:243
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:180
void forceDirty()
Force the object to be marked as dirty (modified)
Definition CBORObject.h:536
void read(const Bytes &buf)
Read CBOR data from a buffer.
Definition CBORObject.h:490
CBORObject & put(int key, uint64_t value)
Put an unsigned 64-bit integer value at a specific integer key.
Definition CBORObject.h:190
CBORObject & put(const char *key, uint64_t value)
Put an unsigned 64-bit integer value at a specific string key.
Definition CBORObject.h:274
String getString(int key) const
Get a string value at a specific integer key.
Definition CBORObject.h:429
CBORObject getMap(int key)
Get a map at a specific integer key.
Definition CBORObject.h:369
void clean()
Reset the object to an empty state.
Definition CBORObject.h:545
CBORObject & put(const char *key, const uint8_t *value, int size)
Put binary data at a specific string key.
Definition CBORObject.h:327
bool hasError()
Definition CBORObject.h:110
bool getBool(const char *key) const
Get a boolean value at a specific string key.
Definition CBORObject.h:399
cn_cbor_errback getLastError()
Get the last error that occurred during CBOR operations.
Definition CBORObject.h:106
CBORObject & put(int key, const char *value)
Put a string value at a specific integer key.
Definition CBORObject.h:220
CBORObject & put(const char *key, int64_t value)
Put a 64-bit integer value at a specific string key.
Definition CBORObject.h:285
virtual ~CBORObject()
Virtual destructor.
Definition CBORObject.h:98
Bytes getBytes(const char *key) const
Get binary data at a specific string key.
Definition CBORObject.h:481
Array putArray(const char *key)
Create or get an array at a specific string key.
Definition CBORObject.h:156
CBORObject & put(const char *key, const char *value)
Put a string value at a specific string key.
Definition CBORObject.h:304
bool isChild() const
Check if this object is a child node in a CBOR tree.
Definition CBORObject.h:519
Bytes build() const
Build the CBOR data into binary format.
Definition CBORObject.h:509
bool getBool(int key) const
Get a boolean value at a specific integer key.
Definition CBORObject.h:389
String getValueAsString(int key) const
Get a value as a string at a specific integer key.
Definition CBORObject.h:450
#define UNIOT_LOG_WARN(...)
Log an WARN level message Used for warnings about potentially problematic situations....
Definition Logger.h:294
#define UNIOT_LOG_WARN_IF(log_cond, log...)
Conditionally log an WARN level message Used for warnings about potentially problematic situations....
Definition Logger.h:302
#define UNIOT_LOG_DEBUG(...)
Log an DEBUG level message Used for general information about system operation. Only compiled if UNIO...
Definition Logger.h:340
#define UNIOT_LOG_ERROR_IF(log_cond, log...)
Conditionally log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:281
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:273
Contains all classes and functions related to the Uniot Core.