2020-06-13 19:27:07 +00:00
|
|
|
#include "bluetooth_core.hpp"
|
2020-06-14 22:18:46 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <cstring>
|
2020-06-13 00:08:43 +00:00
|
|
|
#include "../btdrv_mitm_flags.hpp"
|
|
|
|
|
|
|
|
#include "../btdrv_mitm_logging.hpp"
|
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
namespace ams::bluetooth::core {
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
namespace {
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
std::atomic<bool> g_isInitialized(false);
|
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
os::ThreadType g_eventHandlerThread;
|
|
|
|
alignas(os::ThreadStackAlignment) u8 g_eventHandlerThreadStack[0x2000];
|
2020-06-14 22:18:46 +00:00
|
|
|
|
|
|
|
os::Mutex g_eventDataLock(false);
|
|
|
|
u8 g_eventDataBuffer[0x400];
|
|
|
|
BluetoothEventType g_currentEventType;
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
os::SystemEventType g_btSystemEvent;
|
|
|
|
os::SystemEventType g_btSystemEventFwd;
|
|
|
|
os::SystemEventType g_btSystemEventUser;
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
void EventThreadFunc(void *arg) {
|
|
|
|
while (true) {
|
|
|
|
os::WaitSystemEvent(&g_btSystemEvent);
|
|
|
|
HandleEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsInitialized(void) {
|
|
|
|
return g_isInitialized;
|
2020-06-13 17:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os::SystemEventType *GetSystemEvent(void) {
|
|
|
|
return &g_btSystemEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
os::SystemEventType *GetForwardEvent(void) {
|
|
|
|
return &g_btSystemEventFwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
os::SystemEventType *GetUserForwardEvent(void) {
|
|
|
|
return &g_btSystemEventUser;
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
Result Initialize(Handle eventHandle) {
|
|
|
|
os::AttachReadableHandleToSystemEvent(&g_btSystemEvent, eventHandle, false, os::EventClearMode_AutoClear);
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
R_TRY(os::CreateSystemEvent(&g_btSystemEventFwd, os::EventClearMode_AutoClear, true));
|
|
|
|
R_TRY(os::CreateSystemEvent(&g_btSystemEventUser, os::EventClearMode_AutoClear, true));
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(os::CreateThread(&g_eventHandlerThread,
|
|
|
|
EventThreadFunc,
|
|
|
|
nullptr,
|
|
|
|
g_eventHandlerThreadStack,
|
|
|
|
sizeof(g_eventHandlerThreadStack),
|
|
|
|
9
|
|
|
|
));
|
|
|
|
|
|
|
|
os::StartThread(&g_eventHandlerThread);
|
|
|
|
|
|
|
|
g_isInitialized = true;
|
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
void Finalize(void) {
|
|
|
|
os::DestroyThread(&g_eventHandlerThread);
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
os::DestroySystemEvent(&g_btSystemEventUser);
|
|
|
|
os::DestroySystemEvent(&g_btSystemEventFwd);
|
|
|
|
|
|
|
|
g_isInitialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result GetEventInfo(BluetoothEventType *type, u8* buffer, size_t size) {
|
|
|
|
std::scoped_lock lk(g_eventDataLock);
|
|
|
|
|
|
|
|
*type = g_currentEventType;
|
|
|
|
std::memcpy(buffer, g_eventDataBuffer, size);
|
2020-06-13 17:15:59 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
2020-06-13 00:08:43 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
void HandleEvent(void) {
|
|
|
|
BTDRV_LOG_FMT("bluetooth event fired");
|
|
|
|
|
|
|
|
std::scoped_lock lk(g_eventDataLock);
|
|
|
|
{
|
|
|
|
R_ABORT_UNLESS(btdrvGetEventInfo(&g_currentEventType, g_eventDataBuffer, sizeof(g_eventDataBuffer)));
|
|
|
|
|
|
|
|
BluetoothEventData *event_data = reinterpret_cast<BluetoothEventData *>(g_eventDataBuffer);
|
|
|
|
|
|
|
|
size_t data_size;
|
|
|
|
switch (g_currentEventType) {
|
|
|
|
case BluetoothEvent_DeviceFound:
|
|
|
|
data_size = sizeof(event_data->deviceFound);
|
|
|
|
// Todo: try changing name and cod to look like switch pro controller
|
|
|
|
//snprintf(event_data->deviceFound.name, sizeof(BluetoothName), "Pro Controller");
|
|
|
|
//event_data->deviceFound._unk2 = 0xffffffcb;
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_DiscoveryStateChanged:
|
|
|
|
data_size = sizeof(event_data->discoveryState);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_PinRequest:
|
|
|
|
data_size = sizeof(event_data->pinReply);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_SspRequest:
|
|
|
|
data_size = sizeof(event_data->sspReply);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_BondStateChanged:
|
|
|
|
data_size = sizeof(event_data->bondState.v2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
data_size = sizeof(g_eventDataBuffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
BTDRV_LOG_DATA(event_data, data_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal our forwarder events
|
|
|
|
if (!g_redirectEvents || g_preparingForSleep)
|
|
|
|
os::SignalSystemEvent(&g_btSystemEventFwd);
|
|
|
|
else
|
|
|
|
os::SignalSystemEvent(&g_btSystemEventUser);
|
|
|
|
}
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
}
|