From 9176ef187227ffb56c249c5f321cd1bf50d4cfcc Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 22 Nov 2022 17:05:02 +1100 Subject: Add cbor wrapper, and chunk streaming util --- src/cbor/include/cbor_decoder.hpp | 47 +++++++++++++++++++++++++++++++++++++++ src/cbor/include/cbor_encoder.hpp | 30 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/cbor/include/cbor_decoder.hpp create mode 100644 src/cbor/include/cbor_encoder.hpp (limited to 'src/cbor/include') diff --git a/src/cbor/include/cbor_decoder.hpp b/src/cbor/include/cbor_decoder.hpp new file mode 100644 index 00000000..249db9cc --- /dev/null +++ b/src/cbor/include/cbor_decoder.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +namespace cbor { + + class ArrayDecoder { + public: + static auto Create(uint8_t *buffer, size_t buffer_len) -> cpp::result, CborError>; + + auto ParseString() -> cpp::result; + auto ParseUnsigned() -> cpp::result; + auto ParseSigned() -> cpp::result; + + auto Failed() -> CborError { return error_; } + + ArrayDecoder(const ArrayDecoder&) = delete; + ArrayDecoder& operator=(const ArrayDecoder&) = delete; + private: + CborParser parser_; + CborValue root_; + + CborValue it_; + CborError error_ = CborNoError; + }; + + class MapDecoder { + public: + static auto Create(uint8_t *buffer, size_t buffer_len) -> cpp::result, CborError>; + + auto FindString(const std::string &key) -> std::optional; + auto FindUnsigned(const std::string &key) -> std::optional; + auto FindSigned(const std::string &key) -> std::optional; + + auto Failed() -> CborError { return error_; } + + MapDecoder(const MapDecoder&) = delete; + MapDecoder& operator=(const MapDecoder&) = delete; + private: + CborParser parser_; + CborValue root_; + + CborValue it_; + CborError error_ = CborNoError; + }; + + +} // namespace cbor diff --git a/src/cbor/include/cbor_encoder.hpp b/src/cbor/include/cbor_encoder.hpp new file mode 100644 index 00000000..0edbbdff --- /dev/null +++ b/src/cbor/include/cbor_encoder.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include "esp-idf/components/cbor/tinycbor/src/cbor.h" +namespace cbor { + + class Encoder { + public: + enum ContainerType { + CONTAINER_ARRAY, + CONTAINER_MAP + }; + Encoder(ContainerType type, uint32_t container_len, uint8_t *buffer, size_t buffer_len); + + auto WriteString(const std::string &val) -> void; + auto WriteUnsigned(uint32_t val) -> void; + auto WriteSigned(int32_t val) -> void; + + auto Finish() -> cpp::result; + + Encoder(const Encoder&) = delete; + Encoder& operator=(const Encoder&) = delete; + private: + CborEncoder root_encoder_; + CborEncoder container_encoder_; + + CborError error_ = CborNoError; + }; + +} // namespace cbor -- cgit v1.2.3