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 10 matching lines...) Expand all Loading... | |
21 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 21 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
22 | 22 |
23 using device::BluetoothDevice; | 23 using device::BluetoothDevice; |
24 using device::BluetoothLowEnergyDeviceMac; | 24 using device::BluetoothLowEnergyDeviceMac; |
25 | 25 |
26 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 26 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
27 BluetoothAdapterMac* adapter, | 27 BluetoothAdapterMac* adapter, |
28 CBPeripheral* peripheral) | 28 CBPeripheral* peripheral) |
29 : BluetoothDeviceMac(adapter), | 29 : BluetoothDeviceMac(adapter), |
30 peripheral_(peripheral, base::scoped_policy::RETAIN), | 30 peripheral_(peripheral, base::scoped_policy::RETAIN), |
31 connected_(false), | |
31 discovery_pending_count_(0) { | 32 discovery_pending_count_(0) { |
32 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 33 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
33 DCHECK(peripheral_.get()); | 34 DCHECK(peripheral_.get()); |
34 peripheral_delegate_.reset([[BluetoothLowEnergyPeripheralDelegate alloc] | 35 peripheral_delegate_.reset([[BluetoothLowEnergyPeripheralDelegate alloc] |
35 initWithBluetoothLowEnergyDeviceMac:this]); | 36 initWithBluetoothLowEnergyDeviceMac:this]); |
36 [peripheral_ setDelegate:peripheral_delegate_]; | 37 [peripheral_ setDelegate:peripheral_delegate_]; |
37 identifier_ = GetPeripheralIdentifier(peripheral); | 38 identifier_ = GetPeripheralIdentifier(peripheral); |
38 hash_address_ = GetPeripheralHashAddress(peripheral); | 39 hash_address_ = GetPeripheralHashAddress(peripheral); |
39 UpdateTimestamp(); | 40 UpdateTimestamp(); |
40 } | 41 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 | 90 |
90 bool BluetoothLowEnergyDeviceMac::IsPaired() const { | 91 bool BluetoothLowEnergyDeviceMac::IsPaired() const { |
91 return false; | 92 return false; |
92 } | 93 } |
93 | 94 |
94 bool BluetoothLowEnergyDeviceMac::IsConnected() const { | 95 bool BluetoothLowEnergyDeviceMac::IsConnected() const { |
95 return IsGattConnected(); | 96 return IsGattConnected(); |
96 } | 97 } |
97 | 98 |
98 bool BluetoothLowEnergyDeviceMac::IsGattConnected() const { | 99 bool BluetoothLowEnergyDeviceMac::IsGattConnected() const { |
99 return ([peripheral_ state] == CBPeripheralStateConnected); | 100 // |connected_| can be false while |[peripheral_ state]| is |
101 // |CBPeripheralStateConnected|. This happens | |
102 // BluetoothAdapterMac::DidConnectPeripheral() is called and | |
103 // BluetoothLowEnergyDeviceMac::DidConnectGatt() has not been called yet. | |
104 return connected_; | |
100 } | 105 } |
101 | 106 |
102 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { | 107 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { |
103 return connectable_; | 108 return connectable_; |
104 } | 109 } |
105 | 110 |
106 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { | 111 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { |
107 return ([peripheral_ state] == CBPeripheralStateConnecting); | 112 return ([peripheral_ state] == CBPeripheralStateConnecting); |
108 } | 113 } |
109 | 114 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( | 370 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
366 CBPeripheral* peripheral) { | 371 CBPeripheral* peripheral) { |
367 const size_t kCanonicalAddressNumberOfBytes = 6; | 372 const size_t kCanonicalAddressNumberOfBytes = 6; |
368 char raw[kCanonicalAddressNumberOfBytes]; | 373 char raw[kCanonicalAddressNumberOfBytes]; |
369 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, | 374 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, |
370 sizeof(raw)); | 375 sizeof(raw)); |
371 std::string hash = base::HexEncode(raw, sizeof(raw)); | 376 std::string hash = base::HexEncode(raw, sizeof(raw)); |
372 return BluetoothDevice::CanonicalizeAddress(hash); | 377 return BluetoothDevice::CanonicalizeAddress(hash); |
373 } | 378 } |
374 | 379 |
380 void BluetoothLowEnergyDeviceMac::DidConnectPeripheral() { | |
381 VLOG(1) << *this << ": GATT connected."; | |
382 const bool should_discover_services = !connected_; | |
383 connected_ = true; | |
384 DidConnectGatt(); | |
ortuno
2017/05/04 01:26:11
I wonder if we should even call DidConnectGatt().
jlebel
2017/05/04 01:46:48
Done.
| |
385 if (should_discover_services) { | |
386 DiscoverPrimaryServices(); | |
ortuno
2017/05/04 01:26:11
q: Any reason why we do this after DidConnectGatt(
jlebel
2017/05/04 01:46:48
It is better to first notify that the GATT is conn
| |
387 } else { | |
388 VLOG(1) << *this << ": Already connected, no need to discover services."; | |
389 } | |
390 } | |
391 | |
375 void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() { | 392 void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() { |
376 VLOG(1) << *this << ": DiscoverPrimaryServices, pending count " | 393 VLOG(1) << *this << ": DiscoverPrimaryServices, pending count " |
377 << discovery_pending_count_; | 394 << discovery_pending_count_; |
378 ++discovery_pending_count_; | 395 ++discovery_pending_count_; |
379 [GetPeripheral() discoverServices:nil]; | 396 [GetPeripheral() discoverServices:nil]; |
380 } | 397 } |
381 | 398 |
382 void BluetoothLowEnergyDeviceMac::SendNotificationIfDiscoveryComplete() { | 399 void BluetoothLowEnergyDeviceMac::SendNotificationIfDiscoveryComplete() { |
383 // Notify when all services have been discovered. | 400 // Notify when all services have been discovered. |
384 bool discovery_complete = | 401 bool discovery_complete = |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
428 GetBluetoothRemoteGattService(cb_characteristic.service); | 445 GetBluetoothRemoteGattService(cb_characteristic.service); |
429 DCHECK(gatt_service); | 446 DCHECK(gatt_service); |
430 device::BluetoothRemoteGattCharacteristicMac* gatt_characteristic = | 447 device::BluetoothRemoteGattCharacteristicMac* gatt_characteristic = |
431 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); | 448 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
432 DCHECK(gatt_characteristic); | 449 DCHECK(gatt_characteristic); |
433 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( | 450 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( |
434 cb_descriptor); | 451 cb_descriptor); |
435 } | 452 } |
436 | 453 |
437 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 454 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
455 connected_ = false; | |
438 VLOG(1) << *this << ": Disconnected from peripheral."; | 456 VLOG(1) << *this << ": Disconnected from peripheral."; |
439 if (error) { | 457 if (error) { |
440 VLOG(1) << *this | 458 VLOG(1) << *this |
441 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); | 459 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); |
442 } | 460 } |
443 SetGattServicesDiscoveryComplete(false); | 461 SetGattServicesDiscoveryComplete(false); |
444 // Removing all services at once to ensure that calling GetGattService on | 462 // Removing all services at once to ensure that calling GetGattService on |
445 // removed service in GattServiceRemoved returns null. | 463 // removed service in GattServiceRemoved returns null. |
446 GattServiceMap gatt_services_swapped; | 464 GattServiceMap gatt_services_swapped; |
447 gatt_services_swapped.swap(gatt_services_); | 465 gatt_services_swapped.swap(gatt_services_); |
(...skipping 13 matching lines...) Expand all Loading... | |
461 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); | 479 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); |
462 } | 480 } |
463 | 481 |
464 namespace device { | 482 namespace device { |
465 | 483 |
466 std::ostream& operator<<(std::ostream& out, | 484 std::ostream& operator<<(std::ostream& out, |
467 const BluetoothLowEnergyDeviceMac& device) { | 485 const BluetoothLowEnergyDeviceMac& device) { |
468 // TODO(crbug.com/703878): Should use | 486 // TODO(crbug.com/703878): Should use |
469 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 487 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
470 base::Optional<std::string> name = device.GetName(); | 488 base::Optional<std::string> name = device.GetName(); |
471 const char* name_cstr = name ? name->c_str() : ""; | 489 const char* is_gatt_connected = |
490 device.IsGattConnected() ? "GATT connected" : "GATT disconnected"; | |
472 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 491 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
473 << &device << ", \"" << name_cstr << "\">"; | 492 << &device << ", " << is_gatt_connected << ", \"" |
493 << name.value_or("Unnamed device") << "\">"; | |
474 } | 494 } |
475 } // namespace device | 495 } // namespace device |
OLD | NEW |