bluetooth_remote_gatt_characteristic_winrt.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_REMOTE_GATT_CHARACTERISTIC_WINRT_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_WINRT_H_
  6. #include <windows.devices.bluetooth.genericattributeprofile.h>
  7. #include <wrl/client.h>
  8. #include <stdint.h>
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. #include "base/callback_forward.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "device/bluetooth/bluetooth_export.h"
  15. #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
  16. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  17. namespace device {
  18. class BluetoothRemoteGattDescriptor;
  19. class BluetoothGattDiscovererWinrt;
  20. class BluetoothRemoteGattService;
  21. class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWinrt
  22. : public BluetoothRemoteGattCharacteristic {
  23. public:
  24. static std::unique_ptr<BluetoothRemoteGattCharacteristicWinrt> Create(
  25. BluetoothRemoteGattService* service,
  26. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  27. GenericAttributeProfile::IGattCharacteristic>
  28. characteristic);
  29. BluetoothRemoteGattCharacteristicWinrt(
  30. const BluetoothRemoteGattCharacteristicWinrt&) = delete;
  31. BluetoothRemoteGattCharacteristicWinrt& operator=(
  32. const BluetoothRemoteGattCharacteristicWinrt&) = delete;
  33. ~BluetoothRemoteGattCharacteristicWinrt() override;
  34. // BluetoothGattCharacteristic:
  35. std::string GetIdentifier() const override;
  36. BluetoothUUID GetUUID() const override;
  37. Properties GetProperties() const override;
  38. Permissions GetPermissions() const override;
  39. // BluetoothRemoteGattCharacteristic:
  40. const std::vector<uint8_t>& GetValue() const override;
  41. BluetoothRemoteGattService* GetService() const override;
  42. void ReadRemoteCharacteristic(ValueCallback callback) override;
  43. void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
  44. WriteType write_type,
  45. base::OnceClosure callback,
  46. ErrorCallback error_callback) override;
  47. void DeprecatedWriteRemoteCharacteristic(
  48. const std::vector<uint8_t>& value,
  49. base::OnceClosure callback,
  50. ErrorCallback error_callback) override;
  51. void UpdateDescriptors(BluetoothGattDiscovererWinrt* gatt_discoverer);
  52. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  53. IGattCharacteristic*
  54. GetCharacteristicForTesting();
  55. protected:
  56. // BluetoothRemoteGattCharacteristic:
  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. struct PendingWriteCallbacks {
  66. PendingWriteCallbacks(base::OnceClosure callback,
  67. ErrorCallback error_callback);
  68. ~PendingWriteCallbacks();
  69. base::OnceClosure callback;
  70. ErrorCallback error_callback;
  71. };
  72. using PendingNotificationCallbacks = PendingWriteCallbacks;
  73. BluetoothRemoteGattCharacteristicWinrt(
  74. BluetoothRemoteGattService* service,
  75. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  76. GenericAttributeProfile::IGattCharacteristic>
  77. characteristic,
  78. BluetoothUUID uuid,
  79. Properties proporties,
  80. uint16_t attribute_handle);
  81. void WriteCccDescriptor(
  82. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  83. GattClientCharacteristicConfigurationDescriptorValue value,
  84. base::OnceClosure callback,
  85. ErrorCallback error_callback);
  86. void OnReadValue(Microsoft::WRL::ComPtr<
  87. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  88. IGattReadResult> read_result);
  89. void OnWriteValueWithResultAndOption(
  90. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  91. GenericAttributeProfile::IGattWriteResult>
  92. write_result);
  93. void OnWriteCccDescriptor(
  94. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  95. GenericAttributeProfile::IGattWriteResult>
  96. write_result);
  97. void OnWriteImpl(
  98. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  99. GenericAttributeProfile::IGattWriteResult>
  100. write_result,
  101. std::unique_ptr<PendingWriteCallbacks> callbacks);
  102. void OnValueChanged(
  103. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  104. IGattCharacteristic* characteristic,
  105. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::
  106. IGattValueChangedEventArgs* event_args);
  107. bool RemoveValueChangedHandler();
  108. raw_ptr<BluetoothRemoteGattService> service_;
  109. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  110. GenericAttributeProfile::IGattCharacteristic>
  111. characteristic_;
  112. BluetoothUUID uuid_;
  113. Properties properties_;
  114. uint16_t attribute_handle_;
  115. std::string identifier_;
  116. std::vector<uint8_t> value_;
  117. ValueCallback pending_read_callback_;
  118. std::unique_ptr<PendingWriteCallbacks> pending_write_callbacks_;
  119. std::unique_ptr<PendingNotificationCallbacks> pending_notification_callbacks_;
  120. absl::optional<EventRegistrationToken> value_changed_token_;
  121. // The destructor runs callbacks. Methods can use |destructor_called_| to
  122. // protect against reentrant calls to a partially deleted instance.
  123. bool destructor_called_ = false;
  124. base::WeakPtrFactory<BluetoothRemoteGattCharacteristicWinrt>
  125. weak_ptr_factory_{this};
  126. };
  127. } // namespace device
  128. #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_WINRT_H_