-
Notifications
You must be signed in to change notification settings - Fork 17
/
callsign.cpp
155 lines (135 loc) · 3.26 KB
/
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2020 by Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <iostream>
#include "callsign.h"
#define M17CHARACTERS " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/."
CCallsign::CCallsign()
{
memset(cs, 0, sizeof(cs));
coded = 0;
}
CCallsign::CCallsign(const std::string &callsign)
{
CSIn(callsign);
}
CCallsign::CCallsign(const uint8_t *in)
{
CodeIn(in);
}
void CCallsign::CSIn(const std::string &callsign)
{
const std::string m17_alphabet(M17CHARACTERS);
memset(cs, 0, sizeof(cs));
memcpy(cs, callsign.c_str(), (callsign.size()<10) ? callsign.size() : 9); // no more than 9 chars
coded = 0;
for( int i=int(callsign.size()-1); i>=0; i-- ) {
auto pos = m17_alphabet.find(cs[i]);
if (pos == std::string::npos) {
pos = 0;
}
cs[i] = m17_alphabet[pos]; // replace with valid character
coded *= 40;
coded += pos;
}
// strip trailing spaces (just to be nice!)
auto len = strlen(cs);
while (len && (' ' == cs[len-1]))
cs[--len] = 0;
}
const std::string CCallsign::GetCS(unsigned len) const
{
if (len > 9)
len = 9;
std::string rval(cs);
if (len)
rval.resize(len, ' ');
return rval;
}
void CCallsign::CodeIn(const uint8_t *in)
{
const std::string m17_alphabet(M17CHARACTERS);
memset(cs, 0, 10);
coded = in[0];
for (int i=1; i<6; i++)
coded = (coded << 8) | in[i];
if (coded > 0xee6b27ffffffu) {
std::cerr << "Callsign code is too large, 0x" << std::hex << coded << std::dec << std::endl;
return;
}
auto c = coded;
int i = 0;
while (c) {
cs[i++] = m17_alphabet[c % 40];
c /= 40;
}
}
void CCallsign::CodeOut(uint8_t *out) const
{
memset(out, 0, 6);
auto c = coded;
auto pout = out+5;
while (c)
{
*pout-- = c % 0x100u;
c /= 0x100u;
}
}
char CCallsign::GetModule() const
{
if (cs[8])
return cs[8];
else
return ' ';
}
bool CCallsign::HasSameCallsign(const CCallsign &call) const
{
return (this->GetCS(8) == call.GetCS(8));
}
bool CCallsign::operator==(const CCallsign &rhs) const
{
return coded == rhs.coded;
}
bool CCallsign::operator!=(const CCallsign &rhs) const
{
return coded != rhs.coded;
}
bool CCallsign::HasSameCallsignWithWildcard(const CCallsign &callsign) const
{
bool same = true;
bool done = false;
for ( unsigned i = 0; (i < sizeof(cs)) && same && !done; i++ )
{
if ( !(done = ((cs[i] == '*') || (callsign.cs[i] == '*'))) )
{
same &= (cs[i] == callsign.cs[i]);
}
}
return same;
}
void CCallsign::SetModule(char m)
{
std::string call(cs);
call.resize(8, ' ');
call.append(1, m);
CSIn(call);
}
std::ostream &operator<<(std::ostream &stream, const CCallsign &call)
{
stream << call.cs;
return stream;
}