Chromium Code Reviews| 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 <memory> | 10 #include <memory> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 : public base::RefCountedThreadSafe<BluetoothTaskManagerWin> { | 43 : public base::RefCountedThreadSafe<BluetoothTaskManagerWin> { |
| 44 public: | 44 public: |
| 45 struct DEVICE_BLUETOOTH_EXPORT AdapterState { | 45 struct DEVICE_BLUETOOTH_EXPORT AdapterState { |
| 46 AdapterState(); | 46 AdapterState(); |
| 47 ~AdapterState(); | 47 ~AdapterState(); |
| 48 std::string name; | 48 std::string name; |
| 49 std::string address; | 49 std::string address; |
| 50 bool powered; | 50 bool powered; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 struct DEVICE_BLUETOOTH_EXPORT ServiceRecordState { | 53 class DEVICE_BLUETOOTH_EXPORT ServiceRecordState { |
| 54 public: | |
| 54 ServiceRecordState(); | 55 ServiceRecordState(); |
| 55 ~ServiceRecordState(); | 56 virtual ~ServiceRecordState(); |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
Perhaps declare this class final and skip the virt
dougt
2016/12/22 01:18:03
Acknowledged.
| |
| 56 // Properties common to Bluetooth Classic and LE devices. | 57 // Properties common to Bluetooth Classic and LE devices. |
| 57 std::string name; | 58 std::string name; |
| 58 // Properties specific to Bluetooth Classic devices. | 59 // Properties specific to Bluetooth Classic devices. |
| 59 std::vector<uint8_t> sdp_bytes; | 60 std::vector<uint8_t> sdp_bytes; |
| 60 // Properties specific to Bluetooth LE devices. | 61 // Properties specific to Bluetooth LE devices. |
| 61 BluetoothUUID gatt_uuid; | 62 BluetoothUUID gatt_uuid; |
| 62 uint16_t attribute_handle; | 63 uint16_t attribute_handle; |
| 63 // GATT service device path. | 64 // GATT service device path. |
| 64 // Note: Operation of the included characteristics and descriptors of this | 65 // Note: Operation of the included characteristics and descriptors of this |
| 65 // service must use service device path instead of resident device device | 66 // service must use service device path instead of resident device device |
| 66 // path. | 67 // path. |
| 67 base::FilePath path; | 68 base::FilePath path; |
| 68 }; | 69 }; |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
Maybe DISALLOW_COPY_AND_ASSIGN(ServiceRecordState)
dougt
2016/12/22 01:18:03
Done.
| |
| 69 | 70 |
| 70 struct DEVICE_BLUETOOTH_EXPORT DeviceState { | 71 using ServiceRecordStateList = |
| 72 std::vector<std::unique_ptr<ServiceRecordState>>; | |
| 73 | |
| 74 class DEVICE_BLUETOOTH_EXPORT DeviceState { | |
| 75 public: | |
| 71 DeviceState(); | 76 DeviceState(); |
| 72 ~DeviceState(); | 77 ~DeviceState(); |
| 73 | 78 |
| 74 bool is_bluetooth_classic() const { return path.empty(); } | 79 bool is_bluetooth_classic() const { return path.empty(); } |
| 75 | 80 |
| 76 // Properties common to Bluetooth Classic and LE devices. | 81 // Properties common to Bluetooth Classic and LE devices. |
| 77 std::string address; // This uniquely identifies the device. | 82 std::string address; // This uniquely identifies the device. |
| 78 base::Optional<std::string> name; // Friendly name | 83 base::Optional<std::string> name; // Friendly name |
| 79 bool visible; | 84 bool visible; |
| 80 bool connected; | 85 bool connected; |
| 81 bool authenticated; | 86 bool authenticated; |
| 82 ScopedVector<ServiceRecordState> service_record_states; | 87 ServiceRecordStateList service_record_states; |
| 83 // Properties specific to Bluetooth Classic devices. | 88 // Properties specific to Bluetooth Classic devices. |
| 84 uint32_t bluetooth_class; | 89 uint32_t bluetooth_class; |
| 85 // Properties specific to Bluetooth LE devices. | 90 // Properties specific to Bluetooth LE devices. |
| 86 base::FilePath path; | 91 base::FilePath path; |
| 92 | |
| 93 private: | |
| 94 DISALLOW_COPY_AND_ASSIGN(DeviceState); | |
| 87 }; | 95 }; |
| 88 | 96 |
| 89 class DEVICE_BLUETOOTH_EXPORT Observer { | 97 class DEVICE_BLUETOOTH_EXPORT Observer { |
| 90 public: | 98 public: |
| 91 virtual ~Observer() {} | 99 virtual ~Observer() {} |
| 92 | 100 |
| 93 virtual void AdapterStateChanged(const AdapterState& state) {} | 101 virtual void AdapterStateChanged(const AdapterState& state) {} |
| 94 virtual void DiscoveryStarted(bool success) {} | 102 virtual void DiscoveryStarted(bool success) {} |
| 95 virtual void DiscoveryStopped() {} | 103 virtual void DiscoveryStopped() {} |
| 96 // Called when the adapter has just been polled for the list of *all* known | 104 // Called when the adapter has just been polled for the list of *all* known |
| 97 // devices. This includes devices previously paired, devices paired using | 105 // devices. This includes devices previously paired, devices paired using |
| 98 // the underlying Operating System UI, and devices discovered recently due | 106 // the underlying Operating System UI, and devices discovered recently due |
| 99 // to an active discovery session. Note that for a given device (address), | 107 // to an active discovery session. Note that for a given device (address), |
| 100 // the associated state can change over time. For example, during a | 108 // the associated state can change over time. For example, during a |
| 101 // discovery session, the "friendly" name may initially be "unknown" before | 109 // discovery session, the "friendly" name may initially be "unknown" before |
| 102 // the actual name is retrieved in subsequent poll events. | 110 // the actual name is retrieved in subsequent poll events. |
| 103 virtual void DevicesPolled(const ScopedVector<DeviceState>& devices) {} | 111 virtual void DevicesPolled( |
| 112 const std::vector<std::unique_ptr<DeviceState>>& devices) {} | |
| 104 }; | 113 }; |
| 105 | 114 |
| 106 explicit BluetoothTaskManagerWin( | 115 explicit BluetoothTaskManagerWin( |
| 107 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); | 116 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); |
| 108 | 117 |
| 109 static BluetoothUUID BluetoothLowEnergyUuidToBluetoothUuid( | 118 static BluetoothUUID BluetoothLowEnergyUuidToBluetoothUuid( |
| 110 const BTH_LE_UUID& bth_le_uuid); | 119 const BTH_LE_UUID& bth_le_uuid); |
| 111 | 120 |
| 112 void AddObserver(Observer* observer); | 121 void AddObserver(Observer* observer); |
| 113 void RemoveObserver(Observer* observer); | 122 void RemoveObserver(Observer* observer); |
| 114 | 123 |
| 115 void Initialize(); | 124 void Initialize(); |
| 116 void InitializeWithBluetoothTaskRunner( | 125 void InitializeWithBluetoothTaskRunner( |
| 117 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); | 126 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); |
| 118 void Shutdown(); | 127 void Shutdown(); |
| 119 | 128 |
| 120 void PostSetPoweredBluetoothTask( | 129 void PostSetPoweredBluetoothTask( |
| 121 bool powered, | 130 bool powered, |
| 122 const base::Closure& callback, | 131 const base::Closure& callback, |
| 123 const BluetoothAdapter::ErrorCallback& error_callback); | 132 const BluetoothAdapter::ErrorCallback& error_callback); |
| 124 void PostStartDiscoveryTask(); | 133 void PostStartDiscoveryTask(); |
| 125 void PostStopDiscoveryTask(); | 134 void PostStopDiscoveryTask(); |
| 126 | 135 |
| 127 // Callbacks of asynchronous operations of GATT service. | 136 // Callbacks of asynchronous operations of GATT service. |
| 128 typedef base::Callback<void(HRESULT)> HResultCallback; | 137 using HResultCallback = base::Callback<void(HRESULT)>; |
| 129 typedef base::Callback< | 138 using GetGattIncludedCharacteristicsCallback = base::Callback< |
| 130 void(std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC>, uint16_t, HRESULT)> | 139 void(std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC>, uint16_t, HRESULT)>; |
| 131 GetGattIncludedCharacteristicsCallback; | 140 using GetGattIncludedDescriptorsCallback = base::Callback< |
| 132 typedef base::Callback< | 141 void(std::unique_ptr<BTH_LE_GATT_DESCRIPTOR>, uint16_t, HRESULT)>; |
| 133 void(std::unique_ptr<BTH_LE_GATT_DESCRIPTOR>, uint16_t, HRESULT)> | 142 using ReadGattCharacteristicValueCallback = |
| 134 GetGattIncludedDescriptorsCallback; | 143 base::Callback<void(std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>, |
| 135 typedef base::Callback<void(std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>, | 144 HRESULT)>; |
| 136 HRESULT)> | 145 using GattCharacteristicValueChangedCallback = |
| 137 ReadGattCharacteristicValueCallback; | 146 base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>; |
| 138 typedef base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)> | 147 using GattEventRegistrationCallback = base::Callback<void(PVOID, HRESULT)>; |
| 139 GattCharacteristicValueChangedCallback; | |
| 140 typedef base::Callback<void(PVOID, HRESULT)> GattEventRegistrationCallback; | |
| 141 | 148 |
| 142 // Get all included characteristics of a given service. The service is | 149 // Get all included characteristics of a given service. The service is |
| 143 // uniquely identified by its |uuid| and |attribute_handle| with service | 150 // uniquely identified by its |uuid| and |attribute_handle| with service |
| 144 // device |service_path|. The result is returned asynchronously through | 151 // device |service_path|. The result is returned asynchronously through |
| 145 // |callback|. | 152 // |callback|. |
| 146 void PostGetGattIncludedCharacteristics( | 153 void PostGetGattIncludedCharacteristics( |
| 147 const base::FilePath& service_path, | 154 const base::FilePath& service_path, |
| 148 const BluetoothUUID& uuid, | 155 const BluetoothUUID& uuid, |
| 149 uint16_t attribute_handle, | 156 uint16_t attribute_handle, |
| 150 const GetGattIncludedCharacteristicsCallback& callback); | 157 const GetGattIncludedCharacteristicsCallback& callback); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 | 206 |
| 200 // Logs Win32 errors occurring during polling on the worker thread. The method | 207 // Logs Win32 errors occurring during polling on the worker thread. The method |
| 201 // may discard messages to avoid logging being too verbose. | 208 // may discard messages to avoid logging being too verbose. |
| 202 void LogPollingError(const char* message, int win32_error); | 209 void LogPollingError(const char* message, int win32_error); |
| 203 | 210 |
| 204 // Notify all Observers of updated AdapterState. Should only be called on the | 211 // Notify all Observers of updated AdapterState. Should only be called on the |
| 205 // UI thread. | 212 // UI thread. |
| 206 void OnAdapterStateChanged(const AdapterState* state); | 213 void OnAdapterStateChanged(const AdapterState* state); |
| 207 void OnDiscoveryStarted(bool success); | 214 void OnDiscoveryStarted(bool success); |
| 208 void OnDiscoveryStopped(); | 215 void OnDiscoveryStopped(); |
| 209 void OnDevicesPolled(const ScopedVector<DeviceState>* devices); | 216 void OnDevicesPolled( |
| 217 const std::vector<std::unique_ptr<DeviceState>>* devices); | |
|
Reilly Grant (use Gerrit)
2016/12/21 22:25:15
Pass const& since this function DCHECKS that devic
dougt
2016/12/22 01:18:03
Done.
| |
| 210 | 218 |
| 211 // Called on BluetoothTaskRunner. | 219 // Called on BluetoothTaskRunner. |
| 212 void StartPolling(); | 220 void StartPolling(); |
| 213 void PollAdapter(); | 221 void PollAdapter(); |
| 214 void PostAdapterStateToUi(); | 222 void PostAdapterStateToUi(); |
| 215 void SetPowered(bool powered, | 223 void SetPowered(bool powered, |
| 216 const base::Closure& callback, | 224 const base::Closure& callback, |
| 217 const BluetoothAdapter::ErrorCallback& error_callback); | 225 const BluetoothAdapter::ErrorCallback& error_callback); |
| 218 | 226 |
| 219 // Starts discovery. Once the discovery starts, it issues a discovery inquiry | 227 // Starts discovery. Once the discovery starts, it issues a discovery inquiry |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 231 | 239 |
| 232 // Fetch already known device information. Similar to |StartDiscovery|, except | 240 // Fetch already known device information. Similar to |StartDiscovery|, except |
| 233 // this function does not issue a discovery inquiry. Instead it gets the | 241 // this function does not issue a discovery inquiry. Instead it gets the |
| 234 // device info cached in the adapter. | 242 // device info cached in the adapter. |
| 235 void GetKnownDevices(); | 243 void GetKnownDevices(); |
| 236 | 244 |
| 237 // Looks for Bluetooth Classic and Low Energy devices, as well as the services | 245 // Looks for Bluetooth Classic and Low Energy devices, as well as the services |
| 238 // exposed by those devices. | 246 // exposed by those devices. |
| 239 bool SearchDevices(int timeout_multiplier, | 247 bool SearchDevices(int timeout_multiplier, |
| 240 bool search_cached_devices_only, | 248 bool search_cached_devices_only, |
| 241 ScopedVector<DeviceState>* device_list); | 249 std::vector<std::unique_ptr<DeviceState>>* device_list); |
| 242 | 250 |
| 243 // Sends a device search API call to the adapter to look for Bluetooth Classic | 251 // Sends a device search API call to the adapter to look for Bluetooth Classic |
| 244 // devices. | 252 // devices. |
| 245 bool SearchClassicDevices(int timeout_multiplier, | 253 bool SearchClassicDevices( |
| 246 bool search_cached_devices_only, | 254 int timeout_multiplier, |
| 247 ScopedVector<DeviceState>* device_list); | 255 bool search_cached_devices_only, |
| 256 std::vector<std::unique_ptr<DeviceState>>* device_list); | |
| 248 | 257 |
| 249 // Enumerate Bluetooth Low Energy devices. | 258 // Enumerate Bluetooth Low Energy devices. |
| 250 bool SearchLowEnergyDevices(ScopedVector<DeviceState>* device_list); | 259 bool SearchLowEnergyDevices( |
| 260 std::vector<std::unique_ptr<DeviceState>>* device_list); | |
| 251 | 261 |
| 252 // Discover services for the devices in |device_list|. | 262 // Discover services for the devices in |device_list|. |
| 253 bool DiscoverServices(ScopedVector<DeviceState>* device_list, | 263 bool DiscoverServices(std::vector<std::unique_ptr<DeviceState>>* device_list, |
| 254 bool search_cached_services_only); | 264 bool search_cached_services_only); |
| 255 | 265 |
| 256 // Discover Bluetooth Classic services for the given |device_address|. | 266 // Discover Bluetooth Classic services for the given |device_address|. |
| 257 bool DiscoverClassicDeviceServices( | 267 bool DiscoverClassicDeviceServices( |
| 258 const std::string& device_address, | 268 const std::string& device_address, |
| 259 const GUID& protocol_uuid, | 269 const GUID& protocol_uuid, |
| 260 bool search_cached_services_only, | 270 bool search_cached_services_only, |
| 261 ScopedVector<ServiceRecordState>* service_record_states); | 271 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states); |
| 262 | 272 |
| 263 // Discover Bluetooth Classic services for the given |device_address|. | 273 // Discover Bluetooth Classic services for the given |device_address|. |
| 264 // Returns a Win32 error code. | 274 // Returns a Win32 error code. |
| 265 int DiscoverClassicDeviceServicesWorker( | 275 int DiscoverClassicDeviceServicesWorker( |
| 266 const std::string& device_address, | 276 const std::string& device_address, |
| 267 const GUID& protocol_uuid, | 277 const GUID& protocol_uuid, |
| 268 bool search_cached_services_only, | 278 bool search_cached_services_only, |
| 269 ScopedVector<ServiceRecordState>* service_record_states); | 279 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states); |
| 270 | 280 |
| 271 // Discover Bluetooth Low Energy services for the given |device_path|. | 281 // Discover Bluetooth Low Energy services for the given |device_path|. |
| 272 bool DiscoverLowEnergyDeviceServices( | 282 bool DiscoverLowEnergyDeviceServices( |
| 273 const base::FilePath& device_path, | 283 const base::FilePath& device_path, |
| 274 ScopedVector<ServiceRecordState>* service_record_states); | 284 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states); |
| 275 | 285 |
| 276 // Search for device paths of the GATT services in |*service_record_states| | 286 // Search for device paths of the GATT services in |*service_record_states| |
| 277 // from |device_address|. | 287 // from |device_address|. |
| 278 bool SearchForGattServiceDevicePaths( | 288 bool SearchForGattServiceDevicePaths( |
| 279 const std::string device_address, | 289 const std::string device_address, |
| 280 ScopedVector<ServiceRecordState>* service_record_states); | 290 std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states); |
| 281 | 291 |
| 282 // GATT service related functions. | 292 // GATT service related functions. |
| 283 void GetGattIncludedCharacteristics( | 293 void GetGattIncludedCharacteristics( |
| 284 base::FilePath device_path, | 294 base::FilePath device_path, |
| 285 BluetoothUUID uuid, | 295 BluetoothUUID uuid, |
| 286 uint16_t attribute_handle, | 296 uint16_t attribute_handle, |
| 287 const GetGattIncludedCharacteristicsCallback& callback); | 297 const GetGattIncludedCharacteristicsCallback& callback); |
| 288 void GetGattIncludedDescriptors( | 298 void GetGattIncludedDescriptors( |
| 289 base::FilePath service_path, | 299 base::FilePath service_path, |
| 290 BTH_LE_GATT_CHARACTERISTIC characteristic, | 300 BTH_LE_GATT_CHARACTERISTIC characteristic, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 // Use for discarding too many log messages. | 334 // Use for discarding too many log messages. |
| 325 base::TimeTicks current_logging_batch_ticks_; | 335 base::TimeTicks current_logging_batch_ticks_; |
| 326 int current_logging_batch_count_; | 336 int current_logging_batch_count_; |
| 327 | 337 |
| 328 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); | 338 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); |
| 329 }; | 339 }; |
| 330 | 340 |
| 331 } // namespace device | 341 } // namespace device |
| 332 | 342 |
| 333 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ | 343 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
| OLD | NEW |