bluetooth control motor
Dependents: BLE_LED_IDB0XA1_demo MOTOR_BLE_V2 Motor_Ble_v1 Motor_Ble_v10223 ... more
Fork of X_NUCLEO_IDB0XA1 by
x-nucleo-idb0xa1/BlueNRGGattClient.h@229:9981f62cdb1a, 2016-05-16 (annotated)
- Committer:
- Andrea Palmieri
- Date:
- Mon May 16 17:22:03 2016 +0200
- Revision:
- 229:9981f62cdb1a
- Parent:
- 144:bdf5e8432131
- Child:
- 242:058b2e731adc
Fix issues and add features
- Fix handles management
- Fix UUIDs management
- Implement API to read random address
- Fix clearing/setting of ADV payload
- Fix scanning behaviour
- Fix scanning while a connection is ongoing
- Implement Char Descriptor discovery
- Implement scanning/advertising filter policy (White List partial management)
- Update underlying BlueNRG stack
- Cosmetics
Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Andrea Palmieri |
229:9981f62cdb1a | 1 | /* mbed Microcontroller Library |
Andrea Palmieri |
229:9981f62cdb1a | 2 | * Copyright (c) 2006-2013 ARM Limited |
Andrea Palmieri |
229:9981f62cdb1a | 3 | * |
Andrea Palmieri |
229:9981f62cdb1a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Andrea Palmieri |
229:9981f62cdb1a | 5 | * you may not use this file except in compliance with the License. |
Andrea Palmieri |
229:9981f62cdb1a | 6 | * You may obtain a copy of the License at |
Andrea Palmieri |
229:9981f62cdb1a | 7 | * |
Andrea Palmieri |
229:9981f62cdb1a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Andrea Palmieri |
229:9981f62cdb1a | 9 | * |
Andrea Palmieri |
229:9981f62cdb1a | 10 | * Unless required by applicable law or agreed to in writing, software |
Andrea Palmieri |
229:9981f62cdb1a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Andrea Palmieri |
229:9981f62cdb1a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Andrea Palmieri |
229:9981f62cdb1a | 13 | * See the License for the specific language governing permissions and |
Andrea Palmieri |
229:9981f62cdb1a | 14 | * limitations under the License. |
Andrea Palmieri |
229:9981f62cdb1a | 15 | */ |
Andrea Palmieri |
229:9981f62cdb1a | 16 | /** |
Andrea Palmieri |
229:9981f62cdb1a | 17 | ****************************************************************************** |
Andrea Palmieri |
229:9981f62cdb1a | 18 | * @file BlueNRGGattClient.cpp |
Andrea Palmieri |
229:9981f62cdb1a | 19 | * @author STMicroelectronics |
Andrea Palmieri |
229:9981f62cdb1a | 20 | * @brief Header file for BLE_API GattClient Class |
Andrea Palmieri |
229:9981f62cdb1a | 21 | ****************************************************************************** |
Andrea Palmieri |
229:9981f62cdb1a | 22 | * @copy |
Andrea Palmieri |
229:9981f62cdb1a | 23 | * |
Andrea Palmieri |
229:9981f62cdb1a | 24 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
Andrea Palmieri |
229:9981f62cdb1a | 25 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
Andrea Palmieri |
229:9981f62cdb1a | 26 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
Andrea Palmieri |
229:9981f62cdb1a | 27 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
Andrea Palmieri |
229:9981f62cdb1a | 28 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
Andrea Palmieri |
229:9981f62cdb1a | 29 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
Andrea Palmieri |
229:9981f62cdb1a | 30 | * |
Andrea Palmieri |
229:9981f62cdb1a | 31 | * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2> |
Andrea Palmieri |
229:9981f62cdb1a | 32 | */ |
Andrea Palmieri |
229:9981f62cdb1a | 33 | |
Andrea Palmieri |
229:9981f62cdb1a | 34 | #ifndef __BLUENRG_GATT_CLIENT_H__ |
Andrea Palmieri |
229:9981f62cdb1a | 35 | #define __BLUENRG_GATT_CLIENT_H__ |
Andrea Palmieri |
229:9981f62cdb1a | 36 | |
Andrea Palmieri |
229:9981f62cdb1a | 37 | #include "mbed-drivers/mbed.h" |
Andrea Palmieri |
229:9981f62cdb1a | 38 | #include "ble/blecommon.h" |
Andrea Palmieri |
229:9981f62cdb1a | 39 | #include "btle.h" |
Andrea Palmieri |
229:9981f62cdb1a | 40 | #include "ble/GattClient.h" |
Andrea Palmieri |
229:9981f62cdb1a | 41 | #include "ble/DiscoveredService.h" |
Andrea Palmieri |
229:9981f62cdb1a | 42 | #include "ble/CharacteristicDescriptorDiscovery.h" |
Andrea Palmieri |
229:9981f62cdb1a | 43 | #include "BlueNRGDiscoveredCharacteristic.h" |
Andrea Palmieri |
229:9981f62cdb1a | 44 | |
Andrea Palmieri |
229:9981f62cdb1a | 45 | using namespace std; |
Andrea Palmieri |
229:9981f62cdb1a | 46 | |
Andrea Palmieri |
229:9981f62cdb1a | 47 | #define BLE_TOTAL_DISCOVERED_SERVICES 10 |
Andrea Palmieri |
229:9981f62cdb1a | 48 | #define BLE_TOTAL_DISCOVERED_CHARS 10 |
Andrea Palmieri |
229:9981f62cdb1a | 49 | |
Andrea Palmieri |
229:9981f62cdb1a | 50 | class BlueNRGGattClient : public GattClient |
Andrea Palmieri |
229:9981f62cdb1a | 51 | { |
Andrea Palmieri |
229:9981f62cdb1a | 52 | public: |
Andrea Palmieri |
229:9981f62cdb1a | 53 | static BlueNRGGattClient &getInstance() { |
Andrea Palmieri |
229:9981f62cdb1a | 54 | static BlueNRGGattClient m_instance; |
Andrea Palmieri |
229:9981f62cdb1a | 55 | return m_instance; |
Andrea Palmieri |
229:9981f62cdb1a | 56 | } |
Andrea Palmieri |
229:9981f62cdb1a | 57 | |
Andrea Palmieri |
229:9981f62cdb1a | 58 | enum { |
Andrea Palmieri |
229:9981f62cdb1a | 59 | GATT_IDLE, |
Andrea Palmieri |
229:9981f62cdb1a | 60 | GATT_SERVICE_DISCOVERY, |
Andrea Palmieri |
229:9981f62cdb1a | 61 | GATT_CHAR_DESC_DISCOVERY, |
Andrea Palmieri |
229:9981f62cdb1a | 62 | //GATT_CHARS_DISCOVERY_COMPLETE, |
Andrea Palmieri |
229:9981f62cdb1a | 63 | //GATT_DISCOVERY_TERMINATED, |
Andrea Palmieri |
229:9981f62cdb1a | 64 | GATT_READ_CHAR, |
Andrea Palmieri |
229:9981f62cdb1a | 65 | GATT_WRITE_CHAR |
Andrea Palmieri |
229:9981f62cdb1a | 66 | }; |
Andrea Palmieri |
229:9981f62cdb1a | 67 | |
Andrea Palmieri |
229:9981f62cdb1a | 68 | /* Functions that must be implemented from GattClient */ |
Andrea Palmieri |
229:9981f62cdb1a | 69 | virtual ble_error_t launchServiceDiscovery(Gap::Handle_t connectionHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 70 | ServiceDiscovery::ServiceCallback_t sc = NULL, |
Andrea Palmieri |
229:9981f62cdb1a | 71 | ServiceDiscovery::CharacteristicCallback_t cc = NULL, |
Andrea Palmieri |
229:9981f62cdb1a | 72 | const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN), |
Andrea Palmieri |
229:9981f62cdb1a | 73 | const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)); |
Andrea Palmieri |
229:9981f62cdb1a | 74 | |
Andrea Palmieri |
229:9981f62cdb1a | 75 | virtual ble_error_t discoverServices(Gap::Handle_t connectionHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 76 | ServiceDiscovery::ServiceCallback_t callback, |
Andrea Palmieri |
229:9981f62cdb1a | 77 | const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)); |
Andrea Palmieri |
229:9981f62cdb1a | 78 | |
Andrea Palmieri |
229:9981f62cdb1a | 79 | virtual ble_error_t discoverServices(Gap::Handle_t connectionHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 80 | ServiceDiscovery::ServiceCallback_t callback, |
Andrea Palmieri |
229:9981f62cdb1a | 81 | GattAttribute::Handle_t startHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 82 | GattAttribute::Handle_t endHandle); |
Andrea Palmieri |
229:9981f62cdb1a | 83 | |
Andrea Palmieri |
229:9981f62cdb1a | 84 | virtual bool isServiceDiscoveryActive(void) const; |
Andrea Palmieri |
229:9981f62cdb1a | 85 | virtual void terminateServiceDiscovery(void); |
Andrea Palmieri |
229:9981f62cdb1a | 86 | virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) { |
Andrea Palmieri |
229:9981f62cdb1a | 87 | terminationCallback = callback; |
Andrea Palmieri |
229:9981f62cdb1a | 88 | } |
Andrea Palmieri |
229:9981f62cdb1a | 89 | virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const; |
Andrea Palmieri |
229:9981f62cdb1a | 90 | virtual ble_error_t write(GattClient::WriteOp_t cmd, |
Andrea Palmieri |
229:9981f62cdb1a | 91 | Gap::Handle_t connHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 92 | GattAttribute::Handle_t attributeHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 93 | size_t length, |
Andrea Palmieri |
229:9981f62cdb1a | 94 | const uint8_t *value) const; |
Andrea Palmieri |
229:9981f62cdb1a | 95 | virtual ble_error_t discoverCharacteristicDescriptors( |
Andrea Palmieri |
229:9981f62cdb1a | 96 | const DiscoveredCharacteristic& characteristic, |
Andrea Palmieri |
229:9981f62cdb1a | 97 | const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback, |
Andrea Palmieri |
229:9981f62cdb1a | 98 | const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback); |
Andrea Palmieri |
229:9981f62cdb1a | 99 | |
Andrea Palmieri |
229:9981f62cdb1a | 100 | virtual ble_error_t reset(void); |
Andrea Palmieri |
229:9981f62cdb1a | 101 | |
Andrea Palmieri |
229:9981f62cdb1a | 102 | void gattProcedureCompleteCB(Gap::Handle_t connectionHandle, uint8_t error_code); |
Andrea Palmieri |
229:9981f62cdb1a | 103 | |
Andrea Palmieri |
229:9981f62cdb1a | 104 | void primaryServicesCB(Gap::Handle_t connectionHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 105 | uint8_t event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 106 | uint8_t attribute_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 107 | uint8_t *attribute_data_list); |
Andrea Palmieri |
229:9981f62cdb1a | 108 | |
Andrea Palmieri |
229:9981f62cdb1a | 109 | void primaryServiceCB(Gap::Handle_t connectionHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 110 | uint8_t event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 111 | uint8_t *handles_info_list); |
Andrea Palmieri |
229:9981f62cdb1a | 112 | |
Andrea Palmieri |
229:9981f62cdb1a | 113 | ble_error_t findServiceChars(Gap::Handle_t connectionHandle); |
Andrea Palmieri |
229:9981f62cdb1a | 114 | |
Andrea Palmieri |
229:9981f62cdb1a | 115 | void serviceCharsCB(Gap::Handle_t connectionHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 116 | uint8_t event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 117 | uint8_t handle_value_pair_length, |
Andrea Palmieri |
229:9981f62cdb1a | 118 | uint8_t *handle_value_pair); |
Andrea Palmieri |
229:9981f62cdb1a | 119 | |
Andrea Palmieri |
229:9981f62cdb1a | 120 | void serviceCharByUUIDCB(Gap::Handle_t connectionHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 121 | uint8_t event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 122 | uint16_t attr_handle, |
Andrea Palmieri |
229:9981f62cdb1a | 123 | uint8_t *attr_value); |
Andrea Palmieri |
229:9981f62cdb1a | 124 | |
Andrea Palmieri |
229:9981f62cdb1a | 125 | void discAllCharacDescCB(Gap::Handle_t connHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 126 | uint8_t event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 127 | uint8_t format, |
Andrea Palmieri |
229:9981f62cdb1a | 128 | uint8_t *handle_uuid_pair); |
Andrea Palmieri |
229:9981f62cdb1a | 129 | |
Andrea Palmieri |
229:9981f62cdb1a | 130 | void charReadCB(Gap::Handle_t connHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 131 | uint8_t event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 132 | uint8_t* attribute_value); |
Andrea Palmieri |
229:9981f62cdb1a | 133 | |
Andrea Palmieri |
229:9981f62cdb1a | 134 | void charWritePrepareCB(Gap::Handle_t connHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 135 | uint8_t event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 136 | uint16_t attribute_handle, |
Andrea Palmieri |
229:9981f62cdb1a | 137 | uint16_t offset, |
Andrea Palmieri |
229:9981f62cdb1a | 138 | uint8_t *part_attr_value); |
Andrea Palmieri |
229:9981f62cdb1a | 139 | |
Andrea Palmieri |
229:9981f62cdb1a | 140 | void charWriteExecCB(Gap::Handle_t connHandle, |
Andrea Palmieri |
229:9981f62cdb1a | 141 | uint8_t event_data_length); |
Andrea Palmieri |
229:9981f62cdb1a | 142 | |
Andrea Palmieri |
229:9981f62cdb1a | 143 | protected: |
Andrea Palmieri |
229:9981f62cdb1a | 144 | |
Andrea Palmieri |
229:9981f62cdb1a | 145 | BlueNRGGattClient() { |
Andrea Palmieri |
229:9981f62cdb1a | 146 | _currentState = GATT_IDLE; |
Andrea Palmieri |
229:9981f62cdb1a | 147 | _matchingServiceUUID = BLE_UUID_UNKNOWN; |
Andrea Palmieri |
229:9981f62cdb1a | 148 | _matchingCharacteristicUUIDIn = BLE_UUID_UNKNOWN; |
Andrea Palmieri |
229:9981f62cdb1a | 149 | } |
Andrea Palmieri |
229:9981f62cdb1a | 150 | |
Andrea Palmieri |
229:9981f62cdb1a | 151 | ServiceDiscovery::ServiceCallback_t serviceDiscoveryCallback; |
Andrea Palmieri |
229:9981f62cdb1a | 152 | ServiceDiscovery::CharacteristicCallback_t characteristicDiscoveryCallback; |
Andrea Palmieri |
229:9981f62cdb1a | 153 | ServiceDiscovery::TerminationCallback_t terminationCallback; |
Andrea Palmieri |
229:9981f62cdb1a | 154 | CharacteristicDescriptorDiscovery::DiscoveryCallback_t charDescDiscoveryCallback; |
Andrea Palmieri |
229:9981f62cdb1a | 155 | CharacteristicDescriptorDiscovery::TerminationCallback_t charDescTerminationCallback; |
Andrea Palmieri |
229:9981f62cdb1a | 156 | |
Andrea Palmieri |
229:9981f62cdb1a | 157 | private: |
Andrea Palmieri |
229:9981f62cdb1a | 158 | |
Andrea Palmieri |
229:9981f62cdb1a | 159 | BlueNRGGattClient(BlueNRGGattClient const &); |
Andrea Palmieri |
229:9981f62cdb1a | 160 | void operator=(BlueNRGGattClient const &); |
Andrea Palmieri |
229:9981f62cdb1a | 161 | |
Andrea Palmieri |
229:9981f62cdb1a | 162 | Gap::Handle_t _connectionHandle; |
Andrea Palmieri |
229:9981f62cdb1a | 163 | DiscoveredService discoveredService[BLE_TOTAL_DISCOVERED_SERVICES]; |
Andrea Palmieri |
229:9981f62cdb1a | 164 | BlueNRGDiscoveredCharacteristic discoveredChar[BLE_TOTAL_DISCOVERED_CHARS]; |
Andrea Palmieri |
229:9981f62cdb1a | 165 | |
Andrea Palmieri |
229:9981f62cdb1a | 166 | GattReadCallbackParams readCBParams; |
Andrea Palmieri |
229:9981f62cdb1a | 167 | GattWriteCallbackParams writeCBParams; |
Andrea Palmieri |
229:9981f62cdb1a | 168 | |
Andrea Palmieri |
229:9981f62cdb1a | 169 | // The char for which the descriptor discovery has been launched |
Andrea Palmieri |
229:9981f62cdb1a | 170 | DiscoveredCharacteristic _characteristic; |
Andrea Palmieri |
229:9981f62cdb1a | 171 | |
Andrea Palmieri |
229:9981f62cdb1a | 172 | UUID _matchingServiceUUID; |
Andrea Palmieri |
229:9981f62cdb1a | 173 | UUID _matchingCharacteristicUUIDIn; |
Andrea Palmieri |
229:9981f62cdb1a | 174 | uint8_t _currentState; |
Andrea Palmieri |
229:9981f62cdb1a | 175 | uint8_t _numServices, _servIndex; |
Andrea Palmieri |
229:9981f62cdb1a | 176 | uint8_t _numChars; |
Andrea Palmieri |
229:9981f62cdb1a | 177 | uint8_t _numCharDesc; |
Andrea Palmieri |
229:9981f62cdb1a | 178 | |
Andrea Palmieri |
229:9981f62cdb1a | 179 | }; |
Andrea Palmieri |
229:9981f62cdb1a | 180 | |
Andrea Palmieri |
229:9981f62cdb1a | 181 | #endif /* __BLUENRG_GATT_CLIENT_H__ */ |