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

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

Floss: Implement basic StartScan and StopScan

Basic implementations include multiplexing registered scanners and based
on the is_active state updating the libbluetooth scanning state.

Bug: 233124021
Tag: #floss
Test: Manual - Test with btclient

Change-Id: I63b2e29fdac26b8860b6a13d07b17492db6b7a76
parent a88475ae
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use btstack::bluetooth::{
};
use btstack::bluetooth_gatt::{
    BluetoothGattService, IAdvertisingSetCallback, IBluetoothGattCallback, IScannerCallback, LePhy,
    ScanResult,
};
use btstack::suspend::ISuspendCallback;
use btstack::uuid::UuidWrapper;
@@ -312,6 +313,10 @@ impl IScannerCallback for ScannerCallback {
            scanner_id
        );
    }

    fn on_scan_result(&self, scan_result: ScanResult) {
        print_info!("Scan result: {:#?}", scan_result);
    }
}

impl RPCProxy for ScannerCallback {
+41 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ use crate::ClientContext;
use crate::{console_red, console_yellow, print_error, print_info};
use bt_topshim::btif::{BtConnectionState, BtTransport};
use btstack::bluetooth::{BluetoothDevice, IBluetooth, IBluetoothQA};
use btstack::bluetooth_gatt::IBluetoothGatt;
use btstack::bluetooth_gatt::{IBluetoothGatt, RSSISettings, ScanSettings, ScanType};
use btstack::uuid::{Profile, UuidHelper, UuidWrapper};
use manager_service::iface_bluetooth_manager::IBluetoothManager;

@@ -149,6 +149,8 @@ fn build_commands() -> HashMap<String, CommandOption> {
            rules: vec![
                String::from("le-scan register-scanner"),
                String::from("le-scan unregister-scanner <scanner-id>"),
                String::from("le-scan start-scan <scanner-id>"),
                String::from("le-scan stop-scan <scanner-id>"),
            ],
            description: String::from("LE scanning utilities."),
            function_pointer: CommandHandler::cmd_le_scan,
@@ -833,6 +835,44 @@ impl CommandHandler {
                    print_error!("Failed parsing scanner id");
                }
            }
            "start-scan" => {
                if args.len() < 2 {
                    println!("usage: le-scan start-scan <scanner-id>");
                    return;
                }

                let scanner_id = String::from(&args[1]).parse::<u8>();

                if let Ok(id) = scanner_id {
                    self.context.lock().unwrap().gatt_dbus.as_mut().unwrap().start_scan(
                        id,
                        // TODO(b/217274432): Construct real settings and filters.
                        ScanSettings {
                            interval: 0,
                            window: 0,
                            rssi_settings: RSSISettings { high_threshold: 0, low_threshold: 0 },
                            scan_type: ScanType::Active,
                        },
                        vec![],
                    );
                } else {
                    print_error!("Failed parsing scanner id");
                }
            }
            "stop-scan" => {
                if args.len() < 2 {
                    println!("usage: le-scan stop-scan <scanner-id>");
                    return;
                }

                let scanner_id = String::from(&args[1]).parse::<u8>();

                if let Ok(id) = scanner_id {
                    self.context.lock().unwrap().gatt_dbus.as_mut().unwrap().stop_scan(id);
                } else {
                    print_error!("Failed parsing scanner id");
                }
            }
            _ => {
                println!("Invalid argument '{}'", args[0]);
            }
+45 −4
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ use btstack::bluetooth_gatt::{
    AdvertiseData, AdvertisingSetParameters, BluetoothGattCharacteristic, BluetoothGattDescriptor,
    BluetoothGattService, GattWriteRequestStatus, GattWriteType, IAdvertisingSetCallback,
    IBluetoothGatt, IBluetoothGattCallback, IScannerCallback, LePhy, PeriodicAdvertisingParameters,
    ScanFilter, ScanSettings,
    RSSISettings, ScanFilter, ScanResult, ScanSettings, ScanType,
};
use btstack::socket_manager::{
    BluetoothServerSocket, BluetoothSocket, CallbackId, IBluetoothSocketManager,
@@ -64,6 +64,7 @@ impl_dbus_arg_enum!(GattWriteRequestStatus);
impl_dbus_arg_enum!(GattWriteType);
impl_dbus_arg_enum!(LePhy);
impl_dbus_arg_enum!(Profile);
impl_dbus_arg_enum!(ScanType);
impl_dbus_arg_enum!(SocketType);
impl_dbus_arg_enum!(SuspendType);
impl_dbus_arg_from_into!(Uuid, Vec<u8>);
@@ -130,6 +131,37 @@ pub struct BluetoothDeviceDBus {
    name: String,
}

#[dbus_propmap(RSSISettings)]
pub struct RSSISettingsDBus {
    low_threshold: i32,
    high_threshold: i32,
}

#[dbus_propmap(ScanSettings)]
struct ScanSettingsDBus {
    interval: i32,
    window: i32,
    scan_type: ScanType,
    rssi_settings: RSSISettings,
}

#[dbus_propmap(ScanFilter)]
struct ScanFilterDBus {}

#[dbus_propmap(ScanResult)]
struct ScanResultDBus {
    address: String,
    addr_type: u8,
    event_type: u16,
    primary_phy: u8,
    secondary_phy: u8,
    advertising_sid: u8,
    tx_power: i8,
    rssi: i8,
    periodic_adv_int: u16,
    adv_data: Vec<u8>,
}

struct IBluetoothCallbackDBus {}

impl RPCProxy for IBluetoothCallbackDBus {}
@@ -200,7 +232,14 @@ impl RPCProxy for IScannerCallbackDBus {}
)]
impl IScannerCallback for IScannerCallbackDBus {
    #[dbus_method("OnScannerRegistered")]
    fn on_scanner_registered(&self, uuid: Uuid128Bit, scanner_id: u8, status: GattStatus) {}
    fn on_scanner_registered(&self, uuid: Uuid128Bit, scanner_id: u8, status: GattStatus) {
        dbus_generated!()
    }

    #[dbus_method("OnScanResult")]
    fn on_scan_result(&self, scan_result: ScanResult) {
        dbus_generated!()
    }
}

// Implements RPC-friendly wrapper methods for calling IBluetooth, generated by
@@ -702,11 +741,13 @@ impl IBluetoothGatt for BluetoothGattDBus {
        dbus_generated!()
    }

    fn start_scan(&self, _scanner_id: i32, _settings: ScanSettings, _filters: Vec<ScanFilter>) {
    #[dbus_method("StartScan")]
    fn start_scan(&mut self, _scanner_id: u8, _settings: ScanSettings, _filters: Vec<ScanFilter>) {
        dbus_generated!()
    }

    fn stop_scan(&self, _scanner_id: i32) {
    #[dbus_method("StopScan")]
    fn stop_scan(&mut self, _scanner_id: u8) {
        dbus_generated!()
    }

+6 −0
Original line number Diff line number Diff line
@@ -843,7 +843,9 @@ pub fn generate_dbus_arg(_item: TokenStream) -> TokenStream {
        use dbus::nonblock::SyncConnection;
        use dbus::strings::BusName;
        use dbus_projection::DisconnectWatcher;
        use dbus_projection::impl_dbus_arg_from_into;

        use std::convert::{TryFrom, TryInto};
        use std::error::Error;
        use std::fmt;
        use std::hash::Hash;
@@ -1057,6 +1059,7 @@ pub fn generate_dbus_arg(_item: TokenStream) -> TokenStream {
        impl DirectDBus for u32 {}
        impl DirectDBus for i64 {}
        impl DirectDBus for u64 {}
        impl DirectDBus for i16 {}
        impl DirectDBus for u16 {}
        impl DirectDBus for u8 {}
        impl DirectDBus for String {}
@@ -1077,6 +1080,9 @@ pub fn generate_dbus_arg(_item: TokenStream) -> TokenStream {
            }
        }

        // Represent i8 as D-Bus's i16, since D-Bus only has unsigned type for BYTE.
        impl_dbus_arg_from_into!(i8, i16);

        impl DBusArg for std::fs::File {
            type DBusType = std::fs::File;

+22 −3
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ use btstack::bluetooth_gatt::{
    AdvertiseData, AdvertisingSetParameters, BluetoothGattCharacteristic, BluetoothGattDescriptor,
    BluetoothGattService, GattWriteRequestStatus, GattWriteType, IAdvertisingSetCallback,
    IBluetoothGatt, IBluetoothGattCallback, IScannerCallback, LePhy, PeriodicAdvertisingParameters,
    RSSISettings, ScanFilter, ScanSettings, ScanType,
    RSSISettings, ScanFilter, ScanResult, ScanSettings, ScanType,
};
use btstack::RPCProxy;

@@ -158,6 +158,11 @@ impl IScannerCallback for ScannerCallbackDBus {
    fn on_scanner_registered(&self, uuid: Uuid128Bit, scanner_id: u8, status: GattStatus) {
        dbus_generated!()
    }

    #[dbus_method("OnScanResult")]
    fn on_scan_result(&self, scan_result: ScanResult) {
        dbus_generated!()
    }
}

#[dbus_propmap(BluetoothGattDescriptor)]
@@ -201,6 +206,20 @@ struct ScanSettingsDBus {
    rssi_settings: RSSISettings,
}

#[dbus_propmap(ScanResult)]
struct ScanResultDBus {
    address: String,
    addr_type: u8,
    event_type: u16,
    primary_phy: u8,
    secondary_phy: u8,
    advertising_sid: u8,
    tx_power: i8,
    rssi: i8,
    periodic_adv_int: u16,
    adv_data: Vec<u8>,
}

impl_dbus_arg_enum!(GattStatus);
impl_dbus_arg_enum!(GattWriteRequestStatus);
impl_dbus_arg_enum!(GattWriteType);
@@ -330,12 +349,12 @@ impl IBluetoothGatt for IBluetoothGattDBus {
    }

    #[dbus_method("StartScan")]
    fn start_scan(&self, scanner_id: i32, settings: ScanSettings, filters: Vec<ScanFilter>) {
    fn start_scan(&mut self, scanner_id: u8, settings: ScanSettings, filters: Vec<ScanFilter>) {
        dbus_generated!()
    }

    #[dbus_method("StopScan")]
    fn stop_scan(&self, scanner_id: i32) {
    fn stop_scan(&mut self, scanner_id: u8) {
        dbus_generated!()
    }

Loading