summaryrefslogtreecommitdiff
path: root/src/cbor/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-11-23 17:15:06 +1100
committerjacqueline <me@jacqueline.id.au>2022-11-23 17:15:06 +1100
commita7df2855889055976956a58d2a36f23626371ee9 (patch)
tree16e180e57f84474acaeb1893208cc07e278af6f4 /src/cbor/include
parentdfa9ab6e04689b99267092e016a91d9254f94cd8 (diff)
downloadtangara-fw-a7df2855889055976956a58d2a36f23626371ee9.tar.gz
Mostly done pipeline arch. Now onto cleanup and building.
Diffstat (limited to 'src/cbor/include')
-rw-r--r--src/cbor/include/cbor_decoder.hpp249
-rw-r--r--src/cbor/include/cbor_encoder.hpp55
2 files changed, 164 insertions, 140 deletions
diff --git a/src/cbor/include/cbor_decoder.hpp b/src/cbor/include/cbor_decoder.hpp
index 39151ca8..258d7c0e 100644
--- a/src/cbor/include/cbor_decoder.hpp
+++ b/src/cbor/include/cbor_decoder.hpp
@@ -5,121 +5,138 @@
namespace cbor {
- static auto parse_stdstring(CborValue *val, std::string *out) -> CborError {
- uint8_t *buf; size_t len;
- CborError err = cbor_value_dup_byte_string(val, &buf, &len, NULL);
- if (err != CborNoError) {
- return err;
- }
- *out = std::move(std::string(buf, len));
- free(buf);
- return err
+static auto parse_stdstring(CborValue* val, std::string* out) -> CborError {
+ uint8_t* buf;
+ size_t len;
+ CborError err = cbor_value_dup_byte_string(val, &buf, &len, NULL);
+ if (err != CborNoError) {
+ return err;
}
+ *out = std::move(std::string(buf, len));
+ free(buf);
+ return err
+}
- class ArrayDecoder {
- public:
- static auto Create(uint8_t *buffer, size_t buffer_len)
- -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError>;
-
- template<typename T>
- auto NextValue() -> cpp::result<T, CborError>;
-
- template<> auto NextValue() -> cpp::result<int64_t, CborError> {
- return NextValue(&cbor_value_is_integer, &cbor_value_get_int);
- }
- template<> auto NextValue() -> cpp::result<uint64_t, CborError> {
- return NextValue(&cbor_value_is_unsigned_integer, &cbor_value_get_uint64);
- }
- template<> auto NextValue() -> cpp::result<std::string, CborError> {
- return NextValue(&cbor_value_is_byte_string, &parse_stdstring);
- }
-
- template <typename T>
- auto NextValue(
- bool(*is_valid)(CborValue*),
- CborError(*parse)(CborValue*, T*)) -> cpp::result<T, CborError> {
- if (error_ != CborNoError) {
- return cpp::fail(error_);
- }
- if (!is_valid(&it_)) {
- error_ = CborErrorIllegalType;
- return cpp::fail(error_);
- }
- T ret;
- error_ = parse(&it_, &ret);
- if (error_ != CborNoError) {
- return cpp::fail(error_);
- }
- error_ = cbor_value_advance(&it_);
- if (error_ != CborNoError) {
- return cpp::fail(error_);
- }
- return ret;
- }
-
- 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<std::unique_ptr<MapDecoder>, CborError>;
-
- template<typename T>
- auto FindValue(const std::string &key) -> std::optional<T>;
-
- template<> auto FindValue(const std::string &key) -> std::optional<int64_t> {
- return FindValue(key, &cbor_value_is_integer, &cbor_value_get_int);
- }
- template<> auto FindValue(const std::string &key) -> std::optional<uint64_t> {
- return FindValue(key, &cbor_value_is_unsigned_integer, &cbor_value_get_uint64);
- }
- template<> auto FindValue(const std::string &key) -> std::optional<std::string> {
- return FindValue(key, &cbor_value_is_byte_string, &parse_stdstring);
- }
-
- template <typename T>
- auto FindValue(
- const std::string &key,
- bool(*is_valid)(CborValue*),
- CborError(*parse)(CborValue*, T*)) -> std::optional<T> {
- if (error_ != CborNoError) {
- return {};
- }
- if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
- return {};
- }
- if (!is_valid(&val)) {
- error_ = CborErrorIllegalType;
- return {};
- }
- T ret;
- error_ = parse(&val, &ret);
- if (error_ != CborNoError) {
- return cpp::fail(error_);
- }
- return ret;
- }
-
- 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
+class ArrayDecoder {
+ public:
+ static auto Create(uint8_t* buffer, size_t buffer_len)
+ -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError>;
+
+ static auto Create(CborValue& root)
+ -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError>;
+
+ template <typename T>
+ auto NextValue() -> cpp::result<T, CborError>;
+
+ template <>
+ auto NextValue() -> cpp::result<int64_t, CborError> {
+ return NextValue(&cbor_value_is_integer, &cbor_value_get_int);
+ }
+ template <>
+ auto NextValue() -> cpp::result<uint64_t, CborError> {
+ return NextValue(&cbor_value_is_unsigned_integer, &cbor_value_get_uint64);
+ }
+ template <>
+ auto NextValue() -> cpp::result<std::string, CborError> {
+ return NextValue(&cbor_value_is_byte_string, &parse_stdstring);
+ }
+
+ template <typename T>
+ auto NextValue(bool (*is_valid)(CborValue*),
+ CborError (*parse)(CborValue*, T*))
+ -> cpp::result<T, CborError> {
+ if (error_ != CborNoError) {
+ return cpp::fail(error_);
+ }
+ if (!is_valid(&it_)) {
+ error_ = CborErrorIllegalType;
+ return cpp::fail(error_);
+ }
+ T ret;
+ error_ = parse(&it_, &ret);
+ if (error_ != CborNoError) {
+ return cpp::fail(error_);
+ }
+ error_ = cbor_value_advance(&it_);
+ if (error_ != CborNoError) {
+ return cpp::fail(error_);
+ }
+ return ret;
+ }
+
+ auto Failed() -> CborError { return error_; }
+
+ auto Iterator() -> CborValue& { return it_; }
+
+ 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<std::unique_ptr<MapDecoder>, CborError>;
+
+ static auto Create(CborValue& root)
+ -> cpp::result<std::unique_ptr<MapDecoder>, CborError>;
+
+ template <typename T>
+ auto FindValue(const std::string& key) -> std::optional<T>;
+
+ template <>
+ auto FindValue(const std::string& key) -> std::optional<int64_t> {
+ return FindValue(key, &cbor_value_is_integer, &cbor_value_get_int);
+ }
+ template <>
+ auto FindValue(const std::string& key) -> std::optional<uint64_t> {
+ return FindValue(key, &cbor_value_is_unsigned_integer,
+ &cbor_value_get_uint64);
+ }
+ template <>
+ auto FindValue(const std::string& key) -> std::optional<std::string> {
+ return FindValue(key, &cbor_value_is_byte_string, &parse_stdstring);
+ }
+
+ template <typename T>
+ auto FindValue(const std::string& key,
+ bool (*is_valid)(CborValue*),
+ CborError (*parse)(CborValue*, T*)) -> std::optional<T> {
+ if (error_ != CborNoError) {
+ return {};
+ }
+ if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
+ return {};
+ }
+ if (!is_valid(&val)) {
+ error_ = CborErrorIllegalType;
+ return {};
+ }
+ T ret;
+ error_ = parse(&val, &ret);
+ if (error_ != CborNoError) {
+ return cpp::fail(error_);
+ }
+ return ret;
+ }
+
+ 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
index 0edbbdff..9479a3b6 100644
--- a/src/cbor/include/cbor_encoder.hpp
+++ b/src/cbor/include/cbor_encoder.hpp
@@ -4,27 +4,34 @@
#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<size_t, CborError>;
-
- Encoder(const Encoder&) = delete;
- Encoder& operator=(const Encoder&) = delete;
- private:
- CborEncoder root_encoder_;
- CborEncoder container_encoder_;
-
- CborError error_ = CborNoError;
- };
-
-} // 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);
+
+ template <typename T>
+ auto WriteKeyValue(const std::string& key, const T& val) -> void {
+ WriteValue(key);
+ WriteValue(val);
+ }
+
+ auto WriteValue(const std::string& val) -> void;
+ auto WriteValue(uint32_t val) -> void;
+ auto WriteValue(int32_t val) -> void;
+
+ auto Finish() -> cpp::result<size_t, CborError>;
+
+ Encoder(const Encoder&) = delete;
+ Encoder& operator=(const Encoder&) = delete;
+
+ private:
+ CborEncoder root_encoder_;
+ CborEncoder container_encoder_;
+
+ CborError error_ = CborNoError;
+};
+
+} // namespace cbor