2021-01-26 03:02:15 +00:00
|
|
|
/*
|
2024-01-26 09:23:42 +00:00
|
|
|
* Copyright (c) 2020-2024 ndeadly
|
2021-01-26 03:02:15 +00: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-20 23:16:23 +00:00
|
|
|
#include "mcmitm_config.hpp"
|
2022-03-30 08:27:24 +00:00
|
|
|
#include "async/async.hpp"
|
2021-02-20 23:16:23 +00:00
|
|
|
#include "bluetooth_mitm/btdrv_mitm_service.hpp"
|
2021-02-21 11:51:00 +00:00
|
|
|
#include "bluetooth_mitm/bluetoothmitm_module.hpp"
|
|
|
|
#include "btm_mitm/btmmitm_module.hpp"
|
2021-08-23 23:20:35 +00:00
|
|
|
#include "mc/mc_module.hpp"
|
2021-01-26 03:02:15 +00: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 06:14:07 +00:00
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_hid_report.hpp"
|
2021-01-26 03:02:15 +00:00
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_ble.hpp"
|
2023-04-11 13:56:48 +00:00
|
|
|
#include "usb/mc_usb_handler.hpp"
|
2021-10-16 22:15:56 +00:00
|
|
|
|
2021-01-26 03:02:15 +00:00
|
|
|
namespace ams::mitm {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
os::Event g_init_event(os::EventClearMode_ManualClear);
|
|
|
|
|
2023-07-26 19:55:28 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:15:56 +00:00
|
|
|
void InitializeThreadFunc(void *) {
|
2022-03-30 08:27:24 +00:00
|
|
|
// Start async worker thread(s)
|
|
|
|
ams::async::Initialize();
|
|
|
|
|
2021-02-23 00:41:49 +00:00
|
|
|
// Start bluetooth event handling thread
|
|
|
|
ams::bluetooth::events::Initialize();
|
|
|
|
|
2022-02-08 06:14:07 +00:00
|
|
|
// Start hid report handling thread
|
|
|
|
ams::bluetooth::hid::report::Initialize();
|
|
|
|
|
2021-02-23 00:41:49 +00:00
|
|
|
// Wait for system to call BluetoothEnable
|
|
|
|
ams::bluetooth::core::WaitEnabled();
|
2021-01-26 03:02:15 +00:00
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
// Connect to btdrv service now that we're sure the mitm is up and running
|
2021-04-30 21:05:01 +00:00
|
|
|
R_ABORT_UNLESS(btdrvInitialize());
|
2021-01-26 03:02:15 +00:00
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
// Get global module settings
|
|
|
|
auto config = GetGlobalConfig();
|
2021-01-26 03:02:15 +00:00
|
|
|
|
2021-02-20 23:16:23 +00: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) {
|
2023-07-26 19:55:28 +00:00
|
|
|
R_ABORT_UNLESS(OverrideHostAddress(&config->bluetooth.host_address));
|
2021-02-20 23:16:23 +00:00
|
|
|
}
|
2021-01-26 03:02:15 +00:00
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
// Set bluetooth adapter host name override
|
|
|
|
if (std::strlen(config->bluetooth.host_name) > 0) {
|
2023-07-26 19:55:28 +00:00
|
|
|
R_ABORT_UNLESS(OverrideHostName(config->bluetooth.host_name));
|
2021-02-20 23:16:23 +00:00
|
|
|
}
|
2021-01-26 03:02:15 +00:00
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
g_init_event.Signal();
|
2023-04-11 13:56:48 +00:00
|
|
|
|
|
|
|
// Loop until we can initialise btm:sys
|
|
|
|
while (R_FAILED(btmsysInitialize())) {
|
|
|
|
os::SleepThread(ams::TimeSpan::FromMilliSeconds(200));
|
|
|
|
}
|
2023-04-24 20:28:24 +00:00
|
|
|
|
|
|
|
// Loop until we can initialise btm
|
|
|
|
while (R_FAILED(btmInitialize())) {
|
|
|
|
os::SleepThread(ams::TimeSpan::FromMilliSeconds(200));
|
|
|
|
}
|
2021-01-26 03:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-26 19:55:28 +00:00
|
|
|
void LaunchModules() {
|
2023-11-16 13:33:08 +00:00
|
|
|
constexpr s32 ThreadPriority = -7;
|
|
|
|
constexpr size_t ThreadStackSize = 0x1000;
|
2023-06-30 21:59:07 +00:00
|
|
|
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>();
|
|
|
|
|
2023-07-26 19:55:28 +00:00
|
|
|
// Create and start background initialisation thread
|
2023-06-30 21:59:07 +00:00
|
|
|
R_ABORT_UNLESS(os::CreateThread(&init_thread,
|
2021-10-16 22:15:56 +00:00
|
|
|
InitializeThreadFunc,
|
|
|
|
nullptr,
|
2023-06-30 21:59:07 +00:00
|
|
|
thread_stack.get(),
|
2022-02-08 06:14:07 +00:00
|
|
|
ThreadStackSize,
|
|
|
|
ThreadPriority
|
2021-01-26 03:02:15 +00:00
|
|
|
));
|
2023-06-30 21:59:07 +00:00
|
|
|
os::SetThreadNamePointer(&init_thread, "mc::InitThread");
|
|
|
|
os::StartThread(&init_thread);
|
2021-01-26 03:02:15 +00:00
|
|
|
|
2023-07-26 19:55:28 +00:00
|
|
|
// Launch IPC servers
|
|
|
|
ams::mitm::bluetooth::Launch();
|
|
|
|
ams::mitm::btm::Launch();
|
|
|
|
ams::mc::Launch();
|
2023-06-30 21:59:07 +00:00
|
|
|
|
2023-07-26 19:55:28 +00:00
|
|
|
// Launch additional modules
|
|
|
|
ams::usb::Launch();
|
|
|
|
|
|
|
|
// Wait for initialisation thread to terminate
|
2023-06-30 21:59:07 +00:00
|
|
|
os::WaitThread(&init_thread);
|
|
|
|
os::DestroyThread(&init_thread);
|
|
|
|
}
|
|
|
|
|
2022-04-08 15:12:59 +00:00
|
|
|
void WaitModules() {
|
2023-04-11 13:56:48 +00:00
|
|
|
ams::usb::WaitFinished();
|
2023-07-26 19:55:28 +00:00
|
|
|
ams::mc::WaitFinished();
|
2021-02-21 11:51:00 +00:00
|
|
|
ams::mitm::btm::WaitFinished();
|
|
|
|
ams::mitm::bluetooth::WaitFinished();
|
|
|
|
}
|
|
|
|
|
2023-07-26 19:55:28 +00:00
|
|
|
void WaitInitialized() {
|
|
|
|
g_init_event.Wait();
|
|
|
|
}
|
|
|
|
|
2021-01-26 03:02:15 +00:00
|
|
|
}
|