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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
7 #include "base/logging.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_device_client.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
18 // Default interval for delayed tasks.
19 const int kSimulationIntervalMs
= 750;
23 const char FakeBluetoothAdapterClient::kAdapterPath
[] =
25 const char FakeBluetoothAdapterClient::kAdapterName
[] =
27 const char FakeBluetoothAdapterClient::kAdapterAddress
[] =
30 const char FakeBluetoothAdapterClient::kSecondAdapterPath
[] =
32 const char FakeBluetoothAdapterClient::kSecondAdapterName
[] =
33 "Second Fake Adapter";
34 const char FakeBluetoothAdapterClient::kSecondAdapterAddress
[] =
37 FakeBluetoothAdapterClient::Properties::Properties(
38 const PropertyChangedCallback
& callback
)
39 : BluetoothAdapterClient::Properties(
41 bluetooth_adapter::kBluetoothAdapterInterface
,
45 FakeBluetoothAdapterClient::Properties::~Properties() {
48 void FakeBluetoothAdapterClient::Properties::Get(
49 dbus::PropertyBase
* property
,
50 dbus::PropertySet::GetCallback callback
) {
51 VLOG(1) << "Get " << property
->name();
55 void FakeBluetoothAdapterClient::Properties::GetAll() {
59 void FakeBluetoothAdapterClient::Properties::Set(
60 dbus::PropertyBase
*property
,
61 dbus::PropertySet::SetCallback callback
) {
62 VLOG(1) << "Set " << property
->name();
63 if (property
->name() == powered
.name() ||
64 property
->name() == alias
.name() ||
65 property
->name() == discoverable
.name() ||
66 property
->name() == discoverable_timeout
.name()) {
68 property
->ReplaceValueWithSetValue();
74 FakeBluetoothAdapterClient::FakeBluetoothAdapterClient()
76 second_visible_(false),
77 discovering_count_(0),
78 simulation_interval_ms_(kSimulationIntervalMs
) {
79 properties_
.reset(new Properties(base::Bind(
80 &FakeBluetoothAdapterClient::OnPropertyChanged
, base::Unretained(this))));
82 properties_
->address
.ReplaceValue(kAdapterAddress
);
83 properties_
->name
.ReplaceValue("Fake Adapter (Name)");
84 properties_
->alias
.ReplaceValue(kAdapterName
);
85 properties_
->pairable
.ReplaceValue(true);
87 second_properties_
.reset(new Properties(base::Bind(
88 &FakeBluetoothAdapterClient::OnPropertyChanged
, base::Unretained(this))));
90 second_properties_
->address
.ReplaceValue(kSecondAdapterAddress
);
91 second_properties_
->name
.ReplaceValue("Second Fake Adapter (Name)");
92 second_properties_
->alias
.ReplaceValue(kSecondAdapterName
);
93 second_properties_
->pairable
.ReplaceValue(true);
96 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() {
99 void FakeBluetoothAdapterClient::Init(dbus::Bus
* bus
) {
102 void FakeBluetoothAdapterClient::AddObserver(Observer
* observer
) {
103 observers_
.AddObserver(observer
);
106 void FakeBluetoothAdapterClient::RemoveObserver(Observer
* observer
) {
107 observers_
.RemoveObserver(observer
);
110 std::vector
<dbus::ObjectPath
> FakeBluetoothAdapterClient::GetAdapters() {
111 std::vector
<dbus::ObjectPath
> object_paths
;
113 object_paths
.push_back(dbus::ObjectPath(kAdapterPath
));
115 object_paths
.push_back(dbus::ObjectPath(kSecondAdapterPath
));
119 FakeBluetoothAdapterClient::Properties
*
120 FakeBluetoothAdapterClient::GetProperties(const dbus::ObjectPath
& object_path
) {
121 if (object_path
== dbus::ObjectPath(kAdapterPath
))
122 return properties_
.get();
123 else if (object_path
== dbus::ObjectPath(kSecondAdapterPath
))
124 return second_properties_
.get();
129 void FakeBluetoothAdapterClient::StartDiscovery(
130 const dbus::ObjectPath
& object_path
,
131 const base::Closure
& callback
,
132 const ErrorCallback
& error_callback
) {
133 if (object_path
!= dbus::ObjectPath(kAdapterPath
)) {
134 PostDelayedTask(base::Bind(error_callback
, kNoResponseError
, ""));
138 ++discovering_count_
;
139 VLOG(1) << "StartDiscovery: " << object_path
.value() << ", "
140 << "count is now " << discovering_count_
;
141 PostDelayedTask(callback
);
143 if (discovering_count_
== 1) {
144 properties_
->discovering
.ReplaceValue(true);
146 FakeBluetoothDeviceClient
* device_client
=
147 static_cast<FakeBluetoothDeviceClient
*>(
148 DBusThreadManager::Get()->GetBluetoothDeviceClient());
149 device_client
->BeginDiscoverySimulation(dbus::ObjectPath(kAdapterPath
));
153 void FakeBluetoothAdapterClient::StopDiscovery(
154 const dbus::ObjectPath
& object_path
,
155 const base::Closure
& callback
,
156 const ErrorCallback
& error_callback
) {
157 if (object_path
!= dbus::ObjectPath(kAdapterPath
)) {
158 PostDelayedTask(base::Bind(error_callback
, kNoResponseError
, ""));
162 if (!discovering_count_
) {
163 LOG(WARNING
) << "StopDiscovery called when not discovering";
164 PostDelayedTask(base::Bind(error_callback
, kNoResponseError
, ""));
168 --discovering_count_
;
169 VLOG(1) << "StopDiscovery: " << object_path
.value() << ", "
170 << "count is now " << discovering_count_
;
171 PostDelayedTask(callback
);
173 if (discovering_count_
== 0) {
174 FakeBluetoothDeviceClient
* device_client
=
175 static_cast<FakeBluetoothDeviceClient
*>(
176 DBusThreadManager::Get()->GetBluetoothDeviceClient());
177 device_client
->EndDiscoverySimulation(dbus::ObjectPath(kAdapterPath
));
179 if (simulation_interval_ms_
> 100) {
180 device_client
->BeginIncomingPairingSimulation(
181 dbus::ObjectPath(kAdapterPath
));
184 properties_
->discovering
.ReplaceValue(false);
188 void FakeBluetoothAdapterClient::RemoveDevice(
189 const dbus::ObjectPath
& object_path
,
190 const dbus::ObjectPath
& device_path
,
191 const base::Closure
& callback
,
192 const ErrorCallback
& error_callback
) {
193 if (object_path
!= dbus::ObjectPath(kAdapterPath
)) {
194 error_callback
.Run(kNoResponseError
, "");
198 VLOG(1) << "RemoveDevice: " << object_path
.value()
199 << " " << device_path
.value();
202 FakeBluetoothDeviceClient
* device_client
=
203 static_cast<FakeBluetoothDeviceClient
*>(
204 DBusThreadManager::Get()->GetBluetoothDeviceClient());
205 device_client
->RemoveDevice(dbus::ObjectPath(kAdapterPath
), device_path
);
208 void FakeBluetoothAdapterClient::SetSimulationIntervalMs(int interval_ms
) {
209 simulation_interval_ms_
= interval_ms
;
212 void FakeBluetoothAdapterClient::SetVisible(
214 if (visible
&& !visible_
) {
215 // Adapter becoming visible
218 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
219 AdapterAdded(dbus::ObjectPath(kAdapterPath
)));
221 } else if (visible_
&& !visible
) {
222 // Adapter becoming invisible
225 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
226 AdapterRemoved(dbus::ObjectPath(kAdapterPath
)));
230 void FakeBluetoothAdapterClient::SetSecondVisible(
232 if (visible
&& !second_visible_
) {
233 // Second adapter becoming visible
234 second_visible_
= visible
;
236 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
237 AdapterAdded(dbus::ObjectPath(kSecondAdapterPath
)));
239 } else if (second_visible_
&& !visible
) {
240 // Second adapter becoming invisible
241 second_visible_
= visible
;
243 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
244 AdapterRemoved(dbus::ObjectPath(kSecondAdapterPath
)));
248 void FakeBluetoothAdapterClient::OnPropertyChanged(
249 const std::string
& property_name
) {
250 if (property_name
== properties_
->powered
.name() &&
251 !properties_
->powered
.value()) {
252 VLOG(1) << "Adapter powered off";
254 if (discovering_count_
) {
255 discovering_count_
= 0;
256 properties_
->discovering
.ReplaceValue(false);
260 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
261 AdapterPropertyChanged(dbus::ObjectPath(kAdapterPath
),
265 void FakeBluetoothAdapterClient::PostDelayedTask(
266 const base::Closure
& callback
) {
267 base::MessageLoop::current()->PostDelayedTask(
269 base::TimeDelta::FromMilliseconds(simulation_interval_ms_
));
272 } // namespace chromeos