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:
56:7fe74b03e6b1
Parent:
55:610aa4a2ed3b
--- a/Utility/XBeeApiCmdAt.cpp	Sat Aug 02 16:41:14 2014 +0000
+++ b/Utility/XBeeApiCmdAt.cpp	Fri Aug 08 11:59:52 2014 +0000
@@ -70,6 +70,9 @@
 #define CMD_RESPONSE_SET_FR  'L'
 #define CMD_RESPONSE_SET_IS  'M'
 #define CMD_RESPONSE_SET_RE  'N'
+#define CMD_RESPONSE_SET_KY  'O'
+#define CMD_RESPONSE_GET_EE  'P'
+#define CMD_RESPONSE_SET_EE  'Q'
 
 /** Lowest channel supported by the XBee S1 */
 #define XBEE_CHAN_MIN 0x0b
@@ -173,6 +176,14 @@
 static const uint8_t cmd_re[] =        { 'R', 'E' };
 static const uint8_t cmd_re_set_fid =  CMD_RESPONSE_SET_RE;
 
+static const uint8_t cmd_ky_set[] =    { 'K', 'Y', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+static const uint8_t cmd_ky_set_fid =  CMD_RESPONSE_SET_KY;
+
+static const uint8_t cmd_ee[] =        { 'E', 'E' };
+static const uint8_t cmd_ee_get_fid =  CMD_RESPONSE_GET_EE;
+static const uint8_t cmd_ee_set[] =    { 'E', 'E', 0 };
+static const uint8_t cmd_ee_set_fid =  CMD_RESPONSE_SET_EE;
+
 
 static const uint8_t cmd_d[ XBEE_API_DIO_CHANNEL_COUNT ][2] = {{ 'D', '0' },
                                                                { 'D', '1' },
@@ -197,41 +208,27 @@
 
 #define XBEE_CMD_RESPONS_HAS_DATA( _p_len ) ((_p_len > ( XBEE_CMD_POSN_PARAM_START + 1 ))
 
-/** Template class to create an XBeeApiFrame which can be used to change
+/** Class to create an XBeeApiFrame which can be used to read or change
     the value of one of the XBee parameters.  This class is used by the
-    setXXX methods in XBeeApiCmdAt */
-template< typename T >
-class XBeeApiCmdAtSet : public XBeeApiFrame {
-    uint8_t m_buffer[ XBEE_API_CMD_SET_HEADER_LEN + sizeof( T ) ];
+    requestXXX and setXXX methods in XBeeApiCmdAt */
+class XBeeApiCmdAtFrame : public XBeeApiFrame {
+    uint8_t m_buffer[ XBEE_API_CMD_SET_HEADER_LEN + XBEE_CMD_MAX_PARAM_LENGTH ];
     public:
         /** Constructor
                    
             \param p_data Pointer to a buffer of length 2 bytes identifying
                           the command, e.g. 'V', 'R' would set up a version
                           request
-            \param p_val New value for the parameter 
+            \param p_val Pointer to the new value for the parameter, most
+                        significant byte (MSB) first 
+            \param p_len The length of the data pointed to by p_val 
         */
-        XBeeApiCmdAtSet( const uint8_t        p_frameId,
-                         const uint8_t* const p_data,
-                         const T p_val );
+        XBeeApiCmdAtFrame( const uint8_t        p_frameId,
+                           const uint8_t* const p_data,
+                           const uint8_t* const p_val,
+                           const uint8_t        p_len );
         /** Destructor */
-        virtual ~XBeeApiCmdAtSet();
-};
-
-class XBeeApiCmdAtReq : public XBeeApiFrame {
-    uint8_t m_buffer[ XBEE_API_CMD_REQ_HEADER_LEN ];
-    public:
-        /** Constructor
-                   
-            \param p_data Pointer to a buffer of length 2 bytes identifying
-                          the command, e.g. 'V', 'R' would set up a version
-                          request
-            \param p_val New value for the parameter 
-        */
-        XBeeApiCmdAtReq( const uint8_t        p_frameId,
-                         const uint8_t* const p_data );
-        /** Destructor */
-        virtual ~XBeeApiCmdAtReq();
+        virtual ~XBeeApiCmdAtFrame();
 };
 
 void XBeeApiCmdAt::resetCachedData( void )
@@ -253,6 +250,9 @@
     m_have_sampleRate = false;
     m_have_destHigh = false;
     m_have_destLow = false;
+    m_have_encryptionEnabled = false;
+    m_keySet = false;
+    
     m_writeCount = 0;
     m_applyCount = 0;
     m_resetCount = 0;
@@ -366,6 +366,13 @@
     
     switch( p_data[ XBEE_CMD_POSN_FRAME_ID ] ) 
     {
+        case CMD_RESPONSE_SET_KY:
+            if( RESPONSE_OK( p_data )) 
+            {
+                m_keySet = true;
+            }
+            ret_val = true;
+            break;
         case CMD_RESPONSE_SET_WR:
             if( RESPONSE_OK( p_data )) 
             {
@@ -418,6 +425,7 @@
         PROCESS_SET_GET_RESPONSE_32BIT( DH, destHigh )
         PROCESS_SET_GET_RESPONSE_32BIT( DL, destLow )
         PROCESS_SET_GET_RESPONSE_8BIT( RN, randomDelaySlots )
+        PROCESS_SET_GET_RESPONSE_8BIT_WITHCAST( EE, encryptionEnabled, bool,  )
         PROCESS_SET_GET_RESPONSE_8BIT_WITHCAST( MM, macMode, XBeeApiMACMode_e,  )
         /* TODO: Add D0, D1, D2 response handling */
         PROCESS_SET_GET_RESPONSE_8BIT_WITHCAST( D0, d, XBeeApiDioConfig_e, [0] )
@@ -549,19 +557,11 @@
     return ret_val;
 }
 
-void XBeeApiCmdAt::SendReq( const uint8_t             p_frameId,
-                            const uint8_t*            p_data )
-{
-    XBeeApiCmdAtReq req( p_frameId, p_data );
-    m_device->SendFrame( &req );
-}
-
-
 #define MAKE_REQUEST( _name, _mnemonic, _cmd ) \
 bool XBeeApiCmdAt::request ## _name( void ) \
 {\
     m_have_ ## _mnemonic = false;   \
-    SendReq( _cmd ## _get_fid, _cmd ); \
+    SendCmd( _cmd ## _get_fid, _cmd, NULL, 0 ); \
     return true;\
 }
 
@@ -581,34 +581,35 @@
 MAKE_REQUEST( SampleRate,                  sampleRate,          cmd_ir )
 MAKE_REQUEST( DestinationAddressHigh,      destHigh,            cmd_dh )
 MAKE_REQUEST( DestinationAddressLow,       destLow,             cmd_dl )
+MAKE_REQUEST( EncryptionEnabled,           encryptionEnabled,   cmd_ee )
 
 bool XBeeApiCmdAt::requestWriteSettings( void )
 {
-    SendReq( cmd_wr_set_fid, cmd_wr );
+    SendCmd( cmd_wr_set_fid, cmd_wr, NULL, 0 );
     return true;
 }
 
 bool XBeeApiCmdAt::requestApplyChanges( void )
 {
-    SendReq( cmd_ac_set_fid, cmd_ac );
+    SendCmd( cmd_ac_set_fid, cmd_ac, NULL, 0 );
     return true;
 }
 
 bool XBeeApiCmdAt::requestReset( void )
 {
-    SendReq( cmd_fr_set_fid, cmd_fr );
+    SendCmd( cmd_fr_set_fid, cmd_fr, NULL, 0 );
     return true;
 }
 
 bool XBeeApiCmdAt::requestForceSample( void )
 {
-    SendReq( cmd_is_set_fid, cmd_is );
+    SendCmd( cmd_is_set_fid, cmd_is, NULL, 0 );
     return true;
 }
 
 bool XBeeApiCmdAt::requestRestoreDefaults( void )
 {
-    SendReq( cmd_re_set_fid, cmd_re );
+    SendCmd( cmd_re_set_fid, cmd_re, NULL, 0 );
     return true;
 }
 
@@ -632,7 +633,7 @@
     if( p_chanNo < XBEE_API_DIO_CHANNEL_COUNT )
     {
         m_have_d[ p_chanNo ] = false;   
-        SendReq( cmd_d_get_fid[ p_chanNo ], cmd_d[ p_chanNo ] );
+        SendCmd( cmd_d_get_fid[ p_chanNo ], cmd_d[ p_chanNo ], NULL, 0 );
         ret_val = true;
     }
     return ret_val;
@@ -684,6 +685,7 @@
 MAKE_GET( SampleRate,                  sampleRate,          uint16_t )
 MAKE_GET( DestinationAddressHigh,      destHigh,            uint32_t )
 MAKE_GET( DestinationAddressLow,       destLow,             uint32_t )
+MAKE_GET( EncryptionEnabled,           encryptionEnabled,   bool )
 
 bool XBeeApiCmdAt::getSerialNumber( uint64_t* const p_sn )
 {
@@ -725,6 +727,11 @@
     return ret_val;   
 }
 
+bool XBeeApiCmdAt::getEncryptionKeySet( void )
+{
+    return m_keySet;
+}
+
 #define MAKE_SET( _name, _mnemonic, _cmd, _type, _transmit_type ) \
 bool XBeeApiCmdAt::set ## _name( const _type p_param ) \
 {\
@@ -746,7 +753,17 @@
 MAKE_SET( SampleRate,                  sampleRate,          cmd_ir_set,  uint16_t,         uint16_t )
 MAKE_SET( DestinationAddressHigh,      destHigh,            cmd_dh_set,  uint32_t,         uint32_t )
 MAKE_SET( DestinationAddressLow,       destLow,             cmd_dl_set,  uint32_t,         uint32_t )
+MAKE_SET( EncryptionEnabled,           encryptionEnabled,   cmd_ee_set,  bool,             uint8_t )
 
+#define XBEE_ENCRYPTION_KEY_LEN 16U
+
+bool XBeeApiCmdAt::setEncryptionKey( const uint8_t* p_key )
+{
+    m_keySet = false;
+    SendCmd( cmd_ky_set_fid, cmd_ky_set, p_key, XBEE_ENCRYPTION_KEY_LEN );
+    
+    return true;
+}
 
 bool XBeeApiCmdAt::setDioConfig( const uint8_t p_chanNo, const XBeeApiDioConfig_e p_conf )
 {
@@ -771,81 +788,95 @@
     return true;
 }
 
-
-
 void XBeeApiCmdAt::SendCmd_uint8_t( const uint8_t        p_frameId,
                                     const uint8_t* const p_data,
                                     const uint8_t&       p_val )
 {
-     XBeeApiCmdAtSet<uint8_t> req( p_frameId, p_data, p_val );
-     m_device->SendFrame( &req );
+    SendCmd( p_frameId, p_data, &p_val, sizeof( uint8_t ));
 }
 
 void XBeeApiCmdAt::SendCmd_uint16_t( const uint8_t        p_frameId,
                                      const uint8_t* const p_data,
                                      const uint16_t&       p_val )
 {
-     XBeeApiCmdAtSet<uint16_t> req( p_frameId, p_data, p_val );
-     m_device->SendFrame( &req );
+    uint8_t buffer[ sizeof( uint16_t )];
+    
+    buffer[0] = (uint8_t)((p_val >> 8U) & 0xFF);
+    buffer[1] = (uint8_t)((p_val >> 0U) & 0xFF);
+     
+    SendCmd( p_frameId, p_data, buffer, sizeof( uint16_t ));
 }
 
 void XBeeApiCmdAt::SendCmd_uint32_t( const uint8_t        p_frameId,
                                      const uint8_t* const p_data,
                                      const uint32_t&       p_val )
 {
-     XBeeApiCmdAtSet<uint32_t> req( p_frameId, p_data, p_val );
-     m_device->SendFrame( &req );
+    uint8_t buffer[ sizeof( uint32_t )];
+    
+    buffer[0] = (uint8_t)((p_val >> 24U) & 0xFF);
+    buffer[1] = (uint8_t)((p_val >> 16U) & 0xFF);
+    buffer[2] = (uint8_t)((p_val >> 8U) & 0xFF);
+    buffer[3] = (uint8_t)((p_val >> 0U) & 0xFF);
+     
+    SendCmd( p_frameId, p_data, buffer, sizeof( uint32_t ));
 }
 
-template < typename T >
-XBeeApiCmdAtSet<T>::XBeeApiCmdAtSet( const uint8_t        p_frameId,
-                                     const uint8_t* const p_data,
-                                                    const T p_val ) : XBeeApiFrame( )
+void XBeeApiCmdAt::SendCmd( const uint8_t        p_frameId,
+                            const uint8_t* const p_data,
+                            const uint8_t* const p_val,
+                            const uint8_t        p_len )
 {
-    size_t s;
-    uint8_t* dest;
-    const uint8_t* src = (uint8_t*)(&p_val);
+    XBeeApiCmdAtFrame req( p_frameId, p_data, p_val, p_len );
+    m_device->SendFrame( &req );    
+}
+
+XBeeApiCmdAtFrame::XBeeApiCmdAtFrame( const uint8_t        p_frameId,   
+                                      const uint8_t* const p_data,
+                                      const uint8_t* const p_val,
+                                      const uint8_t        p_len ) : XBeeApiFrame( )
+{
     
     m_apiId = XBEE_CMD_AT_CMD;
     
     m_buffer[0] = p_frameId;
     m_buffer[1] = p_data[0];
     m_buffer[2] = p_data[1];
-    
-    m_dataLen = sizeof( m_buffer );
 
-    /* TODO: This copy code isn't portable - it's assuming that the data in 
-     * p_data is little endian */
-     
-    dest = &( m_buffer[ m_dataLen - 1 ] );
-    
-    for( s = 0;
-         s < sizeof( T );
-         s++, dest--, src++ ) {
-        *dest = *src;         
+    m_data = m_buffer;        
+    m_dataLen = XBEE_API_CMD_SET_HEADER_LEN;
+        
+    if(( p_val != NULL ) &&
+       ( p_len <= XBEE_CMD_MAX_PARAM_LENGTH ))
+    {
+        size_t s = 0;
+        uint8_t* dest = &( m_buffer[ XBEE_API_CMD_SET_HEADER_LEN ] );
+        const uint8_t* src = p_val;
+        
+        for( s = 0;
+             s < p_len;
+             s++, dest++, src++ ) {
+             *dest = *src;         
+        }
+        m_dataLen += p_len;
     }
-    
-    m_data = m_buffer;
+
+#if 0
+    /* Debugging code */
+    extern Serial pc;
+    size_t x = 0;
+    pc.printf("\r\n[%02X][%02X][%02X]",(m_dataLen>>8U)&0xFF,m_dataLen&0xFF,XBEE_CMD_AT_CMD);
+    for( x  = 0; x < m_dataLen; x++ )
+    {
+        if( x < 3 ) 
+        { 
+            pc.printf("%c",m_data[x]); 
+        }
+        pc.printf("[%02X]",m_data[x]);
+    }
+    pc.printf("\r\n");
+#endif
 }
 
-template < typename T >
-XBeeApiCmdAtSet<T>::~XBeeApiCmdAtSet()
-{
-}
-
-XBeeApiCmdAtReq::XBeeApiCmdAtReq( const uint8_t        p_frameId,
-                                  const uint8_t* const p_data )
-{
-    m_apiId = XBEE_CMD_AT_CMD;
-    
-    m_buffer[0] = p_frameId;
-    m_buffer[1] = p_data[0];
-    m_buffer[2] = p_data[1];
-    
-    m_dataLen = sizeof( m_buffer );
-    m_data = m_buffer;
-}
-
-XBeeApiCmdAtReq::~XBeeApiCmdAtReq()
+XBeeApiCmdAtFrame::~XBeeApiCmdAtFrame()
 {
 }
\ No newline at end of file