-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAPRS_IS.pm
414 lines (376 loc) · 11.6 KB
/
APRS_IS.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
package APRS_IS;
# APRS_IS.pm
use strict;
use warnings;
use Config::IniFiles;
use Ham::APRS::IS;
use Term::ANSIColor;
# Needed for FAP:
use FindBin 1.51 qw( $RealBin );
use lib $RealBin;
# Use custom version of FAP:
use FAP;
use P25Link;
my $AppName;
my $Version;
my $My_Latitude;
my $My_Longitude;
my $My_Symbol;
my $My_Altitude;
my $My_Freq;
my $My_Tone;
my $My_Offset;
my $My_NAC;
my $My_Comment;
my $Verbose;
my $Callsign;
my $APRS_IS;
my %APRS_Data;
my %APRS;
##################################################################
# APRS-IS ########################################################
##################################################################
sub Init {
my ($ConfigFile, $AppNameRef, $VersionRef) = @_;
$AppName = $AppNameRef;
$Version = $VersionRef;
print color('green'), "Loading APRS-IS...\n", color('reset');
my $cfg = Config::IniFiles->new( -file => $ConfigFile);
$APRS{'Enabled'} = $cfg->val('APRS', 'Enabled');
$APRS{'Passcode'} = $cfg->val('APRS', 'Passcode');
$APRS{'Suffix'} = $cfg->val('APRS', 'Suffix');
$APRS{'Server'} = $cfg->val('APRS', 'Server');
$APRS{'File'} = $cfg->val('APRS', 'APRS_File');
$APRS{'Refresh_Timer'}{'Interval'} = $cfg->val('APRS', 'APRS_Interval') * 60;
$APRS{'Refresh_Timer'}{'NextTime'} = P25Link::GetTickCount();
$APRS{'Refresh_Timer'}{'Enabled'} = 1;
$My_Latitude = $cfg->val('APRS', 'Latitude');
$My_Longitude = $cfg->val('APRS', 'Longitude');
$My_Symbol = $cfg->val('APRS', 'Symbol');
$My_Altitude = $cfg->val('APRS', 'Altitude');
$My_Freq = $cfg->val('APRS', 'Frequency');
$My_Tone = $cfg->val('APRS', 'AccessTone');
$My_Offset = $cfg->val('APRS', 'Offset');
$My_NAC = $cfg->val('APRS', 'NAC');
$My_Comment = $cfg->val('APRS', 'APRSComment');
$Verbose = $cfg->val('APRS', 'Verbose');
print " Enabled = $APRS{'Enabled'}\n";
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{'Refresh_Timer'}{'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 = $Verbose\n";
$Callsign = $cfg->val('MMDVM', 'Callsign');
if ($APRS{'Passcode'} ne Ham::APRS::IS::aprspass($Callsign)) {
$APRS{'Server'} = undef;
$APRS{'Enabled'} = 0;
warn color('red'), "APRS invalid pasword.\n", color('reset');
}
$APRS{'CallsignAndSuffix'} = $Callsign . '-' . $APRS{'Suffix'};
print " APRS Callsign = $APRS{'CallsignAndSuffix'}\n";
if ($APRS{'Enabled'}) {
$APRS_IS = new Ham::APRS::IS($APRS{'Server'}, $APRS{'CallsignAndSuffix'},
'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";
}
sub 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 Disconnect {
if ($APRS_IS and $APRS_IS->connected()) {
$APRS_IS->disconnect();
print color('yellow'), "APRS-IS Disconected.\n", color('reset');
}
}
sub Refresh_Timer { # APRS-IS
if (P25Link::GetTickCount() >= $APRS{'Refresh_Timer'}{'NextTime'}) {
if ($APRS{'Refresh_Timer'}{'Enabled'}) {
if ($Verbose) { print color('green'), "APRS::Refresh_Timer event\n", color('reset'); }
if ($APRS_IS) {
if (!$APRS_IS->connected()) {
Connect();
}
if ( $APRS_IS->connected() ) {
if ($Verbose) {print color('green'), "APRS-IS Refresh_Timer.\n", color('reset');}
Update_All(Packets::GetLinkedTalkGroup());
}
}
}
Start_Refresh_Timer();
}
}
sub Start_Refresh_Timer {
$APRS{'Refresh_Timer'}{'NextTime'} = P25Link::GetTickCount() + $APRS{'Refresh_Timer'}{'Interval'};
$APRS{'Refresh_Timer'}{'Enabled'} = 1;
}
sub 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');
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 ($Verbose > 1) {print color('green'), " APRS Position is: $APRS_position\n", color('reset');}
my $Packet = sprintf('%s>APTR01:%s', $Call, $APRS_position . $Comment);
if ($Verbose > 2) {print color('blue'), " $Packet\n", color('reset');}
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')," Make_Pos done for $APRS{'CallsignAndSuffix'}\n", color('reset');
}
sub 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');
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 ($Verbose > 1) {print " APRS Object is: $APRS_object\n";}
my $Packet = sprintf('%s>APTR01:%s', $APRS{'CallsignAndSuffix'}, $APRS_object);
if ($Verbose > 2) {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 ($Verbose) { print color('grey12'), " Make_Object $Name sent.\n", color('reset'); }
}
sub 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');
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 ($Verbose > 1) {print " APRS Item is: $APRS_item\n";}
my $Packet = sprintf('%s>APTR01:%s', $APRS{'CallsignAndSuffix'}, $APRS_item);
if ($Verbose > 2) {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;
}
if ($Verbose) { print color('grey12'), " Make_Item $Name sent.\n", color('reset'); }
}
sub Update_TG {
my ($TG) = @_;
Make_Item(
$Callsign . '/' . $APRS{'Suffix'},
$My_Latitude,
$My_Longitude,
$My_Symbol,
-1,
-1,
undef,
1,
0,
0,
$My_Freq . 'MHz ' . $My_Tone . ' ' . $My_Offset . ' NAC-' . $My_NAC . ' ' .
' TG=' . $TG . ' ' . $My_Comment . ' alt ' . $My_Altitude . 'm');
}
sub Update_All {
my ($TG) = @_;
# Station position as Object
if ($Verbose) { print color('green'), "APRS-IS Update:\n", color('reset'); }
Make_Item(
$Callsign . '/' . $APRS{'Suffix'},
$My_Latitude,
$My_Longitude,
$My_Symbol,
-1,
-1,
undef,
1,
0,
0,
$My_Freq . 'MHz ' . $My_Tone . ' ' . $My_Offset . ' NAC-' . $My_NAC . ' ' .
' TG=' . $TG . ' ' . $My_Comment . ' alt ' . $My_Altitude . 'm');
# Objects and Items refresh list loading file.
my $fh;
if ($Verbose) { print color('grey12'), " Loading APRS File...\n", color('reset'); }
if (!open($fh, "<", $APRS{'File'})) {
print color('red'), " *** Error *** $APRS{'File'} File not found.\n", color('reset');
} else {
if ($Verbose) { print color('grey12'), " File Ok.\n", color('reset'); }
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(/\t+/, $Line);
my $Index = $Line[0];
$APRS_Data{$Index}{'Name'} = $Line[0];
$APRS_Data{$Index}{'Type'} = $Line[1];
$APRS_Data{$Index}{'Lat'} = $Line[2];
$APRS_Data{$Index}{'Long'} = $Line[3];
$APRS_Data{$Index}{'Speed'} = $Line[4];
$APRS_Data{$Index}{'Course'} = $Line[5];
if ($Line[6] >= 0) {
$APRS_Data{$Index}{'Altitude'} = $Line[6];
} else {
$APRS_Data{$Index}{'Altitude'} = -1;
}
$APRS_Data{$Index}{'Alive'} = $Line[7];
$APRS_Data{$Index}{'Symbol'} = $Line[8];
$APRS_Data{$Index}{'Comment'} = $Line[9];
if ($Verbose > 1) {
print " APRS Index = $Index";
print ", Name = $APRS_Data{$Index}{'Name'}";
print ", Type = $APRS_Data{$Index}{'Type'}";
print ", Lat = $APRS_Data{$Index}{'Lat'}";
print ", Long = $APRS_Data{$Index}{'Long'}";
print ", Speed = $APRS_Data{$Index}{'Speed'}";
print ", Course = $APRS_Data{$Index}{'Course'}";
print ", Altitude = $APRS_Data{$Index}{'Altitude'}";
print ", Alive = $APRS_Data{$Index}{'Alive'}";
print ", Symbol = $APRS_Data{$Index}{'Symbol'}";
print ", Comment = $APRS_Data{$Index}{'Comment'}";
print "\n";
}
if ($APRS_Data{$Index}{'Type'} eq 'O') {
Make_Object(
$APRS_Data{$Index}{'Name'},
0, # Timestamp
$APRS_Data{$Index}{'Lat'},
$APRS_Data{$Index}{'Long'},
$APRS_Data{$Index}{'Symbol'},
$APRS_Data{$Index}{'Speed'},
$APRS_Data{$Index}{'Course'},
$APRS_Data{$Index}{'Altitude'},
$APRS_Data{$Index}{'Alive'},
0, # Compression
0, # Position Ambiguity
$APRS_Data{$Index}{'Comment'},
);
}
if ($APRS_Data{$Index}{'Type'} eq 'I') {
Make_Item(
$APRS_Data{$Index}{'Name'},
$APRS_Data{$Index}{'Lat'},
$APRS_Data{$Index}{'Long'},
$APRS_Data{$Index}{'Symbol'},
$APRS_Data{$Index}{'Speed'},
$APRS_Data{$Index}{'Course'},
$APRS_Data{$Index}{'Altitude'},
$APRS_Data{$Index}{'Alive'},
0, # Compression
0, # Position Ambiguity
$APRS_Data{$Index}{'Comment'},
);
}
}
close $fh;
if ($Verbose > 2) {
foreach my $key (keys %APRS)
{
print color('green'), " Key field: $key\n";
foreach my $key2 (keys %{$APRS_Data{$key}})
{
print " - $key2 = $APRS_Data{$key}{$key2}\n";
}
print color('reset');
}
}
}
}
sub Verbose {
my ($Value) = @_;
$Verbose = $Value;
}
1;