Digi XBee(R) ANSI C Host Library
device.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2013 Digi International Inc.,
3  * All rights not expressly granted are reserved.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
7  * You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
10  * =======================================================================
11  */
12 
24 #ifndef __XBEE_DEVICE
25 #define __XBEE_DEVICE
26 
27 #include "xbee/platform.h"
28 #include "xbee/serial.h"
29 #include "wpan/types.h"
30 #include "wpan/aps.h"
31 
33 
38 #define XBEE_DEV_FLAG_NONE 0x0000
39 
41 #ifndef XBEE_DEV_MAX_DISPATCH_PER_TICK
42  #define XBEE_DEV_MAX_DISPATCH_PER_TICK 5
43 #endif
44 
54 
58 
62 
66 
70 
73 
77 
81 
85 
88 
92 
96 
99  XBEE_FRAME_RECEIVE = 0x90, // ATAO == 0
100 
103  XBEE_FRAME_RECEIVE_EXPLICIT = 0x91, // ATAO != 0
104 
107 
110 
113 
117 
120 
123 
127 
130 
134 
139 };
140 
145 #define XBEE_RX_OPT_ACKNOWLEDGED 0x01
148 
150 #define XBEE_RX_OPT_BROADCAST 0x02
151 
153 #define XBEE_RX_OPT_SENT_ENCRYPTED 0x10
154 
156 #define XBEE_RX_OPT_APS_ENCRYPT 0x20
157 
159 #define XBEE_RX_OPT_FROM_END_DEVICE 0x40 // appeared in ZB 2x7x
160 
162 #define XBEE_RX_OPT_MODE_MASK 0xC0
163 
165  #define XBEE_RX_OPT_MODE_NONE (0)
166 
168  #define XBEE_RX_OPT_MODE_POINT_MULTIPOINT (1<<6)
169 
171  #define XBEE_RX_OPT_MODE_REPEATER (2<<6)
172 
174  #define XBEE_RX_OPT_MODE_DIGIMESH (3<<6)
175 
178 #ifndef XBEE_MAX_RFPAYLOAD
179  #if XBEE_CELLULAR_ENABLED
180  #define XBEE_MAX_RFPAYLOAD 1500
181  #elif XBEE_WIFI_ENABLED
182  #define XBEE_MAX_RFPAYLOAD 1400
183  #else
184  #define XBEE_MAX_RFPAYLOAD 256
185  #endif
186 #endif
187 
190 #define XBEE_MAX_RX_FRAME_LEN (XBEE_MAX_RFPAYLOAD + 18)
191 
194 #define XBEE_MAX_TX_FRAME_LEN (XBEE_MAX_RFPAYLOAD + 20)
195 
197 #define XBEE_MAX_FRAME_LEN XBEE_MAX_RX_FRAME_LEN
198 
199 // We need to declare struct xbee_dev_t here so the compiler doesn't treat it
200 // as a local definition in the parameter lists for the function pointer
201 // typedefs that follow.
202 struct xbee_dev_t;
203 
204 
209 
236 /*
237  Possible errors that will need unique -Exxx return values:
238  - Invalid xbee_dev_t
239  - No wpan_if assigned to xbee_dev_t
240  - Invalid length (must be > 0)
241  - Frame pointer is NULL
242 
243  @todo What will _xbee_frame_dispatch do with those return values?
244  Does there need to be a return value equivalent to "thanks
245  for that frame, but please remove me from the dispatch table"?
246  Right now, the dispatcher is ignoring the return value.
247  Could be useful when registering to receive status information
248  on outbound frames -- once we have status, we don't need
249  to receive any additional notifications.
250 */
251 typedef int (*xbee_frame_handler_fn)(
252  struct xbee_dev_t *xbee,
253  const void FAR *frame,
254  uint16_t length,
255  void FAR *context
256 );
257 
267 typedef int (*xbee_is_awake_fn)( struct xbee_dev_t *xbee);
268 
276 typedef void (*xbee_reset_fn)( struct xbee_dev_t *xbee, bool_t asserted);
277 
279 struct xbee_node_id_t;
294 typedef void (*xbee_disc_node_id_fn)( struct xbee_dev_t *xbee,
295  const struct xbee_node_id_t *rec);
297 
298 typedef struct xbee_dispatch_table_entry {
301  xbee_frame_handler_fn handler;
302  void FAR *context;
304 
305 
306 
312 };
313 
315 {
322 
324 
331 
332  // (cast to int required by Codewarrior/HCS08 platform if enum is signed)
334 };
335 
341  // Modes used by "AT firmware" and some bootloaders:
349 };
350 
361 typedef struct xbee_dev_t
362 {
365 
368 
371 
374 
377 
384  #define XBEE_HARDWARE_MASK 0xFF00
385  #define XBEE_HARDWARE_S1 0x1700
386  #define XBEE_HARDWARE_S1_PRO 0x1800
387  #define XBEE_HARDWARE_S2 0x1900
388  #define XBEE_HARDWARE_S2_PRO 0x1A00
389  #define XBEE_HARDWARE_900_PRO 0x1B00
390  #define XBEE_HARDWARE_868_PRO 0x1D00
391  #define XBEE_HARDWARE_S2B_PRO 0x1E00
392  #define XBEE_HARDWARE_S2C_PRO 0x2100
393  #define XBEE_HARDWARE_S2C 0x2200
394  #define XBEE_HARDWARE_S3B 0x2300 // XBee 900HP
395  #define XBEE_HARDWARE_S8 0x2400
396  #define XBEE_HARDWARE_S6B 0x2700 // XBee Wi-Fi
397  #define XBEE_HARDWARE_CELL_CAT1_VZW 0x4000
398  #define XBEE_HARDWARE_XB3_MICRO 0x4100
399  #define XBEE_HARDWARE_XB3_TH 0x4200
400  #define XBEE_HARDWARE_CELL_3G 0x4400
401 
409  #define XBEE_HW_SERIES_MASK 0xFF00
410  #define XBEE_HW_SERIES_WIFI_S6 0x0600
411  #define XBEE_HW_SERIES_CELLULAR 0x0B00
412  #define XBEE_HW_SERIES_XBEE3_RF 0x0C00
413 
421  #define XBEE_PROTOCOL_MASK 0xF000
422  // Series 2 (2.4 GHz) hardware
423  #define XBEE_PROTOCOL_ZNET 0x1000
424  #define XBEE_PROTOCOL_ZB 0x2000
425  #define XBEE_PROTOCOL_SMARTENERGY 0x3000
426  #define XBEE_PROTOCOL_ZB_S2C 0x4000
427  #define XBEE_PROTOCOL_SE_S2C 0x5000
428  // Series 4 (900 MHz) hardware
429  #define XBEE_PROTOCOL_MESHLESS 0x1000
430  #define XBEE_PROTOCOL_DIGIMESH 0x8000
431  // XBee 3 hardware
432  #define XBEE_PROTOCOL_XB3_ZIGBEE 0x1000
433  #define XBEE_PROTOCOL_XB3_802_15_4 0x2000
434  #define XBEE_PROTOCOL_XB3_DIGIMESH 0x3000
435 
436  #define XBEE_NODETYPE_MASK 0x0F00
437  #define XBEE_NODETYPE_COORD 0x0100
438  #define XBEE_NODETYPE_ROUTER 0x0300
439  #define XBEE_NODETYPE_ENDDEV 0x0900
440 
443  enum xbee_dev_flags flags;
444 
446 
447  // Need some state variables here if AT mode is supported (necessary when
448  // using modules with AT firmware instead of API firmware, or when doing
449  // firmware updates on DigiMesh 900 with API firmware). Current state:
450  // idle mode, command mode, pre-escape guard-time, post-escape guard-time.
451  // Current timeout: value of MS_TIMER when guard-time expired or we expect
452  // to return to idle mode from command mode.
453  // If we track MS_TIMER from the last byte we sent to the XBee, we can
454  // potentially skip over the pre-escape guard time and send the escape chars.
455 
457  #ifdef XBEE_DEVICE_ENABLE_ATMODE
458  enum xbee_dev_mode mode;
459  uint32_t mode_timer;
460  uint16_t guard_time;
461  uint16_t idle_timeout;
462  char escape_char;
463  #endif
464 
467  struct rx {
469  enum xbee_dev_rx_state state;
470 
473 
476 
478  uint8_t frame_data[XBEE_MAX_FRAME_LEN + 1];
479  } rx;
480 } xbee_dev_t;
481 
493 #if BYTE_ORDER == BIG_ENDIAN
494  #define XBEE_DEV_STACK_VERSION_ADDR(x) ((uint8_t *)&(x).firmware_version + 3)
495 #else
496  #define XBEE_DEV_STACK_VERSION_ADDR(x) ((uint8_t *)&(x).firmware_version)
497 #endif
498 
507 
508 #define XBEE_FRAME_TABLE_END { 0xFF, 0, NULL, NULL }
509 
511 
512 int xbee_dev_init( xbee_dev_t *xbee, const xbee_serial_t *serport,
513  xbee_is_awake_fn is_awake, xbee_reset_fn reset);
514 
515 void xbee_dev_dump_settings( xbee_dev_t *xbee, uint16_t flags);
516  #define XBEE_DEV_DUMP_FLAG_NONE 0x0000
517 #ifndef XBEE_DEV_DUMP_FLAG_DEFAULT
518  #define XBEE_DEV_DUMP_FLAG_DEFAULT XBEE_DEV_DUMP_FLAG_NONE
519 #endif
520 
521 int xbee_dev_reset( xbee_dev_t *xbee);
522 
523 int xbee_dev_tick( xbee_dev_t *xbee);
524 
525 int xbee_frame_write( xbee_dev_t *xbee, const void FAR *header,
526  uint16_t headerlen, const void FAR *data, uint16_t datalen,
527  uint16_t flags);
528 #define XBEE_WRITE_FLAG_NONE 0x0000
529 
530 void xbee_dev_flowcontrol( xbee_dev_t *xbee, bool_t enabled);
531 
532 // private functions exposed for unit testing
533 
534 void _xbee_dispatch_table_dump( const xbee_dev_t *xbee);
535 
536 uint8_t _xbee_checksum( const void FAR *bytes, uint16_t length,
537  uint_fast8_t initial);
538 
539 int _xbee_frame_load( xbee_dev_t *xbee);
540 
541 int _xbee_frame_dispatch( xbee_dev_t *xbee, const void FAR *frame,
542  uint16_t length);
543 
544 
545 typedef XBEE_PACKED(xbee_frame_modem_status_t, {
546  uint8_t frame_type;
547  uint8_t status;
549 
554 #define XBEE_MODEM_STATUS_HW_RESET 0x00
556 #define XBEE_MODEM_STATUS_WATCHDOG 0x01
558 #define XBEE_MODEM_STATUS_JOINED 0x02
560 #define XBEE_MODEM_STATUS_DISASSOC 0x03
562 #define XBEE_MODEM_STATUS_IP_CONFIG_ERROR 0x04
564 #define XBEE_MODEM_STATUS_COORD_START 0x06
566 #define XBEE_MODEM_STATUS_NETWORK_KEY_UPDATED 0x07
568 #define XBEE_MODEM_STATUS_WOKE_UP 0x0B
570 #define XBEE_MODEM_STATUS_SLEEPING 0x0C
572 #define XBEE_MODEM_STATUS_OVERVOLTAGE 0x0D
574 #define XBEE_MODEM_STATUS_CLOUD_CONNECTED 0x0E
576 #define XBEE_MODEM_STATUS_CLOUD_DISCONNECTED 0x0F
578 #define XBEE_MODEM_STATUS_KEY_ESTABLISHED 0x10
580 #define XBEE_MODEM_STATUS_CONFIG_CHANGE_IN_JOIN 0x11
582 #define XBEE_MODEM_STATUS_SS_ESTABLISHED 0x3B
584 #define XBEE_MODEM_STATUS_SS_ENDED 0x3C
586 #define XBEE_MODEM_STATUS_SS_AUTH_FAILED 0x3D
588 #define XBEE_MODEM_STATUS_STACK_ERROR 0x80
590 
603  const void FAR *frame, uint16_t length, void FAR *context);
604 
609 #define XBEE_FRAME_MODEM_STATUS_DEBUG \
610  { XBEE_FRAME_MODEM_STATUS, 0, xbee_frame_dump_modem_status, NULL }
611 
613 
614 // If compiling in Dynamic C, automatically #use the appropriate C file.
615 #ifdef __DC__
616  #use "xbee_device.c"
617 #endif
618 
619 #endif // #ifdef __XBEE_DEVICE
620 
unsigned long uint32_t
32-bit unsigned integer
Definition: platform_config.h:43
Node Type is End Device.
Definition: device.h:327
uint32_t firmware_version
Value of XBee module&#39;s VR register (4-bytes on some devices)
Definition: device.h:416
int xbee_dev_init(xbee_dev_t *xbee, const xbee_serial_t *serport, xbee_is_awake_fn is_awake, xbee_reset_fn reset)
Initialize the XBee device structure and open a serial connection to a local, serially-attached XBee ...
Definition: xbee_device.c:125
void xbee_dev_dump_settings(xbee_dev_t *xbee, uint16_t flags)
Print information to stdout about the XBee device.
Definition: xbee_device.c:229
WPAN datatypes and support functions, valid for ZigBee and DigiMesh.
Frame notifying trust center that a device has attempted to join, rejoin or leave the network...
Definition: device.h:138
#define FAR
On platforms with "far" pointers, define to the proper keyword; empty definition if not required...
Definition: platform.h:396
Firmware is ZNet.
Definition: device.h:328
wpan_dev_t wpan_dev
Generic WPAN device required by the Zigbee Networking layers of the API.
Definition: device.h:364
Data received for specific endpoint/cluster (see xbee_wpan.c), when ATAO is non-zero.
Definition: device.h:103
Node Type is Router.
Definition: device.h:326
Definition: platform_config.h:71
command mode, can send AT commands to XBee
Definition: device.h:344
#define XBEE_BEGIN_DECLS
Macro defined to.
Definition: platform.h:41
Common header for Hardware Abstraction Layer.
Information on device authenticated on Smart Energy network.
Definition: device.h:126
query is in progress
Definition: device.h:321
xbee_disc_node_id_fn node_id_handler
Optional function to receive parsed Node ID messages.
Definition: device.h:376
void(* xbee_disc_node_id_fn)(struct xbee_dev_t *xbee, const struct xbee_node_id_t *rec)
Function to process parsed Node ID messages.
Definition: device.h:294
sent a command and now waiting for a response
Definition: device.h:348
const xbee_dispatch_table_entry_t xbee_frame_handlers[]
Static table used for dispatching frames.
Current modem status (see xbee_frame_modem_status_t).
Definition: device.h:84
xbee_serial_t serport
Platform-specific structure required by xbee_serial.c.
Definition: device.h:367
waiting for initial 0x7E
Definition: device.h:308
Queue an AT command for batch processing on the local device.
Definition: device.h:57
xbee_is_awake_fn is_awake
Optional function to read AWAKE pin.
Definition: device.h:373
Node Type is Coordinator.
Definition: device.h:325
querying timed out or error
Definition: device.h:319
Route Information Frame, sent for DigiMesh unicast transmissions with NACK or Trace Route Enable tran...
Definition: device.h:91
Data received on the transparent serial cluster, when ATAO is set to 0.
Definition: device.h:99
wait for guard-time ms before sending +++
Definition: device.h:345
Current mode of the XBee device (e.g., boot loader, API, command).
Definition: device.h:467
started querying device
Definition: device.h:317
int(* xbee_is_awake_fn)(struct xbee_dev_t *xbee)
Function to check the XBee device&#39;s AWAKE pin to see if it is awake.
Definition: device.h:267
Haven&#39;t started communicating with XBee yet.
Definition: device.h:337
Frame sent in response to Register Joining Device frame (XBEE_FRAME_REG_JOINING_DEV).
Definition: device.h:133
waiting for MSB of length (first byte)
Definition: device.h:309
receiving frame and/or trailing checksum
Definition: device.h:311
xbee_dev_mode
Definition: device.h:336
void xbee_dev_flowcontrol(xbee_dev_t *xbee, bool_t enabled)
Control whether xbee_frame_write() checks the XBee CTS signal before sending frames.
Definition: xbee_device.c:189
xbee_reset_fn reset
Optional function to control reset pin.
Definition: device.h:370
uint8_t frame_type
if 0, match all frames of this type
Definition: device.h:299
int xbee_dev_tick(xbee_dev_t *xbee)
Check for newly received frames on an XBee device and dispatch them to registered frame handlers...
Definition: xbee_device.c:313
#define XBEE_MAX_FRAME_LEN
Deprecated legacy macro, use XBEE_MAX_RX_FRAME_LEN instead.
Definition: device.h:197
uint16_t hardware_version
Value of XBee module&#39;s HV register.
Definition: device.h:379
Register Joining Device (add device to trust center&#39;s key table) [Smart Energy, coordinator].
Definition: device.h:76
Create Source Route (used with many-to-one routing) [ZigBee].
Definition: device.h:72
unsigned short uint16_t
16-bit unsigned integer
Definition: platform_config.h:41
uint8_t xbee_next_frame_id(xbee_dev_t *xbee)
Increment and return current frame ID for a given XBee device.
Definition: xbee_device.c:61
uint16_t hardware_series
Value of XBee module&#39;s HS register.
Definition: device.h:404
void _xbee_dispatch_table_dump(const xbee_dev_t *xbee)
Definition: xbee_device.c:354
querying completed
Definition: device.h:318
[ZigBee, not Smart Energy]
Definition: device.h:106
XBee is in the bootloader, not running firmware.
Definition: device.h:338
Route records received in response to a Route Request. [ZigBee].
Definition: device.h:122
Many-to-One Route Request Indicator [ZigBee].
Definition: device.h:129
in xbee_dev_tick
Definition: device.h:323
int _xbee_frame_dispatch(xbee_dev_t *xbee, const void FAR *frame, uint16_t length)
Function called by _xbee_frame_load() to dispatch any frames read.
Definition: xbee_device.c:894
Platform-specific layer provieds a consistent serial API to upper layers of the driver.
unsigned char uint8_t
8-bit unsigned integer
Definition: platform_config.h:39
Output when a node receives an address update frame and modifies its DH/DL registers.
Definition: device.h:95
int _xbee_frame_load(xbee_dev_t *xbee)
Check XBee&#39;s serial buffer for complete frames and pass them off to the dispatcher.
Definition: xbee_device.c:629
xbee_frame_type
Possible values for the frame_type field of frames sent to and from the XBee module.
Definition: device.h:50
uint16_t bytes_in_frame
bytes in frame being read; does not include checksum byte
Definition: device.h:472
Check CTS before sending.
Definition: device.h:333
Firmware is DigiMesh.
Definition: device.h:330
wait guard-time ms for "OK\r" before command mode
Definition: device.h:346
Cluster/Endpoint layer for WPAN networks (ZigBee and DigiMesh).
Send an AT Command to the local device (see xbee_atcmd.c, xbee_header_at_request_t).
Definition: device.h:53
Firmware is ZigBee.
Definition: device.h:329
void(* xbee_reset_fn)(struct xbee_dev_t *xbee, bool_t asserted)
Function to toggle the /RESET pin to the XBee device.
Definition: device.h:276
uint16_t bytes_read
bytes read so far
Definition: device.h:475
waiting for OK response to ATCN command
Definition: device.h:347
waiting for LSB of length (second byte)
Definition: device.h:310
Response from remote device to AT Command (see xbee_atcmd.c, xbee_cmd_response_t).
Definition: device.h:116
XBee is using API firmware.
Definition: device.h:342
xbee_cmd_init called
Definition: device.h:316
Send an AT command to a remote device on the network (see xbee_atcmd.c, xbee_header_at_request_t).
Definition: device.h:69
parsed Node ID in host-byte-order and fixed length fields
Definition: discovery.h:126
Over-the-Air Firmware Update Status [ZigBee, not Smart Energy].
Definition: device.h:119
xbee_dev_flags
Definition: device.h:314
int xbee_dev_reset(xbee_dev_t *xbee)
Toggles the reset line of the XBee device.
Definition: xbee_device.c:258
Send data to a default endpoint and cluster on a remote device.
Definition: device.h:61
int xbee_frame_write(xbee_dev_t *xbee, const void FAR *header, uint16_t headerlen, const void FAR *data, uint16_t datalen, uint16_t flags)
Copies a frame into the transmit serial buffer to send to an XBee module.
Definition: xbee_device.c:494
[ZigBee, DigiMesh, not Smart Energy]
Definition: device.h:112
Response from local device to AT Command (see xbee_atcmd.c, xbee_cmd_response_t). ...
Definition: device.h:80
Definition: device.h:361
uint8_t frame_id
last frame_id used for sending
Definition: device.h:445
< XBEE_FRAME_MODEM_STATUS (0x8A) See XBEE_MODEM_STATUS_*
Definition: device.h:548
uint8_t frame_id
if 0, match all frames of this identifier
Definition: device.h:300
[ZigBee, not Smart Energy]
Definition: device.h:109
Frame sent upon completion of a Transmit Request. [DigiMesh, ZigBee].
Definition: device.h:87
idle mode, data sent is passed to remote XBee
Definition: device.h:343
#define XBEE_END_DECLS
Macro defined to.
Definition: platform.h:42
xbee_dev_rx_state
Definition: device.h:307
Definition: device.h:298
uint8_t _xbee_checksum(const void FAR *bytes, uint16_t length, uint_fast8_t initial)
Calculate the checksum for an XBee frame.
Definition: xbee_device.c:424
int(* xbee_frame_handler_fn)(struct xbee_dev_t *xbee, const void FAR *frame, uint16_t length, void FAR *context)
Standard API for an XBee frame handler in xbee_frame_handlers global.
Definition: device.h:251
int bool_t
Variable that can hold 0 or 1, may be an int for speed purporses or uint8_t for size optimization...
Definition: platform_config.h:51
Structure used by the WPAN/ZigBee layers.
Definition: aps.h:390
Send data to a specific endpoint and cluster on a remote device (see xbee_wpan.c).
Definition: device.h:65
need to re-query device
Definition: device.h:320
int xbee_frame_dump_modem_status(xbee_dev_t *xbee, const void FAR *frame, uint16_t length, void FAR *context)
Frame handler for 0x8A (XBEE_FRAME_MODEM_STATUS) frames – dumps modem status to STDOUT for debugging...
Definition: xbee_device.c:960