Skip to content

Commit b29258e

Browse files
Adding Cbor utility functions to decode string and byte array
1 parent 55a50dc commit b29258e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/cbor/utils/decoder.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
#pragma once
11+
#include "../tinycbor/cbor-lib.h"
12+
13+
namespace cbor { namespace utils {
14+
15+
inline MessageDecoder::Status copyCBORStringToArray(CborValue * param, char * dest, size_t& dest_size) {
16+
if (!cbor_value_is_text_string(param)) {
17+
return MessageDecoder::Status::Error;
18+
}
19+
20+
// NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
21+
if(_cbor_value_copy_string(param, dest, &dest_size, NULL) != CborNoError) {
22+
return MessageDecoder::Status::Error;
23+
}
24+
25+
return MessageDecoder::Status::InProgress;
26+
}
27+
28+
inline MessageDecoder::Status copyCBORByteToArray(CborValue * param, uint8_t * dest, size_t& dest_size) {
29+
if (!cbor_value_is_byte_string(param)) {
30+
return MessageDecoder::Status::Error;
31+
}
32+
33+
// NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
34+
if(_cbor_value_copy_string(param, dest, &dest_size, NULL) != CborNoError) {
35+
return MessageDecoder::Status::Error;
36+
}
37+
38+
return MessageDecoder::Status::InProgress;
39+
}
40+
}}

0 commit comments

Comments
 (0)