mc.mitm: unfuck the mess created by conflicting bluetooth type definitions in libnx master

This commit is contained in:
ndeadly 2021-03-17 02:44:49 +01:00
parent 8fb7bce267
commit a3310e0d43
9 changed files with 58 additions and 61 deletions

@ -1 +1 @@
Subproject commit e459452c4fed227fd1165ac11ca40d3de27e4e7d Subproject commit 951806cc0ecc673125ff5c6c1b7a32b0c5b82cd8

View file

@ -40,7 +40,7 @@ namespace ams::bluetooth {
struct CircularBufferPacket{ struct CircularBufferPacket{
CircularBufferPacketHeader header; CircularBufferPacketHeader header;
HidReportData data; HidReportEventInfo data;
}; };
class CircularBuffer { class CircularBuffer {

View file

@ -79,19 +79,19 @@ namespace ams::bluetooth::core {
if (program_id == ncm::SystemProgramId::Btm) { if (program_id == ncm::SystemProgramId::Btm) {
switch (g_current_event_type) { switch (g_current_event_type) {
case BtdrvEventType_DeviceFound: case BtdrvEventType_InquiryDevice:
if (controller::IsAllowedDeviceClass(&event_info->device_found.cod) && !controller::IsOfficialSwitchControllerName(event_info->device_found.name)) { if (controller::IsAllowedDeviceClass(&event_info->inquiry_device.class_of_device) && !controller::IsOfficialSwitchControllerName(event_info->inquiry_device.name)) {
std::strncpy(event_info->device_found.name, controller::pro_controller_name, sizeof(event_info->device_found.name) - 1); std::strncpy(event_info->inquiry_device.name, controller::pro_controller_name, sizeof(event_info->inquiry_device.name) - 1);
} }
break; break;
case BtdrvEventType_PinRequest: case BtdrvEventType_PairingPinCodeRequest:
if (!controller::IsOfficialSwitchControllerName(event_info->pin_reply.name)) { if (!controller::IsOfficialSwitchControllerName(event_info->pairing_pin_code_request.name)) {
std::strncpy(event_info->pin_reply.name, controller::pro_controller_name, sizeof(event_info->pin_reply.name) - 1); std::strncpy(event_info->pairing_pin_code_request.name, controller::pro_controller_name, sizeof(event_info->pairing_pin_code_request.name) - 1);
} }
break; break;
case BtdrvEventType_SspRequest: case BtdrvEventType_SspRequest:
if (!controller::IsOfficialSwitchControllerName(event_info->ssp_reply.name)) { if (!controller::IsOfficialSwitchControllerName(event_info->ssp_request.name)) {
std::strncpy(event_info->ssp_reply.name, controller::pro_controller_name, sizeof(event_info->ssp_reply.name) - 1); std::strncpy(event_info->ssp_request.name, controller::pro_controller_name, sizeof(event_info->ssp_request.name) - 1);
} }
break; break;
default: default:
@ -111,13 +111,13 @@ namespace ams::bluetooth::core {
} }
if (!g_redirect_core_events) { if (!g_redirect_core_events) {
if (g_current_event_type == BtdrvEventType_PinRequest) { if (g_current_event_type == BtdrvEventType_PairingPinCodeRequest) {
// Default pin used by bluetooth service // Default pin used by bluetooth service
bluetooth::PinCode pin = {"0000"}; bluetooth::PinCode pin = {"0000"};
uint8_t pin_length = std::strlen(pin.code); uint8_t pin_length = std::strlen(pin.code);
// Reverse host address as pin code for wii devices // Reverse host address as pin code for wii devices
if (std::strncmp(g_event_info.pin_reply.name, controller::wii_controller_prefix, std::strlen(controller::wii_controller_prefix)) == 0) { if (std::strncmp(g_event_info.pairing_pin_code_request.name, controller::wii_controller_prefix, std::strlen(controller::wii_controller_prefix)) == 0) {
// Fetch host adapter address // Fetch host adapter address
bluetooth::Address host_address; bluetooth::Address host_address;
R_ABORT_UNLESS(btdrvGetAdapterProperty(BtdrvBluetoothPropertyType_Address, &host_address, sizeof(bluetooth::Address))); R_ABORT_UNLESS(btdrvGetAdapterProperty(BtdrvBluetoothPropertyType_Address, &host_address, sizeof(bluetooth::Address)));
@ -127,7 +127,7 @@ namespace ams::bluetooth::core {
} }
// Fuck BTM, we're sending the pin response ourselves if it won't. // Fuck BTM, we're sending the pin response ourselves if it won't.
R_ABORT_UNLESS(btdrvRespondToPinRequest(g_event_info.pin_reply.address, false, &pin, pin_length)); R_ABORT_UNLESS(btdrvRespondToPinRequest(g_event_info.pairing_pin_code_request.addr, false, &pin, pin_length));
} }
else { else {
g_system_event_fwd.Signal(); g_system_event_fwd.Signal();

View file

@ -79,19 +79,19 @@ namespace ams::bluetooth::hid {
} }
void HandleConnectionStateEvent(bluetooth::HidEventInfo *event_info) { void HandleConnectionStateEvent(bluetooth::HidEventInfo *event_info) {
switch (event_info->connection_state.state) { switch (event_info->connection.status) {
case BtdrvHidConnectionState_Connected: case BtdrvHidConnectionStatus_Connected:
controller::AttachHandler(&event_info->connection_state.address); controller::AttachHandler(&event_info->connection.addr);
break; break;
case BtdrvHidConnectionState_Disconnected: case BtdrvHidConnectionStatus_Disconnected:
controller::RemoveHandler(&event_info->connection_state.address); controller::RemoveHandler(&event_info->connection.addr);
break; break;
default: default:
break; break;
} }
} }
// void HandleUnknown07Event(bluetooth::HidEventInfo *event_info) { // void HandleExtEvent(bluetooth::HidEventInfo *event_info) {
// ; // ;
// } // }
@ -102,11 +102,11 @@ namespace ams::bluetooth::hid {
} }
switch (g_current_event_type) { switch (g_current_event_type) {
case BtdrvHidEventType_ConnectionState: case BtdrvHidEventType_Connection:
HandleConnectionStateEvent(&g_event_info); HandleConnectionStateEvent(&g_event_info);
break; break;
case BtdrvHidEventType_Unknown7: case BtdrvHidEventType_Ext:
//HandleUnknown07Event(&g_event_info); //HandleExtEvent(&g_event_info);
break; break;
default: default:
break; break;

View file

@ -33,7 +33,7 @@ namespace ams::bluetooth::hid::report {
s32 g_event_handler_thread_priority = mitm::utils::ConvertToUserPriority(17); s32 g_event_handler_thread_priority = mitm::utils::ConvertToUserPriority(17);
// This is only required on fw < 7.0.0 // This is only required on fw < 7.0.0
bluetooth::HidEventInfo g_event_info; bluetooth::HidReportEventInfo g_event_info;
bluetooth::HidEventType g_current_event_type; bluetooth::HidEventType g_current_event_type;
os::SystemEvent g_system_event; os::SystemEvent g_system_event;
@ -48,7 +48,7 @@ namespace ams::bluetooth::hid::report {
bluetooth::CircularBuffer *g_real_buffer; bluetooth::CircularBuffer *g_real_buffer;
bluetooth::CircularBuffer *g_fake_buffer; bluetooth::CircularBuffer *g_fake_buffer;
bluetooth::HidReportData g_fake_report_data; bluetooth::HidReportEventInfo g_fake_report_event_info;
Service *g_forward_service; Service *g_forward_service;
os::ThreadId g_main_thread_id; os::ThreadId g_main_thread_id;
@ -140,16 +140,17 @@ namespace ams::bluetooth::hid::report {
Result WriteHidReportBuffer(const bluetooth::Address *address, const bluetooth::HidReport *report) { Result WriteHidReportBuffer(const bluetooth::Address *address, const bluetooth::HidReport *report) {
if (hos::GetVersion() < hos::Version_9_0_0) { if (hos::GetVersion() < hos::Version_9_0_0) {
g_fake_report_data.v1.size = g_fake_report_data.report.size + 0x11; // Todo: check this may still be necessary
std::memcpy(&g_fake_report_data.v1.address, address, sizeof(bluetooth::Address)); //g_fake_report_event_info.data_report.v7.size = g_fake_report_event_info.data_report.v7.report.size + 0x11;
g_fake_report_event_info.data_report.v7.addr = *address;
std::memcpy(&g_fake_report_event_info.data_report.v7.report, report, report->size + sizeof(report->size));
} }
else { else {
std::memcpy(&g_fake_report_data.v9.address, address, sizeof(bluetooth::Address)); g_fake_report_event_info.data_report.v9.addr = *address;
std::memcpy(&g_fake_report_event_info.data_report.v9.report, report, report->size + sizeof(report->size));
} }
std::memcpy(&g_fake_report_data.report, report, report->size + sizeof(report->size));
g_fake_buffer->Write(BtdrvHidEventType_GetReport, &g_fake_report_data, g_fake_report_data.report.size + 0x11);
g_fake_buffer->Write(BtdrvHidEventType_Data, &g_fake_report_event_info, report->size + 0x11);
g_system_event_fwd.Signal(); g_system_event_fwd.Signal();
return ams::ResultSuccess(); return ams::ResultSuccess();
@ -177,14 +178,14 @@ namespace ams::bluetooth::hid::report {
continue; continue;
} }
else { else {
auto event_info = reinterpret_cast<bluetooth::HidEventInfo *>(buffer); auto event_info = reinterpret_cast<bluetooth::HidReportEventInfo *>(buffer);
*type = static_cast<bluetooth::HidEventType>(packet->header.type); *type = static_cast<bluetooth::HidEventType>(packet->header.type);
std::memcpy(&event_info->get_report.address, &packet->data.v1.address, sizeof(bluetooth::Address)); event_info->data_report.v1.hdr.addr = packet->data.data_report.v7.addr;
event_info->get_report.status = BtdrvHidStatus_Ok; event_info->data_report.v1.hdr.res = 0;
event_info->get_report.report_length = packet->header.size; event_info->data_report.v1.hdr.size = packet->header.size;
event_info->data_report.v1.addr = packet->data.data_report.v7.addr;
std::memcpy(&event_info->get_report.report_data, &packet->data, packet->header.size); std::memcpy(&event_info->data_report.v1.report, &packet->data.data_report.v7.report, packet->header.size);
break; break;
} }
} }
@ -195,20 +196,20 @@ namespace ams::bluetooth::hid::report {
void HandleEvent(void) { void HandleEvent(void) {
if (!g_redirect_hid_report_events) { if (!g_redirect_hid_report_events) {
if (hos::GetVersion() < hos::Version_7_0_0) { if (hos::GetVersion() < hos::Version_7_0_0) {
R_ABORT_UNLESS(btdrvGetHidReportEventInfo(&g_event_info, sizeof(bluetooth::HidEventInfo), &g_current_event_type)); R_ABORT_UNLESS(btdrvGetHidReportEventInfo(&g_event_info, sizeof(bluetooth::HidReportEventInfo), &g_current_event_type));
switch (g_current_event_type) { switch (g_current_event_type) {
case BtdrvHidEventType_GetReport: case BtdrvHidEventType_Data:
{ {
auto device = controller::LocateHandler(&g_event_info.get_report.address); auto device = controller::LocateHandler(&g_event_info.data_report.v1.addr);
if (!device) if (!device)
return; return;
device->HandleIncomingReport(&g_event_info.get_report.report_data.report); device->HandleIncomingReport(reinterpret_cast<bluetooth::HidReport *>(&g_event_info.data_report.v1.report));
} }
break; break;
default: default:
g_fake_buffer->Write(g_current_event_type, &g_event_info.get_report.report_data, g_event_info.get_report.report_length); g_fake_buffer->Write(g_current_event_type, &g_event_info.data_report.v1.report.data, g_event_info.data_report.v1.report.size);
break; break;
} }
} }
@ -223,13 +224,14 @@ namespace ams::bluetooth::hid::report {
switch (real_packet->header.type) { switch (real_packet->header.type) {
case 0xff: case 0xff:
continue; continue;
case BtdrvHidEventType_GetReport: case BtdrvHidEventType_Data:
{ {
auto device = controller::LocateHandler(hos::GetVersion() < hos::Version_9_0_0 ? &real_packet->data.v1.address : &real_packet->data.v9.address); auto device = controller::LocateHandler(hos::GetVersion() < hos::Version_9_0_0 ? &real_packet->data.data_report.v7.addr : &real_packet->data.data_report.v9.addr);
if (!device) if (!device)
continue; continue;
device->HandleIncomingReport(&real_packet->data.report); auto report = hos::GetVersion() < hos::Version_9_0_0 ? reinterpret_cast<bluetooth::HidReport *>(&real_packet->data.data_report.v7.report) : &real_packet->data.data_report.v9.report;
device->HandleIncomingReport(report);
} }
break; break;
default: default:

View file

@ -20,17 +20,13 @@
namespace ams::bluetooth { namespace ams::bluetooth {
typedef BtdrvAddress Address; typedef BtdrvAddress Address;
typedef BtdrvDeviceClass DeviceClass; typedef BtdrvClassOfDevice DeviceClass;
typedef BtdrvBluetoothPinCode PinCode; typedef BtdrvBluetoothPinCode PinCode;
typedef BtdrvAdapterProperty AdapterProperty; typedef BtdrvAdapterProperty AdapterProperty;
typedef BtdrvHidReport HidReport; typedef BtdrvHidReport HidReport;
typedef BtdrvBluetoothHhReportType HhReportType; typedef BtdrvBluetoothHhReportType HhReportType;
typedef SetSysBluetoothDevicesSettings DevicesSettings; typedef SetSysBluetoothDevicesSettings DevicesSettings;
typedef BtdrvBluetoothSspVariant SspVariant;
typedef BtdrvBluetoothTransport Transport;
typedef BtdrvBluetoothDiscoveryState DiscoveryState;
typedef BtdrvBluetoothBondState BondState;
typedef BtdrvEventType EventType; typedef BtdrvEventType EventType;
typedef BtdrvEventInfo EventInfo; typedef BtdrvEventInfo EventInfo;
@ -40,7 +36,6 @@ namespace ams::bluetooth {
typedef BtdrvBleEventType BleEventType; typedef BtdrvBleEventType BleEventType;
typedef BtdrvBleEventInfo BleEventInfo; typedef BtdrvBleEventInfo BleEventInfo;
typedef BtdrvHidConnectionState HidConnectionState; typedef BtdrvHidReportEventInfo HidReportEventInfo;
typedef BtdrvHidReportData HidReportData;
} }

View file

@ -130,7 +130,7 @@ namespace ams::mitm::bluetooth {
uint32_t status; uint32_t status;
} event_data = {tsi == 0xff ? 1u : 0u, address, {0, 0}, 0}; } event_data = {tsi == 0xff ? 1u : 0u, address, {0, 0}, 0};
ams::bluetooth::hid::SignalFakeEvent(BtdrvHidEventType_Unknown7, &event_data, sizeof(event_data)); ams::bluetooth::hid::SignalFakeEvent(BtdrvHidEventType_Ext, &event_data, sizeof(event_data));
} }
else { else {
const struct { const struct {
@ -140,7 +140,7 @@ namespace ams::mitm::bluetooth {
uint8_t pad[2]; uint8_t pad[2];
} event_data = {tsi == 0xff ? 1u : 0u, 0, address, {0, 0}}; } event_data = {tsi == 0xff ? 1u : 0u, 0, address, {0, 0}};
ams::bluetooth::hid::SignalFakeEvent(BtdrvHidEventType_Unknown7, &event_data, sizeof(event_data)); ams::bluetooth::hid::SignalFakeEvent(BtdrvHidEventType_Ext, &event_data, sizeof(event_data));
} }
return ams::ResultSuccess(); return ams::ResultSuccess();

View file

@ -65,10 +65,10 @@ namespace ams::mitm::btm {
auto device_info = reinterpret_cast<BtmDeviceInfoList *>(out.GetPointer()); auto device_info = reinterpret_cast<BtmDeviceInfoList *>(out.GetPointer());
R_TRY(btmGetDeviceInfoFwd(this->forward_service.get(), device_info)); R_TRY(btmGetDeviceInfoFwd(this->forward_service.get(), device_info));
for (unsigned int i = 0; i < device_info->total_entries; ++i) { for (unsigned int i = 0; i < device_info->device_count; ++i) {
auto device = &device_info->devices[i]; auto device = &device_info->devices[i];
if (!controller::IsOfficialSwitchControllerName(device->name)) { if (!controller::IsOfficialSwitchControllerName(device->name.name)) {
std::strncpy(device->name, controller::pro_controller_name, sizeof(device->name) - 1); std::strncpy(device->name.name, controller::pro_controller_name, sizeof(device->name) - 1);
} }
} }

View file

@ -50,7 +50,7 @@ namespace ams::controller {
ControllerType Identify(const bluetooth::DevicesSettings *device) { ControllerType Identify(const bluetooth::DevicesSettings *device) {
if (IsOfficialSwitchControllerName(device->name)) if (IsOfficialSwitchControllerName(device->name.name))
return ControllerType_Switch; return ControllerType_Switch;
for (auto hwId : WiiController::hardware_ids) { for (auto hwId : WiiController::hardware_ids) {
@ -165,8 +165,8 @@ namespace ams::controller {
} }
bool IsAllowedDeviceClass(const bluetooth::DeviceClass *cod) { bool IsAllowedDeviceClass(const bluetooth::DeviceClass *cod) {
return ((cod->cod[1] & 0x0f) == cod_major_peripheral) && return ((cod->class_of_device[1] & 0x0f) == cod_major_peripheral) &&
(((cod->cod[2] & 0x0f) == cod_minor_gamepad) || ((cod->cod[2] & 0x0f) == cod_minor_joystick) || ((cod->cod[2] & 0x40) == cod_minor_keyboard)); (((cod->class_of_device[2] & 0x0f) == cod_minor_gamepad) || ((cod->class_of_device[2] & 0x0f) == cod_minor_joystick) || ((cod->class_of_device[2] & 0x40) == cod_minor_keyboard));
} }
bool IsOfficialSwitchControllerName(const std::string& name) { bool IsOfficialSwitchControllerName(const std::string& name) {