ArduinoBuilder/Monitor.pm

filter: Statementstmt 29.4% 25 / 85 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 8 / 12 Podpod 0.0% 0 / 1 totaltotal 25.9% 33 / 127 CC 15 SCAR 49.3
CC
15
Cov
22%
monitor
240.0
__ANON__
2.0
__ANON__
2.0
CRAP 139.0
« ArduinoBuilder/DepCheck.pm ↑ Summary ArduinoBuilder/Discovery.pm »
1 package App::ArduinoBuilder::Monitor;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use App::ArduinoBuilder::JsonTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::Any::Simple ':default';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub monitor {
Subroutine monitor: not called CC=15 SCAR=54.8
Pod undocumented
14 0 my ($config, $port) = @_;
15  
16 0 my $protocol = $port->get('upload.port.protocol');
17 0 my $board_port = $port->get('upload.port.address');
18  
19 # Some documentation for these properties is at:
20 # https://arduino.github.io/arduino-cli/0.32/platform-specification/#pluggable-monitor
21 0 my $cmd;
22 0 ▶ if ($config->exists("pluggable_monitor.required.${protocol}")) {
Branch: 3 branches
BranchTrueFalseTotal
if ($config->exists("pluggable_monitor.required.$protocol")) { } 0 0 0
elsif ($config->exists("pluggable_monitor.pattern.$protocol")) { } 0 0 0
elsif ($protocol eq 'serial') { } 0 0 0
23 0 my $tooldef = $config->get("pluggable_monitor.required.${protocol}");
24 0 ▶ if ($tooldef =~ m/^([^:]+):(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tooldef =~ /^([^:]+):(.*)$/) { } 0 0 0
25 # Note: for now we’re ignoring the vendor ID part.
26 0 my $tool = $2;
27 0 my $tool_key = "runtime.tools.${tool}.path";
28 0 ▶ if (!$config->exists($tool_key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->exists($tool_key)) 0 0 0
29 0 fatal "Pluggable monitor references unknown tool: ${tool}";
30 }
31 0 my $tool_dir = $config->get($tool_key);
32 0 $cmd = catfile($tool_dir, $tool);
33 0 ▶ $cmd .= '.exe' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
34 0 debug 'Using monitor tool required for protocol %s: %s (%s)', $protocol, $tool, $cmd;
35 } else {
36 0 fatal "Invalid pluggable discovery reference format: $tooldef";
37 }
38 } elsif ($config->exists("pluggable_monitor.pattern.${protocol}")) {
39 0 $cmd = $config->get("pluggable_monitor.pattern.${protocol}");
40 } elsif ($protocol eq 'serial') {
41 0 my $tool_key = "runtime.tools.serial-monitor.path";
42 0 ▶ if (!$config->exists($tool_key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->exists($tool_key)) 0 0 0
43 0 fatal "Built-in serial-monitor is not found, you may need to install the Arduino GUI and/or to set the builder.arduino.install_dir value in the project configuration";
44 }
45 0 my $tool_dir = $config->get($tool_key);
46 0 $cmd = catfile($tool_dir, 'serial-monitor');
47 0 ▶ $cmd .= '.exe' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
48 } else {
49 0 fatal "No pluggable monitor for protocol '$protocol'";
50 }
51  
52 # The protocol is described at:
53 # https://arduino.github.io/arduino-cli/0.32/pluggable-monitor-specification
54  
55 0 my $tool = App::ArduinoBuilder::JsonTool->new($cmd);
56 0 $tool->send("HELLO 1 ${App::ArduinoBuilder::TOOLS_USER_AGENT}\n");
57  
58 # TODO: we should have a way to check whether we are at least logging DEBUG
59 # messages and, if so, building the all_params and set_params variable with a
60 # single call to the tool to pass the result to the log calls.
61 0 ▶ my $get_all_params = sub { $tool->send("DESCRIBE\n")->{port_description}{configuration_parameters} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 ▶ my $get_set_params = sub { { map { $_->{label} => $_->{selected} } values %{$get_all_params->()} } };
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 debug "Monitor port configuration: %s", $get_set_params;
64 0 trace "Monitor port available options: %s", $get_all_params;
65
66 0 my $serv = IO::Socket::INET->new(Listen => 1, Timeout => 5);
67 0 my $serv_addr = sprintf "%s:%s", $serv->sockhost, $serv->sockport();
68 0 debug "Listening for the monitor on '${serv_addr}'";
69
70 # TODO: check OK response (here and in other commands too).
71 0 $tool->send("OPEN ${serv_addr} ${board_port}\n");
72  
73 0 my $client = $serv->accept();
74 0 $serv->close();
75  
76 0 my $select = IO::Select->new();
77 0 $select->add($client);
78 0 $select->add(\*STDIN);
79  
80 # For the RP2040 core, setting the baudrate reset the board, also asserting
81 # the DTR and RTS line at the same time make it enter its bootloader.
82 # TODO: test the behavior of other core and boards.
83 # TODO: make all this be configurable (and happen only with the serial
84 # monitor).
85 0 $tool->send("CONFIGURE baudrate 1200\n");
86 #$tool->send("CONFIGURE rts off\n");
87 #$tool->send("CONFIGURE dtr off\n");
88 #$tool->send("CONFIGURE rts on\n");
89 #$tool->send("CONFIGURE dtr on\n");
90 0 $tool->send("CONFIGURE baudrate 9600\n");
91
92 # TODO: look into what to do about UTF-D when communicating over the socket.
93 {
94 0 my $interrupted = 0;
95 0 ▶ local $SIG{INT} = sub { $interrupted = 1; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 ▶ while (!$interrupted && (my @ready = $select->can_read())) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $interrupted and my(@ready) = $select->can_read) 0 0 0
Condition0%0/3
not $interrupted and my(@ready) = $select->can_read
!ll&&!rl&&r
000
MC/DC0%0/2
not $interrupted and my(@ready) = $select->can_read
not $interrupted my(@ready) = $select->can_read
Truth table0%0/3
not(A) and B
A= not $interruptedB= my(@ready) = $select->can_read
ABresult
0X0
100
111
97 0 for my $s (@ready) {
98 0 ▶ if ($s == \*STDIN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s == \*STDIN) { } 0 0 0
elsif ($s == $client) { } 0 0 0
99 0 my $data = readline STDIN;
100 0 $client->send($data);
101 } elsif ($s == $client) {
102 0 my $ret = $client->recv(my $data, 255);
103 0 ▶ die "Can’t read tool data in monitor: $!" unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 0 0
104 0 print STDOUT $data;
105 }
106 }
107 }
108 }
109  
110 # The tool process was already killed by the SIGINT that we have received,
111 # there is no easy and portable way to detach the tool process from our
112 # control group (or from the terminal on Windows).
113 #$tool->send("CLOSE\n");
114 0 $client->close();
115 #$tool->send("QUIT\n");
116  
117 0 return;
118 }
119  
120 1 1;
« ArduinoBuilder/DepCheck.pm ↑ Summary ArduinoBuilder/Discovery.pm »