1 // Copyright 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/bluetooth_adapter_client.h"
8 #include "base/logging.h"
10 #include "dbus/message.h"
11 #include "dbus/object_manager.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
17 const char BluetoothAdapterClient::kNoResponseError
[] =
18 "org.chromium.Error.NoResponse";
19 const char BluetoothAdapterClient::kUnknownAdapterError
[] =
20 "org.chromium.Error.UnknownAdapter";
22 BluetoothAdapterClient::Properties::Properties(
23 dbus::ObjectProxy
* object_proxy
,
24 const std::string
& interface_name
,
25 const PropertyChangedCallback
& callback
)
26 : dbus::PropertySet(object_proxy
, interface_name
, callback
) {
27 RegisterProperty(bluetooth_adapter::kAddressProperty
, &address
);
28 RegisterProperty(bluetooth_adapter::kNameProperty
, &name
);
29 RegisterProperty(bluetooth_adapter::kAliasProperty
, &alias
);
30 RegisterProperty(bluetooth_adapter::kClassProperty
, &bluetooth_class
);
31 RegisterProperty(bluetooth_adapter::kPoweredProperty
, &powered
);
32 RegisterProperty(bluetooth_adapter::kDiscoverableProperty
, &discoverable
);
33 RegisterProperty(bluetooth_adapter::kPairableProperty
, &pairable
);
34 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty
,
36 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty
,
37 &discoverable_timeout
);
38 RegisterProperty(bluetooth_adapter::kDiscoveringProperty
, &discovering
);
39 RegisterProperty(bluetooth_adapter::kUUIDsProperty
, &uuids
);
40 RegisterProperty(bluetooth_adapter::kModaliasProperty
, &modalias
);
43 BluetoothAdapterClient::Properties::~Properties() {
47 // The BluetoothAdapterClient implementation used in production.
48 class BluetoothAdapterClientImpl
49 : public BluetoothAdapterClient
,
50 public dbus::ObjectManager::Interface
{
52 BluetoothAdapterClientImpl()
53 : object_manager_(NULL
), weak_ptr_factory_(this) {}
55 ~BluetoothAdapterClientImpl() override
{
56 object_manager_
->UnregisterInterface(
57 bluetooth_adapter::kBluetoothAdapterInterface
);
60 // BluetoothAdapterClient override.
61 void AddObserver(BluetoothAdapterClient::Observer
* observer
) override
{
63 observers_
.AddObserver(observer
);
66 // BluetoothAdapterClient override.
67 void RemoveObserver(BluetoothAdapterClient::Observer
* observer
) override
{
69 observers_
.RemoveObserver(observer
);
72 // Returns the list of adapter object paths known to the system.
73 std::vector
<dbus::ObjectPath
> GetAdapters() override
{
74 return object_manager_
->GetObjectsWithInterface(
75 bluetooth_adapter::kBluetoothAdapterInterface
);
78 // dbus::ObjectManager::Interface override.
79 dbus::PropertySet
* CreateProperties(
80 dbus::ObjectProxy
* object_proxy
,
81 const dbus::ObjectPath
& object_path
,
82 const std::string
& interface_name
) override
{
83 Properties
* properties
= new Properties(
86 base::Bind(&BluetoothAdapterClientImpl::OnPropertyChanged
,
87 weak_ptr_factory_
.GetWeakPtr(),
89 return static_cast<dbus::PropertySet
*>(properties
);
92 // BluetoothAdapterClient override.
93 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
{
94 return static_cast<Properties
*>(
95 object_manager_
->GetProperties(
97 bluetooth_adapter::kBluetoothAdapterInterface
));
100 // BluetoothAdapterClient override.
101 void StartDiscovery(const dbus::ObjectPath
& object_path
,
102 const base::Closure
& callback
,
103 const ErrorCallback
& error_callback
) override
{
104 dbus::MethodCall
method_call(
105 bluetooth_adapter::kBluetoothAdapterInterface
,
106 bluetooth_adapter::kStartDiscovery
);
108 dbus::ObjectProxy
* object_proxy
=
109 object_manager_
->GetObjectProxy(object_path
);
111 error_callback
.Run(kUnknownAdapterError
, "");
115 object_proxy
->CallMethodWithErrorCallback(
117 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
118 base::Bind(&BluetoothAdapterClientImpl::OnSuccess
,
119 weak_ptr_factory_
.GetWeakPtr(), callback
),
120 base::Bind(&BluetoothAdapterClientImpl::OnError
,
121 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
124 // BluetoothAdapterClient override.
125 void StopDiscovery(const dbus::ObjectPath
& object_path
,
126 const base::Closure
& callback
,
127 const ErrorCallback
& error_callback
) override
{
128 dbus::MethodCall
method_call(
129 bluetooth_adapter::kBluetoothAdapterInterface
,
130 bluetooth_adapter::kStopDiscovery
);
132 dbus::ObjectProxy
* object_proxy
=
133 object_manager_
->GetObjectProxy(object_path
);
135 error_callback
.Run(kUnknownAdapterError
, "");
139 object_proxy
->CallMethodWithErrorCallback(
141 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
142 base::Bind(&BluetoothAdapterClientImpl::OnSuccess
,
143 weak_ptr_factory_
.GetWeakPtr(), callback
),
144 base::Bind(&BluetoothAdapterClientImpl::OnError
,
145 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
148 // BluetoothAdapterClient override.
149 void RemoveDevice(const dbus::ObjectPath
& object_path
,
150 const dbus::ObjectPath
& device_path
,
151 const base::Closure
& callback
,
152 const ErrorCallback
& error_callback
) override
{
153 dbus::MethodCall
method_call(
154 bluetooth_adapter::kBluetoothAdapterInterface
,
155 bluetooth_adapter::kRemoveDevice
);
157 dbus::MessageWriter
writer(&method_call
);
158 writer
.AppendObjectPath(device_path
);
160 dbus::ObjectProxy
* object_proxy
=
161 object_manager_
->GetObjectProxy(object_path
);
163 error_callback
.Run(kUnknownAdapterError
, "");
167 object_proxy
->CallMethodWithErrorCallback(
169 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
170 base::Bind(&BluetoothAdapterClientImpl::OnSuccess
,
171 weak_ptr_factory_
.GetWeakPtr(), callback
),
172 base::Bind(&BluetoothAdapterClientImpl::OnError
,
173 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
177 void Init(dbus::Bus
* bus
) override
{
178 object_manager_
= bus
->GetObjectManager(
179 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
181 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
182 object_manager_
->RegisterInterface(
183 bluetooth_adapter::kBluetoothAdapterInterface
, this);
187 // Called by dbus::ObjectManager when an object with the adapter interface
188 // is created. Informs observers.
189 void ObjectAdded(const dbus::ObjectPath
& object_path
,
190 const std::string
& interface_name
) override
{
191 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
192 AdapterAdded(object_path
));
195 // Called by dbus::ObjectManager when an object with the adapter interface
196 // is removed. Informs observers.
197 void ObjectRemoved(const dbus::ObjectPath
& object_path
,
198 const std::string
& interface_name
) override
{
199 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
200 AdapterRemoved(object_path
));
203 // Called by dbus::PropertySet when a property value is changed,
204 // either by result of a signal or response to a GetAll() or Get()
205 // call. Informs observers.
206 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
207 const std::string
& property_name
) {
208 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
209 AdapterPropertyChanged(object_path
, property_name
));
212 // Called when a response for successful method call is received.
213 void OnSuccess(const base::Closure
& callback
,
214 dbus::Response
* response
) {
219 // Called when a response for a failed method call is received.
220 void OnError(const ErrorCallback
& error_callback
,
221 dbus::ErrorResponse
* response
) {
222 // Error response has optional error message argument.
223 std::string error_name
;
224 std::string error_message
;
226 dbus::MessageReader
reader(response
);
227 error_name
= response
->GetErrorName();
228 reader
.PopString(&error_message
);
230 error_name
= kNoResponseError
;
233 error_callback
.Run(error_name
, error_message
);
236 dbus::ObjectManager
* object_manager_
;
238 // List of observers interested in event notifications from us.
239 ObserverList
<BluetoothAdapterClient::Observer
> observers_
;
241 // Weak pointer factory for generating 'this' pointers that might live longer
243 // Note: This should remain the last member so it'll be destroyed and
244 // invalidate its weak pointers before any other members are destroyed.
245 base::WeakPtrFactory
<BluetoothAdapterClientImpl
>
248 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl
);
251 BluetoothAdapterClient::BluetoothAdapterClient() {
254 BluetoothAdapterClient::~BluetoothAdapterClient() {
257 BluetoothAdapterClient
* BluetoothAdapterClient::Create() {
258 return new BluetoothAdapterClientImpl
;
261 } // namespace chromeos