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"
18 const char FakeBluetoothAdapterClient::kAdapterPath[] =
20 const char FakeBluetoothAdapterClient::kAdapterName[] =
22 const char FakeBluetoothAdapterClient::kAdapterAddress[] =
25 const char FakeBluetoothAdapterClient::kSecondAdapterPath[] =
27 const char FakeBluetoothAdapterClient::kSecondAdapterName[] =
28 "Second Fake Adapter";
29 const char FakeBluetoothAdapterClient::kSecondAdapterAddress[] =
32 FakeBluetoothAdapterClient::Properties::Properties(
33 const PropertyChangedCallback& callback)
34 : BluetoothAdapterClient::Properties(
36 bluetooth_adapter::kBluetoothAdapterInterface,
40 FakeBluetoothAdapterClient::Properties::~Properties() {
43 void FakeBluetoothAdapterClient::Properties::Get(
44 dbus::PropertyBase* property,
45 dbus::PropertySet::GetCallback callback) {
46 VLOG(1) << "Get " << property->name();
50 void FakeBluetoothAdapterClient::Properties::GetAll() {
54 void FakeBluetoothAdapterClient::Properties::Set(
55 dbus::PropertyBase *property,
56 dbus::PropertySet::SetCallback callback) {
57 VLOG(1) << "Set " << property->name();
58 if (property->name() == powered.name() ||
59 property->name() == alias.name() ||
60 property->name() == discoverable.name() ||
61 property->name() == discoverable_timeout.name()) {
63 property->ReplaceValueWithSetValue();
69 FakeBluetoothAdapterClient::FakeBluetoothAdapterClient()
71 second_visible_(false),
72 discovering_count_(0) {
73 properties_.reset(new Properties(base::Bind(
74 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this))));
76 properties_->address.ReplaceValue(kAdapterAddress);
77 properties_->name.ReplaceValue("Fake Adapter (Name)");
78 properties_->alias.ReplaceValue(kAdapterName);
79 properties_->pairable.ReplaceValue(true);
81 second_properties_.reset(new Properties(base::Bind(
82 &FakeBluetoothAdapterClient::OnPropertyChanged, base::Unretained(this))));
84 second_properties_->address.ReplaceValue(kSecondAdapterAddress);
85 second_properties_->name.ReplaceValue("Second Fake Adapter (Name)");
86 second_properties_->alias.ReplaceValue(kSecondAdapterName);
87 second_properties_->pairable.ReplaceValue(true);
90 FakeBluetoothAdapterClient::~FakeBluetoothAdapterClient() {
93 void FakeBluetoothAdapterClient::Init(dbus::Bus* bus) {
96 void FakeBluetoothAdapterClient::AddObserver(Observer* observer) {
97 observers_.AddObserver(observer);
100 void FakeBluetoothAdapterClient::RemoveObserver(Observer* observer) {
101 observers_.RemoveObserver(observer);
104 std::vector<dbus::ObjectPath> FakeBluetoothAdapterClient::GetAdapters() {
105 std::vector<dbus::ObjectPath> object_paths;
107 object_paths.push_back(dbus::ObjectPath(kAdapterPath));
109 object_paths.push_back(dbus::ObjectPath(kSecondAdapterPath));
113 FakeBluetoothAdapterClient::Properties*
114 FakeBluetoothAdapterClient::GetProperties(const dbus::ObjectPath& object_path) {
115 if (object_path == dbus::ObjectPath(kAdapterPath))
116 return properties_.get();
117 else if (object_path == dbus::ObjectPath(kSecondAdapterPath))
118 return second_properties_.get();
123 void FakeBluetoothAdapterClient::StartDiscovery(
124 const dbus::ObjectPath& object_path,
125 const base::Closure& callback,
126 const ErrorCallback& error_callback) {
127 if (object_path != dbus::ObjectPath(kAdapterPath)) {
128 error_callback.Run(kNoResponseError, "");
132 ++discovering_count_;
133 VLOG(1) << "StartDiscovery: " << object_path.value() << ", "
134 << "count is now " << discovering_count_;
137 if (discovering_count_ == 1) {
138 properties_->discovering.ReplaceValue(true);
140 FakeBluetoothDeviceClient* device_client =
141 static_cast<FakeBluetoothDeviceClient*>(
142 DBusThreadManager::Get()->GetBluetoothDeviceClient());
143 device_client->BeginDiscoverySimulation(dbus::ObjectPath(kAdapterPath));
147 void FakeBluetoothAdapterClient::StopDiscovery(
148 const dbus::ObjectPath& object_path,
149 const base::Closure& callback,
150 const ErrorCallback& error_callback) {
151 if (object_path != dbus::ObjectPath(kAdapterPath)) {
152 error_callback.Run(kNoResponseError, "");
156 if (!discovering_count_) {
157 LOG(WARNING) << "StopDiscovery called when not discovering";
158 error_callback.Run(kNoResponseError, "");
162 --discovering_count_;
163 VLOG(1) << "StopDiscovery: " << object_path.value() << ", "
164 << "count is now " << discovering_count_;
167 if (discovering_count_ == 0) {
168 FakeBluetoothDeviceClient* device_client =
169 static_cast<FakeBluetoothDeviceClient*>(
170 DBusThreadManager::Get()->GetBluetoothDeviceClient());
171 device_client->EndDiscoverySimulation(dbus::ObjectPath(kAdapterPath));
173 properties_->discovering.ReplaceValue(false);
177 void FakeBluetoothAdapterClient::RemoveDevice(
178 const dbus::ObjectPath& object_path,
179 const dbus::ObjectPath& device_path,
180 const base::Closure& callback,
181 const ErrorCallback& error_callback) {
182 if (object_path != dbus::ObjectPath(kAdapterPath)) {
183 error_callback.Run(kNoResponseError, "");
187 VLOG(1) << "RemoveDevice: " << object_path.value()
188 << " " << device_path.value();
191 FakeBluetoothDeviceClient* device_client =
192 static_cast<FakeBluetoothDeviceClient*>(
193 DBusThreadManager::Get()->GetBluetoothDeviceClient());
194 device_client->RemoveDevice(dbus::ObjectPath(kAdapterPath), device_path);
197 void FakeBluetoothAdapterClient::SetVisible(
199 if (visible && !visible_) {
200 // Adapter becoming visible
203 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
204 AdapterAdded(dbus::ObjectPath(kAdapterPath)));
206 } else if (visible_ && !visible) {
207 // Adapter becoming invisible
210 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
211 AdapterRemoved(dbus::ObjectPath(kAdapterPath)));
215 void FakeBluetoothAdapterClient::SetSecondVisible(
217 if (visible && !second_visible_) {
218 // Second adapter becoming visible
219 second_visible_ = visible;
221 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
222 AdapterAdded(dbus::ObjectPath(kSecondAdapterPath)));
224 } else if (second_visible_ && !visible) {
225 // Second adapter becoming invisible
226 second_visible_ = visible;
228 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
229 AdapterRemoved(dbus::ObjectPath(kSecondAdapterPath)));
233 void FakeBluetoothAdapterClient::OnPropertyChanged(
234 const std::string& property_name) {
235 if (property_name == properties_->powered.name() &&
236 !properties_->powered.value()) {
237 VLOG(1) << "Adapter powered off";
239 if (discovering_count_) {
240 discovering_count_ = 0;
241 properties_->discovering.ReplaceValue(false);
245 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer, observers_,
246 AdapterPropertyChanged(dbus::ObjectPath(kAdapterPath),
250 } // namespace chromeos