2020-05-22 10:29:36 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <switch.h>
|
2020-05-11 20:30:08 +00:00
|
|
|
#include "btdrv_mitm_service.hpp"
|
2020-06-13 00:08:43 +00:00
|
|
|
#include "btdrv_mitm_flags.hpp"
|
2020-05-11 20:30:08 +00:00
|
|
|
#include "btdrv_shim.h"
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
#include "bluetooth/bluetooth_events.hpp"
|
2020-06-11 18:34:14 +00:00
|
|
|
#include "controllermanager.hpp"
|
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
namespace ams::mitm::btdrv {
|
|
|
|
|
|
|
|
Result BtdrvMitmService::InitializeBluetooth(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: InitializeBluetooth");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::core::IsInitialized()) {
|
2020-05-22 10:29:36 +00:00
|
|
|
Handle handle = INVALID_HANDLE;
|
2020-06-13 17:15:59 +00:00
|
|
|
R_TRY(btdrvInitializeBluetoothFwd(this->forward_service.get(), &handle));
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(bluetooth::core::Initialize(handle));
|
2020-07-09 17:44:12 +00:00
|
|
|
R_TRY(bluetooth::hid::report::InitializeReportBuffer());
|
2020-06-15 12:13:17 +00:00
|
|
|
|
2020-07-09 17:44:12 +00:00
|
|
|
//if (hos::GetVersion() >= hos::Version_7_0_0)
|
|
|
|
//R_TRY(bluetooth::hid::report::InitializeFakeSharedMemory());
|
2020-06-15 12:13:17 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::core::GetForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
} else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::core::GetUserForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::FinalizeBluetooth(void) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: FinalizeBluetooth");
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
// Only btm should be able to make this call
|
2020-05-22 10:29:36 +00:00
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvFinalizeBluetoothFwd(this->forward_service.get()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetEventInfo(sf::Out<bluetooth::EventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-09 19:58:32 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetEventInfo");
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
R_TRY(bluetooth::core::GetEventInfo(this->client_info.program_id,
|
|
|
|
out_type.GetPointer(),
|
2020-06-14 22:18:46 +00:00
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
2020-05-22 10:29:36 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::InitializeHid(sf::OutCopyHandle out_handle, u16 version) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: InitializeHid");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::hid::IsInitialized()) {
|
|
|
|
Handle handle = INVALID_HANDLE;
|
|
|
|
R_TRY(btdrvInitializeHidFwd(this->forward_service.get(), &handle, version));
|
|
|
|
R_TRY(bluetooth::hid::Initialize(handle));
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::GetForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::GetUserForwardEvent()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:26:22 +00:00
|
|
|
/*
|
|
|
|
void subcmd_response() {
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
SwitchReport0x21 *report;
|
|
|
|
report->timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds() & 0xff;
|
|
|
|
report->conn_info = 0;
|
|
|
|
report->battery = 8;
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::WriteHidData(bluetooth::Address address, const sf::InPointerBuffer &buffer) {
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-07-09 19:26:22 +00:00
|
|
|
auto requestData = reinterpret_cast<const bluetooth::HidData *>(buffer.GetPointer());
|
|
|
|
u8 cmdId = requestData->data[0];
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Hid) {
|
2020-06-09 20:39:30 +00:00
|
|
|
auto controller = locateController(&address);
|
2020-07-09 19:26:22 +00:00
|
|
|
if (controller && !controller->isSwitchController()) {
|
2020-06-30 20:23:16 +00:00
|
|
|
|
|
|
|
if (cmdId == 0x01) {
|
|
|
|
auto subCmdId = static_cast<bluetooth::SubCmdType>(requestData->data[10]);
|
2020-07-09 19:26:22 +00:00
|
|
|
BTDRV_LOG_FMT("Subcommand report [0x%02x]", subCmdId);
|
2020-06-30 20:23:16 +00:00
|
|
|
|
|
|
|
switch (subCmdId) {
|
|
|
|
case bluetooth::SubCmd_RequestDeviceInfo:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x82, 0x02, 0x03, 0x48, 0x03, 0x02, address.address[0], address.address[1], address.address[2], address.address[3], address.address[4], address.address[5], 0x01, 0x02};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bluetooth::SubCmd_SpiFlashRead:
|
|
|
|
{
|
|
|
|
// Official controller reads these
|
|
|
|
// @ 0x00006000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <= Serial
|
|
|
|
// @ 0x00006050: 32 32 32 ff ff ff ff ff ff ff ff ff <= RGB colours (body, buttons, left grip, right grip)
|
|
|
|
// @ 0x00006080: 50 fd 00 00 c6 0f 0f 30 61 ae 90 d9 d4 14 54 41 15 54 c7 79 9c 33 36 63 <= Factory Sensor and Stick device parameters
|
|
|
|
// @ 0x00006098: 0f 30 61 ae 90 d9 d4 14 54 41 15 54 c7 79 9c 33 36 63 <= Stick device parameters 2. Normally the same with 1, even in Pro Contr.
|
|
|
|
// @ 0x00008010: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
|
|
|
|
// @ 0x0000603d: e6 a5 67 1a 58 78 50 56 60 1a f8 7f 20 c6 63 d5 15 5e ff 32 32 32 ff ff ff <= Left analog stick calibration
|
|
|
|
// @ 0x00006020: 64 ff 33 00 b8 01 00 40 00 40 00 40 17 00 d7 ff bd ff 3b 34 3b 34 3b 34 <= 6-Axis motion sensor Factory calibration
|
|
|
|
|
|
|
|
u32 read_addr = *(u32 *)(&requestData->data[11]);
|
|
|
|
u8 read_size = requestData->data[15];
|
2020-07-01 08:41:23 +00:00
|
|
|
BTDRV_LOG_DATA_MSG((void *)requestData, requestData->length+2, "SPI flash read: %d bytes @ 0x%08x", read_size, read_addr);
|
2020-06-30 20:23:16 +00:00
|
|
|
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
|
|
|
|
if (read_addr == 0x6000 && read_size == 0x10) {
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x90, subCmdId, requestData->data[11], requestData->data[12], requestData->data[13], requestData->data[14], requestData->data[15],
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (read_addr == 0x6050 && read_size == 0x0d) {
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x90, subCmdId, requestData->data[11], requestData->data[12], requestData->data[13], requestData->data[14], requestData->data[15],
|
|
|
|
//0x32, 0x32, 0x32, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
|
|
|
0x32, 0x32, 0x32, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00
|
|
|
|
};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (read_addr == 0x6080 && read_size == 0x18) {
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x90, subCmdId, requestData->data[11], requestData->data[12], requestData->data[13], requestData->data[14], requestData->data[15],
|
|
|
|
0x50, 0xfd, 0x00, 0x00, 0xc6, 0x0f, 0x0f, 0x30, 0x61, 0xae, 0x90, 0xd9, 0xd4, 0x14, 0x54, 0x41, 0x15, 0x54, 0xc7, 0x79, 0x9c, 0x33, 0x36, 0x63};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (read_addr == 0x6098 && read_size == 0x12) {
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x90, subCmdId, requestData->data[11], requestData->data[12], requestData->data[13], requestData->data[14], requestData->data[15],
|
|
|
|
0x0f, 0x30, 0x61, 0xae, 0x90, 0xd9, 0xd4, 0x14, 0x54, 0x41, 0x15, 0x54, 0xc7, 0x79, 0x9c, 0x33, 0x36, 0x63};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (read_addr == 0x8010 && read_size == 0x18) {
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x90, subCmdId, requestData->data[11], requestData->data[12], requestData->data[13], requestData->data[14], requestData->data[15],
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (read_addr == 0x603d && read_size == 0x19) {
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x90, subCmdId, requestData->data[11], requestData->data[12], requestData->data[13], requestData->data[14], requestData->data[15],
|
|
|
|
0xe6, 0xa5, 0x67, 0x1a, 0x58, 0x78, 0x50, 0x56, 0x60, 0x1a, 0xf8, 0x7f, 0x20, 0xc6, 0x63, 0xd5, 0x15, 0x5e, 0xff, 0x32, 0x32, 0x32, 0xff, 0xff, 0xff};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (read_addr == 0x6020 && read_size == 0x18) {
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x90, subCmdId, requestData->data[11], requestData->data[12], requestData->data[13], requestData->data[14], requestData->data[15],
|
|
|
|
0x64, 0xff, 0x33, 0x00, 0xb8, 0x01, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x17, 0x00, 0xd7, 0xff, 0xbd, 0xff, 0x3b, 0x34, 0x3b, 0x34, 0x3b, 0x34};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bluetooth::SubCmd_SetInputReportMode:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x80, subCmdId};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bluetooth::SubCmd_TriggersElapsedTime:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x83, subCmdId};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
2020-07-04 19:24:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bluetooth::SubCmd_SetShipPowerState:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x80, subCmdId, 0x00};
|
|
|
|
|
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
2020-06-30 20:23:16 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bluetooth::SubCmd_SetMcuConfig:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0xa0, subCmdId, 0x01, 0x00, 0xff, 0x00, 0x03, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-09 19:19:02 +00:00
|
|
|
case bluetooth::SubCmd_SetMcuState:
|
|
|
|
{
|
|
|
|
//BTDRV_LOG_DATA_MSG((void *)requestData, requestData->length+2, "Set MCU State");
|
|
|
|
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x80, subCmdId};
|
|
|
|
|
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-06-30 20:23:16 +00:00
|
|
|
|
|
|
|
case bluetooth::SubCmd_SetPlayerLeds:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x80, subCmdId};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bluetooth::SubCmd_EnableImu:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x80, subCmdId};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bluetooth::SubCmd_EnableVibration:
|
|
|
|
{
|
|
|
|
s64 timer = os::ConvertToTimeSpan(os::GetSystemTick()).GetMilliSeconds();
|
|
|
|
u8 reportData[] = {0x31, 0x00, 0x21, timer & 0xff, 0x80, 0x00, 0x00, 0x00, 0x0b, 0xb8, 0x78, 0xd9, 0xd7, 0x81, 0x00,
|
|
|
|
0x80, subCmdId};
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
auto responseData = reinterpret_cast<bluetooth::HidData *>(reportData);
|
2020-06-30 20:23:16 +00:00
|
|
|
bluetooth::hid::report::WriteFakeHidData(&address, responseData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmdId == 0x10) {
|
2020-07-01 08:43:27 +00:00
|
|
|
// Rumble report
|
2020-06-30 20:23:16 +00:00
|
|
|
}
|
2020-07-09 19:26:22 +00:00
|
|
|
else {
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: WriteHidData [0x%02x]", cmdId);
|
|
|
|
}
|
2020-06-22 21:14:54 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvWriteHidDataFwd(this->forward_service.get(),
|
2020-05-22 10:29:36 +00:00
|
|
|
&address,
|
2020-07-01 13:34:07 +00:00
|
|
|
reinterpret_cast<const bluetooth::HidData *>(buffer.GetPointer())
|
2020-05-22 10:29:36 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
/*
|
|
|
|
Result BtdrvMitmService::SetHidReport(bluetooth::Address address, bluetooth::HhReportType type, const sf::InPointerBuffer &buffer) {
|
2020-06-22 21:14:54 +00:00
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: SetHidReport");
|
|
|
|
|
|
|
|
R_TRY(btdrvSetHidReportFwd(this->forward_service.get(),
|
|
|
|
&address,
|
|
|
|
type,
|
2020-07-01 13:34:07 +00:00
|
|
|
reinterpret_cast<const bluetooth::HidData *>(buffer.GetPointer())
|
2020-06-22 21:14:54 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetHidReport(bluetooth::Address address, bluetooth::HhReportType type, u8 id) {
|
2020-06-22 21:14:54 +00:00
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetHidReport");
|
|
|
|
|
|
|
|
R_TRY(btdrvGetHidReportFwd(this->forward_service.get(), &address, type, id));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
2020-07-01 13:34:07 +00:00
|
|
|
*/
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetPairedDeviceInfo(sf::Out<bluetooth::DeviceSettings> out, bluetooth::Address address) {
|
2020-06-30 08:05:33 +00:00
|
|
|
|
2020-06-28 15:32:48 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetPairedDeviceInfo");
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-30 08:05:33 +00:00
|
|
|
auto device = reinterpret_cast<BluetoothDevicesSettings *>(out.GetPointer());
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-30 08:05:33 +00:00
|
|
|
R_TRY(btdrvGetPairedDeviceInfoFwd(this->forward_service.get(), &address, device));
|
2020-06-28 15:32:48 +00:00
|
|
|
|
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
|
|
|
|
|
|
|
if (!IsValidSwitchControllerName(device->name)) {
|
2020-07-09 19:26:22 +00:00
|
|
|
//std::strncpy(device->name, "Lic Pro Controller", sizeof(BluetoothLocalName) - 1);
|
|
|
|
std::strncpy(device->name, "Pro Controller", sizeof(BluetoothLocalName) - 1);
|
2020-06-28 15:32:48 +00:00
|
|
|
device->device_class = {0x00, 0x25, 0x08};
|
2020-06-22 21:14:54 +00:00
|
|
|
}
|
2020-06-28 15:32:48 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 19:26:22 +00:00
|
|
|
//BTDRV_LOG_FMT("name: %s\nvid: %04x\npid: %04x", device->name, device->vid, device->pid);
|
2020-06-25 21:50:43 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
2020-06-28 15:32:48 +00:00
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
Result BtdrvMitmService::FinalizeHid(void) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: FinalizeHid");
|
|
|
|
|
2020-06-13 00:08:43 +00:00
|
|
|
// Only btm should be able to make this call
|
2020-05-22 10:29:36 +00:00
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvFinalizeHidFwd(this->forward_service.get()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetHidEventInfo(sf::Out<bluetooth::HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-28 15:32:48 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetHidEventInfo");
|
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
R_TRY(bluetooth::hid::GetEventInfo(this->client_info.program_id,
|
|
|
|
out_type.GetPointer(),
|
2020-06-14 22:18:46 +00:00
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
2020-05-22 10:29:36 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-12 21:25:09 +00:00
|
|
|
/* 1.0.0 - 3.0.2 */
|
|
|
|
Result BtdrvMitmService::RegisterHidReportEventDeprecated(sf::OutCopyHandle out_handle) {
|
|
|
|
return RegisterHidReportEvent(out_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4.0.0+ */
|
2020-05-22 10:29:36 +00:00
|
|
|
Result BtdrvMitmService::RegisterHidReportEvent(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: RegisterHidReportEvent");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::hid::report::IsInitialized()) {
|
2020-06-02 21:24:40 +00:00
|
|
|
Handle handle = INVALID_HANDLE;
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvRegisterHidReportEventFwd(this->forward_service.get(), &handle));
|
|
|
|
R_TRY(bluetooth::hid::report::Initialize(handle));
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::report::GetForwardEvent()));
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::hid::report::GetUserForwardEvent()));
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
2020-05-22 10:29:36 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
/* 1.0.0 - 6.2.0 */
|
2020-07-01 13:34:07 +00:00
|
|
|
Result _GetHidReportEventInfoDeprecated(Service *srv, sf::Out<bluetooth::HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-07-09 19:26:22 +00:00
|
|
|
//BTDRV_LOG_FMT("btdrv-mitm: GetHidReportEventInfo (deprecated)");
|
2020-06-12 21:25:09 +00:00
|
|
|
|
2020-06-22 21:14:54 +00:00
|
|
|
R_TRY(bluetooth::hid::report::GetEventInfo(out_type.GetPointer(),
|
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
2020-06-12 21:25:09 +00:00
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
2020-06-12 21:25:09 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
/* 1.0.0 - 3.0.2 */
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetHidReportEventInfoDeprecated1(sf::Out<bluetooth::HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-14 22:18:46 +00:00
|
|
|
return _GetHidReportEventInfoDeprecated(this->forward_service.get(), out_type, out_buffer);
|
|
|
|
}
|
2020-06-12 21:25:09 +00:00
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
/* 4.0.0 - 6.2.0 */
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetHidReportEventInfoDeprecated2(sf::Out<bluetooth::HidEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-14 22:18:46 +00:00
|
|
|
return _GetHidReportEventInfoDeprecated(this->forward_service.get(), out_type, out_buffer);
|
2020-06-12 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 7.0.0+ */
|
2020-05-22 10:29:36 +00:00
|
|
|
Result BtdrvMitmService::GetHidReportEventInfo(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetHidReportEventInfo");
|
|
|
|
|
|
|
|
Handle handle = INVALID_HANDLE;
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvGetHidReportEventInfoFwd(this->forward_service.get(), &handle));
|
2020-06-13 17:15:59 +00:00
|
|
|
R_TRY(bluetooth::hid::report::MapRemoteSharedMemory(handle));
|
|
|
|
out_handle.SetValue(bluetooth::hid::report::GetFakeSharedMemory()->handle);
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-05-22 10:29:36 +00:00
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::InitializeBle(sf::OutCopyHandle out_handle) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: InitializeBle");
|
|
|
|
|
2020-06-14 22:18:46 +00:00
|
|
|
if (!bluetooth::ble::IsInitialized()) {
|
|
|
|
Handle handle = INVALID_HANDLE;
|
|
|
|
R_TRY(btdrvInitializeBleFwd(this->forward_service.get(), &handle));
|
|
|
|
R_TRY(bluetooth::ble::Initialize(handle));
|
2020-06-22 21:14:54 +00:00
|
|
|
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::ble::GetForwardEvent()));
|
2020-06-13 00:08:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-13 17:15:59 +00:00
|
|
|
out_handle.SetValue(os::GetReadableHandleOfSystemEvent(bluetooth::ble::GetUserForwardEvent()));
|
2020-06-13 00:08:43 +00:00
|
|
|
}
|
2020-05-22 10:29:36 +00:00
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BtdrvMitmService::FinalizeBle(void) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: FinalizeBle");
|
|
|
|
|
|
|
|
if (this->client_info.program_id == ncm::SystemProgramId::Btm) {
|
2020-06-14 22:18:46 +00:00
|
|
|
R_TRY(btdrvFinalizeBleFwd(this->forward_service.get()));
|
2020-05-22 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
2020-05-11 20:30:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetBleManagedEventInfoDeprecated(sf::Out<bluetooth::BleEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-14 23:14:09 +00:00
|
|
|
return GetBleManagedEventInfo(out_type, out_buffer);
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:34:07 +00:00
|
|
|
Result BtdrvMitmService::GetBleManagedEventInfo(sf::Out<bluetooth::BleEventType> out_type, const sf::OutPointerBuffer &out_buffer) {
|
2020-06-22 21:14:54 +00:00
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: GetBleManagedEventInfo");
|
|
|
|
|
|
|
|
R_TRY(bluetooth::ble::GetEventInfo(this->client_info.program_id,
|
|
|
|
out_type.GetPointer(),
|
2020-06-14 23:14:09 +00:00
|
|
|
static_cast<u8 *>(out_buffer.GetPointer()),
|
|
|
|
static_cast<size_t>(out_buffer.GetSize())
|
|
|
|
));
|
|
|
|
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-06-14 14:28:46 +00:00
|
|
|
void BtdrvMitmService::RedirectSystemEvents(bool redirect) {
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-06-25 21:50:43 +00:00
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: RedirectSystemEvents [%s]", redirect ? "on" : "off");
|
2020-06-02 21:24:40 +00:00
|
|
|
|
|
|
|
g_redirectEvents = redirect;
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:38:53 +00:00
|
|
|
void BtdrvMitmService::RedirectHidReportEvents(bool redirect) {
|
|
|
|
|
|
|
|
BTDRV_LOG_FMT("btdrv-mitm: RedirectHidReportEvents [%s]", redirect ? "on" : "off");
|
|
|
|
|
|
|
|
g_redirectHidReportEvents = redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-11 20:30:08 +00:00
|
|
|
}
|