SatCat5
io_cbor.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
38 
39 #pragma once
40 
41 #include <satcat5/io_readable.h>
42 #include <satcat5/io_writeable.h>
43 #include <satcat5/utils.h>
44 
45 // Enable this feature? (See types.h)
46 #if SATCAT5_CBOR_ENABLE
47 #include <qcbor/qcbor_encode.h>
48 #include <qcbor/qcbor_spiffy_decode.h>
49 
51 #ifndef SATCAT5_QCBOR_BUFFER
52 #define SATCAT5_QCBOR_BUFFER 1500
53 #endif
54 
55 namespace satcat5 {
56  namespace cbor {
91  class CborWriter {
92  public:
95  bool close();
96 
98  inline bool close_and_finalize()
99  { return close() && m_dst && m_dst->write_finalize(); }
100 
103  UsefulBufC get_encoded();
104 
108 
113  inline void close_list()
114  { QCBOREncode_CloseArray(cbor); }
115 
120  inline void close_map()
121  { QCBOREncode_CloseMap(cbor); }
122 
123  // Member variables
124  QCBOREncodeContext* const cbor;
125 
126  protected:
140  CborWriter(
142  QCBOREncodeContext* encode,
143  u8* buff, unsigned size,
144  bool automap=false);
145 
149  explicit constexpr CborWriter(QCBOREncodeContext* encode)
150  : cbor(encode)
151  , m_dst(nullptr)
152  , m_encoded(nullptr)
153  , m_encoded_len(0)
154  , m_auto_close(QCBOR_TYPE_NONE)
155  , m_read(encode->OutBuf.UB.ptr, 0)
156  {} // Nothing else to initialize.
157 
158  // Prohibit unsafe copy constructor and assignment operator.
159  CborWriter(const CborWriter& other) = delete;
160  CborWriter& operator=(const CborWriter& other) = delete;
161 
163  template <typename T>
164  void add_unsigned_array(unsigned len, const T* value) const;
165 
167  template <typename T>
168  void add_signed_array(unsigned len, const T* value) const;
169 
170  // Member variables
172  const u8* m_encoded;
173  unsigned m_encoded_len;
176  };
177 
181  class ListWriter : public CborWriter {
182  public:
184  explicit constexpr ListWriter(QCBOREncodeContext* encode)
185  : CborWriter(encode) {}
186 
189  inline void add_bool(bool value) const
190  { QCBOREncode_AddBool(cbor, value); }
191  inline void add_item(s8 value) const
192  { QCBOREncode_AddInt64(cbor, value); }
193  inline void add_item(s16 value) const
194  { QCBOREncode_AddInt64(cbor, value); }
195  inline void add_item(s32 value) const
196  { QCBOREncode_AddInt64(cbor, value); }
197  inline void add_item(s64 value) const
198  { QCBOREncode_AddInt64(cbor, value); }
199  inline void add_item(u8 value) const
200  { QCBOREncode_AddUInt64(cbor, value); }
201  inline void add_item(u16 value) const
202  { QCBOREncode_AddUInt64(cbor, value); }
203  inline void add_item(u32 value) const
204  { QCBOREncode_AddUInt64(cbor, value); }
205  inline void add_item(u64 value) const
206  { QCBOREncode_AddUInt64(cbor, value); }
207  inline void add_item(float value) const
208  { QCBOREncode_AddFloat(cbor, value); }
209  inline void add_item(double value) const
210  { QCBOREncode_AddDouble(cbor, value); }
212 
215  void add_array(u32 len, const bool* value) const;
216  inline void add_array(u32 len, const s8* value) const
217  { add_signed_array<s8>(len, value); }
218  inline void add_array(u32 len, const s16* value) const
219  { add_signed_array<s16>(len, value); }
220  inline void add_array(u32 len, const s32* value) const
221  { add_signed_array<s32>(len, value); }
222  inline void add_array(u32 len, const s64* value) const
223  { add_signed_array<s64>(len, value); }
224  inline void add_array(u32 len, const u8* value) const
225  { add_unsigned_array<u8>(len, value); }
226  inline void add_array(u32 len, const u16* value) const
227  { add_unsigned_array<u16>(len, value); }
228  inline void add_array(u32 len, const u32* value) const
229  { add_unsigned_array<u32>(len, value); }
230  inline void add_array(u32 len, const u64* value) const
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;
235 
238  inline void add_bytes(u32 len, const u8* value) const
239  { QCBOREncode_AddBytes(cbor, {value, len}); }
240  inline void add_bytes(const u8* value, u32 len) const
241  { QCBOREncode_AddBytes(cbor, {value, len}); }
243 
248  QCBOREncodeContext* open_list();
249 
253  QCBOREncodeContext* open_map();
254 
256  inline void add_string(const char* value) const
257  { QCBOREncode_AddSZString(cbor, value); }
258 
260  inline void add_null() const
261  { QCBOREncode_AddNULL(cbor); }
262 
263  protected:
275  QCBOREncodeContext* encode,
276  u8* buff, unsigned size)
277  : CborWriter(dst, encode, buff, size, false)
278  {
279  // Open the top-level list and trigger array auto-close.
280  QCBOREncode_OpenArray(cbor);
281  m_auto_close = QCBOR_TYPE_ARRAY;
282  }
283  };
284 
301  template <typename KEYTYPE = const char*>
302  class MapWriter : virtual public CborWriter {
303  public:
305  explicit constexpr MapWriter(QCBOREncodeContext* encode)
306  : CborWriter(encode) {}
307 
310  inline void add_bool(KEYTYPE key, bool value) const
311  { add_key(key); QCBOREncode_AddBool(cbor, value); }
312  inline void add_item(KEYTYPE key, s8 value) const
313  { add_key(key); QCBOREncode_AddInt64(cbor, value); }
314  inline void add_item(KEYTYPE key, s16 value) const
315  { add_key(key); QCBOREncode_AddInt64(cbor, value); }
316  inline void add_item(KEYTYPE key, s32 value) const
317  { add_key(key); QCBOREncode_AddInt64(cbor, value); }
318  inline void add_item(KEYTYPE key, s64 value) const
319  { add_key(key); QCBOREncode_AddInt64(cbor, value); }
320  inline void add_item(KEYTYPE key, u8 value) const
321  { add_key(key); QCBOREncode_AddUInt64(cbor, value); }
322  inline void add_item(KEYTYPE key, u16 value) const
323  { add_key(key); QCBOREncode_AddUInt64(cbor, value); }
324  inline void add_item(KEYTYPE key, u32 value) const
325  { add_key(key); QCBOREncode_AddUInt64(cbor, value); }
326  inline void add_item(KEYTYPE key, u64 value) const
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); }
333 
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;
356 
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}); }
364 
369  QCBOREncodeContext* open_list(KEYTYPE key);
370 
375  QCBOREncodeContext* open_map(KEYTYPE key);
376 
378  inline void add_string(KEYTYPE key, const char* value) const
379  { add_key(key); QCBOREncode_AddSZString(cbor, value); }
380 
382  inline void add_null(KEYTYPE key) const
383  { add_key(key); QCBOREncode_AddNULL(cbor); }
384 
385  protected:
392  MapWriter() : CborWriter(0, 0, 0, 0, 0) {}
393 
395  void add_key(KEYTYPE key) const;
396  };
397 
402  template <unsigned SIZE = SATCAT5_QCBOR_BUFFER>
403  class ListWriterStatic : public ListWriter {
404  public:
408  explicit ListWriterStatic(satcat5::io::Writeable* dst = nullptr)
409  : ListWriter(dst, &m_cbor, m_raw, SIZE) {}
410 
411  private:
412  QCBOREncodeContext m_cbor;
413  u8 m_raw[SIZE];
414  };
415 
420  template <
421  typename KEYTYPE = const char*,
422  unsigned SIZE = SATCAT5_QCBOR_BUFFER>
423  class MapWriterStatic : public MapWriter<KEYTYPE> {
424  public:
428  explicit MapWriterStatic(satcat5::io::Writeable* dst = nullptr)
429  : CborWriter(dst, &m_cbor, m_raw, SIZE, true) {}
430 
431  private:
432  QCBOREncodeContext m_cbor;
433  u8 m_raw[SIZE];
434  };
435 
465  class CborReader {
466  public:
468  inline bool ok() const
469  { return (get_error() == QCBOR_SUCCESS); }
470 
472  inline QCBORError get_error() const
473  { return QCBORDecode_GetError(cbor); }
474 
477  inline void close_list() const
478  { QCBORDecode_ExitArray(cbor); }
479 
482  inline void close_map() const
483  { QCBORDecode_ExitMap(cbor); }
484 
489  bool copy_item(QCBOREncodeContext* dst);
490 
493  unsigned copy_all(QCBOREncodeContext* dst);
494 
495  // Error return codes for array reading, values in io_cbor.cc.
496  static const int ERR_NOT_FOUND;
497  static const int ERR_OVERFLOW;
498  static const int ERR_BAD_TYPE;
499  static const int ERR_QCBOR_INT;
500 
501  // TODO: Below fails to link?
502  // Get a string representing the QCBOR decoding error, if any.
503  // inline const char* get_error_str() const
504  // { return qcbor_err_to_str(QCBORDecode_GetError(cbor)); }
505 
506  // Member variables
507  QCBORDecodeContext* const cbor;
508 
509  protected:
518  CborReader(
520  QCBORDecodeContext* decode,
521  u8* buff, unsigned size);
522 
524  explicit constexpr CborReader(QCBORDecodeContext* decode)
525  : cbor(decode), m_item{} {}
526 
527  // Prohibit unsafe copy constructor and assignment operator.
528  CborReader(const CborReader& other) = delete;
529  CborReader& operator=(const CborReader& other) = delete;
530 
533  int get_array_internal(
534  satcat5::io::Writeable& dst, u8 qcbor_type, u8 type_size) const;
535 
539  unsigned peek_integer_len(const u8* rdptr) const;
540 
541  // Member variables
542  QCBORItem m_item;
543  };
544 
550  class ListReader : public CborReader {
551  public:
557  explicit constexpr ListReader(QCBORDecodeContext* decode)
558  : CborReader(decode) {}
559 
568 
573 
578 
585  QCBORDecodeContext* open_list() const;
586 
593  QCBORDecodeContext* open_map() const;
594 
620  int get_bool_array(satcat5::io::Writeable& dst) const;
621  int get_bool_array(u8* arr, unsigned arr_len) const {
622  satcat5::io::ArrayWrite dst(arr, arr_len * sizeof(u8));
623  return get_bool_array(dst);
624  }
626 
631  int get_s64_array(satcat5::io::Writeable& dst) const;
632  int get_s64_array(s64* arr, unsigned arr_len) const {
633  satcat5::io::ArrayWrite dst(arr, arr_len * sizeof(s64));
634  return get_s64_array(dst);
635  }
637 
643  int get_double_array(double* arr, unsigned arr_len) const {
644  satcat5::io::ArrayWrite dst(arr, arr_len * sizeof(double));
645  return get_double_array(dst);
646  }
648 
649  protected:
654  ListReader(
656  QCBORDecodeContext* decode,
657  u8* buff, unsigned size);
658  };
659 
677  template <typename KEYTYPE = const char*>
678  class MapReader : public CborReader {
679  public:
685  explicit constexpr MapReader(QCBORDecodeContext* decode)
686  : CborReader(decode) {}
687 
695 
697  bool is_null(KEYTYPE key) const;
698 
703 
708 
715  QCBORDecodeContext* open_list(KEYTYPE key) const;
716 
723  QCBORDecodeContext* open_map(KEYTYPE key) const;
724 
750  int get_bool_array(KEYTYPE key, satcat5::io::Writeable& dst) const;
751  int get_bool_array(KEYTYPE key, u8* arr, unsigned arr_len) const {
752  satcat5::io::ArrayWrite dst(arr, arr_len * sizeof(u8));
753  return get_bool_array(key, dst);
754  }
756 
761  int get_s64_array(KEYTYPE key, satcat5::io::Writeable& dst) const;
762  int get_s64_array(KEYTYPE key, s64* arr, unsigned arr_len) const {
763  satcat5::io::ArrayWrite dst(arr, arr_len * sizeof(s64));
764  return get_s64_array(key, dst);
765  }
767 
772  int get_double_array(KEYTYPE key, satcat5::io::Writeable& dst) const;
773  int get_double_array(KEYTYPE key, double* arr, unsigned arr_len) const {
774  satcat5::io::ArrayWrite dst(arr, arr_len * sizeof(double));
775  return get_double_array(key, dst);
776  }
778 
779  protected:
784  MapReader(
786  QCBORDecodeContext* decode,
787  u8* buff, unsigned size);
788  };
789 
794  template <unsigned SIZE = SATCAT5_QCBOR_BUFFER>
795  class ListReaderStatic : public ListReader {
796  public:
800  : ListReader(src, &m_cbor, m_raw, SIZE) {}
801 
802  private:
803  QCBORDecodeContext m_cbor;
804  u8 m_raw[SIZE];
805  };
806 
811  template <
812  typename KEYTYPE = const char*,
813  unsigned SIZE = SATCAT5_QCBOR_BUFFER>
814  class MapReaderStatic : public MapReader<KEYTYPE> {
815  public:
819  : MapReader<KEYTYPE>(src, &m_cbor, m_raw, SIZE) {}
820 
821  private:
822  QCBORDecodeContext m_cbor;
823  u8 m_raw[SIZE];
824  };
825 
828  struct Logger {
829  const QCBORItem& m_item;
830 
831  constexpr explicit Logger(const QCBORItem& item)
832  : m_item(item) {}
833  Logger(const Logger& t) = default;
834  Logger& operator=(const Logger& t) = default;
835  void log_to(satcat5::log::LogBuffer& wr) const;
836  };
837  }
838 
839  namespace io {
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*>
854  template <
855  typename KEYTYPE = const char*,
856  unsigned SIZE = SATCAT5_QCBOR_BUFFER>
858  template <
859  typename KEYTYPE = const char*,
860  unsigned SIZE = SATCAT5_QCBOR_BUFFER>
863  }
864 }
865 
866 #endif // SATCAT5_CBOR_ENABLE
Creates an ephemeral reader for the Concise Binary Object Representation (CBOR, IETF RFC8949),...
Definition: io_cbor.h:465
static const int ERR_OVERFLOW
Overflowed user array.
Definition: io_cbor.h:497
bool ok() const
Check if any errors have been encountered yet during decoding.
Definition: io_cbor.h:468
unsigned peek_integer_len(const u8 *rdptr) const
Predict the length of the next integer value.
Definition: io_cbor.cc:237
QCBORError get_error() const
Get the QCBOR decoding error, if any.
Definition: io_cbor.h:472
bool copy_item(QCBOREncodeContext *dst)
Copy the next CBOR item to the specified destination.
Definition: io_cbor.cc:194
void close_list() const
Resume parsing at the end of a nested list.
Definition: io_cbor.h:477
QCBORDecodeContext *const cbor
QCBOR context pointer.
Definition: io_cbor.h:507
static const int ERR_NOT_FOUND
Key not found.
Definition: io_cbor.h:496
void close_map() const
Resume parsing at the end of a nested dictionary.
Definition: io_cbor.h:482
unsigned copy_all(QCBOREncodeContext *dst)
Copy all remaining CBOR item(s) to the specified destination.
Definition: io_cbor.cc:227
CborReader(satcat5::io::Readable *src, QCBORDecodeContext *decode, u8 *buff, unsigned size)
Opens a QCBOR Decode Context from an io::Readable.
Definition: io_cbor.cc:179
constexpr CborReader(QCBORDecodeContext *decode)
Create a CborReader from an existing decoder context.
Definition: io_cbor.h:524
static const int ERR_QCBOR_INT
Misc QCBOR error.
Definition: io_cbor.h:499
static const int ERR_BAD_TYPE
Non-matching type.
Definition: io_cbor.h:498
int get_array_internal(satcat5::io::Writeable &dst, u8 qcbor_type, u8 type_size) const
Internal function for shared array logic.
Definition: io_cbor.cc:520
QCBORItem m_item
QCBOR Decoder item.
Definition: io_cbor.h:542
Creates an ephemeral writer for the Concise Binary Object Representation (CBOR, IETF RFC8949),...
Definition: io_cbor.h:91
unsigned m_encoded_len
Encoded length.
Definition: io_cbor.h:173
constexpr CborWriter(QCBOREncodeContext *encode)
Create a CborWriter from an existing encoder context.
Definition: io_cbor.h:149
bool close_and_finalize()
Calls close() then m_dst.write_finalize().
Definition: io_cbor.h:98
void close_map()
Finish writing a nested dictionary to the Map.
Definition: io_cbor.h:120
bool close()
Close the QCBOR encoder by calling QCBOREncode_Finish and copy the resulting bytes to m_dst,...
Definition: io_cbor.cc:34
CborWriter(satcat5::io::Writeable *dst, QCBOREncodeContext *encode, u8 *buff, unsigned size, bool automap=false)
Constructor requires child class to provide a working buffer.
Definition: io_cbor.cc:19
void add_unsigned_array(unsigned len, const T *value) const
Templated function to add an array of unsigned values.
Definition: io_cbor.cc:92
satcat5::io::ArrayRead m_read
Reads working buffer.
Definition: io_cbor.h:175
const u8 * m_encoded
Encoded data.
Definition: io_cbor.h:172
UsefulBufC get_encoded()
Get encoded data as a UsefulBuf, useful if no dst was given.
Definition: io_cbor.cc:51
void add_signed_array(unsigned len, const T *value) const
Templated function to add an array of signed values.
Definition: io_cbor.cc:101
satcat5::io::Readable * get_buffer()
Get encoded data as a Readable, useful if no dst was given.
Definition: io_cbor.cc:56
u8 m_auto_close
Auto-close QCBOR Type.
Definition: io_cbor.h:174
void close_list()
Finish writing a list to the List.
Definition: io_cbor.h:113
QCBOREncodeContext *const cbor
QCBOR context pointer.
Definition: io_cbor.h:124
satcat5::io::Writeable *const m_dst
Writeable sink.
Definition: io_cbor.h:171
Read consecutive values from a CBOR Array/List.
Definition: io_cbor.h:550
satcat5::util::optional< QCBORItem > get_item() const
Read the next scalar value from the List.
Definition: io_cbor.cc:292
satcat5::util::optional< bool > get_bool() const
Read the next scalar value from the List.
Definition: io_cbor.cc:299
int get_bool_array(u8 *arr, unsigned arr_len) const
Read an array of boolean values from the List.
Definition: io_cbor.h:621
int get_bool_array(satcat5::io::Writeable &dst) const
Read an array of boolean values from the List.
Definition: io_cbor.cc:469
satcat5::util::optional< satcat5::io::ArrayRead > get_string() const
Read a string from the List.
Definition: io_cbor.cc:395
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...
Definition: io_cbor.h:643
QCBORDecodeContext * open_list() const
Read a nested list from the List.
Definition: io_cbor.cc:435
constexpr ListReader(QCBORDecodeContext *decode)
Create a ListReader from an existing decoder context.
Definition: io_cbor.h:557
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...
Definition: io_cbor.cc:486
satcat5::util::optional< satcat5::io::ArrayRead > get_bytes() const
Read a byte sequence from the List.
Definition: io_cbor.cc:415
satcat5::util::optional< u64 > get_uint() const
Read the next scalar value from the List.
Definition: io_cbor.cc:340
satcat5::util::optional< double > get_double() const
Read the next scalar value from the List.
Definition: io_cbor.cc:361
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...
Definition: io_cbor.cc:503
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...
Definition: io_cbor.h:632
QCBORDecodeContext * open_map() const
Read a nested dictionary from the List.
Definition: io_cbor.cc:452
satcat5::util::optional< s64 > get_int() const
Read the next scalar value from the List.
Definition: io_cbor.cc:320
ListReader variant with a statically-allocated buffer.
Definition: io_cbor.h:795
ListReaderStatic(satcat5::io::Readable *src)
Create this object and copy from a source io::Readable.
Definition: io_cbor.h:799
Write a series of consecutive items to a CBOR message.
Definition: io_cbor.h:181
void add_array(u32 len, const s8 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:216
void add_item(u16 value) const
Write a scalar value to the List.
Definition: io_cbor.h:201
void add_array(u32 len, const bool *value) const
Write an array of scalar values to the List.
Definition: io_cbor.cc:74
void add_array(u32 len, const u8 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:224
void add_item(s16 value) const
Write a scalar value to the List.
Definition: io_cbor.h:193
void add_bytes(const u8 *value, u32 len) const
Write a byte sequence to the List.
Definition: io_cbor.h:240
void add_item(float value) const
Write a scalar value to the List.
Definition: io_cbor.h:207
void add_array(u32 len, const s32 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:220
void add_array(u32 len, const u64 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:230
void add_string(const char *value) const
Write a null-terminated string to the Map.
Definition: io_cbor.h:256
QCBOREncodeContext * open_map()
Add a nested dictionary to the List.
Definition: io_cbor.cc:161
void add_array(u32 len, const s64 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:222
void add_item(u64 value) const
Write a scalar value to the List.
Definition: io_cbor.h:205
void add_item(s8 value) const
Write a scalar value to the List.
Definition: io_cbor.h:191
void add_item(double value) const
Write a scalar value to the List.
Definition: io_cbor.h:209
QCBOREncodeContext * open_list()
Add a nested list to the List.
Definition: io_cbor.cc:143
void add_array(u32 len, const u16 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:226
void add_item(u32 value) const
Write a scalar value to the List.
Definition: io_cbor.h:203
void add_bytes(u32 len, const u8 *value) const
Write a byte sequence to the List.
Definition: io_cbor.h:238
void add_bool(bool value) const
Write a scalar value to the List.
Definition: io_cbor.h:189
constexpr ListWriter(QCBOREncodeContext *encode)
Create a ListWriter from an existing encoder context.
Definition: io_cbor.h:184
void add_array(u32 len, const s16 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:218
void add_item(s32 value) const
Write a scalar value to the List.
Definition: io_cbor.h:195
void add_null() const
Write a key with a null value to the Map.
Definition: io_cbor.h:260
ListWriter(satcat5::io::Writeable *dst, QCBOREncodeContext *encode, u8 *buff, unsigned size)
Constructor requires child class to provide a working buffer.
Definition: io_cbor.h:273
void add_array(u32 len, const u32 *value) const
Write an array of scalar values to the List.
Definition: io_cbor.h:228
void add_item(s64 value) const
Write a scalar value to the List.
Definition: io_cbor.h:197
void add_item(u8 value) const
Write a scalar value to the List.
Definition: io_cbor.h:199
ListWriter variant with a statically-allocated buffer.
Definition: io_cbor.h:403
ListWriterStatic(satcat5::io::Writeable *dst=nullptr)
Create this object and set the destination.
Definition: io_cbor.h:408
Reads a series of key-value pairs from a CBOR Map.
Definition: io_cbor.h:678
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...
Definition: io_cbor.h:751
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 ...
Definition: io_cbor.h:762
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...
Definition: io_cbor.h:773
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.
Definition: io_cbor.h:685
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.
Definition: io_cbor.h:814
MapReaderStatic(satcat5::io::Readable *src)
Create this object and copy from a source io::Readable.
Definition: io_cbor.h:818
Write a series of key-value pairs to a CBOR Map.
Definition: io_cbor.h:302
void add_bool(KEYTYPE key, bool value) const
Write a scalar value to the Map.
Definition: io_cbor.h:310
void add_item(KEYTYPE key, u64 value) const
Write a scalar value to the Map.
Definition: io_cbor.h:326
void add_item(KEYTYPE key, u8 value) const
Write a scalar value to the Map.
Definition: io_cbor.h:320
void add_null(KEYTYPE key) const
Write a key with a null value to the Map.
Definition: io_cbor.h:382
void add_string(KEYTYPE key, const char *value) const
Write a null-terminated string to the Map.
Definition: io_cbor.h:378
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.
Definition: io_cbor.h:312
void add_bytes(KEYTYPE key, u32 len, const u8 *value) const
Write a byte sequence to the Map.
Definition: io_cbor.h:359
void add_array(KEYTYPE key, u32 len, const s16 *value) const
Write an array of scalar values to the Map.
Definition: io_cbor.h:339
void add_item(KEYTYPE key, s16 value) const
Write a scalar value to the Map.
Definition: io_cbor.h:314
void add_array(KEYTYPE key, u32 len, const u32 *value) const
Write an array of scalar values to the Map.
Definition: io_cbor.h:349
void add_array(KEYTYPE key, u32 len, const s8 *value) const
Write an array of scalar values to the Map.
Definition: io_cbor.h:337
void add_item(KEYTYPE key, s32 value) const
Write a scalar value to the Map.
Definition: io_cbor.h:316
void add_item(KEYTYPE key, s64 value) const
Write a scalar value to the Map.
Definition: io_cbor.h:318
void add_array(KEYTYPE key, u32 len, const bool *value) const
Write an array of scalar values to the Map.
Definition: io_cbor.cc:82
void add_array(KEYTYPE key, u32 len, const u16 *value) const
Write an array of scalar values to the Map.
Definition: io_cbor.h:347
void add_array(KEYTYPE key, u32 len, const s32 *value) const
Write an array of scalar values to the Map.
Definition: io_cbor.h:341
void add_item(KEYTYPE key, double value) const
Write a scalar value to the Map.
Definition: io_cbor.h:330
constexpr MapWriter(QCBOREncodeContext *encode)
Create a MapWriter from an existing encoder context.
Definition: io_cbor.h:305
void add_item(KEYTYPE key, float value) const
Write a scalar value to the Map.
Definition: io_cbor.h:328
void add_item(KEYTYPE key, u16 value) const
Write a scalar value to the Map.
Definition: io_cbor.h:322
MapWriter()
Placeholder constructor with no side-effects.
Definition: io_cbor.h:392
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.
Definition: io_cbor.h:345
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.
Definition: io_cbor.h:351
void add_bytes(KEYTYPE key, const u8 *value, u32 len) const
Write a byte sequence to the Map.
Definition: io_cbor.h:361
void add_array(KEYTYPE key, u32 len, const s64 *value) const
Write an array of scalar values to the Map.
Definition: io_cbor.h:343
void add_item(KEYTYPE key, u32 value) const
Write a scalar value to the Map.
Definition: io_cbor.h:324
MapWriter variant with a statically-allocated buffer.
Definition: io_cbor.h:423
MapWriterStatic(satcat5::io::Writeable *dst=nullptr)
Create this object and set the destination.
Definition: io_cbor.h:428
Ephemeral Readable interface for a simple array.
Definition: io_readable.h:206
Ephemeral Writeable interface for a simple array.
Definition: io_writeable.h:127
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
virtual bool write_finalize()
Mark end of frame and release temporary working data.
Internal buffer used by the Log class.
Definition: log.h:134
#define SATCAT5_QCBOR_BUFFER
Set the default size for the QCBOR buffer.
Definition: io_cbor.h:52
"Readable" I/O interface core definitions
"Writeable" I/O interface core definitions
Helper object for logging contents a QCBOR item.
Definition: io_cbor.h:828
An optional field that may be filled or empty.
Definition: utils.h:53
Miscellaneous mathematical utility functions.