blib/lib/Device/XBee/API.pm

filter: Statementstmt 16.8% 80 / 476 Branchbran 0.0% 0 / 234 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 42.5% 23 / 54 Podpod 56.2% 9 / 16 totaltotal 12.7% 112 / 879 CC 143 SCAR 47.7
CC
143
Cov
10%
remote_at
306.0
tx
306.0
read_escaped_packet
210.0
CRAP 118.5
↑ Summary
1 package Device::XBee::API;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 require Exporter;
6 1 our ( @ISA, @EXPORT_OK, %EXPORT_TAGS );
7  
8 1 our $VERSION = 0.8;
9  
10 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant 1.01;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant XBEE_API_TYPE__MODEM_STATUS => 0x8A;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant XBEE_API_TYPE__AT_COMMAND => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant XBEE_API_TYPE__AT_COMMAND_QUEUE_PARAMETER_VALUE => 0x09;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant XBEE_API_TYPE__AT_COMMAND_RESPONSE => 0x88;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant XBEE_API_TYPE__REMOTE_COMMAND_REQUEST => 0x17;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant XBEE_API_TYPE__REMOTE_COMMAND_RESPONSE => 0x97;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant XBEE_API_TYPE__ZIGBEE_TRANSMIT_REQUEST => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant XBEE_API_TYPE__EXPLICIT_ADDRESSING_ZIGBEE_COMMAND_FRAME => 0x11;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant XBEE_API_TYPE__ZIGBEE_TRANSMIT_STATUS => 0x8B;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant XBEE_API_TYPE__ZIGBEE_RECEIVE_PACKET => 0x90;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant XBEE_API_TYPE__ZIGBEE_EXPLICIT_RX_INDICATOR => 0x91;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant XBEE_API_TYPE__ZIGBEE_IO_DATA_SAMPLE_RX_INDICATOR => 0x92;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant XBEE_API_TYPE__XBEE_SENSOR_READ_INDICATOR_ => 0x94;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant XBEE_API_TYPE__NODE_IDENTIFICATION_INDICATOR => 0x95;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use constant XBEE_API_TYPE_TO_STRING => {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 0x8A => 'MODEM_STATUS',
29 0x08 => 'AT_COMMAND',
30 0x09 => 'AT_COMMAND_QUEUE_PARAMETER_VALUE',
31 0x88 => 'AT_COMMAND_RESPONSE',
32 0x17 => 'REMOTE_COMMAND_REQUEST',
33 0x97 => 'REMOTE_COMMAND_RESPONSE',
34 0x10 => 'ZIGBEE_TRANSMIT_REQUEST',
35 0x11 => 'EXPLICIT_ADDRESSING_ZIGBEE_COMMAND_FRAME',
36 0x8B => 'ZIGBEE_TRANSMIT_STATUS',
37 0x90 => 'ZIGBEE_RECEIVE_PACKET',
38 0x91 => 'ZIGBEE_EXPLICIT_RX_INDICATOR',
39 0x92 => 'ZIGBEE_IO_DATA_SAMPLE_RX_INDICATOR',
40 0x94 => 'XBEE_SENSOR_READ_INDICATOR_',
41 0x95 => 'NODE_IDENTIFICATION_INDICATOR',
42 1 };
43  
44 1 ▶use constant XBEE_API_TRANSMIT_STATUS_TO_STRING => {
Subroutine BEGIN: called CC=1 SCAR=0.0
45 0x00 => 'Success',
46 0x02 => 'CCA Failure',
47 0x15 => 'Invalid destination endpoint',
48 0x21 => 'Network ACK Failure',
49 0x22 => 'Not Joined to Network',
50 0x23 => 'Self-addressed',
51 0x24 => 'Address Not Found',
52 0x25 => 'Route Not Found',
53 0x74 => 'Data payload too large',
54 1 };
55  
56 1 ▶use constant XBEE_API_BAUD_RATE_TABLE => [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200];
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶use constant XBEE_API_BROADCAST_ADDR_H => 0x00;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use constant XBEE_API_BROADCAST_ADDR_L => 0xFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use constant XBEE_API_BROADCAST_NA_UNKNOWN_ADDR => 0xFFFE;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 {
63 1 my @xbee_flags = map { /::([^:]+)$/; $1 }
64 grep( /^Device::XBee::API::XBEE_API_/, keys( %constant::declared ) );
65  
66 1 @ISA = ( 'Exporter' );
67 1 @EXPORT_OK = ( @xbee_flags );
68  
69 1 %EXPORT_TAGS = ( 'xbee_flags' => [@xbee_flags], );
70 }
71  
72 =head1 NAME
73  
74 Device::XBee::API - Object-oriented Perl interface to Digi XBee module API
75 mode.
76  
77 =head1 EXAMPLE
78  
79 A basic example:
80  
81 use Device::SerialPort;
82 use Device::XBee::API;
83 use Data::Dumper;
84 $Data::Dumper::Useqq = 1;
85  
86 my $serial_port_device = Device::SerialPort->new( '/dev/ttyU0' ) || die $!;
87 $serial_port_device->baudrate( 9600 );
88 $serial_port_device->databits( 8 );
89 $serial_port_device->stopbits( 1 );
90 $serial_port_device->parity( 'none' );
91 $serial_port_device->read_char_time( 0 ); # don't wait for each character
92 $serial_port_device->read_const_time( 1000 ); # 1 second per unfulfilled "read" call
93  
94 my $api = Device::XBee::API->new( { fh => $serial_port_device } ) || die $!;
95 if ( !$api->tx( { sh => 0, sl => 0 }, 'hello world!' ) ) {
96 die "Transmit failed!";
97 }
98 my $rx = $api->rx();
99 die Dumper( $rx );
100  
101 =head1 SYNOPSIS
102  
103 Device::XBee::API is a module designed to encapsulate the Digi XBee API in
104 object-oriented Perl. This module expects to communicate with an XBee module
105 using the API firmware via a serial (or serial over USB) device.
106  
107 This module is currently a work in progress and thus the API may change in the
108 future.
109  
110 =head1 LICENSE
111  
112 This module is licensed under the same terms as Perl itself.
113  
114 =head1 CONSTANTS
115  
116 A single set of constants, ':xbee_flags', can be imported. These constants
117 all represent various XBee flags, such as packet types and broadcast addresses.
118 See the XBee datasheet for details. The following constants are available:
119  
120 XBEE_API_TYPE__MODEM_STATUS
121 XBEE_API_TYPE__AT_COMMAND
122 XBEE_API_TYPE__AT_COMMAND_QUEUE_PARAMETER_VALUE
123 XBEE_API_TYPE__AT_COMMAND_RESPONSE
124 XBEE_API_TYPE__REMOTE_COMMAND_REQUEST
125 XBEE_API_TYPE__REMOTE_COMMAND_RESPONSE
126 XBEE_API_TYPE__ZIGBEE_TRANSMIT_REQUEST
127 XBEE_API_TYPE__EXPLICIT_ADDRESSING_ZIGBEE_COMMAND_FRAME
128 XBEE_API_TYPE__ZIGBEE_TRANSMIT_STATUS
129 XBEE_API_TYPE__ZIGBEE_RECEIVE_PACKET
130 XBEE_API_TYPE__ZIGBEE_EXPLICIT_RX_INDICATOR
131 XBEE_API_TYPE__ZIGBEE_IO_DATA_SAMPLE_RX_INDICATOR
132 XBEE_API_TYPE__XBEE_SENSOR_READ_INDICATOR_
133 XBEE_API_TYPE__NODE_IDENTIFICATION_INDICATOR
134
135 XBEE_API_BROADCAST_ADDR_H
136 XBEE_API_BROADCAST_ADDR_L
137 XBEE_API_BROADCAST_NA_UNKNOWN_ADDR
138
139 XBEE_API_TYPE_TO_STRING
140 XBEE_API_BAUD_RATE_TABLE
141  
142 The above should be self explanatory (with the help of the datasheet). The
143 constant "XBEE_API_TYPE_TO_STRING" is a hashref keyed by the numeric id of the
144 packet type with the value being the constant name, to aid in debugging. The
145 constant XBEE_API_BAUD_RATE_TABLE is the baud rate table used by the BD API
146 command.
147  
148 =head1 METHODS
149  
150 =head2 new
151  
152 Object constructor. Accepts a single parameter, a hashref of options. The
153 following options are recognized:
154  
155 =head3 fh
156  
157 Required. The filehandle to be used to communicate with. This object can be a
158 standard filehandle (that can be accessed via sysread() and syswrite()), or a
159 Device::SerialPort object.
160  
161 =head3 packet_timeout
162  
163 Optional, defaults to 20. Amount of time (in seconds) to wait for a read to
164 complete. Smaller values cause the module to wait less time for a packet to be
165 received by the XBee module. Setting this value too low will cause timeouts to
166 be reported in situations where the network is "slow".
167  
168 When using standard filehandles, the timeout is implemented via select(). When
169 using a Device::SerialPort object, the timeout is done via Device::SerialPort's
170 read() method, and will expect the object to be configured with a
171 read_char_time of 0 and a read_const_time of 1000.
172  
173 =head3 node_forget_time
174  
175 If a node has not been heard from in this time, it will be "forgotten" and
176 removed from the list of known nodes. Defaults to one hour. See L<known_nodes>
177 for details.
178  
179 =head3 auto_reuse_frame_id
180  
181 All sent packets need a frame ID to uniquely identify them. There are only 254
182 available IDs and thus there can only be 254 outstanding commands sent to the
183 XBee. Normally frame IDs will be freed and reused once a command reply is
184 received, however there are scenarios where this can not be done (generally
185 those that involve local or remote AT commands, sleeping/offline nodes, etc).
186  
187 Normally, when no frame IDs are available but one is needed, the module will
188 die with an error and the send attempt will be aborted. This condition could be
189 trapped by the caller (via eval) to retry later, or could be treated as fatal.
190  
191 With this flag set, instead of dieing, the oldest frame ID will be reused. This
192 will help work around any issues with frame ID's "leaking", but could cause odd
193 behavior in cases where all outstanding frame IDs are still in use. This option
194 should be used with caution.
195  
196 =head3 alloc_frame_id_func
197 =head3 free_frame_id_func
198  
199 Optional code references to functions used to allocate and free frame IDs. If
200 both are specified they will be called in place of the internal frame ID
201 tracking functions allowing the user more control over how frame IDs are
202 generated. The alloc_frame_id_func will be called when a new frame ID is needed
203 and will be passed as a parameter the reference to the Device:XBee::API object
204 and must return an integer between 1 and 255 inclusive. The free_frame_id_func
205 will be called when the reply frame is received and the frame ID is no longer
206 needed and will be passed as parameters a reference to the Device::XBee::API
207 obect and the frame ID to be freed.
208  
209 See L<auto_reuse_frame_id> for details on how this module uses frame IDs.
210  
211 =head3 api_mode_escape
212  
213 Optional. If set to a true value, the module will automatically escape outgoing
214 data and un-escape incoming data for use with XBee API mode 2. Defaults to
215 false.
216  
217 See the XBee datasheet for details on API mode 2 and escaped characters.
218  
219 =cut
220  
221 0 ▶sub new {
Subroutine new: not called CC=13 SCAR=52.0
Pod documented
222 0 my ( $class, $options ) = @_;
223 0 my $self = {};
224  
225 0 ▶ die "Missing file handle!" unless $options->{'fh'};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"fh"} 0 0 0
226 0 $self->{fh} = $options->{fh};
227 0 ▶ $self->{packet_wait_time} = $options->{packet_timeout} || 20;
Condition0%0/2
$options->{'packet_timeout'} || 20
l!l
00
MC/DC0%0/1
$options->{'packet_timeout'} || 20
$options->{'packet_timeout'}
Truth table0%0/2
A
A= $options->{'packet_timeout'}
Aresult
11
00
228 0 ▶ $self->{node_forget_time} = $options->{node_forget_time} || 60 * 60;
Condition0%0/2
$options->{'node_forget_time'} || 3600
l!l
00
MC/DC0%0/1
$options->{'node_forget_time'} || 3600
$options->{'node_forget_time'}
Truth table0%0/2
A
A= $options->{'node_forget_time'}
Aresult
11
00
229 0 ▶ $self->{auto_reuse_frame_id} = $options->{auto_reuse_frame_id} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'auto_reuse_frame_id'} ? : 0 0 0
230 0 ▶ $self->{api_mode_escape} = $options->{api_mode_escape} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'api_mode_escape'} ? : 0 0 0
231  
232 0 ▶ if ( $options->{alloc_frame_id_func} && $options->{free_frame_id_func} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"alloc_frame_id_func"} and $options->{"free_frame_id_func"}) 0 0 0
Condition0%0/3
$options->{"alloc_frame_id_func"} and $options->{"free_frame_id_func"}
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"alloc_frame_id_func"} and $options->{"free_frame_id_func"}
$options->{"alloc_frame_id_func"} $options->{"free_frame_id_func"}
Truth table0%0/3
A and B
A= $options->{"alloc_frame_id_func"}B= $options->{"free_frame_id_func"}
ABresult
0X0
100
111
233 0 $self->{alloc_frame_id_func} = $options->{alloc_frame_id_func};
234 0 $self->{free_frame_id_func} = $options->{free_frame_id_func};
235 }
236  
237 0 $self->{in_flight_uart_frames} = {};
238 0 $self->{known_nodes} = {};
239 0 $self->{rx_queue} = [];
240  
241 0 ▶ if ( ( ref $self->{fh} ne 'Device::SerialPort' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"fh"} ne "Device::SerialPort" and ref $self->{"fh"} ne "Win32::SerialPort") 0 0 0
Condition0%0/3
ref $self->{"fh"} ne "Device::SerialPort" and ref $self->{"fh"} ne "Win32::SerialPort"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"fh"} ne "Device::SerialPort" and ref $self->{"fh"} ne "Win32::SerialPort"
ref $self->{"fh"} ne "Device::SerialPort" ref $self->{"fh"} ne "Win32::SerialPort"
Truth table0%0/3
A and B
A= ref $self->{"fh"} ne "Device::SerialPort"B= ref $self->{"fh"} ne "Win32::SerialPort"
ABresult
0X0
100
111
242 && ( ref $self->{fh} ne 'Win32::SerialPort' ) )
243 {
244 $self->{fh_sel} = IO::Select->new( $self->{fh} )
245 0 ▶ || die "Failed to initialize IO::Select!";
Condition0%0/2
'IO::Select'->new($self->{'fh'}) || die('Failed to initialize IO::Select!')
l!l
00
MC/DC0%0/1
'IO::Select'->new($self->{'fh'}) || die('Failed to initialize IO::Select!')
'IO::Select'->new($self->{'fh'})
Truth table0%0/2
A
A= 'IO::Select'->new($self->{'fh'})
Aresult
11
00
246 }
247  
248 0 ▶ if ( $self->{api_mode_escape} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"api_mode_escape"}) 0 0 0
249 0 $self->{api_mode_escape_table} = {};
250 0 $self->{api_mode_unescape_table} = {};
251 # Note the unescape re starts with the escape character.
252 0 $self->{api_mode_escape_re} = "([";
253 0 $self->{api_mode_unescape_re} = "\x7D([";
254 # List of characters taken from XBee datasheet.
255 0 foreach my $e ( 0x7E, 0x7D, 0x11, 0x13 ) {
256 0 my $chr_e = chr( $e );
257 0 my $chr_e_20 = chr( $e ^ 0x20 );
258 0 $self->{api_mode_escape_table}->{$chr_e} = $chr_e_20;
259 0 $self->{api_mode_unescape_table}->{$chr_e_20} = $chr_e;
260 0 $self->{api_mode_escape_re} .= quotemeta( $chr_e );
261 0 $self->{api_mode_unescape_re} .= quotemeta( $chr_e_20 );
262 }
263  
264 # Note the trailing "])" to terminate the character class.
265 0 $self->{api_mode_escape_re} = qr/$self->{api_mode_escape_re}])/;
266 0 $self->{api_mode_unescape_re} = qr/$self->{api_mode_unescape_re}])/;
267 }
268  
269 0 bless $self, $class;
270 0 return $self;
271 }
272  
273 0 ▶sub read_bytes {
Subroutine read_bytes: not called CC=12 SCAR=50.5
Pod undocumented
274 0 my ( $self, $to_read ) = @_;
275 0 ▶ die unless $to_read;
Branch: 1 branch
BranchTrueFalseTotal
unless $to_read 0 0 0
276 0 my $chars = 0;
277 0 my $buffer = '';
278 0 my $timeout = $self->{packet_wait_time};
279  
280 0 ▶ if ( !$self->{fh_sel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'fh_sel'}) { } 0 0 0
281 0 ▶ while ( $timeout > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout > 0) 0 0 0
282 0 my ( $count, $saw ) = $self->{fh}->read( $to_read ); # will read _up to_ 255 chars
283 0 ▶ if ( !defined $count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 0 0 0
284 0 die "Error reading from device: $!";
285 }
286 0 ▶ if ( $count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) { } 0 0 0
287 0 $chars += $count;
288 0 $buffer .= $saw;
289 0 ▶ if ( $chars >= $to_read ) { return $buffer; }
Branch: 1 branch
BranchTrueFalseTotal
if ($chars >= $to_read) 0 0 0
290 } else {
291 0 $timeout--;
292 }
293 }
294 } else {
295 0 my $read;
296 0 my $start_ts = time();
297 0 ▶ while ( $to_read > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_read > 0) 0 0 0
298 0 ▶ if ( !$self->{fh_sel}->can_read( $timeout ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh_sel"}->can_read($timeout)) 0 0 0
299 0 return undef;
300 }
301 0 my $c = sysread( $self->{fh}, $read, $to_read );
302 0 ▶ if ( $c ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 0 0 0
303 0 $buffer .= $read;
304 0 $to_read -= $c;
305 0 $timeout = $self->{packet_wait_time} - ( time() - $start_ts );
306 0 ▶ if ( $timeout < 1 && $to_read > 0 ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout < 1 and $to_read > 0) 0 0 0
Condition0%0/3
$timeout < 1 and $to_read > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout < 1 and $to_read > 0
$timeout < 1 $to_read > 0
Truth table0%0/3
A and B
A= $timeout < 1B= $to_read > 0
ABresult
0X0
100
111
307 } else {
308 0 return undef;
309 }
310 }
311 0 return $buffer;
312 }
313 0 return undef;
314 }
315  
316 0 ▶sub read_packet {
Subroutine read_packet: not called CC=8 SCAR=42.8
Pod undocumented
317 0 my ( $self ) = @_;
318 0 my $d;
319 my $packet_data_length;
320  
321 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$d ne "~" and do { do { $d = $self->read_bytes(1); return undef unless defined $d } } 0 0 0
322 0 $d = $self->read_bytes( 1 );
323 0 ▶ return undef if !defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 0 0
324 } while ( $d ne "\x7E" );
325  
326 0 ▶ if ( $self->{api_mode_escape} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'api_mode_escape'}) { } 0 0 0
327 0 ( $packet_data_length, $d ) = $self->read_escaped_packet();
328 } else {
329 0 $d = $self->read_bytes( 2 );
330  
331 0 ( $packet_data_length ) = unpack( 'n', $d );
332  
333 0 $d = $self->read_bytes( $packet_data_length + 1 );
334 0 ▶ if ( !$d ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d) 0 0 0
335 0 return undef;
336 }
337 }
338  
339 # Received either an invalid packet or a packet that has no associated API
340 # data. In either case there's nothing we can do with it.
341 0 ▶ if ( $packet_data_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet_data_length == 0) 0 0 0
342 0 return undef;
343 }
344  
345 0 $packet_data_length--;
346  
347 0 my ( $packet_api_id, $packet_data, $packet_checksum ) = unpack( "Ca[$packet_data_length]C", $d );
348 0 my $validate_checksum = $packet_api_id + $packet_checksum;
349 0 ▶ for ( my $i = 0; $i < $packet_data_length; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $packet_data_length) 0 0 0
350 0 $validate_checksum += unpack( 'c', substr( $packet_data, $i, 1 ) );
351 }
352  
353 0 ▶ if ( ( $validate_checksum & 0xFF ) != 0xFF ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($validate_checksum & 255) != 255) 0 0 0
354 #warn "Invalid checksum!";
355 0 return undef;
356 }
357  
358 0 return ( $packet_api_id, $packet_data );
359 }
360  
361 0 ▶sub read_escaped_packet {
Subroutine read_escaped_packet: not called CC=14 SCAR=53.5
Pod undocumented
362 0 my ( $self ) = @_;
363  
364 0 my $l1 = $self->read_bytes( 1 );
365 0 ▶ return unless defined $l1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l1 0 0 0
366  
367 0 ▶ if ( $l1 eq "\x7D" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l1 eq "}") 0 0 0
368 0 $l1 = $self->read_bytes( 1 );
369 0 ▶ return unless defined $l1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l1 0 0 0
370 0 $l1 ^= "\x20";
371 }
372  
373 0 my $l2 = $self->read_bytes( 1 );
374 0 ▶ return unless defined $l2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l2 0 0 0
375  
376 0 ▶ if ( $l2 eq "\x7D" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l2 eq "}") 0 0 0
377 0 $l2 = $self->read_bytes( 1 );
378 0 ▶ return unless defined $l2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l2 0 0 0
379 0 $l2 ^= "\x20";
380 }
381  
382 0 my $packet_data_length = unpack( 'n', $l1 . $l2 );
383 0 my $data = $self->read_bytes( $packet_data_length + 1 );
384 0 ▶ return unless defined $data; # includes checksum
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
385  
386 0 ▶ if ( $data =~ /\x7D$/ ) { # trailing escape
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\x7D$/) 0 0 0
387 0 my $tail = $self->read_bytes( 1 );
388 0 ▶ return unless defined $tail;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tail 0 0 0
389 0 $data .= $tail;
390 }
391  
392 0 $data =~ s/$self->{api_mode_unescape_re}/$self->{api_mode_unescape_table}->{$1}/g;
393 0 my $need_a_few_more = $packet_data_length - length( $data ) + 1;
394  
395 0 ▶ while ( $need_a_few_more-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_a_few_more--) 0 0 0
396 0 my $b = $self->read_bytes( 1 );
397 0 ▶ return unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
398 0 ▶ if ( $b eq "\x7D" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b eq "}") 0 0 0
399 0 $b = $self->read_bytes( 1 );
400 0 ▶ return unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
401 0 $b ^= "\x20";
402 }
403 0 $data .= $b;
404 }
405  
406 0 return ( $packet_data_length, $data );
407 }
408  
409 0 ▶sub free_frame_id {
Subroutine free_frame_id: not called CC=2 SCAR=17.9
Pod undocumented
410 0 my ( $self, $id ) = @_;
411 0 ▶ if ( $self->{free_frame_id_func} ) { return $self->{free_frame_id_func}->( $self, $id ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"free_frame_id_func"}) 0 0 0
412 0 delete $self->{in_flight_uart_frames}->{$id};
413 }
414  
415 # id 0 is special, don't allocate it. I don't know if we should die here or
416 # return 0 on failure...
417 0 ▶sub alloc_frame_id {
Subroutine alloc_frame_id: not called CC=7 SCAR=40.3
Pod undocumented
418 0 my ( $self ) = @_;
419  
420 0 ▶ if ( $self->{alloc_frame_id_func} ) { return $self->{alloc_frame_id_func}->( $self ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"alloc_frame_id_func"}) 0 0 0
421  
422 0 my $start_id = int( rand( 255 ) ) + 1;
423 0 my $id = $start_id;
424 0 my $oldest_time = 0xFFFFFFFF;
425 0 my $oldest_id;
426  
427 0 while ( 1 ) {
428 0 ▶ if ( !exists $self->{in_flight_uart_frames}->{$id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->{'in_flight_uart_frames'}{$id}) { } 0 0 0
elsif ($self->{'in_flight_uart_frames'}{$id} < $oldest_time) { } 0 0 0
429 0 $self->{in_flight_uart_frames}->{$id} = time();
430 0 return $id;
431 } elsif ( $self->{in_flight_uart_frames}->{$id} < $oldest_time ) {
432 0 $oldest_time = $self->{in_flight_uart_frames}->{$id};
433 0 $oldest_id = $id;
434 }
435 0 $id++;
436 0 ▶ if ( $id > 255 ) { $id = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($id > 255) 0 0 0
437 0 ▶ if ( $id == $start_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id == $start_id) 0 0 0
438 0 ▶ if ( $self->{auto_reuse_frame_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto_reuse_frame_id"}) 0 0 0
439 0 $self->{in_flight_uart_frames}->{$oldest_id} = time();
440 0 return $oldest_id;
441 }
442 0 die "Unable to allocate frame id!";
443 }
444 }
445 }
446  
447 0 ▶sub parse_packet {
Subroutine parse_packet: not called CC=12 SCAR=50.5
Pod undocumented
448 0 my ( $self, $api_id, $api_data, $dont_free_id ) = @_;
449 0 my @u;
450 my $r;
451  
452 0 ▶ if ( $api_id == XBEE_API_TYPE__AT_COMMAND_RESPONSE ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($api_id == 136) { } 0 0 0
elsif ($api_id == 138) { } 0 0 0
elsif ($api_id == 144) { } 0 0 0
elsif ($api_id == 145) { } 0 0 0
elsif ($api_id == 139) { } 0 0 0
elsif ($api_id == 146) { } 0 0 0
elsif ($api_id == 149) { } 0 0 0
elsif ($api_id == 151) { } 0 0 0
elsif ({136 => 'AT_COMMAND_RESPONSE', 138 => 'MODEM_STATUS', 139 => 'ZIGBEE_TRANSMIT_STATUS', 144 => 'ZIGBEE_RECEIVE_PACKET', 145 => 'ZIGBEE_EXPLICIT_RX_INDICATOR', 146 => 'ZIGBEE_IO_DATA_SAMPLE_RX_INDICATOR', 148 => 'XBEE_SENSOR_READ_INDICATOR_', 149 => 'NODE_IDENTIFICATION_INDICATOR', 151 => 'REMOTE_COMMAND_RESPONSE', 16 => 'ZIGBEE_TRANSMIT_REQUEST', 17 => 'EXPLICIT_ADDRESSING_ZIGBEE_COMMAND_FRAME', 23 => 'REMOTE_COMMAND_REQUEST', 8 => 'AT_COMMAND', 9 => 'AT_COMMAND_QUEUE_PARAMETER_VALUE'}->{$api_id}) { } 0 0 0
453 0 $r = __parse_at_command_response( $api_data );
454  
455 } elsif ( $api_id == XBEE_API_TYPE__MODEM_STATUS ) {
456 0 $r = __parse_modem_status( $api_data );
457  
458 } elsif ( $api_id == XBEE_API_TYPE__ZIGBEE_RECEIVE_PACKET ) {
459 0 $r = __parse_zigbee_receive_packet( $api_data );
460  
461 } elsif ( $api_id == XBEE_API_TYPE__ZIGBEE_EXPLICIT_RX_INDICATOR ) {
462 0 $r = __parse_zigbee_explicit_rx_indicator( $api_data );
463  
464 } elsif ( $api_id == XBEE_API_TYPE__ZIGBEE_TRANSMIT_STATUS ) {
465 0 $r = __parse_zigbee_transmit_status( $api_data );
466  
467 } elsif ( $api_id == XBEE_API_TYPE__ZIGBEE_IO_DATA_SAMPLE_RX_INDICATOR ) {
468 0 $r = __parse_zigbee_io_data_sample_rx_indicator( $api_data );
469  
470 } elsif ( $api_id == XBEE_API_TYPE__NODE_IDENTIFICATION_INDICATOR ) {
471 0 $r = __parse_node_identification_indicator( $api_data );
472  
473 } elsif ( $api_id == XBEE_API_TYPE__REMOTE_COMMAND_RESPONSE ) {
474 0 $r = __parse_remote_command_response( $api_data );
475  
476 } elsif ( XBEE_API_TYPE_TO_STRING->{$api_id} ) {
477 0 warn "No code to handle this packet: " . XBEE_API_TYPE_TO_STRING->{$api_id};
478 } else {
479 0 warn "Got unknown packet type: $api_id";
480 }
481  
482 0 ▶ if ( !$dont_free_id && $r->{frame_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dont_free_id and $r->{"frame_id"}) 0 0 0
Condition0%0/3
not $dont_free_id and $r->{"frame_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $dont_free_id and $r->{"frame_id"}
not $dont_free_id $r->{"frame_id"}
Truth table0%0/3
not(A) and B
A= not $dont_free_idB= $r->{"frame_id"}
ABresult
0X0
100
111
483 0 $self->free_frame_id( $r->{frame_id} );
484 }
485 0 $r->{api_type} = $api_id;
486 0 $r->{api_data} = $api_data;
487  
488 0 $self->_add_known_node( $r );
489 0 return $r;
490 }
491  
492 0 ▶sub send_packet {
Subroutine send_packet: not called CC=4 SCAR=30.0
Pod undocumented
493 0 my ( $self, $api_id, $data ) = @_;
494 0 my $xbee_data = pack( 'nC', length( $data ) + 1, $api_id );
495 0 my $checksum = $api_id;
496  
497 0 ▶ for ( my $i = 0; $i < length( $data ); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $data) 0 0 0
498 0 $checksum += unpack( 'C', substr( $data, $i, 1 ) );
499 }
500 0 $checksum = pack( 'C', 0xFF - ( $checksum & 0xFF ) );
501 0 $xbee_data = $xbee_data . $data . $checksum;
502  
503 0 ▶ if ( $self->{api_mode_escape} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"api_mode_escape"}) 0 0 0
504 # Note we insert the \x7D here, it's not part of the table!
505 0 $xbee_data =~ s/$self->{api_mode_escape_re}/\x7D$self->{api_mode_escape_table}->{$1}/g;
506 }
507  
508 0 ▶ if ( !$self->{fh_sel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'fh_sel'}) { } 0 0 0
509 0 $self->{fh}->write( "\x7E" . $xbee_data );
510 } else {
511 0 syswrite( $self->{fh}, "\x7E" . $xbee_data );
512 }
513 }
514  
515 =head2 at
516  
517 Send an AT command to the module. Accepts two parameters, the first is the AT
518 command name (as two-character string), and the second is the expected data
519 for that command (if any). Data will be converted to a string before it is
520 sent, so integers must first be packed before sending. See the XBee datasheet
521 for a list of supported AT commands and expected data for each.
522  
523 Returns the frame ID sent for this packet. This method does not wait for a
524 reply from the XBee, as the expected reply is dependent on the AT command sent.
525 To retrieve the reply (if any), call one of the L<rx> methods.
526  
527 If no reply is expected, the caller should immediately free the returned frame
528 ID via L<free_frame_id> to prevent frame ID leaks.
529  
530 =head3 Example
531  
532 my $at_frame_id = $api->at( 'AO', pack( 'C', 1 ) );
533  
534 =back
535  
536 =cut
537  
538 0 ▶sub at {
Subroutine at: not called CC=2 SCAR=17.9
Pod documented
539 0 my ( $self, $command, $data ) = @_;
540 0 ▶ $data = '' unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
541 0 my $frame_id = $self->alloc_frame_id();
542 0 $self->send_packet( XBEE_API_TYPE__AT_COMMAND, pack( 'C', $frame_id ) . $command . $data );
543 0 return $frame_id;
544 }
545  
546 =head2 remote_at
547  
548 Send an AT command to a remote module. Accepts three parameters: a hashref with
549 endpoint addresses, command options, frame_id; the AT command name (as
550 two-character string); and the third as the expected data for that command (if
551 any) as a string. See the XBee datasheet for a list of supported AT commands
552 and expected data for each.
553  
554 Endpoint addresses should be specified as a hashref containing the following
555 keys:
556  
557 =over 4
558  
559 =item sh
560  
561 The high 32-bits of the destination address.
562  
563 =item sl
564  
565 The low 32-bits of the destination address.
566  
567 =item na
568  
569 The destination network address.
570  
571 =item apply_changes
572  
573 If set, changes are applied immediately. If not set, an AT AC command must be
574 sent separately before the changes will take effect.
575  
576 =back
577  
578 Note: The following command options are not supported on all XBee devices.
579  
580 =over 4
581  
582 =item disable_ack
583  
584 If set, the remote node will not reply with an ack.
585  
586 =item extended_xmit_timeout
587  
588 If set, the exteded transmission timeout will be used.
589  
590 =back
591  
592 Returns the frame ID sent for this packet. To retrieve the reply (if any), call
593 one of the L<rx> methods. If no reply is expected, the caller should immediately
594 free the returned frame ID via L<free_frame_id> to prevent frame ID leaks.
595  
596 =head3 Example
597  
598 The following example disables the blinking association light on the XBee with
599 a constant on.
600  
601 if (
602 !$api->remote_at(
603 { sh => 1234567, sl => 1234567890, apply_changes => 1 },
604 'D5', "\x1"
605 )
606 ) {
607 die "Transmit failed!";
608 }
609 my $rx = $api->rx();
610 warn Dumper( $rx );
611  
612 =cut
613  
614 0 ▶sub remote_at {
Subroutine remote_at: not called CC=17 SCAR=57.2
Pod documented
615 0 my ( $self, $tx, $command, $data ) = @_;
616 0 my @my_rx_queue;
617 0 ▶ if ( !$command ) { die "Invalid parameters"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 0 0 0
618 0 ▶ if ( !$tx && !$data ) { die "Invalid parameters"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx or $data) 0 0 0
Condition0%0/3
$tx or $data
l!l&&r!l&&!r
000
MC/DC0%0/2
$tx or $data
$tx $data
Truth table0%0/3
A or B
A= $txB= $data
ABresult
1X1
011
000
619 0 ▶ if ( !defined $tx && defined $data ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $tx and defined $data) { } 0 0 0
elsif (ref $tx ne 'HASH') { } 0 0 0
Condition0%0/3
not defined $tx and defined $data
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $tx and defined $data
not defined $tx defined $data
Truth table0%0/3
not(A) and B
A= not defined $txB= defined $data
ABresult
0X0
100
111
620 0 $tx = {};
621 } elsif ( ref $tx ne 'HASH' ) {
622 0 $data = $tx;
623 0 $tx = {};
624 }
625  
626 0 ▶ if ( ( $tx->{sh} && !$tx->{sl} )
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->{"sh"} and not $tx->{"sl"} or not $tx->{"sh"} and $tx->{"sl"}) 0 0 0
Condition0%0/9
$tx->{"sh"} and not $tx->{"sl"} or not $tx->{"sh"} and $tx->{"sl"}
l!l&&r!l&&!r
000
$tx->{"sh"} and not $tx->{"sl"}
!ll&&!rl&&r
000
not $tx->{"sh"} and $tx->{"sl"}
!ll&&!rl&&r
000
MC/DC0%0/4
$tx->{"sh"} and not $tx->{"sl"} or not $tx->{"sh"} and $tx->{"sl"}
$tx->{"sh"} not $tx->{"sl"} not $tx->{"sh"} $tx->{"sl"}
Truth table0%0/7
A and not(B) or not(C) and D
A= $tx->{"sh"}B= not $tx->{"sl"}C= not $tx->{"sh"}D= $tx->{"sl"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
627 || ( !$tx->{sh} && $tx->{sl} ) )
628 {
629 0 die "Invalid parameters";
630 }
631  
632 0 ▶ if ( !defined $tx->{na} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tx->{"na"}) 0 0 0
633 0 $tx->{na} = XBEE_API_BROADCAST_NA_UNKNOWN_ADDR;
634 }
635 0 ▶ if ( !defined $tx->{sh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tx->{"sh"}) 0 0 0
636 0 $tx->{sh} = XBEE_API_BROADCAST_ADDR_H;
637 0 $tx->{sl} = XBEE_API_BROADCAST_ADDR_L;
638 }
639 0 my ( $ack, $chg, $timeout );
640 0 ▶ if ( defined $tx->{disable_ack} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tx->{'disable_ack'}) { } 0 0 0
641 0 $ack = 0x01;
642 } else {
643 0 $ack = 0x00;
644 }
645 0 ▶ if ( defined $tx->{apply_changes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tx->{'apply_changes'}) { } 0 0 0
646 0 $chg = 0x02;
647 } else {
648 0 $chg = 0x00;
649 }
650 0 ▶ if ( defined $tx->{extended_xmit_timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tx->{'extended_xmit_timeout'}) { } 0 0 0
651 0 $timeout = 0x40;
652 } else {
653 0 $timeout = 0x00;
654 }
655 0 my $options = $ack + $chg + $timeout;
656  
657 0 ▶ $data = '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
658 0 my $frame_id = $self->alloc_frame_id();
659 0 my $tx_req = pack( 'CNNnC', $frame_id, $tx->{sh}, $tx->{sl}, $tx->{na}, $options );
660 0 $self->send_packet( XBEE_API_TYPE__REMOTE_COMMAND_REQUEST, $tx_req . $command . $data );
661 0 return $frame_id;
662 }
663  
664 =head2 tx
665  
666 Sends a transmit request to the XBee. Accepts three parameters, the first is the
667 endpoint address, the second is a scalar containing the data to be sent, and the
668 third is an optional flag (known as the async flag) specifying whether or not
669 the method should wait for an acknowledgement from the XBee.
670  
671 Endpoint addresses should be specified as a hashref containing the following
672 keys:
673  
674 =over 4
675  
676 =item sh
677  
678 The high 32-bits of the destination address.
679  
680 =item sl
681  
682 The low 32-bits of the destination address.
683  
684 =item na
685  
686 The destination network address. If this is not specified, it will default to
687 XBEE_API_BROADCAST_NA_UNKNOWN_ADDR.
688  
689 =back
690  
691 If both sh and sl are missing or the parameter is undefined,, they will default
692 to XBEE_API_BROADCAST_ADDR_H and XBEE_API_BROADCAST_ADDR_L.
693  
694 The meaning of these addresses can be found in the XBee datasheet. Note: In
695 the future, a Device::XBee::API::Node object will be an acceptable parameter.
696  
697 If the async flag is not set, the method will wait for an acknowledgement packet
698 from the XBee. Return values depend on calling context. In scalar context, true
699 or false will be returned representing transmission acknowledgement by the
700 remote XBee device. In array context, the first return value is the delivery
701 status (as set in the transmit status packet and documented in the datasheet),
702 and the second is the actual transmit status packet (as a hashref) itself.
703  
704 If the async flag is set, the method will not wait for an acknowledgement packet
705 and the tx frame ID will be returned. The caller will need to then receive the
706 transmit status packet (via one of the L<rx> methods) and free the frame ID (via
707 L<free_frame_id>) manually.
708  
709 No retransmissions will be attempted by this module, but the XBee
710 device itself will likely attempt retransmissions as per its configuration (and
711 subject to whether or not the packet was a "broadcast").
712  
713 =cut
714  
715 # API is goofy here. If called in scalar context, returns true or false if the
716 # packet was transmitted. If called in array context, returns the delivery
717 # status and the transmit status packet as an array. Note: the actual delivery
718 # status uses 0 (or false) to indicate success.
719 0 ▶sub tx {
Subroutine tx: not called CC=17 SCAR=57.2
Pod documented
720 0 my ( $self, $tx, $data, $async ) = @_;
721 0 my @my_rx_queue;
722 0 ▶ if ( !$tx && !$data ) { die "Invalid parameters"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx or $data) 0 0 0
Condition0%0/3
$tx or $data
l!l&&r!l&&!r
000
MC/DC0%0/2
$tx or $data
$tx $data
Truth table0%0/3
A or B
A= $txB= $data
ABresult
1X1
011
000
723 0 ▶ if ( !defined $tx && defined $data ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $tx and defined $data) { } 0 0 0
elsif (ref $tx ne 'HASH') { } 0 0 0
Condition0%0/3
not defined $tx and defined $data
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $tx and defined $data
not defined $tx defined $data
Truth table0%0/3
not(A) and B
A= not defined $txB= defined $data
ABresult
0X0
100
111
724 0 $tx = {};
725 } elsif ( ref $tx ne 'HASH' ) {
726 0 $data = $tx;
727 0 $tx = {};
728 }
729  
730 0 ▶ if ( ( $tx->{sh} && !$tx->{sl} ) || ( !$tx->{sh} && $tx->{sl} ) ) { die "Invalid parameters"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->{"sh"} and not $tx->{"sl"} or not $tx->{"sh"} and $tx->{"sl"}) 0 0 0
Condition0%0/9
$tx->{"sh"} and not $tx->{"sl"} or not $tx->{"sh"} and $tx->{"sl"}
l!l&&r!l&&!r
000
$tx->{"sh"} and not $tx->{"sl"}
!ll&&!rl&&r
000
not $tx->{"sh"} and $tx->{"sl"}
!ll&&!rl&&r
000
MC/DC0%0/4
$tx->{"sh"} and not $tx->{"sl"} or not $tx->{"sh"} and $tx->{"sl"}
$tx->{"sh"} not $tx->{"sl"} not $tx->{"sh"} $tx->{"sl"}
Truth table0%0/7
A and not(B) or not(C) and D
A= $tx->{"sh"}B= not $tx->{"sl"}C= not $tx->{"sh"}D= $tx->{"sl"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
731  
732 0 ▶ if ( !defined $tx->{na} ) { $tx->{na} = XBEE_API_BROADCAST_NA_UNKNOWN_ADDR; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tx->{"na"}) 0 0 0
733 0 ▶ if ( !defined $tx->{sh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tx->{"sh"}) 0 0 0
734 0 $tx->{sh} = XBEE_API_BROADCAST_ADDR_H;
735 0 $tx->{sl} = XBEE_API_BROADCAST_ADDR_L;
736 }
737  
738 0 my $frame_id = $self->alloc_frame_id();
739 0 ▶ my $tx_req = pack( 'CNNnCC', $frame_id, $tx->{sh}, $tx->{sl}, $tx->{na}, 0, ( $tx->{broadcast} ? 0x8 : 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
$tx->{'broadcast'} ? : 0 0 0
740 0 $self->send_packet( XBEE_API_TYPE__ZIGBEE_TRANSMIT_REQUEST, $tx_req . $data );
741  
742 0 ▶ if ( $async ) { return $frame_id; }
Branch: 1 branch
BranchTrueFalseTotal
if ($async) 0 0 0
743  
744 # Wait until we get the send result message.
745 0 my $rx = $self->rx_frame_id( $frame_id );
746 0 ▶ return undef unless defined $rx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rx 0 0 0
747  
748 # Wonky return API.
749 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
750 0 return ( $rx->{delivery_status}, $rx );
751 } else {
752 0 ▶ if ( $rx->{delivery_status} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx->{'delivery_status'} == 0) { } 0 0 0
753 0 return 1;
754 } else {
755 0 return 0;
756 }
757 }
758 }
759  
760 0 ▶sub _unshift_rx {
Subroutine _unshift_rx: not called CC=4 SCAR=30.0
761 0 my ( $self, $rxq ) = @_;
762  
763 0 ▶ if ( !$rxq ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($rxq) 0 0 0
764 0 ▶ if ( ref $rxq eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rxq eq '') { } 0 0 0
elsif (ref $rxq eq 'ARRAY') { } 0 0 0
765 0 unshift @{ $self->{rx_queue} }, $rxq;
766 } elsif ( ref $rxq eq 'ARRAY' ) {
767 0 unshift @{ $self->{rx_queue} }, @{$rxq};
768 } else {
769 0 die "Unknown parameter type";
770 }
771 }
772  
773 0 ▶sub _rx_no_queue {
Subroutine _rx_no_queue: not called CC=2 SCAR=17.9
774 0 my ( $self, $dont_free_id ) = @_;
775  
776 0 my ( $type, $data ) = $self->read_packet();
777 0 ▶ return unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
778 0 return $self->parse_packet( $type, $data, $dont_free_id );
779 }
780  
781 =head2 rx
782  
783 Receives a packet from the XBee module. This packet may be a transmission from
784 a remote XBee node or a control packet from the local XBee module.
785  
786 If no packet is received before the timeout period expires, undef is returned.
787  
788 Returned packets will be as a hashref of the packet data, broken out by key for
789 easy access. Note, as this module is a work in progress, not every XBee packet
790 type is supported. Callers should check the "api_type" key to determine the
791 type of the received packet. When possible, packed integers will be unpacked
792 into the "data_as_int" key. If no packed integer is found this key will not be
793 present. If unpacking is not possible (due to an unknown packet type, etc), the
794 value will be undef.
795  
796 Accepts a single parameter, a flag indicating the received frame ID should NOT
797 be freed automatically. See L<rx_frame_id> for why you might want to use this
798 flag (generally, cases when you expect multiple packets to arrive with the same
799 frame ID).
800  
801 =cut
802  
803 0 ▶sub rx {
Subroutine rx: not called CC=2 SCAR=17.9
Pod documented
804 0 my ( $self, $dont_free_id ) = @_;
805  
806 0 ▶ if ( scalar( @{ $self->{rx_queue} } ) > 0 ) { return shift @{ $self->{rx_queue} }; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"rx_queue"};} > 0) 0 0 0
807 0 return $self->_rx_no_queue( $dont_free_id );
808 }
809  
810 =head2 rx_frame_id
811  
812 Like L<rx> but only returns the packet with the requested frame ID number and
813 then frees that frame ID. If no packet with the specified frame ID is received
814 within the object's configured packet_timeout time, undef will be returned. Any
815 other packets received will be enqueued for later processing by another rx
816 function call.
817  
818 Accepts two parameters, the first being the desired frame ID and the second a
819 flag denoting that the frame ID should NOT be automatically freed. In cases
820 where multiple frames with the same ID are expected to be returned (such as
821 after an AT ND command), it is preferable to set this flag to a true value and
822 continue to call rx_frame_id until undef is returned, and then free the ID via
823 L<free_frame_id>.
824  
825 =cut
826  
827 0 ▶sub rx_frame_id {
Subroutine rx_frame_id: not called CC=6 SCAR=37.4
Pod documented
828 0 my ( $self, $frame_id, $dont_free_id ) = @_;
829 0 my @ignored;
830 my $r;
831 0 my $start_time = time();
832  
833 0 while ( 1 ) {
834 0 $r = $self->rx( $dont_free_id );
835 0 ▶ if ( $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
836 0 ▶ if ( $r->{frame_id} && $r->{frame_id} == $frame_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'frame_id'} and $r->{'frame_id'} == $frame_id) { } 0 0 0
Condition0%0/3
$r->{"frame_id"} and $r->{"frame_id"} == $frame_id
!ll&&!rl&&r
000
MC/DC0%0/2
$r->{"frame_id"} and $r->{"frame_id"} == $frame_id
$r->{"frame_id"} $r->{"frame_id"} == $frame_id
Truth table0%0/3
A and B
A= $r->{"frame_id"}B= $r->{"frame_id"} == $frame_id
ABresult
0X0
100
111
837 0 last;
838 } else {
839 0 push @ignored, $r;
840 }
841 }
842 0 ▶ if ( time() - $start_time >= $self->{packet_wait_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $start_time >= $self->{"packet_wait_time"}) 0 0 0
843 0 undef $r;
844 0 last;
845 }
846 }
847 0 ▶ if ( @ignored ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ignored) 0 0 0
848 0 $self->_unshift_rx( \@ignored );
849 }
850 0 return $r;
851 }
852  
853 =head2 discover_network
854  
855 Performs a network node discovery via the ND 'AT' command. Blocks until no
856 replies have been received in packet_timeout seconds.
857  
858 =cut
859  
860 0 ▶sub discover_network {
Subroutine discover_network: not called CC=2 SCAR=17.9
Pod documented
861 0 my ( $self ) = @_;
862 0 my $frame_id = $self->at( 'ND' );
863 0 ▶ while ( defined $self->rx_frame_id( $frame_id, 1 ) ) { }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->rx_frame_id($frame_id, 1)) 0 0 0
864 0 $self->free_frame_id( $frame_id );
865 }
866  
867 =head2 node_info
868  
869 =cut
870  
871 0 ▶sub node_info {
Subroutine node_info: not called CC=2 SCAR=17.9
Pod documented
872 0 my ( $self, $node ) = @_;
873 0 my $sn = __node_sn( $node );
874 0 ▶ if ( !$sn ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sn) 0 0 0
875 0 $node->{sn} = $sn;
876 0 return $self->{known_nodes}->{$sn};
877 }
878  
879 =head2 known_nodes
880  
881 Returns a hashref of all known nodes indexed by their full serial number (i.e.
882 $node->{sh} . '_' . $node->{sl}). Nodes that haven't been heard from in the
883 configured node_forget_time will be automatically removed from this list if
884 they've not been heard from in that time. Nodes are added to that list when a
885 message is received from them or a discover_network call has been made.
886  
887 Note, the age-out mechanism may be susceptable to stepping of the system clock.
888  
889 =cut
890  
891 0 ▶sub known_nodes {
Subroutine known_nodes: not called CC=1 SCAR=6.9
Pod documented
892 0 my ( $self ) = @_;
893 0 $self->_prune_known_nodes();
894 0 return { %{ $self->{known_nodes} } };
895 }
896  
897 ### Private methods
898  
899 0 ▶sub _add_known_node {
Subroutine _add_known_node: not called CC=9 SCAR=45.0
900 0 my ( $self, $node ) = @_;
901  
902 0 my $sn = __node_sn( $node );
903 0 ▶ if ( !$sn ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sn) 0 0 0
904  
905 0 $self->_prune_known_nodes();
906  
907 # Update the node in-place in case someone else is holding onto a
908 # reference.
909 0 ▶ if ( $self->{known_nodes}->{$sn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'known_nodes'}{$sn}) { } 0 0 0
910 0 my $sknsn = $self->{known_nodes}->{$sn};
911 # These are the only known values that should change for a node with a
912 # given serial number. The rest are burned into the chip.
913 0 foreach my $k ( qw/ ni profile_id / ) {
914 0 ▶ if ( $node->{$k}
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$k} and !$sknsn->{$k} || $sknsn->{$k} ne $node->{$k}) 0 0 0
Condition0%0/6
$node->{$k} and !$sknsn->{$k} || $sknsn->{$k} ne $node->{$k}
!ll&&!rl&&r
000
not $sknsn->{$k} or $sknsn->{$k} ne $node->{$k}
l!l&&r!l&&!r
000
MC/DC0%0/3
$node->{$k} and !$sknsn->{$k} || $sknsn->{$k} ne $node->{$k}
$node->{$k} not $sknsn->{$k} $sknsn->{$k} ne $node->{$k}
Truth table0%0/4
A and not(B) or C
A= $node->{$k}B= not $sknsn->{$k}C= $sknsn->{$k} ne $node->{$k}
ABCresult
0XX0
1000
11X1
1011
915 && ( !$sknsn->{$k} || $sknsn->{$k} ne $node->{$k} ) )
916 {
917 0 $sknsn->{$k} = $node->{$k};
918 }
919 }
920 0 ▶ $sknsn->{na} = $node->{na} || $node->{my};
Condition0%0/3
$node->{'na'} || $node->{'my'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->{'na'} || $node->{'my'}
$node->{'na'} $node->{'my'}
Truth table0%0/3
A || B
A= $node->{'na'}B= $node->{'my'}
ABresult
1X1
011
000
921 0 $sknsn->{last_seen_time} = time();
922 } else {
923 $self->{known_nodes}->{$sn} = {
924 sn => $sn,
925 sh => $node->{sh},
926 sl => $node->{sl},
927 na => $node->{na} || $node->{my},
928 ni => $node->{ni},
929 profile_id => $node->{profile_id},
930 device_type => $node->{device_type},
931 manufacturer_id => $node->{manufacturer_id},
932 0 ▶ last_seen_time => time(),
Condition0%0/3
$node->{'na'} || $node->{'my'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->{'na'} || $node->{'my'}
$node->{'na'} $node->{'my'}
Truth table0%0/3
A || B
A= $node->{'na'}B= $node->{'my'}
ABresult
1X1
011
000
933 };
934 }
935 }
936  
937 0 ▶sub _prune_known_nodes {
Subroutine _prune_known_nodes: not called CC=3 SCAR=24.8
938 0 my ( $self ) = @_;
939 0 my $now = time();
940 0 my @saved_nodes;
941 0 ▶ while ( my ( $sn, $node ) = each( %{ $self->{known_nodes} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"known_nodes"};}) 0 0 0
942 0 ▶ if ( $now - $node->{last_seen_time} > $self->{node_forget_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $node->{"last_seen_time"} > $self->{"node_forget_time"}) 0 0 0
943 # Set just in case a caller has held onto the reference for
944 # something.
945 0 $node->{forgotten} = 1;
946 0 delete $self->{known_nodes}->{$sn};
947 }
948 }
949 }
950  
951 ### Private functions
952  
953 0 ▶sub __node_sn {
Subroutine __node_sn: not called CC=3 SCAR=24.8
954 0 my ( $node ) = @_;
955 0 ▶ if ( $node->{sn} ) { return $node->{sn} }
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"sn"}) 0 0 0
956 0 ▶ if ( !$node->{sh} ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->{"sh"}) 0 0 0
957 0 return $node->{sh} . '_' . $node->{sl};
958 }
959  
960 0 ▶sub __get_bits {
Subroutine __get_bits: not called CC=3 SCAR=24.8
961 0 my ( $int ) = @_;
962 0 my $and = 0x80;
963 0 my @list;
964 0 my $any_hits = 0;
965 0 for ( 1 .. 8 ) {
966 0 ▶ if ( $int & $and ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int & $and) { } 0 0 0
967 # if the bit is set == 1
968 0 push @list, 1;
969 0 $any_hits = 1;
970 } else {
971 # if the bit is not set == 0
972 0 push @list, 0;
973 }
974  
975 # shift the constant using right shift
976 0 $and = $and >> 1;
977 }
978 0 return ( $any_hits, @list );
979 }
980  
981 0 ▶sub __parse_at_command_response {
Subroutine __parse_at_command_response: not called CC=2 SCAR=17.9
982 0 my ( $api_data ) = @_;
983  
984 0 my @u = unpack( 'Ca[2]Ca*', $api_data );
985  
986 0 my $r = {
987 frame_id => $u[0],
988 command => $u[1],
989 status => $u[2],
990 data => $u[3],
991 is_ok => $u[2] == 0,
992 is_error => $u[2] == 1,
993 is_invalid_command => $u[2] == 2,
994 is_invalid_parameter => $u[2] == 3,
995 };
996  
997 0 ▶ if ( $r->{command} eq 'ND' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'command'} eq 'ND') { } 0 0 0
998 (
999 $r->{na}, $r->{sh}, $r->{sl},
1000 $r->{ni}, $r->{parent_network_address}, $r->{device_type},
1001 $r->{source_event}, $r->{profile_id}, $r->{manufacturer_id},
1002 0 ) = unpack( 'nNNZ*nCCnna*', $r->{data} );
1003 # The ND API calls it "my" but it's "na" everywhere else. Provide both
1004 # because the user may expect to see "my" after this packet arrives.
1005 # This module only uses "na".
1006 0 $r->{my} = $r->{na};
1007 } else {
1008 0 $r->{data_as_int} = __data_to_int( $r->{data} );
1009 }
1010  
1011 0 return $r;
1012 }
1013  
1014 0 ▶sub __data_to_int {
Subroutine __data_to_int: not called CC=5 SCAR=34.0
1015 0 my ( $data ) = @_;
1016  
1017 0 ▶ if ( length( $data ) == 1 ) {
Branch: 4 branches
BranchTrueFalseTotal
if (length $data == 1) { } 0 0 0
elsif (length $data == 2) { } 0 0 0
elsif (length $data == 4) { } 0 0 0
elsif (length $data == 8) { } 0 0 0
1018 0 return unpack( 'C', $data );
1019 } elsif ( length( $data ) == 2 ) {
1020 0 return unpack( 'n', $data );
1021 } elsif ( length( $data ) == 4 ) {
1022 0 return unpack( 'N', $data );
1023 } elsif ( length( $data ) == 8 ) {
1024 0 my ( $h, $l ) = unpack( 'NN', $data );
1025 0 return ( $l | ( $h << 32 ) );
1026 }
1027 0 return undef;
1028 }
1029  
1030 0 ▶sub __parse_modem_status {
Subroutine __parse_modem_status: not called CC=1 SCAR=6.9
1031 0 my ( $api_data ) = @_;
1032 0 my $u = unpack( 'C', $api_data );
1033 return {
1034 0 status => $u,
1035 is_hardware_reset => $u == 1,
1036 is_wdt_reset => $u == 2,
1037 is_associated => $u == 3,
1038 is_disassociated => $u == 4,
1039 is_sync_lost => $u == 5,
1040 is_coord_realign => $u == 6,
1041 is_coord_start => $u == 7,
1042 };
1043 }
1044  
1045 0 ▶sub __parse_zigbee_receive_packet {
Subroutine __parse_zigbee_receive_packet: not called CC=2 SCAR=17.9
1046 0 my ( $api_data ) = @_;
1047 0 my @u = unpack( 'NNnCa*', $api_data );
1048 # sh sl and na are named to match the fields in a network discovery AT
1049 # packet response
1050 return {
1051 0 ▶ sh => $u[0],
Branch: 1 branch
BranchTrueFalseTotal
$u[3] & 2 ? : 0 0 0
1052 sl => $u[1],
1053 na => $u[2],
1054 options => $u[3],
1055 data => $u[4],
1056 is_ack => $u[3] & 0x01,
1057 is_broadcast => ( $u[3] & 0x02 ? 1 : 0 ),
1058 };
1059 }
1060  
1061 0 ▶sub __parse_zigbee_explicit_rx_indicator {
Subroutine __parse_zigbee_explicit_rx_indicator: not called CC=4 SCAR=30.0
1062 0 my ( $api_data ) = @_;
1063 0 my @u = unpack( 'NNnCCnnCa*', $api_data );
1064  
1065 return {
1066 0 ▶ sh => $u[0],
Branch: 3 branches
BranchTrueFalseTotal
$u[7] & 2 ? : 0 0 0
$u[7] & 32 ? : 0 0 0
$u[7] & 64 ? : 0 0 0
1067 sl => $u[1],
1068 na => $u[2],
1069 se => $u[3],
1070 de => $u[4],
1071 ci => $u[5],
1072 profile_id => $u[6],
1073 options => $u[7],
1074 data => $u[8],
1075 is_ack => $u[7] & 0x01,
1076 is_broadcast => ( $u[7] & 0x02 ? 1 : 0 ),
1077 is_encrypted => ( $u[7] & 0x20 ? 1 : 0 ),
1078 is_from_end_device => ( $u[7] & 0x40 ? 1 : 0 ),
1079 };
1080 }
1081  
1082 0 ▶sub __parse_zigbee_transmit_status {
Subroutine __parse_zigbee_transmit_status: not called CC=1 SCAR=6.9
1083 0 my ( $api_data ) = @_;
1084 0 my @u = unpack( 'CnCCC', $api_data );
1085 return {
1086 0 frame_id => $u[0],
1087 remote_na => $u[1],
1088 tx_retry_count => $u[2],
1089 delivery_status => $u[3],
1090 discovery_status => $u[4]
1091 };
1092 }
1093  
1094 0 ▶sub __parse_zigbee_io_data_sample_rx_indicator {
Subroutine __parse_zigbee_io_data_sample_rx_indicator: not called CC=10 SCAR=47.0
1095 0 my ( $api_data ) = @_;
1096 0 my @u = unpack( 'NNnCCCCCa*', $api_data );
1097 0 my $data = $u[8];
1098 0 ▶ my $r = {
Branch: 1 branch
BranchTrueFalseTotal
$u[3] & 2 ? : 0 0 0
1099 sh => $u[0],
1100 sl => $u[1],
1101 na => $u[2],
1102 options => $u[3],
1103 is_ack => $u[3] & 0x01,
1104 is_broadcast => ( $u[3] & 0x02 ? 1 : 0 ),
1105 number_samples => $u[4],
1106 data => unpack( "h*", $data )
1107 };
1108  
1109 0 my ( $any_d1, $any_d2, $any_a );
1110 0 my @bits;
1111 0 ( $any_d1, @bits ) = __get_bits( $u[5] );
1112 0 $r->{"digital_channel_first"} = [@bits];
1113 0 ( $any_d2, @bits ) = __get_bits( $u[6] );
1114 0 $r->{"digital_channel_second"} = [@bits];
1115 0 ( $any_a, @bits ) = __get_bits( $u[7] );
1116 0 $r->{"analog_channel_bits"} = [@bits];
1117 0 my @digital;
1118  
1119 # do we need grab the digital 16 bits?
1120 0 ▶ if ( $any_d1 + $any_d2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($any_d1 + $any_d2) 0 0 0
1121 0 my ( $d1, $d2 );
1122 0 ( $d1, $d2, $data ) = unpack( "CCa*", $data );
1123 0 my $trash;
1124 my @digital_status;
1125 0 my @digital;
1126 0 ( $trash, @digital_status ) = __get_bits( $d1 );
1127 0 ▶ if ( $r->{"digital_channel_first"}[3] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"digital_channel_first"}[3] == 1) 0 0 0
1128 0 $digital[12] = $digital_status[3];
1129 }
1130 0 ▶ if ( $r->{"digital_channel_first"}[4] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"digital_channel_first"}[4] == 1) 0 0 0
1131 0 $digital[11] = $digital_status[4];
1132 }
1133 0 ▶ if ( $r->{"digital_channel_first"}[5] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"digital_channel_first"}[5] == 1) 0 0 0
1134 0 $digital[10] = $digital_status[5];
1135 }
1136 0 ( $trash, @digital_status ) = __get_bits( $d2 );
1137 0 my $d_number = 7;
1138 0 ▶ for ( my $i = 0; $i < 8; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 8) 0 0 0
1139 0 ▶ if ( $r->{"digital_channel_second"}[$i] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"digital_channel_second"}[$i] == 1) 0 0 0
1140 0 $digital[$d_number] = $digital_status[$i];
1141 }
1142 0 $d_number--;
1143 }
1144 0 $r->{"digital_inputs"} = \@digital;
1145 }
1146  
1147 # now get the analog values, if any
1148 0 my @analog;
1149 0 ▶ for ( my $i = 7; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
1150 0 ▶ if ( $r->{"analog_channel_bits"}[$i] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"analog_channel_bits"}[$i] == 1) 0 0 0
1151 0 ( $analog[7 - $i], $data ) = unpack( 'na*', $data );
1152 }
1153 }
1154 0 $r->{"analog_inputs"} = \@analog;
1155 0 return $r;
1156 }
1157  
1158 0 ▶sub __parse_node_identification_indicator {
Subroutine __parse_node_identification_indicator: not called CC=2 SCAR=17.9
1159 0 my ( $api_data ) = @_;
1160 0 my @u = unpack( 'NNnCnNNZ*nCCnn', $api_data );
1161 return {
1162 0 ▶ source_sh => $u[0],
Branch: 1 branch
BranchTrueFalseTotal
$u[3] & 2 ? : 0 0 0
1163 source_sl => $u[1],
1164 source_na => $u[2],
1165 options => $u[3],
1166 is_ack => $u[3] & 0x01,
1167 is_broadcast => ( $u[3] & 0x02 ? 1 : 0 ),
1168 remote_na => $u[4],
1169 remote_sh => $u[5],
1170 remote_sl => $u[6],
1171 ni => $u[7],
1172 parent_address => $u[8],
1173 device_type => $u[9],
1174 source_event => $u[10],
1175 profile_id => $u[11],
1176 mfg_id => $u[12]
1177 };
1178 }
1179  
1180 0 ▶sub __parse_remote_command_response {
Subroutine __parse_remote_command_response: not called CC=1 SCAR=6.9
1181 0 my ( $api_data ) = @_;
1182 0 my @u = unpack( 'CNNna[2]Ca*', $api_data );
1183 return {
1184 0 frame_id => $u[0],
1185 sh => $u[1],
1186 sl => $u[2],
1187 na => $u[3],
1188 command => $u[4],
1189 status => $u[5],
1190 data => $u[6],
1191 data_as_int => __data_to_int( $u[6] ),
1192 is_ok => $u[5] == 0,
1193 is_error => $u[5] == 1,
1194 is_invalid_command => $u[5] == 2,
1195 is_invalid_parameter => $u[5] == 3,
1196 is_remote_cmd_xmit_failed => $u[5] == 4,
1197 };
1198 }
1199  
1200 =head1 EXAMPLES
1201  
1202 Miscellaneous code examples follow.
1203  
1204 =head2 Fetch modem baud rage
1205  
1206 use Device::SerialPort;
1207 use Device::XBee::API;
1208
1209 # From XBee datasheet pg 73.
1210 my @baud_rate_table = (
1211 1200,
1212 2400,
1213 4800,
1214 9600,
1215 19200,
1216 38400,
1217 57600,
1218 115200
1219 );
1220
1221 # Configure the serial port
1222 my $serial_port_device = Device::SerialPort->new( '/dev/ttyU0' )
1223 || die $!;
1224 $serial_port_device->baudrate( 9600 );
1225 $serial_port_device->databits( 8 );
1226 $serial_port_device->stopbits( 1 );
1227 $serial_port_device->parity( 'none' );
1228 $serial_port_device->read_char_time( 0 );
1229 $serial_port_device->read_const_time( 1000 );
1230
1231 # Create the API object
1232 my $api = Device::XBee::API->new( { fh => $serial_port_device } )
1233 || die $!;
1234
1235 # Send the BD API command
1236 my $at_frame_id = $api->at( 'BD' );
1237 die "Transmit failed" unless $at_frame_id;
1238
1239 # Receive the reply
1240 my $rx = $api->rx_frame_id( $at_frame_id );
1241 die "No reply received" if !$rx;
1242 if ( $rx->{status} != 0 ) {
1243 die "API error" if $rx->{is_error};
1244 die "Invalid command" if $rx->{is_invalid_command};
1245 die "Invalid parameter" if $rx->{is_invalid_parameter};
1246 die "Unknown error";
1247 }
1248
1249 my $baud_rate = $baud_rate_table[ $rx->{data_as_int} ];
1250 if ( !$baud_rate ) {
1251 $baud_rate = $rx->{data_as_int};
1252 }
1253
1254 print "Modem baud rate is $baud_rate bps.\n";
1255  
1256  
1257 =head1 CHANGES
1258  
1259 =head2 0.8, 20140914 - jeagle
1260  
1261 Clean up remote_at function.
1262  
1263 Improve error checking for invalid and empty packets.
1264  
1265 =head2 0.7, 20130330 - jeagle
1266  
1267 Add ability to allow users to specify their own frame allocation routines.
1268  
1269 Update API mode 2 with latest version from jdodgen
1270  
1271 =head2 0.6, 20120624 - jeagle
1272  
1273 Update documentation.
1274  
1275 Add support for API mode 2 escapes. Needs testing.
1276  
1277 Add constant for the "BD" baud rate table.
1278  
1279 =head2 0.5, 20120401 - jeagle
1280  
1281 Add support for Win32::SerialPort to enable Windows support. (Thanks Jerry)
1282  
1283 Fix issue with tx() in async mode. (Thanks Vicente)
1284  
1285 Add support for "explicit rx indicator" packets. (Thanks Vicente)
1286  
1287 =head2 0.4, 20110831 - jeagle
1288  
1289 Fix packet timeout bug reported by Dave S.
1290  
1291 Replace call to die() in __data_to_int with return undef, update docs to
1292 reflect this.
1293  
1294 =head2 0.3, 20110621 - jeagle, jdodgen
1295  
1296 Change from internal Device::SerialPort wrapper to accepting an fh.
1297  
1298 Add asynchronous support to tx and add some helpful methods to support it.
1299  
1300 Handle more command types (remote AT, ZigBee IO, node identification).
1301  
1302 Add an option to re-use frame IDs under high tx load.
1303  
1304 Many more changes!
1305  
1306 =head2 0.2, 20101206 - jeagle
1307  
1308 Initial release to CPAN.
1309  
1310 =cut
1311  
1312 1 1;
↑ Summary