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

Commit 5d0e2e04 authored by Michael Sun's avatar Michael Sun
Browse files

floss: metrics: emit bond state event to gd/metrics

Introduce new topshim APIs to emit bond state events from rust to gd/metrics.

BUG: 232098615
Tag: #floss
Test: emerge-${BOARD} floss
Test: ./build.py
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Change-Id: Ic8fb319351ef18820293fa2eed45dbc333e2119d
parent 0f47cce8
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -37,7 +37,11 @@ void LogMetricsAdapterStateChanged(uint32_t state) {
      .SetIsFloss(true)
      .SetAdapterState((int64_t)ToAdapterState(state))
      .Record();
};
}

void LogMetricsBondCreateAttempt(RawAddress* addr) {}

void LogMetricsBondStateChanged(
    RawAddress* addr, uint32_t device_type, uint32_t status, uint32_t bond_state, int32_t fail_reason) {}
}  // namespace metrics
}  // namespace bluetooth
+6 −1
Original line number Diff line number Diff line
@@ -20,7 +20,12 @@
namespace bluetooth {
namespace metrics {

void LogMetricsAdapterStateChanged(uint32_t state){};
void LogMetricsAdapterStateChanged(uint32_t state) {}

void LogMetricsBondCreateAttempt(RawAddress* addr, uint32_t device_type) {}

void LogMetricsBondStateChanged(
    RawAddress* addr, uint32_t device_type, uint32_t status, uint32_t bond_state, int32_t fail_reason) {}

}  // namespace metrics
}  // namespace bluetooth
+5 −0
Original line number Diff line number Diff line
@@ -17,10 +17,15 @@

#include <cstdint>

#include "types/raw_address.h"

namespace bluetooth {
namespace metrics {

void LogMetricsAdapterStateChanged(uint32_t state);
void LogMetricsBondCreateAttempt(RawAddress* addr, uint32_t device_type);
void LogMetricsBondStateChanged(
    RawAddress* addr, uint32_t device_type, uint32_t status, uint32_t bond_state, int32_t fail_reason);

}  // namespace metrics
}  // namespace bluetooth
+40 −2
Original line number Diff line number Diff line
@@ -754,7 +754,7 @@ impl BtifBluetoothCallbacks for Bluetooth {
        status: BtStatus,
        addr: RawAddress,
        bond_state: BtBondState,
        _fail_reason: i32,
        fail_reason: i32,
    ) {
        let address = addr.to_string();

@@ -801,6 +801,19 @@ impl BtifBluetoothCallbacks for Bluetooth {
                bond_state.to_u32().unwrap(),
            );
        });

        let device_type = match self.get_remote_device_if_found(&address) {
            Some(d) => match d.properties.get(&BtPropertyType::TypeOfDevice) {
                Some(prop) => match prop {
                    BluetoothProperty::TypeOfDevice(type_of_device) => type_of_device.clone(),
                    _ => BtDeviceType::Unknown,
                },
                _ => BtDeviceType::Unknown,
            },
            _ => BtDeviceType::Unknown,
        };

        metrics::bond_state_changed(addr, device_type, status, bond_state, fail_reason);
    }

    fn remote_device_properties_changed(
@@ -1074,16 +1087,41 @@ impl IBluetooth for Bluetooth {
        let addr = RawAddress::from_string(device.address.clone());

        if addr.is_none() {
            metrics::bond_create_attempt(RawAddress::default(), BtDeviceType::Unknown);
            metrics::bond_state_changed(
                RawAddress::default(),
                BtDeviceType::Unknown,
                BtStatus::InvalidParam,
                BtBondState::NotBonded,
                0,
            );
            warn!("Can't create bond. Address {} is not valid", device.address);
            return false;
        }

        let address = addr.unwrap();
        let device_type = self.get_remote_type(device);

        // We explicitly log the attempt to start the bonding separate from logging the bond state.
        // The start of the attempt is critical to help identify a bonding/pairing session.
        metrics::bond_create_attempt(address, device_type.clone());

        // BREDR connection won't work when Inquiry is in progress.
        self.cancel_discovery();

        self.intf.lock().unwrap().create_bond(&address, transport) == 0
        let status = self.intf.lock().unwrap().create_bond(&address, transport);

        if status != 0 {
            metrics::bond_state_changed(
                address,
                device_type,
                BtStatus::from(status as u32),
                BtBondState::NotBonded,
                0,
            );
            return false;
        }
        return true;
    }

    fn cancel_bond_process(&self, device: BluetoothDevice) -> bool {
+17 −0
Original line number Diff line number Diff line
@@ -17,7 +17,11 @@
#include "gd/rust/topshim/metrics/metrics_shim.h"

#include "gd/metrics/metrics.h"
#include "gd/rust/topshim/common/utils.h"
#include "src/metrics.rs.h"
#include "types/raw_address.h"

namespace rusty = ::bluetooth::topshim::rust;

namespace bluetooth {
namespace topshim {
@@ -27,6 +31,19 @@ void adapter_state_changed(uint32_t state) {
  metrics::LogMetricsAdapterStateChanged(state);
}

void bond_create_attempt(RustRawAddress bt_addr, uint32_t device_type) {
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);

  metrics::LogMetricsBondCreateAttempt(&addr, device_type);
}

void bond_state_changed(
    RustRawAddress bt_addr, uint32_t device_type, uint32_t status, uint32_t bond_state, int32_t fail_reason) {
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);

  metrics::LogMetricsBondStateChanged(&addr, device_type, status, bond_state, fail_reason);
}

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth
Loading