bluetooth_remote_gatt_descriptor_mac.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2017 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_DESCRIPTOR_MAC_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_DESCRIPTOR_MAC_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h"
  8. #include <vector>
  9. #include "base/mac/scoped_nsobject.h"
  10. #include "base/memory/weak_ptr.h"
  11. #if defined(__OBJC__)
  12. #import <CoreBluetooth/CoreBluetooth.h>
  13. #endif // defined(__OBJC__)
  14. namespace device {
  15. class BluetoothRemoteGattCharacteristicMac;
  16. // The BluetoothRemoteGattDescriptorMac class implements
  17. // BluetoothRemoteGattDescriptor for remote GATT services on macOS.
  18. class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattDescriptorMac
  19. : public BluetoothRemoteGattDescriptor {
  20. public:
  21. BluetoothRemoteGattDescriptorMac(
  22. BluetoothRemoteGattCharacteristicMac* characteristic,
  23. CBDescriptor* descriptor);
  24. ~BluetoothRemoteGattDescriptorMac() override;
  25. // BluetoothGattDescriptor
  26. std::string GetIdentifier() const override;
  27. BluetoothUUID GetUUID() const override;
  28. BluetoothGattCharacteristic::Permissions GetPermissions() const override;
  29. // BluetoothRemoteGattDescriptor
  30. const std::vector<uint8_t>& GetValue() const override;
  31. BluetoothRemoteGattCharacteristic* GetCharacteristic() const override;
  32. void ReadRemoteDescriptor(ValueCallback callback) override;
  33. void WriteRemoteDescriptor(const std::vector<uint8_t>& new_value,
  34. base::OnceClosure callback,
  35. ErrorCallback error_callback) override;
  36. private:
  37. friend class BluetoothLowEnergyDeviceMac;
  38. friend class BluetoothRemoteGattCharacteristicMac;
  39. friend class BluetoothTestMac;
  40. // Calls callbacks, when -[id<CBPeripheralDelegate>
  41. // peripheral:didUpdateValueForDescriptor:error:] is called.
  42. void DidUpdateValueForDescriptor(NSError* error);
  43. // Calls callbacks, when -[id<CBPeripheralDelegate>
  44. // peripheral:didWriteValueForDescriptor:error:] is called.
  45. void DidWriteValueForDescriptor(NSError* error);
  46. bool HasPendingRead() const { return !read_value_callback_.is_null(); }
  47. bool HasPendingWrite() const {
  48. return !write_value_callbacks_.first.is_null();
  49. }
  50. // Returns CoreBluetooth peripheral.
  51. CBPeripheral* GetCBPeripheral() const;
  52. // Returns CoreBluetooth descriptor.
  53. CBDescriptor* GetCBDescriptor() const;
  54. // gatt_characteristic_ owns instances of this class.
  55. raw_ptr<BluetoothRemoteGattCharacteristicMac> gatt_characteristic_;
  56. // Descriptor from CoreBluetooth.
  57. base::scoped_nsobject<CBDescriptor> cb_descriptor_;
  58. // Descriptor identifier.
  59. std::string identifier_;
  60. // Descriptor UUID.
  61. BluetoothUUID uuid_;
  62. // Descriptor value.
  63. std::vector<uint8_t> value_;
  64. // The destructor runs callbacks. Methods can use |destructor_called_| to
  65. // protect against reentrant calls to a partially deleted instance.
  66. bool destructor_called_ = false;
  67. // ReadRemoteDescriptor request callback.
  68. ValueCallback read_value_callback_;
  69. // WriteRemoteDescriptor request callbacks.
  70. std::pair<base::OnceClosure, ErrorCallback> write_value_callbacks_;
  71. };
  72. // Stream operator for logging.
  73. DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<(
  74. std::ostream& out,
  75. const BluetoothRemoteGattDescriptorMac& descriptor);
  76. } // namespace device
  77. #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_DESCRIPTOR_MAC_H_