Chromium Code Reviews| 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_device_mac.h" | 12 #include "device/bluetooth/bluetooth_device_mac.h" |
| 13 #include "device/bluetooth/bluetooth_gatt_notify_session_mac.h" | 13 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 14 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 15 | 15 |
| 16 namespace device { | 16 namespace device { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 static BluetoothGattCharacteristic::Properties ConvertProperties( | 20 static BluetoothGattCharacteristic::Properties ConvertProperties( |
| 21 CBCharacteristicProperties cb_property) { | 21 CBCharacteristicProperties cb_property) { |
| 22 BluetoothGattCharacteristic::Properties result = | 22 BluetoothGattCharacteristic::Properties result = |
| 23 BluetoothGattCharacteristic::PROPERTY_NONE; | 23 BluetoothGattCharacteristic::PROPERTY_NONE; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 const std::string& identifier) const { | 127 const std::string& identifier) const { |
| 128 NOTIMPLEMENTED(); | 128 NOTIMPLEMENTED(); |
| 129 return nullptr; | 129 return nullptr; |
| 130 } | 130 } |
| 131 | 131 |
| 132 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( | 132 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( |
| 133 const NotifySessionCallback& callback, | 133 const NotifySessionCallback& callback, |
| 134 const ErrorCallback& error_callback) { | 134 const ErrorCallback& error_callback) { |
| 135 if (IsNotifying()) { | 135 if (IsNotifying()) { |
| 136 VLOG(2) << "Already notifying. Creating notify session."; | 136 VLOG(2) << "Already notifying. Creating notify session."; |
| 137 std::unique_ptr<BluetoothGattNotifySessionMac> notify_session( | 137 std::unique_ptr<BluetoothGattNotifySession> notify_session( |
| 138 new BluetoothGattNotifySessionMac(weak_ptr_factory_.GetWeakPtr())); | 138 new BluetoothGattNotifySession(weak_ptr_factory_.GetWeakPtr())); |
| 139 base::ThreadTaskRunnerHandle::Get()->PostTask( | 139 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 140 FROM_HERE, | 140 FROM_HERE, |
| 141 base::Bind(callback, base::Passed(std::move(notify_session)))); | 141 base::Bind(callback, base::Passed(std::move(notify_session)))); |
| 142 return; | 142 return; |
| 143 } | 143 } |
| 144 | 144 |
| 145 if (!SupportsNotificationsOrIndications()) { | 145 if (!SupportsNotificationsOrIndications()) { |
| 146 base::ThreadTaskRunnerHandle::Get()->PostTask( | 146 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 147 FROM_HERE, | 147 FROM_HERE, |
| 148 base::Bind(error_callback, | 148 base::Bind(error_callback, |
| 149 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); | 149 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
| 150 return; | 150 return; |
| 151 } | 151 } |
| 152 | 152 |
| 153 start_notify_session_callbacks_.push_back( | 153 start_notify_session_callbacks_.push_back( |
| 154 std::make_pair(callback, error_callback)); | 154 std::make_pair(callback, error_callback)); |
| 155 | 155 |
| 156 if (start_notifications_in_progress_) { | 156 if (start_notifications_in_progress_) { |
| 157 VLOG(2) << "Start Notifications already in progress. " | 157 VLOG(2) << "Start Notifications already in progress. " |
| 158 << "Request has been queued."; | 158 << "Request has been queued."; |
| 159 return; | 159 return; |
| 160 } | 160 } |
| 161 | 161 |
| 162 [gatt_service_->GetCBPeripheral() setNotifyValue:YES | 162 [gatt_service_->GetCBPeripheral() setNotifyValue:YES |
| 163 forCharacteristic:cb_characteristic_.get()]; | 163 forCharacteristic:cb_characteristic_.get()]; |
| 164 start_notifications_in_progress_ = true; | 164 start_notifications_in_progress_ = true; |
| 165 } | 165 } |
| 166 | 166 |
| 167 void BluetoothRemoteGattCharacteristicMac::StopNotifySession( | |
| 168 BluetoothGattNotifySession* session, | |
| 169 const base::Closure& callback) { | |
| 170 NOTIMPLEMENTED(); | |
|
ortuno
2016/08/19 19:13:27
Could you point to an issue to implement this?
tommyt
2016/08/22 06:34:12
Done.
| |
| 171 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
| 172 } | |
| 173 | |
| 167 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( | 174 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( |
| 168 const ValueCallback& callback, | 175 const ValueCallback& callback, |
| 169 const ErrorCallback& error_callback) { | 176 const ErrorCallback& error_callback) { |
| 170 if (!IsReadable()) { | 177 if (!IsReadable()) { |
| 171 base::ThreadTaskRunnerHandle::Get()->PostTask( | 178 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 172 FROM_HERE, | 179 FROM_HERE, |
| 173 base::Bind(error_callback, | 180 base::Bind(error_callback, |
| 174 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); | 181 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
| 175 return; | 182 return; |
| 176 } | 183 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 forCharacteristic:cb_characteristic_ | 223 forCharacteristic:cb_characteristic_ |
| 217 type:write_type]; | 224 type:write_type]; |
| 218 if (write_type == CBCharacteristicWriteWithoutResponse) { | 225 if (write_type == CBCharacteristicWriteWithoutResponse) { |
| 219 base::ThreadTaskRunnerHandle::Get()->PostTask( | 226 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 220 FROM_HERE, | 227 FROM_HERE, |
| 221 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue, | 228 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue, |
| 222 base::Unretained(this), nil)); | 229 base::Unretained(this), nil)); |
| 223 } | 230 } |
| 224 } | 231 } |
| 225 | 232 |
| 233 void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications( | |
| 234 BluetoothRemoteGattDescriptor* ccc_descriptor, | |
| 235 const base::Closure& callback, | |
| 236 const ErrorCallback& error_callback) { | |
| 237 // TODO(http://crbug.com/633191): Implement this method | |
| 238 NOTIMPLEMENTED(); | |
| 239 } | |
| 240 | |
| 241 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( | |
| 242 BluetoothRemoteGattDescriptor* ccc_descriptor, | |
| 243 const base::Closure& callback, | |
| 244 const ErrorCallback& error_callback) { | |
| 245 // TODO(http://crbug.com/633191): Implement this method | |
| 246 NOTIMPLEMENTED(); | |
| 247 } | |
| 248 | |
| 226 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { | 249 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
| 227 // This method is called when the characteristic is read and when a | 250 // This method is called when the characteristic is read and when a |
| 228 // notification is received. | 251 // notification is received. |
| 229 if (characteristic_value_read_or_write_in_progress_) { | 252 if (characteristic_value_read_or_write_in_progress_) { |
| 230 std::pair<ValueCallback, ErrorCallback> callbacks; | 253 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 231 callbacks.swap(read_characteristic_value_callbacks_); | 254 callbacks.swap(read_characteristic_value_callbacks_); |
| 232 characteristic_value_read_or_write_in_progress_ = false; | 255 characteristic_value_read_or_write_in_progress_ = false; |
| 233 if (error) { | 256 if (error) { |
| 234 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " | 257 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " |
| 235 << base::SysNSStringToUTF8(error.domain) | 258 << base::SysNSStringToUTF8(error.domain) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 << ", error code: " << error.code << ", localized description: " | 322 << ", error code: " << error.code << ", localized description: " |
| 300 << base::SysNSStringToUTF8(error.localizedDescription); | 323 << base::SysNSStringToUTF8(error.localizedDescription); |
| 301 BluetoothGattService::GattErrorCode error_code = | 324 BluetoothGattService::GattErrorCode error_code = |
| 302 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 325 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 303 for (const auto& callback : reentrant_safe_callbacks) { | 326 for (const auto& callback : reentrant_safe_callbacks) { |
| 304 callback.second.Run(error_code); | 327 callback.second.Run(error_code); |
| 305 } | 328 } |
| 306 return; | 329 return; |
| 307 } | 330 } |
| 308 for (const auto& callback : reentrant_safe_callbacks) { | 331 for (const auto& callback : reentrant_safe_callbacks) { |
| 309 callback.first.Run(base::MakeUnique<BluetoothGattNotifySessionMac>( | 332 callback.first.Run(base::MakeUnique<BluetoothGattNotifySession>( |
| 310 weak_ptr_factory_.GetWeakPtr())); | 333 weak_ptr_factory_.GetWeakPtr())); |
| 311 } | 334 } |
| 312 } | 335 } |
| 313 | 336 |
| 314 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { | 337 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { |
| 315 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; | 338 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; |
| 316 } | 339 } |
| 317 | 340 |
| 318 bool BluetoothRemoteGattCharacteristicMac::IsWritable() const { | 341 bool BluetoothRemoteGattCharacteristicMac::IsWritable() const { |
| 319 BluetoothGattCharacteristic::Properties properties = GetProperties(); | 342 BluetoothGattCharacteristic::Properties properties = GetProperties(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 332 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) | 355 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) |
| 333 ? CBCharacteristicWriteWithResponse | 356 ? CBCharacteristicWriteWithResponse |
| 334 : CBCharacteristicWriteWithoutResponse; | 357 : CBCharacteristicWriteWithoutResponse; |
| 335 } | 358 } |
| 336 | 359 |
| 337 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() | 360 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() |
| 338 const { | 361 const { |
| 339 return cb_characteristic_.get(); | 362 return cb_characteristic_.get(); |
| 340 } | 363 } |
| 341 } // namespace device. | 364 } // namespace device. |
| OLD | NEW |