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.

Remote/XBeeDeviceRemoteAt.cpp

Committer:
johnb
Date:
2014-07-28
Revision:
52:0950b05d5270
Parent:
51:a7d0d2ef9261
Child:
54:9f5b7652943e

File content as of revision 52:0950b05d5270:

/** 

Copyright 2014 John Bailey
   
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

#include "XBeeDeviceRemoteAt.hpp"

#define XBEE_API_CMD_REMOTE_REQ_HEADER_LEN 14U

template< typename T >
class XBeeApiCmdAtRemoteSet : public XBeeApiFrame {
    uint8_t m_buffer[ XBEE_API_CMD_REMOTE_REQ_HEADER_LEN + sizeof( T ) ];
    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 
        */
        XBeeApiCmdAtRemoteSet( const uint8_t        p_frameId,
                               const uint16_t       p_addr16Bit,
                               const uint64_t       p_addr64Bit,
                               const bool           p_applyChanges,
                               const uint8_t* const p_data,
                               const T p_val );
        /** Destructor */
        virtual ~XBeeApiCmdAtRemoteSet();
};

class XBeeApiCmdAtRemoteReq : public XBeeApiFrame {
    uint8_t m_buffer[ XBEE_API_CMD_REMOTE_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 
        */
        XBeeApiCmdAtRemoteReq( const uint8_t        p_frameId,
                               const uint16_t       p_addr16Bit,
                               const uint64_t       p_addr64Bit,
                               const uint8_t* const p_data );
        /** Destructor */
        virtual ~XBeeApiCmdAtRemoteReq();
};

XBeeDeviceRemoteAt::XBeeDeviceRemoteAt( XBeeDevice* p_device,
                                        const uint16_t& p_addr16Bit,
                                        const uint64_t& p_addr64Bit,
                                        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 )
{
}

void XBeeDeviceRemoteAt::setApplyChanges( const bool p_apply )
{
    m_applyChanges = p_apply;
}

#define XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS (XBEE_CMD_POSN_ID_SPECIFIC_DATA+1)
#define XBEE_REMOTE_AT_RESPONSE_16BIT_ADDRESS (XBEE_REMOTE_AT_RESPONSE_64BIT_ADDRESS + sizeof( uint64_t))
#define XBEE_REMOTE_AT_RESPONSE_STATUS (17U)

size_t XBeeDeviceRemoteAt::getResponseStatusPos( void ) const
{
    return XBEE_REMOTE_AT_RESPONSE_STATUS;
}

bool XBeeDeviceRemoteAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
{
    bool ret_val = false;
 
    /* TODO: Length check */
 
    if( XBEE_CMD_REMOTE_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] )
    {
        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((( 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 );
        }
    }
    else
    {
        XBeeApiCmdAt::decodeCallback( p_data, p_len );
    }
    
    return ret_val;
}

void XBeeDeviceRemoteAt::SendCmd_uint8_t( const uint8_t        p_frameId,
                                          const uint8_t* const p_data,
                                          const uint8_t&       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 );
}

void XBeeDeviceRemoteAt::SendCmd_uint16_t( const uint8_t        p_frameId,
                                           const uint8_t* const p_data,
                                           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 );
     m_device->SendFrame( &req );
}

void XBeeDeviceRemoteAt::SendCmd_uint32_t( const uint8_t        p_frameId,
                                           const uint8_t* const p_data,
                                           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 );
     m_device->SendFrame( &req );
}

void XBeeDeviceRemoteAt::SendReq( const uint8_t             p_frameId,
                                  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 );
     m_device->SendFrame( &req );
}

template < typename T >
XBeeApiCmdAtRemoteSet<T>::XBeeApiCmdAtRemoteSet( const uint8_t        p_frameId,
                                                 const uint16_t       p_addr16Bit,
                                                 const uint64_t       p_addr64Bit,
                                                 const bool           p_applyChanges,
                                                 const uint8_t* const p_data,
                                                 const T p_val ) : XBeeApiFrame( )
{
    size_t s;
    uint8_t* dest;
    const uint8_t* src = (uint8_t*)(&p_val);
    
    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;

    m_buffer[11] = p_applyChanges << 1;

    m_buffer[12] = p_data[0];
    m_buffer[13] = 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;
}

template < typename T >
XBeeApiCmdAtRemoteSet<T>::~XBeeApiCmdAtRemoteSet()
{
}

XBeeApiCmdAtRemoteReq::XBeeApiCmdAtRemoteReq( const uint8_t        p_frameId,
                                              const uint16_t       p_addr16Bit,
                                              const uint64_t       p_addr64Bit,
                                              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;

    m_buffer[11] = 0;

    m_buffer[12] = p_data[0];
    m_buffer[13] = p_data[1];
    
    m_dataLen = sizeof( m_buffer );
    m_data = m_buffer;
}

XBeeApiCmdAtRemoteReq::~XBeeApiCmdAtRemoteReq()
{
}