X10/ActiveHome.pm untested

filter: Statementstmt 0.0% 0 / 196 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 323 CC 39 SCAR 43.6
CC
39
Cov
0%
ah_sendpacket
210.0
send_word
72.0
handle_input
56.0
CRAP 78.6
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::ActiveHome;
6  
7 # Perl module for communicating with an X10 ActiveHome interface
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 use X10::Event;
18  
19 use vars qw( @ISA %hc_bits %uc_bits %fn_bits %reverse_bits );
20  
21 @ISA = qw( X10::Controller );
22  
23 %hc_bits = (
24 A => 0x60,
25 B => 0xe0,
26 C => 0x20,
27 D => 0xa0,
28 E => 0x10,
29 F => 0x90,
30 G => 0x50,
31 H => 0xd0,
32 I => 0x70,
33 J => 0xf0,
34 K => 0x30,
35 L => 0xb0,
36 M => 0x00,
37 N => 0x80,
38 O => 0x40,
39 P => 0xc0,
40 );
41  
42 %uc_bits = (
43 1 => 0x06,
44 2 => 0x0e,
45 3 => 0x02,
46 4 => 0x0a,
47 5 => 0x01,
48 6 => 0x09,
49 7 => 0x05,
50 8 => 0x0d,
51 9 => 0x07,
52 10 => 0x0f,
53 11 => 0x03,
54 12 => 0x0b,
55 13 => 0x00,
56 14 => 0x08,
57 15 => 0x04,
58 16 => 0x0c,
59 );
60  
61 %fn_bits = (
62 A0 => 0x00, # all off
63 L1 => 0x01, # lights on
64 ON => 0x02, # on
65 OF => 0x03, # off
66 DI => 0x04, # dim
67 BR => 0x05, # bright
68 L0 => 0x06, # lights off
69 EC => 0x07, # extended code
70 HR => 0x08, # hail request
71 HA => 0x09, # hail acknowledge
72 P1 => 0x0a, # preset dim 1
73 P2 => 0x0b, # preset dim 2
74 ED => 0x0c, # extended data
75 S1 => 0x0d, # status on
76 S0 => 0x0e, # status off
77 SR => 0x0f, # status request
78 );
79  
80 %reverse_bits = (
81 0 => [ 'M', 13, 'A0' ],
82 1 => [ 'E', 5, 'L1' ],
83 2 => [ 'C', 3, 'ON' ],
84 3 => [ 'K', 11, 'OF' ],
85 4 => [ 'O', 15, 'DI' ],
86 5 => [ 'G', 7, 'BR' ],
87 6 => [ 'A', 1, 'L0' ],
88 7 => [ 'I', 9, 'EC' ],
89 8 => [ 'N', 14, 'HR' ],
90 9 => [ 'F', 6, 'HA' ],
91 10 => [ 'D', 4, 'P1' ],
92 11 => [ 'L', 12, 'P2' ],
93 12 => [ 'P', 16, 'ED' ],
94 13 => [ 'H', 8, 'S1' ],
95 14 => [ 'B', 2, 'S0' ],
96 15 => [ 'J', 10, 'SR' ],
97 );
98  
99  
100 ### constructors
101  
102 sub new
103 {
104 my $type = shift;
105  
106 my $self = new X10::Controller( @_ );
107 bless $self, $type;
108  
109 unless ($self->{test})
110 {
111 return undef unless $self->{port};
112  
113 $self->{serial_port} = new Device::SerialPort($self->{port});
114 $self->{serial_port}->baudrate(4800);
115 $self->{serial_port}->databits(8);
116 $self->{serial_port}->parity('none');
117 $self->{serial_port}->stopbits(1);
118 $self->{serial_port}->handshake('none');
119 $self->{serial_port}->stty_echo(0);
120  
121 $self->{serial_port}->read_const_time(5000);
122 $self->{serial_port}->read_char_time(5);
123 }
124  
125 $self->{output_word_queue} = [];
126 $self->{states} = {};
127 $self->{working} = 0; # used as a lock
128  
129 return $self;
130 }
131  
132  
133 ### public methods (most overriding parent class)
134  
135 sub select_fds
136 {
137 my $self = shift;
138  
139 if ($self->{test})
140 {
141 return ();
142 }
143 else
144 {
145 return ($self->{serial_port}->{FD});
146 }
147 }
148  
149 sub handle_input
150 {
151 my $self = shift;
152  
153 # get a byte
154 my ($count, $byte) = $self->{serial_port}->read(1);
155  
156 return unless ($count);
157  
158 $byte = unpack("C", $byte);
159  
160 if ($byte == 0xa5)
161 {
162 $self->ah_settime;
163 }
164 elsif ($byte == 0x5a)
165 {
166 $self->get_ahwords;
167 }
168 elsif ($byte == 0x55)
169 {
170 $self->{logger}->('info', "Interface is ready") if $self->{debug};
171 $self->{is_ready} = 1;
172 }
173 else
174 {
175 $self->{logger}->('info', "Got 0x%02X -- not doing anything with it", $byte)
176 if $self->{debug};
177 }
178  
179 }
180  
181 ### mostly-private methods...
182  
183 sub send_word
184 {
185 my $self = shift;
186 my $word = uc(shift);
187  
188 my $good = 0;
189 my $bad = 0;
190  
191 $self->{logger}->('info', "Sending: %s", $word) if $self->{debug};
192  
193 my ($hc, $fn) = $word =~ /^(.)(..)$/;
194  
195 my $packet = '';
196  
197 if ($fn eq 'DI' || $fn eq 'BR')
198 {
199 $self->{logger}->('info', "Sending: %s", $word) if $self->{verbose};
200 # this sends ONE DIM/BRIGHT with a Header byte of 0x16
201 $packet = pack("CC", 0x16, $hc_bits{$hc} | $fn_bits{$fn});
202 $self->ah_sendpacket($packet);
203 }
204 elsif ($fn > 0 && $fn <= 16)
205 {
206 $self->{logger}->('info', "Sending: %s", $word) if $self->{verbose};
207 # my $uc = $fn * 1;
208 $packet = pack("CC", 0x04, ($hc_bits{$hc} | $uc_bits{$fn * 1}));
209 $self->ah_sendpacket($packet);
210 }
211 # elsif ($fn eq 'ON' || $fn eq 'OF')
212 elsif (exists $fn_bits{$fn})
213 {
214 $self->{logger}->('info', "Sending: %s", $word) if $self->{verbose};
215 $packet = pack("CC", 0x06, $hc_bits{$hc} | $fn_bits{$fn});
216 $self->ah_sendpacket($packet);
217 }
218 else
219 {
220 $self->{logger}->('info', "Unsupported word: %s", $word);
221 }
222  
223 }
224  
225  
226 ### private methods -- don't call these from ouside!
227  
228 sub work
229 {
230 my $self = shift;
231  
232 return if $self->{working};
233  
234 $self->{working} = 1;
235 while (my $word = shift @{$self->{output_word_queue}})
236 {
237 $self->send_word($word);
238  
239 $self->got_words($word); # AH interface doesn't feed back
240 }
241 $self->{working} = 0;
242 }
243  
244 sub get_ahwords
245 {
246 my $self = shift;
247  
248 $self->{working} = 1;
249  
250 $self->{logger}->('info', "Sending 'gimme events' byte") if $self->{debug};
251 $self->{serial_port}->write(chr(0xc3));
252  
253 my ($count, $bytes_to_read) = $self->{serial_port}->read(1);
254  
255 return 0 unless $count;
256  
257 $bytes_to_read = unpack("C", $bytes_to_read);
258  
259 return 0 if $bytes_to_read > 10;
260  
261 ($count, my $mask) = $self->{serial_port}->read(1);
262 $mask = unpack("C", $mask);
263 $bytes_to_read--;
264  
265 my @words = ();
266  
267 # foreach (1..$bytes_to_read)
268 for (my $i = 0; $i < $bytes_to_read; $i++)
269 {
270 my $char;
271 ($count, $char) = $self->{serial_port}->read(1);
272 $char = unpack("C", $char);
273  
274 my $fabit = (1 << $i) & $mask;
275  
276 if ($fabit) # function
277 {
278 my $hc = $reverse_bits{($char & 0xf0) >> 4}->[0];
279 my $fn = $reverse_bits{$char & 0x0f}->[2];
280  
281 push @words, sprintf("%1s%2s", $hc, $fn);
282  
283 if ($fn eq 'BR' || $fn eq 'DI')
284 {
285 # discard dim amount for now
286 my ($j1, $j2) = $self->{serial_port}->read(1);
287 $i++;
288 }
289 }
290 else # address
291 {
292 my $hc = $reverse_bits{($char & 0xf0) >> 4}->[0];
293 my $uc = $reverse_bits{$char & 0x0f}->[1];
294  
295 push @words, sprintf("%1s%02s", $hc, $uc);
296 }
297 }
298  
299 $self->got_words($self->optimize_wordlist(@words)) if @words;
300  
301 $self->{working} = 0;
302 $self->work;
303  
304 }
305  
306 sub ah_settime
307 {
308 my $self = shift;
309  
310 $self->{working} = 1;
311  
312 my $sum_ok = 0;
313 my $bad_sums = 0;
314  
315 my @time = localtime;
316  
317 my $seconds = $time[0];
318 my $minutes = $time[2] * 60 + $time[1];
319 my $hours = int($minutes / 12);
320 $minutes %= 120;
321 my $yday = $time[7];
322 my $wday = $time[6] % 7;
323  
324 $self->{logger}->('info', "Setting interface time/date...") if $self->{verbose};
325  
326 $self->{serial_port}->write(chr(0x9b));
327  
328 my $packet = pack("CCCCCC",
329 0,
330 $minutes,
331 $hours,
332 $yday / 2,
333 (($yday % 1) << 7) & (1 << $wday),
334 0x90,
335 );
336  
337 $self->ah_sendpacket($packet);
338  
339 $self->{working} = 0;
340 $self->work;
341 }
342  
343 sub ah_sendpacket
344 {
345 my $self = shift;
346 my $packet = shift;
347  
348 my $checksum = 0;
349 foreach (split(//, $packet))
350 {
351 $checksum += ord($_);
352 }
353 $checksum &= 0xff;
354  
355 my $sum_ok = 0;
356 my $bad_sums = 0;
357  
358 TRY:
359 while ($bad_sums < 3 && !$sum_ok)
360 {
361 $self->{logger}->('info', "Sending %s-byte packet: %s",
362 length($packet),
363 join(", ", map { sprintf("0x%02X", ord($_)) } split(//, $packet) ),
364 ) if $self->{debug};
365 $self->{serial_port}->write($packet);
366  
367 my ($count, $sum_from_device) = $self->{serial_port}->read(1);
368  
369 if ($count == 0)
370 {
371 $self->{logger}->('info', "Got nothing back from the device...")
372 if $self->{debug};
373 $bad_sums++;
374 }
375 else
376 {
377  
378 $sum_from_device = unpack("C", $sum_from_device);
379  
380 if ($checksum == $sum_from_device)
381 {
382 $self->{logger}->('info', "Good send, sending 0x00 as OK")
383 if $self->{debug};
384 $self->{serial_port}->write(chr(0x00));
385  
386 ($count, my $readybyte) = $self->{serial_port}->read(1);
387 if ($count != 1)
388 {
389 $self->{logger}->('info', "Didn't get ready byte")
390 if $self->{debug};
391 $bad_sums++;
392 }
393 elsif (ord($readybyte) != 0x55)
394 {
395 $self->{logger}->('info', "Expected ready 0x55, got 0x%02X", $readybyte)
396 if $self->{debug};
397 $bad_sums++;
398 }
399 else
400 {
401 $self->{is_ready} = 1;
402 $sum_ok = 1;
403 }
404 }
405 else
406 {
407 $self->{logger}->('info', "Expected checksum 0x%02X, got 0x%02X",
408 $checksum, $sum_from_device) if $self->{debug};
409 $bad_sums++;
410 }
411 }
412 }
413  
414 if ($sum_ok)
415 {
416 $self->{logger}->('info', "Packet send successful") if $self->{debug};
417 }
418 else
419 {
420 $self->{logger}->('info', "Aborting send: too many bad checksums") if $self->{debug};
421 }
422  
423 return $sum_ok;
424 }
425  
426 sub ah_ring_signal
427 {
428 my $self = shift;
429 my $bool = shift || 0;
430  
431 $self->ah_sendpacket(chr($bool ? 0xeb : 0xdb));
432 }
433  
434 sub DESTROY
435 {
436 my $self = shift;
437  
438 $self->{serial_port}->close;
439 }
440  
441 ### utility functions -- not called as methods
442  
443  
444  
445 1;
446