From 499d5d83d11f2e8f12ef3df39be5849eb6c021ec Mon Sep 17 00:00:00 2001 From: Tim Song Date: Thu, 21 May 2015 11:15:20 -0700 Subject: [PATCH] Revert "Implementing a proximity_auth::Connection interface for Bluetooth Low Energy devices." Original issue's description: > Implementing a proximity_auth::Connection interface for Bluetooth Low Energy > devices. > > The current version does not handle messages larger than a single characteristic > size (around 150 bytes). Message receiving functionality is not implemented yet. > > BUG=485123 > Committed: https://crrev.com/574c1637312ef98f8fa4bde909c1f040ddf33a4b > Cr-Commit-Position: refs/heads/master@{#329837} BUG=490635 TBR=sacomoto@chromium.org, msarda@chromium.org Review URL: https://codereview.chromium.org/1150033003 Cr-Commit-Position: refs/branch-heads/2403@{#53} Cr-Branched-From: f54b8097a9c45ed4ad308133d49f05325d6c5070-refs/heads/master@{#330231} --- components/proximity_auth.gypi | 4 - components/proximity_auth/ble/BUILD.gn | 4 - .../ble/bluetooth_low_energy_connection.cc | 360 ------------------ .../ble/bluetooth_low_energy_connection.h | 209 ---------- .../bluetooth_low_energy_connection_finder.cc | 81 ++-- .../bluetooth_low_energy_connection_finder.h | 44 +-- .../proximity_auth/ble/fake_wire_message.cc | 28 -- .../proximity_auth/ble/fake_wire_message.h | 32 -- .../ble/proximity_auth_ble_system.cc | 20 +- .../ble/proximity_auth_ble_system.h | 7 +- components/proximity_auth/connection.h | 6 - 11 files changed, 41 insertions(+), 754 deletions(-) delete mode 100644 components/proximity_auth/ble/bluetooth_low_energy_connection.cc delete mode 100644 components/proximity_auth/ble/bluetooth_low_energy_connection.h delete mode 100644 components/proximity_auth/ble/fake_wire_message.cc delete mode 100644 components/proximity_auth/ble/fake_wire_message.h diff --git a/components/proximity_auth.gypi b/components/proximity_auth.gypi index f67f008364ecd..048df3920abf7 100644 --- a/components/proximity_auth.gypi +++ b/components/proximity_auth.gypi @@ -19,12 +19,8 @@ '../net/net.gyp:net', ], 'sources': [ - "proximity_auth/ble/bluetooth_low_energy_connection.cc", - "proximity_auth/ble/bluetooth_low_energy_connection.h", "proximity_auth/ble/bluetooth_low_energy_connection_finder.cc", "proximity_auth/ble/bluetooth_low_energy_connection_finder.h", - "proximity_auth/ble/fake_wire_message.cc", - "proximity_auth/ble/fake_wire_message.h", "proximity_auth/ble/proximity_auth_ble_system.cc", "proximity_auth/ble/proximity_auth_ble_system.h", "proximity_auth/bluetooth_connection.cc", diff --git a/components/proximity_auth/ble/BUILD.gn b/components/proximity_auth/ble/BUILD.gn index 3a6603ecaaa78..71324c5cb1f88 100644 --- a/components/proximity_auth/ble/BUILD.gn +++ b/components/proximity_auth/ble/BUILD.gn @@ -4,12 +4,8 @@ source_set("ble") { sources = [ - "bluetooth_low_energy_connection.cc", - "bluetooth_low_energy_connection.h", "bluetooth_low_energy_connection_finder.cc", "bluetooth_low_energy_connection_finder.h", - "fake_wire_message.cc", - "fake_wire_message.h", "proximity_auth_ble_system.cc", "proximity_auth_ble_system.h", ] diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection.cc b/components/proximity_auth/ble/bluetooth_low_energy_connection.cc deleted file mode 100644 index 13e11b0238501..0000000000000 --- a/components/proximity_auth/ble/bluetooth_low_energy_connection.cc +++ /dev/null @@ -1,360 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" - -#include "base/bind.h" -#include "base/memory/ref_counted.h" -#include "base/memory/weak_ptr.h" -#include "base/time/time.h" -#include "components/proximity_auth/ble/fake_wire_message.h" -#include "components/proximity_auth/connection_finder.h" -#include "components/proximity_auth/wire_message.h" -#include "device/bluetooth/bluetooth_adapter.h" -#include "device/bluetooth/bluetooth_device.h" -#include "device/bluetooth/bluetooth_gatt_characteristic.h" -#include "device/bluetooth/bluetooth_gatt_connection.h" -#include "device/bluetooth/bluetooth_gatt_notify_session.h" -#include "device/bluetooth/bluetooth_uuid.h" - -using device::BluetoothAdapter; -using device::BluetoothDevice; -using device::BluetoothGattConnection; -using device::BluetoothGattService; -using device::BluetoothGattCharacteristic; -using device::BluetoothGattNotifySession; -using device::BluetoothUUID; - -namespace proximity_auth { - -BluetoothLowEnergyConnection::BluetoothLowEnergyConnection( - const RemoteDevice& device, - scoped_refptr adapter, - const BluetoothUUID remote_service_uuid, - const BluetoothUUID to_peripheral_char_uuid, - const BluetoothUUID from_peripheral_char_uuid, - scoped_ptr gatt_connection) - : Connection(device), - adapter_(adapter), - remote_service_({remote_service_uuid, ""}), - to_peripheral_char_({to_peripheral_char_uuid, ""}), - from_peripheral_char_({from_peripheral_char_uuid, ""}), - connection_(gatt_connection.Pass()), - notify_session_pending_(false), - connect_signal_response_pending_(false), - weak_ptr_factory_(this) { - DCHECK(connection_); - DCHECK(adapter_); - DCHECK(adapter_->IsInitialized()); - - start_time_ = base::TimeTicks::Now(); - SetStatus(IN_PROGRESS); - - // We should set the status to IN_PROGRESS before calling this function, as we - // can set the status to CONNECTED by an inner call of - // HandleCharacteristicUpdate(). - ScanRemoteCharacteristics(GetRemoteService()); - - adapter_->AddObserver(this); -} - -BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() { - Disconnect(); - if (adapter_) { - adapter_->RemoveObserver(this); - adapter_ = NULL; - } -} - -void BluetoothLowEnergyConnection::Connect() { - NOTREACHED(); -} - -// This actually forgets the remote BLE device. This is safe as long as we only -// connect to BLE devices advertising the SmartLock service (assuming this -// device has no other being used). -void BluetoothLowEnergyConnection::Disconnect() { - StopNotifySession(); - SetStatus(DISCONNECTED); - if (connection_) { - connection_.reset(); - BluetoothDevice* device = GetRemoteDevice(); - if (device) { - VLOG(1) << "Forget device " << device->GetAddress(); - device->Forget(base::Bind(&base::DoNothing)); - } - } -} - -// TODO(sacomoto): Send a SmartLock BLE socket incoming signal. Implement a -// sender with full support for messages larger than a single characteristic -// value. -void BluetoothLowEnergyConnection::SendMessageImpl( - scoped_ptr message) { - DCHECK(!GetGattCharacteristic(to_peripheral_char_.id)); - VLOG(1) << "Sending message " << message->Serialize(); - - std::string serialized_message = message->Serialize(); - std::vector bytes(serialized_message.begin(), - serialized_message.end()); - - GetGattCharacteristic(to_peripheral_char_.id) - ->WriteRemoteCharacteristic( - bytes, base::Bind(&base::DoNothing), - base::Bind( - &BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError, - weak_ptr_factory_.GetWeakPtr())); -} - -void BluetoothLowEnergyConnection::DeviceRemoved(BluetoothAdapter* adapter, - BluetoothDevice* device) { - if (device && device->GetAddress() == GetRemoteDeviceAddress()) { - VLOG(1) << "Device removed " << GetRemoteDeviceAddress(); - Disconnect(); - } -} - -void BluetoothLowEnergyConnection::GattDiscoveryCompleteForService( - BluetoothAdapter* adapter, - BluetoothGattService* service) { - if (service && service->GetUUID() == remote_service_.uuid) { - VLOG(1) << "All characteristics discovered for " - << remote_service_.uuid.canonical_value(); - - if (to_peripheral_char_.id.empty() || from_peripheral_char_.id.empty()) { - VLOG(1) << "Connection error, missing characteristics for SmartLock " - "service.\n" - << (to_peripheral_char_.id.empty() - ? to_peripheral_char_.uuid.canonical_value() - : "") - << (from_peripheral_char_.id.empty() - ? ", " + from_peripheral_char_.uuid.canonical_value() - : "") << " not found."; - Disconnect(); - } - } -} - -void BluetoothLowEnergyConnection::GattCharacteristicAdded( - BluetoothAdapter* adapter, - BluetoothGattCharacteristic* characteristic) { - VLOG(1) << "New char found: " << characteristic->GetUUID().canonical_value(); - HandleCharacteristicUpdate(characteristic); -} - -// TODO(sacomoto): Parse the SmartLock BLE socket incoming signal. Implement a -// receiver with full suport for messages larger than a single characteristic -// value. -void BluetoothLowEnergyConnection::GattCharacteristicValueChanged( - BluetoothAdapter* adapter, - BluetoothGattCharacteristic* characteristic, - const std::vector& value) { - DCHECK_EQ(adapter, adapter_.get()); - - VLOG(1) << "Characteristic value changed: " - << characteristic->GetUUID().canonical_value(); - - if (characteristic->GetIdentifier() == from_peripheral_char_.id) { - const ControlSignal signal = static_cast(ToUint32(value)); - - switch (signal) { - case ControlSignal::kInvitationResponseSignal: - connect_signal_response_pending_ = false; - CompleteConnection(); - break; - case ControlSignal::kInviteToConnectSignal: - case ControlSignal::kSendSignal: - // TODO(sacomoto): Actually handle the message and call OnBytesReceived - // when complete. - case ControlSignal::kDisconnectSignal: - Disconnect(); - break; - } - } -} - -scoped_ptr BluetoothLowEnergyConnection::DeserializeWireMessage( - bool* is_incomplete_message) { - return FakeWireMessage::Deserialize(received_bytes(), is_incomplete_message); -} - -void BluetoothLowEnergyConnection::ScanRemoteCharacteristics( - BluetoothGattService* service) { - if (service) { - std::vector characteristics = - service->GetCharacteristics(); - for (auto iter = characteristics.begin(); iter != characteristics.end(); - iter++) { - HandleCharacteristicUpdate(*iter); - } - } -} - -void BluetoothLowEnergyConnection::HandleCharacteristicUpdate( - BluetoothGattCharacteristic* characteristic) { - // Checks if |characteristic| is equal to |from_peripheral_char_| or - // |to_peripheral_char_|. - UpdateCharacteristicsStatus(characteristic); - - // Starts a notify session for |from_peripheral_char_|. - if (characteristic->GetIdentifier() == from_peripheral_char_.id) - StartNotifySession(); - - // Sends a invite to connect signal if ready. - SendInviteToConnectSignal(); -} - -void BluetoothLowEnergyConnection::CompleteConnection() { - if (status() == IN_PROGRESS && !connect_signal_response_pending_ && - !to_peripheral_char_.id.empty() && !from_peripheral_char_.id.empty() && - notify_session_) { - VLOG(1) << "Connection completed"; - VLOG(1) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; - SetStatus(CONNECTED); - } -} - -void BluetoothLowEnergyConnection::StartNotifySession() { - BluetoothGattCharacteristic* characteristic = - GetGattCharacteristic(from_peripheral_char_.id); - if (!characteristic) { - VLOG(1) << "Characteristic " << from_peripheral_char_.uuid.canonical_value() - << " not found."; - return; - } - - if (notify_session_ || notify_session_pending_) { - VLOG(1) << "Notify session already started."; - return; - } - - notify_session_pending_ = true; - characteristic->StartNotifySession( - base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionStarted, - weak_ptr_factory_.GetWeakPtr()), - base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionError, - weak_ptr_factory_.GetWeakPtr())); -} - -void BluetoothLowEnergyConnection::OnNotifySessionError( - BluetoothGattService::GattErrorCode error) { - VLOG(1) << "Error starting notification session: " << error; - notify_session_pending_ = false; -} - -void BluetoothLowEnergyConnection::OnNotifySessionStarted( - scoped_ptr notify_session) { - VLOG(1) << "Notification session started " - << notify_session->GetCharacteristicIdentifier(); - notify_session_ = notify_session.Pass(); - notify_session_pending_ = false; - - // Sends an invite to connect signal if ready. - SendInviteToConnectSignal(); -} - -void BluetoothLowEnergyConnection::StopNotifySession() { - if (notify_session_) { - notify_session_->Stop(base::Bind(&base::DoNothing)); - notify_session_.reset(); - } -} - -void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError( - BluetoothGattService::GattErrorCode error) { - VLOG(1) << "Error writing characteristic" - << to_peripheral_char_.uuid.canonical_value(); -} - -void BluetoothLowEnergyConnection::SendInviteToConnectSignal() { - if (status() == IN_PROGRESS && !connect_signal_response_pending_ && - !to_peripheral_char_.id.empty() && !from_peripheral_char_.id.empty() && - notify_session_) { - VLOG(1) << "Sending invite to connect signal"; - connect_signal_response_pending_ = true; - - // The connection status is not CONNECTED yet, so in order to bypass the - // check in SendMessage implementation we need to send the message using the - // private implementation. - SendMessageImpl(scoped_ptr(new FakeWireMessage( - ToString(static_cast(ControlSignal::kInviteToConnectSignal))))); - } -} - -void BluetoothLowEnergyConnection::UpdateCharacteristicsStatus( - BluetoothGattCharacteristic* characteristic) { - if (characteristic) { - BluetoothUUID uuid = characteristic->GetUUID(); - if (to_peripheral_char_.uuid == uuid) - to_peripheral_char_.id = characteristic->GetIdentifier(); - if (from_peripheral_char_.uuid == uuid) - from_peripheral_char_.id = characteristic->GetIdentifier(); - - BluetoothGattService* service = characteristic->GetService(); - if (service && service->GetUUID() == remote_service_.uuid) - remote_service_.id = service->GetIdentifier(); - } -} - -const std::string& BluetoothLowEnergyConnection::GetRemoteDeviceAddress() { - return remote_device().bluetooth_address; -} - -BluetoothDevice* BluetoothLowEnergyConnection::GetRemoteDevice() { - if (!adapter_ || !adapter_->IsInitialized()) { - VLOG(1) << "adapter not ready"; - return NULL; - } - return adapter_->GetDevice(GetRemoteDeviceAddress()); -} - -BluetoothGattService* BluetoothLowEnergyConnection::GetRemoteService() { - BluetoothDevice* remote_device = GetRemoteDevice(); - if (!remote_device) { - VLOG(1) << "device not found"; - return NULL; - } - if (remote_service_.id.empty()) { - std::vector services = - remote_device->GetGattServices(); - for (auto iter = services.begin(); iter != services.end(); ++iter) - if ((*iter)->GetUUID() == remote_service_.uuid) { - remote_service_.id = (*iter)->GetIdentifier(); - break; - } - } - return remote_device->GetGattService(remote_service_.id); -} - -BluetoothGattCharacteristic* -BluetoothLowEnergyConnection::GetGattCharacteristic( - const std::string& gatt_characteristic) { - BluetoothGattService* remote_service = GetRemoteService(); - if (!remote_service) { - VLOG(1) << "service not found"; - return NULL; - } - return remote_service->GetCharacteristic(gatt_characteristic); -} - -// TODO(sacomoto): make this robust to byte ordering in both sides of the -// SmartLock BLE socket. -uint32 BluetoothLowEnergyConnection::ToUint32(const std::vector& bytes) { - return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); -} - -// TODO(sacomoto): make this robust to byte ordering in both sides of the -// SmartLock BLE socket. -const std::string BluetoothLowEnergyConnection::ToString(const uint32 value) { - char bytes[4] = {}; - - bytes[0] = static_cast(value); - bytes[1] = static_cast(value >> 8); - bytes[2] = static_cast(value >> 12); - bytes[3] = static_cast(value >> 24); - - return std::string(bytes); -} - -} // namespace proximity_auth diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection.h b/components/proximity_auth/ble/bluetooth_low_energy_connection.h deleted file mode 100644 index be32258164b9f..0000000000000 --- a/components/proximity_auth/ble/bluetooth_low_energy_connection.h +++ /dev/null @@ -1,209 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H -#define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H - -#include "base/macros.h" -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" -#include "base/time/time.h" -#include "components/proximity_auth/ble/fake_wire_message.h" -#include "components/proximity_auth/connection.h" -#include "device/bluetooth/bluetooth_adapter.h" -#include "device/bluetooth/bluetooth_device.h" -#include "device/bluetooth/bluetooth_gatt_characteristic.h" -#include "device/bluetooth/bluetooth_gatt_notify_session.h" -#include "device/bluetooth/bluetooth_uuid.h" - -namespace proximity_auth { - -// Represents a connection with a remote device over Bluetooth low energy. The -// connection is a persistent bidirectional channel for sending and receiving -// wire messages. The remote device is the peripheral mode and the service -// contains two characteristics: one to send data and another to receive it. -class BluetoothLowEnergyConnection : public Connection, - public device::BluetoothAdapter::Observer { - public: - enum class ControlSignal : uint32 { - kInviteToConnectSignal = 0, - kInvitationResponseSignal = 1, - kSendSignal = 2, - kDisconnectSignal = 3, - }; - - // Constructs a Bluetooth low energy connection to the service with - // |remote_service_| on the |remote_device|. The GATT connection with - // |remote_device| must be already established and |adapter| already - // initialized. - // - // The connection flow is described below. - // - // Discover Reader Characteristic Discover Writer Characteristic - // | | - // Start Notify Session | - // | | - // ---------------- && --------------------- - // | - // | - // | - // Write kInviteToConnectSignal to Writer Characteristic - // | - // | - // | - // Read kInvitationResponseSignal from Reader Characteristic - // | - // | - // | - // Proximity Auth Connection Established - BluetoothLowEnergyConnection( - const RemoteDevice& remote_device, - scoped_refptr adapter, - const device::BluetoothUUID remote_service_uuid, - const device::BluetoothUUID to_peripheral_char_uuid, - const device::BluetoothUUID from_peripheral_char_uuid, - scoped_ptr gatt_connection); - - ~BluetoothLowEnergyConnection() override; - - // proximity_auth::Connection - void Connect() override; - void Disconnect() override; - - protected: - // proximity_auth::Connection - void SendMessageImpl(scoped_ptr message) override; - scoped_ptr DeserializeWireMessage( - bool* is_incomplete_message) override; - - // device::BluetoothAdapter::Observer - void DeviceRemoved(device::BluetoothAdapter* adapter, - device::BluetoothDevice* device) override; - void GattDiscoveryCompleteForService( - device::BluetoothAdapter* adapter, - device::BluetoothGattService* service) override; - void GattCharacteristicAdded( - device::BluetoothAdapter* adapter, - device::BluetoothGattCharacteristic* characteristic) override; - void GattCharacteristicValueChanged( - device::BluetoothAdapter* adapter, - device::BluetoothGattCharacteristic* characteristic, - const std::vector& value) override; - - private: - // Represents a remote characteristic or service. - struct RemoteBleObject { - device::BluetoothUUID uuid; - std::string id; - }; - - // Sends an invite to connect signal to the peripheral if the connection - // is ready. That is, satisfying the conditions: - // (i) |to_peripheral_char_| and |from_peripheral_char_| were found; - // (ii) |notify_session_| was set for |from_peripheral_char_|. - // - // The asynchronous events that can casue these conditions to be - // satisfied are: - // (i) a new characteristic is discovered (HandleCharacteristicUpdate); - // (ii) a new notify session is started (OnNotifySessionStarted). - void SendInviteToConnectSignal(); - - // Called when there is an error writing to the remote characteristic - // |to_peripheral_char_|. - void OnWriteRemoteCharacteristicError( - device::BluetoothGattService::GattErrorCode error); - - // Handles the discovery of a new characteristic. - void HandleCharacteristicUpdate( - device::BluetoothGattCharacteristic* characteristic); - - // Scans the remote chracteristics of |service| calling - // HandleCharacteristicUpdate() for each of them. - void ScanRemoteCharacteristics(device::BluetoothGattService* service); - - // This function verifies if the connection is complete and updates - // the status accordingly. The only asynchronous event - // that can cause the connection to be completed is receiving a - // kInvitationResponseSignal (GattCharateristcValueChanged). - void CompleteConnection(); - - // Starts a notify session for |from_peripheral_char_|. - void StartNotifySession(); - - // Called when there is an error starting a notification session for - // |from_peripheral_char_| characteristic. - void OnNotifySessionError(device::BluetoothGattService::GattErrorCode); - - // Called when a notification session is successfully started for - // |from_peripheral_char_| characteristic. - void OnNotifySessionStarted( - scoped_ptr notify_session); - - // Stops |notify_session_|. - void StopNotifySession(); - - // Updates the value of |to_peripheral_char_| and - // |from_peripheral_char_| - // when |characteristic| was found. - void UpdateCharacteristicsStatus( - device::BluetoothGattCharacteristic* characteristic); - - // Returns the Bluetooth address of the remote device. - const std::string& GetRemoteDeviceAddress(); - - // Returns the device corresponding to |remote_device_address_|. - device::BluetoothDevice* GetRemoteDevice(); - - // Returns the service corresponding to |remote_service_| in the current - // device. - device::BluetoothGattService* GetRemoteService(); - - // Returns the characteristic corresponding to |identifier| in the current - // service. - device::BluetoothGattCharacteristic* GetGattCharacteristic( - const std::string& identifier); - - // Convert the first 4 bytes from a byte vector to a uint32. - uint32 ToUint32(const std::vector& bytes); - - // Convert an uint32 to a byte array. - const std::string ToString(const uint32 value); - - // The Bluetooth adapter over which the Bluetooth connection will be made. - scoped_refptr adapter_; - - // Remote service the |connection_| was established with. - RemoteBleObject remote_service_; - - // Characteristic used to send data to the remote device. - RemoteBleObject to_peripheral_char_; - - // Characteristic used to receive data from the remote device. - RemoteBleObject from_peripheral_char_; - - // The GATT connection with the remote device. - scoped_ptr connection_; - - // Indicates if there is a pending notification session. Used to ensure there - // is only one pending notify session. - bool notify_session_pending_; - - // The notify session for |from_peripheral_char|. - scoped_ptr notify_session_; - - // Indicates if there pending response to a invite to connect signal. - bool connect_signal_response_pending_; - - // Stores when the instace was created. - base::TimeTicks start_time_; - - base::WeakPtrFactory weak_ptr_factory_; - - DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); -}; - -} // namespace proximity_auth - -#endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc index f158ab427a6b9..d911388fa5616 100644 --- a/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc +++ b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc @@ -10,8 +10,6 @@ #include "base/bind_helpers.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" -#include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" -#include "components/proximity_auth/connection.h" #include "device/bluetooth/bluetooth_adapter_factory.h" #include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_discovery_session.h" @@ -25,13 +23,8 @@ using device::BluetoothDiscoveryFilter; namespace proximity_auth { BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( - const std::string& remote_service_uuid, - const std::string& to_peripheral_char_uuid, - const std::string& from_peripheral_char_uuid) + const std::string& remote_service_uuid) : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), - to_peripheral_char_uuid_(device::BluetoothUUID(to_peripheral_char_uuid)), - from_peripheral_char_uuid_( - device::BluetoothUUID(from_peripheral_char_uuid)), connected_(false), weak_ptr_factory_(this) { } @@ -40,12 +33,6 @@ BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { if (discovery_session_) { StopDiscoverySession(); } - - if (connection_) { - connection_->RemoveObserver(this); - connection_.reset(); - } - if (adapter_) { adapter_->RemoveObserver(this); adapter_ = NULL; @@ -53,7 +40,7 @@ BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { } void BluetoothLowEnergyConnectionFinder::Find( - const ConnectionCallback& connection_callback) { + const BluetoothDevice::GattConnectionCallback& connection_callback) { if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; return; @@ -67,6 +54,11 @@ void BluetoothLowEnergyConnectionFinder::Find( weak_ptr_factory_.GetWeakPtr())); } +void BluetoothLowEnergyConnectionFinder::Find( + const ConnectionCallback& connection_callback) { + NOTREACHED(); +} + void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, BluetoothDevice* device) { DCHECK(device); @@ -94,7 +86,7 @@ void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( if (HasService(device)) { VLOG(1) << "Connecting to device " << device->GetAddress(); pending_connections_.insert(device); - CreateGattConnection(device); + CreateConnection(device); } } @@ -201,49 +193,47 @@ bool BluetoothLowEnergyConnectionFinder::HasService( return false; } -void BluetoothLowEnergyConnectionFinder::OnCreateGattConnectionError( +void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( std::string device_address, BluetoothDevice::ConnectErrorCode error_code) { VLOG(1) << "Error creating connection to device " << device_address << " : error code = " << error_code; } -void BluetoothLowEnergyConnectionFinder::OnGattConnectionCreated( - scoped_ptr gatt_connection) { +void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( + scoped_ptr connection) { if (connected_) { - CloseGattConnection(gatt_connection.Pass()); + CloseConnection(connection.Pass()); return; } VLOG(1) << "Connection created"; connected_ = true; pending_connections_.clear(); - - connection_ = CreateConnection(gatt_connection.Pass()); - connection_->AddObserver(this); - + if (!connection_callback_.is_null()) { + connection_callback_.Run(connection.Pass()); + connection_callback_.Reset(); + } StopDiscoverySession(); } -void BluetoothLowEnergyConnectionFinder::CreateGattConnection( +void BluetoothLowEnergyConnectionFinder::CreateConnection( device::BluetoothDevice* remote_device) { VLOG(1) << "SmartLock service found (" << remote_service_uuid_.canonical_value() << ")\n" << "device = " << remote_device->GetAddress() << ", name = " << remote_device->GetName(); remote_device->CreateGattConnection( - base::Bind(&BluetoothLowEnergyConnectionFinder::OnGattConnectionCreated, + base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated, weak_ptr_factory_.GetWeakPtr()), - base::Bind( - &BluetoothLowEnergyConnectionFinder::OnCreateGattConnectionError, - weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress())); + base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, + weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress())); } -void BluetoothLowEnergyConnectionFinder::CloseGattConnection( - scoped_ptr gatt_connection) { - DCHECK(gatt_connection); - std::string device_address = gatt_connection->GetDeviceAddress(); - gatt_connection.reset(); +void BluetoothLowEnergyConnectionFinder::CloseConnection( + scoped_ptr connection) { + std::string device_address = connection->GetDeviceAddress(); + connection.reset(); BluetoothDevice* device = adapter_->GetDevice(device_address); if (device) { DCHECK(HasService(device)); @@ -252,27 +242,4 @@ void BluetoothLowEnergyConnectionFinder::CloseGattConnection( } } -scoped_ptr BluetoothLowEnergyConnectionFinder::CreateConnection( - scoped_ptr gatt_connection) { - RemoteDevice remote_device; - remote_device.bluetooth_address = gatt_connection->GetDeviceAddress(); - - return make_scoped_ptr(new BluetoothLowEnergyConnection( - remote_device, adapter_, remote_service_uuid_, to_peripheral_char_uuid_, - from_peripheral_char_uuid_, gatt_connection.Pass())); -} - -void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( - Connection* connection, - Connection::Status old_status, - Connection::Status new_status) { - DCHECK_EQ(connection, connection_.get()); - - if (!connection_callback_.is_null() && connection_->IsConnected()) { - connection_->RemoveObserver(this); - connection_callback_.Run(connection_.Pass()); - connection_callback_.Reset(); - } -} - } // namespace proximity_auth diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h index 4c421e25b8690..56ffc9d6171f2 100644 --- a/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h +++ b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h @@ -13,9 +13,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" -#include "components/proximity_auth/connection.h" #include "components/proximity_auth/connection_finder.h" -#include "components/proximity_auth/connection_observer.h" #include "device/bluetooth/bluetooth_adapter.h" #include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_discovery_session.h" @@ -27,26 +25,18 @@ namespace proximity_auth { // Low Energy remote device. class BluetoothLowEnergyConnectionFinder : public ConnectionFinder, - public ConnectionObserver, public device::BluetoothAdapter::Observer { public: - BluetoothLowEnergyConnectionFinder( - const std::string& remote_service_uuid, - const std::string& to_peripheral_char_uuid, - const std::string& from_peripheral_char_uuid); + BluetoothLowEnergyConnectionFinder(const std::string& remote_service_uuid); ~BluetoothLowEnergyConnectionFinder() override; // Finds a connection to the remote device. Only the first one is functional. + void Find(const device::BluetoothDevice::GattConnectionCallback& + connection_callback); void Find(const ConnectionCallback& connection_callback) override; // Closes the connection and forgets the device. - void CloseGattConnection( - scoped_ptr gatt_connection); - - // proximity_auth::ConnectionObserver: - void OnConnectionStatusChanged(Connection* connection, - Connection::Status old_status, - Connection::Status new_status) override; + void CloseConnection(scoped_ptr connection); protected: // device::BluetoothAdapter::Observer: @@ -88,31 +78,21 @@ class BluetoothLowEnergyConnectionFinder bool HasService(device::BluetoothDevice* remote_device); // Callback called when there is an error creating the connection. - void OnCreateGattConnectionError( + void OnCreateConnectionError( std::string device_address, device::BluetoothDevice::ConnectErrorCode error_code); - // Callback called when a GATT connection is created. - void OnGattConnectionCreated( - scoped_ptr gatt_connection); + // Callback called when the connection is created. + void OnConnectionCreated( + scoped_ptr connection); // Creates a GATT connection with |remote_device|, |connection_callback_| will // be called once the connection is established. - void CreateGattConnection(device::BluetoothDevice* remote_device); - - // Creates a connection with |remote_device|. - scoped_ptr CreateConnection( - scoped_ptr gatt_connection); + void CreateConnection(device::BluetoothDevice* remote_device); // The uuid of the service it looks for to establish a GattConnection. device::BluetoothUUID remote_service_uuid_; - // Characteristic used to send data to the remote device. - device::BluetoothUUID to_peripheral_char_uuid_; - - // Characteristic used to receive data from the remote device. - device::BluetoothUUID from_peripheral_char_uuid_; - // The Bluetooth adapter over which the Bluetooth connection will be made. scoped_refptr adapter_; @@ -123,12 +103,8 @@ class BluetoothLowEnergyConnectionFinder // service |remote_service_uuid|. bool connected_; - // The connection with |remote_device|. - scoped_ptr connection_; - // Callback called when the connection is established. - // device::BluetoothDevice::GattConnectionCallback connection_callback_; - ConnectionCallback connection_callback_; + device::BluetoothDevice::GattConnectionCallback connection_callback_; // The set of devices this connection finder has tried to connect to. std::set pending_connections_; diff --git a/components/proximity_auth/ble/fake_wire_message.cc b/components/proximity_auth/ble/fake_wire_message.cc deleted file mode 100644 index 193ac04c6496c..0000000000000 --- a/components/proximity_auth/ble/fake_wire_message.cc +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include - -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" -#include "components/proximity_auth/ble/fake_wire_message.h" -#include "components/proximity_auth/wire_message.h" - -namespace proximity_auth { - -FakeWireMessage::FakeWireMessage(const std::string& payload) - : WireMessage("", payload) { -} - -scoped_ptr FakeWireMessage::Deserialize( - const std::string& serialized_message, - bool* is_incomplete_message) { - *is_incomplete_message = false; - return scoped_ptr(new FakeWireMessage(serialized_message)); -} - -std::string FakeWireMessage::Serialize() const { - return std::string(payload()); -} -} diff --git a/components/proximity_auth/ble/fake_wire_message.h b/components/proximity_auth/ble/fake_wire_message.h deleted file mode 100644 index f0f2325456d39..0000000000000 --- a/components/proximity_auth/ble/fake_wire_message.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_PROXIMITY_AUTH_FAKE_WIRE_MESSAGE_H -#define COMPONENTS_PROXIMITY_AUTH_FAKE_WIRE_MESSAGE_H - -#include - -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" - -#include "components/proximity_auth/wire_message.h" - -namespace proximity_auth { - -class FakeWireMessage : public WireMessage { - public: - FakeWireMessage(const std::string& payload); - - static scoped_ptr Deserialize( - const std::string& serialized_message, - bool* is_incomplete_message); - - std::string Serialize() const override; - - private: - DISALLOW_COPY_AND_ASSIGN(FakeWireMessage); -}; -} - -#endif // COMPONENTS_PROXIMITY_AUTH_WIRE_MESSAGE_H diff --git a/components/proximity_auth/ble/proximity_auth_ble_system.cc b/components/proximity_auth/ble/proximity_auth_ble_system.cc index ebe6af3504031..673792d82ed51 100644 --- a/components/proximity_auth/ble/proximity_auth_ble_system.cc +++ b/components/proximity_auth/ble/proximity_auth_ble_system.cc @@ -6,10 +6,8 @@ #include "base/bind.h" #include "base/logging.h" -#include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h" #include "components/proximity_auth/connection.h" -#include "components/proximity_auth/remote_device.h" #include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_gatt_connection.h" @@ -20,12 +18,6 @@ namespace { // The UUID of the Bluetooth Low Energy service. const char kSmartLockServiceUUID[] = "b3b7e28e-a000-3e17-bd86-6e97b9e28c11"; -// The UUID of the characteristic used to send data to the peripheral. -const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; - -// The UUID of the characteristic used to receive data from the peripheral. -const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; - } // namespace ProximityAuthBleSystem::ProximityAuthBleSystem( @@ -54,9 +46,8 @@ void ProximityAuthBleSystem::OnScreenDidLock( break; case ScreenlockBridge::LockHandler::LOCK_SCREEN: DCHECK(!connection_finder_); - connection_finder_.reset(new BluetoothLowEnergyConnectionFinder( - kSmartLockServiceUUID, kToPeripheralCharUUID, - kFromPeripheralCharUUID)); + connection_finder_.reset( + new BluetoothLowEnergyConnectionFinder(kSmartLockServiceUUID)); connection_finder_->Find( base::Bind(&ProximityAuthBleSystem::OnConnectionFound, weak_ptr_factory_.GetWeakPtr())); @@ -70,8 +61,6 @@ void ProximityAuthBleSystem::OnScreenDidLock( void ProximityAuthBleSystem::OnScreenDidUnlock( ScreenlockBridge::LockHandler::ScreenType screen_type) { VLOG(1) << "OnScreenDidUnlock: " << screen_type; - connection_->Disconnect(); - connection_.reset(); connection_finder_.reset(); }; @@ -80,10 +69,11 @@ void ProximityAuthBleSystem::OnFocusedUserChanged(const std::string& user_id) { }; void ProximityAuthBleSystem::OnConnectionFound( - scoped_ptr connection) { + scoped_ptr connection) { VLOG(1) << "Connection found. Unlock."; - connection_ = connection.Pass(); + // Close the connection as it it no longer needed. + connection_finder_->CloseConnection(connection.Pass()); // Unlock the screen when a connection is found. // diff --git a/components/proximity_auth/ble/proximity_auth_ble_system.h b/components/proximity_auth/ble/proximity_auth_ble_system.h index aa0ac0a8e84b7..607f4ce2c7e42 100644 --- a/components/proximity_auth/ble/proximity_auth_ble_system.h +++ b/components/proximity_auth/ble/proximity_auth_ble_system.h @@ -19,9 +19,7 @@ class BluetoothGattConnection; namespace proximity_auth { -class BluetoothLowEnergyConnection; class BluetoothLowEnergyConnectionFinder; -class Connection; // This is the main entry point to start Proximity Auth over Bluetooth Low // Energy. This is the underlying system for the Smart Lock features. It will @@ -42,7 +40,8 @@ class ProximityAuthBleSystem : public ScreenlockBridge::Observer { private: // Handler for a new connection found event. - void OnConnectionFound(scoped_ptr connection); + void OnConnectionFound( + scoped_ptr connection); ScreenlockBridge* screenlock_bridge_; // Not owned. Must outlive this object. content::BrowserContext* @@ -50,8 +49,6 @@ class ProximityAuthBleSystem : public ScreenlockBridge::Observer { scoped_ptr connection_finder_; - scoped_ptr connection_; - base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(ProximityAuthBleSystem); diff --git a/components/proximity_auth/connection.h b/components/proximity_auth/connection.h index 8b2e7fc992e2d..44a3ff55a8d18 100644 --- a/components/proximity_auth/connection.h +++ b/components/proximity_auth/connection.h @@ -85,12 +85,6 @@ class Connection { virtual scoped_ptr DeserializeWireMessage( bool* is_incomplete_message); - // Exposed so it is possible to override DeserializeWireMessage in - // BluetoothLowEnergyConnection class, while mantaining the same - // functionality. - // TODO(sacomoto): remove this when FakeWireMessage is not needed anymore. - const std::string& received_bytes() { return received_bytes_; } - private: // The remote device corresponding to this connection. const RemoteDevice remote_device_;