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 ~BluetoothGattServiceClientImpl() override
{
42 object_manager_
->UnregisterInterface(
43 bluetooth_gatt_service::kBluetoothGattServiceInterface
);
46 // BluetoothGattServiceClientImpl override.
47 void AddObserver(BluetoothGattServiceClient::Observer
* observer
) override
{
49 observers_
.AddObserver(observer
);
52 // BluetoothGattServiceClientImpl override.
53 void RemoveObserver(BluetoothGattServiceClient::Observer
* observer
) override
{
55 observers_
.RemoveObserver(observer
);
58 // BluetoothGattServiceClientImpl override.
59 std::vector
<dbus::ObjectPath
> GetServices() override
{
60 DCHECK(object_manager_
);
61 return object_manager_
->GetObjectsWithInterface(
62 bluetooth_gatt_service::kBluetoothGattServiceInterface
);
65 // BluetoothGattServiceClientImpl override.
66 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
{
67 DCHECK(object_manager_
);
68 return static_cast<Properties
*>(
69 object_manager_
->GetProperties(
71 bluetooth_gatt_service::kBluetoothGattServiceInterface
));
74 // dbus::ObjectManager::Interface override.
75 dbus::PropertySet
* CreateProperties(
76 dbus::ObjectProxy
* object_proxy
,
77 const dbus::ObjectPath
& object_path
,
78 const std::string
& interface_name
) override
{
79 Properties
* properties
= new Properties(
82 base::Bind(&BluetoothGattServiceClientImpl::OnPropertyChanged
,
83 weak_ptr_factory_
.GetWeakPtr(),
85 return static_cast<dbus::PropertySet
*>(properties
);
88 // dbus::ObjectManager::Interface override.
89 void ObjectAdded(const dbus::ObjectPath
& object_path
,
90 const std::string
& interface_name
) override
{
91 VLOG(2) << "Remote GATT service added: " << object_path
.value();
92 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
93 GattServiceAdded(object_path
));
96 // dbus::ObjectManager::Interface override.
97 void ObjectRemoved(const dbus::ObjectPath
& object_path
,
98 const std::string
& interface_name
) override
{
99 VLOG(2) << "Remote GATT service removed: " << object_path
.value();
100 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
101 GattServiceRemoved(object_path
));
105 // chromeos::DBusClient override.
106 void Init(dbus::Bus
* bus
) override
{
107 object_manager_
= bus
->GetObjectManager(
108 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
110 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
111 object_manager_
->RegisterInterface(
112 bluetooth_gatt_service::kBluetoothGattServiceInterface
, this);
116 // Called by dbus::PropertySet when a property value is changed, either by
117 // result of a signal or response to a GetAll() or Get() call. Informs
119 virtual void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
120 const std::string
& property_name
) {
121 VLOG(2) << "Remote GATT service property changed: " << object_path
.value()
122 << ": " << property_name
;
123 FOR_EACH_OBSERVER(BluetoothGattServiceClient::Observer
, observers_
,
124 GattServicePropertyChanged(object_path
, property_name
));
127 dbus::ObjectManager
* object_manager_
;
129 // List of observers interested in event notifications from us.
130 base::ObserverList
<BluetoothGattServiceClient::Observer
> observers_
;
132 // Weak pointer factory for generating 'this' pointers that might live longer
134 // Note: This should remain the last member so it'll be destroyed and
135 // invalidate its weak pointers before any other members are destroyed.
136 base::WeakPtrFactory
<BluetoothGattServiceClientImpl
> weak_ptr_factory_
;
138 DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceClientImpl
);
141 BluetoothGattServiceClient::BluetoothGattServiceClient() {
144 BluetoothGattServiceClient::~BluetoothGattServiceClient() {
148 BluetoothGattServiceClient
* BluetoothGattServiceClient::Create() {
149 return new BluetoothGattServiceClientImpl();
152 } // namespace chromeos