| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/bluetooth_gatt_connection_chromeos.h" | 5 #include "device/bluetooth/bluetooth_gatt_connection_bluez.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "device/bluetooth/bluetooth_adapter.h" | 9 #include "device/bluetooth/bluetooth_adapter.h" |
| 10 #include "device/bluetooth/bluetooth_device.h" | 10 #include "device/bluetooth/bluetooth_device.h" |
| 11 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 11 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 12 | 12 |
| 13 namespace chromeos { | 13 namespace bluez { |
| 14 | 14 |
| 15 BluetoothGattConnectionChromeOS::BluetoothGattConnectionChromeOS( | 15 BluetoothGattConnectionBlueZ::BluetoothGattConnectionBlueZ( |
| 16 scoped_refptr<device::BluetoothAdapter> adapter, | 16 scoped_refptr<device::BluetoothAdapter> adapter, |
| 17 const std::string& device_address, | 17 const std::string& device_address, |
| 18 const dbus::ObjectPath& object_path) | 18 const dbus::ObjectPath& object_path) |
| 19 : BluetoothGattConnection(adapter.get(), device_address), | 19 : BluetoothGattConnection(adapter.get(), device_address), |
| 20 connected_(true), | 20 connected_(true), |
| 21 object_path_(object_path) { | 21 object_path_(object_path) { |
| 22 DCHECK(adapter_.get()); | 22 DCHECK(adapter_.get()); |
| 23 DCHECK(!device_address_.empty()); | 23 DCHECK(!device_address_.empty()); |
| 24 DCHECK(object_path_.IsValid()); | 24 DCHECK(object_path_.IsValid()); |
| 25 | 25 |
| 26 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); | 26 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); |
| 27 } | 27 } |
| 28 | 28 |
| 29 BluetoothGattConnectionChromeOS::~BluetoothGattConnectionChromeOS() { | 29 BluetoothGattConnectionBlueZ::~BluetoothGattConnectionBlueZ() { |
| 30 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( | 30 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( |
| 31 this); | 31 this); |
| 32 Disconnect(); | 32 Disconnect(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 bool BluetoothGattConnectionChromeOS::IsConnected() { | 35 bool BluetoothGattConnectionBlueZ::IsConnected() { |
| 36 // Lazily determine the activity state of the connection. If already | 36 // Lazily determine the activity state of the connection. If already |
| 37 // marked as inactive, then return false. Otherwise, explicitly mark | 37 // marked as inactive, then return false. Otherwise, explicitly mark |
| 38 // |connected_| as false if the device is removed or disconnected. We do this, | 38 // |connected_| as false if the device is removed or disconnected. We do this, |
| 39 // so that if this method is called during a call to DeviceRemoved or | 39 // so that if this method is called during a call to DeviceRemoved or |
| 40 // DeviceChanged somewhere else, it returns the correct status. | 40 // DeviceChanged somewhere else, it returns the correct status. |
| 41 if (!connected_) | 41 if (!connected_) |
| 42 return false; | 42 return false; |
| 43 | 43 |
| 44 bluez::BluetoothDeviceClient::Properties* properties = | 44 bluez::BluetoothDeviceClient::Properties* properties = |
| 45 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 45 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
| 46 object_path_); | 46 object_path_); |
| 47 if (!properties || !properties->connected.value()) | 47 if (!properties || !properties->connected.value()) |
| 48 connected_ = false; | 48 connected_ = false; |
| 49 | 49 |
| 50 return connected_; | 50 return connected_; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void BluetoothGattConnectionChromeOS::Disconnect() { | 53 void BluetoothGattConnectionBlueZ::Disconnect() { |
| 54 if (!connected_) { | 54 if (!connected_) { |
| 55 VLOG(1) << "Connection already inactive."; | 55 VLOG(1) << "Connection already inactive."; |
| 56 return; | 56 return; |
| 57 } | 57 } |
| 58 | 58 |
| 59 // TODO(armansito): There isn't currently a good way to manage the ownership | 59 // TODO(armansito): There isn't currently a good way to manage the ownership |
| 60 // of a connection between Chrome and bluetoothd plugins/profiles. Until | 60 // of a connection between Chrome and bluetoothd plugins/profiles. Until |
| 61 // a proper reference count is kept by bluetoothd, we might unwittingly kill | 61 // a proper reference count is kept by bluetoothd, we might unwittingly kill |
| 62 // a connection that is managed by the daemon (e.g. HoG). For now, just return | 62 // a connection that is managed by the daemon (e.g. HoG). For now, just return |
| 63 // success to indicate that this BluetoothGattConnection is no longer active, | 63 // success to indicate that this BluetoothGattConnection is no longer active, |
| 64 // even though the underlying connection won't actually be disconnected. This | 64 // even though the underlying connection won't actually be disconnected. This |
| 65 // technically doesn't violate the contract put forth by this API. | 65 // technically doesn't violate the contract put forth by this API. |
| 66 connected_ = false; | 66 connected_ = false; |
| 67 } | 67 } |
| 68 | 68 |
| 69 void BluetoothGattConnectionChromeOS::DeviceRemoved( | 69 void BluetoothGattConnectionBlueZ::DeviceRemoved( |
| 70 const dbus::ObjectPath& object_path) { | 70 const dbus::ObjectPath& object_path) { |
| 71 if (object_path != object_path_) | 71 if (object_path != object_path_) |
| 72 return; | 72 return; |
| 73 | 73 |
| 74 connected_ = false; | 74 connected_ = false; |
| 75 } | 75 } |
| 76 | 76 |
| 77 void BluetoothGattConnectionChromeOS::DevicePropertyChanged( | 77 void BluetoothGattConnectionBlueZ::DevicePropertyChanged( |
| 78 const dbus::ObjectPath& object_path, | 78 const dbus::ObjectPath& object_path, |
| 79 const std::string& property_name) { | 79 const std::string& property_name) { |
| 80 if (object_path != object_path_) | 80 if (object_path != object_path_) |
| 81 return; | 81 return; |
| 82 | 82 |
| 83 if (!connected_) | 83 if (!connected_) |
| 84 return; | 84 return; |
| 85 | 85 |
| 86 bluez::BluetoothDeviceClient::Properties* properties = | 86 bluez::BluetoothDeviceClient::Properties* properties = |
| 87 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 87 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
| 88 object_path_); | 88 object_path_); |
| 89 | 89 |
| 90 if (!properties) { | 90 if (!properties) { |
| 91 connected_ = false; | 91 connected_ = false; |
| 92 return; | 92 return; |
| 93 } | 93 } |
| 94 | 94 |
| 95 if (property_name == properties->connected.name() && | 95 if (property_name == properties->connected.name() && |
| 96 !properties->connected.value()) | 96 !properties->connected.value()) |
| 97 connected_ = false; | 97 connected_ = false; |
| 98 | 98 |
| 99 // The remote device's bluetooth address may change if it is paired while | 99 // The remote device's bluetooth address may change if it is paired while |
| 100 // connected. | 100 // connected. |
| 101 if (property_name == properties->address.name()) | 101 if (property_name == properties->address.name()) |
| 102 device_address_ = properties->address.value(); | 102 device_address_ = properties->address.value(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 } // namespace chromeos | 105 } // namespace bluez |
| OLD | NEW |