2020-09-11 17:04:01 +00:00
|
|
|
/*
|
2021-01-26 03:03:26 +00:00
|
|
|
* Copyright (c) 2020-2021 ndeadly
|
2020-09-11 17:04:01 +00:00
|
|
|
*
|
2021-01-26 03:03:26 +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.
|
2020-09-11 17:04:01 +00:00
|
|
|
*
|
2021-01-26 03:03:26 +00:00
|
|
|
* 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.
|
2020-09-11 17:04:01 +00:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-02-03 22:52:39 +00:00
|
|
|
#include "bluetoothmitm_module.hpp"
|
2020-09-11 17:04:01 +00:00
|
|
|
#include "btdrv_mitm_service.hpp"
|
2021-08-09 20:57:16 +00:00
|
|
|
#include "../utils.hpp"
|
2020-09-11 17:04:01 +00:00
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
2021-02-03 22:52:39 +00:00
|
|
|
namespace ams::mitm::bluetooth {
|
2020-09-11 17:04:01 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-02-03 22:21:59 +00:00
|
|
|
enum PortIndex {
|
|
|
|
PortIndex_BtdrvMitm,
|
|
|
|
PortIndex_Count,
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr sm::ServiceName BtdrvMitmServiceName = sm::ServiceName::Encode("btdrv");
|
2020-09-11 17:04:01 +00:00
|
|
|
|
|
|
|
struct ServerOptions {
|
|
|
|
static constexpr size_t PointerBufferSize = 0x1000;
|
|
|
|
static constexpr size_t MaxDomains = 0;
|
|
|
|
static constexpr size_t MaxDomainObjects = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr size_t MaxSessions = 6;
|
|
|
|
|
2021-02-03 22:21:59 +00:00
|
|
|
class ServerManager final : public sf::hipc::ServerManager<PortIndex_Count, ServerOptions, MaxSessions> {
|
|
|
|
private:
|
|
|
|
virtual Result OnNeedsToAccept(int port_index, Server *server) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
ServerManager g_server_manager;
|
|
|
|
|
|
|
|
Result ServerManager::OnNeedsToAccept(int port_index, Server *server) {
|
|
|
|
/* Acknowledge the mitm session. */
|
|
|
|
std::shared_ptr<::Service> fsrv;
|
|
|
|
sm::MitmProcessInfo client_info;
|
|
|
|
server->AcknowledgeMitmSession(std::addressof(fsrv), std::addressof(client_info));
|
|
|
|
|
|
|
|
switch (port_index) {
|
|
|
|
case PortIndex_BtdrvMitm:
|
|
|
|
return this->AcceptMitmImpl(server, sf::CreateSharedObjectEmplaced<IBtdrvMitmInterface, BtdrvMitmService>(decltype(fsrv)(fsrv), client_info), fsrv);
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:04:01 +00:00
|
|
|
os::ThreadType g_btdrv_mitm_thread;
|
2020-09-11 20:14:19 +00:00
|
|
|
alignas(os::ThreadStackAlignment) u8 g_btdrv_mitm_thread_stack[0x2000];
|
2020-09-27 13:46:21 +00:00
|
|
|
s32 g_btdrv_mitm_thread_priority = utils::ConvertToUserPriority(17);
|
2020-09-11 17:04:01 +00:00
|
|
|
|
2020-09-11 20:03:41 +00:00
|
|
|
void BtdrvMitmThreadFunction(void *arg) {
|
2021-02-03 22:21:59 +00:00
|
|
|
R_ABORT_UNLESS((g_server_manager.RegisterMitmServer<BtdrvMitmService>(PortIndex_BtdrvMitm, BtdrvMitmServiceName)));
|
|
|
|
g_server_manager.LoopProcess();
|
2020-09-11 20:03:41 +00:00
|
|
|
}
|
2020-09-11 17:04:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-11 20:03:41 +00:00
|
|
|
Result Launch(void) {
|
|
|
|
R_TRY(os::CreateThread(&g_btdrv_mitm_thread,
|
2020-09-11 17:04:01 +00:00
|
|
|
BtdrvMitmThreadFunction,
|
|
|
|
nullptr,
|
|
|
|
g_btdrv_mitm_thread_stack,
|
|
|
|
sizeof(g_btdrv_mitm_thread_stack),
|
|
|
|
g_btdrv_mitm_thread_priority
|
2020-09-11 20:03:41 +00:00
|
|
|
));
|
2020-09-11 17:04:01 +00:00
|
|
|
|
|
|
|
os::StartThread(&g_btdrv_mitm_thread);
|
2020-09-11 20:03:41 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
2020-09-11 17:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaitFinished(void) {
|
|
|
|
os::WaitThread(&g_btdrv_mitm_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|