Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of football_project by
Diff: PhoneAppIO.cpp
- Revision:
- 11:d3aa5fca2330
- Parent:
- 5:1b9734e68327
- Child:
- 19:afcbb425b3cf
--- a/PhoneAppIO.cpp Tue Apr 21 07:00:55 2015 +0000
+++ b/PhoneAppIO.cpp Thu Apr 23 06:36:57 2015 +0000
@@ -1,63 +1,130 @@
/*
* Buffered IO of pending CharacteristicWrites meant to Nofify
- * the phone/tablet application. ALS 20150416
+ * the phone/tablet application and reading of Characteristic
+ * set by the phone/tablet application. Started 20150416 ALS
*
*/
#include "PhoneAppIO.h"
-PhoneAppIO::PhoneAppIO( BLEDevice *ble, int txBufferSize, int rxBufferSize )
- : mts::MTSBufferedIO( txBufferSize, rxBufferSize )
+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 )
{
- PhoneAppIO::ble = ble;
- data = NULL;
- bytesRead = 0;
+ 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;
-void PhoneAppIO::toPhoneBuf( uint8_t *data, uint16_t bytesRead )
+ 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;
+}
+
+// Maybe write to RX characteristic (From cone to phone.)
+uint16_t PhoneAppIO::maybeHandleWrite() // Called by toPhoneChk() from main loop.
{
- PhoneAppIO::data = data;
- PhoneAppIO::bytesRead = bytesRead;
- handleRead();
+ 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()
{
- int charsToBuf = rxBuffer.write( (char *)data, bytesRead );
+ 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 );
+ ::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()
{
- /** This method should be used to transfer
- * data from the internal write buffer (txBuffer) to the physical interface.
- * Note that this function is called everytime new data is written to the
- * txBuffer though one of the write calls.
- */
-
- while( txBuffer.size() != 0 )
- {
- if( 1 /* ble. writeable() */ )
- {
- char byte;
- if( txBuffer.read( byte ) == 1 )
- {
- // Write Characteristic
- }
-
- } else
- {
- return;
- }
- }
+ 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 */
