-
Notifications
You must be signed in to change notification settings - Fork 118
/
SLIP.hpp
217 lines (191 loc) · 5.03 KB
/
SLIP.hpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma once
#include <vector>
#include <stdint.h>
namespace aoo {
class SLIP {
public:
static const uint8_t END = 192;
static const uint8_t ESC = 219;
static const uint8_t ESC_END = 220;
static const uint8_t ESC_ESC = 221;
void setup(int32_t buffersize);
void reset();
int32_t read_available() const { return balance_; }
int32_t read_bytes(uint8_t *buffer, int32_t size);
int32_t write_available() const { return buffer_.size() - balance_; }
int32_t write_bytes(const uint8_t *data, int32_t size);
int32_t read_packet(uint8_t *buffer, int32_t size);
bool write_packet(const uint8_t *data, int32_t size);
private:
std::vector<uint8_t> buffer_;
int32_t rdhead_ = 0;
int32_t wrhead_ = 0;
int32_t balance_ = 0;
};
inline void SLIP::setup(int32_t buffersize){
buffer_.resize(buffersize);
reset();
}
inline void SLIP::reset(){
rdhead_ = 0;
wrhead_ = 0;
balance_ = 0;
}
inline int32_t SLIP::read_bytes(uint8_t *buffer, int32_t size){
auto capacity = (int32_t)buffer_.size();
if (size > balance_){
size = balance_;
}
auto end = rdhead_ + size;
int32_t n1, n2;
if (end > capacity){
n1 = capacity - rdhead_;
n2 = end - capacity;
} else {
n1 = size;
n2 = 0;
}
std::copy(&buffer_[rdhead_], &buffer_[rdhead_ + n1], buffer);
std::copy(&buffer_[0], &buffer_[n2], buffer + n1);
rdhead_ += size;
if (rdhead_ >= capacity){
rdhead_ -= capacity;
}
balance_ -= size;
return size;
}
inline int32_t SLIP::write_bytes(const uint8_t *data, int32_t size){
auto capacity = (int32_t)buffer_.size();
auto space = capacity - balance_;
if (size > space){
size = space;
}
auto end = wrhead_ + size;
int32_t split;
if (end > capacity){
split = capacity - wrhead_;
} else {
split = size;
}
std::copy(data, data + split, &buffer_[wrhead_]);
std::copy(data + split, data + size, &buffer_[0]);
wrhead_ += size;
if (wrhead_ >= capacity){
wrhead_ -= capacity;
}
balance_ += size;
return size;
}
inline int32_t SLIP::read_packet(uint8_t *buffer, int32_t size){
int32_t nbytes = 0;
int32_t head = rdhead_;
auto read_byte = [&](uint8_t& c) {
if (nbytes >= balance_){
return false;
}
c = buffer_[head++];
if (head >= (int32_t)buffer_.size()){
head = 0;
}
nbytes++;
return true;
};
uint8_t data;
// swallow leading END tokens
do {
if (!read_byte(data)){
// no data
return 0;
}
} while (data == END);
// try to read packet
int32_t packetsize = 0;
int32_t counter = 0;
while (data != END) {
if (data == ESC){
// escape character - read another byte
if (read_byte(data)){
if (data == ESC_END){
data = END;
} else if (data == ESC_ESC){
data = ESC;
} else if (data == END){
// incomplete escape sequence before END
break;
} else {
// bad SLIP packet... just ignore
}
} else {
// too little data
return 0;
}
}
// ignore excessive bytes
if (counter < size){
buffer[counter] = data;
packetsize++;
}
counter++;
// try to get more data
if (!read_byte(data)){
// too little data
return 0;
}
}
// update
rdhead_ = head;
balance_ -= nbytes;
return packetsize;
}
inline bool SLIP::write_packet(const uint8_t *data, int32_t size){
int32_t available = buffer_.size() - balance_;
int32_t nbytes = 0;
int32_t head = wrhead_;
auto write_byte = [&](uint8_t c) {
if (nbytes >= available){
return false;
}
buffer_[head++] = c;
if (head >= (int32_t)buffer_.size()){
head = 0;
}
nbytes++;
return true;
};
if ((size + 2) <= available){
// begin packet
write_byte(END);
// write and escape bytes
for (int i = 0; i < size; ++i){
auto c = data[i];
switch (c){
case END:
if (!(write_byte(ESC) && write_byte(ESC_END))){
return false;
}
break;
case ESC:
if (!(write_byte(ESC) && write_byte(ESC_ESC))){
return false;
}
break;
default:
if (!write_byte(c)){
return false;
}
}
}
// end packet
if (write_byte(END)){
// update
wrhead_ = head;
balance_ += nbytes;
return true;
} else {
return false;
}
} else {
return false;
}
}
} // aoo