OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_low_energy_device_mac.h" | 5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
6 | 6 |
7 #import <CoreFoundation/CoreFoundation.h> | 7 #import <CoreFoundation/CoreFoundation.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 // Don't create services if the device disconnected. | 199 // Don't create services if the device disconnected. |
200 return; | 200 return; |
201 } | 201 } |
202 | 202 |
203 for (CBService* cb_service in GetPeripheral().services) { | 203 for (CBService* cb_service in GetPeripheral().services) { |
204 BluetoothRemoteGattServiceMac* gatt_service = | 204 BluetoothRemoteGattServiceMac* gatt_service = |
205 GetBluetoothRemoteGattService(cb_service); | 205 GetBluetoothRemoteGattService(cb_service); |
206 if (!gatt_service) { | 206 if (!gatt_service) { |
207 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, | 207 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, |
208 true /* is_primary */); | 208 true /* is_primary */); |
209 auto result_iter = gatt_services_.add(gatt_service->GetIdentifier(), | 209 auto insertion = gatt_services_.insert(std::make_pair( |
210 base::WrapUnique(gatt_service)); | 210 gatt_service->GetIdentifier(), std::move(gatt_service))); |
211 DCHECK(result_iter.second); | 211 if (!insertion.second) { |
212 adapter_->NotifyGattServiceAdded(gatt_service); | 212 VLOG(1) << "Insertion of service failed."; |
213 continue; | |
214 } | |
215 adapter_->NotifyGattServiceAdded(insertion.first->second.get()); | |
213 } | 216 } |
214 } | 217 } |
215 for (GattServiceMap::const_iterator it = gatt_services_.begin(); | 218 for (auto& gatt_service : gatt_services_) { |
216 it != gatt_services_.end(); ++it) { | |
217 device::BluetoothRemoteGattService* gatt_service = it->second; | |
218 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 219 device::BluetoothRemoteGattServiceMac* gatt_service_mac = |
219 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 220 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service.second.get()); |
220 gatt_service_mac->DiscoverCharacteristics(); | 221 gatt_service_mac->DiscoverCharacteristics(); |
221 } | 222 } |
222 } | 223 } |
223 | 224 |
224 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( | 225 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( |
225 CBService* cb_service, | 226 CBService* cb_service, |
226 NSError* error) { | 227 NSError* error) { |
227 if (error) { | 228 if (error) { |
228 // TODO(http://crbug.com/609320): Need to pass the error. | 229 // TODO(http://crbug.com/609320): Need to pass the error. |
229 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 230 // TODO(http://crbug.com/609844): Decide what to do if discover failed |
(...skipping 11 matching lines...) Expand all Loading... | |
241 | 242 |
242 BluetoothRemoteGattServiceMac* gatt_service = | 243 BluetoothRemoteGattServiceMac* gatt_service = |
243 GetBluetoothRemoteGattService(cb_service); | 244 GetBluetoothRemoteGattService(cb_service); |
244 DCHECK(gatt_service); | 245 DCHECK(gatt_service); |
245 gatt_service->DidDiscoverCharacteristics(); | 246 gatt_service->DidDiscoverCharacteristics(); |
246 | 247 |
247 // Notify when all services have been discovered. | 248 // Notify when all services have been discovered. |
248 bool discovery_complete = | 249 bool discovery_complete = |
249 std::find_if_not( | 250 std::find_if_not( |
250 gatt_services_.begin(), gatt_services_.end(), | 251 gatt_services_.begin(), gatt_services_.end(), |
251 [](std::pair<std::string, BluetoothRemoteGattService*> pair) { | 252 [](std::pair<const std::string, |
252 BluetoothRemoteGattService* gatt_service = pair.second; | 253 std::unique_ptr<device::BluetoothRemoteGattService>>& |
254 pair) { | |
255 BluetoothRemoteGattService* gatt_service = pair.second.get(); | |
253 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) | 256 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) |
254 ->IsDiscoveryComplete(); | 257 ->IsDiscoveryComplete(); |
255 }) == gatt_services_.end(); | 258 }) == gatt_services_.end(); |
256 if (discovery_complete) { | 259 if (discovery_complete) { |
257 device_uuids_.ReplaceServiceUUIDs(gatt_services_); | 260 device_uuids_.ReplaceServiceUUIDs(*(gatt_services_.get())); |
Reilly Grant (use Gerrit)
2016/12/21 22:25:14
no parenthesis needed
dougt
2016/12/22 01:18:02
Done.
| |
258 SetGattServicesDiscoveryComplete(true); | 261 SetGattServicesDiscoveryComplete(true); |
259 adapter_->NotifyGattServicesDiscovered(this); | 262 adapter_->NotifyGattServicesDiscovered(this); |
260 adapter_->NotifyDeviceChanged(this); | 263 adapter_->NotifyDeviceChanged(this); |
261 } | 264 } |
262 } | 265 } |
263 | 266 |
264 void BluetoothLowEnergyDeviceMac::DidModifyServices( | 267 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
265 NSArray* invalidatedServices) { | 268 NSArray* invalidatedServices) { |
266 VLOG(1) << "DidModifyServices: "; | 269 VLOG(1) << "DidModifyServices: "; |
267 for (CBService* cb_service in invalidatedServices) { | 270 for (CBService* cb_service in invalidatedServices) { |
268 BluetoothRemoteGattServiceMac* gatt_service = | 271 BluetoothRemoteGattServiceMac* gatt_service = |
269 GetBluetoothRemoteGattService(cb_service); | 272 GetBluetoothRemoteGattService(cb_service); |
270 DCHECK(gatt_service); | 273 DCHECK(gatt_service); |
271 VLOG(1) << gatt_service->GetUUID().canonical_value(); | 274 VLOG(1) << gatt_service->GetUUID().canonical_value(); |
272 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 275 |
273 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); | 276 auto iter = gatt_services_.find(gatt_service->GetIdentifier()); |
277 if (iter == gatt_services_.end()) { | |
278 return; | |
279 } | |
Reilly Grant (use Gerrit)
2016/12/21 22:25:14
no braces around single-line if
dougt
2016/12/22 01:18:02
Done.
| |
280 | |
281 auto scoped_service = std::move(iter->second); | |
282 gatt_services_.erase(iter); | |
283 | |
274 adapter_->NotifyGattServiceRemoved(scoped_service.get()); | 284 adapter_->NotifyGattServiceRemoved(scoped_service.get()); |
275 } | 285 } |
276 device_uuids_.ClearServiceUUIDs(); | 286 device_uuids_.ClearServiceUUIDs(); |
277 SetGattServicesDiscoveryComplete(false); | 287 SetGattServicesDiscoveryComplete(false); |
278 adapter_->NotifyDeviceChanged(this); | 288 adapter_->NotifyDeviceChanged(this); |
279 [GetPeripheral() discoverServices:nil]; | 289 [GetPeripheral() discoverServices:nil]; |
280 } | 290 } |
281 | 291 |
282 void BluetoothLowEnergyDeviceMac::DidUpdateValue( | 292 void BluetoothLowEnergyDeviceMac::DidUpdateValue( |
283 CBCharacteristic* characteristic, | 293 CBCharacteristic* characteristic, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 | 345 |
336 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { | 346 CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() { |
337 return peripheral_; | 347 return peripheral_; |
338 } | 348 } |
339 | 349 |
340 device::BluetoothRemoteGattServiceMac* | 350 device::BluetoothRemoteGattServiceMac* |
341 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( | 351 BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattService( |
342 CBService* cb_service) const { | 352 CBService* cb_service) const { |
343 for (GattServiceMap::const_iterator it = gatt_services_.begin(); | 353 for (GattServiceMap::const_iterator it = gatt_services_.begin(); |
344 it != gatt_services_.end(); ++it) { | 354 it != gatt_services_.end(); ++it) { |
345 device::BluetoothRemoteGattService* gatt_service = it->second; | 355 device::BluetoothRemoteGattService* gatt_service = it->second.get(); |
346 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 356 device::BluetoothRemoteGattServiceMac* gatt_service_mac = |
347 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | 357 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); |
348 if (gatt_service_mac->GetService() == cb_service) | 358 if (gatt_service_mac->GetService() == cb_service) |
349 return gatt_service_mac; | 359 return gatt_service_mac; |
350 } | 360 } |
351 return nullptr; | 361 return nullptr; |
352 } | 362 } |
353 | 363 |
354 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 364 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
355 SetGattServicesDiscoveryComplete(false); | 365 SetGattServicesDiscoveryComplete(false); |
356 // Removing all services at once to ensure that calling GetGattService on | 366 // Removing all services at once to ensure that calling GetGattService on |
357 // removed service in GattServiceRemoved returns null. | 367 // removed service in GattServiceRemoved returns null. |
358 GattServiceMap gatt_services_swapped; | 368 GattServiceMap gatt_services_swapped; |
359 gatt_services_swapped.swap(gatt_services_); | 369 gatt_services_swapped.swap(gatt_services_); |
360 gatt_services_swapped.clear(); | 370 gatt_services_swapped.clear(); |
361 device_uuids_.ClearServiceUUIDs(); | 371 device_uuids_.ClearServiceUUIDs(); |
362 // There are two cases in which this function will be called: | 372 // There are two cases in which this function will be called: |
363 // 1. When the connection to the device breaks (either because | 373 // 1. When the connection to the device breaks (either because |
364 // we closed it or the device closed it). | 374 // we closed it or the device closed it). |
365 // 2. When we cancel a pending connection request. | 375 // 2. When we cancel a pending connection request. |
366 if (create_gatt_connection_error_callbacks_.empty()) { | 376 if (create_gatt_connection_error_callbacks_.empty()) { |
367 // If there are no pending callbacks then the connection broke (#1). | 377 // If there are no pending callbacks then the connection broke (#1). |
368 DidDisconnectGatt(); | 378 DidDisconnectGatt(); |
369 return; | 379 return; |
370 } | 380 } |
371 // Else we canceled the connection request (#2). | 381 // Else we canceled the connection request (#2). |
372 // TODO(http://crbug.com/585897): Need to pass the error. | 382 // TODO(http://crbug.com/585897): Need to pass the error. |
373 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); | 383 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); |
374 } | 384 } |
OLD | NEW |