diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp index 0f91af16..ff55248c 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp @@ -37,9 +37,6 @@ #include "bluefruit.h" #include "utility/TimeoutTimer.h" -// TODO larger ATT MTU -#define TXD_FIFO_SIZE (BLE_GATT_ATT_MTU_DEFAULT-3) - /* UART Serivce: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E * UART RXD : 6E400002-B5A3-F393-E0A9-E50E24DCCA9E * UART TXD : 6E400003-B5A3-F393-E0A9-E50E24DCCA9E @@ -121,7 +118,7 @@ void bleuart_txd_buffered_hdlr(TimerHandle_t timer) if ( !svc->_tx_fifo ) return; // flush tx data - svc->flush_tx_buffered(); + (void) svc->flush_tx_buffered(); } void bleuart_txd_cccd_cb(BLECharacteristic& chr, uint16_t value) @@ -133,7 +130,7 @@ void bleuart_txd_cccd_cb(BLECharacteristic& chr, uint16_t value) // Enable TXD timer if configured if (value & BLE_GATT_HVX_NOTIFICATION) { - xTimerStart(svc._buffered_th, 0); + xTimerStart(svc._buffered_th, 0); // if started --> timer got reset }else { xTimerStop(svc._buffered_th, 0); @@ -163,7 +160,7 @@ void BLEUart::bufferTXD(uint8_t enable) if ( _tx_fifo == NULL ) { _tx_fifo = new Adafruit_FIFO(1); - _tx_fifo->begin(TXD_FIFO_SIZE); + _tx_fifo->begin(BLEGATT_ATT_MTU_MAX); } }else { @@ -185,7 +182,7 @@ err_t BLEUart::begin(void) _txd.setProperties(CHR_PROPS_NOTIFY); // TODO enable encryption when bonding is enabled _txd.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); -// _txd.setMaxLen() + _txd.setMaxLen(BLEGATT_ATT_MTU_MAX); _txd.setUserDescriptor("TXD"); VERIFY_STATUS( _txd.begin() ); @@ -195,7 +192,7 @@ err_t BLEUart::begin(void) // TODO enable encryption when bonding is enabled _rxd.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN); - // _rxd.setMaxLen() + _rxd.setMaxLen(BLEGATT_ATT_MTU_MAX); _rxd.setUserDescriptor("RXD"); VERIFY_STATUS(_rxd.begin()); @@ -225,6 +222,9 @@ void BLEUart::_connect_cb (void) // create TXD timer TODO take connInterval into account // ((5*ms2tick(Bluefruit.connInterval())) / 4) / 2 _buffered_th = xTimerCreate(NULL, ms2tick(10), true, this, bleuart_txd_buffered_hdlr); + + // Start the timer + xTimerStart(_buffered_th, 0); } } @@ -257,20 +257,21 @@ size_t BLEUart::write (const uint8_t *content, size_t len) { uint16_t written = _tx_fifo->write(content, len); - if ( !_tx_fifo->full() ) + // TODO multiple prph connections + // Not up to GATT MTU, notify will be sent later by TXD timer handler + if ( _tx_fifo->count() < (Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3) ) { - // Not full, notify will be sent later by txd timer handler return len; } else { - // TX fifo is full, send notify right away + // TX fifo has enough data, send notify right away VERIFY( flush_tx_buffered(), 0); // still more data left, send them all if ( written < len ) { - VERIFY( _txd.notify(content+written, len-written), TXD_FIFO_SIZE ); + VERIFY( _txd.notify(content+written, len-written), written); } return len; @@ -296,15 +297,22 @@ void BLEUart::flush (void) bool BLEUart::flush_tx_buffered(void) { - uint8_t ff_data[TXD_FIFO_SIZE]; - uint16_t len = _tx_fifo->read(ff_data, sizeof(ff_data)); + uint16_t max_hvx = Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3; + uint8_t* ff_data = (uint8_t*) rtos_malloc( max_hvx ); + + if (!ff_data) return false; + + uint16_t len = _tx_fifo->read(ff_data, max_hvx); + bool result = true; if ( len ) { - return _txd.notify(ff_data, len); + result = _txd.notify(ff_data, len); } - return true; + rtos_free(ff_data); + + return result; }