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

Commit ededd793 authored by Zhengping Jiang's avatar Zhengping Jiang
Browse files

Floss: Add bluetooth.Experimental dbus to enable ll privacy

Add dbus call to modify ll privacy in floss config file. If the adapter
is enabled, restart the adapter to apply the change.
Add /org/chromium/bluetooth/Experimental inteface in btmanagerd and add
mixin for Manager and Experimental interface.

BUG: 242100561
Tag: #floss
Test: USE=floss_upstream emerge-volteer floss, change chrome://flags and
dbus-send from command line then check the configuration file.

Change-Id: I4673b5d82963e47ba498a0ca931dfd9b73df38f1
parent 94c31350
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ use dbus_tokio::connection;
use log::LevelFilter;
use manager_service::bluetooth_manager::BluetoothManager;
use manager_service::powerd_suspend_manager::PowerdSuspendManager;
use manager_service::{bluetooth_experimental_dbus, iface_bluetooth_manager};
use manager_service::{bluetooth_manager_dbus, config_util, state_machine};
use std::sync::{Arc, Mutex};
use syslog::{BasicLogger, Facility, Formatter3164};
@@ -121,12 +122,23 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
        &mut cr.lock().unwrap(),
        disconnect_watcher.clone(),
    );
    cr.lock().unwrap().insert(
        "/org/chromium/bluetooth/Manager",
        &[iface],
        bluetooth_manager.clone(),

    // Let's add the "/org/chromium/bluetooth/Experimental" path, which implements
    // the org.chromium.bluetooth.Experimental interface, to the crossroads instance
    let iface_exp = bluetooth_experimental_dbus::export_bluetooth_experimental_dbus_intf(
        conn.clone(),
        &mut cr.lock().unwrap(),
        disconnect_watcher.clone(),
    );

    // Create mixin object for Manager + Experimental interfaces.
    let mixin = Box::new(iface_bluetooth_manager::BluetoothManagerMixin {
        manager: bluetooth_manager.clone(),
        experimental: bluetooth_manager.clone(),
    });

    cr.lock().unwrap().insert("/org/chromium/bluetooth/Manager", &[iface, iface_exp], mixin);

    // We add the Crossroads instance to the connection so that incoming method calls will be handled.
    let cr_clone = cr.clone();
    conn.start_receive(
+23 −0
Original line number Diff line number Diff line
use dbus_macros::{dbus_method, generate_dbus_exporter};
use dbus_projection::dbus_generated;

use crate::dbus_arg::DBusArg;
use crate::iface_bluetooth_experimental::IBluetoothExperimental;

use crate::iface_bluetooth_manager::BluetoothManagerMixin;

/// D-Bus projection of IBluetoothExperimental.
struct BluetoothExperimentalDBus {}

#[generate_dbus_exporter(
    export_bluetooth_experimental_dbus_intf,
    "org.chromium.bluetooth.Experimental",
    BluetoothManagerMixin,
    experimental
)]
impl IBluetoothExperimental for BluetoothExperimentalDBus {
    #[dbus_method("SetLLPrivacy")]
    fn set_ll_privacy(&mut self, enabled: bool) {
        dbus_generated!()
    }
}
+20 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ use log::{error, info, warn};
use std::collections::HashMap;
use std::process::Command;

use crate::iface_bluetooth_experimental::IBluetoothExperimental;
use crate::iface_bluetooth_manager::{
    AdapterWithEnabled, IBluetoothManager, IBluetoothManagerCallback,
};
@@ -163,3 +164,22 @@ impl IBluetoothManager for BluetoothManager {
        self.proxy.set_desired_default_adapter(adapter_index);
    }
}

/// Implementation of IBluetoothExperimental
impl IBluetoothExperimental for BluetoothManager {
    fn set_ll_privacy(&mut self, enabled: bool) {
        let current_status = match config_util::read_floss_ll_privacy_enabled() {
            Ok(true) => true,
            _ => false,
        };

        if current_status == enabled {
            return;
        }

        if let Err(e) = config_util::write_floss_ll_privacy_enabled(enabled) {
            error!("Failed to write ll privacy status: {}", e);
            return;
        }
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ use btstack::RPCProxy;

use crate::dbus_arg::{DBusArg, DBusArgError, RefArgToRust};
use crate::iface_bluetooth_manager::{
    AdapterWithEnabled, IBluetoothManager, IBluetoothManagerCallback,
    AdapterWithEnabled, BluetoothManagerMixin, IBluetoothManager, IBluetoothManagerCallback,
};

#[dbus_propmap(AdapterWithEnabled)]
@@ -20,7 +20,12 @@ pub struct AdapterWithEnabledDbus {
/// D-Bus projection of IBluetoothManager.
struct BluetoothManagerDBus {}

#[generate_dbus_exporter(export_bluetooth_manager_dbus_intf, "org.chromium.bluetooth.Manager")]
#[generate_dbus_exporter(
    export_bluetooth_manager_dbus_intf,
    "org.chromium.bluetooth.Manager",
    BluetoothManagerMixin,
    manager
)]
impl IBluetoothManager for BluetoothManagerDBus {
    #[dbus_method("Start")]
    fn start(&mut self, hci_interface: i32) {
+30 −0
Original line number Diff line number Diff line
@@ -12,6 +12,9 @@ const BLUETOOTH_DAEMON_CURRENT: &str = "/var/lib/bluetooth/bluetooth-daemon.curr
// File to store the config for BluetoothManager
const BTMANAGERD_CONF: &str = "/var/lib/bluetooth/btmanagerd.json";

/// Folder to keep files which override floss configuration
const FLOSS_SYSPROPS_OVERRIDE_DIR: &str = "/var/lib/bluetooth/sysprops.conf.d";

/// Key used for default adapter entry.
const DEFAULT_ADAPTER_KEY: &str = "default_adapter";

@@ -178,6 +181,33 @@ pub fn reset_hci_device(hci: i32) -> bool {
    std::fs::write(path, "1").is_ok()
}

pub fn read_floss_ll_privacy_enabled() -> std::io::Result<bool> {
    let parent = Path::new(FLOSS_SYSPROPS_OVERRIDE_DIR);
    if !parent.is_dir() {
        return Ok(false);
    }

    let data = std::fs::read_to_string(format!(
        "{}/{}",
        FLOSS_SYSPROPS_OVERRIDE_DIR, "privacy_override.conf"
    ))?;

    Ok(data == "[Sysprops]\nbluetooth.core.gap.le.privacy.enabled=true\n")
}

pub fn write_floss_ll_privacy_enabled(enabled: bool) -> std::io::Result<()> {
    let parent = Path::new(FLOSS_SYSPROPS_OVERRIDE_DIR);

    std::fs::create_dir_all(parent)?;

    let data = format!(
        "[Sysprops]\nbluetooth.core.gap.le.privacy.enabled={}",
        if enabled { "true\n" } else { "false\n" }
    );

    std::fs::write(format!("{}/{}", FLOSS_SYSPROPS_OVERRIDE_DIR, "privacy_override.conf"), data)
}

#[cfg(test)]
mod tests {
    use super::*;
Loading