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_characteristic_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 BluetoothGattCharacteristicClient::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_characteristic::kUUIDProperty
, &uuid
);
22 RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty
, &service
);
23 RegisterProperty(bluetooth_gatt_characteristic::kValueProperty
, &value
);
24 RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty
, &flags
);
27 BluetoothGattCharacteristicClient::Properties::~Properties() {
30 // The BluetoothGattCharacteristicClient implementation used in production.
31 class BluetoothGattCharacteristicClientImpl
32 : public BluetoothGattCharacteristicClient
,
33 public dbus::ObjectManager::Interface
{
35 BluetoothGattCharacteristicClientImpl()
36 : object_manager_(NULL
),
37 weak_ptr_factory_(this) {
40 virtual ~BluetoothGattCharacteristicClientImpl() {
41 object_manager_
->UnregisterInterface(
42 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface
);
45 // BluetoothGattCharacteristicClient override.
46 virtual void AddObserver(
47 BluetoothGattCharacteristicClient::Observer
* observer
) OVERRIDE
{
49 observers_
.AddObserver(observer
);
52 // BluetoothGattCharacteristicClient override.
53 virtual void RemoveObserver(
54 BluetoothGattCharacteristicClient::Observer
* observer
) OVERRIDE
{
56 observers_
.RemoveObserver(observer
);
59 // BluetoothGattCharacteristicClient override.
60 virtual std::vector
<dbus::ObjectPath
> GetCharacteristics() OVERRIDE
{
61 DCHECK(object_manager_
);
62 return object_manager_
->GetObjectsWithInterface(
63 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface
);
66 // BluetoothGattCharacteristicClient 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_characteristic::
74 kBluetoothGattCharacteristicInterface
));
77 // dbus::ObjectManager::Interface override.
78 virtual dbus::PropertySet
* CreateProperties(
79 dbus::ObjectProxy
*object_proxy
,
80 const dbus::ObjectPath
& object_path
,
81 const std::string
& interface_name
) OVERRIDE
{
82 Properties
* properties
= new Properties(
85 base::Bind(&BluetoothGattCharacteristicClientImpl::OnPropertyChanged
,
86 weak_ptr_factory_
.GetWeakPtr(),
88 return static_cast<dbus::PropertySet
*>(properties
);
91 // dbus::ObjectManager::Interface override.
92 virtual void ObjectAdded(const dbus::ObjectPath
& object_path
,
93 const std::string
& interface_name
) OVERRIDE
{
94 VLOG(2) << "Remote GATT characteristic added: " << object_path
.value();
95 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer
, observers_
,
96 GattCharacteristicAdded(object_path
));
99 // dbus::ObjectManager::Interface override.
100 virtual void ObjectRemoved(const dbus::ObjectPath
& object_path
,
101 const std::string
& interface_name
) OVERRIDE
{
102 VLOG(2) << "Remote GATT characteristic removed: " << object_path
.value();
103 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer
, observers_
,
104 GattCharacteristicRemoved(object_path
));
108 // chromeos::DBusClient override.
109 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
110 object_manager_
= bus
->GetObjectManager(
111 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
113 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
114 object_manager_
->RegisterInterface(
115 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface
,
120 // Called by dbus::PropertySet when a property value is changed, either by
121 // result of a signal or response to a GetAll() or Get() call. Informs
123 virtual void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
124 const std::string
& property_name
) {
125 VLOG(2) << "Remote GATT characteristic property changed: "
126 << object_path
.value() << ": " << property_name
;
127 FOR_EACH_OBSERVER(BluetoothGattCharacteristicClient::Observer
, observers_
,
128 GattCharacteristicPropertyChanged(object_path
,
132 dbus::ObjectManager
* object_manager_
;
134 // List of observers interested in event notifications from us.
135 ObserverList
<BluetoothGattCharacteristicClient::Observer
> observers_
;
137 // Weak pointer factory for generating 'this' pointers that might live longer
139 // Note: This should remain the last member so it'll be destroyed and
140 // invalidate its weak pointers before any other members are destroyed.
141 base::WeakPtrFactory
<BluetoothGattCharacteristicClientImpl
>
144 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClientImpl
);
147 BluetoothGattCharacteristicClient::BluetoothGattCharacteristicClient() {
150 BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() {
154 BluetoothGattCharacteristicClient
* BluetoothGattCharacteristicClient::Create() {
155 return new BluetoothGattCharacteristicClientImpl();
158 } // namespace chromeos