-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDCSHandler.cpp
784 lines (633 loc) · 21 KB
/
DCSHandler.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
* Copyright (C) 2012-2015 by Jonathan Naylor G4KLX
* Copyright (c) 2017-2018 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 <cassert>
#include "DCSHandler.h"
#include "Utils.h"
CDCSProtocolHandlerPool *CDCSHandler::m_pool = NULL;
CDCSProtocolHandler *CDCSHandler::m_incoming = NULL;
bool CDCSHandler::m_stateChange = false;
GATEWAY_TYPE CDCSHandler::m_gatewayType = GT_REPEATER;
CCallsignList *CDCSHandler::m_whiteList = NULL;
CCallsignList *CDCSHandler::m_blackList = NULL;
std::list<CDCSHandler *> CDCSHandler::m_DCSHandlers;
CDCSHandler::CDCSHandler(IReflectorCallback *handler, const std::string &dcsHandler, const std::string &repeater, CDCSProtocolHandler *protoHandler, const in_addr &address, unsigned int port, DIRECTION direction) :
m_reflector(dcsHandler),
m_repeater(repeater),
m_handler(protoHandler),
m_yourAddress(address),
m_yourPort(port),
m_myPort(0U),
m_direction(direction),
m_linkState(DCS_LINKING),
m_destination(handler),
m_time(),
m_pollTimer(1000U, 5U),
m_pollInactivityTimer(1000U, 60U),
m_tryTimer(1000U, 1U),
m_tryCount(0U),
m_dcsId(0x00U),
m_dcsSeq(0x00U),
m_seqNo(0x00U),
m_inactivityTimer(1000U, NETWORK_TIMEOUT),
m_yourCall(),
m_myCall1(),
m_myCall2(),
m_rptCall1(),
m_rptCall2()
{
assert(protoHandler != NULL);
assert(handler != NULL);
assert(port > 0U);
m_myPort = protoHandler->getPort();
m_pollInactivityTimer.start();
m_time = ::time(NULL);
if (direction == DIR_INCOMING) {
m_pollTimer.start();
m_stateChange = true;
m_linkState = DCS_LINKED;
} else {
m_linkState = DCS_LINKING;
m_tryTimer.start();
}
}
CDCSHandler::~CDCSHandler()
{
if (m_direction == DIR_OUTGOING)
m_pool->release(m_handler);
}
void CDCSHandler::setDCSProtocolHandlerPool(CDCSProtocolHandlerPool *pool)
{
assert(pool != NULL);
m_pool = pool;
}
void CDCSHandler::setDCSProtocolIncoming(CDCSProtocolHandler *handler)
{
assert(handler != NULL);
m_incoming = handler;
}
void CDCSHandler::setGatewayType(GATEWAY_TYPE type)
{
m_gatewayType = type;
}
void CDCSHandler::setWhiteList(CCallsignList *list)
{
assert(list != NULL);
m_whiteList = list;
}
void CDCSHandler::setBlackList(CCallsignList *list)
{
assert(list != NULL);
m_blackList = list;
}
std::string CDCSHandler::getIncoming(const std::string &callsign)
{
std::string incoming;
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
if (dcsHandler->m_direction == DIR_INCOMING && 0==dcsHandler->m_repeater.compare(callsign)) {
incoming.append(dcsHandler->m_reflector);
incoming.append(" ");
}
}
return incoming;
}
void CDCSHandler::getInfo(IReflectorCallback *handler, CRemoteRepeaterData &data)
{
assert(handler != NULL);
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
if (dcsHandler->m_destination == handler) {
if (dcsHandler->m_direction == DIR_INCOMING && 0==dcsHandler->m_repeater.size()) {
if (dcsHandler->m_linkState != DCS_UNLINKING)
data.addLink(dcsHandler->m_reflector, PROTO_DCS, dcsHandler->m_linkState == DCS_LINKED, DIR_INCOMING, true);
} else {
if (dcsHandler->m_linkState != DCS_UNLINKING)
data.addLink(dcsHandler->m_reflector, PROTO_DCS, dcsHandler->m_linkState == DCS_LINKED, dcsHandler->m_direction, false);
}
}
}
}
void CDCSHandler::process(CAMBEData &data)
{
in_addr yourAddress = data.getYourAddress();
unsigned int yourPort = data.getYourPort();
unsigned int myPort = data.getMyPort();
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
if ( dcsHandler->m_yourAddress.s_addr == yourAddress.s_addr &&
dcsHandler->m_yourPort == yourPort &&
dcsHandler->m_myPort == myPort) {
dcsHandler->processInt(data);
return;
}
}
}
void CDCSHandler::process(CPollData &poll)
{
std::string dcsHandler = poll.getData1();
std::string repeater = poll.getData2();
in_addr yourAddress = poll.getYourAddress();
unsigned int yourPort = poll.getYourPort();
unsigned int myPort = poll.getMyPort();
unsigned int length = poll.getLength();
// Check to see if we already have a link
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *handler = *it;
if ( 0==handler->m_reflector.compare(dcsHandler) &&
0==handler->m_repeater.compare(repeater) &&
handler->m_yourAddress.s_addr == yourAddress.s_addr &&
handler->m_yourPort == yourPort &&
handler->m_myPort == myPort &&
handler->m_direction == DIR_OUTGOING &&
handler->m_linkState == DCS_LINKED &&
length == 22U) {
handler->m_pollInactivityTimer.start();
CPollData reply(handler->m_repeater, handler->m_reflector, handler->m_direction, handler->m_yourAddress, handler->m_yourPort);
handler->m_handler->writePoll(reply);
return;
} else if (0==handler->m_reflector.compare(0, LONG_CALLSIGN_LENGTH - 1U, dcsHandler, 0, LONG_CALLSIGN_LENGTH - 1U) &&
handler->m_yourAddress.s_addr == yourAddress.s_addr &&
handler->m_yourPort == yourPort &&
handler->m_myPort == myPort &&
handler->m_direction == DIR_INCOMING &&
handler->m_linkState == DCS_LINKED &&
length == 17U) {
handler->m_pollInactivityTimer.start();
return;
}
}
printf("Unknown incoming DCS poll from %s\n", dcsHandler.c_str());
}
void CDCSHandler::process(CConnectData &connect)
{
CD_TYPE type = connect.getType();
if (type == CT_ACK || type == CT_NAK || type == CT_UNLINK) {
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); ) {
CDCSHandler *dcsHandler = *it;
bool res = dcsHandler->processInt(connect, type);
if (res) {
delete dcsHandler;
it = m_DCSHandlers.erase(it);
} else
it++;
}
return;
}
// else if type == CT_LINK1 or type == CT_LINK2
// someone tried to link directly to a Smart Group!
printf("CDCSHandler::process(CConnectData) type=CT_LINK%c, from repeater=%s\n", (type==CT_LINK1) ? '1' : '2', connect.getRepeater().c_str());
}
void CDCSHandler::link(IReflectorCallback *handler, const std::string &repeater, const std::string &gateway, const in_addr &address)
{
// if the handler is currently unlinking, quit!
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
if (dcsHandler->m_direction == DIR_OUTGOING && dcsHandler->m_destination == handler && dcsHandler->m_linkState != DCS_UNLINKING)
return;
}
CDCSProtocolHandler *protoHandler = m_pool->getHandler();
if (protoHandler == NULL)
return;
CDCSHandler *dcs = new CDCSHandler(handler, gateway, repeater, protoHandler, address, DCS_PORT, DIR_OUTGOING);
if (dcs) {
m_DCSHandlers.push_back(dcs);
CConnectData reply(m_gatewayType, repeater, gateway, CT_LINK1, address, DCS_PORT);
protoHandler->writeConnect(reply);
}
}
void CDCSHandler::unlink(IReflectorCallback *handler, const std::string &callsign, bool exclude)
{
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
if (dcsHandler != NULL) {
bool found = false;
if (exclude) {
if (dcsHandler->m_direction == DIR_OUTGOING && dcsHandler->m_destination == handler && dcsHandler->m_reflector.compare(callsign)) {
printf("Removing outgoing DCS link %s, %s\n", dcsHandler->m_repeater.c_str(), dcsHandler->m_reflector.c_str());
if (dcsHandler->m_linkState == DCS_LINKING || dcsHandler->m_linkState == DCS_LINKED) {
CConnectData connect(dcsHandler->m_repeater, dcsHandler->m_reflector, CT_UNLINK, dcsHandler->m_yourAddress, dcsHandler->m_yourPort);
dcsHandler->m_handler->writeConnect(connect);
dcsHandler->m_linkState = DCS_UNLINKING;
dcsHandler->m_tryTimer.start(1U);
dcsHandler->m_tryCount = 0U;
}
found = true;
}
} else {
if (dcsHandler->m_destination == handler && 0==dcsHandler->m_reflector.compare(callsign)) {
printf("Removing DCS link %s, %s\n", dcsHandler->m_repeater.c_str(), dcsHandler->m_reflector.c_str());
if (dcsHandler->m_linkState == DCS_LINKING || dcsHandler->m_linkState == DCS_LINKED) {
CConnectData connect(dcsHandler->m_repeater, dcsHandler->m_reflector, CT_UNLINK, dcsHandler->m_yourAddress, dcsHandler->m_yourPort);
dcsHandler->m_handler->writeConnect(connect);
dcsHandler->m_linkState = DCS_UNLINKING;
dcsHandler->m_tryTimer.start(1U);
dcsHandler->m_tryCount = 0U;
}
found = true;
}
}
// If an active link with incoming traffic, send an EOT to the repeater
if (found) {
if (dcsHandler->m_dcsId != 0x00U) {
unsigned int seq = dcsHandler->m_dcsSeq + 1U;
if (seq == 21U)
seq = 0U;
CAMBEData data;
data.setData(END_PATTERN_BYTES, DV_FRAME_LENGTH_BYTES);
data.setSeq(seq);
data.setEnd(true);
data.setId(dcsHandler->m_dcsId);
dcsHandler->m_destination->process(data, dcsHandler->m_direction, AS_DCS);
}
m_stateChange = true;
}
}
}
}
void CDCSHandler::unlink(CDCSHandler *dcsHandler)
{
if (dcsHandler != NULL) {
if (dcsHandler->m_repeater.size()) {
printf("Unlinking from DCS dcsHandler %s\n", dcsHandler->m_reflector.c_str());
CConnectData connect(dcsHandler->m_repeater, dcsHandler->m_reflector, CT_UNLINK, dcsHandler->m_yourAddress, dcsHandler->m_yourPort);
dcsHandler->m_handler->writeConnect(connect);
dcsHandler->m_linkState = DCS_UNLINKING;
dcsHandler->m_tryTimer.start(1U);
dcsHandler->m_tryCount = 0U;
}
}
}
void CDCSHandler::unlink()
{
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler* dcsHandler = *it;
CDCSHandler::unlink(dcsHandler);
}
}
void CDCSHandler::writeHeader(IReflectorCallback *handler, CHeaderData &header, DIRECTION direction)
{
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
dcsHandler->writeHeaderInt(handler, header, direction);
}
}
void CDCSHandler::writeAMBE(IReflectorCallback *handler, CAMBEData &data, DIRECTION direction)
{
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
dcsHandler->writeAMBEInt(handler, data, direction);
}
}
void CDCSHandler::gatewayUpdate(const std::string &dcsHandler, const std::string &address)
{
std::string gateway = dcsHandler;
gateway.resize(LONG_CALLSIGN_LENGTH - 1U, ' ');
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
if (0 == dcsHandler->m_reflector.compare(0, LONG_CALLSIGN_LENGTH - 1U, gateway)) {
if (address.size()) {
// A new address, change the value
printf("Changing IP address of DCS gateway or dcsHandler %s to %s\n", dcsHandler->m_reflector.c_str(), address.c_str());
dcsHandler->m_yourAddress.s_addr = ::inet_addr(address.c_str());
} else {
printf("IP address for DCS gateway or dcsHandler %s has been removed\n", dcsHandler->m_reflector.c_str());
// No address, this probably shouldn't happen....
if (dcsHandler->m_direction == DIR_OUTGOING && dcsHandler->m_destination != NULL)
dcsHandler->m_destination->linkFailed(DP_DCS, dcsHandler->m_reflector, false);
m_stateChange = true;
delete dcsHandler;
it = m_DCSHandlers.erase(it);
it--;
}
}
}
}
void CDCSHandler::clock(unsigned int ms)
{
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); ) {
CDCSHandler *handler = *it;
bool ret = handler->clockInt(ms);
if (ret) {
delete handler;
it = m_DCSHandlers.erase(it);
} else
it++;
}
}
void CDCSHandler::finalise()
{
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); ) {
CDCSHandler *handler = *it;
delete handler;
it = m_DCSHandlers.erase(it);
}
}
void CDCSHandler::processInt(CAMBEData &data)
{
// Make a copy of the AMBE data so that any changes made here don't modify the original
CAMBEData temp(data);
unsigned int id = temp.getId();
CHeaderData& header = temp.getHeader();
unsigned int seqNo = temp.getSeq();
std::string my = header.getMyCall1();
std::string rpt2 = header.getRptCall2();
if (m_whiteList != NULL) {
bool res = m_whiteList->isInList(my);
if (!res) {
printf("%s rejected from DCS as not found in the white list\n", my.c_str());
m_dcsId = 0x00U;
return;
}
}
if (m_blackList != NULL) {
bool res = m_blackList->isInList(my);
if (res) {
printf("%s rejected from DCS as found in the black list\n", my.c_str());
m_dcsId = 0x00U;
return;
}
}
if (m_linkState != DCS_LINKED)
return;
switch (m_direction) {
case DIR_OUTGOING:
if (m_reflector.compare(rpt2))
return;
if (m_dcsId == 0x00U && seqNo != 0U)
return;
if (m_dcsId == 0x00U) { // && seqNo == 0U) {
m_dcsId = id;
m_dcsSeq = 0x00U;
m_inactivityTimer.start();
header.setCQCQCQ();
header.setFlags(0x00U, 0x00U, 0x00U);
m_destination->process(header, m_direction, AS_DCS);
}
if (id == m_dcsId) {
m_pollInactivityTimer.start();
m_inactivityTimer.start();
m_dcsSeq = seqNo;
if (m_dcsSeq == 0U) {
// Send the header every 21 frames
header.setCQCQCQ();
header.setFlags(0x00U, 0x00U, 0x00U);
m_destination->process(header, m_direction, AS_DUP);
}
m_destination->process(temp, m_direction, AS_DCS);
if (temp.isEnd()) {
m_dcsId = 0x00U;
m_dcsSeq = 0x00U;
m_inactivityTimer.stop();
}
}
break;
case DIR_INCOMING:
if (m_repeater.compare(rpt2))
return;
if (m_dcsId == 0x00U && seqNo != 0U)
return;
if (m_dcsId == 0x00U) { // && seqNo == 0U) {
m_dcsId = id;
m_dcsSeq = 0x00U;
m_inactivityTimer.start();
header.setCQCQCQ();
header.setFlags(0x00U, 0x00U, 0x00U);
m_destination->process(header, m_direction, AS_DCS);
}
if (id == m_dcsId) {
m_pollInactivityTimer.start();
m_inactivityTimer.start();
m_dcsSeq = seqNo;
if (m_dcsSeq == 0U) {
// Send the header every 21 frames
header.setCQCQCQ();
header.setFlags(0x00U, 0x00U, 0x00U);
m_destination->process(header, m_direction, AS_DUP);
}
m_destination->process(temp, m_direction, AS_DCS);
if (temp.isEnd()) {
m_dcsId = 0x00U;
m_dcsSeq = 0x00U;
m_inactivityTimer.stop();
}
}
break;
}
}
bool CDCSHandler::processInt(CConnectData &connect, CD_TYPE type)
{
in_addr yourAddress = connect.getYourAddress();
unsigned int yourPort = connect.getYourPort();
unsigned int myPort = connect.getMyPort();
std::string repeater = connect.getRepeater();
if (m_yourAddress.s_addr != yourAddress.s_addr || m_yourPort != yourPort || m_myPort != myPort)
return false;
switch (type) {
case CT_ACK:
if (m_repeater.compare(repeater))
return false;
if (m_linkState == DCS_LINKING) {
printf("DCS ACK message received from %s\n", m_reflector.c_str());
if (m_direction == DIR_OUTGOING && m_destination != NULL)
m_destination->linkUp(DP_DCS, m_reflector);
m_tryTimer.stop();
m_stateChange = true;
m_linkState = DCS_LINKED;
}
return false;
case CT_NAK:
if (m_repeater.compare(repeater))
return false;
if (m_linkState == DCS_LINKING) {
printf("DCS NAK message received from %s\n", m_reflector.c_str());
if (m_direction == DIR_OUTGOING && m_destination != NULL)
m_destination->linkRefused(DP_DCS, m_reflector);
return true;
}
if (m_linkState == DCS_UNLINKING) {
printf("DCS NAK message received from %s\n", m_reflector.c_str());
if (m_direction == DIR_OUTGOING && m_destination != NULL)
m_destination->linkFailed(DP_DCS, m_reflector, false);
return true;
}
return false;
case CT_UNLINK:
if (m_reflector.compare(repeater))
return false;
if (m_linkState == DCS_LINKED) {
printf("DCS disconnect message received from %s\n", m_reflector.c_str());
if (m_direction == DIR_OUTGOING && m_destination != NULL)
m_destination->linkFailed(DP_DCS, m_reflector, false);
m_stateChange = true;
}
return true;
default:
return false;
}
}
bool CDCSHandler::clockInt(unsigned int ms)
{
m_pollInactivityTimer.clock(ms);
m_inactivityTimer.clock(ms);
m_pollTimer.clock(ms);
m_tryTimer.clock(ms);
if (m_pollInactivityTimer.isRunning() && m_pollInactivityTimer.hasExpired()) {
m_pollInactivityTimer.start();
m_stateChange = true;
m_dcsId = 0x00U;
m_dcsSeq = 0x00U;
switch (m_linkState) {
case DCS_LINKING:
printf("DCS link to %s has failed to connect\n", m_reflector.c_str());
break;
case DCS_LINKED:
printf("DCS link to %s has failed (poll inactivity)\n", m_reflector.c_str());
break;
case DCS_UNLINKING:
printf("DCS link to %s has failed to disconnect cleanly\n", m_reflector.c_str());
break;
default:
break;
}
if (m_direction == DIR_OUTGOING) {
bool reconnect = m_destination->linkFailed(DP_DCS, m_reflector, true);
if (reconnect) {
CConnectData reply(m_gatewayType, m_repeater, m_reflector, CT_LINK1, m_yourAddress, m_yourPort);
m_handler->writeConnect(reply);
m_linkState = DCS_LINKING;
m_tryTimer.start(1U);
m_tryCount = 0U;
return false;
}
}
return true;
}
if (m_inactivityTimer.isRunning() && m_inactivityTimer.hasExpired()) {
m_dcsId = 0x00U;
m_dcsSeq = 0x00U;
m_inactivityTimer.stop();
}
if (m_pollTimer.isRunning() && m_pollTimer.hasExpired()) {
m_pollTimer.start();
CPollData poll(m_repeater, m_reflector, m_direction, m_yourAddress, m_yourPort);
m_handler->writePoll(poll);
}
if (m_linkState == DCS_LINKING) {
if (m_tryTimer.isRunning() && m_tryTimer.hasExpired()) {
CConnectData reply(m_gatewayType, m_repeater, m_reflector, CT_LINK1, m_yourAddress, m_yourPort);
m_handler->writeConnect(reply);
unsigned int timeout = calcBackoff();
m_tryTimer.start(timeout);
}
}
if (m_linkState == DCS_UNLINKING) {
if (m_tryTimer.isRunning() && m_tryTimer.hasExpired()) {
CConnectData connect(m_repeater, m_reflector, CT_UNLINK, m_yourAddress, m_yourPort);
m_handler->writeConnect(connect);
unsigned int timeout = calcBackoff();
m_tryTimer.start(timeout);
}
}
return false;
}
void CDCSHandler::writeHeaderInt(IReflectorCallback *handler, CHeaderData& header, DIRECTION direction)
{
if (m_linkState != DCS_LINKED)
return;
// Is it link in the right direction
if (m_direction != direction)
return;
if (m_destination != handler)
return;
// Already in use?
if (m_dcsId != 0x00)
return;
m_seqNo = 0U;
m_myCall1 = header.getMyCall1();
m_myCall2 = header.getMyCall2();
m_yourCall = header.getYourCall();
m_rptCall1 = header.getRptCall1();
m_rptCall2 = header.getRptCall2();
}
void CDCSHandler::writeAMBEInt(IReflectorCallback *handler, CAMBEData &data, DIRECTION direction)
{
if (m_linkState != DCS_LINKED)
return;
// Is it link in the right direction
if (m_direction != direction)
return;
if (m_destination != handler)
return;
// Already in use?
if (m_dcsId != 0x00)
return;
CHeaderData& header = data.getHeader();
header.setMyCall1(m_myCall1);
header.setMyCall2(m_myCall2);
header.setRptCall1(m_rptCall1);
header.setRptCall2(m_rptCall2);
header.setCQCQCQ();
data.setRptSeq(m_seqNo++);
data.setDestination(m_yourAddress, m_yourPort);
m_handler->writeData(data);
}
bool CDCSHandler::stateChange()
{
bool stateChange = m_stateChange;
m_stateChange = false;
return stateChange;
}
void CDCSHandler::writeStatus(FILE *file)
{
for (auto it=m_DCSHandlers.begin(); it!=m_DCSHandlers.end(); it++) {
CDCSHandler *dcsHandler = *it;
struct tm *tm = ::gmtime(&dcsHandler->m_time);
switch (dcsHandler->m_direction) {
case DIR_OUTGOING:
if (dcsHandler->m_linkState == DCS_LINKED) {
fprintf(file, "%04d-%02d-%02d %02d:%02d:%02d: DCS link - Type: Repeater Rptr: %s Refl: %s Dir: Outgoing\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
dcsHandler->m_repeater.c_str(), dcsHandler->m_reflector.c_str());
}
break;
case DIR_INCOMING:
if (dcsHandler->m_linkState == DCS_LINKED) {
fprintf(file, "%04d-%02d-%02d %02d:%02d:%02d: DCS link - Type: Repeater Rptr: %s Refl: %s Dir: Incoming\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
dcsHandler->m_repeater.c_str(), dcsHandler->m_reflector.c_str());
}
break;
}
}
}
unsigned int CDCSHandler::calcBackoff()
{
if (m_tryCount >= 7U) {
m_tryCount++;
return 60U;
}
unsigned int timeout = 1U;
for (unsigned int i = 0U; i < m_tryCount; i++)
timeout *= 2U;
m_tryCount++;
if (timeout > 60U)
return 60U;
else
return timeout;
}