btdrv-mitm: refactor event handling code to make use of namespaces

This commit is contained in:
ndeadly 2020-06-13 19:15:59 +02:00
parent 417a7a6f85
commit aedca57a3e
16 changed files with 594 additions and 348 deletions

View file

@ -3,49 +3,73 @@
#include "../btdrv_mitm_logging.hpp"
ams::os::ThreadType g_bt_ble_event_task_thread;
alignas(ams::os::ThreadStackAlignment) u8 g_bt_ble_event_task_stack[0x2000];
namespace ams::bluetooth::ble {
namespace {
os::ThreadType g_eventHandlerThread;
alignas(os::ThreadStackAlignment) u8 g_eventHandlerThreadStack[0x2000];
//u8 g_bt_ble_event_data_buffer[0x400];
//BluetoothEventType g_current_bt_ble_event_type;
ams::os::SystemEventType g_btBleSystemEvent;
ams::os::SystemEventType g_btBleSystemEventFwd;
ams::os::SystemEventType g_btBleSystemEventUser;
os::SystemEventType g_btBleSystemEvent;
os::SystemEventType g_btBleSystemEventFwd;
os::SystemEventType g_btBleSystemEventUser;
}
os::SystemEventType *GetSystemEvent(void) {
return &g_btBleSystemEvent;
}
os::SystemEventType *GetForwardEvent(void) {
return &g_btBleSystemEventFwd;
}
os::SystemEventType *GetUserForwardEvent(void) {
return &g_btBleSystemEventUser;
}
void HandleEvent(void) {
BTDRV_LOG_FMT("ble event fired");
// Signal our forwarder events
if (!g_redirectEvents || g_preparingForSleep)
os::SignalSystemEvent(&g_btBleSystemEventFwd);
else
os::SignalSystemEvent(&g_btBleSystemEventUser);
}
void BluetoothBleEventThreadFunc(void *arg) {
while (true) {
// Wait for real bluetooth event
ams::os::WaitSystemEvent(&g_btBleSystemEvent);
os::WaitSystemEvent(&g_btBleSystemEvent);
BTDRV_LOG_FMT("ble event fired");
// Signal our forwarder events
if (!g_redirectEvents || g_preparingForSleep)
ams::os::SignalSystemEvent(&g_btBleSystemEventFwd);
else
ams::os::SignalSystemEvent(&g_btBleSystemEventUser);
HandleEvent();
}
}
ams::Result InitializeBluetoothBleEvents(void) {
R_TRY(ams::os::CreateSystemEvent(&g_btBleSystemEventFwd, ams::os::EventClearMode_AutoClear, true));
R_TRY(ams::os::CreateSystemEvent(&g_btBleSystemEventUser, ams::os::EventClearMode_AutoClear, true));
Result InitializeEvents(void) {
R_TRY(os::CreateSystemEvent(&g_btBleSystemEventFwd, os::EventClearMode_AutoClear, true));
R_TRY(os::CreateSystemEvent(&g_btBleSystemEventUser, os::EventClearMode_AutoClear, true));
return ams::ResultSuccess();
}
ams::Result StartBluetoothBleEventThread(void) {
R_TRY(ams::os::CreateThread(&g_bt_ble_event_task_thread,
Result StartEventHandlerThread(void) {
R_TRY(os::CreateThread(&g_eventHandlerThread,
BluetoothBleEventThreadFunc,
nullptr,
g_bt_ble_event_task_stack,
sizeof(g_bt_ble_event_task_stack),
g_eventHandlerThreadStack,
sizeof(g_eventHandlerThreadStack),
9
//38 // priority of btm sysmodule + 1
));
ams::os::StartThread(&g_bt_ble_event_task_thread);
os::StartThread(&g_eventHandlerThread);
return ams::ResultSuccess();
}
}

View file

@ -2,10 +2,15 @@
#include <switch.h>
#include <stratosphere.hpp>
extern ams::os::SystemEventType g_btBleSystemEvent;
extern ams::os::SystemEventType g_btBleSystemEventFwd;
extern ams::os::SystemEventType g_btBleSystemEventUser;
namespace ams::bluetooth::ble {
ams::Result InitializeBluetoothBleEvents(void);
ams::Result StartBluetoothBleEventThread(void);
void HandleEvent(void);
os::SystemEventType *GetSystemEvent(void);
os::SystemEventType *GetForwardEvent(void);
os::SystemEventType *GetUserForwardEvent(void);
Result InitializeEvents(void);
Result StartEventHandlerThread(void);
}

View file

@ -3,49 +3,72 @@
#include "../btdrv_mitm_logging.hpp"
ams::os::ThreadType g_bt_event_task_thread;
alignas(ams::os::ThreadStackAlignment) u8 g_bt_event_task_stack[0x2000];
//u8 g_bt_event_data_buffer[0x400];
//BluetoothEventType g_current_bt_event_type;
namespace ams::bluetooth::core {
ams::os::SystemEventType g_btSystemEvent;
ams::os::SystemEventType g_btSystemEventFwd;
ams::os::SystemEventType g_btSystemEventUser;
namespace {
os::ThreadType g_eventHandlerThread;
alignas(os::ThreadStackAlignment) u8 g_eventHandlerThreadStack[0x2000];
//u8 g_eventDataBuffer[0x400];
//BluetoothEventType g_currentEventType;
void BluetoothEventThreadFunc(void *arg) {
while (true) {
// Wait for real bluetooth event
ams::os::WaitSystemEvent(&g_btSystemEvent);
os::SystemEventType g_btSystemEvent;
os::SystemEventType g_btSystemEventFwd;
os::SystemEventType g_btSystemEventUser;
}
os::SystemEventType *GetSystemEvent(void) {
return &g_btSystemEvent;
}
os::SystemEventType *GetForwardEvent(void) {
return &g_btSystemEventFwd;
}
os::SystemEventType *GetUserForwardEvent(void) {
return &g_btSystemEventUser;
}
void HandleEvent(void) {
BTDRV_LOG_FMT("bluetooth event fired");
// Signal our forwarder events
if (!g_redirectEvents || g_preparingForSleep)
ams::os::SignalSystemEvent(&g_btSystemEventFwd);
os::SignalSystemEvent(&g_btSystemEventFwd);
else
ams::os::SignalSystemEvent(&g_btSystemEventUser);
os::SignalSystemEvent(&g_btSystemEventUser);
}
void BluetoothEventThreadFunc(void *arg) {
while (true) {
// Wait for real bluetooth event
os::WaitSystemEvent(&g_btSystemEvent);
HandleEvent();
}
}
ams::Result InitializeBluetoothCoreEvents(void) {
R_TRY(ams::os::CreateSystemEvent(&g_btSystemEventFwd, ams::os::EventClearMode_AutoClear, true));
R_TRY(ams::os::CreateSystemEvent(&g_btSystemEventUser, ams::os::EventClearMode_AutoClear, true));
Result InitializeEvents(void) {
R_TRY(os::CreateSystemEvent(&g_btSystemEventFwd, os::EventClearMode_AutoClear, true));
R_TRY(os::CreateSystemEvent(&g_btSystemEventUser, os::EventClearMode_AutoClear, true));
return ams::ResultSuccess();
}
ams::Result StartBluetoothCoreEventThread(void) {
R_TRY(ams::os::CreateThread(&g_bt_event_task_thread,
Result StartEventHandlerThread(void) {
R_TRY(os::CreateThread(&g_eventHandlerThread,
BluetoothEventThreadFunc,
nullptr,
g_bt_event_task_stack,
sizeof(g_bt_event_task_stack),
g_eventHandlerThreadStack,
sizeof(g_eventHandlerThreadStack),
9
//37 // priority of btm sysmodule
));
ams::os::StartThread(&g_bt_event_task_thread);
os::StartThread(&g_eventHandlerThread);
return ams::ResultSuccess();
}
}

View file

@ -2,9 +2,15 @@
#include <switch.h>
#include <stratosphere.hpp>
extern ams::os::SystemEventType g_btSystemEvent;
extern ams::os::SystemEventType g_btSystemEventFwd;
extern ams::os::SystemEventType g_btSystemEventUser;
namespace ams::bluetooth::core {
ams::Result InitializeBluetoothCoreEvents(void);
ams::Result StartBluetoothCoreEventThread(void);
void HandleEvent(void);
os::SystemEventType *GetSystemEvent(void);
os::SystemEventType *GetForwardEvent(void);
os::SystemEventType *GetUserForwardEvent(void);
Result InitializeEvents(void);
Result StartEventHandlerThread(void);
}

View file

@ -0,0 +1,94 @@
#include <switch.h>
#include "bluetooth_events.hpp"
#include "../pscpm_module.hpp"
namespace ams::bluetooth::events {
namespace {
os::ThreadType g_eventHandlerThread;
alignas(os::ThreadStackAlignment) u8 g_eventHandlerThreadStack[0x2000];
os::WaitableManagerType g_manager;
os::WaitableHolderType g_holderPscPm;
os::WaitableHolderType g_holderBtCore;
os::WaitableHolderType g_holderBtHid;
os::WaitableHolderType g_holderBtBle;
}
void EventHandlerThreadFunc(void *arg) {
while (true) {
auto signalled_holder = os::WaitAny(&g_manager);
switch (os::GetWaitableHolderUserData(signalled_holder)) {
case BtdrvEventType_PscPm:
mitm::btdrv::handlePscPmEvent();
break;
case BtdrvEventType_BluetoothCore:
core::HandleEvent();
break;
case BtdrvEventType_BluetoothHid:
hid::HandleEvent();
break;
case BtdrvEventType_BluetoothBle:
ble::HandleEvent();
break;
default:
break;
}
}
}
void AttachWaitHolder(BtdrvEventType type) {
switch(type) {
case BtdrvEventType_PscPm:
os::InitializeWaitableHolder(&g_holderPscPm, mitm::btdrv::GetPscPmModule()->GetEventPointer()->GetBase());
os::SetWaitableHolderUserData(&g_holderPscPm, BtdrvEventType_PscPm);
os::LinkWaitableHolder(&g_manager, &g_holderPscPm);
break;
case BtdrvEventType_BluetoothCore:
os::InitializeWaitableHolder(&g_holderBtCore, core::GetSystemEvent());
os::SetWaitableHolderUserData(&g_holderBtCore, BtdrvEventType_BluetoothCore);
os::LinkWaitableHolder(&g_manager, &g_holderBtCore);
break;
case BtdrvEventType_BluetoothHid:
os::InitializeWaitableHolder(&g_holderBtHid, hid::GetSystemEvent());
os::SetWaitableHolderUserData(&g_holderBtHid, BtdrvEventType_BluetoothHid);
os::LinkWaitableHolder(&g_manager, &g_holderBtHid);
break;
case BtdrvEventType_BluetoothBle:
os::InitializeWaitableHolder(&g_holderBtBle, ble::GetSystemEvent());
os::SetWaitableHolderUserData(&g_holderBtBle, BtdrvEventType_BluetoothBle);
os::LinkWaitableHolder(&g_manager, &g_holderBtBle);
break;
default:
return;
}
}
Result InitializeSystemEvents(void) {
//R_TRY(InitializePscPmModule());
R_TRY(core::InitializeEvents());
R_TRY(hid::InitializeEvents());
R_TRY(ble::InitializeEvents());
os::InitializeWaitableManager(&g_manager);
return ams::ResultSuccess();
}
Result StartEventHandlerThread(void) {
R_TRY(os::CreateThread(&g_eventHandlerThread,
EventHandlerThreadFunc,
nullptr,
g_eventHandlerThreadStack,
sizeof(g_eventHandlerThreadStack),
9
));
os::StartThread(&g_eventHandlerThread);
return ams::ResultSuccess();
}
}

View file

@ -1,6 +1,22 @@
#pragma once
#include <stratosphere.hpp>
#include "bluetooth_core_events.hpp"
#include "bluetooth_hid_events.hpp"
#include "bluetooth_hid_report_events.hpp"
#include "bluetooth_ble_events.hpp"
namespace ams::bluetooth::events {
enum BtdrvEventType {
BtdrvEventType_PscPm,
BtdrvEventType_BluetoothCore,
BtdrvEventType_BluetoothHid,
BtdrvEventType_BluetoothBle,
};
void AttachWaitHolder(BtdrvEventType type);
Result InitializeSystemEvents(void);
Result StartEventHandlerThread(void);
}

View file

@ -4,15 +4,32 @@
#include "../btdrv_mitm_logging.hpp"
ams::os::ThreadType g_bt_hid_event_task_thread;
alignas(ams::os::ThreadStackAlignment) u8 g_bt_hid_event_task_stack[0x2000];
u8 g_bt_hid_event_data_buffer[0x480];
HidEventType g_current_bt_hid_event_type;
namespace ams::bluetooth::hid {
ams::os::SystemEventType g_btHidSystemEvent;
ams::os::SystemEventType g_btHidSystemEventFwd;
ams::os::SystemEventType g_btHidSystemEventUser;
namespace {
os::ThreadType g_eventHandlerThread;
alignas(os::ThreadStackAlignment) u8 g_eventHandlerThreadStack[0x2000];
u8 g_eventDataBuffer[0x480];
HidEventType g_currentEventType;
os::SystemEventType g_btHidSystemEvent;
os::SystemEventType g_btHidSystemEventFwd;
os::SystemEventType g_btHidSystemEventUser;
}
os::SystemEventType *GetSystemEvent(void) {
return &g_btHidSystemEvent;
}
os::SystemEventType *GetForwardEvent(void) {
return &g_btHidSystemEventFwd;
}
os::SystemEventType *GetUserForwardEvent(void) {
return &g_btHidSystemEventUser;
}
void handleConnectionStateEvent(HidEventData *eventData) {
switch (eventData->connectionState.state) {
@ -30,19 +47,14 @@ void handleConnectionStateEvent(HidEventData *eventData) {
BTDRV_LOG_DATA(&eventData->connectionState.address, sizeof(BluetoothAddress));
}
void BluetoothHidEventThreadFunc(void *arg) {
HidEventData *eventData = reinterpret_cast<HidEventData *>(g_bt_hid_event_data_buffer);
while (true) {
// Wait for real bluetooth event
ams::os::WaitSystemEvent(&g_btHidSystemEvent);
void HandleEvent(void) {
BTDRV_LOG_FMT("hid event fired");
R_ABORT_UNLESS(btdrvGetHidEventInfo(&g_current_bt_hid_event_type, g_bt_hid_event_data_buffer, sizeof(g_bt_hid_event_data_buffer)));
HidEventData *eventData = reinterpret_cast<HidEventData *>(g_eventDataBuffer);
switch (g_current_bt_hid_event_type) {
R_ABORT_UNLESS(btdrvGetHidEventInfo(&g_currentEventType, g_eventDataBuffer, sizeof(g_eventDataBuffer)));
switch (g_currentEventType) {
case HidEvent_ConnectionState:
handleConnectionStateEvent(eventData);
break;
@ -54,32 +66,42 @@ void BluetoothHidEventThreadFunc(void *arg) {
//os::SignalSystemEvent(&g_btHidSystemEventFwd);
if (!g_redirectEvents || g_preparingForSleep) {
ams::os::SignalSystemEvent(&g_btHidSystemEventFwd);
os::SignalSystemEvent(&g_btHidSystemEventFwd);
}
else {
ams::os::SignalSystemEvent(&g_btHidSystemEventUser);
}
os::SignalSystemEvent(&g_btHidSystemEventUser);
}
}
ams::Result InitializeBluetoothHidEvents(void) {
R_TRY(ams::os::CreateSystemEvent(&g_btHidSystemEventFwd, ams::os::EventClearMode_AutoClear, true));
R_TRY(ams::os::CreateSystemEvent(&g_btHidSystemEventUser, ams::os::EventClearMode_AutoClear, true));
void BluetoothHidEventThreadFunc(void *arg) {
while (true) {
// Wait for real bluetooth event
os::WaitSystemEvent(&g_btHidSystemEvent);
HandleEvent();
}
}
Result InitializeEvents(void) {
R_TRY(os::CreateSystemEvent(&g_btHidSystemEventFwd, os::EventClearMode_AutoClear, true));
R_TRY(os::CreateSystemEvent(&g_btHidSystemEventUser, os::EventClearMode_AutoClear, true));
return ams::ResultSuccess();
}
ams::Result StartBluetoothHidEventThread(void) {
R_TRY(ams::os::CreateThread(&g_bt_hid_event_task_thread,
Result StartEventHandlerThread(void) {
R_TRY(os::CreateThread(&g_eventHandlerThread,
BluetoothHidEventThreadFunc,
nullptr,
g_bt_hid_event_task_stack,
sizeof(g_bt_hid_event_task_stack),
g_eventHandlerThreadStack,
sizeof(g_eventHandlerThreadStack),
9
//38 // priority of btm sysmodule + 1
));
ams::os::StartThread(&g_bt_hid_event_task_thread);
os::StartThread(&g_eventHandlerThread);
return ams::ResultSuccess();
}
}

View file

@ -2,9 +2,15 @@
#include <switch.h>
#include <stratosphere.hpp>
extern ams::os::SystemEventType g_btHidSystemEvent;
extern ams::os::SystemEventType g_btHidSystemEventFwd;
extern ams::os::SystemEventType g_btHidSystemEventUser;
namespace ams::bluetooth::hid {
ams::Result InitializeBluetoothHidEvents(void);
ams::Result StartBluetoothHidEventThread(void);
void HandleEvent(void);
os::SystemEventType *GetSystemEvent(void);
os::SystemEventType *GetForwardEvent(void);
os::SystemEventType *GetUserForwardEvent(void);
Result InitializeEvents(void);
Result StartEventHandlerThread(void);
}

View file

@ -1,4 +1,5 @@
#include "bluetooth_hid_report_events.hpp"
#include "../circularbuffer.hpp"
#include "../btdrv_mitm_flags.hpp"
#include "../controllermanager.hpp"
#include "../controllers/bluetoothcontroller.hpp"
@ -6,25 +7,75 @@
#include "../btdrv_mitm_logging.hpp"
ams::os::ThreadType g_bt_hid_report_event_task_thread;
alignas(ams::os::ThreadStackAlignment) u8 g_bt_hid_report_event_task_stack[0x2000];
namespace ams::bluetooth::hid::report {
namespace {
os::ThreadType g_eventHandlerThread;
alignas(os::ThreadStackAlignment) u8 g_eventHandlerThreadStack[0x2000];
SharedMemory g_realBtShmem;
SharedMemory g_fakeBtShmem;
ams::bluetooth::CircularBuffer *g_realCircBuff;
ams::bluetooth::CircularBuffer *g_fakeCircBuff;
bluetooth::CircularBuffer *g_realCircBuff;
bluetooth::CircularBuffer *g_fakeCircBuff;
ams::os::SystemEventType g_btHidReportSystemEvent;
ams::os::SystemEventType g_btHidReportSystemEventFwd;
ams::os::SystemEventType g_btHidReportSystemEventUser;
os::SystemEventType g_btHidReportSystemEvent;
os::SystemEventType g_btHidReportSystemEventFwd;
os::SystemEventType g_btHidReportSystemEventUser;
u8 g_fakeReportBuffer[0x42] = {};
HidReportData *g_fakeReportData = reinterpret_cast<HidReportData *>(g_fakeReportBuffer);
ams::Result ProcessHidReportPackets(ams::bluetooth::CircularBuffer *realBuffer, ams::bluetooth::CircularBuffer *fakeBuffer) {
}
SharedMemory *GetRealSharedMemory(void) {
return &g_realBtShmem;
}
SharedMemory *GetFakeSharedMemory(void) {
return &g_fakeBtShmem;
}
os::SystemEventType *GetSystemEvent(void) {
return &g_btHidReportSystemEvent;
}
os::SystemEventType *GetForwardEvent(void) {
return &g_btHidReportSystemEventFwd;
}
os::SystemEventType *GetUserForwardEvent(void) {
return &g_btHidReportSystemEventUser;
}
Result MapRemoteSharedMemory(Handle handle) {
shmemLoadRemote(&g_realBtShmem, handle, BLUETOOTH_SHAREDMEM_SIZE, Perm_Rw);
R_TRY(shmemMap(&g_realBtShmem));
g_realCircBuff = reinterpret_cast<bluetooth::CircularBuffer *>(shmemGetAddr(&g_realBtShmem));
BTDRV_LOG_FMT("Real shmem @ 0x%p", (void *)g_realCircBuff);
return ams::ResultSuccess();
}
Result InitializeFakeSharedMemory(void) {
R_TRY(shmemCreate(&g_fakeBtShmem, BLUETOOTH_SHAREDMEM_SIZE, Perm_Rw, Perm_Rw));
R_TRY(shmemMap(&g_fakeBtShmem));
g_fakeCircBuff = reinterpret_cast<CircularBuffer *>(shmemGetAddr(&g_fakeBtShmem));
BTDRV_LOG_FMT("Fake shmem @ 0x%p", (void *)g_fakeCircBuff);
// Initialise fake hid report buffer
g_fakeCircBuff->Initialize("HID Report");
g_fakeCircBuff->id = 1;
g_fakeCircBuff->_unk3 = 1;
return ams::ResultSuccess();
}
Result ProcessHidReportPackets(CircularBuffer *realBuffer, CircularBuffer *fakeBuffer) {
controller::BluetoothController *controller;
ams::bluetooth::CircularBufferPacket *realPacket;
CircularBufferPacket *realPacket;
/*
BTDRV_LOG_FMT("realBuffer: name: %s, roffs: %d, woffs: %d, capacity: %d, _unk3: %d, id: %d",
@ -53,13 +104,13 @@ ams::Result ProcessHidReportPackets(ams::bluetooth::CircularBuffer *realBuffer,
// Get packet from real buffer
//realPacket = reinterpret_cast<bluetooth::CircularBufferPacket *>(realBuffer->_read());
realPacket = reinterpret_cast<ams::bluetooth::CircularBufferPacket *>(&realBuffer->data[realBuffer->readOffset]);
realPacket = reinterpret_cast<CircularBufferPacket *>(&realBuffer->data[realBuffer->readOffset]);
if (!realPacket)
break;
// Move read pointer past current packet (I think this is what Free does)
if (realBuffer->readOffset != writeOffset) {
u32 newOffset = realBuffer->readOffset + realPacket->header.size + sizeof(ams::bluetooth::CircularBufferPacketHeader);
u32 newOffset = realBuffer->readOffset + realPacket->header.size + sizeof(CircularBufferPacketHeader);
if (newOffset >= BLUETOOTH_CIRCBUFFER_SIZE)
newOffset = 0;
@ -80,7 +131,7 @@ ams::Result ProcessHidReportPackets(ams::bluetooth::CircularBuffer *realBuffer,
case 4:
{
// Locate the controller that sent the report
controller = ams::mitm::btdrv::locateController(ams::hos::GetVersion() < ams::hos::Version_9_0_0 ? &realPacket->data.address : &realPacket->data.v2.address);
controller = ams::mitm::btdrv::locateController(hos::GetVersion() < hos::Version_9_0_0 ? &realPacket->data.address : &realPacket->data.v2.address);
if (!controller) {
continue;
}
@ -93,7 +144,7 @@ ams::Result ProcessHidReportPackets(ams::bluetooth::CircularBuffer *realBuffer,
const HidReport *inReport;
HidReport *outReport;
// copy address and stuff over
if (ams::hos::GetVersion() < ams::hos::Version_10_0_0) {
if (hos::GetVersion() < hos::Version_10_0_0) {
g_fakeReportData->size = 0; // Todo: calculate size of report data
std::memcpy(&g_fakeReportData->address, &realPacket->data.address, sizeof(BluetoothAddress));
inReport = &realPacket->data.report;
@ -106,7 +157,7 @@ ams::Result ProcessHidReportPackets(ams::bluetooth::CircularBuffer *realBuffer,
}
auto switchData = reinterpret_cast<controller::SwitchReportData *>(&outReport->data);
switchData->report0x30.timer = ams::os::ConvertToTimeSpan(realPacket->header.timestamp).GetMilliSeconds() & 0xff;
switchData->report0x30.timer = os::ConvertToTimeSpan(realPacket->header.timestamp).GetMilliSeconds() & 0xff;
// Translate packet to switch pro format
controller->convertReportFormat(inReport, outReport);
@ -129,6 +180,14 @@ ams::Result ProcessHidReportPackets(ams::bluetooth::CircularBuffer *realBuffer,
return ams::ResultSuccess();
}
void HandleEvent(void) {
ProcessHidReportPackets(g_realCircBuff, g_fakeCircBuff);
// Signal our forwarder events
//os::SignalSystemEvent(&btHidReportSystemEventUser);
os::SignalSystemEvent(&g_btHidReportSystemEventFwd);
}
void BluetoothHidReportEventThreadFunc(void *arg) {
/*
@ -140,15 +199,9 @@ void BluetoothHidReportEventThreadFunc(void *arg) {
while (true) {
// Wait for real bluetooth event
ams::os::WaitSystemEvent(&g_btHidReportSystemEvent);
os::WaitSystemEvent(&g_btHidReportSystemEvent);
ProcessHidReportPackets(g_realCircBuff, g_fakeCircBuff);
// Signal our forwarder events
//os::SignalSystemEvent(&btHidReportSystemEventUser);
ams::os::SignalSystemEvent(&g_btHidReportSystemEventFwd);
//BTDRV_LOG_FMT("wrote hid report packets");
HandleEvent();
}
/*
@ -159,38 +212,26 @@ void BluetoothHidReportEventThreadFunc(void *arg) {
*/
}
ams::Result InitializeBluetoothHidReportEvents(void) {
R_TRY(ams::os::CreateSystemEvent(&g_btHidReportSystemEventFwd, ams::os::EventClearMode_AutoClear, true));
R_TRY(ams::os::CreateSystemEvent(&g_btHidReportSystemEventUser, ams::os::EventClearMode_AutoClear, true));
ams::Result InitializeEvents(void) {
R_TRY(os::CreateSystemEvent(&g_btHidReportSystemEventFwd, os::EventClearMode_AutoClear, true));
R_TRY(os::CreateSystemEvent(&g_btHidReportSystemEventUser, os::EventClearMode_AutoClear, true));
return ams::ResultSuccess();
}
ams::Result InitializeBluetoothHidReportFakeSharedMemory(void) {
R_TRY(shmemCreate(&g_fakeBtShmem, BLUETOOTH_SHAREDMEM_SIZE, Perm_Rw, Perm_Rw));
R_TRY(shmemMap(&g_fakeBtShmem));
g_fakeCircBuff = reinterpret_cast<ams::bluetooth::CircularBuffer *>(shmemGetAddr(&g_fakeBtShmem));
BTDRV_LOG_FMT("Fake shmem @ 0x%p", (void *)g_fakeCircBuff);
// Initialise fake hid report buffer
g_fakeCircBuff->Initialize("HID Report");
g_fakeCircBuff->id = 1;
g_fakeCircBuff->_unk3 = 1;
return ams::ResultSuccess();
}
ams::Result StartBluetoothHidReportEventThread(void) {
R_TRY(ams::os::CreateThread(&g_bt_hid_report_event_task_thread,
ams::Result StartEventHandlerThread(void) {
R_TRY(os::CreateThread(&g_eventHandlerThread,
BluetoothHidReportEventThreadFunc,
nullptr,
g_bt_hid_report_event_task_stack,
sizeof(g_bt_hid_report_event_task_stack),
g_eventHandlerThreadStack,
sizeof(g_eventHandlerThreadStack),
-10
//18 // priority of hid sysmodule
));
ams::os::StartThread(&g_bt_hid_report_event_task_thread);
os::StartThread(&g_eventHandlerThread);
return ams::ResultSuccess();
}
}

View file

@ -2,18 +2,19 @@
#include <switch.h>
#include <stratosphere.hpp>
#include "../circularbuffer.hpp"
namespace ams::bluetooth::hid::report {
extern SharedMemory g_realBtShmem;
extern SharedMemory g_fakeBtShmem;
SharedMemory *GetRealSharedMemory(void);
SharedMemory *GetFakeSharedMemory(void);
extern ams::bluetooth::CircularBuffer *g_realCircBuff;
extern ams::bluetooth::CircularBuffer *g_fakeCircBuff;
os::SystemEventType *GetSystemEvent(void);
os::SystemEventType *GetForwardEvent(void);
os::SystemEventType *GetUserForwardEvent(void);
extern ams::os::SystemEventType g_btHidReportSystemEvent;
extern ams::os::SystemEventType g_btHidReportSystemEventFwd;
extern ams::os::SystemEventType g_btHidReportSystemEventUser;
Result MapRemoteSharedMemory(Handle handle);
Result InitializeFakeSharedMemory(void);
ams::Result InitializeBluetoothHidReportEvents(void);
ams::Result InitializeBluetoothHidReportFakeSharedMemory(void);
ams::Result StartBluetoothHidReportEventThread(void);
Result InitializeEvents(void);
Result StartEventHandlerThread(void);
}

View file

@ -1,7 +1,3 @@
#include <algorithm>
#include <atomic>
#include <memory>
#include <vector>
#include <cstring>
#include <switch.h>
@ -12,7 +8,6 @@
#include "bluetooth/bluetooth_events.hpp"
#include "controllermanager.hpp"
namespace ams::mitm::btdrv {
namespace {
@ -34,27 +29,35 @@ namespace ams::mitm::btdrv {
Handle handle = INVALID_HANDLE;
// Forward to the real bluetooth module with our event handle instead
R_ABORT_UNLESS(btdrvInitializeBluetoothFwd(this->forward_service.get(), &handle));
R_TRY(btdrvInitializeBluetoothFwd(this->forward_service.get(), &handle));
// Attach the handle to our real system event
os::AttachReadableHandleToSystemEvent(&g_btSystemEvent, handle, false, os::EventClearMode_AutoClear);
os::AttachReadableHandleToSystemEvent(bluetooth::core::GetSystemEvent(),
handle,
false,
os::EventClearMode_AutoClear
);
// Create forwarder events
R_ABORT_UNLESS(InitializeBluetoothCoreEvents());
// Create forwarder eventsg
//R_ABORT_UNLESS(bluetooth::core::InitializeEvents());
R_TRY(bluetooth::core::InitializeEvents());
//bluetooth::events::AttachWaitHolder(BtdrvEventType_BluetoothCore);
// Set callers handle to that of our forwarder event
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btSystemEventFwd));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::core::GetForwardEvent()));
// Create and map fake bluetooth hid report shared memory
R_ABORT_UNLESS(InitializeBluetoothHidReportFakeSharedMemory());
R_TRY(bluetooth::hid::report::InitializeFakeSharedMemory());
// Create thread for forwarding events
R_ABORT_UNLESS(StartBluetoothCoreEventThread());
R_ABORT_UNLESS(bluetooth::core::StartEventHandlerThread());
//R_TRY(StartEventHandlerThread());
g_bluetoothInitialized = true;
} else {
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btSystemEventUser));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::core::GetUserForwardEvent()));
}
return ams::ResultSuccess();
@ -143,20 +146,25 @@ namespace ams::mitm::btdrv {
R_ABORT_UNLESS(btdrvInitializeHidFwd(this->forward_service.get(), &handle, version));
// Attach the handle to our real system event
os::AttachReadableHandleToSystemEvent(&g_btHidSystemEvent, handle, false, os::EventClearMode_AutoClear);
os::AttachReadableHandleToSystemEvent(bluetooth::hid::GetSystemEvent(),
handle,
false,
os::EventClearMode_AutoClear
);
// Create forwarder events
R_ABORT_UNLESS(InitializeBluetoothHidEvents());
R_ABORT_UNLESS(bluetooth::hid::InitializeEvents());
//bluetooth::events::AttachWaitHolder(BtdrvEventType_BluetoothHid);
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btHidSystemEventFwd));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::GetForwardEvent()));
// Create thread for forwarding events
R_ABORT_UNLESS(StartBluetoothHidEventThread());
R_ABORT_UNLESS(bluetooth::hid::StartEventHandlerThread());
g_hidInitialized = true;
}
else {
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btHidSystemEventUser));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::GetUserForwardEvent()));
}
return ams::ResultSuccess();
@ -229,21 +237,25 @@ namespace ams::mitm::btdrv {
R_ABORT_UNLESS(btdrvRegisterHidReportEventFwd(this->forward_service.get(), &handle));
// Attach the handle to our real system event
os::AttachReadableHandleToSystemEvent(&g_btHidReportSystemEvent, handle, false, os::EventClearMode_AutoClear);
os::AttachReadableHandleToSystemEvent(bluetooth::hid::report::GetSystemEvent(),
handle,
false,
os::EventClearMode_AutoClear
);
// Create forwarder events
R_ABORT_UNLESS(InitializeBluetoothHidReportEvents());
R_ABORT_UNLESS(bluetooth::hid::report::InitializeEvents());
// Set callers handle to that of our forwarder event
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btHidReportSystemEventFwd));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::report::GetForwardEvent()));
// Create thread for forwarding events
R_ABORT_UNLESS(StartBluetoothHidReportEventThread());
R_ABORT_UNLESS(bluetooth::hid::report::StartEventHandlerThread());
g_hidReportInitialized = true;
}
else {
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btHidReportSystemEventUser));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::report::GetUserForwardEvent()));
}
return ams::ResultSuccess();
@ -295,13 +307,10 @@ namespace ams::mitm::btdrv {
R_ABORT_UNLESS(btdrvGetHidReportEventInfoFwd(this->forward_service.get(), &handle));
// Load and map the real bluetooth shared memory
shmemLoadRemote(&g_realBtShmem, handle, BLUETOOTH_SHAREDMEM_SIZE, Perm_Rw);
R_ABORT_UNLESS(shmemMap(&g_realBtShmem));
g_realCircBuff = reinterpret_cast<bluetooth::CircularBuffer *>(shmemGetAddr(&g_realBtShmem));
BTDRV_LOG_FMT("Real shmem @ 0x%p", (void *)g_realCircBuff);
R_TRY(bluetooth::hid::report::MapRemoteSharedMemory(handle));
// Return the handle of our fake shared memory to the caller instead
out_handle.SetValue(g_fakeBtShmem.handle);
out_handle.SetValue(bluetooth::hid::report::GetFakeSharedMemory()->handle);
return ams::ResultSuccess();
}
@ -317,20 +326,19 @@ namespace ams::mitm::btdrv {
R_ABORT_UNLESS(btdrvInitializeBleFwd(this->forward_service.get(), &handle));
// Attach the handle to our real system event
os::AttachReadableHandleToSystemEvent(&g_btBleSystemEvent, handle, false, os::EventClearMode_AutoClear);
os::AttachReadableHandleToSystemEvent(bluetooth::ble::GetSystemEvent(), handle, false, os::EventClearMode_AutoClear);
// Create forwarder events
R_ABORT_UNLESS(InitializeBluetoothBleEvents());
R_ABORT_UNLESS(bluetooth::ble::InitializeEvents());
//bluetooth::events::AttachWaitHolder(BtdrvEventType_BluetoothBle);
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btBleSystemEventFwd));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::ble::GetForwardEvent()));
// Create thread for forwarding events
R_ABORT_UNLESS(StartBluetoothBleEventThread());
R_ABORT_UNLESS(bluetooth::ble::StartEventHandlerThread());
g_bleInitialized = true;
}
else {
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(&g_btBleSystemEventUser));
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::ble::GetUserForwardEvent()));
}
return ams::ResultSuccess();

View file

@ -1,7 +1,7 @@
#pragma once
#include <stratosphere.hpp>
#include "btdrv_mitm_logging.hpp"
#include "btdrv_mitm_logging.hpp"
namespace ams::mitm::btdrv {

View file

@ -1,7 +1,6 @@
#include "btdrv_shim.h"
#include <stratosphere/sf/sf_mitm_dispatch.h>
Result btdrvInitializeBluetoothFwd(Service* s, Handle *out_handle) {
return serviceMitmDispatch(s, 1,
.out_handle_attrs = { SfOutHandleAttr_HipcCopy },

View file

@ -13,7 +13,6 @@
#include "btdrv_mitm_logging.hpp"
namespace ams::mitm::btdrv {
namespace {

View file

@ -2,7 +2,6 @@
#include <switch.h>
#include "controllers/bluetoothcontroller.hpp"
namespace ams::mitm::btdrv {
controller::ControllerType identifyController(uint16_t vid, uint16_t pid);

View file

@ -56,6 +56,7 @@ void __appInit(void) {
sm::DoWithSession([&]() {
R_ABORT_UNLESS(fsInitialize());
R_ABORT_UNLESS(pscmInitialize());
R_ABORT_UNLESS(pmdmntInitialize());
R_ABORT_UNLESS(pminfoInitialize());
R_ABORT_UNLESS(btdrvInitialize());
@ -68,11 +69,13 @@ void __appInit(void) {
}
void __appExit(void) {
fsdevUnmountAll();
//hiddbgExit();
btdrvExit();
pminfoExit();
pmdmntExit();
pscmExit();
fsdevUnmountAll();
fsExit();
}