« blib/lib/UAV/Pilot/WumpusRover/Video/Mock.pm
↑ Summary
blib/lib/UAV/Pilot/WumpusRover/Control.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::PacketFactory; | ||||||||||||||||||||||||||||
| 25 | 7 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 7 | ▶ | use UAV::Pilot; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 7 | ▶ | use UAV::Pilot::Exceptions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::Ack; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::Heartbeat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::RequestStartupMessage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::StartupMessage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::RadioTrims; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::RadioMins; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::RadioMaxes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 7 | ▶ | use UAV::Pilot::WumpusRover::Packet::RadioOutputs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 7 | ▶ | use constant PACKET_CLASS_PREFIX => 'UAV::Pilot::WumpusRover::Packet::'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | use constant PREAMBLE => 0x3444; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 7 | use constant MESSAGE_ID_CLASS_MAP => { | |||||||||||||||||||||||||||
| 43 | 0x00 => 'Ack', | ||||||||||||||||||||||||||||
| 44 | 0x01 => 'Heartbeat', | ||||||||||||||||||||||||||||
| 45 | 0x07 => 'RequestStartupMessage', | ||||||||||||||||||||||||||||
| 46 | 0x08 => 'StartupMessage', | ||||||||||||||||||||||||||||
| 47 | 0x50 => 'RadioTrims', | ||||||||||||||||||||||||||||
| 48 | 0x51 => 'RadioMins', | ||||||||||||||||||||||||||||
| 49 | 0x52 => 'RadioMaxes', | ||||||||||||||||||||||||||||
| 50 | 0x53 => 'RadioOutputs', | ||||||||||||||||||||||||||||
| 51 | 7 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub read_packet | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 10 | ▶ | my ($self, $packet) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_packet: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 10 | my @packet = unpack 'C*', $packet; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 10 | my $preamble = ($packet[0] << 8 ) | $packet[1]; | |||||||||||||||||||||||||||
| 60 | 10 | ▶ | UAV::Pilot::ArdupilotPacketException::BadHeader->throw({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | got_header => $preamble, | ||||||||||||||||||||||||||||
| 62 | }) if $self->PREAMBLE != $preamble; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 9 | my $payload_length = $packet[2]; | |||||||||||||||||||||||||||
| 65 | 9 | my $message_id = $packet[3]; | |||||||||||||||||||||||||||
| 66 | 9 | my $version = $packet[4]; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 9 | my $last_payload_i = 4 + $payload_length; | |||||||||||||||||||||||||||
| 69 | 9 | my @payload = @packet[5..$last_payload_i]; | |||||||||||||||||||||||||||
| 70 | 9 | my $checksum1 = $packet[$last_payload_i + 1]; | |||||||||||||||||||||||||||
| 71 | 9 | my $checksum2 = $packet[$last_payload_i + 2]; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 9 | my ($expect_checksum1, $expect_checksum2) = UAV::Pilot->checksum_fletcher8( | |||||||||||||||||||||||||||
| 74 | $payload_length, $message_id, $version, @payload ); | ||||||||||||||||||||||||||||
| 75 | 9 | ▶ | UAV::Pilot::ArdupilotPacketException::BadChecksum->throw({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$expect_checksum1 != $checksum1 or $expect_checksum2 != $checksum2
MC/DC50%1/2
$expect_checksum1 != $checksum1 or $expect_checksum2 != $checksum2
$expect_checksum1 != $checksum1
$expect_checksum2 != $checksum2
Truth table66%2/3
A or B
A= $expect_checksum1 != $checksum1B= $expect_checksum2 != $checksum2
| |||||||||||||||||||||||||||||
| 76 | got_checksum1 => $checksum1, | ||||||||||||||||||||||||||||
| 77 | got_checksum2 => $checksum2, | ||||||||||||||||||||||||||||
| 78 | expected_checksum1 => $expect_checksum1, | ||||||||||||||||||||||||||||
| 79 | expected_checksum2 => $expect_checksum2, | ||||||||||||||||||||||||||||
| 80 | }) if ($expect_checksum1 != $checksum1) || ($expect_checksum2 != $checksum2); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 8 | ▶ | if(! exists $self->MESSAGE_ID_CLASS_MAP->{$message_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | warn sprintf( 'No class found for message ID 0x%02x', $message_id ) | |||||||||||||||||||||||||||
| 84 | . "\n"; | ||||||||||||||||||||||||||||
| 85 | 0 | return undef; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | my $class = $self->PACKET_CLASS_PREFIX | ||||||||||||||||||||||||||||
| 88 | 8 | . $self->MESSAGE_ID_CLASS_MAP->{$message_id}; | |||||||||||||||||||||||||||
| 89 | 8 | my $new_packet = $class->new({ | |||||||||||||||||||||||||||
| 90 | preamble => $preamble, | ||||||||||||||||||||||||||||
| 91 | version => $version, | ||||||||||||||||||||||||||||
| 92 | checksum1 => $checksum1, | ||||||||||||||||||||||||||||
| 93 | checksum2 => $checksum2, | ||||||||||||||||||||||||||||
| 94 | payload => \@payload, | ||||||||||||||||||||||||||||
| 95 | }); | ||||||||||||||||||||||||||||
| 96 | 8 | return $new_packet; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub fresh_packet | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 1 | ▶ | my ($self, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fresh_packet: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 1 | my $class = $self->PACKET_CLASS_PREFIX . $type; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | my $packet = eval { | |||||||||||||||||||||||||||
| 105 | 1 | $class->new({ | |||||||||||||||||||||||||||
| 106 | fresh => 1, | ||||||||||||||||||||||||||||
| 107 | }); | ||||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | 1 | ▶ | if( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | die "[PacketFactory] Could not init '$class': $@\n"; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | return $packet; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 7 | 1; | |||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 NAME | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | UAV::Pilot::WumpusRover::PacketFactory | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Where $packet_in is a bunch of bytes read from the network: | ||||||||||||||||||||||||||||
| 127 | my $packet = UAV::Pilot::WumpusRover::PacketFactory->read_packet( | ||||||||||||||||||||||||||||
| 128 | $packet_in ); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Create a fresh packet that we might later send over the network: | ||||||||||||||||||||||||||||
| 131 | my $new_packet = UAV::Pilot::WumpusRover::PacketFactory->fresh_packet( | ||||||||||||||||||||||||||||
| 132 | 'Ack' ); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Creates new packets, either for reading a bunch of bytes off the network, or | ||||||||||||||||||||||||||||
| 137 | for fresh ones that we'll send back over the network. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 METHODS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head2 read_packet | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | read_packet( $bytes ) | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Takes a bunch of bytes and returns a C<UAV::Pilot::WumpusRover::Packet> object | ||||||||||||||||||||||||||||
| 146 | based on that data. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 fresh_packet | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | fresh_packet( $type ) | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Creates a new packet based on C<$type> and returns it. The C<$type> parameter | ||||||||||||||||||||||||||||
| 153 | should be one of the classes under C<UAV::Pilot::WumpusRover::Packet::>, such | ||||||||||||||||||||||||||||
| 154 | as C<Ack> or C<RadioOutputs>. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||