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:
42:81c789ba4c08
Parent:
41:07cb97b44e81
Child:
43:975a28e01dac
--- a/Base/XBeeDevice.cpp	Sat Jul 05 15:33:04 2014 +0000
+++ b/Base/XBeeDevice.cpp	Sun Jul 06 20:06:06 2014 +0000
@@ -39,6 +39,40 @@
 /** ASCII command to the XBee to request that it exit command mode */
 const char exit_cmd_mode_cmd[] = { 'A', 'T', 'C', 'N', '\r' };
 
+#if defined XBEEAPI_CONFIG_USING_RTOS
+#if defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
+
+#if XBEEAPI_CONFIG_RTOS_DISPATCHER_LIST_SIZE < 1
+#error Can't deal with XBEEAPI_CONFIG_RTOS_DISPATCHER_LIST_SIZE being less than 1
+#endif
+
+/** List of XBeeDevices that will receive RX dispatches from the XBeeRxDispatch thread */
+static FixedLengthList<XBeeDevice*, XBEEAPI_CONFIG_RTOS_DISPATCHER_LIST_SIZE> s_XBeeDevices;
+
+/** Serial data is received in the interrupt context which signals this thread to call
+    the XBeeDevice classes in order to deal with the data */
+void XBeeRxDispatch(void const *args) {
+    while (true) {
+        // Wait for a signal to that there's data ready to be examined.
+        osSignalWait(0x1, osWaitForever);
+
+        /* Loop all devices - TODO: could make this more efficient and skip devices with no data?
+           TODO: mutex the list to prevent modification while iterating */
+        for( FixedLengthList<XBeeDevice*, XBEEAPI_CONFIG_RTOS_DISPATCHER_LIST_SIZE>::iterator it = s_XBeeDevices.begin();
+             it!= s_XBeeDevices.end();
+             it++ )
+        {
+            (*it)->checkRxDecode();
+        }
+    }
+}
+
+osThreadDef(XBeeRxDispatch, osPriorityNormal, XBEEAPI_CONFIG_RTOS_DISPATCHER_TASK_STACK_SIZE);
+static osThreadId XBeeRxDispatchTID = NULL;
+
+#endif // defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
+#endif // defined XBEEAPI_CONFIG_USING_RTOS
+
 void XBeeDevice::init()                          
 {
     m_model = XBeeDevice::XBEEDEVICE_S1;
@@ -50,7 +84,7 @@
 XBeeDevice::XBeeDevice( PinName p_tx, PinName p_rx, PinName p_rts, PinName p_cts ):  m_serialNeedsDelete( true )
 {    
     init();
-    
+        
     m_if = new RawSerial( p_tx, p_rx ); 
 
     /* Can only do flow control on devices which support it */
@@ -81,6 +115,10 @@
     {
         delete( m_if );
     }
+#if defined XBEEAPI_CONFIG_USING_RTOS && defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
+    s_XBeeDevices.remove( this );
+#endif // defined XBEEAPI_CONFIG_USING_RTOS && defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
+
 }
 
 XBeeDevice::XBeeDeviceModel_t XBeeDevice::getXBeeModel() const
@@ -133,11 +171,17 @@
     } 
     else 
     {
+#if defined XBEEAPI_CONFIG_USING_RTOS && defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
+        /* Not going to check for API data here in the interrupt handler - set a signal
+           to the handling thread */
+        osSignalSet(XBeeRxDispatchTID, 0x1);
+#else
         /* Check to see if there's API data to decode */
         checkRxDecode();
+#endif
     }
 }
-    
+
 void XBeeDevice::checkRxDecode( void )
 {
     uint8_t buff[INITIAL_PEEK_LEN];
@@ -155,7 +199,7 @@
         /* Get an initial portion of data from the read buffer so that the message length can be determined */
         uint16_t len = m_rxBuff.peek( buff, INITIAL_PEEK_LEN );    
         cont = false;
-        
+
         /* Ensure that sufficient data was received - already know that we should be delimiter aligned based on the above */
         if( len >= INITIAL_PEEK_LEN ) 
         {
@@ -175,6 +219,7 @@
                      ++it ) {
     
                     bool processed = (*it)->decodeCallback( cmdBuff, cmdLen );
+
                     if( processed )
                     {
                         break;
@@ -231,7 +276,7 @@
     uint16_t written = 0;
  
 #if defined  XBEEAPI_CONFIG_USING_RTOS
-    m_ifMutex.lock();
+//    m_ifMutex.lock();
 #endif
 
     xbeeWrite( XBEE_SB_FRAME_DELIMITER, false );
@@ -268,7 +313,7 @@
 #endif
     
 #if defined  XBEEAPI_CONFIG_USING_RTOS
-    m_ifMutex.unlock();
+//    m_ifMutex.unlock();
 #endif
 }
 
@@ -312,7 +357,7 @@
     if( m_inAtCmdMode )
     {
 #if defined  XBEEAPI_CONFIG_USING_RTOS
-        m_ifMutex.lock();
+//        m_ifMutex.lock();
 #endif
         for( size_t i = 0;
              i < p_len;
@@ -342,7 +387,7 @@
             ret_val = XBEEDEVICE_UNEXPECTED_LENGTH;
         }
 #if defined  XBEEAPI_CONFIG_USING_RTOS
-        m_ifMutex.unlock();
+//        m_ifMutex.unlock();
 #endif
     } 
     else 
@@ -386,6 +431,27 @@
     return ret_val;
 }
 
+#if defined XBEEAPI_CONFIG_USING_RTOS && defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
+bool XBeeDevice::setupDispatchTask( void )
+{
+    bool ret_val = false;
+    
+    /* Ensure the dispatch thread exists */
+    if( XBeeRxDispatchTID == NULL )
+    {
+        XBeeRxDispatchTID = osThreadCreate(osThread(XBeeRxDispatch), NULL);
+    }
+    
+    /* Register this device in the list that need dispatch */
+    if( !s_XBeeDevices.push( this ) )
+    {
+        ret_val = true;
+    }
+    
+    return ret_val;
+}
+#endif // defined XBEEAPI_CONFIG_USING_RTOS && defined XBEEAPI_CONFIG_RTOS_USE_DISPATCHER
+
 #if defined XBEEAPI_CONFIG_ENABLE_DEVELOPER
 
 #define PRINTABLE_ASCII_FIRST 32U