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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 base::Unretained(this), nil)); | 205 base::Unretained(this), nil)); |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications( | 209 void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications( |
210 BluetoothRemoteGattDescriptor* ccc_descriptor, | 210 BluetoothRemoteGattDescriptor* ccc_descriptor, |
211 const base::Closure& callback, | 211 const base::Closure& callback, |
212 const ErrorCallback& error_callback) { | 212 const ErrorCallback& error_callback) { |
213 DCHECK(subscribe_to_notification_callbacks_.first.is_null()); | 213 DCHECK(subscribe_to_notification_callbacks_.first.is_null()); |
214 DCHECK(subscribe_to_notification_callbacks_.second.is_null()); | 214 DCHECK(subscribe_to_notification_callbacks_.second.is_null()); |
| 215 DCHECK(unsubscribe_from_notification_callbacks_.first.is_null()); |
| 216 DCHECK(unsubscribe_from_notification_callbacks_.second.is_null()); |
215 subscribe_to_notification_callbacks_ = | 217 subscribe_to_notification_callbacks_ = |
216 std::make_pair(callback, error_callback); | 218 std::make_pair(callback, error_callback); |
217 [GetCBPeripheral() setNotifyValue:YES | 219 [GetCBPeripheral() setNotifyValue:YES |
218 forCharacteristic:cb_characteristic_.get()]; | 220 forCharacteristic:cb_characteristic_.get()]; |
219 } | 221 } |
220 | 222 |
221 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( | 223 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( |
222 BluetoothRemoteGattDescriptor* ccc_descriptor, | 224 BluetoothRemoteGattDescriptor* ccc_descriptor, |
223 const base::Closure& callback, | 225 const base::Closure& callback, |
224 const ErrorCallback& error_callback) { | 226 const ErrorCallback& error_callback) { |
225 // TODO(http://crbug.com/633191): Implement this method | 227 DCHECK(subscribe_to_notification_callbacks_.first.is_null()); |
226 NOTIMPLEMENTED(); | 228 DCHECK(subscribe_to_notification_callbacks_.second.is_null()); |
| 229 DCHECK(unsubscribe_from_notification_callbacks_.first.is_null()); |
| 230 DCHECK(unsubscribe_from_notification_callbacks_.second.is_null()); |
| 231 unsubscribe_from_notification_callbacks_ = |
| 232 std::make_pair(callback, error_callback); |
| 233 [GetCBPeripheral() setNotifyValue:NO |
| 234 forCharacteristic:cb_characteristic_.get()]; |
227 } | 235 } |
228 | 236 |
229 void BluetoothRemoteGattCharacteristicMac::DiscoverDescriptors() { | 237 void BluetoothRemoteGattCharacteristicMac::DiscoverDescriptors() { |
230 is_discovery_complete_ = false; | 238 is_discovery_complete_ = false; |
231 [GetCBPeripheral() | 239 [GetCBPeripheral() |
232 discoverDescriptorsForCharacteristic:cb_characteristic_.get()]; | 240 discoverDescriptorsForCharacteristic:cb_characteristic_.get()]; |
233 } | 241 } |
234 | 242 |
235 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { | 243 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
236 CHECK_EQ(GetCBPeripheral().state, CBPeripheralStateConnected); | 244 CHECK_EQ(GetCBPeripheral().state, CBPeripheralStateConnected); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 296 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
289 callbacks.second.Run(error_code); | 297 callbacks.second.Run(error_code); |
290 return; | 298 return; |
291 } | 299 } |
292 callbacks.first.Run(); | 300 callbacks.first.Run(); |
293 } | 301 } |
294 | 302 |
295 void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState( | 303 void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState( |
296 NSError* error) { | 304 NSError* error) { |
297 PendingNotifyCallbacks reentrant_safe_callbacks; | 305 PendingNotifyCallbacks reentrant_safe_callbacks; |
298 reentrant_safe_callbacks.swap(subscribe_to_notification_callbacks_); | 306 if (!subscribe_to_notification_callbacks_.first.is_null()) { |
| 307 DCHECK([GetCBCharacteristic() isNotifying] || error); |
| 308 reentrant_safe_callbacks.swap(subscribe_to_notification_callbacks_); |
| 309 } else if (!unsubscribe_from_notification_callbacks_.first.is_null()) { |
| 310 DCHECK(![GetCBCharacteristic() isNotifying] || error); |
| 311 reentrant_safe_callbacks.swap(unsubscribe_from_notification_callbacks_); |
| 312 } else { |
| 313 VLOG(1) << "No pending notification update for characteristic " |
| 314 << GetUUID().value(); |
| 315 return; |
| 316 } |
299 if (error) { | 317 if (error) { |
300 VLOG(1) << "Bluetooth error while modifying notification state for " | 318 VLOG(1) << "Bluetooth error while modifying notification state for " |
301 "characteristic, domain: " | 319 "characteristic, domain: " |
302 << base::SysNSStringToUTF8(error.domain) | 320 << base::SysNSStringToUTF8(error.domain) |
303 << ", error code: " << error.code << ", localized description: " | 321 << ", error code: " << error.code << ", localized description: " |
304 << base::SysNSStringToUTF8(error.localizedDescription); | 322 << base::SysNSStringToUTF8(error.localizedDescription); |
305 BluetoothGattService::GattErrorCode error_code = | 323 BluetoothGattService::GattErrorCode error_code = |
306 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 324 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
307 if (!reentrant_safe_callbacks.second.is_null()) { | 325 reentrant_safe_callbacks.second.Run(error_code); |
308 reentrant_safe_callbacks.second.Run(error_code); | |
309 } | |
310 return; | 326 return; |
311 } | 327 } |
312 if (!reentrant_safe_callbacks.first.is_null()) { | 328 reentrant_safe_callbacks.first.Run(); |
313 reentrant_safe_callbacks.first.Run(); | |
314 } | |
315 } | 329 } |
316 | 330 |
317 void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { | 331 void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { |
318 DCHECK(!is_discovery_complete_); | 332 DCHECK(!is_discovery_complete_); |
319 std::unordered_set<std::string> descriptor_identifier_to_remove; | 333 std::unordered_set<std::string> descriptor_identifier_to_remove; |
320 for (const auto& iter : gatt_descriptor_macs_) { | 334 for (const auto& iter : gatt_descriptor_macs_) { |
321 descriptor_identifier_to_remove.insert(iter.first); | 335 descriptor_identifier_to_remove.insert(iter.first); |
322 } | 336 } |
323 | 337 |
324 for (CBDescriptor* cb_descriptor in cb_characteristic_.get().descriptors) { | 338 for (CBDescriptor* cb_descriptor in cb_characteristic_.get().descriptors) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 pair) { | 414 pair) { |
401 return pair.second->GetCBDescriptor() == cb_descriptor; | 415 return pair.second->GetCBDescriptor() == cb_descriptor; |
402 }); | 416 }); |
403 if (found == gatt_descriptor_macs_.end()) { | 417 if (found == gatt_descriptor_macs_.end()) { |
404 return nullptr; | 418 return nullptr; |
405 } else { | 419 } else { |
406 return found->second.get(); | 420 return found->second.get(); |
407 } | 421 } |
408 } | 422 } |
409 } // namespace device. | 423 } // namespace device. |
OLD | NEW |