bluetooth_remote_gatt_characteristic_win.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_WIN_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_WIN_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/task/sequenced_task_runner.h"
  13. #include "device/bluetooth/bluetooth_low_energy_defs_win.h"
  14. #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
  15. #include "device/bluetooth/bluetooth_remote_gatt_service.h"
  16. namespace device {
  17. class BluetoothRemoteGattDescriptorWin;
  18. class BluetoothRemoteGattServiceWin;
  19. class BluetoothTaskManagerWin;
  20. // The BluetoothRemoteGattCharacteristicWin class implements
  21. // BluetoothRemoteGattCharacteristic for remote GATT services on Windows 8 and
  22. // later.
  23. class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin
  24. : public BluetoothRemoteGattCharacteristic {
  25. public:
  26. BluetoothRemoteGattCharacteristicWin(
  27. BluetoothRemoteGattServiceWin* parent_service,
  28. BTH_LE_GATT_CHARACTERISTIC* characteristic_info,
  29. scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
  30. BluetoothRemoteGattCharacteristicWin(
  31. const BluetoothRemoteGattCharacteristicWin&) = delete;
  32. BluetoothRemoteGattCharacteristicWin& operator=(
  33. const BluetoothRemoteGattCharacteristicWin&) = delete;
  34. ~BluetoothRemoteGattCharacteristicWin() override;
  35. // Override BluetoothRemoteGattCharacteristic interfaces.
  36. std::string GetIdentifier() const override;
  37. BluetoothUUID GetUUID() const override;
  38. std::vector<uint8_t>& GetValue() const override;
  39. BluetoothRemoteGattService* GetService() const override;
  40. Properties GetProperties() const override;
  41. Permissions GetPermissions() const override;
  42. bool IsNotifying() const override;
  43. void ReadRemoteCharacteristic(ValueCallback callback) override;
  44. void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
  45. WriteType write_type,
  46. base::OnceClosure callback,
  47. ErrorCallback error_callback) override;
  48. void DeprecatedWriteRemoteCharacteristic(
  49. const std::vector<uint8_t>& value,
  50. base::OnceClosure callback,
  51. ErrorCallback error_callback) override;
  52. // Update included descriptors.
  53. void Update();
  54. uint16_t GetAttributeHandle() const;
  55. BluetoothRemoteGattServiceWin* GetWinService() { return parent_service_; }
  56. protected:
  57. void SubscribeToNotifications(BluetoothRemoteGattDescriptor* ccc_descriptor,
  58. base::OnceClosure callback,
  59. ErrorCallback error_callback) override;
  60. void UnsubscribeFromNotifications(
  61. BluetoothRemoteGattDescriptor* ccc_descriptor,
  62. base::OnceClosure callback,
  63. ErrorCallback error_callback) override;
  64. private:
  65. void OnGetIncludedDescriptorsCallback(
  66. std::unique_ptr<BTH_LE_GATT_DESCRIPTOR> descriptors,
  67. uint16_t num,
  68. HRESULT hr);
  69. void UpdateIncludedDescriptors(PBTH_LE_GATT_DESCRIPTOR descriptors,
  70. uint16_t num);
  71. // Checks if the descriptor with |uuid| and |attribute_handle| has already
  72. // been discovered as included descriptor.
  73. bool IsDescriptorDiscovered(const BTH_LE_UUID& uuid,
  74. uint16_t attribute_handle);
  75. // Checks if |descriptor| still exists in this characteristic according to
  76. // newly discovered |num| of |descriptors|.
  77. static bool DoesDescriptorExist(PBTH_LE_GATT_DESCRIPTOR descriptors,
  78. uint16_t num,
  79. BluetoothRemoteGattDescriptorWin* descriptor);
  80. void OnReadRemoteCharacteristicValueCallback(
  81. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value,
  82. HRESULT hr);
  83. void OnWriteRemoteCharacteristicValueCallback(HRESULT hr);
  84. BluetoothGattService::GattErrorCode HRESULTToGattErrorCode(HRESULT hr);
  85. void OnGattCharacteristicValueChanged(
  86. std::unique_ptr<std::vector<uint8_t>> new_value);
  87. void GattEventRegistrationCallback(base::OnceClosure callback,
  88. ErrorCallback error_callback,
  89. PVOID event_handle,
  90. HRESULT hr);
  91. void ClearIncludedDescriptors();
  92. raw_ptr<BluetoothRemoteGattServiceWin> parent_service_;
  93. scoped_refptr<BluetoothTaskManagerWin> task_manager_;
  94. // Characteristic info from OS and used to interact with OS.
  95. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristic_info_;
  96. scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
  97. BluetoothUUID characteristic_uuid_;
  98. std::vector<uint8_t> characteristic_value_;
  99. std::string characteristic_identifier_;
  100. // Flag indicates if characteristic added notification of this characteristic
  101. // has been sent out to avoid duplicate notification.
  102. bool characteristic_added_notified_;
  103. // ReadRemoteCharacteristic request callback.
  104. ValueCallback read_characteristic_value_callback_;
  105. // WriteRemoteCharacteristic request callbacks.
  106. std::pair<base::OnceClosure, ErrorCallback>
  107. write_characteristic_value_callbacks_;
  108. bool characteristic_value_read_or_write_in_progress_;
  109. // GATT event handle returned by GattEventRegistrationCallback.
  110. PVOID gatt_event_handle_;
  111. // Counts the number of asynchronous operations that are discovering
  112. // descriptors.
  113. int discovery_pending_count_;
  114. base::WeakPtrFactory<BluetoothRemoteGattCharacteristicWin> weak_ptr_factory_{
  115. this};
  116. };
  117. } // namespace device
  118. #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_WIN_H_