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

Commit 3400a9f4 authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Add basic logging and sample upstart btmanagerd.conf

For logging, we print to stdout for now; we will migrate to syslog once
we have the crate.

Start btmanagerd on system start.

Bug: 189501475
Tag: #floss
Test: initctl start btmanagerd
Change-Id: If97c530884ea6c680c187dce7c9edb860e273a0f
parent a996b8e2
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
description     "Start the bluetooth manager daemon"
author          "chromium-os-dev@chromium.org"

start on started system-services
stop on stopping system-services

exec btmanagerd
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ dbus = "0.9.2"
dbus-tokio = "0.7.3"
dbus-crossroads = "0.3.0"
inotify = "*"
log = "0.4.14"
nix = "*"
regex = "1.5"
serde_json = "1.0"
+24 −2
Original line number Diff line number Diff line
@@ -7,11 +7,31 @@ use dbus::message::MatchRule;
use dbus::nonblock::SyncConnection;
use dbus_crossroads::Crossroads;
use dbus_tokio::connection;
use log::{error, info, warn};
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
use std::process::Command;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::Mutex;

struct SimpleLogger;

impl log::Log for SimpleLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        true || metadata.level() <= Level::Info
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            println!("{} - {}", record.level(), record.args());
        }
    }

    fn flush(&self) {}
}

static LOGGER: SimpleLogger = SimpleLogger;

const BLUEZ_INIT_TARGET: &str = "bluetoothd";

#[derive(Clone)]
@@ -25,6 +45,8 @@ struct ManagerContext {

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Debug)).unwrap();

    // Initialize config util
    config_util::fix_config_file_format();

@@ -79,7 +101,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
            (),
            |mut ctx, cr, (hci_interface,): (i32,)| {
                if !config_util::modify_hci_n_enabled(hci_interface, true) {
                    println!("Config is not successfully modified");
                    error!("Config is not successfully modified");
                }
                let proxy = cr.data_mut::<ManagerContext>(ctx.path()).unwrap().proxy.clone();
                async move {
@@ -100,7 +122,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
            |mut ctx, cr, (hci_interface,): (i32,)| {
                let proxy = cr.data_mut::<ManagerContext>(ctx.path()).unwrap().proxy.clone();
                if !config_util::modify_hci_n_enabled(hci_interface, false) {
                    println!("Config is not successfully modified");
                    error!("Config is not successfully modified");
                }
                async move {
                    let result = proxy.stop_bluetooth(hci_interface).await;
+14 −13
Original line number Diff line number Diff line
use bt_common::time::Alarm;
use log::{debug, error, info, warn};
use nix::sys::signal::{self, Signal};
use nix::unistd::Pid;
use regex::Regex;
@@ -165,7 +166,7 @@ pub async fn mainloop<PM>(
                      true => {
                          command_timeout.cancel();
                      }
                      false => println!("unexpected BluetoothStarted pid{} hci{}", pid, hci),
                      false => error!("unexpected BluetoothStarted pid{} hci{}", pid, hci),
                  }
                },
                StateMachineActions::BluetoothStopped() => {
@@ -181,7 +182,7 @@ pub async fn mainloop<PM>(
              }
            },
            expired = command_timeout.expired() => {
                println!("expired {:?}", *context.state_machine.state.lock().await);
                info!("expired {:?}", *context.state_machine.state.lock().await);
                let timeout_action = context.state_machine.action_on_command_timeout().await;
                match timeout_action {
                    StateMachineTimeoutActions::Noop => (),
@@ -194,7 +195,7 @@ pub async fn mainloop<PM>(
                match fd_ready.try_io(|inner| inner.get_mut().read_events(&mut buffer)) {
                    Ok(Ok(events)) => {
                        for event in events {
                            println!("got some events from pid {:?}", event.mask);
                            debug!("got some events from pid {:?}", event.mask);
                            match (event.mask, event.name) {
                                (inotify::EventMask::CREATE, Some(oss)) => {
                                    let path = std::path::Path::new(PID_DIR).join(oss);
@@ -204,7 +205,7 @@ pub async fn mainloop<PM>(
                                            let pid = String::from_utf8(s).expect("invalid pid file").parse::<i32>().unwrap_or(0);
                                            let _ = context.tx.send_timeout(StateMachineActions::BluetoothStarted(pid, hci), TX_SEND_TIMEOUT_DURATION).await.unwrap();
                                        },
                                        (hci, s) => println!("invalid file hci={:?} pid_file={:?}", hci, s),
                                        (hci, s) => warn!("invalid file hci={:?} pid_file={:?}", hci, s),
                                    }
                                },
                                (inotify::EventMask::DELETE, Some(oss)) => {
@@ -213,7 +214,7 @@ pub async fn mainloop<PM>(
                                        context.tx.send_timeout(StateMachineActions::BluetoothStopped(), TX_SEND_TIMEOUT_DURATION).await.unwrap();
                                    }
                                },
                                _ => println!("Ignored event {:?}", event.mask)
                                _ => debug!("Ignored event {:?}", event.mask)
                            }
                        }
                    },
@@ -245,7 +246,7 @@ pub async fn mainloop<PM>(
                                        _ => (),
                                    }
                                },
                                _ => println!("Ignored event {:?}", event.mask)
                                _ => debug!("Ignored event {:?}", event.mask)
                            }
                        }
                    },
@@ -291,7 +292,7 @@ impl ProcessManager for NativeSubprocess {
                self.process_container = None;
            }
            None => {
                println!("Process doesn't exist");
                warn!("Process doesn't exist");
            }
        }
    }
@@ -378,7 +379,7 @@ where
    /// Returns true if we are stopping bluetooth process.
    pub async fn action_stop_bluetooth(&mut self, hci_interface: i32) -> bool {
        if self.hci_interface != hci_interface {
            println!(
            warn!(
                "We are running hci{} but attempting to stop hci{}",
                self.hci_interface, hci_interface
            );
@@ -406,14 +407,14 @@ where
    pub async fn action_on_bluetooth_started(&mut self, pid: i32, hci_interface: i32) -> bool {
        let mut state = self.state.lock().await;
        if self.hci_interface != hci_interface {
            println!(
            warn!(
                "We should start hci{} but hci{} is started; capturing that process",
                self.hci_interface, hci_interface
            );
            self.hci_interface = hci_interface;
        }
        if *state != State::TurningOn {
            println!("Unexpected Bluetooth started");
            warn!("Unexpected Bluetooth started");
        }
        *state = State::On;
        self.bluetooth_pid = pid;
@@ -432,7 +433,7 @@ where
                true
            }
            State::On => {
                println!("Bluetooth stopped unexpectedly, try restarting");
                warn!("Bluetooth stopped unexpectedly, try restarting");
                *state = State::TurningOn;
                self.process_manager.start(format!("{}", self.hci_interface));
                false
@@ -450,14 +451,14 @@ where
        let mut state = self.state.lock().await;
        match *state {
            State::TurningOn => {
                println!("Restarting bluetooth");
                info!("Restarting bluetooth");
                *state = State::TurningOn;
                self.process_manager.stop(format! {"{}", self.hci_interface});
                self.process_manager.start(format! {"{}", self.hci_interface});
                StateMachineTimeoutActions::RetryStart
            }
            State::TurningOff => {
                println!("Killing bluetooth");
                info!("Killing bluetooth");
                self.process_manager.stop(format! {"{}", self.hci_interface});
                StateMachineTimeoutActions::RetryStop
            }