bluetooth_low_energy_device_mac.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2015 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_LOW_ENERGY_DEVICE_MAC_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_DEVICE_MAC_H_
  6. #import <CoreBluetooth/CoreBluetooth.h>
  7. #include <stdint.h>
  8. #include <set>
  9. #include "base/mac/scoped_nsobject.h"
  10. #include "build/build_config.h"
  11. #include "crypto/sha2.h"
  12. #include "device/bluetooth/bluetooth_device_mac.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #if !BUILDFLAG(IS_IOS)
  15. #import <IOBluetooth/IOBluetooth.h>
  16. #endif
  17. @class BluetoothLowEnergyPeripheralDelegate;
  18. namespace device {
  19. class BluetoothAdapterMac;
  20. class BluetoothRemoteGattServiceMac;
  21. class BluetoothRemoteGattCharacteristicMac;
  22. class BluetoothRemoteGattDescriptorMac;
  23. class BluetoothUUID;
  24. class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyDeviceMac
  25. : public BluetoothDeviceMac {
  26. public:
  27. BluetoothLowEnergyDeviceMac(BluetoothAdapterMac* adapter,
  28. CBPeripheral* peripheral);
  29. BluetoothLowEnergyDeviceMac(const BluetoothLowEnergyDeviceMac&) = delete;
  30. BluetoothLowEnergyDeviceMac& operator=(const BluetoothLowEnergyDeviceMac&) =
  31. delete;
  32. ~BluetoothLowEnergyDeviceMac() override;
  33. // BluetoothDevice overrides.
  34. std::string GetIdentifier() const override;
  35. uint32_t GetBluetoothClass() const override;
  36. std::string GetAddress() const override;
  37. AddressType GetAddressType() const override;
  38. BluetoothDevice::VendorIDSource GetVendorIDSource() const override;
  39. uint16_t GetVendorID() const override;
  40. uint16_t GetProductID() const override;
  41. uint16_t GetDeviceID() const override;
  42. uint16_t GetAppearance() const override;
  43. absl::optional<std::string> GetName() const override;
  44. bool IsPaired() const override;
  45. bool IsConnected() const override;
  46. bool IsGattConnected() const override;
  47. bool IsConnectable() const override;
  48. bool IsConnecting() const override;
  49. bool ExpectingPinCode() const override;
  50. bool ExpectingPasskey() const override;
  51. bool ExpectingConfirmation() const override;
  52. void GetConnectionInfo(ConnectionInfoCallback callback) override;
  53. void SetConnectionLatency(ConnectionLatency connection_latency,
  54. base::OnceClosure callback,
  55. ErrorCallback error_callback) override;
  56. void Connect(PairingDelegate* pairing_delegate,
  57. ConnectCallback callback) override;
  58. void SetPinCode(const std::string& pincode) override;
  59. void SetPasskey(uint32_t passkey) override;
  60. void ConfirmPairing() override;
  61. void RejectPairing() override;
  62. void CancelPairing() override;
  63. void Disconnect(base::OnceClosure callback,
  64. ErrorCallback error_callback) override;
  65. void Forget(base::OnceClosure callback,
  66. ErrorCallback error_callback) override;
  67. void ConnectToService(const BluetoothUUID& uuid,
  68. ConnectToServiceCallback callback,
  69. ConnectToServiceErrorCallback error_callback) override;
  70. void ConnectToServiceInsecurely(
  71. const device::BluetoothUUID& uuid,
  72. ConnectToServiceCallback callback,
  73. ConnectToServiceErrorCallback error_callback) override;
  74. bool IsLowEnergyDevice() override;
  75. protected:
  76. // BluetoothDevice override.
  77. void CreateGattConnectionImpl(
  78. absl::optional<BluetoothUUID> serivce_uuid) override;
  79. void DisconnectGatt() override;
  80. // Methods used by BluetoothLowEnergyPeripheralBridge.
  81. void DidDiscoverPrimaryServices(NSError* error);
  82. void DidModifyServices(NSArray* invalidatedServices);
  83. void DidDiscoverCharacteristics(CBService* cb_service, NSError* error);
  84. void DidUpdateValue(CBCharacteristic* characteristic, NSError* error);
  85. void DidWriteValue(CBCharacteristic* characteristic, NSError* error);
  86. void DidUpdateNotificationState(CBCharacteristic* characteristic,
  87. NSError* error);
  88. void DidDiscoverDescriptors(CBCharacteristic* characteristic, NSError* error);
  89. void DidUpdateValueForDescriptor(CBDescriptor* cb_descriptor, NSError* error);
  90. void DidWriteValueForDescriptor(CBDescriptor* descriptor, NSError* error);
  91. static std::string GetPeripheralIdentifier(CBPeripheral* peripheral);
  92. // Hashes and truncates the peripheral identifier to deterministically
  93. // construct an address. The use of fake addresses is a temporary fix before
  94. // we switch to using bluetooth identifiers throughout Chrome.
  95. // http://crbug.com/507824
  96. static std::string GetPeripheralHashAddress(CBPeripheral* peripheral);
  97. static std::string GetPeripheralHashAddress(
  98. base::StringPiece device_identifier);
  99. private:
  100. friend class BluetoothAdapterMac;
  101. friend class BluetoothAdapterMacTest;
  102. friend class BluetoothLowEnergyPeripheralBridge;
  103. friend class BluetoothRemoteGattServiceMac;
  104. friend class BluetoothTestMac;
  105. friend class BluetoothRemoteGattServiceMac;
  106. // Called by the adapter when the device is connected.
  107. void DidConnectPeripheral();
  108. // Calls macOS to discover primary services.
  109. void DiscoverPrimaryServices();
  110. // Sends notification if this device is ready with all services discovered.
  111. void SendNotificationIfDiscoveryComplete();
  112. // Returns the Bluetooth adapter.
  113. BluetoothAdapterMac* GetMacAdapter();
  114. BluetoothAdapterMac* GetMacAdapter() const;
  115. // Returns the CoreBluetooth Peripheral.
  116. CBPeripheral* GetPeripheral();
  117. // Returns BluetoothRemoteGattServiceMac based on the CBService.
  118. BluetoothRemoteGattServiceMac* GetBluetoothRemoteGattServiceMac(
  119. CBService* service) const;
  120. // Returns BluetoothRemoteGattCharacteristicMac based on the CBCharacteristic.
  121. BluetoothRemoteGattCharacteristicMac* GetBluetoothRemoteGattCharacteristicMac(
  122. CBCharacteristic* cb_characteristic) const;
  123. // Returns BluetoothRemoteGattDescriptorMac based on the CBDescriptor.
  124. BluetoothRemoteGattDescriptorMac* GetBluetoothRemoteGattDescriptorMac(
  125. CBDescriptor* cb_descriptor) const;
  126. // Callback used when the CoreBluetooth Peripheral is disconnected.
  127. void DidDisconnectPeripheral(NSError* error);
  128. // CoreBluetooth data structure.
  129. base::scoped_nsobject<CBPeripheral> peripheral_;
  130. // Objective-C delegate for the CBPeripheral.
  131. base::scoped_nsobject<BluetoothLowEnergyPeripheralDelegate>
  132. peripheral_delegate_;
  133. // Whether the device is connected.
  134. bool connected_;
  135. // The peripheral's identifier, as returned by [CBPeripheral identifier].
  136. std::string identifier_;
  137. // A local address for the device created by hashing the peripheral
  138. // identifier.
  139. std::string hash_address_;
  140. // Increases each time -[CBPeripheral discoverServices:] is called, and
  141. // decreases each time DidDiscoverPrimaryServices() is called. Once the
  142. // value is set to 0, characteristics and properties are discovered.
  143. int discovery_pending_count_;
  144. };
  145. // Stream operator for logging.
  146. DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<(
  147. std::ostream& out,
  148. const BluetoothLowEnergyDeviceMac& device);
  149. } // namespace device
  150. #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_DEVICE_MAC_H_