| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_descriptor_mac.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #import "base/mac/foundation_util.h" | 8 #import "base/mac/foundation_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 #import "device/bluetooth/bluetooth_adapter_mac.h" | 11 #import "device/bluetooth/bluetooth_adapter_mac.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_mac_metrics.h" |
| 12 #import "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | 13 #import "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| 13 | 14 |
| 14 using base::mac::ObjCCast; | 15 using base::mac::ObjCCast; |
| 15 | 16 |
| 16 namespace device { | 17 namespace device { |
| 17 | 18 |
| 18 std::vector<uint8_t> VectorValueFromObjC(id objc_value) { | 19 std::vector<uint8_t> VectorValueFromObjC(id objc_value) { |
| 19 // According to | 20 // According to |
| 20 // https://developer.apple.com/reference/corebluetooth/cbdescriptor some | 21 // https://developer.apple.com/reference/corebluetooth/cbdescriptor some |
| 21 // descriptor values can be NSData, NSString or NSNumber. | 22 // descriptor values can be NSData, NSString or NSNumber. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 133 |
| 133 void BluetoothRemoteGattDescriptorMac::DidUpdateValueForDescriptor( | 134 void BluetoothRemoteGattDescriptorMac::DidUpdateValueForDescriptor( |
| 134 NSError* error) { | 135 NSError* error) { |
| 135 if (!value_read_or_write_in_progress_) { | 136 if (!value_read_or_write_in_progress_) { |
| 136 VLOG(1) << *this << ": Value updated, no read in progress."; | 137 VLOG(1) << *this << ": Value updated, no read in progress."; |
| 137 return; | 138 return; |
| 138 } | 139 } |
| 139 std::pair<ValueCallback, ErrorCallback> callbacks; | 140 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 140 callbacks.swap(read_value_callbacks_); | 141 callbacks.swap(read_value_callbacks_); |
| 141 value_read_or_write_in_progress_ = false; | 142 value_read_or_write_in_progress_ = false; |
| 143 RecordDidUpdateValueForDescriptorResult(error); |
| 142 if (error) { | 144 if (error) { |
| 143 BluetoothGattService::GattErrorCode error_code = | 145 BluetoothGattService::GattErrorCode error_code = |
| 144 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 146 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 145 VLOG(1) << *this << ": Read value failed with error: " | 147 VLOG(1) << *this << ": Read value failed with error: " |
| 146 << BluetoothAdapterMac::String(error) | 148 << BluetoothAdapterMac::String(error) |
| 147 << ", converted to error code: " << error_code; | 149 << ", converted to error code: " << error_code; |
| 148 callbacks.second.Run(error_code); | 150 callbacks.second.Run(error_code); |
| 149 return; | 151 return; |
| 150 } | 152 } |
| 151 VLOG(1) << *this << ": Value read."; | 153 VLOG(1) << *this << ": Value read."; |
| 152 value_ = VectorValueFromObjC([cb_descriptor_ value]); | 154 value_ = VectorValueFromObjC([cb_descriptor_ value]); |
| 153 callbacks.first.Run(value_); | 155 callbacks.first.Run(value_); |
| 154 } | 156 } |
| 155 | 157 |
| 156 void BluetoothRemoteGattDescriptorMac::DidWriteValueForDescriptor( | 158 void BluetoothRemoteGattDescriptorMac::DidWriteValueForDescriptor( |
| 157 NSError* error) { | 159 NSError* error) { |
| 158 if (!value_read_or_write_in_progress_) { | 160 if (!value_read_or_write_in_progress_) { |
| 159 VLOG(1) << *this << ": Value written, no write in progress."; | 161 VLOG(1) << *this << ": Value written, no write in progress."; |
| 160 return; | 162 return; |
| 161 } | 163 } |
| 162 std::pair<base::Closure, ErrorCallback> callbacks; | 164 std::pair<base::Closure, ErrorCallback> callbacks; |
| 163 callbacks.swap(write_value_callbacks_); | 165 callbacks.swap(write_value_callbacks_); |
| 164 value_read_or_write_in_progress_ = false; | 166 value_read_or_write_in_progress_ = false; |
| 167 RecordDidWriteValueForDescriptorResult(error); |
| 165 if (error) { | 168 if (error) { |
| 166 BluetoothGattService::GattErrorCode error_code = | 169 BluetoothGattService::GattErrorCode error_code = |
| 167 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 170 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 168 VLOG(1) << *this << ": Write value failed with error: " | 171 VLOG(1) << *this << ": Write value failed with error: " |
| 169 << BluetoothAdapterMac::String(error) | 172 << BluetoothAdapterMac::String(error) |
| 170 << ", converted to error code: " << error_code; | 173 << ", converted to error code: " << error_code; |
| 171 callbacks.second.Run(error_code); | 174 callbacks.second.Run(error_code); |
| 172 return; | 175 return; |
| 173 } | 176 } |
| 174 VLOG(1) << *this << ": Value written."; | 177 VLOG(1) << *this << ": Value written."; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 190 static_cast<const BluetoothRemoteGattCharacteristicMac*>( | 193 static_cast<const BluetoothRemoteGattCharacteristicMac*>( |
| 191 descriptor.GetCharacteristic()); | 194 descriptor.GetCharacteristic()); |
| 192 return out << "<BluetoothRemoteGattDescriptorMac " | 195 return out << "<BluetoothRemoteGattDescriptorMac " |
| 193 << descriptor.GetUUID().canonical_value() << "/" << &descriptor | 196 << descriptor.GetUUID().canonical_value() << "/" << &descriptor |
| 194 << ", characteristic: " | 197 << ", characteristic: " |
| 195 << characteristic_mac->GetUUID().canonical_value() << "/" | 198 << characteristic_mac->GetUUID().canonical_value() << "/" |
| 196 << characteristic_mac << ">"; | 199 << characteristic_mac << ">"; |
| 197 } | 200 } |
| 198 | 201 |
| 199 } // namespace device. | 202 } // namespace device. |
| OLD | NEW |