| Index: components/proximity_auth/ble/bluetooth_low_energy_weave_client_connection.h
|
| diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection.h b/components/proximity_auth/ble/bluetooth_low_energy_weave_client_connection.h
|
| similarity index 62%
|
| copy from components/proximity_auth/ble/bluetooth_low_energy_connection.h
|
| copy to components/proximity_auth/ble/bluetooth_low_energy_weave_client_connection.h
|
| index dec8d419655c3ba270aa455089345cb644fa5be7..bda1ee7b2efc8d73aa1b90fd7f8a4f6e8e188ba7 100644
|
| --- a/components/proximity_auth/ble/bluetooth_low_energy_connection.h
|
| +++ b/components/proximity_auth/ble/bluetooth_low_energy_weave_client_connection.h
|
| @@ -2,12 +2,13 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_
|
| -#define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_
|
| +#ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNECTION_H_
|
| +#define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNECTION_H_
|
|
|
| #include <stddef.h>
|
| #include <stdint.h>
|
|
|
| +#include <map>
|
| #include <memory>
|
| #include <queue>
|
| #include <string>
|
| @@ -17,6 +18,8 @@
|
| #include "base/memory/weak_ptr.h"
|
| #include "base/time/time.h"
|
| #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_finder.h"
|
| +#include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_generator.h"
|
| +#include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiver.h"
|
| #include "components/proximity_auth/ble/fake_wire_message.h"
|
| #include "components/proximity_auth/ble/remote_attribute.h"
|
| #include "components/proximity_auth/connection.h"
|
| @@ -31,70 +34,67 @@ class TaskRunner;
|
| }
|
|
|
| namespace proximity_auth {
|
| -
|
| class BluetoothThrottler;
|
|
|
| -// 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.
|
| -//
|
| -// The connection flow is described below.
|
| -//
|
| -// Discover Reader and Writer Characteristics
|
| -// |
|
| -// |
|
| -// |
|
| -// Start Notify Session
|
| -// |
|
| -// |
|
| -// |
|
| -// Write kInviteToConnectSignal to Writer Characteristic
|
| -// |
|
| -// |
|
| -// |
|
| -// Read kInvitationResponseSignal from Reader Characteristic
|
| -// |
|
| -// |
|
| -// |
|
| -// Proximity Auth Connection Established
|
| -class BluetoothLowEnergyConnection : public Connection,
|
| - public device::BluetoothAdapter::Observer {
|
| +namespace weave {
|
| +// Creates GATT connection on top of the BLE connection and act as a Client.
|
| +// uWeave communication follows the flow:
|
| +// Client | Server
|
| +// ---------------------------------|--------------------------------
|
| +// send connection request |
|
| +// | receive connection request
|
| +// | send connection response
|
| +// receive connection response |
|
| +// opt: send data | opt: send data
|
| +// receive data | receive data
|
| +// opt: close connection | opt: close connection
|
| +class BluetoothLowEnergyWeaveClientConnection
|
| + : public Connection,
|
| + public device::BluetoothAdapter::Observer {
|
| public:
|
| - // Signals sent to the remote device to indicate connection related events.
|
| - enum class ControlSignal : uint32_t {
|
| - kInviteToConnectSignal = 0,
|
| - kInvitationResponseSignal = 1,
|
| - kSendSignal = 2,
|
| - kDisconnectSignal = 3,
|
| + class Factory {
|
| + public:
|
| + static std::unique_ptr<BluetoothLowEnergyWeaveClientConnection>
|
| + NewInstance();
|
| +
|
| + // Exposed for testing.
|
| + static void SetInstanceForTesting(Factory* factory);
|
| +
|
| + protected:
|
| + // Exposed for testing.
|
| + virtual std::unique_ptr<BluetoothLowEnergyWeaveClientConnection>
|
| + BuildInstance();
|
| +
|
| + private:
|
| + static Factory* factory_instance_;
|
| };
|
|
|
| - // The sub-state of a proximity_auth::BluetoothLowEnergyConnection class
|
| + // The sub-state of a proximity_auth::BluetoothLowEnergyWeaveClientConnection
|
| // extends the IN_PROGRESS state of proximity_auth::Connection::Status.
|
| - enum class SubStatus {
|
| + enum SubStatus {
|
| DISCONNECTED,
|
| WAITING_GATT_CONNECTION,
|
| WAITING_CHARACTERISTICS,
|
| CHARACTERISTICS_FOUND,
|
| WAITING_NOTIFY_SESSION,
|
| NOTIFY_SESSION_READY,
|
| - WAITING_RESPONSE_SIGNAL,
|
| + WAITING_CONNECTION_RESPONSE,
|
| CONNECTED,
|
| };
|
|
|
| // Constructs a Bluetooth low energy connection to the service with
|
| // |remote_service_| on the |remote_device|. The |adapter| must be already
|
| - // initaalized and ready. The GATT connection may alreaady be established and
|
| + // initialized and ready. The GATT connection may alreaady be established and
|
| // pass through |gatt_connection|. A subsequent call to Connect() must be
|
| // made.
|
| - BluetoothLowEnergyConnection(
|
| + BluetoothLowEnergyWeaveClientConnection(
|
| const RemoteDevice& remote_device,
|
| scoped_refptr<device::BluetoothAdapter> adapter,
|
| const device::BluetoothUUID remote_service_uuid,
|
| BluetoothThrottler* bluetooth_throttler,
|
| int max_number_of_write_attempts);
|
|
|
| - ~BluetoothLowEnergyConnection() override;
|
| + ~BluetoothLowEnergyWeaveClientConnection() override;
|
|
|
| // proximity_auth::Connection:
|
| void Connect() override;
|
| @@ -103,7 +103,9 @@ class BluetoothLowEnergyConnection : public Connection,
|
|
|
| protected:
|
| // Exposed for testing.
|
| - void SetSubStatus(SubStatus status);
|
| + void DestroyConnection();
|
| +
|
| + // Exposed for testing.
|
| SubStatus sub_status() { return sub_status_; }
|
|
|
| // Sets |task_runner_| for testing.
|
| @@ -127,25 +129,35 @@ class BluetoothLowEnergyConnection : public Connection,
|
| void GattCharacteristicValueChanged(
|
| device::BluetoothAdapter* adapter,
|
| device::BluetoothRemoteGattCharacteristic* characteristic,
|
| - const std::vector<uint8_t>& value) override;
|
| + const Packet& value) override;
|
|
|
| private:
|
| + enum WriteRequestType {
|
| + REGULAR,
|
| + MESSAGE_COMPLETE,
|
| + CONNECTION_REQUEST,
|
| + CONNECTION_CLOSE
|
| + };
|
| +
|
| // Represents a request to write |value| to a some characteristic.
|
| // |is_last_write_for_wire_messsage| indicates whether this request
|
| // corresponds to the last write request for some wire message.
|
| - // A WireMessage corresponds to exactly two WriteRequest: the first containing
|
| - // a kSendSignal + the size of the WireMessage, and the second containing a
|
| - // SendStatusSignal + the serialized WireMessage.
|
| struct WriteRequest {
|
| - WriteRequest(const std::vector<uint8_t>& val, bool flag);
|
| + WriteRequest(const Packet& val,
|
| + WriteRequestType request_type,
|
| + std::shared_ptr<WireMessage> message);
|
| + WriteRequest(const Packet& val, WriteRequestType request_type);
|
| WriteRequest(const WriteRequest& other);
|
| ~WriteRequest();
|
|
|
| - std::vector<uint8_t> value;
|
| - bool is_last_write_for_wire_message;
|
| + Packet value;
|
| + WriteRequestType request_type;
|
| + std::shared_ptr<WireMessage> message;
|
| int number_of_failed_attempts;
|
| };
|
|
|
| + void SetSubStatus(SubStatus status);
|
| +
|
| // Creates the GATT connection with |remote_device|.
|
| void CreateGattConnection();
|
|
|
| @@ -157,36 +169,35 @@ class BluetoothLowEnergyConnection : public Connection,
|
| void OnCreateGattConnectionError(
|
| device::BluetoothDevice::ConnectErrorCode error_code);
|
|
|
| - // Callback called when |to_peripheral_char_| and |from_peripheral_char_| were
|
| + // Callback called when |tx_characteristic_| and |rx_characteristic_| were
|
| // found.
|
| void OnCharacteristicsFound(const RemoteAttribute& service,
|
| - const RemoteAttribute& to_peripheral_char,
|
| - const RemoteAttribute& from_peripheral_char);
|
| + const RemoteAttribute& tx_characteristic,
|
| + const RemoteAttribute& rx_characteristic);
|
|
|
| // Callback called there was an error finding the characteristics.
|
| - void OnCharacteristicsFinderError(
|
| - const RemoteAttribute& to_peripheral_char,
|
| - const RemoteAttribute& from_peripheral_char);
|
| + void OnCharacteristicsFinderError(const RemoteAttribute& tx_characteristic,
|
| + const RemoteAttribute& rx_characteristic);
|
|
|
| - // Starts a notify session for |from_peripheral_char_| when ready
|
| + // Starts a notify session for |rx_characteristic_| when ready
|
| // (SubStatus::CHARACTERISTICS_FOUND).
|
| void StartNotifySession();
|
|
|
| // Called when a notification session is successfully started for
|
| - // |from_peripheral_char_| characteristic.
|
| + // |rx_characteristic_| characteristic.
|
| void OnNotifySessionStarted(
|
| std::unique_ptr<device::BluetoothGattNotifySession> notify_session);
|
|
|
| // Called when there is an error starting a notification session for
|
| - // |from_peripheral_char_| characteristic.
|
| + // |rx_characteristic_| characteristic.
|
| void OnNotifySessionError(device::BluetoothGattService::GattErrorCode);
|
|
|
| // Stops |notify_session_|.
|
| void StopNotifySession();
|
|
|
| - // Sends an invite to connect signal to the peripheral if when ready
|
| + // Sends a connection request to server if ready
|
| // (SubStatus::NOTIFY_SESSION_READY).
|
| - void SendInviteToConnectSignal();
|
| + void SendConnectionRequest();
|
|
|
| // Completes and updates the status accordingly.
|
| void CompleteConnection();
|
| @@ -202,7 +213,7 @@ class BluetoothLowEnergyConnection : public Connection,
|
| // (OnWriteRemoteCharacteristicError()), it remains on the queue and will be
|
| // retried. |request| will remain on the queue until it succeeds or it
|
| // triggers a Disconnect() call (after |max_number_of_tries_|).
|
| - void WriteRemoteCharacteristic(WriteRequest request);
|
| + void WriteRemoteCharacteristic(const WriteRequest& request);
|
|
|
| // Processes the next request in |write_requests_queue_|.
|
| void ProcessNextWriteRequest();
|
| @@ -210,19 +221,14 @@ class BluetoothLowEnergyConnection : public Connection,
|
| // Called when the
|
| // BluetoothRemoteGattCharacteristic::RemoteCharacteristicWrite() is
|
| // successfully complete.
|
| - void OnRemoteCharacteristicWritten(bool run_did_send_message_callback);
|
| + void OnRemoteCharacteristicWritten();
|
|
|
| // Called when there is an error writing to the remote characteristic
|
| - // |to_peripheral_char_|.
|
| + // |tx_characteristic_|.
|
| void OnWriteRemoteCharacteristicError(
|
| - bool run_did_send_message_callback,
|
| device::BluetoothRemoteGattService::GattErrorCode error);
|
|
|
| - // Builds the value to be written on |to_peripheral_char_|. The value
|
| - // corresponds to |signal| concatenated with |payload|.
|
| - WriteRequest BuildWriteRequest(const std::vector<uint8_t>& signal,
|
| - const std::vector<uint8_t>& bytes,
|
| - bool is_last_message_for_wire_message);
|
| + void OnPacketReceiverError();
|
|
|
| // Prints the time elapsed since |Connect()| was called.
|
| void PrintTimeElapsed();
|
| @@ -239,11 +245,10 @@ class BluetoothLowEnergyConnection : public Connection,
|
| device::BluetoothRemoteGattCharacteristic* GetGattCharacteristic(
|
| const std::string& identifier);
|
|
|
| - // Convert the first 4 bytes from a byte vector to a uint32_t.
|
| - uint32_t ToUint32(const std::vector<uint8_t>& bytes);
|
| -
|
| - // Convert an uint32_t to a byte vector.
|
| - const std::vector<uint8_t> ToByteVector(uint32_t value);
|
| + // Get the reason that the other side of the connection decided to close the
|
| + // connection.
|
| + // Will crash if the receiver is not in CONNECTION_CLOSED state.
|
| + std::string GetReasonForClose();
|
|
|
| // The Bluetooth adapter over which the Bluetooth connection will be made.
|
| scoped_refptr<device::BluetoothAdapter> adapter_;
|
| @@ -251,11 +256,17 @@ class BluetoothLowEnergyConnection : public Connection,
|
| // Remote service the |gatt_connection_| was established with.
|
| RemoteAttribute remote_service_;
|
|
|
| + // uWeave packet generator.
|
| + std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> packet_generator_;
|
| +
|
| + // uWeave packet receiver.
|
| + std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> packet_receiver_;
|
| +
|
| // Characteristic used to send data to the remote device.
|
| - RemoteAttribute to_peripheral_char_;
|
| + RemoteAttribute tx_characteristic_;
|
|
|
| // Characteristic used to receive data from the remote device.
|
| - RemoteAttribute from_peripheral_char_;
|
| + RemoteAttribute rx_characteristic_;
|
|
|
| // Throttles repeated connection attempts to the same device. This is a
|
| // workaround for crbug.com/508919. Not owned, must outlive this instance.
|
| @@ -270,19 +281,12 @@ class BluetoothLowEnergyConnection : public Connection,
|
| std::unique_ptr<BluetoothLowEnergyCharacteristicsFinder>
|
| characteristic_finder_;
|
|
|
| - // The notify session for |from_peripheral_char|.
|
| + // The notify session for |rx_characteristic|.
|
| std::unique_ptr<device::BluetoothGattNotifySession> notify_session_;
|
|
|
| // Internal connection status
|
| SubStatus sub_status_;
|
|
|
| - // Indicates a receiving operation is in progress. This is set after a
|
| - // ControlSignal::kSendSignal was received from the remote device.
|
| - bool receiving_bytes_;
|
| -
|
| - // Total number of bytes expected for the current receive operation.
|
| - std::size_t expected_number_of_incoming_bytes_;
|
| -
|
| // Bytes already received for the current receive operation.
|
| std::string incoming_bytes_buffer_;
|
|
|
| @@ -296,21 +300,17 @@ class BluetoothLowEnergyConnection : public Connection,
|
| // Maximum number of tries to send any write request.
|
| int max_number_of_write_attempts_;
|
|
|
| - // Maximum number of bytes that fit in a single chunk to be written in
|
| - // |to_peripheral_char_|. Ideally, this should be the maximum value the
|
| - // peripheral supports and it should be agreed when the GATT connection is
|
| - // created. Currently, there is no API to find this value. The implementation
|
| - // uses a hard-coded constant.
|
| - int max_chunk_size_;
|
| -
|
| // Stores when the instace was created.
|
| base::TimeTicks start_time_;
|
|
|
| - base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_;
|
| + base::WeakPtrFactory<BluetoothLowEnergyWeaveClientConnection>
|
| + weak_ptr_factory_;
|
|
|
| - DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection);
|
| + DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyWeaveClientConnection);
|
| };
|
|
|
| +} // namespace weave
|
| +
|
| } // namespace proximity_auth
|
|
|
| -#endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_
|
| +#endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNECTION_H_
|
|
|