72 : mpParentObject(nullptr),
75 mErr.err = CN_CBOR_NO_ERROR;
111 return mErr.err != CN_CBOR_NO_ERROR;
122 auto existing = cn_cbor_mapget_int(mpMapNode, key);
123 if (existing && existing->type == CN_CBOR_ARRAY) {
124 return Array(
this, existing);
126 auto newArray = cn_cbor_array_create(_errback());
128 if (cn_cbor_mapput_int(mpMapNode, key, newArray, _errback())) {
130 return Array(
this, newArray);
132 cn_cbor_free(newArray);
136 return Array(
this,
nullptr);
147 auto existing = cn_cbor_mapget_string(mpMapNode, key);
148 if (existing && existing->type == CN_CBOR_ARRAY) {
149 return Array(
this, existing);
151 auto newArray = cn_cbor_array_create(_errback());
153 if (cn_cbor_mapput_string(mpMapNode, key, newArray, _errback())) {
155 return Array(
this, newArray);
157 cn_cbor_free(newArray);
161 return Array(
this,
nullptr);
171 return put(key,
static_cast<int64_t
>(value));
181 return put(key,
static_cast<int64_t
>(value));
192 bool updated =
false;
193 auto existing = cn_cbor_mapget_int(mpMapNode, key);
195 updated = cn_cbor_int_update(existing, value);
197 updated = cn_cbor_mapput_int(mpMapNode, key, cn_cbor_int_create(value, _errback()), _errback());
199 _markAsDirty(updated);
211 bool updated =
false;
212 auto existing = cn_cbor_mapget_int(mpMapNode, key);
214 updated = cn_cbor_string_update(existing, value);
216 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%d'", key);
219 updated = cn_cbor_mapput_int(mpMapNode, key, cn_cbor_string_create(value, _errback()), _errback());
221 _markAsDirty(updated);
234 bool updated =
false;
235 auto existing = cn_cbor_mapget_int(mpMapNode, key);
237 updated = cn_cbor_data_update(existing, value, size);
239 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%d'", key);
242 updated = cn_cbor_mapput_int(mpMapNode, key, cn_cbor_data_create(value, size, _errback()), _errback());
244 _markAsDirty(updated);
255 return put(key,
static_cast<int64_t
>(value));
265 return put(key,
static_cast<int64_t
>(value));
276 bool updated =
false;
277 auto existing = cn_cbor_mapget_string(mpMapNode, key);
279 updated = cn_cbor_int_update(existing, value);
281 updated = cn_cbor_mapput_string(mpMapNode, key, cn_cbor_int_create(value, _errback()), _errback());
283 _markAsDirty(updated);
295 bool updated =
false;
296 auto existing = cn_cbor_mapget_string(mpMapNode, key);
298 updated = cn_cbor_string_update(existing, value);
300 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%s'", key);
303 updated = cn_cbor_mapput_string(mpMapNode, key, cn_cbor_string_create(value, _errback()), _errback());
305 _markAsDirty(updated);
318 bool updated =
false;
319 auto existing = cn_cbor_mapget_string(mpMapNode, key);
321 updated = cn_cbor_data_update(existing, value, size);
323 UNIOT_LOG_WARN_IF(_isPtrEqual(existing, value),
"pointer to the same value is specified for '%s'", key);
326 updated = cn_cbor_mapput_string(mpMapNode, key, cn_cbor_data_create(value, size, _errback()), _errback());
328 _markAsDirty(updated);
339 auto existing = cn_cbor_mapget_string(mpMapNode, key);
341 return _getMap(existing);
344 auto newMap = cn_cbor_map_create(_errback());
345 auto success = cn_cbor_mapput_string(mpMapNode, key, newMap, _errback());
360 return _getMap(cn_cbor_mapget_int(mpMapNode, key));
370 return _getMap(cn_cbor_mapget_string(mpMapNode, key));
380 return _getBool(cn_cbor_mapget_int(mpMapNode, key));
390 return _getBool(cn_cbor_mapget_string(mpMapNode, key));
400 return _getInt(cn_cbor_mapget_int(mpMapNode, key));
410 return _getInt(cn_cbor_mapget_string(mpMapNode, key));
420 return _getString(cn_cbor_mapget_int(mpMapNode, key));
430 return _getString(cn_cbor_mapget_string(mpMapNode, key));
441 return _getValueAsString(cn_cbor_mapget_int(mpMapNode, key));
452 return _getValueAsString(cn_cbor_mapget_string(mpMapNode, key));
462 return _getBytes(cn_cbor_mapget_int(mpMapNode, key));
472 return _getBytes(cn_cbor_mapget_string(mpMapNode, key));
481 if (mpParentObject) {
482 UNIOT_LOG_WARN(
"the parent node is not null, the object is not read");
488 mpMapNode = cn_cbor_decode(mBuf.raw(), mBuf.size(), _errback());
500 auto visitSiblings = mpParentObject ==
nullptr;
501 return _build(mpMapNode, visitSiblings);
510 return mpParentObject !=
nullptr;
555 : mpContext(other.mpContext),
556 mpArrayNode(other.mpArrayNode) {
565 if (
this != &other) {
566 mpContext = other.mpContext;
567 mpArrayNode = other.mpArrayNode;
577 mpArrayNode =
nullptr;
585 return mpContext->mErr;
595 auto updated = cn_cbor_array_append(mpArrayNode, cn_cbor_int_create(value, mpContext->_errback()), mpContext->_errback());
596 mpContext->_markAsDirty(updated);
608 auto updated = cn_cbor_array_append(mpArrayNode, cn_cbor_string_create(value, mpContext->_errback()), mpContext->_errback());
609 mpContext->_markAsDirty(updated);
621 template <
typename T>
623 static_assert(std::is_integral<T>::value,
"only integral types are allowed");
625 for (
size_t i = 0; i < size; i++) {
626 append(
static_cast<int>(value[i]));
636 auto newArray = cn_cbor_array_create(mpContext->_errback());
638 auto updated = cn_cbor_array_append(mpArrayNode, newArray, mpContext->_errback());
639 mpContext->_markAsDirty(updated);
641 return Array(mpContext, newArray);
643 cn_cbor_free(newArray);
646 return Array(mpContext,
nullptr);
656 : mpContext(context), mpArrayNode(arrayNode) {}
659 cn_cbor *mpArrayNode;
669 mErr.err = CN_CBOR_NO_ERROR;
672 mpParentObject = parent;
680 mErr.err = CN_CBOR_NO_ERROR;
683 mpMapNode = cn_cbor_map_create(_errback());
684 mpParentObject =
nullptr;
692 if (!mpParentObject) {
694 cn_cbor_free(mpMapNode);
698 mpParentObject =
nullptr;
700 mErr.err = CN_CBOR_NO_ERROR;
711 Bytes _build(cn_cbor *cb,
bool visitSiblings =
true)
const {
712 auto calculated = cn_cbor_encoder_write(NULL, 0, 0, cb, visitSiblings);
713 Bytes bytes(
nullptr, calculated);
714 auto written = bytes.fill([&](uint8_t *buf,
size_t size) {
715 auto actual = cn_cbor_encoder_write(buf, 0, size, cb, visitSiblings);
717 UNIOT_LOG_ERROR(
"%s",
"CBORObject build failed, buffer size too small");
723 return bytes.prune(written);
734 if (cb && CN_CBOR_MAP == cb->type) {
748 long _getBool(cn_cbor *cb)
const {
751 if (CN_CBOR_TRUE == cb->type) {
754 if (CN_CBOR_FALSE == cb->type) {
767 long _getInt(cn_cbor *cb)
const {
769 if (cb && CN_CBOR_INT == cb->type) {
771 }
else if (cb && CN_CBOR_UINT == cb->type) {
783 String _getString(cn_cbor *cb)
const {
785 if (cb && CN_CBOR_TEXT == cb->type) {
786 auto bytes = Bytes(cb->v.bytes, cb->length);
788 return String(bytes.c_str());
799 Bytes _getBytes(cn_cbor *cb)
const {
801 if (cb && CN_CBOR_BYTES == cb->type) {
802 return Bytes(cb->v.bytes, cb->length);
813 String _getValueAsString(cn_cbor *cb)
const {
816 if (CN_CBOR_TEXT == cb->type) {
817 auto bytes = Bytes(cb->v.bytes, cb->length);
819 return String(bytes.c_str());
821 if (CN_CBOR_INT == cb->type) {
822 return String(cb->v.sint);
824 if (CN_CBOR_UINT == cb->type) {
825 return String(cb->v.uint);
827 if (CN_CBOR_FLOAT == cb->type) {
828 return String(cb->v.f);
830 if (CN_CBOR_DOUBLE == cb->type) {
831 return String(cb->v.dbl);
833 if (CN_CBOR_TRUE == cb->type) {
836 if (CN_CBOR_FALSE == cb->type) {
850 bool _isPtrEqual(cn_cbor *cb,
const void *ptr)
const {
851 return ptr == (
const void *)cb->v.bytes;
859 void _markAsDirty(
bool updated) {
862 if (mpParentObject) {
863 mpParentObject->_markAsDirty(
true);
873 cn_cbor_errback *_errback() {
876 mErr.err = CN_CBOR_NO_ERROR;
884 cn_cbor_errback mErr;
Helper class for working with CBOR arrays.
Definition CBORObject.h:546
~Array()
Destructor.
Definition CBORObject.h:575
cn_cbor_errback getLastError()
Get the last error that occurred during array operations.
Definition CBORObject.h:584
friend class CBORObject
Definition CBORObject.h:547
Array(const Array &other)
Copy constructor.
Definition CBORObject.h:554
Array & append(const char *value)
Append a string to the array.
Definition CBORObject.h:606
Array & operator=(const Array &other)
Copy assignment operator.
Definition CBORObject.h:564
Array & append(int value)
Append an integer to the array.
Definition CBORObject.h:593
Array appendArray()
Append a new array as an element.
Definition CBORObject.h:635
Array & append(size_t size, const T *value)
Append multiple values from an array to the CBOR array.
Definition CBORObject.h:622
Definition CBORObject.h:40
CBORObject getMap(const char *key)
Get a map at a specific string key.
Definition CBORObject.h:369
friend class COSEMessage
Definition CBORObject.h:41
bool dirty() const
Check if the object has been modified since creation or last read.
Definition CBORObject.h:518
long getInt(int key) const
Get an integer value at a specific integer key.
Definition CBORObject.h:399
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:461
long getInt(const char *key) const
Get an integer value at a specific string key.
Definition CBORObject.h:409
Array putArray(int key)
Create or get an array at a specific integer key.
Definition CBORObject.h:121
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:191
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:338
String getValueAsString(const char *key) const
Get a value as a string at a specific string key.
Definition CBORObject.h:451
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:254
String getString(const char *key) const
Get a string value at a specific string key.
Definition CBORObject.h:429
CBORObject & put(int key, const uint8_t *value, int size)
Put binary data at a specific integer key.
Definition CBORObject.h:233
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:170
void forceDirty()
Force the object to be marked as dirty (modified)
Definition CBORObject.h:526
void read(const Bytes &buf)
Read CBOR data from a buffer.
Definition CBORObject.h:480
CBORObject & put(int key, uint64_t value)
Put an unsigned 64-bit integer value at a specific integer key.
Definition CBORObject.h:180
CBORObject & put(const char *key, uint64_t value)
Put an unsigned 64-bit integer value at a specific string key.
Definition CBORObject.h:264
String getString(int key) const
Get a string value at a specific integer key.
Definition CBORObject.h:419
CBORObject getMap(int key)
Get a map at a specific integer key.
Definition CBORObject.h:359
void clean()
Reset the object to an empty state.
Definition CBORObject.h:535
CBORObject & put(const char *key, const uint8_t *value, int size)
Put binary data at a specific string key.
Definition CBORObject.h:317
bool hasError()
Definition CBORObject.h:110
bool getBool(const char *key) const
Get a boolean value at a specific string key.
Definition CBORObject.h:389
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:210
CBORObject & put(const char *key, int64_t value)
Put a 64-bit integer value at a specific string key.
Definition CBORObject.h:275
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:471
Array putArray(const char *key)
Create or get an array at a specific string key.
Definition CBORObject.h:146
CBORObject & put(const char *key, const char *value)
Put a string value at a specific string key.
Definition CBORObject.h:294
bool isChild() const
Check if this object is a child node in a CBOR tree.
Definition CBORObject.h:509
Bytes build() const
Build the CBOR data into binary format.
Definition CBORObject.h:499
bool getBool(int key) const
Get a boolean value at a specific integer key.
Definition CBORObject.h:379
String getValueAsString(int key) const
Get a value as a string at a specific integer key.
Definition CBORObject.h:440
#define UNIOT_LOG_WARN(...)
Log an WARN level message Used for warnings about potentially problematic situations....
Definition Logger.h:247
#define UNIOT_LOG_WARN_IF(log_cond, log...)
Conditionally log an WARN level message Used for warnings about potentially problematic situations....
Definition Logger.h:255
#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:234
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:226
Contains all classes and functions related to the Uniot Core.