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 12:48:33 2014 +0000
Revision:
52:0950b05d5270
Parent:
51:a7d0d2ef9261
Child:
54:9f5b7652943e
Add initial support for decoding IO frames.

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 52:0950b05d5270 71 } else
johnb 52:0950b05d5270 72 {
johnb 52:0950b05d5270 73 m_snLow = (p_addr64Bit & 0xFFFFFFFF);
johnb 52:0950b05d5270 74 m_snHigh = ((p_addr64Bit >> 32U) & 0xFFFFFFFF);
johnb 52:0950b05d5270 75 m_sourceAddress = XBEE_USE_64BIT_ADDR;
johnb 52:0950b05d5270 76 m_have_snLow = m_have_snHigh = true;
johnb 52:0950b05d5270 77 }
johnb 50:f76b7e7959a2 78 }
johnb 50:f76b7e7959a2 79
johnb 50:f76b7e7959a2 80 XBeeDeviceRemoteAt::~XBeeDeviceRemoteAt( void )
johnb 50:f76b7e7959a2 81 {
johnb 50:f76b7e7959a2 82 }
johnb 50:f76b7e7959a2 83
johnb 51:a7d0d2ef9261 84 void XBeeDeviceRemoteAt::setApplyChanges( const bool p_apply )
johnb 51:a7d0d2ef9261 85 {
johnb 51:a7d0d2ef9261 86 m_applyChanges = p_apply;
johnb 51:a7d0d2ef9261 87 }
johnb 50:f76b7e7959a2 88
johnb 50:f76b7e7959a2 89 #define XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS (XBEE_CMD_POSN_ID_SPECIFIC_DATA+1)
johnb 50:f76b7e7959a2 90 #define XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS (XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + sizeof( uint64_t))
johnb 50:f76b7e7959a2 91 #define XBEE_REMOTE_AT_RESPONSE_STATUS (17U)
johnb 50:f76b7e7959a2 92
johnb 50:f76b7e7959a2 93 size_t XBeeDeviceRemoteAt::getResponseStatusPos( void ) const
johnb 50:f76b7e7959a2 94 {
johnb 50:f76b7e7959a2 95 return XBEE_REMOTE_AT_RESPONSE_STATUS;
johnb 50:f76b7e7959a2 96 }
johnb 50:f76b7e7959a2 97
johnb 50:f76b7e7959a2 98 bool XBeeDeviceRemoteAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
johnb 50:f76b7e7959a2 99 {
johnb 50:f76b7e7959a2 100 bool ret_val = false;
johnb 50:f76b7e7959a2 101
johnb 50:f76b7e7959a2 102 /* TODO: Length check */
johnb 50:f76b7e7959a2 103
johnb 50:f76b7e7959a2 104 if( XBEE_CMD_REMOTE_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] )
johnb 50:f76b7e7959a2 105 {
johnb 52:0950b05d5270 106 uint32_t srcAddrHigh = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS ]) << 24U) |
johnb 52:0950b05d5270 107 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 1 ]) << 16U) |
johnb 52:0950b05d5270 108 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 2 ]) << 8U) |
johnb 52:0950b05d5270 109 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 3 ]));
johnb 52:0950b05d5270 110 uint32_t srcAddrLow = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 4 ]) << 24U) |
johnb 50:f76b7e7959a2 111 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 5 ]) << 16U) |
johnb 50:f76b7e7959a2 112 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 6 ]) << 8U) |
johnb 50:f76b7e7959a2 113 ((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 7 ]);
johnb 52:0950b05d5270 114
johnb 50:f76b7e7959a2 115 uint16_t src16BitAddr = (((uint16_t)p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS ]) << 8U) |
johnb 50:f76b7e7959a2 116 p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS + 1 ];
johnb 52:0950b05d5270 117
johnb 52:0950b05d5270 118 if((( m_have_sourceAddress ) && ( m_sourceAddress == src16BitAddr )) ||
johnb 52:0950b05d5270 119 ( m_have_snHigh && m_have_snLow && ( srcAddrHigh == m_snHigh ) && ( srcAddrLow == m_snLow )))
johnb 50:f76b7e7959a2 120 {
johnb 50:f76b7e7959a2 121 ret_val = processResponseFrame( p_data, p_len );
johnb 50:f76b7e7959a2 122 }
johnb 50:f76b7e7959a2 123 }
johnb 52:0950b05d5270 124 else
johnb 51:a7d0d2ef9261 125 {
johnb 52:0950b05d5270 126 XBeeApiCmdAt::decodeCallback( p_data, p_len );
johnb 51:a7d0d2ef9261 127 }
johnb 51:a7d0d2ef9261 128
johnb 50:f76b7e7959a2 129 return ret_val;
johnb 50:f76b7e7959a2 130 }
johnb 50:f76b7e7959a2 131
johnb 50:f76b7e7959a2 132 void XBeeDeviceRemoteAt::SendCmd_uint8_t( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 133 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 134 const uint8_t& p_val )
johnb 50:f76b7e7959a2 135 {
johnb 52:0950b05d5270 136 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 137 /* TODO: Add option to force usage of 16 or 64-bit addressing */
johnb 52:0950b05d5270 138 XBeeApiCmdAtRemoteSet<uint8_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
johnb 50:f76b7e7959a2 139 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 140 }
johnb 50:f76b7e7959a2 141
johnb 50:f76b7e7959a2 142 void XBeeDeviceRemoteAt::SendCmd_uint16_t( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 143 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 144 const uint16_t& p_val )
johnb 50:f76b7e7959a2 145 {
johnb 52:0950b05d5270 146 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 147 XBeeApiCmdAtRemoteSet<uint16_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
johnb 51:a7d0d2ef9261 148 m_device->SendFrame( &req );
johnb 51:a7d0d2ef9261 149 }
johnb 51:a7d0d2ef9261 150
johnb 51:a7d0d2ef9261 151 void XBeeDeviceRemoteAt::SendCmd_uint32_t( const uint8_t p_frameId,
johnb 51:a7d0d2ef9261 152 const uint8_t* const p_data,
johnb 51:a7d0d2ef9261 153 const uint32_t& p_val )
johnb 51:a7d0d2ef9261 154 {
johnb 52:0950b05d5270 155 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 156 XBeeApiCmdAtRemoteSet<uint32_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
johnb 50:f76b7e7959a2 157 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 158 }
johnb 50:f76b7e7959a2 159
johnb 50:f76b7e7959a2 160 void XBeeDeviceRemoteAt::SendReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 161 const uint8_t* p_data )
johnb 50:f76b7e7959a2 162 {
johnb 52:0950b05d5270 163 uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
johnb 52:0950b05d5270 164 XBeeApiCmdAtRemoteReq req( p_frameId, m_sourceAddress, addr64Bit, p_data );
johnb 52:0950b05d5270 165 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 166 }
johnb 50:f76b7e7959a2 167
johnb 50:f76b7e7959a2 168 template < typename T >
johnb 50:f76b7e7959a2 169 XBeeApiCmdAtRemoteSet<T>::XBeeApiCmdAtRemoteSet( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 170 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 171 const uint64_t p_addr64Bit,
johnb 51:a7d0d2ef9261 172 const bool p_applyChanges,
johnb 50:f76b7e7959a2 173 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 174 const T p_val ) : XBeeApiFrame( )
johnb 50:f76b7e7959a2 175 {
johnb 50:f76b7e7959a2 176 size_t s;
johnb 50:f76b7e7959a2 177 uint8_t* dest;
johnb 50:f76b7e7959a2 178 const uint8_t* src = (uint8_t*)(&p_val);
johnb 50:f76b7e7959a2 179
johnb 50:f76b7e7959a2 180 m_apiId = XBEE_CMD_REMOTE_AT_CMD;
johnb 50:f76b7e7959a2 181
johnb 50:f76b7e7959a2 182 m_buffer[0] = p_frameId;
johnb 50:f76b7e7959a2 183
johnb 50:f76b7e7959a2 184 m_buffer[1] = p_addr64Bit >> 56U;
johnb 50:f76b7e7959a2 185 m_buffer[2] = p_addr64Bit >> 48U;
johnb 50:f76b7e7959a2 186 m_buffer[3] = p_addr64Bit >> 40U;
johnb 50:f76b7e7959a2 187 m_buffer[4] = p_addr64Bit >> 32U;
johnb 50:f76b7e7959a2 188 m_buffer[5] = p_addr64Bit >> 24U;
johnb 50:f76b7e7959a2 189 m_buffer[6] = p_addr64Bit >> 16U;
johnb 50:f76b7e7959a2 190 m_buffer[7] = p_addr64Bit >> 8U;
johnb 50:f76b7e7959a2 191 m_buffer[8] = p_addr64Bit;
johnb 50:f76b7e7959a2 192
johnb 50:f76b7e7959a2 193 m_buffer[9] = p_addr16Bit >> 8U;
johnb 50:f76b7e7959a2 194 m_buffer[10] = p_addr16Bit;
johnb 50:f76b7e7959a2 195
johnb 51:a7d0d2ef9261 196 m_buffer[11] = p_applyChanges << 1;
johnb 50:f76b7e7959a2 197
johnb 50:f76b7e7959a2 198 m_buffer[12] = p_data[0];
johnb 50:f76b7e7959a2 199 m_buffer[13] = p_data[1];
johnb 50:f76b7e7959a2 200
johnb 50:f76b7e7959a2 201 m_dataLen = sizeof( m_buffer );
johnb 50:f76b7e7959a2 202
johnb 50:f76b7e7959a2 203 /* TODO: This copy code isn't portable - it's assuming that the data in
johnb 50:f76b7e7959a2 204 * p_data is little endian */
johnb 50:f76b7e7959a2 205
johnb 50:f76b7e7959a2 206 dest = &( m_buffer[ m_dataLen - 1 ] );
johnb 50:f76b7e7959a2 207
johnb 50:f76b7e7959a2 208 for( s = 0;
johnb 50:f76b7e7959a2 209 s < sizeof( T );
johnb 50:f76b7e7959a2 210 s++, dest--, src++ ) {
johnb 50:f76b7e7959a2 211 *dest = *src;
johnb 50:f76b7e7959a2 212 }
johnb 50:f76b7e7959a2 213
johnb 50:f76b7e7959a2 214 m_data = m_buffer;
johnb 50:f76b7e7959a2 215 }
johnb 50:f76b7e7959a2 216
johnb 50:f76b7e7959a2 217 template < typename T >
johnb 50:f76b7e7959a2 218 XBeeApiCmdAtRemoteSet<T>::~XBeeApiCmdAtRemoteSet()
johnb 50:f76b7e7959a2 219 {
johnb 50:f76b7e7959a2 220 }
johnb 50:f76b7e7959a2 221
johnb 50:f76b7e7959a2 222 XBeeApiCmdAtRemoteReq::XBeeApiCmdAtRemoteReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 223 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 224 const uint64_t p_addr64Bit,
johnb 50:f76b7e7959a2 225 const uint8_t* const p_data )
johnb 50:f76b7e7959a2 226 {
johnb 50:f76b7e7959a2 227 m_apiId = XBEE_CMD_REMOTE_AT_CMD;
johnb 50:f76b7e7959a2 228
johnb 50:f76b7e7959a2 229 m_buffer[0] = p_frameId;
johnb 50:f76b7e7959a2 230
johnb 50:f76b7e7959a2 231 m_buffer[1] = p_addr64Bit >> 56U;
johnb 50:f76b7e7959a2 232 m_buffer[2] = p_addr64Bit >> 48U;
johnb 50:f76b7e7959a2 233 m_buffer[3] = p_addr64Bit >> 40U;
johnb 50:f76b7e7959a2 234 m_buffer[4] = p_addr64Bit >> 32U;
johnb 50:f76b7e7959a2 235 m_buffer[5] = p_addr64Bit >> 24U;
johnb 50:f76b7e7959a2 236 m_buffer[6] = p_addr64Bit >> 16U;
johnb 50:f76b7e7959a2 237 m_buffer[7] = p_addr64Bit >> 8U;
johnb 50:f76b7e7959a2 238 m_buffer[8] = p_addr64Bit;
johnb 50:f76b7e7959a2 239
johnb 50:f76b7e7959a2 240 m_buffer[9] = p_addr16Bit >> 8U;
johnb 50:f76b7e7959a2 241 m_buffer[10] = p_addr16Bit;
johnb 50:f76b7e7959a2 242
johnb 50:f76b7e7959a2 243 m_buffer[11] = 0;
johnb 50:f76b7e7959a2 244
johnb 50:f76b7e7959a2 245 m_buffer[12] = p_data[0];
johnb 50:f76b7e7959a2 246 m_buffer[13] = p_data[1];
johnb 50:f76b7e7959a2 247
johnb 50:f76b7e7959a2 248 m_dataLen = sizeof( m_buffer );
johnb 50:f76b7e7959a2 249 m_data = m_buffer;
johnb 50:f76b7e7959a2 250 }
johnb 50:f76b7e7959a2 251
johnb 50:f76b7e7959a2 252 XBeeApiCmdAtRemoteReq::~XBeeApiCmdAtRemoteReq()
johnb 50:f76b7e7959a2 253 {
johnb 50:f76b7e7959a2 254 }