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

Commit 02e8b484 authored by Sarvesh Kalwit's avatar Sarvesh Kalwit
Browse files

floss: Support GATT Server connection/disconnection in btclient

Support the ability to connect to and disconnect from a registered GATT
Server in btclient.

Bug: 323943663
Test: m -j && manually with btclient
Flag: Exempt, Floss-only change

Change-Id: I6a5de7a3034db7e5d0540d2bd9d288466a62f1b6
parent bbaba687
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -50,3 +50,17 @@ impl GattClientContext {
        self.auth_req
    }
}

/// User preference of GATT server operations
pub(crate) struct GattServerContext {
    /// Is connection going to be directed?
    pub(crate) is_connect_direct: bool,
    /// Transport of connection
    pub(crate) connect_transport: BtTransport,
}

impl GattServerContext {
    pub(crate) fn new() -> Self {
        GattServerContext { is_connect_direct: false, connect_transport: BtTransport::Le }
    }
}
+55 −0
Original line number Diff line number Diff line
@@ -202,6 +202,10 @@ fn build_commands() -> HashMap<String, CommandOption> {
                ),
                String::from("gatt register-notification <address> <handle> <enable|disable>"),
                String::from("gatt register-server"),
                String::from("gatt server-connect <server_id> <client_address>"),
                String::from("gatt server-disconnect <server_id> <client_address>"),
                String::from("gatt server-set-direct-connect <true|false>"),
                String::from("gatt server-set-connect-transport <Bredr|LE|Auto>"),
            ],
            description: String::from("GATT tools"),
            function_pointer: CommandHandler::cmd_gatt,
@@ -1223,6 +1227,57 @@ impl CommandHandler {
                    false,
                );
            }
            "server-connect" => {
                let server_id = String::from(get_arg(args, 1)?)
                    .parse::<i32>()
                    .or(Err("Failed to parse server_id"))?;
                let client_addr = String::from(get_arg(args, 2)?);
                let is_direct = self.lock_context().gatt_server_context.is_connect_direct;
                let transport = self.lock_context().gatt_server_context.connect_transport;

                if !self.lock_context().gatt_dbus.as_mut().unwrap().server_connect(
                    server_id,
                    client_addr.clone(),
                    is_direct,
                    transport,
                ) {
                    return Err("Connection was unsuccessful".into());
                }
            }
            "server-disconnect" => {
                let server_id = String::from(get_arg(args, 1)?)
                    .parse::<i32>()
                    .or(Err("Failed to parse server_id"))?;
                let client_addr = String::from(get_arg(args, 2)?);

                if !self
                    .lock_context()
                    .gatt_dbus
                    .as_mut()
                    .unwrap()
                    .server_disconnect(server_id, client_addr.clone())
                {
                    return Err("Disconnection was unsuccessful".into());
                }
            }
            "server-set-direct-connect" => {
                let is_direct = String::from(get_arg(args, 1)?)
                    .parse::<bool>()
                    .or(Err("Failed to parse is_direct"))?;

                self.lock_context().gatt_server_context.is_connect_direct = is_direct;
            }
            "server-set-connect-transport" => {
                let transport = match &get_arg(args, 1)?[..] {
                    "Bredr" => BtTransport::Bredr,
                    "LE" => BtTransport::Le,
                    "Auto" => BtTransport::Auto,
                    _ => {
                        return Err("Failed to parse transport".into());
                    }
                };
                self.lock_context().gatt_server_context.connect_transport = transport;
            }
            _ => return Err(CommandError::InvalidArgs),
        }
        Ok(())
+5 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ use tokio::sync::mpsc;
use tokio::time::{sleep, timeout};

use crate::bt_adv::AdvSet;
use crate::bt_gatt::GattClientContext;
use crate::bt_gatt::{GattClientContext, GattServerContext};
use crate::callbacks::{
    AdminCallback, AdvertisingSetCallback, BtCallback, BtConnectionCallback, BtManagerCallback,
    BtSocketManagerCallback, MediaCallback, QACallback, ScannerCallback, SuspendCallback,
@@ -138,6 +138,9 @@ pub(crate) struct ClientContext {
    /// Data of GATT client preference.
    gatt_client_context: GattClientContext,

    /// Data of GATT server preference.
    gatt_server_context: GattServerContext,

    /// The schedule when a socket is connected.
    socket_test_schedule: Option<SocketSchedule>,

@@ -192,6 +195,7 @@ impl ClientContext {
            qa_callback_id: None,
            is_restricted,
            gatt_client_context: GattClientContext::new(),
            gatt_server_context: GattServerContext::new(),
            socket_test_schedule: None,
            mps_sdp_handle: None,
            client_commands_with_callbacks,