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"
18 // TODO(armansito): Move this constant to cros_system_api.
19 const char kValueProperty
[] = "Value";
24 const char BluetoothGattDescriptorClient::kNoResponseError
[] =
25 "org.chromium.Error.NoResponse";
27 const char BluetoothGattDescriptorClient::kUnknownDescriptorError
[] =
28 "org.chromium.Error.UnknownDescriptor";
30 BluetoothGattDescriptorClient::Properties::Properties(
31 dbus::ObjectProxy
* object_proxy
,
32 const std::string
& interface_name
,
33 const PropertyChangedCallback
&callback
)
34 : dbus::PropertySet(object_proxy
, interface_name
, callback
) {
35 RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty
, &uuid
);
36 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty
,
38 RegisterProperty(kValueProperty
, &value
);
41 BluetoothGattDescriptorClient::Properties::~Properties() {
44 // The BluetoothGattDescriptorClient implementation used in production.
45 class BluetoothGattDescriptorClientImpl
46 : public BluetoothGattDescriptorClient
,
47 public dbus::ObjectManager::Interface
{
49 BluetoothGattDescriptorClientImpl()
50 : object_manager_(NULL
),
51 weak_ptr_factory_(this) {
54 ~BluetoothGattDescriptorClientImpl() override
{
55 object_manager_
->UnregisterInterface(
56 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
);
59 // BluetoothGattDescriptorClientImpl override.
60 void AddObserver(BluetoothGattDescriptorClient::Observer
* observer
) override
{
62 observers_
.AddObserver(observer
);
65 // BluetoothGattDescriptorClientImpl override.
67 BluetoothGattDescriptorClient::Observer
* observer
) override
{
69 observers_
.RemoveObserver(observer
);
72 // BluetoothGattDescriptorClientImpl override.
73 std::vector
<dbus::ObjectPath
> GetDescriptors() override
{
74 DCHECK(object_manager_
);
75 return object_manager_
->GetObjectsWithInterface(
76 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
);
79 // BluetoothGattDescriptorClientImpl override.
80 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
{
81 DCHECK(object_manager_
);
82 return static_cast<Properties
*>(
83 object_manager_
->GetProperties(
85 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
));
88 // BluetoothGattDescriptorClientImpl override.
89 void ReadValue(const dbus::ObjectPath
& object_path
,
90 const ValueCallback
& callback
,
91 const ErrorCallback
& error_callback
) override
{
92 dbus::ObjectProxy
* object_proxy
=
93 object_manager_
->GetObjectProxy(object_path
);
95 error_callback
.Run(kUnknownDescriptorError
, "");
99 dbus::MethodCall
method_call(
100 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
101 bluetooth_gatt_descriptor::kReadValue
);
103 object_proxy
->CallMethodWithErrorCallback(
105 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
106 base::Bind(&BluetoothGattDescriptorClientImpl::OnValueSuccess
,
107 weak_ptr_factory_
.GetWeakPtr(),
109 base::Bind(&BluetoothGattDescriptorClientImpl::OnError
,
110 weak_ptr_factory_
.GetWeakPtr(),
114 // BluetoothGattDescriptorClientImpl override.
115 void WriteValue(const dbus::ObjectPath
& object_path
,
116 const std::vector
<uint8
>& value
,
117 const base::Closure
& callback
,
118 const ErrorCallback
& error_callback
) override
{
119 dbus::ObjectProxy
* object_proxy
=
120 object_manager_
->GetObjectProxy(object_path
);
122 error_callback
.Run(kUnknownDescriptorError
, "");
126 dbus::MethodCall
method_call(
127 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
128 bluetooth_gatt_descriptor::kWriteValue
);
129 dbus::MessageWriter
writer(&method_call
);
130 writer
.AppendArrayOfBytes(value
.data(), value
.size());
132 object_proxy
->CallMethodWithErrorCallback(
134 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
135 base::Bind(&BluetoothGattDescriptorClientImpl::OnSuccess
,
136 weak_ptr_factory_
.GetWeakPtr(),
138 base::Bind(&BluetoothGattDescriptorClientImpl::OnError
,
139 weak_ptr_factory_
.GetWeakPtr(),
143 // dbus::ObjectManager::Interface override.
144 dbus::PropertySet
* CreateProperties(
145 dbus::ObjectProxy
* object_proxy
,
146 const dbus::ObjectPath
& object_path
,
147 const std::string
& interface_name
) override
{
148 Properties
* properties
= new Properties(
151 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged
,
152 weak_ptr_factory_
.GetWeakPtr(),
154 return static_cast<dbus::PropertySet
*>(properties
);
157 // dbus::ObjectManager::Interface override.
158 void ObjectAdded(const dbus::ObjectPath
& object_path
,
159 const std::string
& interface_name
) override
{
160 VLOG(2) << "Remote GATT descriptor added: " << object_path
.value();
161 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
162 GattDescriptorAdded(object_path
));
165 // dbus::ObjectManager::Interface override.
166 void ObjectRemoved(const dbus::ObjectPath
& object_path
,
167 const std::string
& interface_name
) override
{
168 VLOG(2) << "Remote GATT descriptor removed: " << object_path
.value();
169 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
170 GattDescriptorRemoved(object_path
));
174 // chromeos::DBusClient override.
175 void Init(dbus::Bus
* bus
) override
{
176 object_manager_
= bus
->GetObjectManager(
177 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
179 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
180 object_manager_
->RegisterInterface(
181 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
186 // Called by dbus::PropertySet when a property value is changed, either by
187 // result of a signal or response to a GetAll() or Get() call. Informs
189 virtual void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
190 const std::string
& property_name
) {
191 VLOG(2) << "Remote GATT descriptor property changed: "
192 << object_path
.value() << ": " << property_name
;
193 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
194 GattDescriptorPropertyChanged(object_path
,
198 // Called when a response for a successful method call is received.
199 void OnSuccess(const base::Closure
& callback
, dbus::Response
* response
) {
204 // Called when a descriptor value response for a successful method call is
206 void OnValueSuccess(const ValueCallback
& callback
, dbus::Response
* response
) {
208 dbus::MessageReader
reader(response
);
210 const uint8
* bytes
= NULL
;
213 if (!reader
.PopArrayOfBytes(&bytes
, &length
))
214 VLOG(2) << "Error reading array of bytes in ValueCallback";
216 std::vector
<uint8
> value
;
219 value
.assign(bytes
, bytes
+ length
);
224 // Called when a response for a failed method call is received.
225 void OnError(const ErrorCallback
& error_callback
,
226 dbus::ErrorResponse
* response
) {
227 // Error response has optional error message argument.
228 std::string error_name
;
229 std::string error_message
;
231 dbus::MessageReader
reader(response
);
232 error_name
= response
->GetErrorName();
233 reader
.PopString(&error_message
);
235 error_name
= kNoResponseError
;
238 error_callback
.Run(error_name
, error_message
);
241 dbus::ObjectManager
* object_manager_
;
243 // List of observers interested in event notifications from us.
244 ObserverList
<BluetoothGattDescriptorClient::Observer
> observers_
;
246 // Weak pointer factory for generating 'this' pointers that might live longer
248 // Note: This should remain the last member so it'll be destroyed and
249 // invalidate its weak pointers before any other members are destroyed.
250 base::WeakPtrFactory
<BluetoothGattDescriptorClientImpl
> weak_ptr_factory_
;
252 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl
);
255 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() {
258 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() {
262 BluetoothGattDescriptorClient
* BluetoothGattDescriptorClient::Create() {
263 return new BluetoothGattDescriptorClientImpl();
266 } // namespace chromeos