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() { |
| 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 happens, just in case it happens with a device, | 209 // This should never happens, 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 229 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 |