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

Commit 2ecb9971 authored by Yun-Hao Chung's avatar Yun-Hao Chung
Browse files

Floss: Remove delay to register callback in non-interactive mode

Only introduce delay on interactive mode. This is because we expect the
user to ensure the adapter interface is ready when they issue the
command in non-interactive mode. Otherwise, there will always have 1
second delay and in most of the case it is not needed.

Bug: 368387445
Tag: #floss
Test: mmm packages/modules/Bluetooth
Test: btclient --command "media info" and observe no delay
Test: btclient adapter enable in interactive mode and observe a delay
Flag: EXEMPT, Floss-only changes

Change-Id: Ica0d9146c1947c3cd9236b5d2541d8fc8c9bf3d8
parent 535a6b8f
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -146,6 +146,9 @@ pub(crate) struct ClientContext {
    /// Is btclient running in restricted mode?
    is_restricted: bool,

    /// Is btclient running in interactive mode?
    is_interactive: bool,

    /// Data of GATT client preference.
    gatt_client_context: GattClientContext,

@@ -174,6 +177,7 @@ impl ClientContext {
        dbus_crossroads: Arc<Mutex<Crossroads>>,
        tx: mpsc::Sender<ForegroundActions>,
        is_restricted: bool,
        is_interactive: bool,
        client_commands_with_callbacks: Vec<String>,
    ) -> ClientContext {
        // Manager interface is almost always available but adapter interface
@@ -212,6 +216,7 @@ impl ClientContext {
            socket_manager_callback_id: None,
            qa_callback_id: None,
            is_restricted,
            is_interactive,
            gatt_client_context: GattClientContext::new(),
            gatt_server_context: GattServerContext::new(),
            socket_test_schedule: None,
@@ -271,11 +276,19 @@ impl ClientContext {

        // Trigger callback registration in the foreground
        let fg = self.fg.clone();
        let is_interactive = self.is_interactive;
        tokio::spawn(async move {
            let adapter = format!("adapter{}", idx);

            // Floss won't export the interface until it is ready to be used.
            // Wait 1 second before registering the callbacks.
            // Only introduce such delay on interactive mode. This is because we expect the user to
            // ensure the adapter interface is ready when they issue the command in non-interactive
            // mode. Otherwise, there will always have 1 second delay and in most of the case it is
            // not needed.
            if is_interactive {
                sleep(Duration::from_millis(1000)).await;
            }
            let _ = fg.send(ForegroundActions::RegisterAdapterCallback(adapter)).await;
        });
    }
@@ -364,6 +377,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
        .get_matches();
    let command = value_t!(matches, "command", String).ok();
    let is_restricted = matches.is_present("restricted");
    let is_interactive = command.is_none();
    let timeout_secs = value_t!(matches, "timeout", u64);

    topstack::get_runtime().block_on(async move {
@@ -408,6 +422,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
            cr.clone(),
            tx.clone(),
            is_restricted,
            is_interactive,
            client_commands_with_callbacks,
        )));

@@ -449,7 +464,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
        };

        let handler = CommandHandler::new(context.clone());
        if command.is_some() {
        if !is_interactive {
            // Timeout applies only to non-interactive commands.
            if let Ok(timeout_secs) = timeout_secs {
                let timeout_duration = Duration::from_secs(timeout_secs);