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

Commit 285c20a7 authored by Chris Manton's avatar Chris Manton
Browse files

Refactor dumpsys apis for legacy and gd

Unify legacy and gd dumpsys syntax.
Fix static initialization order problem.

Bug: 147672133
Test: bt_headless and bluetooth_test_gd
Change-Id: I6f4d0d2c5611deace5a2a11eff8001b624b3dc62
parent 791845da
Loading
Loading
Loading
Loading
+19 −21
Original line number Diff line number Diff line
@@ -38,36 +38,32 @@ constexpr char kModuleName[] = "shim::Dumpsys";
struct Dumpsys::impl {
 public:
  void Dump(int fd, std::promise<void> promise);
  void Register(const void* token, DumpFunction func);
  void Unregister(const void* token);
  void RegisterDumpsysFunction(const void* token, DumpsysFunction func);
  void UnregisterDumpsysFunction(const void* token);

  ~impl();
  ~impl() = default;

 private:
  std::unordered_map<const void*, DumpFunction> dump_functions_;
  std::unordered_map<const void*, DumpsysFunction> dumpsys_functions_;
};

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

Dumpsys::impl::~impl() {
  ASSERT(dump_functions_.empty());
}

void Dumpsys::impl::Dump(int fd, std::promise<void> promise) {
  dprintf(fd, "%s Registered submodules:%zd\n", "gd::shim::dumpsys", dump_functions_.size());
  std::for_each(dump_functions_.begin(), dump_functions_.end(),
                [fd](std::pair<const void*, DumpFunction> element) { element.second(fd); });
  dprintf(fd, "%s Registered submodules:%zd\n", kModuleName, dumpsys_functions_.size());
  std::for_each(dumpsys_functions_.begin(), dumpsys_functions_.end(),
                [fd](std::pair<const void*, DumpsysFunction> element) { element.second(fd); });
  promise.set_value();
}

void Dumpsys::impl::Register(const void* token, DumpFunction func) {
  ASSERT(dump_functions_.find(token) == dump_functions_.end());
  dump_functions_[token] = func;
void Dumpsys::impl::RegisterDumpsysFunction(const void* token, DumpsysFunction func) {
  ASSERT(dumpsys_functions_.find(token) == dumpsys_functions_.end());
  dumpsys_functions_[token] = func;
}

void Dumpsys::impl::Unregister(const void* token) {
  ASSERT(dump_functions_.find(token) != dump_functions_.end());
  dump_functions_.erase(token);
void Dumpsys::impl::UnregisterDumpsysFunction(const void* token) {
  ASSERT(dumpsys_functions_.find(token) != dumpsys_functions_.end());
  dumpsys_functions_.erase(token);
}

void Dumpsys::Dump(int fd) {
@@ -77,12 +73,14 @@ void Dumpsys::Dump(int fd) {
  future.get();
}

void Dumpsys::Register(const void* token, DumpFunction func) {
  GetHandler()->Post(common::BindOnce(&Dumpsys::impl::Register, common::Unretained(pimpl_.get()), token, func));
void Dumpsys::RegisterDumpsysFunction(const void* token, DumpsysFunction func) {
  GetHandler()->Post(
      common::BindOnce(&Dumpsys::impl::RegisterDumpsysFunction, common::Unretained(pimpl_.get()), token, func));
}

void Dumpsys::Unregister(const void* token) {
  GetHandler()->Post(common::BindOnce(&Dumpsys::impl::Unregister, common::Unretained(pimpl_.get()), token));
void Dumpsys::UnregisterDumpsysFunction(const void* token) {
  GetHandler()->Post(
      common::BindOnce(&Dumpsys::impl::UnregisterDumpsysFunction, common::Unretained(pimpl_.get()), token));
}

/**
+2 −2
Original line number Diff line number Diff line
@@ -27,8 +27,8 @@ namespace shim {
class Dumpsys : public bluetooth::Module, public bluetooth::shim::IDumpsys {
 public:
  void Dump(int fd) override;
  void Register(const void* token, DumpFunction func);
  void Unregister(const void* token);
  void RegisterDumpsysFunction(const void* token, DumpsysFunction func);
  void UnregisterDumpsysFunction(const void* token);

  Dumpsys() = default;
  ~Dumpsys() = default;
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@
namespace bluetooth {
namespace shim {

using DumpFunction = std::function<void(int fd)>;
using DumpsysFunction = std::function<void(int fd)>;

struct IDumpsys {
  virtual void Dump(int fd) = 0;
+12 −12
Original line number Diff line number Diff line
@@ -44,18 +44,15 @@
namespace bluetooth {
namespace shim {

namespace {
constexpr char kModuleName[] = "shim::L2cap";
}  // namespace

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

using ConnectionInterfaceDescriptor = uint16_t;
using DeleterFunction = std::function<void(ConnectionInterfaceDescriptor)>;

static const ConnectionInterfaceDescriptor kInvalidConnectionInterfaceDescriptor = 0;
static const ConnectionInterfaceDescriptor kStartConnectionInterfaceDescriptor = 64;
static const ConnectionInterfaceDescriptor kMaxConnections = UINT16_MAX - kStartConnectionInterfaceDescriptor - 1;
namespace {
constexpr char kModuleName[] = "shim::L2cap";
constexpr ConnectionInterfaceDescriptor kInvalidConnectionInterfaceDescriptor = 0;
constexpr ConnectionInterfaceDescriptor kStartConnectionInterfaceDescriptor = 64;
constexpr ConnectionInterfaceDescriptor kMaxConnections = UINT16_MAX - kStartConnectionInterfaceDescriptor - 1;
}  // namespace

using ServiceInterfaceCallback =
    std::function<void(l2cap::Psm psm, l2cap::classic::DynamicChannelManager::RegistrationResult result)>;
@@ -421,6 +418,8 @@ struct L2cap::impl {
  }
};

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

L2cap::impl::impl(L2cap& module, l2cap::classic::L2capClassicModule* l2cap_module)
    : module_(module), l2cap_module_(l2cap_module), handler_(module_.GetHandler()),
      connection_interface_manager_(handler_) {
@@ -571,17 +570,18 @@ void L2cap::SendLoopbackResponse(std::function<void()> function) {
 * Module methods
 */
void L2cap::ListDependencies(ModuleList* list) {
  list->add<l2cap::classic::L2capClassicModule>();
  list->add<shim::Dumpsys>();
  list->add<l2cap::classic::L2capClassicModule>();
}

void L2cap::Start() {
  pimpl_ = std::make_unique<impl>(*this, GetDependency<l2cap::classic::L2capClassicModule>());
  GetDependency<shim::Dumpsys>()->Register(static_cast<void*>(this), [this](int fd) { pimpl_->Dump(fd); });
  GetDependency<shim::Dumpsys>()->RegisterDumpsysFunction(static_cast<void*>(this),
                                                          [this](int fd) { pimpl_->Dump(fd); });
}

void L2cap::Stop() {
  GetDependency<shim::Dumpsys>()->Unregister(static_cast<void*>(this));
  GetDependency<shim::Dumpsys>()->UnregisterDumpsysFunction(static_cast<void*>(this));
  pimpl_.reset();
}

+21 −21
Original line number Diff line number Diff line
@@ -16,38 +16,38 @@

#define LOG_TAG "bt_shim_storage"

#include <base/logging.h>
#include <algorithm>
#include <list>

#include "main/shim/dumpsys.h"
#include "main/shim/entry.h"
#include "main/shim/shim.h"

using ::bluetooth::shim::GetDumpsys;

std::list<bluetooth::legacy::shim::Dumpsys*> dumpsys_manager_;

bluetooth::legacy::shim::Dumpsys::Dumpsys() {
  CHECK(std::find(dumpsys_manager_.begin(), dumpsys_manager_.end(), this) ==
        dumpsys_manager_.end());
  dumpsys_manager_.push_back(this);
namespace {
constexpr char kModuleName[] = "shim::legacy::dumpsys";
static std::unordered_map<const void*, bluetooth::shim::DumpsysFunction>*
    dumpsys_functions_;
}  // namespace

void bluetooth::shim::RegisterDumpsysFunction(const void* token,
                                              DumpsysFunction func) {
  dumpsys_functions_ =
      new std::unordered_map<const void*, bluetooth::shim::DumpsysFunction>();
  CHECK(dumpsys_functions_->find(token) == dumpsys_functions_->end());
  dumpsys_functions_->insert({token, func});
}

bluetooth::legacy::shim::Dumpsys::~Dumpsys() {
  CHECK(std::find(dumpsys_manager_.begin(), dumpsys_manager_.end(), this) !=
        dumpsys_manager_.end());
  dumpsys_manager_.remove(this);
void bluetooth::shim::UnregisterDumpsysFunction(const void* token) {
  CHECK(dumpsys_functions_->find(token) != dumpsys_functions_->end());
  dumpsys_functions_->erase(token);
}

void bluetooth::shim::Dump(int fd) {
  for (auto& dumpsys : dumpsys_manager_) {
    dumpsys->Dump(fd);
  dprintf(fd, "%s Dumping shim legacy targets:%zd\n", kModuleName,
          dumpsys_functions_->size());
  for (auto& dumpsys : *dumpsys_functions_) {
    dumpsys.second(fd);
  }
  if (bluetooth::shim::is_gd_stack_started_up()) {
    GetDumpsys()->Dump(fd);
    bluetooth::shim::GetDumpsys()->Dump(fd);
  } else {
    dprintf(fd, "%s gd stack has not started up\n",
            "gd::shim::legacy::dumpsys");
    dprintf(fd, "%s gd stack has not started up\n", kModuleName);
  }
}
Loading