blib/lib/Ham/APRS/DeviceID.pm

filter: Statementstmt 78.7% 104 / 132 Branchbran 62.5% 40 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 76.9% 10 / 13 Podpod 100% 2 / 2 totaltotal 73.9% 156 / 211 SCAR 41.4
CC
37
Cov
73%
identify
23.5
_a_err
20.0
_a_warn
20.0
CRAP 62.6
↑ Summary
1  
2 package Ham::APRS::DeviceID;
3  
4 =head1 NAME
5  
6 Ham::APRS::DeviceID - APRS device identifier
7  
8 =head1 SYNOPSIS
9  
10 use Ham::APRS::FAP qw(parseaprs);
11 use Ham::APRS::DeviceID;
12 use Data::Dumper;
13
14 my $aprspacket = 'OH2RDP>APZMDR,OH2RDG*,WIDE:!6028.51N/02505.68E#PHG7220/RELAY,WIDE, OH2AP Jarvenpaa';
15 my %packet;
16 my $retval = parseaprs($aprspacket, \%packet);
17 if ($retval == 1) {
18 Ham::APRS::DeviceID::identify(\%packet);
19
20 if (defined $packet{'deviceid'}) {
21 print Dumper($packet{'deviceid'});
22 }
23 }
24  
25 =head1 ABSTRACT
26  
27 This module attempts to identify the manufacturer, model and
28 software version of an APRS transmitter. It looks at details found
29 in the parsed APRS packet (as provided by Ham::APRS::FAP) and updates
30 the hash with the identification information, if possible.
31  
32 The module comes with a device identification database, which is
33 simply a copy of the YAML master file maintained separately
34 at: L<https://github.com/hessu/aprs-deviceid>
35  
36 =head1 DESCRIPTION
37  
38 Unless a debugging mode is enabled, all errors and warnings are reported
39 through the API (as opposed to printing on STDERR or STDOUT), so that
40 they can be reported nicely on the user interface of an application.
41  
42 This module requires a reasonably recent L<Ham::APRS::FAP> module,
43 L<YAML::Tiny> to load the device identification database and
44 L<File::ShareDir> for finding it.
45  
46 =head1 EXPORT
47  
48 None by default.
49  
50 =head1 FUNCTION REFERENCE
51  
52 =cut
53  
54 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 #use Data::Dumper;
58  
59 4 require Exporter;
60 4 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 4 ▶use File::ShareDir ':ALL';
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 4 our @ISA = qw(Exporter);
64  
65 # Items to export into callers namespace by default. Note: do not export
66 # names by default without a very good reason. Use EXPORT_OK instead.
67 # Do not simply export all your public functions/methods/constants.
68  
69 # This allows declaration use Ham::APRS::FAP ':all';
70 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
71 # will save memory.
72 ##our %EXPORT_TAGS = (
73 ## 'all' => [ qw(
74 ##
75 ## ) ],
76 ##);
77  
78 4 our @EXPORT_OK = (
79 ## @{ $EXPORT_TAGS{'all'} },
80 '&identify',
81 );
82  
83 ##our @EXPORT = qw(
84 ##
85 ##);
86  
87 4 our $VERSION = '2.01';
88  
89 # Preloaded methods go here.
90  
91 # no debugging by default
92 4 my $debug = 0;
93  
94 4 my %result_messages = (
95 'unknown' => 'Unsupported packet format',
96 'no_dstcall' => 'Packet has no destination callsign',
97 'no_format' => 'Packet has no defined format',
98 'mice_no_comment' => 'Mic-e packet with no comment defined',
99 'mice_no_deviceid' => 'Mic-e packet with no device identifier in comment',
100 'no_id' => 'No device identification found',
101 );
102  
103 # these functions are used to report warnings and parser errors
104 # from the module
105  
106 sub _a_err($$;$)
107 {
108 0 ▶ my ($rethash, $errcode, $val) = @_;
Subroutine _a_err: not called CC=4 SCAR=30.0
109
110 0 $rethash->{'deviceid_resultcode'} = $errcode;
111 $rethash->{'deviceid_resultmsg'}
112 = defined $result_messages{$errcode}
113 0 ▶ ? $result_messages{$errcode} : $errcode;
Branch: 1 branch
BranchTrueFalseTotal
defined $result_messages{$errcode} ? : 0 0 0
114
115 0 ▶ $rethash->{'deviceid_resultmsg'} .= ': ' . $val if (defined $val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
116
117 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
118 0 warn "Ham::APRS::DeviceID ERROR $errcode: " . $rethash->{'deviceid_resultmsg'} . "\n";
119 }
120
121 0 return 0;
122 }
123  
124 sub _a_warn($$;$)
125 {
126 0 ▶ my ($rethash, $errcode, $val) = @_;
Subroutine _a_warn: not called CC=4 SCAR=30.0
127
128 0 push @{ $rethash->{'deviceid_warncodes'} }, $errcode;
129
130 0 ▶ if ($debug > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
131 warn "Ham::APRS::DeviceID WARNING $errcode: "
132 . (defined $result_messages{$errcode}
133 0 ▶ ? $result_messages{$errcode} : $errcode)
Branch: 2 branches
BranchTrueFalseTotal
defined $result_messages{$errcode} ? : 0 0 0
defined $val ? : 0 0 0
134 . (defined $val ? ": $val" : '')
135 . "\n";
136 }
137
138 0 return 0;
139 }
140  
141  
142 =over
143  
144 =item debug($enable)
145  
146 Enables (debug(1)) or disables (debug(0)) debugging.
147  
148 When debugging is enabled, warnings and errors are emitted using the warn() function,
149 which will normally result in them being printed on STDERR. Succesfully
150 printed packets will be also printed on STDOUT in a human-readable
151 format.
152  
153 When debugging is disabled, nothing will be printed on STDOUT or STDERR -
154 all errors and parsing results need to be collected from the returned
155 hash reference.
156  
157 =back
158  
159 =cut
160  
161 sub debug($)
162 {
163 0 ▶ my $dval = shift @_;
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
164 0 ▶ if ($dval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dval) { } 0 0 0
165 0 $debug = 1;
166 } else {
167 0 $debug = 0;
168 }
169 }
170  
171 #
172 # Prebaked responses for "legacy" devices, including the VX-8 which has a
173 # space (0x20) character as the last byte, which commonly gets eaten by
174 # UI-View
175 #
176 4 my %response = (
177 'd7' => {
178 'vendor' => 'Kenwood',
179 'model' => 'TH-D7',
180 'class' => 'ht',
181 'messaging' => 1,
182 },
183 'd72' => {
184 'vendor' => 'Kenwood',
185 'model' => 'TH-D72',
186 'class' => 'ht',
187 'messaging' => 1,
188 },
189 'd700' => {
190 'vendor' => 'Kenwood',
191 'model' => 'TM-D700',
192 'class' => 'rig',
193 'messaging' => 1,
194 },
195 'd710' => {
196 'vendor' => 'Kenwood',
197 'model' => 'TM-D710',
198 'class' => 'rig',
199 'messaging' => 1,
200 },
201 'vx8' => {
202 'vendor' => 'Yaesu',
203 'model' => 'VX-8',
204 'class' => 'ht',
205 'messaging' => 1,
206 },
207 'unknown' => {
208 'vendor' => 'Unknown',
209 'model' => 'Other Mic-E',
210 }
211 );
212  
213 4 my %mice_codes;
214 my %fixed_dstcalls;
215 4 my @dstcall_regexps;
216  
217 4 my %regexp_prefix;
218  
219 #
220 # init: load YAML definitions
221 #
222  
223 sub _load_tocalls(@)
224 {
225 4 ▶ my(@tcl) = @_;
Subroutine _load_tocalls: called CC=4 SCAR=13.9
226
227 4 foreach my $t (@tcl) {
228 476 my $tocall = $t->{'tocall'};
229 476 delete $t->{'tocall'};
230 476 ▶ if ($tocall =~ /^[A-Z0-9]+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tocall =~ /^[A-Z0-9]+$/) { } 132 344 476
elsif ($tocall =~ /^([A-Z0-9]+)([n\?\*]+)([A-Z0-9]*)$/) { } 344 0 344
231 132 $fixed_dstcalls{$tocall} = $t;
232 } elsif ($tocall =~ /^([A-Z0-9]+)([n\?\*]+)([A-Z0-9]*)$/) {
233 344 my $prefix = $1;
234 344 my $r = $2; # glob (n for numbers, ?/* for single/multi)
235 344 my $suffix = $3;
236 344 $r =~ s/n/\\d/g;
237 344 $r =~ s/\?/./g;
238 344 $r =~ s/\*/.*/g;
239 344 $r = $prefix . '(' . $r . $suffix . ')';
240 344 push @dstcall_regexps, [ $r, $t ];
241 } else {
242 0 die "tocall '$tocall' too hard to parse";
243 }
244 }
245
246 }
247  
248 sub _load_mice(@)
249 {
250 4 ▶ my(@tcl) = @_;
Subroutine _load_mice: called CC=2 SCAR=6.9
251
252 4 foreach my $t (@tcl) {
253 36 my $suffix = $t->{'suffix'};
254 36 delete $t->{'suffix'};
255 36 $mice_codes{$suffix} = $t;
256 }
257
258 }
259  
260 sub _load()
261 {
262 4 ▶ my $src = dist_file('Ham-APRS-DeviceID', 'tocalls.yaml');
Subroutine _load: called CC=2 SCAR=7.0
263 4 my $yaml = YAML::Tiny->new;
264 4 my $c = YAML::Tiny->read($src);
265 4 ▶ if (!defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 0 4 4
266 0 die "Failed to read in $src: " . YAML::Tiny->errstr . "\n";
267 }
268
269 # get the first document of YAML
270 4 $c = $c->[0];
271
272 4 _load_tocalls(@{ $c->{'tocalls'} });
273 4 _load_mice(@{ $c->{'mice'} });
274  
275 }
276  
277 #
278 # init code: compile the regular expressions to speed up matching
279 #
280  
281 sub _compile_regexps()
282 {
283 4 ▶ for (my $i = 0; $i <= $#dstcall_regexps; $i++) {
Subroutine _compile_regexps: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#dstcall_regexps) 344 4 348
284 344 my $dmatch = $dstcall_regexps[$i];
285 344 my($regexp, $response) = @$dmatch;
286
287 344 my $compiled = qr/^$regexp$/;
288 344 $dstcall_regexps[$i] = [ $regexp, $response, $compiled ];
289 }
290 }
291  
292 #
293 # init: optimize regexps with an initial hash lookup
294 #
295  
296 sub _optimize_regexps()
297 {
298 4 ▶ my @left;
Subroutine _optimize_regexps: called CC=4 SCAR=14.0
299
300 4 ▶ for (my $i = 0; $i <= $#dstcall_regexps; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#dstcall_regexps) 344 4 348
301 344 my $dmatch = $dstcall_regexps[$i];
302 344 my($regexp, $response, $compiled) = @$dmatch;
303
304 344 ▶ if ($regexp =~ /^([^\(]{2,5})(\(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regexp =~ /^([^\(]{2,5})(\(.*)$/) { } 344 0 344
305 344 ▶ if (!defined $regexp_prefix{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $regexp_prefix{$1}) { } 340 4 344
306 340 $regexp_prefix{$1} = [ $dmatch ];
307 } else {
308 4 push @{ $regexp_prefix{$1} }, $dmatch;
309 }
310 } else {
311 0 push @left, $dmatch;
312 0 warn "optimize: leaving $regexp over\n";
313 }
314 }
315
316 4 @dstcall_regexps = @left;
317 }
318  
319 4 _load();
320 4 _compile_regexps();
321 4 _optimize_regexps();
322  
323 =over
324  
325 =item identify($hashref)
326  
327 Tries to identify the device.
328  
329 =back
330  
331 =cut
332  
333 sub identify($)
334 {
335 21 ▶ my($p) = @_;
Subroutine identify: called CC=21 SCAR=31.6
Pod documented
336
337 21 $p->{'deviceid_resultcode'} = '';
338
339 21 ▶ return _a_err($p, 'no_format') if (!defined $p->{'format'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p->{"format"} 0 21 21
340 21 ▶ return _a_err($p, 'no_dstcall') if (!defined $p->{'dstcallsign'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p->{"dstcallsign"} 0 21 21
341
342 21 ▶ if ($p->{'format'} eq 'mice') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"format"} eq "mice") 17 4 21
343 #warn Dumper($p);
344 #warn "comment: " . $p->{'comment'} . "\n";
345 17 ▶ if (!defined $p->{'comment'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p->{"comment"}) 0 17 17
346 0 return _a_err($p, 'mice_no_comment');
347 }
348 17 ▶ if ($p->{'comment'} =~ s/^>(.*)=$/$1/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($p->{'comment'} =~ s/^>(.*)=$/$1/) { } 2 15 17
elsif ($p->{'comment'} =~ s/^>//) { } 2 13 15
elsif ($p->{'comment'} =~ s/^\](.*)=$/$1/) { } 1 12 13
elsif ($p->{'comment'} =~ s/^\]//) { } 2 10 12
elsif ($p->{'comment'} =~ s/^`(.*)_\s*$/$1/) { } 3 7 10
elsif ($p->{'comment'} =~ /^([`\'])(.*)(..)$/) { } 7 0 7
349 2 $p->{'deviceid'} = $response{'d72'};
350 } elsif ($p->{'comment'} =~ s/^>//) {
351 2 $p->{'deviceid'} = $response{'d7'};
352 } elsif ($p->{'comment'} =~ s/^\](.*)=$/$1/) {
353 1 $p->{'deviceid'} = $response{'d710'};
354 } elsif ($p->{'comment'} =~ s/^\]//) {
355 2 $p->{'deviceid'} = $response{'d700'};
356 } elsif ($p->{'comment'} =~ s/^`(.*)_\s*$/$1/) {
357 # vx-8 has a space as the last character, which commonly gets eaten by ui-view,
358 # so handle it with a relaxed regexp
359 3 $p->{'deviceid'} = $response{'vx8'};
360 } elsif ($p->{'comment'} =~ /^([`\'])(.*)(..)$/) {
361 7 my($b, $s, $code) = ($1, $2, $3);
362
363 7 ▶ if (defined $mice_codes{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mice_codes{$code}) { } 7 0 7
364 7 $p->{'deviceid'} = $mice_codes{$code};
365 7 $p->{'comment'} = $s;
366 } else {
367 0 $p->{'deviceid'} = $response{'unknown'};
368 0 $p->{'comment'} = $s . $code;
369 }
370 7 ▶ $p->{'messaging'} = 1 if ($b eq '`');
Branch: 1 branch
BranchTrueFalseTotal
if $b eq "`" 4 3 7
371 }
372
373 17 ▶ if ($p->{'deviceid'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"deviceid"}) 17 0 17
374 17 ▶ $p->{'messaging'} = 1 if ($p->{'deviceid'}->{'messaging'});
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"deviceid"}{"messaging"} 10 7 17
375 17 return 1;
376 }
377
378 0 return _a_err($p, 'mice_no_deviceid');
379 }
380
381 4 ▶ if (defined $fixed_dstcalls{$p->{'dstcallsign'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fixed_dstcalls{$p->{"dstcallsign"}}) 2 2 4
382 2 $p->{'deviceid'} = $fixed_dstcalls{$p->{'dstcallsign'}};
383 2 return 1;
384 }
385
386 2 foreach my $len (4, 3, 5, 2) {
387 5 my $prefix = substr($p->{'dstcallsign'}, 0, $len);
388 5 ▶ if (defined $regexp_prefix{$prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regexp_prefix{$prefix}) 2 3 5
389 2 foreach my $dmatch (@{ $regexp_prefix{$prefix} }) {
390 3 my($regexp, $response, $compiled) = @$dmatch;
391 #warn "trying '$regexp' against " . $p->{'dstcallsign'} . "\n";
392 3 ▶ if ($p->{'dstcallsign'} =~ $compiled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"dstcallsign"} =~ /$compiled/) 2 1 3
393 #warn "match!\n";
394 2 my %copy = %{ $response };
395 2 $p->{'deviceid'} = \%copy;
396
397 2 ▶ if ($response->{'version_regexp'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"version_regexp"}) 0 2 2
398 #warn "version_regexp set: $1 from " . $p->{'dstcallsign'} . " using " . $regexp . "\n";
399 0 $p->{'deviceid'}->{'version'} = $1;
400 0 delete $p->{'deviceid'}->{'version_regexp'};
401 }
402
403 2 return 1;
404 }
405 }
406 }
407 }
408
409 0 return _a_err($p, 'no_id');
410 }
411  
412  
413 4 1;
414 __END__
↑ Summary