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 21:55:28 2014 +0000
Revision:
46:a1abbc66f157
Parent:
44:85a66d56e176
Parent:
38:291aee9eb900
Child:
47:5d3608835668
Merge back in branch which was accidentally created.

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 9:ba90e9efd68b 33 }
johnb 12:58319a467943 34
johnb 12:58319a467943 35
johnb 12:58319a467943 36 XBeeApiTxFrame::~XBeeApiTxFrame( void )
johnb 12:58319a467943 37 {
johnb 12:58319a467943 38 }
johnb 12:58319a467943 39
johnb 16:8095c43a2a6e 40 uint8_t XBeeApiTxFrame::getFrameId( void ) const
johnb 16:8095c43a2a6e 41 {
johnb 16:8095c43a2a6e 42 return 'T';
johnb 16:8095c43a2a6e 43 }
johnb 16:8095c43a2a6e 44
johnb 16:8095c43a2a6e 45 uint16_t XBeeApiTxFrame::getCmdLen( void ) const
johnb 16:8095c43a2a6e 46 {
johnb 16:8095c43a2a6e 47 /* Length of the data payload plus the API ID, frame ID and option byte */
johnb 16:8095c43a2a6e 48 uint16_t ret_val = m_dataLen + 3U;
johnb 16:8095c43a2a6e 49
johnb 16:8095c43a2a6e 50 if( m_apiId == XBEE_CMD_TX_16B_ADDR )
johnb 16:8095c43a2a6e 51 {
johnb 16:8095c43a2a6e 52 ret_val += 2U;
johnb 16:8095c43a2a6e 53 }
johnb 16:8095c43a2a6e 54 else
johnb 16:8095c43a2a6e 55 {
johnb 16:8095c43a2a6e 56 ret_val += 8U;
johnb 16:8095c43a2a6e 57 }
johnb 16:8095c43a2a6e 58
johnb 16:8095c43a2a6e 59 return ret_val;
johnb 16:8095c43a2a6e 60 }
johnb 16:8095c43a2a6e 61
johnb 16:8095c43a2a6e 62
johnb 16:8095c43a2a6e 63 void XBeeApiTxFrame::getDataPtr( const uint16_t p_start, const uint8_t** p_buff, uint16_t* const p_len )
johnb 12:58319a467943 64 {
johnb 13:302e7c1ea0b3 65 if( p_start == 0 )
johnb 13:302e7c1ea0b3 66 {
johnb 16:8095c43a2a6e 67 /* Need to keep the XBEE_API_TX_FRAME_BUFFER_SIZE limit in mind when writing to m_buffer */
johnb 16:8095c43a2a6e 68
johnb 16:8095c43a2a6e 69 uint8_t len = 0;
johnb 16:8095c43a2a6e 70 m_buffer[ len++ ] = getFrameId(); // Frame ID
johnb 16:8095c43a2a6e 71
johnb 16:8095c43a2a6e 72 /* Pack the destination address depending on whether it's 16 or 64-bit addressed */
johnb 16:8095c43a2a6e 73 if( m_apiId == XBEE_CMD_TX_16B_ADDR )
johnb 16:8095c43a2a6e 74 {
johnb 16:8095c43a2a6e 75 m_buffer[ len++ ] = m_addr >> 8U;
johnb 16:8095c43a2a6e 76 m_buffer[ len++ ] = m_addr;
johnb 16:8095c43a2a6e 77 }
johnb 16:8095c43a2a6e 78 else
johnb 16:8095c43a2a6e 79 {
johnb 16:8095c43a2a6e 80 m_buffer[ len++ ] = m_addr >> 56U;
johnb 16:8095c43a2a6e 81 m_buffer[ len++ ] = m_addr >> 48U;
johnb 16:8095c43a2a6e 82 m_buffer[ len++ ] = m_addr >> 32U;
johnb 16:8095c43a2a6e 83 m_buffer[ len++ ] = m_addr >> 24U;
johnb 16:8095c43a2a6e 84 m_buffer[ len++ ] = m_addr >> 16U;
johnb 16:8095c43a2a6e 85 m_buffer[ len++ ] = m_addr >> 8U;
johnb 16:8095c43a2a6e 86 m_buffer[ len++ ] = m_addr;
johnb 16:8095c43a2a6e 87 }
johnb 16:8095c43a2a6e 88
johnb 16:8095c43a2a6e 89 /* Frame options */
johnb 16:8095c43a2a6e 90 m_buffer[ len++ ] = m_ack? (0x00U):(0x01U)
johnb 16:8095c43a2a6e 91 | m_panBroadcast?(0x04U):(0x00U);
johnb 16:8095c43a2a6e 92
johnb 13:302e7c1ea0b3 93 *p_buff = &( m_buffer[0] );
johnb 16:8095c43a2a6e 94 *p_len = len;
johnb 16:8095c43a2a6e 95 }
johnb 16:8095c43a2a6e 96 else
johnb 16:8095c43a2a6e 97 {
johnb 16:8095c43a2a6e 98 *p_buff = m_data;
johnb 16:8095c43a2a6e 99 *p_len = m_dataLen;
johnb 13:302e7c1ea0b3 100 }
johnb 12:58319a467943 101 }
johnb 12:58319a467943 102
johnb 16:8095c43a2a6e 103 bool XBeeApiTxFrame::setDataPtr( const uint8_t* const p_buff, const uint16_t p_len )
johnb 16:8095c43a2a6e 104 {
johnb 16:8095c43a2a6e 105 bool ret_val = false;
johnb 16:8095c43a2a6e 106 if( p_len < XBEE_API_MAX_TX_PAYLOAD_LEN )
johnb 16:8095c43a2a6e 107 {
johnb 16:8095c43a2a6e 108 m_data = p_buff;
johnb 16:8095c43a2a6e 109 m_dataLen = p_len;
johnb 38:291aee9eb900 110 ret_val = true;
johnb 16:8095c43a2a6e 111 }
johnb 16:8095c43a2a6e 112 return ret_val;
johnb 16:8095c43a2a6e 113 }
johnb 12:58319a467943 114
johnb 12:58319a467943 115 void XBeeApiTxFrame::setDestAddrType( const XBeeDevice::XBeeApiAddrType_t p_type )
johnb 12:58319a467943 116 {
johnb 12:58319a467943 117 if( p_type == XBeeDevice::XBEE_API_ADDR_TYPE_16BIT )
johnb 12:58319a467943 118 {
johnb 12:58319a467943 119 m_apiId = XBEE_CMD_TX_16B_ADDR;
johnb 12:58319a467943 120 }
johnb 12:58319a467943 121 else
johnb 12:58319a467943 122 {
johnb 12:58319a467943 123 m_apiId = XBEE_CMD_TX_64B_ADDR;
johnb 12:58319a467943 124 }
johnb 12:58319a467943 125 }
johnb 9:ba90e9efd68b 126
johnb 9:ba90e9efd68b 127 void XBeeApiTxFrame::setDestAddr( uint64_t p_addr )
johnb 9:ba90e9efd68b 128 {
johnb 9:ba90e9efd68b 129 m_addr = p_addr;
johnb 9:ba90e9efd68b 130 }
johnb 9:ba90e9efd68b 131
johnb 9:ba90e9efd68b 132 void XBeeApiTxFrame::setDestAddrBroadcast( void )
johnb 9:ba90e9efd68b 133 {
johnb 27:6356ef5fe39b 134 m_addr = XBEE_BROADCAST_ADDR;
johnb 9:ba90e9efd68b 135 }
johnb 9:ba90e9efd68b 136
johnb 9:ba90e9efd68b 137 void XBeeApiTxFrame::setPanBroadcast( const bool p_bc )
johnb 9:ba90e9efd68b 138 {
johnb 9:ba90e9efd68b 139 m_panBroadcast = p_bc;
johnb 9:ba90e9efd68b 140 }
johnb 9:ba90e9efd68b 141
johnb 9:ba90e9efd68b 142 bool XBeeApiTxFrame::decodeCallback( const uint8_t* const p_data, size_t p_len )
johnb 9:ba90e9efd68b 143 {
johnb 9:ba90e9efd68b 144 bool ret_val = false;
johnb 16:8095c43a2a6e 145
johnb 9:ba90e9efd68b 146 if( XBEE_CMD_TX_STATUS == p_data[ XBEE_CMD_POSN_API_ID ] )
johnb 9:ba90e9efd68b 147 {
johnb 15:ff9f12e38f44 148 /* Data transmitted call-back */
johnb 16:8095c43a2a6e 149 frameTxCallback( (XBeeApiTxStatus_e)(p_data[ XBEE_CMD_POSN_ID_SPECIFIC_DATA + 1U ]) );
johnb 9:ba90e9efd68b 150 ret_val = true;
johnb 9:ba90e9efd68b 151 }
johnb 9:ba90e9efd68b 152
johnb 9:ba90e9efd68b 153 return ret_val;
johnb 9:ba90e9efd68b 154 }
johnb 15:ff9f12e38f44 155
johnb 15:ff9f12e38f44 156 void XBeeApiTxFrame::frameTxCallback( const XBeeApiTxStatus_e p_status )
johnb 15:ff9f12e38f44 157 {
johnb 16:8095c43a2a6e 158 /* TODO */
johnb 38:291aee9eb900 159 }