1. 背景
我們在 gd_shim_module 介紹章節中,看到 我們將 VendorSpecificEventManager 模塊加入到了 modules 中。
// system/main/shim/stack.cc
modules.add<hci::VendorSpecificEventManager>();
在 ModuleRegistry::Start 函數中我們對 加入的所有 module 挨個初始化。
而在該函數中啟動一個 module 都要執行那下面幾步:
-
創建module 實體
- Module* instance = module->ctor_();
-
將 當前 module 實體和 gd_stack_thread 線程綁定
- set_registry_and_handler(instance, thread);
-
啟動當前模塊所依賴的所有子模塊。
- instance->ListDependencies(&instance->dependencies_);
- Start(&instance->dependencies_, thread);
-
最后調用自己的 Start() 函數
- instance->Start();
-
將module 實體加入到 started_modules_
- started_modules_[module] = instance;
本篇文章就拿 hal::VendorSpecificEventManager 模塊來具體分析一下 他的啟動。
2. modules.add
我們先來看一下 在調用 modules.add 時, 到底做了那些事情。
modules.add<hal::VendorSpecificEventManager>();
class ModuleList {friend Module;friend ModuleRegistry;public:template <class T>void add() {list_.push_back(&T::Factory); // add 時 添加的是 hal::VendorSpecificEventManager::Factory}private:std::vector<const ModuleFactory*> list_;
};
- 從代碼中不難發現, 我們是將 hal::HciLayer::Factory 加入到 list_ 中的。
// system/gd/hci/vendor_specific_event_manager.cc
const ModuleFactory VendorSpecificEventManager::Factory =ModuleFactory([]() { return new VendorSpecificEventManager(); });// 這里在創建 ModuleFactory 對象時, 傳入了一個 函數, 這個函數 去 new VendorSpecificEventManager 對象
- 這里在創建 ModuleFactory 對象時, 傳入了一個 函數,但是并沒有去調用這個函數。
- 這個函數的目的是 去 new VendorSpecificEventManager 對象
class ModuleFactory {friend ModuleRegistry;friend FuzzTestModuleRegistry;public:ModuleFactory(std::function<Module*()> ctor);private:std::function<Module*()> ctor_;
};// system/gd/module.cc
ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
}
- 在創建 ModuleFactory 對象時, 也僅僅是將 如下的函數賦值給了 ModuleFactory::ctor_ 函數指針。
[]() { return new VendorSpecificEventManager(); }
3. 模塊具體啟動流程
1. 創建module 實體
- 創建module 實體
- Module* instance = module->ctor_();
[]() { return new VendorSpecificEventManager(); }
- 這里就會去實際觸發 該函數,去創建 VendorSpecificEventManager 對象。
- 也就是說, modules.addhal::VendorSpecificEventManager() 模塊對應的 實體其實是 VendorSpecificEventManager 對象。
class VendorSpecificEventManager : public ::bluetooth::Module {}
- VendorSpecificEventManager 繼承 Module
VendorSpecificEventManager::VendorSpecificEventManager() {pimpl_ = std::make_unique<impl>(this);
}
- 在 VendorSpecificEventManager 構造函數里面, 創建了 VendorSpecificEventManager::impl 對象。
2. 將 當前 module 實體和 gd_stack_thread 線程綁定
- 將 當前 module 實體和 gd_stack_thread 線程綁定
- set_registry_and_handler(instance, thread);
void ModuleRegistry::set_registry_and_handler(Module* instance, Thread* thread) const {instance->registry_ = this;instance->handler_ = new Handler(thread);
}
- 將我們的 gd_stack_thread 對應的 handle 直接保存在 Module->handler_ 中。
Handler* Module::GetHandler() const {ASSERT_LOG(handler_ != nullptr, "Can't get handler when it's not started");return handler_;
}
- 通過 Module::GetHandler() 來獲取當前 handler_
3.啟動當前模塊所依賴的所有子模塊
- 啟動當前模塊所依賴的所有子模塊。
- instance->ListDependencies(&instance->dependencies_);
- Start(&instance->dependencies_, thread);
// system/gd/hci/vendor_specific_event_manager.cc
void VendorSpecificEventManager::ListDependencies(ModuleList* list) const {list->add<hci::HciLayer>();list->add<hci::Controller>();
}
- 從上面的代碼中可以看到 VendorSpecificEventManager 模塊依賴
- HciLayer 模塊, 在之前已經啟動了。
- Controller 模塊,之前沒有啟動。 此時就會觸發 Controller 模塊的 啟動流程。 這個我們在下節討論。
4. 最后調用自己的 Start() 函數
- 最后調用自己的 Start() 函數
- instance->Start();
1. VendorSpecificEventManager::Start
void VendorSpecificEventManager::Start() {pimpl_->start(GetHandler(), GetDependency<hci::HciLayer>(), GetDependency<hci::Controller>());
}
- 直接調用了 VendorSpecificEventManager::impl::start 函數。
2. VendorSpecificEventManager::impl::start
// 如下是 VendorSpecificEventManager::impl::start 的實現void start(os::Handler* handler, hci::HciLayer* hci_layer, hci::Controller* controller) {module_handler_ = handler;hci_layer_ = hci_layer;controller_ = controller;hci_layer_->RegisterEventHandler(EventCode::VENDOR_SPECIFIC, handler->BindOn(this, &VendorSpecificEventManager::impl::on_vendor_specific_event)); // 向 hciLayer 層,注冊 VENDOR_SPECIFIC 事件的回調vendor_capabilities_ = controller->GetVendorCapabilities(); // 獲取 contoller 芯片支持的能力}
1. 注冊 VENDOR_SPECIFIC 事件的回調
我們在介紹 hciLayer 模塊時, 就介紹過 RegisterEventHandler 的使用。 這里不再贅述。
當我們收到 VENDOR_SPECIFIC 相關的事件后,就會回調 VendorSpecificEventManager::impl::on_vendor_specific_event 方法
2. on_vendor_specific_event
void on_vendor_specific_event(EventView event_view) {auto vendor_specific_event_view = VendorSpecificEventView::Create(event_view);ASSERT(vendor_specific_event_view.IsValid());VseSubeventCode vse_subevent_code = vendor_specific_event_view.GetSubeventCode();if (subevent_handlers_.find(vse_subevent_code) == subevent_handlers_.end()) {LOG_WARN("Unhandled vendor specific event of type 0x%02hhx", vse_subevent_code);return;}// 會從 subevent_handlers_ 中繼續找到 子事件的回調。subevent_handlers_[vse_subevent_code].Invoke(vendor_specific_event_view);}
3. subevent_handlers_
std::map<VseSubeventCode, common::ContextualCallback<void(VendorSpecificEventView)>> subevent_handlers_;
- subevent_handlers_ 是一個 map.
那么 subevent_handlers_ 中的子事件 回調是怎么注冊進來的?
// VendorSpecificEventManager::impl::register_eventvoid register_event(VseSubeventCode event, common::ContextualCallback<void(VendorSpecificEventView)> handler) {ASSERT_LOG(subevent_handlers_.count(event) == 0,"Can not register a second handler for %02hhx (%s)",event,VseSubeventCodeText(event).c_str());subevent_handlers_[event] = handler;}
void VendorSpecificEventManager::RegisterEventHandler(VseSubeventCode event, common::ContextualCallback<void(VendorSpecificEventView)> handler) {CallOn(pimpl_.get(), &impl::register_event, event, handler);
}
- 整個 VendorSpecificEventManager 很簡單,主要就是 對外提供 RegisterEventHandler 方法。管理廠商相關事件的回調。
VendorSpecificEventManager::RegisterEventHandler 函數主要用在 LeScanningManager 和 LeScanningManager 模塊。 等介紹 這倆模塊時,我們在展開介紹。
5.將module 實體加入到 started_modules_
- 將module 實體加入到 started_modules_
- started_modules_[module] = instance;