-
Notifications
You must be signed in to change notification settings - Fork 4
/
p25link_v2.34.4.pl
3599 lines (3372 loc) · 107 KB
/
p25link_v2.34.4.pl
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
#!/usr/bin/perl
#
#
# Strict and warnings recommended.
use strict;
use warnings;
use diagnostics;
use IO::Select;
use Switch;
use Config::IniFiles;
use Digest::CRC; # For HDLC CRC.
use Device::SerialPort;
use IO::Socket;
use IO::Socket::INET;
#use IO::Socket::Timeout;
use IO::Socket::Multicast;
use JSON;
use Data::Dumper qw(Dumper);
use Time::HiRes qw(nanosleep);
use Sys::Hostname;
#use RPi::Pin;
#use RPi::Const qw(:all);
use Ham::APRS::IS;
use Date::Calc qw(check_date Today Date_to_Time Add_Delta_YM Mktime);
use Term::ReadKey;
use Term::ANSIColor;
# Needed for FAP:
use FindBin 1.51 qw( $RealBin );
use lib $RealBin;
# Use custom version of FAP:
use FAP;
my $MaxLen =1024; # Max Socket Buffer length.
my $StartTime = time();
# About this app.
my $AppName = 'P25Link';
use constant VersionInfo => 2;
use constant MinorVersionInfo => 34;
use constant RevisionInfo => 4;
my $Version = VersionInfo . '.' . MinorVersionInfo . '-' . RevisionInfo;
print "\n##################################################################\n";
print " *** $AppName v$Version ***\n";
print " Released: May 02, 2022. Created October 17, 2019.\n";
print " Created by:\n";
print " Juan Carlos Pérez De Castro (Wodie) KM4NNO / XE1F\n";
print " Bryan Fields W9CR.\n";
print " p25.link\n";
print " www.wodielite.com\n";
print " wodielite at mac.com\n\n";
print " km4nno at yahoo.com\n\n";
print " License:\n";
print " This software is licenced under the GPL v3.\n";
print " If you are using it, please let me know, I will be glad to know it.\n\n";
print " This project is based on the work and information from:\n";
print " Juan Carlos Pérez KM4NNO / XE1F\n";
print " Byan Fields W9CR\n";
print " P25-MMDVM creator Jonathan Naylor G4KLX\n";
print " P25NX creatorDavid Kraus NX4Y\n";
print " David Kierzkowski KD8EYF\n";
print " APRS is a registed trademark and creation of Bob Bruninga WB4APR\n";
print "\n##################################################################\n\n";
# Detect Target OS.
my $OS = $^O;
print color('green'), "Current OS is $OS\n", color('reset');
# Load Settings ini file.
print color('green'), "Loading Settings...\n", color('reset');
my $cfg = Config::IniFiles->new( -file => "/opt/p25link/config.ini");
# Settings:
my $Mode = $cfg->val('Settings', 'HardwareMode'); #0 = v.24, no other modes coded at the momment.
my $HotKeys = $cfg->val('Settings', 'HotKeys');
my $LocalHost = $cfg->val('Settings', 'LocalHost');
#my($LocalIPAddr) = inet_ntoa((gethostbyname(hostname))[4]);
my($LocalHostIP) = inet_ntoa((gethostbyname($LocalHost))[4]);
my $PriorityTG = $cfg->val('Settings', 'PriorityTG');
my $Hangtime = $cfg->val('Settings', 'Hangtime');
my $MuteTGTimeout = $cfg->val('Settings', 'MuteTGTimeout');
my $UseVoicePrompts = $cfg->val('Settings', 'UseVoicePrompts');
my $UseLocalCourtesyTone = $cfg->val('Settings', 'UseLocalCourtesyTone');
my $UseRemoteCourtesyTone = $cfg->val('Settings', 'UseRemoteCourtesyTone');
my $SiteName = $cfg->val('Settings', 'SiteName');
my $SiteInfo = $cfg->val('Settings', 'SiteInfo');
my $Verbose = $cfg->val('Settings', 'Verbose');
print " Mode = $Mode\n";
print " HotKeys = $HotKeys\n";
print " LocalHost = $LocalHost ntoa($LocalHostIP)\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 "----------------------------------------------------------------------\n";
# Data Recorder
print color('green'), "Init data recorder...\n", color('reset');
my $RecordEnable = $cfg->val('Settings', 'RecordEnable');
my $RecordFile = $cfg->val('Settings', 'RecordFile');
print " Enable = $RecordEnable\n";
print " File Name = $RecordFile\n";
open my $recfh, ">", $RecordFile || die "Can't open the recording file: $!";
print "----------------------------------------------------------------------\n";
# TalkGroups:
print color('green'), "Init TalkGroups...\n", color('reset');
my $TalkGroupsFile = $cfg->val('TalkGroups', 'HostsFile');
my $TG_Verbose = $cfg->val('TalkGroups', 'Verbose');
print " Talk Groups File: " . $TalkGroupsFile ."\n";
print " Verbose = $TG_Verbose\n";
our %TG;
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 ($TG_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 ($TG_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;
}
my $ValidNteworkTG = 0;
print "----------------------------------------------------------------------\n";
# Init RDAC.
print color('green'), "Init RDAC.\n", color('reset');
my %RDAC;
$RDAC{'Interval'} = $cfg->val('RDAC', 'Interval');
my $RDAC_Verbose = $cfg->val('RDAC', 'Verbose');
print " Interval = $RDAC{'Interval'}\n";
print " Verbose = $RDAC_Verbose\n";
use constant OpPollReply => 0x2100;
$RDAC{'NextTimer'} = time(); # Make it run as soon as Mail Loop is running.
$RDAC{'Port'} = 30002;
$RDAC{'MulticastAddress'} = P25Link_MakeMulticastAddress(100);
$RDAC{'TalkGroup'} = $PriorityTG;
#$RDAC{'Sock'} = IO::Socket::Multicast->new (
# LocalHost => $RDAC{'MulticastAddress'},
# LocalPort => $RDAC{'Port'},
# Proto => 'udp',
# Blocking => 0,
# Broadcast => 1,
# ReuseAddr => 1,
# PeerPort => $RDAC{'Port'}
#) || die "Can not Bind RDAC Sock : $@\n";
#$RDAC{'Sel'} = IO::Select->new($RDAC{'Sock'}
#) || die " cannot create RDAC_Sock $!\n";
#$RDAC{'Sock'}->mcast_add($RDAC{'MulticastAddress'});
#$RDAC{'Sock'}->mcast_ttl(10);
#$RDAC{'Sock'}->mcast_loopback(0);
#$RDAC{'Connected'} = 1;
print "----------------------------------------------------------------------\n";
# Init MMDVM.
print color('green'), "Init MMDVM.\n", color('reset');
my $MMDVM_Enabled = $cfg->val('MMDVM', 'MMDVM_Enabled');
my $Callsign = $cfg->val('MMDVM', 'Callsign');
my $RadioID = $cfg->val('MMDVM', 'RadioID');
my $MMDVM_Verbose = $cfg->val('MMDVM', 'Verbose');
print " Enabled = $MMDVM_Enabled\n";
print " Callsign = $Callsign\n";
print " RadioID = $RadioID\n";
print " Verbose = $MMDVM_Verbose\n";
my $MMDVM_LocalHost = $LocalHost; # Bind Address.
my $MMDVM_LocalPort = 41020; # Local Port.
my $MMDVM_RemoteHost; # Buffer for Rx data IP.
my $MMDVM_Poll_Timer_Interval = 5; # sec.
my $MMDVM_Poll_NextTimer = time() + $MMDVM_Poll_Timer_Interval;
my $MMDVM_TG = 0;
print "----------------------------------------------------------------------\n";
# Init P25Link.
print color('green'), "Init P25Link.\n", color('reset');
my $P25Link_Enabled = $cfg->val('P25Link', 'P25Link_Enabled');
my $P25Link_Verbose =$cfg->val('P25Link', 'Verbose');
print " Enabled = $P25Link_Enabled\n";
print " Verbose = $P25Link_Verbose\n";
my $P25Link_Port = 30001;
print "----------------------------------------------------------------------\n";
# Init P25NX.
print color('green'), "Init P25NX.\n", color('reset');
my $P25NX_Enabled = $cfg->val('P25NX', 'P25NX_Enabled');
my $P25NX_Verbose =$cfg->val('P25NX', 'Verbose');
print " Enabled = $P25NX_Enabled\n";
print " Verbose = $P25NX_Verbose\n";
my $P25NX_Port = 30000;
print "----------------------------------------------------------------------\n";
# Quantar HDLC Init.
print color('green'), "Init HDLC.\n", color('reset');
my $HDLC_RTRT_Enabled = $cfg->val('HDLC', 'RTRT_Enabled');
my $HDLC_Verbose =$cfg->val('HDLC', 'Verbose');
print " RT/RT ENabled = $HDLC_RTRT_Enabled\n";
print " Verbose = $HDLC_Verbose\n";
my %Quant;
$Quant{'FrameType'} = 0;
$Quant{'LocalRx'} = 0;
$Quant{'LocalRx_Time'} = 0;
$Quant{'IsDigitalVoice'} = 1;
$Quant{'IsPage'} = 0;
$Quant{'dBm'} = 0;
$Quant{'RSSI'} = 0;
$Quant{'RSSI_Is_Valid'} = 0;
$Quant{'InvertedSignal'} = 0;
$Quant{'CandidateAdjustedMM'} = 0;
$Quant{'BER'} = 0;
$Quant{'SourceDev'} = 0;
$Quant{'Encrypted'} = 0;
$Quant{'Explicit'} = 0;
$Quant{'IndividualCall'} = 0;
$Quant{'ManufacturerID'} = 0;
$Quant{'ManufacturerName'} = "";
$Quant{'Emergency'} = 0;
$Quant{'Protected'} = 0;
$Quant{'FullDuplex'} = 0;
$Quant{'PacketMode'} = 0;
$Quant{'Priority'} = 0;
$Quant{'IsTGData'} = 0;
$Quant{'AstroTalkGroup'} = 0;
$Quant{'DestinationRadioID'} = 0;
$Quant{'SourceRadioID'} = 0;
$Quant{'LSD'} = [0, 0, 0, 0];
$Quant{'LSD0'} = 0;
$Quant{'LSD1'} = 0;
$Quant{'LSD2'} = 0;
$Quant{'LSD3'} = 0;
$Quant{'EncryptionI'} = 0;
$Quant{'EncryptionII'} = 0;
$Quant{'EncryptionIII'} = 0;
$Quant{'EncryptionIV'} = 0;
$Quant{'Algorythm'} = 0;
$Quant{'AlgoName'} = "";
$Quant{'KeyID'} = 0;
$Quant{'Speech'} = "";
$Quant{'Raw0x62'} = "";
$Quant{'Raw0x63'} = "";
$Quant{'Raw0x64'} = "";
$Quant{'Raw0x65'} = "";
$Quant{'Raw0x66'} = "";
$Quant{'Raw0x67'} = "";
$Quant{'Raw0x68'} = "";
$Quant{'Raw0x69'} = "";
$Quant{'Raw0x6A'} = "";
$Quant{'Raw0x6B'} = "";
$Quant{'Raw0x6C'} = "";
$Quant{'Raw0x6D'} = "";
$Quant{'Raw0x6E'} = "";
$Quant{'Raw0x6F'} = "";
$Quant{'Raw0x70'} = "";
$Quant{'Raw0x71'} = "";
$Quant{'Raw0x72'} = "";
$Quant{'Raw0x73'} = "";
$Quant{'SuperFrame'} = "";
$Quant{'Tail'} = 0;
$Quant{'PrevFrame'} = "";
#
# ICW (Infrastructure Control Word).
# Byte 1 address.
# Bte 2 frame type.
my $C_RR = 0x41;
my $C_UI = 0x03;
my $C_SABM = 0x3F;
my $C_XID = 0xBF;
# Byte 3.
#0x60 thru 0x73, etc
my $C_RN_Page = 0xA1;
# Byte 4.
# Byte 5 RT mode flag.
my $C_RTRT_Enabled = 0x02;
my $C_RTRT_Disabled = 0x04;
my $C_RTRT_DCRMode = 0x05;
# Byte 6 Op Code Start/Stop flag.
my $C_ChangeChannel = 0x06;
my $C_StartTx = 0x0C;
my $C_EndTx = 0x25;
# Byte 7 OpArg, type flag.
my $C_AVoice = 0x00;
my $C_TMS_Data_Payload = 0x06;
my $C_DVoice = 0x0B;
my $C_TMS_Data = 0x0C;
my $C_From_Comparator_Start = 0x0D;
my $C_From_Comparator_Stop = 0x0E;
my $C_Page = 0x0F;
# Byte 8 ICW flag.
my $C_DIU3000 = 0x00;
my $C_Quantar = 0xC2;
my $C_QuantarAlt = 0x1B;
# Byte 9 LDU1 RSSI.
# Byte 10 1A flag.
my $C_RSSI_Is_Valid = 0x1A;
# Byte 11 LDU1 RSSI.
#
# Byte 12.
my $C_Normal_Page = 0x9F;
my $C_Emergency_Page = 0xA7;
# Byte 13 Page.
my $C_Individual_Page = 0x00;
my $C_Group_Page = 0x90;
#
my $C_SystemCallTG = 0xFFFF;
#
#
my $IsTGData = 0;
my $C_Implicit_MFID = 0;
my $C_Explicit_MFID = 1;
my $Is_TG_Data = 0;
my $SuperframeCounter = 0;
#
#
my $RR_NextTimer = 0;
my $RR_Timeout = 0;
my $RR_TimerInterval = 4; # Seconds.
my $HDLC_Handshake = 0;
my $SABM_Counter = 0;
my $Message = "";
my $HDLC_Buffer = "";
my $RR_TimerEnabled = 0;
#
my $Tx_Started = 0;
my $SuperFrameCounter = 0;
my $HDLC_TxTraffic = 0;
my $LocalRx_Time;
print "----------------------------------------------------------------------\n";
# Init Serial Port for HDLC.
print color('green'), "Init Serial Port.\n", color('reset');
my $SerialPort;
my $SerialPort_Configuration = "SerialConfig.cnf";
if ($Mode == 0) {
# For Mac:
if ($OS eq "darwin") {
$SerialPort = Device::SerialPort->new('/dev/tty.usbserial') || die "Cannot Init Serial Port : $!\n";
}
# For Linux:
if ($OS eq "linux") {
$SerialPort = Device::SerialPort->new('/dev/ttyUSB0') || die "Cannot Init Serial Port : $!\n";
}
$SerialPort->baudrate(19200);
$SerialPort->databits(8);
$SerialPort->parity('none');
$SerialPort->stopbits(1);
$SerialPort->handshake('none');
$SerialPort->buffers(4096, 4096);
$SerialPort->datatype('raw');
$SerialPort->debug(1);
#$SerialPort->write_settings || undef $SerialPort;
#$SerialPort->save($SerialPort_Configuration);
#$TickCount = sprintf("%d", $SerialPort->get_tick_count());
#$FutureTickCount = $TickCount + 5000;
#print " TickCount = $TickCount\n\n";
print color('yellow'),
"To use Raspberry Pi UART you need to disable Bluetooth by editing: /boot/config.txt\n" .
"Add line: dtoverlay=pi3-disable-bt-overlay\n", color('reset'),;
}
print "----------------------------------------------------------------------\n";
# APRS-IS:
print color('green'), "Loading APRS-IS...\n", color('reset');
my $APRS_Passcode = $cfg->val('APRS', 'Passcode');
my $APRS_Suffix = $cfg->val('APRS', 'Suffix');
my $APRS_Server= $cfg->val('APRS', 'Server');
my $APRS_File = $cfg->val('APRS', 'APRS_File');
my $APRS_Interval = $cfg->val('APRS', 'APRS_Interval') * 60;
my $My_Latitude = $cfg->val('APRS', 'Latitude');
my $My_Longitude = $cfg->val('APRS', 'Longitude');
my $My_Symbol = $cfg->val('APRS', 'Symbol');
my $My_Altitude = $cfg->val('APRS', 'Altitude');
my $My_Freq = $cfg->val('APRS', 'Frequency');
my $My_Tone = $cfg->val('APRS', 'AccessTone');
my $My_Offset = $cfg->val('APRS', 'Offset');
my $My_NAC = $cfg->val('APRS', 'NAC');
my $My_Comment = $cfg->val('APRS', 'APRSComment');
my $APRS_Verbose= $cfg->val('APRS', 'Verbose');
print " Passcode = $APRS_Passcode\n";
print " Suffix = $APRS_Suffix\n";
print " Server = $APRS_Server\n";
print " APRS File $APRS_File\n";
print " APRS Interval $APRS_Interval\n";
print " Latitude = $My_Latitude\n";
print " Longitude = $My_Longitude\n";
print " Symbol = $My_Symbol\n";
print " Altitude = $My_Altitude\n";
print " Freq = $My_Freq\n";
print " Tone = $My_Tone\n";
print " Offset = $My_Offset\n";
print " NAC = $My_NAC\n";
print " Comment = $My_Comment\n";
print " Verbose = $APRS_Verbose\n";
my $APRS_IS;
my %APRS;
my $APRS_NextTimer = time();
if ($APRS_Passcode ne Ham::APRS::IS::aprspass($Callsign)) {
$APRS_Server = undef;
warn color('red'), "APRS invalid pasword.\n", color('reset');
}
my $APRS_Callsign = $Callsign . '-' . $APRS_Suffix;
print " APRS Callsign = $APRS_Callsign\n";
if (defined $APRS_Server) {
$APRS_IS = new Ham::APRS::IS($APRS_Server, $APRS_Callsign,
'appid' => "$AppName $Version",
'passcode' => $APRS_Passcode,
'filter' => 't/m');
if (!$APRS_IS) {
warn color('red'), "Failed to create APRS-IS Server object: " . $APRS_IS->{'error'} .
"\n", color('reset');
}
#Ham::APRS::FAP::debug(1);
}
print "----------------------------------------------------------------------\n";
# Voice Announce.
print color('green'), "Loading voice announcements...\n", color('reset');
my $SpeechFile = $cfg->val('Settings', 'SpeechFile');
print " File = $SpeechFile\n";
my $SpeechIni = Config::IniFiles->new( -file => $SpeechFile);
my @Speech_Zero = $SpeechIni->val('Zero', 'byte');
my @Speech_One = $SpeechIni->val('One', 'byte');
my @Speech_Two = $SpeechIni->val('Two', 'byte');
my @Speech_Three = $SpeechIni->val('Three', 'byte');
my @Speech_Four = $SpeechIni->val('Four', 'byte');
my @Speech_Five = $SpeechIni->val('Five', 'byte');
my @Speech_Six = $SpeechIni->val('Six', 'byte');
my @Speech_Seven = $SpeechIni->val('Seven', 'byte');
my @Speech_Eight = $SpeechIni->val('Eight', 'byte');
my @Speech_Nine = $SpeechIni->val('Nine', 'byte');
my @Speech_Local = $SpeechIni->val('Local', 'byte');
my @Speech_WW = $SpeechIni->val('WW', 'byte');
my @Speech_WWTac1 = $SpeechIni->val('WWTac1', 'byte');
my @Speech_WWTac2 = $SpeechIni->val('WWTac2', 'byte');
my @Speech_WWTac3 = $SpeechIni->val('WWTac3', 'byte');
my @Speech_NA = $SpeechIni->val('NA', 'byte');
my @Speech_NATac1 = $SpeechIni->val('NATac1', 'byte');
my @Speech_NATac2 = $SpeechIni->val('NATac2', 'byte');
my @Speech_NATac3 = $SpeechIni->val('NATac3', 'byte');
my @Speech_Europe = $SpeechIni->val('Europe', 'byte');
my @Speech_EuTac1 = $SpeechIni->val('EuTac1', 'byte');
my @Speech_EuTac2 = $SpeechIni->val('EuTac2', 'byte');
my @Speech_EuTac3 = $SpeechIni->val('EuTac3', 'byte');
my @Speech_France = $SpeechIni->val('France', 'byte');
my @Speech_Germany = $SpeechIni->val('Germany', 'byte');
my @Speech_Pacific = $SpeechIni->val('Pacific', 'byte');
my @Speech_PacTac1 = $SpeechIni->val('PacTac1', 'byte');
my @Speech_PacTac2 = $SpeechIni->val('PacTac2', 'byte');
my @Speech_PacTac3 = $SpeechIni->val('PacTac3', 'byte');
my @Speech_Alarm = $SpeechIni->val('Alarm', 'byte');
my @Speech_CTone1 = $SpeechIni->val('CTone1', 'byte');
my @Speech_CTone2 = $SpeechIni->val('CTone2', 'byte');
my @Speech_QuindarToneStart = $SpeechIni->val('QuindarToneStart', 'byte');
my @Speech_QuindarToneEnd = $SpeechIni->val('QuindarToneEnd', 'byte');
my @Speech_3Up = $SpeechIni->val('3Up', 'byte');
my @Speech_BeeBoo = $SpeechIni->val('BeeBoo', 'byte');
my @Speech_BumbleBee = $SpeechIni->val('BumbleBee', 'byte');
my @Speech_Nextel = $SpeechIni->val('Nextel', 'byte');
my @Speech_RC210_9 = $SpeechIni->val('RC210_9', 'byte');
my @Speech_RC210_10 = $SpeechIni->val('RC210_10', 'byte');
my @Speech_LoBat1 = $SpeechIni->val('LoBat1', 'byte');
my @Speech_LoBat2 = $SpeechIni->val('LoBat2', 'byte');
my @Speech_CustomCTone = $SpeechIni->val('customCTone', 'byte');
my @Speech_SystemStartP25NX = $SpeechIni->val('SystemStartP25NX', 'byte');
my @Speech_WellcomeToP25DotLink = $SpeechIni->val('WellcomeToP25DotLink', 'byte');
my @Speech_DefaultRevert = $SpeechIni->val('DefaultRevert', 'byte');
my @Speech_FSG_2050 = $SpeechIni->val('FSG_2050', 'byte');
my @Speech_Wave_4095 = $SpeechIni->val('Wave_4095', 'byte');
my @Speech_TestPattern = $SpeechIni->val('TestPattern', 'byte');
my $Pending_VA = 0;
my $VA_Message = 0;
my $VA_Test = 0xFFFF;
my $Pending_CourtesyTone = 0;
print " Done.\n";
print "----------------------------------------------------------------------\n";
# Connect to Priority and scan TGs.
my $LocalActive = 0;
my $PauseScan = 0;
my $PauseTGScanTimer = time();
my $LinkedTalkGroup = $PriorityTG;
foreach my $key (keys %TG) {
if ($TG{$key}{'Scan'}) {
print color('green'), "Scan TG " . $key . "\n", color('reset');
AddLinkTG($key, 0);
}
}
if ($PriorityTG > 10) {
if (!$TG{$PriorityTG}{'Scan'}) {
$TG{$PriorityTG}{'Scan'} = 100;
}
AddLinkTG($PriorityTG, 0);
}
print "----------------------------------------------------------------------\n";
# Prepare Startup VA Message.
$VA_Message = 0xFFFF11; # 0 = Welcome to P25Link.
$Pending_VA = 1; # Let the system know we wish a Voice Announce when possible.
# Raspberry Pi GPIO
#my $ResetPicPin = RPi::Pin->new(4, "Reset PIC");
#my $Pin5 = RPi::Pin->new(5, "PTT");
#my $Pin5 = RPi::Pin->new(6, "COS");
# This use the BCM pin numbering scheme.
# Valid GPIOs are: 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.
# GPIO 2, 3 Aleternate for I2C.
# GPIO 14, 15 alternate for USART.
#$ResetPicPin->mode(OUTPUT);
#$Pin5->write(HIGH);
#$pin->set_interrupt(EDGE_RISING, 'main::Pin5_Interrupt_Handler');
# Init Cisco STUN TCP
print color('green'), "Init Cisco STUN.\n", color('reset');
my $STUN_ID = sprintf("%x", hex($cfg->val('STUN', 'STUN_ID')));
my $STUN_Verbose =$cfg->val('STUN', 'Verbose');
print " Stun ID = 0x$STUN_ID\n";
print " Verbose = $STUN_Verbose\n";
my $STUN_ServerSocket;
my $STUN_Port = 1994; # Cisco STUN port is 1994;
my $STUN_Connected = 0;
my $STUN_Sel;
my $STUN_ClientSocket;
my $STUN_ClientAddr;
my $STUN_ClientPort;
my $STUN_ClientIP;
my $STUN_fh;
$STUN_ServerSocket = IO::Socket::INET->new (
#LocalHost => '172.31.7.162',
LocalPort => $STUN_Port,
Proto => 'tcp',
Listen => SOMAXCONN,
ReuseAddr =>1,
Blocking => 0
) || die " cannot create CiscoUSTUN_ServerSocket $!\n";
print " Server waiting for client connection on port " . $STUN_Port . ".\n";
# Set timeouts -- may not really be needed
#IO::Socket::Timeout->enable_timeouts_on($STUN_ServerSocket);
#$STUN_ServerSocket->read_timeout(0.0001);
#$STUN_ServerSocket->write_timeout(0.0001);
my $STUN_DataIndex = 0;
my @STUN_Data = [];
print "----------------------------------------------------------------------\n";
# Read Keys:
if ($HotKeys) {
ReadMode 3;
PrintMenu();
}
print "----------------------------------------------------------------------\n";
# Misc
my $Read_Timeout = 0.003;
my $Run = 1;
my $TGVar = 2044;
###################################################################
# MAIN ############################################################
###################################################################
if ($Mode == 1) { # If Cisco STUN (Mode 1) is selected:
if ($STUN_Verbose) {print "Cisco STUN listen for connections:\n";}
while ($Run) {
#if($STUN_ClientAddr = accept($STUN_ClientSocket, $STUN_ServerSocket)) {
if(($STUN_ClientSocket, $STUN_ClientAddr) = $STUN_ServerSocket->accept()) {
my ($Client_Port, $Client_IP) = sockaddr_in($STUN_ClientAddr);
$STUN_ClientIP = inet_ntoa($Client_IP);
if ($STUN_Verbose) {print "STUN_Client IP " . inet_ntoa($Client_IP) .
":" . $STUN_Port . "\n";}
$STUN_ClientSocket->autoflush(1);
$STUN_Sel = IO::Select->new($STUN_ClientSocket);
$STUN_Connected = 1;
MainLoop();
}
}
$STUN_ServerSocket->close();
} else { # If Serial (Mode 0) is selected:
MainLoop();
}
# Program exit:
print "----------------------------------------------------------------------\n";
ReadMode 0; # Set keys back to normal State.
if ($Mode == 0) { # Close Serial Port:
$SerialPort->close || die "Failed to close SerialPort.\n";
}
if ($APRS_IS and $APRS_IS->connected()) {
$APRS_IS->disconnect();
print color('yellow'), "APRS-IS Disconected.\n", color('reset');
}
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);
}
}
print "Good bye cruel World.\n";
print "----------------------------------------------------------------------\n\n";
exit;
##################################################################
# Menu ###########################################################
##################################################################
sub PrintMenu {
print "Shortcuts menu:\n";
print " Q/q = Quit. h = Help..\n";
print " A/a = APRS show/hide verbose. C/c = Voice anounce test. \n";
print " E/e = Emergency Page/Alarm. F/f = Serach user test. \n";
print " A/a = APRS show/hide verbose. H/h = HDLC show/hide verbose. \n";
print " J/j = JSON show/hide verbose. M/m = MMDVM show/hide verbose.\n";
print " P/p = P25NX show/hide verbose. L/l = P25Link show/hide verbose.\n";
print " S/s = STUN show/hide verbose. t = Test. \n\n";
}
#################################################################################
# Recorder ########################################################################
#################################################################################
sub RecorderBytes_2_HexString {
my ($Buffer) = @_;
# Display Rx Hex String.
#print "HDLC_Rx Buffer: ";
my $x = 0;
if (ord(substr($Buffer, $x, 1)) < 0x10) {
print $recfh sprintf("byte = 0x0%x", ord(substr($Buffer, $x, 1)));
if ($Verbose eq 'R') { print sprintf("byte = 0x0%x", ord(substr($Buffer, $x, 1)));}
} else {
print $recfh sprintf("byte = 0x%x", ord(substr($Buffer, $x, 1)));
if ($Verbose eq 'R') { print sprintf("byte = 0x%x", ord(substr($Buffer, $x, 1)));}
}
for (my $x = 1; $x < length($Buffer); $x++) {
if (ord(substr($Buffer, $x, 1)) < 0x10) {
print $recfh sprintf(", 0x0%x", ord(substr($Buffer, $x, 1)));
if ($Verbose eq 'R') {print sprintf(", 0x0%x", ord(substr($Buffer, $x, 1)));}
} else {
print $recfh sprintf(", 0x%x", ord(substr($Buffer, $x, 1)));
if ($Verbose eq 'R') {print sprintf(", 0x%x", ord(substr($Buffer, $x, 1)));}
}
}
print $recfh "\n";
if ($Verbose eq 'R') {print "\n";}
}
##################################################################
# RDAC ###########################################################
##################################################################
sub RDAC_Disconnect {
my ($TalkGroup) = @_;
my $MulticastAddress = P25Link_MakeMulticastAddress($TalkGroup);
$TG{$TalkGroup}{'Sock'}->mcast_drop($MulticastAddress);
# $TG{$TalkGroup}{'Sel'}->remove($TG{$TalkGroup}{'Sock'});
$TG{$TalkGroup}{'P25Link_Connected'} = 0;
$TG{$TalkGroup}{'Sock'}->close();
print color('green'), "P25Link TG $TalkGroup disconnected.\n", color('reset');
}
sub RDAC_Timer {
if (time() >= $RDAC{'NextTimer'}) {
RDAC_Tx($RDAC{'TalkGroup'});
$RDAC{'NextTimer'} = time() + $RDAC{'Interval'};
}
}
sub RDAC_Tx {
my ($TalkGroup) = @_;
if (($P25Link_Enabled == 0) and ($P25NX_Enabled == 0)) { return; }
$RDAC{'TalkGroup'} = $TalkGroup;
my $Buffer;
$Buffer = 'P25Link' . chr(0x00);
$Buffer = $Buffer . chr(OpPollReply & 0xFF) . chr((OpPollReply & 0xFF00) >> 8);
$Buffer = $Buffer . inet_aton($LocalHost);
$Buffer = $Buffer . chr($RDAC{'Port'} & 0xFF) . chr(($RDAC{'Port'} & 0xFF00) >> 8);
$Buffer = $Buffer . chr(VersionInfo); # Software Mayor Version
# 6
$Buffer = $Buffer . chr(MinorVersionInfo); # Software Minor Version
$Buffer = $Buffer . chr(RevisionInfo); # Software Revision Version
# Flags
my $Byte = 0;
if ($Quant{'LocalRx'}) {$Byte = 0x01;} # Receive
if ($Quant{'LocalRx'}) {$Byte = $Byte | 0x02;} # Transmit
if ($Quant{'IsDigitalVoice'} == 1) { # Mode Digital
$Byte = $Byte | 0x04;
}
if ($Quant{'IsDigitalVoice'} == 0) { # Mode Analog
$Byte = $Byte | 0x08;
}
if ($Quant{'IsPage'} == 1) { # Mode Data
$Byte = $Byte | 0x10;
}
$Buffer = $Buffer . chr($Byte); # Flags
# Callsign
my $RDAC_Callsign = $Callsign . "/" . $APRS_Suffix;
$Buffer = $Buffer . $RDAC_Callsign;
for (my $x = length($RDAC_Callsign); $x < 10; $x++) {
$Buffer = $Buffer . ' ';
}
# Site Name
if (length($SiteName) > 30) {
$SiteName = substr($SiteName, 0, 30);
}
$Buffer = $Buffer . $SiteName;
for (my $x = length($SiteName); $x < 30; $x++) {
$Buffer = $Buffer . ' ';
}
# Tak Group
$Buffer = $Buffer . chr($TalkGroup & 0xFF) . chr(($TalkGroup & 0xFF00) >> 8);
# Info
if (length($SiteInfo) > 30) {
$SiteInfo = substr($SiteInfo, 0, 30);
}
$Buffer = $Buffer . $SiteInfo;
for (my $x = length($SiteInfo); $x < 30; $x++) {
$Buffer = $Buffer . ' ';
}
#Filler 10
$Buffer = $Buffer . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0);
#print " Len4 " . length($Buffer) . "\n";
# Tx to the Network.
if ($RDAC_Verbose >= 2) {
print "RDAC_Tx Message.\n";
StrToHex($Buffer);
}
my $Tx_Sock = IO::Socket::Multicast->new(
LocalHost => $RDAC{'MulticastAddress'},
LocalPort => $RDAC{'Port'},
Proto => 'udp',
Blocking => 0,
Broadcast => 1,
ReuseAddr => 1,
PeerPort => $RDAC{'Port'}
)
or die "Can not create Multicast : $@\n";
$Tx_Sock->mcast_ttl(10);
$Tx_Sock->mcast_loopback(0);
$Tx_Sock->mcast_send($Buffer, $RDAC{'MulticastAddress'} . ":" . $RDAC{'Port'});
$Tx_Sock->close;
# if ($RDAC_Verbose) {
print color('green'), "RDAC_Tx IP Mcast " . $RDAC{'MulticastAddress'} . "\n", color('reset');
# }
}
##################################################################
# APRS-IS ########################################################
##################################################################
sub APRS_connect {
my $Ret = $APRS_IS->connect('retryuntil' => 2);
if (!$Ret) {
warn color('red'), "Failed to connect APRS-IS server: " . $APRS_IS->{'error'} . "\n", color('reset');
return;
}
print " APRS-IS: connected.\n";
}
sub APRS_Timer { # APRS-IS
if (time() >= $APRS_NextTimer) {
if ($APRS_IS) {
if (!$APRS_IS->connected()) {
APRS_connect();
}
if ( $APRS_IS->connected() ) {
if ($APRS_Verbose) {print color('green'), "APRS-IS Timer.\n", color('reset');}
APRS_Update($LinkedTalkGroup);
}
}
$APRS_NextTimer = time() + $APRS_Interval;
}
}
sub APRS_Make_Pos {
my ($Call, $Latitude, $Longitude, $Speed, $Course, $Altitude, $Symbol, $Comment) = @_;
if (!$APRS_IS) {
warn color('red'), " APRS-IS does not exist.\n", color('reset');
return;
}
if (!$APRS_IS->connected()) {
warn color('red'), " APRS-IS not connected, trying to reconnect.\n", color('reset');
APRS_connect();
}
if (!$APRS_IS->connected()) {
warn color('red'), "APRS-IS can not connect.\n", color('reset');
return;
}
my %Options;
$Options{'timestamp'} = 0;
$Options{'comment'} = 'Hola';
my $APRS_position = Ham::APRS::FAP::make_position(
$Latitude,
$Longitude,
$Speed, # speed
$Course, # course
$Altitude, # altitude
(defined $Symbol) ? $Symbol : '/[', # symbol
{
#'compression' => 1,
#'ambiguity' => 1, # still can not make it work.
#'timestamp' => time(), # still can not make it work.
'comment' => $Comment,
#'dao' => 1
});
if ($APRS_Verbose > 1) {print color('green'), " APRS Position is: $APRS_position\n", color('reset');}
my $Packet = sprintf('%s>APTR01:%s', $Call, $APRS_position . $Comment);
print color('blue'), " $Packet\n", color('reset');
if ($APRS_Verbose > 2) {print " APRS Packet is: $Packet\n";}
my $Res = $APRS_IS->sendline($Packet);
if (!$Res) {
warn color('red'), "Error sending APRS-IS Pos packet $Res\n", color('reset');
$APRS_IS->disconnect();
return;
}
print color('grey12')," APRS_Make_Pos done for $APRS_Callsign\n", color('reset');
}
sub APRS_Make_Object {
my ($Name, $TimeStamp, $Latitude, $Longitude, $Symbol, $Speed,
$Course, $Altitude, $Alive, $UseCompression, $PosAmbiguity, $Comment) = @_;
if (!$APRS_IS) {
warn color('red'), " APRS-IS does not exist.\n", color('reset');
return;
}
if (!$APRS_IS->connected()) {
warn color('red'), " APRS-IS not connected, trying to reconnect.\n", color('reset');
APRS_connect();
}
if (!$APRS_IS->connected()) {
warn color('red'), "APRS-IS can not connect.\n", color('reset');
return;
}
my $APRS_object = Ham::APRS::FAP::make_object(
$Name, # Name
$TimeStamp,
$Latitude,
$Longitude,
$Symbol, # symbol
$Speed, # speed
$Course,
$Altitude, # altitude
$Alive,
$UseCompression,
$PosAmbiguity,
$Comment
);
if ($APRS_Verbose > 0) {print " APRS Object is: $APRS_object\n";}
my $Packet = sprintf('%s>APTR01:%s', $APRS_Callsign, $APRS_object);
print color('blue'), " $Packet\n", color('reset');
my $Res = $APRS_IS->sendline($Packet);
if (!$Res) {
warn color('red'), "*** Error *** sending APRS-IS Object $Name packet $Res\n", color('reset');
$APRS_IS->disconnect();
return;
}
if ($APRS_Verbose) { print color('grey12'), " APRS_Make_Object $Name sent.\n", color('reset'); }
}
sub APRS_Make_Item {
my ($Name, $Latitude, $Longitude, $Symbol, $Speed,
$Course, $Altitude, $Alive, $UseCompression, $PosAmbiguity, $Comment) = @_;
if (!$APRS_IS) {
warn color('red'), " APRS-IS does not exist.\n", color('reset');
return;
}
if (!$APRS_IS->connected()) {
warn color('red'), " APRS-IS not connected, trying to reconnect.\n", color('reset');
APRS_connect();
}
if (!$APRS_IS->connected()) {
warn color('red'), "APRS-IS can not connect.\n", color('reset');
return;
}
my $APRS_item = Ham::APRS::FAP::make_item(
$Name, # Name
$Latitude,
$Longitude,
$Symbol, # symbol
$Speed, # speed
$Course,
$Altitude, # altitude
$Alive,
$UseCompression,
$PosAmbiguity,
$Comment
);
if ($APRS_Verbose > 0) {print " APRS Item is: $APRS_item\n";}
my $Packet = sprintf('%s>APTR01:%s', $APRS_Callsign, $APRS_item);
print color('blue'), " $Packet\n", color('reset');
my $Res = $APRS_IS->sendline($Packet);
if (!$Res) {
warn color('red'), "*** Error *** sending APRS-IS Item $Name packet $Res\n", color('reset');
$APRS_IS->disconnect();
return;
}