Commit 939b63be authored by Vicky Min's avatar Vicky Min Committed by Commit Bot

[bluetooth] Read/Write serial interface for BT

The class BluetoothSerialPortImpl is intended to support
Serial API features for Bluetooth SPP devices. StartReading and
StartWriting to read from or write to the bluetooth socket
connected, and Flush/Drain to make sure Read and Write are
handled correctly when they are finished.

Bug: 1043300
Change-Id: I97fc2970215c2bfa479a0a8c61d78a03aa9a7ae3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2334682
Commit-Queue: Vicky Min <vickymin@google.com>
Reviewed-by: default avatarMatt Reynolds <mattreynolds@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarOvidio de Jesús Ruiz-Henríquez <odejesush@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798982}
parent e62b6be0
......@@ -8,7 +8,8 @@ if (is_android) {
import("//build/config/android/rules.gni")
}
is_serial_enabled_platform = is_win || ((is_linux || is_chromeos) && use_udev) || is_mac
is_serial_enabled_platform =
is_win || ((is_linux || is_chromeos) && use_udev) || is_mac
source_set("lib") {
# This should be visible only to embedders of the Device Service, and the
......@@ -247,6 +248,7 @@ source_set("tests") {
if (is_serial_enabled_platform) {
sources += [
"serial/bluetooth_serial_port_impl_unittest.cc",
"serial/serial_device_enumerator_linux_unittest.cc",
"serial/serial_device_enumerator_unittest.cc",
"serial/serial_port_impl_unittest.cc",
......@@ -263,6 +265,7 @@ source_set("tests") {
deps += [
"//device/bluetooth:mocks",
"//services/device/public/cpp/bluetooth:bluetooth",
"//services/device/public/cpp/serial:switches",
"//services/device/serial",
"//services/device/serial:test_support",
......
......@@ -11,6 +11,7 @@ source_set("bluetooth") {
deps = [
"//base",
"//device/bluetooth",
"//device/bluetooth/public/cpp:cpp",
"//device/bluetooth/strings",
"//services/device/public/mojom",
"//ui/base",
......
......@@ -139,4 +139,11 @@ base::string16 GetBluetoothDeviceLabelForAccessibility(
name_utf16);
}
const BluetoothUUID& GetSerialPortProfileUUID() {
// The Serial Port Profile (SPP) UUID is 1101.
// https://chromium-review.googlesource.com/c/chromium/src/+/2334682/17..19
static const BluetoothUUID kValue("1101");
return kValue;
}
} // namespace device
......@@ -6,6 +6,7 @@
#define SERVICES_DEVICE_PUBLIC_CPP_BLUETOOTH_BLUETOOTH_UTILS_H_
#include "base/strings/string16.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "services/device/public/mojom/bluetooth_system.mojom.h"
namespace device {
......@@ -25,6 +26,9 @@ base::string16 GetBluetoothDeviceNameForDisplay(
base::string16 GetBluetoothDeviceLabelForAccessibility(
const mojom::BluetoothDeviceInfoPtr& device_info);
// Returns a BluetoothUUID for a Bluetooth SPP device.
const BluetoothUUID& GetSerialPortProfileUUID();
} // namespace device
#endif // SERVICES_DEVICE_PUBLIC_CPP_BLUETOOTH_BLUETOOTH_UTILS_H_
......@@ -23,6 +23,8 @@ if (is_win || ((is_linux || is_chromeos) && use_udev) || is_mac) {
sources = [
"bluetooth_serial_device_enumerator.cc",
"bluetooth_serial_device_enumerator.h",
"bluetooth_serial_port_impl.cc",
"bluetooth_serial_port_impl.h",
"buffer.cc",
"buffer.h",
"serial_device_enumerator.cc",
......@@ -53,6 +55,7 @@ if (is_win || ((is_linux || is_chromeos) && use_udev) || is_mac) {
"//device/bluetooth/public/cpp",
"//mojo/public/cpp/bindings",
"//net",
"//services/device/public/cpp/bluetooth:bluetooth",
"//services/device/public/cpp/serial:switches",
]
......
......@@ -7,6 +7,7 @@
#include "base/command_line.h"
#include "base/unguessable_token.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "services/device/public/cpp/bluetooth/bluetooth_utils.h"
#include "services/device/public/cpp/serial/serial_switches.h"
#include "services/device/public/mojom/serial.mojom.h"
......@@ -20,11 +21,6 @@ BluetoothSerialDeviceEnumerator::BluetoothSerialDeviceEnumerator() {
base::Unretained(this)));
}
const BluetoothUUID& GetSerialPortProfileUUID() {
static const BluetoothUUID kValue("1101");
return kValue;
}
BluetoothSerialDeviceEnumerator::~BluetoothSerialDeviceEnumerator() = default;
void BluetoothSerialDeviceEnumerator::OnGotClassicAdapter(
......
This diff is collapsed.
// Copyright 2020 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 SERVICES_DEVICE_SERIAL_BLUETOOTH_SERIAL_PORT_IMPL_H_
#define SERVICES_DEVICE_SERIAL_BLUETOOTH_SERIAL_PORT_IMPL_H_
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "services/device/public/mojom/serial.mojom.h"
#include "services/device/serial/serial_io_handler.h"
#include "services/device/serial/serial_port_impl.h"
namespace device {
// This class is intended to allow serial communication using a Bluetooth
// SPP device. The Bluetooth device is used to create a Bluetooth socket
// which is closed upon error in any of the interface functions.
class BluetoothSerialPortImpl : public mojom::SerialPort {
public:
// Creates of instance of BluetoothSerialPortImpl using a Bluetooth
// device and a receiver/watcher to create a pipe. The receiver and
// watcher will own this object.
static void Create(
std::unique_ptr<BluetoothDevice> device,
mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher);
BluetoothSerialPortImpl(
std::unique_ptr<BluetoothDevice> device,
mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher);
BluetoothSerialPortImpl(const BluetoothSerialPortImpl&) = delete;
BluetoothSerialPortImpl& operator=(const BluetoothSerialPortImpl&) = delete;
~BluetoothSerialPortImpl() override;
private:
// mojom::SerialPort methods:
void Open(mojom::SerialConnectionOptionsPtr options,
mojo::PendingRemote<mojom::SerialPortClient> client,
OpenCallback callback) override;
void StartWriting(mojo::ScopedDataPipeConsumerHandle consumer) override;
void StartReading(mojo::ScopedDataPipeProducerHandle producer) override;
void Flush(mojom::SerialPortFlushMode mode, FlushCallback callback) override;
void Drain(DrainCallback callback) override;
void GetControlSignals(GetControlSignalsCallback callback) override;
void SetControlSignals(mojom::SerialHostControlSignalsPtr signals,
SetControlSignalsCallback callback) override;
void ConfigurePort(mojom::SerialConnectionOptionsPtr options,
ConfigurePortCallback callback) override;
void GetPortInfo(GetPortInfoCallback callback) override;
void Close(CloseCallback callback) override;
void WriteToSocket(MojoResult result, const mojo::HandleSignalsState& state);
void ReadFromSocketAndWriteOut(MojoResult result,
const mojo::HandleSignalsState& state);
void ReadMore();
void WriteMore();
void OnSocketConnected(OpenCallback callback,
scoped_refptr<BluetoothSocket> socket);
void OnSocketConnectedError(OpenCallback callback,
const std::string& message);
void OnBluetoothSocketReceive(base::span<char> pending_write_buffer,
int num_bytes_received,
scoped_refptr<net::IOBuffer> io_buffer);
void OnBluetoothSocketReceiveError(
device::BluetoothSocket::ErrorReason error_reason,
const std::string& error_message);
void OnBluetoothSocketSend(int num_bytes_sent);
void OnBluetoothSocketSendError(const std::string& error_message);
mojo::Receiver<mojom::SerialPort> receiver_;
mojo::Remote<mojom::SerialPortConnectionWatcher> watcher_;
mojo::Remote<mojom::SerialPortClient> client_;
// Data pipes for input and output.
mojo::ScopedDataPipeConsumerHandle in_stream_;
mojo::SimpleWatcher in_stream_watcher_;
mojo::ScopedDataPipeProducerHandle out_stream_;
mojo::SimpleWatcher out_stream_watcher_;
// Holds the callback for a flush or drain until pending operations have been
// completed.
FlushCallback read_flush_callback_;
FlushCallback write_flush_callback_;
DrainCallback drain_callback_;
scoped_refptr<device::BluetoothSocket> bluetooth_socket_;
std::unique_ptr<BluetoothDevice> bluetooth_device_;
bool read_pending_ = false;
bool write_pending_ = false;
base::WeakPtrFactory<BluetoothSerialPortImpl> weak_ptr_factory_{this};
};
} // namespace device
#endif // SERVICES_DEVICE_SERIAL_BLUETOOTH_SERIAL_PORT_IMPL_H_
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment