23 static const uint16_t table[16] = {
24 0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387,
25 0x8408, 0x9489, 0xA50A, 0xB58B, 0xC60C, 0xD68D, 0xE70E, 0xF78F};
26 fcs = (fcs >> 4) ^ table[(fcs ^ b) & 0x0F];
27 fcs = (fcs >> 4) ^ table[(fcs ^ (b >> 4)) & 0x0F];
32inline uint16_t
fcs16(
const uint8_t *data,
size_t len,
34 for (
size_t i = 0; i < len; i++) fcs =
fcs16Update(fcs, data[i]);
50 void setACCM(uint32_t accm) { _accm = accm; }
53 void begin(uint16_t protocol,
bool escapeAllControl =
false) {
54 _escapeAll = escapeAllControl;
57 _ok = _out !=
nullptr;
62 data(protocol & 0xFF);
72 void data(
const uint8_t *buf,
size_t len) {
73 for (
size_t i = 0; i < len; i++)
data(buf[i]);
87 Print *_out =
nullptr;
88 uint32_t _accm = 0xFFFFFFFF;
89 bool _escapeAll =
true;
95 void stuffed(uint8_t b) {
97 (b < 0x20 && (_escapeAll || (_accm & (1UL << b))));
106 void put(uint8_t b) {
107 if (_len ==
sizeof(_buf)) flush();
112 if (_len == 0)
return;
113 if (_out ==
nullptr) {
121 for (
int retry = 0; retry < 50 && written < _len; retry++) {
122 written += _out->write(_buf + written, _len - written);
124 if (written < _len) _ok =
false;
136template <
size_t MAX_FRAME>
143 if (_len >= 4 && !_overflow) {
146 _frameLen = _len - 2;
164 if (_len <
sizeof(_buf)) {
180 uint8_t _buf[MAX_FRAME + 2];
182 size_t _frameLen = 0;
183 uint32_t _fcsErrors = 0;
184 bool _escape =
false;
185 bool _overflow =
false;
HDLC frame decoder (RFC 1662).
Definition HDLC.h:137
uint32_t fcsErrors() const
Number of frames dropped because of a wrong FCS.
Definition HDLC.h:177
uint8_t * frame()
Frame content without FCS (address/control included if sent)
Definition HDLC.h:173
bool add(uint8_t b)
Feed one byte: returns true when a valid frame is available.
Definition HDLC.h:140
size_t frameLen() const
Length of the frame returned by frame()
Definition HDLC.h:175
HDLC frame encoder (RFC 1662).
Definition HDLC.h:44
void data(const uint8_t *buf, size_t len)
Adds payload bytes.
Definition HDLC.h:72
void data(uint8_t b)
Adds one payload byte.
Definition HDLC.h:66
void begin(uint16_t protocol, bool escapeAllControl=false)
Starts a frame with address/control header and the protocol number.
Definition HDLC.h:53
void setOutput(Print *out)
Defines the output (e.g. the serial port)
Definition HDLC.h:47
void setACCM(uint32_t accm)
Async-Control-Character-Map of the peer: control chars to escape.
Definition HDLC.h:50
bool end()
Appends FCS and closing flag; returns false if the output dropped bytes.
Definition HDLC.h:77
TinyPPP internals: PPP engine, HDLC framing and the IP stack.
Definition HDLC.h:11
static constexpr uint8_t HDLC_ESCAPE
Definition HDLC.h:14
static constexpr uint16_t FCS16_GOOD
Definition HDLC.h:19
static constexpr uint16_t FCS16_INIT
Definition HDLC.h:18
uint16_t fcs16(const uint8_t *data, size_t len, uint16_t fcs=FCS16_INIT)
FCS-16 over a buffer (continues from fcs)
Definition HDLC.h:32
static constexpr uint8_t HDLC_XOR
Definition HDLC.h:15
uint16_t fcs16Update(uint16_t fcs, uint8_t b)
Incremental FCS-16 (RFC 1662, reflected CRC-CCITT) using a nibble table.
Definition HDLC.h:22
static constexpr uint8_t HDLC_FLAG
Definition HDLC.h:13
static constexpr uint8_t HDLC_ALL_STATIONS
Definition HDLC.h:16
static constexpr uint8_t HDLC_UI
Definition HDLC.h:17