forked from sh123/esp32_loraprs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax25_callsign.cpp
138 lines (109 loc) · 2.52 KB
/
ax25_callsign.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
#include "ax25_callsign.h"
namespace AX25 {
Callsign::Callsign()
: isValid_(false)
, call_()
, ssid_(0)
{
}
Callsign::Callsign(const Callsign &callsign)
: isValid_(callsign.isValid_)
, call_(callsign.call_)
, ssid_(callsign.ssid_)
{
}
Callsign& Callsign::operator=(const Callsign &callsign)
{
isValid_ = callsign.isValid_;
call_ = callsign.call_;
ssid_ = callsign.ssid_;
return *this;
}
Callsign::Callsign(const byte *rxPayload, int payloadLength)
: isValid_(false)
, call_()
, ssid_(0)
{
isValid_ = fromBinary(rxPayload, payloadLength);
}
Callsign::Callsign(const String &callsign)
: isValid_(false)
, call_()
, ssid_(0)
{
isValid_ = fromString(callsign);
}
bool Callsign::ToBinary(byte *txPayload, int bufferLength) const
{
if (bufferLength < CallsignSize) return false;
byte *ptr = txPayload;
memset(ptr, 0, bufferLength);
for (int i = 0; i < CallsignSize - 1; i++) {
if (i < call_.length()) {
char c = call_.charAt(i);
*(ptr++) = c << 1;
}
else {
*(ptr++) = char(' ') << 1;
}
}
*(txPayload + CallsignSize - 1) = ssid_ << 1;
return true;
}
String Callsign::ToString(bool isDigirepeater) const
{
String result = call_;
if (ssid_ == 0) {
result += isDigirepeater ? "*" : "";
} else {
result += "-" + String(ssid_);
}
return result;
}
bool Callsign::Digirepeat()
{
if (IsPath()) {
if (ssid_ > 0) {
ssid_--;
return true;
}
}
return false;
}
bool Callsign::fromString(const String &inputCallsign)
{
String callsign = inputCallsign;
// "ABCDEF-XX"
if (callsign.length() > CallsignSize + 2 || callsign.length() == 0) return false;
int delimIndex = callsign.indexOf('-');
// "ABCDEF-"
if (delimIndex != -1 && delimIndex == callsign.length() - 1) return false;
call_ = callsign;
ssid_ = 0;
if (delimIndex == -1) {
// "ABCDEF"
if (call_.length() >= CallsignSize) return false;
}
else {
call_ = callsign.substring(0, delimIndex);
ssid_ = callsign.substring(delimIndex + 1).toInt();
}
return true;
}
bool Callsign::fromBinary(const byte *rxPtr, int length)
{
if (length < CallsignSize) return false;
byte callsign[CallsignSize];
const byte *ptr = rxPtr;
memset(callsign, 0, sizeof(callsign));
for (int i = 0; i < CallsignSize - 1; i++) {
char c = *(ptr++) >> 1;
callsign[i] = (c == ' ') ? '\0' : c;
}
callsign[CallsignSize-1] = '\0';
ssid_ = (*ptr >> 1) & 0x0f;
call_ = String((char*)callsign);
if (call_.length() == 0) return false;
return true;
}
} // AX25