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:
55:610aa4a2ed3b
Parent:
54:9f5b7652943e
Child:
56:7fe74b03e6b1
--- a/Remote/XBeeDeviceRemoteAt.cpp	Mon Jul 28 14:13:20 2014 +0000
+++ b/Remote/XBeeDeviceRemoteAt.cpp	Sat Aug 02 16:41:14 2014 +0000
@@ -34,6 +34,7 @@
         XBeeApiCmdAtRemoteSet( const uint8_t        p_frameId,
                                const uint16_t       p_addr16Bit,
                                const uint64_t       p_addr64Bit,
+                               const XBeeDevice::XBeeApiAddrType_t p_type,
                                const bool           p_applyChanges,
                                const uint8_t* const p_data,
                                const T p_val );
@@ -54,6 +55,7 @@
         XBeeApiCmdAtRemoteReq( const uint8_t        p_frameId,
                                const uint16_t       p_addr16Bit,
                                const uint64_t       p_addr64Bit,
+                               const XBeeDevice::XBeeApiAddrType_t p_type,
                                const uint8_t* const p_data );
         /** Destructor */
         virtual ~XBeeApiCmdAtRemoteReq();
@@ -64,19 +66,35 @@
                                         const uint64_t& p_addr64Bit,
                                         const bool      p_applyChanges  ) : XBeeApiCmdAt( p_device ), m_applyChanges( p_applyChanges )
 {
+    setAddress( p_addr16Bit, p_addr64Bit );
+}
+
+void XBeeDeviceRemoteAt::setAddress( const uint16_t& p_addr16Bit,
+                                     const uint64_t& p_addr64Bit )
+{
     if( p_addr16Bit != XBEE_USE_64BIT_ADDR )
     {
         m_sourceAddress = p_addr16Bit;
         m_have_sourceAddress = true;
         m_snLow = m_snHigh = 0;
+        m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_16BIT;
     } 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;
-    }
+        m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_64BIT;
+    }  
 }
+       
+void XBeeDeviceRemoteAt::reassociate( const uint16_t& p_addr16Bit,
+                                      const uint64_t& p_addr64Bit )
+{
+    resetCachedData();
+    setAddress( p_addr16Bit, p_addr64Bit );
+}
+
         
 XBeeDeviceRemoteAt::~XBeeDeviceRemoteAt( void )
 {
@@ -136,7 +154,7 @@
 {
      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 );
+     XBeeApiCmdAtRemoteSet<uint8_t> req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, m_applyChanges, p_data, p_val );
      m_device->SendFrame( &req );
 }
 
@@ -145,7 +163,7 @@
                                            const uint16_t&      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 );
+     XBeeApiCmdAtRemoteSet<uint16_t> req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, m_applyChanges, p_data, p_val );
      m_device->SendFrame( &req );
 }
 
@@ -154,7 +172,7 @@
                                            const uint32_t&      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 );
+     XBeeApiCmdAtRemoteSet<uint32_t> req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, m_applyChanges, p_data, p_val );
      m_device->SendFrame( &req );
 }
 
@@ -162,14 +180,76 @@
                                   const uint8_t*            p_data )
 {
      uint64_t addr64Bit = (((uint64_t)m_snHigh) << 32U) | (uint64_t)m_snLow;
-     XBeeApiCmdAtRemoteReq req( p_frameId, m_sourceAddress, addr64Bit, p_data );
+     XBeeApiCmdAtRemoteReq req( p_frameId, m_sourceAddress, addr64Bit, m_addressingType, p_data );
      m_device->SendFrame( &req );
 }
 
+bool XBeeDeviceRemoteAt::setAddressingType( const XBeeDevice::XBeeApiAddrType_t p_type )
+{
+    bool ret_val = false;
+    
+    switch( p_type )
+    {
+        case XBeeDevice::XBEE_API_ADDR_TYPE_16BIT:
+            if( m_have_sourceAddress )
+            {
+                m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_16BIT;
+                ret_val = true;
+            }
+            break;
+        case XBeeDevice::XBEE_API_ADDR_TYPE_64BIT:
+            if( m_have_snLow && m_have_snHigh )
+            {
+                m_addressingType = XBeeDevice::XBEE_API_ADDR_TYPE_64BIT;
+                ret_val = true;
+            }
+            break;
+    }
+    
+    return ret_val;
+}
+
+static void writeAddressToBuffer( uint8_t* const p_buffer,
+                                  const uint16_t       p_addr16Bit,
+                                  const uint64_t       p_addr64Bit,
+                                  const XBeeDevice::XBeeApiAddrType_t p_type )
+{
+    if( p_type == XBeeDevice::XBEE_API_ADDR_TYPE_64BIT )
+    {
+        p_buffer[0] = p_addr64Bit >> 56U;
+        p_buffer[1] = p_addr64Bit >> 48U;
+        p_buffer[2] = p_addr64Bit >> 40U;
+        p_buffer[3] = p_addr64Bit >> 32U;
+        p_buffer[4] = p_addr64Bit >> 24U;
+        p_buffer[5] = p_addr64Bit >> 16U;
+        p_buffer[6] = p_addr64Bit >> 8U;
+        p_buffer[7] = p_addr64Bit;
+
+        p_buffer[8] = (uint8_t)(XBEE_USE_64BIT_ADDR >> 8U);
+        p_buffer[9] = (uint8_t)(XBEE_USE_64BIT_ADDR & 0xFF);
+    }
+    else
+    {
+
+        p_buffer[0] = 0;
+        p_buffer[1] = 0;
+        p_buffer[2] = 0;
+        p_buffer[3] = 0;
+        p_buffer[4] = 0;
+        p_buffer[5] = 0;
+        p_buffer[6] = 0;
+        p_buffer[7] = 0;
+        
+        p_buffer[8] = p_addr16Bit >> 8U;
+        p_buffer[9] = p_addr16Bit;
+    }
+}
+
 template < typename T >
 XBeeApiCmdAtRemoteSet<T>::XBeeApiCmdAtRemoteSet( const uint8_t        p_frameId,
                                                  const uint16_t       p_addr16Bit,
                                                  const uint64_t       p_addr64Bit,
+                                                 const XBeeDevice::XBeeApiAddrType_t p_type,
                                                  const bool           p_applyChanges,
                                                  const uint8_t* const p_data,
                                                  const T p_val ) : XBeeApiFrame( )
@@ -182,17 +262,7 @@
     
     m_buffer[0] = p_frameId;
     
-    m_buffer[1] = p_addr64Bit >> 56U;
-    m_buffer[2] = p_addr64Bit >> 48U;
-    m_buffer[3] = p_addr64Bit >> 40U;
-    m_buffer[4] = p_addr64Bit >> 32U;
-    m_buffer[5] = p_addr64Bit >> 24U;
-    m_buffer[6] = p_addr64Bit >> 16U;
-    m_buffer[7] = p_addr64Bit >> 8U;
-    m_buffer[8] = p_addr64Bit;
-
-    m_buffer[9] = p_addr16Bit >> 8U;
-    m_buffer[10] = p_addr16Bit;
+    writeAddressToBuffer( &(m_buffer[1]), p_addr16Bit, p_addr64Bit, p_type );
 
     m_buffer[11] = p_applyChanges << 1;
 
@@ -223,23 +293,14 @@
 XBeeApiCmdAtRemoteReq::XBeeApiCmdAtRemoteReq( const uint8_t        p_frameId,
                                               const uint16_t       p_addr16Bit,
                                               const uint64_t       p_addr64Bit,
+                                              const XBeeDevice::XBeeApiAddrType_t p_type,
                                               const uint8_t* const p_data )
 {
     m_apiId = XBEE_CMD_REMOTE_AT_CMD;
     
     m_buffer[0] = p_frameId;
     
-    m_buffer[1] = p_addr64Bit >> 56U;
-    m_buffer[2] = p_addr64Bit >> 48U;
-    m_buffer[3] = p_addr64Bit >> 40U;
-    m_buffer[4] = p_addr64Bit >> 32U;
-    m_buffer[5] = p_addr64Bit >> 24U;
-    m_buffer[6] = p_addr64Bit >> 16U;
-    m_buffer[7] = p_addr64Bit >> 8U;
-    m_buffer[8] = p_addr64Bit;
-
-    m_buffer[9] = p_addr16Bit >> 8U;
-    m_buffer[10] = p_addr16Bit;
+    writeAddressToBuffer( &(m_buffer[1]), p_addr16Bit, p_addr64Bit, p_type );
 
     m_buffer[11] = 0;