2021-01-26 03:02:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021 ndeadly
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "bluetooth_mitm/btdrv_mitm_service.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"
|
|
|
|
#include "bluetooth_mitm/bluetooth/bluetooth_ble.hpp"
|
|
|
|
|
|
|
|
namespace ams::mitm {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
constexpr size_t InitializeThreadStackSize = 0x1000;
|
2021-01-26 03:02:15 +00:00
|
|
|
|
|
|
|
os::ThreadType g_initialize_thread;
|
|
|
|
alignas(os::ThreadStackAlignment) u8 g_initialize_thread_stack[InitializeThreadStackSize];
|
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
os::Event g_init_event(os::EventClearMode_ManualClear);
|
|
|
|
|
2021-01-26 03:02:15 +00:00
|
|
|
void WaitInterfacesInitialized(void) {
|
2021-02-20 23:16:23 +00:00
|
|
|
while (!(ams::bluetooth::core::IsInitialized() && ams::bluetooth::hid::IsInitialized() && (ams::bluetooth::ble::IsInitialized() || (hos::GetVersion() < hos::Version_5_0_0)))) {
|
2021-01-26 03:02:15 +00:00
|
|
|
svc::SleepThread(1'000'000ul);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
void InitializeThreadFunc(void *arg) {
|
2021-01-26 03:02:15 +00:00
|
|
|
// Wait for all bluetooth interfaces to be initialised
|
|
|
|
WaitInterfacesInitialized();
|
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
// Connect to btdrv service now that we're sure the mitm is up and running
|
2021-01-26 03:02:15 +00:00
|
|
|
sm::DoWithSession([&]() {
|
|
|
|
R_ABORT_UNLESS(btdrvInitialize());
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start bluetooth event handling thread
|
2021-02-20 23:16:23 +00:00
|
|
|
ams::bluetooth::events::Initialize();
|
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
|
|
|
// Wait for system to call BluetoothEnable so the proceeding adapter properties aren't overwritten
|
|
|
|
ams::bluetooth::core::WaitEnabled();
|
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) {
|
|
|
|
R_ABORT_UNLESS(btdrvSetAdapterProperty(BtdrvBluetoothPropertyType_Address, &config->bluetooth.host_address, sizeof(ams::bluetooth::Address)));
|
|
|
|
}
|
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) {
|
|
|
|
R_ABORT_UNLESS(btdrvSetAdapterProperty(BtdrvBluetoothPropertyType_Name, config->bluetooth.host_name, std::strlen(config->bluetooth.host_name)));
|
|
|
|
}
|
2021-01-26 03:02:15 +00:00
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
g_init_event.Signal();
|
2021-01-26 03:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartInitialize(void) {
|
|
|
|
R_ABORT_UNLESS(os::CreateThread(&g_initialize_thread,
|
|
|
|
InitializeThreadFunc,
|
|
|
|
nullptr,
|
|
|
|
g_initialize_thread_stack,
|
|
|
|
sizeof(g_initialize_thread_stack),
|
2021-02-20 23:16:23 +00:00
|
|
|
-7
|
2021-01-26 03:02:15 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
os::StartThread(&g_initialize_thread);
|
|
|
|
}
|
|
|
|
|
2021-02-20 23:16:23 +00:00
|
|
|
void WaitInitialized(void) {
|
|
|
|
g_init_event.Wait();
|
|
|
|
}
|
|
|
|
|
2021-01-26 03:02:15 +00:00
|
|
|
}
|