1 // Copyright 2014 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_gatt_descriptor_client.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/observer_list.h"
11 #include "dbus/object_manager.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
17 const char BluetoothGattDescriptorClient::kNoResponseError
[] =
18 "org.chromium.Error.NoResponse";
20 const char BluetoothGattDescriptorClient::kUnknownDescriptorError
[] =
21 "org.chromium.Error.UnknownDescriptor";
23 BluetoothGattDescriptorClient::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_gatt_descriptor::kUUIDProperty
, &uuid
);
29 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty
,
33 BluetoothGattDescriptorClient::Properties::~Properties() {
36 // The BluetoothGattDescriptorClient implementation used in production.
37 class BluetoothGattDescriptorClientImpl
38 : public BluetoothGattDescriptorClient
,
39 public dbus::ObjectManager::Interface
{
41 BluetoothGattDescriptorClientImpl()
42 : object_manager_(NULL
),
43 weak_ptr_factory_(this) {
46 ~BluetoothGattDescriptorClientImpl() override
{
47 object_manager_
->UnregisterInterface(
48 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
);
51 // BluetoothGattDescriptorClientImpl override.
52 void AddObserver(BluetoothGattDescriptorClient::Observer
* observer
) override
{
54 observers_
.AddObserver(observer
);
57 // BluetoothGattDescriptorClientImpl override.
59 BluetoothGattDescriptorClient::Observer
* observer
) override
{
61 observers_
.RemoveObserver(observer
);
64 // BluetoothGattDescriptorClientImpl override.
65 std::vector
<dbus::ObjectPath
> GetDescriptors() override
{
66 DCHECK(object_manager_
);
67 return object_manager_
->GetObjectsWithInterface(
68 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
);
71 // BluetoothGattDescriptorClientImpl override.
72 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
{
73 DCHECK(object_manager_
);
74 return static_cast<Properties
*>(
75 object_manager_
->GetProperties(
77 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
));
80 // BluetoothGattDescriptorClientImpl override.
81 void ReadValue(const dbus::ObjectPath
& object_path
,
82 const ValueCallback
& callback
,
83 const ErrorCallback
& error_callback
) override
{
84 dbus::ObjectProxy
* object_proxy
=
85 object_manager_
->GetObjectProxy(object_path
);
87 error_callback
.Run(kUnknownDescriptorError
, "");
91 dbus::MethodCall
method_call(
92 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
93 bluetooth_gatt_descriptor::kReadValue
);
95 object_proxy
->CallMethodWithErrorCallback(
97 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
98 base::Bind(&BluetoothGattDescriptorClientImpl::OnValueSuccess
,
99 weak_ptr_factory_
.GetWeakPtr(),
101 base::Bind(&BluetoothGattDescriptorClientImpl::OnError
,
102 weak_ptr_factory_
.GetWeakPtr(),
106 // BluetoothGattDescriptorClientImpl override.
107 void WriteValue(const dbus::ObjectPath
& object_path
,
108 const std::vector
<uint8
>& value
,
109 const base::Closure
& callback
,
110 const ErrorCallback
& error_callback
) override
{
111 dbus::ObjectProxy
* object_proxy
=
112 object_manager_
->GetObjectProxy(object_path
);
114 error_callback
.Run(kUnknownDescriptorError
, "");
118 dbus::MethodCall
method_call(
119 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
120 bluetooth_gatt_descriptor::kWriteValue
);
121 dbus::MessageWriter
writer(&method_call
);
122 writer
.AppendArrayOfBytes(value
.data(), value
.size());
124 object_proxy
->CallMethodWithErrorCallback(
126 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
127 base::Bind(&BluetoothGattDescriptorClientImpl::OnSuccess
,
128 weak_ptr_factory_
.GetWeakPtr(),
130 base::Bind(&BluetoothGattDescriptorClientImpl::OnError
,
131 weak_ptr_factory_
.GetWeakPtr(),
135 // dbus::ObjectManager::Interface override.
136 dbus::PropertySet
* CreateProperties(
137 dbus::ObjectProxy
* object_proxy
,
138 const dbus::ObjectPath
& object_path
,
139 const std::string
& interface_name
) override
{
140 Properties
* properties
= new Properties(
143 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged
,
144 weak_ptr_factory_
.GetWeakPtr(),
146 return static_cast<dbus::PropertySet
*>(properties
);
149 // dbus::ObjectManager::Interface override.
150 void ObjectAdded(const dbus::ObjectPath
& object_path
,
151 const std::string
& interface_name
) override
{
152 VLOG(2) << "Remote GATT descriptor added: " << object_path
.value();
153 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
154 GattDescriptorAdded(object_path
));
157 // dbus::ObjectManager::Interface override.
158 void ObjectRemoved(const dbus::ObjectPath
& object_path
,
159 const std::string
& interface_name
) override
{
160 VLOG(2) << "Remote GATT descriptor removed: " << object_path
.value();
161 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
162 GattDescriptorRemoved(object_path
));
166 // chromeos::DBusClient override.
167 void Init(dbus::Bus
* bus
) override
{
168 object_manager_
= bus
->GetObjectManager(
169 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
171 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
172 object_manager_
->RegisterInterface(
173 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
178 // Called by dbus::PropertySet when a property value is changed, either by
179 // result of a signal or response to a GetAll() or Get() call. Informs
181 virtual void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
182 const std::string
& property_name
) {
183 VLOG(2) << "Remote GATT descriptor property changed: "
184 << object_path
.value() << ": " << property_name
;
185 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
186 GattDescriptorPropertyChanged(object_path
,
190 // Called when a response for a successful method call is received.
191 void OnSuccess(const base::Closure
& callback
, dbus::Response
* response
) {
196 // Called when a descriptor value response for a successful method call is
198 void OnValueSuccess(const ValueCallback
& callback
, dbus::Response
* response
) {
200 dbus::MessageReader
reader(response
);
202 const uint8
* bytes
= NULL
;
205 if (!reader
.PopArrayOfBytes(&bytes
, &length
))
206 VLOG(2) << "Error reading array of bytes in ValueCallback";
208 std::vector
<uint8
> value
;
211 value
.assign(bytes
, bytes
+ length
);
216 // Called when a response for a failed method call is received.
217 void OnError(const ErrorCallback
& error_callback
,
218 dbus::ErrorResponse
* response
) {
219 // Error response has optional error message argument.
220 std::string error_name
;
221 std::string error_message
;
223 dbus::MessageReader
reader(response
);
224 error_name
= response
->GetErrorName();
225 reader
.PopString(&error_message
);
227 error_name
= kNoResponseError
;
230 error_callback
.Run(error_name
, error_message
);
233 dbus::ObjectManager
* object_manager_
;
235 // List of observers interested in event notifications from us.
236 ObserverList
<BluetoothGattDescriptorClient::Observer
> observers_
;
238 // Weak pointer factory for generating 'this' pointers that might live longer
240 // Note: This should remain the last member so it'll be destroyed and
241 // invalidate its weak pointers before any other members are destroyed.
242 base::WeakPtrFactory
<BluetoothGattDescriptorClientImpl
> weak_ptr_factory_
;
244 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl
);
247 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() {
250 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() {
254 BluetoothGattDescriptorClient
* BluetoothGattDescriptorClient::Create() {
255 return new BluetoothGattDescriptorClientImpl();
258 } // namespace chromeos