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_path.h"
13 #include "dbus/object_proxy.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
18 const char BluetoothAdapterClient::kNoResponseError
[] =
19 "org.chromium.Error.NoResponse";
20 const char BluetoothAdapterClient::kUnknownAdapterError
[] =
21 "org.chromium.Error.UnknownAdapter";
23 BluetoothAdapterClient::Properties::Properties(
24 dbus::ObjectProxy
* object_proxy
,
25 const std::string
& interface_name
,
26 const PropertyChangedCallback
& callback
)
27 : dbus::PropertySet(object_proxy
, interface_name
, callback
) {
28 RegisterProperty(bluetooth_adapter::kAddressProperty
, &address
);
29 RegisterProperty(bluetooth_adapter::kNameProperty
, &name
);
30 RegisterProperty(bluetooth_adapter::kAliasProperty
, &alias
);
31 RegisterProperty(bluetooth_adapter::kClassProperty
, &bluetooth_class
);
32 RegisterProperty(bluetooth_adapter::kPoweredProperty
, &powered
);
33 RegisterProperty(bluetooth_adapter::kDiscoverableProperty
, &discoverable
);
34 RegisterProperty(bluetooth_adapter::kPairableProperty
, &pairable
);
35 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty
,
37 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty
,
38 &discoverable_timeout
);
39 RegisterProperty(bluetooth_adapter::kDiscoveringProperty
, &discovering
);
40 RegisterProperty(bluetooth_adapter::kUUIDsProperty
, &uuids
);
41 RegisterProperty(bluetooth_adapter::kModaliasProperty
, &modalias
);
44 BluetoothAdapterClient::Properties::~Properties() {
48 // The BluetoothAdapterClient implementation used in production.
49 class BluetoothAdapterClientImpl
50 : public BluetoothAdapterClient
,
51 public dbus::ObjectManager::Interface
{
53 BluetoothAdapterClientImpl()
54 : object_manager_(NULL
), weak_ptr_factory_(this) {}
56 virtual ~BluetoothAdapterClientImpl() {
57 object_manager_
->UnregisterInterface(
58 bluetooth_adapter::kBluetoothAdapterInterface
);
61 // BluetoothAdapterClient override.
62 virtual void AddObserver(BluetoothAdapterClient::Observer
* observer
)
65 observers_
.AddObserver(observer
);
68 // BluetoothAdapterClient override.
69 virtual void RemoveObserver(BluetoothAdapterClient::Observer
* observer
)
72 observers_
.RemoveObserver(observer
);
75 // Returns the list of adapter object paths known to the system.
76 virtual std::vector
<dbus::ObjectPath
> GetAdapters() override
{
77 return object_manager_
->GetObjectsWithInterface(
78 bluetooth_adapter::kBluetoothAdapterInterface
);
81 // dbus::ObjectManager::Interface override.
82 virtual dbus::PropertySet
* CreateProperties(
83 dbus::ObjectProxy
* object_proxy
,
84 const dbus::ObjectPath
& object_path
,
85 const std::string
& interface_name
) override
{
86 Properties
* properties
= new Properties(
89 base::Bind(&BluetoothAdapterClientImpl::OnPropertyChanged
,
90 weak_ptr_factory_
.GetWeakPtr(),
92 return static_cast<dbus::PropertySet
*>(properties
);
95 // BluetoothAdapterClient override.
96 virtual Properties
* GetProperties(const dbus::ObjectPath
& object_path
)
98 return static_cast<Properties
*>(
99 object_manager_
->GetProperties(
101 bluetooth_adapter::kBluetoothAdapterInterface
));
104 // BluetoothAdapterClient override.
105 virtual void StartDiscovery(const dbus::ObjectPath
& object_path
,
106 const base::Closure
& callback
,
107 const ErrorCallback
& error_callback
) override
{
108 dbus::MethodCall
method_call(
109 bluetooth_adapter::kBluetoothAdapterInterface
,
110 bluetooth_adapter::kStartDiscovery
);
112 dbus::ObjectProxy
* object_proxy
=
113 object_manager_
->GetObjectProxy(object_path
);
115 error_callback
.Run(kUnknownAdapterError
, "");
119 object_proxy
->CallMethodWithErrorCallback(
121 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
122 base::Bind(&BluetoothAdapterClientImpl::OnSuccess
,
123 weak_ptr_factory_
.GetWeakPtr(), callback
),
124 base::Bind(&BluetoothAdapterClientImpl::OnError
,
125 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
128 // BluetoothAdapterClient override.
129 virtual void StopDiscovery(const dbus::ObjectPath
& object_path
,
130 const base::Closure
& callback
,
131 const ErrorCallback
& error_callback
) override
{
132 dbus::MethodCall
method_call(
133 bluetooth_adapter::kBluetoothAdapterInterface
,
134 bluetooth_adapter::kStopDiscovery
);
136 dbus::ObjectProxy
* object_proxy
=
137 object_manager_
->GetObjectProxy(object_path
);
139 error_callback
.Run(kUnknownAdapterError
, "");
143 object_proxy
->CallMethodWithErrorCallback(
145 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
146 base::Bind(&BluetoothAdapterClientImpl::OnSuccess
,
147 weak_ptr_factory_
.GetWeakPtr(), callback
),
148 base::Bind(&BluetoothAdapterClientImpl::OnError
,
149 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
152 // BluetoothAdapterClient override.
153 virtual void RemoveDevice(const dbus::ObjectPath
& object_path
,
154 const dbus::ObjectPath
& device_path
,
155 const base::Closure
& callback
,
156 const ErrorCallback
& error_callback
) override
{
157 dbus::MethodCall
method_call(
158 bluetooth_adapter::kBluetoothAdapterInterface
,
159 bluetooth_adapter::kRemoveDevice
);
161 dbus::MessageWriter
writer(&method_call
);
162 writer
.AppendObjectPath(device_path
);
164 dbus::ObjectProxy
* object_proxy
=
165 object_manager_
->GetObjectProxy(object_path
);
167 error_callback
.Run(kUnknownAdapterError
, "");
171 object_proxy
->CallMethodWithErrorCallback(
173 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
174 base::Bind(&BluetoothAdapterClientImpl::OnSuccess
,
175 weak_ptr_factory_
.GetWeakPtr(), callback
),
176 base::Bind(&BluetoothAdapterClientImpl::OnError
,
177 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
181 virtual void Init(dbus::Bus
* bus
) override
{
182 object_manager_
= bus
->GetObjectManager(
183 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
185 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
186 object_manager_
->RegisterInterface(
187 bluetooth_adapter::kBluetoothAdapterInterface
, this);
191 // Called by dbus::ObjectManager when an object with the adapter interface
192 // is created. Informs observers.
193 virtual void ObjectAdded(const dbus::ObjectPath
& object_path
,
194 const std::string
& interface_name
) override
{
195 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
196 AdapterAdded(object_path
));
199 // Called by dbus::ObjectManager when an object with the adapter interface
200 // is removed. Informs observers.
201 virtual void ObjectRemoved(const dbus::ObjectPath
& object_path
,
202 const std::string
& interface_name
) override
{
203 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
204 AdapterRemoved(object_path
));
207 // Called by dbus::PropertySet when a property value is changed,
208 // either by result of a signal or response to a GetAll() or Get()
209 // call. Informs observers.
210 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
211 const std::string
& property_name
) {
212 FOR_EACH_OBSERVER(BluetoothAdapterClient::Observer
, observers_
,
213 AdapterPropertyChanged(object_path
, property_name
));
216 // Called when a response for successful method call is received.
217 void OnSuccess(const base::Closure
& callback
,
218 dbus::Response
* response
) {
223 // Called when a response for a failed method call is received.
224 void OnError(const ErrorCallback
& error_callback
,
225 dbus::ErrorResponse
* response
) {
226 // Error response has optional error message argument.
227 std::string error_name
;
228 std::string error_message
;
230 dbus::MessageReader
reader(response
);
231 error_name
= response
->GetErrorName();
232 reader
.PopString(&error_message
);
234 error_name
= kNoResponseError
;
237 error_callback
.Run(error_name
, error_message
);
240 dbus::ObjectManager
* object_manager_
;
242 // List of observers interested in event notifications from us.
243 ObserverList
<BluetoothAdapterClient::Observer
> observers_
;
245 // Weak pointer factory for generating 'this' pointers that might live longer
247 // Note: This should remain the last member so it'll be destroyed and
248 // invalidate its weak pointers before any other members are destroyed.
249 base::WeakPtrFactory
<BluetoothAdapterClientImpl
>
252 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterClientImpl
);
255 BluetoothAdapterClient::BluetoothAdapterClient() {
258 BluetoothAdapterClient::~BluetoothAdapterClient() {
261 BluetoothAdapterClient
* BluetoothAdapterClient::Create() {
262 return new BluetoothAdapterClientImpl
;
265 } // namespace chromeos