OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chromeos/dbus/bluetooth_gatt_service_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/observer_list.h" | |
10 #include "dbus/bus.h" | |
11 #include "dbus/object_manager.h" | |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
13 | |
14 namespace chromeos { | |
15 | |
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, | |
26 &characteristics); | |
27 } | |
28 | |
29 BluetoothGattServiceClient::Properties::~Properties() { | |
30 } | |
31 | |
32 // The BluetoothGattServiceClient implementation used in production. | |
33 class BluetoothGattServiceClientImpl : public BluetoothGattServiceClient, | |
34 public dbus::ObjectManager::Interface { | |
35 public: | |
36 BluetoothGattServiceClientImpl() | |
37 : object_manager_(NULL), | |
38 weak_ptr_factory_(this) { | |
39 } | |
40 | |
41 ~BluetoothGattServiceClientImpl() override { | |
42 object_manager_->UnregisterInterface( | |
43 bluetooth_gatt_service::kBluetoothGattServiceInterface); | |
44 } | |
45 | |
46 // BluetoothGattServiceClientImpl override. | |
47 void AddObserver(BluetoothGattServiceClient::Observer* observer) override { | |
48 DCHECK(observer); | |
49 observers_.AddObserver(observer); | |
50 } | |
51 | |
52 // BluetoothGattServiceClientImpl override. | |
53 void RemoveObserver(BluetoothGattServiceClient::Observer* observer) override { | |
54 DCHECK(observer); | |
55 observers_.RemoveObserver(observer); | |
56 } | |
57 | |
58 // BluetoothGattServiceClientImpl override. | |
59 std::vector<dbus::ObjectPath> GetServices() override { | |
60 DCHECK(object_manager_); | |
61 return object_manager_->GetObjectsWithInterface( | |
62 bluetooth_gatt_service::kBluetoothGattServiceInterface); | |
63 } | |
64 | |
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( | |
70 object_path, | |
71 bluetooth_gatt_service::kBluetoothGattServiceInterface)); | |
72 } | |
73 | |
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( | |
80 object_proxy, | |
81 interface_name, | |
82 base::Bind(&BluetoothGattServiceClientImpl::OnPropertyChanged, | |
83 weak_ptr_factory_.GetWeakPtr(), | |
84 object_path)); | |
85 return static_cast<dbus::PropertySet*>(properties); | |
86 } | |
87 | |
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)); | |
94 } | |
95 | |
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)); | |
102 } | |
103 | |
104 protected: | |
105 // chromeos::DBusClient override. | |
106 void Init(dbus::Bus* bus) override { | |
107 object_manager_ = bus->GetObjectManager( | |
108 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | |
109 dbus::ObjectPath( | |
110 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | |
111 object_manager_->RegisterInterface( | |
112 bluetooth_gatt_service::kBluetoothGattServiceInterface, this); | |
113 } | |
114 | |
115 private: | |
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 | |
118 // observers. | |
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)); | |
125 } | |
126 | |
127 dbus::ObjectManager* object_manager_; | |
128 | |
129 // List of observers interested in event notifications from us. | |
130 base::ObserverList<BluetoothGattServiceClient::Observer> observers_; | |
131 | |
132 // Weak pointer factory for generating 'this' pointers that might live longer | |
133 // than we do. | |
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_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceClientImpl); | |
139 }; | |
140 | |
141 BluetoothGattServiceClient::BluetoothGattServiceClient() { | |
142 } | |
143 | |
144 BluetoothGattServiceClient::~BluetoothGattServiceClient() { | |
145 } | |
146 | |
147 // static | |
148 BluetoothGattServiceClient* BluetoothGattServiceClient::Create() { | |
149 return new BluetoothGattServiceClientImpl(); | |
150 } | |
151 | |
152 } // namespace chromeos | |
OLD | NEW |