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:
32:af4e495afd62
Parent:
31:c144106e55b5
Child:
36:cc7e8d1e35dd
diff -r c144106e55b5 -r af4e495afd62 Utility/XBeeApiCmdAt.cpp
--- a/Utility/XBeeApiCmdAt.cpp	Thu Feb 06 22:26:27 2014 +0000
+++ b/Utility/XBeeApiCmdAt.cpp	Fri Feb 07 21:17:32 2014 +0000
@@ -32,6 +32,14 @@
 #define CMD_RESPONSE_GET_PID '0'
 #define CMD_RESPONSE_SET_MY  'a'
 #define CMD_RESPONSE_GET_MY  'b'
+#define CMD_RESPONSE_GET_SH  'c'
+#define CMD_RESPONSE_GET_SL  'd'
+#define CMD_RESPONSE_SET_RR  'e'
+#define CMD_RESPONSE_GET_RR  'f'
+#define CMD_RESPONSE_SET_RN  'g'
+#define CMD_RESPONSE_GET_RN  'h'
+#define CMD_RESPONSE_SET_MM  'i'
+#define CMD_RESPONSE_GET_MM  'j'
 
 /** Lowest channel supported by the XBee S1 */
 #define XBEE_CHAN_MIN 0x0b
@@ -45,24 +53,34 @@
 
 /* Content for the various commands - value of 0 indicates a value to be populated (i.e. variable) */
 
-const uint8_t cmd_vr[] =      { CMD_RESPONSE_GET_VR, 'V', 'R' };
-const uint8_t cmd_hv[] =      { CMD_RESPONSE_GET_HV, 'H', 'V' };
+static const uint8_t cmd_vr[] =      { CMD_RESPONSE_GET_VR, 'V', 'R' };
+static const uint8_t cmd_hv[] =      { CMD_RESPONSE_GET_HV, 'H', 'V' };
+static const uint8_t cmd_sh[] =      { CMD_RESPONSE_GET_SH, 'S', 'H' };
+static const uint8_t cmd_sl[] =      { CMD_RESPONSE_GET_SL, 'S', 'L' };
 
-const uint8_t cmd_ch[] =      { CMD_RESPONSE_GET_CH, 'C', 'H' };
-const uint8_t cmd_set_ch[] =  { CMD_RESPONSE_SET_CH, 'C', 'H', 0 };
+static const uint8_t cmd_ch[] =      { CMD_RESPONSE_GET_CH, 'C', 'H' };
+static const uint8_t cmd_set_ch[] =  { CMD_RESPONSE_SET_CH, 'C', 'H', 0 };
 
-const uint8_t cmd_ce[] =      { CMD_RESPONSE_GET_CE, 'C', 'E' };
-const uint8_t cmd_set_ce[] =  { CMD_RESPONSE_SET_CE, 'C', 'E', 0 };
+static const uint8_t cmd_ce[] =      { CMD_RESPONSE_GET_CE, 'C', 'E' };
+static const uint8_t cmd_set_ce[] =  { CMD_RESPONSE_SET_CE, 'C', 'E', 0 };
+
+static const uint8_t cmd_eda[] =     { CMD_RESPONSE_GET_EDA, 'A', '1' };
+static const uint8_t cmd_set_eda[] = { CMD_RESPONSE_SET_EDA, 'A', '1', 0 };
 
-const uint8_t cmd_eda[] =     { CMD_RESPONSE_GET_EDA, 'A', '1' };
-const uint8_t cmd_set_eda[] = { CMD_RESPONSE_SET_EDA, 'A', '1', 0 };
+static const uint8_t cmd_pid[] =     { CMD_RESPONSE_GET_PID, 'I', 'D' };
+static const uint8_t cmd_set_pid[] = { CMD_RESPONSE_SET_PID, 'I', 'D', 0, 0 };
+
+static const uint8_t cmd_my[] =      { CMD_RESPONSE_GET_MY,  'M', 'Y' };
+static const uint8_t cmd_set_my[] =  { CMD_RESPONSE_SET_MY,  'M', 'Y', 0, 0 };
 
-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 };
+static const uint8_t cmd_rr[] =      { CMD_RESPONSE_GET_RR,  'R', 'R' };
+static const uint8_t cmd_set_rr[] =  { CMD_RESPONSE_SET_RR,  'R', 'R', 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 };
+static const uint8_t cmd_rn[] =      { CMD_RESPONSE_GET_RN,  'R', 'N' };
+static const uint8_t cmd_set_rn[] =  { CMD_RESPONSE_SET_RN,  'R', 'N', 0 };
 
+static const uint8_t cmd_mm[] =      { CMD_RESPONSE_GET_MM,  'M', 'M' };
+static const uint8_t cmd_set_mm[] =  { CMD_RESPONSE_SET_MM,  'M', 'M', 0 };
 
 #define XBEE_CMD_POSN_FRAME_ID (4U)
 #define XBEE_CMD_POSN_STATUS (7U)
@@ -77,10 +95,62 @@
     m_have_PANId( false ),
     m_have_EDA( false ),
     m_have_CE( false ),
-    m_have_sourceAddress( false )
+    m_have_sourceAddress( false ),
+    m_have_snLow( false ),
+    m_have_snHigh( false ),
+    m_have_retries( false ),
+    m_have_randomDelaySlots( false ),
+    m_have_macMode( false )
 {
 }
 
+#define PROCESS_SET_GET_RESPONSE_GENERIC( _type, _var, _src, _t ) \
+            case CMD_RESPONSE_SET_ ## _type: \
+            case CMD_RESPONSE_GET_ ## _type: \
+                if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) \
+                { \
+                    if( CMD_RESPONSE_GET_ ## _type == p_data[ XBEE_CMD_POSN_API_ID ] ) \
+                    { \
+                        m_ ##_var = (_t) (_src); \
+                    } \
+                    else \
+                    { \
+                        m_ ## _var = m_ ## _var ## Pend; \
+                    } \
+                    m_have_ ## _var = true; \
+                } \
+                else \
+                { \
+                    /* TODO */ \
+                } \
+                ret_val = true; \
+                break;
+
+#define PROCESS_GET_RESPONSE_GENERIC( _type, _var, _src ) \
+            case CMD_RESPONSE_GET_ ## _type: \
+                if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) \
+                { \
+                    m_ ##_var = _src; \
+                    m_have_ ## _var = true; \
+                } \
+                else \
+                { \
+                    /* TODO */ \
+                } \
+                ret_val = true; \
+                break;
+
+#define PROCESS_SET_GET_RESPONSE_8BIT_WITHCAST( _type, _var, _t )  PROCESS_SET_GET_RESPONSE_GENERIC( _type, _var, p_data[ XBEE_CMD_POSN_PARAM_START ], _t )
+#define PROCESS_SET_GET_RESPONSE_8BIT( _type, _var )               PROCESS_SET_GET_RESPONSE_GENERIC( _type, _var, p_data[ XBEE_CMD_POSN_PARAM_START ], uint8_t )
+#define PROCESS_SET_GET_RESPONSE_16BIT( _type, _var )              PROCESS_SET_GET_RESPONSE_GENERIC( _type, _var, ((uint16_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 8) | p_data[ XBEE_CMD_POSN_PARAM_START + 1 ], uint16_t )
+
+#define PROCESS_GET_RESPONSE_16BIT( _type, _var ) PROCESS_GET_RESPONSE_GENERIC( _type, _var, ((uint16_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 8) | p_data[ XBEE_CMD_POSN_PARAM_START + 1 ] )
+#define PROCESS_GET_RESPONSE_32BIT( _type, _var ) PROCESS_GET_RESPONSE_GENERIC( _type, _var, ((uint32_t)p_data[ XBEE_CMD_POSN_PARAM_START ] << 24) |\
+                                                                                             ((uint32_t)p_data[ XBEE_CMD_POSN_PARAM_START + 1 ] << 16) |\
+                                                                                             ((uint32_t)p_data[ XBEE_CMD_POSN_PARAM_START + 2 ] << 8) |\
+                                                                                             ((uint32_t)p_data[ XBEE_CMD_POSN_PARAM_START + 3 ]))
+
+
 bool XBeeApiCmdAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
 {
     bool ret_val = false;
@@ -88,140 +158,20 @@
     if( XBEE_CMD_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] ) {
 
         switch( p_data[ XBEE_CMD_POSN_FRAME_ID ] ) {
-            case CMD_RESPONSE_GET_HV:
-                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_have_hwVer = true;
-                } 
-                else 
-                {
-                    /* TODO */
-                }
-                ret_val = true;
-                break;
-
-            case CMD_RESPONSE_GET_VR:
-                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_have_fwVer = true;
-                } 
-                else 
-                {
-                    /* TODO */
-                }
-                ret_val = true;
-                break;
-
-            case CMD_RESPONSE_GET_CH:
-            case CMD_RESPONSE_SET_CH:
-                if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) 
-                {
-                    if( CMD_RESPONSE_GET_CH == p_data[ XBEE_CMD_POSN_API_ID ] )
-                    {
-                        m_chan = p_data[ XBEE_CMD_POSN_PARAM_START ];
-                    } 
-                    else
-                    {  
-                        m_chan = m_chanPend;
-                    }
-                    #if 0
-                    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_have_chan = true;
-                } 
-                else 
-                {
-                    /* TODO */
-                }
-                ret_val = true;
-                break;
-
-            case CMD_RESPONSE_SET_CE:
-            case CMD_RESPONSE_GET_CE:
-                if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) 
-                {
-                    if( CMD_RESPONSE_GET_CE == p_data[ XBEE_CMD_POSN_API_ID ] )
-                    {
-                        m_CE = p_data[ XBEE_CMD_POSN_PARAM_START ];
-                    }
-                    else
-                    {
-                        m_CE = m_CEPend;
-                    }
-                    m_have_CE = true;
-                } 
-                else 
-                {
-                    /* TODO */
-                }
-                ret_val = true;
-                break;
-
-            case CMD_RESPONSE_SET_PID:
-            case CMD_RESPONSE_GET_PID:
-                if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) 
-                {
-                    if( CMD_RESPONSE_GET_PID == p_data[ XBEE_CMD_POSN_API_ID ] )
-                    {
-                        m_PANId = p_data[ XBEE_CMD_POSN_PARAM_START ];
-                    } 
-                    else
-                    {
-                        m_PANId = m_PANIdPend;
-                    }
-                    m_have_PANId = true;
-                } 
-                else 
-                {
-                    /* TODO */
-                }
-                ret_val = true;
-                break;
-
-            case CMD_RESPONSE_SET_EDA:
-            case CMD_RESPONSE_GET_EDA:
-                if( p_data[ XBEE_CMD_POSN_STATUS ] == 0 ) 
-                {
-                    if( CMD_RESPONSE_GET_EDA == p_data[ XBEE_CMD_POSN_API_ID ] )
-                    {
-                        m_EDA = p_data[ XBEE_CMD_POSN_PARAM_START ];
-                    }
-                    else
-                    {
-                        m_EDA = m_EDAPend;
-                    }
-                    m_have_EDA = true;
-                } 
-                else 
-                {
-                    /* 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;
-
+            
+            PROCESS_GET_RESPONSE_16BIT( HV, hwVer )
+            PROCESS_GET_RESPONSE_16BIT( VR, fwVer )
+            
+            PROCESS_SET_GET_RESPONSE_8BIT( CH, chan )
+            PROCESS_SET_GET_RESPONSE_8BIT( CE, CE )
+            PROCESS_SET_GET_RESPONSE_8BIT( PID, PANId )
+            PROCESS_SET_GET_RESPONSE_8BIT( EDA, EDA )
+            PROCESS_SET_GET_RESPONSE_8BIT( RR, retries )
+            PROCESS_SET_GET_RESPONSE_16BIT( MY, sourceAddress )
+            PROCESS_GET_RESPONSE_32BIT( SH, snHigh )
+            PROCESS_GET_RESPONSE_32BIT( SL, snLow )
+            PROCESS_SET_GET_RESPONSE_8BIT( RN, randomDelaySlots )
+            PROCESS_SET_GET_RESPONSE_8BIT_WITHCAST( MM, macMode, XBeeApiMACMode_e )
         }
     }
     return ret_val;
@@ -263,6 +213,19 @@
 MAKE_REQUEST( CoordinatorEnabled, CE, cmd_ce )
 MAKE_REQUEST( EndDeviceAssociationEnabled, EDA, cmd_eda )
 MAKE_REQUEST( SourceAddress, sourceAddress, cmd_my )
+MAKE_REQUEST( Retries, retries, cmd_rr )
+MAKE_REQUEST( RandomDelaySlots, randomDelaySlots, cmd_rn )
+MAKE_REQUEST( MacMode, macMode, cmd_mm );
+
+bool XBeeApiCmdAt::requestSerialNumber( void )
+{
+    XBeeApiFrame req1( XBEE_CMD_AT_CMD, cmd_sh, sizeof( cmd_sh ));
+    XBeeApiFrame req2( XBEE_CMD_AT_CMD, cmd_sl, sizeof( cmd_sl ));
+    m_have_snHigh = m_have_snLow = false;
+    m_device->SendFrame( &req1 );
+    m_device->SendFrame( &req2 );
+    return true;
+}
 
 #define MAKE_GET(_name, _mnemonic, _type ) \
 bool XBeeApiCmdAt::get ## _name( _type* const p_param ) \
@@ -280,6 +243,22 @@
 MAKE_GET( EndDeviceAssociationEnabled, EDA, bool )
 MAKE_GET( PanId, PANId, panId_t )
 MAKE_GET( SourceAddress, sourceAddress, uint16_t )
+MAKE_GET( Retries, retries, uint8_t )
+MAKE_GET( RandomDelaySlots, randomDelaySlots, uint8_t )
+MAKE_GET( MacMode, macMode, XBeeApiMACMode_e )
+
+bool XBeeApiCmdAt::getSerialNumber( uint64_t* const p_sn )
+{
+    /* Need both halves to have the complete serial number */
+    bool have_sn = m_have_snLow && m_have_snHigh;
+    if( have_sn )
+    {
+        *p_sn = m_snHigh;
+        *p_sn = *p_sn << 32U;
+        *p_sn |= m_snLow;
+    }
+    return( have_sn );
+}
 
 #define MAKE_SET( _name, _mnemonic, _cmd, _type ) \
 bool XBeeApiCmdAt::set ## _name( const _type p_param ) \
@@ -292,10 +271,13 @@
     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 )
+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 )
+MAKE_SET( Retries,                     retries,          cmd_set_rr,  uint8_t )
+MAKE_SET( RandomDelaySlots,            randomDelaySlots, cmd_set_rn,  uint8_t )
+MAKE_SET( MacMode,                     macMode,          cmd_set_mm,  XBeeApiMACMode_e )
 
 XBeeApiCmdAtBlocking::XBeeApiCmdAtBlocking( XBeeDevice* const p_device, const uint16_t p_timeout, const uint16_t p_slice ) :
     XBeeApiCmdAt( p_device ),
@@ -392,6 +374,13 @@
                   p_ver );
 }
 
+bool XBeeApiCmdAtBlocking::getSerialNumber( uint64_t* const p_sn )
+{
+    BLOCKING_GET( XBeeApiCmdAt::requestSerialNumber,
+                  XBeeApiCmdAt::getSerialNumber,
+                  p_sn );
+}
+
 bool XBeeApiCmdAtBlocking::getChannel( uint8_t* const p_chan )
 {
     BLOCKING_GET( XBeeApiCmdAt::requestChannel,
@@ -467,6 +456,52 @@
                   uint16_t );
 }
 
+bool XBeeApiCmdAtBlocking::getRetries( uint8_t* const p_addr )
+{
+    BLOCKING_GET( XBeeApiCmdAt::requestRetries,
+                  XBeeApiCmdAt::getRetries,
+                  p_addr );
+}
+
+bool XBeeApiCmdAtBlocking::setRetries( const uint8_t p_addr )
+{
+    BLOCKING_SET( XBeeApiCmdAt::setRetries,
+                  XBeeApiCmdAt::getRetries,
+                  p_addr,
+                  uint8_t );
+}
+
+bool XBeeApiCmdAtBlocking::getRandomDelaySlots( uint8_t* const p_addr )
+{
+    BLOCKING_GET( XBeeApiCmdAt::requestRandomDelaySlots,
+                  XBeeApiCmdAt::getRandomDelaySlots,
+                  p_addr );
+}
+
+bool XBeeApiCmdAtBlocking::setRandomDelaySlots( const uint8_t p_addr )
+{
+    BLOCKING_SET( XBeeApiCmdAt::setRandomDelaySlots,
+                  XBeeApiCmdAt::getRandomDelaySlots,
+                  p_addr,
+                  uint8_t );
+}
+
+bool XBeeApiCmdAtBlocking::getMacMode( XBeeApiMACMode_e* const p_mode )
+{
+    BLOCKING_GET( XBeeApiCmdAt::requestMacMode,
+                  XBeeApiCmdAt::getMacMode,
+                  p_mode );
+}
+           
+bool XBeeApiCmdAtBlocking::setMacMode( const XBeeApiMACMode_e p_mode )
+{
+    BLOCKING_SET( XBeeApiCmdAt::setMacMode,
+                  XBeeApiCmdAt::getMacMode,
+                  p_mode,
+                  XBeeApiMACMode_e );
+}
+
+
 
 template < typename T >
 XBeeApiCmdAt::XBeeApiCmdAtSet<T>::XBeeApiCmdAtSet( const uint8_t* const p_data,