bluetooth_gatt_discoverer_winrt.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2018 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. #ifndef DEVICE_BLUETOOTH_BLUETOOTH_GATT_DISCOVERER_WINRT_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_DISCOVERER_WINRT_H_
  6. #include <windows.devices.bluetooth.genericattributeprofile.h>
  7. #include <windows.devices.bluetooth.h>
  8. #include <wrl/client.h>
  9. #include <stdint.h>
  10. #include <memory>
  11. #include <vector>
  12. #include "base/callback.h"
  13. #include "base/containers/flat_map.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/threading/thread_checker.h"
  16. #include "device/bluetooth/bluetooth_device.h"
  17. #include "device/bluetooth/bluetooth_export.h"
  18. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace device {
  21. // This class is responsible for discovering and enumerating GATT attributes on
  22. // the provided BluetoothLEDevice. Callers are expected to instantiate the class
  23. // and invoke StartGattDiscovery(). Once the discovery completes, the passed-in
  24. // GattDiscoveryCallback is invoked, and discovered GATT attributes can be
  25. // obtained by invoking the appropriate getters.
  26. class DEVICE_BLUETOOTH_EXPORT BluetoothGattDiscovererWinrt {
  27. public:
  28. using GattDiscoveryCallback = base::OnceCallback<void(bool)>;
  29. using GattServiceList = std::vector<
  30. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  31. GenericAttributeProfile::IGattDeviceService>>;
  32. using GattCharacteristicList = std::vector<
  33. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  34. GenericAttributeProfile::IGattCharacteristic>>;
  35. using GattDescriptorList = std::vector<
  36. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  37. GenericAttributeProfile::IGattDescriptor>>;
  38. BluetoothGattDiscovererWinrt(
  39. Microsoft::WRL::ComPtr<
  40. ABI::Windows::Devices::Bluetooth::IBluetoothLEDevice> ble_device,
  41. absl::optional<BluetoothUUID> service_uuid);
  42. BluetoothGattDiscovererWinrt(const BluetoothGattDiscovererWinrt&) = delete;
  43. BluetoothGattDiscovererWinrt& operator=(const BluetoothGattDiscovererWinrt&) =
  44. delete;
  45. ~BluetoothGattDiscovererWinrt();
  46. // Note: In order to avoid running |callback| multiple times on errors,
  47. // clients are expected to synchronously destroy the GattDiscoverer after
  48. // |callback| has been invoked for the first time.
  49. void StartGattDiscovery(GattDiscoveryCallback callback);
  50. const GattServiceList& GetGattServices() const;
  51. const GattCharacteristicList* GetCharacteristics(
  52. uint16_t service_attribute_handle) const;
  53. const GattDescriptorList* GetDescriptors(
  54. uint16_t characteristic_attribute_handle) const;
  55. private:
  56. void OnGetGattServices(
  57. Microsoft::WRL::ComPtr<
  58. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  59. IGattDeviceServicesResult> services_result);
  60. void OnServiceOpen(
  61. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  62. GenericAttributeProfile::IGattDeviceService3>
  63. gatt_service_3,
  64. uint16_t service_attribute_handle,
  65. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::GattOpenStatus
  66. status);
  67. void OnGetCharacteristics(
  68. uint16_t service_attribute_handle,
  69. Microsoft::WRL::ComPtr<
  70. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  71. IGattCharacteristicsResult> characteristics_result);
  72. void OnGetDescriptors(
  73. uint16_t characteristic_attribute_handle,
  74. Microsoft::WRL::ComPtr<
  75. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  76. IGattDescriptorsResult> descriptors_result);
  77. void RunCallbackIfDone();
  78. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothLEDevice>
  79. ble_device_;
  80. GattDiscoveryCallback callback_;
  81. GattServiceList gatt_services_;
  82. base::flat_map<uint16_t, GattCharacteristicList>
  83. service_to_characteristics_map_;
  84. base::flat_map<uint16_t, GattDescriptorList>
  85. characteristic_to_descriptors_map_;
  86. absl::optional<BluetoothUUID> service_uuid_;
  87. size_t num_services_ = 0;
  88. size_t num_characteristics_ = 0;
  89. THREAD_CHECKER(thread_checker_);
  90. // Note: This should remain the last member so it'll be destroyed and
  91. // invalidate its weak pointers before any other members are destroyed.
  92. base::WeakPtrFactory<BluetoothGattDiscovererWinrt> weak_ptr_factory_{this};
  93. };
  94. } // namespace device
  95. #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_DISCOVERER_WINRT_H_