OLD | NEW |
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_chromeos.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.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 "chromeos/dbus/bluetooth_gatt_service_client.h" | 9 #include "chromeos/dbus/bluetooth_gatt_service_client.h" |
10 #include "chromeos/dbus/dbus_thread_manager.h" | 10 #include "chromeos/dbus/dbus_thread_manager.h" |
11 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 11 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
12 #include "device/bluetooth/bluetooth_device_chromeos.h" | 12 #include "device/bluetooth/bluetooth_device_chromeos.h" |
13 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" | 13 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
14 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" |
15 | 15 |
16 namespace chromeos { | 16 namespace chromeos { |
17 | 17 |
18 BluetoothRemoteGattServiceChromeOS::BluetoothRemoteGattServiceChromeOS( | 18 BluetoothRemoteGattServiceChromeOS::BluetoothRemoteGattServiceChromeOS( |
19 BluetoothAdapterChromeOS* adapter, | 19 BluetoothAdapterChromeOS* adapter, |
20 BluetoothDeviceChromeOS* device, | 20 BluetoothDeviceChromeOS* device, |
21 const dbus::ObjectPath& object_path) | 21 const dbus::ObjectPath& object_path) |
22 : object_path_(object_path), | 22 : object_path_(object_path), |
23 adapter_(adapter), | 23 adapter_(adapter), |
24 device_(device), | 24 device_(device), |
| 25 discovery_complete_(false), |
25 weak_ptr_factory_(this) { | 26 weak_ptr_factory_(this) { |
26 VLOG(1) << "Creating remote GATT service with identifier: " | 27 VLOG(1) << "Creating remote GATT service with identifier: " |
27 << object_path.value() << ", UUID: " << GetUUID().canonical_value(); | 28 << object_path.value() << ", UUID: " << GetUUID().canonical_value(); |
28 DCHECK(adapter_); | 29 DCHECK(adapter_); |
29 | 30 |
30 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this); | 31 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this); |
31 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 32 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
32 AddObserver(this); | 33 AddObserver(this); |
33 | 34 |
34 // Add all known GATT characteristics. | 35 // Add all known GATT characteristics. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 VLOG(1) << "A remote GATT service cannot be unregistered."; | 151 VLOG(1) << "A remote GATT service cannot be unregistered."; |
151 error_callback.Run(); | 152 error_callback.Run(); |
152 } | 153 } |
153 | 154 |
154 scoped_refptr<device::BluetoothAdapter> | 155 scoped_refptr<device::BluetoothAdapter> |
155 BluetoothRemoteGattServiceChromeOS::GetAdapter() const { | 156 BluetoothRemoteGattServiceChromeOS::GetAdapter() const { |
156 return adapter_; | 157 return adapter_; |
157 } | 158 } |
158 | 159 |
159 void BluetoothRemoteGattServiceChromeOS::NotifyServiceChanged() { | 160 void BluetoothRemoteGattServiceChromeOS::NotifyServiceChanged() { |
| 161 // Don't send service changed unless we know that all characteristics have |
| 162 // already been discovered. This is to prevent spammy events before sending |
| 163 // out the first Gatt |
| 164 if (!discovery_complete_) |
| 165 return; |
| 166 |
160 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 167 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
161 GattServiceChanged(this)); | 168 GattServiceChanged(this)); |
162 } | 169 } |
163 | 170 |
164 void BluetoothRemoteGattServiceChromeOS::NotifyCharacteristicValueChanged( | 171 void BluetoothRemoteGattServiceChromeOS::NotifyCharacteristicValueChanged( |
165 BluetoothRemoteGattCharacteristicChromeOS* characteristic, | 172 BluetoothRemoteGattCharacteristicChromeOS* characteristic, |
166 const std::vector<uint8>& value) { | 173 const std::vector<uint8>& value) { |
167 DCHECK(characteristic->GetService() == this); | 174 DCHECK(characteristic->GetService() == this); |
168 FOR_EACH_OBSERVER( | 175 FOR_EACH_OBSERVER( |
169 device::BluetoothGattService::Observer, | 176 device::BluetoothGattService::Observer, |
(...skipping 26 matching lines...) Expand all Loading... |
196 device::BluetoothGattService::Observer, observers_, | 203 device::BluetoothGattService::Observer, observers_, |
197 GattDescriptorValueChanged(characteristic, descriptor, value)); | 204 GattDescriptorValueChanged(characteristic, descriptor, value)); |
198 } | 205 } |
199 | 206 |
200 void BluetoothRemoteGattServiceChromeOS::GattServicePropertyChanged( | 207 void BluetoothRemoteGattServiceChromeOS::GattServicePropertyChanged( |
201 const dbus::ObjectPath& object_path, | 208 const dbus::ObjectPath& object_path, |
202 const std::string& property_name){ | 209 const std::string& property_name){ |
203 if (object_path != object_path_) | 210 if (object_path != object_path_) |
204 return; | 211 return; |
205 | 212 |
206 VLOG(1) << "Service property changed: " << object_path.value(); | 213 VLOG(1) << "Service property changed: \"" << property_name << "\", " |
207 NotifyServiceChanged(); | 214 << object_path.value(); |
| 215 BluetoothGattServiceClient::Properties* properties = |
| 216 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->GetProperties( |
| 217 object_path); |
| 218 DCHECK(properties); |
| 219 |
| 220 if (property_name != properties->characteristics.name()) { |
| 221 NotifyServiceChanged(); |
| 222 return; |
| 223 } |
| 224 |
| 225 if (discovery_complete_) |
| 226 return; |
| 227 |
| 228 VLOG(1) << "All characteristics were discovered for service: " |
| 229 << object_path.value(); |
| 230 discovery_complete_ = true; |
| 231 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, |
| 232 observers_, |
| 233 GattDiscoveryCompleteForService(this)); |
208 } | 234 } |
209 | 235 |
210 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicAdded( | 236 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicAdded( |
211 const dbus::ObjectPath& object_path) { | 237 const dbus::ObjectPath& object_path) { |
212 if (characteristics_.find(object_path) != characteristics_.end()) { | 238 if (characteristics_.find(object_path) != characteristics_.end()) { |
213 VLOG(1) << "Remote GATT characteristic already exists: " | 239 VLOG(1) << "Remote GATT characteristic already exists: " |
214 << object_path.value(); | 240 << object_path.value(); |
215 return; | 241 return; |
216 } | 242 } |
217 | 243 |
(...skipping 10 matching lines...) Expand all Loading... |
228 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 254 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
229 | 255 |
230 BluetoothRemoteGattCharacteristicChromeOS* characteristic = | 256 BluetoothRemoteGattCharacteristicChromeOS* characteristic = |
231 new BluetoothRemoteGattCharacteristicChromeOS(this, object_path); | 257 new BluetoothRemoteGattCharacteristicChromeOS(this, object_path); |
232 characteristics_[object_path] = characteristic; | 258 characteristics_[object_path] = characteristic; |
233 DCHECK(characteristic->GetIdentifier() == object_path.value()); | 259 DCHECK(characteristic->GetIdentifier() == object_path.value()); |
234 DCHECK(characteristic->GetUUID().IsValid()); | 260 DCHECK(characteristic->GetUUID().IsValid()); |
235 | 261 |
236 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 262 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
237 GattCharacteristicAdded(this, characteristic)); | 263 GattCharacteristicAdded(this, characteristic)); |
238 NotifyServiceChanged(); | |
239 } | 264 } |
240 | 265 |
241 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicRemoved( | 266 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicRemoved( |
242 const dbus::ObjectPath& object_path) { | 267 const dbus::ObjectPath& object_path) { |
243 CharacteristicMap::iterator iter = characteristics_.find(object_path); | 268 CharacteristicMap::iterator iter = characteristics_.find(object_path); |
244 if (iter == characteristics_.end()) { | 269 if (iter == characteristics_.end()) { |
245 VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value(); | 270 VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value(); |
246 return; | 271 return; |
247 } | 272 } |
248 | 273 |
249 VLOG(1) << "Removing remote GATT characteristic from service: " | 274 VLOG(1) << "Removing remote GATT characteristic from service: " |
250 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); | 275 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
251 | 276 |
252 BluetoothRemoteGattCharacteristicChromeOS* characteristic = iter->second; | 277 BluetoothRemoteGattCharacteristicChromeOS* characteristic = iter->second; |
253 DCHECK(characteristic->object_path() == object_path); | 278 DCHECK(characteristic->object_path() == object_path); |
254 characteristics_.erase(iter); | 279 characteristics_.erase(iter); |
255 | 280 |
256 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, | 281 FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
257 GattCharacteristicRemoved(this, characteristic)); | 282 GattCharacteristicRemoved(this, characteristic)); |
258 NotifyServiceChanged(); | |
259 | 283 |
260 delete characteristic; | 284 delete characteristic; |
261 } | 285 } |
262 | 286 |
263 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicPropertyChanged( | 287 void BluetoothRemoteGattServiceChromeOS::GattCharacteristicPropertyChanged( |
264 const dbus::ObjectPath& object_path, | 288 const dbus::ObjectPath& object_path, |
265 const std::string& property_name) { | 289 const std::string& property_name) { |
266 if (characteristics_.find(object_path) == characteristics_.end()) { | 290 if (characteristics_.find(object_path) == characteristics_.end()) { |
267 VLOG(2) << "Properties of unknown characteristic changed"; | 291 VLOG(3) << "Properties of unknown characteristic changed"; |
268 return; | 292 return; |
269 } | 293 } |
270 | 294 |
271 // We may receive a property changed event in certain cases, e.g. when the | 295 // We may receive a property changed event in certain cases, e.g. when the |
272 // characteristic "Flags" property has been updated with values from the | 296 // characteristic "Flags" property has been updated with values from the |
273 // "Characteristic Extended Properties" descriptor. In this case, kick off | 297 // "Characteristic Extended Properties" descriptor. In this case, kick off |
274 // a service changed observer event to let observers refresh the | 298 // a service changed observer event to let observers refresh the |
275 // characteristics. | 299 // characteristics. |
276 BluetoothGattCharacteristicClient::Properties* properties = | 300 BluetoothGattCharacteristicClient::Properties* properties = |
277 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> | 301 DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
278 GetProperties(object_path); | 302 GetProperties(object_path); |
279 DCHECK(properties); | 303 DCHECK(properties); |
280 if (property_name != properties->flags.name()) | 304 if (property_name != properties->flags.name()) |
281 return; | 305 return; |
282 | 306 |
283 NotifyServiceChanged(); | 307 NotifyServiceChanged(); |
284 } | 308 } |
285 | 309 |
286 } // namespace chromeos | 310 } // namespace chromeos |
OLD | NEW |