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