blib/lib/Net/PJLink.pm

filter: Statementstmt 75.0% 255 / 340 Branchbran 64.2% 72 / 112 Conditioncond 55.5% 15 / 27 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 95.2% 40 / 42 Podpod 100% 21 / 21 totaltotal 72.9% 408 / 559 CC 79 SCAR 21.2
CC
79
Cov
71%
new
23.0
_send_command
18.6
close_all_connections
12.0
CRAP 8.3
↑ Summary
1 package Net::PJLink;
2  
3 22 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Switch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # internal constants
15 22 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 PJLINK_PORT => 4352,
17 PJLINK_C_HEADER => '%1',
18 PJLINK_A_HEADER => 'PJLINK ',
19 CONNECT_TIMEOUT => 0.05,
20 RECEIVE_TIMEOUT => 5,
21 22 };
22  
23 22 our @ISA = qw( Exporter );
24  
25 =head1 NAME
26  
27 Net::PJLink - PJLink protocol implementation
28  
29 =head1 VERSION
30  
31 Version 1.03
32  
33 =cut
34  
35 22 our $VERSION = '1.03';
36  
37  
38 =head1 SYNOPSIS
39  
40 Net::PJLink is a pure perl implementation of the PJLink protocol (L<http://pjlink.jbmia.or.jp/english/>) version 1.00, Class 1.
41 This is a standard protocol for communicating with network-capable projectors.
42 Net::PJLink uses an object-oriented style, with an object representing a group of one or more projectors.
43 An object has methods corresponding to the commands in the PJLink protocol specification.
44  
45 use Net::PJLink;
46  
47 my $prj = Net::PJLink->new(
48 host => [ '10.0.0.1', '10.0.0.2' ],
49 keep_alive => 1,
50 );
51  
52 $prj->set_power(1); # turn on projectors
53  
54 $prj->set_audio_mute(1); # mute sound
55  
56 # retreive the current input being used
57 my $input = $prj->get_input();
58 if ($input->{'10.0.0.1'}->[0] == Net::PJLink::INPUT_RGB) {
59 print "RGB input number " . $input->{'10.0.0.1'}->[1];
60 print " is active on projector 1.";
61 }
62  
63 # close network connections to the projectors
64 $prj->close_all_connections;
65  
66 =head1 EXPORTS
67  
68 Net::PJLink uses constants to represent status codes sent to and received from projectors.
69 These constants can be used like C<Net::PJLink::ERR_COMMAND>, or imported
70 into the local namespace by using the Exporter tag C<:RESPONSES>.
71  
72 use Net::PJLink qw( :RESPONSES );
73  
74 my $prj = Net::PJLink->new(
75 host => '192.168.1.10'
76 );
77 if ($prj->get_power() == POWER_ON) {
78 print "Projector is on.";
79 }
80  
81 The two lists below describe each symbol that is exported by the C<:RESPONSES> tag.
82  
83 =head2 Command Response Constants
84  
85 These are general status codes that are common to many projector commands.
86  
87 =over 4
88  
89 =item * C<OK>
90  
91 The command succeeded.
92  
93 =item * C<WARNING>
94  
95 Status is "warning".
96  
97 =item * C<ERROR>
98  
99 Status is "error".
100  
101 =item * C<ERR_COMMAND>
102  
103 The command could not be recognized or is not supported by the projector.
104 This could happen because the projector is deviating from the specification, the message is getting corrupted, or there is a bug in this module.
105  
106 =item * C<ERR_PARAMETER>
107  
108 An invalid parameter was given in the command.
109  
110 =item * C<ERR_UNAVL_TIME>
111  
112 The command is not available at this time (e.g. projector is on standby, warming up, etc.).
113  
114 =item * C<ERR_PRJT_FAIL>
115  
116 A projector failure occurred when processing the command.
117  
118 =item * C<ERR_NETWORK>
119  
120 A network connection to the projector could not be established.
121  
122 =item * C<ERR_AUTH>
123  
124 Authentication failed.
125  
126 =item * C<ERR_TIMEOUT>
127  
128 A response from the projector was not received.
129  
130 =item * C<ERR_PARSE>
131  
132 The projector's response was received, but could not be understood.
133 This could happen because the projector is deviating from the specification, the message is getting corrupted, or there is a bug in this module.
134  
135 =back
136  
137 =cut
138  
139 22 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
140 22 OK => 0, #'OK',
141 ERR_COMMAND => -1, #'ERR1',
142 ERR_PARAMETER => -2, #'ERR2',
143 ERR_UNAVL_TIME => -3, #'ERR3',
144 ERR_PRJT_FAIL => -4, #'ERR4',
145 ERR_NETWORK => -5,
146 ERR_AUTH => -6,
147 WARNING => -7,
148 ERROR => -8,
149 ERR_TIMEOUT => -9,
150 ERR_PARSE => -10,
151 22 };
152  
153 =head2 Status Responses
154  
155 These values are returned from commands that request information from the projector.
156 See the documentation for each command to find out which values can be returned for that command.
157  
158 =over 4
159  
160 =item * C<POWER_OFF>
161  
162 =item * C<POWER_ON>
163  
164 =item * C<POWER_COOLING>
165  
166 =item * C<POWER_WARMUP>
167  
168 =item * C<INPUT_RGB>
169  
170 =item * C<INPUT_VIDEO>
171  
172 =item * C<INPUT_DIGITAL>
173  
174 =item * C<INPUT_STORAGE>
175  
176 =item * C<INPUT_NETWORK>
177  
178 =item * C<MUTE_VIDEO>
179  
180 =item * C<MUTE_AUDIO>
181  
182 =back
183  
184 =cut
185  
186 22 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
187 22 POWER_OFF => 0,
188 POWER_ON => 1,
189 POWER_COOLING => 2,
190 POWER_WARMUP => 3,
191 INPUT_RGB => 1,
192 INPUT_VIDEO => 2,
193 INPUT_DIGITAL => 3,
194 INPUT_STORAGE => 4,
195 INPUT_NETWORK => 5,
196 MUTE_VIDEO => 1,
197 MUTE_AUDIO => 2,
198 22 };
199  
200 22 our @EXPORT_OK = qw(
201 POWER_OFF POWER_ON POWER_COOLING POWER_WARMUP
202 INPUT_RGB INPUT_VIDEO INPUT_DIGITAL INPUT_STORAGE INPUT_NETWORK
203 MUTE_VIDEO MUTE_AUDIO
204 ERR_COMMAND ERR_PARAMETER ERR_UNAVL_TIME ERR_PRJT_FAIL ERR_NETWORK ERR_AUTH
205 OK WARNING ERROR ERR_TIMEOUT ERR_PARSE
206 );
207 22 our %EXPORT_TAGS = (
208 RESPONSES => [qw(
209 POWER_OFF POWER_ON POWER_COOLING POWER_WARMUP
210 INPUT_RGB INPUT_VIDEO INPUT_DIGITAL INPUT_STORAGE INPUT_NETWORK
211 MUTE_VIDEO MUTE_AUDIO
212 ERR_COMMAND ERR_PARAMETER ERR_UNAVL_TIME ERR_PRJT_FAIL ERR_NETWORK ERR_AUTH
213 OK WARNING ERROR ERR_TIMEOUT ERR_PARSE
214 )]
215 );
216  
217  
218 # used internally
219 # list of command codes
220 22 my %COMMAND = (
221 power => 'POWR',
222 input => 'INPT',
223 mute => 'AVMT',
224 status => 'ERST',
225 lamp => 'LAMP',
226 input_list => 'INST',
227 name => 'NAME',
228 mfr => 'INF1',
229 prod_name => 'INF2',
230 prod_info => 'INFO',
231 class => 'CLSS',
232 );
233  
234 # used internally
235 # response codes that are translated
236 # into constants for all command responses
237 22 my %RESPONSE = (
238 'OK' => OK,
239 'ERR1' => ERR_COMMAND,
240 'ERR2' => ERR_PARAMETER,
241 'ERR3' => ERR_UNAVL_TIME,
242 'ERR4' => ERR_PRJT_FAIL,
243 );
244  
245 =head1 UTILITY METHODS
246  
247 =head2 new(...)
248  
249 use Net::PJLink;
250  
251 # Send commands to two hosts (batch mode),
252 # don't close the connection after each command,
253 # if a host cannot be contacted then remove it,
254 # wait up to 1 second for a connection to be opened
255 my $prj = Net::PJLink->new(
256 host => ['10.0.0.1', '10.0.0.2'],
257 try_once => 1,
258 keep_alive => 1,
259 connect_timeout => 1.0,
260 );
261  
262 Constructor for a new PJLink object.
263 It requires at least the C<host> option to indicate where commands should be sent.
264 The full list of arguments:
265  
266 =over 4
267  
268 =item * host
269  
270 This can be either a string consisting of a hostname or an IP address, or an array of such strings.
271 If you want to add a whole subnet, use something like L<Net::CIDR::Set> to expand CIDR notation to an array of IP addresses.
272 Every command given to this object will be applied to all hosts, and replies will be returned in a hash indexed by hostname or IP address if more than one host was given.
273  
274 =item * try_once
275  
276 True/False. Default is false.
277 Automatically remove unresponsive hosts from the list of hosts.
278 This speeds up any subseqent commands that are issued by not waiting for network timeout on a host that is down.
279 If this option evaluates false, the list of hosts will never be automatically changed.
280  
281 =item * batch
282  
283 True/False.
284 Force "batch mode" to be enabled or disabled.
285 Batch mode is normally set automatically based on whether multiple hosts are being used.
286 With batch mode on, all results will be returned as a hash reference indexed by hostname or IP address.
287 If batch mode is disabled when commands are sent to multiple hosts, only one of the hosts' results will be returned (which one is unpredictable).
288  
289 =item * port
290  
291 Default is 4352, which is the standard PJLink port.
292 Connections will be made to this port on each host.
293  
294 =item * auth_password
295  
296 Set the password that will be used for authentication for those hosts that require it.
297 It must be 32 alphanumeric characters or less.
298 The password is not transmitted over the network; it is used to calculate an MD5 sum.
299  
300 =item * keep_alive
301  
302 True/False. Default is false.
303 If set, connections will not be closed automatically after a response is received.
304 This is useful when sending many commands.
305  
306 =item * connect_timeout
307  
308 The time (in seconds) to wait for a new TCP connection to be established.
309 Default is 0.5.
310 This may need to be changed, depending on your network and/or projector.
311 The default should provide good reliability, and be practical for a small number of projectors.
312 Using a value of 0.05 seems to work well for connecting to a large number of hosts over a fast network in a reasonable amount of time.
313 (Larger values can take a long time when connecting to each host in a /24 subnet.)
314  
315 =item * receive_timeout
316  
317 The time (in seconds) to wait for a reply to be received.
318 If this option is not specified, a default of 5 seconds is used.
319 The value needed here might vary greatly between different projector models.
320  
321 =back
322  
323 =cut
324  
325 21 ▶sub new {
Subroutine new: called CC=12 SCAR=31.3
Pod documented
326 21 my $class = shift;
327 21 my $self = {};
328 21 bless $self, $class;
329 21 my %args = @_;
330  
331 21 ▶ unless (defined $args{'host'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"host"}) 0 21 21
332 0 carp "Missing 'host' argument";
333 0 return undef;
334 }
335 21 switch (ref $args{'host'}) {
336 21 ▶ case '' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("")) 21 0 21
337 21 $self->{'host'} = {$args{'host'} => 0};
338 0 }
339 0 ▶ case 'ARRAY' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case('ARRAY')) { } 0 0 0
340 0 foreach (@{$args{'host'}}) {$self->{'host'}->{$_} = 0;}
341 0 }
342 else {
343 0 carp "Invalid 'host' argument";
344 0 return undef;
345 }
346 0 }
347 21 $self->{'batch'} = (scalar keys %{$self->{'host'}} > 1);
348 21 ▶ $self->{'try_once'} = $args{'try_once'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'try_once'} ? : 2 19 21
349 21 ▶ $self->{'batch'} = $args{'batch'} if (defined $args{'batch'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"batch"} 0 21 21
350 21 ▶ $self->{'port'} = $args{'port'} || PJLINK_PORT;
Condition100%2/2
$args{'port'} || 4352
l!l
183
MC/DC100%1/1
$args{'port'} || 4352
$args{'port'}
Truth table100%2/2
A
A= $args{'port'}
Aresult
11
00
351 21 ▶ $self->{'keep_alive'} = $args{'keep_alive'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'keep_alive'} ? : 0 21 21
352 21 ▶ $self->{'auth_password'} = $args{'auth_password'} if (defined $args{'auth_password'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"auth_password"} 1 20 21
353 21 ▶ $self->{'connect_timeout'} = $args{'connect_timeout'} || CONNECT_TIMEOUT;
Condition50%1/2
$args{'connect_timeout'} || 0.05
l!l
021
MC/DC0%0/1
$args{'connect_timeout'} || 0.05
$args{'connect_timeout'}
Truth table50%1/2
A
A= $args{'connect_timeout'}
Aresult
11
00
354 21 ▶ $self->{'receive_timeout'} = $args{'receive_timeout'} || RECEIVE_TIMEOUT;
Condition50%1/2
$args{'receive_timeout'} || 5
l!l
021
MC/DC0%0/1
$args{'receive_timeout'} || 5
$args{'receive_timeout'}
Truth table50%1/2
A
A= $args{'receive_timeout'}
Aresult
11
00
355 21 return $self;
356 }
357  
358 # internal method
359 # Open a TCP connection
360 170 ▶sub _open_connection {
Subroutine _open_connection: called CC=4 SCAR=14.4
361 170 my $self = shift;
362 170 my $host = shift;
363  
364 170 ▶ if ($self->{'host'}->{$host}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"host"}{$host}) 0 170 170
365 0 warn "Re-opening connection to $host";
366 0 $self->{'host'}->{$host}->close;
367 }
368 my $socket = IO::Socket::INET->new(
369 PeerAddr => $host,
370 PeerPort => $self->{'port'},
371 Proto => 'tcp',
372 170 Timeout => $self->{'connect_timeout'},
373 );
374 170 ▶ return 0 unless ($socket && $socket->connected);
Branch: 1 branch
BranchTrueFalseTotal
unless $socket and $socket->connected 17 153 170
Condition66%2/3
$socket and $socket->connected
!ll&&!rl&&r
170153
MC/DC50%1/2
$socket and $socket->connected
$socket $socket->connected
Truth table66%2/3
A and B
A= $socketB= $socket->connected
ABresult
0X0
100
111
375 153 $socket->autoflush(1);
376 153 $self->{'host'}->{$host} = $socket;
377 153 return $socket;
378 }
379  
380 # internal method
381 # Check authentication status on a just-opened PJLink connection.
382 # If necessary, use auth_password to authenticate the connection.
383 153 ▶sub _auth_connection {
Subroutine _auth_connection: called CC=9 SCAR=24.2
384 153 my $self = shift;
385 153 my $host = shift;
386 153 my $resp;
387  
388 # undef if unknown host
389 153 ▶ return undef unless ($self->{'host'}->{$host});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"host"}{$host} 0 153 153
390 153 my $cnx = $self->{'host'}->{$host};
391 153 $cnx->recv($resp, 128);
392 # false, unless format is correct
393 153 ▶ return 0 unless (defined $resp && $resp =~ /^PJLINK ([01])( ([0-9a-fA-F]+))?\x0d$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resp and $resp =~ /^PJLINK ([01])( ([0-9a-fA-F]+))?\x0d$/ 0 153 153
Condition33%1/3
defined $resp and $resp =~ /^PJLINK ([01])( ([0-9a-fA-F]+))?\x0d$/
!ll&&!rl&&r
00153
MC/DC0%0/2
defined $resp and $resp =~ /^PJLINK ([01])( ([0-9a-fA-F]+))?\x0d$/
defined $resp $resp =~ /^PJLINK ([01])( ([0-9a-fA-F]+))?\x0d$/
Truth table33%1/3
A and B
A= defined $respB= $resp =~ /^PJLINK ([01])( ([0-9a-fA-F]+))?\x0d$/
ABresult
0X0
100
111
394 # true, no auth required
395 153 ▶ return 1 if ($1 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $1 == 0 136 17 153
396 # false, unless password is given
397 17 ▶ return 0 unless (defined $self->{'auth_password'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"auth_password"} 0 17 17
398 # false, unless random number was received
399 17 ▶ return 0 unless ($3);
Branch: 1 branch
BranchTrueFalseTotal
unless $3 0 17 17
400  
401 17 my $digest = Digest::MD5::md5_hex($3 . $self->{'auth_password'});
402 # test command to verify that auth succeeded
403 17 $cnx->send($digest . "%1POWR ?\xd");
404 17 $cnx->recv($resp, 32);
405 17 ▶ return 1 if (defined $resp && $resp =~ /^%1POWR=\d\x0d$/);
Branch: 1 branch
BranchTrueFalseTotal
if defined $resp and $resp =~ /^%1POWR=\d\x0d$/ 17 0 17
Condition33%1/3
defined $resp and $resp =~ /^%1POWR=\d\x0d$/
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $resp and $resp =~ /^%1POWR=\d\x0d$/
defined $resp $resp =~ /^%1POWR=\d\x0d$/
Truth table33%1/3
A and B
A= defined $respB= $resp =~ /^%1POWR=\d\x0d$/
ABresult
0X0
100
111
406 # don't close the connection yet,
407 # because auth might be tried with a
408 # different password
409 0 return 0;
410 }
411  
412 =head2 set_auth_password($pass)
413  
414 Set the password that will be used when connecting to a projector.
415 This will only apply to newly established connections.
416  
417 $prj->set_auth_password('secret');
418  
419 Returns 1 if successful, 0 otherwise (password is too long).
420  
421 =cut
422  
423 37 ▶sub set_auth_password {
Subroutine set_auth_password: called CC=3 SCAR=11.0
Pod documented
424 37 my $self = shift;
425 37 my $pass = shift;
426 37 ▶ if (defined $pass && $pass !~ /^.{1,32}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pass and not $pass =~ /^.{1,32}$/) { } 1 36 37
Condition100%3/3
defined $pass and not $pass =~ /^.{1,32}$/
!ll&&!rl&&r
17191
MC/DC100%2/2
defined $pass and not $pass =~ /^.{1,32}$/
defined $pass not $pass =~ /^.{1,32}$/
Truth table100%3/3
A and not(B)
A= defined $passB= not $pass =~ /^.{1,32}$/
ABresult
0X0
100
111
427 1 carp "auth_password must be less than or equal to 32 bytes";
428 1 return 0;
429 } else {
430 36 $self->{'auth_password'} = $pass;
431 36 return 1;
432 }
433 }
434  
435 =head2 close_connection($host)
436  
437 Manually close the connection to one host, specified by hostname or IP address.
438 Returns 1 if the connection was found and closed, returns 0 otherwise.
439  
440 =cut
441  
442 0 ▶sub close_connection {
Subroutine close_connection: not called CC=2 SCAR=17.9
Pod documented
443 0 my $self = shift;
444 0 my $host = shift;
445  
446 0 ▶ return 0 unless (defined $self->{'hosts'}->{$host});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"hosts"}{$host} 0 0 0
447 0 $self->{'hosts'}->{$host}->close;
448 0 return 1;
449 }
450  
451 =head2 close_all_connections()
452  
453 Manually close all open connections that are managed by this instance.
454 This is usually used when the object has been created with the C<keep_alive> option.
455  
456 =cut
457  
458 0 ▶sub close_all_connections {
Subroutine close_all_connections: not called CC=3 SCAR=24.8
Pod documented
459 0 my $self = shift;
460 0 ▶ foreach (values %{$self->{'hosts'}}) { $_->close if ($_); }
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
461 }
462  
463 # internal method
464 # Build the command message and do some basic sanity
465 # checks on it.
466 182 ▶sub _build_command {
Subroutine _build_command: called CC=3 SCAR=11.2
467 182 my $self = shift;
468 182 my $cmd = shift;
469 182 my $arg = shift;
470 182 ▶ die("Invalid command name \"$cmd\"!") unless (defined $COMMAND{$cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $COMMAND{$cmd} 0 182 182
471 182 ▶ die("Invalid characters in command argument!") if ($arg =~ /\x0d/);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /\x0d/ 0 182 182
472 182 return PJLINK_C_HEADER . $COMMAND{$cmd} . ' ' . $arg . "\xd";
473 }
474  
475 # internal method
476 # Build and send a command string to all active hosts.
477 # The data must be sent separately to each host because
478 # the PJLink protocol requires the use of TCP connections.
479 # This code sends data to each host, then receives responses
480 # from each host. This is probably not the best way to handle
481 # the problem, and it will not work well with multiple
482 # hundreds of hosts (especially when many hosts are not
483 # reachable and thus cause a network timeout delay). This is
484 # because the first connections to be opened will timeout
485 # due to inactivity before the data can be received.
486 # Suggestions are welcome.
487 171 ▶sub _send_command {
Subroutine _send_command: called CC=16 SCAR=29.2
488 171 my $self = shift;
489 171 my $cmd = shift;
490 171 my $arg = shift;
491 171 local $/ = "\xd";
492 171 my(%result, %name);
493 171 my $payload = $self->_build_command($cmd, $arg);
494 171 my $select = IO::Select->new();
495 # send loop: try to connect to each host and send data
496 171 ▶ while (my($host, $cnx) = each %{$self->{'host'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"host"};}) 170 171 341
497 170 $result{$host} = ERR_TIMEOUT;
498 170 ▶ unless ($cnx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cnx) 170 0 170
499 170 ▶ unless ($cnx = $self->_open_connection($host)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cnx = $self->_open_connection($host)) 17 153 170
500 17 $result{$host} = ERR_NETWORK;
501 17 ▶ delete $self->{'host'}->{$host} if ($self->{'try_once'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"try_once"} 1 16 17
502 17 next;
503 }
504 153 ▶ unless ($self->_auth_connection($host)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_auth_connection($host)) 0 153 153
505 0 $result{$host} = ERR_AUTH;
506 0 ▶ delete $self->{'host'}->{$host} if ($self->{'try_once'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"try_once"} 0 0 0
507 0 next;
508 }
509 }
510 153 $cnx->write($payload);
511 153 $select->add($cnx);
512 153 $name{$cnx} = $host;
513 }
514 # recv loop: check connections for responses until 5 second timeout
515 171 my $start_time = time;
516 171 ▶ while ($select->count() && time - $start_time < $self->{'receive_timeout'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->count and time - $start_time < $self->{"receive_timeout"}) 153 171 324
Condition66%2/3
$select->count and time - $start_time < $self->{"receive_timeout"}
!ll&&!rl&&r
1710153
MC/DC50%1/2
$select->count and time - $start_time < $self->{"receive_timeout"}
$select->count time - $start_time < $self->{"receive_timeout"}
Truth table66%2/3
A and B
A= $select->countB= time - $start_time < $self->{"receive_timeout"}
ABresult
0X0
100
111
517 153 my @ready = $select->can_read($self->{'receive_timeout'});
518 153 foreach my $cnx (@ready) {
519 153 my $host = $name{$cnx};
520 153 my $resp;
521 153 my $status = $cnx->recv($resp, 256, MSG_DONTWAIT);
522 153 ▶ next unless (defined $status);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 153 153
523 153 $select->remove($cnx);
524 153 ▶ unless ($self->{'keep_alive'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"keep_alive"}) 153 0 153
525 153 $cnx->close;
526 153 $self->{'host'}->{$host} = 0;
527 }
528 153 my $cmd_symbol = $COMMAND{$cmd};
529 153 ▶ if (defined $resp && $resp =~ /^%1$cmd_symbol=(.*)\x0d$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resp and $resp =~ /^%1$cmd_symbol=(.*)\x0d$/) { } 153 0 153
Condition33%1/3
defined $resp and $resp =~ /^%1$cmd_symbol=(.*)\x0d$/
!ll&&!rl&&r
00153
MC/DC0%0/2
defined $resp and $resp =~ /^%1$cmd_symbol=(.*)\x0d$/
defined $resp $resp =~ /^%1$cmd_symbol=(.*)\x0d$/
Truth table33%1/3
A and B
A= defined $respB= $resp =~ /^%1$cmd_symbol=(.*)\x0d$/
ABresult
0X0
100
111
530 153 ▶ if (defined $RESPONSE{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $RESPONSE{$1}) { } 82 71 153
531 82 $result{$host} = $RESPONSE{$1};
532 } else {
533 71 $result{$host} = $1;
534 }
535 } else {
536 0 $result{$host} = ERR_PARSE;
537 }
538 }
539 }
540 # return data
541 171 ▶ if ($self->{'batch'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'batch'}) { } 0 171 171
542 0 return \%result;
543 } else {
544 171 (undef, my $result) = each %result;
545 171 return $result;
546 }
547 }
548  
549 =head2 add_hosts($host1, ...)
550  
551 Takes arguments of the same form as the C<host> option to the C<new> constructor.
552 These hosts will be appended to the list of hosts that commands will be sent to.
553 Batch mode is enabled if appropriate.
554  
555 =cut
556  
557 1 ▶sub add_hosts {
Subroutine add_hosts: called CC=5 SCAR=17.5
Pod documented
558 1 my $self = shift;
559 1 foreach my $host (@_) {
560 2 switch (ref $host) {
561 2 ▶ case '' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("")) 1 1 2
562 1 $self->{'host'}->{$host} = 0;
563 0 }
564 1 ▶ case 'ARRAY' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case('ARRAY')) { } 1 0 1
565 1 foreach (@{$host}) {$self->{'host'}->{$_} = 0;}
566 0 }
567 else {
568 0 carp "Invalid argument";
569 }
570 0 }
571 }
572 1 $self->{'batch'} = (scalar keys %{$self->{'host'}} > 1);
573 }
574  
575 =head2 remove_hosts($host1, ...)
576  
577 Takes arguments of the same form as the C<host> option to the C<new> constructor.
578 These hosts will be removed from the list of hosts that commands will be sent to.
579 Batch mode is not changed by this function in order to avoid a surprise change in output format.
580  
581 =cut
582  
583 1 ▶sub remove_hosts {
Subroutine remove_hosts: called CC=5 SCAR=17.8
Pod documented
584 1 my $self = shift;
585 1 foreach my $host (@_) {
586 2 switch (ref $host) {
587 2 ▶ case '' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("")) 1 1 2
588 1 delete $self->{'host'}->{$host};
589 0 }
590 1 ▶ case 'ARRAY' {
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case('ARRAY')) { } 1 0 1
591 1 foreach (@{$host}) {delete $self->{'host'}->{$_};}
592 0 }
593 else {
594 0 carp "Invalid argument";
595 }
596 0 }
597 }
598 }
599  
600 =head1 PROJECTOR COMMAND METHODS
601  
602 These methods are all frontends for projector commands; calling them will issue the corresponding command immediately.
603 The actual return value of these functions depends on whether batch mode is enabled (it is automatically enabled when more than one host has been added).
604 If enabled, the return value of these functions will always be a hash reference, with the keys being hostnames or IP addresses and the values being the response received from that host.
605 To illustrate:
606  
607 $prj = Net::PJLink->new(host => '10.0.0.1');
608  
609 $prj->set_power(1);
610 # => 0
611  
612 $prj->add_hosts('10.0.0.2');
613  
614 $prj->set_power(1);
615 # => { '10.0.0.1' => 0, '10.0.0.2' => 0 }
616  
617 The return values described below for each method are the return values for each host.
618  
619 =head2 set_power($state)
620  
621 Turn power on or off.
622 If the single argument is true, turn on; if argument is false, turn off.
623 Returns one of C<OK>, C<ERR_PARAMETER>, C<ERR_UNAVL_TIME>, C<ERR_PRJT_FAIL>.
624  
625 =cut
626  
627 50 ▶sub set_power {
Subroutine set_power: called CC=2 SCAR=6.9
Pod documented
628 50 my $self = shift;
629 50 ▶ my $status = ($_[0] ? '1' : '0');
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 35 15 50
630 50 return $self->_send_command('power', $status);
631 }
632  
633 =head2 get_power()
634  
635 Get the power status.
636 Returns one of C<POWER_OFF>, C<POWER_ON>, C<POWER_COOLING>, C<POWER_WARMUP>, C<ERR_UNAVL_TIME>, or C<ERR_PRJT_FAIL>.
637  
638 =cut
639  
640 17 ▶sub get_power {
Subroutine get_power: called CC=1 SCAR=0.0
Pod documented
641 17 my $self = shift;
642 17 return $self->_send_command('power', '?');
643 }
644  
645 =head2 set_input($input_type, $number)
646  
647 Set the active input.
648 The first argument is the input type, which can be specified using one of the provided values:
649  
650 =over 4
651  
652 =item * C<INPUT_RGB>
653  
654 =item * C<INPUT_VIDEO>
655  
656 =item * C<INPUT_DIGITAL>
657  
658 =item * C<INPUT_STORAGE>
659  
660 =item * C<INPUT_NETWORK>
661  
662 =back
663  
664 The second argument specifies which of the inputs of that type should be used.
665 For example, to use the second video input:
666  
667 $prj->set_input(Net::PJLink::INPUT_VIDEO, 2);
668  
669 See the C<get_input_list()> method for information on available inputs.
670 Returns one of C<OK>, C<ERR_PARAMETER>, C<ERR_UNAVL_TIME>, or C<ERR_PRJT_FAIL>.
671  
672 =cut
673  
674 14 ▶sub set_input {
Subroutine set_input: called CC=3 SCAR=13.0
Pod documented
675 14 my $self = shift;
676 14 my $value = shift;
677 14 my $number = shift;
678 14 ▶ unless ($value =~ /^[1-9]$/ && $number =~ /^[1-9]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^[1-9]$/ and $number =~ /^[1-9]$/) 0 14 14
Condition33%1/3
$value =~ /^[1-9]$/ and $number =~ /^[1-9]$/
!ll&&!rl&&r
0014
MC/DC0%0/2
$value =~ /^[1-9]$/ and $number =~ /^[1-9]$/
$value =~ /^[1-9]$/ $number =~ /^[1-9]$/
Truth table33%1/3
A and B
A= $value =~ /^[1-9]$/B= $number =~ /^[1-9]$/
ABresult
0X0
100
111
679 0 carp "Invalid argument";
680 0 return 0;
681 }
682 14 return $self->_send_command('input', "$value$number");
683 }
684  
685 =head2 get_input()
686  
687 Get the current active input.
688 An array reference is returned, with the first value being the input type and the second value indicating which input of that type.
689 Example:
690  
691 $prj->get_input();
692 # => [ 3, 1 ]
693  
694 The example response indicates that the first C<INPUT_DIGITAL> source is active.
695  
696 =cut
697  
698 13 ▶sub get_input {
Subroutine get_input: called CC=3 SCAR=11.5
Pod documented
699 13 my $self = shift;
700 13 ▶ my $xform = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
701 13 local $_ = shift;
702 13 ▶ return $_ unless (/(\d)(\d)/);
Branch: 1 branch
BranchTrueFalseTotal
unless /(\d)(\d)/ 1 12 13
703 12 return [$1, $2];
704 13 };
705 13 my $resp = $self->_send_command('input', '?');
706 13 ▶ if (not $self->{'batch'}) { return &$xform($resp); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"batch"}) 13 0 13
707 0 foreach (keys %$resp) { $resp->{$_} = &$xform($resp->{$_}); }
708 0 return $resp;
709 }
710  
711 =head2 set_audio_mute($state)
712  
713 Set audio mute on or off.
714 Returns one of C<OK>, C<ERR_PARAMETER>, C<ERR_UNAVL_TIME>, or C<ERR_PRJT_FAIL>.
715  
716 =cut
717  
718 12 ▶sub set_audio_mute {
Subroutine set_audio_mute: called CC=2 SCAR=7.1
Pod documented
719 12 my $self = shift;
720 12 ▶ my $value = ($_[0] ? '1' : '0');
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 12 0 12
721 12 return $self->_send_command('mute', '2' . $value);
722 }
723  
724 =head2 set_video_mute($state)
725  
726 Set video mute on or off.
727 Returns one of C<OK>, C<ERR_PARAMETER>, C<ERR_UNAVL_TIME>, or C<ERR_PRJT_FAIL>.
728  
729 =cut
730  
731 11 ▶sub set_video_mute {
Subroutine set_video_mute: called CC=2 SCAR=6.9
Pod documented
732 11 my $self = shift;
733 11 ▶ my $value = ($_[0] ? '1' : '0');
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 10 11
734 11 return $self->_send_command('mute', '1' . $value);
735 }
736  
737 =head2 get_av_mute()
738  
739 Get the current status of audio and video mute.
740 An array reference is returned, with the first value being audio mute and the second being video mute.
741 If the command failed, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
742  
743 =cut
744  
745 10 ▶sub get_av_mute {
Subroutine get_av_mute: called CC=3 SCAR=15.6
Pod documented
746 10 my $self = shift;
747 10 ▶ my $xform = sub {
Subroutine __ANON__: called CC=5 SCAR=24.2
748 10 local $_ = shift;
749 10 ▶ return $_ unless (/([123])([01])/);
Branch: 1 branch
BranchTrueFalseTotal
unless /([123])([01])/ 1 9 10
750 9 switch ($1) {
751 0 ▶ case 1 { return [1-$2, $2]; }
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case(1)) 0 9 9
752 0 ▶ case 2 { return [$2, 1-$2]; }
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case(2)) 9 0 9
753 0 ▶ case 3 { return [$2, $2]; }
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case(3)) 0 0 0
754 0 }
755 10 };
756 10 my $resp = $self->_send_command('mute', '?');
757 10 ▶ if (not $self->{'batch'}) { return &$xform($resp); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"batch"}) 10 0 10
758 0 foreach (keys %$resp) { $resp->{$_} = &$xform($resp->{$_}); }
759 0 return $resp;
760 }
761  
762 =head2 get_status()
763  
764 Get the health status of various parts of the projector.
765 A hash reference is returned, with the keys being the name of the part.
766  
767 $prj->get_status();
768 # => {
769 # 'fan' => 0,
770 # 'lamp' => 0,
771 # 'temp' => 0,
772 # 'cover' => 0,
773 # 'filter'=> -7,
774 # 'other' => 0,
775 # }
776  
777 The example response indicates that the projector's filter is in a C<WARNING> state, and all other areas are C<OK>.
778  
779 The values will be one of C<OK>, C<WARNING>, or C<ERROR>.
780  
781 Example for finding lamp health from multiple projectors:
782  
783 my $prj = Net::PJLink->new(
784 host => [ '192.168.1.1', '192.168.1.2' ],
785 );
786  
787 my $result = $prj->get_status();
788 while (my($host, $status) = each %$result) {
789 my $lamp = $status->{'lamp'};
790 print "The projector at $host has lamp status: ";
791 print $lamp == OK ? "ok\n" :
792 $lamp == WARNING ? "warning\n" :
793 $lamp == ERROR ? "error\n";
794 }
795  
796 =cut
797  
798 9 ▶sub get_status {
Subroutine get_status: called CC=3 SCAR=11.4
Pod documented
799 9 my $self = shift;
800 9 ▶ my $xform = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
801 9 local $_ = shift;
802 9 my %xlate = (
803 '0' => OK,
804 '1' => WARNING,
805 '2' => ERROR,
806 );
807 9 ▶ return $_ unless (/(\d)(\d)(\d)(\d)(\d)(\d)/);
Branch: 1 branch
BranchTrueFalseTotal
unless /(\d)(\d)(\d)(\d)(\d)(\d)/ 1 8 9
808 return {
809 'fan' => $xlate{$1},
810 'lamp' => $xlate{$2},
811 'temp' => $xlate{$3},
812 'cover' => $xlate{$4},
813 'filter'=> $xlate{$5},
814 8 'other' => $xlate{$6},
815 };
816 9 };
817 9 my $resp = $self->_send_command('status', '?');
818 9 ▶ if (not $self->{'batch'}) { return &$xform($resp); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"batch"}) 9 0 9
819 0 foreach (keys %$resp) { $resp->{$_} = &$xform($resp->{$_}); }
820 0 return $resp;
821 }
822  
823 =head2 get_lamp_info()
824  
825 Get the status and hours used for each lamp. The return value is a data structure like:
826  
827 [
828 [ $status, $hours ],
829 ... # each lamp
830 ]
831  
832 For consistency, this structure is used even if the projector only has one lamp.
833  
834 C<$status> indicates whether the lamp is on or off (1 or 0). $hours is an integer indicating the total number of hours the lamp has been on.
835 If the command was not successful, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
836  
837 =cut
838  
839 8 ▶sub get_lamp_info {
Subroutine get_lamp_info: called CC=3 SCAR=11.2
Pod documented
840 8 my $self = shift;
841 8 ▶ my $xform = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
842 8 local $_ = shift;
843 8 ▶ return $_ unless (/((\d+)\s+([10]))+/);
Branch: 1 branch
BranchTrueFalseTotal
unless /((\d+)\s+([10]))+/ 1 7 8
844 7 my @lamps = split / /;
845 7 my @ret;
846 7 ▶ while (scalar @lamps) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @lamps) 21 7 28
847 21 my($hours, $status) = splice @lamps, 0, 2;
848 21 push @ret, [$status, $hours];
849 }
850 7 return \@ret;
851 8 };
852 8 my $resp = $self->_send_command('lamp', '?');
853 8 ▶ if (not $self->{'batch'}) { return &$xform($resp); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"batch"}) 8 0 8
854 0 foreach (keys %$resp) { $resp->{$_} = &$xform($resp->{$_}); }
855 0 return $resp;
856 }
857  
858 =head2 get_input_list()
859  
860 Get a list of all available inputs. The return value is a data structure like:
861  
862 [
863 [ $type, $index ],
864 ... # each input
865 ]
866  
867 C<$type> corresponds to one of the five input types:
868  
869 =over 4
870  
871 =item * C<INPUT_RGB>
872  
873 =item * C<INPUT_VIDEO>
874  
875 =item * C<INPUT_DIGITAL>
876  
877 =item * C<INPUT_STORAGE>
878  
879 =item * C<INPUT_NETWORK>
880  
881 =back
882  
883 C<$index> is the number of that type (i.e. C<[3, 3]> indicates the third digital input).
884 If the command was not successful, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
885  
886 =cut
887  
888 7 ▶sub get_input_list {
Subroutine get_input_list: called CC=3 SCAR=11.2
Pod documented
889 7 my $self = shift;
890 7 ▶ my $xform = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
891 7 local $_ = shift;
892 7 ▶ return $_ if (/^-?\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^-?\d+$/ 1 6 7
893 6 ▶ return ERR_PARSE unless (/[1-5][1-9]( [1-5][1-9])*/);
Branch: 1 branch
BranchTrueFalseTotal
unless /[1-5][1-9]( [1-5][1-9])*/ 0 6 6
894 6 my @inputs = split / /;
895 6 my @ret;
896 6 ▶ while (scalar @inputs) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @inputs) 48 6 54
897 48 my $inp = shift @inputs;
898 48 $inp =~ /(\d)(\d)/;
899 48 push @ret, [$1, $2];
900 }
901 6 return \@ret;
902 7 };
903 7 my $resp = $self->_send_command('input_list', '?');
904 7 ▶ if (not $self->{'batch'}) { return &$xform($resp); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"batch"}) 7 0 7
905 0 foreach (keys %$resp) { $resp->{$_} = &$xform($resp->{$_}); }
906 0 return $resp;
907 }
908  
909 =head2 get_name()
910  
911 Get the projector name. Returns a string.
912 If the command was not successful, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
913  
914 =cut
915  
916 6 ▶sub get_name {
Subroutine get_name: called CC=1 SCAR=0.0
Pod documented
917 6 my $self = shift;
918 6 return $self->_send_command('name', '?');
919 }
920  
921 =head2 get_manufacturer()
922  
923 Get the manufacturer name. Returns a string.
924 If the command was not successful, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
925  
926 =cut
927  
928 5 ▶sub get_manufacturer {
Subroutine get_manufacturer: called CC=1 SCAR=0.0
Pod documented
929 5 my $self = shift;
930 5 return $self->_send_command('mfr', '?');
931 }
932  
933 =head2 get_product_name()
934  
935 Get the product name. Returns a string.
936 If the command was not successful, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
937  
938 =cut
939  
940 4 ▶sub get_product_name {
Subroutine get_product_name: called CC=1 SCAR=0.0
Pod documented
941 4 my $self = shift;
942 4 return $self->_send_command('prod_name', '?');
943 }
944  
945 =head2 get_product_info()
946  
947 Get "other information". Returns a string.
948 If the command was not successful, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
949  
950 =cut
951  
952 3 ▶sub get_product_info {
Subroutine get_product_info: called CC=1 SCAR=0.0
Pod documented
953 3 my $self = shift;
954 3 return $self->_send_command('prod_info', '?');
955 }
956  
957 =head2 get_class()
958  
959 Get information on supported PJLink Class. Returns a single digit.
960 For example, returning "2" indicates that the projector is compatible with the PJLink Class 2 protocol.
961 The PJLink v.1.00 Class 1 specification only defines return values "1" and "2".
962 If the command was not successful, C<ERR_UNAVL_TIME> or C<ERR_PRJT_FAIL> may be returned.
963  
964 =cut
965  
966 2 ▶sub get_class {
Subroutine get_class: called CC=1 SCAR=0.0
Pod documented
967 2 my $self = shift;
968 2 return $self->_send_command('class', '?');
969 }
970  
971 =head1 AUTHOR
972  
973 Kyle Emmons, C<< <kemmons at tma-0.net> >>
974  
975 =head1 BUGS
976  
977 This module has only been tested on Panasonic PTFW100NTU projectors.
978  
979 The code for opening network connections may not work reliably for a large (~200) number of hosts.
980 This is due to network connections timing out before all hosts have been contacted.
981 If you encounter this problem, adjusting the C<connect_timeout> and C<receive_timeout> arguments may help.
982  
983 Please report any bugs or feature requests to C<bug-net-pjlink at rt.cpan.org>, or through
984 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-PJLink>.
985 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
986  
987  
988  
989  
990 =head1 SUPPORT
991  
992 You can find documentation for this module with the perldoc command.
993  
994 perldoc Net::PJLink
995  
996  
997 You can also look for information at:
998  
999 =over 4
1000  
1001 =item * RT: CPAN's request tracker
1002  
1003 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-PJLink>
1004  
1005 =item * AnnoCPAN: Annotated CPAN documentation
1006  
1007 L<http://annocpan.org/dist/Net-PJLink>
1008  
1009 =item * CPAN Ratings
1010  
1011 L<http://cpanratings.perl.org/d/Net-PJLink>
1012  
1013 =item * Search CPAN
1014  
1015 L<http://search.cpan.org/dist/Net-PJLink/>
1016  
1017 =back
1018  
1019 =head1 LICENSE AND COPYRIGHT
1020  
1021 Copyright 2017 Kyle Emmons.
1022  
1023 This program is free software; you can redistribute it and/or modify it
1024 under the terms of either: the GNU General Public License as published
1025 by the Free Software Foundation; or the Artistic License.
1026  
1027 See http://dev.perl.org/licenses/ for more information.
1028  
1029 The PJLink name is a trademark of Japan Business Machine and Information System Industries Association (JBMIA).
1030  
1031 =cut
1032  
1033 22 1; # End of Net::PJLink
↑ Summary