diff options
author | rkc <rkc@chromium.org> | 2015-10-06 04:25:52 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-10-06 11:26:53 +0000 |
commit | c7eeff6a15962486725ac28b0ded7fd9296f1e77 (patch) | |
tree | 9658dfae6f757fab11e852a17427c43160399ad7 /chromeos/dbus/fake_bluetooth_gatt_service_client.cc | |
parent | 6a4640a83d99484907275aac3bec32c474cb6c63 (diff) | |
download | chromium_src-c7eeff6a15962486725ac28b0ded7fd9296f1e77.zip chromium_src-c7eeff6a15962486725ac28b0ded7fd9296f1e77.tar.gz chromium_src-c7eeff6a15962486725ac28b0ded7fd9296f1e77.tar.bz2 |
Revert "Refactor DBusThreadManager to split away BT clients."
This reverts commit 5d3c8959a7aee861b6750fd734cad96bd24e1f9f.
This commit is breaking the valgrind tests.
R=rkc@chromium.org
TBR=armansito@chromium.org, stevenjb@chromium.org, sky@chromium.org
BUG=None.
Review URL: https://codereview.chromium.org/1382923003
Cr-Commit-Position: refs/heads/master@{#352581}
Diffstat (limited to 'chromeos/dbus/fake_bluetooth_gatt_service_client.cc')
-rw-r--r-- | chromeos/dbus/fake_bluetooth_gatt_service_client.cc | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/chromeos/dbus/fake_bluetooth_gatt_service_client.cc b/chromeos/dbus/fake_bluetooth_gatt_service_client.cc new file mode 100644 index 0000000..47b39b6 --- /dev/null +++ b/chromeos/dbus/fake_bluetooth_gatt_service_client.cc @@ -0,0 +1,187 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" + +#include "base/bind.h" +#include "base/location.h" +#include "base/single_thread_task_runner.h" +#include "base/thread_task_runner_handle.h" +#include "base/time/time.h" +#include "chromeos/dbus/dbus_thread_manager.h" +#include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" +#include "third_party/cros_system_api/dbus/service_constants.h" + +namespace chromeos { + +namespace { + +const int kExposeCharacteristicsDelayIntervalMs = 100; + +} // namespace + +// static +const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] = + "service0000"; +const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] = + "0000180d-0000-1000-8000-00805f9b34fb"; + +FakeBluetoothGattServiceClient::Properties::Properties( + const PropertyChangedCallback& callback) + : BluetoothGattServiceClient::Properties( + NULL, + bluetooth_gatt_service::kBluetoothGattServiceInterface, + callback) {} + +FakeBluetoothGattServiceClient::Properties::~Properties() {} + +void FakeBluetoothGattServiceClient::Properties::Get( + dbus::PropertyBase* property, + dbus::PropertySet::GetCallback callback) { + VLOG(1) << "Get " << property->name(); + callback.Run(false); +} + +void FakeBluetoothGattServiceClient::Properties::GetAll() { + VLOG(1) << "GetAll"; +} + +void FakeBluetoothGattServiceClient::Properties::Set( + dbus::PropertyBase* property, + dbus::PropertySet::GetCallback callback) { + VLOG(1) << "Set " << property->name(); + callback.Run(false); +} + +FakeBluetoothGattServiceClient::FakeBluetoothGattServiceClient() + : weak_ptr_factory_(this) {} + +FakeBluetoothGattServiceClient::~FakeBluetoothGattServiceClient() {} + +void FakeBluetoothGattServiceClient::Init(dbus::Bus* bus) {} + +void FakeBluetoothGattServiceClient::AddObserver(Observer* observer) { + observers_.AddObserver(observer); +} + +void FakeBluetoothGattServiceClient::RemoveObserver(Observer* observer) { + observers_.RemoveObserver(observer); +} + +std::vector<dbus::ObjectPath> FakeBluetoothGattServiceClient::GetServices() { + std::vector<dbus::ObjectPath> paths; + if (heart_rate_service_properties_.get()) { + DCHECK(!heart_rate_service_path_.empty()); + paths.push_back(dbus::ObjectPath(heart_rate_service_path_)); + } + return paths; +} + +FakeBluetoothGattServiceClient::Properties* +FakeBluetoothGattServiceClient::GetProperties( + const dbus::ObjectPath& object_path) { + if (object_path.value() == heart_rate_service_path_) + return heart_rate_service_properties_.get(); + return NULL; +} + +void FakeBluetoothGattServiceClient::ExposeHeartRateService( + const dbus::ObjectPath& device_path) { + if (IsHeartRateVisible()) { + DCHECK(!heart_rate_service_path_.empty()); + VLOG(1) << "Fake Heart Rate Service already exposed."; + return; + } + VLOG(2) << "Exposing fake Heart Rate Service."; + heart_rate_service_path_ = + device_path.value() + "/" + kHeartRateServicePathComponent; + heart_rate_service_properties_.reset(new Properties(base::Bind( + &FakeBluetoothGattServiceClient::OnPropertyChanged, + base::Unretained(this), dbus::ObjectPath(heart_rate_service_path_)))); + heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID); + heart_rate_service_properties_->device.ReplaceValue(device_path); + heart_rate_service_properties_->primary.ReplaceValue(true); + + NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_)); + + base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( + FROM_HERE, + base::Bind( + &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics, + weak_ptr_factory_.GetWeakPtr()), + base::TimeDelta::FromMilliseconds(kExposeCharacteristicsDelayIntervalMs)); +} + +void FakeBluetoothGattServiceClient::HideHeartRateService() { + if (!IsHeartRateVisible()) { + DCHECK(heart_rate_service_path_.empty()); + VLOG(1) << "Fake Heart Rate Service already hidden."; + return; + } + VLOG(2) << "Hiding fake Heart Rate Service."; + FakeBluetoothGattCharacteristicClient* char_client = + static_cast<FakeBluetoothGattCharacteristicClient*>( + DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); + char_client->HideHeartRateCharacteristics(); + + // Notify observers before deleting the properties structure so that it + // can be accessed from the observer method. + NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_)); + + heart_rate_service_properties_.reset(); + heart_rate_service_path_.clear(); +} + +bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const { + return !!heart_rate_service_properties_.get(); +} + +dbus::ObjectPath FakeBluetoothGattServiceClient::GetHeartRateServicePath() + const { + return dbus::ObjectPath(heart_rate_service_path_); +} + +void FakeBluetoothGattServiceClient::OnPropertyChanged( + const dbus::ObjectPath& object_path, + const std::string& property_name) { + VLOG(2) << "Fake GATT Service property changed: " << object_path.value() + << ": " << property_name; + FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, + GattServicePropertyChanged(object_path, property_name)); +} + +void FakeBluetoothGattServiceClient::NotifyServiceAdded( + const dbus::ObjectPath& object_path) { + VLOG(2) << "GATT service added: " << object_path.value(); + FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, + GattServiceAdded(object_path)); +} + +void FakeBluetoothGattServiceClient::NotifyServiceRemoved( + const dbus::ObjectPath& object_path) { + VLOG(2) << "GATT service removed: " << object_path.value(); + FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_, + GattServiceRemoved(object_path)); +} + +void FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics() { + if (!IsHeartRateVisible()) { + VLOG(2) << "Heart Rate service not visible. Not exposing characteristics."; + return; + } + FakeBluetoothGattCharacteristicClient* char_client = + static_cast<FakeBluetoothGattCharacteristicClient*>( + DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()); + char_client->ExposeHeartRateCharacteristics( + dbus::ObjectPath(heart_rate_service_path_)); + + std::vector<dbus::ObjectPath> char_paths; + char_paths.push_back(char_client->GetHeartRateMeasurementPath()); + char_paths.push_back(char_client->GetBodySensorLocationPath()); + char_paths.push_back(char_client->GetHeartRateControlPointPath()); + + heart_rate_service_properties_->characteristics.ReplaceValue(char_paths); +} + +} // namespace chromeos |