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:
Wed Apr 02 21:32:25 2014 +0000
Revision:
38:291aee9eb900
Parent:
27:6356ef5fe39b
Child:
46:a1abbc66f157
Sync from https://github.com/bright-tools/xbeeapi/commit/10fb465f9b314dcf88e007bd3f33af856ab70c8f

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