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 | |
197 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( | 153 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( |
198 const ValueCallback& callback, | 154 const ValueCallback& callback, |
199 const ErrorCallback& error_callback) { | 155 const ErrorCallback& error_callback) { |
200 if (!IsReadable()) { | 156 if (!IsReadable()) { |
201 base::ThreadTaskRunnerHandle::Get()->PostTask( | 157 base::ThreadTaskRunnerHandle::Get()->PostTask( |
202 FROM_HERE, | 158 FROM_HERE, |
203 base::Bind(error_callback, | 159 base::Bind(error_callback, |
204 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); | 160 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); |
205 return; | 161 return; |
206 } | 162 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 FROM_HERE, | 205 FROM_HERE, |
250 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue, | 206 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue, |
251 base::Unretained(this), nil)); | 207 base::Unretained(this), nil)); |
252 } | 208 } |
253 } | 209 } |
254 | 210 |
255 void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications( | 211 void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications( |
256 BluetoothRemoteGattDescriptor* ccc_descriptor, | 212 BluetoothRemoteGattDescriptor* ccc_descriptor, |
257 const base::Closure& callback, | 213 const base::Closure& callback, |
258 const ErrorCallback& error_callback) { | 214 const ErrorCallback& error_callback) { |
259 // TODO(http://crbug.com/633191): Implement this method | 215 if (IsNotifying()) { |
ortuno
2017/01/16 22:56:26
No need for this check. This is already handled by
jlebel
2017/01/20 14:04:58
Done.
| |
260 NOTIMPLEMENTED(); | 216 VLOG(2) << "Already notifying. Creating notify session."; |
217 std::unique_ptr<BluetoothGattNotifySession> notify_session( | |
218 new BluetoothGattNotifySession(weak_ptr_factory_.GetWeakPtr())); | |
219 callback.Run(); | |
220 return; | |
221 } | |
222 | |
223 if (!SupportsNotificationsOrIndications()) { | |
ortuno
2017/01/16 22:56:26
Same here this is already handled by StartNotifySe
jlebel
2017/01/20 14:04:58
Done.
| |
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( | |
ortuno
2017/01/16 22:56:26
We should just DCHECK that the pair is empty.
| |
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; | |
261 } | 243 } |
262 | 244 |
263 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( | 245 void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications( |
264 BluetoothRemoteGattDescriptor* ccc_descriptor, | 246 BluetoothRemoteGattDescriptor* ccc_descriptor, |
265 const base::Closure& callback, | 247 const base::Closure& callback, |
266 const ErrorCallback& error_callback) { | 248 const ErrorCallback& error_callback) { |
267 // TODO(http://crbug.com/633191): Implement this method | 249 // TODO(http://crbug.com/633191): Implement this method |
268 NOTIMPLEMENTED(); | 250 NOTIMPLEMENTED(); |
269 } | 251 } |
270 | 252 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 BluetoothGattService::GattErrorCode error_code = | 311 BluetoothGattService::GattErrorCode error_code = |
330 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 312 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
331 callbacks.second.Run(error_code); | 313 callbacks.second.Run(error_code); |
332 return; | 314 return; |
333 } | 315 } |
334 callbacks.first.Run(); | 316 callbacks.first.Run(); |
335 } | 317 } |
336 | 318 |
337 void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState( | 319 void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState( |
338 NSError* error) { | 320 NSError* error) { |
339 std::vector<std::pair<NotifySessionCallback, ErrorCallback>> | 321 std::vector<PendingStartNotifyCall> reentrant_safe_callbacks; |
340 reentrant_safe_callbacks; | |
341 reentrant_safe_callbacks.swap(start_notify_session_callbacks_); | 322 reentrant_safe_callbacks.swap(start_notify_session_callbacks_); |
342 start_notifications_in_progress_ = false; | 323 start_notifications_in_progress_ = false; |
343 if (error) { | 324 if (error) { |
344 VLOG(1) << "Bluetooth error while modifying notification state for " | 325 VLOG(1) << "Bluetooth error while modifying notification state for " |
345 "characteristic, domain: " | 326 "characteristic, domain: " |
346 << base::SysNSStringToUTF8(error.domain) | 327 << base::SysNSStringToUTF8(error.domain) |
347 << ", error code: " << error.code << ", localized description: " | 328 << ", error code: " << error.code << ", localized description: " |
348 << base::SysNSStringToUTF8(error.localizedDescription); | 329 << base::SysNSStringToUTF8(error.localizedDescription); |
349 BluetoothGattService::GattErrorCode error_code = | 330 BluetoothGattService::GattErrorCode error_code = |
350 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 331 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
351 for (const auto& callback : reentrant_safe_callbacks) { | 332 for (const auto& callback : reentrant_safe_callbacks) { |
352 callback.second.Run(error_code); | 333 callback.second.Run(error_code); |
353 } | 334 } |
354 return; | 335 return; |
355 } | 336 } |
356 for (const auto& callback : reentrant_safe_callbacks) { | 337 for (const auto& callback : reentrant_safe_callbacks) { |
357 callback.first.Run(base::MakeUnique<BluetoothGattNotifySession>( | 338 callback.first.Run(); |
358 weak_ptr_factory_.GetWeakPtr())); | |
359 } | 339 } |
360 } | 340 } |
361 | 341 |
362 void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { | 342 void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { |
363 DCHECK(!is_discovery_complete_); | 343 DCHECK(!is_discovery_complete_); |
364 std::unordered_set<std::string> descriptor_identifier_to_remove; | 344 std::unordered_set<std::string> descriptor_identifier_to_remove; |
365 for (const auto& iter : gatt_descriptor_macs_) { | 345 for (const auto& iter : gatt_descriptor_macs_) { |
366 descriptor_identifier_to_remove.insert(iter.first); | 346 descriptor_identifier_to_remove.insert(iter.first); |
367 } | 347 } |
368 | 348 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 pair) { | 425 pair) { |
446 return pair.second->GetCBDescriptor() == cb_descriptor; | 426 return pair.second->GetCBDescriptor() == cb_descriptor; |
447 }); | 427 }); |
448 if (found == gatt_descriptor_macs_.end()) { | 428 if (found == gatt_descriptor_macs_.end()) { |
449 return nullptr; | 429 return nullptr; |
450 } else { | 430 } else { |
451 return found->second.get(); | 431 return found->second.get(); |
452 } | 432 } |
453 } | 433 } |
454 } // namespace device. | 434 } // namespace device. |
OLD | NEW |