SatCat5
codec_hdlc.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.
35 
36 #pragma once
37 
38 #include <satcat5/crc16_checksum.h>
39 #include <satcat5/eth_checksum.h>
40 #include <satcat5/io_checksum.h>
41 
42 namespace satcat5 {
43  namespace io {
46  public:
48  explicit HdlcEncoder(satcat5::io::Writeable* dst);
49 
51  inline void set_mode_actrl(bool actrl)
52  { m_bstuff.m_actrl = actrl; }
53 
55  void set_mode_crc32(bool mode32);
56 
57  private:
58  // Helper class for byte-stuffing.
60  public:
61  explicit ByteStuff(satcat5::io::Writeable* dst);
62  unsigned get_write_space() const override;
63  void write_abort() override;
64  bool write_finalize() override;
65  void write_next(u8 data) override;
66  satcat5::io::Writeable* const m_dst; // Output object
67  bool m_actrl; // Escape < 0x20?
68  } m_bstuff;
69 
70  // Append checksum using the selected algorithm.
73  };
74 
77  public:
79  explicit HdlcDecoder(satcat5::io::Writeable* dst);
80 
81  // Implement required API from Writeable:
82  unsigned get_write_space() const override;
83 
85  inline void set_mode_actrl(bool actrl)
86  { m_actrl = actrl; }
87 
89  void set_mode_crc32(bool mode32);
90 
91  private:
92  // Implement required API from Writeable:
93  void write_next(u8 data) override;
94  void write_overflow() override;
95 
96  // Verify checksum using the selected algorithm.
99 
100  // Internal state:
101  enum class State {HDLC_RDY, HDLC_ESC, HDLC_EOF, HDLC_ERR};
102  satcat5::io::HdlcDecoder::State m_state; // Decoder state
103  bool m_actrl; // Escape < 0x20?
104  satcat5::io::Writeable* m_crc; // CRC16 or CRC32?
105  };
106  }
107 }
Check and remove FCS from each incoming frame ("KERMIT" variant).
Append FCS to each outgoing frame ("KERMIT" variant).
Check and remove FCS from each incoming frame.
Definition: eth_checksum.h:52
Append FCS to each outgoing frame.
Definition: eth_checksum.h:38
Inline HDLC decoder (framing layer only).
Definition: codec_hdlc.h:76
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: codec_hdlc.cc:119
void set_mode_crc32(bool mode32)
Select CRC16 or CRC32 checksum?
Definition: codec_hdlc.cc:111
void set_mode_actrl(bool actrl)
Enable ACTRL mode?
Definition: codec_hdlc.h:85
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: codec_hdlc.cc:105
void write_overflow() override
Optional error handling for write overflow.
Definition: codec_hdlc.cc:146
HdlcDecoder(satcat5::io::Writeable *dst)
Permanently link this encoder to an output object.
Definition: codec_hdlc.cc:95
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: codec_hdlc.cc:54
void write_abort() override
If possible, abort the current partially-written packet.
Definition: codec_hdlc.cc:62
bool write_finalize() override
Mark end of frame and release temporary working data.
Definition: codec_hdlc.cc:70
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: codec_hdlc.cc:79
Inline HDLC encoder (framing layer only).
Definition: codec_hdlc.h:45
HdlcEncoder(satcat5::io::Writeable *dst)
Permanently link this encoder to an output object.
Definition: codec_hdlc.cc:29
void set_mode_crc32(bool mode32)
Select CRC16 or CRC32 checksum?
Definition: codec_hdlc.cc:38
void set_mode_actrl(bool actrl)
Enable ACTRL mode?
Definition: codec_hdlc.h:51
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Wrapper class for forwarding writes to another object.
Definition: io_writeable.h:218
Inline CRC16 Checksum insertion and verification.
Inline Ethernet Checksum insertion and verification.
Inline checksum insertion (ChecksumTx) and verification (ChecksumRx).