| 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 if (error) { |
| 256 LogNSErrorToHistogram( |
| 257 error, |
| 258 WebBluetoothMacOSAPIs::WEB_BLUETOOTH_MACOS_APIS_DID_UPDATE_VALUE); |
| 259 } |
| 254 if (HasPendingRead()) { | 260 if (HasPendingRead()) { |
| 255 std::pair<ValueCallback, ErrorCallback> callbacks; | 261 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 256 callbacks.swap(read_characteristic_value_callbacks_); | 262 callbacks.swap(read_characteristic_value_callbacks_); |
| 257 if (error) { | 263 if (error) { |
| 258 BluetoothGattService::GattErrorCode error_code = | 264 BluetoothGattService::GattErrorCode error_code = |
| 259 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 265 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 260 VLOG(1) << *this | 266 VLOG(1) << *this |
| 261 << ": Bluetooth error while reading for characteristic, domain: " | 267 << ": Bluetooth error while reading for characteristic, domain: " |
| 262 << BluetoothAdapterMac::String(error) | 268 << BluetoothAdapterMac::String(error) |
| 263 << ", error code: " << error_code; | 269 << ", error code: " << error_code; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 282 } | 288 } |
| 283 } | 289 } |
| 284 | 290 |
| 285 void BluetoothRemoteGattCharacteristicMac::UpdateValue() { | 291 void BluetoothRemoteGattCharacteristicMac::UpdateValue() { |
| 286 NSData* nsdata_value = [cb_characteristic_ value]; | 292 NSData* nsdata_value = [cb_characteristic_ value]; |
| 287 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); | 293 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); |
| 288 value_.assign(buffer, buffer + nsdata_value.length); | 294 value_.assign(buffer, buffer + nsdata_value.length); |
| 289 } | 295 } |
| 290 | 296 |
| 291 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { | 297 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { |
| 298 if (error) { |
| 299 LogNSErrorToHistogram( |
| 300 error, WebBluetoothMacOSAPIs::WEB_BLUETOOTH_MACOS_APIS_DID_WRITE_VALUE); |
| 301 } |
| 292 // We could have called cancelPeripheralConnection, which causes | 302 // We could have called cancelPeripheralConnection, which causes |
| 293 // [CBPeripheral state] to be CBPeripheralStateDisconnected, before or during | 303 // [CBPeripheral state] to be CBPeripheralStateDisconnected, before or during |
| 294 // a write without response callback so we flush all pending writes. | 304 // a write without response callback so we flush all pending writes. |
| 295 // TODO(crbug.com/726534): Remove once we can avoid calling DidWriteValue | 305 // TODO(crbug.com/726534): Remove once we can avoid calling DidWriteValue |
| 296 // when we disconnect before or during a write without response call. | 306 // when we disconnect before or during a write without response call. |
| 297 if (HasPendingWrite() && | 307 if (HasPendingWrite() && |
| 298 GetCBPeripheral().state != CBPeripheralStateConnected) { | 308 GetCBPeripheral().state != CBPeripheralStateConnected) { |
| 299 std::pair<base::Closure, ErrorCallback> callbacks; | 309 std::pair<base::Closure, ErrorCallback> callbacks; |
| 300 callbacks.swap(write_characteristic_value_callbacks_); | 310 callbacks.swap(write_characteristic_value_callbacks_); |
| 301 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); | 311 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 DCHECK([GetCBCharacteristic() isNotifying] || error); | 344 DCHECK([GetCBCharacteristic() isNotifying] || error); |
| 335 reentrant_safe_callbacks.swap(subscribe_to_notification_callbacks_); | 345 reentrant_safe_callbacks.swap(subscribe_to_notification_callbacks_); |
| 336 } else if (!unsubscribe_from_notification_callbacks_.first.is_null()) { | 346 } else if (!unsubscribe_from_notification_callbacks_.first.is_null()) { |
| 337 DCHECK(![GetCBCharacteristic() isNotifying] || error); | 347 DCHECK(![GetCBCharacteristic() isNotifying] || error); |
| 338 reentrant_safe_callbacks.swap(unsubscribe_from_notification_callbacks_); | 348 reentrant_safe_callbacks.swap(unsubscribe_from_notification_callbacks_); |
| 339 } else { | 349 } else { |
| 340 VLOG(1) << *this << ": No pending notification update for characteristic."; | 350 VLOG(1) << *this << ": No pending notification update for characteristic."; |
| 341 return; | 351 return; |
| 342 } | 352 } |
| 343 if (error) { | 353 if (error) { |
| 354 LogNSErrorToHistogram( |
| 355 error, WebBluetoothMacOSAPIs:: |
| 356 WEB_BLUETOOTH_MACOS_APIS_DID_UPDATE_NOTIFICATION_STATE); |
| 344 BluetoothGattService::GattErrorCode error_code = | 357 BluetoothGattService::GattErrorCode error_code = |
| 345 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 358 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 346 VLOG(1) << *this | 359 VLOG(1) << *this |
| 347 << ": Bluetooth error while modifying notification state for " | 360 << ": Bluetooth error while modifying notification state for " |
| 348 "characteristic, error: " | 361 "characteristic, error: " |
| 349 << BluetoothAdapterMac::String(error) | 362 << BluetoothAdapterMac::String(error) |
| 350 << ", error code: " << error_code; | 363 << ", error code: " << error_code; |
| 351 reentrant_safe_callbacks.second.Run(error_code); | 364 reentrant_safe_callbacks.second.Run(error_code); |
| 352 return; | 365 return; |
| 353 } | 366 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 const BluetoothRemoteGattServiceMac* service_mac = | 477 const BluetoothRemoteGattServiceMac* service_mac = |
| 465 static_cast<const BluetoothRemoteGattServiceMac*>( | 478 static_cast<const BluetoothRemoteGattServiceMac*>( |
| 466 characteristic.GetService()); | 479 characteristic.GetService()); |
| 467 return out << "<BluetoothRemoteGattCharacteristicMac " | 480 return out << "<BluetoothRemoteGattCharacteristicMac " |
| 468 << characteristic.GetUUID().canonical_value() << "/" | 481 << characteristic.GetUUID().canonical_value() << "/" |
| 469 << &characteristic | 482 << &characteristic |
| 470 << ", service: " << service_mac->GetUUID().canonical_value() << "/" | 483 << ", service: " << service_mac->GetUUID().canonical_value() << "/" |
| 471 << service_mac << ">"; | 484 << service_mac << ">"; |
| 472 } | 485 } |
| 473 } // namespace device. | 486 } // namespace device. |
| OLD | NEW |