Chromium Code Reviews| Index: device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| index 6ff692f8e70016d79f578a42625342087425b995..f08b69a2ecc09edfe5a3f1221519fb7f48bbc6db 100644 |
| --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| @@ -5,9 +5,11 @@ |
| #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| #include "base/bind.h" |
| +#include "base/memory/ptr_util.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "device/bluetooth/bluetooth_adapter_mac.h" |
| #include "device/bluetooth/bluetooth_device_mac.h" |
| +#include "device/bluetooth/bluetooth_gatt_notify_session_mac.h" |
| #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| namespace device { |
| @@ -67,7 +69,8 @@ BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac( |
| CBCharacteristic* cb_characteristic) |
| : gatt_service_(gatt_service), |
| cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN), |
| - characteristic_value_read_or_write_in_progress_(false) { |
| + characteristic_value_read_or_write_in_progress_(false), |
| + weak_ptr_factory_(this) { |
| uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( |
| [cb_characteristic_.get() UUID]); |
| identifier_ = |
| @@ -109,8 +112,7 @@ BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicMac::GetService() |
| } |
| bool BluetoothRemoteGattCharacteristicMac::IsNotifying() const { |
| - NOTIMPLEMENTED(); |
| - return false; |
| + return cb_characteristic_.get().isNotifying == YES; |
| } |
| std::vector<BluetoothRemoteGattDescriptor*> |
| @@ -129,7 +131,28 @@ BluetoothRemoteGattCharacteristicMac::GetDescriptor( |
| void BluetoothRemoteGattCharacteristicMac::StartNotifySession( |
| const NotifySessionCallback& callback, |
| const ErrorCallback& error_callback) { |
| - NOTIMPLEMENTED(); |
| + if (IsNotifying()) { |
| + std::unique_ptr<BluetoothGattNotifySessionMac> notify_session( |
| + new BluetoothGattNotifySessionMac(weak_ptr_factory_.GetWeakPtr())); |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, base::Passed(std::move(notify_session)))); |
| + return; |
| + } |
| + if (!IsNotifiableOrIndicatable()) { |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, |
| + base::Bind(error_callback, |
| + BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
| + return; |
| + } |
| + start_notify_session_callbacks_.push_back( |
| + std::make_pair(callback, error_callback)); |
| + if (gatt_event_registeration_in_progress_) |
| + return; |
| + [gatt_service_->GetCBPeripheral() setNotifyValue:YES |
| + forCharacteristic:cb_characteristic_.get()]; |
| + gatt_event_registeration_in_progress_ = true; |
| } |
| void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( |
| @@ -192,28 +215,33 @@ void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( |
| } |
| void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
| - if (!characteristic_value_read_or_write_in_progress_) { |
| - // In case of buggy device, nothing should be done if receiving extra |
| - // read confirmation. |
| - return; |
| - } |
| - std::pair<ValueCallback, ErrorCallback> callbacks; |
| - callbacks.swap(read_characteristic_value_callbacks_); |
| - characteristic_value_read_or_write_in_progress_ = false; |
| - if (error) { |
| - VLOG(1) << "Bluetooth error while reading for characteristic, domain: " |
| - << error.domain.UTF8String << ", error code: " << error.code; |
| - BluetoothGattService::GattErrorCode error_code = |
| - BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| - callbacks.second.Run(error_code); |
| - return; |
| + // This method is called when the characteristic is read and when a |
| + // notification received. |
| + if (characteristic_value_read_or_write_in_progress_) { |
| + std::pair<ValueCallback, ErrorCallback> callbacks; |
| + callbacks.swap(read_characteristic_value_callbacks_); |
| + characteristic_value_read_or_write_in_progress_ = false; |
| + if (error) { |
| + VLOG(1) << "Bluetooth error while reading for characteristic, domain: " |
| + << error.domain.UTF8String << ", error code: " << error.code; |
| + BluetoothGattService::GattErrorCode error_code = |
| + BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| + callbacks.second.Run(error_code); |
| + return; |
| + } |
| + UpdateValueAndNotify(); |
| + callbacks.first.Run(value_); |
| + } else if (IsNotifying()) { |
| + UpdateValueAndNotify(); |
| } |
|
ortuno
2016/06/28 16:24:50
We don't expect both conditions to fail right? Cou
jlebel
2016/06/28 18:09:52
Done.
|
| +} |
| + |
| +void BluetoothRemoteGattCharacteristicMac::UpdateValueAndNotify() { |
| NSData* nsdata_value = cb_characteristic_.get().value; |
| const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); |
| value_.assign(buffer, buffer + nsdata_value.length); |
| gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, |
| value_); |
| - callbacks.first.Run(value_); |
| } |
| void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { |
| @@ -241,6 +269,30 @@ void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { |
| callbacks.first.Run(); |
| } |
| +void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState( |
| + NSError* error) { |
| + std::vector<std::pair<NotifySessionCallback, ErrorCallback>> callbacks; |
|
ortuno
2016/06/28 16:24:50
nit: call this reentrant_safe_callbacks
jlebel
2016/06/28 18:09:52
Done.
|
| + callbacks.swap(start_notify_session_callbacks_); |
| + gatt_event_registeration_in_progress_ = false; |
| + if (error) { |
| + VLOG(1) << "Bluetooth error while modifying notification state for " |
| + "characteristic, domain: " |
| + << error.domain.UTF8String << ", error code: " << error.code |
| + << ", localized description: " |
| + << error.localizedDescription.UTF8String; |
| + BluetoothGattService::GattErrorCode error_code = |
| + BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| + for (const auto& callback : callbacks) { |
| + callback.second.Run(error_code); |
| + } |
| + return; |
| + } |
| + for (const auto& callback : callbacks) { |
| + callback.first.Run(base::WrapUnique( |
|
ortuno
2016/06/28 16:24:50
This should be:
base::MakeUnique<BluetoothGattNot
jlebel
2016/06/28 18:09:52
Done.
|
| + new BluetoothGattNotifySessionMac(weak_ptr_factory_.GetWeakPtr()))); |
| + } |
| +} |
| + |
| bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { |
| return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; |
| } |
| @@ -251,6 +303,11 @@ bool BluetoothRemoteGattCharacteristicMac::IsWritable() const { |
| (properties & PROPERTY_WRITE_WITHOUT_RESPONSE); |
| } |
| +bool BluetoothRemoteGattCharacteristicMac::IsNotifiableOrIndicatable() const { |
| + BluetoothGattCharacteristic::Properties properties = GetProperties(); |
| + return (properties & PROPERTY_NOTIFY) || (properties & PROPERTY_INDICATE); |
| +} |
| + |
| CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType() |
| const { |
| return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) |