|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2022 ndeadly |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms and conditions of the GNU General Public License, |
| 6 | + * version 2, as published by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | + * more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | +#include "forced_pro_controller.hpp" |
| 17 | +#include "../mcmitm_config.hpp" |
| 18 | + |
| 19 | +namespace ams::controller { |
| 20 | + |
| 21 | + ForcedProController::ForcedProController(const bluetooth::Address *address, HardwareID id) |
| 22 | + : SwitchController(address, id) {} |
| 23 | + |
| 24 | + |
| 25 | + Result ForcedProController::HandleDataReportEvent(const bluetooth::HidReportEventInfo *event_info) { |
| 26 | + const bluetooth::HidReport *report; |
| 27 | + if (hos::GetVersion() >= hos::Version_9_0_0) { |
| 28 | + report = &event_info->data_report.v9.report; |
| 29 | + } else if (hos::GetVersion() >= hos::Version_7_0_0) { |
| 30 | + report = reinterpret_cast<const bluetooth::HidReport *>(&event_info->data_report.v7.report); |
| 31 | + } else { |
| 32 | + report = reinterpret_cast<const bluetooth::HidReport *>(&event_info->data_report.v1.report); |
| 33 | + } |
| 34 | + |
| 35 | + if (!m_future_responses.empty()) { |
| 36 | + if ((m_future_responses.back()->GetType() == BtdrvHidEventType_Data) && (m_future_responses.back()->GetUserData() == report->data[0])) { |
| 37 | + m_future_responses.back()->SetData(*event_info); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + std::scoped_lock lk(m_input_mutex); |
| 42 | + |
| 43 | + this->UpdateControllerState(report); |
| 44 | + |
| 45 | + auto input_report = reinterpret_cast<SwitchInputReport *>(m_input_report.data); |
| 46 | + if (input_report->id == 0x21) { |
| 47 | + auto response = &input_report->type0x21.hid_command_response; |
| 48 | + switch (response->id) { |
| 49 | + case HidCommand_SerialFlashRead: |
| 50 | + if (response->data.serial_flash_read.address == 0x6050) { |
| 51 | + if (ams::mitm::GetSystemLanguage() == 10) { |
| 52 | + uint8_t data[] = {0xff, 0xd7, 0x00, 0x00, 0x57, 0xb7, 0x00, 0x57, 0xb7, 0x00, 0x57, 0xb7}; |
| 53 | + std::memcpy(response->data.serial_flash_read.data, data, sizeof(data)); |
| 54 | + } |
| 55 | + else { |
| 56 | + uint8_t data[] = {0x32, 0x32, 0x32, 0xe6, 0xe6, 0xe6, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46}; |
| 57 | + std::memcpy(response->data.serial_flash_read.data, data, sizeof(data)); |
| 58 | + } |
| 59 | + } |
| 60 | + break; |
| 61 | + case HidCommand_GetDeviceInfo: |
| 62 | + response->data.get_device_info.type = 0x03; |
| 63 | + response->data.get_device_info._unk2 = 0x02; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + this->ApplyButtonCombos(&input_report->buttons); |
| 69 | + |
| 70 | + return bluetooth::hid::report::WriteHidDataReport(m_address, &m_input_report); |
| 71 | + } |
| 72 | + |
| 73 | + Result ForcedProController::HandleOutputDataReport(const bluetooth::HidReport *report) { |
| 74 | + auto output_report = reinterpret_cast<const SwitchOutputReport *>(&report->data); |
| 75 | + if (output_report->id == 0x01 && (output_report->type0x01.hid_command.id == HidCommand_SerialFlashWrite || output_report->type0x01.hid_command.id == HidCommand_SerialFlashSectorErase)) { |
| 76 | + SwitchInputReport input_report = { |
| 77 | + .id = 0x21, |
| 78 | + .timer = 1, |
| 79 | + .conn_info = 1, |
| 80 | + .battery = BATTERY_MAX, |
| 81 | + .vibrator = 0, |
| 82 | + }; |
| 83 | + |
| 84 | + input_report.type0x21.hid_command_response = { |
| 85 | + .ack = 0x80, |
| 86 | + .id = output_report->type0x01.hid_command.id, |
| 87 | + .data = { |
| 88 | + .serial_flash_write = { |
| 89 | + .status = 0x1 //Force write protected response just to be safe |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + return bluetooth::hid::report::WriteHidDataReport(m_address, &m_input_report); |
| 95 | + } |
| 96 | + return this->WriteDataReport(report); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments