| 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
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | return 0 if $res->{message} ne 'OK'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | fatal "Pluggable discovery returned an error for ${toolname}: %s", $res if $res->{error} && $res->{error} eq 'true'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->{"error"} and $res->{"error"} eq "true"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if (fc($p->{properties}{$k} // '') eq fc($v)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$p->{"properties"}{$k} // ""
MC/DC0%0/1
$p->{"properties"}{$k} // ""
$p->{"properties"}{$k}
Truth table0%0/2
A
A= $p->{"properties"}{$k}
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||