Skip to content

Adding Cbor utility functions to decode string and byte array #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/cbor/utils/decoder.h
Original file line number Diff line number Diff line change
@@ -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;
}
}}
Loading