forked from sh123/esp32_loraprs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax25_payload.cpp
204 lines (169 loc) · 4.9 KB
/
ax25_payload.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
#include "ax25_payload.h"
namespace AX25 {
Payload::Payload(const byte *rxPayload, int payloadLength)
: rptCallsCount_(0)
{
isValid_ = fromBinary(rxPayload, payloadLength);
}
Payload::Payload(const String &textPayload)
: rptCallsCount_(0)
{
isValid_ = fromString(textPayload);
}
void Payload::Dump()
{
Serial.println();
Serial.print("valid: "); Serial.println(isValid_);
Serial.println("src: " + srcCall_.ToString());
Serial.println("dst: " + dstCall_.ToString());
Serial.print("rpt: ");
for (int i = 0; i < rptCallsCount_; i++) {
Serial.print(rptCalls_[i].ToString() + " ");
}
Serial.println();
Serial.println("info: " + info_);
}
int Payload::ToBinary(byte *txPayload, int bufferLength) const
{
byte *txPtr = txPayload;
byte *txEnd = txPayload + bufferLength;
// destination address
if (!dstCall_.ToBinary(txPtr, CallsignSize)) return 0;
txPtr += CallsignSize;
if (txPtr >= txEnd) return 0;
// source address
if (!srcCall_.ToBinary(txPtr, CallsignSize)) return 0;
txPtr += CallsignSize;
if (txPtr >= txEnd) return 0;
// digipeater addresses
for (int i = 0; i < rptCallsCount_; i++) {
if (!rptCalls_[i].ToBinary(txPtr, CallsignSize)) return 0;
txPtr += CallsignSize;
if (txPtr >= txEnd) return 0;
}
*(txPtr - 1) |= 1;
// control + protocol id
if ((txPtr + 2) >= txEnd) return 0;
*(txPtr++) = AX25Ctrl::UI;
*(txPtr++) = AX25Pid::NoLayer3;
// information field
for (int i = 0; i < info_.length(); i++) {
char c = info_.charAt(i);
if (c == '\0') break;
*(txPtr++) = c;
if (txPtr >= txEnd) break;
}
return (int)(txPtr-txPayload);
}
String Payload::ToString(const String &customComment)
{
String txt = srcCall_.ToString() + String(">") + dstCall_.ToString();
for (int i = 0; i < rptCallsCount_; i++) {
if (rptCalls_[i].IsValid()) {
txt += String(",") + rptCalls_[i].ToString();
}
}
txt += String(":") + info_;
if (info_.startsWith("=")) {
txt += customComment;
}
return txt;
}
bool Payload::Digirepeat(const Callsign &ownCallsign)
{
for (int i = 0; i < rptCallsCount_; i++) {
if (rptCalls_[i].Digirepeat()) {
// if trace was digirepeated insert own callsign
if (rptCalls_[i].IsTrace()) {
InsertRptCallsign(ownCallsign, i);
}
return true;
}
}
return false;
}
bool Payload::InsertRptCallsign(const Callsign &rptCallsign, int index)
{
if (rptCallsCount_ >= RptMaxCount
|| index >= RptMaxCount
|| index >= rptCallsCount_) return false;
for (int i = index; i < rptCallsCount_; i++) {
rptCalls_[i + 1] = rptCalls_[i];
}
rptCalls_[index] = rptCallsign;
rptCallsCount_++;
return true;
}
bool Payload::fromBinary(const byte *rxPayload, int payloadLength)
{
const byte *rxPtr = rxPayload;
const byte *rxEnd = rxPayload + payloadLength;
if (payloadLength < CallsignSize) return false;
// destination address
dstCall_ = AX25::Callsign(rxPtr, CallsignSize);
if (!dstCall_.IsValid()) return false;
rxPtr += CallsignSize;
if (rxPtr >= rxEnd) return false;
// source address
srcCall_ = AX25::Callsign(rxPtr, CallsignSize);
if (!srcCall_.IsValid()) return false;
rxPtr += CallsignSize;
if (rxPtr >= rxEnd) return false;
rptCallsCount_ = 0;
// digipeater addresses
for (int i = 0, j = 0; i < RptMaxCount; i++) {
if ((rxPayload[(i + 2) * CallsignSize - 1] & 1) == 0) {
rptCalls_[j] = AX25::Callsign(rxPtr, CallsignSize);
if (rptCalls_[j].IsValid()) {
rptCallsCount_++;
j++;
}
rxPtr += CallsignSize;
if (rxPtr >= rxEnd) return false;
}
else {
break;
}
}
// control + protocol id
if ((rxPtr + 2) >= rxEnd) return false;
if (*(rxPtr++) != AX25Ctrl::UI) return false;
if (*(rxPtr++) != AX25Pid::NoLayer3) return false;
// information field
while (rxPtr < rxEnd) {
info_ += String((char)*(rxPtr++));
}
return true;
}
bool Payload::fromString(const String &textPayload)
{
String inputText = textPayload;
int rptIndex = inputText.indexOf('>');
int infoIndex = inputText.indexOf(':');
// bad format
if (rptIndex == -1 || infoIndex == -1) return false;
info_ = inputText.substring(infoIndex + 1);
srcCall_ = AX25::Callsign(inputText.substring(0, rptIndex));
if (!srcCall_.IsValid()) return false;
String paths = inputText.substring(rptIndex + 1, infoIndex);
rptCallsCount_ = 0;
int index = 0;
while (rptCallsCount_ <= RptMaxCount) {
int nextIndex = paths.indexOf(',', index);
String pathItem = paths.substring(index, nextIndex == -1 ? paths.length() : nextIndex);
if (index == 0) {
dstCall_ = AX25::Callsign(pathItem);
if (!dstCall_.IsValid()) return false;
}
else {
rptCalls_[rptCallsCount_] = AX25::Callsign(pathItem);
if (rptCalls_[rptCallsCount_].IsValid()) {
rptCallsCount_++;
}
}
if (nextIndex == -1) break;
index = nextIndex + 1;
}
return true;
}
} // AX25