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

filter: Statementstmt 88.3% 76 / 86 Branchbran 58.3% 7 / 12 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.9% 10 / 13 Podpod 66.6% 4 / 6 totaltotal 80.4% 99 / 123 CC 13 SCAR 14.2
CC
13
Cov
83%
__ANON__
7.1
get_ordered_payload_value_bytes
6.2
make_checksum_clean
2.0
CRAP 4.1
« blib/lib/UAV/Pilot/WumpusRover/Driver.pm ↑ Summary blib/lib/UAV/Pilot/WumpusRover/Video/Mock.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::Packet;
25 7 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 7 ▶use constant _USE_DEFAULT_BUILDARGS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use constant _PACKET_QUEUE_MAP_KEY_SEPERATOR => '|';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33 7 has 'preamble' => (
34 is => 'rw',
35 isa => 'Int',
36 default => 0x3444,
37 );
38 7 has 'version' => (
39 is => 'rw',
40 isa => 'Int',
41 default => 0x00,
42 );
43 7 has 'checksum1' => (
44 is => 'ro',
45 isa => 'Int',
46 writer => '_set_checksum1',
47 );
48 7 has 'checksum2' => (
49 is => 'ro',
50 isa => 'Int',
51 writer => '_set_checksum2',
52 );
53 7 has '_is_checksum_clean' => (
54 is => 'rw',
55 isa => 'Bool',
56 default => 0,
57 );
58 7 requires 'payload_length';
59 7 requires 'message_id';
60 7 requires 'payload_fields';
61 7 requires 'payload_fields_length';
62  
63 7 with 'UAV::Pilot::Logger';
64  
65  
66 before 'BUILDARGS' => sub {
67 9 ▶ my ($class, $args) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.6
68 9 ▶ return $args if delete $args->{fresh};
Branch: 1 branch
BranchTrueFalseTotal
if delete $args->{"fresh"} 1 8 9
69 8 ▶ return $args unless $class->_USE_DEFAULT_BUILDARGS;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_USE_DEFAULT_BUILDARGS 0 8 8
70  
71 8 my $payload = delete $args->{payload};
72 8 my @payload = @$payload;
73  
74 8 my %payload_fields_length = %{ $class->payload_fields_length };
75 8 foreach my $field (@{ $class->payload_fields }) {
76 $class->_logger->warn(
77 "No entry for '$field' in $class->payload_fields_length"
78 44 ▶ ) unless exists $payload_fields_length{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $payload_fields_length{$field} 0 44 44
79 44 ▶ my $length = $payload_fields_length{$field} // 1;
Condition50%1/2
$payload_fields_length{$field} // 1
l!l
440
MC/DC0%0/1
$payload_fields_length{$field} // 1
$payload_fields_length{$field}
Truth table50%1/2
A
A= $payload_fields_length{$field}
Aresult
11
00
80  
81 44 my $value = 0;
82 44 foreach (1 .. $length) {
83 81 $value <<= 8;
84 81 $value |= shift @payload;
85 }
86  
87 44 $args->{$field} = $value;
88 }
89  
90 8 return $args;
91 7 };
92  
93  
94 sub write
95 {
96 8 ▶ my ($self, $fh) = @_;
Subroutine write: called CC=1 SCAR=0.0
Pod documented
97 8 $self->make_checksum_clean;
98  
99 8 my $packet = $self->make_byte_vector;
100 8 $fh->print( $packet );
101  
102 8 return 1;
103 }
104  
105 sub make_byte_vector
106 {
107 8 ▶ my ($self) = @_;
Subroutine make_byte_vector: called CC=1 SCAR=0.0
Pod documented
108 8 my $packet = pack 'n C*',
109 $self->preamble,
110 $self->payload_length,
111 $self->message_id,
112 $self->version,
113 $self->get_ordered_payload_value_bytes,
114 $self->checksum1,
115 $self->checksum2;
116 8 return $packet;
117 }
118  
119 sub get_ordered_payload_values
120 {
121 0 ▶ my ($self) = @_;
Subroutine get_ordered_payload_values: not called CC=1 SCAR=6.9
Pod undocumented
122 0 return map $self->$_, @{ $self->payload_fields };
123 }
124  
125 sub get_ordered_payload_value_bytes
126 {
127 16 ▶ my ($self) = @_;
Subroutine get_ordered_payload_value_bytes: called CC=6 SCAR=18.2
Pod documented
128 16 my @bytes;
129 16 my %payload_fields_length = %{ $self->payload_fields_length };
130  
131 16 foreach my $field (@{ $self->payload_fields }) {
132 $self->_logger->warn(
133 "No entry for '$field' in $self->payload_fields_length"
134 88 ▶ ) unless exists $payload_fields_length{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $payload_fields_length{$field} 0 88 88
135 88 ▶ my $length = $payload_fields_length{$field} // 1;
Condition50%1/2
$payload_fields_length{$field} // 1
l!l
880
MC/DC0%0/1
$payload_fields_length{$field} // 1
$payload_fields_length{$field}
Truth table50%1/2
A
A= $payload_fields_length{$field}
Aresult
11
00
136  
137 88 my $raw_value = $self->$field;
138 88 my @raw_bytes;
139 88 foreach (1 .. $length) {
140 162 ▶ if( defined $raw_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw_value) { } 162 0 162
141 162 my $value = $raw_value & 0xFF;
142 162 push @raw_bytes, $value;
143 162 $raw_value >>= 8;
144 }
145 else {
146 0 push @raw_bytes, 0;
147 }
148 }
149  
150 88 push @bytes, reverse @raw_bytes;
151 }
152  
153 16 return @bytes;
154 }
155  
156 sub _calc_checksum
157 {
158 8 ▶ my ($self) = @_;
Subroutine _calc_checksum: called CC=1 SCAR=0.0
159 8 my @data = (
160 $self->payload_length,
161 $self->message_id,
162 $self->version,
163 $self->get_ordered_payload_value_bytes,
164 );
165  
166 8 my ($check1, $check2) = UAV::Pilot->checksum_fletcher8( @data );
167 8 $self->_set_checksum1( $check1 );
168 8 $self->_set_checksum2( $check2 );
169 8 return 1;
170 }
171  
172 sub make_checksum_clean
173 {
174 8 ▶ my ($self) = @_;
Subroutine make_checksum_clean: called CC=2 SCAR=7.0
Pod documented
175 8 ▶ return 1 if $self->_is_checksum_clean;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_checksum_clean 0 8 8
176 8 $self->_calc_checksum;
177 8 $self->_is_checksum_clean( 1 );
178 8 return 1;
179 }
180  
181 sub make_packet_queue_map_key
182 {
183 0 ▶ my ($self) = @_;
Subroutine make_packet_queue_map_key: not called CC=1 SCAR=6.9
Pod undocumented
184 # NOTE: any changes here must be reflected in
185 # Packet::Ack::make_ack_packet_queue_key()
186 0 my $key = join( $self->_PACKET_QUEUE_MAP_KEY_SEPERATOR,
187 $self->message_id,
188 $self->checksum1,
189 $self->checksum2,
190 );
191 0 return $key;
192 }
193  
194  
195 sub _make_checksum_unclean
196 {
197 0 ▶ my ($self) = @_;
Subroutine _make_checksum_unclean: not called CC=1 SCAR=6.9
198 0 $self->_is_checksum_clean( 0 );
199 0 return 1;
200 }
201  
202  
203 7 1;
204 __END__
205  
206 =head1 NAME
207  
208 UAV::Pilot::WumpusRover::Packet
209  
210 =head1 DESCRIPTION
211  
212 Role for WumpusRover packets. These are based on the ArduPilot protocol
213 packets, as described here:
214  
215 L<http://code.google.com/p/ardupilot-mega/wiki/Protocol>
216  
217 No attempts have yet been made to test this against an existing ArduPilot
218 implmentation, but it should be close.
219  
220 Do not create Packets directly. Instead, use
221 C<UAV::Pilot::WumpusRover::PacketFactory>.
222  
223 Does the C<UAV::Pilot::Logger> role.
224  
225 =head1 METHODS
226  
227 =head2 write
228  
229 write( $fh )
230  
231 Writes the packet to the given filehandle.
232  
233 =head2 make_checksum_clean
234  
235 Recalculates the checksum based on current field values.
236  
237 =head2 make_byte_vector
238  
239 Returns the packet fields in a single scalar full of bytes.
240  
241 =head2 get_ordered_payload_vales
242  
243 Returns the packet field values in the order they appear in C<payload_fields()>.
244  
245 =head2 get_ordered_payload_value_bytes
246  
247 Returns a byte array of all the packet fields in the order they appear in
248 C<payload_fields()>.
249  
250 =head1 make_packet_queue_map_key
251  
252 Creates a unique key for this packet.
253  
254 =head1 ATTRIBUTES
255  
256 =head2 preamble
257  
258 Fixed bytes that start every packet
259  
260 =head2 version
261  
262 Protocol version
263  
264 =head2 checksum1
265  
266 First checksum byte
267  
268 =head2 checksum2
269  
270 Second checksum byte
271  
272 =head1 REQUIRED METHODS/ATTRIBUTES
273  
274 =head2 message_id
275  
276 ID for this type of message
277  
278 =head2 payload_fields
279  
280 Arrayref. A list of field names in the order they appear in the packet.
281  
282 =head2 payload_length
283  
284 Hashref. Keys match to an entry in C<payload_fields>. Values are the length
285 in bytes of that field.
286  
287 =cut
« blib/lib/UAV/Pilot/WumpusRover/Driver.pm ↑ Summary blib/lib/UAV/Pilot/WumpusRover/Video/Mock.pm »