Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_service_bluez.cc

Issue 1872943002: Add support for local services/characteristics/descriptors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h" 5 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "device/bluetooth/bluetooth_adapter_bluez.h" 9 #include "device/bluetooth/bluetooth_adapter_bluez.h"
10 #include "device/bluetooth/bluetooth_device_bluez.h" 10 #include "device/bluetooth/bluetooth_device_bluez.h"
11 #include "device/bluetooth/bluetooth_gatt_descriptor_bluez.h"
11 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h" 12 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h"
12 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_bluez.h" 13 #include "device/bluetooth/dbus/bluetooth_gatt_manager_client.h"
13 #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h" 14 #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h"
14 #include "device/bluetooth/dbus/bluez_dbus_manager.h" 15 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
15 16
16 namespace bluez { 17 namespace bluez {
17 18
18 namespace {
19
20 // TODO(jamuraa) move these to cros_system_api later
21 const char kErrorFailed[] = "org.bluez.Error.Failed";
22 const char kErrorInProgress[] = "org.bluez.Error.InProgress";
23 const char kErrorInvalidValueLength[] = "org.bluez.Error.InvalidValueLength";
24 const char kErrorNotAuthorized[] = "org.bluez.Error.NotAuthorized";
25 const char kErrorNotPaired[] = "org.bluez.Error.NotPaired";
26 const char kErrorNotSupported[] = "org.bluez.Error.NotSupported";
27 const char kErrorNotPermitted[] = "org.bluez.Error.NotPermitted";
28
29 } // namespace
30
31 BluetoothRemoteGattServiceBlueZ::BluetoothRemoteGattServiceBlueZ( 19 BluetoothRemoteGattServiceBlueZ::BluetoothRemoteGattServiceBlueZ(
32 BluetoothAdapterBlueZ* adapter, 20 BluetoothAdapterBlueZ* adapter,
33 BluetoothDeviceBlueZ* device, 21 BluetoothDeviceBlueZ* device,
34 const dbus::ObjectPath& object_path) 22 const dbus::ObjectPath& object_path)
35 : object_path_(object_path), 23 : BluetoothGattServiceBlueZ(adapter, object_path),
36 adapter_(adapter),
37 device_(device), 24 device_(device),
38 discovery_complete_(false), 25 discovery_complete_(false),
39 weak_ptr_factory_(this) { 26 weak_ptr_factory_(this) {
40 VLOG(1) << "Creating remote GATT service with identifier: " 27 VLOG(1) << "Creating remote GATT service with identifier: "
41 << object_path.value() << ", UUID: " << GetUUID().canonical_value(); 28 << object_path.value();
42 DCHECK(adapter_); 29 DCHECK(GetAdapter());
43 30
44 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver( 31 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver(
45 this); 32 this);
46 bluez::BluezDBusManager::Get() 33 bluez::BluezDBusManager::Get()
47 ->GetBluetoothGattCharacteristicClient() 34 ->GetBluetoothGattCharacteristicClient()
48 ->AddObserver(this); 35 ->AddObserver(this);
49 36
50 // Add all known GATT characteristics. 37 // Add all known GATT characteristics.
51 const std::vector<dbus::ObjectPath>& gatt_chars = 38 const std::vector<dbus::ObjectPath>& gatt_chars =
52 bluez::BluezDBusManager::Get() 39 bluez::BluezDBusManager::Get()
(...skipping 12 matching lines...) Expand all
65 ->GetBluetoothGattCharacteristicClient() 52 ->GetBluetoothGattCharacteristicClient()
66 ->RemoveObserver(this); 53 ->RemoveObserver(this);
67 54
68 // Clean up all the characteristics. Copy the characteristics list here and 55 // Clean up all the characteristics. Copy the characteristics list here and
69 // clear the original so that when we send GattCharacteristicRemoved(), 56 // clear the original so that when we send GattCharacteristicRemoved(),
70 // GetCharacteristics() returns no characteristics. 57 // GetCharacteristics() returns no characteristics.
71 CharacteristicMap characteristics = characteristics_; 58 CharacteristicMap characteristics = characteristics_;
72 characteristics_.clear(); 59 characteristics_.clear();
73 for (CharacteristicMap::iterator iter = characteristics.begin(); 60 for (CharacteristicMap::iterator iter = characteristics.begin();
74 iter != characteristics.end(); ++iter) { 61 iter != characteristics.end(); ++iter) {
75 DCHECK(adapter_); 62 DCHECK(GetAdapter());
76 adapter_->NotifyGattCharacteristicRemoved(iter->second); 63 GetAdapter()->NotifyGattCharacteristicRemoved(iter->second);
77 64
78 delete iter->second; 65 delete iter->second;
79 } 66 }
80 } 67 }
81 68
82 std::string BluetoothRemoteGattServiceBlueZ::GetIdentifier() const {
83 return object_path_.value();
84 }
85
86 device::BluetoothUUID BluetoothRemoteGattServiceBlueZ::GetUUID() const { 69 device::BluetoothUUID BluetoothRemoteGattServiceBlueZ::GetUUID() const {
87 bluez::BluetoothGattServiceClient::Properties* properties = 70 bluez::BluetoothGattServiceClient::Properties* properties =
88 bluez::BluezDBusManager::Get() 71 bluez::BluezDBusManager::Get()
89 ->GetBluetoothGattServiceClient() 72 ->GetBluetoothGattServiceClient()
90 ->GetProperties(object_path_); 73 ->GetProperties(object_path());
91 DCHECK(properties); 74 DCHECK(properties);
92 return device::BluetoothUUID(properties->uuid.value()); 75 return device::BluetoothUUID(properties->uuid.value());
93 } 76 }
94 77
95 bool BluetoothRemoteGattServiceBlueZ::IsLocal() const { 78 bool BluetoothRemoteGattServiceBlueZ::IsLocal() const {
96 return false; 79 return false;
97 } 80 }
98 81
99 bool BluetoothRemoteGattServiceBlueZ::IsPrimary() const { 82 bool BluetoothRemoteGattServiceBlueZ::IsPrimary() const {
100 bluez::BluetoothGattServiceClient::Properties* properties = 83 bluez::BluetoothGattServiceClient::Properties* properties =
101 bluez::BluezDBusManager::Get() 84 bluez::BluezDBusManager::Get()
102 ->GetBluetoothGattServiceClient() 85 ->GetBluetoothGattServiceClient()
103 ->GetProperties(object_path_); 86 ->GetProperties(object_path());
104 DCHECK(properties); 87 DCHECK(properties);
105 return properties->primary.value(); 88 return properties->primary.value();
106 } 89 }
107 90
108 device::BluetoothDevice* BluetoothRemoteGattServiceBlueZ::GetDevice() const { 91 device::BluetoothDevice* BluetoothRemoteGattServiceBlueZ::GetDevice() const {
109 return device_; 92 return device_;
110 } 93 }
111 94
112 std::vector<device::BluetoothGattCharacteristic*>
113 BluetoothRemoteGattServiceBlueZ::GetCharacteristics() const {
114 std::vector<device::BluetoothGattCharacteristic*> characteristics;
115 for (CharacteristicMap::const_iterator iter = characteristics_.begin();
116 iter != characteristics_.end(); ++iter) {
117 characteristics.push_back(iter->second);
118 }
119 return characteristics;
120 }
121
122 std::vector<device::BluetoothGattService*>
123 BluetoothRemoteGattServiceBlueZ::GetIncludedServices() const {
124 // TODO(armansito): Return the actual included services here.
125 return std::vector<device::BluetoothGattService*>();
126 }
127
128 device::BluetoothGattCharacteristic*
129 BluetoothRemoteGattServiceBlueZ::GetCharacteristic(
130 const std::string& identifier) const {
131 CharacteristicMap::const_iterator iter =
132 characteristics_.find(dbus::ObjectPath(identifier));
133 if (iter == characteristics_.end())
134 return NULL;
135 return iter->second;
136 }
137
138 bool BluetoothRemoteGattServiceBlueZ::AddCharacteristic( 95 bool BluetoothRemoteGattServiceBlueZ::AddCharacteristic(
139 device::BluetoothGattCharacteristic* characteristic) { 96 device::BluetoothGattCharacteristic* characteristic) {
140 VLOG(1) << "Characteristics cannot be added to a remote GATT service."; 97 VLOG(1) << "Characteristics cannot be added to a remote GATT service.";
141 return false; 98 return false;
142 } 99 }
143 100
144 bool BluetoothRemoteGattServiceBlueZ::AddIncludedService( 101 bool BluetoothRemoteGattServiceBlueZ::AddIncludedService(
145 device::BluetoothGattService* service) { 102 device::BluetoothGattService* service) {
146 VLOG(1) << "Included services cannot be added to a remote GATT service."; 103 VLOG(1) << "Included services cannot be added to a remote GATT service.";
147 return false; 104 return false;
148 } 105 }
149 106
150 void BluetoothRemoteGattServiceBlueZ::Register( 107 void BluetoothRemoteGattServiceBlueZ::Register(
151 const base::Closure& callback, 108 const base::Closure& callback,
152 const ErrorCallback& error_callback) { 109 const ErrorCallback& error_callback) {
153 VLOG(1) << "A remote GATT service cannot be registered."; 110 VLOG(1) << "A remote GATT service cannot be registered.";
154 error_callback.Run(); 111 error_callback.Run(GATT_ERROR_NOT_SUPPORTED);
155 } 112 }
156 113
157 void BluetoothRemoteGattServiceBlueZ::Unregister( 114 void BluetoothRemoteGattServiceBlueZ::Unregister(
158 const base::Closure& callback, 115 const base::Closure& callback,
159 const ErrorCallback& error_callback) { 116 const ErrorCallback& error_callback) {
160 VLOG(1) << "A remote GATT service cannot be unregistered."; 117 VLOG(1) << "A remote GATT service cannot be unregistered.";
161 error_callback.Run(); 118 error_callback.Run(GATT_ERROR_NOT_SUPPORTED);
162 }
163
164 // static
165 device::BluetoothGattService::GattErrorCode
166 BluetoothRemoteGattServiceBlueZ::DBusErrorToServiceError(
167 std::string error_name) {
168 device::BluetoothGattService::GattErrorCode code = GATT_ERROR_UNKNOWN;
169 if (error_name == kErrorFailed) {
170 code = GATT_ERROR_FAILED;
171 } else if (error_name == kErrorInProgress) {
172 code = GATT_ERROR_IN_PROGRESS;
173 } else if (error_name == kErrorInvalidValueLength) {
174 code = GATT_ERROR_INVALID_LENGTH;
175 } else if (error_name == kErrorNotPermitted) {
176 code = GATT_ERROR_NOT_PERMITTED;
177 } else if (error_name == kErrorNotAuthorized) {
178 code = GATT_ERROR_NOT_AUTHORIZED;
179 } else if (error_name == kErrorNotPaired) {
180 code = GATT_ERROR_NOT_PAIRED;
181 } else if (error_name == kErrorNotSupported) {
182 code = GATT_ERROR_NOT_SUPPORTED;
183 }
184 return code;
185 }
186
187 BluetoothAdapterBlueZ* BluetoothRemoteGattServiceBlueZ::GetAdapter() const {
188 return adapter_;
189 } 119 }
190 120
191 void BluetoothRemoteGattServiceBlueZ::NotifyServiceChanged() { 121 void BluetoothRemoteGattServiceBlueZ::NotifyServiceChanged() {
192 // Don't send service changed unless we know that all characteristics have 122 // Don't send service changed unless we know that all characteristics have
193 // already been discovered. This is to prevent spammy events before sending 123 // already been discovered. This is to prevent spammy events before sending
194 // out the first Gatt 124 // out the first Gatt
195 if (!discovery_complete_) 125 if (!discovery_complete_)
196 return; 126 return;
197 127
198 DCHECK(adapter_); 128 DCHECK(GetAdapter());
199 adapter_->NotifyGattServiceChanged(this); 129 GetAdapter()->NotifyGattServiceChanged(this);
200 } 130 }
201 131
202 void BluetoothRemoteGattServiceBlueZ::NotifyDescriptorAddedOrRemoved( 132 void BluetoothRemoteGattServiceBlueZ::NotifyDescriptorAddedOrRemoved(
203 BluetoothRemoteGattCharacteristicBlueZ* characteristic, 133 BluetoothRemoteGattCharacteristicBlueZ* characteristic,
204 BluetoothRemoteGattDescriptorBlueZ* descriptor, 134 BluetoothGattDescriptorBlueZ* descriptor,
205 bool added) { 135 bool added) {
206 DCHECK(characteristic->GetService() == this); 136 DCHECK(characteristic->GetService() == this);
207 DCHECK(descriptor->GetCharacteristic() == characteristic); 137 DCHECK(descriptor->GetCharacteristic() == characteristic);
208 DCHECK(adapter_); 138 DCHECK(GetAdapter());
209 139
210 if (added) { 140 if (added) {
211 adapter_->NotifyGattDescriptorAdded(descriptor); 141 GetAdapter()->NotifyGattDescriptorAdded(descriptor);
212 return; 142 return;
213 } 143 }
214 144
215 adapter_->NotifyGattDescriptorRemoved(descriptor); 145 GetAdapter()->NotifyGattDescriptorRemoved(descriptor);
216 } 146 }
217 147
218 void BluetoothRemoteGattServiceBlueZ::NotifyDescriptorValueChanged( 148 void BluetoothRemoteGattServiceBlueZ::NotifyDescriptorValueChanged(
219 BluetoothRemoteGattCharacteristicBlueZ* characteristic, 149 BluetoothRemoteGattCharacteristicBlueZ* characteristic,
220 BluetoothRemoteGattDescriptorBlueZ* descriptor, 150 BluetoothGattDescriptorBlueZ* descriptor,
221 const std::vector<uint8_t>& value) { 151 const std::vector<uint8_t>& value) {
222 DCHECK(characteristic->GetService() == this); 152 DCHECK(characteristic->GetService() == this);
223 DCHECK(descriptor->GetCharacteristic() == characteristic); 153 DCHECK(descriptor->GetCharacteristic() == characteristic);
224 DCHECK(adapter_); 154 DCHECK(GetAdapter());
225 adapter_->NotifyGattDescriptorValueChanged(descriptor, value); 155 GetAdapter()->NotifyGattDescriptorValueChanged(descriptor, value);
226 } 156 }
227 157
228 void BluetoothRemoteGattServiceBlueZ::GattServicePropertyChanged( 158 void BluetoothRemoteGattServiceBlueZ::GattServicePropertyChanged(
229 const dbus::ObjectPath& object_path, 159 const dbus::ObjectPath& object_path,
230 const std::string& property_name) { 160 const std::string& property_name) {
231 if (object_path != object_path_) 161 if (object_path != this->object_path())
232 return; 162 return;
233 163
234 VLOG(1) << "Service property changed: \"" << property_name << "\", " 164 VLOG(1) << "Service property changed: \"" << property_name << "\", "
235 << object_path.value(); 165 << object_path.value();
236 bluez::BluetoothGattServiceClient::Properties* properties = 166 bluez::BluetoothGattServiceClient::Properties* properties =
237 bluez::BluezDBusManager::Get() 167 bluez::BluezDBusManager::Get()
238 ->GetBluetoothGattServiceClient() 168 ->GetBluetoothGattServiceClient()
239 ->GetProperties(object_path); 169 ->GetProperties(object_path);
240 DCHECK(properties); 170 DCHECK(properties);
241 171
242 if (property_name != properties->characteristics.name()) { 172 if (property_name != properties->characteristics.name()) {
243 NotifyServiceChanged(); 173 NotifyServiceChanged();
244 return; 174 return;
245 } 175 }
246 176
247 if (discovery_complete_) 177 if (discovery_complete_)
248 return; 178 return;
249 179
250 VLOG(1) << "All characteristics were discovered for service: " 180 VLOG(1) << "All characteristics were discovered for service: "
251 << object_path.value(); 181 << object_path.value();
252 discovery_complete_ = true; 182 discovery_complete_ = true;
253 DCHECK(adapter_); 183 DCHECK(GetAdapter());
254 adapter_->NotifyGattDiscoveryComplete(this); 184 GetAdapter()->NotifyGattDiscoveryComplete(this);
255 } 185 }
256 186
257 void BluetoothRemoteGattServiceBlueZ::GattCharacteristicAdded( 187 void BluetoothRemoteGattServiceBlueZ::GattCharacteristicAdded(
258 const dbus::ObjectPath& object_path) { 188 const dbus::ObjectPath& object_path) {
259 if (characteristics_.find(object_path) != characteristics_.end()) { 189 if (characteristics_.find(object_path) != characteristics_.end()) {
260 VLOG(1) << "Remote GATT characteristic already exists: " 190 VLOG(1) << "Remote GATT characteristic already exists: "
261 << object_path.value(); 191 << object_path.value();
262 return; 192 return;
263 } 193 }
264 194
265 bluez::BluetoothGattCharacteristicClient::Properties* properties = 195 bluez::BluetoothGattCharacteristicClient::Properties* properties =
266 bluez::BluezDBusManager::Get() 196 bluez::BluezDBusManager::Get()
267 ->GetBluetoothGattCharacteristicClient() 197 ->GetBluetoothGattCharacteristicClient()
268 ->GetProperties(object_path); 198 ->GetProperties(object_path);
269 DCHECK(properties); 199 DCHECK(properties);
270 if (properties->service.value() != object_path_) { 200 if (properties->service.value() != this->object_path()) {
271 VLOG(2) << "Remote GATT characteristic does not belong to this service."; 201 VLOG(2) << "Remote GATT characteristic does not belong to this service.";
272 return; 202 return;
273 } 203 }
274 204
275 VLOG(1) << "Adding new remote GATT characteristic for GATT service: " 205 VLOG(1) << "Adding new remote GATT characteristic for GATT service: "
276 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); 206 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
277 207
278 BluetoothRemoteGattCharacteristicBlueZ* characteristic = 208 BluetoothRemoteGattCharacteristicBlueZ* characteristic =
279 new BluetoothRemoteGattCharacteristicBlueZ(this, object_path); 209 new BluetoothRemoteGattCharacteristicBlueZ(this, object_path);
280 characteristics_[object_path] = characteristic; 210 characteristics_[object_path] = characteristic;
281 DCHECK(characteristic->GetIdentifier() == object_path.value()); 211 DCHECK(characteristic->GetIdentifier() == object_path.value());
282 DCHECK(characteristic->GetUUID().IsValid()); 212 DCHECK(characteristic->GetUUID().IsValid());
283 213
284 DCHECK(adapter_); 214 DCHECK(GetAdapter());
285 adapter_->NotifyGattCharacteristicAdded(characteristic); 215 GetAdapter()->NotifyGattCharacteristicAdded(characteristic);
286 } 216 }
287 217
288 void BluetoothRemoteGattServiceBlueZ::GattCharacteristicRemoved( 218 void BluetoothRemoteGattServiceBlueZ::GattCharacteristicRemoved(
289 const dbus::ObjectPath& object_path) { 219 const dbus::ObjectPath& object_path) {
290 CharacteristicMap::iterator iter = characteristics_.find(object_path); 220 CharacteristicMap::iterator iter = characteristics_.find(object_path);
291 if (iter == characteristics_.end()) { 221 if (iter == characteristics_.end()) {
292 VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value(); 222 VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value();
293 return; 223 return;
294 } 224 }
295 225
296 VLOG(1) << "Removing remote GATT characteristic from service: " 226 VLOG(1) << "Removing remote GATT characteristic from service: "
297 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); 227 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
298 228
299 BluetoothRemoteGattCharacteristicBlueZ* characteristic = iter->second; 229 BluetoothGattCharacteristicBlueZ* characteristic = iter->second;
300 DCHECK(characteristic->object_path() == object_path); 230 DCHECK(characteristic->object_path() == object_path);
301 characteristics_.erase(iter); 231 characteristics_.erase(iter);
302 232
303 DCHECK(adapter_); 233 DCHECK(GetAdapter());
304 adapter_->NotifyGattCharacteristicRemoved(characteristic); 234 GetAdapter()->NotifyGattCharacteristicRemoved(characteristic);
305 235
306 delete characteristic; 236 delete characteristic;
307 } 237 }
308 238
309 void BluetoothRemoteGattServiceBlueZ::GattCharacteristicPropertyChanged( 239 void BluetoothRemoteGattServiceBlueZ::GattCharacteristicPropertyChanged(
310 const dbus::ObjectPath& object_path, 240 const dbus::ObjectPath& object_path,
311 const std::string& property_name) { 241 const std::string& property_name) {
312 CharacteristicMap::iterator iter = characteristics_.find(object_path); 242 CharacteristicMap::iterator iter = characteristics_.find(object_path);
313 if (iter == characteristics_.end()) { 243 if (iter == characteristics_.end()) {
314 VLOG(3) << "Properties of unknown characteristic changed"; 244 VLOG(3) << "Properties of unknown characteristic changed";
315 return; 245 return;
316 } 246 }
317 247
318 // We may receive a property changed event in certain cases, e.g. when the 248 // We may receive a property changed event in certain cases, e.g. when the
319 // characteristic "Flags" property has been updated with values from the 249 // characteristic "Flags" property has been updated with values from the
320 // "Characteristic Extended Properties" descriptor. In this case, kick off 250 // "Characteristic Extended Properties" descriptor. In this case, kick off
321 // a service changed observer event to let observers refresh the 251 // a service changed observer event to let observers refresh the
322 // characteristics. 252 // characteristics.
323 bluez::BluetoothGattCharacteristicClient::Properties* properties = 253 bluez::BluetoothGattCharacteristicClient::Properties* properties =
324 bluez::BluezDBusManager::Get() 254 bluez::BluezDBusManager::Get()
325 ->GetBluetoothGattCharacteristicClient() 255 ->GetBluetoothGattCharacteristicClient()
326 ->GetProperties(object_path); 256 ->GetProperties(object_path);
327 257
328 DCHECK(properties); 258 DCHECK(properties);
329 DCHECK(adapter_); 259 DCHECK(GetAdapter());
330 260
331 if (property_name == properties->flags.name()) 261 if (property_name == properties->flags.name())
332 NotifyServiceChanged(); 262 NotifyServiceChanged();
333 else if (property_name == properties->value.name()) 263 else if (property_name == properties->value.name())
334 adapter_->NotifyGattCharacteristicValueChanged(iter->second, 264 GetAdapter()->NotifyGattCharacteristicValueChanged(
335 properties->value.value()); 265 iter->second, properties->value.value());
336 } 266 }
337 267
338 } // namespace bluez 268 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698