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 Jan 29 20:19:59 2014 +0000
Revision:
5:b40a6fd3a334
Child:
6:3cb62daace78
Documentation update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 5:b40a6fd3a334 1 /**
johnb 5:b40a6fd3a334 2
johnb 5:b40a6fd3a334 3 Copyright 2014 John Bailey
johnb 5:b40a6fd3a334 4
johnb 5:b40a6fd3a334 5 Licensed under the Apache License, Version 2.0 (the "License");
johnb 5:b40a6fd3a334 6 you may not use this file except in compliance with the License.
johnb 5:b40a6fd3a334 7 You may obtain a copy of the License at
johnb 5:b40a6fd3a334 8
johnb 5:b40a6fd3a334 9 http://www.apache.org/licenses/LICENSE-2.0
johnb 5:b40a6fd3a334 10
johnb 5:b40a6fd3a334 11 Unless required by applicable law or agreed to in writing, software
johnb 5:b40a6fd3a334 12 distributed under the License is distributed on an "AS IS" BASIS,
johnb 5:b40a6fd3a334 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 5:b40a6fd3a334 14 See the License for the specific language governing permissions and
johnb 5:b40a6fd3a334 15 limitations under the License.
johnb 5:b40a6fd3a334 16
johnb 5:b40a6fd3a334 17 */
johnb 5:b40a6fd3a334 18
johnb 5:b40a6fd3a334 19 #include "XBeeApiCmdAt.hpp"
johnb 5:b40a6fd3a334 20
johnb 5:b40a6fd3a334 21 /* Set of Frame ID codes for the various commands (see XBEE_CMD_POSN_FRAME_ID) */
johnb 5:b40a6fd3a334 22
johnb 5:b40a6fd3a334 23 #define CMD_RESPONSE_GET_VR '1'
johnb 5:b40a6fd3a334 24 #define CMD_RESPONSE_GET_HV '2'
johnb 5:b40a6fd3a334 25 #define CMD_RESPONSE_GET_CH '3'
johnb 5:b40a6fd3a334 26 #define CMD_RESPONSE_SET_CH '4'
johnb 5:b40a6fd3a334 27
johnb 5:b40a6fd3a334 28 /* Content for the various commands - value of 0 indicates a value to be populated (i.e. variable) */
johnb 5:b40a6fd3a334 29
johnb 5:b40a6fd3a334 30 const uint8_t cmd_vr[] = { CMD_RESPONSE_GET_VR, 'V', 'R' };
johnb 5:b40a6fd3a334 31 const uint8_t cmd_hv[] = { CMD_RESPONSE_GET_HV, 'H', 'V' };
johnb 5:b40a6fd3a334 32 const uint8_t cmd_ch[] = { CMD_RESPONSE_GET_CH, 'C', 'H' };
johnb 5:b40a6fd3a334 33 const uint8_t cmd_set_ch[] = { CMD_RESPONSE_SET_CH, 'C', 'H', 0 };
johnb 5:b40a6fd3a334 34
johnb 5:b40a6fd3a334 35 #define XBEE_CMD_POSN_FRAME_ID (4U)
johnb 5:b40a6fd3a334 36 #define XBEE_CMD_POSN_PARAM_START (8U)
johnb 5:b40a6fd3a334 37
johnb 5:b40a6fd3a334 38 XBeeApiCmdAt::XBeeApiCmdAt() : XBeeApiFrameDecoder( ) , m_haveHwVer( false ),
johnb 5:b40a6fd3a334 39 m_haveFwVer( false ),
johnb 5:b40a6fd3a334 40 m_haveChan( false )
johnb 5:b40a6fd3a334 41 {
johnb 5:b40a6fd3a334 42 }
johnb 5:b40a6fd3a334 43
johnb 5:b40a6fd3a334 44 bool XBeeApiCmdAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
johnb 5:b40a6fd3a334 45 {
johnb 5:b40a6fd3a334 46 bool ret_val = false;
johnb 5:b40a6fd3a334 47
johnb 5:b40a6fd3a334 48 if( XBEE_CMD_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] )
johnb 5:b40a6fd3a334 49 {
johnb 5:b40a6fd3a334 50
johnb 5:b40a6fd3a334 51 switch( p_data[ XBEE_CMD_POSN_FRAME_ID ] )
johnb 5:b40a6fd3a334 52 {
johnb 5:b40a6fd3a334 53 case CMD_RESPONSE_GET_HV:
johnb 5:b40a6fd3a334 54 m_haveHwVer = true;
johnb 5:b40a6fd3a334 55 // TODO: Is this right?
johnb 5:b40a6fd3a334 56 m_hwVer = ((uint16_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 8) + p_data[ XBEE_CMD_POSN_PARAM_START + 1 ];
johnb 5:b40a6fd3a334 57 ret_val = true;
johnb 5:b40a6fd3a334 58 break;
johnb 5:b40a6fd3a334 59
johnb 5:b40a6fd3a334 60 case CMD_RESPONSE_GET_VR:
johnb 5:b40a6fd3a334 61 m_haveFwVer = true;
johnb 5:b40a6fd3a334 62 // TODO: Is this right?
johnb 5:b40a6fd3a334 63 m_fwVer = ((uint16_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 8) + p_data[ XBEE_CMD_POSN_PARAM_START + 1 ];
johnb 5:b40a6fd3a334 64 ret_val = true;
johnb 5:b40a6fd3a334 65 break;
johnb 5:b40a6fd3a334 66
johnb 5:b40a6fd3a334 67 case CMD_RESPONSE_GET_CH:
johnb 5:b40a6fd3a334 68 m_haveChan = true;
johnb 5:b40a6fd3a334 69 m_chan = p_data[ XBEE_CMD_POSN_PARAM_START ];
johnb 5:b40a6fd3a334 70 ret_val = true;
johnb 5:b40a6fd3a334 71 break;
johnb 5:b40a6fd3a334 72
johnb 5:b40a6fd3a334 73 case CMD_RESPONSE_SET_CH:
johnb 5:b40a6fd3a334 74 m_chan = m_chanPend;
johnb 5:b40a6fd3a334 75 m_haveChan = true;
johnb 5:b40a6fd3a334 76 ret_val = true;
johnb 5:b40a6fd3a334 77 break;
johnb 5:b40a6fd3a334 78 }
johnb 5:b40a6fd3a334 79 }
johnb 5:b40a6fd3a334 80 return ret_val;
johnb 5:b40a6fd3a334 81 }
johnb 5:b40a6fd3a334 82
johnb 5:b40a6fd3a334 83 bool XBeeApiCmdAt::setChannel( uint8_t const p_chan )
johnb 5:b40a6fd3a334 84 {
johnb 5:b40a6fd3a334 85 XBeeApiCmdAt::XBeeApiCmdAtChannelSet req( p_chan );
johnb 5:b40a6fd3a334 86 m_chanPend = p_chan;
johnb 5:b40a6fd3a334 87 m_device->SendFrame( &req );
johnb 5:b40a6fd3a334 88
johnb 5:b40a6fd3a334 89 return true;
johnb 5:b40a6fd3a334 90 }
johnb 5:b40a6fd3a334 91
johnb 5:b40a6fd3a334 92 void XBeeApiCmdAt::requestHardwareVersion( void )
johnb 5:b40a6fd3a334 93 {
johnb 5:b40a6fd3a334 94 XBeeApiCmdAt::XBeeApiCmdAtHardwareVersionRequest req;
johnb 5:b40a6fd3a334 95 m_haveHwVer = false;
johnb 5:b40a6fd3a334 96 m_device->SendFrame( &req );
johnb 5:b40a6fd3a334 97 }
johnb 5:b40a6fd3a334 98
johnb 5:b40a6fd3a334 99 void XBeeApiCmdAt::requestFirmwareVersion( void )
johnb 5:b40a6fd3a334 100 {
johnb 5:b40a6fd3a334 101 XBeeApiCmdAt::XBeeApiCmdAtFirmwareVersionRequest req;
johnb 5:b40a6fd3a334 102 m_haveFwVer = false;
johnb 5:b40a6fd3a334 103 m_device->SendFrame( &req );
johnb 5:b40a6fd3a334 104 }
johnb 5:b40a6fd3a334 105
johnb 5:b40a6fd3a334 106 void XBeeApiCmdAt::requestChannel( void )
johnb 5:b40a6fd3a334 107 {
johnb 5:b40a6fd3a334 108 XBeeApiCmdAt::XBeeApiCmdAtChannelRequest req;
johnb 5:b40a6fd3a334 109 m_haveChan = false;
johnb 5:b40a6fd3a334 110 m_device->SendFrame( &req );
johnb 5:b40a6fd3a334 111 }
johnb 5:b40a6fd3a334 112
johnb 5:b40a6fd3a334 113 bool XBeeApiCmdAt::getFirmwareVersion( uint16_t* const p_ver ) const
johnb 5:b40a6fd3a334 114 {
johnb 5:b40a6fd3a334 115 if( m_haveFwVer )
johnb 5:b40a6fd3a334 116 {
johnb 5:b40a6fd3a334 117 *p_ver = m_fwVer;
johnb 5:b40a6fd3a334 118 }
johnb 5:b40a6fd3a334 119 return m_haveFwVer;
johnb 5:b40a6fd3a334 120
johnb 5:b40a6fd3a334 121 }
johnb 5:b40a6fd3a334 122
johnb 5:b40a6fd3a334 123 bool XBeeApiCmdAt::getHardwareVersion( uint16_t* const p_ver ) const
johnb 5:b40a6fd3a334 124 {
johnb 5:b40a6fd3a334 125 if( m_haveHwVer )
johnb 5:b40a6fd3a334 126 {
johnb 5:b40a6fd3a334 127 *p_ver = m_hwVer;
johnb 5:b40a6fd3a334 128 }
johnb 5:b40a6fd3a334 129 return m_haveHwVer;
johnb 5:b40a6fd3a334 130 }
johnb 5:b40a6fd3a334 131
johnb 5:b40a6fd3a334 132 bool XBeeApiCmdAt::getChannel( uint8_t* const p_chan ) const
johnb 5:b40a6fd3a334 133 {
johnb 5:b40a6fd3a334 134 if( m_haveChan )
johnb 5:b40a6fd3a334 135 {
johnb 5:b40a6fd3a334 136 *p_chan = m_chan;
johnb 5:b40a6fd3a334 137 }
johnb 5:b40a6fd3a334 138 return m_haveChan;
johnb 5:b40a6fd3a334 139 }
johnb 5:b40a6fd3a334 140
johnb 5:b40a6fd3a334 141
johnb 5:b40a6fd3a334 142 XBeeApiCmdAt::XBeeApiCmdAtFirmwareVersionRequest::XBeeApiCmdAtFirmwareVersionRequest( void ) : XBeeApiFrame( )
johnb 5:b40a6fd3a334 143 {
johnb 5:b40a6fd3a334 144 m_apiId = XBEE_CMD_AT_CMD;
johnb 5:b40a6fd3a334 145 m_data = cmd_vr;
johnb 5:b40a6fd3a334 146 m_dataLen = sizeof( cmd_vr );
johnb 5:b40a6fd3a334 147 }
johnb 5:b40a6fd3a334 148
johnb 5:b40a6fd3a334 149 XBeeApiCmdAt::XBeeApiCmdAtHardwareVersionRequest::XBeeApiCmdAtHardwareVersionRequest( void ) : XBeeApiFrame( )
johnb 5:b40a6fd3a334 150 {
johnb 5:b40a6fd3a334 151 m_apiId = XBEE_CMD_AT_CMD;
johnb 5:b40a6fd3a334 152 m_data = cmd_hv;
johnb 5:b40a6fd3a334 153 m_dataLen = sizeof( cmd_hv );
johnb 5:b40a6fd3a334 154 }
johnb 5:b40a6fd3a334 155
johnb 5:b40a6fd3a334 156 XBeeApiCmdAt::XBeeApiCmdAtChannelRequest::XBeeApiCmdAtChannelRequest( void ) : XBeeApiFrame( )
johnb 5:b40a6fd3a334 157 {
johnb 5:b40a6fd3a334 158 m_apiId = XBEE_CMD_AT_CMD;
johnb 5:b40a6fd3a334 159 m_data = cmd_ch;
johnb 5:b40a6fd3a334 160 m_dataLen = sizeof( cmd_ch );
johnb 5:b40a6fd3a334 161 }
johnb 5:b40a6fd3a334 162
johnb 5:b40a6fd3a334 163 XBeeApiCmdAt::XBeeApiCmdAtChannelSet::XBeeApiCmdAtChannelSet( const uint8_t p_chan ) : XBeeApiFrame( )
johnb 5:b40a6fd3a334 164 {
johnb 5:b40a6fd3a334 165 m_apiId = XBEE_CMD_AT_CMD;
johnb 5:b40a6fd3a334 166 m_buffer[0] = cmd_set_ch[0];
johnb 5:b40a6fd3a334 167 m_buffer[1] = cmd_set_ch[1];
johnb 5:b40a6fd3a334 168 m_buffer[2] = cmd_set_ch[2];
johnb 5:b40a6fd3a334 169 m_buffer[3] = p_chan;
johnb 5:b40a6fd3a334 170 m_data = m_buffer;
johnb 5:b40a6fd3a334 171 m_dataLen = sizeof( cmd_set_ch );
johnb 5:b40a6fd3a334 172 }