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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { | 235 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { |
236 GetMacAdapter()->DisconnectGatt(this); | 236 GetMacAdapter()->DisconnectGatt(this); |
237 } | 237 } |
238 | 238 |
239 void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { | 239 void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { |
240 if (error) { | 240 if (error) { |
241 // TODO(http://crbug.com/609320): Need to pass the error. | 241 // TODO(http://crbug.com/609320): Need to pass the error. |
242 // TODO(http://crbug.com/609844): Decide what to do if discover failed | 242 // TODO(http://crbug.com/609844): Decide what to do if we fail to discover |
243 // a device services. | 243 // a device services. |
244 VLOG(1) << "Can't discover primary services: " | 244 VLOG(1) << "Can't discover primary services: " |
245 << error.localizedDescription.UTF8String << " (" << error.domain | 245 << error.localizedDescription.UTF8String << " (" << error.domain |
246 << ": " << error.code << ")"; | 246 << ": " << error.code << ")"; |
247 return; | 247 return; |
248 } | 248 } |
249 for (CBService* cb_service in GetPeripheral().services) { | 249 for (CBService* cb_service in GetPeripheral().services) { |
250 BluetoothRemoteGattServiceMac* gatt_service = | 250 BluetoothRemoteGattServiceMac* gatt_service = |
251 GetBluetoothRemoteGattService(cb_service); | 251 GetBluetoothRemoteGattService(cb_service); |
252 if (!gatt_service) { | 252 if (!gatt_service) { |
253 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, | 253 gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service, |
254 true /* is_primary */); | 254 true /* is_primary */); |
255 auto result_iter = gatt_services_.add(gatt_service->GetIdentifier(), | 255 auto result_iter = gatt_services_.add(gatt_service->GetIdentifier(), |
256 base::WrapUnique(gatt_service)); | 256 base::WrapUnique(gatt_service)); |
257 DCHECK(result_iter.second); | 257 DCHECK(result_iter.second); |
258 adapter_->NotifyGattServiceAdded(gatt_service); | 258 adapter_->NotifyGattServiceAdded(gatt_service); |
259 } | 259 } |
260 } | 260 } |
261 for (GattServiceMap::const_iterator it = gatt_services_.begin(); | 261 // TODO(http://crbug.com/609064): Services are fully discovered once all |
262 it != gatt_services_.end(); ++it) { | 262 // characteristics have been found. |
263 device::BluetoothRemoteGattService* gatt_service = it->second; | 263 SetGattServicesDiscoveryComplete(true); |
264 device::BluetoothRemoteGattServiceMac* gatt_service_mac = | 264 adapter_->NotifyGattServicesDiscovered(this); |
265 static_cast<BluetoothRemoteGattServiceMac*>(gatt_service); | |
266 gatt_service_mac->DiscoverCharacteristics(); | |
267 } | |
268 } | |
269 | |
270 void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics( | |
271 CBService* cb_service, | |
272 NSError* error) { | |
273 if (error) { | |
274 // TODO(http://crbug.com/609320): Need to pass the error. | |
275 // TODO(http://crbug.com/609844): Decide what to do if discover failed | |
276 VLOG(1) << "Can't discover characteristics: " | |
277 << error.localizedDescription.UTF8String << " (" << error.domain | |
278 << ": " << error.code << ")"; | |
279 return; | |
280 } | |
281 BluetoothRemoteGattServiceMac* gatt_service = | |
282 GetBluetoothRemoteGattService(cb_service); | |
283 DCHECK(gatt_service); | |
284 gatt_service->DidDiscoverCharacteristics(); | |
285 | |
286 // Notify when all services have been discovered. | |
287 bool discovery_complete = | |
288 std::find_if_not( | |
289 gatt_services_.begin(), gatt_services_.end(), | |
290 [](std::pair<std::string, BluetoothRemoteGattService*> pair) { | |
291 BluetoothRemoteGattService* gatt_service = pair.second; | |
292 return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service) | |
293 ->IsDiscoveryComplete(); | |
294 }) == gatt_services_.end(); | |
295 if (discovery_complete) { | |
296 SetGattServicesDiscoveryComplete(true); | |
297 adapter_->NotifyGattServicesDiscovered(this); | |
298 } | |
299 } | 265 } |
300 | 266 |
301 void BluetoothLowEnergyDeviceMac::DidModifyServices( | 267 void BluetoothLowEnergyDeviceMac::DidModifyServices( |
302 NSArray* invalidatedServices) { | 268 NSArray* invalidatedServices) { |
303 for (CBService* cb_service in invalidatedServices) { | 269 for (CBService* cb_service in invalidatedServices) { |
304 BluetoothRemoteGattServiceMac* gatt_service = | 270 BluetoothRemoteGattServiceMac* gatt_service = |
305 GetBluetoothRemoteGattService(cb_service); | 271 GetBluetoothRemoteGattService(cb_service); |
306 DCHECK(gatt_service); | 272 DCHECK(gatt_service); |
307 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 273 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
308 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); | 274 gatt_services_.take_and_erase(gatt_service->GetIdentifier()); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 GattServiceMap gatt_services_swapped; | 328 GattServiceMap gatt_services_swapped; |
363 gatt_services_swapped.swap(gatt_services_); | 329 gatt_services_swapped.swap(gatt_services_); |
364 gatt_services_swapped.clear(); | 330 gatt_services_swapped.clear(); |
365 if (create_gatt_connection_error_callbacks_.empty()) { | 331 if (create_gatt_connection_error_callbacks_.empty()) { |
366 // TODO(http://crbug.com/585897): Need to pass the error. | 332 // TODO(http://crbug.com/585897): Need to pass the error. |
367 DidDisconnectGatt(); | 333 DidDisconnectGatt(); |
368 } else { | 334 } else { |
369 DidFailToConnectGatt(error_code); | 335 DidFailToConnectGatt(error_code); |
370 } | 336 } |
371 } | 337 } |
OLD | NEW |