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

Commit c22df811 authored by Michael Sun's avatar Michael Sun
Browse files

btaa: route HCI packet to BTAA module

Route HCI packets to BTAA for attribution analysis processing.

Tag: #feature
Bug: 177230507
Test: mmma -j system/bt
BYPASS_LONG_LINES_REASON: consist with gd format

Change-Id: Ic26c472758547fa7eeac1ff39c81a78c2545ad5d
parent 189a0770
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -256,6 +256,8 @@ cc_binary {
            shared_libs: [
                "android.hardware.bluetooth@1.0",
                "android.hardware.bluetooth@1.1",
                "android.system.suspend.control-V1-ndk",
                "libbinder_ndk",
                "libhidlbase",
                "libutils",
                "libcutils",
@@ -304,6 +306,8 @@ cc_test {
            shared_libs: [
                "android.hardware.bluetooth@1.0",
                "android.hardware.bluetooth@1.1",
                "android.system.suspend.control-V1-ndk",
                "libbinder_ndk",
                "libhidlbase",
                "libutils",
                "libcutils",
@@ -432,6 +436,8 @@ cc_defaults {
            shared_libs: [
                "android.hardware.bluetooth@1.0",
                "android.hardware.bluetooth@1.1",
                "android.system.suspend.control-V1-ndk",
                "libbinder_ndk",
                "libcutils",
                "libhidlbase",
                "libutils",
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#pragma once

#include "hal/snoop_logger.h"
#include "hci/address.h"
#include "module.h"

@@ -37,6 +38,7 @@ class ActivityAttribution : public bluetooth::Module {
  ActivityAttribution() = default;
  ~ActivityAttribution() = default;

  void Capture(const hal::HciPacket& packet, hal::SnoopLogger::PacketType type);
  void RegisterActivityAttributionCallback(ActivityAttributionCallback* callback);

  static const ModuleFactory Factory;
+30 −9
Original line number Diff line number Diff line
@@ -36,9 +36,7 @@ using namespace ndk;
namespace bluetooth {
namespace activity_attribution {

const ModuleFactory ActivityAttribution::Factory = ModuleFactory([]() {
  return new ActivityAttribution();
});
const ModuleFactory ActivityAttribution::Factory = ModuleFactory([]() { return new ActivityAttribution(); });

static const std::string kBtWakelockName("hal_bluetooth_lock");

@@ -70,16 +68,14 @@ struct ActivityAttribution::impl {
    bool is_registered = false;

    auto control_service =
        ISuspendControlService::fromBinder(SpAIBinder(
          AServiceManager_getService("suspend_control")));
        ISuspendControlService::fromBinder(SpAIBinder(AServiceManager_getService("suspend_control")));
    if (!control_service) {
      LOG_ERROR("Fail to obtain suspend_control");
      return;
    }

    Status register_callback_status =
        control_service->registerCallback(SharedRefBase::make<wakeup_callback>(module),
                                          &is_registered);
        control_service->registerCallback(SharedRefBase::make<wakeup_callback>(module), &is_registered);
    if (!is_registered || !register_callback_status.isOk()) {
      LOG_ERROR("Fail to register wakeup callback");
      return;
@@ -93,6 +89,8 @@ struct ActivityAttribution::impl {
    }
  }

  void on_hci_packet(hal::HciPacket packet, hal::SnoopLogger::PacketType type, uint16_t length) {}

  void register_callback(ActivityAttributionCallback* callback) {
    callback_ = callback;
  }
@@ -100,8 +98,31 @@ struct ActivityAttribution::impl {
  ActivityAttributionCallback* callback_;
};

void ActivityAttribution::RegisterActivityAttributionCallback(
  ActivityAttributionCallback* callback) {
void ActivityAttribution::Capture(const hal::HciPacket& packet, hal::SnoopLogger::PacketType type) {
  uint16_t original_length = packet.size();
  uint16_t truncate_length;

  switch (type) {
    case hal::SnoopLogger::PacketType::CMD:
    case hal::SnoopLogger::PacketType::EVT:
      truncate_length = packet.size();
      break;
    case hal::SnoopLogger::PacketType::ACL:
    case hal::SnoopLogger::PacketType::SCO:
    case hal::SnoopLogger::PacketType::ISO:
      truncate_length = 0;
      break;
  }

  if (!truncate_length) {
    return;
  }

  hal::HciPacket truncate_packet(packet.begin(), packet.begin() + truncate_length);
  CallOn(pimpl_.get(), &impl::on_hci_packet, truncate_packet, type, original_length);
}

void ActivityAttribution::RegisterActivityAttributionCallback(ActivityAttributionCallback* callback) {
  CallOn(pimpl_.get(), &impl::register_callback, callback);
}

+14 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include <future>
#include <vector>

#include "btaa/activity_attribution.h"
#include "common/stop_watch.h"
#include "common/strings.h"
#include "hal/hci_hal.h"
@@ -61,7 +62,8 @@ std::string GetTimerText(const char* func_name, VecType vec) {

class InternalHciCallbacks : public IBluetoothHciCallbacks {
 public:
  InternalHciCallbacks(SnoopLogger* btsnoop_logger) : btsnoop_logger_(btsnoop_logger) {
  InternalHciCallbacks(activity_attribution::ActivityAttribution* btaa_logger_, SnoopLogger* btsnoop_logger)
      : btaa_logger_(btaa_logger_), btsnoop_logger_(btsnoop_logger) {
    init_promise_ = new std::promise<void>();
  }

@@ -88,6 +90,7 @@ class InternalHciCallbacks : public IBluetoothHciCallbacks {
    common::StopWatch(GetTimerText(__func__, event));
    std::vector<uint8_t> received_hci_packet(event.begin(), event.end());
    btsnoop_logger_->Capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::EVT);
    btaa_logger_->Capture(received_hci_packet, SnoopLogger::PacketType::EVT);
    if (callback_ != nullptr) {
      callback_->hciEventReceived(std::move(received_hci_packet));
    }
@@ -98,6 +101,7 @@ class InternalHciCallbacks : public IBluetoothHciCallbacks {
    common::StopWatch(GetTimerText(__func__, data));
    std::vector<uint8_t> received_hci_packet(data.begin(), data.end());
    btsnoop_logger_->Capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
    btaa_logger_->Capture(received_hci_packet, SnoopLogger::PacketType::ACL);
    if (callback_ != nullptr) {
      callback_->aclDataReceived(std::move(received_hci_packet));
    }
@@ -108,6 +112,7 @@ class InternalHciCallbacks : public IBluetoothHciCallbacks {
    common::StopWatch(GetTimerText(__func__, data));
    std::vector<uint8_t> received_hci_packet(data.begin(), data.end());
    btsnoop_logger_->Capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::SCO);
    btaa_logger_->Capture(received_hci_packet, SnoopLogger::PacketType::SCO);
    if (callback_ != nullptr) {
      callback_->scoDataReceived(std::move(received_hci_packet));
    }
@@ -127,6 +132,7 @@ class InternalHciCallbacks : public IBluetoothHciCallbacks {
 private:
  std::promise<void>* init_promise_ = nullptr;
  HciHalCallbacks* callback_ = nullptr;
  activity_attribution::ActivityAttribution* btaa_logger_ = nullptr;
  SnoopLogger* btsnoop_logger_ = nullptr;
};

@@ -145,18 +151,21 @@ class HciHalHidl : public HciHal {
  void sendHciCommand(HciPacket command) override {
    common::StopWatch(GetTimerText(__func__, command));
    btsnoop_logger_->Capture(command, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
    btaa_logger_->Capture(command, SnoopLogger::PacketType::CMD);
    bt_hci_->sendHciCommand(command);
  }

  void sendAclData(HciPacket packet) override {
    common::StopWatch(GetTimerText(__func__, packet));
    btsnoop_logger_->Capture(packet, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
    btaa_logger_->Capture(packet, SnoopLogger::PacketType::ACL);
    bt_hci_->sendAclData(packet);
  }

  void sendScoData(HciPacket packet) override {
    common::StopWatch(GetTimerText(__func__, packet));
    btsnoop_logger_->Capture(packet, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::SCO);
    btaa_logger_->Capture(packet, SnoopLogger::PacketType::SCO);
    bt_hci_->sendScoData(packet);
  }

@@ -174,9 +183,11 @@ class HciHalHidl : public HciHal {
 protected:
  void ListDependencies(ModuleList* list) override {
    list->add<SnoopLogger>();
    list->add<activity_attribution::ActivityAttribution>();
  }

  void Start() override {
    btaa_logger_ = GetDependency<activity_attribution::ActivityAttribution>();
    btsnoop_logger_ = GetDependency<SnoopLogger>();

    bt_hci_1_1_ = IBluetoothHci::getService();
@@ -192,7 +203,7 @@ class HciHalHidl : public HciHal {
    ASSERT_LOG(death_link.isOk(), "Unable to set the death recipient for the Bluetooth HAL");
    // Block allows allocation of a variable that might be bypassed by goto.
    {
      callbacks_ = new InternalHciCallbacks(btsnoop_logger_);
      callbacks_ = new InternalHciCallbacks(btaa_logger_, btsnoop_logger_);
      if (bt_hci_1_1_ != nullptr) {
        bt_hci_1_1_->initialize_1_1(callbacks_);
      } else {
@@ -220,6 +231,7 @@ class HciHalHidl : public HciHal {
  android::sp<InternalHciCallbacks> callbacks_;
  android::sp<IBluetoothHci_1_0> bt_hci_;
  android::sp<IBluetoothHci> bt_hci_1_1_;
  activity_attribution::ActivityAttribution* btaa_logger_;
  SnoopLogger* btsnoop_logger_;
};