API for communicating with XBee devices.

Dependencies:   CircularBuffer FixedLengthList

Dependents:   XBeeApiTest XBeeApiSimpleATCmdsExample XBeeApiBroadcastExample XBeeApiBroadcastExampleRTOS ... more

Overview

XBeeApi is intended to be a library for providing a high-level API interface to the XBee - for example getChannel() and setChannel(2) methods rather than needing to send( "ATCH" ) and send( "ATCH 2" ) - and then de-code the responses.

See the notebook page here for a description of how the API works & some details on the various classes.

Features:

  • Support for transmission & reception of data packets
  • Support for reading & changing settings
  • Support for "Remote AT" interface to access settings & I/O channels on remote XBees
  • XBeeApi should work if you're using mbed-rtos, though it is not currently threadsafe. Take a look at the XBeeApiBroadcastExampleRTOS example if you're including mbed-rtos.

Example Programs

There are also example programs available:

Transmit

Import programXBeeApiSimpleBroadcastExample

Simple example of how to use XBeeApi - set up the XBee, configure P2P networking then transmit a frame.

Import programXBeeApiBroadcastExample

Example for XBeeAPI; a little more involved than XBeeApiSimpleBroadcastExample with report on failure to set up the XBee and on the transmit status of the message.

Import programXBeeApiBroadcastExampleRTOS

Example of using the XBeeApi library to broadcast a message, based on XBeeApiBroadcastExample. This example shows how to use the library when using mbed-rtos. Before compiling you must open "XbeeApi\Config\XBeeApiCfg.hpp" and change the '#if 0' to '#if 1' on the line above the comment reading "Use RTOS features to make XBeeApi threadsafe"

Settings/Status

Import programXBeeApiSimpleATCmdsExample

Simple example of using XBeeApi to send AT-style commands to the XBee

Import programXBeeApiRemoteATCmdsExample

Example of using the XBeeApi library to send AT commands to remote XBee devices in order to read/write settings

Receive

Import programXBeeApiSimpleReceiveExample

Simple example of using XBeeApi to receive data packets via wireless

Import programXBeeApiReceiveCallbackExample

Example of using the XBeeApi library to receive a message via a callback method

Import programXBeeApiReceiveCallbackExampleRTOS

Example of using the XBeeApi library to receive a message via a callback method. This example shows how to use the library when using mbed-rtos. See the comment at the top of main.cpp

Remote I/O

Import programXBeeApiRemoteIOExample

Example of using the XBeeApi library to read inputs on a remote XBee

If you have 2 mbed connected XBees available then you can use XBeeApiSimpleReceiveExample and XBeeApiSimpleBroadcastExample as a pair.

Note that this is still a work in progress! XBeeApiTodoList tracks some of the functionality still to be added.

Committer:
johnb
Date:
Sun Jul 06 22:10:28 2014 +0000
Revision:
47:5d3608835668
Parent:
39:35ab09bff018
Child:
48:48397bedf95d
Re-jig XBeeApiTxFrame to allow getDataPtr to be a const method again.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 5:b40a6fd3a334 1 /**
johnb 5:b40a6fd3a334 2 @file
johnb 5:b40a6fd3a334 3 @brief Class to abstract commands send to the XBee API
johnb 5:b40a6fd3a334 4
johnb 5:b40a6fd3a334 5 @author John Bailey
johnb 5:b40a6fd3a334 6
johnb 5:b40a6fd3a334 7 @copyright Copyright 2013 John Bailey
johnb 5:b40a6fd3a334 8
johnb 5:b40a6fd3a334 9 @section LICENSE
johnb 5:b40a6fd3a334 10
johnb 5:b40a6fd3a334 11 Licensed under the Apache License, Version 2.0 (the "License");
johnb 5:b40a6fd3a334 12 you may not use this file except in compliance with the License.
johnb 5:b40a6fd3a334 13 You may obtain a copy of the License at
johnb 5:b40a6fd3a334 14
johnb 5:b40a6fd3a334 15 http://www.apache.org/licenses/LICENSE-2.0
johnb 5:b40a6fd3a334 16
johnb 5:b40a6fd3a334 17 Unless required by applicable law or agreed to in writing, software
johnb 5:b40a6fd3a334 18 distributed under the License is distributed on an "AS IS" BASIS,
johnb 5:b40a6fd3a334 19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 5:b40a6fd3a334 20 See the License for the specific language governing permissions and
johnb 5:b40a6fd3a334 21 limitations under the License.
johnb 5:b40a6fd3a334 22
johnb 5:b40a6fd3a334 23 */
johnb 5:b40a6fd3a334 24
johnb 5:b40a6fd3a334 25 #if !defined XBEEAPICMD_HPP
johnb 5:b40a6fd3a334 26 #define XBEEAPICMD_HPP
johnb 5:b40a6fd3a334 27
johnb 5:b40a6fd3a334 28 #include <stdint.h>
johnb 5:b40a6fd3a334 29 #include <stddef.h> // for size_t
johnb 5:b40a6fd3a334 30
johnb 5:b40a6fd3a334 31 /** API identifiers - the first byte in the API-specific structure within an API frame which provides an indication as to the type of data which follows */
johnb 5:b40a6fd3a334 32 typedef enum
johnb 5:b40a6fd3a334 33 {
johnb 5:b40a6fd3a334 34 XBEE_CMD_TX_64B_ADDR = 0x00,
johnb 5:b40a6fd3a334 35 XBEE_CMD_TX_16B_ADDR = 0x01,
johnb 5:b40a6fd3a334 36 XBEE_CMD_AT_CMD = 0x08,
johnb 5:b40a6fd3a334 37 XBEE_CMD_QUEUE_PARAM_VAL = 0x09,
johnb 5:b40a6fd3a334 38 XBEE_CMD_REMOTE_AT_CMD = 0x17,
johnb 5:b40a6fd3a334 39 XBEE_CMD_RX_64B_ADDR = 0x80,
johnb 5:b40a6fd3a334 40 XBEE_CMD_RX_16B_ADDR = 0x81,
johnb 5:b40a6fd3a334 41 XBEE_CMD_AT_RESPONSE = 0x88,
johnb 5:b40a6fd3a334 42 XBEE_CMD_TX_STATUS = 0x89,
johnb 5:b40a6fd3a334 43 XBEE_CMD_MODEM_STATUS = 0x8A,
johnb 5:b40a6fd3a334 44 XBEE_CMD_REMOTE_AT_RESPONSE = 0x97,
johnb 5:b40a6fd3a334 45 XBEE_CMD_INVALID = 0xFFF
johnb 5:b40a6fd3a334 46 } XBeeApiIdentifier_e;
johnb 5:b40a6fd3a334 47
johnb 5:b40a6fd3a334 48 /** Position of fixed meaning bytes within the API frame, relative to the start of the frame */
johnb 5:b40a6fd3a334 49 enum
johnb 5:b40a6fd3a334 50 {
johnb 5:b40a6fd3a334 51 /** Start delimiter, should be equal to XBEE_SB_FRAME_DELIMITER */
johnb 5:b40a6fd3a334 52 XBEE_CMD_POSN_SDELIM = 0x00,
johnb 5:b40a6fd3a334 53 /** High byte of the 16-bit length field */
johnb 5:b40a6fd3a334 54 XBEE_CMD_POSN_LEN_HI = 0x01,
johnb 5:b40a6fd3a334 55 /** Low byte of the 16-bit length field */
johnb 5:b40a6fd3a334 56 XBEE_CMD_POSN_LEN_LO = 0x02,
johnb 5:b40a6fd3a334 57 /** API identifier for the data which follows - see XBeeApiIdentifier_e */
johnb 15:ff9f12e38f44 58 XBEE_CMD_POSN_API_ID = 0x03,
johnb 15:ff9f12e38f44 59 /** Start of API identifier specific data */
johnb 15:ff9f12e38f44 60 XBEE_CMD_POSN_ID_SPECIFIC_DATA = 0x04
johnb 5:b40a6fd3a334 61 };
johnb 5:b40a6fd3a334 62
johnb 5:b40a6fd3a334 63 /** Helper macro to retrieve the frame payload length (i.e. excluding overhead - see XBEE_API_FRAME_OVERHEAD) from a buffer.
johnb 5:b40a6fd3a334 64
johnb 5:b40a6fd3a334 65 \param _b Pointer to a buffer containing a received API frame.
johnb 5:b40a6fd3a334 66 \returns Length of the frame payload excluding overhead */
johnb 5:b40a6fd3a334 67 #define MSG_LEN_IN_BUFFER( _b ) ((((uint16_t)_b[ XBEE_CMD_POSN_LEN_HI ]) << 8U) + _b[XBEE_CMD_POSN_LEN_LO])
johnb 5:b40a6fd3a334 68
johnb 5:b40a6fd3a334 69 /** The number of 'overhead' bytes in an API frame (i.e. those not included in the frame payload, namely the start delimiter, 2 length bytes & checksum */
johnb 5:b40a6fd3a334 70 #define XBEE_API_FRAME_OVERHEAD 4U
johnb 5:b40a6fd3a334 71
johnb 16:8095c43a2a6e 72 /* Forward declare this as XBeeDevice is dependent upon XBeeApiFrameDecoder */
johnb 16:8095c43a2a6e 73 class XBeeDevice;
johnb 16:8095c43a2a6e 74
johnb 5:b40a6fd3a334 75 /** Class which represents an API frame, exchanged with the XBee.
johnb 5:b40a6fd3a334 76 This class in itself will not create a valid API frame and needs to be sub-classed.
johnb 5:b40a6fd3a334 77 */
johnb 5:b40a6fd3a334 78 class XBeeApiFrame
johnb 5:b40a6fd3a334 79 {
johnb 5:b40a6fd3a334 80 private:
johnb 5:b40a6fd3a334 81 static const uint8_t m_cmdHeaderLen = 1;
johnb 5:b40a6fd3a334 82
johnb 5:b40a6fd3a334 83 protected:
johnb 5:b40a6fd3a334 84 /** API identifier for the data in the frame */
johnb 5:b40a6fd3a334 85 XBeeApiIdentifier_e m_apiId;
johnb 5:b40a6fd3a334 86
johnb 5:b40a6fd3a334 87 /** API identifier specific data */
johnb 5:b40a6fd3a334 88 const uint8_t* m_data;
johnb 5:b40a6fd3a334 89
johnb 5:b40a6fd3a334 90 /** Length of the data pointed to by m_data */
johnb 5:b40a6fd3a334 91 uint16_t m_dataLen;
johnb 5:b40a6fd3a334 92
johnb 5:b40a6fd3a334 93 public:
johnb 5:b40a6fd3a334 94
johnb 5:b40a6fd3a334 95 /** Constructor */
johnb 5:b40a6fd3a334 96 XBeeApiFrame( void );
johnb 5:b40a6fd3a334 97
johnb 24:2cd1094c4fd7 98 /** Constructor */
johnb 24:2cd1094c4fd7 99 XBeeApiFrame( XBeeApiIdentifier_e p_id,
johnb 38:291aee9eb900 100 const uint8_t* const p_data,
johnb 38:291aee9eb900 101 const size_t p_dataLen );
johnb 24:2cd1094c4fd7 102
johnb 5:b40a6fd3a334 103 /** Return the length of the API-specific structure represented by this frame. i.e. the API frame without the overhead - see XBEE_API_FRAME_OVERHEAD */
johnb 16:8095c43a2a6e 104 virtual uint16_t getCmdLen( void ) const;
johnb 5:b40a6fd3a334 105
johnb 5:b40a6fd3a334 106 /** Retrieve the API identifier for this frame */
johnb 5:b40a6fd3a334 107 XBeeApiIdentifier_e getApiId( void ) const;
johnb 5:b40a6fd3a334 108
johnb 10:0d084d0253a7 109 /** Retrieve a pointer to the API-specific data (i.e. that which follows the API identifier in
johnb 10:0d084d0253a7 110 the frame). In *total*, getDataPtr will return sufficient data to match getCmdLen(), however
johnb 10:0d084d0253a7 111 it may be done in chunks using p_start and *p_len to control the fragmentation process.
johnb 10:0d084d0253a7 112 For example:
johnb 10:0d084d0253a7 113
johnb 10:0d084d0253a7 114 getCmdLen() == 5;
johnb 10:0d084d0253a7 115 getDataPtr( 0, &b, &l );
johnb 10:0d084d0253a7 116 l == 2; b[0] == 'a'; b[1] == 'b';
johnb 10:0d084d0253a7 117 getDataPtr( 2, &b, &l );
johnb 10:0d084d0253a7 118 l == 1; b[2] == 'c';
johnb 10:0d084d0253a7 119 getDataPtr( 3, &b, &l );
johnb 10:0d084d0253a7 120 l == 2; b[0] == 'd'; b[1] == 'e';
johnb 10:0d084d0253a7 121
johnb 10:0d084d0253a7 122 \param[in] p_start The start point within the buffer. It is only meaningful to use a non-zero
johnb 10:0d084d0253a7 123 value in the case that a previous call to getDataPtr did not return a buffer
johnb 10:0d084d0253a7 124 of length getCmdLen(), in which case p_start should be set to be the next
johnb 10:0d084d0253a7 125 byte that is required.
johnb 10:0d084d0253a7 126 \param[out] p_buff Pointer to a pointer to receive the buffer pointer
johnb 10:0d084d0253a7 127 \param[out] p_len Pointer to receive the length of the data pointed to by *p_buff */
johnb 47:5d3608835668 128 virtual void getDataPtr( const uint16_t p_start, const uint8_t** p_buff, uint16_t* const p_len ) const;
johnb 5:b40a6fd3a334 129 };
johnb 5:b40a6fd3a334 130
johnb 6:3cb62daace78 131 /** Class which acts as a receiver for data from the XBee and takes care of decoding it.
johnb 6:3cb62daace78 132 This class needs to be sub-classed in order to create a valid decoder.
johnb 6:3cb62daace78 133
johnb 6:3cb62daace78 134 An object implementing the XBeeApiFrameDecoder interface should be registered with
johnb 6:3cb62daace78 135 an XBeeDevice-type object using the registerDecoder() method. If the class destructor
johnb 6:3cb62daace78 136 is called it will automatically un-register itself from the XBeeDevice. Alternatively
johnb 6:3cb62daace78 137 unregisterDecoder() may be called */
johnb 5:b40a6fd3a334 138 class XBeeApiFrameDecoder
johnb 5:b40a6fd3a334 139 {
johnb 5:b40a6fd3a334 140 protected:
johnb 6:3cb62daace78 141 /** Pointer to the XBeeDevice with which this decoder is associated. Kept around
johnb 6:3cb62daace78 142 so that we know where to go at time of destruction and the XBeeDevice isn't
johnb 6:3cb62daace78 143 left with a pointer to an invalidated object */
johnb 5:b40a6fd3a334 144 XBeeDevice* m_device;
johnb 5:b40a6fd3a334 145
johnb 5:b40a6fd3a334 146 public:
johnb 5:b40a6fd3a334 147
johnb 6:3cb62daace78 148 /** Constructor */
johnb 16:8095c43a2a6e 149 XBeeApiFrameDecoder( XBeeDevice* const p_device = NULL );
johnb 5:b40a6fd3a334 150
johnb 6:3cb62daace78 151 /** Destructor. Un-registers the decoder from any XBeeDevice object with which it is registered */
johnb 5:b40a6fd3a334 152 virtual ~XBeeApiFrameDecoder();
johnb 5:b40a6fd3a334 153
johnb 6:3cb62daace78 154 /** XBeeDevice is a friend so that it can access registerCallback and unregisterCallback */
johnb 6:3cb62daace78 155 friend class XBeeDevice;
johnb 6:3cb62daace78 156
johnb 6:3cb62daace78 157 protected:
johnb 6:3cb62daace78 158
johnb 6:3cb62daace78 159 /** Called by an XBeeDevice object to let this object know that it's being associated with a device */
johnb 5:b40a6fd3a334 160 void registerCallback( XBeeDevice* const p_device );
johnb 5:b40a6fd3a334 161
johnb 6:3cb62daace78 162 /** Called by an XBeeDevice object to let this object know that it's no longer associated with the device */
johnb 5:b40a6fd3a334 163 void unregisterCallback( void );
johnb 6:3cb62daace78 164
johnb 6:3cb62daace78 165 /** Called by an XBeeDevice in order to give this object the opportunity to examine and decode data received
johnb 6:3cb62daace78 166 from the XBee
johnb 6:3cb62daace78 167
johnb 6:3cb62daace78 168 Note: When implementing this method in an inheriting class the method must only return true in the case that
johnb 6:3cb62daace78 169 the data was intended for and was decoded by this class. Returning true in other cases may result in
johnb 6:3cb62daace78 170 other decoders being denied the opportunity to examine the data
johnb 6:3cb62daace78 171
johnb 34:2535760d53a6 172 \param p_data Pointer to the first byte of the data buffer (i.e. XBEE_CMD_POSN_SDELIM). The implementation
johnb 34:2535760d53a6 173 of any over-riding function should not expect that the data referenced by this pointer will
johnb 34:2535760d53a6 174 remain valid after the call-back has completed.
johnb 6:3cb62daace78 175 \param p_len Size of the data pointed to by p_data
johnb 6:3cb62daace78 176 \returns true in the case that the data was examined and decoded successfully
johnb 6:3cb62daace78 177 false in the case that the data was not of interest or was not decoded successfully
johnb 6:3cb62daace78 178 */
johnb 6:3cb62daace78 179 virtual bool decodeCallback( const uint8_t* const p_data, size_t p_len ) = 0;
johnb 5:b40a6fd3a334 180 };
johnb 5:b40a6fd3a334 181
johnb 27:6356ef5fe39b 182 /** Value which represents the broadcast address */
johnb 27:6356ef5fe39b 183 #define XBEE_BROADCAST_ADDR 0xFFFF
johnb 27:6356ef5fe39b 184
johnb 27:6356ef5fe39b 185
johnb 5:b40a6fd3a334 186 #endif