forked from sh123/esp32_loraprs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiss_processor.cpp
309 lines (282 loc) · 7.2 KB
/
kiss_processor.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "kiss_processor.h"
namespace Kiss {
Processor::Processor()
: disableKiss_(false)
, usePrefix3_(false)
, isRawIdle_(true)
, state_(State::GetStart)
{
}
void Processor::sendRigToSerial(Cmd cmd, const byte *packet, int packetLength) {
if (disableKiss_) {
for (int i = 0; i < packetLength; i++) {
byte rxByte = packet[i];
// TNC2 splits on \n
if (rxByte == '\0') {
onSerialTx('\n');
} else {
onSerialTx(rxByte);
}
// append \n for TNC2 compatibility if not there
if (rxByte != '\n' && rxByte != '\0' && i == packetLength - 1) {
onSerialTx('\n');
}
}
} else {
onSerialTx((byte)Marker::Fend);
onSerialTx((byte)cmd);
for (int i = 0; i < packetLength; i++) {
byte rxByte = packet[i];
if (rxByte == Marker::Fend) {
onSerialTx((byte)Marker::Fesc);
onSerialTx((byte)Marker::Tfend);
}
else if (rxByte == Marker::Fesc) {
onSerialTx((byte)Marker::Fesc);
onSerialTx((byte)Marker::Tfesc);
}
else {
onSerialTx(rxByte);
}
}
onSerialTx((byte)Marker::Fend);
}
}
void Processor::queueRigToSerial(Cmd cmd, const byte *packet, int packetLength) {
for (int i = 0; i < packetLength; i++) {
if (!rigToSerialQueue_.unshift(packet[i])) {
LOG_WARN("Rig to serial queue is full!");
return;
}
}
if (!rigToSerialQueueIndex_.unshift(packetLength)) {
LOG_WARN("Rig to serial index queue is full!");
return;
}
}
void Processor::queueSerialToRig(Cmd cmd, const byte *packet, int packetLength) {
bool result = 1;
if (disableKiss_) {
// inject proprietary identifier
if (usePrefix3_) {
result &= serialToRigQueue_.unshift('<');
result &= serialToRigQueue_.unshift(0xff);
result &= serialToRigQueue_.unshift(0x01);
}
// TNC2, send as is, receiveByteRaw will deal with it
for (int i = 0; i < packetLength; i++) {
byte rxByte = packet[i];
result &= serialToRigQueue_.unshift(rxByte);
}
result &= serialToRigQueue_.unshift('\n');
} else {
result &= serialToRigQueue_.unshift(Marker::Fend);
result &= serialToRigQueue_.unshift(cmd);
for (int i = 0; i < packetLength; i++) {
byte rxByte = packet[i];
if (rxByte == Marker::Fend) {
result &= serialToRigQueue_.unshift(Marker::Fesc);
result &= serialToRigQueue_.unshift(Marker::Tfend);
}
else if (rxByte == Marker::Fesc) {
result &= serialToRigQueue_.unshift(Marker::Fesc);
result &= serialToRigQueue_.unshift(Marker::Tfesc);
}
else {
result &= serialToRigQueue_.unshift(rxByte);
}
}
result &= serialToRigQueue_.unshift(Marker::Fend);
}
if (!result) {
LOG_WARN("Serial to rig queue overflow!");
}
}
bool Processor::processRigToSerial()
{
bool isProcessed = false;
if (rigToSerialQueueIndex_.isEmpty()) {
return isProcessed;
}
while (!rigToSerialQueueIndex_.isEmpty()) {
int rxPacketSize = rigToSerialQueueIndex_.pop();
byte buf[rxPacketSize];
int readCnt = rxPacketSize;
for (int i = 0, j = 0; i < readCnt; i++) {
byte rxByte = rigToSerialQueue_.pop();
if (disableKiss_) {
// filter out properietary identifier
if (usePrefix3_) {
if ((i == 0 && rxByte == '<') ||
(i == 1 && rxByte == 0xff) ||
(i == 2 && rxByte == 0x01))
{
rxPacketSize--;
continue;
}
}
}
buf[j++] = rxByte;
}
sendRigToSerial(Cmd::Data, buf, rxPacketSize);
onRigPacket(&buf, rxPacketSize);
isProcessed = true;
yield();
}
return isProcessed;
}
bool Processor::processSerialToRig()
{
bool allProcessed = false;
while (onSerialRxHasData() || !serialToRigQueue_.isEmpty()) {
byte rxByte;
if (onSerialRxHasData()) {
if (onSerialRx(&rxByte)) {
if (!serialToRigQueue_.unshift(rxByte)) {
LOG_WARN("Serial to rig buffer is full!");
}
}
}
if (!serialToRigQueue_.isEmpty()) {
rxByte = serialToRigQueue_.pop();
if (receiveByte(rxByte)) {
allProcessed = true;
} else {
serialToRigQueue_.push(rxByte);
}
}
vTaskDelay(1);
}
return allProcessed;
}
bool Processor::processCommand(byte rxByte) {
switch (rxByte) {
case Cmd::Data:
if (!onRigTxBegin()) {
return false;
}
state_ = State::GetData;
dataType_ = DataType::Raw;
break;
case Cmd::SetHardware:
state_ = State::GetData;
dataType_ = DataType::Control;
cmdBuffer_.clear();
break;
case Cmd::RebootRequested:
state_ = State::GetData;
dataType_ = DataType::Reboot;
cmdBuffer_.clear();
break;
case Cmd::P:
state_ = State::GetP;
break;
case Cmd::SlotTime:
state_ = State::GetSlotTime;
break;
case Marker::Fend:
break;
default:
// unknown command
state_ = State::GetEnd;
return true;
}
return true;
}
void Processor::processData(byte rxByte) {
switch (rxByte) {
case Marker::Fesc:
state_ = State::Escape;
break;
case Marker::Fend:
if (dataType_ == DataType::Raw) {
onRigTxEnd();
} else if (dataType_ == DataType::Control) {
onRadioControlCommand(cmdBuffer_);
} else if (dataType_ == DataType::Reboot) {
onRebootCommand();
}
state_ = State::GetStart;
break;
default:
if (dataType_ == DataType::Raw) {
onRigTx(rxByte);
} else if (dataType_ == DataType::Control) {
cmdBuffer_.push_back(rxByte);
}
break;
}
}
bool Processor::receiveByteRaw(byte rxByte)
{
if (isRawIdle_) {
if (!onRigTxBegin()) {
return false;
}
isRawIdle_ = false;
}
// NOTE, TNC2 uses \n as a packet delimiter
if (rxByte == '\n') {
onRigTxEnd();
isRawIdle_ = true;
} else {
onRigTx(rxByte);
}
return true;
}
bool Processor::receiveByteKiss(byte rxByte)
{
switch (state_) {
case State::GetStart:
if (rxByte == Marker::Fend) {
state_ = State::GetCmd;
}
break;
case State::GetEnd:
if (rxByte == Marker::Fend) {
state_ = State::GetStart;
}
break;
case State::GetCmd:
if (!processCommand(rxByte)) {
return false;
}
break;
case State::GetP:
onControlCommand(Cmd::P, rxByte);
state_ = State::GetEnd;
break;
case State::GetSlotTime:
onControlCommand(Cmd::SlotTime, rxByte);
state_ = State::GetEnd;
break;
case State::GetData:
processData(rxByte);
break;
case State::Escape:
if (rxByte == Marker::Tfend) {
onRigTx((byte)Marker::Fend);
state_ = State::GetData;
}
else if (rxByte == Marker::Tfesc) {
onRigTx((byte)Marker::Fesc);
state_ = State::GetData;
}
else if (rxByte != Marker::Fend) {
state_ = State::GetEnd;
}
break;
default:
// unknown state
state_ = State::GetStart;
break;
}
return true;
}
bool Processor::receiveByte(byte rxByte) {
if (disableKiss_) {
return receiveByteRaw(rxByte);
}
return receiveByteKiss(rxByte);
}
} // kiss