1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include "cbor_decoder.hpp"
#include <cstdint>
#include "esp-idf/components/cbor/tinycbor/src/cbor.h"
#include "include/cbor_decoder.hpp"
namespace cbor {
static auto ArrayDecoder::Create(uint8_t *buffer, size_t buffer_len) -> cpp::result<std::unique_ptr<ArrayDecoder>, CborError> {
auto decoder = std::make_unique<ArrayDecoder>();
cbor_parser_init(buffer, buffer_len, &decoder->parser_, &decoder->root_);
if (!cbor_value_is_array(&decoder->root_)) {
return cpp::fail(CborErrorIllegalType);
}
CborError err = cbor_value_enter_container(&decoder->root_, &decoder->it_);
if (err != CborNoError) {
return cpp::fail(err);
}
return std::move(decoder);
}
static auto MapDecoder::Create(uint8_t *buffer, size_t buffer_len) -> cpp::result<std::unique_ptr<MapDecoder>, CborError> {
auto decoder = std::make_unique<MapDecoder>();
cbor_parser_init(buffer, buffer_len, &decoder->parser_, &decoder->root_);
if (!cbor_value_is_map(&decoder->root_)) {
return cpp::fail(CborErrorIllegalType);
}
CborError err = cbor_value_enter_container(&decoder->root_, &decoder->it_);
if (err != CborNoError) {
return cpp::fail(err);
}
return std::move(decoder);
}
auto MapDecoder::FindString(const std::string &key) -> std::optional<std::string> {
CborValue val;
if (error_ != CborNoError) {
return {};
}
if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
return {};
}
if (!cbor_value_is_byte_string(&val)) {
error_ = CborErrorIllegalType;
return {};
}
uint8_t *buf; size_t len;
error_ = cbor_value_dup_byte_string(&val, &buf, &len, NULL);
if (error_ != CborNoError) {
return cpp::fail(error_);
}
std::string ret(buf, len);
free(buf);
return ret;
}
auto MapDecoder::FindUnsigned(const std::string &key) -> std::optional<uint32_t> {
CborValue val;
if (error_ != CborNoError) {
return {};
}
if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
return {};
}
if (!cbor_value_is_unsigned_integer(&val)) {
error_ = CborErrorIllegalType;
return {};
}
uint64_t ret;
error_ = cbor_value_get_uint64(&val, &ret);
if (error_ != CborNoError) {
return cpp::fail(error_);
}
return ret;
}
auto MapDecoder::FindSigned(const std::string &key) -> std::optional<int32_t> {
CborValue val;
if (error_ != CborNoError) {
return {};
}
if (cbor_value_map_find_value(&it_, key.c_str(), &val) != CborNoError) {
// Don't store this as an error; missing keys are fine.
return {};
}
if (!cbor_value_is_integer(&val)) {
error_ = CborErrorIllegalType;
return {};
}
int32_t ret;
error_ = cbor_value_get_int(&val, &ret);
if (error_ != CborNoError) {
return cpp::fail(error_);
}
return ret;
}
} // namespace cbor
|