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 13 15:47:40 2014 +0000
Revision:
50:f76b7e7959a2
Child:
51:a7d0d2ef9261
Add support for 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 50:f76b7e7959a2 37 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 38 const T p_val );
johnb 50:f76b7e7959a2 39 /** Destructor */
johnb 50:f76b7e7959a2 40 virtual ~XBeeApiCmdAtRemoteSet();
johnb 50:f76b7e7959a2 41 };
johnb 50:f76b7e7959a2 42
johnb 50:f76b7e7959a2 43 class XBeeApiCmdAtRemoteReq : public XBeeApiFrame {
johnb 50:f76b7e7959a2 44 uint8_t m_buffer[ XBEE_API_CMD_REMOTE_REQ_HEADER_LEN ];
johnb 50:f76b7e7959a2 45 public:
johnb 50:f76b7e7959a2 46 /** Constructor
johnb 50:f76b7e7959a2 47
johnb 50:f76b7e7959a2 48 \param p_data Pointer to a buffer of length 2 bytes identifying
johnb 50:f76b7e7959a2 49 the command, e.g. 'V', 'R' would set up a version
johnb 50:f76b7e7959a2 50 request
johnb 50:f76b7e7959a2 51 \param p_val New value for the parameter
johnb 50:f76b7e7959a2 52 */
johnb 50:f76b7e7959a2 53 XBeeApiCmdAtRemoteReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 54 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 55 const uint64_t p_addr64Bit,
johnb 50:f76b7e7959a2 56 const uint8_t* const p_data );
johnb 50:f76b7e7959a2 57 /** Destructor */
johnb 50:f76b7e7959a2 58 virtual ~XBeeApiCmdAtRemoteReq();
johnb 50:f76b7e7959a2 59 };
johnb 50:f76b7e7959a2 60
johnb 50:f76b7e7959a2 61 XBeeDeviceRemoteAt::XBeeDeviceRemoteAt( XBeeDevice* p_device,
johnb 50:f76b7e7959a2 62 const uint16_t& p_addr16Bit,
johnb 50:f76b7e7959a2 63 const uint64_t& p_addr64Bit ) : XBeeApiCmdAt( p_device ), m_addr16Bit( p_addr16Bit ), m_addr64Bit( p_addr64Bit )
johnb 50:f76b7e7959a2 64 {
johnb 50:f76b7e7959a2 65 }
johnb 50:f76b7e7959a2 66
johnb 50:f76b7e7959a2 67 XBeeDeviceRemoteAt::~XBeeDeviceRemoteAt( void )
johnb 50:f76b7e7959a2 68 {
johnb 50:f76b7e7959a2 69 }
johnb 50:f76b7e7959a2 70
johnb 50:f76b7e7959a2 71 extern Serial pc;
johnb 50:f76b7e7959a2 72
johnb 50:f76b7e7959a2 73 #define XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS (XBEE_CMD_POSN_ID_SPECIFIC_DATA+1)
johnb 50:f76b7e7959a2 74 #define XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS (XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + sizeof( uint64_t))
johnb 50:f76b7e7959a2 75 #define XBEE_REMOTE_AT_RESPONSE_STATUS (17U)
johnb 50:f76b7e7959a2 76
johnb 50:f76b7e7959a2 77 size_t XBeeDeviceRemoteAt::getResponseStatusPos( void ) const
johnb 50:f76b7e7959a2 78 {
johnb 50:f76b7e7959a2 79 return XBEE_REMOTE_AT_RESPONSE_STATUS;
johnb 50:f76b7e7959a2 80 }
johnb 50:f76b7e7959a2 81
johnb 50:f76b7e7959a2 82 bool XBeeDeviceRemoteAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
johnb 50:f76b7e7959a2 83 {
johnb 50:f76b7e7959a2 84 bool ret_val = false;
johnb 50:f76b7e7959a2 85
johnb 50:f76b7e7959a2 86 /* TODO: Length check */
johnb 50:f76b7e7959a2 87
johnb 50:f76b7e7959a2 88 if( XBEE_CMD_REMOTE_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] )
johnb 50:f76b7e7959a2 89 {
johnb 50:f76b7e7959a2 90 uint64_t src64BitAddr = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS ]) << 56U) |
johnb 50:f76b7e7959a2 91 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 1 ]) << 48U) |
johnb 50:f76b7e7959a2 92 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 2 ]) << 40U) |
johnb 50:f76b7e7959a2 93 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 3 ]) << 32U) |
johnb 50:f76b7e7959a2 94 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 4 ]) << 24U) |
johnb 50:f76b7e7959a2 95 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 5 ]) << 16U) |
johnb 50:f76b7e7959a2 96 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 6 ]) << 8U) |
johnb 50:f76b7e7959a2 97 ((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 7 ]);
johnb 50:f76b7e7959a2 98 uint16_t src16BitAddr = (((uint16_t)p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS ]) << 8U) |
johnb 50:f76b7e7959a2 99 p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS + 1 ];
johnb 50:f76b7e7959a2 100
johnb 50:f76b7e7959a2 101 if(( src16BitAddr == m_addr16Bit ) ||
johnb 50:f76b7e7959a2 102 ( src64BitAddr == m_addr64Bit ))
johnb 50:f76b7e7959a2 103 {
johnb 50:f76b7e7959a2 104 ret_val = processResponseFrame( p_data, p_len );
johnb 50:f76b7e7959a2 105
johnb 50:f76b7e7959a2 106 #if 0
johnb 50:f76b7e7959a2 107 pc.printf("Len %d\r\n",p_len);
johnb 50:f76b7e7959a2 108 for( uint8_t i = 0; i < p_len; i++ ) {
johnb 50:f76b7e7959a2 109 pc.printf("%02X ",p_data[i]);
johnb 50:f76b7e7959a2 110 }
johnb 50:f76b7e7959a2 111 pc.printf("\r\n");
johnb 50:f76b7e7959a2 112 ret_val = true;
johnb 50:f76b7e7959a2 113 #endif
johnb 50:f76b7e7959a2 114 }
johnb 50:f76b7e7959a2 115 }
johnb 50:f76b7e7959a2 116
johnb 50:f76b7e7959a2 117 return ret_val;
johnb 50:f76b7e7959a2 118 }
johnb 50:f76b7e7959a2 119
johnb 50:f76b7e7959a2 120 void XBeeDeviceRemoteAt::SendCmd_uint8_t( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 121 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 122 const uint8_t& p_val )
johnb 50:f76b7e7959a2 123 {
johnb 50:f76b7e7959a2 124 XBeeApiCmdAtRemoteSet<uint8_t> req( p_frameId, m_addr16Bit, m_addr64Bit, p_data, p_val );
johnb 50:f76b7e7959a2 125 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 126 }
johnb 50:f76b7e7959a2 127
johnb 50:f76b7e7959a2 128 void XBeeDeviceRemoteAt::SendCmd_uint16_t( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 129 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 130 const uint16_t& p_val )
johnb 50:f76b7e7959a2 131 {
johnb 50:f76b7e7959a2 132 XBeeApiCmdAtRemoteSet<uint16_t> req( p_frameId, m_addr16Bit, m_addr64Bit, p_data, p_val );
johnb 50:f76b7e7959a2 133 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 134 }
johnb 50:f76b7e7959a2 135
johnb 50:f76b7e7959a2 136 void XBeeDeviceRemoteAt::SendReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 137 const uint8_t* p_data )
johnb 50:f76b7e7959a2 138 {
johnb 50:f76b7e7959a2 139 XBeeApiCmdAtRemoteReq req( p_frameId, m_addr16Bit, m_addr64Bit, p_data );
johnb 50:f76b7e7959a2 140 m_device->SendFrame( &req );
johnb 50:f76b7e7959a2 141 }
johnb 50:f76b7e7959a2 142
johnb 50:f76b7e7959a2 143 template < typename T >
johnb 50:f76b7e7959a2 144 XBeeApiCmdAtRemoteSet<T>::XBeeApiCmdAtRemoteSet( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 145 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 146 const uint64_t p_addr64Bit,
johnb 50:f76b7e7959a2 147 const uint8_t* const p_data,
johnb 50:f76b7e7959a2 148 const T p_val ) : XBeeApiFrame( )
johnb 50:f76b7e7959a2 149 {
johnb 50:f76b7e7959a2 150 size_t s;
johnb 50:f76b7e7959a2 151 uint8_t* dest;
johnb 50:f76b7e7959a2 152 const uint8_t* src = (uint8_t*)(&p_val);
johnb 50:f76b7e7959a2 153
johnb 50:f76b7e7959a2 154 m_apiId = XBEE_CMD_REMOTE_AT_CMD;
johnb 50:f76b7e7959a2 155
johnb 50:f76b7e7959a2 156 m_buffer[0] = p_frameId;
johnb 50:f76b7e7959a2 157
johnb 50:f76b7e7959a2 158 m_buffer[1] = p_addr64Bit >> 56U;
johnb 50:f76b7e7959a2 159 m_buffer[2] = p_addr64Bit >> 48U;
johnb 50:f76b7e7959a2 160 m_buffer[3] = p_addr64Bit >> 40U;
johnb 50:f76b7e7959a2 161 m_buffer[4] = p_addr64Bit >> 32U;
johnb 50:f76b7e7959a2 162 m_buffer[5] = p_addr64Bit >> 24U;
johnb 50:f76b7e7959a2 163 m_buffer[6] = p_addr64Bit >> 16U;
johnb 50:f76b7e7959a2 164 m_buffer[7] = p_addr64Bit >> 8U;
johnb 50:f76b7e7959a2 165 m_buffer[8] = p_addr64Bit;
johnb 50:f76b7e7959a2 166
johnb 50:f76b7e7959a2 167 m_buffer[9] = p_addr16Bit >> 8U;
johnb 50:f76b7e7959a2 168 m_buffer[10] = p_addr16Bit;
johnb 50:f76b7e7959a2 169
johnb 50:f76b7e7959a2 170 m_buffer[11] = 0;
johnb 50:f76b7e7959a2 171
johnb 50:f76b7e7959a2 172 m_buffer[12] = p_data[0];
johnb 50:f76b7e7959a2 173 m_buffer[13] = p_data[1];
johnb 50:f76b7e7959a2 174
johnb 50:f76b7e7959a2 175 m_dataLen = sizeof( m_buffer );
johnb 50:f76b7e7959a2 176
johnb 50:f76b7e7959a2 177 /* TODO: This copy code isn't portable - it's assuming that the data in
johnb 50:f76b7e7959a2 178 * p_data is little endian */
johnb 50:f76b7e7959a2 179
johnb 50:f76b7e7959a2 180 dest = &( m_buffer[ m_dataLen - 1 ] );
johnb 50:f76b7e7959a2 181
johnb 50:f76b7e7959a2 182 for( s = 0;
johnb 50:f76b7e7959a2 183 s < sizeof( T );
johnb 50:f76b7e7959a2 184 s++, dest--, src++ ) {
johnb 50:f76b7e7959a2 185 *dest = *src;
johnb 50:f76b7e7959a2 186 }
johnb 50:f76b7e7959a2 187
johnb 50:f76b7e7959a2 188 m_data = m_buffer;
johnb 50:f76b7e7959a2 189 }
johnb 50:f76b7e7959a2 190
johnb 50:f76b7e7959a2 191 template < typename T >
johnb 50:f76b7e7959a2 192 XBeeApiCmdAtRemoteSet<T>::~XBeeApiCmdAtRemoteSet()
johnb 50:f76b7e7959a2 193 {
johnb 50:f76b7e7959a2 194 }
johnb 50:f76b7e7959a2 195
johnb 50:f76b7e7959a2 196 XBeeApiCmdAtRemoteReq::XBeeApiCmdAtRemoteReq( const uint8_t p_frameId,
johnb 50:f76b7e7959a2 197 const uint16_t p_addr16Bit,
johnb 50:f76b7e7959a2 198 const uint64_t p_addr64Bit,
johnb 50:f76b7e7959a2 199 const uint8_t* const p_data )
johnb 50:f76b7e7959a2 200 {
johnb 50:f76b7e7959a2 201 m_apiId = XBEE_CMD_REMOTE_AT_CMD;
johnb 50:f76b7e7959a2 202
johnb 50:f76b7e7959a2 203 m_buffer[0] = p_frameId;
johnb 50:f76b7e7959a2 204
johnb 50:f76b7e7959a2 205 m_buffer[1] = p_addr64Bit >> 56U;
johnb 50:f76b7e7959a2 206 m_buffer[2] = p_addr64Bit >> 48U;
johnb 50:f76b7e7959a2 207 m_buffer[3] = p_addr64Bit >> 40U;
johnb 50:f76b7e7959a2 208 m_buffer[4] = p_addr64Bit >> 32U;
johnb 50:f76b7e7959a2 209 m_buffer[5] = p_addr64Bit >> 24U;
johnb 50:f76b7e7959a2 210 m_buffer[6] = p_addr64Bit >> 16U;
johnb 50:f76b7e7959a2 211 m_buffer[7] = p_addr64Bit >> 8U;
johnb 50:f76b7e7959a2 212 m_buffer[8] = p_addr64Bit;
johnb 50:f76b7e7959a2 213
johnb 50:f76b7e7959a2 214 m_buffer[9] = p_addr16Bit >> 8U;
johnb 50:f76b7e7959a2 215 m_buffer[10] = p_addr16Bit;
johnb 50:f76b7e7959a2 216
johnb 50:f76b7e7959a2 217 m_buffer[11] = 0;
johnb 50:f76b7e7959a2 218
johnb 50:f76b7e7959a2 219 m_buffer[12] = p_data[0];
johnb 50:f76b7e7959a2 220 m_buffer[13] = p_data[1];
johnb 50:f76b7e7959a2 221
johnb 50:f76b7e7959a2 222 m_dataLen = sizeof( m_buffer );
johnb 50:f76b7e7959a2 223 m_data = m_buffer;
johnb 50:f76b7e7959a2 224 }
johnb 50:f76b7e7959a2 225
johnb 50:f76b7e7959a2 226 XBeeApiCmdAtRemoteReq::~XBeeApiCmdAtRemoteReq()
johnb 50:f76b7e7959a2 227 {
johnb 50:f76b7e7959a2 228 }