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"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/time/time.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/fake_bluetooth_device_client.h"
13 #include "dbus/object_path.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
20 // Default interval for delayed tasks.
21 const int kSimulationIntervalMs
= 750;
25 const char FakeBluetoothAdapterClient::kAdapterPath
[] =
27 const char FakeBluetoothAdapterClient::kAdapterName
[] =
29 const char FakeBluetoothAdapterClient::kAdapterAddress
[] =
32 const char FakeBluetoothAdapterClient::kSecondAdapterPath
[] =
34 const char FakeBluetoothAdapterClient::kSecondAdapterName
[] =
35 "Second Fake Adapter";
36 const char FakeBluetoothAdapterClient::kSecondAdapterAddress
[] =
39 FakeBluetoothAdapterClient::Properties::Properties(
40 const PropertyChangedCallback
& callback
)
41 : BluetoothAdapterClient::Properties(
43 bluetooth_adapter::kBluetoothAdapterInterface
,
47 FakeBluetoothAdapterClient::Properties::~Properties() {
50 void FakeBluetoothAdapterClient::Properties::Get(
51 dbus::PropertyBase
* property
,
52 dbus::PropertySet::GetCallback callback
) {
53 VLOG(1) << "Get " << property
->name();
57 void FakeBluetoothAdapterClient::Properties::GetAll() {
61 void FakeBluetoothAdapterClient::Properties::Set(
62 dbus::PropertyBase
*property
,
63 dbus::PropertySet::SetCallback callback
) {
64 VLOG(1) << "Set " << property
->name();
65 if (property
->name() == powered
.name() ||
66 property
->name() == alias
.name() ||
67 property
->name() == discoverable
.name() ||
68 property
->name() == discoverable_timeout
.name()) {
70 property
->ReplaceValueWithSetValue();
76 FakeBluetoothAdapterClient::FakeBluetoothAdapterClient()
78 second_visible_(false),
79 discovering_count_(0),
80 simulation_interval_ms_(kSimulationIntervalMs
) {
81 properties_
.reset(new Properties(base::Bind(
82 &FakeBluetoothAdapterClient::OnPropertyChanged
, base::Unretained(this))));
84 properties_
->address
.ReplaceValue(kAdapterAddress
);
85 properties_
->name
.ReplaceValue("Fake Adapter (Name)");
86 properties_
->alias
.ReplaceValue(kAdapterName
);
87 properties_
->pairable
.ReplaceValue(true);
89 second_properties_
.reset(new Properties(base::Bind(
90 &FakeBluetoothAdapterClient::OnPropertyChanged
, base::Unretained(this))));
92 second_properties_
->address
.ReplaceValue(kSecondAdapterAddress
);
93 second_properties_
->name
.ReplaceValue("Second Fake Adapter (Name)");
94 second_properties_
->alias
.ReplaceValue(kSecondAdapterName
);
95 second_properties_
->pairable
.ReplaceValue(true);
98 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() {
101 void FakeBluetoothAdapterClient::Init(dbus::Bus
* bus
) {
104 void FakeBluetoothAdapterClient::AddObserver(Observer
* observer
) {
105 observers_
.AddObserver(observer
);
108 void FakeBluetoothAdapterClient::RemoveObserver(Observer
* observer
) {
109 observers_
.RemoveObserver(observer
);
112 std::vector
<dbus::ObjectPath
> FakeBluetoothAdapterClient::GetAdapters() {
113 std::vector
<dbus::ObjectPath
> object_paths
;
115 object_paths
.push_back(dbus::ObjectPath(kAdapterPath
));
117 object_paths
.push_back(dbus::ObjectPath(kSecondAdapterPath
));
121 FakeBluetoothAdapterClient::Properties
*
122 FakeBluetoothAdapterClient::GetProperties(const dbus::ObjectPath
& object_path
) {
123 if (object_path
== dbus::ObjectPath(kAdapterPath
))
124 return properties_
.get();
125 else if (object_path
== dbus::ObjectPath(kSecondAdapterPath
))
126 return second_properties_
.get();
131 void FakeBluetoothAdapterClient::StartDiscovery(
132 const dbus::ObjectPath
& object_path
,
133 const base::Closure
& callback
,
134 const ErrorCallback
& error_callback
) {
135 if (object_path
!= dbus::ObjectPath(kAdapterPath
)) {
136 PostDelayedTask(base::Bind(error_callback
, kNoResponseError
, ""));
140 ++discovering_count_
;
141 VLOG(1) << "StartDiscovery: " << object_path
.value() << ", "
142 << "count is now " << discovering_count_
;
143 PostDelayedTask(callback
);
145 if (discovering_count_
== 1) {
146 properties_
->discovering
.ReplaceValue(true);
148 FakeBluetoothDeviceClient
* device_client
=
149 static_cast<FakeBluetoothDeviceClient
*>(
150 DBusThreadManager::Get()->GetBluetoothDeviceClient());
151 device_client
->BeginDiscoverySimulation(dbus::ObjectPath(kAdapterPath
));
155 void FakeBluetoothAdapterClient::StopDiscovery(
156 const dbus::ObjectPath
& object_path
,
157 const base::Closure
& callback
,
158 const ErrorCallback
& error_callback
) {
159 if (object_path
!= dbus::ObjectPath(kAdapterPath
)) {
160 PostDelayedTask(base::Bind(error_callback
, kNoResponseError
, ""));
164 if (!discovering_count_
) {
165 LOG(WARNING
) << "StopDiscovery called when not discovering";
166 PostDelayedTask(base::Bind(error_callback
, kNoResponseError
, ""));
170 --discovering_count_
;
171 VLOG(1) << "StopDiscovery: " << object_path
.value() << ", "
172 << "count is now " << discovering_count_
;
173 PostDelayedTask(callback
);
175 if (discovering_count_
== 0) {
176 FakeBluetoothDeviceClient
* device_client
=
177 static_cast<FakeBluetoothDeviceClient
*>(
178 DBusThreadManager::Get()->GetBluetoothDeviceClient());
179 device_client
->EndDiscoverySimulation(dbus::ObjectPath(kAdapterPath
));
181 if (simulation_interval_ms_
> 100) {
182 device_client
->BeginIncomingPairingSimulation(
183 dbus::ObjectPath(kAdapterPath
));
186 properties_
->discovering
.ReplaceValue(false);
190 void FakeBluetoothAdapterClient::RemoveDevice(
191 const dbus::ObjectPath
& object_path
,
192 const dbus::ObjectPath
& device_path
,
193 const base::Closure
& callback
,
194 const ErrorCallback
& error_callback
) {
195 if (object_path
!= dbus::ObjectPath(kAdapterPath
)) {
196 error_callback
.Run(kNoResponseError
, "");
200 VLOG(1) << "RemoveDevice: " << object_path
.value()
201 << " " << device_path
.value();
204 FakeBluetoothDeviceClient
* device_client
=
205 static_cast<FakeBluetoothDeviceClient
*>(
206 DBusThreadManager::Get()->GetBluetoothDeviceClient());
207 device_client
->RemoveDevice(dbus::ObjectPath(kAdapterPath
), device_path
);
210 void FakeBluetoothAdapterClient::SetSimulationIntervalMs(int interval_ms
) {
211 simulation_interval_ms_
= interval_ms
;
214 void FakeBluetoothAdapterClient::SetVisible(
216 if (visible
&& !visible_
) {
217 // Adapter becoming visible
220 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
221 AdapterAdded(dbus::ObjectPath(kAdapterPath
)));
223 } else if (visible_
&& !visible
) {
224 // Adapter becoming invisible
227 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
228 AdapterRemoved(dbus::ObjectPath(kAdapterPath
)));
232 void FakeBluetoothAdapterClient::SetSecondVisible(
234 if (visible
&& !second_visible_
) {
235 // Second adapter becoming visible
236 second_visible_
= visible
;
238 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
239 AdapterAdded(dbus::ObjectPath(kSecondAdapterPath
)));
241 } else if (second_visible_
&& !visible
) {
242 // Second adapter becoming invisible
243 second_visible_
= visible
;
245 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
246 AdapterRemoved(dbus::ObjectPath(kSecondAdapterPath
)));
250 void FakeBluetoothAdapterClient::OnPropertyChanged(
251 const std::string
& property_name
) {
252 if (property_name
== properties_
->powered
.name() &&
253 !properties_
->powered
.value()) {
254 VLOG(1) << "Adapter powered off";
256 if (discovering_count_
) {
257 discovering_count_
= 0;
258 properties_
->discovering
.ReplaceValue(false);
262 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
263 AdapterPropertyChanged(dbus::ObjectPath(kAdapterPath
),
267 void FakeBluetoothAdapterClient::PostDelayedTask(
268 const base::Closure
& callback
) {
269 base::MessageLoop::current()->PostDelayedTask(
271 base::TimeDelta::FromMilliseconds(simulation_interval_ms_
));
274 } // namespace chromeos