ArduinoBuilder/Discovery.pm

filter: Statementstmt 27.2% 30 / 110 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 62.5% 10 / 16 Podpod 0.0% 0 / 1 totaltotal 23.3% 40 / 171 CC 24 SCAR 45.9
CC
24
Cov
20%
discover
240.0
_run_one_discovery
30.0
_test_command_response
12.0
CRAP 98.6
« ArduinoBuilder/Monitor.pm ↑ Summary ArduinoBuilder/Builder.pm »
1 package App::ArduinoBuilder::Discovery;
2  
3 1 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use App::ArduinoBuilder::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::ArduinoBuilder::JsonTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::Util 'all', 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Log::Any::Simple ':default';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes 'usleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 0 ▶sub _test_command_response {
Subroutine _test_command_response: not called CC=3 SCAR=24.8
17 0 my ($res, $cmd) = @_;
18 0 ▶ return 0 if $res->{eventType} ne $cmd;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"eventType"} ne $cmd 0 0 0
19 0 ▶ return 0 if $res->{message} ne 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"message"} ne "OK" 0 0 0
20 0 return 1;
21 }
22  
23 0 ▶sub _fail_invalid_response {
Subroutine _fail_invalid_response: not called CC=2 SCAR=17.9
24 0 my ($res, $cmd, $tool) = @_;
25 0 ▶ return if _test_command_response($res, $cmd);
Branch: 1 branch
BranchTrueFalseTotal
if _test_command_response($res, $cmd) 0 0 0
26 0 fatal "Invalid response for ${tool}:\n%s", $res;
27 }
28  
29 # Specification of the discovery protocol.
30 # https://arduino.github.io/arduino-cli/0.32/pluggable-discovery-specification/
31 0 ▶sub _run_one_discovery {
Subroutine _run_one_discovery: not called CC=5 SCAR=34.0
32 0 my ($toolname, $cmd) = @_;
33  
34 0 my $tool = App::ArduinoBuilder::JsonTool->new($cmd);
35 # TODO: we could test that the tool accept to speak the protocol version 1.
36 # But, as there are no other versions for know, it’s somehow an overkill.
37 0 _fail_invalid_response($tool->send("HELLO 1 ${App::ArduinoBuilder::TOOLS_USER_AGENT}\n"), 'hello', $toolname);
38 0 _fail_invalid_response($tool->send("START\n"), 'start', $toolname);
39 # A small delay here, otherwise the LIST command returns empty results :(
40 0 usleep(50000);
41 0 my $res = $tool->send("LIST\n");
42 0 ▶ if (!@{$res->{ports}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$res->{"ports"};}) 0 0 0
43 # mitigation: if the list was empty initially, let’s try again, maybe the tool has initialized now.
44 0 trace "Retrying due to empty list";
45 0 $res = $tool->send("LIST\n");
46 }
47 0 _fail_invalid_response($tool->send("QUIT\n"), 'quit', $toolname);
48  
49 0 ▶ fatal "Invalid pluggable discovery data (eventType ne 'list') for ${toolname}: %s", $res unless $res->{eventType} eq 'list';
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"eventType"} eq "list" 0 0 0
50 0 ▶ fatal "Pluggable discovery returned an error for ${toolname}: %s", $res if $res->{error} && $res->{error} eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"error"} and $res->{"error"} eq "true" 0 0 0
Condition0%0/3
$res->{"error"} and $res->{"error"} eq "true"
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{"error"} and $res->{"error"} eq "true"
$res->{"error"} $res->{"error"} eq "true"
Truth table0%0/3
A and B
A= $res->{"error"}B= $res->{"error"} eq "true"
ABresult
0X0
100
111
51 0 debug "Pluggable discovery for ${toolname} found: %s", $res->{ports};
52 0 return @{$res->{ports}};
53 }
54  
55 # See: https://arduino.github.io/arduino-cli/0.32/platform-specification/#properties-from-pluggable-discovery
56 0 ▶sub _port_to_config {
Subroutine _port_to_config: not called CC=3 SCAR=24.8
57 0 my ($config, $port) = @_;
58  
59 0 my $port_config = App::ArduinoBuilder::Config->new(base => $config);
60 0 $port_config->parse_perl($port, prefix => 'upload.port');
61 0 ▶ if ($port_config->exists('upload.port.address')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port_config->exists("upload.port.address")) 0 0 0
62 0 $port_config->set('serial.port' => $port_config->get('upload.port.address'));
63 }
64 0 ▶ if ($port_config->get('upload.port.protocol', default => '') eq 'serial') {
Branch: 1 branch
BranchTrueFalseTotal
if ($port_config->get("upload.port.protocol", "default", "") eq "serial") 0 0 0
65 0 $port_config->set('serial.port.file' => $port_config->get('upload.port.label'));
66 }
67  
68 # Case folded versions, later used to compare to the content of the --port
69 # option.
70 0 $port_config->set('upload.port.lc_label', lc($port_config->get('upload.port.label')));
71 0 $port_config->set('upload.port.lc_address', lc($port_config->get('upload.port.address')));
72  
73 0 return $port_config;
74 }
75  
76 # For _some_ documentation, see:
77 # https://arduino.github.io/arduino-cli/0.32/platform-specification/#pluggable-discovery
78 0 ▶sub discover {
Subroutine discover: not called CC=15 SCAR=54.8
Pod undocumented
79 0 my ($config) = @_;
80 0 my $discovery_config = $config->filter('pluggable_discovery');
81 0 ▶ if ($discovery_config->filter('required')->empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discovery_config->filter("required")->empty) 0 0 0
82 0 $discovery_config->set('required.0' => 'builtin:serial-discovery');
83 0 $discovery_config->set('required.1' => 'builtin:mdns-discovery');
84 }
85  
86 # There is no real documentation of how to use the 'VENDOR_ID:DISCOVERY_NAME'
87 # references. For now, we just assume that there is a tool with that discovery
88 # name (we ignore the vendor ID) and we expect a binary of the same name in
89 # the tool directory (this is the format used by the builtin tools).
90 # In general, that syntax could be used to automaticaly download the required
91 # tools if they are not already present.
92  
93 0 my @discovered_ports;
94 0 for my $k ($discovery_config->keys()) {
95 0 ▶ if ($k =~ m/^(.*)\.pattern$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ /^(.*)\.pattern$/u) { } 0 0 0
elsif ($k =~ /required(?:\.\d+)?/u) { } 0 0 0
96 0 push @discovered_ports, _run_one_discovery($1, $discovery_config->get($k));
97 } elsif ($k =~ m/required(?:\.\d+)?/) {
98 0 ▶ if ($discovery_config->get($k) =~ m/^([^:]+):(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discovery_config->get($k) =~ /^([^:]+):(.*)$/u) { } 0 0 0
99 # Note: for now we’re ignoring the vendor ID part.
100 0 my $tool = $2;
101 0 my $tool_key = "runtime.tools.${tool}.path";
102 0 ▶ if (!$config->exists($tool_key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->exists($tool_key)) 0 0 0
103 0 error "Pluggable discovery references unknown tool: ${tool}";
104 0 next;
105 }
106 0 my $tool_dir = $config->get($tool_key);
107 0 my $cmd = catfile($tool_dir, $tool);
108 0 ▶ $cmd .= '.exe' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
109 0 push @discovered_ports, _run_one_discovery($tool, $cmd);
110 } else {
111 0 error "Invalid pluggable discovery reference format: %s", $discovery_config->get($k);
112 }
113 } else {
114 0 error "Invalid pluggable discovery key: %s => %s", $k, $discovery_config->get($k);
115 }
116 }
117  
118 0 ▶ return () unless @discovered_ports;
Branch: 1 branch
BranchTrueFalseTotal
unless @discovered_ports 0 0 0
119  
120 # Something that we don’t implement is that the discovery could be used to
121 # automatically detect the board being used, as well as some of its "menu"
122 # properties.
123 # However, the whole thing is very buggy. For example the Feather 2040 board
124 # will have a match on the vid property but not the pid property, so we accept
125 # partial match (theoretically we should accept full matches).
126  
127 # Some boards have the upload ports properties defined without the
128 # upload_ports prefix (in addition to also having them with the prefix), we
129 # are ignoring that.
130 0 my $defined_ports = $config->filter('upload_port');
131 0 my @property_sets_keys = $defined_ports->top_level_keys();
132 0 my @property_sets;
133 0 ▶ if (all { m/^\d+$/ } @property_sets_keys) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { /^\d+$/u; } , @property_sets_keys)) { } 0 0 0
134 0 @property_sets = map { { $defined_ports->filter($_)->get_hash() } } @property_sets_keys;
135 } else {
136 0 @property_sets = { $defined_ports->get_hash() };
137 }
138
139 # We compute a "match strength" for all discovered ports, corresponding to how
140 # many properties (from a single defined set) the port matches.
141 0 my $all_max_match = 0;
142 0 for my $p (@discovered_ports) {
143 0 my $max_match = 0;
144 0 for my $s (@property_sets) {
145 0 my $match = 0;
146 0 ▶ while (my ($k, $v) = each %{$s}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$s;}) 0 0 0
147 0 ▶ if (fc($p->{properties}{$k} // '') eq fc($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fc($p->{"properties"}{$k} // "") eq fc $v) 0 0 0
Condition0%0/2
$p->{"properties"}{$k} // ""
l!l
00
MC/DC0%0/1
$p->{"properties"}{$k} // ""
$p->{"properties"}{$k}
Truth table0%0/2
A
A= $p->{"properties"}{$k}
Aresult
11
00
148 0 $match++;
149 }
150 }
151 0 $max_match = max($max_match, $match);
152 }
153 0 $all_max_match = max($all_max_match, $max_match);
154 0 $p->{upload_port_match_strength} = $max_match;
155 0 trace "Port %s: match strength == %d", $p->{label}, $max_match;
156 }
157  
158 # Now, we keep all the found ports that have the highest
159 0 @discovered_ports = grep { $_->{upload_port_match_strength} == $all_max_match } @discovered_ports;
160  
161 0 return map { _port_to_config($config, $_) } @discovered_ports;
162 }
163  
164 1 1;
« ArduinoBuilder/Monitor.pm ↑ Summary ArduinoBuilder/Builder.pm »