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/fake_bluetooth_adapter_client.h" |
| 19 #include "dbus/object_path.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Default interval between simulated discovery events. |
| 25 const int kDiscoverySimulationIntervalMs = 750; |
| 26 |
| 27 } |
| 28 |
| 29 namespace chromeos { |
| 30 |
| 31 const dbus::ObjectPath FakeBluetoothDeviceClient::kPairedDevicePath( |
| 32 "/fake/hci0/dev0"); |
| 33 const char FakeBluetoothDeviceClient::kPairedDeviceAddress[] = |
| 34 "00:11:22:33:44:55"; |
| 35 const char FakeBluetoothDeviceClient::kPairedDeviceName[] = |
| 36 "Fake Device"; |
| 37 const uint32 FakeBluetoothDeviceClient::kPairedDeviceClass = 0x000104; |
| 38 |
| 39 const dbus::ObjectPath FakeBluetoothDeviceClient::kAppleMousePath( |
| 40 "/fake/hci0/dev1"); |
| 41 const char FakeBluetoothDeviceClient::kAppleMouseAddress[] = |
| 42 "28:CF:DA:00:00:00"; |
| 43 const char FakeBluetoothDeviceClient::kAppleMouseName[] = |
| 44 "Apple Magic Mouse"; |
| 45 const uint32 FakeBluetoothDeviceClient::kAppleMouseClass = 0x002580; |
| 46 |
| 47 const dbus::ObjectPath FakeBluetoothDeviceClient::kAppleKeyboardPath( |
| 48 "/fake/hci0/dev2"); |
| 49 const char FakeBluetoothDeviceClient::kAppleKeyboardAddress[] = |
| 50 "28:37:37:00:00:00"; |
| 51 const char FakeBluetoothDeviceClient::kAppleKeyboardName[] = |
| 52 "Apple Wireless Keyboard"; |
| 53 const uint32 FakeBluetoothDeviceClient::kAppleKeyboardClass = 0x002540; |
| 54 |
| 55 const dbus::ObjectPath FakeBluetoothDeviceClient::kVanishingDevicePath( |
| 56 "/fake/hci0/dev3"); |
| 57 const char FakeBluetoothDeviceClient::kVanishingDeviceAddress[] = |
| 58 "01:02:03:04:05:06"; |
| 59 const char FakeBluetoothDeviceClient::kVanishingDeviceName[] = |
| 60 "Vanishing Device"; |
| 61 const uint32 FakeBluetoothDeviceClient::kVanishingDeviceClass = 0x000104; |
| 62 |
| 63 const dbus::ObjectPath FakeBluetoothDeviceClient::kMicrosoftMousePath( |
| 64 "/fake/hci0/dev4"); |
| 65 const char FakeBluetoothDeviceClient::kMicrosoftMouseAddress[] = |
| 66 "7C:ED:8D:00:00:00"; |
| 67 const char FakeBluetoothDeviceClient::kMicrosoftMouseName[] = |
| 68 "Microsoft Mouse"; |
| 69 const uint32 FakeBluetoothDeviceClient::kMicrosoftMouseClass = 0x002540; |
| 70 |
| 71 const dbus::ObjectPath FakeBluetoothDeviceClient::kMotorolaKeyboardPath( |
| 72 "/fake/hci0/dev5"); |
| 73 const char FakeBluetoothDeviceClient::kMotorolaKeyboardAddress[] = |
| 74 "00:0F:F6:00:00:00"; |
| 75 const char FakeBluetoothDeviceClient::kMotorolaKeyboardName[] = |
| 76 "Motorola Keyboard"; |
| 77 const uint32 FakeBluetoothDeviceClient::kMotorolaKeyboardClass = 0x002580; |
| 78 |
| 79 const dbus::ObjectPath FakeBluetoothDeviceClient::kPhonePath( |
| 80 "/fake/hci0/dev6"); |
| 81 const char FakeBluetoothDeviceClient::kPhoneAddress[] = |
| 82 "20:7D:74:00:00:00"; |
| 83 const char FakeBluetoothDeviceClient::kPhoneName[] = |
| 84 "Phone"; |
| 85 const uint32 FakeBluetoothDeviceClient::kPhoneClass = 0x7a020c; |
| 86 |
| 87 FakeBluetoothDeviceClient::Properties::Properties( |
| 88 const PropertyChangedCallback& callback) |
| 89 : ExperimentalBluetoothDeviceClient::Properties( |
| 90 NULL, |
| 91 bluetooth_device::kExperimentalBluetoothDeviceInterface, |
| 92 callback) { |
| 93 } |
| 94 |
| 95 FakeBluetoothDeviceClient::Properties::~Properties() { |
| 96 } |
| 97 |
| 98 void FakeBluetoothDeviceClient::Properties::Get( |
| 99 dbus::PropertyBase* property, |
| 100 dbus::PropertySet::GetCallback callback) { |
| 101 VLOG(1) << "Get " << property->name(); |
| 102 callback.Run(false); |
| 103 } |
| 104 |
| 105 void FakeBluetoothDeviceClient::Properties::GetAll() { |
| 106 VLOG(1) << "GetAll"; |
| 107 } |
| 108 |
| 109 void FakeBluetoothDeviceClient::Properties::Set( |
| 110 dbus::PropertyBase *property, |
| 111 dbus::PropertySet::SetCallback callback) { |
| 112 VLOG(1) << "Set " << property->name(); |
| 113 if (property->name() == trusted.name()) { |
| 114 callback.Run(true); |
| 115 property->ReplaceValueWithSetValue(); |
| 116 NotifyPropertyChanged(property->name()); |
| 117 } else { |
| 118 callback.Run(false); |
| 119 } |
| 120 } |
| 121 |
| 122 |
| 123 FakeBluetoothDeviceClient::FakeBluetoothDeviceClient() |
| 124 : discovery_simulation_interval_ms_(kDiscoverySimulationIntervalMs), |
| 125 discovery_simulation_step_(0) { |
| 126 Properties* properties = new Properties(base::Bind( |
| 127 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 128 base::Unretained(this), |
| 129 kPairedDevicePath)); |
| 130 properties->address.ReplaceValue(kPairedDeviceAddress); |
| 131 properties->bluetooth_class.ReplaceValue(kPairedDeviceClass); |
| 132 properties->name.ReplaceValue("Fake Device (Name)"); |
| 133 properties->alias.ReplaceValue(kPairedDeviceName); |
| 134 properties->paired.ReplaceValue(true); |
| 135 properties->trusted.ReplaceValue(true); |
| 136 properties->adapter.ReplaceValue(FakeBluetoothAdapterClient::kAdapterPath); |
| 137 |
| 138 std::vector<std::string> uuids; |
| 139 uuids.push_back("00001800-0000-1000-8000-00805f9b34fb"); |
| 140 uuids.push_back("00001801-0000-1000-8000-00805f9b34fb"); |
| 141 properties->uuids.ReplaceValue(uuids); |
| 142 |
| 143 properties_map_[kPairedDevicePath] = properties; |
| 144 device_list_.push_back(kPairedDevicePath); |
| 145 } |
| 146 |
| 147 FakeBluetoothDeviceClient::~FakeBluetoothDeviceClient() { |
| 148 // Clean up Properties structures |
| 149 STLDeleteValues(&properties_map_); |
| 150 } |
| 151 |
| 152 void FakeBluetoothDeviceClient::AddObserver(Observer* observer) { |
| 153 observers_.AddObserver(observer); |
| 154 } |
| 155 |
| 156 void FakeBluetoothDeviceClient::RemoveObserver(Observer* observer) { |
| 157 observers_.RemoveObserver(observer); |
| 158 } |
| 159 |
| 160 std::vector<dbus::ObjectPath> FakeBluetoothDeviceClient::GetDevicesForAdapter( |
| 161 const dbus::ObjectPath& adapter_path) { |
| 162 if (adapter_path == FakeBluetoothAdapterClient::kAdapterPath) |
| 163 return device_list_; |
| 164 else |
| 165 return std::vector<dbus::ObjectPath>(); |
| 166 } |
| 167 |
| 168 FakeBluetoothDeviceClient::Properties* |
| 169 FakeBluetoothDeviceClient::GetProperties(const dbus::ObjectPath& object_path) { |
| 170 PropertiesMap::iterator iter = properties_map_.find(object_path); |
| 171 if (iter != properties_map_.end()) |
| 172 return iter->second; |
| 173 return NULL; |
| 174 } |
| 175 |
| 176 void FakeBluetoothDeviceClient::Connect( |
| 177 const dbus::ObjectPath& object_path, |
| 178 const base::Closure& callback, |
| 179 const ErrorCallback& error_callback) { |
| 180 VLOG(1) << "Connect: " << object_path.value(); |
| 181 error_callback.Run(kNoResponseError, ""); |
| 182 } |
| 183 |
| 184 void FakeBluetoothDeviceClient::Disconnect( |
| 185 const dbus::ObjectPath& object_path, |
| 186 const base::Closure& callback, |
| 187 const ErrorCallback& error_callback) { |
| 188 VLOG(1) << "Disconnect: " << object_path.value(); |
| 189 error_callback.Run(kNoResponseError, ""); |
| 190 } |
| 191 |
| 192 void FakeBluetoothDeviceClient::ConnectProfile( |
| 193 const dbus::ObjectPath& object_path, |
| 194 const std::string& uuid, |
| 195 const base::Closure& callback, |
| 196 const ErrorCallback& error_callback) { |
| 197 VLOG(1) << "ConnectProfile: " << object_path.value() << " " << uuid; |
| 198 error_callback.Run(kNoResponseError, ""); |
| 199 } |
| 200 |
| 201 void FakeBluetoothDeviceClient::DisconnectProfile( |
| 202 const dbus::ObjectPath& object_path, |
| 203 const std::string& uuid, |
| 204 const base::Closure& callback, |
| 205 const ErrorCallback& error_callback) { |
| 206 VLOG(1) << "DisconnectProfile: " << object_path.value() << " " << uuid; |
| 207 error_callback.Run(kNoResponseError, ""); |
| 208 } |
| 209 |
| 210 void FakeBluetoothDeviceClient::Pair( |
| 211 const dbus::ObjectPath& object_path, |
| 212 const base::Closure& callback, |
| 213 const ErrorCallback& error_callback) { |
| 214 VLOG(1) << "Pair: " << object_path.value(); |
| 215 error_callback.Run(kNoResponseError, ""); |
| 216 } |
| 217 |
| 218 void FakeBluetoothDeviceClient::CancelPairing( |
| 219 const dbus::ObjectPath& object_path, |
| 220 const base::Closure& callback, |
| 221 const ErrorCallback& error_callback) { |
| 222 VLOG(1) << "CancelPairing: " << object_path.value(); |
| 223 error_callback.Run(kNoResponseError, ""); |
| 224 } |
| 225 |
| 226 |
| 227 void FakeBluetoothDeviceClient::BeginDiscoverySimulation( |
| 228 const dbus::ObjectPath& adapter_path) { |
| 229 VLOG(1) << "starting discovery simulation"; |
| 230 |
| 231 discovery_simulation_step_ = 1; |
| 232 |
| 233 MessageLoop::current()->PostDelayedTask( |
| 234 FROM_HERE, |
| 235 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, |
| 236 base::Unretained(this)), |
| 237 base::TimeDelta::FromMilliseconds(discovery_simulation_interval_ms_)); |
| 238 } |
| 239 |
| 240 void FakeBluetoothDeviceClient::EndDiscoverySimulation( |
| 241 const dbus::ObjectPath& adapter_path) { |
| 242 VLOG(1) << "stopping discovery simulation"; |
| 243 discovery_simulation_step_ = 0; |
| 244 } |
| 245 |
| 246 void FakeBluetoothDeviceClient::SetDiscoverySimulationIntervalMs( |
| 247 int interval_ms) { |
| 248 discovery_simulation_interval_ms_ = interval_ms; |
| 249 } |
| 250 |
| 251 void FakeBluetoothDeviceClient::RemoveDevice( |
| 252 const dbus::ObjectPath& adapter_path, |
| 253 const dbus::ObjectPath& device_path) { |
| 254 std::vector<dbus::ObjectPath>::iterator listiter = |
| 255 std::find(device_list_.begin(), device_list_.end(), device_path); |
| 256 if (listiter == device_list_.end()) |
| 257 return; |
| 258 |
| 259 PropertiesMap::iterator iter = properties_map_.find(device_path); |
| 260 Properties* properties = iter->second; |
| 261 |
| 262 VLOG(1) << "removing device: " << properties->alias.value(); |
| 263 device_list_.erase(listiter); |
| 264 |
| 265 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 266 DeviceRemoved(device_path)); |
| 267 |
| 268 delete properties; |
| 269 properties_map_.erase(iter); |
| 270 } |
| 271 |
| 272 void FakeBluetoothDeviceClient::OnPropertyChanged( |
| 273 const dbus::ObjectPath& object_path, |
| 274 const std::string& property_name) { |
| 275 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 276 DevicePropertyChanged(object_path, property_name)); |
| 277 } |
| 278 |
| 279 void FakeBluetoothDeviceClient::DiscoverySimulationTimer() { |
| 280 if (!discovery_simulation_step_) |
| 281 return; |
| 282 |
| 283 // Timer fires every .75s, the numbers below are arbitrary to give a feel |
| 284 // for a discovery process. |
| 285 VLOG(1) << "discovery simulation, step " << discovery_simulation_step_; |
| 286 if (discovery_simulation_step_ == 2) { |
| 287 if (std::find(device_list_.begin(), device_list_.end(), |
| 288 kAppleMousePath) == device_list_.end()) { |
| 289 Properties* properties = new Properties(base::Bind( |
| 290 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 291 base::Unretained(this), |
| 292 kAppleMousePath)); |
| 293 properties->address.ReplaceValue(kAppleMouseAddress); |
| 294 properties->bluetooth_class.ReplaceValue(kAppleMouseClass); |
| 295 properties->name.ReplaceValue("Fake Apple Magic Mouse"); |
| 296 properties->alias.ReplaceValue(kAppleMouseName); |
| 297 properties->adapter.ReplaceValue( |
| 298 FakeBluetoothAdapterClient::kAdapterPath); |
| 299 |
| 300 properties_map_[kAppleMousePath] = properties; |
| 301 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 302 DeviceAdded(kAppleMousePath)); |
| 303 } |
| 304 |
| 305 } else if (discovery_simulation_step_ == 4) { |
| 306 if (std::find(device_list_.begin(), device_list_.end(), |
| 307 kAppleKeyboardPath) == device_list_.end()) { |
| 308 Properties *properties = new Properties(base::Bind( |
| 309 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 310 base::Unretained(this), |
| 311 kAppleKeyboardPath)); |
| 312 properties->address.ReplaceValue(kAppleKeyboardAddress); |
| 313 properties->bluetooth_class.ReplaceValue(kAppleKeyboardClass); |
| 314 properties->name.ReplaceValue("Fake Apple Wireless Keyboard"); |
| 315 properties->alias.ReplaceValue(kAppleKeyboardName); |
| 316 properties->adapter.ReplaceValue( |
| 317 FakeBluetoothAdapterClient::kAdapterPath); |
| 318 |
| 319 properties_map_[kAppleKeyboardPath] = properties; |
| 320 device_list_.push_back(kAppleKeyboardPath); |
| 321 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 322 DeviceAdded(kAppleKeyboardPath)); |
| 323 } |
| 324 |
| 325 if (std::find(device_list_.begin(), device_list_.end(), |
| 326 kVanishingDevicePath) == device_list_.end()) { |
| 327 Properties* properties = new Properties(base::Bind( |
| 328 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 329 base::Unretained(this), |
| 330 kVanishingDevicePath)); |
| 331 properties->address.ReplaceValue(kVanishingDeviceAddress); |
| 332 properties->bluetooth_class.ReplaceValue(kVanishingDeviceClass); |
| 333 properties->name.ReplaceValue("Fake Vanishing Device"); |
| 334 properties->alias.ReplaceValue(kVanishingDeviceName); |
| 335 properties->adapter.ReplaceValue( |
| 336 FakeBluetoothAdapterClient::kAdapterPath); |
| 337 |
| 338 properties_map_[kVanishingDevicePath] = properties; |
| 339 device_list_.push_back(kVanishingDevicePath); |
| 340 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 341 DeviceAdded(kVanishingDevicePath)); |
| 342 } |
| 343 |
| 344 } else if (discovery_simulation_step_ == 7) { |
| 345 if (std::find(device_list_.begin(), device_list_.end(), |
| 346 kMicrosoftMousePath) == device_list_.end()) { |
| 347 Properties* properties = new Properties(base::Bind( |
| 348 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 349 base::Unretained(this), |
| 350 kMicrosoftMousePath)); |
| 351 properties->address.ReplaceValue(kMicrosoftMouseAddress); |
| 352 properties->bluetooth_class.ReplaceValue(kMicrosoftMouseClass); |
| 353 properties->name.ReplaceValue("Fake Microsoft Mouse"); |
| 354 properties->alias.ReplaceValue(kMicrosoftMouseName); |
| 355 properties->adapter.ReplaceValue( |
| 356 FakeBluetoothAdapterClient::kAdapterPath); |
| 357 |
| 358 properties_map_[kMicrosoftMousePath] = properties; |
| 359 device_list_.push_back(kMicrosoftMousePath); |
| 360 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 361 DeviceAdded(kMicrosoftMousePath)); |
| 362 } |
| 363 |
| 364 } else if (discovery_simulation_step_ == 8) { |
| 365 if (std::find(device_list_.begin(), device_list_.end(), |
| 366 kMotorolaKeyboardPath) == device_list_.end()) { |
| 367 Properties* properties = new Properties(base::Bind( |
| 368 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 369 base::Unretained(this), |
| 370 kMotorolaKeyboardPath)); |
| 371 properties->address.ReplaceValue(kMotorolaKeyboardAddress); |
| 372 properties->bluetooth_class.ReplaceValue(kMotorolaKeyboardClass); |
| 373 properties->name.ReplaceValue("Fake Motorola Keyboard"); |
| 374 properties->alias.ReplaceValue(kMotorolaKeyboardName); |
| 375 properties->adapter.ReplaceValue( |
| 376 FakeBluetoothAdapterClient::kAdapterPath); |
| 377 |
| 378 properties_map_[kMotorolaKeyboardPath] = properties; |
| 379 device_list_.push_back(kMotorolaKeyboardPath); |
| 380 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 381 DeviceAdded(kMotorolaKeyboardPath)); |
| 382 } |
| 383 |
| 384 } else if (discovery_simulation_step_ == 10) { |
| 385 if (std::find(device_list_.begin(), device_list_.end(), |
| 386 kPhonePath) == device_list_.end()) { |
| 387 Properties* properties = new Properties(base::Bind( |
| 388 &FakeBluetoothDeviceClient::OnPropertyChanged, |
| 389 base::Unretained(this), |
| 390 kPhonePath)); |
| 391 properties->address.ReplaceValue(kPhoneAddress); |
| 392 properties->bluetooth_class.ReplaceValue(kPhoneClass); |
| 393 properties->name.ReplaceValue("Fake Phone"); |
| 394 properties->alias.ReplaceValue(kPhoneName); |
| 395 properties->adapter.ReplaceValue( |
| 396 FakeBluetoothAdapterClient::kAdapterPath); |
| 397 |
| 398 properties_map_[kPhonePath] = properties; |
| 399 device_list_.push_back(kPhonePath); |
| 400 FOR_EACH_OBSERVER(ExperimentalBluetoothDeviceClient::Observer, observers_, |
| 401 DeviceAdded(kPhonePath)); |
| 402 } |
| 403 |
| 404 } else if (discovery_simulation_step_ == 13) { |
| 405 RemoveDevice(FakeBluetoothAdapterClient::kAdapterPath, |
| 406 kVanishingDevicePath); |
| 407 |
| 408 } else if (discovery_simulation_step_ == 14) { |
| 409 return; |
| 410 |
| 411 } |
| 412 |
| 413 ++discovery_simulation_step_; |
| 414 MessageLoop::current()->PostDelayedTask( |
| 415 FROM_HERE, |
| 416 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, |
| 417 base::Unretained(this)), |
| 418 base::TimeDelta::FromMilliseconds(discovery_simulation_interval_ms_)); |
| 419 } |
| 420 |
| 421 } // namespace chromeos |
OLD | NEW |