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:
Tue Feb 04 23:52:50 2014 +0000
Revision:
16:8095c43a2a6e
Parent:
15:ff9f12e38f44
Child:
27:6356ef5fe39b
Flesh out XBeeApiTXFrame and add XBeeApiTxFrameEx

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