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/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
18 const int kExposeCharacteristicsDelayIntervalMs
= 100;
23 const char FakeBluetoothGattServiceClient::kHeartRateServicePathComponent
[] =
25 const char FakeBluetoothGattServiceClient::kHeartRateServiceUUID
[] =
26 "0000180d-0000-1000-8000-00805f9b34fb";
28 FakeBluetoothGattServiceClient::Properties::Properties(
29 const PropertyChangedCallback
& callback
)
30 : BluetoothGattServiceClient::Properties(
32 bluetooth_gatt_service::kBluetoothGattServiceInterface
,
36 FakeBluetoothGattServiceClient::Properties::~Properties() {
39 void FakeBluetoothGattServiceClient::Properties::Get(
40 dbus::PropertyBase
* property
,
41 dbus::PropertySet::GetCallback callback
) {
42 VLOG(1) << "Get " << property
->name();
46 void FakeBluetoothGattServiceClient::Properties::GetAll() {
50 void FakeBluetoothGattServiceClient::Properties::Set(
51 dbus::PropertyBase
* property
,
52 dbus::PropertySet::GetCallback callback
) {
53 VLOG(1) << "Set " << property
->name();
57 FakeBluetoothGattServiceClient::FakeBluetoothGattServiceClient()
58 : weak_ptr_factory_(this) {
61 FakeBluetoothGattServiceClient::~FakeBluetoothGattServiceClient() {
64 void FakeBluetoothGattServiceClient::Init(dbus::Bus
* bus
) {
67 void FakeBluetoothGattServiceClient::AddObserver(Observer
* observer
) {
68 observers_
.AddObserver(observer
);
71 void FakeBluetoothGattServiceClient::RemoveObserver(Observer
* observer
) {
72 observers_
.RemoveObserver(observer
);
75 std::vector
<dbus::ObjectPath
> FakeBluetoothGattServiceClient::GetServices() {
76 std::vector
<dbus::ObjectPath
> paths
;
77 if (heart_rate_service_properties_
.get()) {
78 DCHECK(!heart_rate_service_path_
.empty());
79 paths
.push_back(dbus::ObjectPath(heart_rate_service_path_
));
84 FakeBluetoothGattServiceClient::Properties
*
85 FakeBluetoothGattServiceClient::GetProperties(
86 const dbus::ObjectPath
& object_path
) {
87 if (object_path
.value() == heart_rate_service_path_
)
88 return heart_rate_service_properties_
.get();
92 void FakeBluetoothGattServiceClient::ExposeHeartRateService(
93 const dbus::ObjectPath
& device_path
) {
94 if (IsHeartRateVisible()) {
95 DCHECK(!heart_rate_service_path_
.empty());
96 VLOG(1) << "Fake Heart Rate Service already exposed.";
99 VLOG(2) << "Exposing fake Heart Rate Service.";
100 heart_rate_service_path_
=
101 device_path
.value() + "/" + kHeartRateServicePathComponent
;
102 heart_rate_service_properties_
.reset(new Properties(base::Bind(
103 &FakeBluetoothGattServiceClient::OnPropertyChanged
,
104 base::Unretained(this),
105 dbus::ObjectPath(heart_rate_service_path_
))));
106 heart_rate_service_properties_
->uuid
.ReplaceValue(kHeartRateServiceUUID
);
107 heart_rate_service_properties_
->device
.ReplaceValue(device_path
);
108 heart_rate_service_properties_
->primary
.ReplaceValue(true);
110 NotifyServiceAdded(dbus::ObjectPath(heart_rate_service_path_
));
112 base::MessageLoop::current()->PostDelayedTask(
115 &FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics
,
116 weak_ptr_factory_
.GetWeakPtr()),
117 base::TimeDelta::FromMilliseconds(
118 kExposeCharacteristicsDelayIntervalMs
));
121 void FakeBluetoothGattServiceClient::HideHeartRateService() {
122 if (!IsHeartRateVisible()) {
123 DCHECK(heart_rate_service_path_
.empty());
124 VLOG(1) << "Fake Heart Rate Service already hidden.";
127 VLOG(2) << "Hiding fake Heart Rate Service.";
128 FakeBluetoothGattCharacteristicClient
* char_client
=
129 static_cast<FakeBluetoothGattCharacteristicClient
*>(
130 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient());
131 char_client
->HideHeartRateCharacteristics();
133 // Notify observers before deleting the properties structure so that it
134 // can be accessed from the observer method.
135 NotifyServiceRemoved(dbus::ObjectPath(heart_rate_service_path_
));
137 heart_rate_service_properties_
.reset();
138 heart_rate_service_path_
.clear();
141 bool FakeBluetoothGattServiceClient::IsHeartRateVisible() const {
142 return !!heart_rate_service_properties_
.get();
146 FakeBluetoothGattServiceClient::GetHeartRateServicePath() const {
147 return dbus::ObjectPath(heart_rate_service_path_
);
150 void FakeBluetoothGattServiceClient::OnPropertyChanged(
151 const dbus::ObjectPath
& object_path
,
152 const std::string
& property_name
) {
153 VLOG(2) << "Fake GATT Service property changed: " << object_path
.value()
154 << ": " << property_name
;
155 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
156 GattServicePropertyChanged(object_path
, property_name
));
159 void FakeBluetoothGattServiceClient::NotifyServiceAdded(
160 const dbus::ObjectPath
& object_path
) {
161 VLOG(2) << "GATT service added: " << object_path
.value();
163 BluetoothGattServiceClient::Observer
, observers_
,
164 GattServiceAdded(object_path
));
167 void FakeBluetoothGattServiceClient::NotifyServiceRemoved(
168 const dbus::ObjectPath
& object_path
) {
169 VLOG(2) << "GATT service removed: " << object_path
.value();
171 BluetoothGattServiceClient::Observer
, observers_
,
172 GattServiceRemoved(object_path
));
175 void FakeBluetoothGattServiceClient::ExposeHeartRateCharacteristics() {
176 if (!IsHeartRateVisible()) {
177 VLOG(2) << "Heart Rate service not visible. Not exposing characteristics.";
180 FakeBluetoothGattCharacteristicClient
* char_client
=
181 static_cast<FakeBluetoothGattCharacteristicClient
*>(
182 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient());
183 char_client
->ExposeHeartRateCharacteristics(
184 dbus::ObjectPath(heart_rate_service_path_
));
187 } // namespace chromeos