Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_gatt_service_client.cc
blob1a566427dadd88239d5d9960300f0496f2d06c35
1 // Copyright 2014 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.
5 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/time/time.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
16 namespace chromeos {
18 namespace {
20 const int kExposeCharacteristicsDelayIntervalMs = 100;
22 } // namespace
24 // static
25 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent[] =
26 "service0000";
27 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID[] =
28 "0000180d-0000-1000-8000-00805f9b34fb";
30 FakeBluetoothGattServiceClient::Properties::Properties(
31 const PropertyChangedCallback& callback)
32 : BluetoothGattServiceClient::Properties(
33 NULL,
34 bluetooth_gatt_service::kBluetoothGattServiceInterface,
35 callback) {
38 FakeBluetoothGattServiceClient::Properties::~Properties() {
41 void FakeBluetoothGattServiceClient::Properties::Get(
42 dbus::PropertyBase* property,
43 dbus::PropertySet::GetCallback callback) {
44 VLOG(1) << "Get " << property->name();
45 callback.Run(false);
48 void FakeBluetoothGattServiceClient::Properties::GetAll() {
49 VLOG(1) << "GetAll";
52 void FakeBluetoothGattServiceClient::Properties::Set(
53 dbus::PropertyBase* property,
54 dbus::PropertySet::GetCallback callback) {
55 VLOG(1) << "Set " << property->name();
56 callback.Run(false);
59 FakeBluetoothGattServiceClient::FakeBluetoothGattServiceClient()
60 : weak_ptr_factory_(this) {
63 FakeBluetoothGattServiceClient::~FakeBluetoothGattServiceClient() {
66 void FakeBluetoothGattServiceClient::Init(dbus::Bus* bus) {
69 void FakeBluetoothGattServiceClient::AddObserver(Observer* observer) {
70 observers_.AddObserver(observer);
73 void FakeBluetoothGattServiceClient::RemoveObserver(Observer* observer) {
74 observers_.RemoveObserver(observer);
77 std::vector<dbus::ObjectPath> FakeBluetoothGattServiceClient::GetServices() {
78 std::vector<dbus::ObjectPath> paths;
79 if (heart_rate_service_properties_.get()) {
80 DCHECK(!heart_rate_service_path_.empty());
81 paths.push_back(dbus::ObjectPath(heart_rate_service_path_));
83 return paths;
86 FakeBluetoothGattServiceClient::Properties*
87 FakeBluetoothGattServiceClient::GetProperties(
88 const dbus::ObjectPath& object_path) {
89 if (object_path.value() == heart_rate_service_path_)
90 return heart_rate_service_properties_.get();
91 return NULL;
94 void FakeBluetoothGattServiceClient::ExposeHeartRateService(
95 const dbus::ObjectPath& device_path) {
96 if (IsHeartRateVisible()) {
97 DCHECK(!heart_rate_service_path_.empty());
98 VLOG(1) << "Fake Heart Rate Service already exposed.";
99 return;
101 VLOG(2) << "Exposing fake Heart Rate Service.";
102 heart_rate_service_path_ =
103 device_path.value() + "/" + kHeartRateServicePathComponent;
104 heart_rate_service_properties_.reset(new Properties(base::Bind(
105 &FakeBluetoothGattServiceClient::OnPropertyChanged,
106 base::Unretained(this),
107 dbus::ObjectPath(heart_rate_service_path_))));
108 heart_rate_service_properties_->uuid.ReplaceValue(kHeartRateServiceUUID);
109 heart_rate_service_properties_->device.ReplaceValue(device_path);
110 heart_rate_service_properties_->primary.ReplaceValue(true);
112 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_));
114 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
115 FROM_HERE,
116 base::Bind(
117 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics,
118 weak_ptr_factory_.GetWeakPtr()),
119 base::TimeDelta::FromMilliseconds(kExposeCharacteristicsDelayIntervalMs));
122 void FakeBluetoothGattServiceClient::HideHeartRateService() {
123 if (!IsHeartRateVisible()) {
124 DCHECK(heart_rate_service_path_.empty());
125 VLOG(1) << "Fake Heart Rate Service already hidden.";
126 return;
128 VLOG(2) << "Hiding fake Heart Rate Service.";
129 FakeBluetoothGattCharacteristicClient* char_client =
130 static_cast<FakeBluetoothGattCharacteristicClient*>(
131 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient());
132 char_client->HideHeartRateCharacteristics();
134 // Notify observers before deleting the properties structure so that it
135 // can be accessed from the observer method.
136 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_));
138 heart_rate_service_properties_.reset();
139 heart_rate_service_path_.clear();
142 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const {
143 return !!heart_rate_service_properties_.get();
146 dbus::ObjectPath
147 FakeBluetoothGattServiceClient::GetHeartRateServicePath() const {
148 return dbus::ObjectPath(heart_rate_service_path_);
151 void FakeBluetoothGattServiceClient::OnPropertyChanged(
152 const dbus::ObjectPath& object_path,
153 const std::string& property_name) {
154 VLOG(2) << "Fake GATT Service property changed: " << object_path.value()
155 << ": " << property_name;
156 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer, observers_,
157 GattServicePropertyChanged(object_path, property_name));
160 void FakeBluetoothGattServiceClient::NotifyServiceAdded(
161 const dbus::ObjectPath& object_path) {
162 VLOG(2) << "GATT service added: " << object_path.value();
163 FOR_EACH_OBSERVER(
164 BluetoothGattServiceClient::Observer, observers_,
165 GattServiceAdded(object_path));
168 void FakeBluetoothGattServiceClient::NotifyServiceRemoved(
169 const dbus::ObjectPath& object_path) {
170 VLOG(2) << "GATT service removed: " << object_path.value();
171 FOR_EACH_OBSERVER(
172 BluetoothGattServiceClient::Observer, observers_,
173 GattServiceRemoved(object_path));
176 void FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics() {
177 if (!IsHeartRateVisible()) {
178 VLOG(2) << "Heart Rate service not visible. Not exposing characteristics.";
179 return;
181 FakeBluetoothGattCharacteristicClient* char_client =
182 static_cast<FakeBluetoothGattCharacteristicClient*>(
183 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient());
184 char_client->ExposeHeartRateCharacteristics(
185 dbus::ObjectPath(heart_rate_service_path_));
187 std::vector<dbus::ObjectPath> char_paths;
188 char_paths.push_back(char_client->GetHeartRateMeasurementPath());
189 char_paths.push_back(char_client->GetBodySensorLocationPath());
190 char_paths.push_back(char_client->GetHeartRateControlPointPath());
192 heart_rate_service_properties_->characteristics.ReplaceValue(char_paths);
195 } // namespace chromeos