Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeadly committed Dec 11, 2020
2 parents 12741f8 + 42ebafc commit dfd00c7
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 244 deletions.
2 changes: 1 addition & 1 deletion Atmosphere-libs
Submodule Atmosphere-libs updated 105 files
32 changes: 16 additions & 16 deletions bluetooth-mitm/source/btdrv_mitm/bluetooth/bluetooth_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, sizeof(bluetooth::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, sizeof(bluetooth::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, sizeof(bluetooth::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:
Expand All @@ -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));
Expand All @@ -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);
Expand Down
36 changes: 18 additions & 18 deletions bluetooth-mitm/source/btdrv_mitm/bluetooth/bluetooth_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 15 additions & 15 deletions bluetooth-mitm/source/btdrv_mitm/bluetooth/bluetooth_hid_report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;

Expand Down
Loading

0 comments on commit dfd00c7

Please sign in to comment.