-
Notifications
You must be signed in to change notification settings - Fork 240
/
DMRDecoderState.java
1234 lines (1138 loc) · 52.5 KB
/
DMRDecoderState.java
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) 2014-2023 Dennis Sheirer
*
* 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 3 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, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.module.decode.dmr;
import com.google.common.eventbus.Subscribe;
import io.github.dsheirer.channel.state.ChangeChannelTimeoutEvent;
import io.github.dsheirer.channel.state.DecoderStateEvent;
import io.github.dsheirer.channel.state.DecoderStateEvent.Event;
import io.github.dsheirer.channel.state.State;
import io.github.dsheirer.channel.state.TimeslotDecoderState;
import io.github.dsheirer.controller.channel.Channel;
import io.github.dsheirer.controller.channel.Channel.ChannelType;
import io.github.dsheirer.controller.channel.ChannelConfigurationChangeNotification;
import io.github.dsheirer.identifier.Form;
import io.github.dsheirer.identifier.Identifier;
import io.github.dsheirer.identifier.IdentifierClass;
import io.github.dsheirer.identifier.IdentifierCollection;
import io.github.dsheirer.identifier.MutableIdentifierCollection;
import io.github.dsheirer.identifier.Role;
import io.github.dsheirer.identifier.integer.IntegerIdentifier;
import io.github.dsheirer.message.IMessage;
import io.github.dsheirer.module.decode.DecoderType;
import io.github.dsheirer.module.decode.dmr.channel.DMRChannel;
import io.github.dsheirer.module.decode.dmr.event.DMRDecodeEvent;
import io.github.dsheirer.module.decode.dmr.identifier.DMRTalkgroup;
import io.github.dsheirer.module.decode.dmr.message.DMRMessage;
import io.github.dsheirer.module.decode.dmr.message.data.DataMessage;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.CSBKMessage;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.motorola.CapacityMaxAloha;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.motorola.CapacityPlusNeighbors;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.motorola.CapacityPlusSiteStatus;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.motorola.ConnectPlusDataChannelGrant;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.motorola.ConnectPlusVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.Aloha;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.Protect;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.acknowledge.Acknowledge;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.ahoy.Ahoy;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.ahoy.ServiceRadioCheck;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.ahoy.StunReviveKill;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.announcement.Announcement;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.announcement.VoteNowAdvice;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.standard.grant.ChannelGrant;
import io.github.dsheirer.module.decode.dmr.message.data.header.HeaderMessage;
import io.github.dsheirer.module.decode.dmr.message.data.lc.LCMessage;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.GPSInformation;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.GroupVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.TalkerAliasComplete;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.UnitToUnitVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.hytera.HyteraGroupVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.hytera.HyteraUnitToUnitVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.motorola.CapacityPlusEncryptedVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.motorola.CapacityPlusWideAreaVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.lc.full.motorola.MotorolaGroupVoiceChannelUser;
import io.github.dsheirer.module.decode.dmr.message.data.lc.shorty.CapacityPlusRestChannel;
import io.github.dsheirer.module.decode.dmr.message.data.packet.DMRPacketMessage;
import io.github.dsheirer.module.decode.dmr.message.data.packet.UDTShortMessageService;
import io.github.dsheirer.module.decode.dmr.message.data.terminator.Terminator;
import io.github.dsheirer.module.decode.dmr.message.type.ServiceOptions;
import io.github.dsheirer.module.decode.dmr.message.voice.VoiceMessage;
import io.github.dsheirer.module.decode.event.DecodeEvent;
import io.github.dsheirer.module.decode.event.DecodeEventType;
import io.github.dsheirer.module.decode.event.PlottableDecodeEvent;
import io.github.dsheirer.module.decode.ip.hytera.sds.HyteraUnknownPacket;
import io.github.dsheirer.module.decode.ip.hytera.sms.HyteraSmsPacket;
import io.github.dsheirer.protocol.Protocol;
import io.github.dsheirer.source.tuner.channel.rotation.AddChannelRotationActiveStateRequest;
import io.github.dsheirer.util.PacketUtil;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Decoder state for an DMR channel. Maintains the call/data/idle state of the channel and produces events by
* monitoring the decoded message stream.
*/
public class DMRDecoderState extends TimeslotDecoderState
{
private final static Logger mLog = LoggerFactory.getLogger(DMRDecoderState.class);
private static final long MAX_VALID_CALL_DURATION_MS = 30000;
private Channel mChannel;
private DMRNetworkConfigurationMonitor mNetworkConfigurationMonitor;
private DMRTrafficChannelManager mTrafficChannelManager;
private DecodeEvent mCurrentCallEvent;
private long mCurrentFrequency;
private boolean mIgnoreCRCChecksums;
private Map<Integer,DecodeEvent> mDetectedCallEventsMap = new TreeMap<>();
private static final AddChannelRotationActiveStateRequest CAPACITY_PLUS_ACTIVE_STATE_REQUEST =
new AddChannelRotationActiveStateRequest(State.ACTIVE);
/**
* Constructs an DMR decoder state with an optional traffic channel manager.
* @param channel with configuration details
* @param timeslot for this decoder state (1 or 2)
* @param trafficChannelManager for handling traffic channel grants.
*/
public DMRDecoderState(Channel channel, int timeslot, DMRTrafficChannelManager trafficChannelManager)
{
super(timeslot);
mChannel = channel;
mTrafficChannelManager = trafficChannelManager;
//The decoder state passes all messages to the network configuration monitor, so we only construct
//the monitor for timeslot 1.
if(timeslot == 1)
{
mNetworkConfigurationMonitor = new DMRNetworkConfigurationMonitor(channel);
}
//For RAS protected systems, allows user to ignore CRC checksums and still decode the system
if(channel.getDecodeConfiguration() instanceof DecodeConfigDMR)
{
mIgnoreCRCChecksums = ((DecodeConfigDMR)channel.getDecodeConfiguration()).getIgnoreCRCChecksums();
}
}
/**
* Indicates if this decoder state has an (optional) traffic channel manager.
*/
private boolean hasTrafficChannelManager()
{
return mTrafficChannelManager != null;
}
/**
* Processes channel configuration change notifications received over the processing chain event bus. This is
* primarily used for Capacity+ systems when the standard channel is converted to a traffic channel. In response,
* we nullify the traffic channel manager to ensure the traffic channel no longer behaves as a standard channel
* regarding channel conversions and allocations.
*
* @param notification of channel configuration change
*/
@Subscribe
public void channelChanged(ChannelConfigurationChangeNotification notification)
{
if(notification.getChannel().isTrafficChannel())
{
mTrafficChannelManager = null;
}
}
/**
* Identifies the decoder type
*/
@Override
public DecoderType getDecoderType()
{
return DecoderType.DMR;
}
/**
* Performs a full reset to prepare this object for reuse on a new channel
*/
@Override
public void reset()
{
super.reset();
resetState();
mCurrentFrequency = 0;
mDetectedCallEventsMap.clear();
}
/**
* Resets any temporal state details
*/
protected void resetState()
{
super.resetState();
closeCurrentCallEvent(System.currentTimeMillis());
}
/**
* Primary message processing method.
*/
@Override
public void receive(IMessage message)
{
if(message.getTimeslot() == getTimeslot())
{
if(message instanceof VoiceMessage)
{
processVoice((VoiceMessage)message);
}
else if(message instanceof DataMessage)
{
processData((DataMessage)message);
}
else if((message.isValid() || mIgnoreCRCChecksums) && message instanceof LCMessage)
{
processLinkControl((LCMessage)message, false);
}
else if(message.isValid() && message instanceof DMRPacketMessage)
{
processPacket((DMRPacketMessage)message);
}
else if(message instanceof UDTShortMessageService sms)
{
processSMS(sms);
}
else if(message instanceof DMRMessage)
{
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.ACTIVE, getTimeslot()));
}
}
//SLCO messages on timeslot 0 to catch capacity plus rest channel events
else if((message.isValid() || mIgnoreCRCChecksums) && message.getTimeslot() == 0 && message instanceof LCMessage)
{
processLinkControl((LCMessage)message, false);
}
//Pass the message to the network configuration monitor, if this decoder state has a non-null instance
if(mNetworkConfigurationMonitor != null && (message.isValid() || mIgnoreCRCChecksums) &&
message instanceof DMRMessage)
{
mNetworkConfigurationMonitor.process((DMRMessage)message);
}
}
/**
* Processes Capacity Plus rest channel notifications to detect when the rest channel has changed. When a new
* rest channel is specified that is different from the current channel, notify the traffic channel manager so that
* it can convert the current channel to a traffic channel and start a new channel for the rest channel, using the
* current channel's configuration details and the specified rest channel frequency. This approach ensures that
* there is no disruption to the currently processing channel and that the original channel configuration can be
* recreated to follow the rest channel.
*
* @param restChannel currently indicated
*/
private void updateRestChannel(DMRChannel restChannel)
{
//Only respond if this is a standard/control channel (not a traffic channel).
if(mChannel.isStandardChannel() && mCurrentFrequency > 0 &&
restChannel.getDownlinkFrequency() > 0 &&
restChannel.getDownlinkFrequency() != mCurrentFrequency && mTrafficChannelManager != null)
{
mTrafficChannelManager.convertToTrafficChannel(mChannel, mCurrentFrequency, restChannel,
mNetworkConfigurationMonitor);
}
}
/**
* Preloads the DMR network configuration monitor that is optionally delivered as preload data. This is primarily
* used during a Capacity plus REST channel rotation to pass the monitor from the previous REST channel to the
* current REST channel to ensure data continuity.
*
* Note: the monitor is only assigned to the timeslot 1 decoder state since the decoder state passes all received
* messages (Timeslots 0, 1, and 2) to the monitor.
*
* Note: this method is invoked over the Guava event but by the ChannelProcessingManager.
*
* @param preloadData containing a DMR network configuration monitor.
*/
@Subscribe
public void preload(DMRNetworkConfigurationPreloadData preloadData)
{
if(getTimeslot() == 1 && preloadData.hasData())
{
mNetworkConfigurationMonitor = preloadData.getData();
}
}
/**
* Processes a short data message carrying SMS text
* @param sms
*/
private void processSMS(UDTShortMessageService sms)
{
broadcast(new DecoderStateEvent(this, Event.START, State.DATA, getTimeslot()));
DecodeEvent smsEvent = DMRDecodeEvent.builder(DecodeEventType.SMS, sms.getTimestamp())
.details("MESSAGE: " + sms.getSMS())
.identifiers(new IdentifierCollection(sms.getIdentifiers()))
.timeslot(sms.getTimeslot())
.build();
broadcast(smsEvent);
}
/**
* Processes a packet message
*/
private void processPacket(DMRPacketMessage packet)
{
broadcast(new DecoderStateEvent(this, Event.START, State.DATA, getTimeslot()));
//Hytera SDS Long SMS message
if(packet.getPacket() instanceof HyteraSmsPacket hyteraSmsPacket)
{
MutableIdentifierCollection mic = new MutableIdentifierCollection(packet.getIdentifiers());
mic.remove(Form.RADIO);
mic.remove(Form.TALKGROUP);
mic.update(hyteraSmsPacket.getSource());
mic.update(hyteraSmsPacket.getDestination());
DecodeEvent smsEvent = DMRDecodeEvent.builder(DecodeEventType.SMS, packet.getTimestamp())
.identifiers(mic)
.timeslot(packet.getTimeslot())
.details("SMS:" + hyteraSmsPacket.getSMS())
.build();
broadcast(smsEvent);
}
//Unknown Hytera Long Data Service Token Message
else if(packet.getPacket() instanceof HyteraUnknownPacket hyteraUnknownPacket)
{
MutableIdentifierCollection mic = new MutableIdentifierCollection(packet.getIdentifiers());
DecodeEvent unknownTokenEvent = DMRDecodeEvent.builder(DecodeEventType.UNKNOWN_PACKET, packet.getTimestamp())
.identifiers(mic)
.timeslot(packet.getTimeslot())
.details("HYTERA UNK TOKEN MSG:" + hyteraUnknownPacket.getHeader().toString())
.build();
broadcast(unknownTokenEvent);
}
else
{
DecodeEvent packetEvent = DMRDecodeEvent.builder(DecodeEventType.DATA_PACKET, packet.getTimestamp())
.identifiers(getMergedIdentifierCollection(packet.getIdentifiers()))
.timeslot(packet.getTimeslot())
.details(packet.toString())
.build();
broadcast(packetEvent);
GeoPosition geoPosition = PacketUtil.extractGeoPosition(packet.getPacket());
if (geoPosition != null) {
PlottableDecodeEvent plottableDecodeEvent = PlottableDecodeEvent.plottableBuilder(DecodeEventType.GPS, packet.getTimestamp())
.channel(getCurrentChannel())
.identifiers(getMergedIdentifierCollection(packet.getIdentifiers()))
.protocol(Protocol.LRRP)
.location(geoPosition)
.build();
broadcast(plottableDecodeEvent);
}
}
}
/**
* Processes voice messages
*/
private void processVoice(VoiceMessage message)
{
if(message.getSyncPattern().isDirectMode())
{
updateCurrentCall(DecodeEventType.CALL, "DIRECT MODE", message.getTimestamp());
//Use the timeslot as the talkgroup identifier since DCDM mode doesn't use talkgroups
getIdentifierCollection().update(DMRTalkgroup.create(getTimeslot()));
}
else
{
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CALL, getTimeslot()));
}
}
/**
* Processes a voice header message
*/
private void processHeader(HeaderMessage header)
{
switch(header.getSlotType().getDataType())
{
case VOICE_HEADER:
broadcast(new DecoderStateEvent(this, Event.START, State.CALL, getTimeslot()));
break;
case PI_HEADER:
case MBC_HEADER:
case DATA_HEADER:
case USB_DATA:
case MBC_ENC_HEADER:
case DATA_ENC_HEADER:
case CHANNEL_CONTROL_ENC_HEADER:
broadcast(new DecoderStateEvent(this, Event.START, State.DATA, getTimeslot()));
break;
}
//Process the link control message to get the identifiers
LCMessage lc = header.getLCMessage();
if(lc.isValid())
{
processLinkControl(lc, false);
}
}
/**
* Process Data Messages
*
* Note: invalid messages are allowed to pass to this method. Messages are selectively checked for isValid()
* to overcome RAS implementation in certain systems.
*/
private void processData(DataMessage message)
{
switch(message.getSlotType().getDataType())
{
case CSBK:
if((message.isValid() || mIgnoreCRCChecksums) && message instanceof CSBKMessage)
{
processCSBK((CSBKMessage)message);
}
break;
case VOICE_HEADER:
if(message instanceof HeaderMessage)
{
processVoiceHeader((HeaderMessage)message);
}
break;
case USB_DATA:
break;
case PI_HEADER:
case MBC_HEADER:
case DATA_HEADER:
case MBC_ENC_HEADER:
case DATA_ENC_HEADER:
case CHANNEL_CONTROL_ENC_HEADER:
if(message instanceof HeaderMessage)
{
processHeader((HeaderMessage)message);
}
break;
case SLOT_IDLE:
closeCurrentCallEvent(message.getTimestamp());
getIdentifierCollection().remove(IdentifierClass.USER);
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.ACTIVE, getTimeslot()));
break;
case TLC:
if(message instanceof Terminator)
{
processTerminator((Terminator)message);
}
break;
case RATE_1_OF_2_DATA:
case RATE_3_OF_4_DATA:
case RATE_1_DATA:
broadcast(new DecoderStateEvent(this, Event.START, State.DATA, getTimeslot()));
break;
case MBC_BLOCK:
case RESERVED_15:
case UNKNOWN:
default:
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.ACTIVE, getTimeslot()));
break;
}
}
/**
* Process terminator with link control messages
*/
private void processTerminator(Terminator terminator)
{
closeCurrentCallEvent(terminator.getTimestamp());
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.ACTIVE, getTimeslot()));
LCMessage lcMessage = terminator.getLCMessage();
if(lcMessage.isValid())
{
processLinkControl(lcMessage, true);
}
}
/**
* Process a voice header message
*/
private void processVoiceHeader(HeaderMessage voiceHeader)
{
LCMessage lcMessage = voiceHeader.getLCMessage();
if(lcMessage.isValid())
{
processLinkControl(lcMessage, false);
}
}
private void processCSBK(CSBKMessage csbk)
{
switch(csbk.getOpcode())
{
case STANDARD_ACKNOWLEDGE_RESPONSE_INBOUND_PAYLOAD:
case STANDARD_ACKNOWLEDGE_RESPONSE_OUTBOUND_PAYLOAD:
if(csbk instanceof Acknowledge)
{
broadcast(getDecodeEvent(csbk, DecodeEventType.RESPONSE,
((Acknowledge) csbk).getReason().toString()));
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.ACTIVE, getTimeslot()));
break;
case STANDARD_ACKNOWLEDGE_RESPONSE_INBOUND_TSCC:
case STANDARD_ACKNOWLEDGE_RESPONSE_OUTBOUND_TSCC:
if(csbk instanceof Acknowledge)
{
broadcast(getDecodeEvent(csbk, DecodeEventType.RESPONSE,
((Acknowledge) csbk).getReason().toString()));
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case STANDARD_AHOY:
if(csbk instanceof Ahoy)
{
switch(((Ahoy)csbk).getServiceKind())
{
case AUTHENTICATE_REGISTER_RADIO_CHECK_SERVICE:
broadcast(getDecodeEvent(csbk, DecodeEventType.COMMAND, DecodeEventType.REGISTER.toString()));
break;
case CANCEL_CALL_SERVICE:
broadcast(getDecodeEvent(csbk, DecodeEventType.COMMAND, "CANCEL CALL"));
break;
case SUPPLEMENTARY_SERVICE:
if(csbk instanceof StunReviveKill)
{
broadcast(getDecodeEvent(csbk, DecodeEventType.COMMAND,
((StunReviveKill)csbk).getCommand() + " RADIO"));
}
break;
case FULL_DUPLEX_MS_TO_MS_PACKET_CALL_SERVICE:
case FULL_DUPLEX_MS_TO_MS_VOICE_CALL_SERVICE:
case INDIVIDUAL_VOICE_CALL_SERVICE:
case INDIVIDUAL_PACKET_CALL_SERVICE:
case INDIVIDUAL_UDT_SHORT_DATA_CALL_SERVICE:
case TALKGROUP_PACKET_CALL_SERVICE:
case TALKGROUP_UDT_SHORT_DATA_CALL_SERVICE:
case TALKGROUP_VOICE_CALL_SERVICE:
if(csbk instanceof ServiceRadioCheck)
{
ServiceRadioCheck src = (ServiceRadioCheck)csbk;
broadcast(getDecodeEvent(csbk, DecodeEventType.RADIO_CHECK,
src.getServiceDescription() + " SERVICE FOR " +
(src.isTalkgroupTarget() ? "TALKGROUP" : "RADIO")));
}
break;
}
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case STANDARD_ALOHA:
if(csbk instanceof Aloha)
{
Aloha aloha = (Aloha)csbk;
if(aloha.hasRadioIdentifier())
{
broadcast(getDecodeEvent(csbk, DecodeEventType.RESPONSE, "Aloha Acknowledge"));
resetState();
}
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case STANDARD_ANNOUNCEMENT:
if(csbk instanceof Announcement)
{
switch(((Announcement)csbk).getAnnouncementType())
{
case MASS_REGISTRATION:
broadcast(getDecodeEvent(csbk, DecodeEventType.REGISTER, "MASS REGISTRATION"));
break;
case VOTE_NOW_ADVICE:
if(csbk instanceof VoteNowAdvice)
{
broadcast(getDecodeEvent(csbk, DecodeEventType.COMMAND,
"VOTE NOW FOR " + ((VoteNowAdvice)csbk).getVotedSystemIdentityCode()));
}
break;
}
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case STANDARD_CLEAR:
broadcast(new DecoderStateEvent(this, Event.END, State.CALL, getTimeslot()));
resetState();
break;
case STANDARD_PREAMBLE:
getIdentifierCollection().update(csbk.getIdentifiers());
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.DATA, getTimeslot()));
break;
case STANDARD_PROTECT:
if(csbk instanceof Protect)
{
broadcast(getDecodeEvent(csbk, DecodeEventType.COMMAND,
"PROTECT: " + ((Protect)csbk).getProtectKind()));
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CALL, getTimeslot()));
break;
case HYTERA_08_ANNOUNCEMENT:
case HYTERA_68_ANNOUNCEMENT:
case HYTERA_68_XPT_SITE_STATE:
case MOTOROLA_CAPPLUS_NEIGHBOR_REPORT:
if(csbk instanceof CapacityPlusNeighbors)
{
//Update state and rest channel
updateRestChannel(((CapacityPlusNeighbors)csbk).getRestChannel());
}
break;
case MOTOROLA_CAPPLUS_SITE_STATUS:
if(csbk instanceof CapacityPlusSiteStatus)
{
CapacityPlusSiteStatus cpss = (CapacityPlusSiteStatus)csbk;
//Channel rotation monitor normally uses only CONTROL state, so when we detect that we're a
//Capacity plus system, add ACTIVE as an active state to the monitor. This can be requested repeatedly.
getInterModuleEventBus().post(CAPACITY_PLUS_ACTIVE_STATE_REQUEST);
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.ACTIVE, getTimeslot()));
//Update state and rest channel
updateRestChannel(cpss.getRestChannel());
}
break;
case MOTOROLA_CONPLUS_NEIGHBOR_REPORT:
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case STANDARD_DUPLEX_PRIVATE_DATA_CHANNEL_GRANT:
case STANDARD_PRIVATE_DATA_CHANNEL_GRANT_SINGLE_ITEM:
case STANDARD_TALKGROUP_DATA_CHANNEL_GRANT_MULTI_ITEM:
case STANDARD_TALKGROUP_DATA_CHANNEL_GRANT_SINGLE_ITEM:
if(csbk instanceof ChannelGrant)
{
ChannelGrant dataGrant = (ChannelGrant)csbk;
DMRChannel channel = dataGrant.getChannel();
IdentifierCollection mergedIdentifiers = getMergedIdentifierCollection(csbk.getIdentifiers());
if(hasTrafficChannelManager())
{
mTrafficChannelManager.processChannelGrant(channel, mergedIdentifiers, csbk.getOpcode(),
csbk.getTimestamp(), csbk.isEncrypted());
}
else
{
DecodeEvent event = mDetectedCallEventsMap.get(channel.getValue());
if(isStale(event, csbk.getTimestamp(), csbk.getIdentifiers()))
{
event = getDecodeEvent(csbk, DecodeEventType.DATA_CALL, channel, mergedIdentifiers);
mDetectedCallEventsMap.put(channel.getValue(), event);
}
else
{
//Update the ending timestamp for the event and rebroadcast
event.end(csbk.getTimestamp());
}
broadcast(event);
}
}
break;
case STANDARD_BROADCAST_TALKGROUP_VOICE_CHANNEL_GRANT:
case STANDARD_TALKGROUP_VOICE_CHANNEL_GRANT:
if(csbk instanceof ChannelGrant)
{
ChannelGrant tgGrant = (ChannelGrant)csbk;
DMRChannel channel = tgGrant.getChannel();
IdentifierCollection mergedIdentifiers = getMergedIdentifierCollection(csbk.getIdentifiers());
if(hasTrafficChannelManager())
{
mTrafficChannelManager.processChannelGrant(channel, mergedIdentifiers,
csbk.getOpcode(), csbk.getTimestamp(), csbk.isEncrypted());
}
else
{
DecodeEvent event = mDetectedCallEventsMap.get(channel.getValue());
if(isStale(event, csbk.getTimestamp(), csbk.getIdentifiers()))
{
event = getDecodeEvent(csbk, DecodeEventType.CALL_GROUP, channel, mergedIdentifiers);
mDetectedCallEventsMap.put(channel.getValue(), event);
}
else
{
//Update the ending timestamp for the event and rebroadcast
event.end(csbk.getTimestamp());
}
broadcast(event);
}
}
break;
case STANDARD_DUPLEX_PRIVATE_VOICE_CHANNEL_GRANT:
case STANDARD_PRIVATE_VOICE_CHANNEL_GRANT:
if(csbk instanceof ChannelGrant)
{
ChannelGrant channelGrant = (ChannelGrant)csbk;
DMRChannel channel = channelGrant.getChannel();
IdentifierCollection mergedIdentifiers = getMergedIdentifierCollection(csbk.getIdentifiers());
if(hasTrafficChannelManager())
{
mTrafficChannelManager.processChannelGrant(channel, mergedIdentifiers,
csbk.getOpcode(), csbk.getTimestamp(), csbk.isEncrypted());
}
else
{
DecodeEvent event = mDetectedCallEventsMap.get(channel.getValue());
if(isStale(event, csbk.getTimestamp(), csbk.getIdentifiers()))
{
event = getDecodeEvent(csbk, DecodeEventType.CALL_UNIT_TO_UNIT, channel, mergedIdentifiers);
mDetectedCallEventsMap.put(channel.getValue(), event);
}
else
{
//Update the ending timestamp for the event and rebroadcast
event.end(csbk.getTimestamp());
}
broadcast(event);
}
}
break;
case MOTOROLA_CAPMAX_ALOHA:
if(csbk instanceof CapacityMaxAloha)
{
CapacityMaxAloha cmAloha = (CapacityMaxAloha)csbk;
if(cmAloha.hasRadioIdentifier())
{
broadcast(getDecodeEvent(csbk, DecodeEventType.RESPONSE, "Aloha Acknowledge"));
resetState();
}
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case MOTOROLA_CONPLUS_DATA_CHANNEL_GRANT:
if(csbk instanceof ConnectPlusDataChannelGrant)
{
ConnectPlusDataChannelGrant cpdcg = (ConnectPlusDataChannelGrant)csbk;
DMRChannel channel = cpdcg.getChannel();
IdentifierCollection mergedIdentifiers = getMergedIdentifierCollection(csbk.getIdentifiers());
if(hasTrafficChannelManager())
{
mTrafficChannelManager.processChannelGrant(channel, mergedIdentifiers, csbk.getOpcode(),
csbk.getTimestamp(), csbk.isEncrypted());
}
else
{
DecodeEvent event = mDetectedCallEventsMap.get(channel.getValue());
if(isStale(event, csbk.getTimestamp(), csbk.getIdentifiers()))
{
event = getDecodeEvent(csbk, DecodeEventType.DATA_CALL, channel, mergedIdentifiers);
mDetectedCallEventsMap.put(channel.getValue(), event);
}
else
{
//Update the ending timestamp for the event and rebroadcast
event.end(csbk.getTimestamp());
}
broadcast(event);
}
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case MOTOROLA_CONPLUS_REGISTRATION_REQUEST:
DecodeEvent event = DMRDecodeEvent.builder(DecodeEventType.REQUEST, csbk.getTimestamp())
.details("Registration Request")
.identifiers(getMergedIdentifierCollection(csbk.getIdentifiers()))
.timeslot(csbk.getTimeslot())
.build();
broadcast(event);
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case MOTOROLA_CONPLUS_REGISTRATION_RESPONSE:
DecodeEvent regRespEvent = DMRDecodeEvent.builder(DecodeEventType.RESPONSE, csbk.getTimestamp())
.details("Registration Response")
.identifiers(getMergedIdentifierCollection(csbk.getIdentifiers()))
.timeslot(csbk.getTimeslot())
.build();
broadcast(regRespEvent);
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case MOTOROLA_CONPLUS_VOICE_CHANNEL_USER:
if(csbk instanceof ConnectPlusVoiceChannelUser)
{
ConnectPlusVoiceChannelUser cpvcu = (ConnectPlusVoiceChannelUser)csbk;
DMRChannel channel = cpvcu.getChannel();
IdentifierCollection mergedIdentifiers = getMergedIdentifierCollection(csbk.getIdentifiers());
if(hasTrafficChannelManager())
{
mTrafficChannelManager.processChannelGrant(channel, mergedIdentifiers, csbk.getOpcode(),
csbk.getTimestamp(), csbk.isEncrypted());
}
else
{
DecodeEvent detectedEvent = mDetectedCallEventsMap.get(channel.getValue());
if(isStale(detectedEvent, csbk.getTimestamp(), csbk.getIdentifiers()))
{
detectedEvent = getDecodeEvent(csbk, DecodeEventType.CALL_GROUP, channel, mergedIdentifiers);
mDetectedCallEventsMap.put(channel.getValue(), detectedEvent);
}
else
{
//Update the ending timestamp for the event and rebroadcast
detectedEvent.end(csbk.getTimestamp());
}
broadcast(detectedEvent);
}
}
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
case MOTOROLA_CONPLUS_TALKGROUP_AFFILIATION:
DecodeEvent affiliateEvent = DMRDecodeEvent.builder(DecodeEventType.AFFILIATE, csbk.getTimestamp())
.details("TALKGROUP AFFILIATION")
.identifiers(getMergedIdentifierCollection(csbk.getIdentifiers()))
.timeslot(csbk.getTimeslot())
.build();
broadcast(affiliateEvent);
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.CONTROL, getTimeslot()));
break;
default:
broadcast(new DecoderStateEvent(this, Event.CONTINUATION, State.ACTIVE, getTimeslot()));
break;
}
}
private DecodeEvent getDecodeEvent(CSBKMessage csbk, DecodeEventType decodeEventType, DMRChannel channel, IdentifierCollection mergedIdentifiers) {
return DMRDecodeEvent.builder(decodeEventType, csbk.getTimestamp())
.channel(channel)
.details(csbk.getOpcode().getLabel())
.identifiers(mergedIdentifiers)
.timeslot(channel.getTimeslot())
.build();
}
private DecodeEvent getDecodeEvent(CSBKMessage csbk, DecodeEventType decodeEventType, String details) {
return DMRDecodeEvent.builder(decodeEventType, csbk.getTimestamp())
.identifiers(getMergedIdentifierCollection(csbk.getIdentifiers()))
.timeslot(csbk.getTimeslot())
.details(details)
.build();
}
/**
* Creates a copy of the current identifier collection, removes any USER class identifiers and loads the identifiers
* argument values into the collection.
* @param identifiers to load into the collection.
* @return copy identifier collection.
*/
private IdentifierCollection getMergedIdentifierCollection(List<Identifier> identifiers)
{
MutableIdentifierCollection mic = new MutableIdentifierCollection(getIdentifierCollection().getIdentifiers());
mic.remove(IdentifierClass.USER);
mic.update(identifiers);
return mic;
}
/**
* Indicates if the event is a stale event, meaning that the event is null, or the event start exceeds the max
* valid call duration threshold, or if the event identifiers don't match the current identifiers.
*
* @param event to check for staleness
* @param timestamp to check the event against
* @param currentIdentifiers to compare against the event
* @return true if the event is stale.
*/
private boolean isStale(DecodeEvent event, long timestamp, List<Identifier> currentIdentifiers)
{
if(event == null || (timestamp - event.getTimeStart() > MAX_VALID_CALL_DURATION_MS))
{
return true;
}
return !isSameCall(event.getIdentifierCollection(), currentIdentifiers);
}
/**
* Indicates if the TO/FROM identifiers in the identifier collection match the TO/FROM identifiers in the list
* of identifiers.
* @param identifierCollection containing TO/FROM identifiers
* @param identifiers containing TO/FROM identifiers
* @return true if the TO/FROM identifiers in each collection match
*/
private boolean isSameCall(IdentifierCollection identifierCollection, List<Identifier> identifiers)
{
IntegerIdentifier to = null;
IntegerIdentifier from = null;
for(Identifier identifier: identifierCollection.getIdentifiers(IdentifierClass.USER, Role.TO))
{
if(identifier instanceof IntegerIdentifier)
{
to = (IntegerIdentifier)identifier;
break;
}
}
for(Identifier identifier: identifierCollection.getIdentifiers(IdentifierClass.USER, Role.FROM))
{
if(identifier instanceof IntegerIdentifier)
{
from = (IntegerIdentifier)identifier;
break;
}
}
if(to == null || from == null)
{
return false;
}
boolean toMatch = false;
boolean fromMatch = false;
for(Identifier identifier: identifiers)
{
if(identifier.getRole() == Role.TO &&
identifier instanceof IntegerIdentifier &&
((IntegerIdentifier)identifier).getValue() == to.getValue())
{
toMatch = true;
}
else if(identifier.getRole() == Role.FROM &&
identifier instanceof IntegerIdentifier &&
((IntegerIdentifier)identifier).getValue() == from.getValue())
{
fromMatch = true;
}
}
return toMatch & fromMatch;
}
/**
* Processes Link Control Messages
* @param isTerminator set to true when the link control is carried by a terminator
*/
private void processLinkControl(LCMessage message, boolean isTerminator)
{
switch(message.getOpcode())
{
case SHORT_CAPACITY_PLUS_REST_CHANNEL_NOTIFICATION:
if(message instanceof CapacityPlusRestChannel)
{
updateRestChannel(((CapacityPlusRestChannel)message).getRestChannel());
}
break;
case FULL_CAPACITY_PLUS_ENCRYPTED_VOICE_CHANNEL_USER:
if(message instanceof CapacityPlusEncryptedVoiceChannelUser cpgvcu)
{
if(isTerminator)
{
getIdentifierCollection().remove(Role.FROM);
getIdentifierCollection().update(cpgvcu.getTalkgroup());
}
else
{
getIdentifierCollection().update(message.getIdentifiers());
ServiceOptions serviceOptions = cpgvcu.getServiceOptions();
updateCurrentCall(serviceOptions.isEncrypted() ? DecodeEventType.CALL_GROUP_ENCRYPTED :
DecodeEventType.CALL_GROUP, serviceOptions.toString(), message.getTimestamp());
}
}
break;
case FULL_CAPACITY_PLUS_GROUP_VOICE_CHANNEL_USER:
if(message instanceof MotorolaGroupVoiceChannelUser cpgvcu)
{
if(isTerminator)
{
getIdentifierCollection().remove(Role.FROM);
getIdentifierCollection().update(cpgvcu.getTalkgroup());
}
else
{
getIdentifierCollection().update(message.getIdentifiers());
ServiceOptions serviceOptions = cpgvcu.getServiceOptions();
updateCurrentCall(serviceOptions.isEncrypted() ? DecodeEventType.CALL_GROUP_ENCRYPTED :
DecodeEventType.CALL_GROUP, serviceOptions.toString(), message.getTimestamp());
}
}
break;
case FULL_CAPACITY_PLUS_WIDE_AREA_VOICE_CHANNEL_USER:
if(message instanceof CapacityPlusWideAreaVoiceChannelUser)
{