| Index: components/proximity_auth/ble/bluetooth_low_energy_weave_connection.cc
|
| diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection.cc b/components/proximity_auth/ble/bluetooth_low_energy_weave_connection.cc
|
| similarity index 57%
|
| copy from components/proximity_auth/ble/bluetooth_low_energy_connection.cc
|
| copy to components/proximity_auth/ble/bluetooth_low_energy_weave_connection.cc
|
| index af6474948da29edac7217010dc358e5315bdb71c..3ce7e36cd5c7c3093a4bc3b4d98027e5294d257f 100644
|
| --- a/components/proximity_auth/ble/bluetooth_low_energy_connection.cc
|
| +++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_connection.cc
|
| @@ -1,30 +1,20 @@
|
| -// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Copyright 2016 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 "components/proximity_auth/ble/bluetooth_low_energy_weave_connection.h"
|
|
|
| #include <utility>
|
|
|
| #include "base/bind.h"
|
| #include "base/location.h"
|
| -#include "base/memory/ref_counted.h"
|
| -#include "base/memory/weak_ptr.h"
|
| #include "base/task_runner.h"
|
| #include "base/threading/thread_task_runner_handle.h"
|
| -#include "base/time/time.h"
|
| -#include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_finder.h"
|
| -#include "components/proximity_auth/ble/fake_wire_message.h"
|
| #include "components/proximity_auth/bluetooth_throttler.h"
|
| #include "components/proximity_auth/connection_finder.h"
|
| #include "components/proximity_auth/logging/logging.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_connection.h"
|
| -#include "device/bluetooth/bluetooth_gatt_notify_session.h"
|
| -#include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
|
| -#include "device/bluetooth/bluetooth_uuid.h"
|
|
|
| using device::BluetoothAdapter;
|
| using device::BluetoothDevice;
|
| @@ -34,25 +24,26 @@ using device::BluetoothRemoteGattCharacteristic;
|
| using device::BluetoothGattNotifySession;
|
| using device::BluetoothUUID;
|
|
|
| -namespace proximity_auth {
|
| namespace {
|
|
|
| -// The UUID of the characteristic used to send data to the peripheral.
|
| -const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a";
|
| +typedef proximity_auth::BluetoothLowEnergyWeavePacketReceiver::State
|
| + ReceiverState;
|
|
|
| -// The UUID of the characteristic used to receive data from the peripheral.
|
| -const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb";
|
| +// TODO(jingxy): figure out the right UUID to use
|
| +// The UUID of the TX characteristic used to send data to the server.
|
| +const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a";
|
|
|
| -// Deprecated signal send as the first byte in send byte operations.
|
| -const int kFirstByteZero = 0;
|
| +// TODO(jingxy): figure out the right UUID to use
|
| +// The UUID of the RX characteristic used to receiver data from the server.
|
| +const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb";
|
|
|
| -// The maximum number of bytes written in a remote characteristic with a single
|
| -// write request. This is not the connection MTU, we are assuming that the
|
| -// remote device allows for writes larger than MTU.
|
| -const int kMaxChunkSize = 500;
|
| } // namespace
|
|
|
| -BluetoothLowEnergyConnection::BluetoothLowEnergyConnection(
|
| +namespace proximity_auth {
|
| +
|
| +// TODO(jingxuy): where/how should I use the factor for the packer receiver
|
| +// and packet generator.
|
| +BluetoothLowEnergyWeaveConnection::BluetoothLowEnergyWeaveConnection(
|
| const RemoteDevice& device,
|
| scoped_refptr<device::BluetoothAdapter> adapter,
|
| const BluetoothUUID remote_service_uuid,
|
| @@ -61,15 +52,18 @@ BluetoothLowEnergyConnection::BluetoothLowEnergyConnection(
|
| : Connection(device),
|
| adapter_(adapter),
|
| remote_service_({remote_service_uuid, ""}),
|
| - to_peripheral_char_({BluetoothUUID(kToPeripheralCharUUID), ""}),
|
| - from_peripheral_char_({BluetoothUUID(kFromPeripheralCharUUID), ""}),
|
| + packet_generator_(
|
| + BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance()),
|
| + packet_receiver_(
|
| + BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance(
|
| + BluetoothLowEnergyWeavePacketReceiver::ReceiverType::CLIENT)),
|
| + tx_characteristic_({BluetoothUUID(kTXCharacteristicUUID), ""}),
|
| + rx_characteristic_({BluetoothUUID(kRXCharacteristicUUID), ""}),
|
| bluetooth_throttler_(bluetooth_throttler),
|
| task_runner_(base::ThreadTaskRunnerHandle::Get()),
|
| sub_status_(SubStatus::DISCONNECTED),
|
| - receiving_bytes_(false),
|
| write_remote_characteristic_pending_(false),
|
| max_number_of_write_attempts_(max_number_of_write_attempts),
|
| - max_chunk_size_(kMaxChunkSize),
|
| weak_ptr_factory_(this) {
|
| DCHECK(adapter_);
|
| DCHECK(adapter_->IsInitialized());
|
| @@ -77,7 +71,7 @@ BluetoothLowEnergyConnection::BluetoothLowEnergyConnection(
|
| adapter_->AddObserver(this);
|
| }
|
|
|
| -BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() {
|
| +BluetoothLowEnergyWeaveConnection::~BluetoothLowEnergyWeaveConnection() {
|
| Disconnect();
|
| if (adapter_) {
|
| adapter_->RemoveObserver(this);
|
| @@ -85,7 +79,7 @@ BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() {
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::Connect() {
|
| +void BluetoothLowEnergyWeaveConnection::Connect() {
|
| DCHECK(sub_status() == SubStatus::DISCONNECTED);
|
|
|
| SetSubStatus(SubStatus::WAITING_GATT_CONNECTION);
|
| @@ -101,7 +95,7 @@ void BluetoothLowEnergyConnection::Connect() {
|
| if (!throttler_delay.is_zero()) {
|
| task_runner_->PostDelayedTask(
|
| FROM_HERE,
|
| - base::Bind(&BluetoothLowEnergyConnection::CreateGattConnection,
|
| + base::Bind(&BluetoothLowEnergyWeaveConnection::CreateGattConnection,
|
| weak_ptr_factory_.GetWeakPtr()),
|
| throttler_delay);
|
| return;
|
| @@ -110,7 +104,7 @@ void BluetoothLowEnergyConnection::Connect() {
|
| CreateGattConnection();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::CreateGattConnection() {
|
| +void BluetoothLowEnergyWeaveConnection::CreateGattConnection() {
|
| DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION);
|
|
|
| BluetoothDevice* remote_device = GetRemoteDevice();
|
| @@ -119,14 +113,15 @@ void BluetoothLowEnergyConnection::CreateGattConnection() {
|
| << remote_device->GetAddress();
|
|
|
| remote_device->CreateGattConnection(
|
| - base::Bind(&BluetoothLowEnergyConnection::OnGattConnectionCreated,
|
| + base::Bind(&BluetoothLowEnergyWeaveConnection::OnGattConnectionCreated,
|
| weak_ptr_factory_.GetWeakPtr()),
|
| - base::Bind(&BluetoothLowEnergyConnection::OnCreateGattConnectionError,
|
| - weak_ptr_factory_.GetWeakPtr()));
|
| + base::Bind(
|
| + &BluetoothLowEnergyWeaveConnection::OnCreateGattConnectionError,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::Disconnect() {
|
| +void BluetoothLowEnergyWeaveConnection::Disconnect() {
|
| if (sub_status() != SubStatus::DISCONNECTED) {
|
| weak_ptr_factory_.InvalidateWeakPtrs();
|
| StopNotifySession();
|
| @@ -146,78 +141,43 @@ void BluetoothLowEnergyConnection::Disconnect() {
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::SetSubStatus(SubStatus new_sub_status) {
|
| +void BluetoothLowEnergyWeaveConnection::SetSubStatus(SubStatus new_sub_status) {
|
| sub_status_ = new_sub_status;
|
|
|
| // Sets the status of parent class proximity_auth::Connection accordingly.
|
| if (new_sub_status == SubStatus::CONNECTED) {
|
| - SetStatus(CONNECTED);
|
| + SetStatus(Status::CONNECTED);
|
| } else if (new_sub_status == SubStatus::DISCONNECTED) {
|
| - SetStatus(DISCONNECTED);
|
| + SetStatus(Status::DISCONNECTED);
|
| } else {
|
| - SetStatus(IN_PROGRESS);
|
| + SetStatus(Status::IN_PROGRESS);
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::SetTaskRunnerForTesting(
|
| +void BluetoothLowEnergyWeaveConnection::SetTaskRunnerForTesting(
|
| scoped_refptr<base::TaskRunner> task_runner) {
|
| task_runner_ = task_runner;
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::SendMessageImpl(
|
| +void BluetoothLowEnergyWeaveConnection::SendMessageImpl(
|
| std::unique_ptr<WireMessage> message) {
|
| PA_LOG(INFO) << "Sending message " << message->Serialize();
|
| std::string serialized_msg = message->Serialize();
|
|
|
| - // [First write]: Build a header with the [send signal] + [size of the
|
| - // message].
|
| - WriteRequest write_request = BuildWriteRequest(
|
| - ToByteVector(static_cast<uint32_t>(ControlSignal::kSendSignal)),
|
| - ToByteVector(static_cast<uint32_t>(serialized_msg.size())), false);
|
| -
|
| - // [First write]: Fill the it with a prefix of |serialized_msg| up to
|
| - // |max_chunk_size_|.
|
| - size_t first_chunk_size = std::min(
|
| - max_chunk_size_ - write_request.value.size(), serialized_msg.size());
|
| - std::vector<uint8_t> bytes(serialized_msg.begin(),
|
| - serialized_msg.begin() + first_chunk_size);
|
| - write_request.value.insert(write_request.value.end(), bytes.begin(),
|
| - bytes.end());
|
| -
|
| - bool is_last_write_request = first_chunk_size == serialized_msg.size();
|
| - write_request.is_last_write_for_wire_message = is_last_write_request;
|
| - WriteRemoteCharacteristic(write_request);
|
| - if (is_last_write_request)
|
| - return;
|
| + std::vector<std::vector<uint8_t>> packets =
|
| + packet_generator_->EncodeDataMessage(serialized_msg);
|
|
|
| - // [Other write requests]: Each chunk has to include a deprecated signal:
|
| - // |kFirstByteZero| as the first byte.
|
| - int chunk_size = max_chunk_size_ - 1;
|
| - std::vector<uint8_t> kFirstByteZeroVector;
|
| - kFirstByteZeroVector.push_back(static_cast<uint8_t>(kFirstByteZero));
|
| -
|
| - int message_size = static_cast<int>(serialized_msg.size());
|
| - int start_index = first_chunk_size;
|
| - while (start_index < message_size) {
|
| - int end_index = (start_index + chunk_size) <= message_size
|
| - ? (start_index + chunk_size)
|
| - : message_size;
|
| - bool is_last_write_request = (end_index == message_size);
|
| - write_request = BuildWriteRequest(
|
| - kFirstByteZeroVector,
|
| - std::vector<uint8_t>(serialized_msg.begin() + start_index,
|
| - serialized_msg.begin() + end_index),
|
| - is_last_write_request);
|
| - WriteRemoteCharacteristic(write_request);
|
| - start_index = end_index;
|
| + for (uint32_t i = 0; i < packets.size(); ++i) {
|
| + WriteRequest request = WriteRequest(packets[i], i == packets.size() - 1);
|
| + WriteRemoteCharacteristic(request);
|
| }
|
| }
|
|
|
| // Changes in the GATT connection with the remote device should be observed
|
| // here. If the GATT connection is dropped, we should call Disconnect() anyway,
|
| // so the object can notify its observers.
|
| -void BluetoothLowEnergyConnection::DeviceChanged(BluetoothAdapter* adapter,
|
| - BluetoothDevice* device) {
|
| +void BluetoothLowEnergyWeaveConnection::DeviceChanged(BluetoothAdapter* adapter,
|
| + BluetoothDevice* device) {
|
| DCHECK(device);
|
| if (sub_status() == SubStatus::DISCONNECTED ||
|
| device->GetAddress() != GetDeviceAddress())
|
| @@ -234,8 +194,8 @@ void BluetoothLowEnergyConnection::DeviceChanged(BluetoothAdapter* adapter,
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::DeviceRemoved(BluetoothAdapter* adapter,
|
| - BluetoothDevice* device) {
|
| +void BluetoothLowEnergyWeaveConnection::DeviceRemoved(BluetoothAdapter* adapter,
|
| + BluetoothDevice* device) {
|
| DCHECK(device);
|
| if (sub_status_ == SubStatus::DISCONNECTED ||
|
| device->GetAddress() != GetDeviceAddress())
|
| @@ -245,7 +205,7 @@ void BluetoothLowEnergyConnection::DeviceRemoved(BluetoothAdapter* adapter,
|
| Disconnect();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::GattCharacteristicValueChanged(
|
| +void BluetoothLowEnergyWeaveConnection::GattCharacteristicValueChanged(
|
| BluetoothAdapter* adapter,
|
| BluetoothRemoteGattCharacteristic* characteristic,
|
| const std::vector<uint8_t>& value) {
|
| @@ -257,79 +217,56 @@ void BluetoothLowEnergyConnection::GattCharacteristicValueChanged(
|
| PA_LOG(INFO) << "Characteristic value changed: "
|
| << characteristic->GetUUID().canonical_value();
|
|
|
| - if (characteristic->GetIdentifier() == from_peripheral_char_.id) {
|
| - if (receiving_bytes_) {
|
| - // Ignoring the first byte, as it contains a deprecated signal.
|
| - const std::string bytes(value.begin() + 1, value.end());
|
| - incoming_bytes_buffer_.append(bytes);
|
| - if (incoming_bytes_buffer_.size() >= expected_number_of_incoming_bytes_) {
|
| - OnBytesReceived(incoming_bytes_buffer_);
|
| - receiving_bytes_ = false;
|
| - }
|
| - return;
|
| - }
|
| -
|
| - if (value.size() < 4) {
|
| - PA_LOG(WARNING) << "Incoming data corrupted, no signal found.";
|
| - return;
|
| - }
|
| -
|
| - const ControlSignal signal = static_cast<ControlSignal>(ToUint32(value));
|
| - switch (signal) {
|
| - case ControlSignal::kInvitationResponseSignal:
|
| - if (sub_status() == SubStatus::WAITING_RESPONSE_SIGNAL)
|
| - CompleteConnection();
|
| - break;
|
| - case ControlSignal::kInviteToConnectSignal:
|
| + if (characteristic->GetIdentifier() == rx_characteristic_.id) {
|
| + ReceiverState state = packet_receiver_->ReceivePacket(value);
|
| + PA_LOG(INFO) << "\nReceiver State: " << state;
|
| + switch (state) {
|
| + case ReceiverState::DATA_READY:
|
| + OnBytesReceived(packet_receiver_->GetDataMessage());
|
| break;
|
| - case ControlSignal::kSendSignal: {
|
| - if (value.size() < 8) {
|
| - PA_LOG(WARNING)
|
| - << "Incoming data corrupted, expected message size not found.";
|
| - return;
|
| - }
|
| - std::vector<uint8_t> size(value.begin() + 4, value.begin() + 8);
|
| - expected_number_of_incoming_bytes_ =
|
| - static_cast<size_t>(ToUint32(size));
|
| - receiving_bytes_ = true;
|
| - incoming_bytes_buffer_.clear();
|
| -
|
| - const std::string bytes(value.begin() + 8, value.end());
|
| - incoming_bytes_buffer_.append(bytes);
|
| - if (incoming_bytes_buffer_.size() >=
|
| - expected_number_of_incoming_bytes_) {
|
| - OnBytesReceived(incoming_bytes_buffer_);
|
| - receiving_bytes_ = false;
|
| - }
|
| + case ReceiverState::CONNECTION_CLOSED:
|
| + // TODO(jingxuy): what to do with the reason for close?
|
| + Disconnect();
|
| break;
|
| - }
|
| - case ControlSignal::kDisconnectSignal:
|
| - PA_LOG(INFO) << "Disconnect signal received.";
|
| + case ReceiverState::ERROR:
|
| + // TODO(jingxuy): send a control close over the channel
|
| Disconnect();
|
| break;
|
| + case ReceiverState::WAITING:
|
| + packet_generator_->SetMaxPacketSize(
|
| + packet_receiver_->GetMaxPacketSize());
|
| + if (sub_status() == SubStatus::WAITING_RESPONSE_SIGNAL)
|
| + CompleteConnection();
|
| + break;
|
| + case ReceiverState::CONNECTING:
|
| + case ReceiverState::RECEIVING_DATA:
|
| + // Normal in between states, so do nothing.
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| }
|
| }
|
| }
|
|
|
| -BluetoothLowEnergyConnection::WriteRequest::WriteRequest(
|
| +BluetoothLowEnergyWeaveConnection::WriteRequest::WriteRequest(
|
| const std::vector<uint8_t>& val,
|
| bool flag)
|
| : value(val),
|
| is_last_write_for_wire_message(flag),
|
| number_of_failed_attempts(0) {}
|
|
|
| -BluetoothLowEnergyConnection::WriteRequest::WriteRequest(
|
| +BluetoothLowEnergyWeaveConnection::WriteRequest::WriteRequest(
|
| const WriteRequest& other) = default;
|
|
|
| -BluetoothLowEnergyConnection::WriteRequest::~WriteRequest() {}
|
| +BluetoothLowEnergyWeaveConnection::WriteRequest::~WriteRequest() {}
|
|
|
| -void BluetoothLowEnergyConnection::CompleteConnection() {
|
| +void BluetoothLowEnergyWeaveConnection::CompleteConnection() {
|
| PA_LOG(INFO) << "Connection completed. Time elapsed: "
|
| << base::TimeTicks::Now() - start_time_;
|
| SetSubStatus(SubStatus::CONNECTED);
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::OnCreateGattConnectionError(
|
| +void BluetoothLowEnergyWeaveConnection::OnCreateGattConnectionError(
|
| device::BluetoothDevice::ConnectErrorCode error_code) {
|
| DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION);
|
| PA_LOG(WARNING) << "Error creating GATT connection to "
|
| @@ -338,7 +275,7 @@ void BluetoothLowEnergyConnection::OnCreateGattConnectionError(
|
| Disconnect();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::OnGattConnectionCreated(
|
| +void BluetoothLowEnergyWeaveConnection::OnGattConnectionCreated(
|
| std::unique_ptr<device::BluetoothGattConnection> gatt_connection) {
|
| DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION);
|
| PA_LOG(INFO) << "GATT connection with " << gatt_connection->GetDeviceAddress()
|
| @@ -351,59 +288,61 @@ void BluetoothLowEnergyConnection::OnGattConnectionCreated(
|
| gatt_connection_ = std::move(gatt_connection);
|
| SetSubStatus(SubStatus::WAITING_CHARACTERISTICS);
|
| characteristic_finder_.reset(CreateCharacteristicsFinder(
|
| - base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFound,
|
| + base::Bind(&BluetoothLowEnergyWeaveConnection::OnCharacteristicsFound,
|
| weak_ptr_factory_.GetWeakPtr()),
|
| - base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFinderError,
|
| - weak_ptr_factory_.GetWeakPtr())));
|
| + base::Bind(
|
| + &BluetoothLowEnergyWeaveConnection::OnCharacteristicsFinderError,
|
| + weak_ptr_factory_.GetWeakPtr())));
|
| }
|
|
|
| BluetoothLowEnergyCharacteristicsFinder*
|
| -BluetoothLowEnergyConnection::CreateCharacteristicsFinder(
|
| +BluetoothLowEnergyWeaveConnection::CreateCharacteristicsFinder(
|
| const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback&
|
| success_callback,
|
| const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback&
|
| error_callback) {
|
| return new BluetoothLowEnergyCharacteristicsFinder(
|
| - adapter_, GetRemoteDevice(), remote_service_, to_peripheral_char_,
|
| - from_peripheral_char_, success_callback, error_callback);
|
| + adapter_, GetRemoteDevice(), remote_service_, tx_characteristic_,
|
| + rx_characteristic_, success_callback, error_callback);
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::OnCharacteristicsFound(
|
| +void BluetoothLowEnergyWeaveConnection::OnCharacteristicsFound(
|
| const RemoteAttribute& service,
|
| - const RemoteAttribute& to_peripheral_char,
|
| - const RemoteAttribute& from_peripheral_char) {
|
| + const RemoteAttribute& tx_characteristic,
|
| + const RemoteAttribute& rx_characteristic) {
|
| PA_LOG(INFO) << "Remote chacteristics found.";
|
| PrintTimeElapsed();
|
|
|
| DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS);
|
| remote_service_ = service;
|
| - to_peripheral_char_ = to_peripheral_char;
|
| - from_peripheral_char_ = from_peripheral_char;
|
| + tx_characteristic_ = tx_characteristic;
|
| + rx_characteristic_ = rx_characteristic;
|
|
|
| SetSubStatus(SubStatus::CHARACTERISTICS_FOUND);
|
| StartNotifySession();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::OnCharacteristicsFinderError(
|
| - const RemoteAttribute& to_peripheral_char,
|
| - const RemoteAttribute& from_peripheral_char) {
|
| +void BluetoothLowEnergyWeaveConnection::OnCharacteristicsFinderError(
|
| + const RemoteAttribute& tx_characteristic,
|
| + const RemoteAttribute& rx_characteristic) {
|
| DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS);
|
| PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock "
|
| "service.\n"
|
| - << (to_peripheral_char.id.empty()
|
| - ? to_peripheral_char.uuid.canonical_value()
|
| + << (tx_characteristic.id.empty()
|
| + ? tx_characteristic.uuid.canonical_value()
|
| : "")
|
| - << (from_peripheral_char.id.empty()
|
| - ? ", " + from_peripheral_char.uuid.canonical_value()
|
| - : "") << " not found.";
|
| + << (rx_characteristic.id.empty()
|
| + ? ", " + rx_characteristic.uuid.canonical_value()
|
| + : "")
|
| + << " not found.";
|
|
|
| Disconnect();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::StartNotifySession() {
|
| +void BluetoothLowEnergyWeaveConnection::StartNotifySession() {
|
| if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) {
|
| BluetoothRemoteGattCharacteristic* characteristic =
|
| - GetGattCharacteristic(from_peripheral_char_.id);
|
| + GetGattCharacteristic(rx_characteristic_.id);
|
| DCHECK(characteristic);
|
|
|
| // This is a workaround for crbug.com/507325. If |characteristic| is already
|
| @@ -419,21 +358,14 @@ void BluetoothLowEnergyConnection::StartNotifySession() {
|
|
|
| SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION);
|
| characteristic->StartNotifySession(
|
| - base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionStarted,
|
| + base::Bind(&BluetoothLowEnergyWeaveConnection::OnNotifySessionStarted,
|
| weak_ptr_factory_.GetWeakPtr()),
|
| - base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionError,
|
| + base::Bind(&BluetoothLowEnergyWeaveConnection::OnNotifySessionError,
|
| weak_ptr_factory_.GetWeakPtr()));
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::OnNotifySessionError(
|
| - BluetoothRemoteGattService::GattErrorCode error) {
|
| - DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION);
|
| - PA_LOG(WARNING) << "Error starting notification session: " << error;
|
| - Disconnect();
|
| -}
|
| -
|
| -void BluetoothLowEnergyConnection::OnNotifySessionStarted(
|
| +void BluetoothLowEnergyWeaveConnection::OnNotifySessionStarted(
|
| std::unique_ptr<BluetoothGattNotifySession> notify_session) {
|
| DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION);
|
| PA_LOG(INFO) << "Notification session started "
|
| @@ -446,36 +378,41 @@ void BluetoothLowEnergyConnection::OnNotifySessionStarted(
|
| SendInviteToConnectSignal();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::StopNotifySession() {
|
| +void BluetoothLowEnergyWeaveConnection::OnNotifySessionError(
|
| + BluetoothRemoteGattService::GattErrorCode error) {
|
| + DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION);
|
| + PA_LOG(WARNING) << "Error starting notification session: " << error;
|
| + Disconnect();
|
| +}
|
| +
|
| +void BluetoothLowEnergyWeaveConnection::StopNotifySession() {
|
| if (notify_session_) {
|
| notify_session_->Stop(base::Bind(&base::DoNothing));
|
| notify_session_.reset();
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::SendInviteToConnectSignal() {
|
| +void BluetoothLowEnergyWeaveConnection::SendInviteToConnectSignal() {
|
| if (sub_status() == SubStatus::NOTIFY_SESSION_READY) {
|
| PA_LOG(INFO) << "Sending invite to connect signal";
|
| SetSubStatus(SubStatus::WAITING_RESPONSE_SIGNAL);
|
|
|
| - WriteRequest write_request = BuildWriteRequest(
|
| - ToByteVector(
|
| - static_cast<uint32_t>(ControlSignal::kInviteToConnectSignal)),
|
| - std::vector<uint8_t>(), false);
|
| + WriteRequest write_request =
|
| + WriteRequest(packet_generator_->CreateConnectionRequest(), false);
|
|
|
| WriteRemoteCharacteristic(write_request);
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::WriteRemoteCharacteristic(
|
| - WriteRequest request) {
|
| +void BluetoothLowEnergyWeaveConnection::WriteRemoteCharacteristic(
|
| + const WriteRequest& request) {
|
| write_requests_queue_.push(request);
|
| ProcessNextWriteRequest();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::ProcessNextWriteRequest() {
|
| +void BluetoothLowEnergyWeaveConnection::ProcessNextWriteRequest() {
|
| BluetoothRemoteGattCharacteristic* characteristic =
|
| - GetGattCharacteristic(to_peripheral_char_.id);
|
| + GetGattCharacteristic(tx_characteristic_.id);
|
| if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ &&
|
| characteristic) {
|
| write_remote_characteristic_pending_ = true;
|
| @@ -483,17 +420,18 @@ void BluetoothLowEnergyConnection::ProcessNextWriteRequest() {
|
| PA_LOG(INFO) << "Writing characteristic...";
|
| characteristic->WriteRemoteCharacteristic(
|
| next_request.value,
|
| - base::Bind(&BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten,
|
| - weak_ptr_factory_.GetWeakPtr(),
|
| - next_request.is_last_write_for_wire_message),
|
| base::Bind(
|
| - &BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError,
|
| + &BluetoothLowEnergyWeaveConnection::OnRemoteCharacteristicWritten,
|
| weak_ptr_factory_.GetWeakPtr(),
|
| - next_request.is_last_write_for_wire_message));
|
| + next_request.is_last_write_for_wire_message),
|
| + base::Bind(&BluetoothLowEnergyWeaveConnection::
|
| + OnWriteRemoteCharacteristicError,
|
| + weak_ptr_factory_.GetWeakPtr(),
|
| + next_request.is_last_write_for_wire_message));
|
| }
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten(
|
| +void BluetoothLowEnergyWeaveConnection::OnRemoteCharacteristicWritten(
|
| bool run_did_send_message_callback) {
|
| PA_LOG(INFO) << "Characteristic written.";
|
| write_remote_characteristic_pending_ = false;
|
| @@ -507,11 +445,11 @@ void BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten(
|
| ProcessNextWriteRequest();
|
| }
|
|
|
| -void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError(
|
| +void BluetoothLowEnergyWeaveConnection::OnWriteRemoteCharacteristicError(
|
| bool run_did_send_message_callback,
|
| BluetoothRemoteGattService::GattErrorCode error) {
|
| PA_LOG(WARNING) << "Error " << error << " writing characteristic: "
|
| - << to_peripheral_char_.uuid.canonical_value();
|
| + << tx_characteristic_.uuid.canonical_value();
|
| write_remote_characteristic_pending_ = false;
|
| // TODO(sacomoto): Actually pass the current message to the observer.
|
| if (run_did_send_message_callback)
|
| @@ -527,21 +465,11 @@ void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError(
|
| ProcessNextWriteRequest();
|
| }
|
|
|
| -BluetoothLowEnergyConnection::WriteRequest
|
| -BluetoothLowEnergyConnection::BuildWriteRequest(
|
| - const std::vector<uint8_t>& signal,
|
| - const std::vector<uint8_t>& bytes,
|
| - bool is_last_write_for_wire_message) {
|
| - std::vector<uint8_t> value(signal.begin(), signal.end());
|
| - value.insert(value.end(), bytes.begin(), bytes.end());
|
| - return WriteRequest(value, is_last_write_for_wire_message);
|
| -}
|
| -
|
| -void BluetoothLowEnergyConnection::PrintTimeElapsed() {
|
| +void BluetoothLowEnergyWeaveConnection::PrintTimeElapsed() {
|
| PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_;
|
| }
|
|
|
| -std::string BluetoothLowEnergyConnection::GetDeviceAddress() {
|
| +std::string BluetoothLowEnergyWeaveConnection::GetDeviceAddress() {
|
| // When the remote device is connected we should rely on the address given by
|
| // |gatt_connection_|. As the device address may change if the device is
|
| // paired. The address in |gatt_connection_| is automatically updated in this
|
| @@ -550,7 +478,7 @@ std::string BluetoothLowEnergyConnection::GetDeviceAddress() {
|
| : remote_device().bluetooth_address;
|
| }
|
|
|
| -BluetoothDevice* BluetoothLowEnergyConnection::GetRemoteDevice() {
|
| +BluetoothDevice* BluetoothLowEnergyWeaveConnection::GetRemoteDevice() {
|
| // It's not possible to simply use
|
| // |adapter_->GetDevice(GetDeviceAddress())| to find the device with MAC
|
| // address |GetDeviceAddress()|. For paired devices,
|
| @@ -567,7 +495,8 @@ BluetoothDevice* BluetoothLowEnergyConnection::GetRemoteDevice() {
|
| return nullptr;
|
| }
|
|
|
| -BluetoothRemoteGattService* BluetoothLowEnergyConnection::GetRemoteService() {
|
| +BluetoothRemoteGattService*
|
| +BluetoothLowEnergyWeaveConnection::GetRemoteService() {
|
| BluetoothDevice* remote_device = GetRemoteDevice();
|
| if (!remote_device) {
|
| PA_LOG(WARNING) << "Remote device not found.";
|
| @@ -586,7 +515,7 @@ BluetoothRemoteGattService* BluetoothLowEnergyConnection::GetRemoteService() {
|
| }
|
|
|
| BluetoothRemoteGattCharacteristic*
|
| -BluetoothLowEnergyConnection::GetGattCharacteristic(
|
| +BluetoothLowEnergyWeaveConnection::GetGattCharacteristic(
|
| const std::string& gatt_characteristic) {
|
| BluetoothRemoteGattService* remote_service = GetRemoteService();
|
| if (!remote_service) {
|
| @@ -598,14 +527,14 @@ BluetoothLowEnergyConnection::GetGattCharacteristic(
|
|
|
| // TODO(sacomoto): make this robust to byte ordering in both sides of the
|
| // SmartLock BLE socket.
|
| -uint32_t BluetoothLowEnergyConnection::ToUint32(
|
| +uint32_t BluetoothLowEnergyWeaveConnection::ToUint32(
|
| const std::vector<uint8_t>& 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::vector<uint8_t> BluetoothLowEnergyConnection::ToByteVector(
|
| +const std::vector<uint8_t> BluetoothLowEnergyWeaveConnection::ToByteVector(
|
| const uint32_t value) {
|
| std::vector<uint8_t> bytes(4, 0);
|
| bytes[0] = static_cast<uint8_t>(value);
|
|
|