| 1 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | package Device::RFXCOM::RX; | ||||||||||||||||||||||||||||||||||||
| 4 | 4 | $Device::RFXCOM::RX::VERSION = '1.142010'; | |||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Module to support RFXCOM RF receiver | ||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | use constant { | ||||||||||||||||||||||||||||||||||||
| 10 | DEBUG => $ENV{DEVICE_RFXCOM_RX_DEBUG}, | ||||||||||||||||||||||||||||||||||||
| 11 | TESTING => $ENV{DEVICE_RFXCOM_RX_TESTING}, | ||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use base 'Device::RFXCOM::Base'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Device::RFXCOM::Response; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | use Module::Pluggable | ||||||||||||||||||||||||||||||||||||
| 19 | 4 | search_path => 'Device::RFXCOM::Decoder', | |||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | instantiate => 'new'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | sub new { | ||||||||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | my $pkg = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 25 | 8 | $pkg->SUPER::_new(device => '/dev/rfxcom-rx', @_); | |||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | sub _init { | ||||||||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | my ($self, $cb) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||
| 30 | 2 | $self->_write(hex => 'F020', desc => 'version check'); | |||||||||||||||||||||||||||||||||||
| 31 | 2 | $self->_write(hex => 'F02A', desc => 'enable all possible receiving modes'); | |||||||||||||||||||||||||||||||||||
| 32 | $self->_write(hex => 'F041', desc => 'variable length with visonic', | ||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | callback => $cb || $self->{init_callback}); | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$cb || $self->{'init_callback'}
MC/DC50%1/2
$cb || $self->{'init_callback'}
$cb
$self->{'init_callback'}
Truth table66%2/3
A || B
A= $cbB= $self->{'init_callback'}
| |||||||||||||||||||||||||||||||||||||
| 34 | 2 | $self->{init} = 1; | |||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | sub read { | ||||||||||||||||||||||||||||||||||||
| 39 | 8 | ▶ | my ($self, $timeout) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 40 | 8 | my $res = $self->read_one(\$self->{_buf}); | |||||||||||||||||||||||||||||||||||
| 41 | 8 | ▶ | return $res if (defined $res); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | $self->_discard_buffer_check() if ($self->{_buf} ne ''); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 43 | 5 | $self->_discard_dup_cache_check(); | |||||||||||||||||||||||||||||||||||
| 44 | 5 | my $fh = $self->filehandle; | |||||||||||||||||||||||||||||||||||
| 45 | 5 | my $sel = IO::Select->new($fh); | |||||||||||||||||||||||||||||||||||
| 46 | 6 | REDO: | |||||||||||||||||||||||||||||||||||
| 47 | my $start = $self->_time_now; | ||||||||||||||||||||||||||||||||||||
| 48 | 6 | ▶ | $sel->can_read($timeout) or return; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 49 | 5 | my $bytes = sysread $fh, $self->{_buf}, 2048, length $self->{_buf}; | |||||||||||||||||||||||||||||||||||
| 50 | 5 | $self->{_last_read} = $self->_time_now; | |||||||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | $timeout -= $self->{_last_read} - $start if (defined $timeout); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | unless ($bytes) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | croak defined $bytes ? 'closed' : 'error: '.$!; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||
| 55 | 5 | $res = $self->read_one(\$self->{_buf}); | |||||||||||||||||||||||||||||||||||
| 56 | 5 | ▶ | $self->_write_now() if (defined $res); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | goto REDO unless ($res); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 58 | 4 | return $res; | |||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | sub read_one { | ||||||||||||||||||||||||||||||||||||
| 64 | 98 | ▶ | my ($self, $rbuf) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
read_one: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 65 | 98 | ▶ | return unless ($$rbuf); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | 95 | print STDERR "rbuf=", (unpack "H*", $$rbuf), "\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 68 | 95 | my $header_byte = unpack "C", $$rbuf; | |||||||||||||||||||||||||||||||||||
| 69 | 95 | my %result = | |||||||||||||||||||||||||||||||||||
| 70 | ( | ||||||||||||||||||||||||||||||||||||
| 71 | header_byte => $header_byte, | ||||||||||||||||||||||||||||||||||||
| 72 | type => 'unknown', | ||||||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||||||
| 74 | 95 | $result{master} = !($header_byte&0x80); | |||||||||||||||||||||||||||||||||||
| 75 | 95 | my $bits = $header_byte & 0x7f; | |||||||||||||||||||||||||||||||||||
| 76 | 95 | my $msg = ''; | |||||||||||||||||||||||||||||||||||
| 77 | 95 | my @bytes; | |||||||||||||||||||||||||||||||||||
| 78 | 95 | ▶ | if (exists $self->{_waiting} && $header_byte == 0x4d) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
exists $self->{"_waiting"} and $header_byte == 77
MC/DC100%2/2
exists $self->{"_waiting"} and $header_byte == 77
exists $self->{"_waiting"}
$header_byte == 77
Truth table100%3/3
A and B
A= exists $self->{"_waiting"}B= $header_byte == 77
| |||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | 1 | print STDERR "got version check response\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 81 | 1 | $msg = $$rbuf; | |||||||||||||||||||||||||||||||||||
| 82 | 1 | substr $msg, 0, 1, ''; | |||||||||||||||||||||||||||||||||||
| 83 | 1 | $$rbuf = ''; | |||||||||||||||||||||||||||||||||||
| 84 | 1 | $result{type} = 'version'; | |||||||||||||||||||||||||||||||||||
| 85 | 1 | @bytes = unpack 'C*', $msg; | |||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | ▶ | } elsif (exists $self->{_waiting} && | |||||||||||||||||||||||||||||||||||
|
Condition66%4/6
exists $self->{"_waiting"} and $header_byte == 44 || $header_byte == 65
$header_byte == 44 or $header_byte == 65
MC/DC33%1/3
exists $self->{"_waiting"} and $header_byte == 44 || $header_byte == 65
exists $self->{"_waiting"}
$header_byte == 44
$header_byte == 65
Truth table75%3/4
A and B or C
A= exists $self->{"_waiting"}B= $header_byte == 44C= $header_byte == 65
| |||||||||||||||||||||||||||||||||||||
| 88 | ( $header_byte == 0x2c || $header_byte == 0x41 )) { | ||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | 2 | print STDERR "got mode response\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 91 | 2 | substr $$rbuf, 0, 1, ''; | |||||||||||||||||||||||||||||||||||
| 92 | 2 | $result{type} = 'mode'; | |||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | } elsif ($bits == 0) { | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | 1 | print STDERR "got empty message\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 97 | 1 | substr $$rbuf, 0, 1, ''; | |||||||||||||||||||||||||||||||||||
| 98 | 1 | $result{type} = 'empty'; | |||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | 91 | my $length = $bits / 8; | |||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||
| 104 | 91 | print STDERR "bits=$bits length=$length\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 105 | 91 | ▶ | return if (length $$rbuf < 1 + $length); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 91 | ▶ | if ($length != int $length) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 108 | 28 | $length = 1 + int $length; | |||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | 91 | $msg = substr $$rbuf, 0, 1 + $length, ''; # message from buffer | |||||||||||||||||||||||||||||||||||
| 112 | 91 | substr $msg, 0, 1, ''; | |||||||||||||||||||||||||||||||||||
| 113 | 91 | @bytes = unpack 'C*', $msg; | |||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||
| 115 | 91 | $result{key} = $bits.'!'.$msg; | |||||||||||||||||||||||||||||||||||
| 116 | 91 | my $entry = $self->_cache_get(\%result); | |||||||||||||||||||||||||||||||||||
| 117 | 91 | ▶ | if ($entry) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 118 | 2 | print STDERR "using cache entry\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 119 | 2 | @result{qw/messages type/} = @{$entry->{result}}{qw/messages type/}; | |||||||||||||||||||||||||||||||||||
| 120 | 2 | $self->_cache_set(\%result); | |||||||||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||||||||||
| 122 | 89 | foreach my $decoder (@{$self->{plugins}}) { | |||||||||||||||||||||||||||||||||||
| 123 | 702 | ▶ | my $matched = $decoder->decode($self, $msg, \@bytes, $bits, \%result) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 124 | or next; | ||||||||||||||||||||||||||||||||||||
| 125 | 61 | ($result{type} = lc ref $decoder) =~ s/.*:://; | |||||||||||||||||||||||||||||||||||
| 126 | 61 | last; | |||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | 89 | $self->_cache_set(\%result); | |||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||
| 132 | 95 | @result{qw/data bytes/} = ($msg, \@bytes); | |||||||||||||||||||||||||||||||||||
| 133 | 95 | return Device::RFXCOM::Response->new(%result); | |||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | sub _cache_get { | ||||||||||||||||||||||||||||||||||||
| 137 | 96 | ▶ | my ($self, $result) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_cache_get: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 138 | 96 | $self->{_cache}->{$result->{key}}; | |||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | sub _cache_set { | ||||||||||||||||||||||||||||||||||||
| 142 | 93 | ▶ | my ($self, $result) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_cache_set: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 143 | 93 | ▶ | return if ($result->{dont_cache}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 144 | 90 | my $entry = $self->{_cache}->{$result->{key}}; | |||||||||||||||||||||||||||||||||||
| 145 | 90 | ▶ | if ($entry) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 146 | 4 | ▶ | $result->{duplicate} = 1 if ($self->_cache_is_duplicate($entry)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 147 | 4 | $entry->{t} = $self->_time_now; | |||||||||||||||||||||||||||||||||||
| 148 | 4 | return $entry; | |||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||
| 150 | $self->{_cache}->{$result->{key}} = | ||||||||||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||||||||||
| 152 | 86 | result => $result, | |||||||||||||||||||||||||||||||||||
| 153 | t => $self->_time_now, | ||||||||||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | sub _cache_is_duplicate { | ||||||||||||||||||||||||||||||||||||
| 158 | 5 | ▶ | my ($self, $entry) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_cache_is_duplicate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 159 | 5 | ($self->_time_now - $entry->{t}) < $self->{dup_timeout}; | |||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | sub _discard_buffer_check { | ||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_discard_buffer_check: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | if ($self->{_buf} ne '' && | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_buf"} ne "" and $self->{"_last_read"} < $self->_time_now - $self->{"discard_timeout"}
MC/DC0%0/2
$self->{"_buf"} ne "" and $self->{"_last_read"} < $self->_time_now - $self->{"discard_timeout"}
$self->{"_buf"} ne ""
$self->{"_last_read"} < $self->_time_now - $self->{"discard_timeout"}
Truth table33%1/3
A and B
A= $self->{"_buf"} ne ""B= $self->{"_last_read"} < $self->_time_now - $self->{"discard_timeout"}
| |||||||||||||||||||||||||||||||||||||
| 165 | $self->{_last_read} < ($self->_time_now - $self->{discard_timeout})) { | ||||||||||||||||||||||||||||||||||||
| 166 | 0 | $self->{_buf} = ''; | |||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||
| 170 | sub _discard_dup_cache_check { | ||||||||||||||||||||||||||||||||||||
| 171 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_discard_dup_cache_check: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 172 | 5 | ▶ | if ($self->{_last_read} < ($self->_time_now - $self->{dup_timeout})) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 173 | 2 | $self->{_cache} = {}; | |||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||
| 177 | 4 | 1; | |||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||||||||||