Jerry Bradshaw
/
mbed-os-test
ddd
HSP/Hsp_BLE/BluetoothLE/BluetoothLE.h@0:e4a10ed6eb92, 2016-10-25 (annotated)
- Committer:
- jbradshaw
- Date:
- Tue Oct 25 15:22:11 2016 +0000
- Revision:
- 0:e4a10ed6eb92
tewt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbradshaw | 0:e4a10ed6eb92 | 1 | /******************************************************************************* |
jbradshaw | 0:e4a10ed6eb92 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
jbradshaw | 0:e4a10ed6eb92 | 3 | * |
jbradshaw | 0:e4a10ed6eb92 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
jbradshaw | 0:e4a10ed6eb92 | 5 | * copy of this software and associated documentation files (the "Software"), |
jbradshaw | 0:e4a10ed6eb92 | 6 | * to deal in the Software without restriction, including without limitation |
jbradshaw | 0:e4a10ed6eb92 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
jbradshaw | 0:e4a10ed6eb92 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
jbradshaw | 0:e4a10ed6eb92 | 9 | * Software is furnished to do so, subject to the following conditions: |
jbradshaw | 0:e4a10ed6eb92 | 10 | * |
jbradshaw | 0:e4a10ed6eb92 | 11 | * The above copyright notice and this permission notice shall be included |
jbradshaw | 0:e4a10ed6eb92 | 12 | * in all copies or substantial portions of the Software. |
jbradshaw | 0:e4a10ed6eb92 | 13 | * |
jbradshaw | 0:e4a10ed6eb92 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
jbradshaw | 0:e4a10ed6eb92 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
jbradshaw | 0:e4a10ed6eb92 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
jbradshaw | 0:e4a10ed6eb92 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
jbradshaw | 0:e4a10ed6eb92 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
jbradshaw | 0:e4a10ed6eb92 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
jbradshaw | 0:e4a10ed6eb92 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
jbradshaw | 0:e4a10ed6eb92 | 21 | * |
jbradshaw | 0:e4a10ed6eb92 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
jbradshaw | 0:e4a10ed6eb92 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
jbradshaw | 0:e4a10ed6eb92 | 24 | * Products, Inc. Branding Policy. |
jbradshaw | 0:e4a10ed6eb92 | 25 | * |
jbradshaw | 0:e4a10ed6eb92 | 26 | * The mere transfer of this software does not imply any licenses |
jbradshaw | 0:e4a10ed6eb92 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
jbradshaw | 0:e4a10ed6eb92 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
jbradshaw | 0:e4a10ed6eb92 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
jbradshaw | 0:e4a10ed6eb92 | 30 | * ownership rights. |
jbradshaw | 0:e4a10ed6eb92 | 31 | ******************************************************************************* |
jbradshaw | 0:e4a10ed6eb92 | 32 | */ |
jbradshaw | 0:e4a10ed6eb92 | 33 | #ifndef _BLUETOOTHLE_H_ |
jbradshaw | 0:e4a10ed6eb92 | 34 | #define _BLUETOOTHLE_H_ |
jbradshaw | 0:e4a10ed6eb92 | 35 | |
jbradshaw | 0:e4a10ed6eb92 | 36 | #include "mbed.h" |
jbradshaw | 0:e4a10ed6eb92 | 37 | #include "BLE.h" |
jbradshaw | 0:e4a10ed6eb92 | 38 | #include "Characteristic.h" |
jbradshaw | 0:e4a10ed6eb92 | 39 | |
jbradshaw | 0:e4a10ed6eb92 | 40 | class BluetoothLE { |
jbradshaw | 0:e4a10ed6eb92 | 41 | public: |
jbradshaw | 0:e4a10ed6eb92 | 42 | BluetoothLE(BLE *ble, int numberOfCharacteristics); |
jbradshaw | 0:e4a10ed6eb92 | 43 | ~BluetoothLE(void); |
jbradshaw | 0:e4a10ed6eb92 | 44 | void addCharacteristic(Characteristic *characteristic); |
jbradshaw | 0:e4a10ed6eb92 | 45 | void initService(uint8_t *serialNumber, uint8_t *deviceName, int nameSize, uint8_t *serviceUUID); |
jbradshaw | 0:e4a10ed6eb92 | 46 | void notifyCharacteristic(int index, uint8_t *data); |
jbradshaw | 0:e4a10ed6eb92 | 47 | |
jbradshaw | 0:e4a10ed6eb92 | 48 | |
jbradshaw | 0:e4a10ed6eb92 | 49 | /** |
jbradshaw | 0:e4a10ed6eb92 | 50 | * @brief Return a characteristic based on incoming index |
jbradshaw | 0:e4a10ed6eb92 | 51 | * @param index index into an array of characteristics |
jbradshaw | 0:e4a10ed6eb92 | 52 | */ |
jbradshaw | 0:e4a10ed6eb92 | 53 | Characteristic *getCharacteristic(int index) { |
jbradshaw | 0:e4a10ed6eb92 | 54 | return characteristics[index]; |
jbradshaw | 0:e4a10ed6eb92 | 55 | } |
jbradshaw | 0:e4a10ed6eb92 | 56 | |
jbradshaw | 0:e4a10ed6eb92 | 57 | /** |
jbradshaw | 0:e4a10ed6eb92 | 58 | * @brief Get the payload for this characteristic |
jbradshaw | 0:e4a10ed6eb92 | 59 | */ |
jbradshaw | 0:e4a10ed6eb92 | 60 | uint8_t *getCharacteristicPayload(int index) { |
jbradshaw | 0:e4a10ed6eb92 | 61 | return characteristics[index]->getPayloadPtr(); |
jbradshaw | 0:e4a10ed6eb92 | 62 | } |
jbradshaw | 0:e4a10ed6eb92 | 63 | typedef void (*PtrFunction)(int index); |
jbradshaw | 0:e4a10ed6eb92 | 64 | |
jbradshaw | 0:e4a10ed6eb92 | 65 | /** |
jbradshaw | 0:e4a10ed6eb92 | 66 | * @brief Used to connect a connection callback |
jbradshaw | 0:e4a10ed6eb92 | 67 | */ |
jbradshaw | 0:e4a10ed6eb92 | 68 | template<typename T> |
jbradshaw | 0:e4a10ed6eb92 | 69 | void onConnect(T *object, void (T::*member)(void)) { |
jbradshaw | 0:e4a10ed6eb92 | 70 | _onConnect.attach( object, member ); |
jbradshaw | 0:e4a10ed6eb92 | 71 | } |
jbradshaw | 0:e4a10ed6eb92 | 72 | /** |
jbradshaw | 0:e4a10ed6eb92 | 73 | * @brief Used to connect a disconnection callback |
jbradshaw | 0:e4a10ed6eb92 | 74 | */ |
jbradshaw | 0:e4a10ed6eb92 | 75 | template<typename T> |
jbradshaw | 0:e4a10ed6eb92 | 76 | void onDisconnect(T *object, void (T::*member)(void)) { |
jbradshaw | 0:e4a10ed6eb92 | 77 | _onDisconnect.attach( object, member ); |
jbradshaw | 0:e4a10ed6eb92 | 78 | } |
jbradshaw | 0:e4a10ed6eb92 | 79 | /** |
jbradshaw | 0:e4a10ed6eb92 | 80 | * @brief Used to connect a characteristic written callback |
jbradshaw | 0:e4a10ed6eb92 | 81 | */ |
jbradshaw | 0:e4a10ed6eb92 | 82 | void onDataWritten(PtrFunction _onDataWritten) { |
jbradshaw | 0:e4a10ed6eb92 | 83 | this->_onDataWritten = _onDataWritten; |
jbradshaw | 0:e4a10ed6eb92 | 84 | } |
jbradshaw | 0:e4a10ed6eb92 | 85 | /** |
jbradshaw | 0:e4a10ed6eb92 | 86 | * @brief Get the connection state of the BLE |
jbradshaw | 0:e4a10ed6eb92 | 87 | * @return true if connection BLE connection is present, false if no connection |
jbradshaw | 0:e4a10ed6eb92 | 88 | */ |
jbradshaw | 0:e4a10ed6eb92 | 89 | bool isConnected(void); |
jbradshaw | 0:e4a10ed6eb92 | 90 | |
jbradshaw | 0:e4a10ed6eb92 | 91 | /** |
jbradshaw | 0:e4a10ed6eb92 | 92 | * @brief Perform a notification test |
jbradshaw | 0:e4a10ed6eb92 | 93 | * @param index Index into an array of characteristic objects |
jbradshaw | 0:e4a10ed6eb92 | 94 | */ |
jbradshaw | 0:e4a10ed6eb92 | 95 | void notifyCharacteristicTest(int index); |
jbradshaw | 0:e4a10ed6eb92 | 96 | |
jbradshaw | 0:e4a10ed6eb92 | 97 | /** |
jbradshaw | 0:e4a10ed6eb92 | 98 | * @brief Get the data that was written to the indexed characteristic |
jbradshaw | 0:e4a10ed6eb92 | 99 | * @return true if connection BLE connection is present, false if no connection |
jbradshaw | 0:e4a10ed6eb92 | 100 | */ |
jbradshaw | 0:e4a10ed6eb92 | 101 | uint8_t *getDataWritten(int index, int *length); |
jbradshaw | 0:e4a10ed6eb92 | 102 | |
jbradshaw | 0:e4a10ed6eb92 | 103 | private: |
jbradshaw | 0:e4a10ed6eb92 | 104 | /** |
jbradshaw | 0:e4a10ed6eb92 | 105 | * @brief Disconnection callback |
jbradshaw | 0:e4a10ed6eb92 | 106 | */ |
jbradshaw | 0:e4a10ed6eb92 | 107 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params); |
jbradshaw | 0:e4a10ed6eb92 | 108 | /** |
jbradshaw | 0:e4a10ed6eb92 | 109 | * @brief Connection callback |
jbradshaw | 0:e4a10ed6eb92 | 110 | */ |
jbradshaw | 0:e4a10ed6eb92 | 111 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params); |
jbradshaw | 0:e4a10ed6eb92 | 112 | /** |
jbradshaw | 0:e4a10ed6eb92 | 113 | * @brief Characteristic written callback |
jbradshaw | 0:e4a10ed6eb92 | 114 | */ |
jbradshaw | 0:e4a10ed6eb92 | 115 | void onDataWritten(const GattWriteCallbackParams *params); |
jbradshaw | 0:e4a10ed6eb92 | 116 | |
jbradshaw | 0:e4a10ed6eb92 | 117 | /// array of characteristic class pointers |
jbradshaw | 0:e4a10ed6eb92 | 118 | Characteristic **characteristics; |
jbradshaw | 0:e4a10ed6eb92 | 119 | /// array of gatt characteristic pointers |
jbradshaw | 0:e4a10ed6eb92 | 120 | GattCharacteristic **gattCharacteristics; |
jbradshaw | 0:e4a10ed6eb92 | 121 | /// pointer to mbed BLE layer |
jbradshaw | 0:e4a10ed6eb92 | 122 | BLE *ble; |
jbradshaw | 0:e4a10ed6eb92 | 123 | /// total number of characteristics |
jbradshaw | 0:e4a10ed6eb92 | 124 | int numberOfCharacteristics; |
jbradshaw | 0:e4a10ed6eb92 | 125 | /// flag to keep track of BLE connection state |
jbradshaw | 0:e4a10ed6eb92 | 126 | bool _isConnected; |
jbradshaw | 0:e4a10ed6eb92 | 127 | /// running index for building characteristic array |
jbradshaw | 0:e4a10ed6eb92 | 128 | int runningIndex; |
jbradshaw | 0:e4a10ed6eb92 | 129 | /// callback function for when a connection is made |
jbradshaw | 0:e4a10ed6eb92 | 130 | FunctionPointer _onConnect; |
jbradshaw | 0:e4a10ed6eb92 | 131 | /// callback function for when a connection is disconnected |
jbradshaw | 0:e4a10ed6eb92 | 132 | FunctionPointer _onDisconnect; |
jbradshaw | 0:e4a10ed6eb92 | 133 | /// callback function for when characteristic data is written |
jbradshaw | 0:e4a10ed6eb92 | 134 | PtrFunction _onDataWritten; |
jbradshaw | 0:e4a10ed6eb92 | 135 | }; |
jbradshaw | 0:e4a10ed6eb92 | 136 | |
jbradshaw | 0:e4a10ed6eb92 | 137 | #endif // _BLUETOOTHLE_H_ |