Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7ea13f1e authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi
Browse files

floss: Update socket manager implementation

Refactor socket manager interface to expose higher level apis. The major
changes in this CL:

* Expose a BluetoothServerSocket and BluetoothSocket via a set of apis
  similar to what is exposed via BluetoothAdapter and BluetoothDevice on
  Android. These are all exposed via a single SocketManager trait.

* All socket apis require a callback id and sockets are always
  associated with that callback. When the callback disconnects, any
  sockets associated with it are removed.

* RPC apis will run in the RPC runtime but socket accept/polling will
  occur on a runtime specific to the socket manager.

Bug: 233123287
Tag: #floss
Test: ./build.py and ran binary on device (feature not tested)
Change-Id: Id0a827565b87d6af097bf34eb6be3ec60f297ea3
parent f5dcab4b
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -619,7 +619,6 @@ pub fn dbus_propmap(attr: TokenStream, item: TokenStream) -> TokenStream {

                return Ok(#struct_ident {
                    #field_idents
                    ..Default::default()
                });
            }

+149 −20
Original line number Diff line number Diff line
extern crate bt_shim;

use bt_topshim::btif::{BtDeviceType, BtPropertyType, BtSspVariant, BtTransport, Uuid, Uuid128Bit};
use bt_topshim::btif::{
    BtDeviceType, BtPropertyType, BtSspVariant, BtStatus, BtTransport, Uuid, Uuid128Bit,
};
use bt_topshim::profiles::socket::SocketType;

use btstack::bluetooth::{
    Bluetooth, BluetoothDevice, IBluetooth, IBluetoothCallback, IBluetoothConnectionCallback,
};
use btstack::socket_manager::{BluetoothSocketManager, IBluetoothSocketManager};
use btstack::socket_manager::{
    BluetoothServerSocket, BluetoothSocket, BluetoothSocketManager, CallbackId,
    IBluetoothSocketManager, IBluetoothSocketManagerCallbacks, SocketId, SocketResult,
};
use btstack::suspend::{ISuspend, ISuspendCallback, Suspend, SuspendType};
use btstack::uuid::Profile;
use btstack::RPCProxy;
@@ -40,6 +45,8 @@ impl RefArgToRust for Uuid {
    }
}

impl_dbus_arg_from_into!(BtStatus, u32);

/// A mixin of the several interfaces. The naming of the fields in the mixin must match
/// what is listed in the `generate_dbus_exporter` invocation.
pub struct BluetoothMixin {
@@ -342,7 +349,70 @@ impl IBluetooth for IBluetoothDBus {

impl_dbus_arg_enum!(SocketType);

#[allow(dead_code)]
#[dbus_propmap(BluetoothServerSocket)]
pub struct BluetoothServerSocketDBus {
    id: SocketId,
    sock_type: SocketType,
    flags: i32,
    psm: Option<i32>,
    channel: Option<i32>,
    name: Option<String>,
    uuid: Option<Uuid>,
}

#[dbus_propmap(BluetoothSocket)]
pub struct BluetoothSocketDBus {
    id: SocketId,
    remote_device: BluetoothDevice,
    sock_type: SocketType,
    flags: i32,
    fd: Option<std::fs::File>,
    port: i32,
    uuid: Option<Uuid>,
    max_rx_size: i32,
    max_tx_size: i32,
}

#[dbus_propmap(SocketResult)]
pub struct SocketResultDBus {
    status: BtStatus,
    id: u64,
}

struct IBluetoothSocketManagerCallbacksDBus {}

#[dbus_proxy_obj(BluetoothSocketCallback, "org.chromium.bluetooth.SocketManagerCallback")]
impl IBluetoothSocketManagerCallbacks for IBluetoothSocketManagerCallbacksDBus {
    #[dbus_method("OnIncomingSocketReady")]
    fn on_incoming_socket_ready(&mut self, socket: BluetoothServerSocket, status: BtStatus) {
        dbus_generated!()
    }

    #[dbus_method("OnIncomingSocketClosed")]
    fn on_incoming_socket_closed(&mut self, listener_id: SocketId, reason: BtStatus) {
        dbus_generated!()
    }

    #[dbus_method("OnHandleIncomingConnection")]
    fn on_handle_incoming_connection(
        &mut self,
        listener_id: SocketId,
        connection: BluetoothSocket,
    ) {
        dbus_generated!()
    }

    #[dbus_method("OnOutgoingConnectionResult")]
    fn on_outgoing_connection_result(
        &mut self,
        connecting_id: SocketId,
        result: BtStatus,
        socket: Option<BluetoothSocket>,
    ) {
        dbus_generated!()
    }
}

struct IBluetoothSocketManagerDBus {}

#[generate_dbus_exporter(
@@ -352,32 +422,91 @@ struct IBluetoothSocketManagerDBus {}
    socket_mgr
)]
impl IBluetoothSocketManager for IBluetoothSocketManagerDBus {
    #[dbus_method("ConnectSocket")]
    fn connect_socket(
    #[dbus_method("RegisterCallback")]
    fn register_callback(
        &mut self,
        callback: Box<dyn IBluetoothSocketManagerCallbacks + Send>,
    ) -> CallbackId {
        dbus_generated!()
    }

    #[dbus_method("ListenUsingInsecureL2capChannel")]
    fn listen_using_insecure_l2cap_channel(&mut self, callback: CallbackId) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("ListenUsingL2capChannel")]
    fn listen_using_l2cap_channel(&mut self, callback: CallbackId) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("ListenUsingInsecureRfcommWithServiceRecord")]
    fn listen_using_insecure_rfcomm_with_service_record(
        &mut self,
        callback: CallbackId,
        name: String,
        uuid: Uuid,
    ) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("ListenUsingRfcommWithServiceRecord")]
    fn listen_using_rfcomm_with_service_record(
        &mut self,
        callback: CallbackId,
        name: String,
        uuid: Uuid,
    ) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("CreateInsecureL2capChannel")]
    fn create_insecure_l2cap_channel(
        &mut self,
        callback: CallbackId,
        device: BluetoothDevice,
        sock_type: SocketType,
        uuid: Option<Uuid>,
        port: i32,
        flags: i32,
    ) -> Option<std::fs::File> {
        psm: i32,
    ) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("CreateSocketChannel")]
    fn create_socket_channel(
    #[dbus_method("CreateL2capChannel")]
    fn create_l2cap_channel(
        &mut self,
        sock_type: SocketType,
        service_name: String,
        uuid: Option<Uuid>,
        port: i32,
        flags: i32,
    ) -> Option<std::fs::File> {
        callback: CallbackId,
        device: BluetoothDevice,
        psm: i32,
    ) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("CreateInsecureRfcommSocketToServiceRecord")]
    fn create_insecure_rfcomm_socket_to_service_record(
        &mut self,
        callback: CallbackId,
        device: BluetoothDevice,
        uuid: Uuid,
    ) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("CreateRfcommSocketToServiceRecord")]
    fn create_rfcomm_socket_to_service_record(
        &mut self,
        callback: CallbackId,
        device: BluetoothDevice,
        uuid: Uuid,
    ) -> SocketResult {
        dbus_generated!()
    }

    #[dbus_method("Accept")]
    fn accept(&mut self, callback: CallbackId, id: SocketId, timeout_ms: Option<u32>) -> BtStatus {
        dbus_generated!()
    }

    #[dbus_method("RequestMaximumTxDataLength")]
    fn request_maximum_tx_data_length(&mut self, device: BluetoothDevice) {
    #[dbus_method("Close")]
    fn close(&mut self, callback: CallbackId, id: SocketId) -> BtStatus {
        dbus_generated!()
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -108,7 +108,8 @@ fn main() -> Result<(), Box<dyn Error>> {
        intf.clone(),
        bluetooth_media.clone(),
    ))));
    let bt_sock_mgr = Arc::new(Mutex::new(Box::new(BluetoothSocketManager::new(intf.clone()))));
    let bt_sock_mgr =
        Arc::new(Mutex::new(Box::new(BluetoothSocketManager::new(intf.clone(), tx.clone()))));

    topstack::get_runtime().block_on(async {
        // Connect to D-Bus system bus.
@@ -146,6 +147,7 @@ fn main() -> Result<(), Box<dyn Error>> {
            bluetooth_gatt.clone(),
            bluetooth_media.clone(),
            suspend.clone(),
            bt_sock_mgr.clone(),
        ));

        // Set up the disconnect watcher to monitor client disconnects.
+3 −3
Original line number Diff line number Diff line
@@ -11,10 +11,10 @@ btif_macros = { path = "btif_macros" }

dbus = "0.9.2"
log = "0.4.14"
nix = "0.19"
num-derive = "0.3"
num-traits = "0.2"
rand = { version = "0.8.3", features = ["small_rng"] }
num-traits = "*"
num-derive = "*"

tokio = { version = "1", features = ['bytes', 'fs', 'io-util', 'libc', 'macros', 'memchr', 'mio', 'net', 'num_cpus', 'rt', 'rt-multi-thread', 'sync', 'time', 'tokio-macros'] }

[lib]
+12 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ use tokio::sync::mpsc::{Receiver, Sender};
use crate::bluetooth::Bluetooth;
use crate::bluetooth_gatt::BluetoothGatt;
use crate::bluetooth_media::{BluetoothMedia, MediaActions};
use crate::socket_manager::{BluetoothSocketManager, SocketActions};
use crate::suspend::Suspend;
use bt_topshim::{
    btif::BaseCallbacks,
@@ -62,6 +63,9 @@ pub enum Message {

    // Scanner related
    ScannerCallbackDisconnected(u32),

    SocketManagerActions(SocketActions),
    SocketManagerCallbackDisconnected(u32),
}

/// Umbrella class for the Bluetooth stack.
@@ -80,6 +84,7 @@ impl Stack {
        bluetooth_gatt: Arc<Mutex<Box<BluetoothGatt>>>,
        bluetooth_media: Arc<Mutex<Box<BluetoothMedia>>>,
        suspend: Arc<Mutex<Box<Suspend>>>,
        bluetooth_socketmgr: Arc<Mutex<Box<BluetoothSocketManager>>>,
    ) {
        loop {
            let m = rx.recv().await;
@@ -159,6 +164,13 @@ impl Stack {
                Message::ScannerCallbackDisconnected(id) => {
                    bluetooth_gatt.lock().unwrap().remove_scanner_callback(id);
                }

                Message::SocketManagerActions(action) => {
                    bluetooth_socketmgr.lock().unwrap().handle_actions(action);
                }
                Message::SocketManagerCallbackDisconnected(id) => {
                    bluetooth_socketmgr.lock().unwrap().remove_callback(id);
                }
            }
        }
    }
Loading