17 #ifndef __BLE_UART_SERVICE_H__ 18 #define __BLE_UART_SERVICE_H__ 20 #ifdef YOTTA_CFG_MBED_OS 21 #include "mbed-drivers/mbed.h" 22 #include "mbed-drivers/Stream.h" 29 #include "ble/pal/Deprecated.h" 31 #if BLE_FEATURE_GATT_SERVER 33 BLE_DEPRECATED_API_USE_BEGIN()
35 extern const uint8_t UARTServiceBaseUUID[
UUID::LENGTH_OF_LONG_UUID];
36 extern const uint16_t UARTServiceShortUUID;
37 extern const uint16_t UARTServiceTXCharacteristicShortUUID;
38 extern const uint16_t UARTServiceRXCharacteristicShortUUID;
40 extern const uint8_t UARTServiceUUID[
UUID::LENGTH_OF_LONG_UUID];
41 extern const uint8_t UARTServiceUUID_reversed[
UUID::LENGTH_OF_LONG_UUID];
43 extern const uint8_t UARTServiceTXCharacteristicUUID[
UUID::LENGTH_OF_LONG_UUID];
44 extern const uint8_t UARTServiceRXCharacteristicUUID[
UUID::LENGTH_OF_LONG_UUID];
54 "This service is deprecated, and no replacement is currently available."
73 receiveBufferIndex(0),
74 txCharacteristic(UARTServiceTXCharacteristicUUID, receiveBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN,
76 rxCharacteristic(UARTServiceRXCharacteristicUUID, sendBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
80 ble.addService(uartService);
88 return txCharacteristic.getValueAttribute().getHandle();
95 return rxCharacteristic.getValueAttribute().getHandle();
115 size_t write(
const void *_buffer,
size_t length) {
116 size_t origLength = length;
117 const uint8_t *buffer =
static_cast<const uint8_t *
>(_buffer);
119 if (
ble.getGapState().connected) {
120 unsigned bufferIndex = 0;
122 unsigned bytesRemainingInSendBuffer = BLE_UART_SERVICE_MAX_DATA_LEN - sendBufferIndex;
123 unsigned bytesToCopy = (length < bytesRemainingInSendBuffer) ? length : bytesRemainingInSendBuffer;
126 memcpy(&sendBuffer[sendBufferIndex], &buffer[bufferIndex], bytesToCopy);
127 length -= bytesToCopy;
128 sendBufferIndex += bytesToCopy;
129 bufferIndex += bytesToCopy;
132 if ((sendBufferIndex == BLE_UART_SERVICE_MAX_DATA_LEN) ||
134 (sendBuffer[sendBufferIndex - 1] ==
'\n')) {
135 ble.gattServer().write(getRXCharacteristicHandle(), static_cast<const uint8_t *>(sendBuffer), sendBufferIndex);
150 return write(str, strlen(str));
158 if (
ble.getGapState().connected) {
159 if (sendBufferIndex != 0) {
160 ble.gattServer().write(getRXCharacteristicHandle(), static_cast<const uint8_t *>(sendBuffer), sendBufferIndex);
174 return (write(&c, 1) == 1) ? 1 : EOF;
183 if (receiveBufferIndex == numBytesReceived) {
187 return receiveBuffer[receiveBufferIndex++];
198 if (params->
handle == getTXCharacteristicHandle()) {
199 uint16_t bytesRead = params->
len;
200 if (bytesRead <= BLE_UART_SERVICE_MAX_DATA_LEN) {
201 numBytesReceived = bytesRead;
202 receiveBufferIndex = 0;
203 memcpy(receiveBuffer, params->
data, numBytesReceived);
211 uint8_t receiveBuffer[BLE_UART_SERVICE_MAX_DATA_LEN];
215 uint8_t sendBuffer[BLE_UART_SERVICE_MAX_DATA_LEN];
218 uint8_t sendBufferIndex;
219 uint8_t numBytesReceived;
220 uint8_t receiveBufferIndex;
229 BLE_DEPRECATED_API_USE_END()
231 #endif // BLE_FEATURE_GATT_SERVER size_t write(const void *_buffer, size_t length)
We attempt to collect bytes before pushing them to the UART RX characteristic; writing to the RX char...
BLE Service to enable UART over BLE.
int _putc(int c)
Override for Stream::_putc().
const uint8_t * data
Pointer to the data to write.
size_t writeString(const char *str)
Helper function to write out strings.
Abstract away BLE-capable radio transceivers or SOCs.
static const unsigned BLE_GATT_MTU_SIZE_DEFAULT
Default MTU size.
GattCharacteristic rxCharacteristic
From the point of view of the external client, this is the characteristic they'd read from in order t...
GattCharacteristic txCharacteristic
From the point of view of the external client, this is the characteristic they'd write into in order ...
Representation of a Universally Unique Identifier (UUID).
void onDataWritten(const GattWriteCallbackParams *params)
This callback allows the UART service to receive updates to the txCharacteristic. ...
GATT Write event definition.
uint16_t len
Length (in bytes) of the data to write.
Representation of a GattServer characteristic.
int _getc()
Override for Stream::_getc().
void flush()
Flush sendBuffer, i.e., forcefully write its contents to the UART RX characteristic even if the buffe...
GattAttribute::Handle_t handle
Handle of the attribute to which the write operation applies.
Representation of a GattServer service.
uint16_t getTXCharacteristicHandle()
Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using thi...
Entry namespace for all BLE API definitions.
uint16_t getRXCharacteristicHandle()
Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using thi...