Tomasz Trela / BLE_API

Fork of BLE_API by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UARTService.h Source File

UARTService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_UART_SERVICE_H__
00018 #define __BLE_UART_SERVICE_H__
00019 
00020 #include "mbed.h"
00021 #include "Stream.h"
00022 
00023 #include "ble/UUID.h"
00024 #include "ble/BLE.h"
00025 
00026 extern const uint8_t  UARTServiceBaseUUID[UUID::LENGTH_OF_LONG_UUID];
00027 extern const uint16_t UARTServiceShortUUID;
00028 extern const uint16_t UARTServiceTXCharacteristicShortUUID;
00029 extern const uint16_t UARTServiceRXCharacteristicShortUUID;
00030 
00031 extern const uint8_t  UARTServiceUUID[UUID::LENGTH_OF_LONG_UUID];
00032 extern const uint8_t  UARTServiceUUID_reversed[UUID::LENGTH_OF_LONG_UUID];
00033 
00034 extern const uint8_t  UARTServiceTXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID];
00035 extern const uint8_t  UARTServiceRXCharacteristicUUID[UUID::LENGTH_OF_LONG_UUID];
00036 
00037 /**
00038 * @class UARTService
00039 * @brief BLE Service to enable UART over BLE
00040 */
00041 class UARTService {
00042 public:
00043     /**< Maximum length of data (in bytes) that can be transmitted by the UART service module to the peer. */
00044     static const unsigned BLE_UART_SERVICE_MAX_DATA_LEN = (BLE_GATT_MTU_SIZE_DEFAULT - 3);
00045 
00046 public:
00047 
00048     /**
00049     * @param[ref] ble
00050     *               BLE object for the underlying controller.
00051     */
00052     UARTService (BLE &_ble) :
00053         ble(_ble),
00054         receiveBuffer(),
00055         sendBuffer(),
00056         sendBufferIndex(0),
00057         numBytesReceived(0),
00058         receiveBufferIndex(0),
00059         txCharacteristic(UARTServiceTXCharacteristicUUID, receiveBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN,
00060                          GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
00061         rxCharacteristic(UARTServiceRXCharacteristicUUID, sendBuffer, 1, BLE_UART_SERVICE_MAX_DATA_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
00062         
00063         rxCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM);
00064         txCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM);
00065         
00066         GattCharacteristic *charTable[] = {&txCharacteristic, &rxCharacteristic};
00067         GattService         uartService(UARTServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00068 
00069         ble.addService(uartService);
00070         ble.onDataWritten(this, &UARTService::onDataWritten);
00071     }
00072 
00073     /**
00074      * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service.
00075      */
00076     uint16_t getTXCharacteristicHandle() {
00077         return txCharacteristic.getValueAttribute().getHandle();
00078     }
00079 
00080     /**
00081      * Note: TX and RX characteristics are to be interpreted from the viewpoint of the GATT client using this service.
00082      */
00083     uint16_t getRXCharacteristicHandle() {
00084         return rxCharacteristic.getValueAttribute().getHandle();
00085     }
00086 
00087     /**
00088      * We attempt to collect bytes before pushing them to the UART RX
00089      * characteristic--writing to the RX characteristic will then generate
00090      * notifications for the client. Updates made in quick succession to a
00091      * notification-generating characteristic will result in data being buffered
00092      * in the bluetooth stack as notifications are sent out. The stack will have
00093      * its limits for this buffering; typically a small number under 10.
00094      * Collecting data into the sendBuffer buffer helps mitigate the rate of
00095      * updates. But we shouldn't buffer a large amount of data before updating
00096      * the characteristic otherwise the client will need to turn around and make
00097      * a long read request; this is because notifications include only the first
00098      * 20 bytes of the updated data.
00099      *
00100      * @param  buffer The received update
00101      * @param  length Amount of characters to be appended.
00102      * @return        Amount of characters appended to the rxCharacteristic.
00103      */
00104     size_t write(const void *_buffer, size_t length) {
00105         size_t         origLength = length;
00106         const uint8_t *buffer     = static_cast<const uint8_t *>(_buffer);
00107 
00108         if (ble.getGapState().connected) {
00109             unsigned bufferIndex = 0;
00110             while (length) {
00111                 unsigned bytesRemainingInSendBuffer = BLE_UART_SERVICE_MAX_DATA_LEN - sendBufferIndex;
00112                 unsigned bytesToCopy                = (length < bytesRemainingInSendBuffer) ? length : bytesRemainingInSendBuffer;
00113 
00114                 /* copy bytes into sendBuffer */
00115                 memcpy(&sendBuffer[sendBufferIndex], &buffer[bufferIndex], bytesToCopy);
00116                 length          -= bytesToCopy;
00117                 sendBufferIndex += bytesToCopy;
00118                 bufferIndex     += bytesToCopy;
00119 
00120                 /* have we collected enough? */
00121                 if ((sendBufferIndex == BLE_UART_SERVICE_MAX_DATA_LEN) ||
00122                     // (sendBuffer[sendBufferIndex - 1] == '\r')          ||
00123                     (sendBuffer[sendBufferIndex - 1] == '\n')) {
00124                     ble.gattServer().write(getRXCharacteristicHandle(), static_cast<const uint8_t *>(sendBuffer), sendBufferIndex);
00125                     sendBufferIndex = 0;
00126                 }
00127             }
00128         }
00129 
00130         return origLength;
00131     }
00132 
00133     /**
00134      * Helper function to write out strings.
00135      * @param  str The received string.
00136      * @return     Amount of characters appended to the rxCharacteristic.
00137      */
00138     size_t writeString(const char *str) {
00139         return write(str, strlen(str));
00140     }
00141 
00142     /**
00143      * Override for Stream::_putc()
00144      * @param  c
00145      *         This function writes the character c, cast to an unsigned char, to stream.
00146      * @return
00147      *     The character written as an unsigned char cast to an int or EOF on error.
00148      */
00149     int _putc(int c) {
00150         return (write(&c, 1) == 1) ? 1 : EOF;
00151     }
00152 
00153     /**
00154      * Override for Stream::_getc()
00155      * @return
00156      *     The character read.
00157      */
00158     int _getc() {
00159         if (receiveBufferIndex == numBytesReceived) {
00160             return EOF;
00161         }
00162 
00163         return receiveBuffer[receiveBufferIndex++];
00164     }
00165 
00166 protected:
00167     /**
00168      * This callback allows the UART service to receive updates to the
00169      * txCharacteristic. The application should forward the call to this
00170      * function from the global onDataWritten() callback handler; or if that's
00171      * not used, this method can be used as a callback directly.
00172      */
00173     void onDataWritten(const GattWriteCallbackParams *params) {
00174         if (params->handle == getTXCharacteristicHandle()) {
00175             uint16_t bytesRead = params->len;
00176             if (bytesRead <= BLE_UART_SERVICE_MAX_DATA_LEN) {
00177                 numBytesReceived   = bytesRead;
00178                 receiveBufferIndex = 0;
00179                 memcpy(receiveBuffer, params->data, numBytesReceived);
00180             }
00181         }
00182     }
00183 
00184 protected:
00185     BLE                &ble;
00186 
00187     uint8_t             receiveBuffer[BLE_UART_SERVICE_MAX_DATA_LEN]; /**< The local buffer into which we receive
00188                                                                        *   inbound data before forwarding it to the
00189                                                                        *   application. */
00190 
00191     uint8_t             sendBuffer[BLE_UART_SERVICE_MAX_DATA_LEN];    /**< The local buffer into which outbound data is
00192                                                                        *   accumulated before being pushed to the
00193                                                                        *   rxCharacteristic. */
00194     uint8_t             sendBufferIndex;
00195     uint8_t             numBytesReceived;
00196     uint8_t             receiveBufferIndex;
00197 
00198     GattCharacteristic  txCharacteristic; /**< From the point of view of the external client, this is the characteristic
00199                                            *   they'd write into in order to communicate with this application. */
00200     GattCharacteristic  rxCharacteristic; /**< From the point of view of the external client, this is the characteristic
00201                                            *   they'd read from in order to receive the bytes transmitted by this
00202                                            *   application. */
00203 };
00204 
00205 #endif /* #ifndef __BLE_UART_SERVICE_H__*/