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

Commit ef66a1a6 authored by Zach Johnson's avatar Zach Johnson Committed by Gerrit Code Review
Browse files

Merge "Create handlers for every module upon module start."

parents cac60c99 6fc480b0
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ using ::bluetooth::hal::HciHalHostRootcanalConfig;
using ::bluetooth::StackManager;
using ::bluetooth::grpc::GrpcModule;
using ::bluetooth::ModuleList;
using ::bluetooth::os::Thread;

namespace {
static StackManager* stack;
@@ -59,8 +60,9 @@ int main(int argc, const char** argv) {
  ModuleList modules;
  modules.add<::bluetooth::hal::facade::HalFacadeModule>();

  Thread* stack_thread = new Thread("stack_thread", Thread::Priority::NORMAL);
  stack = new StackManager();
  stack->StartUp(&modules);
  stack->StartUp(&modules, stack_thread);

  GrpcModule* grpc_module = stack->GetInstance<GrpcModule>();
  grpc_module->StartServer("0.0.0.0", port);
+8 −1
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@

#include <gtest/gtest.h>

#include "os/thread.h"

using ::bluetooth::os::Thread;

namespace bluetooth {
namespace hal {
namespace {
@@ -28,16 +32,19 @@ namespace {
class HciHalHidlTest : public ::testing::Test {
 protected:
  void SetUp() override {
    thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
  }

  void TearDown() override {
    delete thread_;
  }

  ModuleRegistry fake_registry_;
  Thread* thread_;
};

TEST_F(HciHalHidlTest, init_and_close) {
  fake_registry_.Start<HciHal>();
  fake_registry_.Start<HciHal>(thread_);
  fake_registry_.StopAll();
}
}  // namespace
+8 −1
Original line number Diff line number Diff line
@@ -33,8 +33,11 @@
#include <gtest/gtest.h>

#include "os/log.h"
#include "os/thread.h"
#include "os/utils.h"

using ::bluetooth::os::Thread;

namespace bluetooth {
namespace hal {
namespace {
@@ -132,9 +135,11 @@ class FakeRootcanalDesktopHciServer {
class HciHalRootcanalTest : public ::testing::Test {
 protected:
  void SetUp() override {
    thread_ = new Thread("test_thread", Thread::Priority::NORMAL);

    HciHalHostRootcanalConfig::Get()->SetPort(kTestPort);
    fake_server_ = new FakeRootcanalDesktopHciServer;
    fake_registry_.Start<HciHal>();
    fake_registry_.Start<HciHal>(thread_);
    hal_ = fake_registry_.GetInstance<HciHal>();
    hal_->registerIncomingPacketCallback(&callbacks_);
    fake_server_socket_ = fake_server_->Accept();  // accept() after client is connected to avoid blocking
@@ -146,6 +151,7 @@ class HciHalRootcanalTest : public ::testing::Test {
    fake_registry_.StopAll();
    close(fake_server_socket_);
    delete fake_server_;
    delete thread_;
  }

  void SetFakeServerSocketToBlocking() {
@@ -159,6 +165,7 @@ class HciHalRootcanalTest : public ::testing::Test {
  ModuleRegistry fake_registry_;
  TestHciHalCallbacks callbacks_;
  int fake_server_socket_ = -1;
  Thread* thread_;
};

void check_packet_equal(std::pair<uint8_t, HciPacket> hci_packet1_type_data_pair, H4Packet h4_packet2) {
+14 −4
Original line number Diff line number Diff line
@@ -16,30 +16,39 @@

#include "module.h"

using ::bluetooth::os::Handler;
using ::bluetooth::os::Thread;

namespace bluetooth {

ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
}

Handler* Module::GetHandler() {
  return handler_;
}

bool ModuleRegistry::IsStarted(const ModuleFactory* factory) const {
  return started_modules_.find(factory) != started_modules_.end();
}

void ModuleRegistry::Start(ModuleList* modules) {
void ModuleRegistry::Start(ModuleList* modules, Thread* thread) {
  for (auto it = modules->list_.begin(); it != modules->list_.end(); it++) {
    Start(*it);
    Start(*it, thread);
  }
}

void ModuleRegistry::Start(const ModuleFactory* module) {
void ModuleRegistry::Start(const ModuleFactory* module, Thread* thread) {
  if (IsStarted(module)) {
    return;
  }

  Module* instance = module->ctor_();
  instance->handler_ = new Handler(thread);

  ModuleList dependencies;
  instance->ListDependencies(&dependencies);
  Start(&dependencies);
  Start(&dependencies, thread);

  instance->Start(this);
  start_order_.push_back(module);
@@ -54,6 +63,7 @@ void ModuleRegistry::StopAll() {
    ASSERT(instance != started_modules_.end());
    instance->second->Stop(this);

    delete instance->second->handler_;
    delete instance->second;
    started_modules_.erase(instance);
  }
+12 −5
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@
#include <map>

#include "os/log.h"
#include "os/handler.h"
#include "os/thread.h"

namespace bluetooth {

@@ -53,7 +55,7 @@ class ModuleList {
// static const ModuleFactory Factory;
//
// which will provide a constructor for the module registry to call.
// The module registry will also use the Factory as the identifier
// The module registry will also use the factory as the identifier
// for that module.
class Module {
 friend ModuleRegistry;
@@ -68,6 +70,11 @@ class Module {

  // Release all resources, you're about to be deleted
  virtual void Stop(const ModuleRegistry* registry) = 0;

  ::bluetooth::os::Handler* GetHandler();

 private:
  ::bluetooth::os::Handler* handler_;
};

class ModuleRegistry {
@@ -88,14 +95,14 @@ class ModuleRegistry {

  // Start all the modules on this list and their dependencies
  // in dependency order
  void Start(ModuleList* modules);
  void Start(ModuleList* modules, ::bluetooth::os::Thread* thread);

  template <class T>
  void Start() {
    Start(&T::Factory);
  void Start(::bluetooth::os::Thread* thread) {
    Start(&T::Factory, thread);
  }

  void Start(const ModuleFactory* id);
  void Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread);

  // Stop all running modules in reverse order of start
  void StopAll();
Loading