mc.mitm: clean up module initialisation

This commit is contained in:
ndeadly 2023-07-26 21:55:28 +02:00
parent 1409d462ea
commit 2bd061ea78
11 changed files with 93 additions and 86 deletions

View file

@ -70,8 +70,8 @@ namespace ams::mitm::bluetooth {
}
Result Launch() {
R_TRY(os::CreateThread(&g_thread,
void Launch() {
R_ABORT_UNLESS(os::CreateThread(&g_thread,
BtdrvMitmThreadFunction,
nullptr,
g_thread_stack,
@ -81,8 +81,6 @@ namespace ams::mitm::bluetooth {
os::SetThreadNamePointer(&g_thread, "mc::BtdrvMitmThread");
os::StartThread(&g_thread);
R_SUCCEED();
}
void WaitFinished() {

View file

@ -18,7 +18,7 @@
namespace ams::mitm::bluetooth {
Result Launch();
void Launch();
void WaitFinished();
}

View file

@ -70,8 +70,8 @@ namespace ams::mitm::btm {
}
Result Launch() {
R_TRY(os::CreateThread(&g_thread,
void Launch() {
R_ABORT_UNLESS(os::CreateThread(&g_thread,
BtmMitmThreadFunction,
nullptr,
g_thread_stack,
@ -81,8 +81,6 @@ namespace ams::mitm::btm {
os::SetThreadNamePointer(&g_thread, "mc::BtmMitmThread");
os::StartThread(&g_thread);
R_SUCCEED();
}
void WaitFinished() {

View file

@ -18,7 +18,7 @@
namespace ams::mitm::btm {
Result Launch();
void Launch();
void WaitFinished();
}

View file

@ -115,30 +115,31 @@ namespace ams::mitm {
return 1;
}
}
void ParseIniConfig() {
/* Open the file. */
fs::FileHandle file;
{
if (R_FAILED(fs::OpenFile(std::addressof(file), config_file_location, fs::OpenMode_Read))) {
return;
void ParseIniConfiguration() {
fs::FileHandle file;
{
if (R_FAILED(fs::OpenFile(std::addressof(file), config_file_location, fs::OpenMode_Read))) {
return;
}
}
}
ON_SCOPE_EXIT { fs::CloseFile(file); };
ON_SCOPE_EXIT { fs::CloseFile(file); };
util::ini::ParseFile(file, &g_global_config, ConfigIniHandler);
}
void ReadSystemLanguage() {
R_ABORT_UNLESS(setInitialize());
ON_SCOPE_EXIT { setExit(); };
u64 language_code = 0;
R_ABORT_UNLESS(setGetSystemLanguage(&language_code));
R_ABORT_UNLESS(setMakeLanguage(language_code, &g_system_language));
}
/* Parse the config. */
util::ini::ParseFile(file, &g_global_config, ConfigIniHandler);
}
void InitializeConfig() {
ParseIniConfig();
R_ABORT_UNLESS(setInitialize());
ON_SCOPE_EXIT { setExit(); };
u64 language_code = 0;
R_ABORT_UNLESS(setGetSystemLanguage(&language_code));
R_ABORT_UNLESS(setMakeLanguage(language_code, &g_system_language));
void LoadConfiguration() {
ParseIniConfiguration();
ReadSystemLanguage();
}
MissionControlConfig *GetGlobalConfig() {

View file

@ -39,7 +39,7 @@ namespace ams::mitm {
} misc;
};
void InitializeConfig();
void LoadConfiguration();
MissionControlConfig *GetGlobalConfig();
SetLanguage GetSystemLanguage();

View file

@ -35,6 +35,36 @@ namespace ams::mitm {
os::Event g_init_event(os::EventClearMode_ManualClear);
Result OverrideHostAddress(const ams::bluetooth::Address *host_address) {
if (hos::GetVersion() < hos::Version_12_0_0) {
R_TRY(btdrvLegacySetAdapterProperty(BtdrvBluetoothPropertyType_Address, host_address, sizeof(ams::bluetooth::Address)));
}
else {
BtdrvAdapterProperty property;
property.type = BtdrvAdapterPropertyType_Address;
property.size = sizeof(ams::bluetooth::Address);
std::memcpy(property.data, host_address, sizeof(ams::bluetooth::Address));
R_TRY(btdrvSetAdapterProperty(BtdrvAdapterPropertyType_Address, &property));
}
R_SUCCEED();
}
Result OverrideHostName(const char *host_name) {
if (hos::GetVersion() < hos::Version_12_0_0) {
R_TRY(btdrvLegacySetAdapterProperty(BtdrvBluetoothPropertyType_Name, host_name, std::strlen(host_name)));
}
else {
BtdrvAdapterProperty property;
property.type = BtdrvAdapterPropertyType_Name;
property.size = std::strlen(host_name);
std::memcpy(property.data, host_name, std::strlen(host_name));
R_TRY(btdrvSetAdapterProperty(BtdrvAdapterPropertyType_Name, &property));
}
R_SUCCEED();
}
void InitializeThreadFunc(void *) {
// Start async worker thread(s)
ams::async::Initialize();
@ -57,30 +87,12 @@ namespace ams::mitm {
// Set bluetooth adapter host address override
ams::bluetooth::Address null_address = {};
if (std::memcmp(&config->bluetooth.host_address, &null_address, sizeof(ams::bluetooth::Address)) != 0) {
if (hos::GetVersion() < hos::Version_12_0_0) {
R_ABORT_UNLESS(btdrvLegacySetAdapterProperty(BtdrvBluetoothPropertyType_Address, &config->bluetooth.host_address, sizeof(ams::bluetooth::Address)));
}
else {
BtdrvAdapterProperty property;
property.type = BtdrvAdapterPropertyType_Address;
property.size = sizeof(ams::bluetooth::Address);
std::memcpy(property.data, &config->bluetooth.host_address, sizeof(ams::bluetooth::Address));
R_ABORT_UNLESS(btdrvSetAdapterProperty(BtdrvAdapterPropertyType_Address, &property));
}
R_ABORT_UNLESS(OverrideHostAddress(&config->bluetooth.host_address));
}
// Set bluetooth adapter host name override
if (std::strlen(config->bluetooth.host_name) > 0) {
if (hos::GetVersion() < hos::Version_12_0_0) {
R_ABORT_UNLESS(btdrvLegacySetAdapterProperty(BtdrvBluetoothPropertyType_Name, config->bluetooth.host_name, std::strlen(config->bluetooth.host_name)));
}
else {
BtdrvAdapterProperty property;
property.type = BtdrvAdapterPropertyType_Name;
property.size = std::strlen(config->bluetooth.host_name);
std::memcpy(property.data, config->bluetooth.host_name, std::strlen(config->bluetooth.host_name));
R_ABORT_UNLESS(btdrvSetAdapterProperty(BtdrvAdapterPropertyType_Name, &property));
}
R_ABORT_UNLESS(OverrideHostName(config->bluetooth.host_name));
}
g_init_event.Signal();
@ -94,12 +106,11 @@ namespace ams::mitm {
while (R_FAILED(btmInitialize())) {
os::SleepThread(ams::TimeSpan::FromMilliSeconds(200));
}
}
}
void InitializeModules() {
void LaunchModules() {
const s32 ThreadPriority = -7;
const size_t ThreadStackSize = 0x1000;
os::ThreadType init_thread;
@ -108,7 +119,7 @@ namespace ams::mitm {
struct alignas(os::ThreadStackAlignment) ThreadStack { u8 stack[ThreadStackSize]; };
auto thread_stack = std::make_unique<ThreadStack>();
// Create and start initialisation thread
// Create and start background initialisation thread
R_ABORT_UNLESS(os::CreateThread(&init_thread,
InitializeThreadFunc,
nullptr,
@ -119,26 +130,28 @@ namespace ams::mitm {
os::SetThreadNamePointer(&init_thread, "mc::InitThread");
os::StartThread(&init_thread);
// Launch mitm and other modules
R_ABORT_UNLESS(ams::mitm::bluetooth::Launch());
R_ABORT_UNLESS(ams::mitm::btm::Launch());
R_ABORT_UNLESS(ams::mitm::mc::Launch());
R_ABORT_UNLESS(ams::usb::Launch());
// Launch IPC servers
ams::mitm::bluetooth::Launch();
ams::mitm::btm::Launch();
ams::mc::Launch();
// Wait for initialisation thread to finish and destroy it
// Launch additional modules
ams::usb::Launch();
// Wait for initialisation thread to terminate
os::WaitThread(&init_thread);
os::DestroyThread(&init_thread);
}
void WaitModules() {
ams::usb::WaitFinished();
ams::mc::WaitFinished();
ams::mitm::btm::WaitFinished();
ams::mitm::bluetooth::WaitFinished();
}
void WaitInitialized() {
g_init_event.Wait();
}
void WaitModules() {
ams::usb::WaitFinished();
ams::mitm::mc::WaitFinished();
ams::mitm::btm::WaitFinished();
ams::mitm::bluetooth::WaitFinished();
}
}

View file

@ -17,8 +17,8 @@
namespace ams::mitm {
void InitializeModules();
void WaitInitialized();
void LaunchModules();
void WaitModules();
void WaitInitialized();
}

View file

@ -78,31 +78,30 @@ namespace ams {
void FinalizeSystemModule() { /* ... */ }
void Startup() { /* ... */ }
void Startup() {
// Load module configuration from ini file
mitm::LoadConfiguration();
}
}
void Main() {
// Initialise module configuration
mitm::InitializeConfig();
// Launch modules and run initialisation thread
mitm::InitializeModules();
// Launch mitm and other modules
mitm::LaunchModules();
// Initialise pm module
psc::PmModule pm_module;
psc::PmModuleId pm_module_id = static_cast<psc::PmModuleId>(0xBD);
const psc::PmModuleId pm_dependencies[] = { psc::PmModuleId_Fs };
R_ABORT_UNLESS(pm_module.Initialize(pm_module_id, pm_dependencies, sizeof(pm_dependencies) / sizeof(u32), os::EventClearMode_AutoClear));
psc::PmModule pm_module;
psc::PmState pm_state;
psc::PmFlagSet pm_flags;
os::SystemEvent *pm_event = pm_module.GetEventPointer();
R_ABORT_UNLESS(pm_module.Initialize(pm_module_id, pm_dependencies, sizeof(pm_dependencies) / sizeof(u32), os::EventClearMode_AutoClear));
// Loop power management events until shutdown signal is received
bool shutdown = false;
while (!shutdown) {
pm_event->Wait();
pm_module.GetEventPointer()->Wait();
if (R_SUCCEEDED(pm_module.GetRequest(&pm_state, &pm_flags))) {
switch (pm_state) {
case psc::PmState_ShutdownReady:
@ -119,7 +118,7 @@ namespace ams {
R_ABORT_UNLESS(pm_module.Acknowledge(pm_state, ResultSuccess()));
}
// Wait for mitm modules to terminate
// Wait for modules to terminate
mitm::WaitModules();
}

View file

@ -91,8 +91,8 @@ namespace ams::usb {
}
Result Launch() {
R_TRY(os::CreateThread(&g_thread,
void Launch() {
R_ABORT_UNLESS(os::CreateThread(&g_thread,
UsbThreadFunction,
nullptr,
g_thread_stack,
@ -102,8 +102,6 @@ namespace ams::usb {
os::SetThreadNamePointer(&g_thread, "mc::UsbThread");
os::StartThread(&g_thread);
return ams::ResultSuccess();
}
void WaitFinished() {

View file

@ -17,7 +17,7 @@
namespace ams::usb {
Result Launch();
void Launch();
void WaitFinished();
}