-
Notifications
You must be signed in to change notification settings - Fork 274
/
AX25Control.cpp
252 lines (204 loc) · 5.55 KB
/
AX25Control.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
/*
* Copyright (C) 2020 Jonathan Naylor, G4KLX
*
* 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; version 2 of the License.
*
* 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.
*/
#include "AX25Control.h"
#include "AX25Defines.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
#include <ctime>
// #define DUMP_AX25
const unsigned char BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7])
CAX25Control::CAX25Control(CAX25Network* network, bool trace) :
m_network(network),
m_trace(trace),
m_enabled(true),
m_fp(NULL)
{
}
CAX25Control::~CAX25Control()
{
}
bool CAX25Control::writeModem(unsigned char *data, unsigned int len)
{
assert(data != NULL);
if (!m_enabled)
return false;
if (m_trace)
decode(data, len);
CUtils::dump(1U, "AX.25 received packet", data, len);
if (m_network == NULL)
return true;
return m_network->write(data, len);
}
unsigned int CAX25Control::readModem(unsigned char* data)
{
assert(data != NULL);
if (m_network == NULL)
return 0U;
if (!m_enabled)
return 0U;
unsigned int length = m_network->read(data, 500U);
if (length > 0U)
CUtils::dump(1U, "AX.25 transmitted packet", data, length);
return length;
}
bool CAX25Control::openFile()
{
if (m_fp != NULL)
return true;
time_t t;
::time(&t);
struct tm* tm = ::localtime(&t);
char name[100U];
::sprintf(name, "AX25_%04d%02d%02d_%02d%02d%02d.ambe", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
m_fp = ::fopen(name, "wb");
if (m_fp == NULL)
return false;
::fwrite("AX25", 1U, 4U, m_fp);
return true;
}
bool CAX25Control::writeFile(const unsigned char* data, unsigned int length)
{
if (m_fp == NULL)
return false;
::fwrite(&length, 1U, sizeof(unsigned int), m_fp);
::fwrite(data, 1U, length, m_fp);
return true;
}
void CAX25Control::closeFile()
{
if (m_fp != NULL) {
::fclose(m_fp);
m_fp = NULL;
}
}
void CAX25Control::enable(bool enabled)
{
m_enabled = enabled;
}
void CAX25Control::decode(const unsigned char* data, unsigned int length)
{
assert(data != NULL);
assert(length >= 15U);
std::string text;
bool more = decodeAddress(data + 7U, text);
text += '>';
decodeAddress(data + 0U, text);
unsigned int n = 14U;
while (more && n < length) {
text += ',';
more = decodeAddress(data + n, text, true);
n += 7U;
}
text += ' ';
if ((data[n] & 0x01U) == 0x00U) {
// I frame
char t[20U];
::sprintf(t, "<I S%u R%u>", (data[n] >> 1) & 0x07U, (data[n] >> 5) & 0x07U);
text += t;
} else {
if ((data[n] & 0x02U) == 0x00U) {
// S frame
char t[20U];
switch (data[n] & 0x0FU) {
case 0x01U:
sprintf(t, "<RR R%u>", (data[n] >> 5) & 0x07U);
break;
case 0x05U:
sprintf(t, "<RNR R%u>", (data[n] >> 5) & 0x07U);
break;
case 0x09U:
sprintf(t, "<REJ R%u>", (data[n] >> 5) & 0x07U);
break;
case 0x0DU:
sprintf(t, "<SREJ R%u>", (data[n] >> 5) & 0x07U);
break;
default:
sprintf(t, "<Unknown R%u>", (data[n] >> 5) & 0x07U);
break;
}
text += t;
LogMessage("AX.25, %s", text.c_str());
return;
} else {
// U frame
switch (data[n] & 0xEFU) {
case 0x6FU:
text += "<SABME>";
break;
case 0x2FU:
text += "<SABM>";
break;
case 0x43U:
text += "<DISC>";
break;
case 0x0FU:
text += "<DM>";
break;
case 0x63U:
text += "<UA>";
break;
case 0x87U:
text += "<FRMR>";
break;
case 0x03U:
text += "<UI>";
break;
case 0xAFU:
text += "<XID>";
break;
case 0xE3U:
text += "<TEST>";
break;
default:
text += "<Unknown>";
break;
}
if ((data[n] & 0xEFU) != 0x03U) {
LogMessage("AX.25, %s", text.c_str());
return;
}
}
}
n += 2U;
LogMessage("AX.25, %s %.*s", text.c_str(), length - n, data + n);
}
bool CAX25Control::decodeAddress(const unsigned char* data, std::string& text, bool isDigi) const
{
assert(data != NULL);
for (unsigned int i = 0U; i < 6U; i++) {
char c = data[i] >> 1;
if (c != ' ')
text += c;
}
unsigned char ssid = (data[6U] >> 1) & 0x0FU;
if (ssid > 0U) {
text += '-';
if (ssid >= 10U) {
text += '1';
text += '0' + ssid - 10U;
}
else {
text += '0' + ssid;
}
}
if (isDigi) {
if ((data[6U] & 0x80U) == 0x80U)
text += '*';
}
return (data[6U] & 0x01U) == 0x00U;
}