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.
Remote/XBeeDeviceRemoteAt.cpp
- Committer:
- johnb
- Date:
- 2014-08-02
- Revision:
- 55:610aa4a2ed3b
- Parent:
- 54:9f5b7652943e
- Child:
- 56:7fe74b03e6b1
File content as of revision 55:610aa4a2ed3b:
/** 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 "XBeeDeviceRemoteAt.hpp" #define XBEE_API_CMD_REMOTE_REQ_HEADER_LEN 14U template< typename T > class XBeeApiCmdAtRemoteSet : public XBeeApiFrame { uint8_t m_buffer[ XBEE_API_CMD_REMOTE_REQ_HEADER_LEN + sizeof( T ) ]; public: /** Constructor \param p_data Pointer to a buffer of length 2 bytes identifying the command, e.g. 'V', 'R' would set up a version request \param p_val New value for the parameter */ XBeeApiCmdAtRemoteSet( const uint8_t p_frameId, const uint16_t p_addr16Bit, const uint64_t p_addr64Bit, const XBeeDevice::XBeeApiAddrType_t p_type, const bool p_applyChanges, const uint8_t* const p_data, const T p_val ); /** Destructor */ virtual ~XBeeApiCmdAtRemoteSet(); }; class XBeeApiCmdAtRemoteReq : public XBeeApiFrame { uint8_t m_buffer[ XBEE_API_CMD_REMOTE_REQ_HEADER_LEN ]; public: /** Constructor \param p_data Pointer to a buffer of length 2 bytes identifying the command, e.g. 'V', 'R' would set up a version request \param p_val New value for the parameter */ XBeeApiCmdAtRemoteReq( const uint8_t p_frameId, const uint16_t p_addr16Bit, const uint64_t p_addr64Bit, const XBeeDevice::XBeeApiAddrType_t p_type, const uint8_t* const p_data ); /** Destructor */ virtual ~XBeeApiCmdAtRemoteReq(); }; XBeeDeviceRemoteAt::XBeeDeviceRemoteAt( XBeeDevice* p_device, const uint16_t& p_addr16Bit, const uint64_t& p_addr64Bit, const bool p_applyChanges ) : XBeeApiCmdAt( p_device ), m_applyChanges( p_applyChanges ) { setAddress( p_addr16Bit, p_addr64Bit ); } void XBeeDeviceRemoteAt::setAddress( const uint16_t& p_addr16Bit, const uint64_t& p_addr64Bit ) { if( p_addr16Bit != XBEE_USE_64BIT_ADDR ) { m_sourceAddress = p_addr16Bit; m_have_sourceAddress = true; m_snLow = m_snHigh = 0; m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_16BIT; } else { m_snLow = (p_addr64Bit & 0xFFFFFFFF); m_snHigh = ((p_addr64Bit >> 32U) & 0xFFFFFFFF); m_sourceAddress = XBEE_USE_64BIT_ADDR; m_have_snLow = m_have_snHigh = true; m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_64BIT; } } void XBeeDeviceRemoteAt::reassociate( const uint16_t& p_addr16Bit, const uint64_t& p_addr64Bit ) { resetCachedData(); setAddress( p_addr16Bit, p_addr64Bit ); } XBeeDeviceRemoteAt::~XBeeDeviceRemoteAt( void ) { } void XBeeDeviceRemoteAt::setApplyChanges( const bool p_apply ) { m_applyChanges = p_apply; } #define XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS (XBEE_CMD_POSN_ID_SPECIFIC_DATA+1) #define XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS (XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + sizeof( uint64_t)) #define XBEE_REMOTE_AT_RESPONSE_STATUS (17U) size_t XBeeDeviceRemoteAt::getResponseStatusPos( void ) const { return XBEE_REMOTE_AT_RESPONSE_STATUS; } bool XBeeDeviceRemoteAt::decodeCallback( const uint8_t* const p_data, size_t p_len ) { bool ret_val = false; /* TODO: Length check */ if( XBEE_CMD_REMOTE_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] ) { uint32_t srcAddrHigh = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS ]) << 24U) | (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 1 ]) << 16U) | (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 2 ]) << 8U) | (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 3 ])); uint32_t srcAddrLow = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 4 ]) << 24U) | (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 5 ]) << 16U) | (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 6 ]) << 8U) | ((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 7 ]); uint16_t src16BitAddr = (((uint16_t)p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS ]) << 8U) | p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS + 1 ]; if((( m_have_sourceAddress ) && ( m_sourceAddress == src16BitAddr )) || ( m_have_snHigh && m_have_snLow && ( srcAddrHigh == m_snHigh ) && ( srcAddrLow == m_snLow ))) { ret_val = processResponseFrame( p_data, p_len ); } } else { ret_val = XBeeApiCmdAt::decodeCallback( p_data, p_len ); } return ret_val; } void XBeeDeviceRemoteAt::SendCmd_uint8_t( const uint8_t p_frameId, const uint8_t* const p_data, const uint8_t& p_val ) { uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow; /* TODO: Add option to force usage of 16 or 64-bit addressing */ XBeeApiCmdAtRemoteSet<uint8_t> req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, m_applyChanges, p_data, p_val ); m_device->SendFrame( &req ); } void XBeeDeviceRemoteAt::SendCmd_uint16_t( const uint8_t p_frameId, const uint8_t* const p_data, const uint16_t& p_val ) { uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow; XBeeApiCmdAtRemoteSet<uint16_t> req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, m_applyChanges, p_data, p_val ); m_device->SendFrame( &req ); } void XBeeDeviceRemoteAt::SendCmd_uint32_t( const uint8_t p_frameId, const uint8_t* const p_data, const uint32_t& p_val ) { uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow; XBeeApiCmdAtRemoteSet<uint32_t> req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, m_applyChanges, p_data, p_val ); m_device->SendFrame( &req ); } void XBeeDeviceRemoteAt::SendReq( const uint8_t p_frameId, const uint8_t* p_data ) { uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow; XBeeApiCmdAtRemoteReq req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, p_data ); m_device->SendFrame( &req ); } bool XBeeDeviceRemoteAt::setAddressingType( const XBeeDevice::XBeeApiAddrType_t p_type ) { bool ret_val = false; switch( p_type ) { case XBeeDevice::XBEE_API_ADDR_TYPE_16BIT: if( m_have_sourceAddress ) { m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_16BIT; ret_val = true; } break; case XBeeDevice::XBEE_API_ADDR_TYPE_64BIT: if( m_have_snLow && m_have_snHigh ) { m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_64BIT; ret_val = true; } break; } return ret_val; } static void writeAddressToBuffer( uint8_t* const p_buffer, const uint16_t p_addr16Bit, const uint64_t p_addr64Bit, const XBeeDevice::XBeeApiAddrType_t p_type ) { if( p_type == XBeeDevice::XBEE_API_ADDR_TYPE_64BIT ) { p_buffer[0] = p_addr64Bit >> 56U; p_buffer[1] = p_addr64Bit >> 48U; p_buffer[2] = p_addr64Bit >> 40U; p_buffer[3] = p_addr64Bit >> 32U; p_buffer[4] = p_addr64Bit >> 24U; p_buffer[5] = p_addr64Bit >> 16U; p_buffer[6] = p_addr64Bit >> 8U; p_buffer[7] = p_addr64Bit; p_buffer[8] = (uint8_t)(XBEE_USE_64BIT_ADDR >> 8U); p_buffer[9] = (uint8_t)(XBEE_USE_64BIT_ADDR & 0xFF); } else { p_buffer[0] = 0; p_buffer[1] = 0; p_buffer[2] = 0; p_buffer[3] = 0; p_buffer[4] = 0; p_buffer[5] = 0; p_buffer[6] = 0; p_buffer[7] = 0; p_buffer[8] = p_addr16Bit >> 8U; p_buffer[9] = p_addr16Bit; } } template < typename T > XBeeApiCmdAtRemoteSet<T>::XBeeApiCmdAtRemoteSet( const uint8_t p_frameId, const uint16_t p_addr16Bit, const uint64_t p_addr64Bit, const XBeeDevice::XBeeApiAddrType_t p_type, const bool p_applyChanges, const uint8_t* const p_data, const T p_val ) : XBeeApiFrame( ) { size_t s; uint8_t* dest; const uint8_t* src = (uint8_t*)(&p_val); m_apiId = XBEE_CMD_REMOTE_AT_CMD; m_buffer[0] = p_frameId; writeAddressToBuffer( &(m_buffer[1]), p_addr16Bit, p_addr64Bit, p_type ); m_buffer[11] = p_applyChanges << 1; m_buffer[12] = p_data[0]; m_buffer[13] = p_data[1]; m_dataLen = sizeof( m_buffer ); /* TODO: This copy code isn't portable - it's assuming that the data in * p_data is little endian */ dest = &( m_buffer[ m_dataLen - 1 ] ); for( s = 0; s < sizeof( T ); s++, dest--, src++ ) { *dest = *src; } m_data = m_buffer; } template < typename T > XBeeApiCmdAtRemoteSet<T>::~XBeeApiCmdAtRemoteSet() { } XBeeApiCmdAtRemoteReq::XBeeApiCmdAtRemoteReq( const uint8_t p_frameId, const uint16_t p_addr16Bit, const uint64_t p_addr64Bit, const XBeeDevice::XBeeApiAddrType_t p_type, const uint8_t* const p_data ) { m_apiId = XBEE_CMD_REMOTE_AT_CMD; m_buffer[0] = p_frameId; writeAddressToBuffer( &(m_buffer[1]), p_addr16Bit, p_addr64Bit, p_type ); m_buffer[11] = 0; m_buffer[12] = p_data[0]; m_buffer[13] = p_data[1]; m_dataLen = sizeof( m_buffer ); m_data = m_buffer; } XBeeApiCmdAtRemoteReq::~XBeeApiCmdAtRemoteReq() { }