-
Notifications
You must be signed in to change notification settings - Fork 240
/
DMRDataMessageFactory.java
262 lines (247 loc) · 13.1 KB
/
DMRDataMessageFactory.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
/*
* *****************************************************************************
* 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.message.data;
import io.github.dsheirer.bits.BitSetFullException;
import io.github.dsheirer.bits.CorrectedBinaryMessage;
import io.github.dsheirer.edac.BPTC_196_96;
import io.github.dsheirer.edac.CRCDMR;
import io.github.dsheirer.edac.trellis.ViterbiDecoder_3_4_DMR;
import io.github.dsheirer.module.decode.dmr.DMRSyncPattern;
import io.github.dsheirer.module.decode.dmr.message.CACH;
import io.github.dsheirer.module.decode.dmr.message.data.block.DataBlock1Rate;
import io.github.dsheirer.module.decode.dmr.message.data.block.DataBlock1_2Rate;
import io.github.dsheirer.module.decode.dmr.message.data.block.DataBlock3_4Rate;
import io.github.dsheirer.module.decode.dmr.message.data.csbk.CSBKMessageFactory;
import io.github.dsheirer.module.decode.dmr.message.data.header.ConfirmedDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.DataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.DefinedShortDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.HeaderMessage;
import io.github.dsheirer.module.decode.dmr.message.data.header.MBCHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.ProprietaryDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.RawShortDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.ResponseDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.ShortDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.StatusDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.UDTHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.UnconfirmedDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.VoiceHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.hytera.HyteraProprietaryDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.motorola.MNISProprietaryDataHeader;
import io.github.dsheirer.module.decode.dmr.message.data.header.motorola.MotorolaDataEncryptionHeader;
import io.github.dsheirer.module.decode.dmr.message.data.mbc.MBCContinuationBlock;
import io.github.dsheirer.module.decode.dmr.message.data.terminator.Terminator;
import io.github.dsheirer.module.decode.dmr.message.data.usb.USBData;
import io.github.dsheirer.module.decode.dmr.message.type.DataPacketFormat;
import io.github.dsheirer.module.decode.dmr.message.type.ServiceAccessPoint;
import io.github.dsheirer.module.decode.dmr.message.type.Vendor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Factory for creating data messages that contain a 196-bit BPTC protected message.
*/
public class DMRDataMessageFactory
{
private final static Logger mLog = LoggerFactory.getLogger(DMRDataMessageFactory.class);
private static final ViterbiDecoder_3_4_DMR VITERBI_DECODER = new ViterbiDecoder_3_4_DMR();
/**
* Creates a data message class
* @param pattern for the DMR burst
* @param message DMR burst as transmitted
* @param cach from the DMR burst
* @param timestamp for the message
* @param timeslot for the message
* @return data message instance
*/
public static DataMessage create(DMRSyncPattern pattern, CorrectedBinaryMessage message, CACH cach, long timestamp,
int timeslot)
{
SlotType slotType = SlotType.getSlotType(message);
if(slotType.isValid())
{
switch(slotType.getDataType())
{
case SLOT_IDLE:
return new IDLEMessage(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case CSBK:
return CSBKMessageFactory.create(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case USB_DATA:
CorrectedBinaryMessage usbdPayload = getPayload(message);
switch(USBData.getServiceType(usbdPayload))
{
case LIP_SHORT_LOCATION_REQUEST:
default:
return new USBData(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
}
case MBC_ENC_HEADER:
case MBC_HEADER:
return new MBCHeader(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case CHANNEL_CONTROL_ENC_HEADER:
case PI_HEADER:
return new HeaderMessage(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case VOICE_HEADER:
return new VoiceHeader(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case DATA_ENC_HEADER:
case DATA_HEADER:
CorrectedBinaryMessage payload = getPayload(message);
int crcBitCount = CRCDMR.correctCCITT80(payload, 0, 80, 0xCCCC);
boolean valid = crcBitCount < 2;
DataPacketFormat dpf = DataHeader.getDataPacketFormat(payload);
switch(dpf)
{
case CONFIRMED_DATA_PACKET:
ConfirmedDataHeader cdh = new ConfirmedDataHeader(pattern, payload, cach, slotType, timestamp, timeslot);
cdh.setValid(valid);
return cdh;
case PROPRIETARY_DATA_PACKET:
Vendor vendor = ProprietaryDataHeader.getVendor(payload);
switch(vendor)
{
case MOTOROLA_CAPACITY_PLUS:
case MOTOROLA_CONNECT_PLUS:
ServiceAccessPoint sap = ProprietaryDataHeader.getServiceAccessPoint(payload);
if(sap == ServiceAccessPoint.SAP_1)
{
MNISProprietaryDataHeader mpdh = new MNISProprietaryDataHeader(pattern, payload,
cach, slotType, timestamp, timeslot);
mpdh.setValid(valid);
return mpdh;
}
else
{
MotorolaDataEncryptionHeader mprdh = new MotorolaDataEncryptionHeader(pattern,
payload, cach, slotType, timestamp, timeslot);
mprdh.setValid(valid);
return mprdh;
}
case HYTERA_68:
HyteraProprietaryDataHeader hpdh = new HyteraProprietaryDataHeader(pattern, payload,
cach, slotType, timestamp, timeslot);
hpdh.setValid(valid);
return hpdh;
default:
ProprietaryDataHeader pdh = new ProprietaryDataHeader(pattern, payload, cach,
slotType, timestamp, timeslot);
pdh.setValid(valid);
return pdh;
}
case RAW_OR_STATUS_SHORT_DATA:
int appendedBlockCount = ShortDataHeader.getAppendedBlocks(payload);
if(appendedBlockCount == 0)
{
StatusDataHeader sdh = new StatusDataHeader(pattern, payload, cach, slotType, timestamp,
timeslot);
sdh.setValid(valid);
return sdh;
}
else
{
RawShortDataHeader rsdh = new RawShortDataHeader(pattern, payload, cach, slotType,
timestamp, timeslot);
rsdh.setValid(valid);
return rsdh;
}
case RESPONSE_PACKET:
ResponseDataHeader rdh = new ResponseDataHeader(pattern, payload, cach, slotType, timestamp,
timeslot);
rdh.setValid(valid);
return rdh;
case DEFINED_SHORT_DATA:
DefinedShortDataHeader dsdh = new DefinedShortDataHeader(pattern, payload, cach,slotType,
timestamp, timeslot);
dsdh.setValid(valid);
return dsdh;
case UNCONFIRMED_DATA_PACKET:
UnconfirmedDataHeader udh = new UnconfirmedDataHeader(pattern, payload, cach, slotType,
timestamp, timeslot);
udh.setValid(valid);
return udh;
case UNIFIED_DATA_TRANSPORT:
//TODO: this will eventually need its own factory
UDTHeader uh = new UDTHeader(pattern, payload, cach, slotType, timestamp, timeslot);
uh.setValid(valid);
return uh;
default:
DataHeader dh = new DataHeader(pattern, payload, cach, slotType, timestamp, timeslot);
dh.setValid(valid);
return dh;
}
case TLC:
return new Terminator(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case RATE_1_OF_2_DATA:
return new DataBlock1_2Rate(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case RATE_3_OF_4_DATA:
return new DataBlock3_4Rate(pattern, getTrellisPayload(message), cach, slotType, timestamp, timeslot);
case RATE_1_DATA:
return new DataBlock1Rate(pattern, message, cach, slotType, timestamp, timeslot);
case MBC_BLOCK:
return new MBCContinuationBlock(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
case RESERVED_15:
case UNKNOWN:
return new UnknownDataMessage(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
}
}
return new UnknownDataMessage(pattern, getPayload(message), cach, slotType, timestamp, timeslot);
}
/**
* Decodes a 3/4 rate trellis coded message
* @param message containing trellis coded message
* @return decoded message
*/
private static CorrectedBinaryMessage getTrellisPayload(CorrectedBinaryMessage message)
{
CorrectedBinaryMessage descrambled = extract(message);
return VITERBI_DECODER.decode(descrambled);
}
/**
* De-scramble, decode and error check a BPTC protected raw message and return a 96-bit error corrected
* payload or null.
* @param message
* @return
*/
private static CorrectedBinaryMessage getPayload(CorrectedBinaryMessage message)
{
CorrectedBinaryMessage descrambled = extract(message);
return BPTC_196_96.extract(descrambled);
}
/**
* Extracts the 196-bit message payload from the full DMR burst
* @param message as transmitted
* @return extracted message
*/
private static CorrectedBinaryMessage extract(CorrectedBinaryMessage message)
{
CorrectedBinaryMessage extracted = new CorrectedBinaryMessage(196);
try
{
for(int i = 24; i < 122; i++)
{
extracted.add(message.get(i));
}
for(int i = 190; i < 190 + 98; i++)
{
extracted.add(message.get(i));
}
}
catch(BitSetFullException ex)
{
mLog.error("Error extracting DMR burst payload bits");
}
return extracted;
}
}