1 // Copyright 2016 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_WIN_H_ 6 #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_WIN_H_ 7 8 #include <memory> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/files/file.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/sequenced_task_runner.h" 16 #include "device/bluetooth/bluetooth_low_energy_defs_win.h" 17 #include "device/bluetooth/bluetooth_remote_gatt_service.h" 18 19 namespace device { 20 21 class BluetoothAdapterWin; 22 class BluetoothDeviceWin; 23 class BluetoothRemoteGattCharacteristicWin; 24 class BluetoothTaskManagerWin; 25 26 // The BluetoothRemoteGattServiceWin class implements BluetoothRemoteGattService 27 // for remote GATT services on Windows 8 and later. 28 class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceWin 29 : public BluetoothRemoteGattService { 30 public: 31 BluetoothRemoteGattServiceWin( 32 BluetoothDeviceWin* device, 33 base::FilePath service_path, 34 BluetoothUUID service_uuid, 35 uint16_t service_attribute_handle, 36 bool is_primary, 37 BluetoothRemoteGattServiceWin* parent_service, 38 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); 39 ~BluetoothRemoteGattServiceWin() override; 40 41 // Override BluetoothRemoteGattService interfaces. 42 std::string GetIdentifier() const override; 43 BluetoothUUID GetUUID() const override; 44 bool IsPrimary() const override; 45 BluetoothDevice* GetDevice() const override; 46 std::vector<BluetoothRemoteGattService*> GetIncludedServices() const override; 47 48 // Notify |characteristic| discovery complete, |characteristic| is the 49 // included characteritic of this service. 50 void GattCharacteristicDiscoveryComplete( 51 BluetoothRemoteGattCharacteristicWin* characteristic); 52 53 // Update included services and characteristics. 54 void Update(); GetAttributeHandle()55 uint16_t GetAttributeHandle() const { return service_attribute_handle_; } GetServicePath()56 base::FilePath GetServicePath() { return service_path_; } GetWinAdapter()57 BluetoothAdapterWin* GetWinAdapter() { return adapter_; } 58 59 private: 60 void OnGetIncludedCharacteristics( 61 std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristics, 62 uint16_t num, 63 HRESULT hr); 64 void UpdateIncludedCharacteristics( 65 PBTH_LE_GATT_CHARACTERISTIC characteristics, 66 uint16_t num); 67 68 // Sends GattServiceDiscoveryComplete notification if necessary. 69 void NotifyGattServiceDiscoveryCompleteIfNecessary(); 70 71 // Checks if the characteristic with |uuid| and |attribute_handle| has already 72 // been discovered as included characteristic. 73 bool IsCharacteristicDiscovered(const BTH_LE_UUID& uuid, 74 uint16_t attribute_handle); 75 76 // Checks if |characteristic| still exists in this service according to newly 77 // retreived |num| of included |characteristics|. 78 static bool DoesCharacteristicExist( 79 PBTH_LE_GATT_CHARACTERISTIC characteristics, 80 uint16_t num, 81 BluetoothRemoteGattCharacteristicWin* characteristic); 82 83 void RemoveIncludedCharacteristic(std::string identifier); 84 void ClearIncludedCharacteristics(); 85 86 BluetoothAdapterWin* adapter_; 87 BluetoothDeviceWin* device_; 88 base::FilePath service_path_; 89 BluetoothUUID service_uuid_; 90 uint16_t service_attribute_handle_; 91 bool is_primary_; 92 BluetoothRemoteGattServiceWin* parent_service_; 93 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; 94 std::string service_identifier_; 95 96 // BluetoothTaskManagerWin to handle asynchronously Bluetooth IO and platform 97 // dependent operations. 98 scoped_refptr<BluetoothTaskManagerWin> task_manager_; 99 100 // The element of the set is the identifier of 101 // BluetoothRemoteGattCharacteristicWin instance. 102 std::set<std::string> discovery_completed_included_characteristics_; 103 104 // Flag indicates if discovery complete notification has been send out to 105 // avoid duplicate notification. 106 bool discovery_complete_notified_ = false; 107 108 // Counts the number of asynchronous operations that are discovering 109 // characteristics. 110 int discovery_pending_count_ = 0; 111 112 base::WeakPtrFactory<BluetoothRemoteGattServiceWin> weak_ptr_factory_{this}; 113 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattServiceWin); 114 }; 115 116 } // namespace device. 117 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_WIN_H_ 118