Chromium Code Reviews| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { | 184 void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl() { |
| 180 if (!IsGattConnected()) { | 185 if (!IsGattConnected()) { |
| 181 GetMacAdapter()->CreateGattConnection(this); | 186 GetMacAdapter()->CreateGattConnection(this); |
| 182 } | 187 } |
| 183 } | 188 } |
| 184 | 189 |
| 185 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { | 190 void BluetoothLowEnergyDeviceMac::DisconnectGatt() { |
| 186 GetMacAdapter()->DisconnectGatt(this); | 191 GetMacAdapter()->DisconnectGatt(this); |
| 187 } | 192 } |
| 188 | 193 |
| 194 void BluetoothLowEnergyDeviceMac::DidConnectGatt() { | |
|
ortuno
2017/05/02 01:24:23
Why can't we do this in DidConnectPeripheral befor
jlebel
2017/05/02 19:54:46
That's a behavior of the device, no?
ortuno
2017/05/02 23:36:45
Hmm I'm not sure I understand what you mean. DidCo
jlebel
2017/05/03 21:16:19
I'm not sure how to the same in BluetoothAdapterMa
ortuno
2017/05/04 00:31:03
Ah. I missed the fact that DidConnectPeripheral is
| |
| 195 VLOG(1) << *this << ": Gatt connected."; | |
| 196 bool should_discover_services = !connected_; | |
| 197 connected_ = true; | |
| 198 BluetoothDeviceMac::DidConnectGatt(); | |
| 199 if (should_discover_services) { | |
| 200 DiscoverPrimaryServices(); | |
| 201 } else { | |
| 202 VLOG(1) << *this << ": Already connected, no need to discover services."; | |
| 203 } | |
| 204 } | |
| 205 | |
| 189 void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { | 206 void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) { |
| 190 --discovery_pending_count_; | 207 --discovery_pending_count_; |
| 191 if (discovery_pending_count_ < 0) { | 208 if (discovery_pending_count_ < 0) { |
| 192 // This should never happen, just in case it happens with a device, | 209 // This should never happen, just in case it happens with a device, |
| 193 // discovery_pending_count_ is set back to 0. | 210 // discovery_pending_count_ is set back to 0. |
| 194 VLOG(1) << *this | 211 VLOG(1) << *this |
| 195 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " | 212 << ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ " |
| 196 << discovery_pending_count_; | 213 << discovery_pending_count_; |
| 197 discovery_pending_count_ = 0; | 214 discovery_pending_count_ = 0; |
| 198 return; | 215 return; |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 GetBluetoothRemoteGattService(cb_characteristic.service); | 460 GetBluetoothRemoteGattService(cb_characteristic.service); |
| 444 DCHECK(gatt_service); | 461 DCHECK(gatt_service); |
| 445 device::BluetoothRemoteGattCharacteristicMac* gatt_characteristic = | 462 device::BluetoothRemoteGattCharacteristicMac* gatt_characteristic = |
| 446 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); | 463 gatt_service->GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
| 447 DCHECK(gatt_characteristic); | 464 DCHECK(gatt_characteristic); |
| 448 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( | 465 return gatt_characteristic->GetBluetoothRemoteGattDescriptorMac( |
| 449 cb_descriptor); | 466 cb_descriptor); |
| 450 } | 467 } |
| 451 | 468 |
| 452 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { | 469 void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) { |
| 470 connected_ = false; | |
| 453 VLOG(1) << *this << ": Disconnected from peripheral."; | 471 VLOG(1) << *this << ": Disconnected from peripheral."; |
| 454 if (error) { | 472 if (error) { |
| 455 VLOG(1) << *this | 473 VLOG(1) << *this |
| 456 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); | 474 << ": Bluetooth error: " << BluetoothAdapterMac::String(error); |
| 457 } | 475 } |
| 458 SetGattServicesDiscoveryComplete(false); | 476 SetGattServicesDiscoveryComplete(false); |
| 459 // Removing all services at once to ensure that calling GetGattService on | 477 // Removing all services at once to ensure that calling GetGattService on |
| 460 // removed service in GattServiceRemoved returns null. | 478 // removed service in GattServiceRemoved returns null. |
| 461 GattServiceMap gatt_services_swapped; | 479 GattServiceMap gatt_services_swapped; |
| 462 gatt_services_swapped.swap(gatt_services_); | 480 gatt_services_swapped.swap(gatt_services_); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 476 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); | 494 DidFailToConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED); |
| 477 } | 495 } |
| 478 | 496 |
| 479 namespace device { | 497 namespace device { |
| 480 | 498 |
| 481 std::ostream& operator<<(std::ostream& out, | 499 std::ostream& operator<<(std::ostream& out, |
| 482 const BluetoothLowEnergyDeviceMac& device) { | 500 const BluetoothLowEnergyDeviceMac& device) { |
| 483 // TODO(crbug.com/703878): Should use | 501 // TODO(crbug.com/703878): Should use |
| 484 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. | 502 // BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead. |
| 485 base::Optional<std::string> name = device.GetName(); | 503 base::Optional<std::string> name = device.GetName(); |
| 486 const char* name_cstr = name ? name->c_str() : ""; | 504 const char* name_cstr = name ? name->c_str() : ""; |
|
ortuno
2017/05/02 01:24:23
optional. You might be able to avoid this:
<< nam
jlebel
2017/05/02 19:54:46
Done.
| |
| 505 const char* is_gatt_connected = | |
| 506 device.IsGattConnected() ? "Gatt connected" : "Gatt disconnected"; | |
| 487 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" | 507 return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/" |
| 488 << &device << ", \"" << name_cstr << "\">"; | 508 << &device << ", " << is_gatt_connected << "\"" << name_cstr |
|
ortuno
2017/05/02 01:24:23
Would the following work?
<< &device << ", GATT C
jlebel
2017/05/02 19:54:46
The result is:
GATT Connected: 0
GATT Connected: 1
ortuno
2017/05/02 23:36:45
I think that's good enough. But up to you.
jlebel
2017/05/03 21:16:19
I definitely prefer with english words to be more
| |
| 509 << "\">"; | |
| 489 } | 510 } |
| 490 } // namespace device | 511 } // namespace device |
| OLD | NEW |