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.

Revision:
31:c144106e55b5
Parent:
30:9532b01a1ae1
Child:
32:af4e495afd62
diff -r 9532b01a1ae1 -r c144106e55b5 Utility/XBeeApiCmdAt.cpp
--- a/Utility/XBeeApiCmdAt.cpp	Thu Feb 06 21:26:48 2014 +0000
+++ b/Utility/XBeeApiCmdAt.cpp	Thu Feb 06 22:26:27 2014 +0000
@@ -30,6 +30,8 @@
 #define CMD_RESPONSE_GET_EDA '8'
 #define CMD_RESPONSE_SET_PID '9'
 #define CMD_RESPONSE_GET_PID '0'
+#define CMD_RESPONSE_SET_MY  'a'
+#define CMD_RESPONSE_GET_MY  'b'
 
 /** Lowest channel supported by the XBee S1 */
 #define XBEE_CHAN_MIN 0x0b
@@ -58,6 +60,10 @@
 const uint8_t cmd_pid[] =     { CMD_RESPONSE_GET_PID, 'I', 'D' };
 const uint8_t cmd_set_pid[] = { CMD_RESPONSE_SET_PID, 'I', 'D', 0, 0 };
 
+const uint8_t cmd_my[] =      { CMD_RESPONSE_GET_MY,  'M', 'Y' };
+const uint8_t cmd_set_my[] =  { CMD_RESPONSE_SET_MY,  'M', 'Y', 0, 0 };
+
+
 #define XBEE_CMD_POSN_FRAME_ID (4U)
 #define XBEE_CMD_POSN_STATUS (7U)
 #define XBEE_CMD_POSN_PARAM_START (8U)
@@ -65,12 +71,13 @@
 #define XBEE_CMD_RESPONS_HAS_DATA( _p_len ) ((_p_len > ( XBEE_CMD_POSN_PARAM_START + 1 ))
 
 XBeeApiCmdAt::XBeeApiCmdAt( XBeeDevice* const p_device ) : XBeeApiFrameDecoder( p_device ) , 
-    m_haveHwVer( false ),
-    m_haveFwVer( false ),
-    m_haveChan( false ),
-    m_havePANId( false ),
-    m_haveEDA( false ),
-    m_haveCE( false )
+    m_have_hwVer( false ),
+    m_have_fwVer( false ),
+    m_have_chan( false ),
+    m_have_PANId( false ),
+    m_have_EDA( false ),
+    m_have_CE( false ),
+    m_have_sourceAddress( false )
 {
 }
 
@@ -85,11 +92,11 @@
                 if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) 
                 {
                     m_hwVer = ((uint16_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 8) + p_data[ XBEE_CMD_POSN_PARAM_START + 1 ];
-                    m_haveHwVer = true;
+                    m_have_hwVer = true;
                 } 
                 else 
                 {
-                    pc.printf("\r\nERROR1\r\n");    
+                    /* TODO */
                 }
                 ret_val = true;
                 break;
@@ -98,11 +105,11 @@
                 if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) 
                 {
                     m_fwVer = ((uint16_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 8) + p_data[ XBEE_CMD_POSN_PARAM_START + 1 ];
-                    m_haveFwVer = true;
+                    m_have_fwVer = true;
                 } 
                 else 
                 {
-                    pc.printf("\r\nERROR2\r\n");    
+                    /* TODO */
                 }
                 ret_val = true;
                 break;
@@ -123,12 +130,11 @@
                     printf("\r\n%02x (%2d) - %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n",p_data[ XBEE_CMD_POSN_API_ID ],p_len,
                         p_data[0],p_data[1],p_data[2],p_data[3],p_data[4],p_data[5],p_data[6],p_data[7],p_data[8]);
                     #endif
-                    m_haveChan = true;
+                    m_have_chan = true;
                 } 
                 else 
                 {
                     /* TODO */
-                    pc.printf("\r\nERROR3\r\n");    
                 }
                 ret_val = true;
                 break;
@@ -145,11 +151,11 @@
                     {
                         m_CE = m_CEPend;
                     }
-                    m_haveCE = true;
+                    m_have_CE = true;
                 } 
                 else 
                 {
-                    pc.printf("\r\nERROR3\r\n");    
+                    /* TODO */
                 }
                 ret_val = true;
                 break;
@@ -166,11 +172,11 @@
                     {
                         m_PANId = m_PANIdPend;
                     }
-                    m_havePANId = true;
+                    m_have_PANId = true;
                 } 
                 else 
                 {
-                    pc.printf("\r\nERROR3\r\n");    
+                    /* TODO */
                 }
                 ret_val = true;
                 break;
@@ -187,11 +193,31 @@
                     {
                         m_EDA = m_EDAPend;
                     }
-                    m_haveEDA = true;
+                    m_have_EDA = true;
                 } 
                 else 
                 {
-                    pc.printf("\r\nERROR3\r\n");    
+                    /* TODO */
+                }
+                ret_val = true;
+                break;
+            case CMD_RESPONSE_SET_MY:
+            case CMD_RESPONSE_GET_MY:
+                if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) 
+                {
+                    if( CMD_RESPONSE_GET_MY == p_data[ XBEE_CMD_POSN_API_ID ] )
+                    {
+                        m_sourceAddress = ((uint16_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 8) + p_data[ XBEE_CMD_POSN_PARAM_START + 1 ];
+                    }
+                    else
+                    {
+                        m_sourceAddress = m_sourceAddressPend;
+                    }
+                    m_have_sourceAddress = true;
+                } 
+                else 
+                {
+                    /* TODO */
                 }
                 ret_val = true;
                 break;
@@ -221,132 +247,55 @@
     return ret_val;
 }
 
-bool XBeeApiCmdAt::requestHardwareVersion( void )
-{
-    XBeeApiFrame req( XBEE_CMD_AT_CMD, cmd_hv, sizeof( cmd_hv ));
-    m_haveHwVer = false;
-    m_device->SendFrame( &req );
-    return true;
-}
-
-bool XBeeApiCmdAt::requestFirmwareVersion( void )
-{
-    XBeeApiFrame req( XBEE_CMD_AT_CMD, cmd_vr, sizeof( cmd_vr ));
-    m_haveFwVer = false;
-    m_device->SendFrame( &req );
-    return true;
-}
-
-bool XBeeApiCmdAt::requestChannel( void )
-{
-    XBeeApiFrame req( XBEE_CMD_AT_CMD, cmd_ch, sizeof( cmd_ch ));
-    m_haveChan = false;
-    m_device->SendFrame( &req );
-    return true;
-}
-
-bool XBeeApiCmdAt::requestPanId( void )
-{
-    XBeeApiFrame req( XBEE_CMD_AT_CMD, cmd_pid, sizeof( cmd_pid ));
-    m_havePANId = false;
-    m_device->SendFrame( &req );
-    return true;
-}
-
-bool XBeeApiCmdAt::requestCoordinatorEnabled( void )
-{
-    XBeeApiFrame req( XBEE_CMD_AT_CMD, cmd_ce, sizeof( cmd_ce ));
-    m_haveCE = false;
-    m_device->SendFrame( &req );
-    return true;
-}
-
-bool XBeeApiCmdAt::requestEndDeviceAssociationEnabled( void )
-{
-    XBeeApiFrame req( XBEE_CMD_AT_CMD, cmd_eda, sizeof( cmd_eda ));
-    m_haveEDA = false;
-    m_device->SendFrame( &req );
-    return true;
-}
-
-bool XBeeApiCmdAt::getFirmwareVersion( uint16_t* const p_ver )
-{
-    if( m_haveFwVer ) {
-        *p_ver = m_fwVer;
-    }
-    return m_haveFwVer;
-
+#define MAKE_REQUEST( _name, _mnemonic, _cmd ) \
+bool XBeeApiCmdAt::request ## _name( void ) \
+{\
+    XBeeApiFrame req( XBEE_CMD_AT_CMD, _cmd, sizeof( _cmd ));\
+    m_have_ ## _mnemonic = false;\
+    m_device->SendFrame( &req );\
+    return true;\
 }
 
-bool XBeeApiCmdAt::getHardwareVersion( uint16_t* const p_ver )
-{
-    if( m_haveHwVer ) {
-        *p_ver = m_hwVer;
-    }
-    return m_haveHwVer;
-}
-
-bool XBeeApiCmdAt::getChannel( uint8_t* const p_chan )
-{
-    if( m_haveChan ) {
-        *p_chan = m_chan;
-    }
-    return m_haveChan;
-}
+MAKE_REQUEST( HardwareVersion, hwVer, cmd_hv )
+MAKE_REQUEST( FirmwareVersion, fwVer, cmd_vr )
+MAKE_REQUEST( Channel, chan, cmd_ch )
+MAKE_REQUEST( PanId, PANId, cmd_pid )
+MAKE_REQUEST( CoordinatorEnabled, CE, cmd_ce )
+MAKE_REQUEST( EndDeviceAssociationEnabled, EDA, cmd_eda )
+MAKE_REQUEST( SourceAddress, sourceAddress, cmd_my )
 
-bool XBeeApiCmdAt::getCoordinatorEnabled( bool* const p_en )
-{
-    if( m_haveCE ) {
-        *p_en = m_CE;
-    }
-    return m_haveCE;
-}
-
-bool XBeeApiCmdAt::setCoordinatorEnabled( const bool p_en )
-{
-    XBeeApiCmdAtSet<bool> req( cmd_set_ce, p_en );
-
-    m_haveCE = false;
-    m_CEPend = p_en;
-    m_device->SendFrame( &req );
-    return true;
+#define MAKE_GET(_name, _mnemonic, _type ) \
+bool XBeeApiCmdAt::get ## _name( _type* const p_param ) \
+{\
+    if( m_have_ ## _mnemonic ) {\
+        *p_param = m_ ## _mnemonic;\
+    } \
+    return m_have_ ## _mnemonic; \
 }
 
-bool XBeeApiCmdAt::getEndDeviceAssociationEnabled( bool* const p_en )
-{
-    if( m_haveEDA ) {
-        *p_en = m_EDA;
-    }
-    return m_haveEDA;
-}
-bool XBeeApiCmdAt::setEndDeviceAssociationEnabled( const bool p_en  )
-{
-    XBeeApiCmdAtSet<bool> req( cmd_set_eda, p_en );
+MAKE_GET( FirmwareVersion, fwVer, uint16_t )
+MAKE_GET( HardwareVersion, hwVer, uint16_t )
+MAKE_GET( Channel, chan, uint8_t )
+MAKE_GET( CoordinatorEnabled, CE, bool );
+MAKE_GET( EndDeviceAssociationEnabled, EDA, bool )
+MAKE_GET( PanId, PANId, panId_t )
+MAKE_GET( SourceAddress, sourceAddress, uint16_t )
 
-    m_haveEDA = false;
-    m_EDAPend = p_en;
-    m_device->SendFrame( &req );
-    return true;
+#define MAKE_SET( _name, _mnemonic, _cmd, _type ) \
+bool XBeeApiCmdAt::set ## _name( const _type p_param ) \
+{\
+    XBeeApiCmdAtSet<_type> req( _cmd, p_param );\
+\
+    m_have_ ## _mnemonic = false;\
+    m_## _mnemonic ## Pend = p_param;\
+    m_device->SendFrame( &req );\
+    return true;\
 }
 
-bool XBeeApiCmdAt::getPanId( panId_t* const p_chan )
-{
-    if( m_havePANId ) {
-        *p_chan = m_PANId;
-    }
-    return m_havePANId;
-
-}
-bool XBeeApiCmdAt::setPanId( const panId_t p_chan )
-{
-    XBeeApiCmdAtSet<panId_t> panid( cmd_set_pid, p_chan );
-
-    m_havePANId = false;
-    m_PANIdPend = p_chan;
-    m_device->SendFrame( &panid );
-    return true;
-}
-
+MAKE_SET( CoordinatorEnabled, CE, cmd_set_ce, bool )
+MAKE_SET( EndDeviceAssociationEnabled, EDA, cmd_set_eda, bool )
+MAKE_SET( PanId, PANId, cmd_set_pid, panId_t )
+MAKE_SET( SourceAddress, sourceAddress, cmd_set_my, uint16_t )
 
 XBeeApiCmdAtBlocking::XBeeApiCmdAtBlocking( XBeeDevice* const p_device, const uint16_t p_timeout, const uint16_t p_slice ) :
     XBeeApiCmdAt( p_device ),
@@ -372,7 +321,6 @@
         XBeeApiCmdAtBlocking::getHardwareVersion being called due to polymorphism. */
 #define BLOCKING_GET( _REQ_FN, _GET_FN, _VAR ) \
     bool ret_val = false; \
-    uint16_t counter = m_timeout; \
 \
     if( _GET_FN( _VAR ) )\
     {\
@@ -380,9 +328,11 @@
     } \
     else if( _REQ_FN() )\
     {\
-        bool cont = false;\
+        uint16_t counter = m_timeout; \
+        bool cont;\
 \
         do{\
+            cont = false; \
             wait_ms( m_slice );\
             if( _GET_FN( _VAR ) )\
             {\
@@ -408,9 +358,10 @@
 \
     if( _SET_FN( _VAR ) )\
     {\
-        bool cont = false;\
+        bool cont;\
 \
         do{\
+            cont = false;\
             wait_ms( m_slice );\
             if( _GET_FN( &readback ) &&\
                ( readback == _VAR ))\
@@ -501,6 +452,22 @@
                   panId_t );
 }
 
+bool XBeeApiCmdAtBlocking::getSourceAddress( uint16_t* const p_addr )
+{
+    BLOCKING_GET( XBeeApiCmdAt::requestSourceAddress,
+                  XBeeApiCmdAt::getSourceAddress,
+                  p_addr );
+}
+
+bool XBeeApiCmdAtBlocking::setSourceAddress( const uint16_t p_addr )
+{
+    BLOCKING_SET( XBeeApiCmdAt::setSourceAddress,
+                  XBeeApiCmdAt::getSourceAddress,
+                  p_addr,
+                  uint16_t );
+}
+
+
 template < typename T >
 XBeeApiCmdAt::XBeeApiCmdAtSet<T>::XBeeApiCmdAtSet( const uint8_t* const p_data,
                                                    const T p_val ) : XBeeApiFrame( )