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 10:24:16 2014 +0000
Revision:
51:a7d0d2ef9261
Parent:
50:f76b7e7959a2
Child:
52:0950b05d5270
Additional infrastructure for AT & remote AT commands.

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