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.
Revision 38:291aee9eb900, committed 2014-04-02
- Comitter:
- johnb
- Date:
- Wed Apr 02 21:32:25 2014 +0000
- Parent:
- 37:653e048c0fcd
- Child:
- 39:35ab09bff018
- Commit message:
- Sync from https://github.com/bright-tools/xbeeapi/commit/10fb465f9b314dcf88e007bd3f33af856ab70c8f
Changed in this revision
--- a/Base/XBeeApiFrame.cpp Mon Mar 31 19:33:38 2014 +0000 +++ b/Base/XBeeApiFrame.cpp Wed Apr 02 21:32:25 2014 +0000 @@ -26,8 +26,8 @@ } XBeeApiFrame::XBeeApiFrame( XBeeApiIdentifier_e p_id, - const uint8_t* p_data, - const size_t p_dataLen ) : m_apiId( p_id ), m_data( p_data ), m_dataLen( p_dataLen ) + const uint8_t* const p_data, + const size_t p_dataLen ) : m_apiId( p_id ), m_data( p_data ), m_dataLen( p_dataLen ) { } @@ -41,7 +41,7 @@ return m_apiId; } -void XBeeApiFrame::getDataPtr( const uint16_t p_start, const uint8_t** p_buff, uint16_t* const p_len ) +void XBeeApiFrame::getDataPtr( const uint16_t p_start, const uint8_t** p_buff, uint16_t* const p_len ) const { *p_len = m_dataLen; (*p_buff) = m_data;
--- a/Base/XBeeApiFrame.hpp Mon Mar 31 19:33:38 2014 +0000 +++ b/Base/XBeeApiFrame.hpp Wed Apr 02 21:32:25 2014 +0000 @@ -97,8 +97,8 @@ /** Constructor */ XBeeApiFrame( XBeeApiIdentifier_e p_id, - const uint8_t* p_data, - const size_t p_dataLen ); + const uint8_t* const p_data, + const size_t p_dataLen ); /** Return the length of the API-specific structure represented by this frame. i.e. the API frame without the overhead - see XBEE_API_FRAME_OVERHEAD */ virtual uint16_t getCmdLen( void ) const; @@ -125,7 +125,7 @@ byte that is required. \param[out] p_buff Pointer to a pointer to receive the buffer pointer \param[out] p_len Pointer to receive the length of the data pointed to by *p_buff */ - virtual void getDataPtr( const uint16_t p_start, const uint8_t** p_buff, uint16_t* const p_len ); + virtual void getDataPtr( const uint16_t p_start, const uint8_t** p_buff, uint16_t* const p_len ) const; }; /** Class which acts as a receiver for data from the XBee and takes care of decoding it.
--- a/RXTX/XBeeApiRxFrame.cpp Mon Mar 31 19:33:38 2014 +0000 +++ b/RXTX/XBeeApiRxFrame.cpp Wed Apr 02 21:32:25 2014 +0000 @@ -18,70 +18,46 @@ #include "XBeeApiRxFrame.hpp" -XBeeApiRxFrame::XBeeApiRxFrame( XBeeDevice* p_device ) : XBeeApiFrame(), XBeeApiFrameDecoder( p_device ) +XBeeApiRxFrame::XBeeApiRxFrame( void ) : XBeeApiFrame(), + m_dataIsMallocd ( false ) +{ +} + + /** Constructor */ +XBeeApiRxFrame::XBeeApiRxFrame( XBeeApiIdentifier_e p_id, + const uint8_t* const p_data, + const size_t p_dataLen ) : XBeeApiFrame( p_id, p_data, p_dataLen ), + m_dataIsMallocd ( false ) { } XBeeApiRxFrame::~XBeeApiRxFrame( void ) { + if( m_dataIsMallocd ) + { + free( m_mallocdData ); + } } - -bool XBeeApiRxFrame::decodeCallback( const uint8_t* const p_data, size_t p_len ) + +bool XBeeApiRxFrame::deepCopyFrom( const XBeeApiRxFrame& p_frame ) { - bool ret_val = false; - - /* TODO: Length check */ - - if(( XBEE_CMD_RX_64B_ADDR == p_data[ XBEE_CMD_POSN_API_ID ] ) || - ( XBEE_CMD_RX_16B_ADDR == p_data[ XBEE_CMD_POSN_API_ID ] )) + /* TODO: need to catch assignment operator and free data if data is already malloc'd */ + if( m_dataIsMallocd ) { - size_t pos = XBEE_CMD_POSN_ID_SPECIFIC_DATA; - - m_apiId = (XBeeApiIdentifier_e)(p_data[ XBEE_CMD_POSN_API_ID ]); - - /* Depending on the frame type, decode either a 64- or 16-bit address */ - if( XBEE_CMD_RX_64B_ADDR == p_data[ XBEE_CMD_POSN_API_ID ] ) - { - m_addr = (((uint64_t)p_data[ pos ]) << 54U ) | - (((uint64_t)p_data[ pos+1 ]) << 48U ) | - (((uint64_t)p_data[ pos+2 ]) << 40U ) | - (((uint64_t)p_data[ pos+3 ]) << 32U ) | - (((uint64_t)p_data[ pos+4 ]) << 24U ) | - (((uint64_t)p_data[ pos+5 ]) << 16U ) | - (((uint64_t)p_data[ pos+6 ]) << 8U ) | - p_data[ pos+7 ]; - pos += sizeof( uint64_t ); - m_addrIs16bit = false; - } - else - { - m_addr = (((uint16_t)p_data[ pos ]) << 8U ) | - p_data[ pos+1 ]; - pos += sizeof( uint16_t ); - m_addrIs16bit = true; - } - - m_rssi += p_data[ pos++ ]; - - m_addressBroadcast = p_data[ pos ] & 2U; - m_panBroadcast = p_data[ pos ] & 4U; - pos++; - - m_data = &( p_data[ pos ] ); - - /* -1 to account for the checksum */ - m_dataLen = p_len - pos - 1; - - frameRxCallback(); - - // TODO: m_data pointing to p_data here, which will be going away .. - - ret_val = true; + free( m_mallocdData ); + m_dataIsMallocd = false; } - - return ret_val; -} - -void XBeeApiRxFrame::frameRxCallback( void ) -{ + *this = p_frame; + m_mallocdData = (uint8_t*)malloc( p_frame.m_dataLen ); + if( m_mallocdData ) + { + m_dataIsMallocd = true; + memcpy( m_mallocdData, p_frame.m_data, m_dataLen ); + m_data = m_mallocdData; + } + else + { + m_data = NULL; + } + return( m_mallocdData != NULL ); } \ No newline at end of file
--- a/RXTX/XBeeApiRxFrame.hpp Mon Mar 31 19:33:38 2014 +0000 +++ b/RXTX/XBeeApiRxFrame.hpp Wed Apr 02 21:32:25 2014 +0000 @@ -35,7 +35,7 @@ The message data content is accessed via the inherited m_data and m_dataLen members */ -class XBeeApiRxFrame : public XBeeApiFrame, public XBeeApiFrameDecoder +class XBeeApiRxFrame : public XBeeApiFrame { protected: /** The source address of the packet */ @@ -53,27 +53,27 @@ /** Indicate whether or not the message was PAN broadcase */ bool m_panBroadcast; - - /** Called by XBeeDevice in order to offer frame data to the object for - decoding - - \param p_data Pointer to the content of the received data - \param p_len Length of the data pointed to by p_data - */ - virtual bool decodeCallback( const uint8_t* const p_data, size_t p_len ); + + /* TODO: doc */ + bool m_dataIsMallocd; + /* TODO: doc */ + uint8_t* m_mallocdData; public: /** Constructor */ - XBeeApiRxFrame( XBeeDevice* p_device = NULL ); + XBeeApiRxFrame(); + /** Constructor */ + XBeeApiRxFrame( XBeeApiIdentifier_e p_id, + const uint8_t* const p_data, + const size_t p_dataLen ); + + /** Deep copy */ + /* TODO: doc */ + bool deepCopyFrom( const XBeeApiRxFrame& p_frame ); + /** Destructor */ virtual ~XBeeApiRxFrame( void ); - - /* Callback which is invoked when a frame is successfully decoded. The - contents of the frame decode can be inspected via the class member - variables */ - virtual void frameRxCallback( void ); - }; #endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RXTX/XBeeApiRxFrameCircularBuffer.cpp Wed Apr 02 21:32:25 2014 +0000 @@ -0,0 +1,72 @@ +/** + +Copyright 2014 John Bailey + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +*/ + +#include "XBeeApiRxFrameCircularBuffer.hpp" + +XBeeApiRxFrameCircularBuffer::XBeeApiRxFrameCircularBuffer( size_t p_bufferSize, XBeeDevice* p_device ) : XBeeApiRxFrameDecoder( p_device ), + m_bufferSize( p_bufferSize ), + m_head( 0 ), + m_tail( 0 ), + m_count( 0 ) +{ + m_framesBuffer = new( XBeeApiRxFrame[ m_bufferSize ] ); +} + +XBeeApiRxFrameCircularBuffer::~XBeeApiRxFrameCircularBuffer( void ) +{ +} + +void XBeeApiRxFrameCircularBuffer::frameRxCallback( const XBeeApiRxFrame* const p_frame ) +{ + if( m_count < m_bufferSize ) + { + size_t insertPoint = m_head; + m_count++; + m_head = (m_head + 1) % m_bufferSize; + m_framesBuffer[ insertPoint ].deepCopyFrom( *p_frame ); + } +} + +size_t XBeeApiRxFrameCircularBuffer::getFrameCount() const +{ + return( m_count ); +} + +void XBeeApiRxFrameCircularBuffer::clear() +{ + m_count = 0; +} + +void XBeeApiRxFrameCircularBuffer::pop() +{ + if( m_count ) + { + m_tail = (m_tail + 1) % m_bufferSize; + m_count--; + } +} + +const XBeeApiRxFrame* XBeeApiRxFrameCircularBuffer::getTailPtr( void ) const +{ + XBeeApiRxFrame* ret_val = NULL; + if( m_count ) + { + ret_val = &( m_framesBuffer[ m_tail ] ); + } + return ret_val; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RXTX/XBeeApiRxFrameCircularBuffer.hpp Wed Apr 02 21:32:25 2014 +0000 @@ -0,0 +1,65 @@ +/** + @file + @brief Class to decode and store received data frames in a circular + buffer + + @author John Bailey + + @copyright Copyright 2014 John Bailey + + @section LICENSE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +*/ + +#if !defined XBEEAPIRXFRAMECIRCULARBUFFER_HPP +#define XBEEAPIRXFRAMECIRCULARBUFFER_HPP + +#include "XBeeApiRxFrameDecoder.hpp" +#include "XBeeDevice.hpp" + +#include <stdint.h> + +/** Class to deal with decoding of an RX'd data frame +*/ +class XBeeApiRxFrameCircularBuffer : public XBeeApiRxFrameDecoder +{ + protected: + size_t m_bufferSize; + size_t m_head; + size_t m_tail; + size_t m_count; + XBeeApiRxFrame* m_framesBuffer; + public: + /** Constructor */ + XBeeApiRxFrameCircularBuffer( size_t p_bufferSize, XBeeDevice* p_device = NULL ); + + /** Destructor */ + virtual ~XBeeApiRxFrameCircularBuffer( void ); + + /* Callback which is invoked when a frame is successfully decoded + \param p_frame The frame content + */ + virtual void frameRxCallback( const XBeeApiRxFrame* const p_frame ); + + size_t getFrameCount() const; + + void clear(); + + void pop(); + + const XBeeApiRxFrame* getTailPtr() const; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RXTX/XBeeApiRxFrameDecoder.cpp Wed Apr 02 21:32:25 2014 +0000 @@ -0,0 +1,90 @@ +/** + +Copyright 2014 John Bailey + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +*/ + +#include "XBeeApiRxFrameDecoder.hpp" + +XBeeApiRxFrameDecoder::XBeeApiRxFrameDecoder( XBeeDevice* p_device ) : XBeeApiFrameDecoder( p_device ) +{ +} + +XBeeApiRxFrameDecoder::~XBeeApiRxFrameDecoder( void ) +{ +} + +bool XBeeApiRxFrameDecoder::decodeCallback( const uint8_t* const p_data, size_t p_len ) +{ + bool ret_val = false; + + /* TODO: Length check */ + + if(( XBEE_CMD_RX_64B_ADDR == p_data[ XBEE_CMD_POSN_API_ID ] ) || + ( XBEE_CMD_RX_16B_ADDR == p_data[ XBEE_CMD_POSN_API_ID ] )) + { + size_t pos = XBEE_CMD_POSN_ID_SPECIFIC_DATA; + uint64_t addr; + bool addrIs16bit; + uint8_t rssi; + bool addressBroadcast; + bool panBroadcast; + const uint8_t* data; + size_t dataLen; + + /* Depending on the frame type, decode either a 64- or 16-bit address */ + if( XBEE_CMD_RX_64B_ADDR == p_data[ XBEE_CMD_POSN_API_ID ] ) + { + addr = (((uint64_t)p_data[ pos ]) << 54U ) | + (((uint64_t)p_data[ pos+1 ]) << 48U ) | + (((uint64_t)p_data[ pos+2 ]) << 40U ) | + (((uint64_t)p_data[ pos+3 ]) << 32U ) | + (((uint64_t)p_data[ pos+4 ]) << 24U ) | + (((uint64_t)p_data[ pos+5 ]) << 16U ) | + (((uint64_t)p_data[ pos+6 ]) << 8U ) | + p_data[ pos+7 ]; + pos += sizeof( uint64_t ); + addrIs16bit = false; + } + else + { + addr = (((uint16_t)p_data[ pos ]) << 8U ) | + p_data[ pos+1 ]; + pos += sizeof( uint16_t ); + addrIs16bit = true; + } + + rssi += p_data[ pos++ ]; + + addressBroadcast = p_data[ pos ] & 2U; + panBroadcast = p_data[ pos ] & 4U; + pos++; + + data = &( p_data[ pos ] ); + + /* -1 to account for the checksum */ + dataLen = p_len - pos - 1; + + XBeeApiRxFrame new_frame( (XBeeApiIdentifier_e)(p_data[ XBEE_CMD_POSN_API_ID ]), + data, + dataLen ); + + frameRxCallback( &new_frame ); + + ret_val = true; + } + + return ret_val; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RXTX/XBeeApiRxFrameDecoder.hpp Wed Apr 02 21:32:25 2014 +0000 @@ -0,0 +1,59 @@ +/** + @file + @brief Class to support reception of data via the XBee's wireless + + @author John Bailey + + @copyright Copyright 2014 John Bailey + + @section LICENSE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +*/ + +#if !defined XBEEAPIRXFRAMEDECODER_HPP +#define XBEEAPIRXFRAMEDECODER_HPP + +#include "XBeeApiRxFrame.hpp" +#include "XBeeDevice.hpp" + +#include <stdint.h> + +/** Class to deal with decoding of an RX'd data frame +*/ +class XBeeApiRxFrameDecoder : public XBeeApiFrameDecoder +{ + protected: + /** Called by XBeeDevice in order to offer frame data to the object for + decoding + + \param p_data Pointer to the content of the received data + \param p_len Length of the data pointed to by p_data + */ + virtual bool decodeCallback( const uint8_t* const p_data, size_t p_len ); + + public: + /** Constructor */ + XBeeApiRxFrameDecoder( XBeeDevice* p_device = NULL ); + + /** Destructor */ + virtual ~XBeeApiRxFrameDecoder( void ); + + /* Callback which is invoked when a frame is successfully decoded + \param p_frame The frame content + */ + virtual void frameRxCallback( const XBeeApiRxFrame* const p_frame ) = 0; +}; + +#endif \ No newline at end of file
--- a/RXTX/XBeeApiTxFrame.cpp Mon Mar 31 19:33:38 2014 +0000 +++ b/RXTX/XBeeApiTxFrame.cpp Wed Apr 02 21:32:25 2014 +0000 @@ -101,6 +101,7 @@ { m_data = p_buff; m_dataLen = p_len; + ret_val = true; } return ret_val; } @@ -149,4 +150,4 @@ void XBeeApiTxFrame::frameTxCallback( const XBeeApiTxStatus_e p_status ) { /* TODO */ -} +} \ No newline at end of file
--- a/Utility/XBeeApiSetupHelper.cpp Mon Mar 31 19:33:38 2014 +0000 +++ b/Utility/XBeeApiSetupHelper.cpp Wed Apr 02 21:32:25 2014 +0000 @@ -18,13 +18,22 @@ #include "XBeeApiSetupHelper.hpp" -void xbeeSetNetworkTypeP2P( XBeeApiCmdAt* const p_xbeeCmd, +bool xbeeSetNetworkTypeP2P( XBeeApiCmdAt* const p_xbeeCmd, const XBeeApiCmdAt::panId_t p_id, const XBeeApiCmdAt::channel_t p_chan ) { - p_xbeeCmd->setCoordinatorEnabled( false ); - p_xbeeCmd->setEndDeviceAssociationEnabled( false ); - p_xbeeCmd->setChannel( p_chan ); - p_xbeeCmd->setPanId( p_id ); + bool ret_val = p_xbeeCmd->setCoordinatorEnabled( false ); + if( ret_val ) + { + ret_val = p_xbeeCmd->setEndDeviceAssociationEnabled( false ); + } + if( ret_val ) + { + p_xbeeCmd->setChannel( p_chan ); + } + if( ret_val ) + { + p_xbeeCmd->setPanId( p_id ); + } + return ret_val; } -
--- a/Utility/XBeeApiSetupHelper.hpp Mon Mar 31 19:33:38 2014 +0000 +++ b/Utility/XBeeApiSetupHelper.hpp Wed Apr 02 21:32:25 2014 +0000 @@ -45,8 +45,10 @@ as a decoder with an XBee device. \param p_id Network ID to use \param p_chan Channel to use + \return true in the case that the XBee was configured, false in the case that a problem + was encountered */ -extern void xbeeSetNetworkTypeP2P( XBeeApiCmdAt* const p_xbeeCmd, +extern bool xbeeSetNetworkTypeP2P( XBeeApiCmdAt* const p_xbeeCmd, const XBeeApiCmdAt::panId_t p_id, const XBeeApiCmdAt::channel_t p_chan );
--- a/Utility/xbeeapi.hpp Mon Mar 31 19:33:38 2014 +0000 +++ b/Utility/xbeeapi.hpp Wed Apr 02 21:32:25 2014 +0000 @@ -28,6 +28,8 @@ #include "XBeeDevice.hpp" #include "XBeeApiFrame.hpp" #include "XBeeApiRxFrame.hpp" +#include "XBeeApiRxFrameDecoder.hpp" +#include "XBeeApiRxFrameCircularBuffer.hpp" #include "XBeeApiTxFrame.hpp" #include "XBeeApiTxFrameEx.hpp" #include "XBeeApiCmdAt.hpp"