-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
TetraDecoder.cs
429 lines (354 loc) · 17.6 KB
/
TetraDecoder.cs
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
using SDRSharp.Radio;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
namespace SDRSharp.Tetra
{
unsafe class TetraDecoder
{
public delegate void DataReadyDelegate(List<ReceivedData> data);
public event DataReadyDelegate DataReady;
public delegate void SyncInfoReadyDelegate(ReceivedData syncInfo);
public event SyncInfoReadyDelegate SyncInfoReady;
private PhyLevel _phyLevel = new PhyLevel();
private LowerMacLevel _lowerMac = new LowerMacLevel();
private MacLevel _parse = new MacLevel();
private UnsafeBuffer _bbBuffer;
private byte* _bbBufferPtr;
private UnsafeBuffer _bkn1Buffer;
private byte* _bkn1BufferPtr;
private UnsafeBuffer _bkn2Buffer;
private byte* _bkn2BufferPtr;
private UnsafeBuffer _sb1Buffer;
private byte* _sb1BufferPtr;
private LogicChannel _logicChannel = new LogicChannel();
private NetworkTime _networkTime = new NetworkTime();
private ReceivedData _syncInfo = new ReceivedData();
private List<ReceivedData> _data = new List<ReceivedData>();
private int _timeCounter;
private float _badBurstCounter;
private float _averageBer;
private Control _owner;
private int _fpass;
private unsafe void* _ch1InitStruct;
private unsafe void* _ch2InitStruct;
private unsafe void* _ch3InitStruct;
private unsafe void* _ch4InitStruct;
short[] _cdc = new short[276];
short[] _sdc = new short[480];
private bool _haveErrors;
public int NetworkTimeTN { get; internal set; }
public int NetworkTimeFN { get; internal set; }
public int NetworkTimeMN { get; internal set; }
public long DownFrequency { get; private set; }
public bool BurstReceived { get; internal set; }
public float Ber { get; internal set; }
public bool HaveErrors { get { return _haveErrors; } }
public TetraDecoder(Control owner)
{
_bbBuffer = UnsafeBuffer.Create(30);
_bbBufferPtr = (byte*)_bbBuffer;
_bkn1Buffer = UnsafeBuffer.Create(216);
_bkn1BufferPtr = (byte*)_bkn1Buffer;
_bkn2Buffer = UnsafeBuffer.Create(216);
_bkn2BufferPtr = (byte*)_bkn2Buffer;
_sb1Buffer = UnsafeBuffer.Create(120);
_sb1BufferPtr = (byte*)_sb1Buffer;
_owner = owner;
_fpass = 1;
_ch1InitStruct = NativeMethods.tetra_decode_init();
_ch2InitStruct = NativeMethods.tetra_decode_init();
_ch3InitStruct = NativeMethods.tetra_decode_init();
_ch4InitStruct = NativeMethods.tetra_decode_init();
}
public void Dispose()
{
}
public int Process(Burst burst, float* audioOut)
{
var trafficChannel = 0;
BurstReceived = (burst.Type != BurstType.None);
_timeCounter++;
Ber = _averageBer;
if (_timeCounter > 100)
{
Mer = (_badBurstCounter / _timeCounter) * 100.0f;
_timeCounter = 0;
_badBurstCounter = 0;
}
_networkTime.AddTimeSlot();
if (burst.Type == BurstType.None)
{
_haveErrors = true;
if (TetraMode == Mode.TMO)
{
_badBurstCounter++;
//Debug.WriteLine("burst err");
//Debug.Write(string.Format("ts:{0:0} fr:{1:00} Burst_Err", _networkTime.TimeSlot, _networkTime.Frame));
}
return trafficChannel;
}
_haveErrors = false;
//Debug.WriteLine("");
//Debug.Write(string.Format("ts:{0:0} fr:{1:00}", _networkTime.TimeSlot, _networkTime.Frame));
//Debug.Write(" " + burst.Type.ToString());
_parse.ResetAACH();
_data.Clear();
_syncInfo.Clear();
#region Sync burst
if (burst.Type == BurstType.SYNC)
{
_phyLevel.ExtractSBChannels(burst, _sb1BufferPtr);
_logicChannel = _lowerMac.ExtractLogicChannelFromSB(_sb1BufferPtr, _sb1Buffer.Length);
_badBurstCounter += _logicChannel.CrcIsOk ? 0.0f : 0.5f;
_haveErrors |= !_logicChannel.CrcIsOk;
_averageBer = _averageBer * 0.5f + _lowerMac.Ber * 0.5f;
//Debug.Write(" slot 1:" + (_logicChannel.CrcIsOk ? " OK" : " Err"));
if (_logicChannel.CrcIsOk)
{
_parse.SyncPDU(_logicChannel, _syncInfo);
if (_syncInfo.Value(GlobalNames.SystemCode) < 8)
{
TetraMode = Mode.TMO;
_lowerMac.ScramblerCode = TetraUtils.CreateScramblerCode(_syncInfo.Value(GlobalNames.MCC), _syncInfo.Value(GlobalNames.MNC), _syncInfo.Value(GlobalNames.ColorCode));
_networkTime.Synchronize(_syncInfo.Value(GlobalNames.TimeSlot), _syncInfo.Value(GlobalNames.Frame), _syncInfo.Value(GlobalNames.MultiFrame));
}
else
{
TetraMode = Mode.DMO;
if (_syncInfo.Value(GlobalNames.SYNC_PDU_type) == 0)
{
//Debug.Write(_syncInfo.Value(GlobalNames.Master_slave_link_flag) == 1 ? " Master" : " Slave");
if (_syncInfo.Value(GlobalNames.Master_slave_link_flag) == 1 || _syncInfo.Value(GlobalNames.Communication_type) == 0)
{
_networkTime.SynchronizeMaster(_syncInfo.Value(GlobalNames.TimeSlot), _syncInfo.Value(GlobalNames.Frame));
}
else
{
_networkTime.SynchronizeSlave(_syncInfo.Value(GlobalNames.TimeSlot), _syncInfo.Value(GlobalNames.Frame));
}
}
}
}
else
{
//Debug.WriteLine("Sync SB crc error");
}
_phyLevel.ExtractPhyChannels(TetraMode, burst, _bbBufferPtr, _bkn1BufferPtr, _bkn2BufferPtr);
if (TetraMode == Mode.TMO)
{
_logicChannel = _lowerMac.ExtractLogicChannelFromBKN(_bkn2BufferPtr, _bkn2Buffer.Length);
_logicChannel.TimeSlot = _networkTime.TimeSlot;
_logicChannel.Frame = _networkTime.Frame;
_badBurstCounter += _logicChannel.CrcIsOk ? 0.0f : 0.5f;
_haveErrors |= !_logicChannel.CrcIsOk;
_averageBer = _averageBer * 0.5f + _lowerMac.Ber * 0.5f;
//Debug.Write(" slot 2:" + (_logicChannel.CrcIsOk ? " OK" : " Err"));
if (_logicChannel.CrcIsOk)
{
_parse.TmoParseMacPDU(_logicChannel, _data);
}
else
{
//Debug.WriteLine("Sync BKN2 crc error");
}
}
else
{
_logicChannel = _lowerMac.ExtractLogicChannelFromBKN2(_bkn2BufferPtr, _bkn2Buffer.Length);
_logicChannel.TimeSlot = _networkTime.TimeSlot;
_logicChannel.Frame = _networkTime.Frame;
_badBurstCounter += _logicChannel.CrcIsOk ? 0.0f : 0.5f;
_haveErrors |= !_logicChannel.CrcIsOk;
_averageBer = _averageBer * 0.5f + _lowerMac.Ber * 0.5f;
//Debug.Write(" slot 2:" + (_logicChannel.CrcIsOk ? " OK" : " Err"));
if (_logicChannel.CrcIsOk)
{
_parse.SyncPDUHalfSlot(_logicChannel, _syncInfo);
if (_syncInfo.Value(GlobalNames.Communication_type) == 0)
{
if (_syncInfo.Contains(GlobalNames.MNC) && _syncInfo.Contains(GlobalNames.Source_address))
_lowerMac.ScramblerCode = TetraUtils.CreateScramblerCode(_syncInfo.Value(GlobalNames.MNC), _syncInfo.Value(GlobalNames.Source_address));
}
else if (_syncInfo.Value(GlobalNames.Communication_type) == 1)
{
if (_syncInfo.Contains(GlobalNames.Repeater_address) && _syncInfo.Contains(GlobalNames.Source_address))
_lowerMac.ScramblerCode = TetraUtils.CreateScramblerCode(_syncInfo.Value(GlobalNames.Repeater_address), _syncInfo.Value(GlobalNames.Source_address));
}
}
}
UpdateSyncInfo(_syncInfo);
}
#endregion
UpdatePublicProp();
#region Other burst
if (burst.Type != BurstType.SYNC)
{
_phyLevel.ExtractPhyChannels(TetraMode, burst, _bbBufferPtr, _bkn1BufferPtr, _bkn2BufferPtr);
if (TetraMode == Mode.TMO)
{
_logicChannel = _lowerMac.ExtractLogicChannelFromBB(_bbBufferPtr, _bbBuffer.Length);
_logicChannel.TimeSlot = _networkTime.TimeSlot;
_logicChannel.Frame = _networkTime.Frame;
_badBurstCounter += _logicChannel.CrcIsOk ? 0.0f : 0.2f;
_haveErrors |= !_logicChannel.CrcIsOk;
//Debug.Write(_logicChannel.CrcIsOk ? " BB OK" : " BB Err");
if (_logicChannel.CrcIsOk)
{
_parse.AccessAsignPDU(_logicChannel);
}
else
{
//Debug.WriteLine("BB crc error");
}
}
switch (burst.Type)
{
case BurstType.NDB1:
if (((TetraMode == Mode.TMO) && (_parse.DownLinkChannelType == ChannelType.Traffic))
|| ((TetraMode == Mode.DMO) && (!_networkTime.Frame18) && (_networkTime.TimeSlot == 1))
|| ((TetraMode == Mode.DMO) && (!_networkTime.Frame18Slave) && (_networkTime.TimeSlotSlave == 1)))
{
//Debug.Write(" slot 12:voice");
_logicChannel = _lowerMac.ExtractVoiceDataFromBKN1BKN2(_bkn1BufferPtr, _bkn2BufferPtr, _bkn1Buffer.Length);
var itsAudio = DecodeAudio(audioOut, _logicChannel.Ptr, _logicChannel.Length, false, _networkTime.TimeSlot);
trafficChannel = itsAudio ? _networkTime.TimeSlot : 0;
break;
}
else
{
_logicChannel = _lowerMac.ExtractLogicChannelFromBKN1BKN2(_bkn1BufferPtr, _bkn2BufferPtr, _bkn1Buffer.Length);
_logicChannel.TimeSlot = _networkTime.TimeSlot;
_logicChannel.Frame = _networkTime.Frame;
_badBurstCounter += _logicChannel.CrcIsOk ? 0.0f : 0.8f;
_haveErrors |= !_logicChannel.CrcIsOk;
_averageBer = _averageBer * 0.5f + _lowerMac.Ber * 0.5f;
//Debug.Write(" slot 12:" + (_logicChannel.CrcIsOk ? " OK" : " Err"));
if (_logicChannel.CrcIsOk)
{
if (TetraMode == Mode.TMO) _parse.TmoParseMacPDU(_logicChannel, _data);
else _parse.DmoParseMacPDU(_logicChannel, _data);
}
else
{
//Debug.WriteLine("BKN1 BKN2 crc error");
}
}
break;
case BurstType.NDB2:
_logicChannel = _lowerMac.ExtractLogicChannelFromBKN(_bkn1BufferPtr, _bkn1Buffer.Length);
_logicChannel.TimeSlot = _networkTime.TimeSlot;
_logicChannel.Frame = _networkTime.Frame;
_badBurstCounter += _logicChannel.CrcIsOk ? 0.0f : 0.4f;
_haveErrors |= !_logicChannel.CrcIsOk;
_averageBer = _averageBer * 0.5f + _lowerMac.Ber * 0.5f;
//Debug.Write(" slot 1:" + (_logicChannel.CrcIsOk ? " OK" : " Err"));
if (_logicChannel.CrcIsOk)
{
if (TetraMode == Mode.TMO) _parse.TmoParseMacPDU(_logicChannel, _data);
else _parse.DmoParseMacPDU(_logicChannel, _data);
}
else
{
//Debug.WriteLine("BKN1 crc error");
}
if ((_parse.DownLinkChannelType == ChannelType.Traffic && !_parse.HalfSlotStolen)
|| ((TetraMode == Mode.DMO) && (!_networkTime.Frame18) && (_networkTime.TimeSlot == 1) && (!_parse.HalfSlotStolen))
|| ((TetraMode == Mode.DMO) && (!_networkTime.Frame18Slave) && (_networkTime.TimeSlotSlave == 1) && (!_parse.HalfSlotStolen)))
{
//Debug.Write(" slot 2:voice");
_logicChannel = _lowerMac.ExtractVoiceDataFromBKN2(_bkn2BufferPtr, _bkn2Buffer.Length);
var itsAudio = DecodeAudio(audioOut, _logicChannel.Ptr, _logicChannel.Length, true, _networkTime.TimeSlot);
trafficChannel = itsAudio ? _networkTime.TimeSlot : 0;
break;
}
else
{
_logicChannel = _lowerMac.ExtractLogicChannelFromBKN(_bkn2BufferPtr, _bkn2Buffer.Length);
_logicChannel.TimeSlot = _networkTime.TimeSlot;
_logicChannel.Frame = _networkTime.Frame;
_badBurstCounter += _logicChannel.CrcIsOk ? 0.0f : 0.4f;
_haveErrors |= !_logicChannel.CrcIsOk;
_averageBer = _averageBer * 0.5f + _lowerMac.Ber * 0.5f;
//Debug.Write(" slot 2:" + (_logicChannel.CrcIsOk ? " OK" : " Err"));
if (_logicChannel.CrcIsOk)
{
if (TetraMode == Mode.TMO) _parse.TmoParseMacPDU(_logicChannel, _data);
else _parse.DmoParseMacPDU(_logicChannel, _data);
}
else
{
//Debug.WriteLine("BKN2 crc error");
}
}
break;
default:
break;
}
}
if (_data.Count > 0) UpdateData(_data);
#endregion
return trafficChannel;
}
private void UpdateSyncInfo(ReceivedData syncInfo)
{
if (SyncInfoReady != null)
{
_owner.BeginInvoke(SyncInfoReady, syncInfo);
}
}
public void UpdateData(List<ReceivedData> data)
{
if (DataReady != null)
{
_owner.BeginInvoke(DataReady, data.ToList());
}
}
private void UpdatePublicProp()
{
if (_networkTime != null)
{
NetworkTimeFN = _networkTime.Frame;
NetworkTimeTN = _networkTime.TimeSlot;
NetworkTimeMN = _networkTime.SuperFrame;
}
}
private bool DecodeAudio(float* audioBuffer, byte* buf, int length, bool stolten, int ch)
{
var noErrors = true;
fixed (short* cdcPtr = _cdc, sdcPtr = _sdc)
{
NativeMethods.tetra_cdec(_fpass, buf, cdcPtr, stolten ? 1 : 0);
_fpass = 0;
noErrors = (cdcPtr[0] == 0) || (cdcPtr[138] == 0);
_badBurstCounter += (cdcPtr[0] == 0 || stolten) ? 0 : 0.4f;
_badBurstCounter += cdcPtr[138] == 0 ? 0 : 0.4f;
//Debug.WriteIf(!stolten, " fr1_" + (cdcPtr[0] == 0 ? "Ok" : "Err"));
//Debug.Write(" fr2_" + (cdcPtr[138] == 0 ? "Ok" : "Err"));
var initStruct = _ch1InitStruct;
switch (ch)
{
case 1: initStruct = _ch1InitStruct; break;
case 2: initStruct = _ch2InitStruct; break;
case 3: initStruct = _ch3InitStruct; break;
case 4: initStruct = _ch4InitStruct; break;
}
NativeMethods.tetra_sdec(cdcPtr, sdcPtr, initStruct);
ShortToFloatPtr(sdcPtr, audioBuffer, _sdc.Length);
}
return noErrors;
}
private void ShortToFloatPtr(short* source, float* dest, int length)
{
var gain = 0.0001f / Int16.MaxValue;
for (int i = 0; i < length; i++)
{
dest[i] = source[i] * gain;
}
}
public float Mer { get; set; }
public Mode TetraMode { get; set; }
}
}