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"
|
2020-06-17 23:40:11 +00:00
|
|
|
#include "../controllermanager.hpp"
|
2020-06-13 00:08:43 +00:00
|
|
|
|
|
|
|
#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-22 21:14:54 +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-22 21:14:54 +00:00
|
|
|
/*
|
2020-06-14 22:18:46 +00:00
|
|
|
void EventThreadFunc(void *arg) {
|
|
|
|
while (true) {
|
|
|
|
os::WaitSystemEvent(&g_btSystemEvent);
|
|
|
|
HandleEvent();
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 21:14:54 +00:00
|
|
|
*/
|
2020-06-14 22:18:46 +00:00
|
|
|
|
2020-06-17 23:40:11 +00:00
|
|
|
void _LogEvent(BluetoothEventType type, BluetoothEventData *eventData) {
|
|
|
|
|
|
|
|
size_t dataSize;
|
|
|
|
switch (type) {
|
|
|
|
case BluetoothEvent_DeviceFound:
|
|
|
|
dataSize = sizeof(eventData->deviceFound);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_DiscoveryStateChanged:
|
|
|
|
dataSize = sizeof(eventData->discoveryState);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_PinRequest:
|
|
|
|
dataSize = sizeof(eventData->pinReply);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_SspRequest:
|
|
|
|
dataSize = sizeof(eventData->sspReply);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_BondStateChanged:
|
|
|
|
if (hos::GetVersion() < hos::Version_9_0_0)
|
|
|
|
dataSize = sizeof(eventData->bondState);
|
|
|
|
else
|
|
|
|
dataSize = sizeof(eventData->bondState.v2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dataSize = sizeof(g_eventDataBuffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
//BTDRV_LOG_DATA(eventData, dataSize);
|
|
|
|
BTDRV_LOG_DATA_MSG(eventData, dataSize, "Bluetooth core event [%02d]", type);
|
2020-06-17 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-06-22 21:14:54 +00:00
|
|
|
//os::AttachReadableHandleToSystemEvent(&g_btSystemEvent, eventHandle, false, os::EventClearMode_AutoClear);
|
|
|
|
os::AttachReadableHandleToSystemEvent(&g_btSystemEvent, eventHandle, true, 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-22 21:14:54 +00:00
|
|
|
|
|
|
|
/*
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(os::CreateThread(&g_eventHandlerThread,
|
|
|
|
EventThreadFunc,
|
|
|
|
nullptr,
|
|
|
|
g_eventHandlerThreadStack,
|
|
|
|
sizeof(g_eventHandlerThreadStack),
|
|
|
|
9
|
|
|
|
));
|
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
os::StartThread(&g_eventHandlerThread);
|
|
|
|
*/
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
|
|
|
|
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) {
|
2020-06-22 21:14:54 +00:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
2020-06-17 23:40:11 +00:00
|
|
|
void handleDeviceFoundEvent(BluetoothEventData *eventData) {
|
|
|
|
if (ams::mitm::btdrv::IsController(&eventData->deviceFound.cod) && !ams::mitm::btdrv::IsValidSwitchControllerName(eventData->deviceFound.name)) {
|
|
|
|
std::strncpy(eventData->deviceFound.name, "Lic Pro Controller", sizeof(BluetoothName) - 1);
|
|
|
|
eventData->pinReply.cod = {0x00, 0x25, 0x08};
|
|
|
|
}
|
2020-06-22 21:14:54 +00:00
|
|
|
else {
|
|
|
|
BTDRV_LOG_FMT("handleDeviceFoundEvent: [%02x%02x%02x] | %s",
|
|
|
|
eventData->deviceFound.cod.cod[0],
|
|
|
|
eventData->deviceFound.cod.cod[1],
|
|
|
|
eventData->deviceFound.cod.cod[2],
|
|
|
|
eventData->deviceFound.name
|
|
|
|
);
|
|
|
|
}
|
2020-06-17 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handlePinRequesEvent(BluetoothEventData *eventData) {
|
|
|
|
if (ams::mitm::btdrv::IsController(&eventData->pinReply.cod) && !ams::mitm::btdrv::IsValidSwitchControllerName(eventData->pinReply.name)) {
|
|
|
|
std::strncpy(eventData->pinReply.name, "Lic Pro Controller", sizeof(BluetoothName) - 1);
|
|
|
|
eventData->pinReply.cod = {0x00, 0x25, 0x08};
|
|
|
|
}
|
2020-06-22 21:14:54 +00:00
|
|
|
else {
|
|
|
|
BTDRV_LOG_FMT("handleDeviceFoundEvent: [%02x%02x%02x] | %s",
|
|
|
|
eventData->pinReply.cod.cod[0],
|
|
|
|
eventData->pinReply.cod.cod[1],
|
|
|
|
eventData->pinReply.cod.cod[2],
|
|
|
|
eventData->pinReply.name
|
|
|
|
);
|
|
|
|
}
|
2020-06-17 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleSspRequesEvent(BluetoothEventData *eventData) {
|
|
|
|
if (ams::mitm::btdrv::IsController(&eventData->sspReply.cod) && !ams::mitm::btdrv::IsValidSwitchControllerName(eventData->sspReply.name)) {
|
|
|
|
std::strncpy(eventData->sspReply.name, "Lic Pro Controller", sizeof(BluetoothName) - 1);
|
|
|
|
eventData->pinReply.cod = {0x00, 0x25, 0x08};
|
|
|
|
}
|
2020-06-22 21:14:54 +00:00
|
|
|
else {
|
|
|
|
BTDRV_LOG_FMT("handleDeviceFoundEvent: [%02x%02x%02x] | %s",
|
|
|
|
eventData->sspReply.cod.cod[0],
|
|
|
|
eventData->sspReply.cod.cod[1],
|
|
|
|
eventData->sspReply.cod.cod[2],
|
|
|
|
eventData->sspReply.name
|
|
|
|
);
|
|
|
|
}
|
2020-06-17 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetEventInfo(ncm::ProgramId program_id, BluetoothEventType *type, u8* buffer, size_t size) {
|
2020-06-14 22:18:46 +00:00
|
|
|
std::scoped_lock lk(g_eventDataLock);
|
2020-06-17 23:40:11 +00:00
|
|
|
{
|
|
|
|
*type = g_currentEventType;
|
|
|
|
std::memcpy(buffer, g_eventDataBuffer, size);
|
|
|
|
|
|
|
|
BluetoothEventData *eventData = reinterpret_cast<BluetoothEventData *>(buffer);
|
|
|
|
|
|
|
|
if (program_id == ncm::SystemProgramId::Btm) {
|
|
|
|
|
|
|
|
switch (g_currentEventType) {
|
|
|
|
case BluetoothEvent_DeviceFound:
|
|
|
|
handleDeviceFoundEvent(eventData);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_DiscoveryStateChanged:
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_PinRequest:
|
|
|
|
handlePinRequesEvent(eventData);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_SspRequest:
|
|
|
|
handleSspRequesEvent(eventData);
|
|
|
|
break;
|
|
|
|
case BluetoothEvent_BondStateChanged:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-06-22 21:14:54 +00:00
|
|
|
} else {
|
|
|
|
BTDRV_LOG_FMT("Not BTM: [%02x]", program_id);
|
2020-06-17 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_LogEvent(g_currentEventType, eventData);
|
2020-06-13 17:15:59 +00:00
|
|
|
|
2020-06-17 23:40:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
// Might not be required if original event is autoclear?
|
|
|
|
//os::ClearSystemEvent(&g_btSystemEvent);
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
std::scoped_lock lk(g_eventDataLock);
|
2020-06-17 23:40:11 +00:00
|
|
|
|
|
|
|
R_ABORT_UNLESS(btdrvGetEventInfo(&g_currentEventType, g_eventDataBuffer, sizeof(g_eventDataBuffer)));
|
2020-06-14 22:18:46 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
//BTDRV_LOG_FMT("[%02d] Bluetooth Core Event", g_currentEventType);
|
2020-06-17 23:40:11 +00:00
|
|
|
//BTDRV_LOG_DATA(g_eventDataBuffer, sizeof(g_eventDataBuffer));
|
2020-06-14 23:14:09 +00:00
|
|
|
|
2020-06-17 23:40:11 +00:00
|
|
|
//BluetoothEventData *eventData = reinterpret_cast<BluetoothEventData *>(g_eventDataBuffer);
|
2020-06-14 22:18:46 +00:00
|
|
|
|
2020-06-17 23:40:11 +00:00
|
|
|
//_LogEvent(g_currentEventType, eventData);
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (g_currentEventType == BluetoothEvent_DeviceFound) {
|
2020-06-14 22:18:46 +00:00
|
|
|
|
2020-06-17 23:40:11 +00:00
|
|
|
if (ams::mitm::btdrv::IsController(&eventData->deviceFound.cod)) {
|
|
|
|
if (std::strncmp(eventData->deviceFound.name, "Nintendo RVL-CNT-01", sizeof(BluetoothName)) == 0 ||
|
|
|
|
std::strncmp(eventData->deviceFound.name, "Nintendo RVL-CNT-01-UC", sizeof(BluetoothName)) == 0)
|
|
|
|
{
|
|
|
|
BTDRV_LOG_FMT("!!!!! Calling CreateBond");
|
|
|
|
btdrvCreateBond(&eventData->deviceFound.address, BluetoothTransport_Auto);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 22:18:46 +00:00
|
|
|
}
|
2020-06-17 23:40:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (g_currentEventType == BluetoothEvent_PinRequest) {
|
|
|
|
BTDRV_LOG_FMT("!!!!! Calling PinReply");
|
|
|
|
BluetoothPinCode pincode = {};
|
|
|
|
btdrvRespondToPinRequest(&eventData->pinReply.address, false, &pincode, sizeof(BluetoothAddress));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
// Signal our forwarder events
|
2020-06-22 21:14:54 +00:00
|
|
|
//if (!g_redirectEvents || g_preparingForSleep)
|
|
|
|
if (!g_redirectEvents)
|
2020-06-14 22:18:46 +00:00
|
|
|
os::SignalSystemEvent(&g_btSystemEventFwd);
|
|
|
|
else
|
|
|
|
os::SignalSystemEvent(&g_btSystemEventUser);
|
|
|
|
}
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
}
|