parent
71a4f5166f
commit
222c810b07
@ -1,131 +0,0 @@ |
|||||||
#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 ArrayDecoder::Create(CborValue& root) |
|
||||||
-> cpp::result<std::unique_ptr<ArrayDecoder>, CborError> { |
|
||||||
auto decoder = std::make_unique<ArrayDecoder>(); |
|
||||||
decoder->root_ = 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); |
|
||||||
} |
|
||||||
|
|
||||||
static auto MapDecoder::Create(CborValue& root) |
|
||||||
-> cpp::result<std::unique_ptr<MapDecoder>, CborError> { |
|
||||||
auto decoder = std::make_unique<MapDecoder>(); |
|
||||||
decoder->root_ = 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
|
|
@ -0,0 +1,112 @@ |
|||||||
|
#include "cbor_decoder.hpp" |
||||||
|
|
||||||
|
#include <cstdint> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "cbor.h" |
||||||
|
#include "result.hpp" |
||||||
|
|
||||||
|
static const int kDecoderFlags = 0; |
||||||
|
|
||||||
|
namespace cbor { |
||||||
|
|
||||||
|
auto parse_stdstring(const CborValue* val, std::string* out) -> CborError { |
||||||
|
char* buf; |
||||||
|
size_t len; |
||||||
|
CborError err = cbor_value_dup_text_string(val, &buf, &len, NULL); |
||||||
|
if (err != CborNoError) { |
||||||
|
return err; |
||||||
|
} |
||||||
|
*out = std::string(buf, len); |
||||||
|
free(buf); |
||||||
|
return err; |
||||||
|
} |
||||||
|
|
||||||
|
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, kDecoderFlags, &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); |
||||||
|
} |
||||||
|
|
||||||
|
auto ArrayDecoder::Create(CborValue& root) |
||||||
|
-> cpp::result<std::unique_ptr<ArrayDecoder>, CborError> { |
||||||
|
auto decoder = std::make_unique<ArrayDecoder>(); |
||||||
|
decoder->root_ = 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); |
||||||
|
} |
||||||
|
|
||||||
|
template <> |
||||||
|
auto ArrayDecoder::NextValue() -> cpp::result<int64_t, CborError> { |
||||||
|
return NextValue(&cbor_value_is_integer, &cbor_value_get_int); |
||||||
|
} |
||||||
|
template <> |
||||||
|
auto ArrayDecoder::NextValue() -> cpp::result<uint64_t, CborError> { |
||||||
|
return NextValue(&cbor_value_is_unsigned_integer, &cbor_value_get_uint64); |
||||||
|
} |
||||||
|
template <> |
||||||
|
auto ArrayDecoder::NextValue() -> cpp::result<std::string, CborError> { |
||||||
|
return NextValue(&cbor_value_is_byte_string, &parse_stdstring); |
||||||
|
} |
||||||
|
|
||||||
|
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, kDecoderFlags, &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::Create(CborValue& root) |
||||||
|
-> cpp::result<std::unique_ptr<MapDecoder>, CborError> { |
||||||
|
auto decoder = std::make_unique<MapDecoder>(); |
||||||
|
decoder->root_ = 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); |
||||||
|
} |
||||||
|
|
||||||
|
template <> |
||||||
|
auto MapDecoder::FindValue(const std::string& key) -> std::optional<int64_t> { |
||||||
|
return FindValue(key, &cbor_value_is_integer, &cbor_value_get_int); |
||||||
|
} |
||||||
|
template <> |
||||||
|
auto MapDecoder::FindValue(const std::string& key) -> std::optional<uint64_t> { |
||||||
|
return FindValue(key, &cbor_value_is_unsigned_integer, |
||||||
|
&cbor_value_get_uint64); |
||||||
|
} |
||||||
|
template <> |
||||||
|
auto MapDecoder::FindValue(const std::string& key) |
||||||
|
-> std::optional<std::string> { |
||||||
|
return FindValue(key, &cbor_value_is_byte_string, &parse_stdstring); |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace cbor
|
@ -1,6 +1,8 @@ |
|||||||
idf_component_register( |
idf_component_register( |
||||||
SRCS "codec.cpp" "mad.cpp" |
SRCS "codec.cpp" "mad.cpp" |
||||||
INCLUDE_DIRS "include") |
INCLUDE_DIRS "include" |
||||||
|
REQUIRES "result") |
||||||
|
|
||||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) |
|
||||||
add_dependencies("${COMPONENT_LIB}" libmad) |
add_dependencies("${COMPONENT_LIB}" libmad) |
||||||
|
target_compile_options("${COMPONENT_LIB}" PRIVATE ${EXTRA_WARNINGS}) |
||||||
|
target_include_directories("${COMPONENT_LIB}" PRIVATE "${LIBMAD_INCLUDE}") |
||||||
|
@ -1,10 +1,13 @@ |
|||||||
#include "codec.hpp" |
#include "codec.hpp" |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include "mad.hpp" |
||||||
|
|
||||||
namespace codecs { |
namespace codecs { |
||||||
|
|
||||||
auto CreateCodecForExtension(std::string extension) |
auto CreateCodecForExtension(std::string extension) |
||||||
-> cpp::result<std::unique_ptr<ICodec>, CreateCodecError> { |
-> cpp::result<std::unique_ptr<ICodec>, CreateCodecError> { |
||||||
return cpp::fail(UNKNOWN_EXTENSION); |
return std::make_unique<MadMp3Decoder>(); // TODO.
|
||||||
} |
} |
||||||
|
|
||||||
} // namespace codecs
|
} // namespace codecs
|
||||||
|
Loading…
Reference in new issue