btdrv-mitm: properly intercept BLE events and mitm GetBleManagedEventInfo

This commit is contained in:
ndeadly 2020-06-15 01:14:09 +02:00
parent 5a178f5228
commit 48376acd37
8 changed files with 71 additions and 13 deletions

View file

@ -1,6 +1,8 @@
#include "bluetooth_ble.hpp"
#include <atomic>
#include <mutex>
#include <cstring>
#include "../btdrv_mitm_flags.hpp"
#include "../btdrv_mitm_logging.hpp"
@ -13,8 +15,10 @@ namespace ams::bluetooth::ble {
os::ThreadType g_eventHandlerThread;
alignas(os::ThreadStackAlignment) u8 g_eventHandlerThreadStack[0x2000];
//u8 g_eventDataBuffer[0x400];
//BluetoothEventType g_currentEventType;
os::Mutex g_eventDataLock(false);
u8 g_eventDataBuffer[0x400];
BleEventType g_currentEventType;
os::SystemEventType g_btBleSystemEvent;
os::SystemEventType g_btBleSystemEventFwd;
@ -75,8 +79,23 @@ namespace ams::bluetooth::ble {
g_isInitialized = false;
}
Result GetEventInfo(BleEventType *type, u8* buffer, size_t size) {
std::scoped_lock lk(g_eventDataLock);
*type = g_currentEventType;
std::memcpy(buffer, g_eventDataBuffer, size);
return ams::ResultSuccess();
}
void HandleEvent(void) {
BTDRV_LOG_FMT("ble event fired");
std::scoped_lock lk(g_eventDataLock);
{
R_ABORT_UNLESS(btdrvGetBleManagedEventInfo(&g_currentEventType, g_eventDataBuffer, sizeof(g_eventDataBuffer)));
BTDRV_LOG_FMT("[%02d] BLE Event", g_currentEventType);
}
// Signal our forwarder events
if (!g_redirectEvents || g_preparingForSleep)

View file

@ -13,6 +13,7 @@ namespace ams::bluetooth::ble {
Result Initialize(Handle eventHandle);
void Finalize(void);
Result GetEventInfo(BleEventType *type, u8* buffer, size_t size);
void HandleEvent(void);
}

View file

@ -89,12 +89,13 @@ namespace ams::bluetooth::core {
}
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)));
BTDRV_LOG_FMT("[%02d] Bluetooth Core Event", g_currentEventType);
BluetoothEventData *event_data = reinterpret_cast<BluetoothEventData *>(g_eventDataBuffer);
size_t data_size;

View file

@ -114,14 +114,14 @@ namespace ams::bluetooth::hid {
}
void HandleEvent(void) {
BTDRV_LOG_FMT("hid event fired");
HidEventData *eventData = reinterpret_cast<HidEventData *>(g_eventDataBuffer);
std::scoped_lock lk(g_eventDataLock);
{
R_ABORT_UNLESS(btdrvGetHidEventInfo(&g_currentEventType, g_eventDataBuffer, sizeof(g_eventDataBuffer)));
BTDRV_LOG_FMT("[%02d] HID Event", g_currentEventType);
switch (g_currentEventType) {
case HidEvent_ConnectionState:

View file

@ -66,7 +66,7 @@ namespace ams::mitm::btdrv {
static_cast<size_t>(out_buffer.GetSize())
));
BTDRV_LOG_FMT("btdrv-mitm: GetEventInfo [%02d]", out_type.GetValue());
BTDRV_LOG_FMT("btdrv-mitm: GetEventInfo");
return ams::ResultSuccess();
}
@ -136,7 +136,7 @@ namespace ams::mitm::btdrv {
));
BTDRV_LOG_FMT("btdrv-mitm: GetHidEventInfo [%02d]", out_type.GetValue());
BTDRV_LOG_FMT("btdrv-mitm: GetHidEventInfo");
return ams::ResultSuccess();
}
@ -244,6 +244,21 @@ namespace ams::mitm::btdrv {
return ams::ResultSuccess();
}
Result BtdrvMitmService::GetBleManagedEventInfoDeprecated(sf::Out<BleEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
return GetBleManagedEventInfo(out_type, out_buffer);
}
Result BtdrvMitmService::GetBleManagedEventInfo(sf::Out<BleEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
R_TRY(bluetooth::ble::GetEventInfo(out_type.GetPointer(),
static_cast<u8 *>(out_buffer.GetPointer()),
static_cast<size_t>(out_buffer.GetSize())
));
BTDRV_LOG_FMT("btdrv-mitm: GetBleManagedEventInfo");
return ams::ResultSuccess();
}
void BtdrvMitmService::RedirectSystemEvents(bool redirect) {
BTDRV_LOG_FMT("btdrv-mitm: RedirectSystemEvents");

View file

@ -25,6 +25,8 @@ namespace ams::mitm::btdrv {
/* 5.0.0+ */
InitializeBle = 46,
FinalizeBle = 49,
GetBleManagedEventInfoDeprecated = 78,
GetBleManagedEventInfo = 79,
/* Extensions */
RedirectSystemEvents = 65000,
@ -54,6 +56,8 @@ namespace ams::mitm::btdrv {
Result GetHidReportEventInfo(sf::OutCopyHandle out_handle);
Result InitializeBle(sf::OutCopyHandle out_handle);
Result FinalizeBle(void);
Result GetBleManagedEventInfoDeprecated(sf::Out<BleEventType> out_type, const sf::OutPointerBuffer &out_buffer);
Result GetBleManagedEventInfo(sf::Out<BleEventType> out_type, const sf::OutPointerBuffer &out_buffer);
void RedirectSystemEvents(bool redirect);
@ -74,6 +78,8 @@ namespace ams::mitm::btdrv {
MAKE_SERVICE_COMMAND_META(GetHidReportEventInfo, hos::Version_7_0_0),
MAKE_SERVICE_COMMAND_META(InitializeBle, hos::Version_5_0_0),
MAKE_SERVICE_COMMAND_META(FinalizeBle, hos::Version_5_0_0),
MAKE_SERVICE_COMMAND_META(GetBleManagedEventInfoDeprecated, hos::Version_5_0_0, hos::Version_5_0_2),
MAKE_SERVICE_COMMAND_META(GetBleManagedEventInfo, hos::Version_5_1_0),
MAKE_SERVICE_COMMAND_META(RedirectSystemEvents),
};

View file

@ -12,7 +12,7 @@ Result btdrvFinalizeBluetoothFwd(Service* s) {
return serviceMitmDispatch(s, 4);
}
/*
Result btdrvCancelBondFwd(Service* s, const BluetoothAddress *address) {
const struct {
BluetoothAddress address;
@ -20,14 +20,16 @@ Result btdrvCancelBondFwd(Service* s, const BluetoothAddress *address) {
return serviceMitmDispatchIn(s, 12, in);
}
*/
/*
Result btdrvGetEventInfoFwd(Service* s, BluetoothEventType *type, u8 *buffer, size_t size) {
return serviceMitmDispatchOut(s, 15, *type,
.buffer_attrs = { SfBufferAttr_HipcPointer | SfBufferAttr_Out },
.buffers = { {buffer, size} }
);
}
*/
Result btdrvInitializeHidFwd(Service* s, Handle *out_handle, u16 version) {
return serviceMitmDispatchIn(s, 16, version,
@ -66,12 +68,14 @@ Result btdrvFinalizeHidFwd(Service* s) {
return serviceMitmDispatch(s, 26);
}
/*
Result btdrvGetHidEventInfoFwd(Service* s, HidEventType *type, u8 *buffer, size_t size) {
return serviceMitmDispatchOut(s, 27, *type,
.buffer_attrs = { SfBufferAttr_HipcPointer | SfBufferAttr_Out },
.buffers = { {buffer, size} }
);
}
*/
Result btdrvRegisterHidReportEventFwd(Service* s, Handle *out_handle) {
return serviceMitmDispatch(s, hosversionBefore(4, 0, 0) ? 36 : 37,
@ -104,3 +108,15 @@ Result btdrvInitializeBleFwd(Service* s, Handle *out_handle) {
Result btdrvFinalizeBleFwd(Service* s) {
return serviceMitmDispatch(s, 49);
}
/*
Result btdrvGetBleManagedEventInfoFwd(Service* s, BleEventType *type, u8 *buffer, u16 length) {
if (hosversionBefore(5, 0, 0))
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
return serviceDispatchOut(s, hosversionBefore(5, 1, 0) ? 78 : 79, *type,
.buffer_attrs = { SfBufferAttr_HipcPointer | SfBufferAttr_Out },
.buffers = { {buffer, length} }
);
}
*/

View file

@ -10,20 +10,20 @@ Result btdrvFinalizeBluetoothFwd(Service* s);
Result btdrvCancelBondFwd(Service* s, const BluetoothAddress *address);
Result btdrvGetEventInfoFwd(Service* s, BluetoothEventType *type, u8 *buffer, size_t size);
//Result btdrvGetEventInfoFwd(Service* s, BluetoothEventType *type, u8 *buffer, size_t size);
Result btdrvInitializeHidFwd(Service* s, Handle *out_handle, u16 version);
Result btdrvWriteHidDataFwd(Service* s, const BluetoothAddress *address, const BluetoothHidData *data);
//Result btdrvGetPairedDeviceInfoFwd(Service* s, const BluetoothAddress *address, BluetoothDevicesSettings *device);
Result btdrvFinalizeHidFwd(Service* s);
Result btdrvGetHidEventInfoFwd(Service* s, HidEventType *type, u8 *buffer, size_t size);
//Result btdrvGetHidEventInfoFwd(Service* s, HidEventType *type, u8 *buffer, size_t size);
Result btdrvRegisterHidReportEventFwd(Service* s, Handle *out_handle);
Result btdrvGetHidReportEventInfoDeprecatedFwd(Service* s, HidEventType *type, u8 *buffer, size_t size);
Result btdrvGetHidReportEventInfoFwd(Service* s, Handle *out_handle);
Result btdrvInitializeBleFwd(Service* s, Handle *out_handle);
Result btdrvFinalizeBleFwd(Service* s);
//Result btdrvGetBleManagedEventInfoFwd(Service* s, BleEventType *type, u8 *buffer, u16 length)
#ifdef __cplusplus
}