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:
Mon Jul 07 19:03:09 2014 +0000
Revision:
48:48397bedf95d
Parent:
42:81c789ba4c08
Child:
53:7b65422d7a32
Add checksum verification on the received frames and the ability to turn it on/off.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 3:c3d96b94041a 1 /**
johnb 3:c3d96b94041a 2 @file
johnb 3:c3d96b94041a 3 @brief Class to abstract the XBee serial interface
johnb 3:c3d96b94041a 4
johnb 3:c3d96b94041a 5 @author John Bailey
johnb 3:c3d96b94041a 6
johnb 3:c3d96b94041a 7 @copyright Copyright 2014 John Bailey
johnb 3:c3d96b94041a 8
johnb 3:c3d96b94041a 9 @section LICENSE
johnb 3:c3d96b94041a 10
johnb 3:c3d96b94041a 11 Licensed under the Apache License, Version 2.0 (the "License");
johnb 3:c3d96b94041a 12 you may not use this file except in compliance with the License.
johnb 3:c3d96b94041a 13 You may obtain a copy of the License at
johnb 3:c3d96b94041a 14
johnb 3:c3d96b94041a 15 http://www.apache.org/licenses/LICENSE-2.0
johnb 3:c3d96b94041a 16
johnb 3:c3d96b94041a 17 Unless required by applicable law or agreed to in writing, software
johnb 3:c3d96b94041a 18 distributed under the License is distributed on an "AS IS" BASIS,
johnb 3:c3d96b94041a 19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 3:c3d96b94041a 20 See the License for the specific language governing permissions and
johnb 3:c3d96b94041a 21 limitations under the License.
johnb 3:c3d96b94041a 22
johnb 3:c3d96b94041a 23 */
johnb 3:c3d96b94041a 24
johnb 3:c3d96b94041a 25 #if !defined XBEEDEVICE_HPP
johnb 3:c3d96b94041a 26 #define XBEEDEVICE_HPP
johnb 3:c3d96b94041a 27
johnb 3:c3d96b94041a 28 #include "XBeeApiCfg.hpp"
johnb 3:c3d96b94041a 29
johnb 3:c3d96b94041a 30 #include "mbed.h" // For serial interface
johnb 3:c3d96b94041a 31 #if defined XBEEAPI_CONFIG_USING_RTOS
johnb 3:c3d96b94041a 32 #include "rtos.h" // Mutex support
johnb 3:c3d96b94041a 33 #endif
johnb 3:c3d96b94041a 34
johnb 3:c3d96b94041a 35 #include "FixedLengthList.hpp"
johnb 3:c3d96b94041a 36 #include "CircularBuffer.h"
johnb 3:c3d96b94041a 37
johnb 3:c3d96b94041a 38 #include "XBeeApiFrame.hpp"
johnb 3:c3d96b94041a 39
johnb 3:c3d96b94041a 40 /** Class to represent an XBee device & provide an interface to communicate with it
johnb 3:c3d96b94041a 41
johnb 3:c3d96b94041a 42 Actual communication is performed by:
johnb 3:c3d96b94041a 43 tx - using SendFrame to transmit messages to the XBee
johnb 3:c3d96b94041a 44 rx - registering one or more decoders (via registerDecoder) to be called when
johnb 3:c3d96b94041a 45 a message is received */
johnb 3:c3d96b94041a 46 class XBeeDevice
johnb 3:c3d96b94041a 47 {
johnb 29:c6d037cceb02 48 public:
johnb 29:c6d037cceb02 49 /** Represent the different XBee models */
johnb 29:c6d037cceb02 50 typedef enum {
johnb 29:c6d037cceb02 51 /* XBee S1 (aka XBee 802.15.4) - see http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/point-multipoint-rfmodules/xbee-series1-module */
johnb 29:c6d037cceb02 52 XBEEDEVICE_S1,
johnb 29:c6d037cceb02 53 /* XBee S1 Pro (aka XBee 802.15.4 Pro) */
johnb 29:c6d037cceb02 54 XBEEDEVICE_S1_PRO
johnb 29:c6d037cceb02 55 } XBeeDeviceModel_t;
johnb 29:c6d037cceb02 56
johnb 3:c3d96b94041a 57 private:
johnb 3:c3d96b94041a 58
johnb 33:eccf4725930c 59 /** Common class initialisation, shared between constructors */
johnb 33:eccf4725930c 60 void init( void );
johnb 33:eccf4725930c 61
johnb 3:c3d96b94041a 62 #if defined XBEEAPI_CONFIG_USING_RTOS
johnb 42:81c789ba4c08 63
johnb 3:c3d96b94041a 64 /** Mutex for accessing the serial interface */
johnb 3:c3d96b94041a 65 rtos::Mutex m_ifMutex;
johnb 42:81c789ba4c08 66
johnb 42:81c789ba4c08 67 #if defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
johnb 42:81c789ba4c08 68
johnb 42:81c789ba4c08 69 friend void XBeeRxDispatch(void const *args);
johnb 42:81c789ba4c08 70
johnb 42:81c789ba4c08 71 #endif // defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
johnb 42:81c789ba4c08 72
johnb 42:81c789ba4c08 73 #endif // defined XBEEAPI_CONFIG_USING_RTOS
johnb 3:c3d96b94041a 74
johnb 29:c6d037cceb02 75 /** The model of XBee that this XBeeDevice is associated with */
johnb 29:c6d037cceb02 76 XBeeDeviceModel_t m_model;
johnb 29:c6d037cceb02 77
johnb 3:c3d96b94041a 78 /** Track whether the XBee is in CMD mode or API mode */
johnb 3:c3d96b94041a 79 bool m_inAtCmdMode;
johnb 3:c3d96b94041a 80
johnb 3:c3d96b94041a 81 /** Track whether or not the last byte received from the XBee was an escape (i.e. the
johnb 3:c3d96b94041a 82 next incoming byte needs to be un-escaped) */
johnb 3:c3d96b94041a 83 uint16_t m_rxMsgLastWasEsc;
johnb 3:c3d96b94041a 84
johnb 41:07cb97b44e81 85 /** Serial interface for the XBee comms
johnb 41:07cb97b44e81 86 Use RawSerial rather than Serial so that we're OK if the RTOS is used */
johnb 41:07cb97b44e81 87 RawSerial* m_if;
johnb 33:eccf4725930c 88
johnb 33:eccf4725930c 89 /** Flag to indicate if the Serial object m_if was created by this class and
johnb 33:eccf4725930c 90 hence needs deleting in the destructor */
johnb 33:eccf4725930c 91 bool m_serialNeedsDelete;
johnb 3:c3d96b94041a 92
johnb 3:c3d96b94041a 93 /** Call-back function from MBED triggered when data is
johnb 3:c3d96b94041a 94 received on the XBee's serial interface */
johnb 3:c3d96b94041a 95 void if_rx( void );
johnb 3:c3d96b94041a 96
johnb 3:c3d96b94041a 97 /** Helper function to determine whether or not there's a message to decode and to
johnb 3:c3d96b94041a 98 offer it round any registered decoders */
johnb 3:c3d96b94041a 99 void checkRxDecode( void );
johnb 3:c3d96b94041a 100
johnb 3:c3d96b94041a 101 /** Write a byte to the XBee serial interface, taking care of any
johnb 3:c3d96b94041a 102 escaping requirements (see m_escape)
johnb 3:c3d96b94041a 103
johnb 3:c3d96b94041a 104 @param p_byte Byte to be written
johnb 3:c3d96b94041a 105 @return Sum of the byte(s) written - to be addded to the checksum
johnb 3:c3d96b94041a 106 */
johnb 3:c3d96b94041a 107 uint8_t xbeeWrite( uint8_t p_byte, bool p_doEscape = true );
johnb 3:c3d96b94041a 108
johnb 3:c3d96b94041a 109 /** Flag to indicate whether or not the dataflow is currentl being escaped */
johnb 3:c3d96b94041a 110 bool m_escape;
johnb 3:c3d96b94041a 111
johnb 3:c3d96b94041a 112 /** Buffer of bytes received from the XBee so far */
johnb 3:c3d96b94041a 113 CircularBuffer<XBEEAPI_CONFIG_RX_BUFFER_SIZE> m_rxBuff;
johnb 3:c3d96b94041a 114
johnb 3:c3d96b94041a 115 /** List of objects which are registered to de-code received frames */
johnb 3:c3d96b94041a 116 FixedLengthList<XBeeApiFrameDecoder*, XBEEAPI_CONFIG_DECODER_LIST_SIZE> m_decoders;
johnb 3:c3d96b94041a 117
johnb 48:48397bedf95d 118 /** Option as to whether bad checksum should be ignored or not */
johnb 48:48397bedf95d 119 bool m_ignoreBadCsum;
johnb 48:48397bedf95d 120
johnb 48:48397bedf95d 121 /** Rolling counter of the number of messages which failed checksum verification when
johnb 48:48397bedf95d 122 this option is enabled. See getBadChecksumCount() */
johnb 48:48397bedf95d 123 uint16_t m_badCsumCount;
johnb 48:48397bedf95d 124
johnb 48:48397bedf95d 125 /** Rolling counter of the number of received serial data bytes which have been discarded
johnb 48:48397bedf95d 126 See getSerialBytesDiscardedCount() */
johnb 48:48397bedf95d 127 uint16_t m_serialBytesDiscarded;
johnb 48:48397bedf95d 128
johnb 3:c3d96b94041a 129 public:
johnb 3:c3d96b94041a 130
johnb 3:c3d96b94041a 131 /** Represent the status of an XBee message exchange */
johnb 3:c3d96b94041a 132 typedef enum {
johnb 3:c3d96b94041a 133 /** Successful communication */
johnb 3:c3d96b94041a 134 XBEEDEVICE_OK = 0,
johnb 3:c3d96b94041a 135 /** No response received from the XBee within the expected time */
johnb 3:c3d96b94041a 136 XBEEDEVICE_TIMEOUT = 1,
johnb 3:c3d96b94041a 137 /** Data received from the XBee, but was of the wrong length */
johnb 3:c3d96b94041a 138 XBEEDEVICE_UNEXPECTED_LENGTH = 2,
johnb 3:c3d96b94041a 139 /** Data received from the XBee was in an unexpected format */
johnb 3:c3d96b94041a 140 XBEEDEVICE_UNEXPECTED_DATA = 3,
johnb 3:c3d96b94041a 141 /** XBee is currently in the wrong mode to support this request (e.g. requesting AT ASCII
johnb 3:c3d96b94041a 142 communications when in API mode) */
johnb 3:c3d96b94041a 143 XBEEDEVICE_WRONG_MODE = 4,
johnb 3:c3d96b94041a 144 } XBeeDeviceReturn_t;
johnb 3:c3d96b94041a 145
johnb 12:58319a467943 146 /** Represents the different types of addressing for XBee devices */
johnb 12:58319a467943 147 typedef enum {
johnb 12:58319a467943 148 XBEE_API_ADDR_TYPE_16BIT = 0,
johnb 12:58319a467943 149 XBEE_API_ADDR_TYPE_64BIT = 1
johnb 12:58319a467943 150 } XBeeApiAddrType_t;
johnb 12:58319a467943 151
johnb 3:c3d96b94041a 152 /** Constructor. Parameters are used to specify the particulars of the connection to the XBee
johnb 3:c3d96b94041a 153
johnb 29:c6d037cceb02 154 Objects using this constructor will default to be associated with an XBee S1 (see XBeeDeviceModel_t).
johnb 29:c6d037cceb02 155 This should be altered via setXBeeModel() if required
johnb 29:c6d037cceb02 156
johnb 3:c3d96b94041a 157 @param p_tx Serial interface TX pin
johnb 3:c3d96b94041a 158 @param p_rx Serial interface RX pin
johnb 3:c3d96b94041a 159 @param p_rts Pin to use for RTS (flow control). Will only be used if supported. Can specify NC to disable.
johnb 3:c3d96b94041a 160 @param p_rts Pin to use for CTS (flow control). Will only be used if supported. Can specify NC to disable.
johnb 3:c3d96b94041a 161 */
johnb 3:c3d96b94041a 162 XBeeDevice( PinName p_tx, PinName p_rx, PinName p_rts, PinName p_cts );
johnb 3:c3d96b94041a 163
johnb 33:eccf4725930c 164 /** Constructor. Parameters are used to specify the particulars of the connection to the XBee
johnb 33:eccf4725930c 165
johnb 33:eccf4725930c 166 Objects using this constructor will default to be associated with an XBee S1 (see XBeeDeviceModel_t).
johnb 33:eccf4725930c 167 This should be altered via setXBeeModel() if required
johnb 33:eccf4725930c 168
johnb 33:eccf4725930c 169 @param p_serialIf Pointer to the serial interface to be used to communicate with the XBee.
johnb 33:eccf4725930c 170 The referenced object must remain valid for as long as the XBeeDevice object is
johnb 33:eccf4725930c 171 being used. Must not be NULL.
johnb 33:eccf4725930c 172 */
johnb 41:07cb97b44e81 173 XBeeDevice( RawSerial* p_serialIf );
johnb 33:eccf4725930c 174
johnb 29:c6d037cceb02 175 /** Destructor */
johnb 3:c3d96b94041a 176 virtual ~XBeeDevice( void );
johnb 3:c3d96b94041a 177
johnb 29:c6d037cceb02 178 /** Determine what type of XBee model this object is associated with */
johnb 29:c6d037cceb02 179 XBeeDeviceModel_t getXBeeModel() const;
johnb 29:c6d037cceb02 180
johnb 29:c6d037cceb02 181 /** Set the type of XBee model this object is associated with */
johnb 29:c6d037cceb02 182 void setXBeeModel( const XBeeDeviceModel_t p_model );
johnb 29:c6d037cceb02 183
johnb 5:b40a6fd3a334 184 /** Transmit the specified frame to the XBee. This method does not block waiting for a response, but returns and
johnb 5:b40a6fd3a334 185 expects that any response will be dealt with by an appropriately registered decoder
johnb 5:b40a6fd3a334 186
johnb 5:b40a6fd3a334 187 The XBee represents frames as:
johnb 5:b40a6fd3a334 188
johnb 5:b40a6fd3a334 189 --------------------------------------------------------------------------
johnb 5:b40a6fd3a334 190 | Start Delimiter | Length | API identifier | ID specific data | Checksum |
johnb 5:b40a6fd3a334 191 | 1 byte | 2 bytes | 1 byte | x bytes | 1 byte |
johnb 5:b40a6fd3a334 192 | 0x7e | MSB LSB | APIID | ... ... ... | CKSUM |
johnb 5:b40a6fd3a334 193 --------------------------------------------------------------------------
johnb 5:b40a6fd3a334 194
johnb 5:b40a6fd3a334 195 The method uses the XBeeApiFrame class methods to fill in the length, API ID & data.
johnb 3:c3d96b94041a 196
johnb 3:c3d96b94041a 197 \param p_cmd Frame to be transmitted
johnb 3:c3d96b94041a 198 */
johnb 16:8095c43a2a6e 199 void SendFrame( XBeeApiFrame* const p_cmd );
johnb 3:c3d96b94041a 200
johnb 3:c3d96b94041a 201 /** Set the XBee up in API mode. Note that this method needs to know something about the way in which the
johnb 3:c3d96b94041a 202 attached XBee is configured (namely the guard time). This is configured via XBeeApiCmd.hpp, currently */
johnb 3:c3d96b94041a 203 XBeeDeviceReturn_t setUpApi( void );
johnb 3:c3d96b94041a 204
johnb 3:c3d96b94041a 205 /** Register an object as being interested in decoding messages from the XBee. Note that each
johnb 3:c3d96b94041a 206 decoder MUST only be registered with ONE XBeeDevice.
johnb 3:c3d96b94041a 207
johnb 3:c3d96b94041a 208 \param p_decoder Decoder to be registered
johnb 3:c3d96b94041a 209 \returns true in the case that registration was successful, false otherwise (decoder list full, decoder already registered, etc) */
johnb 3:c3d96b94041a 210 bool registerDecoder( XBeeApiFrameDecoder* const p_decoder );
johnb 3:c3d96b94041a 211
johnb 3:c3d96b94041a 212 /** Remove a previous registration for decoding of messages. The decoder will be removed from
johnb 3:c3d96b94041a 213 the list and no-longer called when XBee data is received
johnb 3:c3d96b94041a 214
johnb 3:c3d96b94041a 215 \param p_decoder Decoder to be unregistered
johnb 3:c3d96b94041a 216 \returns true in the case that unregistration was successful, false otherwise (decoder not in list) */
johnb 3:c3d96b94041a 217 bool unregisterDecoder( XBeeApiFrameDecoder* const p_decoder );
johnb 3:c3d96b94041a 218
johnb 48:48397bedf95d 219 /** Set whether or not the device should ignore checksums on messages that are incorrect.
johnb 48:48397bedf95d 220 By default the class will verify the checksums and discard frames which do not
johnb 48:48397bedf95d 221 pass this test, incrementing a rolling counter which can be retrieved via
johnb 48:48397bedf95d 222 getBadChecksumCount(). Setting the class to ignore bad checksums will result in all
johnb 48:48397bedf95d 223 frames being processed.
johnb 48:48397bedf95d 224
johnb 48:48397bedf95d 225 \param p_shouldIgnore If set to true, frames with incorrect checksums will still be processed
johnb 48:48397bedf95d 226 If set to false, frames with incorrect checksums will not be processed
johnb 48:48397bedf95d 227 */
johnb 48:48397bedf95d 228 void setIgnoreBadChecksum( const bool p_shouldIgnore );
johnb 48:48397bedf95d 229
johnb 48:48397bedf95d 230 uint16_t getBadChecksumCount( void ) const;
johnb 48:48397bedf95d 231
johnb 48:48397bedf95d 232 /** Retrieve the number of bytes received from the serial data stream which have been discarded
johnb 48:48397bedf95d 233 Due to the fact that each API frame starts with a specific value, each time an API frame is
johnb 48:48397bedf95d 234 to be read, the first byte should be the frame header. If it is not then the class will
johnb 48:48397bedf95d 235 keep reading from the stream until the frame header is encountered in order to attempt to
johnb 48:48397bedf95d 236 re-synchronise with the stream. Reasons for becoming out-of-sync could include not
johnb 48:48397bedf95d 237 managing to process the data stream fast enough
johnb 48:48397bedf95d 238
johnb 48:48397bedf95d 239 \returns A rolling counter of the number of bytes discarded so far */
johnb 48:48397bedf95d 240 uint16_t getSerialBytesDiscardedCount( void ) const;
johnb 48:48397bedf95d 241
johnb 3:c3d96b94041a 242 #if defined XBEEAPI_CONFIG_ENABLE_DEVELOPER
johnb 3:c3d96b94041a 243 void dumpRxBuffer( Stream* p_buf, const bool p_hexView );
johnb 3:c3d96b94041a 244 #endif
johnb 3:c3d96b94041a 245
johnb 42:81c789ba4c08 246 #if defined XBEEAPI_CONFIG_USING_RTOS && defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
johnb 42:81c789ba4c08 247 bool setupDispatchTask();
johnb 42:81c789ba4c08 248 #endif
johnb 42:81c789ba4c08 249
johnb 3:c3d96b94041a 250 protected:
johnb 3:c3d96b94041a 251 /** Send an ASCII frame to the XBee. This method blocks until a response is received
johnb 3:c3d96b94041a 252 or a timeout occurs.
johnb 3:c3d96b94041a 253
johnb 3:c3d96b94041a 254 Note that this is a protected method as it is here to support setUpApi() with regard
johnb 3:c3d96b94041a 255 to getting the XBee into API mode. It's not intended to be used for general
johnb 3:c3d96b94041a 256 communication with the XBee.
johnb 3:c3d96b94041a 257
johnb 3:c3d96b94041a 258 \param p_dat ASCII data to be sent
johnb 3:c3d96b94041a 259 \param p_len Length of the data pointed to by p_dat
johnb 3:c3d96b94041a 260 \param p_wait_ms Time to wait for a response
johnb 3:c3d96b94041a 261 */
johnb 3:c3d96b94041a 262 XBeeDeviceReturn_t SendFrame( const char* const p_dat, size_t p_len, int p_wait_ms = 1000 );
johnb 3:c3d96b94041a 263
johnb 48:48397bedf95d 264 /** Verify the checksum of the frame pointed to.
johnb 48:48397bedf95d 265 The referenced frame should be in unescaped form
johnb 48:48397bedf95d 266
johnb 48:48397bedf95d 267 \param p_data Pointer to a buffer where the first byte is the frame start delimiter
johnb 48:48397bedf95d 268 \param p_len Length of the p_data buffer in bytes
johnb 48:48397bedf95d 269 \returns true in the case that the checksum passed, false in the case that it does not.
johnb 48:48397bedf95d 270 */
johnb 48:48397bedf95d 271 bool verifyFrameChecksum( const uint8_t* const p_data, size_t p_len );
johnb 3:c3d96b94041a 272
johnb 3:c3d96b94041a 273 };
johnb 3:c3d96b94041a 274
johnb 3:c3d96b94041a 275 #endif