SatCat5
codec_cobs.h
Go to the documentation of this file.
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
29 
30 #pragma once
31 
32 #include <satcat5/io_buffer.h>
33 #include <satcat5/pkt_buffer.h>
34 
35 // Default size parameters
36 // (Must be large enough for one full-size Ethernet frame + metadata,
37 // larger sizes are fine if you have the memory for it.)
38 #ifndef SATCAT5_COBS_BUFFSIZE
39 #define SATCAT5_COBS_BUFFSIZE 1600 // One full-size Ethernet frame
40 #endif
41 
42 #ifndef SATCAT5_COBS_PACKETS
43 #define SATCAT5_COBS_PACKETS 32 // ...or many smaller frames
44 #endif
45 
46 namespace satcat5 {
47  namespace io {
50  public:
52  explicit CobsEncoder(satcat5::io::Writeable* dst);
53 
54  // Implement required API from Writeable:
55  unsigned get_write_space() const override;
56  bool write_finalize() override;
57  void write_overflow() override;
58 
59  private:
60  // Implement required API from Writeable:
61  void write_next(u8 data) override;
62 
63  // Flush the lookahead buffer.
64  void flush_segment();
65 
66  // Internal state:
67  satcat5::io::Writeable* const m_dst; // Output object
68  u8 m_ctr; // Run-length counter
69  u8 m_ovr; // Overflow flag
70  u8 m_buf[254]; // Lookahead buffer
71  };
72 
75  public:
77  explicit CobsDecoder(satcat5::io::Writeable* dst);
78 
79  // Implement required API from Writeable:
80  unsigned get_write_space() const override;
81 
82  private:
83  // Implement required API from Writeable:
84  void write_next(u8 data) override;
85  void write_overflow() override;
86 
87  // Internal state:
88  satcat5::io::Writeable* const m_dst; // Output object
89  u8 m_blk; // Block type
90  u8 m_ctr; // Bytes remaining
91  u8 m_ovr; // Overflow flag
92  };
93 
103  class CobsCodec
104  : public satcat5::io::CobsEncoder
106  {
107  public:
110  CobsCodec(
112  satcat5::io::Readable* src);
113 
114  protected:
118  u8 m_rawbuff[SATCAT5_COBS_BUFFSIZE];
119  };
120 
129  : public satcat5::io::CobsDecoder
131  {
132  public:
137  satcat5::io::Readable* src);
138 
139  protected:
143  u8 m_rawbuff[SATCAT5_COBS_BUFFSIZE];
144  };
145  }
146 }
BufferedCopy copies from any Readable source to any Writeable sink.
Definition: io_buffer.h:68
Buffered COBS encoder / decoder pair.
Definition: codec_cobs.h:106
satcat5::io::BufferedCopy m_copy
Push/pull adapter.
Definition: codec_cobs.h:117
CobsCodec(satcat5::io::Writeable *dst, satcat5::io::Readable *src)
Constructor links to specified source and destination.
Definition: codec_cobs.cc:138
u8 m_rawbuff[SATCAT5_COBS_BUFFSIZE]
Working buffer for m_rx.
Definition: codec_cobs.h:118
satcat5::io::CobsDecoder m_decode
Decoder object.
Definition: codec_cobs.h:116
satcat5::io::PacketBuffer m_buff
Decoder writes to buffer.
Definition: codec_cobs.h:115
Buffered COBS encoder / decoder pair with opposite polarity.
Definition: codec_cobs.h:131
satcat5::io::PacketBuffer m_buff
Encoder writes to buffer.
Definition: codec_cobs.h:140
CobsCodecInverse(satcat5::io::Writeable *dst, satcat5::io::Readable *src)
Constructor links to specified source and destination.
Definition: codec_cobs.cc:148
u8 m_rawbuff[SATCAT5_COBS_BUFFSIZE]
Working buffer for m_rx.
Definition: codec_cobs.h:143
satcat5::io::BufferedCopy m_copy
Push/pull adapter.
Definition: codec_cobs.h:142
satcat5::io::CobsEncoder m_encode
Encoder object.
Definition: codec_cobs.h:141
Inline COBS decoder.
Definition: codec_cobs.h:74
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: codec_cobs.cc:98
CobsDecoder(satcat5::io::Writeable *dst)
Permanently link this encoder to an output object.
Definition: codec_cobs.cc:89
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: codec_cobs.cc:103
void write_overflow() override
Optional error handling for write overflow.
Definition: codec_cobs.cc:132
Inline COBS encoder.
Definition: codec_cobs.h:49
void write_overflow() override
Optional error handling for write overflow.
Definition: codec_cobs.cc:64
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: codec_cobs.cc:36
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: codec_cobs.cc:68
bool write_finalize() override
Mark end of frame and release temporary working data.
Definition: codec_cobs.cc:47
CobsEncoder(satcat5::io::Writeable *dst)
Permanently link this encoder to an output object.
Definition: codec_cobs.cc:27
The PacketBuffer class is a wrapper for a circular buffer, with optional logic to support retention o...
Definition: pkt_buffer.h:66
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Wrapper class for forwarding reads to another object.
Definition: io_readable.h:299
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Buffered I/O wrappers for PacketBuffer.