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

Commit 3264378f authored by Sonny Sasaka's avatar Sonny Sasaka
Browse files

Floss: Refactor btmanagerd bin and lib

In Rust, if a crate is both a bin and lib, only lib will get custom
linker flags applied. In our case, we need to apply -lc++ to link with
libc++ and not libstdc++, and the current code structure results in
btmanagerd linking to both c++ libraries. To fix this, we move as much
code as possible to the lib so that all will be linked with libc++, and
main.rs should only contain glue code that uses all the components from
the lib.

Bug: 224606285
Tag: #floss
Test: Build Floss on Chrome OS

Change-Id: Id40ca1a7c42159d4df72c0734060750bbce18e03
parent 644867c6
Loading
Loading
Loading
Loading
+10 −21
Original line number Original line Diff line number Diff line
mod bluetooth_manager;
// The manager binary (btmanagerd) is a fairly barebone bin file that depends on the manager_service
mod bluetooth_manager_dbus;
// library which implements most of the logic. The code is separated in this way so that we can
mod config_util;
// apply certain linker flags (which is applied to the library but not the binary).
mod dbus_arg;
// Please keep main.rs logic light and write the heavy logic in the manager_service library instead.
mod dbus_iface;

mod powerd_suspend_manager;
mod service_watcher;
mod state_machine;

use crate::bluetooth_manager::BluetoothManager;
use crate::powerd_suspend_manager::PowerdSuspendManager;
use dbus::channel::MatchingReceiver;
use dbus::channel::MatchingReceiver;
use dbus::message::MatchRule;
use dbus::message::MatchRule;
use dbus_crossroads::Crossroads;
use dbus_crossroads::Crossroads;
use dbus_projection::DisconnectWatcher;
use dbus_projection::DisconnectWatcher;
use dbus_tokio::connection;
use dbus_tokio::connection;
use log::LevelFilter;
use log::LevelFilter;
use manager_service::bluetooth_manager::{BluetoothManager, ManagerContext};
use manager_service::powerd_suspend_manager::PowerdSuspendManager;
use manager_service::{bluetooth_manager_dbus, config_util, state_machine};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex};
use syslog::{BasicLogger, Facility, Formatter3164};
use syslog::{BasicLogger, Facility, Formatter3164};


#[derive(Clone)]
struct ManagerContext {
    proxy: state_machine::StateMachineProxy,
    floss_enabled: Arc<AtomicBool>,
}

#[tokio::main]
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let formatter = Formatter3164 {
    let formatter = Formatter3164 {
@@ -62,10 +53,8 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {


    let context = state_machine::start_new_state_machine_context(invoker);
    let context = state_machine::start_new_state_machine_context(invoker);
    let proxy = context.get_proxy();
    let proxy = context.get_proxy();
    let manager_context = ManagerContext {
    let manager_context =
        proxy: proxy,
        ManagerContext::new(proxy, Arc::new(AtomicBool::new(config_util::is_floss_enabled())));
        floss_enabled: Arc::new(AtomicBool::new(config_util::is_floss_enabled())),
    };


    // The resource is a task that should be spawned onto a tokio compatible
    // The resource is a task that should be spawned onto a tokio compatible
    // reactor ASAP. If the resource ever finishes, you lost connection to D-Bus.
    // reactor ASAP. If the resource ever finishes, you lost connection to D-Bus.
+19 −7
Original line number Original line Diff line number Diff line
use log::{error, info, warn};
use log::{error, info, warn};


use manager_service::iface_bluetooth_manager::{
    AdapterWithEnabled, IBluetoothManager, IBluetoothManagerCallback,
};

use std::collections::HashMap;
use std::collections::HashMap;
use std::process::Command;
use std::process::Command;
use std::sync::atomic::Ordering;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;


use crate::{config_util, state_machine, ManagerContext};
use crate::iface_bluetooth_manager::{
    AdapterWithEnabled, IBluetoothManager, IBluetoothManagerCallback,
};
use crate::{config_util, state_machine};


const BLUEZ_INIT_TARGET: &str = "bluetoothd";
const BLUEZ_INIT_TARGET: &str = "bluetoothd";


#[derive(Clone)]
pub struct ManagerContext {
    proxy: state_machine::StateMachineProxy,
    floss_enabled: Arc<AtomicBool>,
}

impl ManagerContext {
    pub fn new(proxy: state_machine::StateMachineProxy, floss_enabled: Arc<AtomicBool>) -> Self {
        Self { proxy, floss_enabled }
    }
}

/// Implementation of IBluetoothManager.
/// Implementation of IBluetoothManager.
pub struct BluetoothManager {
pub struct BluetoothManager {
    manager_context: ManagerContext,
    manager_context: ManagerContext,
@@ -20,7 +32,7 @@ pub struct BluetoothManager {
}
}


impl BluetoothManager {
impl BluetoothManager {
    pub(crate) fn new(manager_context: ManagerContext) -> BluetoothManager {
    pub fn new(manager_context: ManagerContext) -> BluetoothManager {
        BluetoothManager {
        BluetoothManager {
            manager_context,
            manager_context,
            callbacks: HashMap::new(),
            callbacks: HashMap::new(),
+3 −3
Original line number Original line Diff line number Diff line
@@ -4,11 +4,11 @@ use dbus_macros::{dbus_method, dbus_propmap, dbus_proxy_obj, generate_dbus_expor
use dbus_projection::{dbus_generated, DisconnectWatcher};
use dbus_projection::{dbus_generated, DisconnectWatcher};


use btstack::RPCProxy;
use btstack::RPCProxy;
use manager_service::iface_bluetooth_manager::{
    AdapterWithEnabled, IBluetoothManager, IBluetoothManagerCallback,
};


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


#[dbus_propmap(AdapterWithEnabled)]
#[dbus_propmap(AdapterWithEnabled)]
pub struct AdapterWithEnabledDbus {
pub struct AdapterWithEnabledDbus {
Loading