Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

PhoneAppIO.cpp

Committer:
AntonLS
Date:
2015-12-01
Revision:
19:afcbb425b3cf
Parent:
11:d3aa5fca2330
Child:
75:1b357bee1839

File content as of revision 19:afcbb425b3cf:

/*
 *  Buffered IO of pending CharacteristicWrites meant to Nofify
 *   the phone/tablet application and reading of Characteristic
 *   set by the phone/tablet application.    Started 20150416  ALS
 *
 */

#include "PhoneAppIO.h"

PhoneAppIO::PhoneAppIO( BLEDevice &ble, GattAttribute::Handle_t toPhoneHandle,
                                        GattAttribute::Handle_t toConeHandle,
                                        int txBufferSize, int rxBufferSize )
                      : mts::MTSBufferedIO( txBufferSize, rxBufferSize ),
                        bleP( &ble ), toPhoneHandle( toPhoneHandle ),
                                      toConeHandle( toConeHandle )
{
    loopbackMode  = false;
    bleWasntReady = false;

    enableTxIrq();
    enableRxIrq();

    data      = NULL;
    bytesRead = 0;

    memset( rx_buf, 0, TXRX_BUF_LEN );
    rx_len = 0;
}

PhoneAppIO::~PhoneAppIO()
{
}

uint16_t PhoneAppIO::maybeHandleRead( const GattCharacteristicWriteCBParams *params )  // Called by onDataWritten() from BLE.
{                                                                                      //  also writes to txPayload
    bytesRead = 0;

    if( params->charHandle == toConeHandle )
    {
        uint8_t buf[TXRX_BUF_LEN];
        bytesRead = MIN( params->len, TXRX_BUF_LEN );

        if( loopbackMode )
        {
            // Loopback data from Central.
            updateCharacteristic( toPhoneHandle, params->data, bytesRead );  // Notifies.  // TODO  apply retries to this as well,
                                                                                           //  but they might not be needed here.
        }

        bleP->readCharacteristicValue( toConeHandle, buf, &bytesRead );

        // Copy data to accessible location for others and handleRead().
        memset( txPayload, 0, TXRX_BUF_LEN );
        memcpy( txPayload, buf, bytesRead );

        // Buffer the data.
        data = (char *)txPayload;
        handleRead();
    }

    return  bytesRead;
}

uint16_t PhoneAppIO::injectHandleRead( char *buf, int len )  // Inject data from elsewhere simulating BLE-in.
{                                                            //  also writes to txPayload
    int bytesRead = 0;

    if( (NULL != buf) && (0 < len) )
    {
        bytesRead = MIN( len, TXRX_BUF_LEN );

        // Copy data to accessible location for others and handleRead().
        memset( txPayload, 0, TXRX_BUF_LEN );
        memcpy( txPayload, buf, bytesRead );

        // Buffer the data.
        data = (char *)txPayload;
        handleRead();
    }

    return  bytesRead;
}

// Maybe write to RX characteristic (From cone to phone.)
uint16_t PhoneAppIO::maybeHandleWrite()  // Called by toPhoneChk() from main loop.
{
    uint16_t charsWritten = 0;

    while( true )
    {
        if( !bleWasntReady )
        {
            char ch;

            while( writing );  // Block.

            if( 0 == txBuffer.read( ch ) )  break;

            if( '\n' == ch )  continue;  // Filter linefeeds.
            rx_buf[rx_len++] = ch;
        }
        if( bleWasntReady || rx_len>=TXRX_BUF_LEN || rx_buf[rx_len-1]=='\r' || rx_buf[rx_len-1]=='\0' )
        {
            handleWrite();

            if( bleWasntReady )  break;  // Don't loop on not ready (Don't starve main loop.)

            charsWritten = rx_len;

            rx_len = 0;
            break;
        }
    }

    return  charsWritten;
}

void PhoneAppIO::handleRead()
{
    while( reading );  // Block.

    int charsToBuf = rxBuffer.write( data, bytesRead );
    if( charsToBuf != bytesRead )
    {
        ::printf( "[ERROR] ToPhoneCharacteristic Data Lost, tried %d, got %d\r\n", bytesRead, charsToBuf );
    }
}

/** This method used to transfer data from the internal write buffer
* (txBuffer) to the physical interface, but we have an intermediate
* buffer (rx_buf) in case of failure, so it can be retried.
*/
void PhoneAppIO::handleWrite()
{
    bleWasntReady = updateCharacteristic( toPhoneHandle, rx_buf, rx_len );  // Notifies.
    busy = bleWasntReady;
}

// I think we don't need to use these because I'm guessing
//  all reads happen during main loop ble.waitForEvent()?

// For locking out read() during write() to prevent buffer state corruption.
void PhoneAppIO::disableTxIrq(){  writing = true;   }
void PhoneAppIO::enableTxIrq() {  writing = false;  }

// For locking out write() during read() to prevent buffer state corruption.
void PhoneAppIO::disableRxIrq(){  reading = true;   }
void PhoneAppIO::enableRxIrq() {  reading = false;  }

/* EOF */