forked from g4klx/MMDVMHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMRSlot.cpp
2256 lines (1788 loc) · 60 KB
/
DMRSlot.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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015-2021,2023 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 "DMRAccessControl.h"
#include "DMRDataHeader.h"
#include "DMRSlotType.h"
#include "DMRShortLC.h"
#include "DMRTrellis.h"
#include "DMRFullLC.h"
#include "BPTC19696.h"
#include "DMRSlot.h"
#include "DMRCSBK.h"
#include "DMREMB.h"
#include "Utils.h"
#include "Sync.h"
#include "CRC.h"
#include "Log.h"
#include <cassert>
#include <ctime>
#include <algorithm>
#include <cstdint>
#include <cmath>
unsigned int CDMRSlot::m_colorCode = 0U;
bool CDMRSlot::m_embeddedLCOnly = false;
bool CDMRSlot::m_dumpTAData = true;
CModem* CDMRSlot::m_modem = NULL;
IDMRNetwork* CDMRSlot::m_network = NULL;
CDisplay* CDMRSlot::m_display = NULL;
bool CDMRSlot::m_duplex = true;
CDMRLookup* CDMRSlot::m_lookup = NULL;
unsigned int CDMRSlot::m_hangCount = 3U * 17U;
DMR_OVCM_TYPES CDMRSlot::m_ovcm = DMR_OVCM_OFF;
CRSSIInterpolator* CDMRSlot::m_rssiMapper = NULL;
unsigned int CDMRSlot::m_jitterTime = 360U;
unsigned int CDMRSlot::m_jitterSlots = 6U;
unsigned char* CDMRSlot::m_idle = NULL;
FLCO CDMRSlot::m_flco1;
unsigned char CDMRSlot::m_id1 = 0U;
ACTIVITY_TYPE CDMRSlot::m_activity1 = ACTIVITY_NONE;
FLCO CDMRSlot::m_flco2;
unsigned char CDMRSlot::m_id2 = 0U;
ACTIVITY_TYPE CDMRSlot::m_activity2 = ACTIVITY_NONE;
const unsigned char TALKER_ID_NONE = 0x00U;
const unsigned char TALKER_ID_HEADER = 0x01U;
const unsigned char TALKER_ID_BLOCK1 = 0x02U;
const unsigned char TALKER_ID_BLOCK2 = 0x04U;
const unsigned char TALKER_ID_BLOCK3 = 0x08U;
const unsigned int NO_HEADERS_SIMPLEX = 8U;
const unsigned int NO_HEADERS_DUPLEX = 3U;
const unsigned int NO_PREAMBLE_CSBK = 15U;
// #define DUMP_DMR
CDMRSlot::CDMRSlot(unsigned int slotNo, unsigned int timeout) :
m_slotNo(slotNo),
m_queue(5000U, "DMR Slot"),
m_rfState(RS_RF_LISTENING),
m_netState(RS_NET_IDLE),
m_rfEmbeddedLC(),
m_rfEmbeddedData(NULL),
m_rfEmbeddedReadN(0U),
m_rfEmbeddedWriteN(1U),
m_rfTalkerId(TALKER_ID_NONE),
m_rfTalkerAlias(slotNo),
m_netEmbeddedLC(),
m_netEmbeddedData(NULL),
m_netEmbeddedReadN(0U),
m_netEmbeddedWriteN(1U),
m_netTalkerId(TALKER_ID_NONE),
m_netTalkerAlias(slotNo),
m_rfLC(NULL),
m_netLC(NULL),
m_rfSeqNo(0U),
m_rfN(0U),
m_lastrfN(0U),
m_netN(0U),
m_networkWatchdog(1000U, 0U, 1500U),
m_rfTimeoutTimer(1000U, timeout),
m_netTimeoutTimer(1000U, timeout),
m_packetTimer(1000U, 0U, 50U),
m_interval(),
m_elapsed(),
m_rfFrames(0U),
m_netFrames(0U),
m_netLost(0U),
m_fec(),
m_rfBits(1U),
m_netBits(1U),
m_rfErrs(0U),
m_netErrs(0U),
m_rfTimeout(false),
m_netTimeout(false),
m_lastFrame(NULL),
m_lastFrameValid(false),
m_rssi(0U),
m_maxRSSI(0U),
m_minRSSI(0U),
m_aveRSSI(0U),
m_rssiCount(0U),
m_enabled(true),
m_fp(NULL)
{
m_lastFrame = new unsigned char[DMR_FRAME_LENGTH_BYTES + 2U];
m_rfEmbeddedData = new CDMREmbeddedData[2U];
m_netEmbeddedData = new CDMREmbeddedData[2U];
m_interval.start();
}
CDMRSlot::~CDMRSlot()
{
delete[] m_rfEmbeddedData;
delete[] m_netEmbeddedData;
delete[] m_lastFrame;
}
bool CDMRSlot::writeModem(unsigned char *data, unsigned int len)
{
assert(data != NULL);
if (!m_enabled)
return false;
if (data[0U] == TAG_LOST && m_rfState == RS_RF_AUDIO) {
std::string src = m_lookup->find(m_rfLC->getSrcId());
std::string dst = m_lookup->find(m_rfLC->getDstId());
FLCO flco = m_rfLC->getFLCO();
if (m_rssi != 0U)
LogMessage("DMR Slot %u, RF voice transmission lost from %s to %s%s, %.1f seconds, BER: %.1f%%, RSSI: -%u/-%u/-%u dBm", m_slotNo, src.c_str(), flco == FLCO_GROUP ? "TG " : "", dst.c_str(), float(m_rfFrames) / 16.667F, float(m_rfErrs * 100U) / float(m_rfBits), m_minRSSI, m_maxRSSI, m_aveRSSI / m_rssiCount);
else
LogMessage("DMR Slot %u, RF voice transmission lost from %s to %s%s, %.1f seconds, BER: %.1f%%", m_slotNo, src.c_str(), flco == FLCO_GROUP ? "TG " : "", dst.c_str(), float(m_rfFrames) / 16.667F, float(m_rfErrs * 100U) / float(m_rfBits));
if (m_rfTimeout) {
writeEndRF();
return false;
} else {
writeEndRF(true);
return true;
}
}
if (data[0U] == TAG_LOST && m_rfState == RS_RF_DATA) {
std::string src = m_lookup->find(m_rfLC->getSrcId());
std::string dst = m_lookup->find(m_rfLC->getDstId());
FLCO flco = m_rfLC->getFLCO();
LogMessage("DMR Slot %u, RF data transmission lost from %s to %s%s", m_slotNo, src.c_str(), flco == FLCO_GROUP ? "TG " : "", dst.c_str());
writeEndRF();
return false;
}
if (data[0U] == TAG_LOST) {
m_rfState = RS_RF_LISTENING;
return false;
}
// Have we got RSSI bytes on the end?
if (len == (DMR_FRAME_LENGTH_BYTES + 4U)) {
uint16_t raw = 0U;
raw |= (data[35U] << 8) & 0xFF00U;
raw |= (data[36U] << 0) & 0x00FFU;
// Convert the raw RSSI to dBm
int rssi = m_rssiMapper->interpolate(raw);
if (rssi != 0)
LogDebug("DMR Slot %u, raw RSSI: %u, reported RSSI: %d dBm", m_slotNo, raw, rssi);
// RSSI is always reported as positive
m_rssi = (rssi >= 0) ? rssi : -rssi;
if (m_rssi > m_minRSSI)
m_minRSSI = m_rssi;
if (m_rssi < m_maxRSSI)
m_maxRSSI = m_rssi;
m_aveRSSI += m_rssi;
m_rssiCount++;
}
bool dataSync = (data[1U] & DMR_SYNC_DATA) == DMR_SYNC_DATA;
bool audioSync = (data[1U] & DMR_SYNC_AUDIO) == DMR_SYNC_AUDIO;
if (dataSync) {
// Get the type from the packet metadata
unsigned char dataType = data[1U] & 0x0FU;
CDMRSlotType slotType;
slotType.setColorCode(m_colorCode);
slotType.setDataType(dataType);
if (dataType == DT_VOICE_LC_HEADER) {
if (m_rfState == RS_RF_AUDIO)
return true;
CDMRFullLC fullLC;
CDMRLC* lc = fullLC.decode(data + 2U, DT_VOICE_LC_HEADER);
if (lc == NULL)
return false;
unsigned int srcId = lc->getSrcId();
unsigned int dstId = lc->getDstId();
FLCO flco = lc->getFLCO();
if (!CDMRAccessControl::validateSrcId(srcId)) {
LogMessage("DMR Slot %u, RF user %u rejected", m_slotNo, srcId);
delete lc;
m_rfState = RS_RF_LISTENING;
return false;
}
if (!CDMRAccessControl::validateTGId(m_slotNo, flco == FLCO_GROUP, dstId)) {
LogMessage("DMR Slot %u, RF user %u rejected for using TG %u", m_slotNo, srcId, dstId);
delete lc;
m_rfState = RS_RF_LISTENING;
return false;
}
if (m_ovcm == DMR_OVCM_TX_ON || m_ovcm == DMR_OVCM_ON)
lc->setOVCM(true);
else if (m_ovcm == DMR_OVCM_FORCE_OFF)
lc->setOVCM(false);
m_rfLC = lc;
// The standby LC data
m_rfEmbeddedLC.setLC(*m_rfLC);
m_rfEmbeddedData[0U].setLC(*m_rfLC);
m_rfEmbeddedData[1U].setLC(*m_rfLC);
// Regenerate the LC data
fullLC.encode(*m_rfLC, data + 2U, DT_VOICE_LC_HEADER);
// Regenerate the Slot Type
slotType.getData(data + 2U);
// Convert the Data Sync to be from the BS or MS as needed
CSync::addDMRDataSync(data + 2U, m_duplex);
data[0U] = TAG_DATA;
data[1U] = 0x00U;
m_rfTimeoutTimer.start();
m_rfTimeout = false;
m_rfFrames = 0U;
m_rfSeqNo = 0U;
m_rfBits = 1U;
m_rfErrs = 0U;
m_rfEmbeddedReadN = 0U;
m_rfEmbeddedWriteN = 1U;
m_rfTalkerId = TALKER_ID_NONE;
m_minRSSI = m_rssi;
m_maxRSSI = m_rssi;
m_aveRSSI = m_rssi;
m_rssiCount = 1U;
if (m_duplex) {
m_queue.clear();
m_modem->writeDMRAbort(m_slotNo);
for (unsigned int i = 0U; i < NO_HEADERS_DUPLEX; i++)
writeQueueRF(data);
}
writeNetworkRF(data, DT_VOICE_LC_HEADER);
m_rfState = RS_RF_AUDIO;
std::string src = m_lookup->find(srcId);
std::string dst = m_lookup->find(dstId);
if (m_netState == RS_NET_IDLE) {
setShortLC(m_slotNo, dstId, flco, ACTIVITY_VOICE);
m_display->writeDMR(m_slotNo, src, flco == FLCO_GROUP, dst, "R");
m_display->writeDMRRSSI(m_slotNo, m_rssi);
}
LogMessage("DMR Slot %u, received RF voice header from %s to %s%s", m_slotNo, src.c_str(), flco == FLCO_GROUP ? "TG " : "", dst.c_str());
return true;
} else if (dataType == DT_VOICE_PI_HEADER) {
if (m_rfState != RS_RF_AUDIO)
return false;
// Regenerate the Slot Type
slotType.getData(data + 2U);
// Convert the Data Sync to be from the BS or MS as needed
CSync::addDMRDataSync(data + 2U, m_duplex);
// Regenerate the payload
CBPTC19696 bptc;
unsigned char payload[12U];
bptc.decode(data + 2U, payload);
bptc.encode(payload, data + 2U);
data[0U] = TAG_DATA;
data[1U] = 0x00U;
if (m_duplex)
writeQueueRF(data);
writeNetworkRF(data, DT_VOICE_PI_HEADER);
return true;
} else if (dataType == DT_TERMINATOR_WITH_LC) {
if (m_rfState != RS_RF_AUDIO)
return false;
// Regenerate the LC data
CDMRFullLC fullLC;
fullLC.encode(*m_rfLC, data + 2U, DT_TERMINATOR_WITH_LC);
// Regenerate the Slot Type
slotType.getData(data + 2U);
// Convert the Data Sync to be from the BS or MS as needed
CSync::addDMRDataSync(data + 2U, m_duplex);
if (!m_rfTimeout) {
data[0U] = TAG_EOT;
data[1U] = 0x00U;
writeNetworkRF(data, DT_TERMINATOR_WITH_LC);
if (m_duplex) {
for (unsigned int i = 0U; i < m_hangCount; i++)
writeQueueRF(data);
}
}
std::string src = m_lookup->find(m_rfLC->getSrcId());
std::string dst = m_lookup->find(m_rfLC->getDstId());
FLCO flco = m_rfLC->getFLCO();
if (m_rssi != 0U)
LogMessage("DMR Slot %u, received RF end of voice transmission from %s to %s%s, %.1f seconds, BER: %.1f%%, RSSI: -%u/-%u/-%u dBm", m_slotNo, src.c_str(), flco == FLCO_GROUP ? "TG " : "", dst.c_str(), float(m_rfFrames) / 16.667F, float(m_rfErrs * 100U) / float(m_rfBits), m_minRSSI, m_maxRSSI, m_aveRSSI / m_rssiCount);
else
LogMessage("DMR Slot %u, received RF end of voice transmission from %s to %s%s, %.1f seconds, BER: %.1f%%", m_slotNo, src.c_str(), flco == FLCO_GROUP ? "TG " : "", dst.c_str(), float(m_rfFrames) / 16.667F, float(m_rfErrs * 100U) / float(m_rfBits));
m_display->writeDMRTA(m_slotNo, NULL, " ");
if (m_rfTimeout) {
writeEndRF();
return false;
} else {
writeEndRF();
return true;
}
} else if (dataType == DT_DATA_HEADER) {
if (m_rfState == RS_RF_DATA)
return true;
CDMRDataHeader dataHeader;
bool valid = dataHeader.put(data + 2U);
if (!valid)
return false;
bool gi = dataHeader.getGI();
unsigned int srcId = dataHeader.getSrcId();
unsigned int dstId = dataHeader.getDstId();
if (!CDMRAccessControl::validateSrcId(srcId)) {
LogMessage("DMR Slot %u, RF user %u rejected", m_slotNo, srcId);
m_rfState = RS_RF_LISTENING;
return false;
}
if (!CDMRAccessControl::validateTGId(m_slotNo, gi, dstId)) {
LogMessage("DMR Slot %u, RF user %u rejected for using TG %u", m_slotNo, srcId, dstId);
m_rfState = RS_RF_LISTENING;
return false;
}
m_rfFrames = dataHeader.getBlocks();
m_rfLC = new CDMRLC(gi ? FLCO_GROUP : FLCO_USER_USER, srcId, dstId);
// Regenerate the data header
dataHeader.get(data + 2U);
// Regenerate the Slot Type
slotType.getData(data + 2U);
// Convert the Data Sync to be from the BS or MS as needed
CSync::addDMRDataSync(data + 2U, m_duplex);
data[0U] = m_rfFrames == 0U ? TAG_EOT : TAG_DATA;
data[1U] = 0x00U;
if (m_duplex)
writeQueueRF(data);
writeNetworkRF(data, DT_DATA_HEADER);
m_rfState = RS_RF_DATA;
std::string src = m_lookup->find(srcId);
std::string dst = m_lookup->find(dstId);
if (m_netState == RS_NET_IDLE) {
setShortLC(m_slotNo, dstId, gi ? FLCO_GROUP : FLCO_USER_USER, ACTIVITY_DATA);
m_display->writeDMR(m_slotNo, src, gi, dst, "R");
m_display->writeDMRRSSI(m_slotNo, m_rssi);
}
LogMessage("DMR Slot %u, received RF data header from %s to %s%s, %u blocks", m_slotNo, src.c_str(), gi ? "TG ": "", dst.c_str(), m_rfFrames);
if (m_rfFrames == 0U) {
LogMessage("DMR Slot %u, ended RF data transmission from %s to %s%s", m_slotNo, src.c_str(), gi ? "TG " : "", dst.c_str());
writeEndRF();
}
return true;
} else if (dataType == DT_CSBK) {
CDMRCSBK csbk;
bool valid = csbk.put(data + 2U);
if (!valid)
return false;
CSBKO csbko = csbk.getCSBKO();
if (csbko == CSBKO_BSDWNACT)
return false;
// set the OVCM bit for the supported csbk
if (m_ovcm == DMR_OVCM_TX_ON || m_ovcm == DMR_OVCM_ON)
csbk.setOVCM(true);
else if (m_ovcm == DMR_OVCM_FORCE_OFF)
csbk.setOVCM(false);
bool gi = csbk.getGI();
unsigned int srcId = csbk.getSrcId();
unsigned int dstId = csbk.getDstId();
if (srcId != 0U || dstId != 0U) {
if (!CDMRAccessControl::validateSrcId(srcId)) {
LogMessage("DMR Slot %u, RF user %u rejected", m_slotNo, srcId);
m_rfState = RS_RF_LISTENING;
return false;
}
if (!CDMRAccessControl::validateTGId(m_slotNo, gi, dstId)) {
LogMessage("DMR Slot %u, RF user %u rejected for using TG %u", m_slotNo, srcId, dstId);
m_rfState = RS_RF_LISTENING;
return false;
}
}
// Regenerate the CSBK data
csbk.get(data + 2U);
// Regenerate the Slot Type
slotType.getData(data + 2U);
// Convert the Data Sync to be from the BS or MS as needed
CSync::addDMRDataSync(data + 2U, m_duplex);
data[0U] = TAG_DATA;
data[1U] = 0x00U;
if (m_duplex)
writeQueueRF(data);
writeNetworkRF(data, DT_CSBK, gi ? FLCO_GROUP : FLCO_USER_USER, srcId, dstId);
std::string src = m_lookup->find(srcId);
std::string dst = m_lookup->find(dstId);
switch (csbko) {
case CSBKO_UUVREQ:
LogMessage("DMR Slot %u, received RF Unit to Unit Voice Service Request CSBK from %s to %s%s", m_slotNo, src.c_str(), gi ? "TG ": "", dst.c_str());
break;
case CSBKO_UUANSRSP:
LogMessage("DMR Slot %u, received RF Unit to Unit Voice Service Answer Response CSBK from %s to %s%s", m_slotNo, src.c_str(), gi ? "TG ": "", dst.c_str());
break;
case CSBKO_NACKRSP:
LogMessage("DMR Slot %u, received RF Negative Acknowledgment Response CSBK from %s to %s%s", m_slotNo, src.c_str(), gi ? "TG ": "", dst.c_str());
break;
case CSBKO_PRECCSBK:
LogMessage("DMR Slot %u, received RF %s Preamble CSBK (%u to follow) from %s to %s%s", m_slotNo, csbk.getDataContent() ? "Data" : "CSBK", csbk.getCBF(), src.c_str(), gi ? "TG ": "", dst.c_str());
break;
case CSBKO_CALL_ALERT:
LogMessage("DMR Slot %u, received RF Call Alert CSBK from %s to %s%s", m_slotNo, src.c_str(), gi ? "TG " : "", dst.c_str());
break;
case CSBKO_CALL_ALERT_ACK:
LogMessage("DMR Slot %u, received RF Call Alert Ack CSBK from %s to %s%s", m_slotNo, src.c_str(), gi ? "TG " : "", dst.c_str());
break;
case CSBKO_RADIO_CHECK:
LogMessage("DMR Slot %u, received RF Radio Check %s CSBK from %s to %s%s", m_slotNo, /* TBD */ 1 ? "Req" : "Ack", src.c_str(), gi ? "TG " : "", dst.c_str());
break;
case CSBKO_CALL_EMERGENCY:
LogMessage("DMR Slot %u, received RF Call Emergency CSBK from %s to %s%s", m_slotNo, src.c_str(), gi ? "TG " : "", dst.c_str());
break;
default:
LogWarning("DMR Slot %u, unhandled RF CSBK type - 0x%02X", m_slotNo, csbko);
break;
}
// If data preamble, signal its existence
if (m_netState == RS_NET_IDLE && csbko == CSBKO_PRECCSBK && csbk.getDataContent()) {
setShortLC(m_slotNo, dstId, gi ? FLCO_GROUP : FLCO_USER_USER, ACTIVITY_DATA);
m_display->writeDMR(m_slotNo, src, gi, dst, "R");
m_display->writeDMRRSSI(m_slotNo, m_rssi);
}
return true;
} else if (dataType == DT_RATE_12_DATA || dataType == DT_RATE_34_DATA || dataType == DT_RATE_1_DATA) {
if (m_rfState != RS_RF_DATA || m_rfFrames == 0U)
return false;
char title[80U];
// Regenerate the rate 1/2 payload
if (dataType == DT_RATE_12_DATA) {
CBPTC19696 bptc;
unsigned char payload[12U];
bptc.decode(data + 2U, payload);
::sprintf(title, "DMR Slot %u, Data 1/2", m_slotNo);
CUtils::dump(1U, title, payload, 12U);
bptc.encode(payload, data + 2U);
} else if (dataType == DT_RATE_34_DATA) {
CDMRTrellis trellis;
unsigned char payload[18U];
bool ret = trellis.decode(data + 2U, payload);
if (ret) {
::sprintf(title, "DMR Slot %u, Data 3/4", m_slotNo);
CUtils::dump(1U, title, payload, 18U);
trellis.encode(payload, data + 2U);
} else {
LogMessage("DMR Slot %u, unfixable RF rate 3/4 data", m_slotNo);
CUtils::dump(1U, "Data", data + 2U, DMR_FRAME_LENGTH_BYTES);
}
} else {
::sprintf(title, "DMR Slot %u, Data 1/1", m_slotNo);
CUtils::dump(1U, title, data + 2U, 24U);
}
// Regenerate the Slot Type
slotType.getData(data + 2U);
// Convert the Data Sync to be from the BS or MS as needed
CSync::addDMRDataSync(data + 2U, m_duplex);
m_rfFrames--;
data[0U] = m_rfFrames == 0U ? TAG_EOT : TAG_DATA;
data[1U] = 0x00U;
if (m_duplex)
writeQueueRF(data);
writeNetworkRF(data, dataType);
if (m_rfFrames == 0U) {
LogMessage("DMR Slot %u, ended RF data transmission", m_slotNo);
writeEndRF();
}
return true;
}
} else if (audioSync) {
if (m_rfState == RS_RF_AUDIO) {
m_lastrfN = 0;
// Convert the Audio Sync to be from the BS or MS as needed
CSync::addDMRAudioSync(data + 2U, m_duplex);
unsigned int errors = 0U;
unsigned char fid = m_rfLC->getFID();
if (fid == FID_ETSI || fid == FID_DMRA) {
errors = m_fec.regenerateDMR(data + 2U);
LogDebug("DMR Slot %u, audio sequence no. 0, errs: %u/141 (%.1f%%)", m_slotNo, errors, float(errors) / 1.41F);
m_display->writeDMRBER(m_slotNo, float(errors) / 1.41F);
m_rfErrs += errors;
}
m_rfBits += 141U;
m_rfFrames++;
m_rfEmbeddedReadN = (m_rfEmbeddedReadN + 1U) % 2U;
m_rfEmbeddedWriteN = (m_rfEmbeddedWriteN + 1U) % 2U;
m_rfEmbeddedData[m_rfEmbeddedWriteN].reset();
m_display->writeDMRRSSI(m_slotNo, m_rssi);
if (!m_rfTimeout) {
data[0U] = TAG_DATA;
data[1U] = 0x00U;
if (m_duplex)
writeQueueRF(data);
writeNetworkRF(data, DT_VOICE_SYNC, errors);
return true;
}
return false;
} else if (m_rfState == RS_RF_LISTENING) {
m_rfEmbeddedLC.reset();
m_rfState = RS_RF_LATE_ENTRY;
return false;
}
} else {
if (m_rfState == RS_RF_AUDIO) {
m_rfN = data[1U] & 0x0FU;
if (m_rfN > 5U)
return false;
if (m_rfN == m_lastrfN)
return false;
if (m_rfN != (m_lastrfN + 1U))
return false;
m_lastrfN = m_rfN;
unsigned int errors = 0U;
unsigned char fid = m_rfLC->getFID();
if (fid == FID_ETSI || fid == FID_DMRA) {
errors = m_fec.regenerateDMR(data + 2U);
LogDebug("DMR Slot %u, audio sequence no. %u, errs: %u/141 (%.1f%%)", m_slotNo, m_rfN, errors, float(errors) / 1.41F);
m_display->writeDMRBER(m_slotNo, float(errors) / 1.41F);
m_rfErrs += errors;
}
m_rfBits += 141U;
m_rfFrames++;
// Get the LCSS from the EMB
CDMREMB emb;
emb.putData(data + 2U);
unsigned char lcss = emb.getLCSS();
// Dump any interesting Embedded Data
bool ret = m_rfEmbeddedData[m_rfEmbeddedWriteN].addData(data + 2U, lcss);
if (ret) {
FLCO flco = m_rfEmbeddedData[m_rfEmbeddedWriteN].getFLCO();
unsigned char data[9U];
m_rfEmbeddedData[m_rfEmbeddedWriteN].getRawData(data);
char text[80U];
switch (flco) {
case FLCO_GROUP:
case FLCO_USER_USER:
// ::sprintf(text, "DMR Slot %u, Embedded LC", m_slotNo);
// CUtils::dump(1U, text, data, 9U);
break;
case FLCO_GPS_INFO:
if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded GPS Info", m_slotNo);
CUtils::dump(1U, text, data, 9U);
logGPSPosition(data);
}
if (m_network != NULL)
m_network->writeRadioPosition(m_rfLC->getSrcId(), data);
break;
case FLCO_TALKER_ALIAS_HEADER:
if (m_network != NULL)
m_network->writeTalkerAlias(m_rfLC->getSrcId(), 0U, data);
if (!(m_rfTalkerId & TALKER_ID_HEADER)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(0U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");
if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Header", m_slotNo);
CUtils::dump(1U, text, data, 9U);
}
m_rfTalkerId |= TALKER_ID_HEADER;
}
break;
case FLCO_TALKER_ALIAS_BLOCK1:
if (m_network != NULL)
m_network->writeTalkerAlias(m_rfLC->getSrcId(), 1U, data);
if (!(m_rfTalkerId & TALKER_ID_BLOCK1)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(1U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");
if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 1", m_slotNo);
CUtils::dump(1U, text, data, 9U);
}
m_rfTalkerId |= TALKER_ID_BLOCK1;
}
break;
case FLCO_TALKER_ALIAS_BLOCK2:
if (m_network != NULL)
m_network->writeTalkerAlias(m_rfLC->getSrcId(), 2U, data);
if (!(m_rfTalkerId & TALKER_ID_BLOCK2)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(2U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");
if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 2", m_slotNo);
CUtils::dump(1U, text, data, 9U);
}
m_rfTalkerId |= TALKER_ID_BLOCK2;
}
break;
case FLCO_TALKER_ALIAS_BLOCK3:
if (m_network != NULL)
m_network->writeTalkerAlias(m_rfLC->getSrcId(), 3U, data);
if (!(m_rfTalkerId & TALKER_ID_BLOCK3)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(3U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");
if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 3", m_slotNo);
CUtils::dump(1U, text, data, 9U);
}
m_rfTalkerId |= TALKER_ID_BLOCK3;
}
break;
default:
::sprintf(text, "DMR Slot %u, Unknown Embedded Data", m_slotNo);
CUtils::dump(1U, text, data, 9U);
break;
}
}
// Regenerate the previous super blocks Embedded Data or substitude the LC for it
if (m_rfEmbeddedData[m_rfEmbeddedReadN].isValid())
lcss = m_rfEmbeddedData[m_rfEmbeddedReadN].getData(data + 2U, m_rfN);
else
lcss = m_rfEmbeddedLC.getData(data + 2U, m_rfN);
// Regenerate the EMB
emb.setColorCode(m_colorCode);
emb.setLCSS(lcss);
emb.getData(data + 2U);
if (!m_rfTimeout) {
data[0U] = TAG_DATA;
data[1U] = 0x00U;
writeNetworkRF(data, DT_VOICE, errors);
if (m_embeddedLCOnly) {
// Only send the previously received LC
lcss = m_rfEmbeddedLC.getData(data + 2U, m_rfN);
// Regenerate the EMB
emb.setColorCode(m_colorCode);
emb.setLCSS(lcss);
emb.getData(data + 2U);
}
if (m_duplex)
writeQueueRF(data);
return true;
}
return false;
} else if (m_rfState == RS_RF_LATE_ENTRY) {
CDMREMB emb;
emb.putData(data + 2U);
// If we haven't received an LC yet, then be strict on the color code
unsigned char colorCode = emb.getColorCode();
if (colorCode != m_colorCode)
return false;
m_rfEmbeddedLC.addData(data + 2U, emb.getLCSS());
CDMRLC* lc = m_rfEmbeddedLC.getLC();
if (lc != NULL) {
unsigned int srcId = lc->getSrcId();
unsigned int dstId = lc->getDstId();
FLCO flco = lc->getFLCO();
if (!CDMRAccessControl::validateSrcId(srcId)) {
LogMessage("DMR Slot %u, RF user %u rejected", m_slotNo, srcId);
delete lc;
m_rfState = RS_RF_LISTENING;
return false;
}
if (!CDMRAccessControl::validateTGId(m_slotNo, flco == FLCO_GROUP, dstId)) {
LogMessage("DMR Slot %u, RF user %u rejected for using TG %u", m_slotNo, srcId, dstId);
delete lc;
m_rfState = RS_RF_LISTENING;
return false;
}
if (m_ovcm == DMR_OVCM_TX_ON || m_ovcm == DMR_OVCM_ON)
lc->setOVCM(true);
else if (m_ovcm == DMR_OVCM_FORCE_OFF)
lc->setOVCM(false);
m_rfLC = lc;
// The standby LC data
m_rfEmbeddedLC.setLC(*m_rfLC);
m_rfEmbeddedData[0U].setLC(*m_rfLC);
m_rfEmbeddedData[1U].setLC(*m_rfLC);
// Create a dummy start frame to replace the received frame
unsigned char start[DMR_FRAME_LENGTH_BYTES + 2U];
CSync::addDMRDataSync(start + 2U, m_duplex);
CDMRFullLC fullLC;
fullLC.encode(*m_rfLC, start + 2U, DT_VOICE_LC_HEADER);
CDMRSlotType slotType;
slotType.setColorCode(m_colorCode);
slotType.setDataType(DT_VOICE_LC_HEADER);
slotType.getData(start + 2U);
start[0U] = TAG_DATA;
start[1U] = 0x00U;
m_rfTimeoutTimer.start();
m_rfTimeout = false;
m_rfFrames = 0U;
m_rfSeqNo = 0U;
m_rfBits = 1U;
m_rfErrs = 0U;
m_rfEmbeddedReadN = 0U;
m_rfEmbeddedWriteN = 1U;
m_rfTalkerId = TALKER_ID_NONE;
m_minRSSI = m_rssi;
m_maxRSSI = m_rssi;
m_aveRSSI = m_rssi;
m_rssiCount = 1U;
if (m_duplex) {
m_queue.clear();
m_modem->writeDMRAbort(m_slotNo);
for (unsigned int i = 0U; i < NO_HEADERS_DUPLEX; i++)
writeQueueRF(start);
}
writeNetworkRF(start, DT_VOICE_LC_HEADER);
m_rfN = data[1U] & 0x0FU;
if (m_rfN > 5U)
return false;
if (m_rfN == m_lastrfN)
return false;
m_lastrfN = m_rfN;
// Regenerate the EMB
emb.getData(data + 2U);
// Send the original audio frame out
unsigned int errors = 0U;
unsigned char fid = m_rfLC->getFID();
if (fid == FID_ETSI || fid == FID_DMRA) {
errors = m_fec.regenerateDMR(data + 2U);
LogDebug("DMR Slot %u, audio sequence no. %u, errs: %u/141 (%.1f%%)", m_slotNo, m_rfN, errors, float(errors) / 1.41F);
m_rfErrs += errors;
}
m_rfBits += 141U;
m_rfFrames++;
data[0U] = TAG_DATA;
data[1U] = 0x00U;
if (m_duplex)
writeQueueRF(data);
writeNetworkRF(data, DT_VOICE, errors);
m_rfState = RS_RF_AUDIO;
std::string src = m_lookup->find(srcId);
std::string dst = m_lookup->find(dstId);
if (m_netState == RS_NET_IDLE) {
setShortLC(m_slotNo, dstId, flco, ACTIVITY_VOICE);
m_display->writeDMR(m_slotNo, src, flco == FLCO_GROUP, dst, "R");
m_display->writeDMRRSSI(m_slotNo, m_rssi);
m_display->writeDMRBER(m_slotNo, float(errors) / 1.41F);
}
LogMessage("DMR Slot %u, received RF late entry from %s to %s%s", m_slotNo, src.c_str(), flco == FLCO_GROUP ? "TG " : "", dst.c_str());
return true;
}
}
}
return false;
}
unsigned int CDMRSlot::readModem(unsigned char* data)
{
assert(data != NULL);
if (m_queue.isEmpty())
return 0U;
unsigned char len = 0U;
m_queue.getData(&len, 1U);
m_queue.getData(data, len);
return len;
}
void CDMRSlot::writeEndRF(bool writeEnd)
{
m_rfState = RS_RF_LISTENING;
if (m_netState == RS_NET_IDLE) {
setShortLC(m_slotNo, 0U);
m_display->clearDMR(m_slotNo);
}
if (writeEnd) {
if (m_netState == RS_NET_IDLE && m_duplex && !m_rfTimeout) {
// Create a dummy start end frame
unsigned char data[DMR_FRAME_LENGTH_BYTES + 2U];
CSync::addDMRDataSync(data + 2U, m_duplex);
CDMRFullLC fullLC;
fullLC.encode(*m_rfLC, data + 2U, DT_TERMINATOR_WITH_LC);
CDMRSlotType slotType;
slotType.setColorCode(m_colorCode);
slotType.setDataType(DT_TERMINATOR_WITH_LC);
slotType.getData(data + 2U);
data[0U] = TAG_EOT;
data[1U] = 0x00U;
for (unsigned int i = 0U; i < m_hangCount; i++)
writeQueueRF(data);
}
}
m_rfTimeoutTimer.stop();
m_rfTimeout = false;
m_rfFrames = 0U;
m_rfErrs = 0U;
m_rfBits = 1U;
m_rfSeqNo = 0U;
m_rfN = 0U;
delete m_rfLC;
m_rfLC = NULL;
}
void CDMRSlot::writeEndNet(bool writeEnd)
{
m_netState = RS_NET_IDLE;
setShortLC(m_slotNo, 0U);