RFXCOM/RX.pm

filter: Statementstmt 98.3% 117 / 119 Branchbran 88.1% 37 / 42 Conditioncond 66.6% 10 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 19 / 19 Podpod 100% 3 / 3 totaltotal 91.7% 190 / 207 SCAR 33.4
CC
28
Cov
93%
read_one
13.0
read
9.1
_discard_buffer_check
4.1
CRAP 28.2
↑ Summary RFXCOM/Decoder/Oregon.pm »
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'}
l!l&&r!l&&!r
011
MC/DC50%1/2
$cb || $self->{'init_callback'}
$cb $self->{'init_callback'}
Truth table66%2/3
A || B
A= $cbB= $self->{'init_callback'}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if defined $res 3 5 8
42 5 ▶ $self->_discard_buffer_check() if ($self->{_buf} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_buf"} ne "" 1 4 5
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
BranchTrueFalseTotal
$sel->can_read($timeout) or return 1 5 6
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
BranchTrueFalseTotal
if defined $timeout 2 3 5
52 5 ▶ unless ($bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 0 5 5
53 0 ▶ croak defined $bytes ? 'closed' : 'error: '.$!;
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes ? : 0 0 0
54 }
55 5 $res = $self->read_one(\$self->{_buf});
56 5 ▶ $self->_write_now() if (defined $res);
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 4 1 5
57 5 ▶ goto REDO unless ($res);
Branch: 1 branch
BranchTrueFalseTotal
unless $res 1 4 5
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
BranchTrueFalseTotal
unless $$rbuf 3 95 98
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
BranchTrueFalseTotal
if (exists $self->{'_waiting'} and $header_byte == 77) { } 1 94 95
elsif (exists $self->{'_waiting'} and $header_byte == 44 || $header_byte == 65) { } 2 92 94
elsif ($bits == 0) { } 1 91 92
Condition100%3/3
exists $self->{"_waiting"} and $header_byte == 77
!ll&&!rl&&r
9221
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
ABresult
0X0
100
111
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
!ll&&!rl&&r
9202
$header_byte == 44 or $header_byte == 65
l!l&&r!l&&!r
110
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
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
if length $$rbuf < 1 + $length 0 91 91
106  
107 91 ▶ if ($length != int $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != int $length) 28 63 91
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
BranchTrueFalseTotal
if ($entry) { } 2 89 91
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
BranchTrueFalseTotal
my $matched = $decoder->decode($self, $msg, \@bytes, $bits, \%result) or next 641 61 702
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
BranchTrueFalseTotal
if $result->{"dont_cache"} 3 90 93
144 90 my $entry = $self->{_cache}->{$result->{key}};
145 90 ▶ if ($entry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) 4 86 90
146 4 ▶ $result->{duplicate} = 1 if ($self->_cache_is_duplicate($entry));
Branch: 1 branch
BranchTrueFalseTotal
if $self->_cache_is_duplicate($entry) 3 1 4
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
BranchTrueFalseTotal
if ($self->{"_buf"} ne "" and $self->{"_last_read"} < $self->_time_now - $self->{"discard_timeout"}) 0 1 1
Condition33%1/3
$self->{"_buf"} ne "" and $self->{"_last_read"} < $self->_time_now - $self->{"discard_timeout"}
!ll&&!rl&&r
010
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($self->{"_last_read"} < $self->_time_now - $self->{"dup_timeout"}) 2 3 5
173 2 $self->{_cache} = {};
174 }
175 }
176  
177 4 1;
178  
179 __END__
↑ Summary RFXCOM/Decoder/Oregon.pm »