bluetooth-mitm: update for ams 0.16.1 (11.0.1 support)

This commit is contained in:
ndeadly 2020-12-12 00:07:41 +01:00
parent e971110fdf
commit 42ebafced7
8 changed files with 66 additions and 225 deletions

@ -1 +1 @@
Subproject commit 2c3ccef17e9b267a5d9d232f1aba689f2c591b95
Subproject commit 59b4e75021fbaf3fcf4c0e8dd2c07e309e1869d1

View file

@ -26,8 +26,8 @@ namespace ams::bluetooth::core {
std::atomic<bool> g_is_initialized(false);
os::Mutex g_event_data_lock(false);
uint8_t g_event_data_buffer[0x400];
os::Mutex g_event_info_lock(false);
uint8_t g_event_info_buffer[0x400];
BtdrvEventType g_current_event_type;
os::SystemEventType g_system_event;
@ -74,27 +74,27 @@ namespace ams::bluetooth::core {
}
Result GetEventInfo(ncm::ProgramId program_id, EventType *type, uint8_t* buffer, size_t size) {
std::scoped_lock lk(g_event_data_lock);
std::scoped_lock lk(g_event_info_lock);
*type = g_current_event_type;
std::memcpy(buffer, g_event_data_buffer, size);
std::memcpy(buffer, g_event_info_buffer, size);
auto event_data = reinterpret_cast<EventData *>(buffer);
auto event_info = reinterpret_cast<bluetooth::EventInfo *>(buffer);
if (program_id == ncm::SystemProgramId::Btm) {
switch (g_current_event_type) {
case BtdrvEventType_DeviceFound:
if (controller::IsAllowedDevice(&event_data->device_found.cod) && !controller::IsOfficialSwitchControllerName(event_data->device_found.name)) {
std::strncpy(event_data->device_found.name, controller::pro_controller_name, sizeof(bluetooth::Name) - 1);
if (controller::IsAllowedDeviceClass(&event_info->device_found.cod) && !controller::IsOfficialSwitchControllerName(event_info->device_found.name)) {
std::strncpy(event_info->device_found.name, controller::pro_controller_name, sizeof(event_info->device_found.name) - 1);
}
break;
case BtdrvEventType_PinRequest:
if (!controller::IsOfficialSwitchControllerName(event_data->pin_reply.name)) {
std::strncpy(event_data->pin_reply.name, controller::pro_controller_name, sizeof(bluetooth::Name) - 1);
if (!controller::IsOfficialSwitchControllerName(event_info->pin_reply.name)) {
std::strncpy(event_info->pin_reply.name, controller::pro_controller_name, sizeof(event_info->pin_reply.name) - 1);
}
break;
case BtdrvEventType_SspRequest:
if (!controller::IsOfficialSwitchControllerName(event_data->ssp_reply.name)) {
std::strncpy(event_data->ssp_reply.name, controller::pro_controller_name, sizeof(bluetooth::Name) - 1);
if (!controller::IsOfficialSwitchControllerName(event_info->ssp_reply.name)) {
std::strncpy(event_info->ssp_reply.name, controller::pro_controller_name, sizeof(event_info->ssp_reply.name) - 1);
}
break;
default:
@ -109,19 +109,19 @@ namespace ams::bluetooth::core {
void HandleEvent(void) {
{
std::scoped_lock lk(g_event_data_lock);
R_ABORT_UNLESS(btdrvGetEventInfo(g_event_data_buffer, sizeof(g_event_data_buffer), &g_current_event_type));
std::scoped_lock lk(g_event_info_lock);
R_ABORT_UNLESS(btdrvGetEventInfo(g_event_info_buffer, sizeof(g_event_info_buffer), &g_current_event_type));
}
if (!g_redirect_core_events) {
if (g_current_event_type == BtdrvEventType_PinRequest) {
auto event_data = reinterpret_cast<EventData *>(g_event_data_buffer);
auto event_info = reinterpret_cast<bluetooth::EventInfo *>(g_event_info_buffer);
bluetooth::PinCode pin_code = {0x30, 0x30, 0x30, 0x30};
uint8_t pin_length = sizeof(uint32_t);
// Reverse host address as pin code for wii devices
if (std::strncmp(event_data->pin_reply.name, controller::wii_controller_prefix, std::strlen(controller::wii_controller_prefix)) == 0) {
if (std::strncmp(event_info->pin_reply.name, controller::wii_controller_prefix, std::strlen(controller::wii_controller_prefix)) == 0) {
// Fetch host adapter properties
AdapterProperty properties;
R_ABORT_UNLESS(btdrvGetAdapterProperties(&properties));
@ -131,7 +131,7 @@ namespace ams::bluetooth::core {
}
// Fuck BTM, we're sending the pin response ourselves if it won't.
R_ABORT_UNLESS(btdrvRespondToPinRequest(event_data->pin_reply.address, false, &pin_code, pin_length));
R_ABORT_UNLESS(btdrvRespondToPinRequest(event_info->pin_reply.address, false, &pin_code, pin_length));
}
else {
os::SignalSystemEvent(&g_system_event_fwd);

View file

@ -26,8 +26,8 @@ namespace ams::bluetooth::hid {
std::atomic<bool> g_is_initialized(false);
os::Mutex g_event_data_lock(false);
uint8_t g_event_data_buffer[0x480];
os::Mutex g_event_info_lock(false);
uint8_t g_event_info_buffer[0x480];
HidEventType g_current_event_type;
os::SystemEventType g_system_event;
@ -74,52 +74,52 @@ namespace ams::bluetooth::hid {
}
Result GetEventInfo(ncm::ProgramId program_id, HidEventType *type, uint8_t* buffer, size_t size) {
std::scoped_lock lk(g_event_data_lock);
std::scoped_lock lk(g_event_info_lock);
*type = g_current_event_type;
std::memcpy(buffer, g_event_data_buffer, size);
std::memcpy(buffer, g_event_info_buffer, size);
os::SignalEvent(&g_data_read_event);
return ams::ResultSuccess();
}
void handleConnectionStateEvent(HidEventData *event_data) {
switch (event_data->connection_state.state) {
case BluetoothHidConnectionState_Connected:
controller::AttachHandler(&event_data->connection_state.address);
void handleConnectionStateEvent(bluetooth::HidEventInfo *event_info) {
switch (event_info->connection_state.state) {
case BtdrvHidConnectionState_Connected:
controller::AttachHandler(&event_info->connection_state.address);
break;
case BluetoothHidConnectionState_Disconnected:
controller::RemoveHandler(&event_data->connection_state.address);
case BtdrvHidConnectionState_Disconnected:
controller::RemoveHandler(&event_info->connection_state.address);
break;
default:
break;
}
}
void handleUnknown07Event(HidEventData *event_data) {
void handleUnknown07Event(bluetooth::HidEventInfo *event_info) {
// Fix for xbox one disconnection. Don't know what this value is for, but it appears to be 0 for other controllers
if (hos::GetVersion() < hos::Version_9_0_0)
event_data->unknown07._unk1 = 0;
event_info->type7.v1.unk_xC = 0;
else
event_data->unknown07.v2._unk1 = 0;
event_info->type7.v9.unk_x4 = 0;
}
void HandleEvent(void) {
{
std::scoped_lock lk(g_event_data_lock);
R_ABORT_UNLESS(btdrvGetHidEventInfo(g_event_data_buffer, sizeof(g_event_data_buffer), &g_current_event_type));
std::scoped_lock lk(g_event_info_lock);
R_ABORT_UNLESS(btdrvGetHidEventInfo(g_event_info_buffer, sizeof(g_event_info_buffer), &g_current_event_type));
}
auto event_data = reinterpret_cast<HidEventData *>(g_event_data_buffer);
auto event_info = reinterpret_cast<bluetooth::HidEventInfo *>(g_event_info_buffer);
switch (g_current_event_type) {
case BtdrvHidEventType_ConnectionState:
handleConnectionStateEvent(event_data);
handleConnectionStateEvent(event_info);
break;
case BtdrvHidEventType_Unknown7:
handleUnknown07Event(event_data);
handleUnknown07Event(event_info);
break;
default:
break;

View file

@ -36,7 +36,7 @@ namespace ams::bluetooth::hid::report {
s32 g_event_handler_thread_priority = mitm::utils::ConvertToUserPriority(17);
// This is only required on fw < 7.0.0
uint8_t g_event_data_buffer[0x480];
uint8_t g_event_info_buffer[0x480];
bluetooth::HidEventType g_current_event_type;
os::SystemEventType g_system_event;
@ -145,11 +145,11 @@ namespace ams::bluetooth::hid::report {
Result WriteHidReportBuffer(const bluetooth::Address *address, const bluetooth::HidReport *report) {
if (hos::GetVersion() < hos::Version_9_0_0) {
g_fake_report_data.size = g_fake_report_data.report.size + 0x11;
std::memcpy(&g_fake_report_data.address, address, sizeof(bluetooth::Address));
g_fake_report_data.v1.size = g_fake_report_data.report.size + 0x11;
std::memcpy(&g_fake_report_data.v1.address, address, sizeof(bluetooth::Address));
}
else {
std::memcpy(&g_fake_report_data.v2.address, address, sizeof(bluetooth::Address));
std::memcpy(&g_fake_report_data.v9.address, address, sizeof(bluetooth::Address));
}
std::memcpy(&g_fake_report_data.report, report, report->size + sizeof(report->size));
@ -182,14 +182,14 @@ namespace ams::bluetooth::hid::report {
continue;
}
else {
auto event_data = reinterpret_cast<bluetooth::HidEventData *>(buffer);
auto event_info = reinterpret_cast<bluetooth::HidEventInfo *>(buffer);
*type = static_cast<bluetooth::HidEventType>(packet->header.type);
std::memcpy(&event_data->get_report.address, &packet->data.address, sizeof(bluetooth::Address));
event_data->get_report.status = BluetoothHidStatus_Ok;
event_data->get_report.report_length = packet->header.size;
std::memcpy(&event_info->get_report.address, &packet->data.v1.address, sizeof(bluetooth::Address));
event_info->get_report.status = BtdrvHidStatus_Ok;
event_info->get_report.report_length = packet->header.size;
std::memcpy(&event_data->get_report.report_data, &packet->data, packet->header.size);
std::memcpy(&event_info->get_report.report_data, &packet->data, packet->header.size);
break;
}
}
@ -200,21 +200,21 @@ namespace ams::bluetooth::hid::report {
void HandleEvent(void) {
if (!g_redirect_hid_report_events) {
if (hos::GetVersion() < hos::Version_7_0_0) {
auto event_data = reinterpret_cast<bluetooth::HidEventData *>(g_event_data_buffer);
R_ABORT_UNLESS(btdrvGetHidReportEventInfo(g_event_data_buffer, sizeof(g_event_data_buffer), &g_current_event_type));
auto event_info = reinterpret_cast<bluetooth::HidEventInfo *>(g_event_info_buffer);
R_ABORT_UNLESS(btdrvGetHidReportEventInfo(g_event_info_buffer, sizeof(g_event_info_buffer), &g_current_event_type));
switch (g_current_event_type) {
case BtdrvHidEventType_GetReport:
{
auto device = controller::LocateHandler(&event_data->get_report.address);
auto device = controller::LocateHandler(&event_info->get_report.address);
if (!device)
return;
device->HandleIncomingReport(&event_data->get_report.report_data.report);
device->HandleIncomingReport(&event_info->get_report.report_data.report);
}
break;
default:
g_fake_buffer->Write(g_current_event_type, &event_data->get_report.report_data, event_data->get_report.report_length);
g_fake_buffer->Write(g_current_event_type, &event_info->get_report.report_data, event_info->get_report.report_length);
break;
}
}
@ -231,7 +231,7 @@ namespace ams::bluetooth::hid::report {
continue;
case BtdrvHidEventType_GetReport:
{
auto device = controller::LocateHandler(hos::GetVersion() < hos::Version_9_0_0 ? &real_packet->data.address : &real_packet->data.v2.address);
auto device = controller::LocateHandler(hos::GetVersion() < hos::Version_9_0_0 ? &real_packet->data.v1.address : &real_packet->data.v9.address);
if (!device)
continue;

View file

@ -19,187 +19,28 @@
namespace ams::bluetooth {
typedef char Name[0xf9];
typedef BtdrvAddress Address;
typedef BtdrvDeviceClass DeviceClass;
typedef BtdrvBluetoothPinCode PinCode;
typedef BtdrvAdapterProperty AdapterProperty;
typedef BtdrvHidReport HidReport;
typedef BtdrvBluetoothHhReportType HhReportType;
typedef SetSysBluetoothDevicesSettings DevicesSettings;
typedef BtdrvBluetoothSspVariant SspVariant;
typedef BtdrvBluetoothTransport Transport;
typedef BtdrvBluetoothDiscoveryState DiscoveryState;
typedef BtdrvBluetoothBondState BondState;
typedef BtdrvEventType EventType;
typedef BtdrvEventInfo EventInfo;
typedef BtdrvHidEventType HidEventType;
typedef BtdrvHidEventInfo HidEventInfo;
typedef BtdrvBleEventType BleEventType;
typedef BtdrvBleEventInfo BleEventInfo;
struct DeviceClass {
u8 cod[0x3];
};
struct HidReportData {
union {
// Pre 9.0.0
struct {
u16 size;
u8 _unk0;
Address address;
u8 _unk1[3];
};
// 9.0.0+
struct {
u8 _unk0[5];
Address address;
u8 _unk1;
} v2;
};
HidReport report;
};
enum SspVariant {
BluetoothSspVariant_PasskeyConfirmation,
BluetoothSspVariant_PasskeyEntry,
BluetoothSspVariant_Consent,
BluetoothSspVariant_PasskeyNotification
};
enum Transport {
BluetoothTransport_Auto,
BluetoothTransport_BREDR,
BluetoothTransport_LE
};
enum DiscoveryState {
BluetoothDiscoveryState_Stopped,
BluetoothDiscoveryState_Started
};
enum BondState {
BluetoothBondState_None,
BluetoothBondState_Bonding,
BluetoothBondState_Bonded
};
enum Status {
BluetoothStatus_Success,
BluetoothStatus_Fail,
BluetoothStatus_NotReady,
BluetoothStatus_NoMemory,
BluetoothStatus_Busy,
BluetoothStatus_Done,
BluetoothStatus_Unsupported,
BluetoothStatus_ParameterInvalid,
BluetoothStatus_Unhandled,
BluetoothStatus_AuthenticationFailure,
BluetoothStatus_RemoteDeviceDown,
BluetoothStatus_AuthenticationRejected,
BluetoothStatus_JniEnvironmentError,
BluetoothStatus_JniThreadAttachError,
BluetoothStatus_WakelockError
};
enum HidConnectionState {
BluetoothHidConnectionState_Connected = 0,
BluetoothHidConnectionState_Connecting,
BluetoothHidConnectionState_Disconnected,
BluetoothHidConnectionState_Disconnecting,
BluetoothHidConnectionState_FailedMouseFromHost,
BluetoothHidConnectionState_FailedKeyboardFromHost,
BluetoothHidConnectionState_FailedTooManyDevices,
BluetoothHidConnectionState_FailedNoBluetoothHidDriver,
BluetoothHidConnectionState_FailedGeneric,
BluetoothHidConnectionState_Unknown
};
enum HidStatus {
BluetoothHidStatus_Ok = 0,
BluetoothHidStatus_HandshakeHidNotReady,
BluetoothHidStatus_HandshakeInvalidReportId,
BluetoothHidStatus_HandshakeTransactionNotSpt,
BluetoothHidStatus_HandshakeInvalidParameter,
BluetoothHidStatus_HandshakeError,
BluetoothHidStatus_Error,
BluetoothHidStatus_ErrorSdp,
BluetoothHidStatus_ErrorProtocol,
BluetoothHidStatus_ErrorDatabaseFull,
BluetoothHidStatus_ErrorDeviceTypeUnsupported,
BluetoothHidStatus_ErrorNoResources,
BluetoothHidStatus_ErrorAuthenicationFailed,
BluetoothHidStatus_ErrorHdl
};
union EventData {
u8 raw[0x480];
struct __attribute__ ((__packed__)) {
Name name;
Address address;
u8 uuid[0x10];
DeviceClass cod;
/* + more items we don't care about */
u8 _unk0;
u8 _unk1[0x252];
u32 _unk2;
} device_found;
struct {
DiscoveryState state;
} discovery_state;
struct {
Address address;
Name name;
DeviceClass cod;
} pin_reply;
struct {
Address address;
Name name;
DeviceClass cod;
SspVariant variant;
u32 passkey;
} ssp_reply;
union {
struct {
Address address;
Status status;
BondState state;
};
struct {
Status status;
Address address;
BondState state;
} v2;
} bond_state;
};
union HidEventData {
u8 raw[0x480];
struct {
Address address;
HidConnectionState state;
} connection_state;
struct {
Address address;
HidStatus status;
u32 report_length;
HidReportData report_data;
} get_report;
union {
struct {
Address address;
u32 _unk0;
u32 _unk1;
};
struct {
u32 _unk0;
u32 _unk1;
Address address;
} v2;
} unknown07;
};
typedef BtdrvHidConnectionState HidConnectionState;
typedef BtdrvHidReportData HidReportData;
}

View file

@ -158,7 +158,7 @@ namespace ams::controller {
return ControllerType_Unknown;
}
bool IsAllowedDevice(const bluetooth::DeviceClass *cod) {
bool IsAllowedDeviceClass(const bluetooth::DeviceClass *cod) {
return ((cod->cod[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));
}

View file

@ -72,7 +72,7 @@ namespace ams::controller {
};
ControllerType Identify(const bluetooth::DevicesSettings *device);
bool IsAllowedDevice(const bluetooth::DeviceClass *cod);
bool IsAllowedDeviceClass(const bluetooth::DeviceClass *cod);
bool IsOfficialSwitchControllerName(const std::string& name);
void AttachHandler(const bluetooth::Address *address);

2
libnx

@ -1 +1 @@
Subproject commit 9d939bcd7e821cef3cb8ef2529b409252b883041
Subproject commit be5801d3d67bc6e823e5d5a8e2582ed9e4f0ec22