-
Notifications
You must be signed in to change notification settings - Fork 0
/
QnetIcomStack.cpp
383 lines (341 loc) · 11 KB
/
QnetIcomStack.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (C) 2022 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 <exception>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <csignal>
#include <ctime>
#include <cstdlib>
#include <netdb.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <thread>
#include "QnetIcomStack.h"
#include "QnetTypeDefs.h"
#include "QnetConfigure.h"
#define RELAY_VERSION "20217"
CQnetIcomStack::CQnetIcomStack() : G2_COUNTER_OUT(0)
{
}
CQnetIcomStack::~CQnetIcomStack()
{
}
bool CQnetIcomStack::Initialize(const char *cfgfile)
{
if (ReadConfig(cfgfile))
return true;
// for the sendto address of the icom stack
icom_stack.Initialize(AF_INET, REPEATER_PORT, REPEATER_IP.c_str());
// open the icom socket
printf("Open the UDP socket to the icom stack\n");
icom_sock.Initialize(AF_INET, REPEATER_PORT, LOCAL_IP.c_str());
icom_fd = OpenSocket(icom_sock);
if (icom_fd < 0)
return true;
// open the unix socket to the gateway
printf("Connecting to the gateway at %s\n", togate.c_str());
if (ToGate.Open(togate.c_str(), this))
return true;
gateway_fd = ToGate.GetFD();
printf("File descriptors: icom=%d, gateway=%d\n", icom_fd, gateway_fd);
return false;
}
void CQnetIcomStack::IcomInit()
{
// send INIT to Icom Stack
unsigned char buf[500];
memset(buf, 0, 10);
memcpy(buf, "INIT", 4);
buf[6] = 0x73u;
SendToIcom(buf, 10);
printf("Initializing the Icom controller...\n");
// get the acknowledgement from the ICOM Stack
while (IsRunning()) {
CSockAddress addr(AF_INET);
socklen_t addrlen = addr.GetSize();
int recvlen = recvfrom(icom_fd, buf, 500, 0, addr.GetPointer(), &addrlen);
if (10==recvlen && 0==memcmp(buf, "INIT", 4) && 0x72U==buf[6] && 0x0U==buf[7]) {
OLD_REPLY_SEQ = 256u * buf[4] + buf[5];
NEW_REPLY_SEQ = OLD_REPLY_SEQ + 1;
G2_COUNTER_OUT = NEW_REPLY_SEQ;
printf("Detected the Icom controller! Counter=%u\n", G2_COUNTER_OUT);
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int CQnetIcomStack::OpenSocket(const CSockAddress &sock)
{
int fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
printf("Cannot create the UDP socket, err: %d, %s\n", errno, strerror(errno));
return -1;
}
int reuse = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) == -1)
{
printf("Cannot set the UDP socket %s:%u option, err: %d, %s\n", sock.GetAddress(), sock.GetPort(), errno, strerror(errno));
close(fd);
return -1;
}
if (0 > bind(fd, sock.GetCPointer(), sock.GetSize()))
{
printf("Cannot bind the UDP socket %s:%u address, err: %d, %s\n", sock.GetAddress(), sock.GetPort(), errno, strerror(errno));
close(fd);
return -1;
}
return fd;
}
void CQnetIcomStack::Run()
{
IcomInit();
while (IsRunning())
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(icom_fd, &readfds);
FD_SET(gateway_fd, &readfds);
int maxfs = (icom_fd > gateway_fd) ? icom_fd : gateway_fd;
// don't care about writefds and exceptfds:
// and we'll wait as long as needed
int ret = ::select(maxfs+1, &readfds, NULL, NULL, NULL);
if (ret < 0)
{
printf("ERROR: Run: select returned err=%d, %s\n", errno, strerror(errno));
break;
}
if (ret == 0)
continue;
// there is something to read!
sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in));
socklen_t size = sizeof(sockaddr);
ssize_t len;
if (FD_ISSET(icom_fd, &readfds))
{
SDSTR dstr;
len = recvfrom(icom_fd, dstr.title, sizeof(SDSTR), 0, (sockaddr *)&addr, &size);
if (ntohs(addr.sin_port) != REPEATER_PORT)
printf("Unexpected icom port was %u, expected %u.\n", ntohs(addr.sin_port), REPEATER_PORT);
if (0 == memcmp(dstr.title, "DSTR", 4))
{
if ((58 == len || 32 == len || 29 == len) && (0x73u == dstr.flag[0]) && (0x12u == dstr.flag[1]) && (0x0u == dstr.flag[2]))
{ // regular packet!
// first: acknowledge the packet
unsigned char ackn[10];
memcpy(ackn, dstr.title, 6);
ackn[6] = 0x72u;
memset(ackn+7, 0, 3);
SendToIcom(ackn, 10);
// finally: process the packet and send it on...
SDSVT dsvt;
memcpy(dsvt.title, "DSVT", 4);
dsvt.config = (58 == len) ? 0x10u : 0x20u;
memset(dsvt.flaga, 0, 3);
dsvt.id = 0x20;
memcpy(dsvt.flagb, dstr.vpkt.flagb, 3);
dsvt.streamid = dstr.vpkt.streamid;
dsvt.ctrl = dstr.vpkt.ctrl;
if (58 == len)
{
if (LOG_QSO)
{
printf("id=%04x from RPTR count=%u f=%02x%02x%02x icmid=%02x%02x%02x%02x flag=%02x%02x%02x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", ntohs(dstr.vpkt.streamid), ntohs(dstr.counter), dstr.flag[0], dstr.flag[1], dstr.flag[2], dstr.vpkt.icm_id, dstr.vpkt.flagb[0], dstr.vpkt.flagb[1], dstr.vpkt.flagb[2], dstr.vpkt.hdr.flag[0], dstr.vpkt.hdr.flag[1], dstr.vpkt.hdr.flag[2], dstr.vpkt.hdr.ur, dstr.vpkt.hdr.r1, dstr.vpkt.hdr.r2, dstr.vpkt.hdr.my, dstr.vpkt.hdr.nm);
}
memcpy(dsvt.hdr.flag, dstr.vpkt.hdr.flag, 3); // flags
memcpy(dsvt.hdr.rpt1, dstr.vpkt.hdr.r2, 8); // reverse order...
memcpy(dsvt.hdr.rpt2, dstr.vpkt.hdr.r1, 8); // to make it right for the gateway
memcpy(dsvt.hdr.urcall, dstr.vpkt.hdr.ur, 22); // ur, my, nm, pfcs 8+8+4+2==22
ToGate.Write(dsvt.title, 56);
}
else // 29==len or 32==len
{
if (LOG_QSO && (dstr.vpkt.ctrl & 0x40u))
{
printf("id=%04x from RPTR count=%u end of transmission\n", ntohs(dstr.vpkt.streamid), ntohs(dstr.counter));
}
memcpy(dsvt.vasd.voice, (29==len)?dstr.vpkt.vasd.voice:dstr.vpkt.vasd1.voice, 12);
ToGate.Write(dsvt.title, 27);
}
}
else if (26 == len)
{
// printf("SPKT: my=%.8s rpt=%.8s\n", dstr.spkt.mycall, dstr.spkt.rpt);
}
else if ((10 == len) && (0x72u == dstr.flag[0]))
{
NEW_REPLY_SEQ = ntohs(dstr.counter);
if (NEW_REPLY_SEQ == OLD_REPLY_SEQ) {
G2_COUNTER_OUT = NEW_REPLY_SEQ;
OLD_REPLY_SEQ = NEW_REPLY_SEQ - 1;
} else
OLD_REPLY_SEQ = NEW_REPLY_SEQ;
}
else if (0x73U==dstr.flag[0] && (0x21U==dstr.flag[1] || 0x11U==dstr.flag[1] || 0x0U==dstr.flag[1]))
{
unsigned char buf[10];
memcpy(buf, dstr.title, 6);
buf[6]=0x72u;
memset(buf+7, 0, 3);
SendToIcom(buf, 10);
}
else
{
Dump("Unexpected packet from Icom repeater:", dstr.title, len);
}
}
else if (len < 0)
{
fprintf(stderr, "ERROR: Run: recvfrom() return error %d: %s\n", errno, strerror(errno));
break;
}
else if (0 == len)
{
printf("Read zero bytes from the Icom repeater\n");
}
else
{
Dump("Unexpected packet from Icom reapeater:", dstr.title, len);
}
}
if (FD_ISSET(gateway_fd, &readfds))
{
SDSVT dsvt;
len = ToGate.Read(dsvt.title, sizeof(SDSVT));
if ((56 == len || 27 == len) && (0 == memcmp(dsvt.title, "DSVT", 4)))
{
SDSTR dstr;
memcpy(dstr.title, "DSTR", 4);
dstr.counter = ntohs(G2_COUNTER_OUT++);
dstr.flag[0] = 0x73u;
dstr.flag[1] = 0x12u;
dstr.flag[2] = 0x00u;
dstr.remaining = (56 == len) ? 48 : 19;
dstr.vpkt.icm_id = 0x20u;
if (56 == len)
{
memcpy(dstr.vpkt.flagb, dsvt.flagb, 47);
SendToIcom(dstr.title, 58);
if (LOG_QSO)
{
printf("id=%04x to RPTR count=%u f=%02x%02x%02x icmid=%02x%02x%02x%02x flag=%02x%02x%02x ur=%.8s r1=%.8s r2=%.8s my=%.8s/%.4s\n", ntohs(dstr.vpkt.streamid), ntohs(dstr.counter), dstr.flag[0], dstr.flag[1], dstr.flag[2], dstr.vpkt.icm_id, dstr.vpkt.flagb[0], dstr.vpkt.flagb[1], dstr.vpkt.flagb[2], dstr.vpkt.hdr.flag[0], dstr.vpkt.hdr.flag[1], dstr.vpkt.hdr.flag[2], dstr.vpkt.hdr.ur, dstr.vpkt.hdr.r1, dstr.vpkt.hdr.r2, dstr.vpkt.hdr.my, dstr.vpkt.hdr.nm);
}
}
else
{
memcpy(dstr.vpkt.flagb, dsvt.flagb, 18);
SendToIcom(dstr.title, 29);
if (LOG_QSO && (dstr.vpkt.ctrl & 0x40u))
{
printf("id=%04x to RPTR count=%u end of transmission\n", ntohs(dstr.vpkt.streamid), ntohs(dstr.counter));
}
}
}
else if (len < 0)
{
fprintf(stderr, "ERROR: Run: ToGate.Read() returned error %d: %s\n", errno, strerror(errno));
break;
}
else if (0 == len)
{
printf("Read zero bytes from the gateway\n");
}
else
{
Dump("Unexpected packet from the gateway", dsvt.title, len);
}
}
}
close(icom_fd);
ToGate.Close();
}
void CQnetIcomStack::SendToIcom(const unsigned char *buf, const int size) const
{
int len = sendto(icom_fd, buf, size, 0, icom_stack.GetCPointer(), icom_stack.GetSize());
if (len == size)
return;
if (len < 0)
printf("sendto() Icom at %s:%u error: %s\n", icom_sock.GetAddress(), icom_sock.GetPort(), strerror(errno));
else
printf("ERROR: Icom short send %d bytes, actually sent %d.\n", size, len);
}
// process configuration file and return true if there was a problem
bool CQnetIcomStack::ReadConfig(const char *cfgFile)
{
CQnetConfigure cfg;
printf("Reading file %s\n", cfgFile);
if (cfg.Initialize(cfgFile))
return true;
const std::string estr; // an empty GetDefaultString
std::string icom_path("module_");
std::string type;
// we need to find an Icom module
for (int i=0; i<3; i++)
{
std::string test(icom_path);
test.append(1, 'a'+i);
if (cfg.KeyExists(test))
{
cfg.GetValue(test, estr, type, 1, 16);
if (type.compare("icom"))
{
fprintf(stderr, "Found an incompatible module, '%s', aborting!\n", type.c_str());
return true;
}
available_module = i;
break;
}
}
cfg.GetValue("log_qso", estr, LOG_QSO);
cfg.GetValue("gateway_to_icom", estr, togate, 1, FILENAME_MAX);
cfg.GetValue("icom_internal_ip", estr, LOCAL_IP, 7, IP_SIZE);
cfg.GetValue("icom_external_ip", estr, REPEATER_IP, 7, IP_SIZE);
int i;
cfg.GetValue("icom_port", estr, i, 10000, 65535);
REPEATER_PORT = (unsigned short)i;
return false;
}
int main(int argc, const char **argv)
{
setbuf(stdout, NULL);
if (2 != argc)
{
fprintf(stderr, "usage: %s path_to_config_file\n", argv[0]);
return 1;
}
if ('-' == argv[1][0])
{
printf("QnetIcomStack Version #%s Copyright (C) 2022 by Thomas A. Early N7TAE\n", RELAY_VERSION);
printf("QnetIcomStack comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n");
printf("This is free software, and you are welcome to distribute it\nunder certain conditions that are discussed in the LICENSE file.\n");
return 0;
}
CQnetIcomStack qnistack;
if (qnistack.Initialize(argv[1]))
return 1;
qnistack.Run();
printf("%s is closing.\n", argv[0]);
return 0;
}