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"
18 // TODO(armansito): Add these to service_constants.h when they are defined
20 const char kDeviceProperty
[] = "Device";
21 const char kPrimaryProperty
[] = "Primary";
25 BluetoothGattServiceClient::Properties::Properties(
26 dbus::ObjectProxy
* object_proxy
,
27 const std::string
& interface_name
,
28 const PropertyChangedCallback
&callback
)
29 : dbus::PropertySet(object_proxy
, interface_name
, callback
) {
30 RegisterProperty(bluetooth_gatt_service::kUUIDProperty
, &uuid
);
31 RegisterProperty(bluetooth_gatt_service::kIncludesProperty
, &includes
);
32 RegisterProperty(kDeviceProperty
, &device
);
33 RegisterProperty(kPrimaryProperty
, &primary
);
36 BluetoothGattServiceClient::Properties::~Properties() {
39 // The BluetoothGattServiceClient implementation used in production.
40 class BluetoothGattServiceClientImpl
: public BluetoothGattServiceClient
,
41 public dbus::ObjectManager::Interface
{
43 BluetoothGattServiceClientImpl()
44 : object_manager_(NULL
),
45 weak_ptr_factory_(this) {
48 virtual ~BluetoothGattServiceClientImpl() {
49 object_manager_
->UnregisterInterface(
50 bluetooth_gatt_service::kBluetoothGattServiceInterface
);
53 // BluetoothGattServiceClientImpl override.
54 virtual void AddObserver(
55 BluetoothGattServiceClient::Observer
* observer
) OVERRIDE
{
57 observers_
.AddObserver(observer
);
60 // BluetoothGattServiceClientImpl override.
61 virtual void RemoveObserver(
62 BluetoothGattServiceClient::Observer
* observer
) OVERRIDE
{
64 observers_
.RemoveObserver(observer
);
67 // BluetoothGattServiceClientImpl override.
68 virtual std::vector
<dbus::ObjectPath
> GetServices() OVERRIDE
{
69 DCHECK(object_manager_
);
70 return object_manager_
->GetObjectsWithInterface(
71 bluetooth_gatt_service::kBluetoothGattServiceInterface
);
74 // BluetoothGattServiceClientImpl override.
75 virtual Properties
* GetProperties(
76 const dbus::ObjectPath
& object_path
) OVERRIDE
{
77 DCHECK(object_manager_
);
78 return static_cast<Properties
*>(
79 object_manager_
->GetProperties(
81 bluetooth_gatt_service::kBluetoothGattServiceInterface
));
84 // dbus::ObjectManager::Interface override.
85 virtual dbus::PropertySet
* CreateProperties(
86 dbus::ObjectProxy
*object_proxy
,
87 const dbus::ObjectPath
& object_path
,
88 const std::string
& interface_name
) OVERRIDE
{
89 Properties
* properties
= new Properties(
92 base::Bind(&BluetoothGattServiceClientImpl::OnPropertyChanged
,
93 weak_ptr_factory_
.GetWeakPtr(),
95 return static_cast<dbus::PropertySet
*>(properties
);
98 // dbus::ObjectManager::Interface override.
99 virtual void ObjectAdded(const dbus::ObjectPath
& object_path
,
100 const std::string
& interface_name
) OVERRIDE
{
101 VLOG(2) << "Remote GATT service added: " << object_path
.value();
102 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
103 GattServiceAdded(object_path
));
106 // dbus::ObjectManager::Interface override.
107 virtual void ObjectRemoved(const dbus::ObjectPath
& object_path
,
108 const std::string
& interface_name
) OVERRIDE
{
109 VLOG(2) << "Remote GATT service removed: " << object_path
.value();
110 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
111 GattServiceRemoved(object_path
));
115 // chromeos::DBusClient override.
116 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
117 object_manager_
= bus
->GetObjectManager(
118 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
120 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
121 object_manager_
->RegisterInterface(
122 bluetooth_gatt_service::kBluetoothGattServiceInterface
, this);
126 // Called by dbus::PropertySet when a property value is changed, either by
127 // result of a signal or response to a GetAll() or Get() call. Informs
129 virtual void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
130 const std::string
& property_name
) {
131 VLOG(2) << "Remote GATT service property changed: " << object_path
.value()
132 << ": " << property_name
;
133 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
134 GattServicePropertyChanged(object_path
, property_name
));
137 dbus::ObjectManager
* object_manager_
;
139 // List of observers interested in event notifications from us.
140 ObserverList
<BluetoothGattServiceClient::Observer
> observers_
;
142 // Weak pointer factory for generating 'this' pointers that might live longer
144 // Note: This should remain the last member so it'll be destroyed and
145 // invalidate its weak pointers before any other members are destroyed.
146 base::WeakPtrFactory
<BluetoothGattServiceClientImpl
> weak_ptr_factory_
;
148 DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceClientImpl
);
151 BluetoothGattServiceClient::BluetoothGattServiceClient() {
154 BluetoothGattServiceClient::~BluetoothGattServiceClient() {
158 BluetoothGattServiceClient
* BluetoothGattServiceClient::Create() {
159 return new BluetoothGattServiceClientImpl();
162 } // namespace chromeos