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 06 22:10:28 2014 +0000
Revision:
47:5d3608835668
Parent:
46:a1abbc66f157
Re-jig XBeeApiTxFrame to allow getDataPtr to be a const method again.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 9:ba90e9efd68b 1 /**
johnb 9:ba90e9efd68b 2
johnb 9:ba90e9efd68b 3 Copyright 2014 John Bailey
johnb 9:ba90e9efd68b 4
johnb 9:ba90e9efd68b 5 Licensed under the Apache License, Version 2.0 (the "License");
johnb 9:ba90e9efd68b 6 you may not use this file except in compliance with the License.
johnb 9:ba90e9efd68b 7 You may obtain a copy of the License at
johnb 9:ba90e9efd68b 8
johnb 9:ba90e9efd68b 9 http://www.apache.org/licenses/LICENSE-2.0
johnb 9:ba90e9efd68b 10
johnb 9:ba90e9efd68b 11 Unless required by applicable law or agreed to in writing, software
johnb 9:ba90e9efd68b 12 distributed under the License is distributed on an "AS IS" BASIS,
johnb 9:ba90e9efd68b 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 9:ba90e9efd68b 14 See the License for the specific language governing permissions and
johnb 9:ba90e9efd68b 15 limitations under the License.
johnb 9:ba90e9efd68b 16
johnb 9:ba90e9efd68b 17 */
johnb 9:ba90e9efd68b 18
johnb 9:ba90e9efd68b 19 #include "XBeeApiTxFrame.hpp"
johnb 9:ba90e9efd68b 20
johnb 44:85a66d56e176 21 XBeeApiTxFrame::XBeeApiTxFrame( XBeeDevice* p_device,
johnb 44:85a66d56e176 22 const uint8_t* p_data,
johnb 44:85a66d56e176 23 const size_t p_dataLen,
johnb 44:85a66d56e176 24 const uint64_t p_addr,
johnb 44:85a66d56e176 25 const XBeeDevice::XBeeApiAddrType_t p_type ) :
johnb 44:85a66d56e176 26 XBeeApiFrame(), XBeeApiFrameDecoder( p_device ),
johnb 44:85a66d56e176 27 m_addr( p_addr ),
johnb 12:58319a467943 28 m_ack( true ),
johnb 12:58319a467943 29 m_panBroadcast( false )
johnb 9:ba90e9efd68b 30 {
johnb 44:85a66d56e176 31 setDestAddrType( p_type );
johnb 44:85a66d56e176 32 setDataPtr( p_data, p_dataLen );
johnb 47:5d3608835668 33
johnb 47:5d3608835668 34 updateBuffer();
johnb 9:ba90e9efd68b 35 }
johnb 12:58319a467943 36
johnb 47:5d3608835668 37 void XBeeApiTxFrame::updateBuffer( void )
johnb 47:5d3608835668 38 {
johnb 47:5d3608835668 39 /* TODO - could optimise this so that it doesn't re-write the entire buffer every time */
johnb 47:5d3608835668 40
johnb 47:5d3608835668 41 /* Need to keep the XBEE_API_TX_FRAME_BUFFER_SIZE limit in mind when writing to m_buffer */
johnb 47:5d3608835668 42
johnb 47:5d3608835668 43 uint8_t len = 0;
johnb 47:5d3608835668 44
johnb 47:5d3608835668 45 m_buffer[ len++ ] = getFrameId(); // Frame ID
johnb 47:5d3608835668 46
johnb 47:5d3608835668 47 /* Pack the destination address depending on whether it's 16 or 64-bit addressed */
johnb 47:5d3608835668 48 if( m_apiId == XBEE_CMD_TX_16B_ADDR )
johnb 47:5d3608835668 49 {
johnb 47:5d3608835668 50 m_buffer[ len++ ] = m_addr >> 8U;
johnb 47:5d3608835668 51 m_buffer[ len++ ] = m_addr;
johnb 47:5d3608835668 52 }
johnb 47:5d3608835668 53 else
johnb 47:5d3608835668 54 {
johnb 47:5d3608835668 55 m_buffer[ len++ ] = m_addr >> 56U;
johnb 47:5d3608835668 56 m_buffer[ len++ ] = m_addr >> 48U;
johnb 47:5d3608835668 57 m_buffer[ len++ ] = m_addr >> 32U;
johnb 47:5d3608835668 58 m_buffer[ len++ ] = m_addr >> 24U;
johnb 47:5d3608835668 59 m_buffer[ len++ ] = m_addr >> 16U;
johnb 47:5d3608835668 60 m_buffer[ len++ ] = m_addr >> 8U;
johnb 47:5d3608835668 61 m_buffer[ len++ ] = m_addr;
johnb 47:5d3608835668 62 }
johnb 47:5d3608835668 63
johnb 47:5d3608835668 64 /* Frame options */
johnb 47:5d3608835668 65 m_buffer[ len++ ] = m_ack? (0x00U):(0x01U)
johnb 47:5d3608835668 66 | m_panBroadcast?(0x04U):(0x00U);
johnb 47:5d3608835668 67
johnb 47:5d3608835668 68 m_bufferLen = len;
johnb 47:5d3608835668 69 }
johnb 12:58319a467943 70
johnb 12:58319a467943 71 XBeeApiTxFrame::~XBeeApiTxFrame( void )
johnb 12:58319a467943 72 {
johnb 12:58319a467943 73 }
johnb 12:58319a467943 74
johnb 16:8095c43a2a6e 75 uint8_t XBeeApiTxFrame::getFrameId( void ) const
johnb 16:8095c43a2a6e 76 {
johnb 16:8095c43a2a6e 77 return 'T';
johnb 16:8095c43a2a6e 78 }
johnb 16:8095c43a2a6e 79
johnb 16:8095c43a2a6e 80 uint16_t XBeeApiTxFrame::getCmdLen( void ) const
johnb 16:8095c43a2a6e 81 {
johnb 16:8095c43a2a6e 82 /* Length of the data payload plus the API ID, frame ID and option byte */
johnb 16:8095c43a2a6e 83 uint16_t ret_val = m_dataLen + 3U;
johnb 16:8095c43a2a6e 84
johnb 16:8095c43a2a6e 85 if( m_apiId == XBEE_CMD_TX_16B_ADDR )
johnb 16:8095c43a2a6e 86 {
johnb 16:8095c43a2a6e 87 ret_val += 2U;
johnb 16:8095c43a2a6e 88 }
johnb 16:8095c43a2a6e 89 else
johnb 16:8095c43a2a6e 90 {
johnb 16:8095c43a2a6e 91 ret_val += 8U;
johnb 16:8095c43a2a6e 92 }
johnb 16:8095c43a2a6e 93
johnb 16:8095c43a2a6e 94 return ret_val;
johnb 16:8095c43a2a6e 95 }
johnb 16:8095c43a2a6e 96
johnb 16:8095c43a2a6e 97
johnb 47:5d3608835668 98 void XBeeApiTxFrame::getDataPtr( const uint16_t p_start, const uint8_t** p_buff, uint16_t* const p_len ) const
johnb 12:58319a467943 99 {
johnb 13:302e7c1ea0b3 100 if( p_start == 0 )
johnb 13:302e7c1ea0b3 101 {
johnb 13:302e7c1ea0b3 102 *p_buff = &( m_buffer[0] );
johnb 47:5d3608835668 103 *p_len = m_bufferLen;
johnb 16:8095c43a2a6e 104 }
johnb 16:8095c43a2a6e 105 else
johnb 16:8095c43a2a6e 106 {
johnb 16:8095c43a2a6e 107 *p_buff = m_data;
johnb 16:8095c43a2a6e 108 *p_len = m_dataLen;
johnb 13:302e7c1ea0b3 109 }
johnb 12:58319a467943 110 }
johnb 12:58319a467943 111
johnb 16:8095c43a2a6e 112 bool XBeeApiTxFrame::setDataPtr( const uint8_t* const p_buff, const uint16_t p_len )
johnb 16:8095c43a2a6e 113 {
johnb 16:8095c43a2a6e 114 bool ret_val = false;
johnb 16:8095c43a2a6e 115 if( p_len < XBEE_API_MAX_TX_PAYLOAD_LEN )
johnb 16:8095c43a2a6e 116 {
johnb 16:8095c43a2a6e 117 m_data = p_buff;
johnb 16:8095c43a2a6e 118 m_dataLen = p_len;
johnb 38:291aee9eb900 119 ret_val = true;
johnb 16:8095c43a2a6e 120 }
johnb 16:8095c43a2a6e 121 return ret_val;
johnb 16:8095c43a2a6e 122 }
johnb 12:58319a467943 123
johnb 12:58319a467943 124 void XBeeApiTxFrame::setDestAddrType( const XBeeDevice::XBeeApiAddrType_t p_type )
johnb 12:58319a467943 125 {
johnb 12:58319a467943 126 if( p_type == XBeeDevice::XBEE_API_ADDR_TYPE_16BIT )
johnb 12:58319a467943 127 {
johnb 12:58319a467943 128 m_apiId = XBEE_CMD_TX_16B_ADDR;
johnb 12:58319a467943 129 }
johnb 12:58319a467943 130 else
johnb 12:58319a467943 131 {
johnb 12:58319a467943 132 m_apiId = XBEE_CMD_TX_64B_ADDR;
johnb 12:58319a467943 133 }
johnb 47:5d3608835668 134 updateBuffer();
johnb 12:58319a467943 135 }
johnb 9:ba90e9efd68b 136
johnb 9:ba90e9efd68b 137 void XBeeApiTxFrame::setDestAddr( uint64_t p_addr )
johnb 9:ba90e9efd68b 138 {
johnb 9:ba90e9efd68b 139 m_addr = p_addr;
johnb 47:5d3608835668 140 updateBuffer();
johnb 9:ba90e9efd68b 141 }
johnb 9:ba90e9efd68b 142
johnb 9:ba90e9efd68b 143 void XBeeApiTxFrame::setDestAddrBroadcast( void )
johnb 9:ba90e9efd68b 144 {
johnb 27:6356ef5fe39b 145 m_addr = XBEE_BROADCAST_ADDR;
johnb 47:5d3608835668 146 updateBuffer();
johnb 9:ba90e9efd68b 147 }
johnb 9:ba90e9efd68b 148
johnb 9:ba90e9efd68b 149 void XBeeApiTxFrame::setPanBroadcast( const bool p_bc )
johnb 9:ba90e9efd68b 150 {
johnb 9:ba90e9efd68b 151 m_panBroadcast = p_bc;
johnb 47:5d3608835668 152 updateBuffer();
johnb 9:ba90e9efd68b 153 }
johnb 9:ba90e9efd68b 154
johnb 9:ba90e9efd68b 155 bool XBeeApiTxFrame::decodeCallback( const uint8_t* const p_data, size_t p_len )
johnb 9:ba90e9efd68b 156 {
johnb 9:ba90e9efd68b 157 bool ret_val = false;
johnb 16:8095c43a2a6e 158
johnb 9:ba90e9efd68b 159 if( XBEE_CMD_TX_STATUS == p_data[ XBEE_CMD_POSN_API_ID ] )
johnb 9:ba90e9efd68b 160 {
johnb 15:ff9f12e38f44 161 /* Data transmitted call-back */
johnb 16:8095c43a2a6e 162 frameTxCallback( (XBeeApiTxStatus_e)(p_data[ XBEE_CMD_POSN_ID_SPECIFIC_DATA + 1U ]) );
johnb 9:ba90e9efd68b 163 ret_val = true;
johnb 9:ba90e9efd68b 164 }
johnb 9:ba90e9efd68b 165
johnb 9:ba90e9efd68b 166 return ret_val;
johnb 9:ba90e9efd68b 167 }
johnb 15:ff9f12e38f44 168
johnb 15:ff9f12e38f44 169 void XBeeApiTxFrame::frameTxCallback( const XBeeApiTxStatus_e p_status )
johnb 15:ff9f12e38f44 170 {
johnb 16:8095c43a2a6e 171 /* TODO */
johnb 38:291aee9eb900 172 }