18
18
#define INVERT_OCTET 0x20
19
19
20
20
// / 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 ()
22
22
// / Corresponding CRC function in Qt (www.qt.io) is qChecksum()
23
23
#define CRC16_CCITT_INIT_VAL 0xFFFF
24
24
@@ -66,6 +66,7 @@ class HDLCStream : public Stream {
66
66
*/
67
67
HDLCStream (Print &out, uint16_t max_frame_length) {
68
68
setOutput (out);
69
+ if (getTimeout ()==0 ) setTimeOut (1000 ); // default timeout of 1 second
69
70
this ->max_frame_length = max_frame_length;
70
71
begin ();
71
72
}
@@ -78,6 +79,7 @@ class HDLCStream : public Stream {
78
79
*/
79
80
HDLCStream (Stream &io, uint16_t max_frame_length) {
80
81
setStream (io);
82
+ if (getTimeout ()==0 ) setTimeOut (1000 ); // default timeout of 1 second
81
83
this ->max_frame_length = max_frame_length;
82
84
begin ();
83
85
}
@@ -114,7 +116,7 @@ class HDLCStream : public Stream {
114
116
* @return int Available space for writing or 0 if no output is defined
115
117
*/
116
118
int availableForWrite () override {
117
- return p_out == nullptr ? 0 : DEFAULT_BUFFER_SIZE ;
119
+ return p_out == nullptr ? 0 : max_frame_length ;
118
120
}
119
121
120
122
/* *
@@ -136,7 +138,6 @@ class HDLCStream : public Stream {
136
138
case HDLCWriteLogic::OnFlush:
137
139
for (int j = 0 ; j < len; j++) {
138
140
bool ok = frame_buffer.write (data[j]);
139
- assert (ok);
140
141
if (frame_buffer.isFull ()) {
141
142
LOGE (" Buffer full - increase size!" );
142
143
}
@@ -298,10 +299,10 @@ class HDLCStream : public Stream {
298
299
bool escape_character = false ;
299
300
SingleBuffer<uint8_t > frame_buffer{0 };
300
301
uint8_t frame_position = 0 ;
301
- // 16bit CRC sum for _crc_ccitt_update
302
+ // 16bit CRC sum for crc_ccitt_update
302
303
uint16_t frame_checksum;
303
304
uint16_t max_frame_length;
304
- HDLCWriteLogic write_logic = HDLCWriteLogic::OnBufferFull ;
305
+ HDLCWriteLogic write_logic = HDLCWriteLogic::OnWrite ;
305
306
306
307
// / Function to find valid HDLC frame from incoming data: returns the
307
308
// / available result bytes in the buffer
@@ -338,13 +339,14 @@ class HDLCStream : public Stream {
338
339
frame_buffer_data[frame_position] = data;
339
340
340
341
if (frame_position - 2 >= 0 ) {
341
- frame_checksum = _crc_ccitt_update (frame_checksum,
342
+ frame_checksum = crc_ccitt_update (frame_checksum,
342
343
frame_buffer_data[frame_position - 2 ]);
343
344
}
344
345
345
346
frame_position++;
346
347
347
348
if (frame_position == max_frame_length) {
349
+ LOGE (" buffer overflow: %d" , frame_position);
348
350
frame_position = 0 ;
349
351
}
350
352
return result;
@@ -360,7 +362,7 @@ class HDLCStream : public Stream {
360
362
361
363
while (frame_length) {
362
364
data = *framebuffer++;
363
- fcs = HDLCStream::_crc_ccitt_update (fcs, data);
365
+ fcs = HDLCStream::crc_ccitt_update (fcs, data);
364
366
if ((data == CONTROL_ESCAPE_OCTET) || (data == FRAME_BOUNDARY_OCTET)) {
365
367
p_out->write ((uint8_t )CONTROL_ESCAPE_OCTET);
366
368
data ^= INVERT_OCTET;
@@ -393,7 +395,7 @@ class HDLCStream : public Stream {
393
395
* @param data Byte to include in CRC calculation
394
396
* @return uint16_t Updated CRC value
395
397
*/
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) {
397
399
data ^= lo8 (crc);
398
400
data ^= data << 4 ;
399
401
return ((((uint16_t )data << 8 ) | hi8 (crc)) ^ (uint8_t )(data >> 4 ) ^
0 commit comments