OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
18 #include "base/win/scoped_handle.h" | 18 #include "base/win/scoped_handle.h" |
19 #include "device/bluetooth/bluetooth_adapter.h" | 19 #include "device/bluetooth/bluetooth_adapter.h" |
20 #include "device/bluetooth/bluetooth_export.h" | 20 #include "device/bluetooth/bluetooth_export.h" |
| 21 #include "device/bluetooth/bluetooth_low_energy_win.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 | 24 |
24 class SequencedTaskRunner; | 25 class SequencedTaskRunner; |
25 class SequencedWorkerPool; | 26 class SequencedWorkerPool; |
26 | 27 |
27 } // namespace base | 28 } // namespace base |
28 | 29 |
29 namespace device { | 30 namespace device { |
30 | 31 |
(...skipping 18 matching lines...) Expand all Loading... |
49 | 50 |
50 struct DEVICE_BLUETOOTH_EXPORT ServiceRecordState { | 51 struct DEVICE_BLUETOOTH_EXPORT ServiceRecordState { |
51 ServiceRecordState(); | 52 ServiceRecordState(); |
52 ~ServiceRecordState(); | 53 ~ServiceRecordState(); |
53 // Properties common to Bluetooth Classic and LE devices. | 54 // Properties common to Bluetooth Classic and LE devices. |
54 std::string name; | 55 std::string name; |
55 // Properties specific to Bluetooth Classic devices. | 56 // Properties specific to Bluetooth Classic devices. |
56 std::vector<uint8_t> sdp_bytes; | 57 std::vector<uint8_t> sdp_bytes; |
57 // Properties specific to Bluetooth LE devices. | 58 // Properties specific to Bluetooth LE devices. |
58 BluetoothUUID gatt_uuid; | 59 BluetoothUUID gatt_uuid; |
| 60 uint16_t gatt_attribute_handle; |
| 61 base::FilePath path; |
59 }; | 62 }; |
60 | 63 |
61 struct DEVICE_BLUETOOTH_EXPORT DeviceState { | 64 struct DEVICE_BLUETOOTH_EXPORT DeviceState { |
62 DeviceState(); | 65 DeviceState(); |
63 ~DeviceState(); | 66 ~DeviceState(); |
64 | 67 |
65 bool is_bluetooth_classic() const { return path.empty(); } | 68 bool is_bluetooth_classic() const { return path.empty(); } |
66 | 69 |
67 // Properties common to Bluetooth Classic and LE devices. | 70 // Properties common to Bluetooth Classic and LE devices. |
68 std::string address; // This uniquely identifies the device. | 71 std::string address; // This uniquely identifies the device. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); | 108 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); |
106 void Shutdown(); | 109 void Shutdown(); |
107 | 110 |
108 void PostSetPoweredBluetoothTask( | 111 void PostSetPoweredBluetoothTask( |
109 bool powered, | 112 bool powered, |
110 const base::Closure& callback, | 113 const base::Closure& callback, |
111 const BluetoothAdapter::ErrorCallback& error_callback); | 114 const BluetoothAdapter::ErrorCallback& error_callback); |
112 void PostStartDiscoveryTask(); | 115 void PostStartDiscoveryTask(); |
113 void PostStopDiscoveryTask(); | 116 void PostStopDiscoveryTask(); |
114 | 117 |
| 118 // Callbacks of asynchronous operations of GATT services, characteristics and |
| 119 // descriptors. |
| 120 typedef base::Callback<void(HRESULT)> HResultCallback; |
| 121 typedef base::Callback<void(PBTH_LE_GATT_SERVICE, uint16_t, HRESULT)> |
| 122 GetGattIncludedServicesCallback; |
| 123 typedef base::Callback<void(PBTH_LE_GATT_CHARACTERISTIC, uint16_t, HRESULT)> |
| 124 GetGattIncludedCharacteristicsCallback; |
| 125 typedef base::Callback<void(PBTH_LE_GATT_CHARACTERISTIC_VALUE, HRESULT)> |
| 126 ReadCharacteristicValueCallback; |
| 127 typedef base::Callback<void(PBTH_LE_GATT_DESCRIPTOR, uint16_t, HRESULT)> |
| 128 GetGattIncludedDescriptorsCallback; |
| 129 typedef base::Callback<void(PBTH_LE_GATT_DESCRIPTOR_VALUE, HRESULT)> |
| 130 ReadDescriptorValueCallback; |
| 131 typedef base::Callback<void(BLUETOOTH_GATT_EVENT_HANDLE, HRESULT)> |
| 132 GattEventRegistrationCallback; |
| 133 |
| 134 // Get all included services of a given service. The service is uniquely |
| 135 // identified by its |uuid| and |attribute_handle| on service device |
| 136 // |service_path|. The result is returned asynchronously through |callback|. |
| 137 void PostGetGattIncludedServices( |
| 138 const base::FilePath& service_path, |
| 139 const BluetoothUUID& uuid, |
| 140 uint16_t attribute_handle, |
| 141 const GetGattIncludedServicesCallback& callback); |
| 142 |
| 143 // Get all included characteristics of a given service. The service is |
| 144 // uniquely identified by its |uuid| and |attribute_handle| on service device |
| 145 // |service_path|. The result is returned asynchronously through |callback|. |
| 146 void PostGetGattIncludedCharacteristics( |
| 147 const base::FilePath& service_path, |
| 148 const BluetoothUUID& uuid, |
| 149 uint16_t attribute_handle, |
| 150 const GetGattIncludedCharacteristicsCallback& callback); |
| 151 |
| 152 // Read the value of a given |characteristic| in service |service_path|. The |
| 153 // result is returned asynchronously through |callback|. |
| 154 void PostReadCharacteristicValue( |
| 155 const base::FilePath& device_path, |
| 156 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 157 const ReadCharacteristicValueCallback& callback); |
| 158 |
| 159 // Write the value of a given |characteristic| in service |service_path| with |
| 160 // the |new_value|. The operation result is returned asynchronously through |
| 161 // |callback|. |
| 162 void PostWriteCharacteristicValue( |
| 163 const base::FilePath& service_path, |
| 164 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 165 const std::vector<uint8_t>& new_value, |
| 166 const HResultCallback& callback); |
| 167 |
| 168 // Reliable write the value of a given |characteristic| in service |
| 169 // |service_path| with the |new_value|. The operation result is returned |
| 170 // asynchronously through |callback|. |
| 171 void PostReliableWriteCharacteristicValue( |
| 172 const base::FilePath& service_path, |
| 173 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 174 const std::vector<uint8_t>& new_value, |
| 175 const HResultCallback& callback); |
| 176 |
| 177 // Get included descriptors of a given |characterisitc| in service |
| 178 // |service_path|. The result is returned asynchronously through |callback|. |
| 179 void PostGetGattIncludedDescriptors( |
| 180 const base::FilePath& service_path, |
| 181 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 182 const GetGattIncludedDescriptorsCallback& callback); |
| 183 |
| 184 // Read a given |descriptor| value in service |service_path|. The result is |
| 185 // returned asynchronously through |callback|. |
| 186 void PostReadDescriptorValue(const base::FilePath& service_path, |
| 187 const PBTH_LE_GATT_DESCRIPTOR descriptor, |
| 188 const ReadDescriptorValueCallback& callback); |
| 189 |
| 190 // Write a given |descriptor| value in service |service_path| with the |
| 191 // |new_value|. The operation result is returned asynchronously through |
| 192 // |callback|. |
| 193 void PostWriteDescriptorValue(const base::FilePath& service_path, |
| 194 const PBTH_LE_GATT_DESCRIPTOR descriptor, |
| 195 const std::vector<uint8_t>& new_value, |
| 196 const HResultCallback& callback); |
| 197 |
| 198 // Register a given |characteristic|'s value change notification of a service |
| 199 // |service_path| with |registered_callback|. |context| is the given void |
| 200 // pointer send back to caller through |registered_callback|. The operation |
| 201 // result is returned asynchronously through |callback|. |
| 202 void PostRegisterCharacteristicValueChangedEvent( |
| 203 const base::FilePath& service_path, |
| 204 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 205 const GattEventRegistrationCallback& callback, |
| 206 PFNBLUETOOTH_GATT_EVENT_CALLBACK registered_callback, |
| 207 void* context); |
| 208 |
| 209 // Unregister characteristic value change notification. |event_handle| was |
| 210 // returned by PostRegisterCharacteristicValueChangedEvent. |
| 211 void UnregisterCharacteristicValueChangedEvent( |
| 212 BLUETOOTH_GATT_EVENT_HANDLE event_handle); |
| 213 |
| 214 BluetoothUUID BluetoothLowEnergyUuidToBluetoothUuid( |
| 215 const BTH_LE_UUID& bth_le_uuid); |
| 216 |
115 private: | 217 private: |
116 friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>; | 218 friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>; |
117 friend class BluetoothTaskManagerWinTest; | 219 friend class BluetoothTaskManagerWinTest; |
118 | 220 |
119 static const int kPollIntervalMs; | 221 static const int kPollIntervalMs; |
120 | 222 |
121 virtual ~BluetoothTaskManagerWin(); | 223 virtual ~BluetoothTaskManagerWin(); |
122 | 224 |
123 // Logs Win32 errors occurring during polling on the worker thread. The method | 225 // Logs Win32 errors occurring during polling on the worker thread. The method |
124 // may discard messages to avoid logging being too verbose. | 226 // may discard messages to avoid logging being too verbose. |
125 void LogPollingError(const char* message, int win32_error); | 227 void LogPollingError(const char* message, int win32_error); |
126 | 228 |
127 // Notify all Observers of updated AdapterState. Should only be called on the | 229 // Notify all Observers of updated AdapterState. Should only be called on the |
128 // UI thread. | 230 // UI thread. |
129 void OnAdapterStateChanged(const AdapterState* state); | 231 void OnAdapterStateChanged(const AdapterState* state); |
130 void OnDiscoveryStarted(bool success); | 232 void OnDiscoveryStarted(bool success); |
131 void OnDiscoveryStopped(); | 233 void OnDiscoveryStopped(); |
132 void OnDevicesPolled(const ScopedVector<DeviceState>* devices); | 234 void OnDevicesPolled(const ScopedVector<DeviceState>* devices); |
133 | 235 |
134 // Called on BluetoothTaskRunner. | 236 // Called on BluetoothTaskRunner. |
135 void StartPolling(); | 237 void StartPolling(); |
136 void PollAdapter(); | 238 void PollAdapter(); |
137 void PostAdapterStateToUi(); | 239 void PostAdapterStateToUi(); |
138 void SetPowered(bool powered, | 240 void SetPowered(bool powered, |
139 const base::Closure& callback, | 241 const base::Closure& callback, |
140 const BluetoothAdapter::ErrorCallback& error_callback); | 242 const BluetoothAdapter::ErrorCallback& error_callback); |
141 | 243 |
| 244 // Gatt services related functions. |
| 245 void GetGattIncludedServices(base::FilePath service_path, |
| 246 BluetoothUUID uuid, |
| 247 uint16_t attribute_handle, |
| 248 const GetGattIncludedServicesCallback& callback); |
| 249 void GetGattIncludedCharacteristics( |
| 250 base::FilePath device_path, |
| 251 BluetoothUUID uuid, |
| 252 uint16_t attribute_handle, |
| 253 const GetGattIncludedCharacteristicsCallback& callback); |
| 254 void ReadCharacteristicValue(base::FilePath service_path, |
| 255 BTH_LE_GATT_CHARACTERISTIC characteristic, |
| 256 const ReadCharacteristicValueCallback& callback); |
| 257 void WriteCharacteristicValue(base::FilePath device_path, |
| 258 BTH_LE_GATT_CHARACTERISTIC characteristic, |
| 259 std::vector<uint8_t> new_value, |
| 260 const HResultCallback& callback); |
| 261 void ReliableWriteCharacteristicValue( |
| 262 base::FilePath service_path, |
| 263 BTH_LE_GATT_CHARACTERISTIC characteristic, |
| 264 std::vector<uint8_t> new_value, |
| 265 const HResultCallback& callback); |
| 266 void GetGattIncludedDescriptors( |
| 267 base::FilePath service_path, |
| 268 BTH_LE_GATT_CHARACTERISTIC characteristic, |
| 269 const GetGattIncludedDescriptorsCallback& callback); |
| 270 void ReadDescriptorValue(base::FilePath service_path, |
| 271 BTH_LE_GATT_DESCRIPTOR descriptor, |
| 272 const ReadDescriptorValueCallback& callback); |
| 273 void WriteDescriptorValue(base::FilePath service_path, |
| 274 BTH_LE_GATT_DESCRIPTOR descriptor, |
| 275 std::vector<uint8_t> new_value, |
| 276 const HResultCallback& callback); |
| 277 void RegisterCharacteristicValueChangedEvent( |
| 278 base::FilePath service_path, |
| 279 BTH_LE_GATT_CHARACTERISTIC characteristic, |
| 280 const GattEventRegistrationCallback& callback, |
| 281 PFNBLUETOOTH_GATT_EVENT_CALLBACK registered_callback, |
| 282 void* context); |
| 283 |
142 // Starts discovery. Once the discovery starts, it issues a discovery inquiry | 284 // Starts discovery. Once the discovery starts, it issues a discovery inquiry |
143 // with a short timeout, then issues more inquiries with greater timeout | 285 // with a short timeout, then issues more inquiries with greater timeout |
144 // values. The discovery finishes when StopDiscovery() is called or timeout | 286 // values. The discovery finishes when StopDiscovery() is called or timeout |
145 // has reached its maximum value. | 287 // has reached its maximum value. |
146 void StartDiscovery(); | 288 void StartDiscovery(); |
147 void StopDiscovery(); | 289 void StopDiscovery(); |
148 | 290 |
149 // Issues a device inquiry that runs for |timeout_multiplier| * 1.28 seconds. | 291 // Issues a device inquiry that runs for |timeout_multiplier| * 1.28 seconds. |
150 // This posts itself again with |timeout_multiplier| + 1 until | 292 // This posts itself again with |timeout_multiplier| + 1 until |
151 // |timeout_multiplier| reaches the maximum value or stop discovery call is | 293 // |timeout_multiplier| reaches the maximum value or stop discovery call is |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 // Returns a Win32 error code. | 329 // Returns a Win32 error code. |
188 int DiscoverClassicDeviceServicesWorker( | 330 int DiscoverClassicDeviceServicesWorker( |
189 const std::string& device_address, | 331 const std::string& device_address, |
190 const GUID& protocol_uuid, | 332 const GUID& protocol_uuid, |
191 bool search_cached_services_only, | 333 bool search_cached_services_only, |
192 ScopedVector<ServiceRecordState>* service_record_states); | 334 ScopedVector<ServiceRecordState>* service_record_states); |
193 | 335 |
194 // Discover Bluetooth Low Energy services for the given |device_path|. | 336 // Discover Bluetooth Low Energy services for the given |device_path|. |
195 bool DiscoverLowEnergyDeviceServices( | 337 bool DiscoverLowEnergyDeviceServices( |
196 const base::FilePath& device_path, | 338 const base::FilePath& device_path, |
| 339 const std::string device_address, |
197 ScopedVector<ServiceRecordState>* service_record_states); | 340 ScopedVector<ServiceRecordState>* service_record_states); |
198 | 341 |
| 342 bool BluetoothUUIDToBLEUUIDWin(const BluetoothUUID& uuid, |
| 343 BTH_LE_UUID* win_uuid); |
| 344 |
199 // UI task runner reference. | 345 // UI task runner reference. |
200 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | 346 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
201 | 347 |
202 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 348 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
203 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_; | 349 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_; |
204 | 350 |
205 // List of observers interested in event notifications. | 351 // List of observers interested in event notifications. |
206 base::ObserverList<Observer> observers_; | 352 base::ObserverList<Observer> observers_; |
207 | 353 |
208 // Adapter handle owned by bluetooth task runner. | 354 // Adapter handle owned by bluetooth task runner. |
209 base::win::ScopedHandle adapter_handle_; | 355 base::win::ScopedHandle adapter_handle_; |
210 | 356 |
211 // indicates whether the adapter is in discovery mode or not. | 357 // indicates whether the adapter is in discovery mode or not. |
212 bool discovering_; | 358 bool discovering_; |
213 | 359 |
214 // Use for discarding too many log messages. | 360 // Use for discarding too many log messages. |
215 base::TimeTicks current_logging_batch_ticks_; | 361 base::TimeTicks current_logging_batch_ticks_; |
216 int current_logging_batch_count_; | 362 int current_logging_batch_count_; |
217 | 363 |
218 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); | 364 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); |
219 }; | 365 }; |
220 | 366 |
221 } // namespace device | 367 } // namespace device |
222 | 368 |
223 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ | 369 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
OLD | NEW |