| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/bluetooth/bluetooth_gatt_connection_bluez.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "device/bluetooth/bluetooth_adapter.h" | |
| 10 #include "device/bluetooth/bluetooth_device.h" | |
| 11 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 12 | |
| 13 namespace bluez { | |
| 14 | |
| 15 BluetoothGattConnectionBlueZ::BluetoothGattConnectionBlueZ( | |
| 16 scoped_refptr<device::BluetoothAdapter> adapter, | |
| 17 const std::string& device_address, | |
| 18 const dbus::ObjectPath& object_path) | |
| 19 : BluetoothGattConnection(adapter.get(), device_address), | |
| 20 connected_(true), | |
| 21 object_path_(object_path) { | |
| 22 DCHECK(adapter_.get()); | |
| 23 DCHECK(!device_address_.empty()); | |
| 24 DCHECK(object_path_.IsValid()); | |
| 25 | |
| 26 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); | |
| 27 } | |
| 28 | |
| 29 BluetoothGattConnectionBlueZ::~BluetoothGattConnectionBlueZ() { | |
| 30 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( | |
| 31 this); | |
| 32 Disconnect(); | |
| 33 } | |
| 34 | |
| 35 bool BluetoothGattConnectionBlueZ::IsConnected() { | |
| 36 // Lazily determine the activity state of the connection. If already | |
| 37 // marked as inactive, then return false. Otherwise, explicitly mark | |
| 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 | |
| 40 // DeviceChanged somewhere else, it returns the correct status. | |
| 41 if (!connected_) | |
| 42 return false; | |
| 43 | |
| 44 bluez::BluetoothDeviceClient::Properties* properties = | |
| 45 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
| 46 object_path_); | |
| 47 if (!properties || !properties->connected.value()) | |
| 48 connected_ = false; | |
| 49 | |
| 50 return connected_; | |
| 51 } | |
| 52 | |
| 53 void BluetoothGattConnectionBlueZ::Disconnect() { | |
| 54 if (!connected_) { | |
| 55 VLOG(1) << "Connection already inactive."; | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 connected_ = false; | |
| 60 BluetoothGattConnection::Disconnect(); | |
| 61 } | |
| 62 | |
| 63 void BluetoothGattConnectionBlueZ::DeviceRemoved( | |
| 64 const dbus::ObjectPath& object_path) { | |
| 65 if (object_path != object_path_) | |
| 66 return; | |
| 67 | |
| 68 connected_ = false; | |
| 69 } | |
| 70 | |
| 71 void BluetoothGattConnectionBlueZ::DevicePropertyChanged( | |
| 72 const dbus::ObjectPath& object_path, | |
| 73 const std::string& property_name) { | |
| 74 if (object_path != object_path_) | |
| 75 return; | |
| 76 | |
| 77 if (!connected_) | |
| 78 return; | |
| 79 | |
| 80 bluez::BluetoothDeviceClient::Properties* properties = | |
| 81 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
| 82 object_path_); | |
| 83 | |
| 84 if (!properties) { | |
| 85 connected_ = false; | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 if (property_name == properties->connected.name() && | |
| 90 !properties->connected.value()) | |
| 91 connected_ = false; | |
| 92 | |
| 93 // The remote device's bluetooth address may change if it is paired while | |
| 94 // connected. | |
| 95 if (property_name == properties->address.name()) | |
| 96 device_address_ = properties->address.value(); | |
| 97 } | |
| 98 | |
| 99 } // namespace bluez | |
| OLD | NEW |