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

Commit 9a179526 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

floss: Add GetUuids adapter api

Bug: 196886186
Tag: #floss
Test: dbus-send and verify values
Change-Id: Iac1b51378766dc15c72816285617b0e4193f349c
parent e483416d
Loading
Loading
Loading
Loading
+40 −1
Original line number Diff line number Diff line
use std::collections::HashMap;
use std::fmt::{Display, Formatter, Result};
use std::sync::{Arc, Mutex};

use num_traits::cast::FromPrimitive;
@@ -6,6 +7,7 @@ use num_traits::cast::FromPrimitive;
use crate::callbacks::BtGattCallback;
use crate::ClientContext;
use crate::{console_red, console_yellow, print_error, print_info};
use bt_topshim::btif::Uuid128Bit;
use btstack::bluetooth::{BluetoothDevice, BluetoothTransport, IBluetooth};
use btstack::bluetooth_gatt::IBluetoothGatt;
use manager_service::iface_bluetooth_manager::IBluetoothManager;
@@ -34,6 +36,33 @@ pub(crate) struct CommandHandler {
    command_options: HashMap<String, CommandOption>,
}

struct DisplayList<T>(Vec<T>);

impl<T: Display> Display for DisplayList<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let _ = write!(f, "[\n");
        for item in self.0.iter() {
            let _ = write!(f, "  {}\n", item);
        }

        write!(f, "]")
    }
}

struct DisplayUuid128Bit(Uuid128Bit);

// UUID128Bit should have a standard output display format
impl Display for DisplayUuid128Bit {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(f, "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
            self.0[0], self.0[1], self.0[2], self.0[3],
            self.0[4], self.0[5],
            self.0[6], self.0[7],
            self.0[8], self.0[9],
            self.0[10], self.0[11], self.0[12], self.0[13], self.0[14], self.0[15])
    }
}

fn enforce_arg_len<F>(args: &Vec<String>, min_len: usize, msg: &str, mut action: F)
where
    F: FnMut(),
@@ -224,8 +253,18 @@ impl CommandHandler {
                    Some(x) => x.clone(),
                    None => String::from(""),
                };
                print_info!("State: {}", if enabled { "enabled" } else { "disabled" });
                let uuids = self.context.lock().unwrap().adapter_dbus.as_ref().unwrap().get_uuids();
                print_info!("Address: {}", address);
                print_info!("State: {}", if enabled { "enabled" } else { "disabled" });
                print_info!(
                    "Uuids: {}",
                    DisplayList(
                        uuids
                            .iter()
                            .map(|&x| DisplayUuid128Bit(x))
                            .collect::<Vec<DisplayUuid128Bit>>()
                    )
                );
            }
            _ => {
                println!("Invalid argument '{}'", args[0]);
+8 −3
Original line number Diff line number Diff line
//! D-Bus proxy implementations of the APIs.

use bt_topshim::btif::BtSspVariant;
use bt_topshim::btif::{BtSspVariant, Uuid128Bit};
use bt_topshim::profiles::gatt::GattStatus;

use btstack::bluetooth::{BluetoothDevice, BluetoothTransport, IBluetooth, IBluetoothCallback};
use btstack::bluetooth_gatt::{
    BluetoothGattCharacteristic, BluetoothGattDescriptor, BluetoothGattService,
    GattWriteRequestStatus, GattWriteType, IBluetoothGatt, IBluetoothGattCallback,
    IScannerCallback, LePhy, ScanFilter, ScanSettings, Uuid128Bit,
    IScannerCallback, LePhy, ScanFilter, ScanSettings,
};

use dbus::arg::{AppendAll, RefArg};
@@ -196,7 +196,7 @@ impl BluetoothDBus {
    }
}

// TODO: These are boilerplate codes, consider creating a macro to generate.
// TODO(b/200732080): These are boilerplate codes, consider creating a macro to generate.
impl IBluetooth for BluetoothDBus {
    fn register_callback(&mut self, callback: Box<dyn IBluetoothCallback + Send>) {
        let path_string = callback.get_object_id();
@@ -226,6 +226,11 @@ impl IBluetooth for BluetoothDBus {
        self.client_proxy.method("GetAddress", ())
    }

    fn get_uuids(&self) -> Vec<Uuid128Bit> {
        let result: Vec<Vec<u8>> = self.client_proxy.method("GetUuids", ());
        <Vec<Uuid128Bit> as DBusArg>::from_dbus(result, None, None, None).unwrap()
    }

    fn start_discovery(&self) -> bool {
        self.client_proxy.method("StartDiscovery", ())
    }
+6 −1
Original line number Diff line number Diff line
extern crate bt_shim;

use bt_topshim::btif::BtSspVariant;
use bt_topshim::btif::{BtSspVariant, Uuid128Bit};

use btstack::bluetooth::{BluetoothDevice, BluetoothTransport, IBluetooth, IBluetoothCallback};
use btstack::RPCProxy;
@@ -78,6 +78,11 @@ impl IBluetooth for IBluetoothDBus {
        String::from("")
    }

    #[dbus_method("GetUuids")]
    fn get_uuids(&self) -> Vec<Uuid128Bit> {
        vec![]
    }

    #[dbus_method("StartDiscovery")]
    fn start_discovery(&self) -> bool {
        true
+2 −2
Original line number Diff line number Diff line
use bt_topshim::profiles::gatt::GattStatus;
use bt_topshim::{btif::Uuid128Bit, profiles::gatt::GattStatus};

use btstack::bluetooth_gatt::{
    BluetoothGattCharacteristic, BluetoothGattDescriptor, BluetoothGattService,
    GattWriteRequestStatus, GattWriteType, IBluetoothGatt, IBluetoothGattCallback,
    IScannerCallback, LePhy, RSSISettings, ScanFilter, ScanSettings, ScanType, Uuid128Bit,
    IScannerCallback, LePhy, RSSISettings, ScanFilter, ScanSettings, ScanType,
};
use btstack::RPCProxy;

+16 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
use bt_topshim::btif::{
    BaseCallbacks, BaseCallbacksDispatcher, BluetoothInterface, BluetoothProperty, BtBondState,
    BtDiscoveryState, BtPropertyType, BtSspVariant, BtState, BtStatus, BtTransport, RawAddress,
    Uuid128Bit,
};
use bt_topshim::profiles::hid_host::{HHCallbacksDispatcher, HidHost};
use bt_topshim::topstack;
@@ -38,6 +39,9 @@ pub trait IBluetooth {
    /// Returns the Bluetooth address of the local adapter.
    fn get_address(&self) -> String;

    /// Gets supported UUIDs by the local adapter.
    fn get_uuids(&self) -> Vec<Uuid128Bit>;

    /// Starts BREDR Inquiry.
    fn start_discovery(&self) -> bool;

@@ -496,6 +500,18 @@ impl IBluetooth for Bluetooth {
        }
    }

    fn get_uuids(&self) -> Vec<Uuid128Bit> {
        match self.properties.get(&BtPropertyType::Uuids) {
            Some(prop) => match prop {
                BluetoothProperty::Uuids(uuids) => {
                    uuids.iter().map(|&x| x.uu.clone()).collect::<Vec<Uuid128Bit>>()
                }
                _ => vec![],
            },
            _ => vec![],
        }
    }

    fn start_discovery(&self) -> bool {
        self.intf.lock().unwrap().start_discovery() == 0
    }
Loading