| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_remote_gatt_service_mac.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 6 | 6 |
| 7 #import <CoreBluetooth/CoreBluetooth.h> | 7 #import <CoreBluetooth/CoreBluetooth.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 const std::string& identifier) const { | 71 const std::string& identifier) const { |
| 72 auto searched_pair = gatt_characteristic_macs_.find(identifier); | 72 auto searched_pair = gatt_characteristic_macs_.find(identifier); |
| 73 if (searched_pair == gatt_characteristic_macs_.end()) { | 73 if (searched_pair == gatt_characteristic_macs_.end()) { |
| 74 return nullptr; | 74 return nullptr; |
| 75 } | 75 } |
| 76 return static_cast<BluetoothRemoteGattCharacteristic*>( | 76 return static_cast<BluetoothRemoteGattCharacteristic*>( |
| 77 searched_pair->second.get()); | 77 searched_pair->second.get()); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void BluetoothRemoteGattServiceMac::DiscoverCharacteristics() { | 80 void BluetoothRemoteGattServiceMac::DiscoverCharacteristics() { |
| 81 VLOG(1) << *this << ": DiscoverCharacteristics."; |
| 81 is_discovery_complete_ = false; | 82 is_discovery_complete_ = false; |
| 82 [GetCBPeripheral() discoverCharacteristics:nil forService:GetService()]; | 83 [GetCBPeripheral() discoverCharacteristics:nil forService:GetService()]; |
| 83 } | 84 } |
| 84 | 85 |
| 85 void BluetoothRemoteGattServiceMac::DidDiscoverCharacteristics() { | 86 void BluetoothRemoteGattServiceMac::DidDiscoverCharacteristics() { |
| 86 DCHECK(!is_discovery_complete_); | 87 DCHECK(!is_discovery_complete_); |
| 88 VLOG(1) << *this << ": DidDiscoverCharacteristics."; |
| 87 std::unordered_set<std::string> characteristic_identifier_to_remove; | 89 std::unordered_set<std::string> characteristic_identifier_to_remove; |
| 88 for (const auto& iter : gatt_characteristic_macs_) { | 90 for (const auto& iter : gatt_characteristic_macs_) { |
| 89 characteristic_identifier_to_remove.insert(iter.first); | 91 characteristic_identifier_to_remove.insert(iter.first); |
| 90 } | 92 } |
| 91 | 93 |
| 92 for (CBCharacteristic* cb_characteristic in GetService().characteristics) { | 94 for (CBCharacteristic* cb_characteristic in GetService().characteristics) { |
| 93 BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = | 95 BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = |
| 94 GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); | 96 GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
| 95 if (gatt_characteristic_mac) { | 97 if (gatt_characteristic_mac) { |
| 98 VLOG(1) << *gatt_characteristic_mac |
| 99 << ": Known characteristic, properties " |
| 100 << gatt_characteristic_mac->GetProperties(); |
| 96 const std::string& identifier = gatt_characteristic_mac->GetIdentifier(); | 101 const std::string& identifier = gatt_characteristic_mac->GetIdentifier(); |
| 97 characteristic_identifier_to_remove.erase(identifier); | 102 characteristic_identifier_to_remove.erase(identifier); |
| 98 gatt_characteristic_mac->DiscoverDescriptors(); | 103 gatt_characteristic_mac->DiscoverDescriptors(); |
| 99 continue; | 104 continue; |
| 100 } | 105 } |
| 101 gatt_characteristic_mac = | 106 gatt_characteristic_mac = |
| 102 new BluetoothRemoteGattCharacteristicMac(this, cb_characteristic); | 107 new BluetoothRemoteGattCharacteristicMac(this, cb_characteristic); |
| 103 const std::string& identifier = gatt_characteristic_mac->GetIdentifier(); | 108 const std::string& identifier = gatt_characteristic_mac->GetIdentifier(); |
| 104 auto result_iter = gatt_characteristic_macs_.insert( | 109 auto result_iter = gatt_characteristic_macs_.insert( |
| 105 {identifier, base::WrapUnique(gatt_characteristic_mac)}); | 110 {identifier, base::WrapUnique(gatt_characteristic_mac)}); |
| 106 DCHECK(result_iter.second); | 111 DCHECK(result_iter.second); |
| 112 VLOG(1) << *gatt_characteristic_mac << ": New characteristic, properties " |
| 113 << gatt_characteristic_mac->GetProperties(); |
| 107 gatt_characteristic_mac->DiscoverDescriptors(); | 114 gatt_characteristic_mac->DiscoverDescriptors(); |
| 108 GetMacAdapter()->NotifyGattCharacteristicAdded(gatt_characteristic_mac); | 115 GetMacAdapter()->NotifyGattCharacteristicAdded(gatt_characteristic_mac); |
| 109 } | 116 } |
| 110 | 117 |
| 111 for (const std::string& identifier : characteristic_identifier_to_remove) { | 118 for (const std::string& identifier : characteristic_identifier_to_remove) { |
| 112 auto pair_to_remove = gatt_characteristic_macs_.find(identifier); | 119 auto pair_to_remove = gatt_characteristic_macs_.find(identifier); |
| 113 std::unique_ptr<BluetoothRemoteGattCharacteristicMac> | 120 std::unique_ptr<BluetoothRemoteGattCharacteristicMac> |
| 114 characteristic_to_remove; | 121 characteristic_to_remove; |
| 115 pair_to_remove->second.swap(characteristic_to_remove); | 122 pair_to_remove->second.swap(characteristic_to_remove); |
| 123 VLOG(1) << *characteristic_to_remove << ": Removed characteristic."; |
| 116 gatt_characteristic_macs_.erase(pair_to_remove); | 124 gatt_characteristic_macs_.erase(pair_to_remove); |
| 117 GetMacAdapter()->NotifyGattCharacteristicRemoved( | 125 GetMacAdapter()->NotifyGattCharacteristicRemoved( |
| 118 characteristic_to_remove.get()); | 126 characteristic_to_remove.get()); |
| 119 } | 127 } |
| 120 SendNotificationIfComplete(); | 128 SendNotificationIfComplete(); |
| 121 } | 129 } |
| 122 | 130 |
| 123 void BluetoothRemoteGattServiceMac::DidDiscoverDescriptors( | 131 void BluetoothRemoteGattServiceMac::DidDiscoverDescriptors( |
| 124 CBCharacteristic* characteristic) { | 132 CBCharacteristic* characteristic) { |
| 125 DCHECK(!is_discovery_complete_); | 133 DCHECK(!is_discovery_complete_); |
| 126 BluetoothRemoteGattCharacteristicMac* gatt_characteristic = | 134 BluetoothRemoteGattCharacteristicMac* gatt_characteristic = |
| 127 GetBluetoothRemoteGattCharacteristicMac(characteristic); | 135 GetBluetoothRemoteGattCharacteristicMac(characteristic); |
| 128 DCHECK(gatt_characteristic); | 136 DCHECK(gatt_characteristic); |
| 129 gatt_characteristic->DidDiscoverDescriptors(); | 137 gatt_characteristic->DidDiscoverDescriptors(); |
| 130 SendNotificationIfComplete(); | 138 SendNotificationIfComplete(); |
| 131 } | 139 } |
| 132 | 140 |
| 133 void BluetoothRemoteGattServiceMac::SendNotificationIfComplete() { | 141 void BluetoothRemoteGattServiceMac::SendNotificationIfComplete() { |
| 134 DCHECK(!is_discovery_complete_); | 142 DCHECK(!is_discovery_complete_); |
| 135 // Notify when all characteristics have been fully discovered. | 143 // Notify when all characteristics have been fully discovered. |
| 136 is_discovery_complete_ = | 144 is_discovery_complete_ = |
| 137 std::find_if_not( | 145 std::find_if_not( |
| 138 gatt_characteristic_macs_.begin(), gatt_characteristic_macs_.end(), | 146 gatt_characteristic_macs_.begin(), gatt_characteristic_macs_.end(), |
| 139 [](const std::pair< | 147 [](const std::pair< |
| 140 const std::string, | 148 const std::string, |
| 141 std::unique_ptr<BluetoothRemoteGattCharacteristicMac>>& pair) { | 149 std::unique_ptr<BluetoothRemoteGattCharacteristicMac>>& pair) { |
| 142 return pair.second->IsDiscoveryComplete(); | 150 return pair.second->IsDiscoveryComplete(); |
| 143 }) == gatt_characteristic_macs_.end(); | 151 }) == gatt_characteristic_macs_.end(); |
| 144 if (is_discovery_complete_) { | 152 if (is_discovery_complete_) { |
| 153 VLOG(1) << *this << ": Discovery complete."; |
| 145 GetMacAdapter()->NotifyGattServiceChanged(this); | 154 GetMacAdapter()->NotifyGattServiceChanged(this); |
| 146 } | 155 } |
| 147 } | 156 } |
| 148 | 157 |
| 149 void BluetoothRemoteGattServiceMac::DidUpdateValue( | 158 void BluetoothRemoteGattServiceMac::DidUpdateValue( |
| 150 CBCharacteristic* characteristic, | 159 CBCharacteristic* characteristic, |
| 151 NSError* error) { | 160 NSError* error) { |
| 152 BluetoothRemoteGattCharacteristicMac* gatt_characteristic = | 161 BluetoothRemoteGattCharacteristicMac* gatt_characteristic = |
| 153 GetBluetoothRemoteGattCharacteristicMac(characteristic); | 162 GetBluetoothRemoteGattCharacteristicMac(characteristic); |
| 154 DCHECK(gatt_characteristic); | 163 DCHECK(gatt_characteristic); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 std::unique_ptr<BluetoothRemoteGattCharacteristicMac>>& pair) { | 209 std::unique_ptr<BluetoothRemoteGattCharacteristicMac>>& pair) { |
| 201 return pair.second->GetCBCharacteristic() == characteristic; | 210 return pair.second->GetCBCharacteristic() == characteristic; |
| 202 }); | 211 }); |
| 203 if (found == gatt_characteristic_macs_.end()) { | 212 if (found == gatt_characteristic_macs_.end()) { |
| 204 return nullptr; | 213 return nullptr; |
| 205 } else { | 214 } else { |
| 206 return found->second.get(); | 215 return found->second.get(); |
| 207 } | 216 } |
| 208 } | 217 } |
| 209 | 218 |
| 219 DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<( |
| 220 std::ostream& out, |
| 221 const BluetoothRemoteGattServiceMac& service) { |
| 222 const BluetoothLowEnergyDeviceMac* bluetooth_device_mac_ = |
| 223 static_cast<const BluetoothLowEnergyDeviceMac*>(service.GetDevice()); |
| 224 return out << "<BluetoothRemoteGattServiceMac " |
| 225 << service.GetUUID().canonical_value() << "/" << &service |
| 226 << ", device: " << bluetooth_device_mac_->GetAddress() << "/" |
| 227 << bluetooth_device_mac_ << ">"; |
| 228 } |
| 229 |
| 210 } // namespace device | 230 } // namespace device |
| OLD | NEW |