|
SatCat5
|
Read consecutive values from a CBOR Array/List.
CBOR makes no formal distinction between arrays and lists; they are simply consecutive values that may have different underlying types.
Creates an ephemeral reader for the Concise Binary Object Representation (CBOR, IETF RFC8949), holding underlying objects and providing more concise member functions. This reader wraps the QCBOR library into a more familiar interface that performs stack buffer allocation and io::Readable handling automatically. QCBOR decode requires a working buffer to hold the in-progress object, which should be allocated by a child class, typically cbor::ListReaderStatic or cbor::MapReaderStatic.
Most users should be able to hit target functionality with usage of cbor::ListReader cbor::MapReader or other future wrappers. However, the QCBOR decode context cbor is a public member variable that may be used for calls to QCBORDecode_* functions for complex use-cases outside the scope of provided wrapper classes.
Usage:
QCBORDecode_* functions with the given QCBOR context cbor.Most users should instantiate ListReaderStatic or MapReaderStatic instead of this to perform all stack buffer allocation.
#include <io_cbor.h>
Public Member Functions | |
| constexpr | ListReader (QCBORDecodeContext *decode) |
| Create a ListReader from an existing decoder context. More... | |
| satcat5::util::optional< satcat5::io::ArrayRead > | get_string () const |
| Read a string from the List. More... | |
| satcat5::util::optional< satcat5::io::ArrayRead > | get_bytes () const |
| Read a byte sequence from the List. More... | |
| QCBORDecodeContext * | open_list () const |
| Read a nested list from the List. More... | |
| QCBORDecodeContext * | open_map () const |
| Read a nested dictionary from the List. More... | |
| bool | ok () const |
| Check if any errors have been encountered yet during decoding. | |
| QCBORError | get_error () const |
| Get the QCBOR decoding error, if any. | |
| void | close_list () const |
| Resume parsing at the end of a nested list. More... | |
| void | close_map () const |
| Resume parsing at the end of a nested dictionary. More... | |
| bool | copy_item (QCBOREncodeContext *dst) |
| Copy the next CBOR item to the specified destination. More... | |
| unsigned | copy_all (QCBOREncodeContext *dst) |
| Copy all remaining CBOR item(s) to the specified destination. More... | |
| 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. | |
| satcat5::util::optional< s64 > | get_int () const |
| Read the next scalar value 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_bool_array (satcat5::io::Writeable &dst) const |
| Read an array of boolean values from the List. More... | |
| int | get_bool_array (u8 *arr, unsigned arr_len) const |
| Read an array of boolean values from the List. More... | |
| 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 cover any size in the List. More... | |
| 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 cover any size in the List. More... | |
| 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 precision in the List. More... | |
| 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 precision in the List. More... | |
Public Attributes | |
| QCBORDecodeContext *const | cbor |
| QCBOR context pointer. | |
Static Public Attributes | |
| static const int | ERR_NOT_FOUND = -1 |
| Key not found. | |
| static const int | ERR_OVERFLOW = -2 |
| Overflowed user array. | |
| static const int | ERR_BAD_TYPE = -3 |
| Non-matching type. | |
| static const int | ERR_QCBOR_INT = -4 |
| Misc QCBOR error. | |
Protected Member Functions | |
| ListReader (satcat5::io::Readable *src, QCBORDecodeContext *decode, u8 *buff, unsigned size) | |
| Opens a QCBOR Decode Context from an io::Readable. More... | |
| int | get_array_internal (satcat5::io::Writeable &dst, u8 qcbor_type, u8 type_size) const |
| Internal function for shared array logic. More... | |
| unsigned | peek_integer_len (const u8 *rdptr) const |
| Predict the length of the next integer value. More... | |
Protected Attributes | |
| QCBORItem | m_item |
| QCBOR Decoder item. | |
|
inlineexplicitconstexpr |
Create a ListReader from an existing decoder context.
The caller MUST have already entered the array/list in question.
|
protected |
Opens a QCBOR Decode Context from an io::Readable.
This constructor assumes the first element is an array and automatically enters that array.
Opens a QCBOR Decode Context from an io::Readable. Constructor requires child class to provide a working buffer.
| src | Readable source to decode, or NULL if buffers are already populated. |
| decode | Pointer to an uninitialized QCBOR encoder object. |
| buff | Backing buffer for QCBOR. |
| size | Backing buffer size for QCBOR. |
Definition at line 273 of file io_cbor.cc.
|
inlineinherited |
Resume parsing at the end of a nested list.
|
inlineinherited |
Resume parsing at the end of a nested dictionary.
|
inherited |
Copy all remaining CBOR item(s) to the specified destination.
Definition at line 227 of file io_cbor.cc.
|
inherited |
Copy the next CBOR item to the specified destination.
If the next item is a nested data structure, this copies the entire data structure to the destination object.
Definition at line 194 of file io_cbor.cc.
|
protectedinherited |
Internal function for shared array logic.
This function is called by all other "get_array" variants.
Definition at line 520 of file io_cbor.cc.
| int satcat5::cbor::ListReader::get_bool_array | ( | satcat5::io::Writeable & | dst | ) | const |
Read an array of boolean values from the List.
This method always writes true/false values as single bytes (u8 per boolean value), regardless of platform sizeof(bool).
Two interfaces are provided: one takes a pointer to an array, and the other uses io::Writeable. All values are always written in CPU-native endian order.
Example usage to directly populate an array:
| arr | Destination array for read values. |
| arr_len | Length of arr. |
| dst | Writeable destination for array elements. |
dst. If there was not enough space in dst, this returns -1. If any CBOR item has a mismatched type, this returns -2. Definition at line 469 of file io_cbor.cc.
|
inline |
Read an array of boolean values from the List.
This method always writes true/false values as single bytes (u8 per boolean value), regardless of platform sizeof(bool).
Two interfaces are provided: one takes a pointer to an array, and the other uses io::Writeable. All values are always written in CPU-native endian order.
Example usage to directly populate an array:
| arr | Destination array for read values. |
| arr_len | Length of arr. |
| dst | Writeable destination for array elements. |
dst. If there was not enough space in dst, this returns -1. If any CBOR item has a mismatched type, this returns -2. Read a byte sequence from the List.
Definition at line 415 of file io_cbor.cc.
|
inline |
Read an array of floating-point values from the List, always written as doubles to cover any precision in the List.
This method always writes true/false values as single bytes (u8 per boolean value), regardless of platform sizeof(bool).
Two interfaces are provided: one takes a pointer to an array, and the other uses io::Writeable. All values are always written in CPU-native endian order.
Example usage to directly populate an array:
| arr | Destination array for read values. |
| arr_len | Length of arr. |
| dst | Writeable destination for array elements. |
dst. If there was not enough space in dst, this returns -1. If any CBOR item has a mismatched type, this returns -2. | int satcat5::cbor::ListReader::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 precision in the List.
This method always writes true/false values as single bytes (u8 per boolean value), regardless of platform sizeof(bool).
Two interfaces are provided: one takes a pointer to an array, and the other uses io::Writeable. All values are always written in CPU-native endian order.
Example usage to directly populate an array:
| arr | Destination array for read values. |
| arr_len | Length of arr. |
| dst | Writeable destination for array elements. |
dst. If there was not enough space in dst, this returns -1. If any CBOR item has a mismatched type, this returns -2. Definition at line 503 of file io_cbor.cc.
|
inline |
Read an array of integer values from the List, always written as 64-bit length signed values (s64) to cover any size in the List.
This method always writes true/false values as single bytes (u8 per boolean value), regardless of platform sizeof(bool).
Two interfaces are provided: one takes a pointer to an array, and the other uses io::Writeable. All values are always written in CPU-native endian order.
Example usage to directly populate an array:
| arr | Destination array for read values. |
| arr_len | Length of arr. |
| dst | Writeable destination for array elements. |
dst. If there was not enough space in dst, this returns -1. If any CBOR item has a mismatched type, this returns -2. | int satcat5::cbor::ListReader::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 cover any size in the List.
This method always writes true/false values as single bytes (u8 per boolean value), regardless of platform sizeof(bool).
Two interfaces are provided: one takes a pointer to an array, and the other uses io::Writeable. All values are always written in CPU-native endian order.
Example usage to directly populate an array:
| arr | Destination array for read values. |
| arr_len | Length of arr. |
| dst | Writeable destination for array elements. |
dst. If there was not enough space in dst, this returns -1. If any CBOR item has a mismatched type, this returns -2. Definition at line 486 of file io_cbor.cc.
Read a string from the List.
Definition at line 395 of file io_cbor.cc.
| QCBORDecodeContext * satcat5::cbor::ListReader::open_list | ( | ) | const |
Read a nested list from the List.
Allows parsing of keys where the value is a list of items, which may have various type(s). Once finished, the caller MUST then call CborReader::close_list.
Definition at line 435 of file io_cbor.cc.
| QCBORDecodeContext * satcat5::cbor::ListReader::open_map | ( | ) | const |
Read a nested dictionary from the List.
Allows parsing of keys where the value is another self-contained key/value dictionary. Once finished, the caller MUST then call CborReader::close_map.
Definition at line 452 of file io_cbor.cc.
|
protectedinherited |
Predict the length of the next integer value.
Standard integers may be 1, 2, 3, 5, or 9 bytes.
Definition at line 237 of file io_cbor.cc.