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"
16 BluetoothGattDescriptorClient::Properties::Properties(
17 dbus::ObjectProxy
* object_proxy
,
18 const std::string
& interface_name
,
19 const PropertyChangedCallback
&callback
)
20 : dbus::PropertySet(object_proxy
, interface_name
, callback
) {
21 RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty
, &uuid
);
22 RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty
,
24 RegisterProperty(bluetooth_gatt_descriptor::kValueProperty
, &value
);
27 BluetoothGattDescriptorClient::Properties::~Properties() {
30 // The BluetoothGattDescriptorClient implementation used in production.
31 class BluetoothGattDescriptorClientImpl
32 : public BluetoothGattDescriptorClient
,
33 public dbus::ObjectManager::Interface
{
35 BluetoothGattDescriptorClientImpl()
36 : object_manager_(NULL
),
37 weak_ptr_factory_(this) {
40 virtual ~BluetoothGattDescriptorClientImpl() {
41 object_manager_
->UnregisterInterface(
42 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
);
45 // BluetoothGattDescriptorClientImpl override.
46 virtual void AddObserver(
47 BluetoothGattDescriptorClient::Observer
* observer
) OVERRIDE
{
49 observers_
.AddObserver(observer
);
52 // BluetoothGattDescriptorClientImpl override.
53 virtual void RemoveObserver(
54 BluetoothGattDescriptorClient::Observer
* observer
) OVERRIDE
{
56 observers_
.RemoveObserver(observer
);
59 // BluetoothGattDescriptorClientImpl override.
60 virtual std::vector
<dbus::ObjectPath
> GetDescriptors() OVERRIDE
{
61 DCHECK(object_manager_
);
62 return object_manager_
->GetObjectsWithInterface(
63 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
);
66 // BluetoothGattDescriptorClientImpl override.
67 virtual Properties
* GetProperties(
68 const dbus::ObjectPath
& object_path
) OVERRIDE
{
69 DCHECK(object_manager_
);
70 return static_cast<Properties
*>(
71 object_manager_
->GetProperties(
73 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
));
76 // dbus::ObjectManager::Interface override.
77 virtual dbus::PropertySet
* CreateProperties(
78 dbus::ObjectProxy
*object_proxy
,
79 const dbus::ObjectPath
& object_path
,
80 const std::string
& interface_name
) OVERRIDE
{
81 Properties
* properties
= new Properties(
84 base::Bind(&BluetoothGattDescriptorClientImpl::OnPropertyChanged
,
85 weak_ptr_factory_
.GetWeakPtr(),
87 return static_cast<dbus::PropertySet
*>(properties
);
90 // dbus::ObjectManager::Interface override.
91 virtual void ObjectAdded(const dbus::ObjectPath
& object_path
,
92 const std::string
& interface_name
) OVERRIDE
{
93 VLOG(2) << "Remote GATT descriptor added: " << object_path
.value();
94 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
95 GattDescriptorAdded(object_path
));
98 // dbus::ObjectManager::Interface override.
99 virtual void ObjectRemoved(const dbus::ObjectPath
& object_path
,
100 const std::string
& interface_name
) OVERRIDE
{
101 VLOG(2) << "Remote GATT descriptor removed: " << object_path
.value();
102 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
103 GattDescriptorRemoved(object_path
));
107 // chromeos::DBusClient override.
108 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
109 object_manager_
= bus
->GetObjectManager(
110 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
112 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
113 object_manager_
->RegisterInterface(
114 bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface
,
119 // Called by dbus::PropertySet when a property value is changed, either by
120 // result of a signal or response to a GetAll() or Get() call. Informs
122 virtual void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
123 const std::string
& property_name
) {
124 VLOG(2) << "Remote GATT descriptor property changed: "
125 << object_path
.value() << ": " << property_name
;
126 FOR_EACH_OBSERVER(BluetoothGattDescriptorClient::Observer
, observers_
,
127 GattDescriptorPropertyChanged(object_path
,
131 dbus::ObjectManager
* object_manager_
;
133 // List of observers interested in event notifications from us.
134 ObserverList
<BluetoothGattDescriptorClient::Observer
> observers_
;
136 // Weak pointer factory for generating 'this' pointers that might live longer
138 // Note: This should remain the last member so it'll be destroyed and
139 // invalidate its weak pointers before any other members are destroyed.
140 base::WeakPtrFactory
<BluetoothGattDescriptorClientImpl
> weak_ptr_factory_
;
142 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorClientImpl
);
145 BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() {
148 BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() {
152 BluetoothGattDescriptorClient
* BluetoothGattDescriptorClient::Create() {
153 return new BluetoothGattDescriptorClientImpl();
156 } // namespace chromeos