bluetooth_remote_gatt_characteristic_mac.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_MAC_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_MAC_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
  8. #import <CoreBluetooth/CoreBluetooth.h>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/mac/scoped_nsobject.h"
  13. #include "base/memory/weak_ptr.h"
  14. namespace device {
  15. class BluetoothAdapterMac;
  16. class BluetoothRemoteGattDescriptorMac;
  17. class BluetoothRemoteGattServiceMac;
  18. // The BluetoothRemoteGattCharacteristicMac class implements
  19. // BluetoothRemoteGattCharacteristic for remote GATT services on OS X.
  20. class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac
  21. : public BluetoothRemoteGattCharacteristic {
  22. public:
  23. BluetoothRemoteGattCharacteristicMac(
  24. BluetoothRemoteGattServiceMac* gatt_service,
  25. CBCharacteristic* cb_characteristic);
  26. BluetoothRemoteGattCharacteristicMac(
  27. const BluetoothRemoteGattCharacteristicMac&) = delete;
  28. BluetoothRemoteGattCharacteristicMac& operator=(
  29. const BluetoothRemoteGattCharacteristicMac&) = delete;
  30. ~BluetoothRemoteGattCharacteristicMac() override;
  31. // Override BluetoothGattCharacteristic methods.
  32. std::string GetIdentifier() const override;
  33. BluetoothUUID GetUUID() const override;
  34. Properties GetProperties() const override;
  35. Permissions GetPermissions() const override;
  36. // Override BluetoothRemoteGattCharacteristic methods.
  37. const std::vector<uint8_t>& GetValue() const override;
  38. BluetoothRemoteGattService* GetService() const override;
  39. bool IsNotifying() const override;
  40. void ReadRemoteCharacteristic(ValueCallback callback) override;
  41. void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
  42. WriteType write_type,
  43. base::OnceClosure callback,
  44. ErrorCallback error_callback) override;
  45. void DeprecatedWriteRemoteCharacteristic(
  46. const std::vector<uint8_t>& value,
  47. base::OnceClosure callback,
  48. ErrorCallback error_callback) override;
  49. protected:
  50. void SubscribeToNotifications(BluetoothRemoteGattDescriptor* ccc_descriptor,
  51. base::OnceClosure callback,
  52. ErrorCallback error_callback) override;
  53. void UnsubscribeFromNotifications(
  54. BluetoothRemoteGattDescriptor* ccc_descriptor,
  55. base::OnceClosure callback,
  56. ErrorCallback error_callback) override;
  57. private:
  58. friend class BluetoothLowEnergyDeviceMac;
  59. friend class BluetoothRemoteGattDescriptorMac;
  60. friend class BluetoothRemoteGattServiceMac;
  61. friend class BluetoothTestMac;
  62. void DiscoverDescriptors();
  63. // Called by the BluetoothRemoteGattServiceMac instance when the
  64. // characteristics value has been read.
  65. void DidUpdateValue(NSError* error);
  66. // Updates value_.
  67. void UpdateValue();
  68. // Called by the BluetoothRemoteGattServiceMac instance when the
  69. // characteristics value has been written.
  70. void DidWriteValue(NSError* error);
  71. // Called by the BluetoothRemoteGattServiceMac instance when the notify
  72. // session has been started or failed to be started.
  73. void DidUpdateNotificationState(NSError* error);
  74. // Called by the BluetoothRemoteGattServiceMac instance when the descriptors
  75. // has been discovered.
  76. void DidDiscoverDescriptors();
  77. // Returns true if the characteristic is readable.
  78. bool IsReadable() const;
  79. // Returns true if the characteristic is writable.
  80. bool IsWritable() const;
  81. // Returns true if the characteristic supports notifications or indications.
  82. bool SupportsNotificationsOrIndications() const;
  83. // Returns the write type (with or without responses).
  84. CBCharacteristicWriteType GetCBWriteType() const;
  85. // Returns CoreBluetooth characteristic.
  86. CBCharacteristic* GetCBCharacteristic() const;
  87. // Returns the mac adapter.
  88. BluetoothAdapterMac* GetMacAdapter() const;
  89. // Returns CoreBluetooth peripheral.
  90. CBPeripheral* GetCBPeripheral() const;
  91. // Returns true if this characteristic has been fully discovered.
  92. bool IsDiscoveryComplete() const;
  93. // Returns BluetoothRemoteGattDescriptorMac from CBDescriptor.
  94. BluetoothRemoteGattDescriptorMac* GetBluetoothRemoteGattDescriptorMac(
  95. CBDescriptor* cb_descriptor) const;
  96. bool HasPendingRead() const {
  97. return !read_characteristic_value_callback_.is_null();
  98. }
  99. bool HasPendingWrite() const {
  100. return !write_characteristic_value_callbacks_.first.is_null();
  101. }
  102. // Is true if the characteristic has been discovered with all its descriptors
  103. // and discovery_pending_count_ is 0.
  104. bool is_discovery_complete_;
  105. // Increased each time DiscoverDescriptors() is called. And decreased when
  106. // DidDiscoverDescriptors() is called.
  107. int discovery_pending_count_;
  108. // gatt_service_ owns instances of this class.
  109. raw_ptr<BluetoothRemoteGattServiceMac> gatt_service_;
  110. // A characteristic from CBPeripheral.services.characteristics.
  111. base::scoped_nsobject<CBCharacteristic> cb_characteristic_;
  112. // Characteristic identifier.
  113. std::string identifier_;
  114. // Service UUID.
  115. BluetoothUUID uuid_;
  116. // Characteristic value.
  117. std::vector<uint8_t> value_;
  118. // The destructor runs callbacks. Methods can use |destructor_called_| to
  119. // protect against reentrant calls to a partially deleted instance.
  120. bool destructor_called_ = false;
  121. // ReadRemoteCharacteristic request callback.
  122. ValueCallback read_characteristic_value_callback_;
  123. // WriteRemoteCharacteristic request callbacks.
  124. std::pair<base::OnceClosure, ErrorCallback>
  125. write_characteristic_value_callbacks_;
  126. // Stores callbacks for SubscribeToNotifications and
  127. // UnsubscribeFromNotifications requests.
  128. typedef std::pair<base::OnceClosure, ErrorCallback> PendingNotifyCallbacks;
  129. // Stores SubscribeToNotifications request callbacks.
  130. PendingNotifyCallbacks subscribe_to_notification_callbacks_;
  131. // Stores UnsubscribeFromNotifications request callbacks.
  132. PendingNotifyCallbacks unsubscribe_from_notification_callbacks_;
  133. base::WeakPtrFactory<BluetoothRemoteGattCharacteristicMac> weak_ptr_factory_;
  134. };
  135. // Stream operator for logging.
  136. DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<(
  137. std::ostream& out,
  138. const BluetoothRemoteGattCharacteristicMac& characteristic);
  139. } // namespace device
  140. #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_MAC_H_