OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <utility> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/bind.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/message_loop.h" |
| 16 #include "base/stl_util.h" |
| 17 #include "base/time.h" |
| 18 #include "chromeos/dbus/dbus_thread_manager.h" |
| 19 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
| 20 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" |
| 21 #include "chromeos/dbus/fake_bluetooth_agent_service_provider.h" |
| 22 #include "dbus/object_path.h" |
| 23 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Default interval between simulated events. |
| 28 const int kSimulationIntervalMs = 750; |
| 29 |
| 30 } |
| 31 |
| 32 namespace chromeos { |
| 33 |
| 34 const dbus::ObjectPath FakeBluetoothDeviceClient::kPairedDevicePath( |
| 35 "/fake/hci0/dev0"); |
| 36 const char FakeBluetoothDeviceClient::kPairedDeviceAddress[] = |
| 37 "00:11:22:33:44:55"; |
| 38 const char FakeBluetoothDeviceClient::kPairedDeviceName[] = |
| 39 "Fake Device"; |
| 40 const uint32 FakeBluetoothDeviceClient::kPairedDeviceClass = 0x000104; |
| 41 |
| 42 const dbus::ObjectPath FakeBluetoothDeviceClient::kAppleMousePath( |
| 43 "/fake/hci0/dev1"); |
| 44 const char FakeBluetoothDeviceClient::kAppleMouseAddress[] = |
| 45 "28:CF:DA:00:00:00"; |
| 46 const char FakeBluetoothDeviceClient::kAppleMouseName[] = |
| 47 "Apple Magic Mouse"; |
| 48 const uint32 FakeBluetoothDeviceClient::kAppleMouseClass = 0x002580; |
| 49 |
| 50 const dbus::ObjectPath FakeBluetoothDeviceClient::kAppleKeyboardPath( |
| 51 "/fake/hci0/dev2"); |
| 52 const char FakeBluetoothDeviceClient::kAppleKeyboardAddress[] = |
| 53 "28:37:37:00:00:00"; |
| 54 const char FakeBluetoothDeviceClient::kAppleKeyboardName[] = |
| 55 "Apple Wireless Keyboard"; |
| 56 const uint32 FakeBluetoothDeviceClient::kAppleKeyboardClass = 0x002540; |
| 57 |
| 58 const dbus::ObjectPath FakeBluetoothDeviceClient::kVanishingDevicePath( |
| 59 "/fake/hci0/dev3"); |
| 60 const char FakeBluetoothDeviceClient::kVanishingDeviceAddress[] = |
| 61 "01:02:03:04:05:06"; |
| 62 const char FakeBluetoothDeviceClient::kVanishingDeviceName[] = |
| 63 "Vanishing Device"; |
| 64 const uint32 FakeBluetoothDeviceClient::kVanishingDeviceClass = 0x000104; |
| 65 |
| 66 const dbus::ObjectPath FakeBluetoothDeviceClient::kMicrosoftMousePath( |
| 67 "/fake/hci0/dev4"); |
| 68 const char FakeBluetoothDeviceClient::kMicrosoftMouseAddress[] = |
| 69 "7C:ED:8D:00:00:00"; |
| 70 const char FakeBluetoothDeviceClient::kMicrosoftMouseName[] = |
| 71 "Microsoft Mouse"; |
| 72 const uint32 FakeBluetoothDeviceClient::kMicrosoftMouseClass = 0x002540; |
| 73 |
| 74 const dbus::ObjectPath FakeBluetoothDeviceClient::kMotorolaKeyboardPath( |
| 75 "/fake/hci0/dev5"); |
| 76 const char FakeBluetoothDeviceClient::kMotorolaKeyboardAddress[] = |
| 77 "00:0F:F6:00:00:00"; |
| 78 const char FakeBluetoothDeviceClient::kMotorolaKeyboardName[] = |
| 79 "Motorola Keyboard"; |
| 80 const uint32 FakeBluetoothDeviceClient::kMotorolaKeyboardClass = 0x002580; |
| 81 |
| 82 const dbus::ObjectPath FakeBluetoothDeviceClient::kSonyHeadphonesPath( |
| 83 "/fake/hci0/dev6"); |
| 84 const char FakeBluetoothDeviceClient::kSonyHeadphonesAddress[] = |
| 85 "00:24:BE:00:00:00"; |
| 86 const char FakeBluetoothDeviceClient::kSonyHeadphonesName[] = |
| 87 "Sony BT-00"; |
| 88 const uint32 FakeBluetoothDeviceClient::kSonyHeadphonesClass = 0x240408; |
| 89 |
| 90 const dbus::ObjectPath FakeBluetoothDeviceClient::kPhonePath( |
| 91 "/fake/hci0/dev7"); |
| 92 const char FakeBluetoothDeviceClient::kPhoneAddress[] = |
| 93 "20:7D:74:00:00:00"; |
| 94 const char FakeBluetoothDeviceClient::kPhoneName[] = |
| 95 "Phone"; |
| 96 const uint32 FakeBluetoothDeviceClient::kPhoneClass = 0x7a020c; |
| 97 |
| 98 const dbus::ObjectPath FakeBluetoothDeviceClient::kWeirdDevicePath( |
| 99 "/fake/hci0/dev8"); |
| 100 const char FakeBluetoothDeviceClient::kWeirdDeviceAddress[] = |
| 101 "20:7D:74:00:00:01"; |
| 102 const char FakeBluetoothDeviceClient::kWeirdDeviceName[] = |
| 103 "Weird Device"; |
| 104 const uint32 FakeBluetoothDeviceClient::kWeirdDeviceClass = 0x7a020c; |
| 105 |
| 106 FakeBluetoothDeviceClient::Properties::Properties( |
| 107 const PropertyChangedCallback& callback) |
| 108 : ExperimentalBluetoothDeviceClient::Properties( |
| 109 NULL, |
| 110 bluetooth_device::kExperimentalBluetoothDeviceInterface, |
| 111 callback) { |
| 112 } |
| 113 |
| 114 FakeBluetoothDeviceClient::Properties::~Properties() { |
| 115 } |
| 116 |
| 117 void FakeBluetoothDeviceClient::Properties::Get( |
| 118 dbus::PropertyBase* property, |
| 119 dbus::PropertySet::GetCallback callback) { |
| 120 VLOG(1) << "Get " << property->name(); |
| 121 callback.Run(false); |
| 122 } |
| 123 |
| 124 void FakeBluetoothDeviceClient::Properties::GetAll() { |
| 125 VLOG(1) << "GetAll"; |
| 126 } |
| 127 |
| 128 void FakeBluetoothDeviceClient::Properties::Set( |
| 129 dbus::PropertyBase *property, |
| 130 dbus::PropertySet::SetCallback callback) { |
| 131 VLOG(1) << "Set " << property->name(); |
| 132 if (property->name() == trusted.name()) { |
| 133 callback.Run(true); |
| 134 property->ReplaceValueWithSetValue(); |
| 135 NotifyPropertyChanged(property->name()); |
| 136 } else { |
| 137 callback.Run(false); |
| 138 } |
| 139 } |
| 140 |
| 141 |
| 142 FakeBluetoothDeviceClient::FakeBluetoothDeviceClient() |
| 143 : simulation_interval_ms_(kSimulationIntervalMs), |
| 144 discovery_simulation_step_(0), |
| 145 pairing_cancelled_(false) { |
| 146 Properties* properties = new Properties(base::Bind( |
| 147 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 148 base::Unretained(this), |
| 149 kPairedDevicePath)); |
| 150 properties->address.ReplaceValue(kPairedDeviceAddress); |
| 151 properties->bluetooth_class.ReplaceValue(kPairedDeviceClass); |
| 152 properties->name.ReplaceValue("Fake Device (Name)"); |
| 153 properties->alias.ReplaceValue(kPairedDeviceName); |
| 154 properties->paired.ReplaceValue(true); |
| 155 properties->trusted.ReplaceValue(true); |
| 156 properties->adapter.ReplaceValue(FakeBluetoothAdapterClient::kAdapterPath); |
| 157 |
| 158 std::vector<std::string> uuids; |
| 159 uuids.push_back("00001800-0000-1000-8000-00805f9b34fb"); |
| 160 uuids.push_back("00001801-0000-1000-8000-00805f9b34fb"); |
| 161 properties->uuids.ReplaceValue(uuids); |
| 162 |
| 163 properties_map_[kPairedDevicePath] = properties; |
| 164 device_list_.push_back(kPairedDevicePath); |
| 165 } |
| 166 |
| 167 FakeBluetoothDeviceClient::~FakeBluetoothDeviceClient() { |
| 168 // Clean up Properties structures |
| 169 STLDeleteValues(&properties_map_); |
| 170 } |
| 171 |
| 172 void FakeBluetoothDeviceClient::AddObserver(Observer* observer) { |
| 173 observers_.AddObserver(observer); |
| 174 } |
| 175 |
| 176 void FakeBluetoothDeviceClient::RemoveObserver(Observer* observer) { |
| 177 observers_.RemoveObserver(observer); |
| 178 } |
| 179 |
| 180 std::vector<dbus::ObjectPath> FakeBluetoothDeviceClient::GetDevicesForAdapter( |
| 181 const dbus::ObjectPath& adapter_path) { |
| 182 if (adapter_path == FakeBluetoothAdapterClient::kAdapterPath) |
| 183 return device_list_; |
| 184 else |
| 185 return std::vector<dbus::ObjectPath>(); |
| 186 } |
| 187 |
| 188 FakeBluetoothDeviceClient::Properties* |
| 189 FakeBluetoothDeviceClient::GetProperties(const dbus::ObjectPath& object_path) { |
| 190 PropertiesMap::iterator iter = properties_map_.find(object_path); |
| 191 if (iter != properties_map_.end()) |
| 192 return iter->second; |
| 193 return NULL; |
| 194 } |
| 195 |
| 196 void FakeBluetoothDeviceClient::Connect( |
| 197 const dbus::ObjectPath& object_path, |
| 198 const base::Closure& callback, |
| 199 const ErrorCallback& error_callback) { |
| 200 VLOG(1) << "Connect: " << object_path.value(); |
| 201 Properties* properties = GetProperties(object_path); |
| 202 |
| 203 if (properties->connected.value() == true) { |
| 204 // Already connected. |
| 205 callback.Run(); |
| 206 return; |
| 207 } |
| 208 |
| 209 if (properties->paired.value() != true && |
| 210 object_path != kMicrosoftMousePath) { |
| 211 // Must be paired. |
| 212 error_callback.Run(bluetooth_adapter::kErrorFailed, "Not paired"); |
| 213 return; |
| 214 } else if (properties->paired.value() == true && |
| 215 object_path == kMicrosoftMousePath) { |
| 216 // Must not be paired |
| 217 error_callback.Run(bluetooth_adapter::kErrorFailed, |
| 218 "Connection fails while paired"); |
| 219 return; |
| 220 } |
| 221 |
| 222 // The device can be connected. |
| 223 properties->connected.ReplaceValue(true); |
| 224 |
| 225 callback.Run(); |
| 226 properties->NotifyPropertyChanged(properties->connected.name()); |
| 227 } |
| 228 |
| 229 void FakeBluetoothDeviceClient::Disconnect( |
| 230 const dbus::ObjectPath& object_path, |
| 231 const base::Closure& callback, |
| 232 const ErrorCallback& error_callback) { |
| 233 VLOG(1) << "Disconnect: " << object_path.value(); |
| 234 Properties* properties = GetProperties(object_path); |
| 235 |
| 236 if (properties->connected.value() == true) { |
| 237 properties->connected.ReplaceValue(false); |
| 238 |
| 239 callback.Run(); |
| 240 properties->NotifyPropertyChanged(properties->connected.name()); |
| 241 } else { |
| 242 error_callback.Run("org.bluez.Error.NotConnected", "Not Connected"); |
| 243 } |
| 244 } |
| 245 |
| 246 void FakeBluetoothDeviceClient::ConnectProfile( |
| 247 const dbus::ObjectPath& object_path, |
| 248 const std::string& uuid, |
| 249 const base::Closure& callback, |
| 250 const ErrorCallback& error_callback) { |
| 251 VLOG(1) << "ConnectProfile: " << object_path.value() << " " << uuid; |
| 252 error_callback.Run(kNoResponseError, ""); |
| 253 } |
| 254 |
| 255 void FakeBluetoothDeviceClient::DisconnectProfile( |
| 256 const dbus::ObjectPath& object_path, |
| 257 const std::string& uuid, |
| 258 const base::Closure& callback, |
| 259 const ErrorCallback& error_callback) { |
| 260 VLOG(1) << "DisconnectProfile: " << object_path.value() << " " << uuid; |
| 261 error_callback.Run(kNoResponseError, ""); |
| 262 } |
| 263 |
| 264 void FakeBluetoothDeviceClient::Pair( |
| 265 const dbus::ObjectPath& object_path, |
| 266 const base::Closure& callback, |
| 267 const ErrorCallback& error_callback) { |
| 268 VLOG(1) << "Pair: " << object_path.value(); |
| 269 Properties* properties = GetProperties(object_path); |
| 270 |
| 271 if (properties->paired.value() == true) { |
| 272 // Already paired. |
| 273 callback.Run(); |
| 274 return; |
| 275 } |
| 276 |
| 277 FakeBluetoothAgentManagerClient* fake_bluetooth_agent_manager_client = |
| 278 static_cast<FakeBluetoothAgentManagerClient*>( |
| 279 DBusThreadManager::Get()-> |
| 280 GetExperimentalBluetoothAgentManagerClient()); |
| 281 FakeBluetoothAgentServiceProvider* agent_service_provider = |
| 282 fake_bluetooth_agent_manager_client->GetAgentServiceProvider(); |
| 283 if (agent_service_provider == NULL) { |
| 284 error_callback.Run(kNoResponseError, "Missing agent"); |
| 285 return; |
| 286 } |
| 287 |
| 288 if (object_path == kAppleMousePath || object_path == kMicrosoftMousePath) { |
| 289 // No need to call anything on the pairing delegate, just wait 3 times |
| 290 // the interval before acting as if the other end accepted it. |
| 291 MessageLoop::current()->PostDelayedTask( |
| 292 FROM_HERE, |
| 293 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing, |
| 294 base::Unretained(this), |
| 295 object_path, callback, error_callback), |
| 296 base::TimeDelta::FromMilliseconds(3 * simulation_interval_ms_)); |
| 297 |
| 298 } else if (object_path == kAppleKeyboardPath) { |
| 299 // Display a Pincode, and wait 7 times the interval before acting as |
| 300 // if the other end accepted it. |
| 301 agent_service_provider->DisplayPinCode(object_path, "123456"); |
| 302 |
| 303 MessageLoop::current()->PostDelayedTask( |
| 304 FROM_HERE, |
| 305 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing, |
| 306 base::Unretained(this), |
| 307 object_path, callback, error_callback), |
| 308 base::TimeDelta::FromMilliseconds(7 * simulation_interval_ms_)); |
| 309 |
| 310 } else if (object_path == kVanishingDevicePath) { |
| 311 // The vanishing device simulates being too far away, and thus times out. |
| 312 MessageLoop::current()->PostDelayedTask( |
| 313 FROM_HERE, |
| 314 base::Bind(&FakeBluetoothDeviceClient::TimeoutSimulatedPairing, |
| 315 base::Unretained(this), |
| 316 object_path, error_callback), |
| 317 base::TimeDelta::FromMilliseconds(4 * simulation_interval_ms_)); |
| 318 |
| 319 } else if (object_path == kMotorolaKeyboardPath) { |
| 320 // Display a passkey, and each interval act as if another key was entered |
| 321 // for it. |
| 322 agent_service_provider->DisplayPasskey(object_path, 123456, 0); |
| 323 |
| 324 MessageLoop::current()->PostDelayedTask( |
| 325 FROM_HERE, |
| 326 base::Bind(&FakeBluetoothDeviceClient::SimulateKeypress, |
| 327 base::Unretained(this), |
| 328 1, object_path, callback, error_callback), |
| 329 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 330 |
| 331 } else if (object_path == kSonyHeadphonesPath) { |
| 332 // Request a Pincode. |
| 333 agent_service_provider->RequestPinCode( |
| 334 object_path, |
| 335 base::Bind(&FakeBluetoothDeviceClient::PinCodeCallback, |
| 336 base::Unretained(this), |
| 337 object_path, |
| 338 callback, |
| 339 error_callback)); |
| 340 |
| 341 } else if (object_path == kPhonePath) { |
| 342 // Request confirmation of a Passkey. |
| 343 agent_service_provider->RequestConfirmation( |
| 344 object_path, 123456, |
| 345 base::Bind(&FakeBluetoothDeviceClient::ConfirmationCallback, |
| 346 base::Unretained(this), |
| 347 object_path, |
| 348 callback, |
| 349 error_callback)); |
| 350 |
| 351 } else if (object_path == kWeirdDevicePath) { |
| 352 // Request a Passkey from the user. |
| 353 agent_service_provider->RequestPasskey( |
| 354 object_path, |
| 355 base::Bind(&FakeBluetoothDeviceClient::PasskeyCallback, |
| 356 base::Unretained(this), |
| 357 object_path, |
| 358 callback, |
| 359 error_callback)); |
| 360 |
| 361 } else { |
| 362 error_callback.Run(kNoResponseError, "No pairing fake"); |
| 363 } |
| 364 } |
| 365 |
| 366 void FakeBluetoothDeviceClient::CancelPairing( |
| 367 const dbus::ObjectPath& object_path, |
| 368 const base::Closure& callback, |
| 369 const ErrorCallback& error_callback) { |
| 370 VLOG(1) << "CancelPairing: " << object_path.value(); |
| 371 pairing_cancelled_ = true; |
| 372 callback.Run(); |
| 373 } |
| 374 |
| 375 |
| 376 void FakeBluetoothDeviceClient::BeginDiscoverySimulation( |
| 377 const dbus::ObjectPath& adapter_path) { |
| 378 VLOG(1) << "starting discovery simulation"; |
| 379 |
| 380 discovery_simulation_step_ = 1; |
| 381 |
| 382 MessageLoop::current()->PostDelayedTask( |
| 383 FROM_HERE, |
| 384 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, |
| 385 base::Unretained(this)), |
| 386 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 387 } |
| 388 |
| 389 void FakeBluetoothDeviceClient::EndDiscoverySimulation( |
| 390 const dbus::ObjectPath& adapter_path) { |
| 391 VLOG(1) << "stopping discovery simulation"; |
| 392 discovery_simulation_step_ = 0; |
| 393 } |
| 394 |
| 395 void FakeBluetoothDeviceClient::SetSimulationIntervalMs(int interval_ms) { |
| 396 simulation_interval_ms_ = interval_ms; |
| 397 } |
| 398 |
| 399 void FakeBluetoothDeviceClient::RemoveDevice( |
| 400 const dbus::ObjectPath& adapter_path, |
| 401 const dbus::ObjectPath& device_path) { |
| 402 std::vector<dbus::ObjectPath>::iterator listiter = |
| 403 std::find(device_list_.begin(), device_list_.end(), device_path); |
| 404 if (listiter == device_list_.end()) |
| 405 return; |
| 406 |
| 407 PropertiesMap::iterator iter = properties_map_.find(device_path); |
| 408 Properties* properties = iter->second; |
| 409 |
| 410 VLOG(1) << "removing device: " << properties->alias.value(); |
| 411 device_list_.erase(listiter); |
| 412 |
| 413 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 414 DeviceRemoved(device_path)); |
| 415 |
| 416 delete properties; |
| 417 properties_map_.erase(iter); |
| 418 } |
| 419 |
| 420 void FakeBluetoothDeviceClient::OnPropertyChanged( |
| 421 const dbus::ObjectPath& object_path, |
| 422 const std::string& property_name) { |
| 423 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 424 DevicePropertyChanged(object_path, property_name)); |
| 425 } |
| 426 |
| 427 void FakeBluetoothDeviceClient::DiscoverySimulationTimer() { |
| 428 if (!discovery_simulation_step_) |
| 429 return; |
| 430 |
| 431 // Timer fires every .75s, the numbers below are arbitrary to give a feel |
| 432 // for a discovery process. |
| 433 VLOG(1) << "discovery simulation, step " << discovery_simulation_step_; |
| 434 if (discovery_simulation_step_ == 2) { |
| 435 if (std::find(device_list_.begin(), device_list_.end(), |
| 436 kAppleMousePath) == device_list_.end()) { |
| 437 Properties* properties = new Properties(base::Bind( |
| 438 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 439 base::Unretained(this), |
| 440 kAppleMousePath)); |
| 441 properties->address.ReplaceValue(kAppleMouseAddress); |
| 442 properties->bluetooth_class.ReplaceValue(kAppleMouseClass); |
| 443 properties->name.ReplaceValue("Fake Apple Magic Mouse"); |
| 444 properties->alias.ReplaceValue(kAppleMouseName); |
| 445 properties->adapter.ReplaceValue( |
| 446 FakeBluetoothAdapterClient::kAdapterPath); |
| 447 |
| 448 properties_map_[kAppleMousePath] = properties; |
| 449 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 450 DeviceAdded(kAppleMousePath)); |
| 451 } |
| 452 |
| 453 } else if (discovery_simulation_step_ == 4) { |
| 454 if (std::find(device_list_.begin(), device_list_.end(), |
| 455 kAppleKeyboardPath) == device_list_.end()) { |
| 456 Properties *properties = new Properties(base::Bind( |
| 457 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 458 base::Unretained(this), |
| 459 kAppleKeyboardPath)); |
| 460 properties->address.ReplaceValue(kAppleKeyboardAddress); |
| 461 properties->bluetooth_class.ReplaceValue(kAppleKeyboardClass); |
| 462 properties->name.ReplaceValue("Fake Apple Wireless Keyboard"); |
| 463 properties->alias.ReplaceValue(kAppleKeyboardName); |
| 464 properties->adapter.ReplaceValue( |
| 465 FakeBluetoothAdapterClient::kAdapterPath); |
| 466 |
| 467 properties_map_[kAppleKeyboardPath] = properties; |
| 468 device_list_.push_back(kAppleKeyboardPath); |
| 469 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 470 DeviceAdded(kAppleKeyboardPath)); |
| 471 } |
| 472 |
| 473 if (std::find(device_list_.begin(), device_list_.end(), |
| 474 kVanishingDevicePath) == device_list_.end()) { |
| 475 Properties* properties = new Properties(base::Bind( |
| 476 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 477 base::Unretained(this), |
| 478 kVanishingDevicePath)); |
| 479 properties->address.ReplaceValue(kVanishingDeviceAddress); |
| 480 properties->bluetooth_class.ReplaceValue(kVanishingDeviceClass); |
| 481 properties->name.ReplaceValue("Fake Vanishing Device"); |
| 482 properties->alias.ReplaceValue(kVanishingDeviceName); |
| 483 properties->adapter.ReplaceValue( |
| 484 FakeBluetoothAdapterClient::kAdapterPath); |
| 485 |
| 486 properties_map_[kVanishingDevicePath] = properties; |
| 487 device_list_.push_back(kVanishingDevicePath); |
| 488 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 489 DeviceAdded(kVanishingDevicePath)); |
| 490 } |
| 491 |
| 492 } else if (discovery_simulation_step_ == 7) { |
| 493 if (std::find(device_list_.begin(), device_list_.end(), |
| 494 kMicrosoftMousePath) == device_list_.end()) { |
| 495 Properties* properties = new Properties(base::Bind( |
| 496 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 497 base::Unretained(this), |
| 498 kMicrosoftMousePath)); |
| 499 properties->address.ReplaceValue(kMicrosoftMouseAddress); |
| 500 properties->bluetooth_class.ReplaceValue(kMicrosoftMouseClass); |
| 501 properties->name.ReplaceValue("Fake Microsoft Mouse"); |
| 502 properties->alias.ReplaceValue(kMicrosoftMouseName); |
| 503 properties->adapter.ReplaceValue( |
| 504 FakeBluetoothAdapterClient::kAdapterPath); |
| 505 |
| 506 properties_map_[kMicrosoftMousePath] = properties; |
| 507 device_list_.push_back(kMicrosoftMousePath); |
| 508 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 509 DeviceAdded(kMicrosoftMousePath)); |
| 510 } |
| 511 |
| 512 } else if (discovery_simulation_step_ == 8) { |
| 513 if (std::find(device_list_.begin(), device_list_.end(), |
| 514 kMotorolaKeyboardPath) == device_list_.end()) { |
| 515 Properties* properties = new Properties(base::Bind( |
| 516 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 517 base::Unretained(this), |
| 518 kMotorolaKeyboardPath)); |
| 519 properties->address.ReplaceValue(kMotorolaKeyboardAddress); |
| 520 properties->bluetooth_class.ReplaceValue(kMotorolaKeyboardClass); |
| 521 properties->name.ReplaceValue("Fake Motorola Keyboard"); |
| 522 properties->alias.ReplaceValue(kMotorolaKeyboardName); |
| 523 properties->adapter.ReplaceValue( |
| 524 FakeBluetoothAdapterClient::kAdapterPath); |
| 525 |
| 526 properties_map_[kMotorolaKeyboardPath] = properties; |
| 527 device_list_.push_back(kMotorolaKeyboardPath); |
| 528 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 529 DeviceAdded(kMotorolaKeyboardPath)); |
| 530 } |
| 531 |
| 532 if (std::find(device_list_.begin(), device_list_.end(), |
| 533 kSonyHeadphonesPath) == device_list_.end()) { |
| 534 Properties* properties = new Properties(base::Bind( |
| 535 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 536 base::Unretained(this), |
| 537 kSonyHeadphonesPath)); |
| 538 properties->address.ReplaceValue(kSonyHeadphonesAddress); |
| 539 properties->bluetooth_class.ReplaceValue(kSonyHeadphonesClass); |
| 540 properties->name.ReplaceValue("Fake Sony Headphones"); |
| 541 properties->alias.ReplaceValue(kSonyHeadphonesName); |
| 542 properties->adapter.ReplaceValue( |
| 543 FakeBluetoothAdapterClient::kAdapterPath); |
| 544 |
| 545 properties_map_[kSonyHeadphonesPath] = properties; |
| 546 device_list_.push_back(kSonyHeadphonesPath); |
| 547 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 548 DeviceAdded(kSonyHeadphonesPath)); |
| 549 } |
| 550 |
| 551 } else if (discovery_simulation_step_ == 10) { |
| 552 if (std::find(device_list_.begin(), device_list_.end(), |
| 553 kPhonePath) == device_list_.end()) { |
| 554 Properties* properties = new Properties(base::Bind( |
| 555 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 556 base::Unretained(this), |
| 557 kPhonePath)); |
| 558 properties->address.ReplaceValue(kPhoneAddress); |
| 559 properties->bluetooth_class.ReplaceValue(kPhoneClass); |
| 560 properties->name.ReplaceValue("Fake Phone"); |
| 561 properties->alias.ReplaceValue(kPhoneName); |
| 562 properties->adapter.ReplaceValue( |
| 563 FakeBluetoothAdapterClient::kAdapterPath); |
| 564 |
| 565 properties_map_[kPhonePath] = properties; |
| 566 device_list_.push_back(kPhonePath); |
| 567 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 568 DeviceAdded(kPhonePath)); |
| 569 } |
| 570 |
| 571 if (std::find(device_list_.begin(), device_list_.end(), |
| 572 kWeirdDevicePath) == device_list_.end()) { |
| 573 Properties* properties = new Properties(base::Bind( |
| 574 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 575 base::Unretained(this), |
| 576 kWeirdDevicePath)); |
| 577 properties->address.ReplaceValue(kWeirdDeviceAddress); |
| 578 properties->bluetooth_class.ReplaceValue(kWeirdDeviceClass); |
| 579 properties->name.ReplaceValue("Fake Weird Device"); |
| 580 properties->alias.ReplaceValue(kWeirdDeviceName); |
| 581 properties->adapter.ReplaceValue( |
| 582 FakeBluetoothAdapterClient::kAdapterPath); |
| 583 |
| 584 properties_map_[kWeirdDevicePath] = properties; |
| 585 device_list_.push_back(kWeirdDevicePath); |
| 586 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 587 DeviceAdded(kWeirdDevicePath)); |
| 588 } |
| 589 |
| 590 } else if (discovery_simulation_step_ == 13) { |
| 591 RemoveDevice(FakeBluetoothAdapterClient::kAdapterPath, |
| 592 kVanishingDevicePath); |
| 593 |
| 594 } else if (discovery_simulation_step_ == 14) { |
| 595 return; |
| 596 |
| 597 } |
| 598 |
| 599 ++discovery_simulation_step_; |
| 600 MessageLoop::current()->PostDelayedTask( |
| 601 FROM_HERE, |
| 602 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, |
| 603 base::Unretained(this)), |
| 604 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 605 } |
| 606 |
| 607 |
| 608 void FakeBluetoothDeviceClient::CompleteSimulatedPairing( |
| 609 const dbus::ObjectPath& object_path, |
| 610 const base::Closure& callback, |
| 611 const ErrorCallback& error_callback) { |
| 612 VLOG(1) << "CompleteSimulatedPairing: " << object_path.value(); |
| 613 if (pairing_cancelled_) { |
| 614 pairing_cancelled_ = false; |
| 615 |
| 616 error_callback.Run(bluetooth_adapter::kErrorAuthenticationCanceled, |
| 617 "Cancaled"); |
| 618 } else { |
| 619 Properties* properties = GetProperties(object_path); |
| 620 |
| 621 properties->paired.ReplaceValue(true); |
| 622 |
| 623 callback.Run(); |
| 624 properties->NotifyPropertyChanged(properties->paired.name()); |
| 625 } |
| 626 } |
| 627 |
| 628 void FakeBluetoothDeviceClient::TimeoutSimulatedPairing( |
| 629 const dbus::ObjectPath& object_path, |
| 630 const ErrorCallback& error_callback) { |
| 631 VLOG(1) << "TimeoutSimulatedPairing: " << object_path.value(); |
| 632 |
| 633 error_callback.Run(bluetooth_adapter::kErrorAuthenticationTimeout, |
| 634 "Timed out"); |
| 635 } |
| 636 |
| 637 void FakeBluetoothDeviceClient::CancelSimulatedPairing( |
| 638 const dbus::ObjectPath& object_path, |
| 639 const ErrorCallback& error_callback) { |
| 640 VLOG(1) << "CancelSimulatedPairing: " << object_path.value(); |
| 641 |
| 642 error_callback.Run(bluetooth_adapter::kErrorAuthenticationCanceled, |
| 643 "Canceled"); |
| 644 } |
| 645 |
| 646 void FakeBluetoothDeviceClient::RejectSimulatedPairing( |
| 647 const dbus::ObjectPath& object_path, |
| 648 const ErrorCallback& error_callback) { |
| 649 VLOG(1) << "RejectSimulatedPairing: " << object_path.value(); |
| 650 |
| 651 error_callback.Run(bluetooth_adapter::kErrorAuthenticationRejected, |
| 652 "Rejected"); |
| 653 } |
| 654 |
| 655 void FakeBluetoothDeviceClient::PinCodeCallback( |
| 656 const dbus::ObjectPath& object_path, |
| 657 const base::Closure& callback, |
| 658 const ErrorCallback& error_callback, |
| 659 ExperimentalBluetoothAgentServiceProvider::Delegate::Status status, |
| 660 const std::string& pincode) { |
| 661 VLOG(1) << "PinCodeCallback: " << object_path.value(); |
| 662 |
| 663 if (status == ExperimentalBluetoothAgentServiceProvider::Delegate::SUCCESS) { |
| 664 MessageLoop::current()->PostDelayedTask( |
| 665 FROM_HERE, |
| 666 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing, |
| 667 base::Unretained(this), |
| 668 object_path, callback, error_callback), |
| 669 base::TimeDelta::FromMilliseconds(3 * simulation_interval_ms_)); |
| 670 |
| 671 } else if (status == |
| 672 ExperimentalBluetoothAgentServiceProvider::Delegate::CANCELLED) { |
| 673 MessageLoop::current()->PostDelayedTask( |
| 674 FROM_HERE, |
| 675 base::Bind(&FakeBluetoothDeviceClient::CancelSimulatedPairing, |
| 676 base::Unretained(this), |
| 677 object_path, error_callback), |
| 678 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 679 |
| 680 } else if (status == |
| 681 ExperimentalBluetoothAgentServiceProvider::Delegate::REJECTED) { |
| 682 MessageLoop::current()->PostDelayedTask( |
| 683 FROM_HERE, |
| 684 base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing, |
| 685 base::Unretained(this), |
| 686 object_path, error_callback), |
| 687 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 688 |
| 689 } |
| 690 } |
| 691 |
| 692 void FakeBluetoothDeviceClient::PasskeyCallback( |
| 693 const dbus::ObjectPath& object_path, |
| 694 const base::Closure& callback, |
| 695 const ErrorCallback& error_callback, |
| 696 ExperimentalBluetoothAgentServiceProvider::Delegate::Status status, |
| 697 uint32 passkey) { |
| 698 VLOG(1) << "PasskeyCallback: " << object_path.value(); |
| 699 |
| 700 if (status == ExperimentalBluetoothAgentServiceProvider::Delegate::SUCCESS) { |
| 701 MessageLoop::current()->PostDelayedTask( |
| 702 FROM_HERE, |
| 703 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing, |
| 704 base::Unretained(this), |
| 705 object_path, callback, error_callback), |
| 706 base::TimeDelta::FromMilliseconds(3 * simulation_interval_ms_)); |
| 707 |
| 708 } else if (status == |
| 709 ExperimentalBluetoothAgentServiceProvider::Delegate::CANCELLED) { |
| 710 MessageLoop::current()->PostDelayedTask( |
| 711 FROM_HERE, |
| 712 base::Bind(&FakeBluetoothDeviceClient::CancelSimulatedPairing, |
| 713 base::Unretained(this), |
| 714 object_path, error_callback), |
| 715 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 716 |
| 717 } else if (status == |
| 718 ExperimentalBluetoothAgentServiceProvider::Delegate::REJECTED) { |
| 719 MessageLoop::current()->PostDelayedTask( |
| 720 FROM_HERE, |
| 721 base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing, |
| 722 base::Unretained(this), |
| 723 object_path, error_callback), |
| 724 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 725 |
| 726 } |
| 727 } |
| 728 |
| 729 void FakeBluetoothDeviceClient::ConfirmationCallback( |
| 730 const dbus::ObjectPath& object_path, |
| 731 const base::Closure& callback, |
| 732 const ErrorCallback& error_callback, |
| 733 ExperimentalBluetoothAgentServiceProvider::Delegate::Status status) { |
| 734 VLOG(1) << "ConfirmationCallback: " << object_path.value(); |
| 735 |
| 736 if (status == ExperimentalBluetoothAgentServiceProvider::Delegate::SUCCESS) { |
| 737 MessageLoop::current()->PostDelayedTask( |
| 738 FROM_HERE, |
| 739 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing, |
| 740 base::Unretained(this), |
| 741 object_path, callback, error_callback), |
| 742 base::TimeDelta::FromMilliseconds(3 * simulation_interval_ms_)); |
| 743 |
| 744 } else if (status == |
| 745 ExperimentalBluetoothAgentServiceProvider::Delegate::CANCELLED) { |
| 746 MessageLoop::current()->PostDelayedTask( |
| 747 FROM_HERE, |
| 748 base::Bind(&FakeBluetoothDeviceClient::CancelSimulatedPairing, |
| 749 base::Unretained(this), |
| 750 object_path, error_callback), |
| 751 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 752 |
| 753 } else if (status == |
| 754 ExperimentalBluetoothAgentServiceProvider::Delegate::REJECTED) { |
| 755 MessageLoop::current()->PostDelayedTask( |
| 756 FROM_HERE, |
| 757 base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing, |
| 758 base::Unretained(this), |
| 759 object_path, error_callback), |
| 760 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 761 |
| 762 } |
| 763 } |
| 764 |
| 765 void FakeBluetoothDeviceClient::SimulateKeypress( |
| 766 int16 entered, |
| 767 const dbus::ObjectPath& object_path, |
| 768 const base::Closure& callback, |
| 769 const ErrorCallback& error_callback) { |
| 770 VLOG(1) << "SimulateKeypress " << entered << ": " << object_path.value(); |
| 771 |
| 772 FakeBluetoothAgentManagerClient* fake_bluetooth_agent_manager_client = |
| 773 static_cast<FakeBluetoothAgentManagerClient*>( |
| 774 DBusThreadManager::Get()-> |
| 775 GetExperimentalBluetoothAgentManagerClient()); |
| 776 FakeBluetoothAgentServiceProvider* agent_service_provider = |
| 777 fake_bluetooth_agent_manager_client->GetAgentServiceProvider(); |
| 778 agent_service_provider->DisplayPasskey(object_path, 123456, entered); |
| 779 |
| 780 if (entered < 6) { |
| 781 MessageLoop::current()->PostDelayedTask( |
| 782 FROM_HERE, |
| 783 base::Bind(&FakeBluetoothDeviceClient::SimulateKeypress, |
| 784 base::Unretained(this), |
| 785 entered + 1, object_path, callback, error_callback), |
| 786 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 787 |
| 788 } else { |
| 789 MessageLoop::current()->PostDelayedTask( |
| 790 FROM_HERE, |
| 791 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing, |
| 792 base::Unretained(this), |
| 793 object_path, callback, error_callback), |
| 794 base::TimeDelta::FromMilliseconds(simulation_interval_ms_)); |
| 795 |
| 796 } |
| 797 } |
| 798 |
| 799 } // namespace chromeos |
OLD | NEW |