Commit 69808eef authored by Jie Jiang's avatar Jie Jiang Committed by Commit Bot

arc: bluetooth: Implement L2CAP LE socket (2/2)

This patch adds and implements the new interfaces/methods for Bluetooth
socket. Compared with the old interfaces, the new ones:
- add a sock_type field (RFCOMM or L2CAP);
- pass Android socket flags directly instead of parsing those into BlueZ
  flags on Android side, to avoid expose two groups of constants in
  Android.

Also marks the old methods as deprecated, which will be removed when the
corresponding patch(es) are landed in both P and R.

Android patch see: http://ag/12418518

BUG=b:163099156
TEST=manually tested on octopus with P to make sure the old code path
  for RFCOMM still works; also see the TEST line in Android patch for R.

Change-Id: Ic35732e4e9ee6a45599f9aecd6b2695b60dd124a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2366445
Commit-Queue: Jie Jiang <jiejiang@chromium.org>
Reviewed-by: default avatarGreg Kerr <kerrnel@chromium.org>
Reviewed-by: default avatarMiao-chen Chou <mcchou@chromium.org>
Reviewed-by: default avatarHidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813048}
parent 2f652b8c
......@@ -267,7 +267,8 @@ class ArcBluetoothBridge
void ReadRemoteRssi(mojom::BluetoothAddressPtr remote_addr,
ReadRemoteRssiCallback callback) override;
void OpenBluetoothSocket(OpenBluetoothSocketCallback callback) override;
void OpenBluetoothSocketDeprecated(
OpenBluetoothSocketDeprecatedCallback callback) override;
// Bluetooth Mojo host interface - Bluetooth Gatt Server functions
// Android counterpart link:
......@@ -316,13 +317,25 @@ class ArcBluetoothBridge
RemoveSdpRecordCallback callback) override;
// Bluetooth Mojo host interface - Bluetooth RFCOMM functions
void RfcommListen(int32_t channel,
int32_t optval,
RfcommListenCallback callback) override;
void RfcommConnect(mojom::BluetoothAddressPtr remote_addr,
int32_t channel,
int32_t optval,
RfcommConnectCallback callback) override;
void RfcommListenDeprecated(int32_t channel,
int32_t optval,
RfcommListenDeprecatedCallback callback) override;
void RfcommConnectDeprecated(
mojom::BluetoothAddressPtr remote_addr,
int32_t channel,
int32_t optval,
RfcommConnectDeprecatedCallback callback) override;
// Bluetooth Mojo host interface - Bluetooth socket functions
void BluetoothSocketListen(mojom::BluetoothSocketType sock_type,
mojom::BluetoothSocketFlagsPtr sock_flags,
int32_t port,
BluetoothSocketListenCallback callback) override;
void BluetoothSocketConnect(mojom::BluetoothSocketType sock_type,
mojom::BluetoothSocketFlagsPtr sock_flags,
mojom::BluetoothAddressPtr remote_addr,
int32_t port,
BluetoothSocketConnectCallback callback) override;
// Set up or disable multiple advertising.
void ReserveAdvertisementHandle(
......@@ -551,20 +564,24 @@ class ArcBluetoothBridge
// Data structures for Bluetooth listening/connecting sockets that live in
// Chrome.
struct BluetoothListeningSocket {
mojom::BluetoothSocketType sock_type;
// TODO(b/163099156): Remove the following two fields when
// RfcommListen()/RfcommConnect() are removed.
// RfcommListenDeprecated()/RfcommConnectDeprecated() are removed.
bool created_by_deprecated_method = false;
mojo::Remote<mojom::RfcommListeningSocketClient> deprecated_remote;
mojo::Remote<mojom::BluetoothListenSocketClient> remote;
base::ScopedFD file;
std::unique_ptr<base::FileDescriptorWatcher::Controller> controller;
BluetoothListeningSocket();
~BluetoothListeningSocket();
};
struct BluetoothConnectingSocket {
mojom::BluetoothSocketType sock_type;
// TODO(b/163099156): Remove the following two fields when
// RfcommListen()/RfcommConnect() are removed.
// RfcommListenDeprecated()/RfcommConnectDeprecated() are removed.
bool created_by_deprecated_method = false;
mojo::Remote<mojom::RfcommConnectingSocketClient> deprecated_remote;
mojo::Remote<mojom::BluetoothConnectSocketClient> remote;
base::ScopedFD file;
std::unique_ptr<base::FileDescriptorWatcher::Controller> controller;
BluetoothConnectingSocket();
......@@ -572,19 +589,21 @@ class ArcBluetoothBridge
};
// Creates a Bluetooth socket with socket option |optval|, and then bind() and
// listen() with requested |channel| number. The actual channel number will be
// filled in |channel| as the return value. Returns a BluetoothListeningSocket
// listen() with requested |port| number. The actual port number will be
// filled in |port| as the return value. Returns a BluetoothListeningSocket
// that holds the socket.
std::unique_ptr<BluetoothListeningSocket> CreateBluetoothListenSocket(
mojom::BluetoothSocketType type,
int32_t optval,
uint16_t* channel);
uint16_t* port);
// Creates a Bluetooth socket with socket option |optval|, and then calls
// connect() to (|addr|, |channel|). This connect() call is non-blocking.
// connect() to (|addr|, |port|). This connect() call is non-blocking.
// Returns a BluetoothConnectingSocket that holds the socket.
std::unique_ptr<BluetoothConnectingSocket> CreateBluetoothConnectSocket(
mojom::BluetoothSocketType type,
int32_t optval,
mojom::BluetoothAddressPtr addr,
uint16_t channel);
uint16_t port);
// Closes Bluetooth sockets. Releases the corresponding resources.
void CloseBluetoothListeningSocket(BluetoothListeningSocket* socket);
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Next MinVersion: 18
// Next MinVersion: 19
module arc.mojom;
......@@ -316,8 +316,56 @@ interface RfcommConnectingSocketClient {
OnConnectFailed@1();
};
// Next Method ID: 47
// Deprecated Method ID: 4, 5, 6, 7, 20, 21
// Copied from Android. See android.bluetooth.BluetoothSocket.
// Currently we only support RFCOMM and LE CoC (L2CAP_LE) sockets.
[Extensible]
enum BluetoothSocketType {
TYPE_RFCOMM = 1,
TYPE_L2CAP_LE = 4,
};
// Bluetooth socket security flags in Android, used in open socket requests.
// Chrome should set the socket option based on these flags.
struct BluetoothSocketFlags {
bool encrypt;
bool auth;
bool auth_mitm;
bool auth_16_digit;
};
// BluetoothSocketConnection contains the information for a new Bluetooth socket
// connection. Note that |port| is either the channel number for an RFCOMM
// socket or PSM for an L2CAP socket. Since we cannot get socket (or peer) name
// on the transferred socket on Android side, we also need to pass the peer
// address and port number.
struct BluetoothSocketConnection {
handle sock;
BluetoothAddress addr;
int32 port;
};
// The mojo connection represents a listening socket.
// Next Method ID: 1
interface BluetoothListenSocketClient {
// Called when accept() succeeds. |port| in |connection| is the peer port
// number.
OnAccepted@0(BluetoothSocketConnection connection);
};
// The mojo connection represents a connecting (not connected yet) socket.
// After connect() either succeeds or fails, Android is responsible for closing
// this mojo connection.
// Next Method ID: 2
interface BluetoothConnectSocketClient {
// Called when connect() succeeds. |port| in |connection| is the port number
// on our side.
OnConnected@0(BluetoothSocketConnection connection);
// Called when connect() fails.
OnConnectFailed@1();
};
// Next Method ID: 49
// Deprecated Method ID: 4, 5, 6, 7, 20, 21, 29, 45, 46
interface BluetoothHost {
EnableAdapter@0() => (BluetoothAdapterState state);
DisableAdapter@1() => (BluetoothAdapterState state);
......@@ -375,7 +423,9 @@ interface BluetoothHost {
[MinVersion=1] ReadRemoteRssi@28(BluetoothAddress remote_addr)
=> (int32 rssi);
[MinVersion=2] OpenBluetoothSocket@29()
// DEPRECATED: Use BluetoothSocketListen@47 or BluetoothSocketConnect@48
// instead.
[MinVersion=2] OpenBluetoothSocketDeprecated@29()
=> (handle sock);
// Bluetooth Gatt Server functions
......@@ -430,7 +480,7 @@ interface BluetoothHost {
// we will select a channel number automatically. If this process succeeds,
// returns SUCCESS in |status|, the actual listening channel in |channel|,
// and a new mojo connection which represents the listening socket.
[MinVersion=15] RfcommListen@45(int32 channel, int32 optval)
[MinVersion=15] RfcommListenDeprecated@45(int32 channel, int32 optval)
=> (BluetoothStatus status, int32 channel,
RfcommListeningSocketClient&? client);
// Opens a bluetooth socket with socket option |optval|, and then connect()
......@@ -438,9 +488,27 @@ interface BluetoothHost {
// in |status| and a new mojo connection which holds the connecting socket.
// Unlike in RfcommListen(), |channel| here could not be 0, since this is the
// peer channel number.
[MinVersion=15] RfcommConnect@46(BluetoothAddress remote_addr,
int32 channel, int32 optval)
[MinVersion=15] RfcommConnectDeprecated@46(BluetoothAddress remote_addr,
int32 channel, int32 optval)
=> (BluetoothStatus status, RfcommConnectingSocketClient&? client);
// Bluetooth socket (RFCOMM and L2CAP LE) functions
// Opens a |sock_type| socket with security options in |sock_flags|, and
// listens on |port| (RFCOMM channel or L2CAP PSM). When |port| = 0, we will
// select a port number automatically. If this process succeeds, the actual
// listening port will be returned in |port|.
[MinVersion=18] BluetoothSocketListen@47(BluetoothSocketType sock_type,
BluetoothSocketFlags sock_flags,
int32 port)
=> (BluetoothStatus status, int32 port,
BluetoothListenSocketClient&? client);
// Opens a |sock_type| socket with security options in |sock_flags|, and
// connects to |remote_addr| which is listening on |remote_port|.
[MinVersion=18] BluetoothSocketConnect@48(BluetoothSocketType sock_type,
BluetoothSocketFlags sock_flags,
BluetoothAddress remote_addr,
int32 remote_port)
=> (BluetoothStatus status, BluetoothConnectSocketClient&? client);
};
// Next Method ID: 24
......
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