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.

Files at this revision

API Documentation at this revision

Comitter:
johnb
Date:
Mon Jul 28 12:48:33 2014 +0000
Parent:
51:a7d0d2ef9261
Child:
53:7b65422d7a32
Commit message:
Add initial support for decoding IO frames.

Changed in this revision

Remote/XBeeDeviceRemoteAt.cpp Show annotated file Show diff for this revision Revisions of this file
Remote/XBeeDeviceRemoteAt.hpp Show annotated file Show diff for this revision Revisions of this file
Utility/XBeeApiCmdAt.cpp Show annotated file Show diff for this revision Revisions of this file
Utility/XBeeApiCmdAt.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/Remote/XBeeDeviceRemoteAt.cpp	Mon Jul 28 10:24:16 2014 +0000
+++ b/Remote/XBeeDeviceRemoteAt.cpp	Mon Jul 28 12:48:33 2014 +0000
@@ -62,8 +62,19 @@
 XBeeDeviceRemoteAt::XBeeDeviceRemoteAt( XBeeDevice* p_device,
                                         const uint16_t& p_addr16Bit,
                                         const uint64_t& p_addr64Bit,
-                                        const bool      p_applyChanges  ) : XBeeApiCmdAt( p_device ), m_addr16Bit( p_addr16Bit ), m_addr64Bit( p_addr64Bit ), m_applyChanges( p_applyChanges )
+                                        const bool      p_applyChanges  ) : XBeeApiCmdAt( p_device ), m_applyChanges( p_applyChanges )
 {
+    if( p_addr16Bit != XBEE_USE_64BIT_ADDR )
+    {
+        m_sourceAddress = p_addr16Bit;
+        m_have_sourceAddress = true;
+    } else
+    {
+        m_snLow = (p_addr64Bit & 0xFFFFFFFF);
+        m_snHigh = ((p_addr64Bit >> 32U) & 0xFFFFFFFF);
+        m_sourceAddress = XBEE_USE_64BIT_ADDR;
+        m_have_snLow = m_have_snHigh = true;
+    }
 }
         
 XBeeDeviceRemoteAt::~XBeeDeviceRemoteAt( void )
@@ -92,37 +103,27 @@
  
     if( XBEE_CMD_REMOTE_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] )
     {
-        uint64_t src64BitAddr = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS ]) << 56U) |
-                                (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 1 ]) << 48U) |
-                                (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 2 ]) << 40U) |
-                                (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 3 ]) << 32U) |
-                                (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 4 ]) << 24U) |
+        uint32_t srcAddrHigh = (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS ]) << 24U) |
+                               (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 1 ]) << 16U) |
+                               (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 2 ]) << 8U) |
+                               (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 3 ]));
+        uint32_t srcAddrLow =  (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 4 ]) << 24U) |
                                 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 5 ]) << 16U) |
                                 (((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 6 ]) << 8U) |
                                 ((uint64_t)p_data[ XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + 7 ]);
+
         uint16_t src16BitAddr = (((uint16_t)p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS ]) << 8U) | 
                                 p_data[ XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS + 1 ];
-                
-        if(( src16BitAddr == m_addr16Bit ) ||
-           ( src64BitAddr == m_addr64Bit ))
+                        
+        if((( m_have_sourceAddress ) && ( m_sourceAddress == src16BitAddr )) ||
+           ( m_have_snHigh && m_have_snLow && ( srcAddrHigh == m_snHigh ) && ( srcAddrLow == m_snLow )))
         {
             ret_val = processResponseFrame( p_data, p_len );
-            
-#if 0
-            pc.printf("Len %d\r\n",p_len);
-            for( uint8_t i = 0; i < p_len; i++ ) {
-                pc.printf("%02X ",p_data[i]);
-            }
-            pc.printf("\r\n");
-            ret_val = true;
-#endif
         }
     }
-    
-    if(( XBEE_CMD_RX_64B_IO == p_data[ XBEE_CMD_POSN_API_ID ] ) ||
-       ( XBEE_CMD_RX_16B_IO == p_data[ XBEE_CMD_POSN_API_ID ] ))
+    else
     {
-        // TODO
+        XBeeApiCmdAt::decodeCallback( p_data, p_len );
     }
     
     return ret_val;
@@ -132,7 +133,9 @@
                                           const uint8_t* const p_data,
                                           const uint8_t&       p_val )
 {
-     XBeeApiCmdAtRemoteSet<uint8_t> req( p_frameId, m_addr16Bit, m_addr64Bit, m_applyChanges, p_data, p_val );
+     uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
+     /* TODO: Add option to force usage of 16 or 64-bit addressing */
+     XBeeApiCmdAtRemoteSet<uint8_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
      m_device->SendFrame( &req );
 }
 
@@ -140,7 +143,8 @@
                                            const uint8_t* const p_data,
                                            const uint16_t&      p_val )
 {
-     XBeeApiCmdAtRemoteSet<uint16_t> req( p_frameId, m_addr16Bit, m_addr64Bit, m_applyChanges, p_data, p_val );
+     uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
+     XBeeApiCmdAtRemoteSet<uint16_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
      m_device->SendFrame( &req );
 }
 
@@ -148,15 +152,17 @@
                                            const uint8_t* const p_data,
                                            const uint32_t&      p_val )
 {
-     XBeeApiCmdAtRemoteSet<uint32_t> req( p_frameId, m_addr16Bit, m_addr64Bit, m_applyChanges, p_data, p_val );
+     uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
+     XBeeApiCmdAtRemoteSet<uint32_t> req( p_frameId, m_sourceAddress, addr64Bit, m_applyChanges, p_data, p_val );
      m_device->SendFrame( &req );
 }
 
 void XBeeDeviceRemoteAt::SendReq( const uint8_t             p_frameId,
                                   const uint8_t*            p_data )
 {
-    XBeeApiCmdAtRemoteReq req( p_frameId, m_addr16Bit, m_addr64Bit, p_data );
-    m_device->SendFrame( &req );
+     uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
+     XBeeApiCmdAtRemoteReq req( p_frameId, m_sourceAddress, addr64Bit, p_data );
+     m_device->SendFrame( &req );
 }
 
 template < typename T >
--- a/Remote/XBeeDeviceRemoteAt.hpp	Mon Jul 28 10:24:16 2014 +0000
+++ b/Remote/XBeeDeviceRemoteAt.hpp	Mon Jul 28 12:48:33 2014 +0000
@@ -56,8 +56,6 @@
 
         virtual size_t getResponseStatusPos( void ) const;
 
-        uint16_t m_addr16Bit;
-        uint64_t m_addr64Bit;
         bool     m_applyChanges;
 
     public:
--- a/Utility/XBeeApiCmdAt.cpp	Mon Jul 28 10:24:16 2014 +0000
+++ b/Utility/XBeeApiCmdAt.cpp	Mon Jul 28 12:48:33 2014 +0000
@@ -252,11 +252,19 @@
     m_resetCount( 0 ),
     m_sampleCount( 0 )
 {
-    for( uint8_t i = 0;
+    uint8_t i;
+    for( i = 0;
          i < XBEE_API_DIO_CHANNEL_COUNT;
          i++ )
     {
         m_have_d[ i ] = false;
+        m_ioDigitalUpdatedTime[ i ] = 0;
+    }
+    for( i = 0;
+         i < XBEE_API_ADC_CHANNEL_COUNT;
+         i++ )
+    {
+        m_ioAnalogueUpdatedTime[ i ] = 0;
     }
 }
 
@@ -392,6 +400,65 @@
     return ret_val;
 }
 
+#define DIO_CHANNEL_MASK (0x01FFU)
+#define ADC_CHANNEL_MASK (0x7E00U)
+
+bool XBeeApiCmdAt::processIOFrame( const uint8_t* const p_data, size_t p_len, const size_t p_start )
+{
+#if 0
+    /* This is the number of sample sets that are contained in the packet, set using the IT AT command */
+    uint8_t sampleCount = p_data[ p_start ];
+#endif
+    
+    uint16_t channelMask = (((uint16_t)p_data[ p_start + 1 ]) << 8) |
+                           ((uint16_t)p_data[ p_start + 2 ]);
+    const uint8_t* dataPtr = &( p_data[ p_start + 3 ] );
+    const uint16_t dioMask = channelMask & DIO_CHANNEL_MASK;
+    uint16_t adcMask = (channelMask & ADC_CHANNEL_MASK) >> 9U;
+    uint8_t adc = 0;
+
+    time_t seconds = time( NULL );
+
+    if( dioMask )
+    {
+        uint16_t dioData = (((uint16_t)dataPtr[ 0 ]) << 8U) |
+                           (uint16_t)dataPtr[ 1 ];
+        dataPtr += 2;
+        
+        for( unsigned i = 0;
+             i < XBEE_API_DIO_CHANNEL_COUNT;
+             i++ )
+        {
+            if(( dioMask >> i ) & 0x01 )
+            {
+                m_ioDigitalState[i] = ( dioData >> i ) & 0x01;
+                m_ioDigitalUpdatedTime[i] = seconds;
+            }
+        }
+    }
+    
+    while( adcMask )
+    {
+        if( adcMask & 0x01 )
+        {
+            uint16_t adcData = (((uint16_t)dataPtr[ 0 ]) << 8U) |
+                               (uint16_t)dataPtr[ 1 ];
+            dataPtr += 2;
+            
+            m_ioAnalogueVal[adc] = adcData;
+            m_ioAnalogueUpdatedTime[adc] = seconds;
+        }
+        adcMask = adcMask >> 1U;
+        adc++;
+    }    
+    
+    return true;                       
+}
+
+#define XBEE_IO_PACKET_ADDRESS (XBEE_CMD_POSN_ID_SPECIFIC_DATA)
+#define XBEE_IO_PACKET_64BIT_DATA_START (XBEE_IO_PACKET_ADDRESS + sizeof( uint64_t) + 1U + 1U)
+#define XBEE_IO_PACKET_16BIT_DATA_START (XBEE_IO_PACKET_ADDRESS + sizeof( uint16_t) + 1U + 1U)
+
 bool XBeeApiCmdAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
 {
     bool ret_val = false;
@@ -399,7 +466,39 @@
     if( XBEE_CMD_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] ) 
     {
         ret_val = processResponseFrame( p_data, p_len );
+    } 
+    else if( XBEE_CMD_RX_16B_IO == p_data[ XBEE_CMD_POSN_API_ID ] )
+    {
+        uint16_t src16BitAddr = (((uint16_t)p_data[ XBEE_IO_PACKET_ADDRESS ]) << 8U) | 
+                                p_data[ XBEE_IO_PACKET_ADDRESS + 1 ];
+        if( m_have_sourceAddress &&
+           ( src16BitAddr == m_sourceAddress ))
+        {
+            ret_val = processIOFrame( p_data, p_len, XBEE_IO_PACKET_16BIT_DATA_START );
+        }
     }
+    else if( XBEE_CMD_RX_64B_IO == p_data[ XBEE_CMD_POSN_API_ID ] )
+    {
+        uint32_t srcAddrHigh = (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS ]) << 24U) |
+                               (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 1 ]) << 16U) |
+                               (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 2 ]) << 8U) |
+                               (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 3 ]));
+        uint32_t srcAddrLow =  (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 4 ]) << 24U) |
+                                (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 5 ]) << 16U) |
+                                (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 6 ]) << 8U) |
+                                ((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 7 ]);
+        if( m_have_snLow &&
+            m_have_snHigh &&
+           ( srcAddrHigh == m_snHigh ) &&
+           ( srcAddrLow  == m_snLow ))
+        {
+            ret_val = processIOFrame( p_data, p_len, XBEE_IO_PACKET_64BIT_DATA_START );
+        }
+    }
+    else
+    {
+    }
+
     return ret_val;
 }
 
@@ -504,6 +603,27 @@
     return ret_val;
 }
 
+time_t XBeeApiCmdAt::getDigitalState( const uint8_t p_chanNo, bool& p_state )
+{
+    time_t ret_val = 0;
+    if( p_chanNo < XBEE_API_DIO_CHANNEL_COUNT )
+    {
+        p_state = m_ioDigitalState[ p_chanNo ];
+        ret_val = m_ioDigitalUpdatedTime[ p_chanNo ];
+    }    
+    return ret_val;
+}
+
+time_t XBeeApiCmdAt::getAnalogueValue( const uint8_t p_chanNo, uint16_t& p_val )
+{
+    time_t ret_val = 0;
+    if( p_chanNo < XBEE_API_ADC_CHANNEL_COUNT )
+    {
+        p_val = m_ioAnalogueVal[ p_chanNo ];
+        ret_val = m_ioAnalogueUpdatedTime[ p_chanNo ];
+    }        
+    return ret_val;
+}
 
 #define MAKE_GET(_name, _mnemonic, _type ) \
 bool XBeeApiCmdAt::get ## _name( _type* const p_param ) \
--- a/Utility/XBeeApiCmdAt.hpp	Mon Jul 28 10:24:16 2014 +0000
+++ b/Utility/XBeeApiCmdAt.hpp	Mon Jul 28 12:48:33 2014 +0000
@@ -40,6 +40,7 @@
 #define XBEE_API_CMD_REQ_HEADER_LEN 3U
 
 #define XBEE_API_DIO_CHANNEL_COUNT 8U
+#define XBEE_API_ADC_CHANNEL_COUNT 6U
 
 /** Class to access the configuration interface of the XBee.
     Requests to the XBee are non-blocking meaning that code
@@ -141,6 +142,12 @@
         uint32_t m_applyCount;
         uint32_t m_resetCount; 
         uint32_t m_sampleCount;
+        
+        time_t   m_ioDigitalUpdatedTime[ XBEE_API_DIO_CHANNEL_COUNT ];
+        bool     m_ioDigitalState[ XBEE_API_DIO_CHANNEL_COUNT ];
+        time_t   m_ioAnalogueUpdatedTime[ XBEE_API_ADC_CHANNEL_COUNT ];
+        uint16_t m_ioAnalogueVal[ XBEE_API_ADC_CHANNEL_COUNT ];
+
 
         virtual void SendCmd_uint8_t( const uint8_t        p_frameId,
                                       const uint8_t* const p_data,
@@ -162,6 +169,7 @@
 
         virtual bool processResponseFrame( const uint8_t* const p_data, size_t p_len );
 
+        virtual bool processIOFrame( const uint8_t* const p_data, size_t p_len, const size_t p_start );
 
     public:
 
@@ -173,6 +181,9 @@
         /** Destructor */
         virtual ~XBeeApiCmdAt( void ) {};
        
+        time_t getDigitalState( const uint8_t p_chanNo, bool& p_state );
+        time_t getAnalogueValue( const uint8_t p_chanNo, uint16_t& p_val );
+       
         /** Request the hardware version identifier from the XBee.
             As the data is retrieved asynchronously to this call,
             once the response is received it can be accessed via