Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: chromeos/dbus/fake_bluetooth_device_client.cc

Issue 206443009: chromeos/dbus: Add fake D-Bus clients for GATT client-mode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_device_client.h" 5 #include "chromeos/dbus/fake_bluetooth_device_client.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <unistd.h> 8 #include <unistd.h>
9 #include <sys/types.h> 9 #include <sys/types.h>
10 #include <sys/socket.h> 10 #include <sys/socket.h>
11 11
12 #include <algorithm> 12 #include <algorithm>
13 #include <map> 13 #include <map>
14 #include <string> 14 #include <string>
15 #include <utility> 15 #include <utility>
16 #include <vector> 16 #include <vector>
17 17
18 #include "base/bind.h" 18 #include "base/bind.h"
19 #include "base/logging.h" 19 #include "base/logging.h"
20 #include "base/memory/scoped_ptr.h" 20 #include "base/memory/scoped_ptr.h"
21 #include "base/message_loop/message_loop.h" 21 #include "base/message_loop/message_loop.h"
22 #include "base/stl_util.h" 22 #include "base/stl_util.h"
23 #include "base/threading/worker_pool.h" 23 #include "base/threading/worker_pool.h"
24 #include "base/time/time.h" 24 #include "base/time/time.h"
25 #include "chromeos/dbus/dbus_thread_manager.h" 25 #include "chromeos/dbus/dbus_thread_manager.h"
26 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" 26 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
27 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" 27 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
28 #include "chromeos/dbus/fake_bluetooth_agent_service_provider.h" 28 #include "chromeos/dbus/fake_bluetooth_agent_service_provider.h"
29 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
29 #include "chromeos/dbus/fake_bluetooth_input_client.h" 30 #include "chromeos/dbus/fake_bluetooth_input_client.h"
30 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h" 31 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
31 #include "chromeos/dbus/fake_bluetooth_profile_service_provider.h" 32 #include "chromeos/dbus/fake_bluetooth_profile_service_provider.h"
32 #include "dbus/file_descriptor.h" 33 #include "dbus/file_descriptor.h"
33 #include "dbus/object_path.h" 34 #include "dbus/object_path.h"
34 #include "third_party/cros_system_api/dbus/service_constants.h" 35 #include "third_party/cros_system_api/dbus/service_constants.h"
35 36
36 namespace { 37 namespace {
37 38
38 // Default interval between simulated events. 39 // Default interval between simulated events.
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 const uint32 FakeBluetoothDeviceClient::kUnpairableDeviceClass = 0x002540; 161 const uint32 FakeBluetoothDeviceClient::kUnpairableDeviceClass = 0x002540;
161 162
162 const char FakeBluetoothDeviceClient::kJustWorksPath[] = 163 const char FakeBluetoothDeviceClient::kJustWorksPath[] =
163 "/fake/hci0/devB"; 164 "/fake/hci0/devB";
164 const char FakeBluetoothDeviceClient::kJustWorksAddress[] = 165 const char FakeBluetoothDeviceClient::kJustWorksAddress[] =
165 "00:0C:8A:00:00:00"; 166 "00:0C:8A:00:00:00";
166 const char FakeBluetoothDeviceClient::kJustWorksName[] = 167 const char FakeBluetoothDeviceClient::kJustWorksName[] =
167 "Just-Works Device"; 168 "Just-Works Device";
168 const uint32 FakeBluetoothDeviceClient::kJustWorksClass = 0x240428; 169 const uint32 FakeBluetoothDeviceClient::kJustWorksClass = 0x240428;
169 170
171 const char FakeBluetoothDeviceClient::kLowEnergyPath[] =
172 "/fake/hci0/devC";
173 const char FakeBluetoothDeviceClient::kLowEnergyAddress[] =
174 "00:1A:11:00:15:30";
175 const char FakeBluetoothDeviceClient::kLowEnergyName[] =
176 "Bluetooth 4.0 Heart Rate Monitor";
177 const uint32 FakeBluetoothDeviceClient::kLowEnergyClass =
178 0x000918; // Major class "Health", Minor class "Heart/Pulse Rate Monitor."
179
170 FakeBluetoothDeviceClient::Properties::Properties( 180 FakeBluetoothDeviceClient::Properties::Properties(
171 const PropertyChangedCallback& callback) 181 const PropertyChangedCallback& callback)
172 : BluetoothDeviceClient::Properties( 182 : BluetoothDeviceClient::Properties(
173 NULL, 183 NULL,
174 bluetooth_device::kBluetoothDeviceInterface, 184 bluetooth_device::kBluetoothDeviceInterface,
175 callback) { 185 callback) {
176 } 186 }
177 187
178 FakeBluetoothDeviceClient::Properties::~Properties() { 188 FakeBluetoothDeviceClient::Properties::~Properties() {
179 } 189 }
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 // Must not be paired 296 // Must not be paired
287 error_callback.Run(bluetooth_device::kErrorFailed, 297 error_callback.Run(bluetooth_device::kErrorFailed,
288 "Connection fails while paired"); 298 "Connection fails while paired");
289 return; 299 return;
290 } 300 }
291 301
292 // The device can be connected. 302 // The device can be connected.
293 properties->connected.ReplaceValue(true); 303 properties->connected.ReplaceValue(true);
294 callback.Run(); 304 callback.Run();
295 305
306 // Expose GATT services if connected to LE device.
307 if (object_path == dbus::ObjectPath(kLowEnergyPath)) {
308 FakeBluetoothGattServiceClient* gatt_service_client =
309 static_cast<FakeBluetoothGattServiceClient*>(
310 DBusThreadManager::Get()->GetBluetoothGattServiceClient());
311 gatt_service_client->ExposeHeartRateService(
312 dbus::ObjectPath(kLowEnergyPath));
313 }
314
296 AddInputDeviceIfNeeded(object_path, properties); 315 AddInputDeviceIfNeeded(object_path, properties);
297 } 316 }
298 317
299 void FakeBluetoothDeviceClient::Disconnect( 318 void FakeBluetoothDeviceClient::Disconnect(
300 const dbus::ObjectPath& object_path, 319 const dbus::ObjectPath& object_path,
301 const base::Closure& callback, 320 const base::Closure& callback,
302 const ErrorCallback& error_callback) { 321 const ErrorCallback& error_callback) {
303 VLOG(1) << "Disconnect: " << object_path.value(); 322 VLOG(1) << "Disconnect: " << object_path.value();
304 Properties* properties = GetProperties(object_path); 323 Properties* properties = GetProperties(object_path);
305 324
306 if (properties->connected.value() == true) { 325 if (!properties->connected.value()) {
307 callback.Run();
308 properties->connected.ReplaceValue(false);
309 } else {
310 error_callback.Run("org.bluez.Error.NotConnected", "Not Connected"); 326 error_callback.Run("org.bluez.Error.NotConnected", "Not Connected");
327 return;
311 } 328 }
329
330 // Hide the Heart Rate Service if disconnected from LE device.
331 if (object_path == dbus::ObjectPath(kLowEnergyPath)) {
332 FakeBluetoothGattServiceClient* gatt_service_client =
333 static_cast<FakeBluetoothGattServiceClient*>(
334 DBusThreadManager::Get()->GetBluetoothGattServiceClient());
335 gatt_service_client->HideHeartRateService();
336 }
337
338 callback.Run();
339 properties->connected.ReplaceValue(false);
312 } 340 }
313 341
314 void FakeBluetoothDeviceClient::ConnectProfile( 342 void FakeBluetoothDeviceClient::ConnectProfile(
315 const dbus::ObjectPath& object_path, 343 const dbus::ObjectPath& object_path,
316 const std::string& uuid, 344 const std::string& uuid,
317 const base::Closure& callback, 345 const base::Closure& callback,
318 const ErrorCallback& error_callback) { 346 const ErrorCallback& error_callback) {
319 VLOG(1) << "ConnectProfile: " << object_path.value() << " " << uuid; 347 VLOG(1) << "ConnectProfile: " << object_path.value() << " " << uuid;
320 348
321 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client = 349 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client =
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 properties->bluetooth_class.ReplaceValue(kUnpairableDeviceClass); 585 properties->bluetooth_class.ReplaceValue(kUnpairableDeviceClass);
558 properties->name.ReplaceValue("Fake Unpairable Device"); 586 properties->name.ReplaceValue("Fake Unpairable Device");
559 properties->alias.ReplaceValue(kUnpairableDeviceName); 587 properties->alias.ReplaceValue(kUnpairableDeviceName);
560 588
561 } else if (device_path == dbus::ObjectPath(kJustWorksPath)) { 589 } else if (device_path == dbus::ObjectPath(kJustWorksPath)) {
562 properties->address.ReplaceValue(kJustWorksAddress); 590 properties->address.ReplaceValue(kJustWorksAddress);
563 properties->bluetooth_class.ReplaceValue(kJustWorksClass); 591 properties->bluetooth_class.ReplaceValue(kJustWorksClass);
564 properties->name.ReplaceValue("JustWorks"); 592 properties->name.ReplaceValue("JustWorks");
565 properties->alias.ReplaceValue(kJustWorksName); 593 properties->alias.ReplaceValue(kJustWorksName);
566 594
595 } else if (device_path == dbus::ObjectPath(kLowEnergyPath)) {
596 properties->address.ReplaceValue(kLowEnergyAddress);
597 properties->bluetooth_class.ReplaceValue(kLowEnergyClass);
598 properties->name.ReplaceValue("Heart Rate Monitor");
599 properties->alias.ReplaceValue(kLowEnergyName);
600
601 std::vector<std::string> uuids;
602 uuids.push_back(FakeBluetoothGattServiceClient::kHeartRateServiceUUID);
603 properties->uuids.ReplaceValue(uuids);
604
567 } else { 605 } else {
568 NOTREACHED(); 606 NOTREACHED();
569 607
570 } 608 }
571 609
572 properties_map_[device_path] = properties; 610 properties_map_[device_path] = properties;
573 device_list_.push_back(device_path); 611 device_list_.push_back(device_path);
574 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_, 612 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
575 DeviceAdded(device_path)); 613 DeviceAdded(device_path));
576 } 614 }
(...skipping 13 matching lines...) Expand all
590 device_list_.erase(listiter); 628 device_list_.erase(listiter);
591 629
592 // Remove the Input interface if it exists. This should be called before the 630 // Remove the Input interface if it exists. This should be called before the
593 // BluetoothDeviceClient::Observer::DeviceRemoved because it deletes the 631 // BluetoothDeviceClient::Observer::DeviceRemoved because it deletes the
594 // BluetoothDeviceChromeOS object, including the device_path referenced here. 632 // BluetoothDeviceChromeOS object, including the device_path referenced here.
595 FakeBluetoothInputClient* fake_bluetooth_input_client = 633 FakeBluetoothInputClient* fake_bluetooth_input_client =
596 static_cast<FakeBluetoothInputClient*>( 634 static_cast<FakeBluetoothInputClient*>(
597 DBusThreadManager::Get()->GetBluetoothInputClient()); 635 DBusThreadManager::Get()->GetBluetoothInputClient());
598 fake_bluetooth_input_client->RemoveInputDevice(device_path); 636 fake_bluetooth_input_client->RemoveInputDevice(device_path);
599 637
638 if (device_path == dbus::ObjectPath(kLowEnergyPath)) {
639 FakeBluetoothGattServiceClient* gatt_service_client =
640 static_cast<FakeBluetoothGattServiceClient*>(
641 DBusThreadManager::Get()->GetBluetoothGattServiceClient());
642 gatt_service_client->HideHeartRateService();
643 }
644
600 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_, 645 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
601 DeviceRemoved(device_path)); 646 DeviceRemoved(device_path));
602 647
603 delete properties; 648 delete properties;
604 properties_map_.erase(iter); 649 properties_map_.erase(iter);
605 } 650 }
606 651
607 void FakeBluetoothDeviceClient::OnPropertyChanged( 652 void FakeBluetoothDeviceClient::OnPropertyChanged(
608 const dbus::ObjectPath& object_path, 653 const dbus::ObjectPath& object_path,
609 const std::string& property_name) { 654 const std::string& property_name) {
(...skipping 20 matching lines...) Expand all
630 675
631 } else if (discovery_simulation_step_ == 7) { 676 } else if (discovery_simulation_step_ == 7) {
632 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 677 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
633 dbus::ObjectPath(kConnectUnpairablePath)); 678 dbus::ObjectPath(kConnectUnpairablePath));
634 679
635 } else if (discovery_simulation_step_ == 8) { 680 } else if (discovery_simulation_step_ == 8) {
636 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 681 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
637 dbus::ObjectPath(kDisplayPasskeyPath)); 682 dbus::ObjectPath(kDisplayPasskeyPath));
638 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 683 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
639 dbus::ObjectPath(kRequestPinCodePath)); 684 dbus::ObjectPath(kRequestPinCodePath));
685 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
686 dbus::ObjectPath(kLowEnergyPath));
keybuk 2014/03/21 19:57:53 Since this is an LE device, following the pattern
armansito 2014/03/21 23:18:11 Done.
640 687
641 } else if (discovery_simulation_step_ == 10) { 688 } else if (discovery_simulation_step_ == 10) {
642 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 689 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
643 dbus::ObjectPath(kConfirmPasskeyPath)); 690 dbus::ObjectPath(kConfirmPasskeyPath));
644 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 691 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
645 dbus::ObjectPath(kRequestPasskeyPath)); 692 dbus::ObjectPath(kRequestPasskeyPath));
646 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 693 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
647 dbus::ObjectPath(kUnconnectableDevicePath)); 694 dbus::ObjectPath(kUnconnectableDevicePath));
648 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 695 CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
649 dbus::ObjectPath(kUnpairableDevicePath)); 696 dbus::ObjectPath(kUnpairableDevicePath));
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 784
738 FakeBluetoothAgentManagerClient* fake_bluetooth_agent_manager_client = 785 FakeBluetoothAgentManagerClient* fake_bluetooth_agent_manager_client =
739 static_cast<FakeBluetoothAgentManagerClient*>( 786 static_cast<FakeBluetoothAgentManagerClient*>(
740 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()); 787 DBusThreadManager::Get()->GetBluetoothAgentManagerClient());
741 FakeBluetoothAgentServiceProvider* agent_service_provider = 788 FakeBluetoothAgentServiceProvider* agent_service_provider =
742 fake_bluetooth_agent_manager_client->GetAgentServiceProvider(); 789 fake_bluetooth_agent_manager_client->GetAgentServiceProvider();
743 CHECK(agent_service_provider != NULL); 790 CHECK(agent_service_provider != NULL);
744 791
745 if (object_path == dbus::ObjectPath(kLegacyAutopairPath) || 792 if (object_path == dbus::ObjectPath(kLegacyAutopairPath) ||
746 object_path == dbus::ObjectPath(kConnectUnpairablePath) || 793 object_path == dbus::ObjectPath(kConnectUnpairablePath) ||
747 object_path == dbus::ObjectPath(kUnconnectableDevicePath)) { 794 object_path == dbus::ObjectPath(kUnconnectableDevicePath) ||
795 object_path == dbus::ObjectPath(kLowEnergyPath)) {
748 // No need to call anything on the pairing delegate, just wait 3 times 796 // No need to call anything on the pairing delegate, just wait 3 times
749 // the interval before acting as if the other end accepted it. 797 // the interval before acting as if the other end accepted it.
750 base::MessageLoop::current()->PostDelayedTask( 798 base::MessageLoop::current()->PostDelayedTask(
751 FROM_HERE, 799 FROM_HERE,
752 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing, 800 base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
753 base::Unretained(this), 801 base::Unretained(this),
754 object_path, callback, error_callback), 802 object_path, callback, error_callback),
755 base::TimeDelta::FromMilliseconds(3 * simulation_interval_ms_)); 803 base::TimeDelta::FromMilliseconds(3 * simulation_interval_ms_));
756 804
757 } else if (object_path == dbus::ObjectPath(kDisplayPinCodePath)) { 805 } else if (object_path == dbus::ObjectPath(kDisplayPinCodePath)) {
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 // TODO(keybuk): tear down this side of the connection 1141 // TODO(keybuk): tear down this side of the connection
1094 callback.Run(); 1142 callback.Run();
1095 } else if (status == BluetoothProfileServiceProvider::Delegate::CANCELLED) { 1143 } else if (status == BluetoothProfileServiceProvider::Delegate::CANCELLED) {
1096 error_callback.Run(bluetooth_device::kErrorFailed, "Canceled"); 1144 error_callback.Run(bluetooth_device::kErrorFailed, "Canceled");
1097 } else if (status == BluetoothProfileServiceProvider::Delegate::REJECTED) { 1145 } else if (status == BluetoothProfileServiceProvider::Delegate::REJECTED) {
1098 error_callback.Run(bluetooth_device::kErrorFailed, "Rejected"); 1146 error_callback.Run(bluetooth_device::kErrorFailed, "Rejected");
1099 } 1147 }
1100 } 1148 }
1101 1149
1102 } // namespace chromeos 1150 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698