2021-01-26 04:02:15 +01:00
|
|
|
/*
|
2023-02-23 21:57:56 +01:00
|
|
|
* Copyright (c) 2020-2023 ndeadly
|
2021-01-26 04:02:15 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include <switch.h>
|
|
|
|
#include "mcmitm_initialization.hpp"
|
2021-02-21 00:16:23 +01:00
|
|
|
#include "mcmitm_config.hpp"
|
2022-03-30 19:27:24 +11:00
|
|
|
#include "async/async.hpp"
|
2021-02-21 00:16:23 +01:00
|
|
|
#include "bluetooth_mitm/btdrv_mitm_service.hpp"
|
2021-02-21 12:51:00 +01:00
|
|
|
#include "bluetooth_mitm/bluetoothmitm_module.hpp"
|
|
|
|
#include "btm_mitm/btmmitm_module.hpp"
|
2021-08-24 01:20:35 +02:00
|
|
|
#include "mc/mc_module.hpp"
|
2021-01-26 04:02:15 +01:00
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_events.hpp"
|
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_core.hpp"
|
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_hid.hpp"
|
2022-02-08 07:14:07 +01:00
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_hid_report.hpp"
|
2021-01-26 04:02:15 +01:00
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_ble.hpp"
|
2023-04-11 15:56:48 +02:00
|
|
|
#include "usb/mc_usb_handler.hpp"
|
2021-10-16 15:15:56 -07:00
|
|
|
|
2021-01-26 04:02:15 +01:00
|
|
|
namespace ams::mitm {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-02-21 00:16:23 +01:00
|
|
|
os::Event g_init_event(os::EventClearMode_ManualClear);
|
|
|
|
|
2021-10-16 15:15:56 -07:00
|
|
|
void InitializeThreadFunc(void *) {
|
2022-03-30 19:27:24 +11:00
|
|
|
// Start async worker thread(s)
|
|
|
|
ams::async::Initialize();
|
|
|
|
|
2021-02-23 01:41:49 +01:00
|
|
|
// Start bluetooth event handling thread
|
|
|
|
ams::bluetooth::events::Initialize();
|
|
|
|
|
2022-02-08 07:14:07 +01:00
|
|
|
// Start hid report handling thread
|
|
|
|
ams::bluetooth::hid::report::Initialize();
|
|
|
|
|
2021-02-23 01:41:49 +01:00
|
|
|
// Wait for system to call BluetoothEnable
|
|
|
|
ams::bluetooth::core::WaitEnabled();
|
2021-01-26 04:02:15 +01:00
|
|
|
|
2021-02-21 00:16:23 +01:00
|
|
|
// Connect to btdrv service now that we're sure the mitm is up and running
|
2021-04-30 23:05:01 +02:00
|
|
|
R_ABORT_UNLESS(btdrvInitialize());
|
2021-01-26 04:02:15 +01:00
|
|
|
|
2021-02-21 00:16:23 +01:00
|
|
|
// Get global module settings
|
|
|
|
auto config = GetGlobalConfig();
|
2021-01-26 04:02:15 +01:00
|
|
|
|
2021-02-21 00:16:23 +01:00
|
|
|
// 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) {
|
2021-05-17 19:46:50 +02:00
|
|
|
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));
|
|
|
|
}
|
2021-02-21 00:16:23 +01:00
|
|
|
}
|
2021-01-26 04:02:15 +01:00
|
|
|
|
2021-02-21 00:16:23 +01:00
|
|
|
// Set bluetooth adapter host name override
|
|
|
|
if (std::strlen(config->bluetooth.host_name) > 0) {
|
2021-05-17 19:46:50 +02:00
|
|
|
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));
|
|
|
|
}
|
2021-02-21 00:16:23 +01:00
|
|
|
}
|
2021-01-26 04:02:15 +01:00
|
|
|
|
2021-02-21 00:16:23 +01:00
|
|
|
g_init_event.Signal();
|
2023-04-11 15:56:48 +02:00
|
|
|
|
|
|
|
// Loop until we can initialise btm:sys
|
|
|
|
while (R_FAILED(btmsysInitialize())) {
|
|
|
|
os::SleepThread(ams::TimeSpan::FromMilliSeconds(200));
|
|
|
|
}
|
2023-04-24 22:28:24 +02:00
|
|
|
|
|
|
|
// Loop until we can initialise btm
|
|
|
|
while (R_FAILED(btmInitialize())) {
|
|
|
|
os::SleepThread(ams::TimeSpan::FromMilliSeconds(200));
|
|
|
|
}
|
2023-04-11 15:56:48 +02:00
|
|
|
|
2021-01-26 04:02:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-30 23:59:07 +02:00
|
|
|
void InitializeModules() {
|
|
|
|
const s32 ThreadPriority = -7;
|
|
|
|
const size_t ThreadStackSize = 0x1000;
|
|
|
|
os::ThreadType init_thread;
|
|
|
|
|
|
|
|
// Allocate temporary thread stack on heap
|
|
|
|
struct alignas(os::ThreadStackAlignment) ThreadStack { u8 stack[ThreadStackSize]; };
|
|
|
|
auto thread_stack = std::make_unique<ThreadStack>();
|
|
|
|
|
|
|
|
// Create and start initialisation thread
|
|
|
|
R_ABORT_UNLESS(os::CreateThread(&init_thread,
|
2021-10-16 15:15:56 -07:00
|
|
|
InitializeThreadFunc,
|
|
|
|
nullptr,
|
2023-06-30 23:59:07 +02:00
|
|
|
thread_stack.get(),
|
2022-02-08 07:14:07 +01:00
|
|
|
ThreadStackSize,
|
|
|
|
ThreadPriority
|
2021-01-26 04:02:15 +01:00
|
|
|
));
|
2023-06-30 23:59:07 +02:00
|
|
|
os::SetThreadNamePointer(&init_thread, "mc::InitThread");
|
|
|
|
os::StartThread(&init_thread);
|
2021-01-26 04:02:15 +01:00
|
|
|
|
2023-06-30 23:59:07 +02:00
|
|
|
// Launch mitm and other modules
|
2021-02-21 12:51:00 +01:00
|
|
|
R_ABORT_UNLESS(ams::mitm::bluetooth::Launch());
|
|
|
|
R_ABORT_UNLESS(ams::mitm::btm::Launch());
|
2021-08-24 01:20:35 +02:00
|
|
|
R_ABORT_UNLESS(ams::mitm::mc::Launch());
|
2023-04-11 15:56:48 +02:00
|
|
|
R_ABORT_UNLESS(ams::usb::Launch());
|
2023-06-30 23:59:07 +02:00
|
|
|
|
|
|
|
// Wait for initialisation thread to finish and destroy it
|
|
|
|
os::WaitThread(&init_thread);
|
|
|
|
os::DestroyThread(&init_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitInitialized() {
|
|
|
|
g_init_event.Wait();
|
2021-02-21 12:51:00 +01:00
|
|
|
}
|
|
|
|
|
2022-04-09 01:12:59 +10:00
|
|
|
void WaitModules() {
|
2023-04-11 15:56:48 +02:00
|
|
|
ams::usb::WaitFinished();
|
2021-08-24 01:20:35 +02:00
|
|
|
ams::mitm::mc::WaitFinished();
|
2021-02-21 12:51:00 +01:00
|
|
|
ams::mitm::btm::WaitFinished();
|
|
|
|
ams::mitm::bluetooth::WaitFinished();
|
|
|
|
}
|
|
|
|
|
2021-01-26 04:02:15 +01:00
|
|
|
}
|