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

Commit 0fd67b00 authored by Rahul Arya's avatar Rahul Arya
Browse files

[Private GATT] Add logging at FFI interface

This logging will be printed only in full debug mode. Useful to see the
complete set of interactions with Rust (since the lower tester already
has logs via snoop logs).

Test: unit
Bug: 274945531
Change-Id: I23b61eb627956747f49e9174ee06b7991eb3d732
parent 58aeaefd
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -397,6 +397,9 @@ pub fn load(raw_flags: Vec<String>) {
    let flags = InitFlags::parse(raw_flags);
    info!("Flags loaded: {}", flags);
    *FLAGS.lock().unwrap() = flags;

    // re-init to respect log levels set by flags
    crate::init_logging();
}

#[cfg(test)]
+22 −2
Original line number Diff line number Diff line
//! Bluetooth common library

use init_flags::{
    get_log_level_for_tag, LOG_TAG_DEBUG, LOG_TAG_ERROR, LOG_TAG_FATAL, LOG_TAG_INFO,
    LOG_TAG_NOTICE, LOG_TAG_VERBOSE, LOG_TAG_WARN,
};

/// Provides waking timer abstractions
pub mod time;

@@ -21,22 +26,37 @@ pub mod init_flags;
/// Provides runtime configured system properties. Stubbed for non-Android.
pub mod sys_prop;

fn get_log_level() -> log::Level {
    match get_log_level_for_tag("bluetooth_core") {
        LOG_TAG_FATAL => log::Level::Error,
        LOG_TAG_ERROR => log::Level::Error,
        LOG_TAG_WARN => log::Level::Warn,
        LOG_TAG_NOTICE => log::Level::Info,
        LOG_TAG_INFO => log::Level::Info,
        LOG_TAG_DEBUG => log::Level::Debug,
        LOG_TAG_VERBOSE => log::Level::Trace,
        _ => log::Level::Info, // default level
    }
}

/// Inits logging for Android
#[cfg(target_os = "android")]
pub fn init_logging() {
    android_logger::init_once(
        android_logger::Config::default().with_tag("bt").with_min_level(log::Level::Debug),
        android_logger::Config::default().with_tag("bt").with_min_level(get_log_level()),
    );
    log::set_max_level(get_log_level().to_level_filter())
}

/// Inits logging for host
#[cfg(not(target_os = "android"))]
pub fn init_logging() {
    env_logger::Builder::new()
        .filter(None, log::LevelFilter::Debug)
        .filter(None, get_log_level().to_level_filter())
        .parse_default_env()
        .try_init()
        .ok();
    log::set_max_level(get_log_level().to_level_filter())
}

/// Indicates the object can be converted to a GRPC service
+12 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ use bt_common::init_flags::{
};
use cxx::UniquePtr;
pub use inner::*;
use log::{error, info, warn};
use log::{error, info, trace, warn};
use tokio::task::spawn_local;

use crate::{
@@ -196,6 +196,7 @@ impl GattCallbacks for GattCallbacksImpl {
        attr_type: AttributeBackingType,
        offset: u32,
    ) {
        trace!("on_server_read ({conn_id:?}, {trans_id:?}, {handle:?}, {attr_type:?}, {offset:?}");
        self.0.as_ref().unwrap().on_server_read(
            conn_id.0,
            trans_id.0,
@@ -215,6 +216,9 @@ impl GattCallbacks for GattCallbacksImpl {
        write_type: GattWriteType,
        value: AttAttributeDataView,
    ) {
        trace!(
            "on_server_write ({conn_id:?}, {trans_id:?}, {handle:?}, {attr_type:?}, {write_type:?}"
        );
        self.0.as_ref().unwrap().on_server_write(
            conn_id.0,
            trans_id.0,
@@ -235,6 +239,7 @@ impl GattCallbacks for GattCallbacksImpl {
        conn_id: ConnectionId,
        result: Result<(), IndicationError>,
    ) {
        trace!("on_indication_sent_confirmation ({conn_id:?}, {result:?}");
        self.0.as_ref().unwrap().on_indication_sent_confirmation(
            conn_id.0,
            match result {
@@ -250,6 +255,7 @@ impl GattCallbacks for GattCallbacksImpl {
        trans_id: TransactionId,
        decision: TransactionDecision,
    ) {
        trace!("on_execute ({conn_id:?}, {trans_id:?}, {decision:?}");
        self.0.as_ref().unwrap().on_execute(
            conn_id.0,
            trans_id.0,
@@ -444,6 +450,9 @@ fn send_response(_server_id: u8, conn_id: u16, trans_id: u32, status: u8, value:
    } else {
        Err(AttErrorCode::try_from(status).unwrap_or(AttErrorCode::UNLIKELY_ERROR))
    };

    trace!("send_response {conn_id:?}, {trans_id:?}, {:?}", value.as_ref().err());

    do_in_rust_thread(move |modules| {
        match modules.gatt_incoming_callbacks.send_response(
            ConnectionId(conn_id),
@@ -465,6 +474,8 @@ fn send_indication(_server_id: u8, handle: u16, conn_id: u16, value: &[u8]) {
    let conn_id = ConnectionId(conn_id);
    let value = AttAttributeDataChild::RawData(value.into());

    trace!("send_indication {handle:?}, {conn_id:?}");

    do_in_rust_thread(move |modules| {
        let Some(bearer) = modules.gatt_module.get_bearer(conn_id) else {
            error!("connection {conn_id:?} does not exist");