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 28 14:13:20 2014 +0000
Revision:
54:9f5b7652943e
Parent:
52:0950b05d5270
Child:
55:610aa4a2ed3b
Add missing return assignment in XBeeDeviceRemoteAt::decodeCallback(); Ensure that serial number is initialised to a "safe" value when using 16-bit addressing with XBeeDeviceRemoteAt.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 50:f76b7e7959a2 1 /**
johnb 50:f76b7e7959a2 2
johnb 50:f76b7e7959a2 3 Copyright 2014 John Bailey
johnb 50:f76b7e7959a2 4
johnb 50:f76b7e7959a2 5 Licensed under the Apache License, Version 2.0 (the "License");
johnb 50:f76b7e7959a2 6 you may not use this file except in compliance with the License.
johnb 50:f76b7e7959a2 7 You may obtain a copy of the License at
johnb 50:f76b7e7959a2 8
johnb 50:f76b7e7959a2 9 http://www.apache.org/licenses/LICENSE-2.0
johnb 50:f76b7e7959a2 10
johnb 50:f76b7e7959a2 11 Unless required by applicable law or agreed to in writing, software
johnb 50:f76b7e7959a2 12 distributed under the License is distributed on an "AS IS" BASIS,
johnb 50:f76b7e7959a2 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 50:f76b7e7959a2 14 See the License for the specific language governing permissions and
johnb 50:f76b7e7959a2 15 limitations under the License.
johnb 50:f76b7e7959a2 16
johnb 50:f76b7e7959a2 17 */
johnb 50:f76b7e7959a2 18
johnb 50:f76b7e7959a2 19 #include "XBeeDeviceRemoteAt.hpp"
johnb 50:f76b7e7959a2 20
johnb 50:f76b7e7959a2 21 #define XBEE_API_CMD_REMOTE_REQ_HEADER_LEN 14U
johnb 50:f76b7e7959a2 22
johnb 50:f76b7e7959a2 23 template< typename T >
johnb 50:f76b7e7959a2 24 class XBeeApiCmdAtRemoteSet : public XBeeApiFrame {
johnb 50:f76b7e7959a2 25 uint8_t m_buffer[ XBEE_API_CMD_REMOTE_REQ_HEADER_LEN + sizeof( T ) ];
johnb 50:f76b7e7959a2 26 public:
johnb 50:f76b7e7959a2 27 /** Constructor
johnb 50:f76b7e7959a2 28
johnb 50:f76b7e7959a2 29 \param p_data Pointer to a buffer of length 2 bytes identifying
johnb 50:f76b7e7959a2 30 the command, e.g. 'V', 'R' would set up a version
johnb 50:f76b7e7959a2 31 request
johnb 50:f76b7e7959a2 32 \param p_val New value for the parameter
johnb 50:f76b7e7959a2 33 */
johnb 50:f76b7e7959a2 34 XBeeApiCmdAtRemoteSet( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 35 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 36 const uint64_t p_addr64Bit,
johnb 51:a7d0d2ef9261 37 const bool p_applyChanges,
johnb 50:f76b7e7959a2 38 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 39 const T p_val );
johnb 50:f76b7e7959a2 40 /** Destructor */
johnb 50:f76b7e7959a2 41 virtual ~XBeeApiCmdAtRemoteSet();
johnb 50:f76b7e7959a2 42 };
johnb 50:f76b7e7959a2 43
johnb 50:f76b7e7959a2 44 class XBeeApiCmdAtRemoteReq : public XBeeApiFrame {
johnb 50:f76b7e7959a2 45 uint8_t m_buffer[ XBEE_API_CMD_REMOTE_REQ_HEADER_LEN ];
johnb 50:f76b7e7959a2 46 public:
johnb 50:f76b7e7959a2 47 /** Constructor
johnb 50:f76b7e7959a2 48
johnb 50:f76b7e7959a2 49 \param p_data Pointer to a buffer of length 2 bytes identifying
johnb 50:f76b7e7959a2 50 the command, e.g. 'V', 'R' would set up a version
johnb 50:f76b7e7959a2 51 request
johnb 50:f76b7e7959a2 52 \param p_val New value for the parameter
johnb 50:f76b7e7959a2 53 */
johnb 50:f76b7e7959a2 54 XBeeApiCmdAtRemoteReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 55 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 56 const uint64_t p_addr64Bit,
johnb 50:f76b7e7959a2 57 const uint8_t* const p_data );
johnb 50:f76b7e7959a2 58 /** Destructor */
johnb 50:f76b7e7959a2 59 virtual ~XBeeApiCmdAtRemoteReq();
johnb 50:f76b7e7959a2 60 };
johnb 50:f76b7e7959a2 61
johnb 50:f76b7e7959a2 62 XBeeDeviceRemoteAt::XBeeDeviceRemoteAt( XBeeDevice* p_device,
johnb 50:f76b7e7959a2 63 const uint16_t& p_addr16Bit,
johnb 51:a7d0d2ef9261 64 const uint64_t& p_addr64Bit,
johnb 52:0950b05d5270 65 const bool p_applyChanges ) : XBeeApiCmdAt( p_device ), m_applyChanges( p_applyChanges )
johnb 50:f76b7e7959a2 66 {
johnb 52:0950b05d5270 67 if( p_addr16Bit != XBEE_USE_64BIT_ADDR )
johnb 52:0950b05d5270 68 {
johnb 52:0950b05d5270 69 m_sourceAddress = p_addr16Bit;
johnb 52:0950b05d5270 70 m_have_sourceAddress = true;
johnb 54:9f5b7652943e 71 m_snLow = m_snHigh = 0;
johnb 52:0950b05d5270 72 } else
johnb 52:0950b05d5270 73 {
johnb 52:0950b05d5270 74 m_snLow = (p_addr64Bit & 0xFFFFFFFF);
johnb 52:0950b05d5270 75 m_snHigh = ((p_addr64Bit >> 32U) & 0xFFFFFFFF);
johnb 52:0950b05d5270 76 m_sourceAddress = XBEE_USE_64BIT_ADDR;
johnb 52:0950b05d5270 77 m_have_snLow = m_have_snHigh = true;
johnb 52:0950b05d5270 78 }
johnb 50:f76b7e7959a2 79 }
johnb 50:f76b7e7959a2 80
johnb 50:f76b7e7959a2 81 XBeeDeviceRemoteAt::~XBeeDeviceRemoteAt( void )
johnb 50:f76b7e7959a2 82 {
johnb 50:f76b7e7959a2 83 }
johnb 50:f76b7e7959a2 84
johnb 51:a7d0d2ef9261 85 void XBeeDeviceRemoteAt::setApplyChanges( const bool p_apply )
johnb 51:a7d0d2ef9261 86 {
johnb 51:a7d0d2ef9261 87 m_applyChanges = p_apply;
johnb 51:a7d0d2ef9261 88 }
johnb 50:f76b7e7959a2 89
johnb 50:f76b7e7959a2 90 #define XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS (XBEE_CMD_POSN_ID_SPECIFIC_DATA+1)
johnb 50:f76b7e7959a2 91 #define XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS (XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + sizeof( uint64_t))
johnb 50:f76b7e7959a2 92 #define XBEE_REMOTE_AT_RESPONSE_STATUS (17U)
johnb 50:f76b7e7959a2 93
johnb 50:f76b7e7959a2 94 size_t XBeeDeviceRemoteAt::getResponseStatusPos( void ) const
johnb 50:f76b7e7959a2 95 {
johnb 50:f76b7e7959a2 96 return XBEE_REMOTE_AT_RESPONSE_STATUS;
johnb 50:f76b7e7959a2 97 }
johnb 50:f76b7e7959a2 98
johnb 50:f76b7e7959a2 99 bool XBeeDeviceRemoteAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
johnb 50:f76b7e7959a2 100 {
johnb 50:f76b7e7959a2 101 bool ret_val = false;
johnb 50:f76b7e7959a2 102
johnb 50:f76b7e7959a2 103 /* TODO: Length check */
johnb 50:f76b7e7959a2 104
johnb 50:f76b7e7959a2 105 if( XBEE_CMD_REMOTE_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] )
johnb 50:f76b7e7959a2 106 {
johnb 52:0950b05d5270 107 uint32_t srcAddrHigh = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS ]) << 24U) |
johnb 52:0950b05d5270 108 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 1 ]) << 16U) |
johnb 52:0950b05d5270 109 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 2 ]) << 8U) |
johnb 52:0950b05d5270 110 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 3 ]));
johnb 52:0950b05d5270 111 uint32_t srcAddrLow = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 4 ]) << 24U) |
johnb 50:f76b7e7959a2 112 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 5 ]) << 16U) |
johnb 50:f76b7e7959a2 113 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 6 ]) << 8U) |
johnb 50:f76b7e7959a2 114 ((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 7 ]);
johnb 52:0950b05d5270 115
johnb 50:f76b7e7959a2 116 uint16_t src16BitAddr = (((uint16_t)p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS ]) << 8U) |
johnb 50:f76b7e7959a2 117 p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS + 1 ];
johnb 52:0950b05d5270 118
johnb 52:0950b05d5270 119 if((( m_have_sourceAddress ) && ( m_sourceAddress == src16BitAddr )) ||
johnb 52:0950b05d5270 120 ( m_have_snHigh && m_have_snLow && ( srcAddrHigh == m_snHigh ) && ( srcAddrLow == m_snLow )))
johnb 50:f76b7e7959a2 121 {
johnb 50:f76b7e7959a2 122 ret_val = processResponseFrame( p_data, p_len );
johnb 50:f76b7e7959a2 123 }
johnb 50:f76b7e7959a2 124 }
johnb 52:0950b05d5270 125 else
johnb 51:a7d0d2ef9261 126 {
johnb 54:9f5b7652943e 127 ret_val = XBeeApiCmdAt::decodeCallback( p_data, p_len );
johnb 51:a7d0d2ef9261 128 }
johnb 51:a7d0d2ef9261 129
johnb 50:f76b7e7959a2 130 return ret_val;
johnb 50:f76b7e7959a2 131 }
johnb 50:f76b7e7959a2 132
johnb 50:f76b7e7959a2 133 void XBeeDeviceRemoteAt::SendCmd_uint8_t( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 134 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 135 const uint8_t& p_val )
johnb 50:f76b7e7959a2 136 {
johnb 52:0950b05d5270 137 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 138 /* TODO: Add option to force usage of 16 or 64-bit addressing */
johnb 52:0950b05d5270 139 XBeeApiCmdAtRemoteSet<uint8_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
johnb 50:f76b7e7959a2 140 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 141 }
johnb 50:f76b7e7959a2 142
johnb 50:f76b7e7959a2 143 void XBeeDeviceRemoteAt::SendCmd_uint16_t( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 144 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 145 const uint16_t& p_val )
johnb 50:f76b7e7959a2 146 {
johnb 52:0950b05d5270 147 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 148 XBeeApiCmdAtRemoteSet<uint16_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
johnb 51:a7d0d2ef9261 149 m_device->SendFrame( &req );
johnb 51:a7d0d2ef9261 150 }
johnb 51:a7d0d2ef9261 151
johnb 51:a7d0d2ef9261 152 void XBeeDeviceRemoteAt::SendCmd_uint32_t( const uint8_t p_frameId,
johnb 51:a7d0d2ef9261 153 const uint8_t* const p_data,
johnb 51:a7d0d2ef9261 154 const uint32_t& p_val )
johnb 51:a7d0d2ef9261 155 {
johnb 52:0950b05d5270 156 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 157 XBeeApiCmdAtRemoteSet<uint32_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
johnb 50:f76b7e7959a2 158 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 159 }
johnb 50:f76b7e7959a2 160
johnb 50:f76b7e7959a2 161 void XBeeDeviceRemoteAt::SendReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 162 const uint8_t* p_data )
johnb 50:f76b7e7959a2 163 {
johnb 52:0950b05d5270 164 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 165 XBeeApiCmdAtRemoteReq req( p_frameId, m_sourceAddress, addr64Bit, p_data );
johnb 52:0950b05d5270 166 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 167 }
johnb 50:f76b7e7959a2 168
johnb 50:f76b7e7959a2 169 template < typename T >
johnb 50:f76b7e7959a2 170 XBeeApiCmdAtRemoteSet<T>::XBeeApiCmdAtRemoteSet( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 171 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 172 const uint64_t p_addr64Bit,
johnb 51:a7d0d2ef9261 173 const bool p_applyChanges,
johnb 50:f76b7e7959a2 174 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 175 const T p_val ) : XBeeApiFrame( )
johnb 50:f76b7e7959a2 176 {
johnb 50:f76b7e7959a2 177 size_t s;
johnb 50:f76b7e7959a2 178 uint8_t* dest;
johnb 50:f76b7e7959a2 179 const uint8_t* src = (uint8_t*)(&p_val);
johnb 50:f76b7e7959a2 180
johnb 50:f76b7e7959a2 181 m_apiId = XBEE_CMD_REMOTE_AT_CMD;
johnb 50:f76b7e7959a2 182
johnb 50:f76b7e7959a2 183 m_buffer[0] = p_frameId;
johnb 50:f76b7e7959a2 184
johnb 50:f76b7e7959a2 185 m_buffer[1] = p_addr64Bit >> 56U;
johnb 50:f76b7e7959a2 186 m_buffer[2] = p_addr64Bit >> 48U;
johnb 50:f76b7e7959a2 187 m_buffer[3] = p_addr64Bit >> 40U;
johnb 50:f76b7e7959a2 188 m_buffer[4] = p_addr64Bit >> 32U;
johnb 50:f76b7e7959a2 189 m_buffer[5] = p_addr64Bit >> 24U;
johnb 50:f76b7e7959a2 190 m_buffer[6] = p_addr64Bit >> 16U;
johnb 50:f76b7e7959a2 191 m_buffer[7] = p_addr64Bit >> 8U;
johnb 50:f76b7e7959a2 192 m_buffer[8] = p_addr64Bit;
johnb 50:f76b7e7959a2 193
johnb 50:f76b7e7959a2 194 m_buffer[9] = p_addr16Bit >> 8U;
johnb 50:f76b7e7959a2 195 m_buffer[10] = p_addr16Bit;
johnb 50:f76b7e7959a2 196
johnb 51:a7d0d2ef9261 197 m_buffer[11] = p_applyChanges << 1;
johnb 50:f76b7e7959a2 198
johnb 50:f76b7e7959a2 199 m_buffer[12] = p_data[0];
johnb 50:f76b7e7959a2 200 m_buffer[13] = p_data[1];
johnb 50:f76b7e7959a2 201
johnb 50:f76b7e7959a2 202 m_dataLen = sizeof( m_buffer );
johnb 50:f76b7e7959a2 203
johnb 50:f76b7e7959a2 204 /* TODO: This copy code isn't portable - it's assuming that the data in
johnb 50:f76b7e7959a2 205 * p_data is little endian */
johnb 50:f76b7e7959a2 206
johnb 50:f76b7e7959a2 207 dest = &( m_buffer[ m_dataLen - 1 ] );
johnb 50:f76b7e7959a2 208
johnb 50:f76b7e7959a2 209 for( s = 0;
johnb 50:f76b7e7959a2 210 s < sizeof( T );
johnb 50:f76b7e7959a2 211 s++, dest--, src++ ) {
johnb 50:f76b7e7959a2 212 *dest = *src;
johnb 50:f76b7e7959a2 213 }
johnb 50:f76b7e7959a2 214
johnb 50:f76b7e7959a2 215 m_data = m_buffer;
johnb 50:f76b7e7959a2 216 }
johnb 50:f76b7e7959a2 217
johnb 50:f76b7e7959a2 218 template < typename T >
johnb 50:f76b7e7959a2 219 XBeeApiCmdAtRemoteSet<T>::~XBeeApiCmdAtRemoteSet()
johnb 50:f76b7e7959a2 220 {
johnb 50:f76b7e7959a2 221 }
johnb 50:f76b7e7959a2 222
johnb 50:f76b7e7959a2 223 XBeeApiCmdAtRemoteReq::XBeeApiCmdAtRemoteReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 224 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 225 const uint64_t p_addr64Bit,
johnb 50:f76b7e7959a2 226 const uint8_t* const p_data )
johnb 50:f76b7e7959a2 227 {
johnb 50:f76b7e7959a2 228 m_apiId = XBEE_CMD_REMOTE_AT_CMD;
johnb 50:f76b7e7959a2 229
johnb 50:f76b7e7959a2 230 m_buffer[0] = p_frameId;
johnb 50:f76b7e7959a2 231
johnb 50:f76b7e7959a2 232 m_buffer[1] = p_addr64Bit >> 56U;
johnb 50:f76b7e7959a2 233 m_buffer[2] = p_addr64Bit >> 48U;
johnb 50:f76b7e7959a2 234 m_buffer[3] = p_addr64Bit >> 40U;
johnb 50:f76b7e7959a2 235 m_buffer[4] = p_addr64Bit >> 32U;
johnb 50:f76b7e7959a2 236 m_buffer[5] = p_addr64Bit >> 24U;
johnb 50:f76b7e7959a2 237 m_buffer[6] = p_addr64Bit >> 16U;
johnb 50:f76b7e7959a2 238 m_buffer[7] = p_addr64Bit >> 8U;
johnb 50:f76b7e7959a2 239 m_buffer[8] = p_addr64Bit;
johnb 50:f76b7e7959a2 240
johnb 50:f76b7e7959a2 241 m_buffer[9] = p_addr16Bit >> 8U;
johnb 50:f76b7e7959a2 242 m_buffer[10] = p_addr16Bit;
johnb 50:f76b7e7959a2 243
johnb 50:f76b7e7959a2 244 m_buffer[11] = 0;
johnb 50:f76b7e7959a2 245
johnb 50:f76b7e7959a2 246 m_buffer[12] = p_data[0];
johnb 50:f76b7e7959a2 247 m_buffer[13] = p_data[1];
johnb 50:f76b7e7959a2 248
johnb 50:f76b7e7959a2 249 m_dataLen = sizeof( m_buffer );
johnb 50:f76b7e7959a2 250 m_data = m_buffer;
johnb 50:f76b7e7959a2 251 }
johnb 50:f76b7e7959a2 252
johnb 50:f76b7e7959a2 253 XBeeApiCmdAtRemoteReq::~XBeeApiCmdAtRemoteReq()
johnb 50:f76b7e7959a2 254 {
johnb 50:f76b7e7959a2 255 }