From 2fd13bffca28eac35b58e2138f38707237fabf81 Mon Sep 17 00:00:00 2001 From: Akos Kiss Date: Tue, 29 Aug 2017 18:53:21 +0200 Subject: [PATCH] Add flush to the BLE UART Service If only buffer-full events and LF characters trigger the flush of the send buffer then only line-based communication can be implemented over the BLE UART Service. This patch extends the service API by adding an explicit `flush` method to force sending the buffer contents, thus enabling protocols with short (e.g., single character) messages. --- features/FEATURE_BLE/ble/services/UARTService.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/features/FEATURE_BLE/ble/services/UARTService.h b/features/FEATURE_BLE/ble/services/UARTService.h index 1043ddbf46..e8701ab155 100644 --- a/features/FEATURE_BLE/ble/services/UARTService.h +++ b/features/FEATURE_BLE/ble/services/UARTService.h @@ -140,6 +140,19 @@ public: return write(str, strlen(str)); } + /** + * Flush sendBuffer, i.e., forcefully write its contents to the UART RX + * characteristic even if the buffer is not full. + */ + void flush() { + if (ble.getGapState().connected) { + if (sendBufferIndex != 0) { + ble.gattServer().write(getRXCharacteristicHandle(), static_cast(sendBuffer), sendBufferIndex); + sendBufferIndex = 0; + } + } + } + /** * Override for Stream::_putc(). * @param c