46 #if SATCAT5_CBOR_ENABLE
47 #include <qcbor/qcbor_encode.h>
48 #include <qcbor/qcbor_spiffy_decode.h>
51 #ifndef SATCAT5_QCBOR_BUFFER
52 #define SATCAT5_QCBOR_BUFFER 1500
114 { QCBOREncode_CloseArray(
cbor); }
121 { QCBOREncode_CloseMap(
cbor); }
124 QCBOREncodeContext*
const cbor;
142 QCBOREncodeContext* encode,
143 u8* buff,
unsigned size,
155 ,
m_read(encode->OutBuf.UB.ptr, 0)
163 template <
typename T>
167 template <
typename T>
190 { QCBOREncode_AddBool(
cbor, value); }
192 { QCBOREncode_AddInt64(
cbor, value); }
194 { QCBOREncode_AddInt64(
cbor, value); }
196 { QCBOREncode_AddInt64(
cbor, value); }
198 { QCBOREncode_AddInt64(
cbor, value); }
200 { QCBOREncode_AddUInt64(
cbor, value); }
202 { QCBOREncode_AddUInt64(
cbor, value); }
204 { QCBOREncode_AddUInt64(
cbor, value); }
206 { QCBOREncode_AddUInt64(
cbor, value); }
208 { QCBOREncode_AddFloat(
cbor, value); }
210 { QCBOREncode_AddDouble(
cbor, value); }
215 void add_array(u32 len,
const bool* value)
const;
217 { add_signed_array<s8>(len, value); }
219 { add_signed_array<s16>(len, value); }
221 { add_signed_array<s32>(len, value); }
223 { add_signed_array<s64>(len, value); }
225 { add_unsigned_array<u8>(len, value); }
227 { add_unsigned_array<u16>(len, value); }
229 { add_unsigned_array<u32>(len, value); }
231 { add_unsigned_array<u64>(len, value); }
232 void add_array(u32 len,
const float* value)
const;
233 void add_array(u32 len,
const double* value)
const;
239 { QCBOREncode_AddBytes(
cbor, {value, len}); }
241 { QCBOREncode_AddBytes(
cbor, {value, len}); }
257 { QCBOREncode_AddSZString(
cbor, value); }
261 { QCBOREncode_AddNULL(
cbor); }
275 QCBOREncodeContext* encode,
276 u8* buff,
unsigned size)
280 QCBOREncode_OpenArray(
cbor);
301 template <
typename KEYTYPE = const
char*>
305 explicit constexpr
MapWriter(QCBOREncodeContext* encode)
310 inline void add_bool(KEYTYPE key,
bool value)
const
311 {
add_key(key); QCBOREncode_AddBool(
cbor, value); }
313 {
add_key(key); QCBOREncode_AddInt64(
cbor, value); }
315 {
add_key(key); QCBOREncode_AddInt64(
cbor, value); }
317 {
add_key(key); QCBOREncode_AddInt64(
cbor, value); }
319 {
add_key(key); QCBOREncode_AddInt64(
cbor, value); }
321 {
add_key(key); QCBOREncode_AddUInt64(
cbor, value); }
323 {
add_key(key); QCBOREncode_AddUInt64(
cbor, value); }
325 {
add_key(key); QCBOREncode_AddUInt64(
cbor, value); }
327 {
add_key(key); QCBOREncode_AddUInt64(
cbor, value); }
328 inline void add_item(KEYTYPE key,
float value)
const
329 {
add_key(key); QCBOREncode_AddFloat(
cbor, value); }
330 inline void add_item(KEYTYPE key,
double value)
const
331 {
add_key(key); QCBOREncode_AddDouble(
cbor, value); }
336 void add_array(KEYTYPE key, u32 len,
const bool* value)
const;
337 inline void add_array(KEYTYPE key, u32 len,
const s8* value)
const
338 {
add_key(key); add_signed_array<s8>(len, value); }
339 inline void add_array(KEYTYPE key, u32 len,
const s16* value)
const
340 {
add_key(key); add_signed_array<s16>(len, value); }
341 inline void add_array(KEYTYPE key, u32 len,
const s32* value)
const
342 {
add_key(key); add_signed_array<s32>(len, value); }
343 inline void add_array(KEYTYPE key, u32 len,
const s64* value)
const
344 {
add_key(key); add_signed_array<s64>(len, value); }
345 inline void add_array(KEYTYPE key, u32 len,
const u8* value)
const
346 {
add_key(key); add_unsigned_array<u8>(len, value); }
347 inline void add_array(KEYTYPE key, u32 len,
const u16* value)
const
348 {
add_key(key); add_unsigned_array<u16>(len, value); }
349 inline void add_array(KEYTYPE key, u32 len,
const u32* value)
const
350 {
add_key(key); add_unsigned_array<u32>(len, value); }
351 inline void add_array(KEYTYPE key, u32 len,
const u64* value)
const
352 {
add_key(key); add_unsigned_array<u64>(len, value); }
353 void add_array(KEYTYPE key, u32 len,
const float* value)
const;
354 void add_array(KEYTYPE key, u32 len,
const double* value)
const;
359 inline void add_bytes(KEYTYPE key, u32 len,
const u8* value)
const
360 {
add_key(key); QCBOREncode_AddBytes(
cbor, {value, len}); }
361 inline void add_bytes(KEYTYPE key,
const u8* value, u32 len)
const
362 {
add_key(key); QCBOREncode_AddBytes(
cbor, {value, len}); }
379 {
add_key(key); QCBOREncode_AddSZString(
cbor, value); }
402 template <
unsigned SIZE = SATCAT5_QCBOR_BUFFER>
412 QCBOREncodeContext m_cbor;
421 typename KEYTYPE =
const char*,
429 :
CborWriter(dst, &m_cbor, m_raw, SIZE, true) {}
432 QCBOREncodeContext m_cbor;
468 inline bool ok()
const
469 {
return (
get_error() == QCBOR_SUCCESS); }
473 {
return QCBORDecode_GetError(
cbor); }
478 { QCBORDecode_ExitArray(
cbor); }
483 { QCBORDecode_ExitMap(
cbor); }
493 unsigned copy_all(QCBOREncodeContext* dst);
507 QCBORDecodeContext*
const cbor;
520 QCBORDecodeContext* decode,
521 u8* buff,
unsigned size);
593 QCBORDecodeContext*
open_map()
const;
656 QCBORDecodeContext* decode,
657 u8* buff,
unsigned size);
677 template <
typename KEYTYPE = const
char*>
685 explicit constexpr
MapReader(QCBORDecodeContext* decode)
786 QCBORDecodeContext* decode,
787 u8* buff,
unsigned size);
794 template <
unsigned SIZE = SATCAT5_QCBOR_BUFFER>
803 QCBORDecodeContext m_cbor;
812 typename KEYTYPE =
const char*,
819 :
MapReader<KEYTYPE>(src, &m_cbor, m_raw, SIZE) {}
822 QCBORDecodeContext m_cbor;
829 const QCBORItem& m_item;
831 constexpr
explicit Logger(
const QCBORItem& item)
846 template <
unsigned SIZE = SATCAT5_QCBOR_BUFFER>
848 template <
unsigned SIZE = SATCAT5_QCBOR_BUFFER>
850 template <
typename KEYTYPE = const
char*>
852 template <
typename KEYTYPE = const
char*>
855 typename KEYTYPE =
const char*,
859 typename KEYTYPE =
const char*,
Creates an ephemeral reader for the Concise Binary Object Representation (CBOR, IETF RFC8949),...
static const int ERR_OVERFLOW
Overflowed user array.
bool ok() const
Check if any errors have been encountered yet during decoding.
unsigned peek_integer_len(const u8 *rdptr) const
Predict the length of the next integer value.
QCBORError get_error() const
Get the QCBOR decoding error, if any.
bool copy_item(QCBOREncodeContext *dst)
Copy the next CBOR item to the specified destination.
void close_list() const
Resume parsing at the end of a nested list.
QCBORDecodeContext *const cbor
QCBOR context pointer.
static const int ERR_NOT_FOUND
Key not found.
void close_map() const
Resume parsing at the end of a nested dictionary.
unsigned copy_all(QCBOREncodeContext *dst)
Copy all remaining CBOR item(s) to the specified destination.
CborReader(satcat5::io::Readable *src, QCBORDecodeContext *decode, u8 *buff, unsigned size)
Opens a QCBOR Decode Context from an io::Readable.
constexpr CborReader(QCBORDecodeContext *decode)
Create a CborReader from an existing decoder context.
static const int ERR_QCBOR_INT
Misc QCBOR error.
static const int ERR_BAD_TYPE
Non-matching type.
int get_array_internal(satcat5::io::Writeable &dst, u8 qcbor_type, u8 type_size) const
Internal function for shared array logic.
QCBORItem m_item
QCBOR Decoder item.
Creates an ephemeral writer for the Concise Binary Object Representation (CBOR, IETF RFC8949),...
unsigned m_encoded_len
Encoded length.
constexpr CborWriter(QCBOREncodeContext *encode)
Create a CborWriter from an existing encoder context.
bool close_and_finalize()
Calls close() then m_dst.write_finalize().
void close_map()
Finish writing a nested dictionary to the Map.
bool close()
Close the QCBOR encoder by calling QCBOREncode_Finish and copy the resulting bytes to m_dst,...
CborWriter(satcat5::io::Writeable *dst, QCBOREncodeContext *encode, u8 *buff, unsigned size, bool automap=false)
Constructor requires child class to provide a working buffer.
void add_unsigned_array(unsigned len, const T *value) const
Templated function to add an array of unsigned values.
satcat5::io::ArrayRead m_read
Reads working buffer.
const u8 * m_encoded
Encoded data.
UsefulBufC get_encoded()
Get encoded data as a UsefulBuf, useful if no dst was given.
void add_signed_array(unsigned len, const T *value) const
Templated function to add an array of signed values.
satcat5::io::Readable * get_buffer()
Get encoded data as a Readable, useful if no dst was given.
u8 m_auto_close
Auto-close QCBOR Type.
void close_list()
Finish writing a list to the List.
QCBOREncodeContext *const cbor
QCBOR context pointer.
satcat5::io::Writeable *const m_dst
Writeable sink.
Read consecutive values from a CBOR Array/List.
satcat5::util::optional< QCBORItem > get_item() const
Read the next scalar value from the List.
satcat5::util::optional< bool > get_bool() const
Read the next scalar value from the List.
int get_bool_array(u8 *arr, unsigned arr_len) const
Read an array of boolean values from the List.
int get_bool_array(satcat5::io::Writeable &dst) const
Read an array of boolean values from the List.
satcat5::util::optional< satcat5::io::ArrayRead > get_string() const
Read a string from the List.
int get_double_array(double *arr, unsigned arr_len) const
Read an array of floating-point values from the List, always written as doubles to cover any precisio...
QCBORDecodeContext * open_list() const
Read a nested list from the List.
constexpr ListReader(QCBORDecodeContext *decode)
Create a ListReader from an existing decoder context.
int get_s64_array(satcat5::io::Writeable &dst) const
Read an array of integer values from the List, always written as 64-bit length signed values (s64) to...
satcat5::util::optional< satcat5::io::ArrayRead > get_bytes() const
Read a byte sequence from the List.
satcat5::util::optional< u64 > get_uint() const
Read the next scalar value from the List.
satcat5::util::optional< double > get_double() const
Read the next scalar value from the List.
int get_double_array(satcat5::io::Writeable &dst) const
Read an array of floating-point values from the List, always written as doubles to cover any precisio...
int get_s64_array(s64 *arr, unsigned arr_len) const
Read an array of integer values from the List, always written as 64-bit length signed values (s64) to...
QCBORDecodeContext * open_map() const
Read a nested dictionary from the List.
satcat5::util::optional< s64 > get_int() const
Read the next scalar value from the List.
ListReader variant with a statically-allocated buffer.
ListReaderStatic(satcat5::io::Readable *src)
Create this object and copy from a source io::Readable.
Write a series of consecutive items to a CBOR message.
void add_array(u32 len, const s8 *value) const
Write an array of scalar values to the List.
void add_item(u16 value) const
Write a scalar value to the List.
void add_array(u32 len, const bool *value) const
Write an array of scalar values to the List.
void add_array(u32 len, const u8 *value) const
Write an array of scalar values to the List.
void add_item(s16 value) const
Write a scalar value to the List.
void add_bytes(const u8 *value, u32 len) const
Write a byte sequence to the List.
void add_item(float value) const
Write a scalar value to the List.
void add_array(u32 len, const s32 *value) const
Write an array of scalar values to the List.
void add_array(u32 len, const u64 *value) const
Write an array of scalar values to the List.
void add_string(const char *value) const
Write a null-terminated string to the Map.
QCBOREncodeContext * open_map()
Add a nested dictionary to the List.
void add_array(u32 len, const s64 *value) const
Write an array of scalar values to the List.
void add_item(u64 value) const
Write a scalar value to the List.
void add_item(s8 value) const
Write a scalar value to the List.
void add_item(double value) const
Write a scalar value to the List.
QCBOREncodeContext * open_list()
Add a nested list to the List.
void add_array(u32 len, const u16 *value) const
Write an array of scalar values to the List.
void add_item(u32 value) const
Write a scalar value to the List.
void add_bytes(u32 len, const u8 *value) const
Write a byte sequence to the List.
void add_bool(bool value) const
Write a scalar value to the List.
constexpr ListWriter(QCBOREncodeContext *encode)
Create a ListWriter from an existing encoder context.
void add_array(u32 len, const s16 *value) const
Write an array of scalar values to the List.
void add_item(s32 value) const
Write a scalar value to the List.
void add_null() const
Write a key with a null value to the Map.
ListWriter(satcat5::io::Writeable *dst, QCBOREncodeContext *encode, u8 *buff, unsigned size)
Constructor requires child class to provide a working buffer.
void add_array(u32 len, const u32 *value) const
Write an array of scalar values to the List.
void add_item(s64 value) const
Write a scalar value to the List.
void add_item(u8 value) const
Write a scalar value to the List.
ListWriter variant with a statically-allocated buffer.
ListWriterStatic(satcat5::io::Writeable *dst=nullptr)
Create this object and set the destination.
Reads a series of key-value pairs from a CBOR Map.
int get_bool_array(KEYTYPE key, u8 *arr, unsigned arr_len) const
Read an array of boolean values from the Map, always written as u8 values regardless of platform size...
satcat5::util::optional< satcat5::io::ArrayRead > get_bytes(KEYTYPE key) const
Read a byte sequence from the Map.
int get_double_array(KEYTYPE key, satcat5::io::Writeable &dst) const
Read an array of floating-point values from the Map, always written as doubles to cover any precision...
QCBORDecodeContext * open_map(KEYTYPE key) const
Read a nested dictionary from the Map.
int get_s64_array(KEYTYPE key, s64 *arr, unsigned arr_len) const
Read an array of integer values from the Map, always written as 64-bit length signed values (s64) to ...
satcat5::util::optional< s64 > get_int(KEYTYPE key) const
Read a scalar value from the Map.
int get_double_array(KEYTYPE key, double *arr, unsigned arr_len) const
Read an array of floating-point values from the Map, always written as doubles to cover any precision...
QCBORDecodeContext * open_list(KEYTYPE key) const
Read a nested list from the Map.
satcat5::util::optional< bool > get_bool(KEYTYPE key) const
Read a scalar value from the Map.
int get_bool_array(KEYTYPE key, satcat5::io::Writeable &dst) const
Read an array of boolean values from the Map, always written as u8 values regardless of platform size...
satcat5::util::optional< u64 > get_uint(KEYTYPE key) const
Read a scalar value from the Map.
constexpr MapReader(QCBORDecodeContext *decode)
Create a MapReader from an existing decoder context.
satcat5::util::optional< double > get_double(KEYTYPE key) const
Read a scalar value from the Map.
int get_s64_array(KEYTYPE key, satcat5::io::Writeable &dst) const
Read an array of integer values from the Map, always written as 64-bit length signed values (s64) to ...
bool is_null(KEYTYPE key) const
Check if a key in the Map exists and is NULL.
satcat5::util::optional< satcat5::io::ArrayRead > get_string(KEYTYPE key) const
Read a string from the Map.
MapReader variant with a statically-allocated buffer.
MapReaderStatic(satcat5::io::Readable *src)
Create this object and copy from a source io::Readable.
Write a series of key-value pairs to a CBOR Map.
void add_bool(KEYTYPE key, bool value) const
Write a scalar value to the Map.
void add_item(KEYTYPE key, u64 value) const
Write a scalar value to the Map.
void add_item(KEYTYPE key, u8 value) const
Write a scalar value to the Map.
void add_null(KEYTYPE key) const
Write a key with a null value to the Map.
void add_string(KEYTYPE key, const char *value) const
Write a null-terminated string to the Map.
void add_key(KEYTYPE key) const
Templated function to write the correct key type to the Map.
void add_item(KEYTYPE key, s8 value) const
Write a scalar value to the Map.
void add_bytes(KEYTYPE key, u32 len, const u8 *value) const
Write a byte sequence to the Map.
void add_array(KEYTYPE key, u32 len, const s16 *value) const
Write an array of scalar values to the Map.
void add_item(KEYTYPE key, s16 value) const
Write a scalar value to the Map.
void add_array(KEYTYPE key, u32 len, const u32 *value) const
Write an array of scalar values to the Map.
void add_array(KEYTYPE key, u32 len, const s8 *value) const
Write an array of scalar values to the Map.
void add_item(KEYTYPE key, s32 value) const
Write a scalar value to the Map.
void add_item(KEYTYPE key, s64 value) const
Write a scalar value to the Map.
void add_array(KEYTYPE key, u32 len, const bool *value) const
Write an array of scalar values to the Map.
void add_array(KEYTYPE key, u32 len, const u16 *value) const
Write an array of scalar values to the Map.
void add_array(KEYTYPE key, u32 len, const s32 *value) const
Write an array of scalar values to the Map.
void add_item(KEYTYPE key, double value) const
Write a scalar value to the Map.
constexpr MapWriter(QCBOREncodeContext *encode)
Create a MapWriter from an existing encoder context.
void add_item(KEYTYPE key, float value) const
Write a scalar value to the Map.
void add_item(KEYTYPE key, u16 value) const
Write a scalar value to the Map.
MapWriter()
Placeholder constructor with no side-effects.
QCBOREncodeContext * open_list(KEYTYPE key)
Add a list to the Map.
void add_array(KEYTYPE key, u32 len, const u8 *value) const
Write an array of scalar values to the Map.
QCBOREncodeContext * open_map(KEYTYPE key)
Add a nested dictionary to the Map.
void add_array(KEYTYPE key, u32 len, const u64 *value) const
Write an array of scalar values to the Map.
void add_bytes(KEYTYPE key, const u8 *value, u32 len) const
Write a byte sequence to the Map.
void add_array(KEYTYPE key, u32 len, const s64 *value) const
Write an array of scalar values to the Map.
void add_item(KEYTYPE key, u32 value) const
Write a scalar value to the Map.
MapWriter variant with a statically-allocated buffer.
MapWriterStatic(satcat5::io::Writeable *dst=nullptr)
Create this object and set the destination.
Ephemeral Readable interface for a simple array.
Ephemeral Writeable interface for a simple array.
Abstract API for reading byte-streams and packets.
Abstract API for writing byte-streams and packets.
virtual bool write_finalize()
Mark end of frame and release temporary working data.
Internal buffer used by the Log class.
#define SATCAT5_QCBOR_BUFFER
Set the default size for the QCBOR buffer.
"Readable" I/O interface core definitions
"Writeable" I/O interface core definitions
Helper object for logging contents a QCBOR item.
An optional field that may be filled or empty.
Miscellaneous mathematical utility functions.