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_chromeos.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 chromeos { | |
14 | |
15 BluetoothGattConnectionChromeOS::BluetoothGattConnectionChromeOS( | |
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 BluetoothGattConnectionChromeOS::~BluetoothGattConnectionChromeOS() { | |
30 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( | |
31 this); | |
32 Disconnect(); | |
33 } | |
34 | |
35 bool BluetoothGattConnectionChromeOS::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 BluetoothGattConnectionChromeOS::Disconnect() { | |
54 if (!connected_) { | |
55 VLOG(1) << "Connection already inactive."; | |
56 return; | |
57 } | |
58 | |
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 | |
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 | |
63 // success to indicate that this BluetoothGattConnection is no longer active, | |
64 // even though the underlying connection won't actually be disconnected. This | |
65 // technically doesn't violate the contract put forth by this API. | |
66 connected_ = false; | |
67 } | |
68 | |
69 void BluetoothGattConnectionChromeOS::DeviceRemoved( | |
70 const dbus::ObjectPath& object_path) { | |
71 if (object_path != object_path_) | |
72 return; | |
73 | |
74 connected_ = false; | |
75 } | |
76 | |
77 void BluetoothGattConnectionChromeOS::DevicePropertyChanged( | |
78 const dbus::ObjectPath& object_path, | |
79 const std::string& property_name) { | |
80 if (object_path != object_path_) | |
81 return; | |
82 | |
83 if (!connected_) | |
84 return; | |
85 | |
86 bluez::BluetoothDeviceClient::Properties* properties = | |
87 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
88 object_path_); | |
89 | |
90 if (!properties) { | |
91 connected_ = false; | |
92 return; | |
93 } | |
94 | |
95 if (property_name == properties->connected.name() && | |
96 !properties->connected.value()) | |
97 connected_ = false; | |
98 | |
99 // The remote device's bluetooth address may change if it is paired while | |
100 // connected. | |
101 if (property_name == properties->address.name()) | |
102 device_address_ = properties->address.value(); | |
103 } | |
104 | |
105 } // namespace chromeos | |
OLD | NEW |