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"
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"
20 const int kExposeCharacteristicsDelayIntervalMs
= 100;
25 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent
[] =
27 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID
[] =
28 "0000180d-0000-1000-8000-00805f9b34fb";
30 FakeBluetoothGattServiceClient::Properties::Properties(
31 const PropertyChangedCallback
& callback
)
32 : BluetoothGattServiceClient::Properties(
34 bluetooth_gatt_service::kBluetoothGattServiceInterface
,
38 FakeBluetoothGattServiceClient::Properties::~Properties() {
41 void FakeBluetoothGattServiceClient::Properties::Get(
42 dbus::PropertyBase
* property
,
43 dbus::PropertySet::GetCallback callback
) {
44 VLOG(1) << "Get " << property
->name();
48 void FakeBluetoothGattServiceClient::Properties::GetAll() {
52 void FakeBluetoothGattServiceClient::Properties::Set(
53 dbus::PropertyBase
* property
,
54 dbus::PropertySet::GetCallback callback
) {
55 VLOG(1) << "Set " << property
->name();
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_
));
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();
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.";
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(
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.";
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();
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();
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();
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.";
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