X10/TwoWay.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 140 CC 13 SCAR 22.9
CC
13
Cov
0%
send_word
20.0
get_word
20.0
new
12.0
CRAP 9.9
1  
2 # Copyright (c) 1999-2017 Rob Fugina <robf@fugina.com>
3 # Distributed under the terms of the GNU Public License, Version 3.0
4  
5 package X10::TwoWay;
6  
7 # Perl module for communicating with a TwoWay/TW523 combination
8  
9 use Data::Dumper;
10 use Device::SerialPort;
11 use FileHandle;
12 use IO::Select;
13  
14 use strict;
15  
16 use X10::Controller;
17  
18 use vars qw( @ISA );
19  
20 @ISA = qw( X10::Controller );
21  
22 ### constructors
23  
24 sub new
25 {
26 my $type = shift;
27  
28 my $self = new X10::Controller( @_ );
29 bless $self, $type;
30  
31 unless ($self->{test})
32 {
33 return undef unless $self->{port};
34  
35 $self->{serial_port} = new Device::SerialPort($self->{port});
36 $self->{serial_port}->baudrate(2400);
37 $self->{serial_port}->databits(8);
38 $self->{serial_port}->parity('none');
39 $self->{serial_port}->stopbits(1);
40 $self->{serial_port}->handshake('none');
41 $self->{serial_port}->stty_echo(0);
42  
43 $self->{serial_port}->read_const_time(2000);
44 $self->{serial_port}->read_char_time(5);
45 }
46  
47 return $self;
48 }
49  
50  
51 ### public methods (most overriding parent class)
52  
53 sub select_fds
54 {
55 my $self = shift;
56  
57 if ($self->{test})
58 {
59 return ();
60 }
61 else
62 {
63 return ($self->{serial_port}->{FD});
64 }
65 }
66  
67 sub handle_input
68 {
69 my $self = shift;
70  
71 return if $self->{test};
72  
73 $self->get_word;
74 }
75  
76 ### mostly-private methods...
77  
78 sub send_word
79 {
80 my $self = shift;
81 my $word = shift;
82  
83 my $good = 0;
84 my $bad = 0;
85  
86 while ($bad < 5 && !$good)
87 {
88 write_paced($self->{serial_port}, 100, $word, "\r") unless $self->{test};
89  
90 if ($self->get_ack($word))
91 {
92 $good = 1;
93 }
94 else
95 {
96 $bad++;
97 }
98 }
99  
100 if ($bad >= 5)
101 {
102 warn "Too many bad -- aborting!";
103 }
104  
105 }
106  
107 sub get_ack
108 {
109 my $self = shift;
110 my $word = shift;
111  
112 return 0 unless $word;
113  
114 # this is done so we can fake out the event handlers when we're in test mode
115 if ($self->{test})
116 {
117 $self->got_words($word);
118 return 1;
119 }
120 else
121 {
122 my $got = $self->get_word;
123 return ($got eq $word);
124 }
125 }
126  
127 sub get_word
128 {
129 my $self = shift;
130 my $buf = '';
131 my $word;
132  
133 READ_BYTE:
134 while (my ($count, $char) = $self->{serial_port}->read(1) )
135 {
136 $buf .= $char;
137  
138 if ($buf =~ /!([a-p](\d\d|on|of|l1|a0|br|di))\r\n/is)
139 {
140 $word = $1;
141 last READ_BYTE;
142 }
143 }
144  
145 # NOTE: data in $buf is discarded if we timeout before finding a word
146  
147 $self->{logger}->('info', "Word: %s", $word) if $self->{verbose};
148  
149 $self->got_words($word) if $word;
150  
151 return $word;
152 }
153  
154 sub DESTROY
155 {
156 my $self = shift;
157 $self->{serial_port}->close;
158 }
159  
160 ### utility functions -- not called as methods
161  
162 sub write_paced
163 {
164 my $serial_port = shift;
165 my $pace = shift;
166  
167 foreach ( map { split(//) } @_ )
168 {
169 select(undef, undef, undef, $pace/2000);
170 $serial_port->write($_);
171 select(undef, undef, undef, $pace/2000);
172 }
173 }
174  
175 sub usleep
176 {
177 my $usecs = shift;
178 select(undef, undef, undef, $usecs / 1000000);
179 }
180  
181 1;
182