-
Notifications
You must be signed in to change notification settings - Fork 24
/
SLIPEncodedSerial.cpp
executable file
·135 lines (116 loc) · 3.54 KB
/
SLIPEncodedSerial.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "SLIPEncodedSerial.h"
/*
CONSTRUCTOR
*/
SLIPEncodedSerial::SLIPEncodedSerial()
{
rstate = WAITING;
rxBufHead = 0;
rxBufTail = 0;
rxPacketIndex = 0;
encodedBufIndex = 0;
decodedBufIndex = 0;
}
static const uint8_t eot = 0300;
static const uint8_t slipesc = 0333;
static const uint8_t slipescend = 0334;
static const uint8_t slipescesc = 0335;
int SLIPEncodedSerial::sendMessage(const uint8_t *buf, uint32_t len, Serial &s)
{
encode(buf, len);
return s.writeBuffer(encodedBuf, encodedLength);
}
//int SLIPEncodedSerial::recvPacket(uint8_t * buf, uint32_t len)
int SLIPEncodedSerial::recvMessage(Serial &s)
{
int i, len;
// fill up recv buffer from serial port
len = s.readBuffer(serialIn, SERIAL_READ_SIZE);
if (len == -1) {
// printf("Error reading from serial port\n");
}
else if (len == 0) {
//printf("No more data\n");
}
else {
for (i = 0; i < len; i++) {
rxBuf[rxBufHead++] = serialIn[i];
if (rxBufHead >= (RX_BUF_SIZE - 1)) rxBufHead = 0;
}
}
// process rx buffer, this might return before the whole thing
// is proccessed, but we'll just get it next time
while (rxBufTail != rxBufHead) {
uint8_t tmp8 = rxBuf[rxBufTail++];
if (rxBufTail >= (RX_BUF_SIZE - 1)) rxBufTail = 0;
// uint8_t tmp8 = serialIn[i];
if (rstate == WAITING) {
if (tmp8 == eot) rstate = WAITING; // just keep waiting for something afer EOT
else {
rxPacketIndex = 0;
rxPacket[rxPacketIndex++] = tmp8;
rstate = RECEIVING;
}
} // waiting
else if (rstate == RECEIVING){
if (tmp8 == eot) { //TODO: exit if message len > max
rstate = WAITING;
decode(rxPacket, rxPacketIndex);
return 1;
}
else {
rxPacket[rxPacketIndex++] = tmp8;
rstate = RECEIVING;
}
} //receiving
} // gettin bytes
return 0;
}
//encode SLIP, put it in the encoded buffer
void SLIPEncodedSerial::encode(uint8_t b){
if(b == eot){
encodedBuf[encodedBufIndex++] = slipesc;
encodedBuf[encodedBufIndex++] = slipescend;
} else if(b==slipesc) {
encodedBuf[encodedBufIndex++] = slipesc;
encodedBuf[encodedBufIndex++] = slipescesc;
} else {
encodedBuf[encodedBufIndex++] = b;
}
}
void SLIPEncodedSerial::encode(const uint8_t *buf, int size)
{
beginPacket();
while(size--) encode(*buf++);
endPacket();
}
// decode SLIP, put it in the decoded buffer
void SLIPEncodedSerial::decode(const uint8_t *buf, int size)
{
int i;
decodedBufIndex = 0;
i = 0;
while (i < size) {
if (buf[i] == slipesc) { // TODO error out here if slipescend or slipescesc doesn't follow slipesc
i++;
if (buf[i] == slipescend) decodedBuf[decodedBufIndex++] = eot;
if (buf[i] == slipescesc) decodedBuf[decodedBufIndex++] = slipesc;
i++;
}
else {
decodedBuf[decodedBufIndex++] = buf[i];
i++;
}
}
decodedLength = decodedBufIndex;
}
//SLIP specific method which begins a transmitted packet
void SLIPEncodedSerial::beginPacket() {
encodedBufIndex = 0;
encodedBuf[encodedBufIndex++] = eot;
}
//signify the end of the packet with an EOT
void SLIPEncodedSerial::endPacket(){
encodedBuf[encodedBufIndex++] = eot;
encodedLength = encodedBufIndex;
}