diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.cpp index 0c42810c65..75809d9f5d 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.cpp @@ -22,9 +22,27 @@ constexpr std::chrono::milliseconds CANCEL_POLL_PERIOD(50); // 1.5 frames. constexpr std::chrono::milliseconds ONE_FRAME(17); +// "GRANT 0" holds the emulator where it is and says which frame that is. +uint64_t hold_and_read_frame(Logger& logger, Connection& connection){ + std::string reply = connection.command("GRANT 0"); + std::optional frame = parse_ok_frame_reply(reply); + if (!frame){ + throw ConnectionException(&logger, std::format("MgbaLink: unexpected GRANT reply: {}", reply)); + } + return *frame; +} + } // namespace +JumpCommand JumpCommand::load_state(std::string_view file){ + return JumpCommand(std::format("LOAD {}", file)); +} +JumpCommand JumpCommand::reset(){ + return JumpCommand("RESET"); +} + + KeySender::KeySender( Logger& logger, ThreadPool& thread_pool, @@ -69,7 +87,8 @@ KeySender::Attachment& KeySender::attach(){ )); } if (m_attachment && m_attachment->serial == link->serial){ - if (m_pacer && !m_pacer->running()){ + // No pacer: a LOAD or RESET lost the connection before granting again. + if (!m_pacer || !m_pacer->running()){ throw ConnectionException(&m_logger, "MgbaLink: the connection to mGBA was lost."); } return *m_attachment; @@ -80,15 +99,11 @@ KeySender::Attachment& KeySender::attach(){ m_attachment.reset(); m_input_log = InputLog(); - // "GRANT 0" holds the emulator where it is and says which frame that - // is: the anchor. The pacer releases it from there. + // The frame the emulator is held on is the anchor. The pacer releases + // it from there. WallClock now = current_time(); - std::string reply = link->connection->command("GRANT 0"); - std::optional frame = parse_ok_frame_reply(reply); - if (!frame){ - throw ConnectionException(&m_logger, "MgbaLink: unexpected GRANT reply: " + reply); - } - FrameClock clock(now, *frame); + uint64_t frame = hold_and_read_frame(m_logger, *link->connection); + FrameClock clock(now, frame); m_attachment.emplace(Attachment{ .serial = link->serial, .connection = link->connection, @@ -96,7 +111,7 @@ KeySender::Attachment& KeySender::attach(){ .schedule = KeySchedule(clock), }); m_pacer.emplace(m_logger, m_thread_pool, link->connection, clock, m_grant_period); - m_logger.log(std::format("MgbaLink: controller on {} anchored at frame {}.", m_address, *frame)); + m_logger.log(std::format("MgbaLink: controller on {} anchored at frame {}.", m_address, frame)); return *m_attachment; } @@ -220,6 +235,47 @@ void KeySender::replace_on_next(){ m_cv.notify_all(); } +uint64_t KeySender::jump(const JumpCommand& command){ + std::lock_guard lg(m_lock); + // What waits to be sent was meant for the frames before the jump. + m_epoch++; + m_replace_pending = false; + m_cv.notify_all(); + Attachment& attachment = attach(); + std::shared_ptr link = connection(attachment); + std::string_view line = command.line(); + std::string_view name = line.substr(0, line.find(' ')); + + // Stop granting first: a grant on the old frames would stall mGBA + // after a jump ahead, or let it run free after a jump back. + m_pacer.reset(); + WallClock now; + uint64_t frame = 0; + try{ + link->command(line); + now = current_time(); + frame = hold_and_read_frame(m_logger, *link); + }catch (ConnectionException&){ + // A refused command leaves mGBA where it was: the old clock holds. + m_pacer.emplace(m_logger, m_thread_pool, attachment.connection, attachment.clock, m_grant_period); + throw; + } + + // The bridge dropped its queued KEYS but still holds the last keys it + // applied: release them on the first frame of the new timeline. + bool keys_sent = attachment.last_sent_frame.has_value(); + attachment.clock = FrameClock(now, frame); + attachment.last_sent_frame.reset(); + m_input_log = InputLog(); + if (keys_sent){ + send(attachment, KeysCommand{frame, 0}); + } + restart_schedule(attachment); + m_pacer.emplace(m_logger, m_thread_pool, attachment.connection, attachment.clock, m_grant_period); + m_logger.log(std::format("MgbaLink: {} on {}: re-anchored at frame {}.", name, m_address, frame)); + return frame; +} + std::string KeySender::input_log_text() const{ std::lock_guard lg(m_lock); return m_input_log.to_text(); diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.h index 610568a848..35080a8554 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.h +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender.h @@ -14,8 +14,14 @@ * then. A cancel then drops what is not sent yet and releases the keys at * the first frame no sent command can override. * + * LOAD and RESET move mGBA to another frame, under the clock's anchor. The + * sender stops granting, drops what is not sent yet, sends the command + * (the bridge clears its queued KEYS and holds at the new frame), reads + * the new frame with "GRANT 0", anchors a new clock and input log there, + * releases the keys on that frame, and grants again. + * * Qt-free. Thread-safe: push() and wait_for_all() come from the issuing - * thread, cancel() and replace_on_next() from any thread. + * thread; cancel(), replace_on_next() and jump() from any thread. * */ @@ -27,6 +33,7 @@ #include #include #include +#include #include "Common/Cpp/CancellableScope.h" #include "Common/Cpp/Concurrency/ConditionVariable.h" #include "Common/Cpp/Concurrency/Mutex.h" @@ -41,6 +48,27 @@ namespace PokemonAutomation{ namespace MgbaLink{ +// "LOAD " or "RESET": a command that moves mGBA to another frame. +class JumpCommand{ +public: + // Loads the mGBA save state "file" (a path on the Mac running mGBA). + static JumpCommand load_state(std::string_view file); + // Resets the GBA. + static JumpCommand reset(); + + const std::string& line() const{ + return m_line; + } + +private: + explicit JumpCommand(std::string line) + : m_line(std::move(line)) + {} + + std::string m_line; +}; + + // Sends one controller's key states to the mGBA published under an address. class KeySender{ public: @@ -86,7 +114,16 @@ class KeySender{ // and the keys it holds replace the current ones. void replace_on_next(); - // " " per change since the clock was anchored. + // Sends "command" and re-anchors the clock on the frame mGBA is on + // after it. Drops what is not sent yet: a waiting push() returns + // without sending. Returns the new anchor frame. Throws + // ConnectionException if no connection is published, it is lost, or + // the bridge refuses ("ERR load failed"); a refused command leaves the + // old clock running. + uint64_t jump(const JumpCommand& command); + + // " " per change since the clock was anchored (at connect, + // or at the last LOAD or RESET). std::string input_log_text() const; private: diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender_Tests.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender_Tests.cpp index 5bf0db625b..9e28880efc 100644 --- a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender_Tests.cpp +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_KeySender_Tests.cpp @@ -15,6 +15,7 @@ #include #include #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Concurrency/AsyncTask.h" #include "Common/Cpp/Concurrency/ConditionVariable.h" #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/TestRunners/UnitTest.h" @@ -43,8 +44,10 @@ using FakeServerSocket = ServerSocket_POSIX; using std::chrono::milliseconds; -// The fake emulator is always about to run this frame. +// The fake emulator is about to run this frame until a LOAD or RESET. constexpr uint64_t BRIDGE_FRAME = 120; +// The frame the fake emulator is on after a RESET. +constexpr uint64_t RESET_FRAME = 1; std::string join_lines(const std::vector& lines){ @@ -135,12 +138,26 @@ class FakeBridge : public AbstractServerSocket::Listener{ } } - static std::string reply_for(std::string_view line){ + // "LOAD " loads a state taken on frame ; any other file fails. + std::string reply_for(std::string_view line){ + std::lock_guard lg(m_lock); if (line == "HELLO 1"){ return "OK 1 BPRE bridge-0.1 paced\n"; } if (line.starts_with("GRANT ")){ - return std::format("OK {}\n", BRIDGE_FRAME); + return std::format("OK {}\n", m_frame); + } + if (line.starts_with("LOAD ")){ + std::optional loaded = number_after(line, "LOAD "); + if (!loaded){ + return "ERR load failed\n"; + } + m_frame = *loaded; + return std::format("OK {}\n", m_frame); + } + if (line == "RESET"){ + m_frame = RESET_FRAME; + return "OK\n"; } if (line.starts_with("KEYS ")){ return "OK\n"; @@ -158,6 +175,7 @@ class FakeBridge : public AbstractServerSocket::Listener{ ConditionVariable m_cv; std::optional m_listen_error; std::vector m_commands; + uint64_t m_frame = BRIDGE_FRAME; }; // The registry: which connection a lookup finds after publications start @@ -571,6 +589,287 @@ void add_sender_cancel_tests(UnitTestDatabase& database){ } } + + +// LOAD and RESET: the emulator jumps to another frame, so the sender +// re-anchors its clock there. Every KEYS and GRANT after the jump is on +// the new timeline, and nothing queued for the old one plays. +enum class ReanchorStep{ + PUSH, + WAIT_FOR_ALL, + LOAD, + // A LOAD from another thread, 100 ms after the step. + LOAD_LATER, + RESET, +}; +struct ReanchorAction{ + ReanchorStep step = ReanchorStep::PUSH; + uint16_t mask = 0; + milliseconds duration{}; + std::string file = {}; +}; +struct ReanchorInput{ + std::vector actions; +}; +struct ReanchorExpectation{ + // "KEYS ", "GRANT 0", "LOAD ", "RESET" and "GRANT run-on", + // in order. + std::vector commands; + // How many steps threw ConnectionException. + size_t failed_steps = 0; + milliseconds max_run_time{}; +}; +struct ReanchorCase{ + std::string name; + ReanchorInput input; + ReanchorExpectation expectation; +}; + +ReanchorAction hold(uint16_t mask, int ms){ + return ReanchorAction{.step = ReanchorStep::PUSH, .mask = mask, .duration = milliseconds(ms)}; +} +ReanchorAction finish(){ + return ReanchorAction{.step = ReanchorStep::WAIT_FOR_ALL}; +} +ReanchorAction load(std::string file){ + return ReanchorAction{.step = ReanchorStep::LOAD, .file = std::move(file)}; +} +ReanchorAction load_later(std::string file){ + return ReanchorAction{.step = ReanchorStep::LOAD_LATER, .file = std::move(file)}; +} +ReanchorAction reset(){ + return ReanchorAction{.step = ReanchorStep::RESET}; +} + +void run_reanchor_actions( + const std::vector& actions, + KeySender& sender, ThreadPool& thread_pool, + std::vector& results +){ + AsyncTask later; + for (const ReanchorAction& action : actions){ + try{ + switch (action.step){ + case ReanchorStep::PUSH: + sender.push(nullptr, action.mask, action.duration); + break; + case ReanchorStep::WAIT_FOR_ALL: + sender.wait_for_all(nullptr); + break; + case ReanchorStep::LOAD: + sender.jump(JumpCommand::load_state(action.file)); + break; + case ReanchorStep::LOAD_LATER: + later = thread_pool.dispatch_now_blocking([&sender, file = action.file]{ + std::this_thread::sleep_for(milliseconds(100)); + sender.jump(JumpCommand::load_state(file)); + }); + break; + case ReanchorStep::RESET: + sender.jump(JumpCommand::reset()); + break; + } + }catch (ConnectionException&){ + results.emplace_back("error"); + } + } + later.wait_and_ignore_exceptions(); +} + +// Checks the frames of every KEYS and pacer GRANT against the timeline +// they belong to, and keeps the other commands and the KEYS masks in +// order (see ReanchorExpectation). +class TimelineCheck{ +public: + void read(const std::string& line){ + if (line == "GRANT 0"){ + m_kept.emplace_back(line); + start_timeline(); + }else if (line == std::format("GRANT {}", KeySender::RUN_ON_GRANT)){ + m_kept.emplace_back("GRANT run-on"); + }else if (line.starts_with("LOAD ") || line == "RESET"){ + m_kept.emplace_back(line); + m_jumped = true; + m_grants_since_jump = 0; + m_next_anchor = line == "RESET" ? std::optional(RESET_FRAME) : number_after(line, "LOAD "); + }else if (std::optional grant = number_after(line, "GRANT ")){ + check_on_timeline(line, *grant); + m_grants_since_jump++; + }else if (std::optional frame = number_after(line, "KEYS ")){ + read_keys(line, *frame); + } + } + + // Empty if every frame fits. + std::string error() const{ + // The pacer runs again after every LOAD or RESET, failed or not. + if (m_error.empty() && m_jumped && m_grants_since_jump == 0){ + return "no GRANT after the last LOAD or RESET: mGBA stays stalled"; + } + return m_error; + } + const std::vector& kept() const{ + return m_kept; + } + +private: + // Generous: a short case never runs a second past its anchor. + static constexpr uint64_t MAX_FRAMES_PAST_ANCHOR = 60; + + void start_timeline(){ + if (!m_next_anchor){ + return; + } + m_anchor = *m_next_anchor; + m_next_anchor.reset(); + m_reanchored = true; + m_last_keys_frame.reset(); + } + void check_on_timeline(std::string_view line, uint64_t frame){ + if (m_error.empty() && (frame < m_anchor || frame > m_anchor + MAX_FRAMES_PAST_ANCHOR)){ + m_error = std::format("<{}> is off the timeline anchored at frame {}", line, m_anchor); + } + } + void read_keys(const std::string& line, uint64_t frame){ + check_on_timeline(line, frame); + std::string_view mask = std::string_view(line).substr(line.rfind(' ') + 1); + // The release that starts a new timeline is on its first frame. + if (m_error.empty() && m_reanchored && !m_last_keys_frame && mask == "0" && frame != m_anchor){ + m_error = std::format("<{}>: the release after a jump is not on frame {}", line, m_anchor); + } + if (m_error.empty() && m_last_keys_frame && frame <= *m_last_keys_frame){ + m_error = std::format("<{}> is not after the KEYS before it", line); + } + m_last_keys_frame = frame; + m_kept.emplace_back(std::format("KEYS {}", mask)); + } + + std::vector m_kept; + std::string m_error; + uint64_t m_anchor = BRIDGE_FRAME; + std::optional m_next_anchor; + bool m_reanchored = false; + std::optional m_last_keys_frame; + bool m_jumped = false; + size_t m_grants_since_jump = 0; +}; + +class Test_Reanchor : public UnitTest{ +public: + Test_Reanchor(ReanchorCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - key sender re-anchor - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + WallClock start = current_time(); + std::vector results; + std::optional> commands = with_fake_link( + logger, + [&](ThreadPool& thread_pool, LinkRegistry& registry, const std::string& address, std::shared_ptr){ + KeySender sender(logger, thread_pool, registry, address); + run_reanchor_actions(m_case.input.actions, sender, thread_pool, results); + } + ); + milliseconds run_time = std::chrono::duration_cast(current_time() - start); + if (!commands){ + return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); + } + TimelineCheck check; + for (const std::string& line : *commands){ + check.read(line); + } + if (!check.error().empty()){ + return std::format("{}: {}", check.error(), join_lines(*commands)); + } + const std::vector& kept = check.kept(); + if (results.size() != m_case.expectation.failed_steps){ + return std::format("{} step(s) threw, expected {}", results.size(), m_case.expectation.failed_steps); + } + if (kept != m_case.expectation.commands){ + return std::format("the bridge saw {}, expected {}", join_lines(kept), join_lines(m_case.expectation.commands)); + } + if (run_time > m_case.expectation.max_run_time){ + return std::format("took {} ms, expected at most {} ms", run_time.count(), m_case.expectation.max_run_time.count()); + } + return true; + } + +private: + ReanchorCase m_case; +}; + + +void add_load_tests(UnitTestDatabase& database){ + const ReanchorCase LOAD_CASES[] = { + { + // The pacer must not grant the old frames past the loaded one. + .name = "load moves keys and grants to the loaded frame", + .input = {{hold(GBA_KEY_A, 80), load("9000"), hold(GBA_KEY_B, 80), finish()}}, + .expectation = { + .commands = {"GRANT 0", "KEYS 1", "LOAD 9000", "GRANT 0", "KEYS 0", "KEYS 2", "KEYS 0", "GRANT run-on"}, + .max_run_time = milliseconds(2000), + }, + }, + { + // Old grants would let mGBA run free for 90 frames. + .name = "load of an earlier frame", + .input = {{hold(GBA_KEY_A, 80), load("30"), hold(GBA_KEY_B, 80), finish()}}, + .expectation = { + .commands = {"GRANT 0", "KEYS 1", "LOAD 30", "GRANT 0", "KEYS 0", "KEYS 2", "KEYS 0", "GRANT run-on"}, + .max_run_time = milliseconds(2000), + }, + }, + { + // Nothing held yet: nothing to release. + .name = "load before any key", + .input = {{load("9000"), hold(GBA_KEY_A, 80), finish()}}, + .expectation = { + .commands = {"GRANT 0", "LOAD 9000", "GRANT 0", "KEYS 1", "KEYS 0", "GRANT run-on"}, + .max_run_time = milliseconds(2000), + }, + }, + }; + for (const ReanchorCase& test_case : LOAD_CASES){ + database.add(test_case); + } +} + +void add_reset_and_drop_tests(UnitTestDatabase& database){ + const ReanchorCase RESET_AND_DROP_CASES[] = { + { + // RESET replies without a frame: "GRANT 0" reads it. + .name = "reset moves keys and grants to the reset frame", + .input = {{hold(GBA_KEY_A, 80), reset(), hold(GBA_KEY_B, 80), finish()}}, + .expectation = { + .commands = {"GRANT 0", "KEYS 1", "RESET", "GRANT 0", "KEYS 0", "KEYS 2", "KEYS 0", "GRANT run-on"}, + .max_run_time = milliseconds(2000), + }, + }, + { + // B waits ~3 s for its turn behind A; the LOAD drops it. + .name = "load drops a state waiting for its turn", + .input = {{hold(GBA_KEY_A, 3000), load_later("9000"), hold(GBA_KEY_B, 80), finish()}}, + .expectation = { + .commands = {"GRANT 0", "KEYS 1", "LOAD 9000", "GRANT 0", "KEYS 0", "GRANT run-on"}, + .max_run_time = milliseconds(1500), + }, + }, + { + // mGBA did not jump: the old clock and the pacer carry on. + .name = "failed load keeps the old timeline", + .input = {{hold(GBA_KEY_A, 80), load("missing"), hold(GBA_KEY_B, 80), finish()}}, + .expectation = { + .commands = {"GRANT 0", "KEYS 1", "LOAD missing", "KEYS 2", "KEYS 0", "GRANT run-on"}, + .failed_steps = 1, + .max_run_time = milliseconds(2000), + }, + }, + }; + for (const ReanchorCase& test_case : RESET_AND_DROP_CASES){ + database.add(test_case); + } +} + } // namespace @@ -579,6 +878,8 @@ void add_key_sender_tests(UnitTestDatabase& database){ add_pacer_tests(database); add_sender_tests(database); add_sender_cancel_tests(database); + add_load_tests(database); + add_reset_and_drop_tests(database); } diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.cpp index dd684821e9..ea19c5fd88 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.cpp @@ -23,6 +23,18 @@ ProController_MgbaLink::ProController_MgbaLink(Logger& logger, std::string addre ProController_MgbaLink::~ProController_MgbaLink() = default; +uint64_t ProController_MgbaLink::load_state(std::string_view file){ + std::lock_guard lg(m_state_lock); + m_scheduler.clear_on_next(); + return m_sender.jump(MgbaLink::JumpCommand::load_state(file)); +} +uint64_t ProController_MgbaLink::reset_console(){ + std::lock_guard lg(m_state_lock); + m_scheduler.clear_on_next(); + return m_sender.jump(MgbaLink::JumpCommand::reset()); +} + + void ProController_MgbaLink::cancel_all_commands(){ std::lock_guard lg(m_state_lock); m_scheduler.clear_on_next(); diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.h b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.h index e8ca067e37..20acf7adb4 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/MgbaLink/NintendoSwitch_MgbaLink_ProController.h @@ -15,7 +15,9 @@ #ifndef PokemonAutomation_NintendoSwitch_MgbaLink_ProController_H #define PokemonAutomation_NintendoSwitch_MgbaLink_ProController_H +#include #include +#include #include "Controllers/ControllerTypes.h" #include "Controllers/MgbaLink/MgbaLink_KeySender.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -37,6 +39,15 @@ class ProController_MgbaLink final : ProController_MgbaLink(Logger& logger, std::string address); ~ProController_MgbaLink(); + // Loads the mGBA save state "file" (a path on the Mac running mGBA). + // Drops the commands not sent yet and releases the keys, as + // cancel_all_commands() does; the clock re-anchors on the loaded frame. + // Returns that frame. Throws ConnectionException if mGBA refuses it. + uint64_t load_state(std::string_view file); + + // Resets the GBA, like load_state() otherwise. + uint64_t reset_console(); + // The ProController interface. Callers reach it through ProController // (the program's context), so the overrides stay out of this class's