-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReliableSerial.cpp
63 lines (48 loc) · 1.39 KB
/
ReliableSerial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "ReliableSerial.h"
#include "util/encoding.h"
#include "util/checksum.h"
#include <Arduino.h>
#include <functional.h>
void serialSendPacket(uint8_t *packet, uint8_t length) {
uint8_t *next = withCheckBytes(packet, packet, length);
uint8_t nextLength = length + 2;
uint8_t buf[SERIAL_PACKET_MAX_LENGTH];
uint8_t *encPacket = cobsEncode(next, buf, nextLength);
uint8_t encLen = nextLength + 1;
Serial.write(COBS_BOUNDARY);
forEach(encPacket, encLen, Serial.write);
Serial.write(COBS_BOUNDARY);
Serial.flush();
}
// Packets are surrounded by null bytes, have two check bytes at the end, and are COBS-encoded
// Returns true if a valid packet is received, otherwise false
bool serialRecvPacket(uint8_t *packet, uint8_t *length) {
// No data available
if (Serial.available() == 0) return false;
uint8_t buf[SERIAL_PACKET_MAX_LENGTH];
bool skip = true;
uint8_t i = 0;
while (Serial.available() > 0 && i < SERIAL_PACKET_MAX_LENGTH) {
uint8_t b = Serial.read();
if (skip) {
if (b != COBS_BOUNDARY) {
skip = false;
buf[i++] = b;
}
}
if (!skip) {
if (b == COBS_BOUNDARY) break;
buf[i++] = b;
}
}
// COBS decode
uint8_t *decPacket = cobsDecode(buf, packet, i);
uint8_t decLen = i - 1;
// Checksum
if (check(decPacket, decLen) == false) {
return false;
}
packet = withoutCheckBytes(decPacket, packet, decLen);
*length = decLen - 2;
return true;
}