blib/lib/AnyEvent/Onkyo.pm

filter: Statementstmt 81.3% 96 / 118 Branchbran 43.7% 7 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 24 / 30 Podpod 100% 4 / 4 totaltotal 77.9% 131 / 168 CC 9 SCAR 7.3
CC
9
Cov
77%
__ANON__
6.0
cleanup
6.0
_handle_setup
3.6
CRAP 2.1
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package AnyEvent::Onkyo;
4 {
5 2 $AnyEvent::Onkyo::VERSION = '1.130220';
6 }
7 2 ▶use base 'Device::Onkyo';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use AnyEvent::SerialPort;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 DEBUG => $ENV{ANYEVENT_ONKYO_DEBUG},
16 2 };
17  
18  
19 # ABSTRACT: AnyEvent module for controlling Onkyo/Integra AV equipment
20  
21  
22 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
23 1 my ($pkg, %p) = @_;
24 1 ▶ croak $pkg.'->new: callback parameter is required' unless ($p{callback});
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"callback"} 0 1 1
25 1 my $self = $pkg->SUPER::new(device => 'discover', %p);
26 1 $self;
27 }
28  
29  
30 1 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
31 1 my $self = shift;
32 1 my $cv = AnyEvent->condvar;
33 1 my $weak_cv = $cv;
34 1 weaken $weak_cv;
35 1 ▶ $self->SUPER::command(@_, subname 'command_cb' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
36 1 ▶ $weak_cv->send() if ($weak_cv);
Branch: 1 branch
BranchTrueFalseTotal
if $weak_cv 1 0 1
37 1 });
38 1 return $cv;
39 }
40  
41 1 ▶sub _open {
Subroutine _open: called CC=1 SCAR=0.0
42 1 my $self = shift;
43 1 $self->SUPER::_open($self->_open_condvar);
44 1 return 1;
45 }
46  
47 1 ▶sub _open_tcp_port {
Subroutine _open_tcp_port: called CC=2 SCAR=7.8
48 1 my ($self, $cv) = @_;
49 1 my $dev = $self->{device};
50 1 print STDERR "Opening $dev as tcp socket\n" if DEBUG;
51 1 my ($host, $port) = split /:/, $dev, 2;
52 1 ▶ $port = $self->{port} unless (defined $port);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 0 1 1
53 $self->{handle} =
54 AnyEvent::Handle->new(connect => [$host, $port],
55 1 ▶ on_connect => subname('tcp_connect_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 1 my ($hdl, $h, $p) = @_;
57 1 warn ref $self, " connected to $h:$p\n" if DEBUG;
58 1 $cv->send();
59 }),
60 on_connect_error =>
61 0 ▶ subname('tcp_connect_error_cb' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 my ($hdl, $msg) = @_;
63 0 my $err =
64 (ref $self).": Can't connect to $dev: $msg";
65 0 warn "Connect error: $err\n" if DEBUG;
66 0 $self->cleanup($err);
67 0 $cv->croak;
68 1 }));
69 1 return $cv;
70 }
71  
72 0 ▶sub _open_serial_port {
Subroutine _open_serial_port: not called CC=1 SCAR=6.9
73 0 my ($self, $cv) = @_;
74 $self->{handle} =
75 0 AnyEvent::SerialPort->new(serial_port =>
76 [ $self->device,
77 [ baudrate => $self->baud ] ]);
78 0 $cv->send();
79 0 return $cv;
80 }
81  
82 1 ▶sub _handle_setup {
Subroutine _handle_setup: called CC=3 SCAR=12.9
83 1 my $self = shift;
84 1 my $handle = $self->{handle};
85 1 my $weak_self = $self;
86 1 weaken $weak_self;
87  
88 0 ▶ $handle->on_error(subname('on_error' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
89 0 my ($hdl, $fatal, $msg) = @_;
90 0 print STDERR $hdl.": error $msg\n" if DEBUG;
91 0 $hdl->destroy;
92 0 ▶ if ($fatal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal) 0 0 0
93 0 $weak_self->cleanup($msg);
94 }
95 1 }));
96  
97 0 ▶ $handle->on_eof(subname('on_eof' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 my ($hdl) = @_;
99 0 print STDERR $hdl.": eof\n" if DEBUG;
100 0 $weak_self->cleanup('connection closed');
101 1 }));
102  
103 1 ▶ $handle->on_read(subname 'on_read_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 1 my ($hdl) = @_;
105 $hdl->push_read(ref $self => $self,
106 1 ▶ subname 'push_read_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
107 1 $weak_self->{callback}->(@_);
108 1 $weak_self->_write_now();
109 1 return 1;
110 1 });
111 1 });
112  
113 1 ▶ $self->{handle}->on_timeout($self->{on_timeout}) if ($self->{on_timeout});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_timeout"} 0 1 1
114 1 ▶ $self->{handle}->timeout($self->{timeout}) if ($self->{timeout});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} 0 1 1
115 1 1;
116 }
117  
118 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
119 0 $_[0]->cleanup;
120 }
121  
122  
123 0 ▶sub cleanup {
Subroutine cleanup: not called CC=2 SCAR=17.9
Pod documented
124 0 my ($self, $error) = @_;
125 0 print STDERR $self."->cleanup\n" if DEBUG;
126 0 ▶ $self->{handle}->destroy if ($self->{handle});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 0 0 0
127 0 delete $self->{handle};
128 }
129  
130 1 ▶sub _open_condvar {
Subroutine _open_condvar: called CC=1 SCAR=0.0
131 1 my $self = shift;
132 1 print STDERR $self."->open_condvar\n" if DEBUG;
133 1 my $cv = AnyEvent->condvar;
134 1 my $weak_self = $self;
135 1 weaken $weak_self;
136  
137 1 ▶ $cv->cb(subname 'open_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 1 print STDERR "start cb ", $weak_self->{handle}, " @_\n" if DEBUG;
139 1 $weak_self->_handle_setup();
140 1 $weak_self->_write_now();
141 1 });
142 1 $weak_self->{_waiting} = ['fake for async open'];
143 1 return $cv;
144 }
145  
146 1 ▶sub _real_write {
Subroutine _real_write: called CC=1 SCAR=0.0
147 1 my ($self, $str, $desc, $cb) = @_;
148 1 print STDERR "Sending: ", $desc, "\n" if DEBUG;
149 1 $self->{handle}->push_write($str);
150 }
151  
152 1 ▶sub _time_now {
Subroutine _time_now: called CC=1 SCAR=0.0
153 1 AnyEvent->now;
154 }
155  
156  
157 1 ▶sub anyevent_read_type {
Subroutine anyevent_read_type: called CC=1 SCAR=0.0
Pod documented
158 1 my ($handle, $cb, $self) = @_;
159  
160 1 my $weak_self = $self;
161 1 weaken $weak_self;
162  
163 1 ▶ subname 'anyevent_read_type_reader' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
164 1 my ($handle) = @_;
165 1 my $rbuf = \$handle->{rbuf};
166 1 while (1) { # read all message from the buffer
167 2 print STDERR "Before: ", (unpack 'H*', $$rbuf||''), "\n" if DEBUG;
168 2 my $res = $weak_self->read_one($rbuf);
169 2 ▶ return unless ($res);
Branch: 1 branch
BranchTrueFalseTotal
unless $res 1 1 2
170 1 print STDERR "After: ", (unpack 'H*', $$rbuf), "\n" if DEBUG;
171 1 $res = $cb->($res);
172 }
173 }
174 1 }
175  
176 2 1;
177  
178 __END__
↑ Summary