| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 | |
| 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_BLUEZ_H_ | |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_BLUEZ_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/sequenced_task_runner.h" | |
| 17 #include "dbus/object_path.h" | |
| 18 #include "device/bluetooth/bluetooth_device.h" | |
| 19 #include "device/bluetooth/bluetooth_export.h" | |
| 20 #include "device/bluetooth/dbus/bluetooth_device_client.h" | |
| 21 #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h" | |
| 22 | |
| 23 namespace device { | |
| 24 class BluetoothSocketThread; | |
| 25 } // namespace device | |
| 26 | |
| 27 namespace bluez { | |
| 28 | |
| 29 class BluetoothAdapterBlueZ; | |
| 30 class BluetoothPairingBlueZ; | |
| 31 | |
| 32 // The BluetoothDeviceBlueZ class implements BluetoothDevice for platforms using | |
| 33 // BlueZ. | |
| 34 // | |
| 35 // This class is not thread-safe, but is only called from the UI thread. | |
| 36 // | |
| 37 // A socket thread is used to create sockets but posts all callbacks on the UI | |
| 38 // thread. | |
| 39 class DEVICE_BLUETOOTH_EXPORT BluetoothDeviceBlueZ | |
| 40 : public device::BluetoothDevice, | |
| 41 public bluez::BluetoothGattServiceClient::Observer { | |
| 42 public: | |
| 43 // BluetoothDevice override | |
| 44 uint32_t GetBluetoothClass() const override; | |
| 45 std::string GetAddress() const override; | |
| 46 VendorIDSource GetVendorIDSource() const override; | |
| 47 uint16_t GetVendorID() const override; | |
| 48 uint16_t GetProductID() const override; | |
| 49 uint16_t GetDeviceID() const override; | |
| 50 uint16_t GetAppearance() const override; | |
| 51 bool IsPaired() const override; | |
| 52 bool IsConnected() const override; | |
| 53 bool IsGattConnected() const override; | |
| 54 bool IsConnectable() const override; | |
| 55 bool IsConnecting() const override; | |
| 56 UUIDList GetUUIDs() const override; | |
| 57 int16_t GetInquiryRSSI() const override; | |
| 58 int16_t GetInquiryTxPower() const override; | |
| 59 bool ExpectingPinCode() const override; | |
| 60 bool ExpectingPasskey() const override; | |
| 61 bool ExpectingConfirmation() const override; | |
| 62 void GetConnectionInfo(const ConnectionInfoCallback& callback) override; | |
| 63 void Connect(device::BluetoothDevice::PairingDelegate* pairing_delegate, | |
| 64 const base::Closure& callback, | |
| 65 const ConnectErrorCallback& error_callback) override; | |
| 66 void SetPinCode(const std::string& pincode) override; | |
| 67 void SetPasskey(uint32_t passkey) override; | |
| 68 void ConfirmPairing() override; | |
| 69 void RejectPairing() override; | |
| 70 void CancelPairing() override; | |
| 71 void Disconnect(const base::Closure& callback, | |
| 72 const ErrorCallback& error_callback) override; | |
| 73 void Forget(const base::Closure& callback, | |
| 74 const ErrorCallback& error_callback) override; | |
| 75 void ConnectToService( | |
| 76 const device::BluetoothUUID& uuid, | |
| 77 const ConnectToServiceCallback& callback, | |
| 78 const ConnectToServiceErrorCallback& error_callback) override; | |
| 79 void ConnectToServiceInsecurely( | |
| 80 const device::BluetoothUUID& uuid, | |
| 81 const ConnectToServiceCallback& callback, | |
| 82 const ConnectToServiceErrorCallback& error_callback) override; | |
| 83 void CreateGattConnection( | |
| 84 const GattConnectionCallback& callback, | |
| 85 const ConnectErrorCallback& error_callback) override; | |
| 86 void Pair(device::BluetoothDevice::PairingDelegate* pairing_delegate, | |
| 87 const base::Closure& callback, | |
| 88 const ConnectErrorCallback& error_callback) override; | |
| 89 | |
| 90 // Creates a pairing object with the given delegate |pairing_delegate| and | |
| 91 // establishes it as the pairing context for this device. All pairing-related | |
| 92 // method calls will be forwarded to this object until it is released. | |
| 93 BluetoothPairingBlueZ* BeginPairing( | |
| 94 BluetoothDevice::PairingDelegate* pairing_delegate); | |
| 95 | |
| 96 // Releases the current pairing object, any pairing-related method calls will | |
| 97 // be ignored. | |
| 98 void EndPairing(); | |
| 99 | |
| 100 // Returns the current pairing object or NULL if no pairing is in progress. | |
| 101 BluetoothPairingBlueZ* GetPairing() const; | |
| 102 | |
| 103 // Returns the object path of the device. | |
| 104 const dbus::ObjectPath& object_path() const { return object_path_; } | |
| 105 | |
| 106 // Returns the adapter which owns this device instance. | |
| 107 BluetoothAdapterBlueZ* adapter() const; | |
| 108 | |
| 109 protected: | |
| 110 // BluetoothDevice override | |
| 111 std::string GetDeviceName() const override; | |
| 112 void CreateGattConnectionImpl() override; | |
| 113 void DisconnectGatt() override; | |
| 114 | |
| 115 private: | |
| 116 friend class BluetoothAdapterBlueZ; | |
| 117 | |
| 118 BluetoothDeviceBlueZ( | |
| 119 BluetoothAdapterBlueZ* adapter, | |
| 120 const dbus::ObjectPath& object_path, | |
| 121 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
| 122 scoped_refptr<device::BluetoothSocketThread> socket_thread); | |
| 123 ~BluetoothDeviceBlueZ() override; | |
| 124 | |
| 125 // bluez::BluetoothGattServiceClient::Observer overrides. | |
| 126 void GattServiceAdded(const dbus::ObjectPath& object_path) override; | |
| 127 void GattServiceRemoved(const dbus::ObjectPath& object_path) override; | |
| 128 | |
| 129 // Called by dbus:: on completion of the D-Bus method call to get the | |
| 130 // connection attributes of the current connection to the device. | |
| 131 void OnGetConnInfo(const ConnectionInfoCallback& callback, | |
| 132 int16_t rssi, | |
| 133 int16_t transmit_power, | |
| 134 int16_t max_transmit_power); | |
| 135 void OnGetConnInfoError(const ConnectionInfoCallback& callback, | |
| 136 const std::string& error_name, | |
| 137 const std::string& error_message); | |
| 138 | |
| 139 // Internal method to initiate a connection to this device, and methods called | |
| 140 // by dbus:: on completion of the D-Bus method call. | |
| 141 void ConnectInternal(bool after_pairing, | |
| 142 const base::Closure& callback, | |
| 143 const ConnectErrorCallback& error_callback); | |
| 144 void OnConnect(bool after_pairing, const base::Closure& callback); | |
| 145 void OnCreateGattConnection(const GattConnectionCallback& callback); | |
| 146 void OnConnectError(bool after_pairing, | |
| 147 const ConnectErrorCallback& error_callback, | |
| 148 const std::string& error_name, | |
| 149 const std::string& error_message); | |
| 150 | |
| 151 // Called by dbus:: on completion of the D-Bus method call to pair the device, | |
| 152 // made inside |Connect()|. | |
| 153 void OnPairDuringConnect(const base::Closure& callback, | |
| 154 const ConnectErrorCallback& error_callback); | |
| 155 void OnPairDuringConnectError(const ConnectErrorCallback& error_callback, | |
| 156 const std::string& error_name, | |
| 157 const std::string& error_message); | |
| 158 | |
| 159 // Called by dbus: on completion of the D-Bus method call to pair the device, | |
| 160 // made inside |Pair()|. | |
| 161 void OnPair(const base::Closure& callback); | |
| 162 void OnPairError(const ConnectErrorCallback& error_callback, | |
| 163 const std::string& error_name, | |
| 164 const std::string& error_message); | |
| 165 | |
| 166 // Called by dbus:: on failure of the D-Bus method call to cancel pairing, | |
| 167 // there is no matching completion call since we don't do anything special | |
| 168 // in that case. | |
| 169 void OnCancelPairingError(const std::string& error_name, | |
| 170 const std::string& error_message); | |
| 171 | |
| 172 // Internal method to set the device as trusted. Trusted devices can connect | |
| 173 // to us automatically, and we can connect to them after rebooting; it also | |
| 174 // causes the device to be remembered by the stack even if not paired. | |
| 175 // |success| to the callback indicates whether or not the request succeeded. | |
| 176 void SetTrusted(); | |
| 177 void OnSetTrusted(bool success); | |
| 178 | |
| 179 // Called by dbus:: on completion of the D-Bus method call to disconnect the | |
| 180 // device. | |
| 181 void OnDisconnect(const base::Closure& callback); | |
| 182 void OnDisconnectError(const ErrorCallback& error_callback, | |
| 183 const std::string& error_name, | |
| 184 const std::string& error_message); | |
| 185 | |
| 186 // Called by dbus:: on failure of the D-Bus method call to unpair the device; | |
| 187 // there is no matching completion call since this object is deleted in the | |
| 188 // process of unpairing. | |
| 189 void OnForgetError(const ErrorCallback& error_callback, | |
| 190 const std::string& error_name, | |
| 191 const std::string& error_message); | |
| 192 | |
| 193 // The dbus object path of the device object. | |
| 194 dbus::ObjectPath object_path_; | |
| 195 | |
| 196 // Number of ongoing calls to Connect(). | |
| 197 int num_connecting_calls_; | |
| 198 | |
| 199 // True if the connection monitor has been started, tracking the connection | |
| 200 // RSSI and TX power. | |
| 201 bool connection_monitor_started_; | |
| 202 | |
| 203 // UI thread task runner and socket thread object used to create sockets. | |
| 204 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | |
| 205 scoped_refptr<device::BluetoothSocketThread> socket_thread_; | |
| 206 | |
| 207 // During pairing this is set to an object that we don't own, but on which | |
| 208 // we can make method calls to request, display or confirm PIN Codes and | |
| 209 // Passkeys. Generally it is the object that owns this one. | |
| 210 std::unique_ptr<BluetoothPairingBlueZ> pairing_; | |
| 211 | |
| 212 // Note: This should remain the last member so it'll be destroyed and | |
| 213 // invalidate its weak pointers before any other members are destroyed. | |
| 214 base::WeakPtrFactory<BluetoothDeviceBlueZ> weak_ptr_factory_; | |
| 215 | |
| 216 DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceBlueZ); | |
| 217 }; | |
| 218 | |
| 219 } // namespace bluez | |
| 220 | |
| 221 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_BLUEZ_H_ | |
| OLD | NEW |