FAP.pm

filter: Statementstmt 47.7% 664 / 1391 Branchbran 40.9% 348 / 850 Conditioncond 35.3% 88 / 249 MC/DCmcdc 19.4% 27 / 139 Subroutinesub 60.0% 27 / 45 Podpod 100% 14 / 14 totaltotal 43.4% 1168 / 2688 SCAR 107.4
CC
513
Cov
44%
make_position
1056.0
kiss_to_tnc2
870.0
tnc2_to_kiss
756.0
CRAP 46293.6
↑ Summary IS.pm »
1  
2 package Ham::APRS::FAP;
3  
4 =head1 NAME
5  
6 Ham::APRS::FAP - Finnish APRS Parser (Fabulous APRS Parser)
7  
8 =head1 SYNOPSIS
9  
10 use Ham::APRS::FAP qw(parseaprs);
11 my $aprspacket = 'OH2RDP>BEACON,OH2RDG*,WIDE:!6028.51N/02505.68E#PHG7220/RELAY,WIDE, OH2AP Jarvenpaa';
12 my %packetdata;
13 my $retval = parseaprs($aprspacket, \%packetdata);
14 if ($retval == 1) {
15 # decoding ok, do something with the data
16 while (my ($key, $value) = each(%packetdata)) {
17 print "$key: $value\n";
18 }
19 } else {
20 warn "Parsing failed: $packetdata{resultmsg} ($packetdata{resultcode})\n";
21 }
22  
23 =head1 ABSTRACT
24  
25 This module is a fairly complete APRS parser. It parses normal,
26 mic-e and compressed location packets, NMEA location packets,
27 objects, items, messages, telemetry and most weather packets. It is
28 stable and fast enough to parse the APRS-IS stream in real time.
29  
30 The package also contains the Ham::APRS::IS module which, in turn,
31 is an APRS-IS client library.
32  
33 =head1 DESCRIPTION
34  
35 Unless a debugging mode is enabled, all errors and warnings are reported
36 through the API (as opposed to printing on STDERR or STDOUT), so that
37 they can be reported nicely on the user interface of an application.
38 This parser is not known to crash on invalid packets. It is used to power
39 the L<http://aprs.fi/> web site.
40  
41 APRS features specifically NOT handled by this module:
42  
43 =over
44  
45 =item * special objects (area, signpost, etc)
46  
47 =item * network tunneling/third party packets
48  
49 =item * direction finding
50  
51 =item * station capability queries
52  
53 =item * status reports (partially)
54  
55 =item * user defined data formats
56  
57 =back
58  
59 This module is based (on those parts that are implemented)
60 on APRS specification 1.0.1.
61  
62 This module requires a reasonably recent L<Date::Calc> module.
63  
64 =head1 EXPORT
65  
66 None by default.
67  
68 =head1 FUNCTION REFERENCE
69  
70 =cut
71  
72 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 22 ▶use Date::Calc qw(check_date Today Date_to_Time Add_Delta_YM Mktime);
Subroutine BEGIN: called CC=1 SCAR=0.0
75 22 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 22 require Exporter;
78  
79 22 our @ISA = qw(Exporter);
80  
81 # Items to export into callers namespace by default. Note: do not export
82 # names by default without a very good reason. Use EXPORT_OK instead.
83 # Do not simply export all your public functions/methods/constants.
84  
85 # This allows declaration use Ham::APRS::FAP ':all';
86 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
87 # will save memory.
88 ##our %EXPORT_TAGS = (
89 ## 'all' => [ qw(
90 ##
91 ## ) ],
92 ##);
93  
94 22 our @EXPORT_OK = (
95 ## @{ $EXPORT_TAGS{'all'} },
96 '&parseaprs',
97 '&kiss_to_tnc2',
98 '&tnc2_to_kiss',
99 '&aprs_duplicate_parts',
100 '&count_digihops',
101 '&check_ax25_call',
102 '&distance',
103 '&direction',
104 '&make_object',
105 '&make_timestamp',
106 '&make_position',
107 '&mice_mbits_to_message',
108 );
109  
110 ##our @EXPORT = qw(
111 ##
112 ##);
113  
114 22 our $VERSION = '1.20';
115  
116  
117 # Preloaded methods go here.
118  
119 # no debugging by default
120 22 my $debug = 0;
121  
122 22 my %result_messages = (
123 'unknown' => 'Unsupported packet format',
124
125 'packet_no' => 'No packet given to parse',
126 'packet_short' => 'Too short packet',
127 'packet_nobody' => 'No body in packet',
128
129 'srccall_noax25' => 'Source callsign is not a valid AX.25 call',
130 'srccall_badchars' => 'Source callsign contains bad characters',
131
132 'dstpath_toomany' => 'Too many destination path components to be AX.25',
133 'dstcall_none' => 'No destination field in packet',
134 'dstcall_noax25' => 'Destination callsign is not a valid AX.25 call',
135
136 'digicall_noax25' => 'Digipeater callsign is not a valid AX.25 call',
137 'digicall_badchars' => 'Digipeater callsign contains bad characters',
138
139 'timestamp_inv_loc' => 'Invalid timestamp in location',
140 'timestamp_inv_obj' => 'Invalid timestamp in object',
141 'timestamp_inv_sta' => 'Invalid timestamp in status',
142 'timestamp_inv_gpgga' => 'Invalid timestamp in GPGGA sentence',
143 'timestamp_inv_gpgll' => 'Invalid timestamp in GPGLL sentence',
144
145 'packet_invalid' => 'Invalid packet',
146
147 'nmea_inv_cval' => 'Invalid coordinate value in NMEA sentence',
148 'nmea_large_ew' => 'Too large value in NMEA sentence (east/west)',
149 'nmea_large_ns' => 'Too large value in NMEA sentence (north/south)',
150 'nmea_inv_sign' => 'Invalid lat/long sign in NMEA sentence',
151 'nmea_inv_cksum' => 'Invalid checksum in NMEA sentence',
152
153 'gprmc_fewfields' => 'Less than ten fields in GPRMC sentence ',
154 'gprmc_nofix' => 'No GPS fix in GPRMC sentence',
155 'gprmc_inv_time' => 'Invalid timestamp in GPRMC sentence',
156 'gprmc_inv_date' => 'Invalid date in GPRMC sentence',
157 'gprmc_date_out' => 'GPRMC date does not fit in an Unix timestamp',
158
159 'gpgga_fewfields' => 'Less than 11 fields in GPGGA sentence',
160 'gpgga_nofix' => 'No GPS fix in GPGGA sentence',
161
162 'gpgll_fewfields' => 'Less than 5 fields in GPGLL sentence',
163 'gpgll_nofix' => 'No GPS fix in GPGLL sentence',
164
165 'nmea_unsupp' => 'Unsupported NMEA sentence type',
166
167 'obj_short' => 'Too short object',
168 'obj_inv' => 'Invalid object',
169 'obj_dec_err' => 'Error in object location decoding',
170
171 'item_short' => 'Too short item',
172 'item_inv' => 'Invalid item',
173 'item_dec_err' => 'Error in item location decoding',
174
175 'loc_short' => 'Too short uncompressed location',
176 'loc_inv' => 'Invalid uncompressed location',
177 'loc_large' => 'Degree value too large',
178 'loc_amb_inv' => 'Invalid position ambiguity',
179
180 'mice_short' => 'Too short mic-e packet',
181 'mice_inv' => 'Invalid characters in mic-e packet',
182 'mice_inv_info' => 'Invalid characters in mic-e information field',
183 'mice_amb_large' => 'Too much position ambiguity in mic-e packet',
184 'mice_amb_inv' => 'Invalid position ambiguity in mic-e packet',
185 'mice_amb_odd' => 'Odd position ambiguity in mic-e packet',
186
187 'comp_inv' => 'Invalid compressed packet',
188
189 'msg_inv' => 'Invalid message packet',
190
191 'wx_unsupp' => 'Unsupported weather format',
192 'user_unsupp' => 'Unsupported user format',
193
194 'dx_inv_src' => 'Invalid DX spot source callsign',
195 'dx_inf_freq' => 'Invalid DX spot frequency',
196 'dx_no_dx' => 'No DX spot callsign found',
197
198 'tlm_inv' => 'Invalid telemetry packet',
199 'tlm_large' => 'Too large telemetry value',
200 'tlm_unsupp' => 'Unsupported telemetry',
201
202 'exp_unsupp' => 'Unsupported experimental',
203
204 'sym_inv_table' => 'Invalid symbol table or overlay',
205 );
206  
207 =over
208  
209 =item result_messages( )
210  
211 Returns a reference to a hash containing all possible
212 return codes as the keys and their plain english descriptions
213 as the values of the hash.
214  
215 =back
216  
217 =cut
218  
219 sub result_messages()
220 {
221 0 ▶ return \%result_messages;
Subroutine result_messages: not called CC=1 SCAR=6.9
Pod documented
222 }
223  
224 # these functions are used to report warnings and parser errors
225 # from the module
226  
227 sub _a_err($$;$)
228 {
229 7 ▶ my ($rethash, $errcode, $val) = @_;
Subroutine _a_err: called CC=4 SCAR=15.2
230
231 7 $rethash->{'resultcode'} = $errcode;
232 $rethash->{'resultmsg'}
233 = defined $result_messages{$errcode}
234 7 ▶ ? $result_messages{$errcode} : $errcode;
Branch: 1 branch
BranchTrueFalseTotal
defined $result_messages{$errcode} ? : 7 0 7
235
236 7 ▶ $rethash->{'resultmsg'} .= ': ' . $val if (defined $val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 7 7
237
238 7 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 7 7
239 0 warn "Ham::APRS::FAP ERROR $errcode: " . $rethash->{'resultmsg'} . "\n";
240 }
241 }
242  
243 sub _a_warn($$;$)
244 {
245 0 ▶ my ($rethash, $errcode, $val) = @_;
Subroutine _a_warn: not called CC=4 SCAR=30.0
246
247 0 push @{ $rethash->{'warncodes'} }, $errcode;
248
249 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
250 warn "Ham::APRS::FAP WARNING $errcode: "
251 . (defined $result_messages{$errcode}
252 0 ▶ ? $result_messages{$errcode} : $errcode)
Branch: 2 branches
BranchTrueFalseTotal
defined $result_messages{$errcode} ? : 0 0 0
defined $val ? : 0 0 0
253 . (defined $val ? ": $val" : '')
254 . "\n";
255 }
256 }
257  
258 # message bit types for mic-e
259 # from left to right, bits a, b and c
260 # standard one bit is 1, custom one bit is 2
261 22 my %mice_messagetypes = (
262 '111' => 'off duty',
263 '222' => 'custom 0',
264 '110' => 'en route',
265 '220' => 'custom 1',
266 '101' => 'in service',
267 '202' => 'custom 2',
268 '100' => 'returning',
269 '200' => 'custom 3',
270 '011' => 'committed',
271 '022' => 'custom 4',
272 '010' => 'special',
273 '020' => 'custom 5',
274 '001' => 'priority',
275 '002' => 'custom 6',
276 '000' => 'emergency',
277 );
278  
279 =over
280  
281 =item mice_mbits_to_message($packetdata{'mbits'})
282  
283 Convert mic-e message bits (three numbers 0-2) to a textual message.
284 Returns the message on success, undef on failure.
285  
286 =back
287  
288 =cut
289  
290 sub mice_mbits_to_message($) {
291 0 ▶ my $bits = shift @_;
Subroutine mice_mbits_to_message: not called CC=3 SCAR=24.8
Pod documented
292 0 ▶ if ($bits =~ /^\s*([0-2]{3})\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits =~ /^\s*([0-2]{3})\s*$/o) 0 0 0
293 0 $bits = $1;
294 0 ▶ if (defined($mice_messagetypes{$bits})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mice_messagetypes{$bits}) 0 0 0
295 0 return $mice_messagetypes{$bits};
296 }
297 }
298 0 return undef;
299 }
300  
301 # A list of mappings from GPSxyz (or SPCxyz)
302 # to APRS symbols. Overlay characters (z) are
303 # not handled here
304 22 my %dstsymbol = (
305 'BB' => q(/!), 'BC' => q(/"), 'BD' => q(/#), 'BE' => q(/$),
306 'BF' => q(/%), 'BG' => q(/&), 'BH' => q(/'), 'BI' => q!/(!,
307 'BJ' => q!/)!, 'BK' => q(/*), 'BL' => q(/+), 'BM' => q(/,),
308 'BN' => q(/-), 'BO' => q(/.), 'BP' => q(//),
309  
310 'P0' => q(/0), 'P1' => q(/1), 'P2' => q(/2), 'P3' => q(/3),
311 'P4' => q(/4), 'P5' => q(/5), 'P6' => q(/6), 'P7' => q(/7),
312 'P8' => q(/8), 'P9' => q(/9),
313  
314 'MR' => q(/:), 'MS' => q(/;), 'MT' => q(/<), 'MU' => q(/=),
315 'MV' => q(/>), 'MW' => q(/?), 'MX' => q(/@),
316  
317 'PA' => q(/A), 'PB' => q(/B), 'PC' => q(/C), 'PD' => q(/D),
318 'PE' => q(/E), 'PF' => q(/F), 'PG' => q(/G), 'PH' => q(/H),
319 'PI' => q(/I), 'PJ' => q(/J), 'PK' => q(/K), 'PL' => q(/L),
320 'PM' => q(/M), 'PN' => q(/N), 'PO' => q(/O), 'PP' => q(/P),
321 'PQ' => q(/Q), 'PR' => q(/R), 'PS' => q(/S), 'PT' => q(/T),
322 'PU' => q(/U), 'PV' => q(/V), 'PW' => q(/W), 'PX' => q(/X),
323 'PY' => q(/Y), 'PZ' => q(/Z),
324  
325 'HS' => q(/[), 'HT' => q(/\\), 'HU' => q(/]), 'HV' => q(/^),
326 'HW' => q(/_), 'HX' => q(/`),
327  
328 'LA' => q(/a), 'LB' => q(/b), 'LC' => q(/c), 'LD' => q(/d),
329 'LE' => q(/e), 'LF' => q(/f), 'LG' => q(/g), 'LH' => q(/h),
330 'LI' => q(/i), 'LJ' => q(/j), 'LK' => q(/k), 'LL' => q(/l),
331 'LM' => q(/m), 'LN' => q(/n), 'LO' => q(/o), 'LP' => q(/p),
332 'LQ' => q(/q), 'LR' => q(/r), 'LS' => q(/s), 'LT' => q(/t),
333 'LU' => q(/u), 'LV' => q(/v), 'LW' => q(/w), 'LX' => q(/x),
334 'LY' => q(/y), 'LZ' => q(/z),
335  
336 'J1' => q(/{), 'J2' => q(/|), 'J3' => q(/}), 'J4' => q(/~),
337  
338 'OB' => q(\\!), 'OC' => q(\\"), 'OD' => q(\\#), 'OE' => q(\\$),
339 'OF' => q(\\%), 'OG' => q(\\&), 'OH' => q(\\'), 'OI' => q!\\(!,
340 'OJ' => q!\\)!, 'OK' => q(\\*), 'OL' => q(\\+), 'OM' => q(\\,),
341 'ON' => q(\\-), 'OO' => q(\\.), 'OP' => q(\\/),
342  
343 'A0' => q(\\0), 'A1' => q(\\1), 'A2' => q(\\2), 'A3' => q(\\3),
344 'A4' => q(\\4), 'A5' => q(\\5), 'A6' => q(\\6), 'A7' => q(\\7),
345 'A8' => q(\\8), 'A9' => q(\\9),
346  
347 'NR' => q(\\:), 'NS' => q(\\;), 'NT' => q(\\<), 'NU' => q(\\=),
348 'NV' => q(\\>), 'NW' => q(\\?), 'NX' => q(\\@),
349  
350 'AA' => q(\\A), 'AB' => q(\\B), 'AC' => q(\\C), 'AD' => q(\\D),
351 'AE' => q(\\E), 'AF' => q(\\F), 'AG' => q(\\G), 'AH' => q(\\H),
352 'AI' => q(\\I), 'AJ' => q(\\J), 'AK' => q(\\K), 'AL' => q(\\L),
353 'AM' => q(\\M), 'AN' => q(\\N), 'AO' => q(\\O), 'AP' => q(\\P),
354 'AQ' => q(\\Q), 'AR' => q(\\R), 'AS' => q(\\S), 'AT' => q(\\T),
355 'AU' => q(\\U), 'AV' => q(\\V), 'AW' => q(\\W), 'AX' => q(\\X),
356 'AY' => q(\\Y), 'AZ' => q(\\Z),
357  
358 'DS' => q(\\[), 'DT' => q(\\\\), 'DU' => q(\\]), 'DV' => q(\\^),
359 'DW' => q(\\_), 'DX' => q(\\`),
360  
361 'SA' => q(\\a), 'SB' => q(\\b), 'SC' => q(\\c), 'SD' => q(\\d),
362 'SE' => q(\\e), 'SF' => q(\\f), 'SG' => q(\\g), 'SH' => q(\\h),
363 'SI' => q(\\i), 'SJ' => q(\\j), 'SK' => q(\\k), 'SL' => q(\\l),
364 'SM' => q(\\m), 'SN' => q(\\n), 'SO' => q(\\o), 'SP' => q(\\p),
365 'SQ' => q(\\q), 'SR' => q(\\r), 'SS' => q(\\s), 'ST' => q(\\t),
366 'SU' => q(\\u), 'SV' => q(\\v), 'SW' => q(\\w), 'SX' => q(\\x),
367 'SY' => q(\\y), 'SZ' => q(\\z),
368  
369 'Q1' => q(\\{), 'Q2' => q(\\|), 'Q3' => q(\\}), 'Q4' => q(\\~),
370 );
371  
372 # conversion constants
373 22 our $knot_to_kmh = 1.852; # nautical miles per hour to kilometers per hour
374 22 our $mph_to_kmh = 1.609344; # miles per hour to kilometers per hour
375 22 our $kmh_to_ms = 10 / 36; # kilometers per hour to meters per second
376 22 our $mph_to_ms = $mph_to_kmh * $kmh_to_ms; # miles per hour to meters per second
377 22 our $hinch_to_mm = 0.254; # hundredths of an inch to millimeters
378  
379 =over
380  
381 =item debug($enable)
382  
383 Enables (debug(1)) or disables (debug(0)) debugging.
384  
385 When debugging is enabled, warnings and errors are emitted using the warn() function,
386 which will normally result in them being printed on STDERR. Succesfully
387 printed packets will be also printed on STDOUT in a human-readable
388 format.
389  
390 When debugging is disabled, nothing will be printed on STDOUT or STDERR -
391 all errors and parsing results need to be collected from the returned
392 hash reference.
393  
394 =back
395  
396 =cut
397  
398 sub debug($)
399 {
400 0 ▶ my $dval = shift @_;
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
401 0 ▶ if ($dval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dval) { } 0 0 0
402 0 $debug = 1;
403 } else {
404 0 $debug = 0;
405 }
406 }
407  
408 # Return a human readable timestamp in UTC.
409 # If no parameter is given, use current time,
410 # else use the unix timestamp given in the parameter.
411  
412 sub _gettime {
413 0 ▶ my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday);
Subroutine _gettime: not called CC=2 SCAR=17.9
414 0 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) { } 0 0 0
415 0 my $tstamp = shift @_;
416 0 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($tstamp);
417 } else {
418 0 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime();
419 }
420 0 my $timestring = sprintf('%d-%02d-%02d %02d:%02d:%02d UTC',
421 $year + 1900,
422 $mon + 1,
423 $mday,
424 $hour,
425 $min,
426 $sec);
427 0 return $timestring;
428 }
429  
430 =over
431  
432 =item distance($lon0, $lat0, $lon1, $lat1)
433  
434 Returns the distance in kilometers between two locations
435 given in decimal degrees. Arguments are given in order as
436 lon0, lat0, lon1, lat1, east and north positive.
437 The calculation uses the great circle distance, it
438 is not too exact, but good enough for us.
439  
440 =back
441  
442 =cut
443  
444 sub distance($$$$) {
445 0 ▶ my $lon0 = shift @_;
Subroutine distance: not called CC=1 SCAR=6.9
Pod documented
446 0 my $lat0 = shift @_;
447 0 my $lon1 = shift @_;
448 0 my $lat1 = shift @_;
449
450 # decimal to radian
451 0 $lon0 = deg2rad($lon0);
452 0 $lon1 = deg2rad($lon1);
453 0 $lat0 = deg2rad($lat0);
454 0 $lat1 = deg2rad($lat1);
455
456 # Use the haversine formula for distance calculation
457 # http://mathforum.org/library/drmath/view/51879.html
458 0 my $dlon = $lon1 - $lon0;
459 0 my $dlat = $lat1 - $lat0;
460 0 my $a = (sin($dlat/2)) ** 2 + cos($lat0) * cos($lat1) * (sin($dlon/2)) ** 2;
461 0 my $c = 2 * atan2(sqrt($a), sqrt(1-$a));
462 0 my $distance = $c * 6366.71; # in kilometers
463  
464 0 return $distance;
465 }
466  
467 =over
468  
469 =item direction($lon0, $lat0, $lon1, $lat1)
470  
471 Returns the initial great circle direction in degrees
472 from lat0/lon0 to lat1/lon1. Locations are input
473 in decimal degrees, north and east positive.
474  
475 =back
476  
477 =cut
478  
479 sub direction($$$$) {
480 0 ▶ my $lon0 = shift @_;
Subroutine direction: not called CC=2 SCAR=17.9
Pod documented
481 0 my $lat0 = shift @_;
482 0 my $lon1 = shift @_;
483 0 my $lat1 = shift @_;
484  
485 0 $lon0 = deg2rad($lon0);
486 0 $lon1 = deg2rad($lon1);
487 0 $lat0 = deg2rad($lat0);
488 0 $lat1 = deg2rad($lat1);
489  
490 # direction from Aviation Formulary V1.42 by Ed Williams
491 # by way of http://mathforum.org/library/drmath/view/55417.html
492 0 my $direction = atan2(sin($lon1-$lon0)*cos($lat1),
493 cos($lat0)*sin($lat1)-sin($lat0)*cos($lat1)*cos($lon1-$lon0));
494 0 ▶ if ($direction < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction < 0) 0 0 0
495 # make direction positive
496 0 $direction += 2 * pi;
497 }
498  
499 0 return rad2deg($direction);
500 }
501  
502 =over
503  
504 =item count_digihops($header)
505  
506 Count the number of digipeated hops in a (KISS) packet and
507 return it. Returns -1 in case of error.
508 The header parameter can contain the full packet or just the header
509 in TNC2 format. All callsigns in the header must be AX.25 compatible
510 and remember that the number returned is just an educated guess, not
511 absolute truth.
512  
513 =back
514  
515 =cut
516  
517 sub count_digihops($) {
518 0 ▶ my $header = shift @_;
Subroutine count_digihops: not called CC=22 SCAR=62.3
Pod documented
519  
520 # Do a rough check on the header syntax
521 0 $header =~ tr/\r\n//d;
522 0 $header = uc($header);
523 0 ▶ if ($header =~ /^([^:]+):/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^([^:]+):/o) 0 0 0
524 # remove data part of packet, if present
525 0 $header = $1;
526 }
527 0 my $hops = undef;
528 0 ▶ if ($header =~ /^([A-Z0-9-]+)\>([A-Z0-9-]+)$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($header =~ /^([A-Z0-9-]+)\>([A-Z0-9-]+)$/o) { } 0 0 0
elsif ($header =~ /^([A-Z0-9-]+)\>([A-Z0-9-]+),([A-Z0-9,*-]+)$/o) { } 0 0 0
529 # check the callsigns for validity
530 0 my $retval = check_ax25_call($1);
531 0 ▶ if (not(defined($retval))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retval) 0 0 0
532 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
533 0 warn "count_digihops: invalid source callsign ($1)\n";
534 }
535 0 return -1;
536 }
537 0 $retval = check_ax25_call($2);
538 0 ▶ if (not(defined($retval))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retval) 0 0 0
539 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
540 0 warn "count_digihops: invalid destination callsign ($2)\n";
541 }
542 0 return -1;
543 }
544 # no path at all, so zero hops
545 0 return 0;
546  
547 } elsif ($header =~ /^([A-Z0-9-]+)\>([A-Z0-9-]+),([A-Z0-9,*-]+)$/o) {
548 0 my $retval = check_ax25_call($1);
549 0 ▶ if (not(defined($retval))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retval) 0 0 0
550 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
551 0 warn "count_digihops: invalid source callsign ($1)\n";
552 }
553 0 return -1;
554 }
555 0 $retval = check_ax25_call($2);
556 0 ▶ if (not(defined($retval))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retval) 0 0 0
557 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
558 0 warn "count_digihops: invalid destination callsign ($2)\n";
559 }
560 0 return -1;
561 }
562 # some hops
563 0 $hops = $3;
564  
565 } else {
566 # invalid
567 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
568 0 warn "count_digihops: invalid packet header\n";
569 }
570 0 return -1;
571 }
572  
573 0 my $hopcount = 0;
574 # split the path into parts
575 0 my @parts = split(/,/, $hops);
576 # now examine the parts one by one
577 0 foreach my $piece (@parts) {
578 # remove the possible "digistar" from the end of callsign
579 # and take note of its existence
580 0 my $wasdigied = 0;
581 0 ▶ if ($piece =~ /^[A-Z0-9-]+\*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece =~ /^[A-Z0-9-]+\*$/o) 0 0 0
582 0 $wasdigied = 1;
583 0 $piece =~ s/\*$//;
584 }
585 # check the callsign for validity and expand it
586 0 my $call = check_ax25_call($piece);
587 0 ▶ if (not(defined($call))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $call) 0 0 0
588 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
589 0 warn "count_digihops: invalid callsign in path ($piece)\n";
590 }
591 0 return -1;
592 }
593 # check special cases, wideN-N and traceN-N for now
594 0 ▶ if ($call =~ /^WIDE([1-7])-([0-7])$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($call =~ /^WIDE([1-7])-([0-7])$/o) { } 0 0 0
elsif ($call =~ /^TRACE([1-7])-([0-7])$/o) { } 0 0 0
595 0 my $difference = $1 - $2;
596 0 ▶ if ($difference < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($difference < 0) 0 0 0
597 # ignore reversed N-N
598 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
599 0 warn "count_digihops: reversed N-N in path ($call)\n";
600 }
601 0 next;
602 }
603 0 $hopcount += $difference;
604  
605 } elsif ($call =~ /^TRACE([1-7])-([0-7])$/o) {
606 # skip traceN-N because the hops are already individually shown
607 # before this
608 0 next;
609  
610 } else {
611 # just a normal packet. if "digistar" is there,
612 # increment the digicounter by one
613 0 ▶ if ($wasdigied == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wasdigied == 1) 0 0 0
614 0 $hopcount++;
615 }
616 }
617 }
618  
619 0 return $hopcount;
620 }
621  
622  
623 # Return a unix timestamp based on an
624 # APRS six (+ one char for type) character timestamp.
625 # If an invalid timestamp is given, return 0.
626 sub _parse_timestamp($$) {
627 15 ▶ my($options, $stamp) = @_;
Subroutine _parse_timestamp: called CC=27 SCAR=36.1
628
629 # Check initial format
630 15 ▶ return 0 if ($stamp !~ /^(\d{2})(\d{2})(\d{2})(z|h|\/)$/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $stamp =~ m[^(\d{2})(\d{2})(\d{2})(z|h|/)$]o 0 15 15
631
632 15 ▶ return "$1$2$3" if ($options->{'raw_timestamp'});
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"raw_timestamp"} 3 12 15
633
634 12 my $stamptype = $4;
635
636 12 ▶ if ($stamptype eq 'h') {
Branch: 2 branches
BranchTrueFalseTotal
if ($stamptype eq 'h') { } 3 9 12
elsif ($stamptype eq 'z' or $stamptype eq '/') { } 9 0 9
637 # HMS format
638 3 my $hour = $1;
639 3 my $minute = $2;
640 3 my $second = $3;
641
642 # Check for invalid time
643 3 ▶ if ($hour > 23 || $minute > 59 || $second > 59) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hour > 23 or $minute > 59 or $second > 59) 0 3 3
Condition33%2/6
$hour > 23 or $minute > 59 or $second > 59
l!l&&r!l&&!r
003
$hour > 23 or $minute > 59
l!l&&r!l&&!r
003
MC/DC0%0/3
$hour > 23 or $minute > 59 or $second > 59
$hour > 23 $minute > 59 $second > 59
Truth table25%1/4
A or B or C
A= $hour > 23B= $minute > 59C= $second > 59
ABCresult
1XX1
01X1
0011
0000
644 0 return 0;
645 }
646
647 # All calculations here are in UTC, but
648 # if this is run under old MacOS (pre-OSX), then
649 # Date_to_Time could be in local time..
650 3 my $currenttime = time();
651 3 my ($cyear, $cmonth, $cday) = Today(1);
652 3 my $tstamp = Date_to_Time($cyear, $cmonth, $cday, $hour, $minute, $second);
653
654 # If the time is more than about one hour
655 # into the future, roll the timestamp
656 # one day backwards.
657 3 ▶ if ($currenttime + 3900 < $tstamp) {
Branch: 2 branches
BranchTrueFalseTotal
if ($currenttime + 3900 < $tstamp) { } 2 1 3
elsif ($currenttime - 82500 > $tstamp) { } 0 1 1
658 2 $tstamp -= 86400;
659 # If the time is more than about 23 hours
660 # into the past, roll the timestamp one
661 # day forwards.
662 } elsif ($currenttime - 82500 > $tstamp) {
663 0 $tstamp += 86400;
664 }
665 3 return $tstamp;
666  
667 ▶ } elsif ($stamptype eq 'z' ||
Condition66%2/3
$stamptype eq "z" or $stamptype eq "/"
l!l&&r!l&&!r
810
MC/DC0%0/2
$stamptype eq "z" or $stamptype eq "/"
$stamptype eq "z" $stamptype eq "/"
Truth table66%2/3
A or B
A= $stamptype eq "z"B= $stamptype eq "/"
ABresult
1X1
011
000
668 $stamptype eq '/') {
669 # Timestamp is DHM, UTC (z) or local (/).
670 # Always intepret local to mean local
671 # to this computer.
672 9 my $day = $1;
673 9 my $hour = $2;
674 9 my $minute = $3;
675
676 9 ▶ if ($day < 1 || $day > 31 || $hour > 23 || $minute > 59) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day < 1 or $day > 31 or $hour > 23 or $minute > 59) 0 9 9
Condition33%3/9
$day < 1 or $day > 31 or $hour > 23 or $minute > 59
l!l&&r!l&&!r
009
$day < 1 or $day > 31 or $hour > 23
l!l&&r!l&&!r
009
$day < 1 or $day > 31
l!l&&r!l&&!r
009
MC/DC0%0/4
$day < 1 or $day > 31 or $hour > 23 or $minute > 59
$day < 1 $day > 31 $hour > 23 $minute > 59
Truth table20%1/5
A or B or C or D
A= $day < 1B= $day > 31C= $hour > 23D= $minute > 59
ABCDresult
1XXX1
01XX1
001X1
00011
00000
677 0 return 0;
678 }
679
680 # If time is under about 12 hours into
681 # the future, go there.
682 # Otherwise get the first matching
683 # time in the past.
684 9 my $currenttime = time();
685 9 my ($cyear, $cmonth, $cday);
686 9 ▶ if ($stamptype eq 'z') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stamptype eq 'z') { } 8 1 9
687 8 ($cyear, $cmonth, $cday) = Today(1);
688 } else {
689 1 ($cyear, $cmonth, $cday) = Today(0);
690 }
691 # Form the possible timestamps in
692 # this, the next and the previous month
693 9 my ($fwdyear, $fwdmonth) = (Add_Delta_YM($cyear, $cmonth, $cday, 0, 1))[0,1];
694 9 my ($backyear, $backmonth) = (Add_Delta_YM($cyear, $cmonth, $cday, 0, -1))[0,1];
695 9 my $fwdtstamp = undef;
696 9 my $currtstamp = undef;
697 9 my $backtstamp = undef;
698 9 ▶ if (check_date($cyear, $cmonth, $day)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_date($cyear, $cmonth, $day)) 9 0 9
699 9 ▶ if ($stamptype eq 'z') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stamptype eq 'z') { } 8 1 9
700 8 $currtstamp = Date_to_Time($cyear, $cmonth, $day, $hour, $minute, 0);
701 } else {
702 1 $currtstamp = Mktime($cyear, $cmonth, $day, $hour, $minute, 0);
703 }
704 }
705 9 ▶ if (check_date($fwdyear, $fwdmonth, $day)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_date($fwdyear, $fwdmonth, $day)) 9 0 9
706 9 ▶ if ($stamptype eq 'z') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stamptype eq 'z') { } 8 1 9
707 8 $fwdtstamp = Date_to_Time($fwdyear, $fwdmonth, $day, $hour, $minute, 0);
708 } else {
709 1 $fwdtstamp = Mktime($cyear, $cmonth, $day, $hour, $minute, 0);
710 }
711 }
712 9 ▶ if (check_date($backyear, $backmonth, $day)) {
Branch: 1 branch
BranchTrueFalseTotal
if (check_date($backyear, $backmonth, $day)) 9 0 9
713 9 ▶ if ($stamptype eq 'z') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stamptype eq 'z') { } 8 1 9
714 8 $backtstamp = Date_to_Time($backyear, $backmonth, $day, $hour, $minute, 0);
715 } else {
716 1 $backtstamp = Mktime($cyear, $cmonth, $day, $hour, $minute, 0);
717 }
718 }
719 # Select the timestamp to use. Pick the timestamp
720 # that is largest, but under about 12 hours from
721 # current time.
722 9 ▶ if (defined($fwdtstamp) && ($fwdtstamp - $currenttime) < 43400) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $fwdtstamp and $fwdtstamp - $currenttime < 43400) { } 1 8 9
elsif (defined $currtstamp and $currtstamp - $currenttime < 43400) { } 4 4 8
elsif (defined $backtstamp) { } 4 0 4
Condition66%2/3
defined $fwdtstamp and $fwdtstamp - $currenttime < 43400
!ll&&!rl&&r
081
MC/DC50%1/2
defined $fwdtstamp and $fwdtstamp - $currenttime < 43400
defined $fwdtstamp $fwdtstamp - $currenttime < 43400
Truth table66%2/3
A and B
A= defined $fwdtstampB= $fwdtstamp - $currenttime < 43400
ABresult
0X0
100
111
723 1 return $fwdtstamp;
724 ▶ } elsif (defined($currtstamp) && ($currtstamp - $currenttime) < 43400) {
Condition66%2/3
defined $currtstamp and $currtstamp - $currenttime < 43400
!ll&&!rl&&r
044
MC/DC50%1/2
defined $currtstamp and $currtstamp - $currenttime < 43400
defined $currtstamp $currtstamp - $currenttime < 43400
Truth table66%2/3
A and B
A= defined $currtstampB= $currtstamp - $currenttime < 43400
ABresult
0X0
100
111
725 4 return $currtstamp;
726 } elsif (defined($backtstamp)) {
727 4 return $backtstamp;
728 }
729 }
730  
731 # return failure if we haven't returned with
732 # a success earlier
733 0 return 0;
734 }
735  
736 # clean up a comment string - remove control codes
737 # but stay UTF-8 clean
738 sub _cleanup_comment($)
739 {
740 35 ▶ $_[0] =~ tr/[\x20-\x7e\x80-\xfe]//cd;
Subroutine _cleanup_comment: called CC=1 SCAR=0.0
741 35 $_[0] =~ s/^\s+//;
742 35 $_[0] =~ s/\s+$//;
743
744 35 return $_[0];
745 }
746  
747 # Return position resolution in meters based on the number
748 # of minute decimal digits. Also accepts negative numbers,
749 # i.e. -1 for 10 minute resolution and -2 for 1 degree resolution.
750 # Calculation is based on latitude so it is worst case
751 # (resolution in longitude gets better as you get closer to the poles).
752 sub _get_posresolution($)
753 {
754 42 ▶ return $knot_to_kmh * ($_[0] <= -2 ? 600 : 1000) * 10 ** (-1 * $_[0]);
Subroutine _get_posresolution: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] <= -2 ? : 1 41 42
755 }
756  
757  
758 # return an NMEA latitude or longitude.
759 # 1st parameter is the (dd)dmm.m(mmm..) string and
760 # 2nd is the north/south or east/west indicator
761 # returns undef on error. The returned value
762 # is decimal degrees, north and east positive.
763 sub _nmea_getlatlon($$$)
764 {
765 2 ▶ my ($value, $sign, $rh) = @_;
Subroutine _nmea_getlatlon: called CC=8 SCAR=25.4
766
767 # upcase the sign for compatibility
768 2 $sign = uc($sign);
769  
770 # Be leninent on what to accept, anything
771 # goes as long as degrees has 1-3 digits,
772 # minutes has 2 digits and there is at least
773 # one decimal minute.
774 2 ▶ if ($value =~ /^\s*(\d{1,3})([0-5][0-9])\.(\d+)\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\s*(\d{1,3})([0-5][0-9])\.(\d+)\s*$/o) { } 2 0 2
775 2 my $minutes = $2 . '.' . $3;
776 2 $value = $1 + ($minutes / 60);
777 # capture position resolution in meters based
778 # on the amount of minute decimals present
779 2 $rh->{'posresolution'} = _get_posresolution(length($3));
780 } else {
781 0 _a_err($rh, 'nmea_inv_cval', $value);
782 0 return undef;
783 }
784  
785 2 ▶ if ($sign =~ /^\s*[EW]\s*$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sign =~ /^\s*[EW]\s*$/o) { } 1 1 2
elsif ($sign =~ /^\s*[NS]\s*$/o) { } 1 0 1
786 # make sure the value is ok
787 1 ▶ if ($value > 179.999999) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > "179.999999") 0 1 1
788 0 _a_err($rh, 'nmea_large_ew', $value);
789 0 return undef;
790 }
791 # west negative
792 1 ▶ if ($sign =~ /^\s*W\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign =~ /^\s*W\s*$/o) 1 0 1
793 1 $value *= -1;
794 }
795 } elsif ($sign =~ /^\s*[NS]\s*$/o) {
796 # make sure the value is ok
797 1 ▶ if ($value > 89.999999) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > "89.999999") 0 1 1
798 0 _a_err($rh, 'nmea_large_ns', $value);
799 0 return undef;
800 }
801 # south negative
802 1 ▶ if ($sign =~ /^\s*S\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign =~ /^\s*S\s*$/o) 0 1 1
803 0 $value *= -1;
804 }
805 } else {
806 # incorrect sign
807 0 _a_err($rh, 'nmea_inv_sign', $sign);
808 0 return undef;
809 }
810  
811 # all ok
812 2 return $value;
813 }
814  
815  
816 # return a two element array, first containing
817 # the symbol table id (or overlay) and second
818 # containing symbol id. return undef in error
819 sub _get_symbol_fromdst($) {
820 1 ▶ my $dstcallsign = shift @_;
Subroutine _get_symbol_fromdst: called CC=18 SCAR=56.0
821  
822 1 my $table = undef;
823 1 my $code = undef;
824  
825 1 ▶ if ($dstcallsign =~ /^(GPS|SPC)([A-Z0-9]{2,3})/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dstcallsign =~ /^(GPS|SPC)([A-Z0-9]{2,3})/o) { } 0 1 1
826 0 my $leftoverstring = $2;
827 0 my $type = substr($leftoverstring, 0, 1);
828 0 my $sublength = length($leftoverstring);
829 0 ▶ if ($sublength == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sublength == 3) { } 0 0 0
830 0 ▶ if ($type eq 'C' || $type eq 'E') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'C' or $type eq 'E') { } 0 0 0
Condition0%0/3
$type eq "C" or $type eq "E"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "C" or $type eq "E"
$type eq "C" $type eq "E"
Truth table0%0/3
A or B
A= $type eq "C"B= $type eq "E"
ABresult
1X1
011
000
831 0 my $numberid = substr($leftoverstring, 1, 2);
832 0 ▶ if ($numberid =~ /^(\d{2})$/o &&
Branch: 1 branch
BranchTrueFalseTotal
if ($numberid =~ /^(\d{2})$/o and $numberid > 0 and $numberid < 95) { } 0 0 0
Condition0%0/6
$numberid =~ /^(\d{2})$/o and $numberid > 0 and $numberid < 95
!ll&&!rl&&r
000
$numberid =~ /^(\d{2})$/o and $numberid > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$numberid =~ /^(\d{2})$/o and $numberid > 0 and $numberid < 95
$numberid =~ /^(\d{2})$/o $numberid > 0 $numberid < 95
Truth table0%0/4
A and B and C
A= $numberid =~ /^(\d{2})$/oB= $numberid > 0C= $numberid < 95
ABCresult
0XX0
10X0
1100
1111
833 $numberid > 0 &&
834 $numberid < 95) {
835 0 $code = chr($1 + 32);
836 0 ▶ if ($type eq 'C') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'C') { } 0 0 0
837 0 $table = '/';
838 } else {
839 0 $table = "\\";
840 }
841 0 return ($table, $code);
842 } else {
843 0 return undef;
844 }
845 } else {
846 # secondary symbol table, with overlay
847 # Check first that we really are in the
848 # secondary symbol table
849 0 my $dsttype = substr($leftoverstring, 0, 2);
850 0 my $overlay = substr($leftoverstring, 2, 1);
851 0 ▶ if (($type eq 'O' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'O' || $type eq 'A' || $type eq 'N' || $type eq 'D' || $type eq 'S' || $type eq 'Q' and $overlay =~ /^[A-Z0-9]$/o) { } 0 0 0
Condition0%0/18
$type eq "O" || $type eq "A" || $type eq "N" || $type eq "D" || $type eq "S" || $type eq "Q" and $overlay =~ /^[A-Z0-9]$/o
!ll&&!rl&&r
000
$type eq "O" or $type eq "A" or $type eq "N" or $type eq "D" or $type eq "S" or $type eq "Q"
l!l&&r!l&&!r
000
$type eq "O" or $type eq "A" or $type eq "N" or $type eq "D" or $type eq "S"
l!l&&r!l&&!r
000
$type eq "O" or $type eq "A" or $type eq "N" or $type eq "D"
l!l&&r!l&&!r
000
$type eq "O" or $type eq "A" or $type eq "N"
l!l&&r!l&&!r
000
$type eq "O" or $type eq "A"
l!l&&r!l&&!r
000
MC/DC0%0/7
$type eq "O" || $type eq "A" || $type eq "N" || $type eq "D" || $type eq "S" || $type eq "Q" and $overlay =~ /^[A-Z0-9]$/o
$type eq "O" $type eq "A" $type eq "N" $type eq "D" $type eq "S" $type eq "Q" $overlay =~ /^[A-Z0-9]$/o
Truth table0%0/13
A or B or C or D or E or F and G
A= $type eq "O"B= $type eq "A"C= $type eq "N"D= $type eq "D"E= $type eq "S"F= $type eq "Q"G= $overlay =~ /^[A-Z0-9]$/o
ABCDEFGresult
000000X0
1XXXXX00
01XXXX00
001XXX00
0001XX00
00001X00
00000100
1XXXXX11
01XXXX11
001XXX11
0001XX11
00001X11
00000111
852 $type eq 'A' ||
853 $type eq 'N' ||
854 $type eq 'D' ||
855 $type eq 'S' ||
856 $type eq 'Q') && $overlay =~ /^[A-Z0-9]$/o) {
857 0 ▶ if (defined($dstsymbol{$dsttype})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dstsymbol{$dsttype}) { } 0 0 0
858 0 $code = substr($dstsymbol{$dsttype}, 1, 1);
859 0 return ($overlay, $code);
860 } else {
861 0 return undef;
862 }
863 } else {
864 0 return undef;
865 }
866 }
867 } else {
868 # primary or secondary symbol table, no overlay
869 0 ▶ if (defined($dstsymbol{$leftoverstring})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dstsymbol{$leftoverstring}) { } 0 0 0
870 0 $table = substr($dstsymbol{$leftoverstring}, 0, 1);
871 0 $code = substr($dstsymbol{$leftoverstring}, 1, 1);
872 0 return ($table, $code);
873 } else {
874 0 return undef;
875 }
876 }
877 } else {
878 1 return undef;
879 }
880  
881 # failsafe catch-all
882 0 return undef;
883 }
884  
885  
886 # Parse an NMEA location
887 sub _nmea_to_decimal($$$$$) {
888 #(substr($body, 1), $srccallsign, $dstcallsign, \%poshash)
889 1 ▶ my($options, $body, $srccallsign, $dstcallsign, $rethash) = @_;
Subroutine _nmea_to_decimal: called CC=42 SCAR=62.9
890  
891 1 ▶ if ($debug > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 1 1
892 # print packet, after stripping control chars
893 0 my $printbody = $body;
894 0 $printbody =~ tr/[\x00-\x1f]//d;
895 0 warn "NMEA: from $srccallsign to $dstcallsign: $printbody\n";
896 }
897  
898 # verify checksum first, if it is provided
899 1 $body =~ s/\s+$//; # remove possible white space from the end
900 1 ▶ if ($body =~ /^([\x20-\x7e]+)\*([0-9A-F]{2})$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^([\x20-\x7e]+)\*([0-9A-F]{2})$/io) 1 0 1
901 1 my $checksumarea = $1;
902 1 my $checksumgiven = hex($2);
903 1 my $checksumcalculated = 0;
904 1 ▶ for (my $i = 0; $i < length($checksumarea); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $checksumarea) 64 1 65
905 64 $checksumcalculated ^= ord(substr($checksumarea, $i, 1));
906 }
907 1 ▶ if ($checksumgiven != $checksumcalculated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksumgiven != $checksumcalculated) 0 1 1
908 # invalid checksum
909 0 _a_err($rethash, 'nmea_inv_cksum');
910 0 return 0;
911 }
912 # make a note of the existance of a checksum
913 1 $rethash->{'checksumok'} = 1;
914 }
915  
916 # checksum ok or not provided
917  
918 1 $rethash->{'format'} = 'nmea';
919
920 # use a dot as a default symbol if one is not defined in
921 # the destination callsign
922 1 my ($symtable, $symcode) = _get_symbol_fromdst($dstcallsign);
923 1 ▶ if (not(defined($symtable)) || not(defined($symcode))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $symtable && defined $symcode) { } 1 0 1
Condition33%1/3
defined $symtable && defined $symcode
!ll&&!rl&&r
100
MC/DC0%0/2
defined $symtable && defined $symcode
defined $symtable defined $symcode
Truth table33%1/3
A && B
A= defined $symtableB= defined $symcode
ABresult
0X0
100
111
924 1 $rethash->{'symboltable'} = '/';
925 1 $rethash->{'symbolcode'} = '/';
926 } else {
927 0 $rethash->{'symboltable'} = $symtable;
928 0 $rethash->{'symbolcode'} = $symcode;
929 }
930  
931 # Split to NMEA fields
932 1 $body =~ s/\*[0-9A-F]{2}$//; # remove checksum from body first
933 1 my @nmeafields = split(/,/, $body);
934  
935 # Now check the sentence type and get as much info
936 # as we can (want).
937 1 ▶ if ($nmeafields[0] eq 'GPRMC') {
Branch: 3 branches
BranchTrueFalseTotal
if ($nmeafields[0] eq 'GPRMC') { } 1 0 1
elsif ($nmeafields[0] eq 'GPGGA') { } 0 0 0
elsif ($nmeafields[0] eq 'GPGLL') { } 0 0 0
938 # we want at least 10 fields
939 1 ▶ if (@nmeafields < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nmeafields < 10) 0 1 1
940 0 _a_err($rethash, 'gprmc_fewfields', scalar(@nmeafields));
941 0 return 0;
942 }
943  
944 1 ▶ if ($nmeafields[2] ne 'A') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[2] ne "A") 0 1 1
945 # invalid position
946 0 _a_err($rethash, 'gprmc_nofix');
947 0 return 0;
948 }
949  
950 # check and save the timestamp
951 1 my ($hour, $minute, $second);
952 1 ▶ if ($nmeafields[1] =~ /^\s*(\d{2})(\d{2})(\d{2})(|\.\d+)\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[1] =~ /^\s*(\d{2})(\d{2})(\d{2})(|\.\d+)\s*$/o) { } 1 0 1
953 # if seconds has a decimal part, ignore it
954 # leap seconds are not taken into account...
955 1 ▶ if ($1 > 23 || $2 > 59 || $3 > 59) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 > 23 or $2 > 59 or $3 > 59) 0 1 1
Condition33%2/6
$1 > 23 or $2 > 59 or $3 > 59
l!l&&r!l&&!r
001
$1 > 23 or $2 > 59
l!l&&r!l&&!r
001
MC/DC0%0/3
$1 > 23 or $2 > 59 or $3 > 59
$1 > 23 $2 > 59 $3 > 59
Truth table25%1/4
A or B or C
A= $1 > 23B= $2 > 59C= $3 > 59
ABCresult
1XX1
01X1
0011
0000
956 0 _a_err($rethash, 'gprmc_inv_time', $nmeafields[1]);
957 0 return 0;
958 }
959 1 $hour = $1 + 0; # force numeric
960 1 $minute = $2 + 0;
961 1 $second = $3 + 0;
962 } else {
963 0 _a_err($rethash, 'gprmc_inv_time');
964 0 return 0;
965 }
966 1 my ($year, $month, $day);
967 1 ▶ if ($nmeafields[9] =~ /^\s*(\d{2})(\d{2})(\d{2})\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[9] =~ /^\s*(\d{2})(\d{2})(\d{2})\s*$/o) { } 1 0 1
968 # check the date for validity. Assume
969 # years 0-69 are 21st century and years
970 # 70-99 are 20th century
971 1 $year = 2000 + $3;
972 1 ▶ if ($3 >= 70) {
Branch: 1 branch
BranchTrueFalseTotal
if ($3 >= 70) 0 1 1
973 0 $year = 1900 + $3;
974 }
975 # check for invalid date
976 1 ▶ if (not(check_date($year, $2, $1))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (check_date($year, $2, $1)) 0 1 1
977 0 _a_err($rethash, 'gprmc_inv_date', "$year $2 $1");
978 0 return 0;
979 }
980 1 $month = $2 + 0; # force numeric
981 1 $day = $1 + 0;
982 } else {
983 0 _a_err($rethash, 'gprmc_inv_date');
984 0 return 0;
985 }
986 # Date_to_Time() can only handle 32-bit unix timestamps,
987 # so make sure it is not used for those years that
988 # are outside that range.
989 1 ▶ if ($year >= 2038 || $year < 1970) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year >= 2038 or $year < 1970) { } 0 1 1
Condition33%1/3
$year >= 2038 or $year < 1970
l!l&&r!l&&!r
001
MC/DC0%0/2
$year >= 2038 or $year < 1970
$year >= 2038 $year < 1970
Truth table33%1/3
A or B
A= $year >= 2038B= $year < 1970
ABresult
1X1
011
000
990 0 $rethash->{'timestamp'} = 0;
991 0 _a_err($rethash, 'gprmc_date_out', $year);
992 0 return 0;
993 } else {
994 1 $rethash->{'timestamp'} = Date_to_Time($year, $month, $day, $hour, $minute, $second);
995 }
996  
997 # speed (knots) and course, make these optional
998 # in the parsing sense (don't fail if speed/course
999 # can't be decoded).
1000 1 ▶ if ($nmeafields[7] =~ /^\s*(\d+(|\.\d+))\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[7] =~ /^\s*(\d+(|\.\d+))\s*$/o) 1 0 1
1001 # convert to km/h
1002 1 $rethash->{'speed'} = $1 * $knot_to_kmh;
1003 }
1004 1 ▶ if ($nmeafields[8] =~ /^\s*(\d+(|\.\d+))\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[8] =~ /^\s*(\d+(|\.\d+))\s*$/o) { } 1 0 1
1005 # round to nearest integer
1006 1 my $course = int($1 + 0.5);
1007 # if zero, set to 360 because in APRS
1008 # zero means invalid course...
1009 1 ▶ if ($course == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($course == 0) { } 0 1 1
elsif ($course > 360) { } 0 1 1
1010 0 $course = 360;
1011 } elsif ($course > 360) {
1012 0 $course = 0; # invalid
1013 }
1014 1 $rethash->{'course'} = $course;
1015 } else {
1016 0 $rethash->{'course'} = 0; # unknown
1017 }
1018  
1019 # latitude and longitude
1020 1 my $latitude = _nmea_getlatlon($nmeafields[3], $nmeafields[4], $rethash);
1021 1 ▶ if (not(defined($latitude))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $latitude) 0 1 1
1022 0 return 0;
1023 }
1024 1 $rethash->{'latitude'} = $latitude;
1025 1 my $longitude = _nmea_getlatlon($nmeafields[5], $nmeafields[6], $rethash);
1026 1 ▶ if (not(defined($longitude))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $longitude) 0 1 1
1027 0 return 0;
1028 }
1029 1 $rethash->{'longitude'} = $longitude;
1030  
1031 # we have everything we want, return
1032 1 return 1;
1033  
1034 } elsif ($nmeafields[0] eq 'GPGGA') {
1035 # we want at least 11 fields
1036 0 ▶ if (@nmeafields < 11) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nmeafields < 11) 0 0 0
1037 0 _a_err($rethash, 'gpgga_fewfields', scalar(@nmeafields));
1038 0 return 0;
1039 }
1040  
1041 # check for position validity
1042 0 ▶ if ($nmeafields[6] =~ /^\s*(\d+)\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[6] =~ /^\s*(\d+)\s*$/o) { } 0 0 0
1043 0 ▶ if ($1 < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 < 1) 0 0 0
1044 0 _a_err($rethash, 'gpgga_nofix', $1);
1045 0 return 0;
1046 }
1047 } else {
1048 0 _a_err($rethash, 'gpgga_nofix');
1049 0 return 0;
1050 }
1051  
1052 # Use the APRS time parsing routines to check
1053 # the time and convert it to timestamp.
1054 # But before that, remove a possible decimal part
1055 0 $nmeafields[1] =~ s/\.\d+$//;
1056 0 $rethash->{'timestamp'} = _parse_timestamp($options, $nmeafields[1] . 'h');
1057 0 ▶ if ($rethash->{'timestamp'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{"timestamp"} == 0) 0 0 0
1058 0 _a_err($rethash, 'timestamp_inv_gpgga');
1059 0 return 0;
1060 }
1061  
1062 # latitude and longitude
1063 0 my $latitude = _nmea_getlatlon($nmeafields[2], $nmeafields[3], $rethash);
1064 0 ▶ if (not(defined($latitude))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $latitude) 0 0 0
1065 0 return 0;
1066 }
1067 0 $rethash->{'latitude'} = $latitude;
1068 0 my $longitude = _nmea_getlatlon($nmeafields[4], $nmeafields[5], $rethash);
1069 0 ▶ if (not(defined($longitude))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $longitude) 0 0 0
1070 0 return 0;
1071 }
1072 0 $rethash->{'longitude'} = $longitude;
1073  
1074 # altitude, only meters are accepted
1075 0 ▶ if ($nmeafields[10] eq 'M' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[10] eq "M" and $nmeafields[9] =~ /^(-?\d+(|\.\d+))$/o) 0 0 0
Condition0%0/3
$nmeafields[10] eq "M" and $nmeafields[9] =~ /^(-?\d+(|\.\d+))$/o
!ll&&!rl&&r
000
MC/DC0%0/2
$nmeafields[10] eq "M" and $nmeafields[9] =~ /^(-?\d+(|\.\d+))$/o
$nmeafields[10] eq "M" $nmeafields[9] =~ /^(-?\d+(|\.\d+))$/o
Truth table0%0/3
A and B
A= $nmeafields[10] eq "M"B= $nmeafields[9] =~ /^(-?\d+(|\.\d+))$/o
ABresult
0X0
100
111
1076 $nmeafields[9] =~ /^(-?\d+(|\.\d+))$/o) {
1077 # force numeric interpretation
1078 0 $rethash->{'altitude'} = $1 + 0;
1079 }
1080  
1081 # ok
1082 0 return 1;
1083  
1084 } elsif ($nmeafields[0] eq 'GPGLL') {
1085 # we want at least 5 fields
1086 0 ▶ if (@nmeafields < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nmeafields < 5) 0 0 0
1087 0 _a_err($rethash, 'gpgll_fewfields', scalar(@nmeafields));
1088 0 return 0;
1089 }
1090  
1091 # latitude and longitude
1092 0 my $latitude = _nmea_getlatlon($nmeafields[1], $nmeafields[2], $rethash);
1093 0 ▶ if (not(defined($latitude))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $latitude) 0 0 0
1094 0 return 0;
1095 }
1096 0 $rethash->{'latitude'} = $latitude;
1097 0 my $longitude = _nmea_getlatlon($nmeafields[3], $nmeafields[4], $rethash);
1098 0 ▶ if (not(defined($longitude))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $longitude) 0 0 0
1099 0 return 0;
1100 }
1101 0 $rethash->{'longitude'} = $longitude;
1102  
1103 # Use the APRS time parsing routines to check
1104 # the time and convert it to timestamp.
1105 # But before that, remove a possible decimal part
1106 0 ▶ if (@nmeafields >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nmeafields >= 6) 0 0 0
1107 0 $nmeafields[5] =~ s/\.\d+$//;
1108 0 $rethash->{'timestamp'} = _parse_timestamp($options, $nmeafields[5] . 'h');
1109 0 ▶ if ($rethash->{'timestamp'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{"timestamp"} == 0) 0 0 0
1110 0 _a_err($rethash, 'timestamp_inv_gpgll');
1111 0 return 0;
1112 }
1113 }
1114  
1115 0 ▶ if (@nmeafields >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nmeafields >= 7) 0 0 0
1116 # GPS fix validity supplied
1117 0 ▶ if ($nmeafields[6] ne 'A') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmeafields[6] ne "A") 0 0 0
1118 0 _a_err($rethash, 'gpgll_nofix');
1119 0 return 0;
1120 }
1121 }
1122  
1123 # ok
1124 0 return 1;
1125  
1126 ##} elsif ($nmeafields[0] eq 'GPVTG') {
1127 ##} elsif ($nmeafields[0] eq 'GPWPT') {
1128 } else {
1129 0 $nmeafields[0] =~ tr/[\x00-\x1f]//d;
1130 0 _a_err($rethash, 'nmea_unsupp', $nmeafields[0]);
1131 0 return 0;
1132 }
1133  
1134 0 return 0;
1135 }
1136  
1137  
1138 # Parse the possible APRS data extension
1139 # as well as comment
1140 sub _comments_to_decimal($$$) {
1141 22 ▶ my $rest = shift @_;
Subroutine _comments_to_decimal: called CC=14 SCAR=27.5
1142 22 my $srccallsign = shift @_;
1143 22 my $rethash = shift @_;
1144
1145 # First check the possible APRS data extension,
1146 # immediately following the packet
1147 22 ▶ if (length($rest) >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest >= 7) 22 0 22
1148 22 ▶ if ($rest =~ /^([0-9. ]{3})\/([0-9. ]{3})/o) {
Branch: 4 branches
BranchTrueFalseTotal
if ($rest =~ m[^([0-9. ]{3})/([0-9. ]{3})]o) { } 10 12 22
elsif ($rest =~ m[^PHG(\d[\x30-\x7e]\d\d[0-9A-Z])/]o) { } 1 11 12
elsif ($rest =~ /^PHG(\d[\x30-\x7e]\d\d)/o) { } 6 5 11
elsif ($rest =~ /^RNG(\d{4})/o) { } 0 5 5
1149 10 my $course = $1;
1150 10 my $speed = $2;
1151 10 ▶ if ($course =~ /^\d{3}$/o &&
Branch: 1 branch
BranchTrueFalseTotal
if ($course =~ /^\d{3}$/o and $course <= 360 and $course >= 1) { } 10 0 10
Condition33%2/6
$course =~ /^\d{3}$/o and $course <= 360 and $course >= 1
!ll&&!rl&&r
0010
$course =~ /^\d{3}$/o and $course <= 360
!ll&&!rl&&r
0010
MC/DC0%0/3
$course =~ /^\d{3}$/o and $course <= 360 and $course >= 1
$course =~ /^\d{3}$/o $course <= 360 $course >= 1
Truth table25%1/4
A and B and C
A= $course =~ /^\d{3}$/oB= $course <= 360C= $course >= 1
ABCresult
0XX0
10X0
1100
1111
1152 $course <= 360 &&
1153 $course >= 1) {
1154 # force numeric interpretation
1155 10 $course += 0;
1156 10 $rethash->{'course'} = $course;
1157 } else {
1158 # course is invalid, set it to zero
1159 0 $rethash->{'course'} = 0;
1160 }
1161 10 ▶ if ($speed =~ /^\d{3}$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($speed =~ /^\d{3}$/o) { } 10 0 10
1162 # force numeric interpretation
1163 # and convert to km/h
1164 10 $rethash->{'speed'} = $speed * $knot_to_kmh;
1165 } else {
1166 # If speed is invalid, don't set it
1167 # (zero speed is a valid speed).
1168 }
1169 10 $rest = substr($rest, 7);
1170  
1171 } elsif ($rest =~ /^PHG(\d[\x30-\x7e]\d\d[0-9A-Z])\//o) {
1172 # PHGR
1173 1 $rethash->{'phg'} = $1;
1174 1 $rest = substr($rest, 8);
1175  
1176 } elsif ($rest =~ /^PHG(\d[\x30-\x7e]\d\d)/o) {
1177 # don't do anything fancy with PHG, just store it
1178 6 $rethash->{'phg'} = $1;
1179 6 $rest = substr($rest, 7);
1180  
1181 } elsif ($rest =~ /^RNG(\d{4})/o) {
1182 # radio range, in miles, so convert
1183 # to km
1184 0 $rethash->{'radiorange'} = $1 * $mph_to_kmh;
1185 0 $rest = substr($rest, 7);
1186 }
1187 }
1188  
1189 # Check for optional altitude anywhere in the comment,
1190 # take the first occurrence
1191 22 ▶ if ($rest =~ /^(.*?)\/A=(-\d{5}|\d{6})(.*)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ m[^(.*?)/A=(-\d{5}|\d{6})(.*)$]o) 10 12 22
1192 # convert to meters as well
1193 10 $rethash->{'altitude'} = $2 * 0.3048;
1194 10 $rest = $1 . $3;
1195 }
1196  
1197 # Check for new-style base-91 comment telemetry
1198 22 $rest = _comment_telemetry($rethash, $rest);
1199
1200 # Check for !DAO!, take the last occurrence (per recommendation)
1201 22 ▶ if ($rest =~ /^(.*)\!([\x21-\x7b][\x20-\x7b]{2})\!(.*?)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^(.*)\!([\x21-\x7b][\x20-\x7b]{2})\!(.*?)$/o) 6 16 22
1202 6 my $daofound = _dao_parse($2, $srccallsign, $rethash);
1203 6 ▶ if ($daofound == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daofound == 1) 6 0 6
1204 6 $rest = $1 . $3;
1205 }
1206 }
1207
1208 # Strip a / or a ' ' from the beginning of a comment
1209 # (delimiter after PHG or other data stuffed within the comment)
1210 22 $rest =~ s/^[\/\s]//;
1211
1212 # Save the rest as a separate comment, if
1213 # anything is left (trim unprintable chars
1214 # out first and white space from both ends)
1215 22 ▶ if (length($rest) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest > 0) 21 1 22
1216 21 $rethash->{'comment'} = _cleanup_comment($rest);
1217 }
1218  
1219 # Always succeed as these are optional
1220 22 return 1;
1221 }
1222  
1223 # Parse an object
1224 sub _object_to_decimal($$$$) {
1225 2 ▶ my($options, $packet, $srccallsign, $rethash) = @_;
Subroutine _object_to_decimal: called CC=9 SCAR=25.5
1226  
1227 # Minimum length for an object is 31 characters
1228 # (or 46 characters for non-compressed)
1229 2 ▶ if (length($packet) < 31) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $packet < 31) 0 2 2
1230 0 _a_err($rethash, 'obj_short');
1231 0 return 0;
1232 }
1233  
1234 # Parse the object up to the location
1235 2 my $timestamp = undef;
1236 2 ▶ if ($packet =~ /^;([\x20-\x7e]{9})(\*|_)(\d{6})(z|h|\/)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ m[^;([\x20-\x7e]{9})(\*|_)(\d{6})(z|h|/)]o) { } 1 1 2
1237 # hash member 'objectname' signals an object
1238 1 $rethash->{'objectname'} = $1;
1239 1 ▶ if ($2 eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq '*') { } 1 0 1
1240 1 $rethash->{'alive'} = 1;
1241 } else {
1242 0 $rethash->{'alive'} = 0;
1243 }
1244 1 $timestamp = $3 . $4;
1245 } else {
1246 1 _a_err($rethash, 'obj_inv');
1247 1 return 0;
1248 }
1249  
1250 # Check the timestamp for validity and convert
1251 # to UNIX epoch. If the timestamp is invalid, set it
1252 # to zero.
1253 1 $rethash->{'timestamp'} = _parse_timestamp($options, $timestamp);
1254 1 ▶ if ($rethash->{'timestamp'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{"timestamp"} == 0) 0 1 1
1255 0 _a_warn($rethash, 'timestamp_inv_obj');
1256 }
1257  
1258 # Forward the location parsing onwards
1259 1 my $locationoffset = 18; # object location always starts here
1260 1 my $locationchar = substr($packet, $locationoffset, 1);
1261 1 my $retval = undef;
1262 1 ▶ if ($locationchar =~ /^[\/\\A-Za-j]$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($locationchar =~ m[^[/\\A-Za-j]$]o) { } 1 0 1
elsif ($locationchar =~ /^\d$/io) { } 0 0 0
1263 # compressed
1264 1 $retval = _compressed_to_decimal(substr($packet, $locationoffset, 13), $srccallsign, $rethash);
1265 1 $locationoffset += 13; # now points to APRS data extension/comment
1266 } elsif ($locationchar =~ /^\d$/io) {
1267 # normal
1268 0 $retval = _normalpos_to_decimal(substr($packet, $locationoffset), $srccallsign, $rethash);
1269 0 $locationoffset += 19; # now points to APRS data extension/comment
1270 } else {
1271 # error
1272 0 _a_err($rethash, 'obj_dec_err');
1273 0 return 0;
1274 }
1275 1 ▶ return 0 if ($retval != 1);
Branch: 1 branch
BranchTrueFalseTotal
if $retval != 1 0 1 1
1276  
1277 # Check the APRS data extension and possible comments,
1278 # unless it is a weather report (we don't want erroneus
1279 # course/speed figures and weather in the comments..)
1280 1 ▶ if ($rethash->{'symbolcode'} ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{'symbolcode'} ne '_') { } 1 0 1
1281 1 _comments_to_decimal(substr($packet, $locationoffset), $srccallsign, $rethash);
1282 } else {
1283 # possibly a weather object, try to parse
1284 0 _wx_parse(substr($packet, $locationoffset), $rethash);
1285 }
1286  
1287 1 return 1;
1288 }
1289  
1290 # Parse a status report. Only timestamps
1291 # and text report are supported. Maidenhead,
1292 # beam headings and symbols are not.
1293 sub _status_parse($$$$) {
1294 1 ▶ my($options, $packet, $srccallsign, $rethash) = @_;
Subroutine _status_parse: called CC=3 SCAR=11.1
1295  
1296 # Remove CRs, LFs and trailing spaces
1297 1 $packet =~ tr/\r\n//d;
1298 1 $packet =~ s/\s+$//;
1299  
1300 # Check for a timestamp
1301 1 ▶ if ($packet =~ /^(\d{6}z)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ /^(\d{6}z)/o) 1 0 1
1302 1 $rethash->{'timestamp'} = _parse_timestamp({}, $1);
1303 1 ▶ _a_warn($rethash, 'timestamp_inv_sta') if ($rethash->{'timestamp'} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $rethash->{"timestamp"} == 0 0 1 1
1304 1 $packet = substr($packet, 7);
1305 }
1306  
1307 # Save the rest as the report
1308 1 $rethash->{'status'} = $packet;
1309  
1310 1 return 1;
1311 }
1312  
1313 # Parse a station capabilities packet
1314 sub _capabilities_parse($$$) {
1315 0 ▶ my $packet = shift @_;
Subroutine _capabilities_parse: not called CC=5 SCAR=34.0
1316 0 my $srccallsign = shift @_;
1317 0 my $rethash = shift @_;
1318  
1319 # Remove CRs, LFs and trailing spaces
1320 0 $packet =~ tr/\r\n//d;
1321 0 $packet =~ s/\s+$//;
1322 # Then just split the packet, we aren't too picky about the format here.
1323 # Also duplicates and case changes are not handled in any way,
1324 # so the last part will override an earlier part and different
1325 # cases can be present. Just remove trailing/leading spaces.
1326 0 my @caps = split(/,/, $packet);
1327 0 my %caphash = ();
1328 0 foreach my $cap (@caps) {
1329 0 ▶ if ($cap =~ /^\s*([^=]+?)\s*=\s*(.*?)\s*$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cap =~ /^\s*([^=]+?)\s*=\s*(.*?)\s*$/o) { } 0 0 0
elsif ($cap =~ /^\s*([^=]+?)\s*$/o) { } 0 0 0
1330 # TOKEN=VALUE
1331 0 $caphash{$1} = $2;
1332 } elsif ($cap =~ /^\s*([^=]+?)\s*$/o) {
1333 # just TOKEN
1334 0 $caphash{$1} = undef;
1335 }
1336 }
1337  
1338 0 my $keycount = keys(%caphash);
1339 0 ▶ if ($keycount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keycount > 0) 0 0 0
1340 # store the capabilities in the return hash
1341 0 $rethash->{'capabilities'} = \%caphash;
1342 0 return 1;
1343 }
1344
1345 # at least one capability has to be defined for a capability
1346 # packet to be counted as valid
1347 0 return 0;
1348 }
1349  
1350 # Parse a message
1351 # possible TODO: ack piggybacking
1352 sub _message_parse($$$) {
1353 245 ▶ my $packet = shift @_;
Subroutine _message_parse: called CC=6 SCAR=18.5
1354 245 my $srccallsign = shift @_;
1355 245 my $rethash = shift @_;
1356  
1357 # Check format
1358 245 ▶ if ($packet =~ /^:([A-Za-z0-9_ -]{9}):([\x20-\x7e\x80-\xfe]+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ /^:([A-Za-z0-9_ -]{9}):([\x20-\x7e\x80-\xfe]+)$/o) 245 0 245
1359 245 my $destination = $1;
1360 245 my $message = $2;
1361 # remove trailing spaces from the recipient
1362 245 $destination =~ s/\s+$//;
1363 245 $rethash->{'destination'} = $destination;
1364 # check whether this is an ack
1365 245 ▶ if ($message =~ /^ack([A-Za-z0-9}]{1,5})\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /^ack([A-Za-z0-9}]{1,5})\s*$/o) 6 239 245
1366 # trailing spaces are allowed because some
1367 # broken software insert them..
1368 6 $rethash->{'messageack'} = $1;
1369 6 return 1;
1370 }
1371 # check whether this is a message reject
1372 239 ▶ if ($message =~ /^rej([A-Za-z0-9}]{1,5})\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /^rej([A-Za-z0-9}]{1,5})\s*$/o) 6 233 239
1373 6 $rethash->{'messagerej'} = $1;
1374 6 return 1;
1375 }
1376 # separate message-id from the body, if present
1377 233 ▶ if ($message =~ /^([^{]*)\{([A-Za-z0-9}]{1,5})\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /^([^{]*)\{([A-Za-z0-9}]{1,5})\s*$/o) { } 233 0 233
1378 233 $rethash->{'message'} = $1;
1379 233 $rethash->{'messageid'} = $2;
1380 } else {
1381 0 $rethash->{'message'} = $message;
1382 }
1383 # catch telemetry messages
1384 233 ▶ if ($message =~ /^(BITS|PARM|UNIT|EQNS)\./i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /^(BITS|PARM|UNIT|EQNS)\./i) 0 233 233
1385 0 $rethash->{'type'} = 'telemetry-message';
1386 }
1387 233 return 1;
1388 }
1389
1390 0 _a_err($rethash, 'msg_inv');
1391
1392 0 return 0;
1393 }
1394  
1395 #
1396 sub _comment_telemetry($$)
1397 {
1398 32 ▶ my($rethash, $rest) = @_;
Subroutine _comment_telemetry: called CC=7 SCAR=19.5
1399
1400 32 ▶ if ($rest =~ /^(.*)\|([!-{]{2})([!-{]{2})([!-{]{2}|)([!-{]{2}|)([!-{]{2}|)([!-{]{2}|)([!-{]{2}|)\|(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^(.*)\|([!-{]{2})([!-{]{2})([!-{]{2}|)([!-{]{2}|)([!-{]{2}|)([!-{]{2}|)([!-{]{2}|)\|(.*)$/) 6 26 32
1401 6 $rest = $1 . $9;
1402 6 ▶ $rethash->{'telemetry'} = {
Branch: 4 branches
BranchTrueFalseTotal
$4 ne '' ? : 5 1 6
$5 ne '' ? : 5 1 6
$6 ne '' ? : 4 2 6
$7 ne '' ? : 4 2 6
1403 'seq' => (ord(substr($2, 0, 1)) - 33) * 91 +
1404 (ord(substr($2, 1, 1)) - 33),
1405 'vals' => [
1406 (ord(substr($3, 0, 1)) - 33) * 91 +
1407 (ord(substr($3, 1, 1)) - 33),
1408 $4 ne '' ? (ord(substr($4, 0, 1)) - 33) * 91 +
1409 (ord(substr($4, 1, 1)) - 33) : undef,
1410 $5 ne '' ? (ord(substr($5, 0, 1)) - 33) * 91 +
1411 (ord(substr($5, 1, 1)) - 33) : undef,
1412 $6 ne '' ? (ord(substr($6, 0, 1)) - 33) * 91 +
1413 (ord(substr($6, 1, 1)) - 33) : undef,
1414 $7 ne '' ? (ord(substr($7, 0, 1)) - 33) * 91 +
1415 (ord(substr($7, 1, 1)) - 33) : undef,
1416 ]
1417 };
1418 6 ▶ if ($8 ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($8 ne "") 4 2 6
1419 # bits: first, decode the base-91 integer
1420 4 my $bitint = (ord(substr($8, 0, 1)) - 33) * 91 +
1421 (ord(substr($8, 1, 1)) - 33);
1422 # then, decode the 8 bits of telemetry
1423 4 $rethash->{'telemetry'}->{'bits'} = unpack('b8', pack('C', $bitint));
1424 }
1425 }
1426
1427 32 return $rest;
1428 }
1429  
1430 # Parse an item
1431 sub _item_to_decimal($$$) {
1432 0 ▶ my $packet = shift @_;
Subroutine _item_to_decimal: not called CC=8 SCAR=42.8
1433 0 my $srccallsign = shift @_;
1434 0 my $rethash = shift @_;
1435  
1436 # Minimum length for an item is 18 characters
1437 # (or 24 characters for non-compressed)
1438 0 ▶ if (length($packet) < 18) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $packet < 18) 0 0 0
1439 0 _a_err($rethash, 'item_short');
1440 0 return 0;
1441 }
1442  
1443 # Parse the item up to the location
1444 0 ▶ if ($packet =~ /^\)([\x20\x22-\x5e\x60-\x7e]{3,9})(!|_)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ /^\)([\x20\x22-\x5e\x60-\x7e]{3,9})(!|_)/o) { } 0 0 0
1445 # hash member 'itemname' signals an item
1446 0 $rethash->{'itemname'} = $1;
1447 0 ▶ if ($2 eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq '!') { } 0 0 0
1448 0 $rethash->{'alive'} = 1;
1449 } else {
1450 0 $rethash->{'alive'} = 0;
1451 }
1452 } else {
1453 0 _a_err($rethash, 'item_inv');
1454 0 return 0;
1455 }
1456  
1457 # Forward the location parsing onwards
1458 0 my $locationoffset = 2 + length($rethash->{'itemname'});
1459 0 my $locationchar = substr($packet, $locationoffset, 1);
1460 0 my $retval = undef;
1461 0 ▶ if ($locationchar =~ /^[\/\\A-Za-j]$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($locationchar =~ m[^[/\\A-Za-j]$]o) { } 0 0 0
elsif ($locationchar =~ /^\d$/io) { } 0 0 0
1462 # compressed
1463 0 $retval = _compressed_to_decimal(substr($packet, $locationoffset, 13), $srccallsign, $rethash);
1464 0 $locationoffset += 13;
1465 } elsif ($locationchar =~ /^\d$/io) {
1466 # normal
1467 0 $retval = _normalpos_to_decimal(substr($packet, $locationoffset), $srccallsign, $rethash);
1468 0 $locationoffset += 19;
1469 } else {
1470 # error
1471 0 _a_err($rethash, 'item_dec_err');
1472 0 return 0;
1473 }
1474 0 ▶ return 0 if ($retval != 1);
Branch: 1 branch
BranchTrueFalseTotal
if $retval != 1 0 0 0
1475  
1476 # Check the APRS data extension and possible comments,
1477 # unless it is a weather report (we don't want erroneus
1478 # course/speed figures and weather in the comments..)
1479 0 ▶ if ($rethash->{'symbolcode'} ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{"symbolcode"} ne "_") 0 0 0
1480 0 _comments_to_decimal(substr($packet, $locationoffset), $srccallsign, $rethash);
1481 }
1482  
1483 0 return 1;
1484 }
1485  
1486 # Parse a normal uncompressed location
1487 sub _normalpos_to_decimal($$$) {
1488 24 ▶ my $packet = shift @_;
Subroutine _normalpos_to_decimal: called CC=23 SCAR=38.9
1489 24 my $srccallsign = shift @_;
1490 24 my $rethash = shift @_;
1491  
1492 # Check the length
1493 24 ▶ if (length($packet) < 19) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $packet < 19) 0 24 24
1494 0 _a_err($rethash, 'loc_short');
1495 0 return 0;
1496 }
1497
1498 24 $rethash->{'format'} = 'uncompressed';
1499
1500 # Make a more detailed check on the format, but do the
1501 # actual value checks later
1502 24 my $lon_deg = undef;
1503 24 my $lat_deg = undef;
1504 24 my $lon_min = undef;
1505 24 my $lat_min = undef;
1506 24 my $issouth = 0;
1507 24 my $iswest = 0;
1508 24 my $symboltable;
1509 24 ▶ if ($packet =~ /^(\d{2})([0-7 ][0-9 ]\.[0-9 ]{2})([NnSs])(.)(\d{3})([0-7 ][0-9 ]\.[0-9 ]{2})([EeWw])([\x21-\x7b\x7d])/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ /^(\d{2})([0-7 ][0-9 ]\.[0-9 ]{2})([NnSs])(.)(\d{3})([0-7 ][0-9 ]\.[0-9 ]{2})([EeWw])([\x21-\x7b\x7d])/o) { } 23 1 24
1510 23 my $sind = uc($3);
1511 23 my $wind = uc($7);
1512 23 $symboltable = $4;
1513 23 $rethash->{'symbolcode'} = $8;
1514 23 ▶ if ($sind eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sind eq "S") 7 16 23
1515 7 $issouth = 1;
1516 }
1517 23 ▶ if ($wind eq 'W') {
Branch: 1 branch
BranchTrueFalseTotal
if ($wind eq "W") 12 11 23
1518 12 $iswest = 1;
1519 }
1520 23 $lat_deg = $1;
1521 23 $lat_min = $2;
1522 23 $lon_deg = $5;
1523 23 $lon_min = $6;
1524 } else {
1525 1 _a_err($rethash, 'loc_inv');
1526 1 return 0;
1527 }
1528
1529 23 ▶ if ($symboltable !~ /^[\/\\A-Z0-9]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($symboltable =~ m[^[/\\A-Z0-9]$]) 1 22 23
1530 1 _a_err($rethash, 'sym_inv_table');
1531 1 return 0;
1532 }
1533 22 $rethash->{'symboltable'} = $symboltable;
1534
1535 # Check the degree values
1536 22 ▶ if ($lat_deg > 89 || $lon_deg > 179) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat_deg > 89 or $lon_deg > 179) 0 22 22
Condition33%1/3
$lat_deg > 89 or $lon_deg > 179
l!l&&r!l&&!r
0022
MC/DC0%0/2
$lat_deg > 89 or $lon_deg > 179
$lat_deg > 89 $lon_deg > 179
Truth table33%1/3
A or B
A= $lat_deg > 89B= $lon_deg > 179
ABresult
1X1
011
000
1537 0 _a_err($rethash, 'loc_large');
1538 0 return 0;
1539 }
1540  
1541 # Find out the amount of position ambiguity
1542 22 my $tmplat = $lat_min;
1543 22 $tmplat =~ s/\.//; # remove the period
1544 # Count the amount of spaces at the end
1545 22 ▶ if ($tmplat =~ /^(\d{0,4})( {0,4})$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmplat =~ /^(\d{0,4})( {0,4})$/io) { } 22 0 22
1546 22 $rethash->{'posambiguity'} = length($2);
1547 } else {
1548 0 _a_err($rethash, 'loc_amb_inv');
1549 0 return 0;
1550 }
1551  
1552 22 my $latitude = undef;
1553 22 my $longitude = undef;
1554 22 ▶ if ($rethash->{'posambiguity'} == 0) {
Branch: 5 branches
BranchTrueFalseTotal
if ($rethash->{'posambiguity'} == 0) { } 20 2 22
elsif ($rethash->{'posambiguity'} == 4) { } 1 1 2
elsif ($rethash->{'posambiguity'} == 1) { } 0 1 1
elsif ($rethash->{'posambiguity'} == 2) { } 0 1 1
elsif ($rethash->{'posambiguity'} == 3) { } 1 0 1
1555 # No position ambiguity. Check longitude for invalid spaces
1556 20 ▶ if ($lon_min =~ / /io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lon_min =~ / /io) 0 20 20
1557 0 _a_err($rethash, 'loc_amb_inv', 'longitude 0');
1558 0 return 0;
1559 }
1560 20 $latitude = $lat_deg + ($lat_min/60);
1561 20 $longitude = $lon_deg + ($lon_min/60);
1562 } elsif ($rethash->{'posambiguity'} == 4) {
1563 # disregard the minutes and add 0.5 to the degree values
1564 1 $latitude = $lat_deg + 0.5;
1565 1 $longitude = $lon_deg + 0.5;
1566 } elsif ($rethash->{'posambiguity'} == 1) {
1567 # the last digit is not used
1568 0 $lat_min = substr($lat_min, 0, 4);
1569 0 $lon_min = substr($lon_min, 0, 4);
1570 0 ▶ if ($lat_min =~ / /io || $lon_min =~ / /io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat_min =~ / /io or $lon_min =~ / /io) 0 0 0
Condition0%0/3
$lat_min =~ / /io or $lon_min =~ / /io
l!l&&r!l&&!r
000
MC/DC0%0/2
$lat_min =~ / /io or $lon_min =~ / /io
$lat_min =~ / /io $lon_min =~ / /io
Truth table0%0/3
A or B
A= $lat_min =~ / /ioB= $lon_min =~ / /io
ABresult
1X1
011
000
1571 0 _a_err($rethash, 'loc_amb_inv', 'lat/lon 1');
1572 0 return 0;
1573 }
1574 0 $latitude = $lat_deg + (($lat_min + 0.05)/60);
1575 0 $longitude = $lon_deg + (($lon_min + 0.05)/60);
1576 } elsif ($rethash->{'posambiguity'} == 2) {
1577 # the minute decimals are not used
1578 0 $lat_min = substr($lat_min, 0, 2);
1579 0 $lon_min = substr($lon_min, 0, 2);
1580 0 ▶ if ($lat_min =~ / /io || $lon_min =~ / /io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat_min =~ / /io or $lon_min =~ / /io) 0 0 0
Condition0%0/3
$lat_min =~ / /io or $lon_min =~ / /io
l!l&&r!l&&!r
000
MC/DC0%0/2
$lat_min =~ / /io or $lon_min =~ / /io
$lat_min =~ / /io $lon_min =~ / /io
Truth table0%0/3
A or B
A= $lat_min =~ / /ioB= $lon_min =~ / /io
ABresult
1X1
011
000
1581 0 _a_err($rethash, 'loc_amb_inv', 'lat/lon 2');
1582 0 return 0;
1583 }
1584 0 $latitude = $lat_deg + (($lat_min + 0.5)/60);
1585 0 $longitude = $lon_deg + (($lon_min + 0.5)/60);
1586 } elsif ($rethash->{'posambiguity'} == 3) {
1587 # the single minutes are not used
1588 1 $lat_min = substr($lat_min, 0, 1) . '5';
1589 1 $lon_min = substr($lon_min, 0, 1) . '5';
1590 1 ▶ if ($lat_min =~ / /io || $lon_min =~ / /io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat_min =~ / /io or $lon_min =~ / /io) 0 1 1
Condition33%1/3
$lat_min =~ / /io or $lon_min =~ / /io
l!l&&r!l&&!r
001
MC/DC0%0/2
$lat_min =~ / /io or $lon_min =~ / /io
$lat_min =~ / /io $lon_min =~ / /io
Truth table33%1/3
A or B
A= $lat_min =~ / /ioB= $lon_min =~ / /io
ABresult
1X1
011
000
1591 0 _a_err($rethash, 'loc_amb_inv', 'lat/lon 3');
1592 0 return 0;
1593 }
1594 1 $latitude = $lat_deg + ($lat_min/60);
1595 1 $longitude = $lon_deg + ($lon_min/60);
1596 } else {
1597 0 _a_err($rethash, 'loc_amb_inv');
1598 0 return 0;
1599 }
1600  
1601 # Finally apply south/west indicators
1602 22 ▶ if ($issouth == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($issouth == 1) 7 15 22
1603 7 $latitude = 0 - $latitude;
1604 }
1605 22 ▶ if ($iswest == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iswest == 1) 12 10 22
1606 12 $longitude = 0 - $longitude;
1607 }
1608 # Store the locations
1609 22 $rethash->{'latitude'} = $latitude;
1610 22 $rethash->{'longitude'} = $longitude;
1611 # Calculate position resolution based on position ambiguity
1612 # calculated above.
1613 22 $rethash->{'posresolution'} = _get_posresolution(2 - $rethash->{'posambiguity'});
1614  
1615 # Parse possible APRS data extension
1616 # afterwards along with comments
1617  
1618  
1619 22 return 1;
1620 }
1621  
1622 # convert a mic-encoder packet
1623 sub _mice_to_decimal($$$$$) {
1624 11 ▶ my ($packet, $dstcallsign, $srccallsign, $rethash, $options) = @_;
Subroutine _mice_to_decimal: called CC=37 SCAR=41.0
1625  
1626 # We only want the base callsign
1627 11 $dstcallsign =~ s/-\d+$//;
1628  
1629 11 $rethash->{'format'} = 'mice';
1630
1631 # Check the format
1632 11 ▶ if (length($packet) < 8 || length($dstcallsign) != 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $packet < 8 or length $dstcallsign != 6) 0 11 11
Condition33%1/3
length $packet < 8 or length $dstcallsign != 6
l!l&&r!l&&!r
0011
MC/DC0%0/2
length $packet < 8 or length $dstcallsign != 6
length $packet < 8 length $dstcallsign != 6
Truth table33%1/3
A or B
A= length $packet < 8B= length $dstcallsign != 6
ABresult
1X1
011
000
1633 # too short packet to be mic-e
1634 0 _a_err($rethash, 'mice_short');
1635 0 return 0;
1636 }
1637 11 ▶ if (not($dstcallsign =~ /^[0-9A-LP-Z]{3}[0-9LP-Z]{3}$/io)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dstcallsign =~ /^[0-9A-LP-Z]{3}[0-9LP-Z]{3}$/io) 0 11 11
1638 # A-K characters are not used in the last 3 characters
1639 # and MNO are never used
1640 0 _a_err($rethash, 'mice_inv');
1641 0 return 0;
1642 }
1643
1644 # check the information field (longitude, course, speed and
1645 # symbol table and code are checked). Not bullet proof..
1646 11 my $mice_fixed;
1647 11 my $symboltable = substr($packet, 7, 1);
1648 11 ▶ if ($packet !~ /^[\x26-\x7f][\x26-\x61][\x1c-\x7f]{2}[\x1c-\x7d][\x1c-\x7f][\x21-\x7b\x7d][\/\\A-Z0-9]/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($packet =~ m[^[\x26-\x7f][\x26-\x61][\x1c-\x7f]{2}[\x1c-\x7d][\x1c-\x7f][\x21-\x7b\x7d][/\\A-Z0-9]]o) 2 9 11
1649 # If the accept_broken_mice option is given, check for a known
1650 # corruption in the packets and try to fix it - aprsd is
1651 # replacing some valid but non-printable mic-e packet
1652 # characters with spaces, and some other software is replacing
1653 # the multiple spaces with a single space. This regexp
1654 # replaces the single space with two spaces, so that the rest
1655 # of the code can still parse the position data.
1656 2 ▶ if (($options->{'accept_broken_mice'})
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'accept_broken_mice'} and $packet =~ s[^([\x26-\x7f][\x26-\x61][\x1c-\x7f]{2})\x20([\x21-\x7b\x7d][/\\A-Z0-9])(.*)][$1 $2$3]o) { } 1 1 2
Condition66%2/3
$options->{"accept_broken_mice"} and $packet =~ s[^([\x26-\x7f][\x26-\x61][\x1c-\x7f]{2})\x20([\x21-\x7b\x7d][/\\A-Z0-9])(.*)][$1 $2$3]o
!ll&&!rl&&r
101
MC/DC50%1/2
$options->{"accept_broken_mice"} and $packet =~ s[^([\x26-\x7f][\x26-\x61][\x1c-\x7f]{2})\x20([\x21-\x7b\x7d][/\\A-Z0-9])(.*)][$1 $2$3]o
$options->{"accept_broken_mice"} $packet =~ s[^([\x26-\x7f][\x26-\x61][\x1c-\x7f]{2})\x20([\x21-\x7b\x7d][/\\A-Z0-9])(.*)][$1 $2$3]o
Truth table66%2/3
A and B
A= $options->{"accept_broken_mice"}B= $packet =~ s[^([\x26-\x7f][\x26-\x61][\x1c-\x7f]{2})\x20([\x21-\x7b\x7d][/\\A-Z0-9])(.*)][$1 $2$3]o
ABresult
0X0
100
111
1657 && $packet =~ s/^([\x26-\x7f][\x26-\x61][\x1c-\x7f]{2})\x20([\x21-\x7b\x7d][\/\\A-Z0-9])(.*)/$1\x20\x20$2$3/o) {
1658 1 $mice_fixed = 1;
1659 # Now the symbol table identifier is again in the correct spot...
1660 1 $symboltable = substr($packet, 7, 1);
1661 1 ▶ if ($symboltable !~ /^[\/\\A-Z0-9]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($symboltable =~ m[^[/\\A-Z0-9]$]) 0 1 1
1662 0 _a_err($rethash, 'sym_inv_table');
1663 0 return 0;
1664 }
1665 } else {
1666 # Get a more precise error message for invalid symbol table
1667 1 ▶ if ($symboltable !~ /^[\/\\A-Z0-9]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $symboltable =~ m[^[/\\A-Z0-9]$]) { } 1 0 1
1668 1 _a_err($rethash, 'sym_inv_table');
1669 } else {
1670 0 _a_err($rethash, 'mice_inv_info');
1671 }
1672 1 return 0;
1673 }
1674 }
1675  
1676 # First do the destination callsign
1677 # (latitude, message bits, N/S and W/E indicators and long. offset)
1678  
1679 # Translate the characters to get the latitude
1680 10 my $tmplat = $dstcallsign;
1681 10 $tmplat =~ tr/A-JP-YKLZ/0-90-9___/;
1682 # Find out the amount of position ambiguity
1683 10 ▶ if ($tmplat =~ /^(\d+)(_*)$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmplat =~ /^(\d+)(_*)$/io) { } 10 0 10
1684 10 my $amount = 6 - length($1);
1685 10 ▶ if ($amount > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount > 4) 0 10 10
1686 # only minutes and decimal minutes can
1687 # be masked out
1688 0 _a_err($rethash, 'mice_amb_large');
1689 0 return 0;
1690 }
1691 10 $rethash->{'posambiguity'} = $amount;
1692 # Calculate position resolution based on position ambiguity
1693 # calculated above.
1694 10 $rethash->{'posresolution'} = _get_posresolution(2 - $amount);
1695 } else {
1696 # no digits in the beginning, baaad..
1697 # or the ambiguity digits weren't continuous
1698 0 _a_err($rethash, 'mice_amb_inv');
1699 0 return 0;
1700 }
1701  
1702 # convert the latitude to the midvalue if position ambiguity
1703 # is used
1704 10 ▶ if ($rethash->{'posambiguity'} >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{'posambiguity'} >= 4) { } 0 10 10
1705 # the minute is between 0 and 60, so
1706 # the middle point is 30
1707 0 $tmplat =~ s/_/3/;
1708 } else {
1709 10 $tmplat =~ s/_/5/; # the first is changed to digit 5
1710 }
1711 10 $tmplat =~ s/_/0/g; # the rest are changed to digit 0
1712  
1713 # get the degrees
1714 10 my $latitude = substr($tmplat, 0, 2);
1715 # the minutes
1716 10 my $latminutes = substr($tmplat, 2, 2) . '.' . substr($tmplat, 4, 2);
1717 # convert the minutes to decimal degrees and combine
1718 10 $latitude += ($latminutes/60);
1719  
1720 # check the north/south direction and correct the latitude
1721 # if necessary
1722 10 my $nschar = ord(substr($dstcallsign, 3, 1));
1723 10 ▶ if ($nschar <= 0x4c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nschar <= 76) 4 6 10
1724 4 $latitude = 0 - $latitude;
1725 }
1726  
1727 # Latitude is finally complete, so store it
1728 10 $rethash->{'latitude'} = $latitude;
1729  
1730 # Get the message bits. 1 is standard one-bit and
1731 # 2 is custom one-bit. %mice_messagetypes provides
1732 # the mappings to message names
1733 10 my $mbitstring = substr($dstcallsign, 0, 3);
1734 10 $mbitstring =~ tr/0-9/0/;
1735 10 $mbitstring =~ tr/L/0/;
1736 10 $mbitstring =~ tr/P-Z/1/;
1737 10 $mbitstring =~ tr/A-K/2/;
1738 10 $rethash->{'mbits'} = $mbitstring;
1739  
1740 # Decode the longitude, the first three bytes of the
1741 # body after the data type indicator.
1742 # First longitude degrees, remember the longitude offset
1743 10 my $longitude = ord(substr($packet, 0, 1)) - 28;
1744 10 my $longoffsetchar = ord(substr($dstcallsign, 4, 1));
1745 10 ▶ if ($longoffsetchar >= 0x50) {
Branch: 1 branch
BranchTrueFalseTotal
if ($longoffsetchar >= 80) 5 5 10
1746 5 $longitude += 100;
1747 }
1748 10 ▶ if ($longitude >= 180 && $longitude <= 189) {
Branch: 2 branches
BranchTrueFalseTotal
if ($longitude >= 180 and $longitude <= 189) { } 0 10 10
elsif ($longitude >= 190 and $longitude <= 199) { } 0 20 20
Condition33%1/3
$longitude >= 180 and $longitude <= 189
!ll&&!rl&&r
1000
MC/DC0%0/2
$longitude >= 180 and $longitude <= 189
$longitude >= 180 $longitude <= 189
Truth table33%1/3
A and B
A= $longitude >= 180B= $longitude <= 189
ABresult
0X0
100
111
1749 0 $longitude -= 80;
1750 ▶ } elsif ($longitude >= 190 && $longitude <= 199) {
Condition33%1/3
$longitude >= 190 and $longitude <= 199
!ll&&!rl&&r
1000
MC/DC0%0/2
$longitude >= 190 and $longitude <= 199
$longitude >= 190 $longitude <= 199
Truth table33%1/3
A and B
A= $longitude >= 190B= $longitude <= 199
ABresult
0X0
100
111
1751 0 $longitude -= 190;
1752 }
1753  
1754 # Decode the longitude minutes
1755 10 my $longminutes = ord(substr($packet, 1, 1)) - 28;
1756 10 ▶ if ($longminutes >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($longminutes >= 60) 1 9 10
1757 1 $longminutes -= 60;
1758 }
1759 # ... and minute decimals
1760 10 $longminutes = sprintf('%02d.%02d',
1761 $longminutes,
1762 ord(substr($packet, 2, 1)) - 28);
1763 # apply position ambiguity to longitude
1764 10 ▶ if ($rethash->{'posambiguity'} == 4) {
Branch: 5 branches
BranchTrueFalseTotal
if ($rethash->{'posambiguity'} == 4) { } 0 10 10
elsif ($rethash->{'posambiguity'} == 3) { } 0 10 10
elsif ($rethash->{'posambiguity'} == 2) { } 0 10 10
elsif ($rethash->{'posambiguity'} == 1) { } 0 10 10
elsif ($rethash->{'posambiguity'} == 0) { } 10 0 10
1765 # minute is unused -> add 0.5 degrees to longitude
1766 0 $longitude += 0.5;
1767 } elsif ($rethash->{'posambiguity'} == 3) {
1768 0 my $lontmp = substr($longminutes, 0, 1) . '5';
1769 0 $longitude += ($lontmp/60);
1770 } elsif ($rethash->{'posambiguity'} == 2) {
1771 0 my $lontmp = substr($longminutes, 0, 2) . '.5';
1772 0 $longitude += ($lontmp/60);
1773 } elsif ($rethash->{'posambiguity'} == 1) {
1774 0 my $lontmp = substr($longminutes, 0, 4) . '5';
1775 0 $longitude += ($lontmp/60);
1776 } elsif ($rethash->{'posambiguity'} == 0) {
1777 10 $longitude += ($longminutes/60);
1778 } else {
1779 0 _a_err($rethash, 'mice_amb_odd', $rethash->{'posambiguity'});
1780 0 return 0;
1781 }
1782  
1783 # check the longitude E/W sign
1784 10 my $ewchar = ord(substr($dstcallsign, 5, 1));
1785 10 ▶ if ($ewchar >= 0x50) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ewchar >= 80) 3 7 10
1786 3 $longitude = 0 - $longitude;
1787 }
1788  
1789 # Longitude is finally complete, so store it
1790 10 $rethash->{'longitude'} = $longitude;
1791  
1792 # Now onto speed and course.
1793 # If the packet has had a mic-e fix applied, course and speed are likely to be off.
1794 10 ▶ if (!$mice_fixed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mice_fixed) 9 1 10
1795 9 my $speed = (ord(substr($packet, 3, 1)) - 28) * 10;
1796 9 my $coursespeed = ord(substr($packet, 4, 1)) - 28;
1797 9 my $coursespeedtmp = int($coursespeed / 10);
1798 9 $speed += $coursespeedtmp;
1799 9 $coursespeed -= $coursespeedtmp * 10;
1800 9 my $course = 100 * $coursespeed;
1801 9 $course += ord(substr($packet, 5, 1)) - 28;
1802 # do some important adjustements
1803 9 ▶ if ($speed >= 800) {
Branch: 1 branch
BranchTrueFalseTotal
if ($speed >= 800) 5 4 9
1804 5 $speed -= 800;
1805 }
1806 9 ▶ if ($course >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($course >= 400) 9 0 9
1807 9 $course -= 400;
1808 }
1809 # convert speed to km/h and store
1810 9 $rethash->{'speed'} = $speed * $knot_to_kmh;
1811 # also zero course is saved, which means unknown
1812 9 ▶ if ($course >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($course >= 0) 9 0 9
1813 9 $rethash->{'course'} = $course;
1814 }
1815 }
1816  
1817 # save the symbol table and code
1818 10 $rethash->{'symbolcode'} = substr($packet, 6, 1);
1819 10 $rethash->{'symboltable'} = $symboltable;
1820  
1821 # Check for possible altitude and comment data.
1822 # It is base-91 coded and in format 'xxx}' where
1823 # x are the base-91 digits in meters, origin is 10000 meters
1824 # below sea.
1825 10 ▶ if (length($packet) > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $packet > 8) 10 0 10
1826 10 my $rest = substr($packet, 8);
1827
1828 # check for Mic-E Telemetry Data
1829 10 ▶ if ($rest =~ /^'([0-9a-f]{2})([0-9a-f]{2})(.*)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^'([0-9a-f]{2})([0-9a-f]{2})(.*)$/i) 1 9 10
1830 # two hexadecimal values: channels 1 and 3
1831 1 $rest = $3;
1832 1 $rethash->{'telemetry'} = {
1833 'vals' => [ unpack('C*', pack('H*', $1 . '00' . $2)) ]
1834 };
1835 }
1836 10 ▶ if ($rest =~ /^‘([0-9a-f]{10})(.*)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^\342\200\230([0-9a-f]{10})(.*)$/i) 1 9 10
1837 # five channels:
1838 1 $rest = $2;
1839 1 $rethash->{'telemetry'} = {
1840 'vals' => [ unpack('C*', pack('H*', $1)) ]
1841 };
1842 }
1843
1844 # check for altitude
1845 10 ▶ if ($rest =~ /^(.*?)([\x21-\x7b])([\x21-\x7b])([\x21-\x7b])\}(.*)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^(.*?)([\x21-\x7b])([\x21-\x7b])([\x21-\x7b])\}(.*)$/o) 3 7 10
1846 3 $rethash->{'altitude'} = (
1847 (ord($2) - 33) * 91 ** 2 +
1848 (ord($3) - 33) * 91 +
1849 (ord($4) - 33)) - 10000;
1850 3 $rest = $1 . $5;
1851 }
1852  
1853 # Check for new-style base-91 comment telemetry
1854 10 $rest = _comment_telemetry($rethash, $rest);
1855
1856 # Check for !DAO!, take the last occurrence (per recommendation)
1857 10 ▶ if ($rest =~ /^(.*)\!([\x21-\x7b][\x20-\x7b]{2})\!(.*?)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^(.*)\!([\x21-\x7b][\x20-\x7b]{2})\!(.*?)$/o) 2 8 10
1858 2 my $daofound = _dao_parse($2, $srccallsign, $rethash);
1859 2 ▶ if ($daofound == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daofound == 1) 2 0 2
1860 2 $rest = $1 . $3;
1861 }
1862 }
1863
1864 # If anything is left, store it as a comment
1865 # after removing non-printable ASCII
1866 # characters
1867 10 ▶ if (length($rest) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest > 0) 10 0 10
1868 10 $rethash->{'comment'} = _cleanup_comment($rest);
1869 }
1870 }
1871
1872 10 ▶ if ($mice_fixed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mice_fixed) 1 9 10
1873 1 $rethash->{'mice_mangled'} = 1;
1874 #warn "$srccallsign: fixed packet was parsed\n";
1875 }
1876
1877 10 return 1;
1878 }
1879  
1880 # convert a compressed position to decimal degrees
1881 sub _compressed_to_decimal($$$)
1882 {
1883 5 ▶ my ($packet, $srccallsign, $rethash) = @_;
Subroutine _compressed_to_decimal: called CC=11 SCAR=24.4
1884  
1885 # A compressed position is always 13 characters long.
1886 # Make sure we get at least 13 characters and that they are ok.
1887 # Also check the allowed base-91 characters at the same time.
1888 5 ▶ if (not($packet =~ /^[\/\\A-Za-j]{1}[\x21-\x7b]{8}[\x21-\x7b\x7d]{1}[\x20-\x7b]{3}/o)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($packet =~ m[^[/\\A-Za-j]{1}[\x21-\x7b]{8}[\x21-\x7b\x7d]{1}[\x20-\x7b]{3}]o) 0 5 5
1889 0 _a_err($rethash, 'comp_inv');
1890 0 return 0;
1891 }
1892  
1893 5 $rethash->{'format'} = 'compressed';
1894
1895 5 my $symboltable = substr($packet, 0, 1);
1896 5 my $lat1 = ord(substr($packet, 1, 1)) - 33;
1897 5 my $lat2 = ord(substr($packet, 2, 1)) - 33;
1898 5 my $lat3 = ord(substr($packet, 3, 1)) - 33;
1899 5 my $lat4 = ord(substr($packet, 4, 1)) - 33;
1900 5 my $long1 = ord(substr($packet, 5, 1)) - 33;
1901 5 my $long2 = ord(substr($packet, 6, 1)) - 33;
1902 5 my $long3 = ord(substr($packet, 7, 1)) - 33;
1903 5 my $long4 = ord(substr($packet, 8, 1)) - 33;
1904 5 my $symbolcode = substr($packet, 9, 1);
1905 5 my $c1 = ord(substr($packet, 10, 1)) - 33;
1906 5 my $s1 = ord(substr($packet, 11, 1)) - 33;
1907 5 my $comptype = ord(substr($packet, 12, 1)) - 33;
1908  
1909 # save the symbol table and code
1910 5 $rethash->{'symbolcode'} = $symbolcode;
1911 # the symbol table values a..j are really 0..9
1912 5 $symboltable =~ tr/a-j/0-9/;
1913 5 $rethash->{'symboltable'} = $symboltable;
1914  
1915 # calculate latitude and longitude
1916 5 $rethash->{'latitude'} = 90 -
1917 (($lat1 * 91 ** 3 +
1918 $lat2 * 91 ** 2 +
1919 $lat3 * 91 +
1920 $lat4) / 380926);
1921 5 $rethash->{'longitude'} = -180 +
1922 (($long1 * 91 ** 3 +
1923 $long2 * 91 ** 2 +
1924 $long3 * 91 +
1925 $long4) / 190463);
1926 # save best-case position resolution in meters
1927 # 1852 meters * 60 minutes in a degree * 180 degrees
1928 # / 91 ** 4
1929 5 $rethash->{'posresolution'} = 0.291;
1930  
1931 # GPS fix status, only if csT is used
1932 5 ▶ if ($c1 != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c1 != -1) 4 1 5
1933 4 ▶ if (($comptype & 0x20) == 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (($comptype & 32) == 32) { } 3 1 4
1934 3 $rethash->{'gpsfixstatus'} = 1;
1935 } else {
1936 1 $rethash->{'gpsfixstatus'} = 0;
1937 }
1938 }
1939  
1940 # check the compression type, if GPGGA, then
1941 # the cs bytes are altitude. Otherwise try
1942 # to decode it as course and speed. And
1943 # finally as radio range
1944 # if c is space, then csT is not used.
1945 # Also require that s is not a space.
1946 5 ▶ if ($c1 == -1 || $s1 == -1) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c1 == -1 or $s1 == -1) { } 1 4 5
elsif (($comptype & 24) == 16) { } 0 4 4
elsif ($c1 >= 0 and $c1 <= 89) { } 2 2 4
elsif ($c1 == 90) { } 2 0 2
Condition66%2/3
$c1 == -1 or $s1 == -1
l!l&&r!l&&!r
104
MC/DC50%1/2
$c1 == -1 or $s1 == -1
$c1 == -1 $s1 == -1
Truth table66%2/3
A or B
A= $c1 == -1B= $s1 == -1
ABresult
1X1
011
000
1947 # csT not used
1948 } elsif (($comptype & 0x18) == 0x10) {
1949 # cs is altitude
1950 0 my $cs = $c1 * 91 + $s1;
1951 # convert directly to meters
1952 0 $rethash->{'altitude'} = (1.002 ** $cs) * 0.3048;
1953 ▶ } elsif ($c1 >= 0 && $c1 <= 89) {
Condition66%2/3
$c1 >= 0 and $c1 <= 89
!ll&&!rl&&r
022
MC/DC50%1/2
$c1 >= 0 and $c1 <= 89
$c1 >= 0 $c1 <= 89
Truth table66%2/3
A and B
A= $c1 >= 0B= $c1 <= 89
ABresult
0X0
100
111
1954 2 ▶ if ($c1 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c1 == 0) { } 1 1 2
1955 # special case of north, APRS spec
1956 # uses zero for unknown and 360 for north.
1957 # so remember to convert north here.
1958 1 $rethash->{'course'} = 360;
1959 } else {
1960 1 $rethash->{'course'} = $c1 * 4;
1961 }
1962 # convert directly to km/h
1963 2 $rethash->{'speed'} = (1.08 ** $s1 - 1) * $knot_to_kmh;
1964 } elsif ($c1 == 90) {
1965 # convert directly to km
1966 2 $rethash->{'radiorange'} = (2 * 1.08 ** $s1) * $mph_to_kmh;
1967 }
1968  
1969 5 return 1;
1970 }
1971  
1972  
1973 # Parse a possible !DAO! extension (datum and extra
1974 # lat/lon digits). Returns 1 if a valid !DAO! extension was
1975 # detected in the test subject (and stored in $rethash), 0 if not.
1976 # Only the "DAO" should be passed as the candidate parameter,
1977 # not the delimiting exclamation marks.
1978 sub _dao_parse($$$)
1979 {
1980 8 ▶ my ($daocandidate, $srccallsign, $rethash) = @_;
Subroutine _dao_parse: called CC=7 SCAR=22.3
1981  
1982 # datum character is the first character and also
1983 # defines how the rest is interpreted
1984 8 my ($latoff, $lonoff) = undef;
1985 8 ▶ if ($daocandidate =~ /^([A-Z])(\d)(\d)$/o) {
Branch: 3 branches
BranchTrueFalseTotal
if ($daocandidate =~ /^([A-Z])(\d)(\d)$/o) { } 5 3 8
elsif ($daocandidate =~ /^([a-z])([\x21-\x7b])([\x21-\x7b])$/o) { } 3 0 3
elsif ($daocandidate =~ /^([\x21-\x7b]) $/o) { } 0 0 0
1986 # human readable (datum byte A...Z)
1987 5 $rethash->{'daodatumbyte'} = $1;
1988 5 $rethash->{'posresolution'} = _get_posresolution(3);
1989 5 $latoff = $2 * 0.001 / 60;
1990 5 $lonoff = $3 * 0.001 / 60;
1991  
1992 } elsif ($daocandidate =~ /^([a-z])([\x21-\x7b])([\x21-\x7b])$/o) {
1993 # base-91 (datum byte a...z)
1994 # store the datum in upper case, still
1995 3 $rethash->{'daodatumbyte'} = uc($1);
1996 # close enough.. not exact:
1997 3 $rethash->{'posresolution'} = _get_posresolution(4);
1998 # do proper scaling of base-91 values
1999 3 $latoff = (ord($2) - 33) / 91 * 0.01 / 60;
2000 3 $lonoff = (ord($3) - 33) / 91 * 0.01 / 60;
2001  
2002 } elsif ($daocandidate =~ /^([\x21-\x7b]) $/o) {
2003 # only datum information, no lat/lon digits
2004 0 my $daodatumbyte = $1;
2005 0 ▶ if ($daodatumbyte =~ /^[a-z]$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daodatumbyte =~ /^[a-z]$/o) 0 0 0
2006 0 $daodatumbyte = uc($daodatumbyte);
2007 }
2008 0 $rethash->{'daodatumbyte'} = $daodatumbyte;
2009 0 return 1;
2010  
2011 } else {
2012 0 return 0;
2013 }
2014  
2015 # check N/S and E/W
2016 8 ▶ if ($rethash->{'latitude'} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{'latitude'} < 0) { } 0 8 8
2017 0 $rethash->{'latitude'} -= $latoff;
2018 } else {
2019 8 $rethash->{'latitude'} += $latoff;
2020 }
2021 8 ▶ if ($rethash->{'longitude'} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{'longitude'} < 0) { } 6 2 8
2022 6 $rethash->{'longitude'} -= $lonoff;
2023 } else {
2024 2 $rethash->{'longitude'} += $lonoff;
2025 }
2026 8 return 1;
2027 }
2028  
2029 =over
2030  
2031 =item check_ax25_call($callsign)
2032  
2033 Check the callsign for a valid AX.25 callsign format and
2034 return cleaned up (OH2XYZ-0) callsign or undef if the callsign
2035 is not a valid AX.25 address.
2036  
2037 Please note that it's very common to use invalid callsigns on the APRS-IS.
2038  
2039 =back
2040  
2041 =cut
2042  
2043 sub check_ax25_call($) {
2044 303 ▶ if ($_[0] =~ /^([A-Z0-9]{1,6})(-\d{1,2}|)$/o) {
Subroutine check_ax25_call: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^([A-Z0-9]{1,6})(-\d{1,2}|)$/o) 301 2 303
2045 301 ▶ if (length($2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $2 == 0) { } 289 12 301
2046 289 return $1;
2047 } else {
2048 # convert SSID to positive and numeric
2049 12 my $ssid = 0 - $2;
2050 12 ▶ if ($ssid < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssid < 16) 11 1 12
2051 # 15 is maximum in AX.25
2052 11 return $1 . '-' . $ssid;
2053 }
2054 }
2055 }
2056  
2057 # no successfull return yet, so error
2058 3 return undef;
2059 }
2060  
2061 # _dx_parse($sourcecall, $info, $rethash)
2062 #
2063 # Parses the body of a DX spot packet. Returns the following
2064 # hash elements: dxsource (source of the info), dxfreq (frequency),
2065 # dxcall (DX callsign) and dxinfo (info string).
2066 #
2067  
2068 sub _dx_parse($$$)
2069 {
2070 0 ▶ my ($sourcecall, $info, $rh) = @_;
Subroutine _dx_parse: not called CC=5 SCAR=34.0
2071
2072 0 ▶ if (!defined check_ax25_call($sourcecall)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &check_ax25_call($sourcecall)) 0 0 0
2073 0 _a_err($rh, 'dx_inv_src', $sourcecall);
2074 0 return 0;
2075 }
2076 0 $rh->{'dxsource'} = $sourcecall;
2077
2078 0 $info =~ s/^\s*(.*?)\s*$/$1/; # strip whitespace
2079 0 ▶ if ($info =~ s/\s*(\d{3,4}Z)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info =~ s/\s*(\d{3,4}Z)//) 0 0 0
2080 0 $rh->{'dxtime'} = $1;
2081 }
2082 0 ▶ _a_err($rh, 'dx_inv_freq') if ($info !~ s/^(\d+\.\d+)\s*//);
Branch: 1 branch
BranchTrueFalseTotal
unless $info =~ s/^(\d+\.\d+)\s*// 0 0 0
2083 0 $rh->{'dxfreq'} = $1;
2084 0 ▶ _a_err($rh, 'dx_no_dx') if ($info !~ s/^([a-zA-Z0-9-\/]+)\s*//);
Branch: 1 branch
BranchTrueFalseTotal
unless $info =~ s[^([a-zA-Z0-9-/]+)\s*][] 0 0 0
2085 0 $rh->{'dxcall'} = $1;
2086
2087 0 $info =~ s/\s+/ /g;
2088 0 $rh->{'dxinfo'} = $info;
2089
2090 0 return 1;
2091 }
2092  
2093 # _wx_parse($s, $rethash)
2094 #
2095 # Parses a normal uncompressed weather report packet.
2096 #
2097  
2098 sub _fahrenheit_to_celsius($)
2099 {
2100 9 ▶ return ($_[0] - 32) / 1.8;
Subroutine _fahrenheit_to_celsius: called CC=1 SCAR=0.0
2101 }
2102  
2103 sub _wx_parse($$)
2104 {
2105 8 ▶ my ($s, $rh) = @_;
Subroutine _wx_parse: called CC=30 SCAR=38.7
2106
2107 #my $initial = $s;
2108
2109 # 257/007g013t055r000P000p000h56b10160v31
2110 # 045/000t064r000p000h35b10203.open2300v1.10
2111 # 175/007g007p...P000r000t062h32b10224wRSW
2112 8 my %w;
2113 8 my ($wind_dir, $wind_speed, $temp, $wind_gust) = ('', '', '', '');
2114 8 ▶ if ($s =~ s/^_{0,1}([\d \.\-]{3})\/([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)//
Branch: 4 branches
BranchTrueFalseTotal
if ($s =~ s[^_{0,1}([\d \.\-]{3})/([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)][] or $s =~ s/^_{0,1}c([\d \.\-]{3})s([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)//) { } 4 4 8
elsif ($s =~ s[^_{0,1}([\d \.\-]{3})/([\d \.]{3})t(-{0,1}[\d \.]+)][]) { } 0 4 4
elsif ($s =~ s[^_{0,1}([\d \.\-]{3})/([\d \.]{3})g([\d \.]+)][]) { } 0 4 4
elsif ($s =~ s/^g(\d+)t(-{0,1}[\d \.]+)//) { } 1 3 4
Condition100%3/3
$s =~ s[^_{0,1}([\d \.\-]{3})/([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)][] or $s =~ s/^_{0,1}c([\d \.\-]{3})s([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)//
l!l&&r!l&&!r
314
MC/DC100%2/2
$s =~ s[^_{0,1}([\d \.\-]{3})/([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)][] or $s =~ s/^_{0,1}c([\d \.\-]{3})s([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)//
$s =~ s[^_{0,1}([\d \.\-]{3})/([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)][] $s =~ s/^_{0,1}c([\d \.\-]{3})s([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)//
Truth table100%3/3
A or B
A= $s =~ s[^_{0,1}([\d \.\-]{3})/([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)][]B= $s =~ s/^_{0,1}c([\d \.\-]{3})s([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)//
ABresult
1X1
011
000
2115 || $s =~ s/^_{0,1}c([\d \.\-]{3})s([\d \.]{3})g([\d \.]+)t(-{0,1}[\d \.]+)//) {
2116 #warn "wind $1 / $2 gust $3 temp $4\n";
2117 4 ($wind_dir, $wind_speed, $wind_gust, $temp) = ($1, $2, $3, $4);
2118 } elsif ($s =~ s/^_{0,1}([\d \.\-]{3})\/([\d \.]{3})t(-{0,1}[\d \.]+)//) {
2119 #warn "$initial\nwind $1 / $2 temp $3\n";
2120 0 ($wind_dir, $wind_speed, $temp) = ($1, $2, $3);
2121 } elsif ($s =~ s/^_{0,1}([\d \.\-]{3})\/([\d \.]{3})g([\d \.]+)//) {
2122 #warn "$initial\nwind $1 / $2 gust $3\n";
2123 0 ($wind_dir, $wind_speed, $wind_gust) = ($1, $2, $3);
2124 } elsif ($s =~ s/^g(\d+)t(-{0,1}[\d \.]+)//) {
2125 # g000t054r000p010P010h65b10073WS 2300 {UIV32N}
2126 1 ($wind_gust, $temp) = ($1, $2);
2127 } else {
2128 #warn "wx_parse: no initial match: $s\n";
2129 3 return 0;
2130 }
2131
2132 5 ▶ if (!defined $temp && $s =~ s/t(-{0,1}\d{1,3})//) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $temp and $s =~ s/t(-{0,1}\d{1,3})//) 0 5 5
Condition33%1/3
not defined $temp and $s =~ s/t(-{0,1}\d{1,3})//
!ll&&!rl&&r
500
MC/DC0%0/2
not defined $temp and $s =~ s/t(-{0,1}\d{1,3})//
not defined $temp $s =~ s/t(-{0,1}\d{1,3})//
Truth table33%1/3
not(A) and B
A= not defined $tempB= $s =~ s/t(-{0,1}\d{1,3})//
ABresult
0X0
100
111
2133 0 $temp = $1;
2134 }
2135
2136 5 ▶ $w{'wind_gust'} = sprintf('%.1f', $wind_gust * $mph_to_ms) if ($wind_gust =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $wind_gust =~ /^\d+$/ 5 0 5
2137 5 ▶ $w{'wind_direction'} = sprintf('%.0f', $wind_dir) if ($wind_dir =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $wind_dir =~ /^\d+$/ 4 1 5
2138 5 ▶ $w{'wind_speed'} = sprintf('%.1f', $wind_speed * $mph_to_ms) if ($wind_speed =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $wind_speed =~ /^\d+$/ 4 1 5
2139 5 ▶ $w{'temp'} = sprintf('%.1f', _fahrenheit_to_celsius($temp)) if ($temp =~ /^-{0,1}\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $temp =~ /^-{0,1}\d+$/ 5 0 5
2140
2141 5 ▶ if ($s =~ s/r(\d{1,3})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/r(\d{1,3})//) 5 0 5
2142 5 $w{'rain_1h'} = sprintf('%.1f', $1*$hinch_to_mm); # during last 1h
2143 }
2144 5 ▶ if ($s =~ s/p(\d{1,3})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/p(\d{1,3})//) 5 0 5
2145 5 $w{'rain_24h'} = sprintf('%.1f', $1*$hinch_to_mm); # during last 24h
2146 }
2147 5 ▶ if ($s =~ s/P(\d{1,3})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/P(\d{1,3})//) 5 0 5
2148 5 $w{'rain_midnight'} = sprintf('%.1f', $1*$hinch_to_mm); # since midnight
2149 }
2150
2151 5 ▶ if ($s =~ s/h(\d{1,3})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/h(\d{1,3})//) 5 0 5
2152 5 $w{'humidity'} = sprintf('%.0f', $1); # percentage
2153 5 ▶ $w{'humidity'} = 100 if ($w{'humidity'} eq 0);
Branch: 1 branch
BranchTrueFalseTotal
if $w{"humidity"} eq 0 1 4 5
2154 5 ▶ undef $w{'humidity'} if ($w{'humidity'} > 100 || $w{'humidity'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $w{"humidity"} > 100 or $w{"humidity"} < 1 0 5 5
Condition33%1/3
$w{"humidity"} > 100 or $w{"humidity"} < 1
l!l&&r!l&&!r
005
MC/DC0%0/2
$w{"humidity"} > 100 or $w{"humidity"} < 1
$w{"humidity"} > 100 $w{"humidity"} < 1
Truth table33%1/3
A or B
A= $w{"humidity"} > 100B= $w{"humidity"} < 1
ABresult
1X1
011
000
2155 }
2156
2157 5 ▶ if ($s =~ s/b(\d{4,5})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/b(\d{4,5})//) 5 0 5
2158 5 $w{'pressure'} = sprintf('%.1f', $1/10); # results in millibars
2159 }
2160
2161 5 ▶ if ($s =~ s/([lL])(\d{1,3})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/([lL])(\d{1,3})//) 1 4 5
2162 1 $w{'luminosity'} = sprintf('%.0f', $2); # watts / m2
2163 1 ▶ $w{'luminosity'} += 1000 if ($1 eq 'l');
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "l" 0 1 1
2164 }
2165
2166 5 ▶ if ($s =~ s/v([\-\+]{0,1}\d+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/v([\-\+]{0,1}\d+)//) 0 5 5
2167 # what ?
2168 }
2169
2170 5 ▶ if ($s =~ s/s(\d{1,3})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/s(\d{1,3})//) 1 4 5
2171 # snowfall
2172 1 $w{'snow_24h'} = sprintf('%.1f', $1*$hinch_to_mm);
2173 }
2174
2175 5 ▶ if ($s =~ s/#(\d+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/#(\d+)//) 0 5 5
2176 # raw rain counter
2177 }
2178
2179 5 $s =~ s/^([rPphblLs#][\. ]{1,5})+//;
2180
2181 5 $s =~ s/^\s+//;
2182 5 $s =~ s/\s+/ /;
2183 5 ▶ if ($s =~ /^[a-zA-Z0-9\-_]{3,5}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^[a-zA-Z0-9\-_]{3,5}$/) { } 1 4 5
2184 1 ▶ $w{'soft'} = substr($s, 0, 16) if ($s ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $s ne "" 1 0 1
2185 } else {
2186 4 $rh->{'comment'} = _cleanup_comment($s);
2187 }
2188
2189 5 ▶ if (defined $w{'temp'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"}) 5 0 5
Condition16%1/6
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"}
l!l&&r!l&&!r
500
defined $w{"wind_speed"} and defined $w{"wind_direction"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"}
defined $w{"temp"} defined $w{"wind_speed"} defined $w{"wind_direction"}
Truth table25%1/4
A or B and C
A= defined $w{"temp"}B= defined $w{"wind_speed"}C= defined $w{"wind_direction"}
ABCresult
1XX1
0111
00X0
0100
2190 || (defined $w{'wind_speed'} && defined $w{'wind_direction'})
2191 ) {
2192 #warn "ok: $initial\n$s\n";
2193 5 $rh->{'wx'} = \%w;
2194 5 return 1;
2195 }
2196
2197 0 return 0;
2198 }
2199  
2200 # _wx_parse_peet_packet($s, $sourcecall, $rethash)
2201 #
2202 # Parses a Peet bros Ultimeter weather packet ($ULTW header).
2203 #
2204  
2205 sub _wx_parse_peet_packet($$$)
2206 {
2207 2 ▶ my ($s, $sourcecall, $rh) = @_;
Subroutine _wx_parse_peet_packet: called CC=21 SCAR=35.6
2208
2209 #warn "\$ULTW: $s\n";
2210 # 0000000001FF000427C70002CCD30001026E003A050F00040000
2211 2 my %w;
2212 my $t;
2213 2 my @vals;
2214 2 ▶ while ($s =~ s/^([0-9a-f]{4}|----)//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/^([0-9a-f]{4}|----)//i) 24 2 26
2215 24 ▶ if ($1 eq '----') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '----') { } 0 24 24
2216 0 push @vals, undef;
2217 } else {
2218 # Signed 16-bit integers in network (big-endian) order
2219 # encoded in hex, high nybble first.
2220 # Perl 5.10 unpack supports n! for signed ints, 5.8
2221 # requires tricks like this:
2222 24 my $v = unpack('n', pack('H*', $1));
2223
2224 24 ▶ push @vals, ($v < 32768) ? $v : $v - 65536;
Branch: 1 branch
BranchTrueFalseTotal
$v < 32768 ? : 21 3 24
2225 }
2226 }
2227 2 ▶ return 0 if (!@vals);
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 2 2
2228
2229 2 $t = shift @vals;
2230 2 ▶ $w{'wind_gust'} = sprintf('%.1f', $t * $kmh_to_ms / 10) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 2 0 2
2231 2 $t = shift @vals;
2232 2 ▶ $w{'wind_direction'} = sprintf('%.0f', ($t& 0xff) * 1.41176) if (defined $t); # 1/255 => 1/360
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 2 0 2
2233 2 $t = shift @vals;
2234 2 ▶ $w{'temp'} = sprintf('%.1f', _fahrenheit_to_celsius($t / 10)) if (defined $t); # 1/255 => 1/360
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 2 0 2
2235 2 $t = shift @vals;
2236 2 ▶ $w{'rain_midnight'} = sprintf('%.1f', $t * $hinch_to_mm) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 2 0 2
2237 2 $t = shift @vals;
2238 2 ▶ $w{'pressure'} = sprintf('%.1f', $t / 10) if (defined $t && $t >= 10);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t and $t >= 10 2 0 2
Condition33%1/3
defined $t and $t >= 10
!ll&&!rl&&r
002
MC/DC0%0/2
defined $t and $t >= 10
defined $t $t >= 10
Truth table33%1/3
A and B
A= defined $tB= $t >= 10
ABresult
0X0
100
111
2239 2 shift @vals; # Barometer Delta
2240 2 shift @vals; # Barometer Corr. Factor (LSW)
2241 2 shift @vals; # Barometer Corr. Factor (MSW)
2242 2 $t = shift @vals;
2243 2 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 2 0 2
2244 2 $w{'humidity'} = sprintf('%.0f', $t / 10); # percentage
2245 2 ▶ delete $w{'humidity'} if ($w{'humidity'} > 100 || $w{'humidity'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $w{"humidity"} > 100 or $w{"humidity"} < 1 0 2 2
Condition33%1/3
$w{"humidity"} > 100 or $w{"humidity"} < 1
l!l&&r!l&&!r
002
MC/DC0%0/2
$w{"humidity"} > 100 or $w{"humidity"} < 1
$w{"humidity"} > 100 $w{"humidity"} < 1
Truth table33%1/3
A or B
A= $w{"humidity"} > 100B= $w{"humidity"} < 1
ABresult
1X1
011
000
2246 }
2247 2 shift @vals; # date
2248 2 shift @vals; # time
2249 2 $t = shift @vals;
2250 2 ▶ $w{'rain_midnight'} = sprintf('%.1f', $t * $hinch_to_mm) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 1 2
2251 2 $t = shift @vals;
2252 2 ▶ $w{'wind_speed'} = sprintf('%.1f', $t * $kmh_to_ms / 10) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 1 2
2253
2254 2 ▶ if (defined $w{'temp'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"} or defined $w{"humidity"}) 0 0 0
Condition25%3/12
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"} or defined $w{"humidity"}
l!l&&r!l&&!r
200
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"}
l!l&&r!l&&!r
200
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"}
l!l&&r!l&&!r
200
defined $w{"wind_speed"} and defined $w{"wind_direction"}
!ll&&!rl&&r
000
MC/DC0%0/5
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"} or defined $w{"humidity"}
defined $w{"temp"} defined $w{"wind_speed"} defined $w{"wind_direction"} defined $w{"pressure"} defined $w{"humidity"}
Truth table12%1/8
A or B and C or D or E
A= defined $w{"temp"}B= defined $w{"wind_speed"}C= defined $w{"wind_direction"}D= defined $w{"pressure"}E= defined $w{"humidity"}
ABCDEresult
1XXXX1
011XX1
00X1X1
0101X1
00X011
010011
00X000
010000
2255 || (defined $w{'wind_speed'} && defined $w{'wind_direction'})
2256 || (defined $w{'pressure'})
2257 || (defined $w{'humidity'})
2258 ) {
2259 2 $rh->{'wx'} = \%w;
2260 2 return 1;
2261 }
2262
2263 0 return 0;
2264 }
2265  
2266 # _wx_parse_peet_logging($s, $sourcecall, $rethash)
2267 #
2268 # Parses a Peet bros Ultimeter weather logging frame (!! header).
2269 #
2270  
2271 sub _wx_parse_peet_logging($$$)
2272 {
2273 1 ▶ my ($s, $sourcecall, $rh) = @_;
Subroutine _wx_parse_peet_logging: called CC=29 SCAR=45.8
2274
2275 #warn "\!!: $s\n";
2276 # 0000000001FF000427C70002CCD30001026E003A050F00040000
2277 1 my %w;
2278 my $t;
2279 1 my @vals;
2280 1 ▶ while ($s =~ s/^([0-9a-f]{4}|----)//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/^([0-9a-f]{4}|----)//i) 12 1 13
2281 12 ▶ if ($1 eq '----') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '----') { } 2 10 12
2282 2 push @vals, undef;
2283 } else {
2284 # Signed 16-bit integers in network (big-endian) order
2285 # encoded in hex, high nybble first.
2286 # Perl 5.10 unpack supports n! for signed ints, 5.8
2287 # requires tricks like this:
2288 10 my $v = unpack('n', pack('H*', $1));
2289
2290 10 ▶ push @vals, ($v < 32768) ? $v : $v - 65536;
Branch: 1 branch
BranchTrueFalseTotal
$v < 32768 ? : 10 0 10
2291 }
2292 }
2293 1 ▶ return 0 if (!@vals);
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 1 1
2294
2295 1 $t = shift @vals; # instant wind speed
2296 1 ▶ $w{'wind_speed'} = sprintf('%.1f', $t * $kmh_to_ms / 10) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 0 1
2297 1 $t = shift @vals;
2298 1 ▶ $w{'wind_direction'} = sprintf('%.0f', ($t& 0xff) * 1.41176) if (defined $t); # 1/255 => 1/360
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 0 1
2299 1 $t = shift @vals;
2300 1 ▶ $w{'temp'} = sprintf('%.1f', _fahrenheit_to_celsius($t / 10)) if (defined $t); # 1/255 => 1/360
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 0 1
2301 1 $t = shift @vals;
2302 1 ▶ $w{'rain_midnight'} = sprintf('%.1f', $t * $hinch_to_mm) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 0 1
2303 1 $t = shift @vals;
2304 1 ▶ $w{'pressure'} = sprintf('%.1f', $t / 10) if (defined $t && $t >= 10);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t and $t >= 10 1 0 1
Condition33%1/3
defined $t and $t >= 10
!ll&&!rl&&r
001
MC/DC0%0/2
defined $t and $t >= 10
defined $t $t >= 10
Truth table33%1/3
A and B
A= defined $tB= $t >= 10
ABresult
0X0
100
111
2305 1 $t = shift @vals;
2306 1 ▶ $w{'temp_in'} = sprintf('%.1f', _fahrenheit_to_celsius($t / 10)) if (defined $t); # 1/255 => 1/360
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 0 1
2307 1 $t = shift @vals;
2308 1 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 0 1 1
2309 0 $w{'humidity'} = sprintf('%.0f', $t / 10); # percentage
2310 0 ▶ delete $w{'humidity'} if ($w{'humidity'} > 100 || $w{'humidity'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $w{"humidity"} > 100 or $w{"humidity"} < 1 0 0 0
Condition0%0/3
$w{"humidity"} > 100 or $w{"humidity"} < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$w{"humidity"} > 100 or $w{"humidity"} < 1
$w{"humidity"} > 100 $w{"humidity"} < 1
Truth table0%0/3
A or B
A= $w{"humidity"} > 100B= $w{"humidity"} < 1
ABresult
1X1
011
000
2311 }
2312 1 $t = shift @vals;
2313 1 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 0 1 1
2314 0 $w{'humidity_in'} = sprintf('%.0f', $t / 10); # percentage
2315 0 ▶ delete $w{'humidity_in'} if ($w{'humidity_in'} > 100 || $w{'humidity_in'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $w{"humidity_in"} > 100 or $w{"humidity_in"} < 1 0 0 0
Condition0%0/3
$w{"humidity_in"} > 100 or $w{"humidity_in"} < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$w{"humidity_in"} > 100 or $w{"humidity_in"} < 1
$w{"humidity_in"} > 100 $w{"humidity_in"} < 1
Truth table0%0/3
A or B
A= $w{"humidity_in"} > 100B= $w{"humidity_in"} < 1
ABresult
1X1
011
000
2316 }
2317 1 shift @vals; # date
2318 1 shift @vals; # time
2319 1 $t = shift @vals;
2320 1 ▶ $w{'rain_midnight'} = sprintf('%.1f', $t * $hinch_to_mm) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 0 1
2321 1 $t = shift @vals; # avg wind speed
2322 1 ▶ $w{'wind_speed'} = sprintf('%.1f', $t * $kmh_to_ms / 10) if (defined $t);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 1 0 1
2323
2324 # if inside temperature exists but no outside, use inside
2325 1 ▶ $w{'temp'} = $w{'temp_in'} if (defined $w{'temp_in'} && !defined $w{'temp'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $w{"temp_in"} and not defined $w{"temp"} 0 1 1
Condition33%1/3
defined $w{"temp_in"} and not defined $w{"temp"}
!ll&&!rl&&r
010
MC/DC0%0/2
defined $w{"temp_in"} and not defined $w{"temp"}
defined $w{"temp_in"} not defined $w{"temp"}
Truth table33%1/3
A and not(B)
A= defined $w{"temp_in"}B= not defined $w{"temp"}
ABresult
0X0
100
111
2326 1 ▶ $w{'humidity'} = $w{'humidity_in'} if (defined $w{'humidity_in'} && !defined $w{'humidity'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $w{"humidity_in"} and not defined $w{"humidity"} 0 1 1
Condition33%1/3
defined $w{"humidity_in"} and not defined $w{"humidity"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $w{"humidity_in"} and not defined $w{"humidity"}
defined $w{"humidity_in"} not defined $w{"humidity"}
Truth table33%1/3
A and not(B)
A= defined $w{"humidity_in"}B= not defined $w{"humidity"}
ABresult
0X0
100
111
2327
2328 1 ▶ if (defined $w{'temp'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"} or defined $w{"humidity"}) 0 0 0
Condition25%3/12
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"} or defined $w{"humidity"}
l!l&&r!l&&!r
100
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"}
l!l&&r!l&&!r
100
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"}
l!l&&r!l&&!r
100
defined $w{"wind_speed"} and defined $w{"wind_direction"}
!ll&&!rl&&r
000
MC/DC0%0/5
defined $w{"temp"} or defined $w{"wind_speed"} and defined $w{"wind_direction"} or defined $w{"pressure"} or defined $w{"humidity"}
defined $w{"temp"} defined $w{"wind_speed"} defined $w{"wind_direction"} defined $w{"pressure"} defined $w{"humidity"}
Truth table12%1/8
A or B and C or D or E
A= defined $w{"temp"}B= defined $w{"wind_speed"}C= defined $w{"wind_direction"}D= defined $w{"pressure"}E= defined $w{"humidity"}
ABCDEresult
1XXXX1
011XX1
00X1X1
0101X1
00X011
010011
00X000
010000
2329 || (defined $w{'wind_speed'} && defined $w{'wind_direction'})
2330 || (defined $w{'pressure'})
2331 || (defined $w{'humidity'})
2332 ) {
2333 1 $rh->{'wx'} = \%w;
2334 1 return 1;
2335 }
2336
2337 0 return 0;
2338 }
2339  
2340 # _telemetry_parse($s, $rethash)
2341 #
2342 # Parses a telemetry packet.
2343 #
2344  
2345 sub _telemetry_parse($$)
2346 {
2347 1 ▶ my ($s, $rh) = @_;
Subroutine _telemetry_parse: called CC=7 SCAR=21.8
2348
2349 1 my $initial = $s;
2350
2351 1 my ($seq, $v1, $v2, $v3, $v4, $v5, $bits);
2352 1 my %t;
2353 1 ▶ if ($s =~ s/^(\d+),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),([01]{0,8})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/^(\d+),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),(-|)(\d{1,6}|\d+\.\d+|\.\d+|),([01]{0,8})//) { } 1 0 1
2354 1 $t{'seq'} = $1;
2355 1 my @vals = ( "$2$3", "$4$5", "$6$7", "$8$9", "$10$11" );
2356 1 ▶ for (my $i = 0; $i <= $#vals; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#vals) 5 1 6
2357 5 ▶ $vals[$i] = $vals[$i] eq '' ? 0 : sprintf('%.2f', $vals[$i]);
Branch: 1 branch
BranchTrueFalseTotal
$vals[$i] eq '' ? : 0 5 5
2358 5 ▶ if ($vals[$i] >= 999999 || $vals[$i] <= -999999) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vals[$i] >= 999999 or $vals[$i] <= -999999) 0 5 5
Condition33%1/3
$vals[$i] >= 999999 or $vals[$i] <= -999999
l!l&&r!l&&!r
005
MC/DC0%0/2
$vals[$i] >= 999999 or $vals[$i] <= -999999
$vals[$i] >= 999999 $vals[$i] <= -999999
Truth table33%1/3
A or B
A= $vals[$i] >= 999999B= $vals[$i] <= -999999
ABresult
1X1
011
000
2359 0 _a_err($rh, 'tlm_large');
2360 0 return 0;
2361 }
2362 }
2363 1 $t{'vals'} = \@vals;
2364 1 $t{'bits'} = $12;
2365 # expand bits to 8 bits if some are missing
2366 1 ▶ if ((my $l = length($t{'bits'})) < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $l = length $t{"bits"}) < 8) 0 1 1
2367 0 $t{'bits'} .= '0' x (8-$l);
2368 }
2369 } else {
2370 # todo: return an error code
2371 0 _a_err($rh, 'tlm_inv');
2372 0 return 0;
2373 }
2374
2375 1 $rh->{'telemetry'} = \%t;
2376 #warn 'ok: ' . Dumper(\%t);
2377 1 return 1;
2378 }
2379  
2380 =over
2381  
2382 =item parseaprs($packet, $hashref, %options)
2383  
2384 Parse an APRS packet given as a string, e.g.
2385 "OH2XYZ>APRS,RELAY*,WIDE:!2345.56N/12345.67E-PHG0123 hi there"
2386 Second parameter has to be a reference to a hash. That hash will
2387 be filled with as much data as possible based on the packet
2388 given as parameter.
2389  
2390 Returns 1 if the decoding was successfull,
2391 returns 0 if not. In case zero is returned, the contents of
2392 the parameter hash should be discarded, except for the error cause
2393 as reported via hash elements resultcode and resultmsg.
2394  
2395  
2396 The third parameter is an optional hash containing any of the following
2397 options:
2398  
2399 B<isax25> - the packet should be examined in a form
2400 that can exist on an AX.25 network (1) or whether the frame is
2401 from the Internet (0 - default).
2402  
2403 B<accept_broken_mice> - if the packet contains corrupted
2404 mic-e fields, but some of the data is still recovable, decode
2405 the packet instead of reporting an error. At least aprsd produces
2406 these packets. 1: try to decode, 0: report an error (default).
2407 Packets which have been successfully demangled will contain the
2408 B<mice_mangled> flag.
2409  
2410 B<raw_timestamp> - Timestamps within the packets are not decoded
2411 to an UNIX timestamp, but are returned as raw strings.
2412  
2413 Example:
2414  
2415 my %hash;
2416  
2417 my $ret = parseaprs("OH2XYZ>APRS,RELAY*,WIDE:!2345.56N/12345.67E-PHG0123 hi",
2418 \%hash, 'isax25' => 0, 'accept_broken_mice' => 0);
2419  
2420 =back
2421  
2422 =cut
2423  
2424 sub parseaprs($$;%) {
2425 298 ▶ my($packet, $rethash, %options) = @_;
Subroutine parseaprs: called CC=80 SCAR=57.7
Pod documented
2426 298 ▶ my $isax25 = ($options{'isax25'}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options{'isax25'} ? : 0 298 298
2427
2428 298 ▶ if (!defined $packet) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packet) 0 298 298
2429 0 _a_err($rethash, 'packet_no');
2430 0 return 0;
2431 }
2432 298 ▶ if (length($packet) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $packet < 1) 0 298 298
2433 0 _a_err($rethash, 'packet_short');
2434 0 return 0;
2435 }
2436
2437 # Separate the header and packet body on the first
2438 # colon.
2439 298 my ($header, $body) = split(/:/, $packet, 2);
2440  
2441 # If no body, skip
2442 298 ▶ if (!defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 298 298
2443 0 _a_err($rethash, 'packet_nobody');
2444 0 return 0;
2445 }
2446  
2447 # Save all the parts of the packet
2448 298 $rethash->{'origpacket'} = $packet;
2449 298 $rethash->{'header'} = $header;
2450 298 $rethash->{'body'} = $body;
2451  
2452 # Source callsign, put the rest in $rest
2453 298 my($srccallsign, $rest);
2454 298 ▶ if ($header =~ /^([A-Z0-9-]{1,9})>(.*)$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^([A-Z0-9-]{1,9})>(.*)$/io) { } 297 1 298
2455 297 $rest = $2;
2456 297 ▶ if ($isax25 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isax25 == 0) { } 297 0 297
2457 297 $srccallsign = $1;
2458 } else {
2459 0 $srccallsign = check_ax25_call(uc($1));
2460 0 ▶ if (not(defined($srccallsign))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $srccallsign) 0 0 0
2461 0 _a_err($rethash, 'srccall_noax25');
2462 0 return 0;
2463 }
2464 }
2465 } else {
2466 # can't be a valid amateur radio callsign, even
2467 # in the extended sense of APRS-IS callsigns
2468 1 _a_err($rethash, 'srccall_badchars');
2469 1 return 0;
2470 }
2471 297 $rethash->{'srccallsign'} = $srccallsign;
2472  
2473 # Get the destination callsign and digipeaters.
2474 # Only TNC-2 format is supported, AEA (with digipeaters) is not.
2475 297 my @pathcomponents = split(/,/, $rest);
2476 # More than 9 (dst callsign + 8 digipeaters) path components
2477 # from AX.25 or less than 1 from anywhere is invalid.
2478 297 ▶ if ($isax25 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isax25 == 1) 0 297 297
2479 0 ▶ if (scalar(@pathcomponents) > 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pathcomponents > 9) 0 0 0
2480 # too many fields to be from AX.25
2481 0 _a_err($rethash, 'dstpath_toomany');
2482 0 return 0;
2483 }
2484 }
2485 297 ▶ if (scalar(@pathcomponents) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pathcomponents < 1) 0 297 297
2486 # no destination field
2487 0 _a_err($rethash, 'dstcall_none');
2488 0 return 0;
2489 }
2490
2491 # Destination callsign. We are strict here, there
2492 # should be no need to use a non-AX.25 compatible
2493 # destination callsigns in the APRS-IS.
2494 297 my $dstcallsign = check_ax25_call(shift @pathcomponents);
2495 297 ▶ if (!defined $dstcallsign) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dstcallsign) 0 297 297
2496 0 _a_err($rethash, 'dstcall_noax25');
2497 0 return 0;
2498 }
2499 297 $rethash->{'dstcallsign'} = $dstcallsign;
2500  
2501 # digipeaters
2502 297 my @digipeaters;
2503 297 ▶ if ($isax25 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isax25 == 1) { } 0 297 297
2504 0 foreach my $digi (@pathcomponents) {
2505 0 ▶ if ($digi =~ /^([A-Z0-9-]+)(\*|)$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digi =~ /^([A-Z0-9-]+)(\*|)$/io) { } 0 0 0
2506 0 my $digitested = check_ax25_call(uc($1));
2507 0 ▶ if (not(defined($digitested))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $digitested) 0 0 0
2508 0 _a_err($rethash, 'digicall_noax25');
2509 0 return 0;
2510 }
2511 0 my $wasdigied = 0;
2512 0 ▶ if ($2 eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "*") 0 0 0
2513 0 $wasdigied = 1;
2514 }
2515 # add it to the digipeater array
2516 0 push(@digipeaters, { 'call' => $digitested,
2517 'wasdigied' => $wasdigied });
2518 } else {
2519 0 _a_err($rethash, 'digicall_badchars');
2520 0 return 0;
2521 }
2522 }
2523 } else {
2524 297 my $seen_qconstr = 0;
2525
2526 297 foreach my $digi (@pathcomponents) {
2527 # From the internet. Apply the same checks as for
2528 # APRS-IS packet originator. Allow long hexadecimal IPv6
2529 # address after the Q construct.
2530 1135 ▶ if ($digi =~ /^([A-Z0-9a-z-]{1,9})(\*|)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digi =~ /^([A-Z0-9a-z-]{1,9})(\*|)$/o) { } 1132 3 1135
2531 1132 ▶ push(@digipeaters, { 'call' => $1,
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '*' ? : 34 1098 1132
2532 'wasdigied' => ($2 eq '*') ? 1 : 0 });
2533 1132 ▶ $seen_qconstr = 1 if ($1 =~ /^q..$/);
Branch: 1 branch
BranchTrueFalseTotal
if $1 =~ /^q..$/ 285 847 1132
2534 } else {
2535 3 ▶ if ($seen_qconstr && $digi =~ /^([0-9A-F]{32})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_qconstr and $digi =~ /^([0-9A-F]{32})$/) { } 1 2 3
Condition100%3/3
$seen_qconstr and $digi =~ /^([0-9A-F]{32})$/
!ll&&!rl&&r
111
MC/DC100%2/2
$seen_qconstr and $digi =~ /^([0-9A-F]{32})$/
$seen_qconstr $digi =~ /^([0-9A-F]{32})$/
Truth table100%3/3
A and B
A= $seen_qconstrB= $digi =~ /^([0-9A-F]{32})$/
ABresult
0X0
100
111
2536 1 push(@digipeaters, { 'call' => $1, 'wasdigied' => 0 });
2537 } else {
2538 2 _a_err($rethash, 'digicall_badchars');
2539 2 return 0;
2540 }
2541 }
2542 }
2543 }
2544 295 $rethash->{'digipeaters'} = \@digipeaters;
2545
2546 # So now we have source and destination callsigns and
2547 # digipeaters parsed and ok. Move on to the body.
2548  
2549 # Check the first character of the packet
2550 # and determine the packet type
2551 295 my $retval = -1;
2552 295 my $packettype = substr($body, 0, 1);
2553 295 my $paclen = length($body);
2554  
2555  
2556 # Check the packet type and proceed depending on it
2557  
2558 # Mic-encoder packet
2559 295 ▶ if (ord($packettype) == 0x27 || ord($packettype) == 0x60) {
Branch: 12 branches
BranchTrueFalseTotal
if (ord $packettype == 39 or ord $packettype == 96) { } 11 284 295
elsif ($packettype eq '!' or $packettype eq '=' or $packettype eq '/' or $packettype eq '@') { } 29 255 284
elsif ($packettype eq '_') { } 1 254 255
elsif ($packettype eq ';') { } 2 252 254
elsif ($packettype eq '$') { } 3 249 252
elsif ($packettype eq ')') { } 0 249 249
elsif ($packettype eq ':') { } 245 4 249
elsif ($packettype eq '<') { } 0 4 4
elsif ($packettype eq '>') { } 1 3 4
elsif ($body =~ /^T#(.*?),(.*)$/) { } 1 2 3
elsif ($body =~ /^DX\s+de\s+(.*?)\s*[:>]\s*(.*)$/i) { } 0 2 2
elsif ($body =~ /^{{/i) { } 0 2 2
Condition100%3/3
ord $packettype == 39 or ord $packettype == 96
l!l&&r!l&&!r
65284
MC/DC100%2/2
ord $packettype == 39 or ord $packettype == 96
ord $packettype == 39 ord $packettype == 96
Truth table100%3/3
A or B
A= ord $packettype == 39B= ord $packettype == 96
ABresult
1X1
011
000
2560 # the following are obsolete mic-e types: 0x1c 0x1d
2561 # mic-encoder data
2562 # minimum body length 9 chars
2563 11 ▶ if ($paclen >= 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paclen >= 9) 11 0 11
2564 11 $rethash->{'type'} = 'location';
2565 11 return _mice_to_decimal(substr($body, 1), $dstcallsign, $srccallsign, $rethash, \%options);
2566 }
2567  
2568 # Normal or compressed location packet, with or without
2569 # timestamp, with or without messaging capability
2570 ▶ } elsif ($packettype eq '!' ||
Condition100%9/9
$packettype eq "!" or $packettype eq "=" or $packettype eq "/" or $packettype eq "\@"
l!l&&r!l&&!r
236255
$packettype eq "!" or $packettype eq "=" or $packettype eq "/"
l!l&&r!l&&!r
167261
$packettype eq "!" or $packettype eq "="
l!l&&r!l&&!r
133268
MC/DC100%4/4
$packettype eq "!" or $packettype eq "=" or $packettype eq "/" or $packettype eq "\@"
$packettype eq "!" $packettype eq "=" $packettype eq "/" $packettype eq "\@"
Truth table100%5/5
A or B or C or D
A= $packettype eq "!"B= $packettype eq "="C= $packettype eq "/"D= $packettype eq "\@"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2571 $packettype eq '=' ||
2572 $packettype eq '/' ||
2573 $packettype eq '@') {
2574 # with or without messaging
2575 29 ▶ if ($packettype eq '!' || $packettype eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($packettype eq '!' or $packettype eq '/') { } 20 9 29
Condition100%3/3
$packettype eq "!" or $packettype eq "/"
l!l&&r!l&&!r
1379
MC/DC100%2/2
$packettype eq "!" or $packettype eq "/"
$packettype eq "!" $packettype eq "/"
Truth table100%3/3
A or B
A= $packettype eq "!"B= $packettype eq "/"
ABresult
1X1
011
000
2576 20 $rethash->{'messaging'} = 0;
2577 } else {
2578 9 $rethash->{'messaging'} = 1;
2579 }
2580
2581 29 ▶ if ($paclen >= 14) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paclen >= 14) { } 29 0 29
2582 29 $rethash->{'type'} = 'location';
2583 29 ▶ if ($packettype eq '/' || $packettype eq '@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($packettype eq "/" or $packettype eq "\@") 13 16 29
Condition100%3/3
$packettype eq "/" or $packettype eq "\@"
l!l&&r!l&&!r
7616
MC/DC100%2/2
$packettype eq "/" or $packettype eq "\@"
$packettype eq "/" $packettype eq "\@"
Truth table100%3/3
A or B
A= $packettype eq "/"B= $packettype eq "\@"
ABresult
1X1
011
000
2584 # With a prepended timestamp, check it and jump over.
2585 # If the timestamp is invalid, it will be set to zero.
2586 13 $rethash->{'timestamp'} = _parse_timestamp(\%options, substr($body, 1, 7));
2587 13 ▶ if ($rethash->{'timestamp'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rethash->{"timestamp"} == 0) 0 13 13
2588 0 _a_warn($rethash, 'timestamp_inv_loc');
2589 }
2590 13 $body = substr($body, 7);
2591 }
2592 29 $body = substr($body, 1); # remove the first character
2593 # grab the ascii value of the first byte of body
2594 29 my $poschar = ord($body);
2595 29 ▶ if ($poschar >= 48 && $poschar <= 57) {
Branch: 3 branches
BranchTrueFalseTotal
if ($poschar >= 48 and $poschar <= 57) { } 23 6 29
elsif ($poschar == 47 or $poschar == 92 or $poschar >= 65 and $poschar <= 90 or $poschar >= 97 and $poschar <= 106) { } 5 1 6
elsif ($poschar == 33) { } 1 0 1
Condition100%3/3
$poschar >= 48 and $poschar <= 57
!ll&&!rl&&r
5123
MC/DC100%2/2
$poschar >= 48 and $poschar <= 57
$poschar >= 48 $poschar <= 57
Truth table100%3/3
A and B
A= $poschar >= 48B= $poschar <= 57
ABresult
0X0
100
111
2596 # poschar is a digit... normal uncompressed position
2597 23 ▶ if (length($body) >= 19) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body >= 19) 23 0 23
2598 23 $retval = _normalpos_to_decimal($body, $srccallsign, $rethash);
2599 # continue parsing with possible comments, but only
2600 # if this is not a weather report (course/speed mixup,
2601 # weather as comment)
2602 # if the comments don't parse, don't raise an error
2603 23 ▶ if ($retval == 1 && $rethash->{'symbolcode'} ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval == 1 and $rethash->{'symbolcode'} ne '_') { } 17 6 23
Condition100%3/3
$retval == 1 and $rethash->{"symbolcode"} ne "_"
!ll&&!rl&&r
2417
MC/DC100%2/2
$retval == 1 and $rethash->{"symbolcode"} ne "_"
$retval == 1 $rethash->{"symbolcode"} ne "_"
Truth table100%3/3
A and B
A= $retval == 1B= $rethash->{"symbolcode"} ne "_"
ABresult
0X0
100
111
2604 17 _comments_to_decimal(substr($body, 19), $srccallsign, $rethash);
2605 } else {
2606 #warn "maybe a weather report?\n" . substr($body, 19) . "\n";
2607 6 _wx_parse(substr($body, 19), $rethash);
2608 }
2609 }
2610 ▶ } elsif ($poschar == 47 || $poschar == 92
Condition66%10/15
$poschar == 47 or $poschar == 92 or $poschar >= 65 and $poschar <= 90 or $poschar >= 97 and $poschar <= 106
l!l&&r!l&&!r
501
$poschar == 47 or $poschar == 92 or $poschar >= 65 and $poschar <= 90
l!l&&r!l&&!r
411
$poschar == 47 or $poschar == 92
l!l&&r!l&&!r
402
$poschar >= 65 and $poschar <= 90
!ll&&!rl&&r
101
$poschar >= 97 and $poschar <= 106
!ll&&!rl&&r
100
MC/DC33%2/6
$poschar == 47 or $poschar == 92 or $poschar >= 65 and $poschar <= 90 or $poschar >= 97 and $poschar <= 106
$poschar == 47 $poschar == 92 $poschar >= 65 $poschar <= 90 $poschar >= 97 $poschar <= 106
Truth table33%3/9
A or B or C and D or E and F
A= $poschar == 47B= $poschar == 92C= $poschar >= 65D= $poschar <= 90E= $poschar >= 97F= $poschar <= 106
ABCDEFresult
1XXXXX1
01XXXX1
0011XX1
000X111
0010111
000X0X0
000X100
00100X0
0010100
2611 || ($poschar >= 65 && $poschar <= 90) || ($poschar >= 97 && $poschar <= 106) ) {
2612 # $poschar =~ /^[\/\\A-Za-j]$/o
2613 # compressed position
2614 5 ▶ if (length($body) >= 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body >= 13) 4 1 5
2615 4 $retval = _compressed_to_decimal(substr($body, 0, 13), $srccallsign, $rethash);
2616 # continue parsing with possible comments, but only
2617 # if this is not a weather report (course/speed mixup,
2618 # weather as comment)
2619 # if the comments don't parse, don't raise an error
2620 4 ▶ if ($retval == 1 && $rethash->{'symbolcode'} ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval == 1 and $rethash->{'symbolcode'} ne '_') { } 3 1 4
Condition66%2/3
$retval == 1 and $rethash->{"symbolcode"} ne "_"
!ll&&!rl&&r
013
MC/DC50%1/2
$retval == 1 and $rethash->{"symbolcode"} ne "_"
$retval == 1 $rethash->{"symbolcode"} ne "_"
Truth table66%2/3
A and B
A= $retval == 1B= $rethash->{"symbolcode"} ne "_"
ABresult
0X0
100
111
2621 3 _comments_to_decimal(substr($body, 13), $srccallsign, $rethash);
2622 } else {
2623 #warn "maybe a weather report?\n" . substr($body, 13) . "\n";
2624 1 _wx_parse(substr($body, 13), $rethash);
2625 }
2626 }
2627 } elsif ($poschar == 33) { # '!'
2628 # Weather report from Ultimeter 2000
2629 1 $rethash->{'type'} = 'wx';
2630 1 return _wx_parse_peet_logging(substr($body, 1), $srccallsign, $rethash);
2631 } else {
2632 0 _a_err($rethash, 'packet_invalid');
2633 0 return 0;
2634 }
2635 } else {
2636 0 _a_err($rethash, 'packet_short', 'location');
2637 0 return 0;
2638 }
2639  
2640 # Weather report
2641 } elsif ($packettype eq '_') {
2642 1 ▶ if ($body =~ /_(\d{8})c[\- \.\d]{1,3}s[\- \.\d]{1,3}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /_(\d{8})c[\- \.\d]{1,3}s[\- \.\d]{1,3}/) { } 1 0 1
2643 1 $rethash->{'type'} = 'wx';
2644 1 return _wx_parse(substr($body, 9), $rethash);
2645 } else {
2646 0 _a_err($rethash, 'wx_unsupp', 'Positionless');
2647 0 return 0;
2648 }
2649
2650 # Object
2651 } elsif ($packettype eq ';') {
2652 2 ▶ if ($paclen >= 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paclen >= 31) 2 0 2
2653 2 $rethash->{'type'} = 'object';
2654 2 return _object_to_decimal(\%options, $body, $srccallsign, $rethash);
2655 }
2656  
2657 # NMEA data
2658 } elsif ($packettype eq '$') {
2659 # don't try to parse the weather stations, require "$GP" start
2660 3 ▶ if (substr($body, 0, 3) eq '$GP') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($body, 0, 3) eq '$GP') { } 1 2 3
elsif (substr($body, 0, 5) eq '$ULTW') { } 2 0 2
2661 # dstcallsign can contain the APRS symbol to use,
2662 # so read that one too
2663 1 $rethash->{'type'} = 'location';
2664 1 return _nmea_to_decimal(\%options, substr($body, 1), $srccallsign, $dstcallsign, $rethash);
2665 } elsif (substr($body, 0, 5) eq '$ULTW') {
2666 2 $rethash->{'type'} = 'wx';
2667 2 return _wx_parse_peet_packet(substr($body, 5), $srccallsign, $rethash);
2668 }
2669  
2670 # Item
2671 } elsif ($packettype eq ')') {
2672 0 ▶ if ($paclen >= 18) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paclen >= 18) 0 0 0
2673 0 $rethash->{'type'} = 'item';
2674 0 return _item_to_decimal($body, $srccallsign, $rethash);
2675 }
2676  
2677 # Message, bulletin or an announcement
2678 } elsif ($packettype eq ':') {
2679 245 ▶ if ($paclen >= 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paclen >= 11) 245 0 245
2680 # all are labeled as messages for the time being
2681 245 $rethash->{'type'} = 'message';
2682 245 return _message_parse($body, $srccallsign, $rethash);
2683 }
2684  
2685 # Station capabilities
2686 } elsif ($packettype eq '<') {
2687 # at least one other character besides '<' required
2688 0 ▶ if ($paclen >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paclen >= 2) 0 0 0
2689 0 $rethash->{'type'} = 'capabilities';
2690 0 return _capabilities_parse(substr($body, 1), $srccallsign, $rethash);
2691 }
2692  
2693 # Status reports
2694 } elsif ($packettype eq '>') {
2695 # we can live with empty status reports
2696 1 ▶ if ($paclen >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paclen >= 1) 1 0 1
2697 1 $rethash->{'type'} = 'status';
2698 1 return _status_parse(\%options, substr($body, 1), $srccallsign, $rethash);
2699 }
2700
2701 # Telemetry
2702 } elsif ($body =~ /^T#(.*?),(.*)$/) {
2703 1 $rethash->{'type'} = 'telemetry';
2704 1 return _telemetry_parse(substr($body, 2), $rethash);
2705
2706 # DX spot
2707 } elsif ($body =~ /^DX\s+de\s+(.*?)\s*[:>]\s*(.*)$/i) {
2708 0 $rethash->{'type'} = 'dx';
2709 0 return _dx_parse($1, $2, $rethash);
2710
2711 # Experimental
2712 } elsif ($body =~ /^{{/i) {
2713 0 _a_err($rethash, 'exp_unsupp');
2714 0 return 0;
2715
2716 # When all else fails, try to look for a !-position that can
2717 # occur anywhere within the 40 first characters according
2718 # to the spec.
2719 } else {
2720 2 my $pos = index($body, '!');
2721 2 ▶ if ($pos >= 0 && $pos <= 39) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0 and $pos <= 39) 1 1 2
Condition66%2/3
$pos >= 0 and $pos <= 39
!ll&&!rl&&r
101
MC/DC50%1/2
$pos >= 0 and $pos <= 39
$pos >= 0 $pos <= 39
Truth table66%2/3
A and B
A= $pos >= 0B= $pos <= 39
ABresult
0X0
100
111
2722 1 $rethash->{'type'} = 'location';
2723 1 $rethash->{'messaging'} = 0;
2724 1 my $pchar = substr($body, $pos + 1, 1);
2725 1 ▶ if ($pchar =~ /^[\/\\A-Za-j]$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pchar =~ m[^[/\\A-Za-j]$]o) { } 0 1 1
elsif ($pchar =~ /^\d$/io) { } 1 0 1
2726 # compressed position
2727 0 ▶ if (length($body) >= $pos + 1 + 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body >= $pos + 1 + 13) 0 0 0
2728 0 $retval = _compressed_to_decimal(substr($body, $pos + 1, 13), $srccallsign, $rethash);
2729 # check the APRS data extension and comment,
2730 # if not weather data
2731 0 ▶ if ($retval == 1 && $rethash->{'symbolcode'} ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval == 1 and $rethash->{"symbolcode"} ne "_") 0 0 0
Condition0%0/3
$retval == 1 and $rethash->{"symbolcode"} ne "_"
!ll&&!rl&&r
000
MC/DC0%0/2
$retval == 1 and $rethash->{"symbolcode"} ne "_"
$retval == 1 $rethash->{"symbolcode"} ne "_"
Truth table0%0/3
A and B
A= $retval == 1B= $rethash->{"symbolcode"} ne "_"
ABresult
0X0
100
111
2732 0 _comments_to_decimal(substr($body, $pos + 14), $srccallsign, $rethash);
2733 }
2734 }
2735 } elsif ($pchar =~ /^\d$/io) {
2736 # normal uncompressed position
2737 1 ▶ if (length($body) >= $pos + 1 + 19) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body >= $pos + 1 + 19) 1 0 1
2738 1 $retval = _normalpos_to_decimal(substr($body, $pos + 1), $srccallsign, $rethash);
2739 # check the APRS data extension and comment,
2740 # if not weather data
2741 1 ▶ if ($retval == 1 && $rethash->{'symbolcode'} ne '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval == 1 and $rethash->{"symbolcode"} ne "_") 1 0 1
Condition33%1/3
$retval == 1 and $rethash->{"symbolcode"} ne "_"
!ll&&!rl&&r
001
MC/DC0%0/2
$retval == 1 and $rethash->{"symbolcode"} ne "_"
$retval == 1 $rethash->{"symbolcode"} ne "_"
Truth table33%1/3
A and B
A= $retval == 1B= $rethash->{"symbolcode"} ne "_"
ABresult
0X0
100
111
2742 1 _comments_to_decimal(substr($body, $pos + 20), $srccallsign, $rethash);
2743 }
2744 }
2745 }
2746 }
2747 }
2748  
2749 # Return success for an ok packet
2750 30 ▶ if ($retval == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval == 1) 26 4 30
2751 26 return 1;
2752 }
2753
2754 4 return 0;
2755 }
2756  
2757  
2758 # Checks a callsign for validity and strips
2759 # trailing spaces out and returns the string.
2760 # Returns undef on invalid callsign
2761 sub _kiss_checkcallsign($)
2762 {
2763 0 ▶ if ($_[0] =~ /^([A-Z0-9]+)\s*(|-\d+)$/o) {
Subroutine _kiss_checkcallsign: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^([A-Z0-9]+)\s*(|-\d+)$/o) 0 0 0
2764 0 ▶ if (length($2) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $2 > 0) 0 0 0
2765 # check the SSID if given
2766 0 ▶ if ($2 < -15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 < -15) 0 0 0
2767 0 return undef;
2768 }
2769 }
2770 0 return $1 . $2;
2771 }
2772  
2773 # no match
2774 0 return undef;
2775 }
2776  
2777  
2778 =over
2779  
2780 =item kiss_to_tnc2($kissframe)
2781  
2782 Convert a KISS-frame into a TNC-2 compatible UI-frame.
2783 Non-UI and non-pid-F0 frames are dropped. The KISS-frame
2784 to be decoded should not have FEND (0xC0) characters
2785 in the beginning or in the end. Byte unstuffing
2786 must not be done before calling this function. Returns
2787 a string containing the TNC-2 frame (no CR and/or LF)
2788 or undef on error.
2789  
2790 =back
2791  
2792 =cut
2793  
2794 sub kiss_to_tnc2($) {
2795 0 ▶ my $kissframe = shift @_;
Subroutine kiss_to_tnc2: not called CC=29 SCAR=67.7
Pod documented
2796  
2797 0 my $asciiframe = "";
2798 0 my $dstcallsign = "";
2799 0 my $callsigntmp = "";
2800 0 my $digipeatercount = 0; # max. 8 digipeaters
2801  
2802 # perform byte unstuffing for kiss first
2803 0 $kissframe =~ s/\xdb\xdc/\xc0/g;
2804 0 $kissframe =~ s/\xdb\xdd/\xdb/g;
2805  
2806 # length checking _after_ byte unstuffing
2807 0 ▶ if (length($kissframe) < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $kissframe < 16) 0 0 0
2808 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2809 0 warn "too short frame to be valid kiss\n";
2810 }
2811 0 return undef;
2812 }
2813  
2814 # the first byte has to be zero (kiss data)
2815 0 ▶ if (ord(substr($kissframe, 0, 1)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord substr($kissframe, 0, 1) != 0) 0 0 0
2816 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2817 0 warn "not a kiss data frame\n";
2818 }
2819 0 return undef;
2820 }
2821  
2822 0 my $addresspart = 0;
2823 0 my $addresscount = 0;
2824 0 ▶ while (length($kissframe) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $kissframe > 0) 0 0 0
2825 # in the first run this removes the zero byte,
2826 # in subsequent runs this removes the previous byte
2827 0 $kissframe = substr($kissframe, 1);
2828 0 my $charri = substr($kissframe, 0, 1);
2829  
2830 0 ▶ if ($addresspart == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($addresspart == 0) { } 0 0 0
elsif ($addresspart == 1) { } 0 0 0
elsif ($addresspart == 2) { } 0 0 0
2831 0 $addresscount++;
2832 # we are in the address field, go on
2833 # decoding it
2834 # switch to numeric
2835 0 $charri = ord($charri);
2836 # check whether this is the last
2837 # (0-bit is one)
2838 0 ▶ if ($charri & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charri & 1) 0 0 0
2839 0 ▶ if ($addresscount < 14 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($addresscount < 14 or $addresscount % 7 != 0) 0 0 0
Condition0%0/3
$addresscount < 14 or $addresscount % 7 != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$addresscount < 14 or $addresscount % 7 != 0
$addresscount < 14 $addresscount % 7 != 0
Truth table0%0/3
A or B
A= $addresscount < 14B= $addresscount % 7 != 0
ABresult
1X1
011
000
2840 ($addresscount % 7) != 0) {
2841 # addresses ended too soon or in the
2842 # wrong place
2843 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2844 0 warn "addresses ended too soon or in the wrong place in kiss frame\n";
2845 }
2846 0 return undef;
2847 }
2848 # move on to control field next time
2849 0 $addresspart = 1;
2850 }
2851 # check the complete callsign
2852 # (7 bytes)
2853 0 ▶ if (($addresscount % 7) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addresscount % 7 == 0) 0 0 0
2854 # this is SSID, get the number
2855 0 my $ssid = ($charri >> 1) & 0xf;
2856 0 ▶ if ($ssid != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssid != 0) 0 0 0
2857 # don't print zero SSID
2858 0 $callsigntmp .= "-" . $ssid;
2859 }
2860 # check the callsign for validity
2861 0 my $chkcall = _kiss_checkcallsign($callsigntmp);
2862 0 ▶ if (not(defined($chkcall))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chkcall) 0 0 0
2863 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2864 0 warn "Invalid callsign in kiss frame, discarding\n";
2865 }
2866 0 return undef;
2867 }
2868 0 ▶ if ($addresscount == 7) {
Branch: 3 branches
BranchTrueFalseTotal
if ($addresscount == 7) { } 0 0 0
elsif ($addresscount == 14) { } 0 0 0
elsif ($addresscount > 14) { } 0 0 0
2869 # we have a destination callsign
2870 0 $dstcallsign = $chkcall;
2871 0 $callsigntmp = "";
2872 0 next;
2873 } elsif ($addresscount == 14) {
2874 # we have a source callsign, copy
2875 # it to the final frame directly
2876 0 $asciiframe = $chkcall . ">" . $dstcallsign;
2877 0 $callsigntmp = "";
2878 } elsif ($addresscount > 14) {
2879 # get the H-bit as well if we
2880 # are in the path part
2881 0 $asciiframe .= $chkcall;
2882 0 $callsigntmp = "";
2883 0 ▶ if ($charri & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charri & 128) 0 0 0
2884 0 $asciiframe .= "*";
2885 }
2886 0 $digipeatercount++;
2887 } else {
2888 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2889 0 warn "Internal error 1 in kiss_to_tnc2()\n";
2890 }
2891 0 return undef;
2892 }
2893 0 ▶ if ($addresspart == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addresspart == 0) { } 0 0 0
2894 # more address fields will follow
2895 # check that there are a maximum
2896 # of eight digipeaters in the path
2897 0 ▶ if ($digipeatercount >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digipeatercount >= 8) 0 0 0
2898 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2899 0 warn "Too many digipeaters in kiss packet, discarding\n";
2900 }
2901 0 return undef;
2902 }
2903 0 $asciiframe .= ",";
2904 } else {
2905 # end of address fields
2906 0 $asciiframe .= ":";
2907 }
2908 0 next;
2909 }
2910 # shift one bit right to get the ascii
2911 # character
2912 0 $charri >>= 1;
2913 0 $callsigntmp .= chr($charri);
2914  
2915 } elsif ($addresspart == 1) {
2916 # control field. we are only interested in
2917 # UI frames, discard others
2918 0 $charri = ord($charri);
2919 0 ▶ if ($charri != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charri != 3) 0 0 0
2920 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2921 0 warn "not UI frame, skipping\n";
2922 }
2923 0 return undef;
2924 }
2925 #print " control $charri";
2926 0 $addresspart = 2;
2927  
2928 } elsif ($addresspart == 2) {
2929 # PID
2930 #printf(" PID %02x data: ", ord($charri));
2931 # we want PID 0xFO
2932 0 $charri = ord($charri);
2933 0 ▶ if ($charri != 0xf0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charri != 240) 0 0 0
2934 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2935 0 warn "PID not 0xF0, skipping\n";
2936 }
2937 0 return undef;
2938 }
2939 0 $addresspart = 3;
2940  
2941 } else {
2942 # body
2943 0 $asciiframe .= $charri;
2944 }
2945 }
2946  
2947 # Ok, return whole frame
2948 0 return $asciiframe;
2949 }
2950  
2951 =over
2952  
2953 =item tnc2_to_kiss($tnc2frame)
2954  
2955 Convert a TNC-2 compatible UI-frame into a KISS data
2956 frame (single port KISS TNC). The frame will be complete,
2957 i.e. it has byte stuffing done and FEND (0xC0) characters
2958 on both ends. If conversion fails, return undef.
2959  
2960 =back
2961  
2962 =cut
2963  
2964 sub tnc2_to_kiss($) {
2965 0 ▶ my $gotframe = shift @_;
Subroutine tnc2_to_kiss: not called CC=27 SCAR=66.3
Pod documented
2966  
2967 0 my $kissframe = chr(0); # kiss frame starts with byte 0x00
2968 0 my $body;
2969 my $header;
2970  
2971 # separate header and body
2972 0 ▶ if ($gotframe =~ /^([A-Z0-9,*>-]+):(.+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gotframe =~ /^([A-Z0-9,*>-]+):(.+)$/o) { } 0 0 0
2973 0 $header = $1;
2974 0 $body = $2;
2975 } else {
2976 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2977 0 warn "tnc2_to_kiss(): separation into header and body failed\n";
2978 }
2979 0 return undef;
2980 }
2981  
2982 # separate the sender, recipient and digipeaters
2983 0 my $sender;
2984 my $sender_ssid;
2985 0 my $receiver;
2986 0 my $receiver_ssid;
2987 0 my $digipeaters;
2988 0 ▶ if ($header =~ /^([A-Z0-9]{1,6})(-\d+|)>([A-Z0-9]{1,6})(-\d+|)(|,.*)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^([A-Z0-9]{1,6})(-\d+|)>([A-Z0-9]{1,6})(-\d+|)(|,.*)$/o) { } 0 0 0
2989 0 $sender = $1;
2990 0 $sender_ssid = $2;
2991 0 $receiver = $3;
2992 0 $receiver_ssid = $4;
2993 0 $digipeaters = $5;
2994 } else {
2995 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
2996 0 warn "tnc2_to_kiss(): separation of sender and receiver from header failed\n";
2997 }
2998 0 return undef;
2999 }
3000  
3001 # Check SSID format and convert to number
3002 0 ▶ if (length($sender_ssid) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sender_ssid > 0) { } 0 0 0
3003 0 $sender_ssid = 0 - $sender_ssid;
3004 0 ▶ if ($sender_ssid > 15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sender_ssid > 15) 0 0 0
3005 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
3006 0 warn "tnc2_to_kiss(): sender SSID ($sender_ssid) is over 15\n";
3007 }
3008 0 return undef;
3009 }
3010 } else {
3011 0 $sender_ssid = 0;
3012 }
3013 0 ▶ if (length($receiver_ssid) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $receiver_ssid > 0) { } 0 0 0
3014 0 $receiver_ssid = 0 - $receiver_ssid;
3015 0 ▶ if ($receiver_ssid > 15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($receiver_ssid > 15) 0 0 0
3016 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
3017 0 warn "tnc2_to_kiss(): receiver SSID ($receiver_ssid) is over 15\n";
3018 }
3019 0 return undef;
3020 }
3021 } else {
3022 0 $receiver_ssid = 0;
3023 }
3024 # pad callsigns to 6 characters with space
3025 0 $sender .= ' ' x (6 - length($sender));
3026 0 $receiver .= ' ' x (6 - length($receiver));
3027 # encode destination and source
3028 0 ▶ for (my $i = 0; $i < 6; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 6) 0 0 0
3029 0 $kissframe .= chr(ord(substr($receiver, $i, 1)) << 1);
3030 }
3031 0 $kissframe .= chr(0xe0 | ($receiver_ssid << 1));
3032 0 ▶ for (my $i = 0; $i < 6; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 6) 0 0 0
3033 0 $kissframe .= chr(ord(substr($sender, $i, 1)) << 1);
3034 }
3035 0 ▶ if (length($digipeaters) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $digipeaters > 0) { } 0 0 0
3036 0 $kissframe .= chr(0x60 | ($sender_ssid << 1));
3037 } else {
3038 0 $kissframe .= chr(0x61 | ($sender_ssid << 1));
3039 }
3040  
3041 # if there are digipeaters, add them
3042 0 ▶ if (length($digipeaters) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $digipeaters > 0) 0 0 0
3043 0 $digipeaters =~ s/,//; # remove the first comma
3044 # split into parts
3045 0 my @digis = split(/,/, $digipeaters);
3046 0 my $digicount = scalar(@digis);
3047 0 ▶ if ($digicount > 8 || $digicount < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digicount > 8 or $digicount < 1) 0 0 0
Condition0%0/3
$digicount > 8 or $digicount < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$digicount > 8 or $digicount < 1
$digicount > 8 $digicount < 1
Truth table0%0/3
A or B
A= $digicount > 8B= $digicount < 1
ABresult
1X1
011
000
3048 # too many (or none?!?) digipeaters
3049 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
3050 0 warn "tnc2_to_kiss(): too many (or zero) digipeaters: $digicount\n";
3051 }
3052 0 return undef;
3053 }
3054 0 ▶ for (my $i = 0; $i < $digicount; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $digicount) 0 0 0
3055 # split into callsign, SSID and h-bit
3056 0 ▶ if ($digis[$i] =~ /^([A-Z0-9]{1,6})(-\d+|)(\*|)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digis[$i] =~ /^([A-Z0-9]{1,6})(-\d+|)(\*|)$/o) { } 0 0 0
3057 0 my $callsign = $1 . ' ' x (6 - length($1));
3058 0 my $ssid = 0;
3059 0 my $hbit = 0x00;
3060 0 ▶ if (length($2) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $2 > 0) 0 0 0
3061 0 $ssid = 0 - $2;
3062 0 ▶ if ($ssid > 15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssid > 15) 0 0 0
3063 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
3064 0 warn "tnc2_to_kiss(): digipeater nr. $i SSID ($ssid) invalid\n";
3065 }
3066 0 return undef;
3067 }
3068 }
3069 0 ▶ if ($3 eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($3 eq "*") 0 0 0
3070 0 $hbit = 0x80;
3071 }
3072 # add to kiss frame
3073 0 ▶ for (my $k = 0; $k < 6; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 6) 0 0 0
3074 0 $kissframe .= chr(ord(substr($callsign, $k, 1)) << 1);
3075 }
3076 0 ▶ if ($i + 1 < $digicount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $digicount) { } 0 0 0
3077 # more digipeaters to follow
3078 0 $kissframe .= chr($hbit | 0x60 | ($ssid << 1));
3079 } else {
3080 # last digipeater
3081 0 $kissframe .= chr($hbit | 0x61 | ($ssid << 1));
3082 }
3083
3084 } else {
3085 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
3086 0 warn "tnc2_to_kiss(): digipeater nr. $i parsing failed\n";
3087 }
3088 0 return undef;
3089 }
3090 }
3091 }
3092  
3093 # add frame type (0x03) and PID (0xF0)
3094 0 $kissframe .= chr(0x03) . chr(0xf0);
3095 # add frame body
3096 0 $kissframe .= $body;
3097 # perform KISS byte stuffing
3098 0 $kissframe =~ s/\xdb/\xdb\xdd/g;
3099 0 $kissframe =~ s/\xc0/\xdb\xdc/g;
3100 # add FENDs
3101 0 $kissframe = chr(0xc0) . $kissframe . chr(0xc0);
3102  
3103 0 return $kissframe;
3104 }
3105  
3106 =over
3107  
3108 =item aprs_duplicate_parts($packet)
3109  
3110 Accepts a TNC-2 format frame and extracts the original
3111 sender callsign, destination callsign (without ssid) and
3112 payload data for duplicate detection. Returns
3113 sender, receiver and body on success, undef on error.
3114 In the case of third party packets, always gets this
3115 information from the innermost data. Also removes
3116 possible trailing spaces to improve detection
3117 (e.g. aprsd replaces trailing CRs or LFs in a packet with a space).
3118  
3119 =back
3120  
3121 =cut
3122  
3123 sub aprs_duplicate_parts($)
3124 {
3125 0 ▶ my ($packet) = @_;
Subroutine aprs_duplicate_parts: not called CC=4 SCAR=30.0
Pod documented
3126  
3127 # If this is a third party packet format,
3128 # strip out the outer layer and focus on the inside.
3129 # Do this several times in a row if necessary
3130 0 while (1) {
3131 0 ▶ if ($packet =~ /^[^:]+:\}(.*)$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ /^[^:]+:\}(.*)$/io) { } 0 0 0
3132 0 $packet = $1;
3133 } else {
3134 0 last;
3135 }
3136 }
3137  
3138 0 ▶ if ($packet =~ /^([A-Z0-9]{1,6})(-[A-Z0-9]{1,2}|)>([A-Z0-9]{1,6})(-\d{1,2}|)(:|,[^:]+:)(.*)$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ /^([A-Z0-9]{1,6})(-[A-Z0-9]{1,2}|)>([A-Z0-9]{1,6})(-\d{1,2}|)(:|,[^:]+:)(.*)$/io) 0 0 0
3139 0 my $source;
3140 my $destination;
3141 0 my $body = $6;
3142 0 ▶ if ($2 eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq '') { } 0 0 0
3143 # ssid 0
3144 0 $source = $1 . "-0";
3145 } else {
3146 0 $source = $1 . $2;
3147 }
3148 # drop SSID for destination
3149 0 $destination = $3;
3150 # remove trailing spaces from body
3151 0 $body =~ s/\s+$//;
3152 0 return ($source, $destination, $body);
3153 }
3154  
3155 0 return undef;
3156 }
3157  
3158 =over
3159  
3160 =item make_object($name, $tstamp, $lat, $lon, $symbols, $speed, $course, $altitude, $alive, $usecompression, $posambiguity, $comment)
3161  
3162 Creates an APRS object. Returns a body of an APRS object, i.e. ";OBJECTNAM*DDHHMM/DDMM.hhN/DDDMM.hhW$CSE/SPDcomments..."
3163 or undef on error.
3164  
3165 Parameters:
3166  
3167 1st: object name, has to be valid APRS object name, does not need to be space-padded
3168 2nd: object timestamp as a unix timestamp, or zero to use current time
3169 3rd: object latitude, decimal degrees
3170 4th: object longitude, decimal degrees
3171 5th: object symbol table (or overlay) and symbol code, two bytes if the given symbole length is zero (""), use point (//)
3172 6th: object speed, -1 if non-moving (km/h)
3173 7th: object course, -1 if non-moving
3174 8th: object altitude, -10000 or less if not used
3175 9th: alive or dead object (0 == dead, 1 == alive)
3176 10th: compressed (1) or uncompressed (0)
3177 11th: position ambiguity (0..4)
3178 12th: object comment text
3179  
3180  
3181 Note: Course/speed/altitude/compression is not implemented.
3182  
3183 This function API will probably change in the near future. The long list of
3184 parameters should be changed to hash with named parameters.
3185  
3186 =back
3187  
3188 =cut
3189  
3190 sub make_object($$$$$$$$$$$$) {
3191 # FIXME: course/speed/altitude/compression not implemented
3192 0 ▶ my $name = shift @_;
Subroutine make_object: not called CC=6 SCAR=37.4
Pod documented
3193 0 my $tstamp = shift @_;
3194 0 my $lat = shift @_;
3195 0 my $lon = shift @_;
3196 0 my $symbols = shift @_;
3197 0 my $speed = shift @_;
3198 0 my $course = shift @_;
3199 0 my $altitude = shift @_;
3200 0 my $alive = shift @_;
3201 0 my $usecompression = shift @_;
3202 0 my $posambiguity = shift @_;
3203 0 my $comment = shift @_;
3204  
3205 0 my $packetbody = ";";
3206  
3207 # name
3208 0 ▶ if ($name =~ /^([\x20-\x7e]{1,9})$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^([\x20-\x7e]{1,9})$/o) { } 0 0 0
3209 # also pad with whitespace
3210 0 $packetbody .= $1 . " " x (9 - length($1));
3211 } else {
3212 0 return undef;
3213 }
3214  
3215 # dead/alive
3216 0 ▶ if ($alive == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($alive == 1) { } 0 0 0
elsif ($alive == 0) { } 0 0 0
3217 0 $packetbody .= "*";
3218 } elsif ($alive == 0) {
3219 0 $packetbody .= "_";
3220 } else {
3221 0 return undef;
3222 }
3223  
3224 # timestamp, hardwired for DHM
3225 0 my $aptime = make_timestamp($tstamp, 0);
3226 0 ▶ if (not(defined($aptime))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $aptime) { } 0 0 0
3227 0 return undef;
3228 } else {
3229 0 $packetbody .= $aptime;
3230 }
3231  
3232 # actual position
3233 0 my $posstring = make_position($lat, $lon, $speed, $course, $altitude, $symbols, $usecompression, $posambiguity);
3234 0 ▶ if (not(defined($posstring))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $posstring) { } 0 0 0
3235 0 return undef;
3236 } else {
3237 0 $packetbody .= $posstring;
3238 }
3239  
3240 # add comments to the end
3241 0 $packetbody .= $comment;
3242  
3243 0 return $packetbody;
3244 }
3245  
3246 =over
3247  
3248 =item make_timestamp($timestamp, $format)
3249  
3250 Create an APRS (UTC) six digit (DHM or HMS) timestamp from a unix timestamp.
3251 The first parameter is the unix timestamp to use, or zero to use
3252 current time. Second parameter should be one for
3253 HMS format, zero for DHM format.
3254  
3255 Returns a 7-character string (e.g. "291345z") or undef on error.
3256  
3257 =back
3258  
3259 =cut
3260  
3261 sub make_timestamp($$) {
3262 0 ▶ my $tstamp = shift @_;
Subroutine make_timestamp: not called CC=5 SCAR=34.0
Pod documented
3263 0 my $tformat = shift @_;
3264  
3265 0 ▶ if ($tstamp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tstamp == 0) 0 0 0
3266 0 $tstamp = time();
3267 }
3268  
3269 0 my ($day, $hour, $minute, $sec) = (gmtime($tstamp))[3,2,1,0];
3270 0 ▶ if (not(defined($day))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $day) 0 0 0
3271 0 return undef;
3272 }
3273  
3274 0 my $tstring = "";
3275 0 ▶ if ($tformat == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tformat == 0) { } 0 0 0
elsif ($tformat == 1) { } 0 0 0
3276 0 $tstring = sprintf("%02d%02d%02dz", $day, $hour, $minute);
3277 } elsif ($tformat == 1) {
3278 0 $tstring = sprintf("%02d%02d%02dh", $hour, $minute, $sec);
3279 } else {
3280 0 return undef;
3281 }
3282 0 return $tstring;
3283 }
3284  
3285 =over
3286  
3287 =item make_position($lat, $lon, $speed, $course, $altitude, $symbols, $usecompression, $posambiguity)
3288  
3289 Creates an APRS position for position/object/item. Parameters:
3290  
3291 1st: latitude in decimal degrees
3292 2nd: longitude in decimal degrees
3293 3rd: speed in km/h, -1 == don't include
3294 4th: course in degrees, -1 == don't include. zero == unknown course, 360 == north
3295 5th: altitude in meters above mean sea level, -10000 or under == don't use
3296 6th: aprs symbols to use, first table/overlay and then code (two bytes). If string length is zero (""), uses default.
3297 7th: use compression (1) or not (0)
3298 8th: use amount (0..4) of position ambiguity. Note that position ambiguity and compression can't be used at the same time.
3299  
3300 Returns a string such as "1234.56N/12345.67E/CSD/SPD" or in
3301 compressed form "F*-X;n_Rv&{-A" or undef on error.
3302  
3303 Please note: course/speed/altitude are not supported yet, and neither is compressed format or position ambiguity.
3304  
3305 This function API will probably change in the near future. The long list of
3306 parameters should be changed to hash with named parameters.
3307  
3308 =back
3309  
3310 =cut
3311  
3312 sub make_position($$$$$$$$) {
3313 # FIXME: course/speed/altitude are not supported yet,
3314 # neither is compressed format or position ambiguity
3315 0 ▶ my $lat = shift @_;
Subroutine make_position: not called CC=32 SCAR=69.6
Pod documented
3316 0 my $lon = shift @_;
3317 0 my $speed = shift @_;
3318 0 my $course = shift @_;
3319 0 my $altitude = shift @_;
3320 0 my $symbols = shift @_;
3321 0 my $usecompression = shift @_;
3322 0 my $posambiguity = shift @_;
3323  
3324 0 ▶ if ($lat < -89.99999 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($lat < "-89.99999" or $lat > "89.99999" or $lon < "-179.99999" or $lon > "179.99999") 0 0 0
Condition0%0/9
$lat < "-89.99999" or $lat > "89.99999" or $lon < "-179.99999" or $lon > "179.99999"
l!l&&r!l&&!r
000
$lat < "-89.99999" or $lat > "89.99999" or $lon < "-179.99999"
l!l&&r!l&&!r
000
$lat < "-89.99999" or $lat > "89.99999"
l!l&&r!l&&!r
000
MC/DC0%0/4
$lat < "-89.99999" or $lat > "89.99999" or $lon < "-179.99999" or $lon > "179.99999"
$lat < "-89.99999" $lat > "89.99999" $lon < "-179.99999" $lon > "179.99999"
Truth table0%0/5
A or B or C or D
A= $lat < "-89.99999"B= $lat > "89.99999"C= $lon < "-179.99999"D= $lon > "179.99999"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3325 $lat > 89.99999 ||
3326 $lon < -179.99999 ||
3327 $lon > 179.99999) {
3328 # invalid location
3329 0 return undef;
3330 }
3331  
3332 0 my $symboltable = "";
3333 0 my $symbolcode = "";
3334 0 ▶ if (length($symbols) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $symbols == 0) { } 0 0 0
elsif ($symbols =~ m[^([/\\A-Z0-9])([\x21-\x7b\x7d])$]o) { } 0 0 0
3335 0 $symboltable = "/";
3336 0 $symbolcode = "/";
3337 } elsif ($symbols =~ /^([\/\\A-Z0-9])([\x21-\x7b\x7d])$/o) {
3338 0 $symboltable = $1;
3339 0 $symbolcode = $2;
3340 } else {
3341 0 return undef;
3342 }
3343  
3344  
3345 0 ▶ if ($usecompression == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usecompression == 1) { } 0 0 0
3346 0 my $latval = 380926 * (90 - $lat);
3347 0 my $lonval = 190463 * (180 + $lon);
3348 0 my $latstring = "";
3349 0 my $lonstring = "";
3350 0 ▶ for (my $i = 3; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
3351 # latitude character
3352 0 my $value = int($latval / (91 ** $i));
3353 0 $latval = $latval % (91 ** $i);
3354 0 $latstring .= chr($value + 33);
3355 # longitude character
3356 0 $value = int($lonval / (91 ** $i));
3357 0 $lonval = $lonval % (91 ** $i);
3358 0 $lonstring .= chr($value + 33);
3359 }
3360 # encode overlay character if it is a number
3361 0 $symboltable =~ tr/0-9/a-j/;
3362 # FIXME: no speed/course/altitude/radiorange encoding
3363 0 my $retstring = $symboltable . $latstring . $lonstring . $symbolcode;
3364 0 ▶ if ($speed >= 0 && $course > 0 && $course <= 360) {
Branch: 1 branch
BranchTrueFalseTotal
if ($speed >= 0 and $course > 0 and $course <= 360) { } 0 0 0
Condition0%0/6
$speed >= 0 and $course > 0 and $course <= 360
!ll&&!rl&&r
000
$speed >= 0 and $course > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$speed >= 0 and $course > 0 and $course <= 360
$speed >= 0 $course > 0 $course <= 360
Truth table0%0/4
A and B and C
A= $speed >= 0B= $course > 0C= $course <= 360
ABCresult
0XX0
10X0
1100
1111
3365 # In APRS spec unknown course is zero normally (and north is 360),
3366 # but in compressed aprs north is zero and there is no unknown course.
3367 # So round course to nearest 4-degree section and remember
3368 # to do the 360 -> 0 degree transformation.
3369 0 my $cval = int(($course + 2) / 4);
3370 0 ▶ if ($cval > 89) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cval > 89) 0 0 0
3371 0 $cval = 0;
3372 }
3373 0 $retstring .= chr($cval + 33);
3374 # speed is in knots in compressed form. round to nearest integer
3375 0 my $speednum = int((log(($speed / $knot_to_kmh) + 1) / log(1.08)) + 0.5);
3376 0 ▶ if ($speednum > 89) {
Branch: 1 branch
BranchTrueFalseTotal
if ($speednum > 89) 0 0 0
3377 # limit top speed
3378 0 $speednum = 89;
3379 }
3380 0 $retstring .= chr($speednum + 33) . "A";
3381 } else {
3382 0 $retstring .= " A";
3383 }
3384 0 return $retstring;
3385  
3386 # normal position format
3387 } else {
3388 # convert to degrees and minutes
3389 0 my $isnorth = 1;
3390 0 ▶ if ($lat < 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat < 0) 0 0 0
3391 0 $lat = 0 - $lat;
3392 0 $isnorth = 0;
3393 }
3394 0 my $latdeg = int($lat);
3395 0 my $latmin = sprintf("%04d", ($lat - $latdeg) * 6000);
3396 0 my $latstring = sprintf("%02d%02d.%02d", $latdeg, substr($latmin, 0, 2), substr($latmin, 2, 2));
3397 0 ▶ if ($posambiguity > 0 || $posambiguity <= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posambiguity > 0 or $posambiguity <= 4) 0 0 0
Condition0%0/3
$posambiguity > 0 or $posambiguity <= 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$posambiguity > 0 or $posambiguity <= 4
$posambiguity > 0 $posambiguity <= 4
Truth table0%0/3
A or B
A= $posambiguity > 0B= $posambiguity <= 4
ABresult
1X1
011
000
3398 # position ambiguity
3399 0 ▶ if ($posambiguity <= 2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($posambiguity <= 2) { } 0 0 0
elsif ($posambiguity == 3) { } 0 0 0
elsif ($posambiguity == 4) { } 0 0 0
3400 # only minute decimals are blanked
3401 0 $latstring = substr($latstring, 0, 7 - $posambiguity) . " " x $posambiguity;
3402 } elsif ($posambiguity == 3) {
3403 0 $latstring = substr($latstring, 0, 3) . " . ";
3404 } elsif ($posambiguity == 4) {
3405 0 $latstring = substr($latstring, 0, 2) . " . ";
3406 }
3407 }
3408 0 ▶ if ($isnorth == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isnorth == 1) { } 0 0 0
3409 0 $latstring .= "N";
3410 } else {
3411 0 $latstring .= "S";
3412 }
3413 0 my $iseast = 1;
3414 0 ▶ if ($lon < 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lon < 0) 0 0 0
3415 0 $lon = 0 - $lon;
3416 0 $iseast = 0;
3417 }
3418 0 my $londeg = int($lon);
3419 0 my $lonmin = sprintf("%04d", ($lon - $londeg) * 6000);
3420 0 my $lonstring = sprintf("%03d%02d.%02d", $londeg, substr($lonmin, 0, 2), substr($lonmin, 2, 2));
3421 0 ▶ if ($posambiguity > 0 || $posambiguity <= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posambiguity > 0 or $posambiguity <= 4) 0 0 0
Condition0%0/3
$posambiguity > 0 or $posambiguity <= 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$posambiguity > 0 or $posambiguity <= 4
$posambiguity > 0 $posambiguity <= 4
Truth table0%0/3
A or B
A= $posambiguity > 0B= $posambiguity <= 4
ABresult
1X1
011
000
3422 # position ambiguity
3423 0 ▶ if ($posambiguity <= 2) {
Branch: 3 branches
BranchTrueFalseTotal
if ($posambiguity <= 2) { } 0 0 0
elsif ($posambiguity == 3) { } 0 0 0
elsif ($posambiguity == 4) { } 0 0 0
3424 # only minute decimals are blanked
3425 0 $lonstring = substr($lonstring, 0, 8 - $posambiguity) . " " x $posambiguity;
3426 } elsif ($posambiguity == 3) {
3427 0 $lonstring = substr($lonstring, 0, 4) . " . ";
3428 } elsif ($posambiguity == 4) {
3429 0 $lonstring = substr($lonstring, 0, 3) . " . ";
3430 }
3431 }
3432 0 ▶ if ($iseast == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iseast == 1) { } 0 0 0
3433 0 $lonstring .= "E";
3434 } else {
3435 0 $lonstring .= "W";
3436 }
3437 0 my $retstring = $latstring . $symboltable . $lonstring . $symbolcode;
3438 # add course/speed, if given
3439 0 ▶ if ($speed >= 0 && $course >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($speed >= 0 and $course >= 0) 0 0 0
Condition0%0/3
$speed >= 0 and $course >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$speed >= 0 and $course >= 0
$speed >= 0 $course >= 0
Truth table0%0/3
A and B
A= $speed >= 0B= $course >= 0
ABresult
0X0
100
111
3440 # convert speed to knots
3441 0 $speed = $speed / $knot_to_kmh;
3442 0 ▶ if ($speed > 999) {
Branch: 1 branch
BranchTrueFalseTotal
if ($speed > 999) 0 0 0
3443 0 $speed = 999; # maximum speed
3444 }
3445 0 ▶ if ($course > 360) {
Branch: 1 branch
BranchTrueFalseTotal
if ($course > 360) 0 0 0
3446 0 $course = 0; # unknown course
3447 }
3448 0 $retstring .= sprintf("%03d/%03d", $course, $speed);
3449 }
3450 0 return $retstring;
3451 }
3452 }
3453  
3454  
3455 22 1;
3456 __END__
↑ Summary IS.pm »