-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
TetraPanel.cs
1710 lines (1421 loc) · 60.4 KB
/
TetraPanel.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
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
using SDRSharp.Common;
using SDRSharp.Radio;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace SDRSharp.Tetra
{
public unsafe partial class TetraPanel : UserControl
{
private const float TwoPi = (float)(Math.PI * 2.0);
private const float Pi = (float)Math.PI;
private const float PiDivTwo = (float)(Math.PI / 2.0);
private const float PiDivFor = (float)(Math.PI / 4.0);
private const int SamplesPerSymbol = 4;
private const int BurstLengthBits = 510;
private const int BurstLengthSymbols = 255;
private const int SamplesPerBurst = BurstLengthSymbols * SamplesPerSymbol;
private const int ChannelActiveDelay = 10;
private ISharpControl _controlInterface;
private IFProcessor _ifProcessor;
private Demodulator _demodulator;
private TetraDecoder _decoder;
private UnsafeBuffer _iqBuffer;
private Complex* _iqBufferPtr;
private UnsafeBuffer _symbolsBuffer;
private float* _symbolsBufferPtr;
private ComplexFifoStream _radioFifoBuffer;
private UnsafeBuffer _displayBuffer;
private float* _displayBufferPtr;
private UnsafeBuffer _outAudioBuffer;
private float* _outAudioBufferPtr;
private UnsafeBuffer _resampledAudio;
private float* _resampledAudioPtr;
private Resampler _audioResampler;
private FloatFifoStream _audioStreamChannel;
private double _audioSamplerate;
private Thread _decodingThread;
private bool _decodingIsStarted;
private double _iqSamplerate;
private bool _processIsStarted;
private int _lostBuffers;
private bool _dispayBufferReady;
private TextFile _textFile = new TextFile();
private TetraSettings _tetraSettings;
private SettingsPersister _settingsPersister;
private bool _needDisplayBufferUpdate;
private AudioProcessor _audioProcessor;
private bool _showInfo;
private bool _needCloseInfo;
private bool _channel1Listen;
private bool _channel2Listen;
private bool _channel3Listen;
private bool _channel4Listen;
private bool _ch1IsActive;
private bool _ch2IsActive;
private bool _ch3IsActive;
private bool _ch4IsActive;
private bool _autoSelectChannel;
private int _activeCounter1;
private int _activeCounter2;
private int _activeCounter3;
private int _activeCounter4;
private NetInfoWindow _infoWindow;
private List<ReceivedData> _rawData = new List<ReceivedData>();
private List<ReceivedData> _cmceData = new List<ReceivedData>();
private List<ReceivedData> _syncData = new List<ReceivedData>();
private ReceivedData _syncInfo = new ReceivedData();
private ReceivedData _sysInfo = new ReceivedData();
private CurrentLoad[] _currentCellLoad = new CurrentLoad[4];
private int _currentCell_NMI;
private int _currentCell_MNC;
private int _currentCell_MCC;
private int _currentCell_LA;
private int _currentCell_CC;
private int _currentCell_Carrier;
private int _mainCell_Carrier;
private long _mainCell_Frequency;
private SortedDictionary<int, CallsEntry> _currentCalls = new SortedDictionary<int, CallsEntry>();
private Dictionary<int, NetworkEntry> _networkBase = new Dictionary<int, NetworkEntry>();
private int _resetCounter;
private bool _writerBlocked;
private const string DefaultLogEntryRules = "date + time + mcc + mnc + la + cc + carrier + slot + callid + type + from + to + encryption + duplex";
private const string DefaultLogFileNameRules = "date \\ frequency \\ mcc \"_\" mnc \"_\" la";
private const string DefaultLogSeparator = " ; ";
private bool _needGroupsUpdate;
private int _lastDateTime;
private float _prevAngle;
private int _afcCounter;
private double _freqError;
private float _averageAngle;
private bool _isAfcWork;
private Mode _tetraMode;
private UnsafeBuffer _diBitsBuffer;
private unsafe byte* _diBitsBufferPtr;
private const int CallTimeout = 10;
private int _currentChPriority;
#region Init and store settings
public unsafe TetraPanel(ISharpControl control)
{
try
{
InitializeComponent();
InitArrays();
_settingsPersister = new SettingsPersister("tetraSettings.xml");
_tetraSettings = _settingsPersister.ReadStored();
#region Default Settings
if (_tetraSettings.LogEntryRules == null || _tetraSettings.LogEntryRules == string.Empty)
_tetraSettings.LogEntryRules = DefaultLogEntryRules;
if (_tetraSettings.LogFileNameRules == null || _tetraSettings.LogFileNameRules == string.Empty)
_tetraSettings.LogFileNameRules = DefaultLogFileNameRules;
if (_tetraSettings.LogSeparator == null || _tetraSettings.LogSeparator == string.Empty)
_tetraSettings.LogSeparator = DefaultLogSeparator;
if (_tetraSettings.NetworkBase == null)
_tetraSettings.NetworkBase = new List<GroupsEntries>();
if (_tetraSettings.UdpPort == 0) _tetraSettings.UdpPort = 20025;
#endregion
UpdateGlobals();
blockNumericUpDown.Value = _tetraSettings.BlockedLevel;
_networkBase = NetworkBaseDeserializer(_tetraSettings.NetworkBase);
_needGroupsUpdate = true;
_controlInterface = control;
_infoWindow = new NetInfoWindow();
_infoWindow.FormClosing += _infoWindow_FormClosing;
_ifProcessor = new IFProcessor();
_controlInterface.RegisterStreamHook(_ifProcessor, ProcessorType.DecimatedAndFilteredIQ);
_ifProcessor.IQReady += IQSamplesAvailable;
_audioProcessor = new AudioProcessor();
_controlInterface.RegisterStreamHook(_audioProcessor, ProcessorType.DemodulatorOutput);
_audioProcessor.AudioReady += AudioSamplesNedeed;
_decoder = new TetraDecoder(this);
_decoder.DataReady += _decoder_DataReady;
_decoder.SyncInfoReady += _decoder_SyncInfoReady;
_demodulator = new Demodulator();
_controlInterface.PropertyChanged += _controlInterface_PropertyChanged;
_displayBuffer = UnsafeBuffer.Create(BurstLengthBits / 2, sizeof(float));
_displayBufferPtr = (float*)_displayBuffer;
_outAudioBuffer = UnsafeBuffer.Create(480, sizeof(float));
_outAudioBufferPtr = (float*)_outAudioBuffer;
_audioStreamChannel = new FloatFifoStream(BlockMode.None);
autoCheckBox.Checked = _tetraSettings.AutoPlay;
AutoCheckBox_CheckedChanged(null, null);
}
catch (Exception ex)
{
MessageBox.Show("Tetra plugin exception -" + ex.ToString());
}
}
private void InitArrays()
{
for (int i = 0; i < 4; i++)
{
_currentCellLoad[i] = new CurrentLoad();
}
}
private Dictionary<int, NetworkEntry> NetworkBaseDeserializer(List<GroupsEntries> list)
{
if (list.Count == 0) return new Dictionary<int, NetworkEntry>();
var result = new Dictionary<int, NetworkEntry>();
foreach (var entry in list)
{
if (result.ContainsKey(entry.NMI))
{
var newGroup = new GroupsEntry
{
Name = entry.Name,
Priority = entry.Priopity
};
result[entry.NMI].KnowGroups.Add(entry.GSSI, newGroup);
}
else
{
var newLine = new NetworkEntry
{
KnowGroups = new Dictionary<int, GroupsEntry>()
};
var newGroup = new GroupsEntry
{
Name = entry.Name,
Priority = entry.Priopity
};
newLine.KnowGroups.Add(entry.GSSI, newGroup);
result.Add(entry.NMI, newLine);
}
}
return result;
}
private List<GroupsEntries> NetworkBaseSerializer(Dictionary<int, NetworkEntry> dict)
{
if (dict.Count == 0) return new List<GroupsEntries>();
var result = new List<GroupsEntries>();
foreach (var entry in dict)
{
foreach (var groupEntry in entry.Value.KnowGroups)
{
var newEntry = new GroupsEntries
{
NMI = entry.Key,
GSSI = groupEntry.Key,
Name = groupEntry.Value.Name,
Priopity = groupEntry.Value.Priority
};
result.Add(newEntry);
}
}
return result;
}
private void _infoWindow_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
_needCloseInfo = true;
}
public void SaveSettings()
{
if (_processIsStarted) StopDecoding();
_tetraSettings.AutoPlay = autoCheckBox.Checked;
_tetraSettings.NetworkBase = NetworkBaseSerializer(_networkBase);
_settingsPersister.PersistStored(_tetraSettings);
}
private void _controlInterface_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "StartRadio":
break;
case "StopRadio":
break;
case "Frequency":
if (!_isAfcWork)
{
ResetDecoder();
}
_isAfcWork = false;
break;
case "DetectorType":
break;
}
}
private void ResetDecoder()
{
_resetCounter = 10;
_freqError = 0;
_currentCell_NMI = 0;
_currentCell_MNC = 0;
_currentCell_MCC = 0;
_currentCell_LA = 0;
_currentCell_CC = 0;
_mainCell_Frequency = 0;
_mainCell_Carrier = 0;
_activeCounter1 = 0;
_activeCounter2 = 0;
_activeCounter3 = 0;
_activeCounter4 = 0;
_ch1IsActive = false;
_ch2IsActive = false;
_ch3IsActive = false;
_ch4IsActive = false;
_currentChPriority = int.MinValue;
_needGroupsUpdate = true;
_currentCell_Carrier = (int)Math.Round((_controlInterface.Frequency % 100000000) / 25000.0);
_sysInfo.Clear();
_currentCalls.Clear();
_cmceData.Clear();
_rawData.Clear();
_syncInfo.Clear();
_infoWindow.ResetInfo();
InitArrays();
}
#endregion
#region DQPSK demodulator
private void StartDecoding()
{
_ifProcessor.Enabled = true;
_processIsStarted = true;
DecoderStart();
}
private void StopDecoding()
{
_ifProcessor.Enabled = false;
_processIsStarted = false;
DecoderStop();
}
/**
* Verify if SDR# it's ready to decode tetra signals.
*/
private bool CheckConditions()
{
this._ifProcessor.SampleRate = 25000;
this._controlInterface.StartRadio();
this._controlInterface.DetectorType = DetectorType.WFM;
this._controlInterface.FrequencyShift = 28000;
if (_ifProcessor.SampleRate < 25000)
{
if (System.Globalization.CultureInfo.CurrentCulture.Name == "ru-RU")
{
MessageBox.Show("Слишком низкая частота дискретизации IF. Измените вид модуляции на WFM или установите параметр minOutputSampleRate value = 32000", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("IF samplerate too low. Change the modulation type to WFM or set the parameter minOutputSampleRate value = 32000", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return false;
}
return true;
}
/// <summary>
/// @todo VERIFY!!!
/// </summary>
/// <param name="samples"></param>
/// <param name="samplerate"></param>
/// <param name="length"></param>
public unsafe void IQSamplesAvailable(Complex* samples, double samplerate, int length)
{
this._iqSamplerate = samplerate;
if (this._radioFifoBuffer == null)
this._radioFifoBuffer = new ComplexFifoStream(BlockMode.None);
if ((double)this._radioFifoBuffer.Length < samplerate)
this._radioFifoBuffer.Write(samples, length);
else
++this._lostBuffers;
}
private void AutomaticFrequencyControl(float* buffer, int length)
{
var found = false;
var resultAngel = 0.0f;
for (int i = 0; i < length; i++)
{
if (Math.Abs(buffer[i] - _prevAngle) < (PiDivFor))
{
_afcCounter++;
_averageAngle += buffer[i];
}
else
{
found = (_afcCounter == 31) || (_afcCounter == 32);
resultAngel = _averageAngle / _afcCounter;
_afcCounter = 0;
_averageAngle = 0;
}
_prevAngle = buffer[i];
if (found)
{
_freqError = _freqError * 0.9f + (0.1f * ((resultAngel - PiDivFor) / (TwoPi / 18000.0f)));
break;
}
}
}
#endregion
#region Tetra-decoder
private void DecoderStart()
{
_decodingIsStarted = true;
_decodingThread = new Thread(DecodingThread)
{
Priority = ThreadPriority.Normal,
Name = "TetraDecocerThread"
};
_decodingThread.Start();
_audioProcessor.Enabled = true;
_needDisplayBufferUpdate = true;
}
private void DecoderStop()
{
_audioProcessor.Enabled = false;
_decodingIsStarted = false;
if (_decodingThread != null)
{
_decodingThread.Join();
_decodingThread = null;
}
_controlInterface.AudioIsMuted = false;
}
/**
* Thread que espera a que se cargue el radiobuffer y luego lo procesa.
* Buffer de 510 Bits
* @todo verify and traspose
*/
private unsafe void DecodingThread()
{
_iqBuffer = UnsafeBuffer.Create(SamplesPerBurst, sizeof(Complex));
_iqBufferPtr = (Complex*)_iqBuffer;
_symbolsBuffer = UnsafeBuffer.Create(BurstLengthSymbols, sizeof(float));
_symbolsBufferPtr = (float*)_symbolsBuffer;
_diBitsBuffer = UnsafeBuffer.Create(BurstLengthBits, sizeof(byte));
_diBitsBufferPtr = (byte*)_diBitsBuffer;
UdpClient server = new UdpClient("127.0.0.1", _tetraSettings.UdpPort);
var audioSamplerate = 0d;
var burst = new Burst
{
Ptr = _diBitsBufferPtr
};
while (this._decodingIsStarted)
{
if ((_radioFifoBuffer == null) || (_radioFifoBuffer.Length < SamplesPerBurst) || (_iqSamplerate == 0))
{
Thread.Sleep(10);
continue;
}
burst.Mode = this._tetraMode;
///@todo CRITICAL PENDING
///Original
///_radioFifoBuffer.Read(_iqBufferPtr, SamplesPerBurst);
///_demodulator.ProcessBuffer(burst, _iqBufferPtr, _symbolsBufferPtr);
///SamplesPerBurst 255 *4 -> 1020
///BurstLengthBits -> 510
///Con 1024 no encuentra los paquetes!!!
this._radioFifoBuffer.Read(this._iqBufferPtr, BurstLengthBits);
this._demodulator.ProcessBuffer(burst, this._iqBufferPtr, this._iqSamplerate, BurstLengthBits, this._symbolsBufferPtr);
/// END CRITICAL
if (burst.Type == BurstType.WaitBurst)
continue;
AutomaticFrequencyControl(_symbolsBufferPtr, BurstLengthSymbols);
if (_tetraSettings.UdpEnabled)
{
server.SendAsync(ConvertAngleToDiBits(_symbolsBufferPtr, BurstLengthSymbols), BurstLengthBits);
}
var audioChannel = this._decoder.Process(burst, this._outAudioBufferPtr);
_tetraMode = _decoder.TetraMode;
if (_needDisplayBufferUpdate)// && _decoder.HaveErrors)
{
_needDisplayBufferUpdate = false;
Utils.Memcpy(_displayBufferPtr, _symbolsBufferPtr, _displayBuffer.Length * sizeof(float));
_dispayBufferReady = true;
}
if (audioChannel == 0 || _audioSamplerate == 0) continue;
if (audioSamplerate != _audioSamplerate)
{
audioSamplerate = _audioSamplerate;
_audioResampler = new Resampler(8000, audioSamplerate);
_resampledAudio = UnsafeBuffer.Create((int)audioSamplerate, sizeof(float));
_resampledAudioPtr = (float*)_resampledAudio;
}
switch (audioChannel)
{
case 1:
_ch1IsActive = true;
_activeCounter1 = ChannelActiveDelay;
if (!_channel1Listen) continue;
break;
case 2:
_ch2IsActive = true;
_activeCounter2 = ChannelActiveDelay;
if (!_channel2Listen) continue;
break;
case 3:
_ch3IsActive = true;
_activeCounter3 = ChannelActiveDelay;
if (!_channel3Listen) continue;
break;
case 4:
_ch4IsActive = true;
_activeCounter4 = ChannelActiveDelay;
if (!_channel4Listen) continue;
break;
}
//resample buffer
var audioLength = _audioResampler.Process(_outAudioBufferPtr, _resampledAudioPtr, _outAudioBuffer.Length);
//Clone to stereo
// audioLength = MonoToStereo(_resampledAudioPtr, audioLength);
// Copy to output fifo
_audioStreamChannel.Write(_resampledAudioPtr, audioLength);
}
_iqBuffer.Dispose();
_iqBuffer = null;
_iqBufferPtr = null;
_symbolsBuffer.Dispose();
_symbolsBuffer = null;
_symbolsBufferPtr = null;
_diBitsBuffer.Dispose();
_diBitsBuffer = null;
_diBitsBufferPtr = null;
}
private void ConvertAngleToDiBits(byte* bitsBuffer, float* angles, int sourceLength)
{
float delta;
while (sourceLength-- > 0)
{
delta = *angles++;
*bitsBuffer++ = delta < 0 ? (byte)1 : (byte)0;
*bitsBuffer++ = Math.Abs(delta) > PiDivTwo ? (byte)1 : (byte)0;
}
}
private byte[] ConvertAngleToDiBits(float* angles, int sourceLength)
{
var bitsBuffer = new byte[sourceLength * 2];
float delta;
int indexout = 0;
while (sourceLength-- > 0)
{
delta = *angles++;
bitsBuffer[indexout++] = delta < 0 ? (byte)1 : (byte)0;
bitsBuffer[indexout++] = Math.Abs(delta) > PiDivTwo ? (byte)1 : (byte)0;
}
return bitsBuffer;
}
private int MonoToStereo(float* buffer, int monoLength)
{
var monoIndex = monoLength - 1;
var stereoIndex = monoLength * 2 - 1;
for (int i = 0; i < monoLength; i++)
{
buffer[stereoIndex--] = buffer[monoIndex];
buffer[stereoIndex--] = buffer[monoIndex];
monoIndex--;
}
return monoLength * 2;
}
#endregion
#region Audio Out
public void AudioSamplesNedeed(float* samples, double samplerate, int length)
{
_audioSamplerate = samplerate;
if (_audioStreamChannel.Length >= length)
{
_audioStreamChannel.Read(samples, length);
}
else
{
for (int i = 0; i < length; i++)
{
samples[i] = 0;
}
}
}
#endregion
#region Received Data Extractor
//private const int CallTimeout = 10;
//private int _currentChPriority;
void _decoder_DataReady(List<ReceivedData> data)
{
////Debug.WriteLine("data delegate " + Thread.CurrentThread.ManagedThreadId.ToString());
if (_resetCounter > 0)
{
return;
}
while (data.Count > 0)
{
if (_rawData.Count < 100)
_rawData.Add(data[0]);
data.RemoveAt(0);
}
}
public void UpdateCallsInfo(ReceivedData data)
{
if (!data.Contains(GlobalNames.CMCE_Primitives_Type)) return;
var callId = 0;
if (!data.TryGetValue(GlobalNames.Call_identifier, ref callId))
return;
var ssi = 0;
var type = 0;
var value = 0;
var logEvent = false;
if (!_networkBase.ContainsKey(_currentCell_NMI))
{
var entry = new NetworkEntry
{
KnowGroups = new Dictionary<int, GroupsEntry>()
};
_networkBase.Add(_currentCell_NMI, entry);
var newGroup = new GroupsEntry
{
Name = "Individual",
Priority = 0
};
_networkBase[_currentCell_NMI].KnowGroups.Add(0, newGroup);
_needGroupsUpdate = true;
}
data.TryGetValue(GlobalNames.SSI, ref ssi);
if (data.TryGetValue(GlobalNames.Basic_service_Communication_type, ref value))
{
type = value;
if (value == (int)CommunicationType.Group)
{
if (!_networkBase[_currentCell_NMI].KnowGroups.ContainsKey(ssi))
{
var newGroup = new GroupsEntry
{
Name = string.Empty,
Priority = 0
};
_networkBase[_currentCell_NMI].KnowGroups.Add(ssi, newGroup);
_needGroupsUpdate = true;
}
}
}
if (!_currentCalls.ContainsKey(callId))
{
var entry = new CallsEntry
{
To = ssi,
CallID = callId,
From = 0,
Type = type,
IsClear = 0,
Duplex = 0
};
_currentCalls.Add(callId, entry);
logEvent = true;
}
_currentCalls[callId].To = ssi;
if (data.TryGetValue(GlobalNames.Basic_service_Communication_type, ref value))
{
_currentCalls[callId].Type = value;
}
if (data.TryGetValue(GlobalNames.Carrier_number, ref value))
{
_currentCalls[callId].Carrier = value;
}
_currentCalls[callId].WatchDog = 5;
var transmGrant = 0;
var timeslot = data.Value(GlobalNames.CurrTimeSlot);
var assignedSlot = -1;
var fromNew = -1;
switch ((CmcePrimitivesType)data.Value(GlobalNames.CMCE_Primitives_Type))
{
case CmcePrimitivesType.D_Disconnect:
case CmcePrimitivesType.D_Release:
assignedSlot = 0;
fromNew = 0;
_currentCalls[callId].WatchDog = 5;
break;
case CmcePrimitivesType.D_TX_Ceased:
fromNew = 0;
_currentCalls[callId].WatchDog = 5;
break;
case CmcePrimitivesType.D_Info:
if (data.TryGetValue(GlobalNames.Slot_granting_element, ref value))
{
if (data.TryGetValue(GlobalNames.SSI, ref value))
{
fromNew = value;
}
if (data.TryGetValue(GlobalNames.Timeslot_assigned, ref value))
{
assignedSlot = value;
}
else
{
assignedSlot = 0x10 >> timeslot;
}
}
break;
case CmcePrimitivesType.D_Connect:
case CmcePrimitivesType.D_Setup:
case CmcePrimitivesType.D_TX_Granted:
if (data.TryGetValue(GlobalNames.Calling_party_address_SSI, ref value))
{
_currentCalls[callId].From = value;
}
if (data.TryGetValue(GlobalNames.Transmitting_party_address_SSI, ref value))
{
_currentCalls[callId].From = value;
}
var pduEncrypted = false;
var baseEncrypted = false;
var encrypt = false;
if (data.TryGetValue(GlobalNames.Encryption_mode, ref value))
{
pduEncrypted = value != 0;
}
if (data.TryGetValue(GlobalNames.Encryption_control, ref value))
{
encrypt = value != 0;
}
if (data.TryGetValue(GlobalNames.Basic_service_Encryption_flag, ref value))
{
baseEncrypted = value != 0;
}
_currentCalls[callId].IsClear = (!pduEncrypted && !baseEncrypted && !encrypt) ? 1 : 0;
if (data.TryGetValue(GlobalNames.Simplex_duplex, ref value))
{
_currentCalls[callId].Duplex = value;
}
if (data.TryGetValue(GlobalNames.Timeslot_assigned, ref value))
{
assignedSlot = value;
}
else
{
assignedSlot = 0x10 >> timeslot;
}
if (data.TryGetValue(GlobalNames.Transmission_grant, ref transmGrant))
{
switch ((TransmissionGranted)transmGrant)
{
case TransmissionGranted.Granted:
if (data.TryGetValue(GlobalNames.SSI, ref value))
{
fromNew = value;
}
break;
case TransmissionGranted.Granted_to_another_user:
if (data.TryGetValue(GlobalNames.Calling_party_address_SSI, ref value))
{
fromNew = value;
}
else if (data.TryGetValue(GlobalNames.Transmitting_party_address_SSI, ref value))
{
fromNew = value;
}
break;
default:
fromNew = 0;
break;
}
}
_currentCalls[callId].WatchDog = 20;
break;
}
if (assignedSlot != -1)
{
_currentCalls[callId].AssignedSlot = assignedSlot;
}
if (fromNew != -1)
{
if (_currentCalls[callId].From != fromNew)
{
_currentCalls[callId].From = fromNew;
logEvent = true;
}
}
if (logEvent)
{
Log_Tick(_currentCalls[callId]);
}
}
void _decoder_SyncInfoReady(ReceivedData syncInfo)
{
if (_resetCounter > 0)
{
return;
}
syncInfo.Data.CopyTo(_syncInfo.Data, 0);
}
private void UpdateSysInfo(ReceivedData data)
{
if ((MAC_PDU_Type)data.Value(GlobalNames.MAC_PDU_Type) == MAC_PDU_Type.Broadcast)
{
data.Data.CopyTo(_sysInfo.Data, 0);
_sysInfo.TryGetValue(GlobalNames.Location_Area, ref _currentCell_LA);
var band = 0;
var offset = 0;
var carrier = 0;
var isFull = data.TryGetValue(GlobalNames.Frequency_Band, ref band)
&& data.TryGetValue(GlobalNames.Main_Carrier, ref carrier)
&& data.TryGetValue(GlobalNames.Offset, ref offset);
_mainCell_Frequency = Global.FrequencyCalc(isFull, carrier, band, offset);
_mainCell_Carrier = carrier;
_currentCell_Carrier = Global.CarrierCalc(_controlInterface.Frequency);
}
}
#endregion
#region GUI events
private void MarkerTimer_Tick(object sender, EventArgs e)
{
if (_displayBuffer != null)
{
if (_dispayBufferReady)
{
_dispayBufferReady = false;
display.Perform(_displayBufferPtr, _displayBuffer.Length);
display.Refresh();
_needDisplayBufferUpdate = true;
}
}
if (!_processIsStarted) return;
label1.Text = _currentCellLoad[0].From.ToString();
ch1RadioButton.ForeColor = _ch1IsActive ? Color.Red : Color.Gray;
label2.Text = _currentCellLoad[1].From.ToString();
ch2RadioButton.ForeColor = _ch2IsActive ? Color.Red : Color.Gray;
label3.Text = _currentCellLoad[2].From.ToString();
ch3RadioButton.ForeColor = _ch3IsActive ? Color.Red : Color.Gray;
label4.Text = _currentCellLoad[3].From.ToString();
ch4RadioButton.ForeColor = _ch4IsActive ? Color.Red : Color.Gray;
label9.Text = (_currentCellLoad[0].Type == 1 ? "g " : "") + _currentCellLoad[0].GroupName;
label8.Text = (_currentCellLoad[1].Type == 1 ? "g " : "") + _currentCellLoad[1].GroupName;
label7.Text = (_currentCellLoad[2].Type == 1 ? "g " : "") + _currentCellLoad[2].GroupName;
label6.Text = (_currentCellLoad[3].Type == 1 ? "g " : "") + _currentCellLoad[3].GroupName;
_activeCounter1--;
if (_activeCounter1 < 0)
{
_activeCounter1 = 0;
_ch1IsActive = false;
}
_activeCounter2--;
if (_activeCounter2 < 0)
{
_activeCounter2 = 0;
_ch2IsActive = false;
}