| 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_characteristic_mac.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "device/bluetooth/bluetooth_adapter_mac.h" | 11 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_mac_metrics.h" |
| 12 #include "device/bluetooth/bluetooth_device_mac.h" | 13 #include "device/bluetooth/bluetooth_device_mac.h" |
| 13 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 14 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 14 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" | 15 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" |
| 15 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 16 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 16 | 17 |
| 17 namespace device { | 18 namespace device { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 static BluetoothGattCharacteristic::Properties ConvertProperties( | 22 static BluetoothGattCharacteristic::Properties ConvertProperties( |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 VLOG(1) << *this << ": Discover descriptors."; | 245 VLOG(1) << *this << ": Discover descriptors."; |
| 245 is_discovery_complete_ = false; | 246 is_discovery_complete_ = false; |
| 246 ++discovery_pending_count_; | 247 ++discovery_pending_count_; |
| 247 [GetCBPeripheral() discoverDescriptorsForCharacteristic:cb_characteristic_]; | 248 [GetCBPeripheral() discoverDescriptorsForCharacteristic:cb_characteristic_]; |
| 248 } | 249 } |
| 249 | 250 |
| 250 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { | 251 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
| 251 CHECK_EQ(GetCBPeripheral().state, CBPeripheralStateConnected); | 252 CHECK_EQ(GetCBPeripheral().state, CBPeripheralStateConnected); |
| 252 // This method is called when the characteristic is read and when a | 253 // This method is called when the characteristic is read and when a |
| 253 // notification is received. | 254 // notification is received. |
| 255 LogDidUpdateValueErrorToHistogram(error); |
| 254 if (HasPendingRead()) { | 256 if (HasPendingRead()) { |
| 255 std::pair<ValueCallback, ErrorCallback> callbacks; | 257 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 256 callbacks.swap(read_characteristic_value_callbacks_); | 258 callbacks.swap(read_characteristic_value_callbacks_); |
| 257 if (error) { | 259 if (error) { |
| 258 BluetoothGattService::GattErrorCode error_code = | 260 BluetoothGattService::GattErrorCode error_code = |
| 259 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 261 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 260 VLOG(1) << *this | 262 VLOG(1) << *this |
| 261 << ": Bluetooth error while reading for characteristic, domain: " | 263 << ": Bluetooth error while reading for characteristic, domain: " |
| 262 << BluetoothAdapterMac::String(error) | 264 << BluetoothAdapterMac::String(error) |
| 263 << ", error code: " << error_code; | 265 << ", error code: " << error_code; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 282 } | 284 } |
| 283 } | 285 } |
| 284 | 286 |
| 285 void BluetoothRemoteGattCharacteristicMac::UpdateValue() { | 287 void BluetoothRemoteGattCharacteristicMac::UpdateValue() { |
| 286 NSData* nsdata_value = [cb_characteristic_ value]; | 288 NSData* nsdata_value = [cb_characteristic_ value]; |
| 287 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); | 289 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); |
| 288 value_.assign(buffer, buffer + nsdata_value.length); | 290 value_.assign(buffer, buffer + nsdata_value.length); |
| 289 } | 291 } |
| 290 | 292 |
| 291 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { | 293 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { |
| 294 LogDidWriteValueErrorToHistogram(error); |
| 292 // We could have called cancelPeripheralConnection, which causes | 295 // We could have called cancelPeripheralConnection, which causes |
| 293 // [CBPeripheral state] to be CBPeripheralStateDisconnected, before or during | 296 // [CBPeripheral state] to be CBPeripheralStateDisconnected, before or during |
| 294 // a write without response callback so we flush all pending writes. | 297 // a write without response callback so we flush all pending writes. |
| 295 // TODO(crbug.com/726534): Remove once we can avoid calling DidWriteValue | 298 // TODO(crbug.com/726534): Remove once we can avoid calling DidWriteValue |
| 296 // when we disconnect before or during a write without response call. | 299 // when we disconnect before or during a write without response call. |
| 297 if (HasPendingWrite() && | 300 if (HasPendingWrite() && |
| 298 GetCBPeripheral().state != CBPeripheralStateConnected) { | 301 GetCBPeripheral().state != CBPeripheralStateConnected) { |
| 299 std::pair<base::Closure, ErrorCallback> callbacks; | 302 std::pair<base::Closure, ErrorCallback> callbacks; |
| 300 callbacks.swap(write_characteristic_value_callbacks_); | 303 callbacks.swap(write_characteristic_value_callbacks_); |
| 301 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); | 304 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 if (!subscribe_to_notification_callbacks_.first.is_null()) { | 336 if (!subscribe_to_notification_callbacks_.first.is_null()) { |
| 334 DCHECK([GetCBCharacteristic() isNotifying] || error); | 337 DCHECK([GetCBCharacteristic() isNotifying] || error); |
| 335 reentrant_safe_callbacks.swap(subscribe_to_notification_callbacks_); | 338 reentrant_safe_callbacks.swap(subscribe_to_notification_callbacks_); |
| 336 } else if (!unsubscribe_from_notification_callbacks_.first.is_null()) { | 339 } else if (!unsubscribe_from_notification_callbacks_.first.is_null()) { |
| 337 DCHECK(![GetCBCharacteristic() isNotifying] || error); | 340 DCHECK(![GetCBCharacteristic() isNotifying] || error); |
| 338 reentrant_safe_callbacks.swap(unsubscribe_from_notification_callbacks_); | 341 reentrant_safe_callbacks.swap(unsubscribe_from_notification_callbacks_); |
| 339 } else { | 342 } else { |
| 340 VLOG(1) << *this << ": No pending notification update for characteristic."; | 343 VLOG(1) << *this << ": No pending notification update for characteristic."; |
| 341 return; | 344 return; |
| 342 } | 345 } |
| 346 LogDidUpdateNotificationStateErrorToHistogram(error); |
| 343 if (error) { | 347 if (error) { |
| 344 BluetoothGattService::GattErrorCode error_code = | 348 BluetoothGattService::GattErrorCode error_code = |
| 345 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 349 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 346 VLOG(1) << *this | 350 VLOG(1) << *this |
| 347 << ": Bluetooth error while modifying notification state for " | 351 << ": Bluetooth error while modifying notification state for " |
| 348 "characteristic, error: " | 352 "characteristic, error: " |
| 349 << BluetoothAdapterMac::String(error) | 353 << BluetoothAdapterMac::String(error) |
| 350 << ", error code: " << error_code; | 354 << ", error code: " << error_code; |
| 351 reentrant_safe_callbacks.second.Run(error_code); | 355 reentrant_safe_callbacks.second.Run(error_code); |
| 352 return; | 356 return; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 const BluetoothRemoteGattServiceMac* service_mac = | 468 const BluetoothRemoteGattServiceMac* service_mac = |
| 465 static_cast<const BluetoothRemoteGattServiceMac*>( | 469 static_cast<const BluetoothRemoteGattServiceMac*>( |
| 466 characteristic.GetService()); | 470 characteristic.GetService()); |
| 467 return out << "<BluetoothRemoteGattCharacteristicMac " | 471 return out << "<BluetoothRemoteGattCharacteristicMac " |
| 468 << characteristic.GetUUID().canonical_value() << "/" | 472 << characteristic.GetUUID().canonical_value() << "/" |
| 469 << &characteristic | 473 << &characteristic |
| 470 << ", service: " << service_mac->GetUUID().canonical_value() << "/" | 474 << ", service: " << service_mac->GetUUID().canonical_value() << "/" |
| 471 << service_mac << ">"; | 475 << service_mac << ">"; |
| 472 } | 476 } |
| 473 } // namespace device. | 477 } // namespace device. |
| OLD | NEW |