bluetooth_device_win.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (c) 2012 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_DEVICE_WIN_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_WIN_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/task/sequenced_task_runner.h"
  13. #include "device/bluetooth/bluetooth_device.h"
  14. #include "device/bluetooth/bluetooth_export.h"
  15. #include "device/bluetooth/bluetooth_task_manager_win.h"
  16. namespace device {
  17. class BluetoothAdapterWin;
  18. class BluetoothRemoteGattServiceWin;
  19. class BluetoothServiceRecordWin;
  20. class BluetoothSocketThread;
  21. class BluetoothUUID;
  22. class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceWin
  23. : public BluetoothDevice,
  24. public BluetoothAdapter::Observer {
  25. public:
  26. explicit BluetoothDeviceWin(
  27. BluetoothAdapterWin* adapter,
  28. const BluetoothTaskManagerWin::DeviceState& device_state,
  29. scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
  30. scoped_refptr<BluetoothSocketThread> socket_thread);
  31. BluetoothDeviceWin(const BluetoothDeviceWin&) = delete;
  32. BluetoothDeviceWin& operator=(const BluetoothDeviceWin&) = delete;
  33. ~BluetoothDeviceWin() override;
  34. // BluetoothDevice override
  35. uint32_t GetBluetoothClass() const override;
  36. std::string GetAddress() const override;
  37. AddressType GetAddressType() const override;
  38. 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. UUIDSet GetUUIDs() const override;
  50. absl::optional<int8_t> GetInquiryRSSI() const override;
  51. absl::optional<int8_t> GetInquiryTxPower() const override;
  52. bool ExpectingPinCode() const override;
  53. bool ExpectingPasskey() const override;
  54. bool ExpectingConfirmation() const override;
  55. void GetConnectionInfo(ConnectionInfoCallback callback) override;
  56. void SetConnectionLatency(ConnectionLatency connection_latency,
  57. base::OnceClosure callback,
  58. ErrorCallback error_callback) override;
  59. void Connect(PairingDelegate* pairing_delegate,
  60. ConnectCallback callback) override;
  61. void SetPinCode(const std::string& pincode) override;
  62. void SetPasskey(uint32_t passkey) override;
  63. void ConfirmPairing() override;
  64. void RejectPairing() override;
  65. void CancelPairing() override;
  66. void Disconnect(base::OnceClosure callback,
  67. ErrorCallback error_callback) override;
  68. void Forget(base::OnceClosure callback,
  69. ErrorCallback error_callback) override;
  70. void ConnectToService(const BluetoothUUID& uuid,
  71. ConnectToServiceCallback callback,
  72. ConnectToServiceErrorCallback error_callback) override;
  73. void ConnectToServiceInsecurely(
  74. const BluetoothUUID& uuid,
  75. ConnectToServiceCallback callback,
  76. ConnectToServiceErrorCallback error_callback) override;
  77. // Used by BluetoothProfileWin to retrieve the service record for the given
  78. // |uuid|.
  79. const BluetoothServiceRecordWin* GetServiceRecord(
  80. const device::BluetoothUUID& uuid) const;
  81. // Returns true if all fields and services of this instance are equal to the
  82. // fields and services stored in |device_state|.
  83. bool IsEqual(const BluetoothTaskManagerWin::DeviceState& device_state);
  84. // Updates this instance with all fields and properties stored in
  85. // |device_state|.
  86. void Update(const BluetoothTaskManagerWin::DeviceState& device_state);
  87. // Notify |service| discovery complete, |service| is a remote GATT service of
  88. // this device.
  89. void GattServiceDiscoveryComplete(BluetoothRemoteGattServiceWin* service);
  90. protected:
  91. // BluetoothDevice override
  92. void CreateGattConnectionImpl(
  93. absl::optional<BluetoothUUID> service_uuid) override;
  94. void DisconnectGatt() override;
  95. private:
  96. friend class BluetoothAdapterWin;
  97. // Used by BluetoothAdapterWin to update the visible state during
  98. // discovery.
  99. void SetVisible(bool visible);
  100. // Updates the services with services stored in |device_state|.
  101. void UpdateServices(const BluetoothTaskManagerWin::DeviceState& device_state);
  102. // Checks if GATT service with |uuid| and |attribute_handle| has already been
  103. // discovered.
  104. bool IsGattServiceDiscovered(const BluetoothUUID& uuid,
  105. uint16_t attribute_handle);
  106. // Checks if |service| still exist on device according to newly discovered
  107. // |service_state|.
  108. bool DoesGattServiceExist(
  109. const std::vector<std::unique_ptr<
  110. BluetoothTaskManagerWin::ServiceRecordState>>& service_state,
  111. BluetoothRemoteGattService* service);
  112. // Updates the GATT services with the services stored in |service_state|.
  113. void UpdateGattServices(
  114. const std::vector<
  115. std::unique_ptr<BluetoothTaskManagerWin::ServiceRecordState>>&
  116. service_state);
  117. scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
  118. scoped_refptr<BluetoothSocketThread> socket_thread_;
  119. // The Bluetooth class of the device, a bitmask that may be decoded using
  120. // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm
  121. uint32_t bluetooth_class_;
  122. // The name of the device, as supplied by the remote device.
  123. absl::optional<std::string> name_;
  124. // The Bluetooth address of the device.
  125. std::string address_;
  126. // Tracked device state, updated by the adapter managing the lifecycle of
  127. // the device.
  128. bool paired_;
  129. bool connected_;
  130. bool is_low_energy_;
  131. // Used to send change notifications when a device disappears during
  132. // discovery.
  133. bool visible_;
  134. // The services (identified by UUIDs) that this device provides.
  135. UUIDSet uuids_;
  136. // The service records retrieved from SDP.
  137. std::vector<std::unique_ptr<BluetoothServiceRecordWin>> service_record_list_;
  138. // The element of the set is the uuid / attribute handle pair of the
  139. // BluetoothRemoteGattServiceWin instance.
  140. std::set<std::pair<BluetoothUUID, uint16_t>>
  141. discovery_completed_included_services_;
  142. };
  143. } // namespace device
  144. #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_WIN_H_