| 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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 BluetoothRemoteGattCharacteristicMac::GetDescriptor( | 143 BluetoothRemoteGattCharacteristicMac::GetDescriptor( |
| 144 const std::string& identifier) const { | 144 const std::string& identifier) const { |
| 145 auto searched_pair = gatt_descriptor_macs_.find(identifier); | 145 auto searched_pair = gatt_descriptor_macs_.find(identifier); |
| 146 if (searched_pair == gatt_descriptor_macs_.end()) { | 146 if (searched_pair == gatt_descriptor_macs_.end()) { |
| 147 return nullptr; | 147 return nullptr; |
| 148 } | 148 } |
| 149 return static_cast<BluetoothRemoteGattDescriptor*>( | 149 return static_cast<BluetoothRemoteGattDescriptor*>( |
| 150 searched_pair->second.get()); | 150 searched_pair->second.get()); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( |
| 154 const NotifySessionCallback& callback, |
| 155 const ErrorCallback& error_callback) { |
| 156 if (IsNotifying()) { |
| 157 VLOG(2) << "Already notifying. Creating notify session."; |
| 158 std::unique_ptr<BluetoothGattNotifySession> notify_session( |
| 159 new BluetoothGattNotifySession(weak_ptr_factory_.GetWeakPtr())); |
| 160 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 161 FROM_HERE, |
| 162 base::Bind(callback, base::Passed(std::move(notify_session)))); |
| 163 return; |
| 164 } |
| 165 |
| 166 if (!SupportsNotificationsOrIndications()) { |
| 167 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 168 FROM_HERE, |
| 169 base::Bind(error_callback, |
| 170 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
| 171 return; |
| 172 } |
| 173 |
| 174 start_notify_session_callbacks_.push_back( |
| 175 std::make_pair(callback, error_callback)); |
| 176 |
| 177 if (start_notifications_in_progress_) { |
| 178 VLOG(2) << "Start Notifications already in progress. " |
| 179 << "Request has been queued."; |
| 180 return; |
| 181 } |
| 182 |
| 183 [GetCBPeripheral() setNotifyValue:YES |
| 184 forCharacteristic:cb_characteristic_.get()]; |
| 185 start_notifications_in_progress_ = true; |
| 186 } |
| 187 |
| 188 void BluetoothRemoteGattCharacteristicMac::StopNotifySession( |
| 189 BluetoothGattNotifySession* session, |
| 190 const base::Closure& callback) { |
| 191 // TODO(http://crbug.com/633191): Remove this method and use the base version. |
| 192 // Instead, we should implement SubscribeToNotifications and |
| 193 // UnsubscribeFromNotifications. |
| 194 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 195 } |
| 196 |
| 153 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( | 197 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( |
| 154 const ValueCallback& callback, | 198 const ValueCallback& callback, |
| 155 const ErrorCallback& error_callback) { | 199 const ErrorCallback& error_callback) { |
| 156 if (!IsReadable()) { | 200 if (!IsReadable()) { |
| 157 base::ThreadTaskRunnerHandle::Get()->PostTask( | 201 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 158 FROM_HERE, | 202 FROM_HERE, |
| 159 base::Bind(error_callback, | 203 base::Bind(error_callback, |
| 160 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); | 204 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
| 161 return; | 205 return; |
| 162 } | 206 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 FROM_HERE, | 249 FROM_HERE, |
| 206 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue, | 250 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue, |
| 207 base::Unretained(this), nil)); | 251 base::Unretained(this), nil)); |
| 208 } | 252 } |
| 209 } | 253 } |
| 210 | 254 |
| 211 void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications( | 255 void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications( |
| 212 BluetoothRemoteGattDescriptor* ccc_descriptor, | 256 BluetoothRemoteGattDescriptor* ccc_descriptor, |
| 213 const base::Closure& callback, | 257 const base::Closure& callback, |
| 214 const ErrorCallback& error_callback) { | 258 const ErrorCallback& error_callback) { |
| 215 if (IsNotifying()) { | 259 // TODO(http://crbug.com/633191): Implement this method |
| 216 VLOG(2) << "Already notifying. Creating notify session."; | 260 NOTIMPLEMENTED(); |
| 217 std::unique_ptr<BluetoothGattNotifySession> notify_session( | |
| 218 new BluetoothGattNotifySession(weak_ptr_factory_.GetWeakPtr())); | |
| 219 callback.Run(); | |
| 220 return; | |
| 221 } | |
| 222 | |
| 223 if (!SupportsNotificationsOrIndications()) { | |
| 224 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 225 FROM_HERE, | |
| 226 base::Bind(error_callback, | |
| 227 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); | |
| 228 return; | |
| 229 } | |
| 230 | |
| 231 start_notify_session_callbacks_.push_back( | |
| 232 std::make_pair(callback, error_callback)); | |
| 233 | |
| 234 if (start_notifications_in_progress_) { | |
| 235 VLOG(2) << "Start Notifications already in progress. " | |
| 236 << "Request has been queued."; | |
| 237 return; | |
| 238 } | |
| 239 | |
| 240 [GetCBPeripheral() setNotifyValue:YES | |
| 241 forCharacteristic:cb_characteristic_.get()]; | |
| 242 start_notifications_in_progress_ = true; | |
| 243 } | 261 } |
| 244 | 262 |
| 245 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( | 263 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( |
| 246 BluetoothRemoteGattDescriptor* ccc_descriptor, | 264 BluetoothRemoteGattDescriptor* ccc_descriptor, |
| 247 const base::Closure& callback, | 265 const base::Closure& callback, |
| 248 const ErrorCallback& error_callback) { | 266 const ErrorCallback& error_callback) { |
| 249 // TODO(http://crbug.com/633191): Implement this method | 267 // TODO(http://crbug.com/633191): Implement this method |
| 250 NOTIMPLEMENTED(); | 268 NOTIMPLEMENTED(); |
| 251 } | 269 } |
| 252 | 270 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 BluetoothGattService::GattErrorCode error_code = | 329 BluetoothGattService::GattErrorCode error_code = |
| 312 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 330 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 313 callbacks.second.Run(error_code); | 331 callbacks.second.Run(error_code); |
| 314 return; | 332 return; |
| 315 } | 333 } |
| 316 callbacks.first.Run(); | 334 callbacks.first.Run(); |
| 317 } | 335 } |
| 318 | 336 |
| 319 void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState( | 337 void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState( |
| 320 NSError* error) { | 338 NSError* error) { |
| 321 std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; | 339 std::vector<std::pair<NotifySessionCallback, ErrorCallback>> |
| 340 reentrant_safe_callbacks; |
| 322 reentrant_safe_callbacks.swap(start_notify_session_callbacks_); | 341 reentrant_safe_callbacks.swap(start_notify_session_callbacks_); |
| 323 start_notifications_in_progress_ = false; | 342 start_notifications_in_progress_ = false; |
| 324 if (error) { | 343 if (error) { |
| 325 VLOG(1) << "Bluetooth error while modifying notification state for " | 344 VLOG(1) << "Bluetooth error while modifying notification state for " |
| 326 "characteristic, domain: " | 345 "characteristic, domain: " |
| 327 << base::SysNSStringToUTF8(error.domain) | 346 << base::SysNSStringToUTF8(error.domain) |
| 328 << ", error code: " << error.code << ", localized description: " | 347 << ", error code: " << error.code << ", localized description: " |
| 329 << base::SysNSStringToUTF8(error.localizedDescription); | 348 << base::SysNSStringToUTF8(error.localizedDescription); |
| 330 BluetoothGattService::GattErrorCode error_code = | 349 BluetoothGattService::GattErrorCode error_code = |
| 331 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 350 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 332 for (const auto& callback : reentrant_safe_callbacks) { | 351 for (const auto& callback : reentrant_safe_callbacks) { |
| 333 callback.second.Run(error_code); | 352 callback.second.Run(error_code); |
| 334 } | 353 } |
| 335 return; | 354 return; |
| 336 } | 355 } |
| 337 for (const auto& callback : reentrant_safe_callbacks) { | 356 for (const auto& callback : reentrant_safe_callbacks) { |
| 338 callback.first.Run(); | 357 callback.first.Run(base::MakeUnique<BluetoothGattNotifySession>( |
| 358 weak_ptr_factory_.GetWeakPtr())); |
| 339 } | 359 } |
| 340 } | 360 } |
| 341 | 361 |
| 342 void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { | 362 void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { |
| 343 DCHECK(!is_discovery_complete_); | 363 DCHECK(!is_discovery_complete_); |
| 344 std::unordered_set<std::string> descriptor_identifier_to_remove; | 364 std::unordered_set<std::string> descriptor_identifier_to_remove; |
| 345 for (const auto& iter : gatt_descriptor_macs_) { | 365 for (const auto& iter : gatt_descriptor_macs_) { |
| 346 descriptor_identifier_to_remove.insert(iter.first); | 366 descriptor_identifier_to_remove.insert(iter.first); |
| 347 } | 367 } |
| 348 | 368 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 pair) { | 445 pair) { |
| 426 return pair.second->GetCBDescriptor() == cb_descriptor; | 446 return pair.second->GetCBDescriptor() == cb_descriptor; |
| 427 }); | 447 }); |
| 428 if (found == gatt_descriptor_macs_.end()) { | 448 if (found == gatt_descriptor_macs_.end()) { |
| 429 return nullptr; | 449 return nullptr; |
| 430 } else { | 450 } else { |
| 431 return found->second.get(); | 451 return found->second.get(); |
| 432 } | 452 } |
| 433 } | 453 } |
| 434 } // namespace device. | 454 } // namespace device. |
| OLD | NEW |