From b29258eba7bd9a761bd2bbef7275651bad4ca4f7 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Fri, 9 May 2025 08:53:19 +0200 Subject: [PATCH] Adding Cbor utility functions to decode string and byte array --- src/cbor/utils/decoder.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/cbor/utils/decoder.h diff --git a/src/cbor/utils/decoder.h b/src/cbor/utils/decoder.h new file mode 100644 index 0000000..c28225b --- /dev/null +++ b/src/cbor/utils/decoder.h @@ -0,0 +1,40 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ +#pragma once +#include "../tinycbor/cbor-lib.h" + +namespace cbor { namespace utils { + + inline MessageDecoder::Status copyCBORStringToArray(CborValue * param, char * dest, size_t& dest_size) { + if (!cbor_value_is_text_string(param)) { + return MessageDecoder::Status::Error; + } + + // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string + if(_cbor_value_copy_string(param, dest, &dest_size, NULL) != CborNoError) { + return MessageDecoder::Status::Error; + } + + return MessageDecoder::Status::InProgress; + } + + inline MessageDecoder::Status copyCBORByteToArray(CborValue * param, uint8_t * dest, size_t& dest_size) { + if (!cbor_value_is_byte_string(param)) { + return MessageDecoder::Status::Error; + } + + // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string + if(_cbor_value_copy_string(param, dest, &dest_size, NULL) != CborNoError) { + return MessageDecoder::Status::Error; + } + + return MessageDecoder::Status::InProgress; + } +}}