-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPackets.pm
497 lines (439 loc) · 14.9 KB
/
Packets.pm
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
package Packets;
# Packets.pm
use strict;
use warnings;
use Switch;
use Config::IniFiles;
use IO::Socket::IP;
use Term::ANSIColor;
# Local modules:
use FindBin 1.51 qw( $RealBin );
use lib $RealBin;
use CiscoSTUN;
use SuperFrame;
our $Mode;
our $Hotkeys;
my $LocalHost;
my $LocalHostIP;
my $IPV6Addr;
our $PriorityTG;
my $Hangtime;
my $MuteTGTimeout;
our $UseVoicePrompts;
our $UseLocalCourtesyTone;
our $UseRemoteCourtesyTone;
my $SiteName;
my $SiteInfo;
my $Verbose;
our $SuperFrame;
my $TalkGroupsFile;
our %TG;
our $Scan = 0;
my $LocalActive = 0;
our $PauseScan = 0;
my $ValidNteworkTG = 0;
our $Pending_CourtesyTone = 0;
my $PauseTGScanTime = 0;
my $LinkedTalkGroup;
our $Pending_VA = 0;
our $VA_Message = 0;
our $Trigger = 0;
##################################################################
# Settings #######################################################
##################################################################
sub Load_Settings {
my ($ConfigFile) = @_;
# Load Settings ini file.
print color('green'), "Loading Settings...\n", color('reset');
my $cfg = Config::IniFiles->new( -file => $ConfigFile);
# Settings:
$Mode = $cfg->val('Settings', 'HardwareMode'); #0 = v.24, no other modes coded at the momment.
$Packets::HotKeys = $cfg->val('Settings', 'HotKeys');
$LocalHost = $cfg->val('Settings', 'LocalHost');
#my($LocalIPAddr) = inet_ntoa((gethostbyname(hostname))[4]);
($LocalHostIP) = inet_ntoa((gethostbyname($LocalHost))[4]);
$IPV6Addr = $cfg->val('Settings', 'IPV6Addr');
# my($IPV6AddrIP) = inet_ntop((gethostbyname($IPV6Addr))[4]);
$PriorityTG = $cfg->val('Settings', 'PriorityTG');
$Hangtime = $cfg->val('Settings', 'Hangtime');
$MuteTGTimeout = $cfg->val('Settings', 'MuteTGTimeout');
$UseVoicePrompts = $cfg->val('Settings', 'UseVoicePrompts');
$UseLocalCourtesyTone = $cfg->val('Settings', 'UseLocalCourtesyTone');
$UseRemoteCourtesyTone = $cfg->val('Settings', 'UseRemoteCourtesyTone');
$SiteName = $cfg->val('Settings', 'SiteName');
$SiteInfo = $cfg->val('Settings', 'SiteInfo');
$Verbose = $cfg->val('Settings', 'Verbose');
$SuperFrame = $cfg->val('P25Link', 'SuperFrame');
print " Mode = $Mode\n";
print " HotKeys = $Packets::HotKeys\n";
print " LocalHost = $LocalHost ntoa($LocalHostIP)\n";
print " IPV6Addr = $IPV6Addr \n";
print " Mute Talk Group Timeout = $MuteTGTimeout seconds.\n";
print " Use Voice Prompts = $UseVoicePrompts\n";
print " Use Local Courtesy Tone = $UseLocalCourtesyTone\n";
print " Use Remote Courtesy Tone = $UseRemoteCourtesyTone\n";
print " Site Name = $SiteName\n";
print " Site Info = $SiteInfo\n";
print " Verbose = $Verbose\n";
print " SuperFrame = $SuperFrame\n";
if ($SiteName eq "Default" or $SiteInfo eq "Default") {
die("Please review all fields on your config file, you can not use Default as a value.");
}
print "----------------------------------------------------------------------\n";
# TalkGroups:
print color('green'), "Init TalkGroups...\n", color('reset');
$TalkGroupsFile = $cfg->val('TalkGroups', 'HostsFile');
print " Talk Groups File: " . $TalkGroupsFile ."\n";
my $fh;
print " Loading TalkGroupsFile...\n";
if (!open($fh, "<", $TalkGroupsFile)) {
warn " *** Error *** File $TalkGroupsFile not found.\n";
} else {
print " File Ok.\n";
my %result;
while (my $Line = <$fh>) {
chomp $Line;
## skip comments and blank lines and optional repeat of title line
next if $Line =~ /^\#/ || $Line =~ /^\s*$/ || $Line =~ /^\+/;
#split each line into array
#my @Line = split(/\s+/, $Line);
my @Line = split(/\t+/, $Line);
my $TalkGroup = $Line[0];
$TG{$TalkGroup}{'TalkGroup'} = $Line[0];
$TG{$TalkGroup}{'Mode'} = $Line[1];
$TG{$TalkGroup}{'MMDVM_URL'} = $Line[2];
$TG{$TalkGroup}{'MMDVM_Port'} = $Line[3];
$TG{$TalkGroup}{'Scan'} = $Line[4];
$TG{$TalkGroup}{'Linked'} = 0;
$TG{$TalkGroup}{'P25Link_Connected'} = 0;
$TG{$TalkGroup}{'P25NX_Connected'} = 0;
$TG{$TalkGroup}{'MMDVM_Connected'} = 0;
if ($Verbose) {
print " TG Index " . $TalkGroup;
print ", Mode " . $TG{$TalkGroup}{'Mode'};
print ", URL " . $TG{$TalkGroup}{'MMDVM_URL'};
print ", Port " . $TG{$TalkGroup}{'MMDVM_Port'};
print ", Scan " . $TG{$TalkGroup}{'Scan'};
print "\n";
}
}
close $fh;
# System Call:
my $TalkGroup = 65535;
$TG{$TalkGroup}{'TalkGroup'} = $TalkGroup;
$TG{$TalkGroup}{'Mode'} = 'Local';
$TG{$TalkGroup}{'MMDVM_URL'} = '';
$TG{$TalkGroup}{'MMDVM_Port'} = 0;
$TG{$TalkGroup}{'Scan'} = 0;
$TG{$TalkGroup}{'Linked'} = 0;
$TG{$TalkGroup}{'P25Link_Connected'} = 0;
$TG{$TalkGroup}{'P25NX_Connected'} = 0;
$TG{$TalkGroup}{'MMDVM_Connected'} = 0;
if ($Verbose > 2) {
foreach my $key (keys %TG)
{
print " Key field: $key\n";
foreach my $key2 (keys %{$TG{$key}})
{
print " - $key2 = $TG{$key}{$key2}\n";
}
}
}
}
my $NumberOfTalkGroups = scalar keys %TG;
print "\n Total number of Internet TGs is: " . $NumberOfTalkGroups . "\n";
if ($NumberOfTalkGroups <= 0) {
warn "*** Error***\tNo hosts.\n";
die;
}
print "----------------------------------------------------------------------\n";
}
sub InitScanList {
print color('green'), "Init Priority and Scan TGs...\n", color('reset');
$ValidNteworkTG = 0;
# Voice Announce.
$Pending_CourtesyTone = 0;
# Connect to Priority and scan TGs.
$LocalActive = 0;
$PauseScan = 0;
$PauseTGScanTime = P25Link::GetTickCount();
$LinkedTalkGroup = $PriorityTG;
foreach my $key (keys %TG) {
if ($TG{$key}{'Scan'}) {
print " Scan TG " . $key . "\n";
AddLinkTG($key, 0);
}
}
if ($PriorityTG > 10) {
if (!$TG{$PriorityTG}{'Scan'}) {
$TG{$PriorityTG}{'Scan'} = 100;
}
AddLinkTG($PriorityTG, 0);
}
print "----------------------------------------------------------------------\n";
}
##################################################################
# Traffic control ################################################
##################################################################
sub Tx_to_Network {
my ($Buffer) = @_;
if (($LinkedTalkGroup <= 10) or ($ValidNteworkTG == 0)) {
return;
}
if ($Verbose) {print color('grey12'), "Tx_to_Network " . $TG{$LinkedTalkGroup}{'Mode'} .
" TalkGroup " . $LinkedTalkGroup . "\n", color('reset'); }
# Fix Astro TAC RT/RT
if ($Quantar::HDLC_ATAC_Enabled) {
if ( (substr($Buffer, 0 , 1) eq chr(0x60)) or (substr($Buffer, 0 , 1) eq chr(0x62)) or
(substr($Buffer, 0 , 1) eq chr(0x6B)) or (substr($Buffer, 0 , 1) eq chr(0xA1)) ) {
#print ("Hey\n");
#Bytes_2_HexString($Buffer);
$Buffer = substr($Buffer, 0, 2) . chr(0x02) . substr($Buffer, 3, length($Buffer) - 3);
#print ("New\n");
#Bytes_2_HexString($Buffer);
}
}
if ( P25Link::GetEnabled() and ($TG{$LinkedTalkGroup}{'Mode'} eq 'P25Link') and
($LinkedTalkGroup > 10) and ($LinkedTalkGroup < 65535) ) { # Case P25Link.
HDLC_to_P25Link($Buffer);
return;
}
if ( P25NX::GetEnabled() and ($TG{$LinkedTalkGroup}{'Mode'} eq 'P25NX') and
($LinkedTalkGroup >= 10100) and ($LinkedTalkGroup < 10600) ) { # case P25NX.
HDLC_to_P25NX($Buffer);
return;
}
if ( MMDVM::GetEnabled() and ($TG{$LinkedTalkGroup}{'Mode'} eq 'MMDVM') and
($LinkedTalkGroup > 10) and ($LinkedTalkGroup < 65535) ) { # Case MMDVM.
HDLC_to_MMDVM($LinkedTalkGroup, $Buffer);
}
}
sub HDLC_to_MMDVM {
my ($TalkGroup, $Buffer) = @_;
my $OpCode = ord(substr($Buffer, 2 , 1));
switch ($OpCode) {
case 0x00 {
switch (ord(substr($Buffer, 5, 1))) {
case 0x0C {
if ($Verbose) {print color('yellow'), "HDLC_to_MMDVM A output:\n", color('reset');}
if ($MMDVM::Version2 == 0) {
MMDVM::Tx($TalkGroup, chr(0x72) . chr(0x7B) .
chr(0x3D) . chr(0x9E) . chr(0x44) . chr(0x00) );
}
}
case 0x25 {
if ($Verbose) {print "HDLC_to_MMDVM ICW Terminate output:\n";}
if ($MMDVM::Version2 == 0) {
MMDVM::Tx($TalkGroup, chr(0x80) . chr(0x00). chr(0x00) .
chr(0x00) . chr(0x00) . chr(0x00) . chr(0x00) .
chr(0x00) . chr(0x00) . chr(0x00) . chr(0x00) .
chr(0x00) . chr(0x00) . chr(0x00) . chr(0x00) .
chr(0x00) );
}
}
}
}
case [0x60..0x61] {
$Buffer = substr($Buffer, 2, length($Buffer)); # Here we remove first 2 Quantar Bytes.
if ($Verbose) {print "HDLC_to_MMDVM Header output:\n";}
if ($Verbose >= 2) {P25Link::Bytes_2_HexString($Buffer);}
if ($MMDVM::Version2) {
SuperFrame::UpdateSuperFrame($TalkGroup, $Buffer);
} else {
MMDVM::Tx($TalkGroup, $Buffer);
}
}
case [0x62..0x73] {
$Buffer = substr($Buffer, 2, length($Buffer)); # Here we remove first 2 Quantar Bytes.
if ($Verbose) {print "HDLC_to_MMDVM Voice output:\n";}
if ($Verbose >= 2) {P25Link::Bytes_2_HexString($Buffer);}
if ($MMDVM::Version2) {
SuperFrame::UpdateSuperFrame($TalkGroup, $Buffer);
} else {
MMDVM::Tx($TalkGroup, $Buffer);
}
}
else {
warn "HDLC_to_MMDVM Error code " . (ord(substr($Buffer, 2, 1))) . "\n";
P25Link::Bytes_2_HexString($Buffer);
return;
}
}
}
sub HDLC_to_P25Link {
my ($Buffer) = @_;
my $Stun_Header = chr(0x08) . chr(0x31) . chr(0x00) . chr(0x00) . chr(0x00) .
chr(2 + length($Buffer)) . chr(CiscoSTUN::GetSTUN_ID()); #STUN Header.
$Buffer = $Stun_Header . $Buffer;
#print "HDLC_to_P25Link.\n";
if ($SuperFrame) {
SuperFrame::UpdateSuperFrame($LinkedTalkGroup, $Buffer);
} else {
P25Link::Tx($LinkedTalkGroup, $Buffer);
}
}
sub HDLC_to_P25NX {
my ($Buffer) = @_;
my $Stun_Header = chr(0x08) . chr(0x31) . chr(0x00) . chr(0x00) . chr(0x00) .
chr(2 + length($Buffer)) . chr(CiscoSTUN::GetSTUN_ID()); #STUN Header.
$Buffer = $Stun_Header . $Buffer;
print "HDLC_to_P25NX.\n";
P25NX::Tx($LinkedTalkGroup, $Buffer);
}
##################################################################
sub AddLinkTG {
my ($TalkGroup, $DoPauseScan) = @_;
if ($DoPauseScan == 1) {
PauseTGScan();
}
# Local TGs. Keep them Local.
if (($TalkGroup > 0) and ($TalkGroup <= 10)) {
if ($TalkGroup != $LinkedTalkGroup) {
RDAC::Tx($TalkGroup, Quantar::GetLocalRx(), Quantar::GetLocalTx(),
Quantar::GetIsDigitalVoice(), Quantar::GetIsPage(), Quantar::GetStationType());
$VA_Message = $TalkGroup; # Select VA.
$Pending_VA = 1;
}
$LinkedTalkGroup = $TalkGroup;
$ValidNteworkTG = 0;
print color('blue'), "Local Talk Group $TalkGroup.\n", color('reset');
return;
} elsif ($TG{$TalkGroup}{'Linked'} eq '') { # Undefined TG > 10, keep it local only.
$TG{$TalkGroup}{'Linked'} = '';
$ValidNteworkTG = 0;
print color('yellow'), "Undefined TG $TalkGroup.\n", color('reset');
return;
} elsif ($TG{$TalkGroup}{'Linked'} == 1) { # Defined TG, and linked.
if ($TalkGroup != $LinkedTalkGroup) {
$VA_Message = $TalkGroup; # Select VA.
$Pending_VA = 1;
}
$LinkedTalkGroup = $TalkGroup;
$ValidNteworkTG = 1;
if ($TG{$TalkGroup}{'Scan'} == 0) {
$TG{$TalkGroup}{'Timer'} = P25Link::GetTickCount();
}
print color('blue'), " System already linked to TG $TalkGroup.\n", color('reset');
return;
}
# Defined TG but not linked, create a link by connecting to the network.
if (P25Link::GetEnabled() and ($TG{$TalkGroup}{'Mode'} eq 'P25Link')) { # Case P25Link.
P25Link::JoinTG($TalkGroup);
} elsif ( P25NX::GetEnabled() and ($TalkGroup >= 10100) and ($TalkGroup < 10600)
and ($TG{$TalkGroup}{'Mode'} eq 'P25NX')) { # case P25NX.
P25NX::JoinTG($TalkGroup);
} elsif ( MMDVM::GetEnabled() and ($TG{$TalkGroup}{'Mode'} eq 'MMDVM') ) { # Case MMDVM.
# Disconnect previous Reflector
if (MMDVM::GetLinkedTG() != 0 ) {
RemoveLinkTG (MMDVM::GetLinkedTG());
}
# Search if reflector exist
if (exists($TG{$TalkGroup}{'MMDVM_URL'}) != 1) {
if ($Verbose) {warn color('red'), "This is a local only TG and program shold not reach here.\n",
color('reset');}
return;
}
# Connect to TG.
MMDVM::Join($TalkGroup, $TG{$TalkGroup}{'MMDVM_URL'}, $TG{$TalkGroup}{'MMDVM_Port'});
}
# Finalize linking new TG.
$TG{$TalkGroup}{'Linked'} = 1;
$LinkedTalkGroup = $TalkGroup;
$ValidNteworkTG = 1;
if ($TG{$TalkGroup}{'Scan'} == 0) {
$TG{$TalkGroup}{'Timer'} = P25Link::GetTickCount();
}
$VA_Message = $TalkGroup; # Linked TalkGroup.
$Pending_VA = 1;
print color('grey12'), " System Linked to TG $TalkGroup\n", color('reset');
}
sub RemoveLinkTG {
my ($TalkGroup) = @_;
if ($TG{$TalkGroup}{'Linked'} == 0) {
return;
}
print color('magenta'), "RemoveLinkTG $TalkGroup\n", color('reset');
# Disconnect from current network.
if ($TG{$TalkGroup}{'MMDVM_Connected'}) {
MMDVM::WriteUnlink($TalkGroup);
MMDVM::SetLinkedTG(0);
}
if ($TG{$TalkGroup}{'MMDVM_Connected'}) { MMDVM::WriteUnlink($TalkGroup); }
if ($TG{$TalkGroup}{'P25Link_Connected'}) { P25Link::Disconnect($TalkGroup); }
if ($TG{$TalkGroup}{'P25NX_Connected'}) { P25NX::Disconnect($TalkGroup); }
$TG{$TalkGroup}{'Linked'} = 0;
#print " System Disconnected from TG $TalkGroup\n";
}
sub Disconnect {
foreach my $key (keys %TG){ # Close Socket connections:
if (($TG{$key}{'MMDVM_Connected'} >= 1) or ($TG{$key}{'P25Link_Connected'} >= 1) or
($TG{$key}{'P25NX_Connected'} >= 1)) {
RemoveLinkTG($key);
}
}
}
sub PauseTGScan {
print color('yellow'), "PauseTGScan starting.\n", color('reset');
$PauseScan = 1;
$PauseTGScanTime = P25Link::GetTickCount() + $MuteTGTimeout;
}
sub PauseTGScan_Timer {
if (($PauseScan == 1) and (P25Link::GetTickCount() >= $PauseTGScanTime)) {
print color('yellow'), "PauseTGScan_Timer expired.\n", color('reset');
$PauseScan = 0;
RDAC::Tx($LinkedTalkGroup, Quantar::GetLocalRx(), Quantar::GetLocalTx(),
Quantar::GetIsDigitalVoice(), Quantar::GetIsPage(), Quantar::GetStationType());
#$VA_Message = 0xFFFF13; # Default Revert.
#$Pending_VA = 1;
}
}
sub RemoveDynamicTGLink {
foreach my $key (keys %TG) {
if ( ($TG{$key}{'P25Link_Connected'} or $TG{$key}{'P25NX_Connected'} or
$TG{$key}{'MMDVM_Connected'} ) and (Packets::GetTGScan($key) == 0) ) {
if (P25Link::GetTickCount() > ($TG{$key}{'Timer'} + $Hangtime)) {
print color('yellow'), "RemoveDinamicTGLink $key.\n", color('reset');
RemoveLinkTG($key);
RDAC::Tx($PriorityTG, Quantar::GetLocalRx(), Quantar::GetLocalTx(),
Quantar::GetIsDigitalVoice(), Quantar::GetIsPage(), Quantar::GetStationType());
$VA_Message = 0xFFFF13; # Default Revert.
$Pending_VA = 1;
}
}
}
}
sub TxLossTimeout_Timer { # End of Tx timmer (1 sec).
if ((Quantar::GetLocalRx() > 0) and (P25Link::GetTickCount() >= int(Quantar::GetLocalRx_Time()) + 2)) {
print color('green'), "TxLosTimeout_Timer event " . Quantar::GetLocalRx() . "\n", color('reset');
#if (int(Quant::GetLocalRx_Time()) + 1000) <= $TickCount) {
# print "bla\n" ;
#}
Quantar::SetLocalRx(0);
$Pending_CourtesyTone = 1; # Let the system know we wish a courtesy tone when possible.
}
}
sub GetLocalActive {
return($LocalActive);
}
sub GetPauseTGScanTime {
return($PauseTGScanTime);
}
sub GetLinkedTalkGroup {
return($LinkedTalkGroup);
}
sub SetLinkedTalkGroup {
my ($Value) = @_;
$LinkedTalkGroup = $Value;
}
sub GetValidNteworkTG {
return($ValidNteworkTG);
}
sub SetValidNteworkTG {
my ($Value) = @_;
$ValidNteworkTG = $Value;
}
sub Verbose {
my ($Value) = @_;
$Verbose = $Value;
}
1;