forked from n7tae/mrefd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallsign.cpp
143 lines (125 loc) · 3.1 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
/*
* 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));
memset(code, 0, sizeof(code));
}
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);
uint64_t encoded = 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;
}
encoded *= 40;
encoded += pos;
}
for (int i=0; i<6; i++) {
code[i] = (encoded >> (8*(5-i)) & 0xFFU);
}
}
void CCallsign::CodeIn(const uint8_t *in)
{
const std::string m17_alphabet(M17CHARACTERS);
memset(cs, 0, 10);
uint64_t 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;
}
memcpy(code, in, 6);
int i = 0;
while (coded) {
cs[i++] = m17_alphabet[coded % 40];
coded /= 40;
}
}
const std::string CCallsign::GetCS(unsigned len) const
{
if (len > 9)
len = 9;
std::string rval(cs);
rval.resize(len, ' ');
return rval;
}
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 (0 == memcmp(code, rhs.code, 6));
}
bool CCallsign::operator!=(const CCallsign &rhs) const
{
return (0 != memcmp(code, rhs.code, 6));
}
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);
}
bool CCallsign::IsValid() const
{
return true;
}
std::ostream &operator<<(std::ostream &stream, const CCallsign &call)
{
stream << call.cs;
return stream;
}