Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_connection.cc

Issue 1898643002: Refactor device::BluetoothGattXXX classes to split into remote/local. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/proximity_auth/ble/bluetooth_low_energy_connection.h" 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "base/task_runner.h" 13 #include "base/task_runner.h"
14 #include "base/thread_task_runner_handle.h" 14 #include "base/thread_task_runner_handle.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" 16 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h"
17 #include "components/proximity_auth/ble/fake_wire_message.h" 17 #include "components/proximity_auth/ble/fake_wire_message.h"
18 #include "components/proximity_auth/bluetooth_throttler.h" 18 #include "components/proximity_auth/bluetooth_throttler.h"
19 #include "components/proximity_auth/connection_finder.h" 19 #include "components/proximity_auth/connection_finder.h"
20 #include "components/proximity_auth/logging/logging.h" 20 #include "components/proximity_auth/logging/logging.h"
21 #include "components/proximity_auth/wire_message.h" 21 #include "components/proximity_auth/wire_message.h"
22 #include "device/bluetooth/bluetooth_adapter.h" 22 #include "device/bluetooth/bluetooth_adapter.h"
23 #include "device/bluetooth/bluetooth_device.h" 23 #include "device/bluetooth/bluetooth_device.h"
24 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
25 #include "device/bluetooth/bluetooth_gatt_connection.h" 24 #include "device/bluetooth/bluetooth_gatt_connection.h"
26 #include "device/bluetooth/bluetooth_gatt_notify_session.h" 25 #include "device/bluetooth/bluetooth_gatt_notify_session.h"
26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
27 #include "device/bluetooth/bluetooth_uuid.h" 27 #include "device/bluetooth/bluetooth_uuid.h"
28 28
29 using device::BluetoothAdapter; 29 using device::BluetoothAdapter;
30 using device::BluetoothDevice; 30 using device::BluetoothDevice;
31 using device::BluetoothGattConnection; 31 using device::BluetoothGattConnection;
32 using device::BluetoothGattService; 32 using device::BluetoothRemoteGattService;
33 using device::BluetoothGattCharacteristic; 33 using device::BluetoothRemoteGattCharacteristic;
34 using device::BluetoothGattNotifySession; 34 using device::BluetoothGattNotifySession;
35 using device::BluetoothUUID; 35 using device::BluetoothUUID;
36 36
37 namespace proximity_auth { 37 namespace proximity_auth {
38 namespace { 38 namespace {
39 39
40 // The UUID of the characteristic used to send data to the peripheral. 40 // The UUID of the characteristic used to send data to the peripheral.
41 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; 41 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a";
42 42
43 // The UUID of the characteristic used to receive data from the peripheral. 43 // The UUID of the characteristic used to receive data from the peripheral.
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (sub_status_ == SubStatus::DISCONNECTED || 240 if (sub_status_ == SubStatus::DISCONNECTED ||
241 device->GetAddress() != GetDeviceAddress()) 241 device->GetAddress() != GetDeviceAddress())
242 return; 242 return;
243 243
244 PA_LOG(INFO) << "Device removed " << GetDeviceAddress(); 244 PA_LOG(INFO) << "Device removed " << GetDeviceAddress();
245 Disconnect(); 245 Disconnect();
246 } 246 }
247 247
248 void BluetoothLowEnergyConnection::GattCharacteristicValueChanged( 248 void BluetoothLowEnergyConnection::GattCharacteristicValueChanged(
249 BluetoothAdapter* adapter, 249 BluetoothAdapter* adapter,
250 BluetoothGattCharacteristic* characteristic, 250 BluetoothRemoteGattCharacteristic* characteristic,
251 const std::vector<uint8_t>& value) { 251 const std::vector<uint8_t>& value) {
252 DCHECK_EQ(adapter, adapter_.get()); 252 DCHECK_EQ(adapter, adapter_.get());
253 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL && 253 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL &&
254 sub_status() != SubStatus::CONNECTED) 254 sub_status() != SubStatus::CONNECTED)
255 return; 255 return;
256 256
257 PA_LOG(INFO) << "Characteristic value changed: " 257 PA_LOG(INFO) << "Characteristic value changed: "
258 << characteristic->GetUUID().canonical_value(); 258 << characteristic->GetUUID().canonical_value();
259 259
260 if (characteristic->GetIdentifier() == from_peripheral_char_.id) { 260 if (characteristic->GetIdentifier() == from_peripheral_char_.id) {
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 : "") 395 : "")
396 << (from_peripheral_char.id.empty() 396 << (from_peripheral_char.id.empty()
397 ? ", " + from_peripheral_char.uuid.canonical_value() 397 ? ", " + from_peripheral_char.uuid.canonical_value()
398 : "") << " not found."; 398 : "") << " not found.";
399 399
400 Disconnect(); 400 Disconnect();
401 } 401 }
402 402
403 void BluetoothLowEnergyConnection::StartNotifySession() { 403 void BluetoothLowEnergyConnection::StartNotifySession() {
404 if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) { 404 if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) {
405 BluetoothGattCharacteristic* characteristic = 405 BluetoothRemoteGattCharacteristic* characteristic =
406 GetGattCharacteristic(from_peripheral_char_.id); 406 GetGattCharacteristic(from_peripheral_char_.id);
407 DCHECK(characteristic); 407 DCHECK(characteristic);
408 408
409 // This is a workaround for crbug.com/507325. If |characteristic| is already 409 // This is a workaround for crbug.com/507325. If |characteristic| is already
410 // notifying |characteristic->StartNotifySession()| will fail with 410 // notifying |characteristic->StartNotifySession()| will fail with
411 // GATT_ERROR_FAILED. 411 // GATT_ERROR_FAILED.
412 if (characteristic->IsNotifying()) { 412 if (characteristic->IsNotifying()) {
413 PA_LOG(INFO) << characteristic->GetUUID().canonical_value() 413 PA_LOG(INFO) << characteristic->GetUUID().canonical_value()
414 << " already notifying."; 414 << " already notifying.";
415 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); 415 SetSubStatus(SubStatus::NOTIFY_SESSION_READY);
416 SendInviteToConnectSignal(); 416 SendInviteToConnectSignal();
417 return; 417 return;
418 } 418 }
419 419
420 SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION); 420 SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION);
421 characteristic->StartNotifySession( 421 characteristic->StartNotifySession(
422 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionStarted, 422 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionStarted,
423 weak_ptr_factory_.GetWeakPtr()), 423 weak_ptr_factory_.GetWeakPtr()),
424 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionError, 424 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionError,
425 weak_ptr_factory_.GetWeakPtr())); 425 weak_ptr_factory_.GetWeakPtr()));
426 } 426 }
427 } 427 }
428 428
429 void BluetoothLowEnergyConnection::OnNotifySessionError( 429 void BluetoothLowEnergyConnection::OnNotifySessionError(
430 BluetoothGattService::GattErrorCode error) { 430 BluetoothRemoteGattService::GattErrorCode error) {
431 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); 431 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION);
432 PA_LOG(WARNING) << "Error starting notification session: " << error; 432 PA_LOG(WARNING) << "Error starting notification session: " << error;
433 Disconnect(); 433 Disconnect();
434 } 434 }
435 435
436 void BluetoothLowEnergyConnection::OnNotifySessionStarted( 436 void BluetoothLowEnergyConnection::OnNotifySessionStarted(
437 scoped_ptr<BluetoothGattNotifySession> notify_session) { 437 scoped_ptr<BluetoothGattNotifySession> notify_session) {
438 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); 438 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION);
439 PA_LOG(INFO) << "Notification session started " 439 PA_LOG(INFO) << "Notification session started "
440 << notify_session->GetCharacteristicIdentifier(); 440 << notify_session->GetCharacteristicIdentifier();
(...skipping 26 matching lines...) Expand all
467 } 467 }
468 } 468 }
469 469
470 void BluetoothLowEnergyConnection::WriteRemoteCharacteristic( 470 void BluetoothLowEnergyConnection::WriteRemoteCharacteristic(
471 WriteRequest request) { 471 WriteRequest request) {
472 write_requests_queue_.push(request); 472 write_requests_queue_.push(request);
473 ProcessNextWriteRequest(); 473 ProcessNextWriteRequest();
474 } 474 }
475 475
476 void BluetoothLowEnergyConnection::ProcessNextWriteRequest() { 476 void BluetoothLowEnergyConnection::ProcessNextWriteRequest() {
477 BluetoothGattCharacteristic* characteristic = 477 BluetoothRemoteGattCharacteristic* characteristic =
478 GetGattCharacteristic(to_peripheral_char_.id); 478 GetGattCharacteristic(to_peripheral_char_.id);
479 if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ && 479 if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ &&
480 characteristic) { 480 characteristic) {
481 write_remote_characteristic_pending_ = true; 481 write_remote_characteristic_pending_ = true;
482 WriteRequest next_request = write_requests_queue_.front(); 482 WriteRequest next_request = write_requests_queue_.front();
483 PA_LOG(INFO) << "Writing characteristic..."; 483 PA_LOG(INFO) << "Writing characteristic...";
484 characteristic->WriteRemoteCharacteristic( 484 characteristic->WriteRemoteCharacteristic(
485 next_request.value, 485 next_request.value,
486 base::Bind(&BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten, 486 base::Bind(&BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten,
487 weak_ptr_factory_.GetWeakPtr(), 487 weak_ptr_factory_.GetWeakPtr(),
(...skipping 14 matching lines...) Expand all
502 OnDidSendMessage(WireMessage(std::string(), std::string()), true); 502 OnDidSendMessage(WireMessage(std::string(), std::string()), true);
503 503
504 // Removes the top of queue (already processed) and process the next request. 504 // Removes the top of queue (already processed) and process the next request.
505 DCHECK(!write_requests_queue_.empty()); 505 DCHECK(!write_requests_queue_.empty());
506 write_requests_queue_.pop(); 506 write_requests_queue_.pop();
507 ProcessNextWriteRequest(); 507 ProcessNextWriteRequest();
508 } 508 }
509 509
510 void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError( 510 void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError(
511 bool run_did_send_message_callback, 511 bool run_did_send_message_callback,
512 BluetoothGattService::GattErrorCode error) { 512 BluetoothRemoteGattService::GattErrorCode error) {
513 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " 513 PA_LOG(WARNING) << "Error " << error << " writing characteristic: "
514 << to_peripheral_char_.uuid.canonical_value(); 514 << to_peripheral_char_.uuid.canonical_value();
515 write_remote_characteristic_pending_ = false; 515 write_remote_characteristic_pending_ = false;
516 // TODO(sacomoto): Actually pass the current message to the observer. 516 // TODO(sacomoto): Actually pass the current message to the observer.
517 if (run_did_send_message_callback) 517 if (run_did_send_message_callback)
518 OnDidSendMessage(WireMessage(std::string(), std::string()), false); 518 OnDidSendMessage(WireMessage(std::string(), std::string()), false);
519 519
520 // Increases the number of failed attempts and retry. 520 // Increases the number of failed attempts and retry.
521 DCHECK(!write_requests_queue_.empty()); 521 DCHECK(!write_requests_queue_.empty());
522 if (++write_requests_queue_.front().number_of_failed_attempts >= 522 if (++write_requests_queue_.front().number_of_failed_attempts >=
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 // crbug.com/497841). 560 // crbug.com/497841).
561 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); 561 std::vector<BluetoothDevice*> devices = adapter_->GetDevices();
562 for (const auto& device : devices) { 562 for (const auto& device : devices) {
563 if (device->GetAddress() == GetDeviceAddress()) 563 if (device->GetAddress() == GetDeviceAddress())
564 return device; 564 return device;
565 } 565 }
566 566
567 return nullptr; 567 return nullptr;
568 } 568 }
569 569
570 BluetoothGattService* BluetoothLowEnergyConnection::GetRemoteService() { 570 BluetoothRemoteGattService* BluetoothLowEnergyConnection::GetRemoteService() {
571 BluetoothDevice* remote_device = GetRemoteDevice(); 571 BluetoothDevice* remote_device = GetRemoteDevice();
572 if (!remote_device) { 572 if (!remote_device) {
573 PA_LOG(WARNING) << "Remote device not found."; 573 PA_LOG(WARNING) << "Remote device not found.";
574 return NULL; 574 return NULL;
575 } 575 }
576 if (remote_service_.id.empty()) { 576 if (remote_service_.id.empty()) {
577 std::vector<BluetoothGattService*> services = 577 std::vector<BluetoothRemoteGattService*> services =
578 remote_device->GetGattServices(); 578 remote_device->GetGattServices();
579 for (const auto& service : services) 579 for (const auto& service : services)
580 if (service->GetUUID() == remote_service_.uuid) { 580 if (service->GetUUID() == remote_service_.uuid) {
581 remote_service_.id = service->GetIdentifier(); 581 remote_service_.id = service->GetIdentifier();
582 break; 582 break;
583 } 583 }
584 } 584 }
585 return remote_device->GetGattService(remote_service_.id); 585 return remote_device->GetGattService(remote_service_.id);
586 } 586 }
587 587
588 BluetoothGattCharacteristic* 588 BluetoothRemoteGattCharacteristic*
589 BluetoothLowEnergyConnection::GetGattCharacteristic( 589 BluetoothLowEnergyConnection::GetGattCharacteristic(
590 const std::string& gatt_characteristic) { 590 const std::string& gatt_characteristic) {
591 BluetoothGattService* remote_service = GetRemoteService(); 591 BluetoothRemoteGattService* remote_service = GetRemoteService();
592 if (!remote_service) { 592 if (!remote_service) {
593 PA_LOG(WARNING) << "Remote service not found."; 593 PA_LOG(WARNING) << "Remote service not found.";
594 return NULL; 594 return NULL;
595 } 595 }
596 return remote_service->GetCharacteristic(gatt_characteristic); 596 return remote_service->GetCharacteristic(gatt_characteristic);
597 } 597 }
598 598
599 // TODO(sacomoto): make this robust to byte ordering in both sides of the 599 // TODO(sacomoto): make this robust to byte ordering in both sides of the
600 // SmartLock BLE socket. 600 // SmartLock BLE socket.
601 uint32_t BluetoothLowEnergyConnection::ToUint32( 601 uint32_t BluetoothLowEnergyConnection::ToUint32(
602 const std::vector<uint8_t>& bytes) { 602 const std::vector<uint8_t>& bytes) {
603 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); 603 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
604 } 604 }
605 605
606 // TODO(sacomoto): make this robust to byte ordering in both sides of the 606 // TODO(sacomoto): make this robust to byte ordering in both sides of the
607 // SmartLock BLE socket. 607 // SmartLock BLE socket.
608 const std::vector<uint8_t> BluetoothLowEnergyConnection::ToByteVector( 608 const std::vector<uint8_t> BluetoothLowEnergyConnection::ToByteVector(
609 const uint32_t value) { 609 const uint32_t value) {
610 std::vector<uint8_t> bytes(4, 0); 610 std::vector<uint8_t> bytes(4, 0);
611 bytes[0] = static_cast<uint8_t>(value); 611 bytes[0] = static_cast<uint8_t>(value);
612 bytes[1] = static_cast<uint8_t>(value >> 8); 612 bytes[1] = static_cast<uint8_t>(value >> 8);
613 bytes[2] = static_cast<uint8_t>(value >> 16); 613 bytes[2] = static_cast<uint8_t>(value >> 16);
614 bytes[3] = static_cast<uint8_t>(value >> 24); 614 bytes[3] = static_cast<uint8_t>(value >> 24);
615 return bytes; 615 return bytes;
616 } 616 }
617 617
618 } // namespace proximity_auth 618 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698