blib/lib/UAV/Pilot/WumpusRover/Driver.pm

filter: Statementstmt 29.8% 20 / 67 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 7 / 14 Podpod n/a totaltotal 30.6% 27 / 88 CC 5 SCAR 14.3
CC
5
Cov
27%
send_radio_output_packet
12.0
_init_connection
6.0
_process_ack
6.0
CRAP 4.2
« Build ↑ Summary blib/lib/UAV/Pilot/WumpusRover/Packet.pm »
1 # Copyright (c) 2014 Timm Murray
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24 package UAV::Pilot::WumpusRover::Driver;
25 6 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use UAV::Pilot::WumpusRover;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use UAV::Pilot::WumpusRover::PacketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 6 ▶use constant MAX_PACKET_QUEUE_LENGTH => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 has 'port' => (
36 is => 'ro',
37 isa => 'Int',
38 default => UAV::Pilot::WumpusRover::DEFAULT_PORT,
39 );
40 has 'host' => (
41 is => 'ro',
42 isa => 'Str',
43 );
44 has '_socket' => (
45 is => 'rw',
46 isa => 'IO::Socket::INET',
47 );
48 has '_ack_callback' => (
49 is => 'rw',
50 isa => 'CodeRef',
51 default => sub { sub {} },
52 writer => 'set_ack_callback',
53 );
54 has '_packet_queue' => (
55 is => 'ro',
56 isa => 'HashRef',
57 default => sub {
58 my %tie = ();
59 tie %tie, 'Tie::IxHash';
60 \%tie;
61 },
62 );
63  
64 with 'UAV::Pilot::Driver';
65 with 'UAV::Pilot::Logger';
66  
67  
68 sub connect
69 {
70 0 ▶ my ($self) = @_;
Subroutine connect: not called CC=1 SCAR=6.9
71 0 my $logger = $self->_logger;
72  
73 0 $logger->info( 'Connecting . . . ' );
74 0 $self->_init_connection;
75  
76 0 my $startup_request = UAV::Pilot::WumpusRover::PacketFactory->fresh_packet(
77 'RequestStartupMessage' );
78 # TODO find out what Ardupilot wants for these params
79 0 $startup_request->system_type( 0x00 );
80 0 $startup_request->system_id( 0x00 );
81 0 $logger->info( 'Sending RequestStartupMessage packet' );
82 0 $self->_send_packet( $startup_request );
83  
84 0 return 1;
85 }
86  
87  
88 sub send_radio_output_packet
89 {
90 0 ▶ my ($self, @channels) = @_;
Subroutine send_radio_output_packet: not called CC=3 SCAR=24.8
91 0 my $radio_packet = UAV::Pilot::WumpusRover::PacketFactory->fresh_packet(
92 'RadioOutputs' );
93  
94 0 foreach my $i (1..8) {
95 0 ▶ my $value = $channels[$i-1] // 0;
Condition0%0/2
$channels[$i - 1] // 0
l!l
00
MC/DC0%0/1
$channels[$i - 1] // 0
$channels[$i - 1]
Truth table0%0/2
A
A= $channels[$i - 1]
Aresult
11
00
96 0 my $packet_field = 'ch' . $i . '_out';
97 0 $radio_packet->$packet_field( $value );
98 }
99  
100 0 $self->_send_packet( $radio_packet );
101 0 return 1;
102 }
103  
104  
105 sub _init_connection
106 {
107 0 ▶ my ($self) = @_;
Subroutine _init_connection: not called CC=2 SCAR=17.9
108 0 my $logger = $self->_logger;
109  
110 0 $logger->info( 'Open UDP socket to ' . $self->host . ':' . $self->port );
111 0 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("Proto", "udp", "PeerHost", $self->host, "PeerPort", $self->port) or "UAV::Pilot::IOException"->throw({"error", 'Could not open socket: ' . $!}) 0 0 0
112 Proto => 'udp',
113 PeerHost => $self->host,
114 PeerPort => $self->port,
115 ) or UAV::Pilot::IOException->throw({
116 error => 'Could not open socket: ' . $!,
117 });
118 0 $logger->info( 'Done opening socket' );
119  
120 0 $self->_socket( $socket );
121 0 return 1;
122 }
123  
124 sub _packet_queue_size
125 {
126 0 ▶ my ($self) = @_;
Subroutine _packet_queue_size: not called CC=1 SCAR=6.9
127 0 return scalar keys %{ $self->_packet_queue };
128 }
129  
130 sub _send_packet
131 {
132 0 ▶ my ($self, $packet) = @_;
Subroutine _send_packet: not called CC=1 SCAR=6.9
133 0 $packet->make_checksum_clean;
134 0 $packet->write( $self->_socket );
135 0 $self->_add_to_packet_queue( $packet );
136 0 return 1;
137 }
138  
139 sub _process_ack
140 {
141 0 ▶ my ($self, $ack) = @_;
Subroutine _process_ack: not called CC=2 SCAR=17.9
142  
143 0 my $key = $ack->make_ack_packet_queue_key;
144 0 $self->_logger->info( "Processing ack packet with key [$key]" );
145  
146 0 my $orig_packet = delete $self->_packet_queue->{$key};
147 0 ▶ if( defined $orig_packet ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orig_packet) { } 0 0 0
148 0 $self->_ack_callback->( $orig_packet, $ack )
149 }
150 else {
151 0 $self->_logger->warn( "Received Ack packet for key $key, but couldn't"
152 . " find a matching packet" );
153 }
154  
155 0 return 1;
156 }
157  
158 sub _add_to_packet_queue
159 {
160 0 ▶ my ($self, $packet) = @_;
Subroutine _add_to_packet_queue: not called CC=1 SCAR=6.9
161  
162 0 my $key = $packet->make_packet_queue_map_key;
163 0 $self->_logger->info( "Adding packet to queue for key $key" );
164  
165 0 $self->_reduce_queue_length_to_max;
166 0 $self->_packet_queue->{$key} = $packet;
167  
168 0 return 1;
169 }
170  
171 sub _reduce_queue_length_to_max
172 {
173 my ($self) = @_;
174  
175 my @keys = keys $self->_packet_queue;
176 my $packets_removed = 0;
177 while( scalar(@keys) >= $self->MAX_PACKET_QUEUE_LENGTH ) {
178 my $next_key = shift @keys;
179 $self->_logger->warn( "Queue too large, removing packet $next_key" );
180  
181 delete $self->_packet_queue->{$next_key};
182 $packets_removed++;
183 }
184  
185 return $packets_removed;
186 }
187  
188  
189 no Moose;
190 __PACKAGE__->meta->make_immutable;
191 1;
192 __END__
193  
194  
195 =head1 NAME
196  
197 UAV::Pilot::WumpusRover::Driver
198  
199 =head1 SYNOPSIS
200  
201 use UAV::Pilot::WumpusRover::Driver;
202
203 my $driver = UAV::Pilot::WumpusRover::Driver->new({
204 host => '10.0.0.10',
205 });
206 $driver->connect;
207  
208 $driver->send_radio_output_packet( 90, 100 );
209  
210 =head1 DESCRIPTION
211  
212 Driver for the WumpusRover. Does the C<UAV::Pilot::Driver> role.
213  
214 This is not intended to be used directly. See
215 C<UAV::Pilot::WumpusRover::Control::Event> for the best way to control the
216 WumpusRover from client code.
217  
218 =cut
« Build ↑ Summary blib/lib/UAV/Pilot/WumpusRover/Packet.pm »