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