Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

PhoneAppIO.h

Committer:
AntonLS
Date:
2016-01-04
Revision:
30:c60b0d52b067
Parent:
23:26f27c462976
Child:
41:dee3fd34e37a

File content as of revision 30:c60b0d52b067:

/*
 *  True Agility
 *
 *  Buffered IO of pending CharacteristicWrites meant to Nofify
 *   the phone/tablet application.        Started 20150416  ALS
 *
 */

#ifndef PHONEAPPIO_H
#define PHONEAPPIO_H

#include "mbed.h"
#include "BLEDevice.h"
#include <cstdarg>
#include <cstdio>
#include "MTSBufferedIO.h"
#include "Vars.h"

#define STRING_STACK_LIMIT     64 /* 120 */
#define TXRX_BUF_LEN           20

extern uint8_t txPayload[];
extern bool    updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead );

// namespace moo
// {

class PhoneAppIO : public mts::MTSBufferedIO
{
  public:
    PhoneAppIO( BLEDevice &ble, GattAttribute::Handle_t toPhoneHandle,
                                GattAttribute::Handle_t toConeHandle,
                                int txBufferSize=1280, int rxBufferSize=120 );

    /** Destructs a PhoneAppIO object and frees all related resources, including
    * internal buffers.
    */
    ~PhoneAppIO();

    /** Write a string to the buffer
     *
     * @param str The string to write (append your own CR)
     *
     * @returns 0 if the write succeeds, EOF for error
     */
    int puts( const char *str )
    {
        return  write( str, strlen( str ) );
    }

    int printf( const char *format, ... )
    {
        va_list arg;
        va_start( arg, format );

        int len = PhoneAppIO::vprintf( format, arg );

        va_end( arg );

        return  len;
    }
    int vprintf( const char *format, va_list arg )
    {
        int len = vsnprintf( NULL, 0, format, arg );
        if( len < STRING_STACK_LIMIT )
        {
            char temp[STRING_STACK_LIMIT];
            vsnprintf( temp, sizeof(temp), format, arg );
            puts( temp );

        } else
          {
              char *temp = new char[len + 1];
              vsprintf( temp, format, arg );
              puts( temp );
              delete[] temp;
          }

        return  len;
    }

    int putchar( int ch )
    {
        return  ((0 == write( (char)ch )) ? EOF : ch);
    }

    int getchar()
    {
        char ch;
        return  ((0 == atomicRead( ch )) ? EOF : ch);
    }

    uint16_t maybeHandleRead( const GattCharacteristicWriteCBParams *params );  // Called by onDataWritten() from BLE.
    uint16_t maybeHandleWrite();                                                // Called by toPhoneChk() from main loop.
    uint16_t injectHandleRead( char *buf, int len );                            // Inject data from elsewhere simulating BLE-in.

    bool loopbackMode;
    bool busy;

  protected:
    BLEDevice *bleP;
    bool      bleWasntReady;

    GattAttribute::Handle_t  toPhoneHandle;
    GattAttribute::Handle_t  toConeHandle;

    char     *data;
    uint16_t  bytesRead;

    uint8_t   rx_buf[TXRX_BUF_LEN];
    uint8_t   rx_len;

  private:
    virtual void handleWrite(); // Method for handling data to be written
    virtual void handleRead();  // Method for handling data to be read

    // For locking out read() during write() to prevent buffer state corruption.
    virtual void disableTxIrq();
    virtual void enableTxIrq();

    // For locking out write() during read() to prevent buffer state corruption.
    virtual void disableRxIrq();
    virtual void enableRxIrq();

    bool writing;
    bool reading;
};

// }

#endif /* PHONEAPPIO_H */

/* EOF */