blib/lib/ZWave/Protocol.pm

filter: Statementstmt 25.0% 30 / 120 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.0% 9 / 20 Podpod 66.6% 8 / 12 totaltotal 26.5% 47 / 177 CC 14 SCAR 20.0
CC
14
Cov
21%
ack_recv
20.0
connect
12.0
bytes_dump
12.0
CRAP 7.4
↑ Summary
1 package ZWave::Protocol;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Device::SerialPort;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = "0.02";
9  
10 1 ▶has device => ( is => 'rw', default => sub { "/dev/ttyUSB0" } );
Subroutine __ANON__: called CC=1 SCAR=0.0
11 1 has error => ( is => 'rw' );
12 1 has port => ( is => 'rw' );
13 1 ▶has read_timeout_ms => ( is => 'rw', default => sub { "200" } );
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 ▶has ack_timeout_ms => ( is => 'rw', default => sub { "5000" } );
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 sub connect {
17 0 ▶ my( $self ) = @_;
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
18  
19 0 my $port = Device::SerialPort->new( $self->device, 1 );
20  
21 0 ▶ if( !defined $port ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
22 0 ERROR "Can't connect to ", $self->device;
23 0 return undef;
24 }
25  
26 0 $port->baudrate( 115200 );
27 0 $port->databits( 8 );
28 0 $port->parity( "none" );
29 0 $port->stopbits( 1 );
30 0 $port->handshake( "none" );
31 0 $port->dtr_active( 1 );
32  
33 0 $port->error_msg( 1 );
34 0 $port->user_msg( 0 );
35  
36 0 ▶ $port->write_settings or
Branch: 1 branch
BranchTrueFalseTotal
$port->write_settings or die 'Failed to initialize USB port ' . $self->device . ": $@" 0 0 0
37 die "Failed to initialize USB port " . $self->device . ": $@";
38  
39 0 $self->port( $port );
40  
41 0 return 1;
42 }
43  
44 sub checksum {
45 1 ▶ my( $self, @bytes ) = @_;
Subroutine checksum: called CC=2 SCAR=6.9
Pod documented
46  
47 1 my $checksum = 0xFF;
48  
49 # x-or all elements but the first one
50 1 for my $byte ( splice @bytes, 1, @bytes - 1 ) {
51 2 $checksum ^= $byte;
52 }
53  
54 1 return $checksum;
55 }
56  
57 sub packet_dump {
58 0 ▶ my( $self, $packet ) = @_;
Subroutine packet_dump: not called CC=1 SCAR=6.9
Pod undocumented
59  
60 0 my @bytes = unpack "C*", $packet;
61  
62 0 return $self->bytes_dump( @bytes );
63 }
64  
65 sub bytes_dump {
66 0 ▶ my( $self, @bytes ) = @_;
Subroutine bytes_dump: not called CC=3 SCAR=24.8
Pod undocumented
67  
68 0 my $string = "";
69  
70 0 for my $byte ( @bytes ) {
71 0 ▶ if( length $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 0 0 0
72 0 $string .= " ";
73 }
74 0 $string .= sprintf "%02x", $byte;
75 }
76  
77 0 return "[ $string ]";
78 }
79  
80 sub request_packet {
81 0 ▶ my( $self, @payload ) = @_;
Subroutine request_packet: not called CC=1 SCAR=6.9
Pod documented
82  
83 0 my $length = @payload + 1;
84  
85 0 my @bytes = ( 0x1, $length, @payload );
86 0 my $checksum = $self->checksum( @bytes );
87  
88 0 DEBUG "Checksum of ", $self->bytes_dump( @bytes ), " is ",
89 $self->bytes_dump( $checksum );
90  
91 0 push @bytes, $checksum;
92  
93 0 return pack "C*", @bytes;
94 }
95  
96 sub payload_send {
97 0 ▶ my( $self, @payload ) = @_;
Subroutine payload_send: not called CC=2 SCAR=17.9
Pod documented
98  
99 0 my $request = $self->request_packet( @payload );
100  
101 0 DEBUG "Sending request: ", $self->packet_dump( $request );
102  
103 0 ▶ if( !$self->port->write( $request ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->port->write($request)) 0 0 0
104 0 $self->error( "Failed to send payload " .
105 $self->bytes_dump( @payload ) . ": $@" );
106 0 return undef;
107 }
108  
109 0 return 1;
110 }
111  
112 sub ack_recv {
113 0 ▶ my( $self ) = @_;
Subroutine ack_recv: not called CC=4 SCAR=30.0
Pod documented
114  
115 0 DEBUG "Waiting for ACK";
116  
117 0 my $packet = $self->packet_recv( );
118 0 my @bytes = unpack "C*", $packet;
119  
120 0 DEBUG "ACK bytes: ", $self->bytes_dump( @bytes );
121  
122 0 ▶ if( @bytes == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bytes == 0) 0 0 0
123 0 INFO "Received nothing";
124 0 return 0;
125 }
126  
127 0 ▶ if( defined $bytes[ 0 ] and $bytes[ 0 ] == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes[0] and $bytes[0] == 6) 0 0 0
Condition0%0/3
defined $bytes[0] and $bytes[0] == 6
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes[0] and $bytes[0] == 6
defined $bytes[0] $bytes[0] == 6
Truth table0%0/3
A and B
A= defined $bytes[0]B= $bytes[0] == 6
ABresult
0X0
100
111
128 0 INFO "Received ACK";
129 0 $self->ack_send();
130 0 return 1;
131 }
132  
133 0 INFO "Received non-ACK";
134  
135 0 return undef;
136 }
137  
138 sub ack_send {
139 0 ▶ my( $self, $node_id ) = @_;
Subroutine ack_send: not called CC=1 SCAR=6.9
Pod documented
140  
141 0 INFO "Sending ACK";
142 0 $self->port->write( pack( "C", 6 ) );
143  
144 0 return 1;
145 }
146  
147 sub payload_transmit {
148 0 ▶ my( $self, @payload ) = @_;
Subroutine payload_transmit: not called CC=3 SCAR=24.8
Pod documented
149  
150 0 ▶ if( !$self->payload_send( @payload ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->payload_send(@payload)) 0 0 0
151 0 return undef;
152 }
153  
154 0 ▶ if( !$self->ack_recv() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ack_recv) 0 0 0
155 0 $self->error( "Failed to receive ack for sent payload @payload: $@" );
156 0 return undef;
157 }
158  
159 0 return 1;
160 }
161  
162 sub error_process {
163 0 ▶ my( $self, $string ) = @_;
Subroutine error_process: not called CC=1 SCAR=6.9
Pod undocumented
164  
165 0 local $Log::Log4perl::caller_depth =
166 $Log::Log4perl::caller_depth + 1;
167  
168 0 ERROR $string;
169 0 $self->error( $string );
170  
171 0 return 1;
172 }
173  
174 sub packet_recv {
175 0 ▶ my( $self ) = @_;
Subroutine packet_recv: not called CC=2 SCAR=17.9
Pod undocumented
176  
177 0 $self->port->read_const_time( $self->read_timeout_ms );
178  
179 # read one byte at a time until there's no more
180 0 my $sofar = "";
181 0 while( 1 ) {
182 0 my( $count, $bytes ) = $self->port->read( 1 );
183 0 DEBUG "Read $count bytes: ", $self->packet_dump( $bytes );
184 0 $sofar .= $bytes;
185 0 ▶ if( !$count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 0 0 0
186 0 last;
187 }
188 }
189  
190 0 DEBUG "Read packet: ", $self->packet_dump( $sofar );
191  
192 0 return $sofar;
193 }
194  
195 sub payload_recv {
196 0 ▶ my( $self ) = @_;
Subroutine payload_recv: not called CC=2 SCAR=17.9
Pod documented
197  
198 0 my @bytes = unpack "C*", $self->packet_recv;
199  
200 0 my( $type, $len, @rest ) = @bytes;
201  
202 0 my $checksum = pop @rest;
203  
204 0 ▶ if( $checksum != $self->checksum( $len, @rest ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksum != $self->checksum($len, @rest)) 0 0 0
205 0 $self->error_process( "Received package with invalid checksum: " .
206 $self->bytes_dump( @bytes ) . " (checksum should be " .
207 $self->bytes_dump( $checksum ) . ")" );
208 0 return undef;
209 }
210  
211 0 DEBUG "Received payload: ", $self->bytes_dump( @rest );
212  
213 0 return @rest;
214 }
215  
216 1 1;
217  
218 __END__
219  
220 =head1 NAME
221  
222 ZWave::Protocol - Protocol helpers for Z-Wave communication
223  
224 =head1 SYNOPSIS
225  
226 use ZWave::Protocol;
227 my $zw = ZWave::Protocol->new( device => "/dev/ttyUSB0" );
228  
229 $zw->connect();
230  
231 my $node_id = 3;
232 my $state = 255; # "on"
233  
234 $zw->transmit( 0x13, $node_id,
235 0x03, 0x20, 0x01, $state, 0x05 );
236  
237 =head1 DESCRIPTION
238  
239 TODO: Retries
240  
241 ZWave::Protocol helps with the low-level details of the Z-Wave protocol, and
242 offers support for packing packets with length headers and checksums, as well
243 as connecting, sending, and receiving to the Z-Wave controller plugged into the
244 USB port.
245  
246 =head1 METHODS
247  
248 =over 4
249  
250 =item C<new( device => "/dev/ttyUSB0" )>
251  
252 Constructor, takes the device path to the plugged in Z-Wave controller.
253  
254 =item C<connect()>
255  
256 Initialize a connection with the Z-Wave controller plugged into the USB port.
257  
258 =item C<payload_transmit( $payload_byte1, $payload_byte2, ... )>
259  
260 A combination of C<send() and C<recv_ack()>.
261  
262 =item C<payload_send( $payload_byte1, $payload_byte2, ... )>
263  
264 Wrap the given payload bytes into a package and send the result over to
265 the USB port.
266  
267 =item C<payload_recv()>
268  
269 Wait for a payload packet to arrive and receive it.
270  
271 =item C<ack_recv()>
272  
273 Wait for an ACK to arrive from the recipient of the previous C<send()>.
274  
275 =item C<ack_send()>
276  
277 Send an ACK back to acknowledge receiving a packet.
278  
279 =item C<request_packet( $payload_byte1, $payload_byte2, ... )>
280  
281 Packs a sequence of payload bytes into a request packet, by adding a request
282 header, packet length, and a trailing checksum. For example,
283  
284 0x00, 0x13, 0x03, 0x03, 0x20, 0x01, 0x00, 0x05
285  
286 becomes
287  
288 0x01, 0x09, 0x00, 0x13, 0x03, 0x03, 0x20, 0x01, 0x00, 0x05, 0xc1
289  
290 with C<0x01> being the packet header, C<0x09> being the packet length,
291 and C<0xc1> being the checksum, which is calculated over all bytes
292 except the first one (see the ZWave protocol spec for details).
293  
294 =item C<checksum( $byte1, $byte2, ... )>
295  
296 Calculate the Z-Wave checksum required at the end of a package.
297  
298 =back
299  
300 =head2 ERROR HANDLING
301  
302 If one of the methods above returns a non-true value, the underlying
303 error can be obtained by calling
304  
305 print $zwave->error();
306  
307 Additional insight can be obtained by bumping up the Log4perl level
308 to $DEBUG in the C<CWave::Protocol> or root categories.
309  
310 =head1 LEGALESE
311  
312 Copyright 2015 by Mike Schilli, all rights reserved.
313 This program is free software, you can redistribute it and/or
314 modify it under the same terms as Perl itself.
315  
316 =head1 AUTHOR
317  
318 2015, Mike Schilli <m@perlmeister.com>
↑ Summary