blib/lib/OWNet.pm

filter: Statementstmt 12.8% 25 / 195 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 35.0% 7 / 20 Podpod 100% 5 / 5 totaltotal 9.2% 37 / 402 CC 77 SCAR 51.5
CC
77
Cov
7%
_new
552.0
dir
132.0
_BonjourLookup
90.0
CRAP 172.7
↑ Summary
1 package OWNet ;
2  
3 # OWNet module for perl
4 # $Id: OWNet.pm,v 1.8 2007/02/08 05:14:15 alfille Exp $
5 #
6 # Paul H Alfille -- copyright 2007
7 # Part of the OWFS suite:
8 # http://www.owfs.org
9 # http://owfs.sourceforge.net
10  
11 =head1 NAME
12  
13 B<OWNet>
14 Light weight access to B<owserver>
15  
16 =head1 SYNOPSIS
17  
18 OWNet is an easy way to access B<owserver> and thence the 1-wire bus.
19  
20 Dallas Semiconductor's 1-wire system uses simple wiring and unique addresses for it's interesting devices. The B<One Wire File System> is a suite of programs that hides 1-wire details behind a file system metaphor. B<owserver> connects to the 1-wire bus and provides network access.
21  
22 B<OWNet> is a perl module that connects to B<owserver> and allows reading, writing and listing the 1-wire bus.
23  
24 Then the following perl program prints the temperature:
25  
26 use OWNet ;
27 print OWNET::read( "localhost:3000" , "/10.67C6697351FF/temperature" ) ."\n" ;
28  
29 There is the alternative object oriented form:
30  
31 use OWNet ;
32 my $owserver = OWNET->new( "localhost:4304" ) ;
33 print $owserver->read( "/10.67C6697351FF/temperature" ) ."\n" ;
34  
35 =head1 SYNTAX
36  
37 =head2 methods
38  
39 =over
40  
41 =item B<new>
42  
43 my $owserver = OWNet -> new( address ) ;
44  
45 =item B<read>
46  
47 read( address, path )
48 $owserver -> read( path )
49  
50 =item B<write>
51  
52 write( address, path, value )
53 $owserver -> write( path, value )
54  
55 =item B<dir>
56  
57 dir( address, path )
58 $owserver -> dir( path )
59  
60 =item B<present>
61  
62 present( address, path )
63 $owserver -> present( path )
64  
65 =back
66  
67 =head2 I<address>
68  
69 TCP/IP I<address> of B<owserver>. Valid forms:
70  
71 =over
72  
73 =item I<name> test.owfs.net:3001
74  
75 =item I<quad> number: 123.231.312.213:3001
76  
77 =item I<host> localhost:3001
78  
79 =item I<port> 3001
80  
81 =back
82  
83 Temperature scale can also be specified in the I<address>. Same syntax as the other OWFS programs:
84  
85 =over
86  
87 =item -C Celsius (Centigrade)
88  
89 =item -F Fahrenheit
90  
91 =item -K Kelvin
92  
93 =item -R Rankine
94  
95 =back
96  
97 Device display format (1-wire unique address) can also be specified in the I<address>, with the general form of -ff[.]i[[.]c] (I<f>amily I<i>d I<c>rc):
98  
99 =over
100  
101 =item -ff.i /10.67C6697351FF (default)
102  
103 =item -ffi /1067C6697351FF
104  
105 =item -ff.i.c /10.67C6697351FF.8D
106  
107 =item -ff.ic /10.67C6697351FF8D
108  
109 =item -ffi.c /1067C6697351FF.8D
110  
111 =item -ffic /1067C6697351FF8D
112  
113 =back
114  
115 Warning messages will only be display if verbose flag is specified in I<address>
116  
117 =over
118  
119 =item -v verbose
120  
121 =back
122  
123 =head2 I<path>
124  
125 B<owfs>-type I<path> to an item on the 1-wire bus. Valid forms:
126  
127 =over
128  
129 =item main directories
130  
131 Used for the I<dir> method. E.g. "/" "/uncached" "/1F.321432320000/main"
132  
133 =item device directory
134  
135 Used for the I<dir> and I<present> method. E.g. "/10.4300AC220000" "/statistics"
136  
137 =item device properties
138  
139 Used to I<read>, I<write>. E.g. "/10.4300AC220000/temperature"
140  
141 =back
142  
143 =head2 I<value>
144  
145 New I<value> for a device property. Used by I<write>.
146  
147 =head1 METHODS
148  
149 =over
150  
151 =cut
152  
153 1 ▶BEGIN { }
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 1 ▶use 5.008 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 1 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159 1 ▶use IO::Socket::INET ;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 1 ▶use bytes ;
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 1 my $msg_read = 2 ;
163 1 my $msg_write = 3 ;
164 1 my $msg_dir = 4 ;
165 1 my $msg_presence = 6 ;
166 1 my $msg_dirall = 7 ;
167 1 my $msg_get = 8 ;
168  
169 1 my $persistence_bit = 0x04 ;
170 1 my $default_sg = 0x102 ;
171 1 my $default_block = 4096 ;
172  
173 #if ( !defined(&MSG_DONTWAIT) ) {
174 # sub MSG_DONTWAIT { return 0 ; }
175 #}
176  
177 1 our $VERSION=(split(/ /,q[$Revision: 1.8 $]))[1] ;
178  
179 0 ▶sub _new($$) {
Subroutine _new: not called CC=23 SCAR=63.1
180 0 my ($self,$addr) = @_ ;
181  
182 0 my $tempscale = 0 ;
183 TEMPSCALE: {
184 0 ▶ $tempscale = 0x00000 , last TEMPSCALE if $addr =~ /-C/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-C/ 0 0 0
185 0 ▶ $tempscale = 0x10000 , last TEMPSCALE if $addr =~ /-F/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-F/ 0 0 0
186 0 ▶ $tempscale = 0x20000 , last TEMPSCALE if $addr =~ /-K/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-K/ 0 0 0
187 0 ▶ $tempscale = 0x30000 , last TEMPSCALE if $addr =~ /-R/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-R/ 0 0 0
188 }
189  
190 0 my $format = 0 ;
191 FORMAT: {
192 0 ▶ $format = 0x2000000 , last FORMAT if $addr =~ /-ff\.i\.c/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-ff\.i\.c/ 0 0 0
193 0 ▶ $format = 0x4000000 , last FORMAT if $addr =~ /-ffi\.c/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-ffi\.c/ 0 0 0
194 0 ▶ $format = 0x3000000 , last FORMAT if $addr =~ /-ff\.ic/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-ff\.ic/ 0 0 0
195 0 ▶ $format = 0x5000000 , last FORMAT if $addr =~ /-ffic/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-ffic/ 0 0 0
196 0 ▶ $format = 0x0000000 , last FORMAT if $addr =~ /-ff\.i/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-ff\.i/ 0 0 0
197 0 ▶ $format = 0x1000000 , last FORMAT if $addr =~ /-ffi/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-ffi/ 0 0 0
198 0 ▶ $format = 0x2000000 , last FORMAT if $addr =~ /-f\.i\.c/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-f\.i\.c/ 0 0 0
199 0 ▶ $format = 0x4000000 , last FORMAT if $addr =~ /-fi\.c/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-fi\.c/ 0 0 0
200 0 ▶ $format = 0x3000000 , last FORMAT if $addr =~ /-f\.ic/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-f\.ic/ 0 0 0
201 0 ▶ $format = 0x5000000 , last FORMAT if $addr =~ /-fic/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-fic/ 0 0 0
202 0 ▶ $format = 0x0000000 , last FORMAT if $addr =~ /-f\.i/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-f\.i/ 0 0 0
203 0 ▶ $format = 0x1000000 , last FORMAT if $addr =~ /-fi/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-fi/ 0 0 0
204 }
205  
206 0 ▶ $self->{VERBOSE} = 1 if $addr =~ /-v/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /-v/ 0 0 0
207  
208 0 $addr =~ s/-[\w\.]*//g ;
209 0 $addr =~ s/ //g ;
210  
211 0 ▶ if ( $addr =~ /:/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($addr =~ /:/) { } 0 0 0
elsif ($addr =~ /\./) { } 0 0 0
elsif ($addr eq 'localhost') { } 0 0 0
212 0 ▶ $addr = "127.0.0.1".$addr if $addr =~ /^:/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^:/ 0 0 0
213 0 ▶ $addr = $addr."4304" if $addr =~ /:$/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /:$/ 0 0 0
214 } elsif ( $addr =~/\./ ) {
215 0 $addr .= ":4304" ;
216 } elsif ( $addr eq "localhost" ) {
217 0 $addr .= ":4304" ;
218 }
219  
220 0 $self->{ADDR} = $addr ;
221 0 $self->{SG} = $default_sg + $tempscale + $format ;
222 0 $self->{VER} = 0 ;
223 }
224  
225 0 ▶sub _Sock($) {
Subroutine _Sock: not called CC=5 SCAR=34.0
226 0 my $self = shift ;
227 # persistent socket already there?
228 0 ▶ if ( defined($self->{SOCK} && $self->{PERSIST} != 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($self->{"SOCK"} && $self->{"PERSIST"} != 0)) 0 0 0
Condition0%0/3
$self->{'SOCK'} && $self->{'PERSIST'} != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'SOCK'} && $self->{'PERSIST'} != 0
$self->{'SOCK'} $self->{'PERSIST'} != 0
Truth table0%0/3
A && B
A= $self->{'SOCK'}B= $self->{'PERSIST'} != 0
ABresult
0X0
100
111
229 0 return 1 ;
230 }
231 # New socket
232 0 ▶ $self->{SOCK} = IO::Socket::INET->new(PeerAddr=>$self->{ADDR},Proto=>'tcp') || do {
Condition0%0/3
'IO::Socket::INET'->new('PeerAddr', $self->{'ADDR'}, 'Proto', 'tcp') || do { warn "Can't open $self->{'ADDR'} ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
'IO::Socket::INET'->new('PeerAddr', $self->{'ADDR'}, 'Proto', 'tcp') || do { warn "Can't open $self->{'ADDR'} ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
'IO::Socket::INET'->new('PeerAddr', $self->{'ADDR'}, 'Proto', 'tcp') do { warn "Can't open $self->{'ADDR'} ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
Truth table0%0/3
A || B
A= 'IO::Socket::INET'->new('PeerAddr', $self->{'ADDR'}, 'Proto', 'tcp')B= do { warn "Can't open $self->{'ADDR'} ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
ABresult
1X1
011
000
233 0 ▶ warn("Can't open $self->{ADDR} ($!) \n") if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
234 0 $self->{SOCK} = undef ;
235 0 return ;
236 } ;
237 0 return 1 ;
238 }
239  
240 0 ▶sub _self($) {
Subroutine _self: not called CC=6 SCAR=37.4
241 0 my $addr = shift ;
242 0 my $self ;
243 0 ▶ if ( ref($addr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $addr) { } 0 0 0
244 0 $self = $addr ;
245 0 $self->{PERSIST} = $persistence_bit ;
246 } else {
247 0 $self = {} ;
248 0 _new($self,$addr) ;
249 0 $self->{PERSIST} = 0 ;
250 }
251 0 ▶ if ( $self->{ADDR} =~ // ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ADDR'} =~ //) { } 0 0 0
252 0 ▶ _DefaultLookup($self) || _BonjourLookup($self) || return ;
Branch: 1 branch
BranchTrueFalseTotal
&_DefaultLookup($self) or &_BonjourLookup($self) or return 0 0 0
Condition0%0/3
&_DefaultLookup($self) or &_BonjourLookup($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
&_DefaultLookup($self) or &_BonjourLookup($self)
&_DefaultLookup($self) &_BonjourLookup($self)
Truth table0%0/3
A or B
A= &_DefaultLookup($self)B= &_BonjourLookup($self)
ABresult
1X1
011
000
253 } else {
254 0 ▶ _Sock($self) || return ;
Branch: 1 branch
BranchTrueFalseTotal
&_Sock($self) or return 0 0 0
255 }
256 0 return $self;
257 }
258  
259 0 ▶sub _DefaultLookup($) {
Subroutine _DefaultLookup: not called CC=3 SCAR=24.8
260 0 my $self = shift ;
261 # New socket
262 0 ▶ $self->{SOCK} = IO::Socket::INET->new(PeerAddr=>"localhost:owserver(4304)",Proto=>'tcp') || do {
Condition0%0/3
'IO::Socket::INET'->new('PeerAddr', 'localhost:owserver(4304)', 'Proto', 'tcp') || do { warn "Can't open default port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
'IO::Socket::INET'->new('PeerAddr', 'localhost:owserver(4304)', 'Proto', 'tcp') || do { warn "Can't open default port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
'IO::Socket::INET'->new('PeerAddr', 'localhost:owserver(4304)', 'Proto', 'tcp') do { warn "Can't open default port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
Truth table0%0/3
A || B
A= 'IO::Socket::INET'->new('PeerAddr', 'localhost:owserver(4304)', 'Proto', 'tcp')B= do { warn "Can't open default port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
ABresult
1X1
011
000
263 0 ▶ warn("Can't open default port ($!) \n") if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
264 0 $self->{SOCK} = undef ;
265 0 return ;
266 } ;
267 0 $self->{ADDR} = $self->{SOCK}->peeraddr.":".$self->{SOCK}->peerport ;
268 0 return 1 ;
269 }
270  
271 0 ▶sub _BonjourLookup($) {
Subroutine _BonjourLookup: not called CC=9 SCAR=45.0
272 0 my $self = shift ;
273 0 eval { require Net::Rendezvous; };
274 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
275 0 ▶ print "$@\n" if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
276 0 return ;
277 }
278 0 ▶ my $owservers = Net::Rendezvous->new('owserver') || do {
Condition0%0/3
'Net::Rendezvous'->new('owserver') || do { print "Unable to start owserver discovery via Net::Rendezvous $!\n" if $self->{'VERBOSE'}; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
'Net::Rendezvous'->new('owserver') || do { print "Unable to start owserver discovery via Net::Rendezvous $!\n" if $self->{'VERBOSE'}; return }
'Net::Rendezvous'->new('owserver') do { print "Unable to start owserver discovery via Net::Rendezvous $!\n" if $self->{'VERBOSE'}; return }
Truth table0%0/3
A || B
A= 'Net::Rendezvous'->new('owserver')B= do { print "Unable to start owserver discovery via Net::Rendezvous $!\n" if $self->{'VERBOSE'}; return }
ABresult
1X1
011
000
279 0 ▶ print "Unable to start owserver discovery via Net::Rendezvous $!\n" if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
280 0 return ;
281 } ;
282 0 $owservers->discover ;
283 0 ▶ my $owserver_selected = $owservers->shift_entry || do {
Condition0%0/3
$owservers->shift_entry || do { print "No owserver discovered by Net::Rendezvous\n" if $self->{'VERBOSE'}; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
$owservers->shift_entry || do { print "No owserver discovered by Net::Rendezvous\n" if $self->{'VERBOSE'}; return }
$owservers->shift_entry do { print "No owserver discovered by Net::Rendezvous\n" if $self->{'VERBOSE'}; return }
Truth table0%0/3
A || B
A= $owservers->shift_entryB= do { print "No owserver discovered by Net::Rendezvous\n" if $self->{'VERBOSE'}; return }
ABresult
1X1
011
000
284 0 ▶ print "No owserver discovered by Net::Rendezvous\n" if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
285 0 return ;
286 } ;
287 0 print $owserver_selected->host.":".$owserver_selected->port."\n" ;
288 # New socket
289 0 ▶ $self->{SOCK} = IO::Socket::INET->new(PeerAddr=>$owserver_selected->host,PeerPort=>$owserver_selected->port,Proto=>'tcp') || do {
Condition0%0/3
'IO::Socket::INET'->new('PeerAddr', $owserver_selected->host, 'PeerPort', $owserver_selected->port, 'Proto', 'tcp') || do { warn "Can't open Bonjour (autodiscovered) port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
'IO::Socket::INET'->new('PeerAddr', $owserver_selected->host, 'PeerPort', $owserver_selected->port, 'Proto', 'tcp') || do { warn "Can't open Bonjour (autodiscovered) port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
'IO::Socket::INET'->new('PeerAddr', $owserver_selected->host, 'PeerPort', $owserver_selected->port, 'Proto', 'tcp') do { warn "Can't open Bonjour (autodiscovered) port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
Truth table0%0/3
A || B
A= 'IO::Socket::INET'->new('PeerAddr', $owserver_selected->host, 'PeerPort', $owserver_selected->port, 'Proto', 'tcp')B= do { warn "Can't open Bonjour (autodiscovered) port ($!) \n" if $self->{'VERBOSE'}; $self->{'SOCK'} = undef; return }
ABresult
1X1
011
000
290 0 ▶ warn("Can't open Bonjour (autodiscovered) port ($!) \n") if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
291 0 $self->{SOCK} = undef ;
292 0 return ;
293 } ;
294 0 $self->{ADDR} = $self->{SOCK}->peeraddr.":".$self->{SOCK}->peerport ;
295 0 return 1 ;
296 }
297  
298 0 ▶sub _ToServer ($$$$$;$) {
Subroutine _ToServer: not called CC=7 SCAR=40.3
299 0 my ($self, $payload_length, $msg_type, $size, $offset, $payload_data) = @_ ;
300 0 my $f = "N6" ;
301 0 ▶ $f .= 'Z'.$payload_length if ( $payload_length > 0 ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $payload_length > 0 0 0 0
302 0 my $message = pack($f,$self->{VER},$payload_length,$msg_type,$self->{SG}|$self->{PERSIST},$size,$offset,$payload_data) ;
303  
304 # try to send
305 # send( $self->{SOCK}, $message, MSG_DONTWAIT ) && return 1 ;
306 0 ▶ send( $self->{SOCK}, $message, 0 ) && return 1 ;
Branch: 1 branch
BranchTrueFalseTotal
send $self->{"SOCK"}, $message, 0 and return 1 0 0 0
307  
308 # maybe bad persistent connection
309 0 ▶ if ( $self->{PERSIST} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PERSIST"} != 0) 0 0 0
310 0 $self->{SOCK} = undef ;
311 0 ▶ _Sock($self) || return ;
Branch: 1 branch
BranchTrueFalseTotal
&_Sock($self) or return 0 0 0
312 # send( $self->{SOCK}, $message, MSG_DONTWAIT ) && return 1 ;
313 0 ▶ send( $self->{SOCK}, $message, 0 ) && return 1 ;
Branch: 1 branch
BranchTrueFalseTotal
send $self->{"SOCK"}, $message, 0 and return 1 0 0 0
314 }
315  
316 0 ▶ warn("Send problem $! \n") if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
317 0 return ;
318 }
319  
320 0 ▶sub _FromServerLow($$) {
Subroutine _FromServerLow: not called CC=6 SCAR=37.4
321 0 my $self = shift ;
322 0 my $length_wanted = shift ;
323 0 ▶ return '' if $length_wanted == 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $length_wanted == 0 0 0 0
324 0 my $fileno = $self->{SOCK}->fileno ;
325 0 my $selectreadbits = '' ;
326 0 vec($selectreadbits,$fileno,1) = 1 ;
327 0 my $remaininglength = $length_wanted ;
328 0 my $fullread = '' ;
329 #print "LOOOP for length $length \n" ;
330 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$remaininglength > 0 and do { do { select $selectreadbits, undef, undef, 1; return if vec($selectreadbits, $fileno, 1) == 0; my $partialread; defined recv($self->{"SOCK"}, $partialread, $remaininglength, 0) or do { warn "Trouble getting data back $! after $remaininglength of $length_wanted" if $self->{"VERBOSE"}; return }; $fullread .= $partialread; $remaininglength = $length_wanted - length($fullread) } } 0 0 0
331 0 select($selectreadbits,undef,undef,1) ;
332 0 ▶ return if vec($selectreadbits,$fileno,1) == 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if vec($selectreadbits, $fileno, 1) == 0 0 0 0
333 # return if $sel->can_read(1) == 0 ;
334 0 my $partialread ;
335 # defined( recv( $self->{SOCK}, $partialread, $remaininglength, MSG_DONTWAIT ) ) || do {
336 0 ▶ defined( recv( $self->{SOCK}, $partialread, $remaininglength, 0 ) ) || do {
Branch: 1 branch
BranchTrueFalseTotal
defined recv($self->{"SOCK"}, $partialread, $remaininglength, 0) or do { warn "Trouble getting data back $! after $remaininglength of $length_wanted" if $self->{"VERBOSE"}; return } 0 0 0
337 0 ▶ warn("Trouble getting data back $! after $remaininglength of $length_wanted") if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
338 0 return ;
339 } ;
340 #print "reading=".$a."\n";
341 #print " length a=".length($a)." length ret=".length($ret)." length a+ret=".length($a.$ret)." \n" ;
342 0 $fullread .= $partialread ;
343 0 $remaininglength = $length_wanted - length($fullread) ;
344 #print "_FromServerLow (a.len=".length($a)." $len of $length \n" ;
345 } while $remaininglength > 0 ;
346 0 return $fullread ;
347 }
348  
349 0 ▶sub _FromServer($) {
Subroutine _FromServer: not called CC=7 SCAR=40.3
350 0 my $self = shift ;
351 0 my ( $version, $payload_length, $return_status, $sg, $size, $offset, $payload_data ) ;
352 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$payload_length > 66000 and do { do { my $r = &_FromServerLow($self, 24) || do { warn "Trouble getting header $!" if $self->{"VERBOSE"}; return }; () = unpack("N6", $r); return if $return_status > 66000 } } 0 0 0
353 0 ▶ my $r = _FromServerLow( $self,24 ) || do {
Condition0%0/3
&_FromServerLow($self, 24) || do { warn "Trouble getting header $!" if $self->{'VERBOSE'}; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
&_FromServerLow($self, 24) || do { warn "Trouble getting header $!" if $self->{'VERBOSE'}; return }
&_FromServerLow($self, 24) do { warn "Trouble getting header $!" if $self->{'VERBOSE'}; return }
Truth table0%0/3
A || B
A= &_FromServerLow($self, 24)B= do { warn "Trouble getting header $!" if $self->{'VERBOSE'}; return }
ABresult
1X1
011
000
354 0 ▶ warn("Trouble getting header $!") if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
355 0 return ;
356 } ;
357 0 ($version, $payload_length, $return_status, $sg, $size, $offset) = unpack('N6', $r ) ;
358 # returns unsigned (though originals signed
359 # assume anything above 66000 is an error
360 0 ▶ return if $return_status > 66000 ;
Branch: 1 branch
BranchTrueFalseTotal
if $return_status > 66000 0 0 0
361 #print "From Server, size = $siz, ret = $ret payload = $pay \n" ;
362 } while $payload_length > 66000 ;
363 0 $payload_data = _FromServerLow( $self,$payload_length ) ;
364 0 ▶ if ( !defined($payload_data) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $payload_data) 0 0 0
365 0 ▶ warn("Trouble getting payload $!") if $self->{VERBOSE} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"VERBOSE"} 0 0 0
366 0 return ;
367 } ;
368 0 $payload_data = substr($payload_data,0,$size) ;
369 #print "From Server, payload retrieved <$dat> \n" ;
370 0 $self->{PERSIST} = $sg & $persistence_bit ;
371 0 return ($version, $payload_length, $return_status, $sg, $size, $offset, $payload_data ) ;
372 }
373  
374 =item I<new>
375  
376 B<new>( I<address> )
377  
378 Create a new OWNet object -- corresponds to an B<owserver>.
379  
380 Error (and undef return value) if:
381  
382 =over
383  
384 =item 1 Badly formed tcp/ip I<address>
385  
386 =item 2 No <B>owserver at I<address>
387  
388 =back
389  
390 =cut
391  
392 0 ▶sub new($$) {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
393 0 my $class = shift ;
394 0 ▶ my $addr = shift || "" ;
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
395 0 my $self = {} ;
396 0 _new($self,$addr) ;
397 0 ▶ if ( !defined($self->{ADDR}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ADDR"}) 0 0 0
398 0 return ;
399 } ;
400 0 bless $self, $class ;
401 0 return $self ;
402 }
403  
404 =item I<read>
405  
406 =over
407  
408 =item Non object-oriented:
409  
410 B<read>( I<address> , I<path> )
411  
412 =item Object-oriented:
413  
414 $ownet->B<read>( I<path> )
415  
416 =back
417  
418  
419 Read the value of a 1-wire device property. Returns the (scalar string) value of the property.
420  
421 Error (and undef return value) if:
422  
423 =over
424  
425 =item 1 (Non object) No B<owserver> at I<address>
426  
427 =item 2 (Object form) Not called with a valid OWNet object
428  
429 =item 3 Bad I<path>
430  
431 =item 4 I<path> not a readable device property
432  
433 =back
434  
435 =cut
436  
437 0 ▶sub read($$) {
Subroutine read: not called CC=3 SCAR=24.8
Pod documented
438 0 ▶ my $self = _self(shift) || return ;
Condition0%0/2
&_self(shift()) || (return)
l!l
00
MC/DC0%0/1
&_self(shift()) || (return)
&_self(shift())
Truth table0%0/2
A
A= &_self(shift())
Aresult
11
00
439 0 my $path = shift ;
440 0 _ToServer($self,length($path)+1,$msg_read,$default_block,0,$path) ;
441 0 my @r = _FromServer($self) ;
442 0 ▶ return if !@r ;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 0 0 0
443 0 return $r[6] ;
444 }
445  
446 =item I<write>
447  
448 =over
449  
450 =item Non object-oriented:
451  
452 B<write>( I<address> , I<path> , I<value> )
453  
454 =item Object-oriented:
455  
456 $ownet->B<write>( I<path> , I<value> )
457  
458 =back
459  
460 Set the value of a 1-wire device property. Returns "1" on success.
461  
462 Error (and undef return value) if:
463  
464 =over
465  
466 =item 1 (Non object) No B<owserver> at I<address>
467  
468 =item 2 (Object form) Not called with a valid OWNet object
469  
470 =item 3 Bad I<path>
471  
472 =item 4 I<path> not a writable device property
473  
474 =item 5 I<value> incorrect size or format
475  
476 =back
477  
478 =cut
479  
480 0 ▶sub write($$$) {
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
481 0 ▶ my $self = _self(shift) || return ;
Condition0%0/2
&_self(shift()) || (return)
l!l
00
MC/DC0%0/1
&_self(shift()) || (return)
&_self(shift())
Truth table0%0/2
A
A= &_self(shift())
Aresult
11
00
482 0 my $path = shift ;
483 0 my $val = shift ;
484  
485 0 my $value_length = length($val) ;
486 0 my $path_length = length($path)+1 ;
487 0 my $payload = pack( 'Z'.$path_length.'A'.$value_length,$path,$val ) ;
488 0 _ToServer($self,length($payload),$msg_write,$value_length,0,$payload) ;
489 0 my @r = _FromServer($self) ;
490 0 ▶ return if !@r ;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 0 0 0
491 0 return $r[2]>=0 ;
492 }
493  
494 =item I<present>
495  
496 =over
497  
498 =item Non object-oriented:
499  
500 B<present>( I<address> , I<path> )
501  
502 =item Object-oriented:
503  
504 $ownet->B<present>( I<path> )
505  
506 =back
507  
508 Test if a 1-wire device exists.
509  
510 Error (and undef return value) if:
511  
512 =over
513  
514 =item 1 (Non object) No B<owserver> at I<address>
515  
516 =item 2 (Object form) Not called with a valid OWNet object
517  
518 =item 3 Bad I<path>
519  
520 =item 4 I<path> not a device
521  
522 =back
523  
524 =cut
525  
526 0 ▶sub present($$) {
Subroutine present: not called CC=3 SCAR=24.8
Pod documented
527 0 ▶ my $self = _self(shift) || return ;
Condition0%0/2
&_self(shift()) || (return)
l!l
00
MC/DC0%0/1
&_self(shift()) || (return)
&_self(shift())
Truth table0%0/2
A
A= &_self(shift())
Aresult
11
00
528 0 my $path = shift ;
529 0 _ToServer($self,length($path)+1,$msg_presence,$default_block,0,$path) ;
530 0 my @r = _FromServer($self) ;
531 0 ▶ return if !@r ;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 0 0 0
532 0 return $r[2]>=0 ;
533 }
534  
535 =item I<dir>
536  
537 =over
538  
539 =item Non object-oriented:
540  
541 B<dir>( I<address> , I<path> )
542  
543 =item Object-oriented:
544  
545 $ownet->B<dir>( I<path> )
546  
547 =back
548  
549 Return a comma-separated list of the entries in I<path>. Entries are equivalent to "fully qualified names" -- full path names.
550  
551 Error (and undef return value) if:
552  
553 =over
554  
555 =item 1 (Non object) No B<owserver> at I<address>
556  
557 =item 2 (Object form) Not called with a valid OWNet object
558  
559 =item 3 Bad I<path>
560  
561 =item 4 I<path> not a directory
562  
563 =back
564  
565 =cut
566  
567 0 ▶sub dir($$) {
Subroutine dir: not called CC=11 SCAR=48.8
Pod documented
568 0 ▶ my $self = _self(shift) || return ;
Condition0%0/2
&_self(shift()) || (return)
l!l
00
MC/DC0%0/1
&_self(shift()) || (return)
&_self(shift())
Truth table0%0/2
A
A= &_self(shift())
Aresult
11
00
569 0 my $path = shift ;
570  
571 # new msg_dirall method -- single packet
572 0 ▶ _ToServer($self,length($path)+1,$msg_dirall,$default_block,0,$path) || return ;
Branch: 1 branch
BranchTrueFalseTotal
&_ToServer($self, length($path) + 1, $msg_dirall, $default_block, 0, $path) or return 0 0 0
573 0 my @r = _FromServer($self) ;
574 0 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) 0 0 0
575 0 ▶ $self->{SOCK} = undef if $self->{PERSIST} == 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PERSIST"} == 0 0 0 0
576 0 return $r[6] ;
577 } ;
578  
579 # old msg_dir method -- many packets
580 0 ▶ _Sock($self) || return ;
Branch: 1 branch
BranchTrueFalseTotal
&_Sock($self) or return 0 0 0
581 0 ▶ _ToServer($self,length($path)+1,$msg_dir,$default_block,0,$path) || return ;
Branch: 1 branch
BranchTrueFalseTotal
&_ToServer($self, length($path) + 1, $msg_dir, $default_block, 0, $path) or return 0 0 0
582 0 my $dirlist = '' ;
583 0 while (1) {
584 0 ▶ @r = _FromServer($self) || return ;
Condition0%0/2
&_FromServer($self) || (return)
l!l
00
MC/DC0%0/1
&_FromServer($self) || (return)
&_FromServer($self)
Truth table0%0/2
A
A= &_FromServer($self)
Aresult
11
00
585 0 ▶ return if !@r ;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 0 0 0
586 0 ▶ if ( $r[1] == 0 ) { # last null packet
Branch: 1 branch
BranchTrueFalseTotal
if ($r[1] == 0) 0 0 0
587 0 ▶ $self->{SOCK} = undef if $self->{PERSIST} == 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PERSIST"} == 0 0 0 0
588 0 return substr($dirlist,1) ; # not starting comma
589 }
590 0 $dirlist .= ','.$r[6] ;
591 }
592 }
593  
594  
595 1 return 1 ;
596  
597 1 ▶END { }
Subroutine END: called CC=1 SCAR=0.0
598  
599 =back
600  
601 =head1 DESCRIPTION
602  
603 =head2 OWFS
604  
605 I<OWFS> is a suite of programs that allows easy access to I<Dallas Semiconductor>'s 1-wire bus and devices. I<OWFS> provides a consistent naming scheme, safe multplexing of 1-wire traffice, multiple methods of access and display, and network access. The basic I<OWFS> metaphor is a file-system, with the bus beinng the root directory, each device a subdirectory, and the the device properties (e.g. voltage, temperature, memory) a file.
606  
607 =head2 1-Wire
608  
609 I<1-wire> is a protocol allowing simple connection of inexpensive devices. Each device has a unique ID number (used in it's OWFS address) and is individually addressable. The bus itself is extremely simple -- a data line and a ground. The data line also provides power. 1-wire devices come in a variety of packages -- chips, commercial boxes, and iButtons (stainless steel cans). 1-wire devices have a variety of capabilities, from simple ID to complex voltage, temperature, current measurements, memory, and switch control.
610  
611 =head2 Programs
612  
613 Connection to the 1-wire bus is either done by bit-banging a digital pin on the processor, or by using a bus master -- USB, serial, i2c, parallel. The heavy-weight I<OWFS> programs: B<owserver> B<owfs> B<owhttpd> B<owftpd> and the heavy-weight perl module B<OW> all link in the full I<OWFS> library and can connect directly to the bus master(s) and/or to B<owserver>.
614  
615 B<OWNet> is a light-weight module. It connects only to an B<owserver>, does not link in the I<OWFS> library, and should be more portable..
616  
617 =head2 Object-oriented
618  
619 I<OWNet> can be used in either a classical (non-object-oriented) manner, or with objects. The object stored the ip address of the B<owserver> and a network socket to communicate. I<OWNet> will use persistent tcp connections for the object form -- potentially a performance boost over a slow network.
620  
621 =head1 EXAMPLES
622  
623 =head2 owserver
624  
625 owserver is a separate process that must be accessible on the network. It allows multiple clients, and can connect to many physical 1-wire adapters and 1-wire devices. It's address must be discoverable -- either set on the command line, or at it's default location, or by using Bonjour (zeroconf) service discovery.
626  
627 An example owserver invocation for a serial adapter and explicitly the default port:
628  
629 owserver -d /dev/ttyS0 -p 4304
630  
631 =head2 OWNet
632  
633 use OWNet ;
634
635 # Create owserver object
636 my $owserver = OWNet->new('localhost:4304 -v -F') ; #default location, verbose errors, Fahrenheit degrees
637 # my $owserver = OWNet->new() ; #simpler, again default location, no error messages, default Celsius
638  
639 #print directory
640 print $owserver->dir('/') ;
641  
642 #print temperature from known device (DS18S20, ID: 10.13224366A280)
643 print "Temperature: ".$owserver->read('/uncached/10.13224366A280/temperature') ;
644  
645 # Now for some fun -- a tree of everything:
646 sub Tree($$) {
647 my $ow = shift ;
648 my $path = shift ;
649  
650 print "$path\t" ;
651
652 # first try to read
653 my $value = $ow->read($path) ;
654 if ( defined($value) ) {
655 print "$value\n";
656 return ;
657 }
658  
659 # not readable, try as directory
660 my $dirstring = $ow->dir($path) ;
661 if ( defined($dirstring) ) {
662 print "<directory>\n" ;
663 my @dir = split /,/ , $ow->dir($path) ;
664 foreach (@dir) {
665 Tree($ow,$_) ;
666 }
667 return ;
668 }
669
670 # can't read, not directory
671 print "<write-only>\n" ;
672 return ;
673 }
674  
675 Tree( $owserver, '/' ) ;
676  
677 =head1 INTERNALS
678  
679 =head2 Object properties (All private)
680  
681 =item ADDR
682  
683 literal sting for the IP address, in ip:port format. This property is also used to indicate a substantiated object.
684  
685 =item SG
686  
687 Flag sent to server, and returned, that encodes temperature scale and display format. Persistence is also encoded in this word in the actual tcp message, but kept separately in the object.
688  
689 =item VERBOSE
690  
691 Print error messages? Set by "-v" in object invocation.
692  
693 =item SOCK
694  
695 Socket address (object) for communication. Stays defined for persistent connections, else deleted between calls.
696  
697 =head2 Private methods
698  
699 =item _self
700  
701 Takes either the implicit object reference (if called on an object) or the ip address in non-object format. In either case a socket is created, the persistence bit is property set, and the address parsed. Returns the object reference, or undef on error. Called by each external method (read,write,dir) on the first parameter.
702  
703 =item _new
704  
705 Takes command line invocation parameters (for an object or not) and properly parses and sets up the properties in a hash array.
706  
707 =item _Sock
708  
709 Socket processing, including tests for persistence, and opening.
710  
711 =item _ToServer
712  
713 Sends formats and sends a message to owserver. If a persistent socket fails, retries after new socket created.
714  
715 =item _FromServerLow
716  
717 Reads a specified length from server
718  
719 =item _FromServer
720  
721 Reads whole packet from server, using _FromServerLow (first for header, then payload/tokens). Discards ping packets silently.
722  
723 =item _DefaultLookup
724  
725 Uses the IANA allocated well known port (4304) for owserver. First looks in /etc/services, then just tries 4304.
726  
727 =item _BonjourLookup
728  
729 Uses the mDNS service discovery protocol to find an available owserver.
730 Employs NET::Rendezvous (an earlier name or Apple's Bonjour)
731 This module is loaded only if available using the method of http://sial.org/blog/2006/12/optional_perl_module_loading.html
732  
733 Bounjour details for owserver at:
734  
735 =head1 AUTHOR
736  
737 Paul H Alfille paul.alfille @ gmail . com
738  
739 =head1 BUGS
740  
741 Support for proper timeout using the "select" function seems broken in perl. This might leave the routines vulnerable to network timing errors.
742  
743 =head1 SEE ALSO
744  
745 =over
746  
747 =item http://www.owfs.org
748  
749 Documentation for the full B<owfs> program suite, including man pages for each of the supported 1-wire devices, nand more extensive explanatation of owfs components.
750  
751 =item http://owfs.sourceforge.net
752  
753 Location where source code is hosted.
754  
755 =back
756  
757 =head1 COPYRIGHT
758  
759 Copyright (c) 2007 Paul H Alfille. All rights reserved.
760 This program is free software; you can redistribute it and/or
761 modify it under the same terms as Perl itself.
762  
763 =cut
↑ Summary