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_service_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 BluetoothGattServiceClient::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_service::kUUIDProperty
, &uuid
);
22 RegisterProperty(bluetooth_gatt_service::kIncludesProperty
, &includes
);
23 RegisterProperty(bluetooth_gatt_service::kDeviceProperty
, &device
);
24 RegisterProperty(bluetooth_gatt_service::kPrimaryProperty
, &primary
);
25 RegisterProperty(bluetooth_gatt_service::kCharacteristicsProperty
,
29 BluetoothGattServiceClient::Properties::~Properties() {
32 // The BluetoothGattServiceClient implementation used in production.
33 class BluetoothGattServiceClientImpl
: public BluetoothGattServiceClient
,
34 public dbus::ObjectManager::Interface
{
36 BluetoothGattServiceClientImpl()
37 : object_manager_(NULL
),
38 weak_ptr_factory_(this) {
41 virtual ~BluetoothGattServiceClientImpl() {
42 object_manager_
->UnregisterInterface(
43 bluetooth_gatt_service::kBluetoothGattServiceInterface
);
46 // BluetoothGattServiceClientImpl override.
47 virtual void AddObserver(
48 BluetoothGattServiceClient::Observer
* observer
) OVERRIDE
{
50 observers_
.AddObserver(observer
);
53 // BluetoothGattServiceClientImpl override.
54 virtual void RemoveObserver(
55 BluetoothGattServiceClient::Observer
* observer
) OVERRIDE
{
57 observers_
.RemoveObserver(observer
);
60 // BluetoothGattServiceClientImpl override.
61 virtual std::vector
<dbus::ObjectPath
> GetServices() OVERRIDE
{
62 DCHECK(object_manager_
);
63 return object_manager_
->GetObjectsWithInterface(
64 bluetooth_gatt_service::kBluetoothGattServiceInterface
);
67 // BluetoothGattServiceClientImpl override.
68 virtual Properties
* GetProperties(
69 const dbus::ObjectPath
& object_path
) OVERRIDE
{
70 DCHECK(object_manager_
);
71 return static_cast<Properties
*>(
72 object_manager_
->GetProperties(
74 bluetooth_gatt_service::kBluetoothGattServiceInterface
));
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(&BluetoothGattServiceClientImpl::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 service added: " << object_path
.value();
95 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
96 GattServiceAdded(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 service removed: " << object_path
.value();
103 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
104 GattServiceRemoved(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_service::kBluetoothGattServiceInterface
, this);
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 service property changed: " << object_path
.value()
125 << ": " << property_name
;
126 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
127 GattServicePropertyChanged(object_path
, property_name
));
130 dbus::ObjectManager
* object_manager_
;
132 // List of observers interested in event notifications from us.
133 ObserverList
<BluetoothGattServiceClient::Observer
> observers_
;
135 // Weak pointer factory for generating 'this' pointers that might live longer
137 // Note: This should remain the last member so it'll be destroyed and
138 // invalidate its weak pointers before any other members are destroyed.
139 base::WeakPtrFactory
<BluetoothGattServiceClientImpl
> weak_ptr_factory_
;
141 DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceClientImpl
);
144 BluetoothGattServiceClient::BluetoothGattServiceClient() {
147 BluetoothGattServiceClient::~BluetoothGattServiceClient() {
151 BluetoothGattServiceClient
* BluetoothGattServiceClient::Create() {
152 return new BluetoothGattServiceClientImpl();
155 } // namespace chromeos