OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chromeos/dbus/fake_bluetooth_adapter_client.h" | 5 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
13 #include "chromeos/dbus/fake_bluetooth_device_client.h" | 13 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
15 | 15 |
16 namespace chromeos { | 16 namespace bluez { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Default interval for delayed tasks. | 20 // Default interval for delayed tasks. |
21 const int kSimulationIntervalMs = 750; | 21 const int kSimulationIntervalMs = 750; |
22 | 22 |
23 } // namespace | 23 } // namespace |
24 | 24 |
25 const char FakeBluetoothAdapterClient::kAdapterPath[] = | 25 const char FakeBluetoothAdapterClient::kAdapterPath[] = "/fake/hci0"; |
26 "/fake/hci0"; | 26 const char FakeBluetoothAdapterClient::kAdapterName[] = "Fake Adapter"; |
27 const char FakeBluetoothAdapterClient::kAdapterName[] = | 27 const char FakeBluetoothAdapterClient::kAdapterAddress[] = "01:1A:2B:1A:2B:03"; |
28 "Fake Adapter"; | |
29 const char FakeBluetoothAdapterClient::kAdapterAddress[] = | |
30 "01:1A:2B:1A:2B:03"; | |
31 | 28 |
32 const char FakeBluetoothAdapterClient::kSecondAdapterPath[] = | 29 const char FakeBluetoothAdapterClient::kSecondAdapterPath[] = "/fake/hci1"; |
33 "/fake/hci1"; | |
34 const char FakeBluetoothAdapterClient::kSecondAdapterName[] = | 30 const char FakeBluetoothAdapterClient::kSecondAdapterName[] = |
35 "Second Fake Adapter"; | 31 "Second Fake Adapter"; |
36 const char FakeBluetoothAdapterClient::kSecondAdapterAddress[] = | 32 const char FakeBluetoothAdapterClient::kSecondAdapterAddress[] = |
37 "00:DE:51:10:01:00"; | 33 "00:DE:51:10:01:00"; |
38 | 34 |
39 FakeBluetoothAdapterClient::Properties::Properties( | 35 FakeBluetoothAdapterClient::Properties::Properties( |
40 const PropertyChangedCallback& callback) | 36 const PropertyChangedCallback& callback) |
41 : BluetoothAdapterClient::Properties( | 37 : BluetoothAdapterClient::Properties( |
42 NULL, | 38 NULL, |
43 bluetooth_adapter::kBluetoothAdapterInterface, | 39 bluetooth_adapter::kBluetoothAdapterInterface, |
44 callback) { | 40 callback) {} |
45 } | |
46 | 41 |
47 FakeBluetoothAdapterClient::Properties::~Properties() { | 42 FakeBluetoothAdapterClient::Properties::~Properties() {} |
48 } | |
49 | 43 |
50 void FakeBluetoothAdapterClient::Properties::Get( | 44 void FakeBluetoothAdapterClient::Properties::Get( |
51 dbus::PropertyBase* property, | 45 dbus::PropertyBase* property, |
52 dbus::PropertySet::GetCallback callback) { | 46 dbus::PropertySet::GetCallback callback) { |
53 VLOG(1) << "Get " << property->name(); | 47 VLOG(1) << "Get " << property->name(); |
54 callback.Run(false); | 48 callback.Run(false); |
55 } | 49 } |
56 | 50 |
57 void FakeBluetoothAdapterClient::Properties::GetAll() { | 51 void FakeBluetoothAdapterClient::Properties::GetAll() { |
58 VLOG(1) << "GetAll"; | 52 VLOG(1) << "GetAll"; |
59 } | 53 } |
60 | 54 |
61 void FakeBluetoothAdapterClient::Properties::Set( | 55 void FakeBluetoothAdapterClient::Properties::Set( |
62 dbus::PropertyBase *property, | 56 dbus::PropertyBase* property, |
63 dbus::PropertySet::SetCallback callback) { | 57 dbus::PropertySet::SetCallback callback) { |
64 VLOG(1) << "Set " << property->name(); | 58 VLOG(1) << "Set " << property->name(); |
65 if (property->name() == powered.name() || | 59 if (property->name() == powered.name() || property->name() == alias.name() || |
66 property->name() == alias.name() || | |
67 property->name() == discoverable.name() || | 60 property->name() == discoverable.name() || |
68 property->name() == discoverable_timeout.name()) { | 61 property->name() == discoverable_timeout.name()) { |
69 callback.Run(true); | 62 callback.Run(true); |
70 property->ReplaceValueWithSetValue(); | 63 property->ReplaceValueWithSetValue(); |
71 } else { | 64 } else { |
72 callback.Run(false); | 65 callback.Run(false); |
73 } | 66 } |
74 } | 67 } |
75 | 68 |
76 FakeBluetoothAdapterClient::FakeBluetoothAdapterClient() | 69 FakeBluetoothAdapterClient::FakeBluetoothAdapterClient() |
(...skipping 12 matching lines...) Expand all Loading... |
89 | 82 |
90 second_properties_.reset(new Properties(base::Bind( | 83 second_properties_.reset(new Properties(base::Bind( |
91 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this)))); | 84 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this)))); |
92 | 85 |
93 second_properties_->address.ReplaceValue(kSecondAdapterAddress); | 86 second_properties_->address.ReplaceValue(kSecondAdapterAddress); |
94 second_properties_->name.ReplaceValue("Second Fake Adapter (Name)"); | 87 second_properties_->name.ReplaceValue("Second Fake Adapter (Name)"); |
95 second_properties_->alias.ReplaceValue(kSecondAdapterName); | 88 second_properties_->alias.ReplaceValue(kSecondAdapterName); |
96 second_properties_->pairable.ReplaceValue(true); | 89 second_properties_->pairable.ReplaceValue(true); |
97 } | 90 } |
98 | 91 |
99 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() { | 92 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() {} |
100 } | |
101 | 93 |
102 void FakeBluetoothAdapterClient::Init(dbus::Bus* bus) { | 94 void FakeBluetoothAdapterClient::Init(dbus::Bus* bus) {} |
103 } | |
104 | 95 |
105 void FakeBluetoothAdapterClient::AddObserver(Observer* observer) { | 96 void FakeBluetoothAdapterClient::AddObserver(Observer* observer) { |
106 observers_.AddObserver(observer); | 97 observers_.AddObserver(observer); |
107 } | 98 } |
108 | 99 |
109 void FakeBluetoothAdapterClient::RemoveObserver(Observer* observer) { | 100 void FakeBluetoothAdapterClient::RemoveObserver(Observer* observer) { |
110 observers_.RemoveObserver(observer); | 101 observers_.RemoveObserver(observer); |
111 } | 102 } |
112 | 103 |
113 std::vector<dbus::ObjectPath> FakeBluetoothAdapterClient::GetAdapters() { | 104 std::vector<dbus::ObjectPath> FakeBluetoothAdapterClient::GetAdapters() { |
(...skipping 27 matching lines...) Expand all Loading... |
141 ++discovering_count_; | 132 ++discovering_count_; |
142 VLOG(1) << "StartDiscovery: " << object_path.value() << ", " | 133 VLOG(1) << "StartDiscovery: " << object_path.value() << ", " |
143 << "count is now " << discovering_count_; | 134 << "count is now " << discovering_count_; |
144 PostDelayedTask(callback); | 135 PostDelayedTask(callback); |
145 | 136 |
146 if (discovering_count_ == 1) { | 137 if (discovering_count_ == 1) { |
147 properties_->discovering.ReplaceValue(true); | 138 properties_->discovering.ReplaceValue(true); |
148 | 139 |
149 FakeBluetoothDeviceClient* device_client = | 140 FakeBluetoothDeviceClient* device_client = |
150 static_cast<FakeBluetoothDeviceClient*>( | 141 static_cast<FakeBluetoothDeviceClient*>( |
151 DBusThreadManager::Get()->GetBluetoothDeviceClient()); | 142 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); |
152 device_client->BeginDiscoverySimulation(dbus::ObjectPath(kAdapterPath)); | 143 device_client->BeginDiscoverySimulation(dbus::ObjectPath(kAdapterPath)); |
153 } | 144 } |
154 } | 145 } |
155 | 146 |
156 void FakeBluetoothAdapterClient::StopDiscovery( | 147 void FakeBluetoothAdapterClient::StopDiscovery( |
157 const dbus::ObjectPath& object_path, | 148 const dbus::ObjectPath& object_path, |
158 const base::Closure& callback, | 149 const base::Closure& callback, |
159 const ErrorCallback& error_callback) { | 150 const ErrorCallback& error_callback) { |
160 if (object_path != dbus::ObjectPath(kAdapterPath)) { | 151 if (object_path != dbus::ObjectPath(kAdapterPath)) { |
161 PostDelayedTask(base::Bind(error_callback, kNoResponseError, "")); | 152 PostDelayedTask(base::Bind(error_callback, kNoResponseError, "")); |
162 return; | 153 return; |
163 } | 154 } |
164 | 155 |
165 if (!discovering_count_) { | 156 if (!discovering_count_) { |
166 LOG(WARNING) << "StopDiscovery called when not discovering"; | 157 LOG(WARNING) << "StopDiscovery called when not discovering"; |
167 PostDelayedTask(base::Bind(error_callback, kNoResponseError, "")); | 158 PostDelayedTask(base::Bind(error_callback, kNoResponseError, "")); |
168 return; | 159 return; |
169 } | 160 } |
170 | 161 |
171 --discovering_count_; | 162 --discovering_count_; |
172 VLOG(1) << "StopDiscovery: " << object_path.value() << ", " | 163 VLOG(1) << "StopDiscovery: " << object_path.value() << ", " |
173 << "count is now " << discovering_count_; | 164 << "count is now " << discovering_count_; |
174 PostDelayedTask(callback); | 165 PostDelayedTask(callback); |
175 | 166 |
176 if (discovering_count_ == 0) { | 167 if (discovering_count_ == 0) { |
177 FakeBluetoothDeviceClient* device_client = | 168 FakeBluetoothDeviceClient* device_client = |
178 static_cast<FakeBluetoothDeviceClient*>( | 169 static_cast<FakeBluetoothDeviceClient*>( |
179 DBusThreadManager::Get()->GetBluetoothDeviceClient()); | 170 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); |
180 device_client->EndDiscoverySimulation(dbus::ObjectPath(kAdapterPath)); | 171 device_client->EndDiscoverySimulation(dbus::ObjectPath(kAdapterPath)); |
181 | 172 |
182 if (simulation_interval_ms_ > 100) { | 173 if (simulation_interval_ms_ > 100) { |
183 device_client->BeginIncomingPairingSimulation( | 174 device_client->BeginIncomingPairingSimulation( |
184 dbus::ObjectPath(kAdapterPath)); | 175 dbus::ObjectPath(kAdapterPath)); |
185 } | 176 } |
186 | 177 |
187 discovery_filter_.reset(); | 178 discovery_filter_.reset(); |
188 properties_->discovering.ReplaceValue(false); | 179 properties_->discovering.ReplaceValue(false); |
189 } | 180 } |
190 } | 181 } |
191 | 182 |
192 void FakeBluetoothAdapterClient::RemoveDevice( | 183 void FakeBluetoothAdapterClient::RemoveDevice( |
193 const dbus::ObjectPath& object_path, | 184 const dbus::ObjectPath& object_path, |
194 const dbus::ObjectPath& device_path, | 185 const dbus::ObjectPath& device_path, |
195 const base::Closure& callback, | 186 const base::Closure& callback, |
196 const ErrorCallback& error_callback) { | 187 const ErrorCallback& error_callback) { |
197 if (object_path != dbus::ObjectPath(kAdapterPath)) { | 188 if (object_path != dbus::ObjectPath(kAdapterPath)) { |
198 error_callback.Run(kNoResponseError, ""); | 189 error_callback.Run(kNoResponseError, ""); |
199 return; | 190 return; |
200 } | 191 } |
201 | 192 |
202 VLOG(1) << "RemoveDevice: " << object_path.value() | 193 VLOG(1) << "RemoveDevice: " << object_path.value() << " " |
203 << " " << device_path.value(); | 194 << device_path.value(); |
204 callback.Run(); | 195 callback.Run(); |
205 | 196 |
206 FakeBluetoothDeviceClient* device_client = | 197 FakeBluetoothDeviceClient* device_client = |
207 static_cast<FakeBluetoothDeviceClient*>( | 198 static_cast<FakeBluetoothDeviceClient*>( |
208 DBusThreadManager::Get()->GetBluetoothDeviceClient()); | 199 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); |
209 device_client->RemoveDevice(dbus::ObjectPath(kAdapterPath), device_path); | 200 device_client->RemoveDevice(dbus::ObjectPath(kAdapterPath), device_path); |
210 } | 201 } |
211 | 202 |
212 void FakeBluetoothAdapterClient::MakeSetDiscoveryFilterFail() { | 203 void FakeBluetoothAdapterClient::MakeSetDiscoveryFilterFail() { |
213 set_discovery_filter_should_fail_ = true; | 204 set_discovery_filter_should_fail_ = true; |
214 } | 205 } |
215 | 206 |
216 void FakeBluetoothAdapterClient::SetDiscoveryFilter( | 207 void FakeBluetoothAdapterClient::SetDiscoveryFilter( |
217 const dbus::ObjectPath& object_path, | 208 const dbus::ObjectPath& object_path, |
218 const DiscoveryFilter& discovery_filter, | 209 const DiscoveryFilter& discovery_filter, |
(...skipping 18 matching lines...) Expand all Loading... |
237 | 228 |
238 void FakeBluetoothAdapterClient::SetSimulationIntervalMs(int interval_ms) { | 229 void FakeBluetoothAdapterClient::SetSimulationIntervalMs(int interval_ms) { |
239 simulation_interval_ms_ = interval_ms; | 230 simulation_interval_ms_ = interval_ms; |
240 } | 231 } |
241 | 232 |
242 BluetoothAdapterClient::DiscoveryFilter* | 233 BluetoothAdapterClient::DiscoveryFilter* |
243 FakeBluetoothAdapterClient::GetDiscoveryFilter() { | 234 FakeBluetoothAdapterClient::GetDiscoveryFilter() { |
244 return discovery_filter_.get(); | 235 return discovery_filter_.get(); |
245 } | 236 } |
246 | 237 |
247 void FakeBluetoothAdapterClient::SetVisible( | 238 void FakeBluetoothAdapterClient::SetVisible(bool visible) { |
248 bool visible) { | |
249 if (visible && !visible_) { | 239 if (visible && !visible_) { |
250 // Adapter becoming visible | 240 // Adapter becoming visible |
251 visible_ = visible; | 241 visible_ = visible; |
252 | 242 |
253 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | 243 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, |
254 AdapterAdded(dbus::ObjectPath(kAdapterPath))); | 244 AdapterAdded(dbus::ObjectPath(kAdapterPath))); |
255 | 245 |
256 } else if (visible_ && !visible) { | 246 } else if (visible_ && !visible) { |
257 // Adapter becoming invisible | 247 // Adapter becoming invisible |
258 visible_ = visible; | 248 visible_ = visible; |
259 | 249 |
260 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | 250 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, |
261 AdapterRemoved(dbus::ObjectPath(kAdapterPath))); | 251 AdapterRemoved(dbus::ObjectPath(kAdapterPath))); |
262 } | 252 } |
263 } | 253 } |
264 | 254 |
265 void FakeBluetoothAdapterClient::SetSecondVisible( | 255 void FakeBluetoothAdapterClient::SetSecondVisible(bool visible) { |
266 bool visible) { | |
267 if (visible && !second_visible_) { | 256 if (visible && !second_visible_) { |
268 // Second adapter becoming visible | 257 // Second adapter becoming visible |
269 second_visible_ = visible; | 258 second_visible_ = visible; |
270 | 259 |
271 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | 260 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, |
272 AdapterAdded(dbus::ObjectPath(kSecondAdapterPath))); | 261 AdapterAdded(dbus::ObjectPath(kSecondAdapterPath))); |
273 | 262 |
274 } else if (second_visible_ && !visible) { | 263 } else if (second_visible_ && !visible) { |
275 // Second adapter becoming invisible | 264 // Second adapter becoming invisible |
276 second_visible_ = visible; | 265 second_visible_ = visible; |
277 | 266 |
278 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | 267 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, |
279 AdapterRemoved(dbus::ObjectPath(kSecondAdapterPath))); | 268 AdapterRemoved(dbus::ObjectPath(kSecondAdapterPath))); |
280 } | 269 } |
281 } | 270 } |
282 | 271 |
283 void FakeBluetoothAdapterClient::OnPropertyChanged( | 272 void FakeBluetoothAdapterClient::OnPropertyChanged( |
284 const std::string& property_name) { | 273 const std::string& property_name) { |
285 if (property_name == properties_->powered.name() && | 274 if (property_name == properties_->powered.name() && |
286 !properties_->powered.value()) { | 275 !properties_->powered.value()) { |
287 VLOG(1) << "Adapter powered off"; | 276 VLOG(1) << "Adapter powered off"; |
288 | 277 |
289 if (discovering_count_) { | 278 if (discovering_count_) { |
290 discovering_count_ = 0; | 279 discovering_count_ = 0; |
291 properties_->discovering.ReplaceValue(false); | 280 properties_->discovering.ReplaceValue(false); |
292 } | 281 } |
293 } | 282 } |
294 | 283 |
295 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_, | 284 FOR_EACH_OBSERVER( |
296 AdapterPropertyChanged(dbus::ObjectPath(kAdapterPath), | 285 BluetoothAdapterClient::Observer, observers_, |
297 property_name)); | 286 AdapterPropertyChanged(dbus::ObjectPath(kAdapterPath), property_name)); |
298 } | 287 } |
299 | 288 |
300 void FakeBluetoothAdapterClient::PostDelayedTask( | 289 void FakeBluetoothAdapterClient::PostDelayedTask( |
301 const base::Closure& callback) { | 290 const base::Closure& callback) { |
302 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 291 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
303 FROM_HERE, callback, | 292 FROM_HERE, callback, |
304 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); | 293 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
305 } | 294 } |
306 | 295 |
307 } // namespace chromeos | 296 } // namespace bluez |
OLD | NEW |