| 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" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 characteristic_value_read_or_write_in_progress_(false), | 73 characteristic_value_read_or_write_in_progress_(false), |
| 74 start_notifications_in_progress_(false), | 74 start_notifications_in_progress_(false), |
| 75 weak_ptr_factory_(this) { | 75 weak_ptr_factory_(this) { |
| 76 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( | 76 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( |
| 77 [cb_characteristic_.get() UUID]); | 77 [cb_characteristic_.get() UUID]); |
| 78 identifier_ = base::SysNSStringToUTF8( | 78 identifier_ = base::SysNSStringToUTF8( |
| 79 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), | 79 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), |
| 80 (void*)cb_characteristic_]); | 80 (void*)cb_characteristic_]); |
| 81 } | 81 } |
| 82 | 82 |
| 83 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {} | 83 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() { |
| 84 if (!read_characteristic_value_callbacks_.first.is_null()) { |
| 85 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 86 callbacks.swap(read_characteristic_value_callbacks_); |
| 87 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); |
| 88 } |
| 89 if (!write_characteristic_value_callbacks_.first.is_null()) { |
| 90 std::pair<base::Closure, ErrorCallback> callbacks; |
| 91 callbacks.swap(write_characteristic_value_callbacks_); |
| 92 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); |
| 93 } |
| 94 } |
| 84 | 95 |
| 85 std::string BluetoothRemoteGattCharacteristicMac::GetIdentifier() const { | 96 std::string BluetoothRemoteGattCharacteristicMac::GetIdentifier() const { |
| 86 return identifier_; | 97 return identifier_; |
| 87 } | 98 } |
| 88 | 99 |
| 89 BluetoothUUID BluetoothRemoteGattCharacteristicMac::GetUUID() const { | 100 BluetoothUUID BluetoothRemoteGattCharacteristicMac::GetUUID() const { |
| 90 return uuid_; | 101 return uuid_; |
| 91 } | 102 } |
| 92 | 103 |
| 93 BluetoothGattCharacteristic::Properties | 104 BluetoothGattCharacteristic::Properties |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 | 253 |
| 243 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( | 254 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( |
| 244 BluetoothRemoteGattDescriptor* ccc_descriptor, | 255 BluetoothRemoteGattDescriptor* ccc_descriptor, |
| 245 const base::Closure& callback, | 256 const base::Closure& callback, |
| 246 const ErrorCallback& error_callback) { | 257 const ErrorCallback& error_callback) { |
| 247 // TODO(http://crbug.com/633191): Implement this method | 258 // TODO(http://crbug.com/633191): Implement this method |
| 248 NOTIMPLEMENTED(); | 259 NOTIMPLEMENTED(); |
| 249 } | 260 } |
| 250 | 261 |
| 251 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { | 262 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
| 263 CHECK_EQ(gatt_service_->GetCBPeripheral().state, CBPeripheralStateConnected); |
| 252 // This method is called when the characteristic is read and when a | 264 // This method is called when the characteristic is read and when a |
| 253 // notification is received. | 265 // notification is received. |
| 254 if (characteristic_value_read_or_write_in_progress_) { | 266 if (characteristic_value_read_or_write_in_progress_) { |
| 255 std::pair<ValueCallback, ErrorCallback> callbacks; | 267 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 256 callbacks.swap(read_characteristic_value_callbacks_); | 268 callbacks.swap(read_characteristic_value_callbacks_); |
| 257 characteristic_value_read_or_write_in_progress_ = false; | 269 characteristic_value_read_or_write_in_progress_ = false; |
| 258 if (error) { | 270 if (error) { |
| 259 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " | 271 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " |
| 260 << base::SysNSStringToUTF8(error.domain) | 272 << base::SysNSStringToUTF8(error.domain) |
| 261 << ", error code: " << error.code; | 273 << ", error code: " << error.code; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 278 | 290 |
| 279 void BluetoothRemoteGattCharacteristicMac::UpdateValueAndNotify() { | 291 void BluetoothRemoteGattCharacteristicMac::UpdateValueAndNotify() { |
| 280 NSData* nsdata_value = cb_characteristic_.get().value; | 292 NSData* nsdata_value = cb_characteristic_.get().value; |
| 281 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); |
| 282 value_.assign(buffer, buffer + nsdata_value.length); | 294 value_.assign(buffer, buffer + nsdata_value.length); |
| 283 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, | 295 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, |
| 284 value_); | 296 value_); |
| 285 } | 297 } |
| 286 | 298 |
| 287 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { | 299 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { |
| 300 CHECK_EQ(gatt_service_->GetCBPeripheral().state, CBPeripheralStateConnected); |
| 288 if (!characteristic_value_read_or_write_in_progress_) { | 301 if (!characteristic_value_read_or_write_in_progress_) { |
| 289 // In case of buggy device, nothing should be done if receiving extra | 302 // In case of buggy device, nothing should be done if receiving extra |
| 290 // write confirmation. | 303 // write confirmation. |
| 291 VLOG(1) << "Write notification while no write operation pending."; | 304 VLOG(1) << "Write notification while no write operation pending."; |
| 292 return; | 305 return; |
| 293 } | 306 } |
| 294 std::pair<base::Closure, ErrorCallback> callbacks; | 307 std::pair<base::Closure, ErrorCallback> callbacks; |
| 295 callbacks.swap(write_characteristic_value_callbacks_); | 308 callbacks.swap(write_characteristic_value_callbacks_); |
| 296 characteristic_value_read_or_write_in_progress_ = false; | 309 characteristic_value_read_or_write_in_progress_ = false; |
| 297 if (error) { | 310 if (error) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) | 370 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) |
| 358 ? CBCharacteristicWriteWithResponse | 371 ? CBCharacteristicWriteWithResponse |
| 359 : CBCharacteristicWriteWithoutResponse; | 372 : CBCharacteristicWriteWithoutResponse; |
| 360 } | 373 } |
| 361 | 374 |
| 362 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() | 375 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() |
| 363 const { | 376 const { |
| 364 return cb_characteristic_.get(); | 377 return cb_characteristic_.get(); |
| 365 } | 378 } |
| 366 } // namespace device. | 379 } // namespace device. |
| OLD | NEW |