mc.mitm: add locks to control access to controller io buffers

This commit is contained in:
ndeadly 2022-09-07 23:27:06 +02:00
parent dca863fb51
commit 6be4ebb7ad
7 changed files with 33 additions and 2 deletions

View file

@ -265,6 +265,8 @@ namespace ams::controller {
}
Result DualsenseController::PushRumbleLedState() {
std::scoped_lock lk(m_output_mutex);
DualsenseReportData report = {};
report.id = 0x31;
report.output0x31.data[0] = 0x02;

View file

@ -224,6 +224,8 @@ namespace ams::controller {
}
Result Dualshock4Controller::PushRumbleLedState() {
std::scoped_lock lk(m_output_mutex);
Dualshock4ReportData report = {};
report.id = 0x11;
report.output0x11.data[0] = static_cast<uint8_t>(0xc0 | (m_report_rate & 0xff));

View file

@ -556,6 +556,8 @@ namespace ams::controller {
}
Result EmulatedSwitchController::FakeSubCmdResponse(const SwitchSubcommandResponse *response) {
std::scoped_lock lk(m_input_mutex);
m_input_report.size = sizeof(SwitchInputReport0x21) + 1;
auto report_data = reinterpret_cast<SwitchReportData *>(m_input_report.data);
report_data->id = 0x21;

View file

@ -97,6 +97,8 @@ namespace ams::controller {
}
}
std::scoped_lock lk(m_input_mutex);
this->UpdateControllerState(report);
auto switch_report = reinterpret_cast<SwitchReportData *>(m_input_report.data);

View file

@ -350,7 +350,9 @@ namespace ams::controller {
SwitchController(const bluetooth::Address *address, HardwareID id)
: m_address(*address)
, m_id(id)
, m_settsi_supported(true) { }
, m_settsi_supported(true)
, m_input_mutex(false)
, m_output_mutex(false) { }
virtual ~SwitchController() { };
@ -382,7 +384,10 @@ namespace ams::controller {
bool m_settsi_supported;
os::Mutex m_input_mutex;
bluetooth::HidReport m_input_report;
os::Mutex m_output_mutex;
bluetooth::HidReport m_output_report;
std::queue<std::shared_ptr<HidResponse>> m_future_responses;

View file

@ -737,6 +737,8 @@ namespace ams::controller {
}
Result WiiController::SetReportMode(uint8_t mode) {
std::scoped_lock lk(m_output_mutex);
m_output_report.size = sizeof(WiiOutputReport0x12) + 1;
auto report_data = reinterpret_cast<WiiReportData *>(m_output_report.data);
report_data->id = 0x12;
@ -748,6 +750,8 @@ namespace ams::controller {
}
Result WiiController::QueryStatus() {
std::scoped_lock lk(m_output_mutex);
m_output_report.size = sizeof(WiiOutputReport0x15) + 1;
auto report_data = reinterpret_cast<WiiReportData *>(m_output_report.data);
report_data->id = 0x15;
@ -757,12 +761,14 @@ namespace ams::controller {
return ams::ResultSuccess();
}
Result WiiController::WriteMemory(uint32_t write_addr, const void *data, uint8_t size) {
Result WiiController::WriteMemory(uint32_t write_addr, const void *data, uint8_t size) {
os::SleepThread(ams::TimeSpan::FromMilliSeconds(30));
Result result;
auto output = std::make_unique<bluetooth::HidReport>();
std::scoped_lock lk(m_output_mutex);
int attempts = 0;
do {
m_output_report.size = sizeof(WiiOutputReport0x16) + 1;
@ -786,6 +792,8 @@ namespace ams::controller {
Result result;
auto output = std::make_unique<bluetooth::HidReport>();
std::scoped_lock lk(m_output_mutex);
int attempts = 0;
do {
m_output_report.size = sizeof(WiiOutputReport0x17) + 1;
@ -909,6 +917,8 @@ namespace ams::controller {
rumble_data[1].low_band_amp > 0 ||
rumble_data[1].high_band_amp > 0;
std::scoped_lock lk(m_output_mutex);
m_output_report.size = sizeof(WiiOutputReport0x10) + 1;
auto report_data = reinterpret_cast<WiiReportData *>(m_output_report.data);
report_data->id = 0x10;
@ -920,6 +930,8 @@ namespace ams::controller {
Result WiiController::CancelVibration() {
m_rumble_state = 0;
std::scoped_lock lk(m_output_mutex);
m_output_report.size = sizeof(WiiOutputReport0x10) + 1;
auto report_data = reinterpret_cast<WiiReportData *>(m_output_report.data);
report_data->id = 0x10;
@ -929,6 +941,8 @@ namespace ams::controller {
}
Result WiiController::SetPlayerLed(uint8_t led_mask) {
std::scoped_lock lk(m_output_mutex);
m_output_report.size = sizeof(WiiOutputReport0x11) + 1;
auto report_data = reinterpret_cast<WiiReportData *>(m_output_report.data);
report_data->id = 0x11;

View file

@ -29,9 +29,13 @@ namespace ams::controller {
Result XiaomiController::Initialize() {
R_TRY(EmulatedSwitchController::Initialize());
std::scoped_lock lk(m_output_mutex);
m_output_report.size = sizeof(init_packet);
std::memcpy(m_output_report.data, init_packet, sizeof(init_packet));
R_TRY(this->WriteDataReport(&m_output_report));
return ams::ResultSuccess();
}