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

Commit da692fb4 authored by Sonny Sasaka's avatar Sonny Sasaka
Browse files

Forward SspRequest pairing event to callback

This forwards SspRequest to callbacks and implements the client to
handle displaying of passcode.

Bug: 188718349
Tag: #floss
Test: manual - Build floss on Linux, run btclient and create_bond to an
LE keyboard

Change-Id: I6b0b3ac1ba1e4c1e03405deb360bb8f2b0ead2c1
parent c6bfb58a
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
//! D-Bus proxy implementations of the APIs.

use bt_topshim::btif::BtSspVariant;

use btstack::bluetooth::{BluetoothDevice, BluetoothTransport, IBluetooth, IBluetoothCallback};
use btstack::RPCProxy;

@@ -19,6 +21,7 @@ use std::sync::{Arc, Mutex};
use crate::dbus_arg::{DBusArg, DBusArgError, RefArgToRust};

impl_dbus_arg_enum!(BluetoothTransport);
impl_dbus_arg_enum!(BtSspVariant);

#[dbus_propmap(BluetoothDevice)]
pub struct BluetoothDeviceDBus {
@@ -98,6 +101,16 @@ impl IBluetoothCallback for IBluetoothCallbackDBus {

    #[dbus_method("OnDiscoveringChanged")]
    fn on_discovering_changed(&self, discovering: bool) {}

    #[dbus_method("OnSspRequest")]
    fn on_ssp_request(
        &self,
        remote_device: BluetoothDevice,
        cod: u32,
        variant: BtSspVariant,
        passkey: u32,
    ) {
    }
}

// TODO: These are boilerplate codes, consider creating a macro to generate.
+22 −0
Original line number Diff line number Diff line
use bt_topshim::btif::BtSspVariant;
use bt_topshim::topstack;

use btstack::bluetooth::{BluetoothDevice, IBluetooth, IBluetoothCallback};
@@ -43,6 +44,27 @@ impl IBluetoothCallback for BtCallback {
    fn on_discovering_changed(&self, discovering: bool) {
        print_info!("Discovering: {}", discovering);
    }

    fn on_ssp_request(
        &self,
        remote_device: BluetoothDevice,
        _cod: u32,
        variant: BtSspVariant,
        passkey: u32,
    ) {
        if variant == BtSspVariant::PasskeyNotification {
            print_info!(
                "device {}{} would like to pair, enter passkey on remote device: {:06}",
                remote_device.address.to_string(),
                if remote_device.name.len() > 0 {
                    format!(" ({})", remote_device.name)
                } else {
                    String::from("")
                },
                passkey
            );
        }
    }
}

impl RPCProxy for BtCallback {
+12 −0
Original line number Diff line number Diff line
extern crate bt_shim;

use bt_topshim::btif::BtSspVariant;

use btstack::bluetooth::{BluetoothDevice, BluetoothTransport, IBluetooth, IBluetoothCallback};
use btstack::RPCProxy;

@@ -37,9 +39,19 @@ impl IBluetoothCallback for BluetoothCallbackDBus {
    fn on_device_found(&self, remote_device: BluetoothDevice) {}
    #[dbus_method("OnDiscoveringChanged")]
    fn on_discovering_changed(&self, discovering: bool) {}
    #[dbus_method("OnSspRequest")]
    fn on_ssp_request(
        &self,
        remote_device: BluetoothDevice,
        cod: u32,
        variant: BtSspVariant,
        passkey: u32,
    ) {
    }
}

impl_dbus_arg_enum!(BluetoothTransport);
impl_dbus_arg_enum!(BtSspVariant);

#[allow(dead_code)]
struct IBluetoothDBus {}
+26 −3
Original line number Diff line number Diff line
@@ -96,6 +96,15 @@ pub trait IBluetoothCallback: RPCProxy {

    /// When the discovery state is changed.
    fn on_discovering_changed(&self, discovering: bool);

    /// When there is a pairing/bonding process and requires agent to display the event to UI.
    fn on_ssp_request(
        &self,
        remote_device: BluetoothDevice,
        cod: u32,
        variant: BtSspVariant,
        passkey: u32,
    );
}

/// Implementation of the adapter API.
@@ -242,14 +251,28 @@ impl BtifBluetoothCallbacks for Bluetooth {
    fn ssp_request(
        &mut self,
        remote_addr: RawAddress,
        _remote_name: String,
        _cod: u32,
        remote_name: String,
        cod: u32,
        variant: BtSspVariant,
        passkey: u32,
    ) {
        // Currently this supports many agent because we accept many callbacks.
        // TODO: We need a way to select the default agent.
        self.for_all_callbacks(|callback| {
            callback.on_ssp_request(
                BluetoothDevice {
                    address: BDAddr::from_byte_vec(&remote_addr.address.to_vec())
                        .unwrap()
                        .to_string(),
                    name: remote_name.clone(),
                },
                cod,
                variant.clone(),
                passkey,
            );
        });
        // Immediately accept the pairing.
        // TODO: Delegate the pairing confirmation to agent.
        // TODO: Implement other pairing confirmations (passkey, passcode, etc);
        self.intf.lock().unwrap().ssp_reply(&remote_addr, variant, 1, passkey);
    }

+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ impl From<BtTransport> for i32 {
    }
}

#[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, PartialOrd)]
#[derive(Clone, Debug, FromPrimitive, ToPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BtSspVariant {
    PasskeyConfirmation = 0,