fork BLE_API to add update adv payload API
Fork of BLE_API by
ble/services/UARTService.h@718:cb41f5a422a9, 2015-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Jul 02 09:06:11 2015 +0100
- Revision:
- 718:cb41f5a422a9
- Parent:
- 717:3129b149a6b2
- Child:
- 722:eab9499e4250
Synchronized with git rev 26015995
Author: Rohit Grover
#46: rename GATT_MTU_SIZE_DEFAULT to BLE_GATT_MTU_SIZE_DEFAULT. This avoids a name conflict with the contant defined in Nordic-SDK.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 712:b04b5db36865 | 1 | /* mbed Microcontroller Library |
rgrover1 | 712:b04b5db36865 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 712:b04b5db36865 | 3 | * |
rgrover1 | 712:b04b5db36865 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 712:b04b5db36865 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 712:b04b5db36865 | 6 | * You may obtain a copy of the License at |
rgrover1 | 712:b04b5db36865 | 7 | * |
rgrover1 | 712:b04b5db36865 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 712:b04b5db36865 | 9 | * |
rgrover1 | 712:b04b5db36865 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 712:b04b5db36865 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 712:b04b5db36865 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 712:b04b5db36865 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 712:b04b5db36865 | 14 | * limitations under the License. |
rgrover1 | 712:b04b5db36865 | 15 | */ |
rgrover1 | 712:b04b5db36865 | 16 | |
rgrover1 | 712:b04b5db36865 | 17 | #ifndef __BLE_UART_SERVICE_H__ |
rgrover1 | 712:b04b5db36865 | 18 | #define __BLE_UART_SERVICE_H__ |
rgrover1 | 712:b04b5db36865 | 19 | |
rgrover1 | 712:b04b5db36865 | 20 | #include "mbed.h" |
rgrover1 | 712:b04b5db36865 | 21 | #include "Stream.h" |
rgrover1 | 712:b04b5db36865 | 22 | |
rgrover1 | 712:b04b5db36865 | 23 | #include "ble/UUID.h" |
rgrover1 | 712:b04b5db36865 | 24 | #include "ble/BLE.h" |
rgrover1 | 712:b04b5db36865 | 25 | |
rgrover1 | 712:b04b5db36865 | 26 | extern const uint8_t UARTServiceBaseUUID[UUID::LENGTH_OF_LONG_UUID]; |
rgrover1 | 712:b04b5db36865 | 27 | extern const uint16_t UARTServiceShortUUID; |
rgrover1 | 712:b04b5db36865 | 28 | extern const uint16_t UARTServiceTXCharacteristicShortUUID; |
rgrover1 | 712:b04b5db36865 | 29 | extern const uint16_t UARTServiceRXCharacteristicShortUUID; |
rgrover1 | 712:b04b5db36865 | 30 | |
rgrover1 | 712:b04b5db36865 | 31 | extern const uint8_t UARTServiceUUID[UUID::LENGTH_OF_LONG_UUID]; |
rgrover1 | 712:b04b5db36865 | 32 | extern const uint8_t UARTServiceUUID_reversed[UUID::LENGTH_OF_LONG_UUID]; |
rgrover1 | 712:b04b5db36865 | 33 | |
rgrover1 | 712:b04b5db36865 | 34 | extern const uint8_t UARTServiceTXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID]; |
rgrover1 | 712:b04b5db36865 | 35 | extern const uint8_t UARTServiceRXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID]; |
rgrover1 | 712:b04b5db36865 | 36 | |
rgrover1 | 712:b04b5db36865 | 37 | /** |
rgrover1 | 712:b04b5db36865 | 38 | * @class UARTService |
rgrover1 | 712:b04b5db36865 | 39 | * @brief BLE Service to enable UART over BLE |
rgrover1 | 712:b04b5db36865 | 40 | */ |
rgrover1 | 712:b04b5db36865 | 41 | class UARTService { |
rgrover1 | 712:b04b5db36865 | 42 | public: |
rgrover1 | 712:b04b5db36865 | 43 | /**< Maximum length of data (in bytes) that can be transmitted by the UART service module to the peer. */ |
rgrover1 | 718:cb41f5a422a9 | 44 | static const unsigned BLE_UART_SERVICE_MAX_DATA_LEN = (BLE_GATT_MTU_SIZE_DEFAULT - 3); |
rgrover1 | 712:b04b5db36865 | 45 | |
rgrover1 | 712:b04b5db36865 | 46 | public: |
rgrover1 | 712:b04b5db36865 | 47 | |
rgrover1 | 712:b04b5db36865 | 48 | /** |
rgrover1 | 712:b04b5db36865 | 49 | * @param[ref] ble |
rgrover1 | 712:b04b5db36865 | 50 | * BLE object for the underlying controller. |
rgrover1 | 712:b04b5db36865 | 51 | */ |
rgrover1 | 712:b04b5db36865 | 52 | UARTService(BLE &_ble) : |
rgrover1 | 712:b04b5db36865 | 53 | ble(_ble), |
rgrover1 | 712:b04b5db36865 | 54 | receiveBuffer(), |
rgrover1 | 712:b04b5db36865 | 55 | sendBuffer(), |
rgrover1 | 712:b04b5db36865 | 56 | sendBufferIndex(0), |
rgrover1 | 712:b04b5db36865 | 57 | numBytesReceived(0), |
rgrover1 | 712:b04b5db36865 | 58 | receiveBufferIndex(0), |
rgrover1 | 712:b04b5db36865 | 59 | txCharacteristic(UARTServiceTXCharacteristicUUID, receiveBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN, |
rgrover1 | 712:b04b5db36865 | 60 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), |
rgrover1 | 712:b04b5db36865 | 61 | rxCharacteristic(UARTServiceRXCharacteristicUUID, sendBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
rgrover1 | 712:b04b5db36865 | 62 | GattCharacteristic *charTable[] = {&txCharacteristic, &rxCharacteristic}; |
rgrover1 | 712:b04b5db36865 | 63 | GattService uartService(UARTServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 712:b04b5db36865 | 64 | |
rgrover1 | 712:b04b5db36865 | 65 | ble.addService(uartService); |
rgrover1 | 712:b04b5db36865 | 66 | ble.onDataWritten(this, &UARTService::onDataWritten); |
rgrover1 | 712:b04b5db36865 | 67 | } |
rgrover1 | 712:b04b5db36865 | 68 | |
rgrover1 | 712:b04b5db36865 | 69 | /** |
rgrover1 | 712:b04b5db36865 | 70 | * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service. |
rgrover1 | 712:b04b5db36865 | 71 | */ |
rgrover1 | 712:b04b5db36865 | 72 | uint16_t getTXCharacteristicHandle() { |
rgrover1 | 712:b04b5db36865 | 73 | return txCharacteristic.getValueAttribute().getHandle(); |
rgrover1 | 712:b04b5db36865 | 74 | } |
rgrover1 | 712:b04b5db36865 | 75 | |
rgrover1 | 712:b04b5db36865 | 76 | /** |
rgrover1 | 712:b04b5db36865 | 77 | * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service. |
rgrover1 | 712:b04b5db36865 | 78 | */ |
rgrover1 | 712:b04b5db36865 | 79 | uint16_t getRXCharacteristicHandle() { |
rgrover1 | 712:b04b5db36865 | 80 | return rxCharacteristic.getValueAttribute().getHandle(); |
rgrover1 | 712:b04b5db36865 | 81 | } |
rgrover1 | 712:b04b5db36865 | 82 | |
rgrover1 | 712:b04b5db36865 | 83 | /** |
rgrover1 | 712:b04b5db36865 | 84 | * We attempt to collect bytes before pushing them to the UART RX |
rgrover1 | 712:b04b5db36865 | 85 | * characteristic--writing to the RX characteristic will then generate |
rgrover1 | 712:b04b5db36865 | 86 | * notifications for the client. Updates made in quick succession to a |
rgrover1 | 712:b04b5db36865 | 87 | * notification-generating characteristic will result in data being buffered |
rgrover1 | 712:b04b5db36865 | 88 | * in the bluetooth stack as notifications are sent out. The stack will have |
rgrover1 | 712:b04b5db36865 | 89 | * its limits for this buffering; typically a small number under 10. |
rgrover1 | 712:b04b5db36865 | 90 | * Collecting data into the sendBuffer buffer helps mitigate the rate of |
rgrover1 | 712:b04b5db36865 | 91 | * updates. But we shouldn't buffer a large amount of data before updating |
rgrover1 | 712:b04b5db36865 | 92 | * the characteristic otherwise the client will need to turn around and make |
rgrover1 | 712:b04b5db36865 | 93 | * a long read request; this is because notifications include only the first |
rgrover1 | 712:b04b5db36865 | 94 | * 20 bytes of the updated data. |
rgrover1 | 712:b04b5db36865 | 95 | * |
rgrover1 | 712:b04b5db36865 | 96 | * @param buffer The received update |
rgrover1 | 712:b04b5db36865 | 97 | * @param length Amount of characters to be appended. |
rgrover1 | 712:b04b5db36865 | 98 | * @return Amount of characters appended to the rxCharacteristic. |
rgrover1 | 712:b04b5db36865 | 99 | */ |
rgrover1 | 712:b04b5db36865 | 100 | size_t write(const void *_buffer, size_t length) { |
rgrover1 | 712:b04b5db36865 | 101 | size_t origLength = length; |
rgrover1 | 712:b04b5db36865 | 102 | const uint8_t *buffer = static_cast<const uint8_t *>(_buffer); |
rgrover1 | 712:b04b5db36865 | 103 | |
rgrover1 | 712:b04b5db36865 | 104 | if (ble.getGapState().connected) { |
rgrover1 | 712:b04b5db36865 | 105 | unsigned bufferIndex = 0; |
rgrover1 | 712:b04b5db36865 | 106 | while (length) { |
rgrover1 | 712:b04b5db36865 | 107 | unsigned bytesRemainingInSendBuffer = BLE_UART_SERVICE_MAX_DATA_LEN - sendBufferIndex; |
rgrover1 | 712:b04b5db36865 | 108 | unsigned bytesToCopy = (length < bytesRemainingInSendBuffer) ? length : bytesRemainingInSendBuffer; |
rgrover1 | 712:b04b5db36865 | 109 | |
rgrover1 | 712:b04b5db36865 | 110 | /* copy bytes into sendBuffer */ |
rgrover1 | 712:b04b5db36865 | 111 | memcpy(&sendBuffer[sendBufferIndex], &buffer[bufferIndex], bytesToCopy); |
rgrover1 | 712:b04b5db36865 | 112 | length -= bytesToCopy; |
rgrover1 | 712:b04b5db36865 | 113 | sendBufferIndex += bytesToCopy; |
rgrover1 | 712:b04b5db36865 | 114 | bufferIndex += bytesToCopy; |
rgrover1 | 712:b04b5db36865 | 115 | |
rgrover1 | 712:b04b5db36865 | 116 | /* have we collected enough? */ |
rgrover1 | 712:b04b5db36865 | 117 | if ((sendBufferIndex == BLE_UART_SERVICE_MAX_DATA_LEN) || |
rgrover1 | 712:b04b5db36865 | 118 | // (sendBuffer[sendBufferIndex - 1] == '\r') || |
rgrover1 | 712:b04b5db36865 | 119 | (sendBuffer[sendBufferIndex - 1] == '\n')) { |
rgrover1 | 712:b04b5db36865 | 120 | ble.updateCharacteristicValue(getRXCharacteristicHandle(), static_cast<const uint8_t *>(sendBuffer), sendBufferIndex); |
rgrover1 | 712:b04b5db36865 | 121 | sendBufferIndex = 0; |
rgrover1 | 712:b04b5db36865 | 122 | } |
rgrover1 | 712:b04b5db36865 | 123 | } |
rgrover1 | 712:b04b5db36865 | 124 | } |
rgrover1 | 712:b04b5db36865 | 125 | |
rgrover1 | 712:b04b5db36865 | 126 | return origLength; |
rgrover1 | 712:b04b5db36865 | 127 | } |
rgrover1 | 712:b04b5db36865 | 128 | |
rgrover1 | 712:b04b5db36865 | 129 | /** |
rgrover1 | 712:b04b5db36865 | 130 | * Helper function to write out strings. |
rgrover1 | 712:b04b5db36865 | 131 | * @param str The received string. |
rgrover1 | 712:b04b5db36865 | 132 | * @return Amount of characters appended to the rxCharacteristic. |
rgrover1 | 712:b04b5db36865 | 133 | */ |
rgrover1 | 712:b04b5db36865 | 134 | size_t writeString(const char *str) { |
rgrover1 | 712:b04b5db36865 | 135 | return write(str, strlen(str)); |
rgrover1 | 712:b04b5db36865 | 136 | } |
rgrover1 | 712:b04b5db36865 | 137 | |
rgrover1 | 712:b04b5db36865 | 138 | /** |
rgrover1 | 712:b04b5db36865 | 139 | * Override for Stream::_putc() |
rgrover1 | 712:b04b5db36865 | 140 | * @param c |
rgrover1 | 712:b04b5db36865 | 141 | * This function writes the character c, cast to an unsigned char, to stream. |
rgrover1 | 712:b04b5db36865 | 142 | * @return |
rgrover1 | 712:b04b5db36865 | 143 | * The character written as an unsigned char cast to an int or EOF on error. |
rgrover1 | 712:b04b5db36865 | 144 | */ |
rgrover1 | 712:b04b5db36865 | 145 | int _putc(int c) { |
rgrover1 | 712:b04b5db36865 | 146 | return (write(&c, 1) == 1) ? 1 : EOF; |
rgrover1 | 712:b04b5db36865 | 147 | } |
rgrover1 | 712:b04b5db36865 | 148 | |
rgrover1 | 712:b04b5db36865 | 149 | /** |
rgrover1 | 712:b04b5db36865 | 150 | * Override for Stream::_getc() |
rgrover1 | 712:b04b5db36865 | 151 | * @return |
rgrover1 | 712:b04b5db36865 | 152 | * The character read. |
rgrover1 | 712:b04b5db36865 | 153 | */ |
rgrover1 | 712:b04b5db36865 | 154 | int _getc() { |
rgrover1 | 712:b04b5db36865 | 155 | if (receiveBufferIndex == numBytesReceived) { |
rgrover1 | 712:b04b5db36865 | 156 | return EOF; |
rgrover1 | 712:b04b5db36865 | 157 | } |
rgrover1 | 712:b04b5db36865 | 158 | |
rgrover1 | 712:b04b5db36865 | 159 | return receiveBuffer[receiveBufferIndex++]; |
rgrover1 | 712:b04b5db36865 | 160 | } |
rgrover1 | 712:b04b5db36865 | 161 | |
rgrover1 | 712:b04b5db36865 | 162 | private: |
rgrover1 | 712:b04b5db36865 | 163 | /** |
rgrover1 | 712:b04b5db36865 | 164 | * This callback allows the UART service to receive updates to the |
rgrover1 | 712:b04b5db36865 | 165 | * txCharacteristic. The application should forward the call to this |
rgrover1 | 712:b04b5db36865 | 166 | * function from the global onDataWritten() callback handler; or if that's |
rgrover1 | 712:b04b5db36865 | 167 | * not used, this method can be used as a callback directly. |
rgrover1 | 712:b04b5db36865 | 168 | */ |
rgrover1 | 712:b04b5db36865 | 169 | void onDataWritten(const GattWriteCallbackParams *params) { |
rgrover1 | 712:b04b5db36865 | 170 | if (params->handle == getTXCharacteristicHandle()) { |
rgrover1 | 712:b04b5db36865 | 171 | uint16_t bytesRead = params->len; |
rgrover1 | 712:b04b5db36865 | 172 | if (bytesRead <= BLE_UART_SERVICE_MAX_DATA_LEN) { |
rgrover1 | 712:b04b5db36865 | 173 | numBytesReceived = bytesRead; |
rgrover1 | 712:b04b5db36865 | 174 | receiveBufferIndex = 0; |
rgrover1 | 712:b04b5db36865 | 175 | memcpy(receiveBuffer, params->data, numBytesReceived); |
rgrover1 | 712:b04b5db36865 | 176 | } |
rgrover1 | 712:b04b5db36865 | 177 | } |
rgrover1 | 712:b04b5db36865 | 178 | } |
rgrover1 | 712:b04b5db36865 | 179 | |
rgrover1 | 712:b04b5db36865 | 180 | private: |
rgrover1 | 712:b04b5db36865 | 181 | BLE &ble; |
rgrover1 | 712:b04b5db36865 | 182 | |
rgrover1 | 712:b04b5db36865 | 183 | uint8_t receiveBuffer[BLE_UART_SERVICE_MAX_DATA_LEN]; /**< The local buffer into which we receive |
rgrover1 | 712:b04b5db36865 | 184 | * inbound data before forwarding it to the |
rgrover1 | 712:b04b5db36865 | 185 | * application. */ |
rgrover1 | 712:b04b5db36865 | 186 | |
rgrover1 | 712:b04b5db36865 | 187 | uint8_t sendBuffer[BLE_UART_SERVICE_MAX_DATA_LEN]; /**< The local buffer into which outbound data is |
rgrover1 | 712:b04b5db36865 | 188 | * accumulated before being pushed to the |
rgrover1 | 712:b04b5db36865 | 189 | * rxCharacteristic. */ |
rgrover1 | 712:b04b5db36865 | 190 | uint8_t sendBufferIndex; |
rgrover1 | 712:b04b5db36865 | 191 | uint8_t numBytesReceived; |
rgrover1 | 712:b04b5db36865 | 192 | uint8_t receiveBufferIndex; |
rgrover1 | 712:b04b5db36865 | 193 | |
rgrover1 | 712:b04b5db36865 | 194 | GattCharacteristic txCharacteristic; /**< From the point of view of the external client, this is the characteristic |
rgrover1 | 712:b04b5db36865 | 195 | * they'd write into in order to communicate with this application. */ |
rgrover1 | 712:b04b5db36865 | 196 | GattCharacteristic rxCharacteristic; /**< From the point of view of the external client, this is the characteristic |
rgrover1 | 712:b04b5db36865 | 197 | * they'd read from in order to receive the bytes transmitted by this |
rgrover1 | 712:b04b5db36865 | 198 | * application. */ |
rgrover1 | 712:b04b5db36865 | 199 | }; |
rgrover1 | 712:b04b5db36865 | 200 | |
rgrover1 | 712:b04b5db36865 | 201 | #endif /* #ifndef __BLE_UART_SERVICE_H__*/ |