Skip to content

Commit afd73d4

Browse files
committed
2 parents 4152103 + 025cd7e commit afd73d4

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/AudioTools/Sandbox/HDLCStream.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define INVERT_OCTET 0x20
1919

2020
/// The frame check sequence (FCS) is a 16-bit CRC-CCITT
21-
/// AVR Libc CRC function is _crc_ccitt_update()
21+
/// AVR Libc CRC function is crc_ccitt_update()
2222
/// Corresponding CRC function in Qt (www.qt.io) is qChecksum()
2323
#define CRC16_CCITT_INIT_VAL 0xFFFF
2424

@@ -66,6 +66,7 @@ class HDLCStream : public Stream {
6666
*/
6767
HDLCStream(Print &out, uint16_t max_frame_length) {
6868
setOutput(out);
69+
if (getTimeout()==0) setTimeOut(1000); // default timeout of 1 second
6970
this->max_frame_length = max_frame_length;
7071
begin();
7172
}
@@ -78,6 +79,7 @@ class HDLCStream : public Stream {
7879
*/
7980
HDLCStream(Stream &io, uint16_t max_frame_length) {
8081
setStream(io);
82+
if (getTimeout()==0) setTimeOut(1000); // default timeout of 1 second
8183
this->max_frame_length = max_frame_length;
8284
begin();
8385
}
@@ -114,7 +116,7 @@ class HDLCStream : public Stream {
114116
* @return int Available space for writing or 0 if no output is defined
115117
*/
116118
int availableForWrite() override {
117-
return p_out == nullptr ? 0 : DEFAULT_BUFFER_SIZE;
119+
return p_out == nullptr ? 0 : max_frame_length;
118120
}
119121

120122
/**
@@ -136,7 +138,6 @@ class HDLCStream : public Stream {
136138
case HDLCWriteLogic::OnFlush:
137139
for (int j = 0; j < len; j++) {
138140
bool ok = frame_buffer.write(data[j]);
139-
assert(ok);
140141
if (frame_buffer.isFull()) {
141142
LOGE("Buffer full - increase size!");
142143
}
@@ -298,10 +299,10 @@ class HDLCStream : public Stream {
298299
bool escape_character = false;
299300
SingleBuffer<uint8_t> frame_buffer{0};
300301
uint8_t frame_position = 0;
301-
// 16bit CRC sum for _crc_ccitt_update
302+
// 16bit CRC sum for crc_ccitt_update
302303
uint16_t frame_checksum;
303304
uint16_t max_frame_length;
304-
HDLCWriteLogic write_logic = HDLCWriteLogic::OnBufferFull;
305+
HDLCWriteLogic write_logic = HDLCWriteLogic::OnWrite;
305306

306307
/// Function to find valid HDLC frame from incoming data: returns the
307308
/// available result bytes in the buffer
@@ -338,13 +339,14 @@ class HDLCStream : public Stream {
338339
frame_buffer_data[frame_position] = data;
339340

340341
if (frame_position - 2 >= 0) {
341-
frame_checksum = _crc_ccitt_update(frame_checksum,
342+
frame_checksum = crc_ccitt_update(frame_checksum,
342343
frame_buffer_data[frame_position - 2]);
343344
}
344345

345346
frame_position++;
346347

347348
if (frame_position == max_frame_length) {
349+
LOGE("buffer overflow: %d", frame_position);
348350
frame_position = 0;
349351
}
350352
return result;
@@ -360,7 +362,7 @@ class HDLCStream : public Stream {
360362

361363
while (frame_length) {
362364
data = *framebuffer++;
363-
fcs = HDLCStream::_crc_ccitt_update(fcs, data);
365+
fcs = HDLCStream::crc_ccitt_update(fcs, data);
364366
if ((data == CONTROL_ESCAPE_OCTET) || (data == FRAME_BOUNDARY_OCTET)) {
365367
p_out->write((uint8_t)CONTROL_ESCAPE_OCTET);
366368
data ^= INVERT_OCTET;
@@ -393,7 +395,7 @@ class HDLCStream : public Stream {
393395
* @param data Byte to include in CRC calculation
394396
* @return uint16_t Updated CRC value
395397
*/
396-
static uint16_t _crc_ccitt_update(uint16_t crc, uint8_t data) {
398+
static uint16_t crc_ccitt_update(uint16_t crc, uint8_t data) {
397399
data ^= lo8(crc);
398400
data ^= data << 4;
399401
return ((((uint16_t)data << 8) | hi8(crc)) ^ (uint8_t)(data >> 4) ^

0 commit comments

Comments
 (0)