RFXCOM/Base.pm

filter: Statementstmt 100% 95 / 95 Branchbran 95.8% 23 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 23 / 23 Podpod 100% 3 / 3 totaltotal 99.3% 144 / 145 SCAR 25.6
CC
13
Cov
99%
_write
4.0
_write_now
4.0
_open_serial_port
3.0
CRAP 13.0
« RFXCOM/Decoder/RFXSensor.pm ↑ Summary RFXCOM/Decoder/X10Security.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Device::RFXCOM::Base;
4 5 $Device::RFXCOM::Base::VERSION = '1.142010';
5 # ABSTRACT: module for RFXCOM device base class
6  
7  
8 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use constant {
10 DEBUG => $ENV{DEVICE_RFXCOM_BASE_DEBUG},
11 TESTING => $ENV{DEVICE_RFXCOM_TESTING},
12 5 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Symbol qw/gensym/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Device::SerialPort qw( :PARAM :STAT 0.07 );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub _new {
22 12 ▶ my ($pkg, %p) = @_;
Subroutine _new: called CC=2 SCAR=6.9
23 12 my $self = bless
24 {
25 baud => 4800,
26 port => 10001,
27 discard_timeout => 0.03,
28 ack_timeout => 2,
29 dup_timeout => 0.5,
30 _q => [],
31 _buf => '',
32 _last_read => 0,
33 init_callback => undef,
34 %p,
35 }, $pkg;
36 12 ▶ $self->{plugins} = [$self->plugins()] unless ($self->{plugins});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"plugins"} 10 2 12
37 12 $self->_open();
38 6 $self->_init();
39 6 $self;
40 }
41  
42 sub DESTROY {
43 12 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
44 12 delete $self->{init};
45 }
46  
47  
48 sub queue {
49 1 ▶ scalar @{$_[0]->{_q}};
Subroutine queue: called CC=1 SCAR=0.0
Pod documented
50 }
51  
52  
53 sub _write {
54 22 ▶ my $self = shift;
Subroutine _write: called CC=4 SCAR=13.9
55 22 my %p = @_;
56 22 ▶ $p{raw} = pack 'H*', $p{hex} unless (exists $p{raw});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"raw"} 14 8 22
57 22 ▶ $p{hex} = unpack 'H*', $p{raw} unless (exists $p{hex});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"hex"} 8 14 22
58 22 print STDERR "Queued: ", $p{hex}, ' ', ($p{desc}||''), "\n" if DEBUG;
59 22 push @{$self->{_q}}, \%p;
60 22 ▶ $self->_write_now unless ($self->{_waiting});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_waiting"} 10 12 22
61 22 1;
62 }
63  
64 sub _write_now {
65 28 ▶ my $self = shift;
Subroutine _write_now: called CC=4 SCAR=13.9
66 28 my $rec = shift @{$self->{_q}};
67 28 my $wait_record = $self->{_waiting};
68 28 ▶ if ($wait_record) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_record) 19 9 28
69 19 delete $self->{_waiting};
70 19 my $cb = $wait_record->[1]->{callback};
71 19 ▶ $cb->() if ($cb);
Branch: 1 branch
BranchTrueFalseTotal
if $cb 3 16 19
72 }
73 28 ▶ return unless (defined $rec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 9 19 28
74 19 $self->_real_write($rec);
75 19 $self->{_waiting} = [ $self->_time_now, $rec ];
76 }
77  
78 sub _real_write {
79 19 ▶ my ($self, $rec) = @_;
Subroutine _real_write: called CC=1 SCAR=0.0
80 19 print STDERR "Sending: ", $rec->{hex}, ' ', ($rec->{desc}||''), "\n" if DEBUG;
81 19 syswrite $self->{fh}, $rec->{raw}, length $rec->{raw};
82 }
83  
84  
85 sub filehandle {
86 shift->{fh}
87 26 ▶}
Subroutine filehandle: called CC=1 SCAR=0.0
Pod documented
88  
89 sub _open {
90 11 ▶ my $self = shift;
Subroutine _open: called CC=2 SCAR=6.9
91 11 ▶ $self->{device} =~ m![/\\]! ?
Branch: 1 branch
BranchTrueFalseTotal
$self->{'device'} =~ m([/\\]) ? : 2 9 11
92 $self->_open_serial_port(@_) : $self->_open_tcp_port(@_)
93 }
94  
95 sub _open_tcp_port {
96 9 ▶ my $self = shift;
Subroutine _open_tcp_port: called CC=3 SCAR=11.0
97 9 my $dev = $self->{device};
98 9 print STDERR "Opening $dev as tcp socket\n" if DEBUG;
99 9 require IO::Socket::INET; import IO::Socket::INET;
100 9 ▶ $dev .= ':'.$self->{port} unless ($dev =~ /:/);
Branch: 1 branch
BranchTrueFalseTotal
unless $dev =~ /:/ 2 7 9
101 9 ▶ my $fh = IO::Socket::INET->new($dev) or
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Socket::INET"->new($dev) or croak("TCP connect to '${dev}' failed: $!") 5 4 9
102 croak "TCP connect to '$dev' failed: $!";
103 4 return $self->{fh} = $fh;
104 }
105  
106 sub _open_serial_port {
107 2 ▶ my $self = shift;
Subroutine _open_serial_port: called CC=3 SCAR=11.0
108 2 my $dev = $self->{device};
109 2 print STDERR "Opening $dev as serial port\n" if DEBUG;
110 2 my $fh = gensym();
111 2 ▶ my $sport = tie (*$fh, 'Device::SerialPort', $dev) or
Branch: 1 branch
BranchTrueFalseTotal
my $sport = tie(*$fh, "Device::SerialPort", $dev) or croak("Could not tie serial port, $dev, to file handle: $!") 0 2 2
112 croak "Could not tie serial port, $dev, to file handle: $!";
113 2 $sport->baudrate($self->baud);
114 2 $sport->databits(8);
115 2 $sport->parity("none");
116 2 $sport->stopbits(1);
117 2 $sport->datatype("raw");
118 2 $sport->write_settings();
119  
120 2 ▶ sysopen $fh, $dev, O_RDWR|O_NOCTTY|O_NDELAY or
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, $dev, 2306 or croak("sysopen of '${dev}' failed: $!") 1 1 2
121 croak "sysopen of '$dev' failed: $!";
122 1 $fh->autoflush(1);
123 1 binmode($fh);
124 1 return $self->{fh} = $fh;
125 }
126  
127  
128 sub baud {
129 shift->{baud}
130 3 ▶}
Subroutine baud: called CC=1 SCAR=0.0
Pod documented
131  
132 sub _time_now {
133 132 ▶ Time::HiRes::time
Subroutine _time_now: called CC=1 SCAR=0.0
134 }
135  
136 5 1;
137  
138 __END__
« RFXCOM/Decoder/RFXSensor.pm ↑ Summary RFXCOM/Decoder/X10Security.pm »