OLD | NEW |
| (Empty) |
1 // Copyright 2015 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_adapter_profile_bluez.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/logging.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "dbus/bus.h" | |
15 #include "dbus/object_path.h" | |
16 #include "device/bluetooth/bluetooth_adapter_bluez.h" | |
17 #include "device/bluetooth/bluetooth_uuid.h" | |
18 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" | |
19 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
20 | |
21 namespace bluez { | |
22 | |
23 // static | |
24 void BluetoothAdapterProfileBlueZ::Register( | |
25 const device::BluetoothUUID& uuid, | |
26 const bluez::BluetoothProfileManagerClient::Options& options, | |
27 const ProfileRegisteredCallback& success_callback, | |
28 const bluez::BluetoothProfileManagerClient::ErrorCallback& error_callback) { | |
29 std::unique_ptr<BluetoothAdapterProfileBlueZ> profile( | |
30 new BluetoothAdapterProfileBlueZ(uuid)); | |
31 | |
32 VLOG(1) << "Registering profile: " << profile->object_path().value(); | |
33 const dbus::ObjectPath& object_path = profile->object_path(); | |
34 bluez::BluezDBusManager::Get() | |
35 ->GetBluetoothProfileManagerClient() | |
36 ->RegisterProfile(object_path, uuid.canonical_value(), options, | |
37 base::Bind(success_callback, base::Passed(&profile)), | |
38 error_callback); | |
39 } | |
40 | |
41 BluetoothAdapterProfileBlueZ::BluetoothAdapterProfileBlueZ( | |
42 const device::BluetoothUUID& uuid) | |
43 : uuid_(uuid), weak_ptr_factory_(this) { | |
44 std::string uuid_path; | |
45 base::ReplaceChars(uuid.canonical_value(), ":-", "_", &uuid_path); | |
46 object_path_ = | |
47 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path); | |
48 | |
49 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus(); | |
50 profile_.reset(bluez::BluetoothProfileServiceProvider::Create( | |
51 system_bus, object_path_, this)); | |
52 DCHECK(profile_.get()); | |
53 } | |
54 | |
55 BluetoothAdapterProfileBlueZ::~BluetoothAdapterProfileBlueZ() {} | |
56 | |
57 bool BluetoothAdapterProfileBlueZ::SetDelegate( | |
58 const dbus::ObjectPath& device_path, | |
59 bluez::BluetoothProfileServiceProvider::Delegate* delegate) { | |
60 DCHECK(delegate); | |
61 VLOG(1) << "SetDelegate: " << object_path_.value() << " dev " | |
62 << device_path.value(); | |
63 | |
64 if (delegates_.find(device_path.value()) != delegates_.end()) { | |
65 return false; | |
66 } | |
67 | |
68 delegates_[device_path.value()] = delegate; | |
69 return true; | |
70 } | |
71 | |
72 void BluetoothAdapterProfileBlueZ::RemoveDelegate( | |
73 const dbus::ObjectPath& device_path, | |
74 const base::Closure& unregistered_callback) { | |
75 VLOG(1) << object_path_.value() << " dev " << device_path.value() | |
76 << ": RemoveDelegate"; | |
77 | |
78 if (delegates_.find(device_path.value()) == delegates_.end()) | |
79 return; | |
80 | |
81 delegates_.erase(device_path.value()); | |
82 | |
83 if (delegates_.size() != 0) | |
84 return; | |
85 | |
86 VLOG(1) << device_path.value() << " No delegates left, unregistering."; | |
87 | |
88 // No users left, release the profile. | |
89 bluez::BluezDBusManager::Get() | |
90 ->GetBluetoothProfileManagerClient() | |
91 ->UnregisterProfile( | |
92 object_path_, unregistered_callback, | |
93 base::Bind(&BluetoothAdapterProfileBlueZ::OnUnregisterProfileError, | |
94 weak_ptr_factory_.GetWeakPtr(), unregistered_callback)); | |
95 } | |
96 | |
97 void BluetoothAdapterProfileBlueZ::OnUnregisterProfileError( | |
98 const base::Closure& unregistered_callback, | |
99 const std::string& error_name, | |
100 const std::string& error_message) { | |
101 LOG(WARNING) << this->object_path().value() | |
102 << ": Failed to unregister profile: " << error_name << ": " | |
103 << error_message; | |
104 | |
105 unregistered_callback.Run(); | |
106 } | |
107 | |
108 // bluez::BluetoothProfileServiceProvider::Delegate: | |
109 void BluetoothAdapterProfileBlueZ::Released() { | |
110 VLOG(1) << object_path_.value() << ": Release"; | |
111 } | |
112 | |
113 void BluetoothAdapterProfileBlueZ::NewConnection( | |
114 const dbus::ObjectPath& device_path, | |
115 std::unique_ptr<dbus::FileDescriptor> fd, | |
116 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | |
117 const ConfirmationCallback& callback) { | |
118 dbus::ObjectPath delegate_path = device_path; | |
119 | |
120 if (delegates_.find(device_path.value()) == delegates_.end()) | |
121 delegate_path = dbus::ObjectPath(""); | |
122 | |
123 if (delegates_.find(delegate_path.value()) == delegates_.end()) { | |
124 VLOG(1) << object_path_.value() << ": New connection for device " | |
125 << device_path.value() << " which has no delegates!"; | |
126 callback.Run(REJECTED); | |
127 return; | |
128 } | |
129 | |
130 delegates_[delegate_path.value()]->NewConnection(device_path, std::move(fd), | |
131 options, callback); | |
132 } | |
133 | |
134 void BluetoothAdapterProfileBlueZ::RequestDisconnection( | |
135 const dbus::ObjectPath& device_path, | |
136 const ConfirmationCallback& callback) { | |
137 dbus::ObjectPath delegate_path = device_path; | |
138 | |
139 if (delegates_.find(device_path.value()) == delegates_.end()) | |
140 delegate_path = dbus::ObjectPath(""); | |
141 | |
142 if (delegates_.find(delegate_path.value()) == delegates_.end()) { | |
143 VLOG(1) << object_path_.value() << ": RequestDisconnection for device " | |
144 << device_path.value() << " which has no delegates!"; | |
145 return; | |
146 } | |
147 | |
148 delegates_[delegate_path.value()]->RequestDisconnection(device_path, | |
149 callback); | |
150 } | |
151 | |
152 void BluetoothAdapterProfileBlueZ::Cancel() { | |
153 // Cancel() should only go to a delegate accepting connections. | |
154 if (delegates_.find("") == delegates_.end()) { | |
155 VLOG(1) << object_path_.value() << ": Cancel with no delegate!"; | |
156 return; | |
157 } | |
158 | |
159 delegates_[""]->Cancel(); | |
160 } | |
161 | |
162 } // namespace bluez | |
OLD | NEW |