diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp new file mode 100644 index 0000000000..6bddd0252b --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp @@ -0,0 +1,334 @@ +/* Video Source (mGBA over MgbaLink) + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/ImageTypes/ImageRGB32_Qt.h" +#include "CommonFramework/Tools/GlobalThreadPools.h" +#include "CommonFramework/VideoPipeline/Backends/VideoFrameQt.h" +#include "VideoSource_MgbaLink.h" + +namespace PokemonAutomation{ + +using namespace MgbaLink; + + +namespace{ + +// Re-anchor the frame clock when a frame maps further than this before +// the time it was read (mGBA paused, or slower than real time). +constexpr std::chrono::milliseconds MAX_CLOCK_LAG(500); + +constexpr std::chrono::milliseconds RECONNECT_DELAY(2000); + +// One GBA frame, rounded down: polling faster only repeats a frame number. +constexpr std::chrono::milliseconds POLL_PERIOD(16); + + +// Forwards to "logger" every line but a repeat of the last one forwarded: +// a bridge that stays away fails the same way on every retry. +class RepeatSuppressingLogger : public Logger{ +public: + explicit RepeatSuppressingLogger(Logger& logger) + : m_logger(logger) + {} + + virtual void log(const std::string& msg, Color color) override{ + { + std::lock_guard lg(m_lock); + if (msg == m_last){ + return; + } + m_last = msg; + } + m_logger.log(msg, color); + } + +private: + Logger& m_logger; + Mutex m_lock; + std::string m_last; +}; + +bool is_local_host(std::string_view host){ + return host == "127.0.0.1" || host == "localhost"; +} + +} // namespace + + + +ImageRGB32 compose_mgba_frame(const ImageViewRGB32& gba_frame, const MgbaComposeGeometry& geometry){ + if (geometry.box_x + geometry.box_width > geometry.canvas_width || + geometry.box_y + geometry.box_height > geometry.canvas_height + ){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + std::format( + "mGBA box {}x{} at ({}, {}) does not fit in a {}x{} canvas.", + geometry.box_width, geometry.box_height, geometry.box_x, geometry.box_y, + geometry.canvas_width, geometry.canvas_height + ) + ); + } + ImageRGB32 canvas(geometry.canvas_width, geometry.canvas_height); + canvas.fill(0xff000000); + const size_t src_width = gba_frame.width(); + const size_t src_height = gba_frame.height(); + if (src_width == 0 || src_height == 0){ + return canvas; + } + std::vector src_x(geometry.box_width); + for (size_t x = 0; x < geometry.box_width; x++){ + src_x[x] = (2 * x + 1) * src_width / (2 * geometry.box_width); + } + for (size_t y = 0; y < geometry.box_height; y++){ + const size_t sy = (2 * y + 1) * src_height / (2 * geometry.box_height); + for (size_t x = 0; x < geometry.box_width; x++){ + canvas.pixel(geometry.box_x + x, geometry.box_y + y) = gba_frame.pixel(src_x[x], sy); + } + } + return canvas; +} + + +std::optional> parse_mgba_address(std::string_view address){ + const size_t colon = address.rfind(':'); + if (colon == std::string_view::npos || colon == 0){ + return std::nullopt; + } + std::string_view port_text = address.substr(colon + 1); + uint16_t port = 0; + std::from_chars_result result = std::from_chars(port_text.data(), port_text.data() + port_text.size(), port); + if (port_text.empty() || result.ec != std::errc() || result.ptr != port_text.data() + port_text.size() || port == 0){ + return std::nullopt; + } + std::string_view host = address.substr(0, colon); + if (!is_local_host(host)){ + return std::nullopt; + } + return std::make_pair(std::string(host), port); +} + + + +std::string VideoSourceDescriptor_MgbaLink::address() const{ + ReadSpinLock lg(m_lock, PA_CURRENT_FUNCTION); + return m_address; +} +bool VideoSourceDescriptor_MgbaLink::operator==(const VideoSourceDescriptor& x) const{ + if (typeid(*this) != typeid(x)){ + return false; + } + return address() == static_cast(x).address(); +} +std::string VideoSourceDescriptor_MgbaLink::display_name() const{ + return "mGBA (" + address() + ")"; +} +JsonValue VideoSourceDescriptor_MgbaLink::to_json() const{ + return address(); +} +void VideoSourceDescriptor_MgbaLink::load_json(const JsonValue& json){ + const std::string* address = json.to_string(); + if (address != nullptr && parse_mgba_address(*address)){ + WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); + m_address = *address; + } +} +std::unique_ptr VideoSourceDescriptor_MgbaLink::make_VideoSource( + Logger& logger, + Resolution resolution, + VideoFormat format, + FramesPerSecond fps +) const{ + return std::make_unique(logger, address()); +} + + + +VideoSource_MgbaLink::VideoSource_MgbaLink(Logger& logger, std::string address) + : VideoSource(logger, false) + , m_logger(logger) + , m_address(std::move(address)) +{ + m_formats[{1920, 1080}][VideoFormat::OTHER] = {60}; + m_poller = GlobalThreadPools::unlimited_realtime().dispatch_now_blocking( + [this]{ poll_loop(); } + ); +} +VideoSource_MgbaLink::~VideoSource_MgbaLink(){ + { + std::lock_guard lg(m_lock); + m_stopping = true; + if (m_connection != nullptr){ + m_connection->abort(); + } + } + m_cv.notify_all(); + m_poller.wait_and_ignore_exceptions(); +} + + +VideoSnapshot VideoSource_MgbaLink::snapshot_latest_blocking(){ + std::lock_guard lg(m_lock); + return m_last; +} +VideoSnapshot VideoSource_MgbaLink::snapshot_recent_nonblocking(WallClock min_time){ + std::lock_guard lg(m_lock); + if (m_last.timestamp < min_time){ + return VideoSnapshot(); + } + return m_last; +} + + +bool VideoSource_MgbaLink::wait_or_stop(std::chrono::milliseconds duration){ + std::unique_lock lg(m_lock); + return m_cv.wait_for(lg, duration, [this]{ return m_stopping; }); +} + +void VideoSource_MgbaLink::poll_loop(){ + std::optional> address = parse_mgba_address(m_address); + if (!address){ + m_logger.log( + "MgbaLink: invalid address \"" + m_address + "\"; expected 127.0.0.1: or localhost:.", + COLOR_RED + ); + return; + } + m_logger.log(std::format( + "MgbaLink: polling {}; retrying every {} s while it is away.", + m_address, std::chrono::duration_cast(RECONNECT_DELAY).count() + )); + RepeatSuppressingLogger logger(m_logger); + do{ + try{ + run_connection(logger, address->first, address->second); + }catch (Exception&){ + // Already logged (once) by the exception; retry. + } + }while (!wait_or_stop(RECONNECT_DELAY)); +} + +void VideoSource_MgbaLink::run_connection(Logger& logger, const std::string& host, uint16_t port){ + Connection connection(logger, GlobalThreadPools::unlimited_realtime(), host, port); + { + std::lock_guard lg(m_lock); + if (m_stopping){ + return; + } + m_connection = &connection; + } + try{ + connection.open(); + poll_connected(logger, connection); + }catch (...){ + std::lock_guard lg(m_lock); + m_connection = nullptr; + m_last = VideoSnapshot(); + throw; + } + std::lock_guard lg(m_lock); + m_connection = nullptr; + m_last = VideoSnapshot(); +} + +void VideoSource_MgbaLink::poll_connected(Logger& logger, Connection& connection){ + std::optional clock; + uint64_t last_frame = 0; + do{ + FrameReply reply = connection.frame(); + WallClock now = current_time(); + if (clock && reply.frame == last_frame){ + continue; + } + if (!clock){ + clock.emplace(now, reply.frame); + } + FrameClock::Stamp stamp = clock->stamp(now, reply.frame, MAX_CLOCK_LAG); + if (stamp.reanchored_after_lag){ + m_logger.log(std::format("MgbaLink: frame clock re-anchored at frame {} (mGBA paused or slow).", reply.frame)); + } + last_frame = reply.frame; + + // Read the PNG before the next FRAME overwrites it. + ImageRGB32 gba_frame(reply.path); + if (gba_frame.width() != reply.width || gba_frame.height() != reply.height){ + throw ConnectionException(&logger, std::format( + "MgbaLink: FRAME {} said {}x{}, but {} is {}x{}.", + reply.frame, reply.width, reply.height, reply.path, gba_frame.width(), gba_frame.height() + )); + } + publish_frame(compose_mgba_frame(gba_frame), stamp.time); + }while (!wait_or_stop(POLL_PERIOD)); +} + +void VideoSource_MgbaLink::publish_frame(ImageRGB32 frame, WallClock timestamp){ + VideoSnapshot snapshot(std::move(frame), timestamp); + { + std::lock_guard lg(m_lock); + m_last = snapshot; + } + + // For the FPS readout and stream history. The QImage shares the + // snapshot's pixels and keeps them alive. + const ImageRGB32& pixels = *snapshot.frame; + QImage image( + (const uchar*)pixels.data(), + (int)pixels.width(), + (int)pixels.height(), + (qsizetype)pixels.bytes_per_row(), + QImage::Format_ARGB32, + [](void* owner){ delete static_cast*>(owner); }, + new std::shared_ptr(snapshot.frame) + ); + report_source_frame(std::make_shared(timestamp, QVideoFrame(image))); +} + + + +class VideoWidget_MgbaLink : public QWidget{ +public: + VideoWidget_MgbaLink(QWidget* parent, VideoSource_MgbaLink& source) + : QWidget(parent) + , m_source(source) + { + QTimer* timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, [this]{ update(); }); + timer->start(33); + } + +private: + virtual void paintEvent(QPaintEvent* event) override{ + QWidget::paintEvent(event); + VideoSnapshot snapshot = m_source.snapshot_latest_blocking(); + if (!snapshot.frame || snapshot.frame->width() == 0){ + return; + } + QPainter painter(this); + painter.drawImage(QRect(0, 0, width(), height()), to_QImage_ref(*snapshot.frame)); + m_source.report_rendered_frame(current_time()); + } + +private: + VideoSource_MgbaLink& m_source; +}; + + +QWidget* VideoSource_MgbaLink::make_display_QtWidget(QWidget* parent){ + return new VideoWidget_MgbaLink(parent, *this); +} + + + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h new file mode 100644 index 0000000000..a167ec857c --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h @@ -0,0 +1,155 @@ +/* Video Source (mGBA over MgbaLink) + * + * From: https://github.com/PokemonAutomation/ + * + * Shows the picture of an mGBA instance running "bridge.lua" (MgbaLink v1). + * Each new emulated frame is composed into a 1920x1080 frame at FRLG's + * GAME_BOX with nearest-neighbour scaling, so detectors see console + * geometry. While mGBA runs no faster than 59.7275 fps, a frame is stamped + * with the time its frame number maps to; when it runs ahead (e.g. synced + * to a 60 Hz display), with the time it was read. Never later than the + * read time. The source reconnects itself, so it opts out of the session + * no-video watchdog. Developer mode only (fork lab tooling). + * + */ + +#ifndef PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_H +#define PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Concurrency/SpinLock.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "Controllers/MgbaLink/MgbaLink_Connection.h" +#include "CommonFramework/VideoPipeline/VideoSourceDescriptor.h" +#include "CommonFramework/VideoPipeline/VideoSource.h" + +namespace PokemonAutomation{ + + +// Where the GBA picture goes: GAME_BOX {0.09375, 0.00462963, 0.8125, +// 0.962963} of a 1920x1080 frame, i.e. x = 180..1740, y = 5..1045 (6.5x). +struct MgbaComposeGeometry{ + size_t canvas_width = 1920; + size_t canvas_height = 1080; + size_t box_x = 180; + size_t box_y = 5; + size_t box_width = 1560; + size_t box_height = 1040; +}; + +// Draws "gba_frame" into a black canvas at the box, nearest-neighbour with +// pixel-centre sampling (as Pillow's NEAREST, which the geometry gate used): +// canvas pixel x takes source column floor((2 (x - box_x) + 1) w / (2 box_width)). +// Throws InternalProgramError if the box does not fit inside the canvas. +ImageRGB32 compose_mgba_frame( + const ImageViewRGB32& gba_frame, + const MgbaComposeGeometry& geometry = MgbaComposeGeometry() +); + + +// "host:port" -> (host, port). nullopt if the port is missing or invalid, +// or the host is not this machine ("127.0.0.1" or "localhost"): FRAME +// names a PNG on the bridge's disk, which this app opens as a local path. +std::optional> parse_mgba_address(std::string_view address); + + + +// Selects an mGBA bridge by "host:port" (saved in the profile). +class VideoSourceDescriptor_MgbaLink : public VideoSourceDescriptor{ +public: + static constexpr std::string_view DEFAULT_ADDRESS = "127.0.0.1:47801"; + + VideoSourceDescriptor_MgbaLink() + : VideoSourceDescriptor(VideoSourceType::MgbaLink) + , m_address(DEFAULT_ADDRESS) + {} + + std::string address() const; + + virtual bool operator==(const VideoSourceDescriptor& x) const override; + virtual std::string display_name() const override; + + virtual JsonValue to_json() const override; + virtual void load_json(const JsonValue& json) override; + + virtual std::unique_ptr make_VideoSource( + Logger& logger, + Resolution resolution, + VideoFormat format, + FramesPerSecond fps + ) const override; + +private: + mutable SpinLock m_lock; + std::string m_address; +}; + + + +// Polls FRAME on its own connection and keeps the latest composed frame; +// drops it when the connection is lost, so a dead bridge shows no video. +// +// The bridge holds one client at a time, so this source takes its only +// slot. A controller for the same mGBA (slice 2: KEYS, GRANT) must not open +// a second connection: it has to share this one (Connection serializes +// commands from several threads). +class VideoSource_MgbaLink : public VideoSource{ +public: + VideoSource_MgbaLink(Logger& logger, std::string address); + ~VideoSource_MgbaLink(); + + virtual Resolution current_resolution() const override{ + return Resolution(1920, 1080); + } + virtual VideoFormat current_format() const override{ + return VideoFormat::OTHER; + } + virtual FramesPerSecond current_fps() const override{ + return 60; + } + virtual const VideoFormatSet& supported_formats() const override{ + return m_formats; + } + + virtual VideoSnapshot snapshot_latest_blocking() override; + virtual VideoSnapshot snapshot_recent_nonblocking(WallClock min_time) override; + + virtual QWidget* make_display_QtWidget(QWidget* parent) override; + +private: + friend class VideoWidget_MgbaLink; + + void poll_loop(); + void run_connection(Logger& logger, const std::string& host, uint16_t port); + void poll_connected(Logger& logger, MgbaLink::Connection& connection); + void publish_frame(ImageRGB32 frame, WallClock timestamp); + bool wait_or_stop(std::chrono::milliseconds duration); + +private: + Logger& m_logger; + const std::string m_address; + VideoFormatSet m_formats; + + Mutex m_lock; + ConditionVariable m_cv; + bool m_stopping = false; + VideoSnapshot m_last; + // The poller's open connection, if any; the destructor aborts it so it + // never waits out a reply timeout. + MgbaLink::Connection* m_connection = nullptr; + + AsyncTask m_poller; +}; + + + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp new file mode 100644 index 0000000000..f0ce9056f9 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp @@ -0,0 +1,202 @@ +/* Video Source (mGBA over MgbaLink) Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "VideoSource_MgbaLink.h" +#include "VideoSource_MgbaLink_Tests.h" + +namespace PokemonAutomation{ + +namespace{ + + +// A 240x160 frame whose pixels name their own coordinates. +ImageRGB32 make_gba_frame(){ + ImageRGB32 frame(240, 160); + for (size_t y = 0; y < 160; y++){ + for (size_t x = 0; x < 240; x++){ + frame.pixel(x, y) = 0xff000000 | (uint32_t)(x << 8) | (uint32_t)y; + } + } + return frame; +} + +struct Point{ + size_t x; + size_t y; +}; + + +// Which GBA pixel lands on a canvas pixel of the composed 1920x1080 frame. +struct ComposeInput{ + Point canvas; +}; +struct ComposeExpectation{ + std::optional gba_pixel; // nullopt: black border. +}; +struct ComposeCase{ + std::string name; + ComposeInput input; + ComposeExpectation expectation; +}; + +class Test_Compose : public UnitTest{ +public: + Test_Compose(ComposeCase test_case) + : UnitTest(std::format("CommonFramework::VideoSource_MgbaLink - compose - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ImageRGB32 canvas = compose_mgba_frame(make_gba_frame()); + if (canvas.width() != 1920 || canvas.height() != 1080){ + return std::format("canvas is {}x{}, expected 1920x1080", canvas.width(), canvas.height()); + } + const std::optional& gba = m_case.expectation.gba_pixel; + uint32_t expected = gba ? 0xff000000 | (uint32_t)(gba->x << 8) | (uint32_t)gba->y : 0xff000000; + uint32_t actual = canvas.pixel(m_case.input.canvas.x, m_case.input.canvas.y); + if (actual != expected){ + return std::format("pixel {:08x}, expected {:08x}", actual, expected); + } + return true; + } + +private: + ComposeCase m_case; +}; + + +// "host:port" parsing for the descriptor. +struct AddressInput{ + std::string address; +}; +struct AddressExpectation{ + std::optional> host_and_port; +}; +struct AddressCase{ + std::string name; + AddressInput input; + AddressExpectation expectation; +}; + +class Test_Address : public UnitTest{ +public: + Test_Address(AddressCase test_case) + : UnitTest(std::format("CommonFramework::VideoSource_MgbaLink - address - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::optional> parsed = parse_mgba_address(m_case.input.address); + if (parsed != m_case.expectation.host_and_port){ + return std::format( + "parsed {}, expected {}", + describe(parsed), describe(m_case.expectation.host_and_port) + ); + } + return true; + } + +private: + static std::string describe(const std::optional>& host_and_port){ + return host_and_port + ? std::format("<{}> port {}", host_and_port->first, host_and_port->second) + : std::string("nothing"); + } + + AddressCase m_case; +}; + + +// compose_mgba_frame() refuses a box that does not fit the canvas. +struct GeometryInput{ + MgbaComposeGeometry geometry; +}; +struct GeometryExpectation{ + bool accepted = false; +}; +struct GeometryCase{ + std::string name; + GeometryInput input; + GeometryExpectation expectation; +}; + +class Test_Geometry : public UnitTest{ +public: + Test_Geometry(GeometryCase test_case) + : UnitTest(std::format("CommonFramework::VideoSource_MgbaLink - geometry - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + bool accepted = true; + try{ + compose_mgba_frame(make_gba_frame(), m_case.input.geometry); + }catch (InternalProgramError&){ + accepted = false; + } + if (accepted != m_case.expectation.accepted){ + return std::format("accepted={}, expected {}", accepted, m_case.expectation.accepted); + } + return true; + } + +private: + GeometryCase m_case; +}; + +} // namespace + + +void add_tests_VideoSource_MgbaLink(UnitTestDatabase& database){ + // GAME_BOX at 1080p is x = 180..1739, y = 5..1044; 6.5x, so source + // columns alternate 6 and 7 canvas pixels. + const ComposeCase COMPOSE_CASES[] = { + {.name = "box top-left", .input = {{180, 5}}, .expectation = {Point{0, 0}}}, + {.name = "box bottom-right", .input = {{1739, 1044}}, .expectation = {Point{239, 159}}}, + {.name = "left of the box", .input = {{179, 500}}, .expectation = {std::nullopt}}, + {.name = "right of the box", .input = {{1740, 500}}, .expectation = {std::nullopt}}, + {.name = "above the box", .input = {{960, 4}}, .expectation = {std::nullopt}}, + {.name = "below the box", .input = {{960, 1045}}, .expectation = {std::nullopt}}, + {.name = "last pixel of column 0", .input = {{185, 5}}, .expectation = {Point{0, 0}}}, + {.name = "first pixel of column 1", .input = {{186, 5}}, .expectation = {Point{1, 0}}}, + {.name = "last pixel of column 1", .input = {{192, 5}}, .expectation = {Point{1, 0}}}, + {.name = "first pixel of column 2", .input = {{193, 5}}, .expectation = {Point{2, 0}}}, + {.name = "first pixel of row 1", .input = {{180, 11}}, .expectation = {Point{0, 1}}}, + {.name = "centre", .input = {{960, 525}}, .expectation = {Point{120, 80}}}, + }; + for (const ComposeCase& test_case : COMPOSE_CASES){ + database.add(test_case); + } + + const AddressCase ADDRESS_CASES[] = { + {.name = "default bridge", .input = {"127.0.0.1:47801"}, .expectation = {std::make_pair(std::string("127.0.0.1"), uint16_t(47801))}}, + {.name = "host name", .input = {"localhost:47802"}, .expectation = {std::make_pair(std::string("localhost"), uint16_t(47802))}}, + {.name = "no port", .input = {"127.0.0.1"}, .expectation = {std::nullopt}}, + {.name = "empty port", .input = {"127.0.0.1:"}, .expectation = {std::nullopt}}, + {.name = "port 0", .input = {"127.0.0.1:0"}, .expectation = {std::nullopt}}, + {.name = "port too large", .input = {"127.0.0.1:70000"}, .expectation = {std::nullopt}}, + {.name = "no host", .input = {":47801"}, .expectation = {std::nullopt}}, + {.name = "remote host refused", .input = {"192.168.1.20:47801"}, .expectation = {std::nullopt}}, + }; + for (const AddressCase& test_case : ADDRESS_CASES){ + database.add(test_case); + } + + const GeometryCase GEOMETRY_CASES[] = { + {.name = "GAME_BOX at 1080p fits", .input = {MgbaComposeGeometry()}, .expectation = {true}}, + {.name = "box fills the canvas", .input = {{.canvas_width = 240, .canvas_height = 160, .box_x = 0, .box_y = 0, .box_width = 240, .box_height = 160}}, .expectation = {true}}, + {.name = "box past the right edge", .input = {{.box_x = 400}}, .expectation = {false}}, + {.name = "box past the bottom edge", .input = {{.box_y = 50}}, .expectation = {false}}, + }; + for (const GeometryCase& test_case : GEOMETRY_CASES){ + database.add(test_case); + } +} + + + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h new file mode 100644 index 0000000000..18ceaa14e6 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h @@ -0,0 +1,20 @@ +/* Video Source (mGBA over MgbaLink) Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_Tests_H +#define PokemonAutomation_VideoPipeline_VideoSource_MgbaLink_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + + +// Compose geometry (nearest-neighbour into GAME_BOX) and address parsing. +void add_tests_VideoSource_MgbaLink(UnitTestDatabase& database); + + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp index 10957abbd6..7d27106a9b 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/UI/VideoSourceSelectorWidget.cpp @@ -9,6 +9,7 @@ #include #include #include "Common/Qt/NoWheelComboBox.h" +#include "CommonFramework/StaticGlobals.h" #include "CommonFramework/GlobalSettingsPanel.h" #include "CommonFramework/VideoPipeline/VideoPipelineOptions.h" #include "CommonFramework/Panels/ConsoleSettingsStretch.h" @@ -138,6 +139,9 @@ void VideoSourceSelectorWidget::update_source_list(){ m_session.save(option); m_sources.emplace_back(option.get_descriptor_from_cache(VideoSourceType::None)); m_sources.emplace_back(option.get_descriptor_from_cache(VideoSourceType::StillImage)); + if (STATIC_GLOBALS.DEVELOPER_MODE){ + m_sources.emplace_back(option.get_descriptor_from_cache(VideoSourceType::MgbaLink)); + } } // Now add all the cameras. diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h index 24db4ea985..ab874a6df1 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceDescriptor.h @@ -23,6 +23,7 @@ enum class VideoSourceType{ StillImage, VideoPlayback, Camera, + MgbaLink, // Developer mode only (fork lab tooling). }; diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp index 6c11a5daca..d905f8daf6 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/VideoSourceOption.cpp @@ -8,10 +8,12 @@ #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/StaticGlobals.h" #include "Sources/VideoSource_Null.h" #include "Sources/VideoSource_StillImage.h" #include "Sources/VideoSource_Camera.h" +#include "Sources/VideoSource_MgbaLink.h" #include "VideoSourceOption.h" @@ -27,9 +29,31 @@ const EnumStringMap VIDEO_TYPE_STRINGS{ {VideoSourceType::StillImage, "Still Image"}, {VideoSourceType::VideoPlayback, "Video Playback"}, {VideoSourceType::Camera, "Camera"}, + {VideoSourceType::MgbaLink, "mGBA Link"}, }; +namespace{ + +// Loads the saved parameters of one source type into the cache, if present. +template +void load_cached_descriptor( + std::map>& cache, + const JsonObject& obj, + VideoSourceType type +){ + const JsonValue* params = obj.get_value(VIDEO_TYPE_STRINGS.get_string(type)); + if (params == nullptr){ + return; + } + auto descriptor = std::make_unique(); + descriptor->load_json(*params); + cache[type] = std::move(descriptor); +} + +} // namespace + + VideoSourceOption::VideoSourceOption() : m_resolution(1920, 1080) , m_format(VideoFormat::OTHER) @@ -57,6 +81,9 @@ std::shared_ptr VideoSourceOption::get_descriptor_from_ca case VideoSourceType::Camera: descriptor.reset(new VideoSourceDescriptor_Camera()); break; + case VideoSourceType::MgbaLink: + descriptor = std::make_shared(); + break; default:; descriptor.reset(new VideoSourceDescriptor_Null()); } @@ -119,22 +146,10 @@ void VideoSourceOption::load_json(const JsonValue& json){ } #endif - const JsonValue* params; - params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::StillImage)); - if (params != nullptr){ - auto x = std::make_unique(); - x->load_json(*params); - m_descriptor_cache[VideoSourceType::StillImage] = std::move(x); - } - params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::VideoPlayback)); - if (params != nullptr){ -// m_descriptor_cache[VideoSourceType::VideoPlayback] = - } - params = obj->get_value(VIDEO_TYPE_STRINGS.get_string(VideoSourceType::Camera)); - if (params != nullptr){ - auto x = std::make_unique(); - x->load_json(*params); - m_descriptor_cache[VideoSourceType::Camera] = std::move(x); + load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::StillImage); + load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::Camera); + if (STATIC_GLOBALS.DEVELOPER_MODE){ + load_cached_descriptor(m_descriptor_cache, *obj, VideoSourceType::MgbaLink); } auto iter = m_descriptor_cache.find(VIDEO_TYPE_STRINGS.get_enum(*type, VideoSourceType::None)); diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index e2ef8b4ce7..ead4cfb09a 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -23,6 +23,8 @@ #include "CommonFramework/ProgramStats/StatsTracking.h" #include "CommonFramework/Tools/GlobalThreadPools.h" #include "CommonFramework/Windows/LabChannel_Tests.h" +#include "CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h" +#include "Controllers/MgbaLink/MgbaLink_Tests.h" #include "CommonFramework/Windows/LabSnapshot_Tests.h" #include "CommonFramework/Windows/LabStateJson_Tests.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" @@ -71,6 +73,8 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_SinkPipeline(ret); add_tests_AudioFileLoader(ret); add_tests_LabChannel(ret); + MgbaLink::add_tests(ret); + add_tests_VideoSource_MgbaLink(ret); add_tests_LabSnapshot(ret); add_tests_LabStateJson(ret); OCR::add_tests(ret); diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp new file mode 100644 index 0000000000..e0998ebdfc --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.cpp @@ -0,0 +1,225 @@ +/* MgbaLink Connection + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "MgbaLink_Connection.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + + +Connection::Connection( + Logger& logger, + ThreadPool& thread_pool, + std::string host, + uint16_t port, + std::chrono::milliseconds timeout +) + : m_logger(logger) + , m_host(std::move(host)) + , m_port(port) + , m_timeout(timeout) + , m_socket(thread_pool) +{ + m_socket.add_listener(*this); +} +Connection::~Connection(){ + close(); + m_socket.remove_listener(*this); +} + + +HelloReply Connection::open(){ + bool opened_before; + { + std::lock_guard lg(m_lock); + opened_before = m_opened; + m_opened = true; + } + if (opened_before){ + throw InternalProgramError( + &m_logger, PA_CURRENT_FUNCTION, + "MgbaLink: open() called twice on one connection; make a new one to reconnect." + ); + } + connect_socket(); + std::string reply = command("HELLO 1"); + std::optional hello = parse_hello_reply(reply); + if (!hello){ + fail(std::format("Unexpected HELLO reply: {}", reply)); + } + m_logger.log(std::format( + "MgbaLink: connected to {}:{}, game {}, {}, mode {}.", + m_host, m_port, hello->game_code, hello->bridge_version, hello->mode + )); + return *hello; +} + +void Connection::connect_socket(){ + m_socket.connect(m_host, m_port); + std::optional error; + { + std::unique_lock lg(m_lock); + bool finished = m_cv.wait_for(lg, m_timeout, [this]{ + return m_aborted || m_connect_error.has_value(); + }); + if (m_aborted){ + error = "aborted"; + }else if (!finished){ + error = "timed out"; + }else if (!m_connect_error->empty()){ + error = *m_connect_error; + } + } + if (error){ + fail(std::format("Unable to connect to {}:{}: {}", m_host, m_port, *error)); + } +} + +void Connection::close() noexcept{ + bool aborted = true; + try{ + std::lock_guard lg(m_lock); + aborted = m_aborted; + }catch (...){} + if (!aborted && m_socket.state() == AbstractClientSocket::State::CONNECTED){ + try{ + command("BYE"); + }catch (...){} + } + m_socket.close(); +} + +void Connection::abort() noexcept{ + try{ + std::lock_guard lg(m_lock); + m_aborted = true; + m_cv.notify_all(); + }catch (...){} + m_socket.close(); +} + + +std::string Connection::command(std::string_view line){ + std::lock_guard command_lg(m_command_lock); + std::string_view name = line.substr(0, line.find(' ')); + // A line the bridge sent unasked ("ERR busy" right after connecting) + // is still read as this command's reply, even after the bridge closed. + std::string data(line); + data += '\n'; + bool sent = m_socket.send(data.data(), data.size(), m_timeout) == data.size(); + bool nothing_to_read; + { + std::lock_guard lg(m_lock); + nothing_to_read = !sent && m_replies.empty() && !m_aborted; + } + if (nothing_to_read){ + fail(std::format("Unable to send {}.", name)); + } + return wait_for_reply(name); +} + +std::string Connection::wait_for_reply(std::string_view command_name){ + std::unique_lock lg(m_lock); + bool ready = m_cv.wait_for(lg, m_timeout, [this]{ + return m_aborted || !m_replies.empty() || m_disconnect_error.has_value(); + }); + if (m_aborted || m_replies.empty()){ + std::string reason; + if (m_aborted){ + reason = "aborted"; + }else if (ready){ + reason = std::format("connection lost: {}", *m_disconnect_error); + }else{ + reason = "timed out"; + // A late reply would pair with the next command: drop the link. + m_disconnect_error = std::format("no reply to {}", command_name); + } + lg.unlock(); + m_socket.close(); + fail(std::format("No reply to {} ({}).", command_name, reason)); + } + std::string reply = std::move(m_replies.front()); + m_replies.pop_front(); + lg.unlock(); + if (reply.starts_with("ERR")){ + fail(std::format("{} -> {}", command_name, reply)); + } + return reply; +} + +FrameReply Connection::frame(){ + std::string reply = command("FRAME"); + std::optional frame = parse_frame_reply(reply); + if (!frame){ + fail(std::format("Unexpected FRAME reply: {}", reply)); + } + return std::move(*frame); +} + +uint64_t Connection::keys(uint64_t frame, uint16_t mask){ + std::string reply = command(std::format("KEYS {} {}", frame, mask)); + std::optional late = parse_keys_reply(reply); + if (!late){ + fail(std::format("Unexpected KEYS reply: {}", reply)); + } + if (*late > 0){ + m_logger.log( + std::format("MgbaLink: KEYS for frame {} was {} frame(s) late.", frame, *late), + COLOR_ORANGE + ); + } + return *late; +} + +void Connection::fail(std::string message){ + throw ConnectionException(&m_logger, std::format("MgbaLink: {}", message)); +} + + + +void Connection::on_connect_finished(const std::string& error_message){ + std::lock_guard lg(m_lock); + m_connect_error = error_message; + m_disconnect_error.reset(); + m_splitter = LineSplitter(); + m_cv.notify_all(); +} + +void Connection::on_receive_data(const void* data, size_t bytes){ + bool overflowed; + { + std::lock_guard lg(m_lock); + for (std::string& line : m_splitter.push(std::string_view(static_cast(data), bytes))){ + m_replies.emplace_back(std::move(line)); + } + overflowed = m_splitter.overflowed(); + if (overflowed){ + m_disconnect_error = std::format("reply line longer than {} bytes", LineSplitter::MAX_LINE_BYTES); + } + m_cv.notify_all(); + } + if (overflowed){ + m_socket.close(); + } +} + +void Connection::on_disconnect(const std::string& error_message){ + std::lock_guard lg(m_lock); + if (!m_disconnect_error){ + m_disconnect_error = error_message.empty() ? "closed by the bridge" : error_message; + } + m_cv.notify_all(); +} + + + +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h new file mode 100644 index 0000000000..1ea367da7d --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Connection.h @@ -0,0 +1,112 @@ +/* MgbaLink Connection + * + * From: https://github.com/PokemonAutomation/ + * + * One TCP connection to one "bridge.lua" running inside mGBA (MgbaLink v1, + * lab/mgba/PROTOCOL.md on the fork's agents branch). Every command is one + * line and gets exactly one reply line; commands from several threads are + * serialized. The bridge holds one client at a time, so the destructor + * says BYE, which also releases every key and grant it holds (unless the + * connection was aborted: then it only closes the socket, which the bridge + * treats the same way). + * + * Qt-free. The socket's receive thread comes from the thread pool passed in. + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Connection_H +#define PokemonAutomation_Controllers_MgbaLink_Connection_H + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/Sockets/ClientSocket.h" +#include "MgbaLink_Protocol.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +// A synchronous MgbaLink v1 client: open(), then one command at a time. +class Connection final : private AbstractClientSocket::Listener{ +public: + Connection( + Logger& logger, + ThreadPool& thread_pool, + std::string host, + uint16_t port, + std::chrono::milliseconds timeout = std::chrono::seconds(5) + ); + ~Connection(); + + // Connects and says "HELLO 1". Throws ConnectionException if the bridge + // cannot be reached, refuses the client ("ERR busy"), replies with + // anything but a v1 HELLO, or the connection is aborted. Call it once + // per object (a second call throws InternalProgramError); to reconnect, + // make a new Connection. + HelloReply open(); + + // Sends "BYE" (best effort) and closes. Safe to call more than once. + void close() noexcept; + + // Ends the connection without waiting for the bridge: open() or a + // command in progress throws ConnectionException at once, as does every + // later one, and close() skips BYE. Safe to call from any thread. + void abort() noexcept; + + // Sends one command line and returns its reply line. Throws + // ConnectionException on a timeout, a lost connection or an "ERR" reply. + std::string command(std::string_view line); + + // "FRAME": the bridge writes the last finished frame as a PNG. + FrameReply frame(); + + // "KEYS ": hold "mask" from "frame" on. Returns how many + // frames late the command arrived (0 = on time). + uint64_t keys(uint64_t frame, uint16_t mask); + +private: + virtual void on_connect_finished(const std::string& error_message) override; + virtual void on_receive_data(const void* data, size_t bytes) override; + virtual void on_disconnect(const std::string& error_message) override; + + void connect_socket(); + std::string wait_for_reply(std::string_view command_name); + [[noreturn]] void fail(std::string message); + +private: + Logger& m_logger; + const std::string m_host; + const uint16_t m_port; + const std::chrono::milliseconds m_timeout; + + // Held for a whole command: one request in flight at a time. + Mutex m_command_lock; + + // Guards everything below; the socket's receive thread writes it. + Mutex m_lock; + ConditionVariable m_cv; + std::optional m_connect_error; + std::optional m_disconnect_error; + bool m_opened = false; + bool m_aborted = false; + LineSplitter m_splitter; + std::deque m_replies; + + // Last member: destroyed first, which joins its receive thread before + // the state above goes away. + ClientSocket m_socket; +}; + + + +} // namespace MgbaLink +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp new file mode 100644 index 0000000000..44fe179482 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp @@ -0,0 +1,162 @@ +/* MgbaLink Protocol + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "MgbaLink_Protocol.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +namespace{ + +// Splits on single spaces, at most "max_parts" parts; the last part keeps +// the rest of the line (a FRAME path may contain spaces). +std::vector split_spaces(std::string_view text, size_t max_parts){ + std::vector parts; + while (parts.size() + 1 < max_parts){ + size_t space = text.find(' '); + if (space == std::string_view::npos){ + break; + } + parts.emplace_back(text.substr(0, space)); + text.remove_prefix(space + 1); + } + parts.emplace_back(text); + return parts; +} + +// Digits only: no sign, no spaces, no empty string. +std::optional parse_unsigned(std::string_view text){ + uint64_t value = 0; + if (text.empty()){ + return std::nullopt; + } + std::from_chars_result result = std::from_chars(text.data(), text.data() + text.size(), value); + if (result.ec != std::errc() || result.ptr != text.data() + text.size()){ + return std::nullopt; + } + return value; +} + +} // namespace + + + +std::optional parse_hello_reply(std::string_view reply){ + std::vector parts = split_spaces(reply, 6); + if (parts.size() != 5 || parts[0] != "OK" || parts[1] != "1"){ + return std::nullopt; + } + for (std::string_view part : parts){ + if (part.empty()){ + return std::nullopt; + } + } + return HelloReply{std::string(parts[2]), std::string(parts[3]), std::string(parts[4])}; +} + +std::optional parse_ok_frame_reply(std::string_view reply){ + std::vector parts = split_spaces(reply, 3); + if (parts.size() != 2 || parts[0] != "OK"){ + return std::nullopt; + } + return parse_unsigned(parts[1]); +} + +std::optional parse_keys_reply(std::string_view reply){ + if (reply == "OK"){ + return 0; + } + std::vector parts = split_spaces(reply, 3); + if (parts.size() != 2 || parts[0] != "LATE"){ + return std::nullopt; + } + return parse_unsigned(parts[1]); +} + +std::optional parse_frame_reply(std::string_view reply){ + std::vector parts = split_spaces(reply, 5); + if (parts.size() != 5 || parts[0] != "FRAME" || parts[4].empty()){ + return std::nullopt; + } + std::optional frame = parse_unsigned(parts[1]); + std::optional width = parse_unsigned(parts[2]); + std::optional height = parse_unsigned(parts[3]); + if (!frame || !width || !height){ + return std::nullopt; + } + return FrameReply{*frame, (size_t)*width, (size_t)*height, std::string(parts[4])}; +} + + + +std::vector LineSplitter::push(std::string_view data){ + std::vector lines; + if (m_overflowed){ + return lines; + } + while (!data.empty()){ + size_t newline = data.find('\n'); + std::string_view piece = data.substr(0, newline); + if (m_buffer.size() + piece.size() > m_max_line + 1){ + // "+ 1" leaves room for the CR that is dropped below. + m_buffer.clear(); + m_overflowed = true; + return lines; + } + m_buffer.append(piece); + if (newline == std::string_view::npos){ + break; + } + if (!m_buffer.empty() && m_buffer.back() == '\r'){ + m_buffer.pop_back(); + } + if (m_buffer.size() > m_max_line){ + m_buffer.clear(); + m_overflowed = true; + return lines; + } + lines.emplace_back(std::move(m_buffer)); + m_buffer.clear(); + data.remove_prefix(newline + 1); + } + return lines; +} + + + +WallClock FrameClock::timestamp(uint64_t frame) const{ + double frames = frame >= m_f0 + ? (double)(frame - m_f0) + : -(double)(m_f0 - frame); + double microseconds = std::round(frames * 1000000.0 / GBA_FRAMES_PER_SECOND); + return m_t0 + std::chrono::duration_cast( + std::chrono::microseconds((int64_t)microseconds) + ); +} + + +FrameClock::Stamp FrameClock::stamp(WallClock now, uint64_t frame, WallDuration max_lag){ + Stamp result; + WallClock time = timestamp(frame); + if (time > now || now - time > max_lag){ + result.reanchored_after_lag = time < now; + m_t0 = now; + m_f0 = frame; + time = now; + } + if (m_last_stamp && time < *m_last_stamp){ + time = *m_last_stamp; + } + m_last_stamp = time; + result.time = time; + return result; +} + +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h new file mode 100644 index 0000000000..9a1e9aec49 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Protocol.h @@ -0,0 +1,134 @@ +/* MgbaLink Protocol + * + * From: https://github.com/PokemonAutomation/ + * + * Reply parsing, line framing and the frame clock of MgbaLink v1, the text + * protocol spoken by "bridge.lua" inside mGBA (lab/mgba/PROTOCOL.md on the + * fork's agents branch). Pure functions; the socket lives in + * MgbaLink_Connection. + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Protocol_H +#define PokemonAutomation_Controllers_MgbaLink_Protocol_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +// The fields of a "HELLO 1" reply. +struct HelloReply{ + std::string game_code; // "BPRE" for FireRed US. + std::string bridge_version; // "bridge-0.1" + std::string mode; // "paced" or "free" +}; + +// The fields of a "FRAME" reply (0.10.5: the frame is a PNG on disk). +struct FrameReply{ + uint64_t frame = 0; + size_t width = 0; + size_t height = 0; + std::string path; // PNG written by the bridge; may contain spaces. +}; + + +// Each parser returns nullopt when the reply does not have the expected +// shape (including any "ERR ..." reply). + +// "OK 1 " +std::optional parse_hello_reply(std::string_view reply); + +// "OK " (GRANT, SAVE, LOAD) +std::optional parse_ok_frame_reply(std::string_view reply); + +// KEYS: "OK" -> 0, "LATE " -> n (frames late). +std::optional parse_keys_reply(std::string_view reply); + +// "FRAME " +std::optional parse_frame_reply(std::string_view reply); + + + +// Splits a byte stream into lines: LF ends a line, a CR right before it is +// dropped. A line longer than "max_line" bytes sets "overflowed()" and +// drops everything buffered so far; the caller should then disconnect. +class LineSplitter{ +public: + // The bridge drops a client whose command line is longer than this; + // replies are bounded the same way (a FRAME path is the longest reply). + static constexpr size_t MAX_LINE_BYTES = 4096; + + explicit LineSplitter(size_t max_line = MAX_LINE_BYTES) + : m_max_line(max_line) + {} + + // Appends "data" and returns the lines it completed, in order. + std::vector push(std::string_view data); + + bool overflowed() const{ + return m_overflowed; + } + +private: + size_t m_max_line; + std::string m_buffer; + bool m_overflowed = false; +}; + + + +// Maps emulated frame numbers to app time. Anchored at a known pair +// (t0, F0): frame F happened at t0 + (F - F0) * 1000 / 59.7275 ms, so a +// frame's timestamp comes from its number, not from when the app read it. +class FrameClock{ +public: + // GBA frame rate: 16.78 MHz / 280,896 cycles per frame. + static constexpr double GBA_FRAMES_PER_SECOND = 59.7275; + + // The time a frame is stamped with, and whether the clock had to catch + // up with a paused or slow mGBA to get it. + struct Stamp{ + WallClock time{}; + bool reanchored_after_lag = false; + }; + + FrameClock(WallClock t0, uint64_t f0) + : m_t0(t0) + , m_f0(f0) + {} + + WallClock timestamp(uint64_t frame) const; + + // The stamp for "frame", read from the bridge at "now". While mGBA runs + // free (no GRANT pacing yet), its clock drifts against the app's and + // stops while it is paused, so the clock re-anchors at (now, frame) + // when "frame" maps after "now" (mGBA running ahead: a frame cannot be + // stamped later than it was read) or more than "max_lag" before it + // (mGBA paused or slow). Stamps never go backwards: a frame that maps + // before the previous stamp (a LOAD) gets the previous stamp. + Stamp stamp(WallClock now, uint64_t frame, WallDuration max_lag); + + WallClock anchor_time() const{ + return m_t0; + } + uint64_t anchor_frame() const{ + return m_f0; + } + +private: + WallClock m_t0; + uint64_t m_f0; + std::optional m_last_stamp; +}; + + +} // namespace MgbaLink +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp new file mode 100644 index 0000000000..4163405e9a --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.cpp @@ -0,0 +1,604 @@ +/* MgbaLink Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#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" +#include "CommonFramework/Tools/GlobalThreadPools.h" +#ifdef _WIN32 +#include "Common/Cpp/Sockets/ServerSocket_WinSocket.h" +#else +#include "Common/Cpp/Sockets/ServerSocket_POSIX.h" +#endif +#include "MgbaLink_Connection.h" +#include "MgbaLink_Protocol.h" +#include "MgbaLink_Tests.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + +namespace{ + +#ifdef _WIN32 +using FakeServerSocket = ServerSocket_WinSocket; +#else +using FakeServerSocket = ServerSocket_POSIX; +#endif + + +// Line framing over a byte stream. +struct LineFramingInput{ + std::vector chunks; + size_t max_line; +}; +struct LineFramingExpectation{ + std::vector lines; + bool overflowed; +}; +struct LineFramingCase{ + std::string name; + LineFramingInput input; + LineFramingExpectation expectation; +}; + +class Test_LineFraming : public UnitTest{ +public: + Test_LineFraming(LineFramingCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - line framing - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + LineSplitter splitter(m_case.input.max_line); + std::vector lines; + for (const std::string& chunk : m_case.input.chunks){ + for (std::string& line : splitter.push(chunk)){ + lines.emplace_back(std::move(line)); + } + } + if (lines != m_case.expectation.lines || splitter.overflowed() != m_case.expectation.overflowed){ + return std::format("got {} line(s), overflowed={}", lines.size(), splitter.overflowed()); + } + return true; + } + +private: + LineFramingCase m_case; +}; + + + +// Reply parsing. Each parser's result is printed as one string, so one table +// covers all reply kinds; nullopt means the reply was rejected. +enum class ReplyKind{ + HELLO, + OK_FRAME, + KEYS, + FRAME, +}; +struct ReplyInput{ + ReplyKind kind; + std::string reply; +}; +struct ReplyExpectation{ + std::optional parsed; +}; +struct ReplyCase{ + std::string name; + ReplyInput input; + ReplyExpectation expectation; +}; + +std::optional describe_reply(const ReplyInput& input){ + switch (input.kind){ + case ReplyKind::HELLO:{ + std::optional hello = parse_hello_reply(input.reply); + if (!hello){ + return std::nullopt; + } + return std::format("{}|{}|{}", hello->game_code, hello->bridge_version, hello->mode); + } + case ReplyKind::OK_FRAME:{ + std::optional frame = parse_ok_frame_reply(input.reply); + return frame ? std::optional(std::to_string(*frame)) : std::nullopt; + } + case ReplyKind::KEYS:{ + std::optional late = parse_keys_reply(input.reply); + return late ? std::optional(std::to_string(*late)) : std::nullopt; + } + case ReplyKind::FRAME:{ + std::optional frame = parse_frame_reply(input.reply); + if (!frame){ + return std::nullopt; + } + return std::format("{}|{}|{}|{}", frame->frame, frame->width, frame->height, frame->path); + } + } + return std::nullopt; +} + +class Test_Reply : public UnitTest{ +public: + Test_Reply(ReplyCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - reply - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::optional parsed = describe_reply(m_case.input); + if (parsed != m_case.expectation.parsed){ + return std::format("parsed as <{}>", parsed.value_or("(rejected)")); + } + return true; + } + +private: + ReplyCase m_case; +}; + + + +// Frame numbers to timestamps, anchored at (t0, frame 1000). +struct FrameClockInput{ + uint64_t frame; +}; +struct FrameClockExpectation{ + std::chrono::microseconds offset_from_anchor; +}; +struct FrameClockCase{ + std::string name; + FrameClockInput input; + FrameClockExpectation expectation; +}; + +const WallClock CLOCK_T0 = WallClock() + std::chrono::hours(1000); + +class Test_FrameClock : public UnitTest{ +public: + Test_FrameClock(FrameClockCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - frame clock - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + FrameClock clock(CLOCK_T0, 1000); + WallDuration offset = clock.timestamp(m_case.input.frame) - CLOCK_T0; + if (offset != std::chrono::duration_cast(m_case.expectation.offset_from_anchor)){ + return std::format("offset {} us", std::chrono::duration_cast(offset).count()); + } + return true; + } + +private: + FrameClockCase m_case; +}; + + + +// Stamps for a series of reads, the clock anchored at (t0, frame 1000). +// Frame 1060 maps to 1004.6 ms after the anchor. +struct ClockRead{ + uint64_t frame = 0; + std::chrono::milliseconds read_after_anchor{}; +}; +ClockRead frame_read_at(uint64_t frame, std::chrono::milliseconds read_after_anchor){ + return ClockRead{frame, read_after_anchor}; +} +struct StampInput{ + std::vector reads; +}; +struct StampExpectation{ + std::vector stamps_after_anchor; + size_t lag_reanchors = 0; +}; +struct StampCase{ + std::string name; + StampInput input; + StampExpectation expectation; +}; + +std::string join_milliseconds(const std::vector& values){ + std::string text; + for (std::chrono::milliseconds value : values){ + text += text.empty() ? "" : ", "; + text += std::to_string(value.count()); + } + return text; +} + +class Test_Stamp : public UnitTest{ +public: + Test_Stamp(StampCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - frame stamps - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + FrameClock clock(CLOCK_T0, 1000); + std::vector stamps; + size_t lag_reanchors = 0; + for (const ClockRead& read : m_case.input.reads){ + FrameClock::Stamp stamp = clock.stamp(CLOCK_T0 + read.read_after_anchor, read.frame, std::chrono::milliseconds(500)); + stamps.emplace_back(std::chrono::duration_cast(stamp.time - CLOCK_T0)); + lag_reanchors += stamp.reanchored_after_lag ? 1 : 0; + } + if (stamps != m_case.expectation.stamps_after_anchor || lag_reanchors != m_case.expectation.lag_reanchors){ + return std::format( + "stamps [{}] ms with {} lag re-anchor(s), expected [{}] ms with {}", + join_milliseconds(stamps), lag_reanchors, + join_milliseconds(m_case.expectation.stamps_after_anchor), m_case.expectation.lag_reanchors + ); + } + return true; + } + +private: + StampCase m_case; +}; + + + +// How the fake bridge treats its client. +enum class BridgeMode{ + ANSWERS, // Answers each command line from a script. + BUSY, // Refuses the client with "ERR busy", as a bridge that has one. + HANGS, // Answers HELLO, then never answers again. +}; + +// A bridge stand-in. +class FakeBridge : public AbstractServerSocket::Listener{ +public: + FakeBridge(ThreadPool& thread_pool, BridgeMode mode) + : m_server(thread_pool) + , m_mode(mode) + { + m_server.add_listener(*this); + } + ~FakeBridge(){ + m_server.remove_listener(*this); + } + + // Listens on a random port in 40000..59999; nullopt if 8 tries fail. + std::optional start(); + + std::vector commands(){ + std::lock_guard lg(m_lock); + return m_commands; + } + + // Waits up to 5 s for the client to send "count" commands. + bool wait_for_commands(size_t count){ + std::unique_lock lg(m_lock); + return m_cv.wait_for(lg, std::chrono::seconds(5), [&]{ return m_commands.size() >= count; }); + } + +private: + virtual void on_listen_finished(const std::string& error_message) override{ + std::lock_guard lg(m_lock); + m_listen_error = error_message; + m_cv.notify_all(); + } + virtual void on_connect() override{ + if (m_mode == BridgeMode::BUSY){ + // Closed once the client's first line is read (no reset that + // could drop this line before the client reads it). + send("ERR busy\n"); + } + } + virtual void on_receive_data(const void* data, size_t bytes) override; + + void send(std::string_view text){ + m_server.send(text.data(), text.size()); + } + + static std::string reply_for(std::string_view line); + +private: + FakeServerSocket m_server; + const BridgeMode m_mode; + LineSplitter m_splitter; + Mutex m_lock; + ConditionVariable m_cv; + std::optional m_listen_error; + std::vector m_commands; +}; + +std::optional FakeBridge::start(){ + std::mt19937 rng{std::random_device{}()}; + for (int attempt = 0; attempt < 8; attempt++){ + uint16_t port = (uint16_t)(40000 + rng() % 20000); + { + std::lock_guard lg(m_lock); + m_listen_error.reset(); + } + m_server.start("127.0.0.1", port); + std::unique_lock lg(m_lock); + m_cv.wait_for(lg, std::chrono::seconds(10), [this]{ return m_listen_error.has_value(); }); + if (m_listen_error && m_listen_error->empty()){ + return port; + } + } + return std::nullopt; +} + +void FakeBridge::on_receive_data(const void* data, size_t bytes){ + if (m_mode == BridgeMode::BUSY){ + // Record what the client sent, then refuse it. + std::lock_guard lg(m_lock); + m_commands.emplace_back("(refused)"); + m_cv.notify_all(); + m_server.close_connection(); + return; + } + std::vector lines = m_splitter.push(std::string_view(static_cast(data), bytes)); + for (const std::string& line : lines){ + { + std::lock_guard lg(m_lock); + m_commands.emplace_back(line); + m_cv.notify_all(); + } + if (m_mode == BridgeMode::HANGS && line != "HELLO 1"){ + continue; + } + std::string reply = reply_for(line); + // Two writes, so the client has to reassemble the line. + send(std::string_view(reply).substr(0, 3)); + send(std::string_view(reply).substr(3)); + if (line == "BYE"){ + m_server.close_connection(); + } + } +} + +std::string FakeBridge::reply_for(std::string_view line){ + // The emulator is about to run frame 120. + if (line == "HELLO 1"){ + return "OK 1 BPRE bridge-0.1 paced\r\n"; + } + if (line == "FRAME"){ + return "FRAME 120 240 160 /tmp/mgba bridge.png\n"; + } + if (line == "KEYS 100 1"){ + return "LATE 20\n"; + } + if (line == "KEYS 130 1"){ + return "OK\n"; + } + if (line == "BYE"){ + return "OK BYE\n"; + } + return std::format("ERR unknown {}\n", line.substr(0, line.find(' '))); +} + + + +// A whole session against the fake bridge: open (twice, if asked), FRAME, +// KEYS late and on time, an ERR reply, then close (BYE). +struct SessionInput{ + BridgeMode bridge_mode = BridgeMode::ANSWERS; + bool open_twice = false; + // Abort from another thread once the bridge has seen FRAME. + bool abort_after_frame = false; +}; +struct SessionExpectation{ + std::vector results; + std::vector bridge_saw; +}; +struct SessionCase{ + std::string name; + SessionInput input; + SessionExpectation expectation; +}; + +std::string join_lines(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += text.empty() ? "<" : ", <"; + text += line; + text += ">"; + } + return text; +} + +void run_commands(Connection& connection, std::vector& results){ + FrameReply frame = connection.frame(); + results.emplace_back(std::format("frame {} {}", frame.frame, frame.path)); + results.emplace_back(std::format("late {}", connection.keys(100, 1))); + results.emplace_back(std::format("late {}", connection.keys(130, 1))); + connection.command("FLY"); + results.emplace_back("FLY accepted"); +} + +std::vector run_session( + Logger& logger, ThreadPool& thread_pool, FakeBridge& bridge, uint16_t port, const SessionInput& input +){ + std::vector results; + Connection connection(logger, thread_pool, "127.0.0.1", port); + AsyncTask aborter; + try{ + HelloReply hello = connection.open(); + results.emplace_back(std::format("hello {} {}", hello.game_code, hello.mode)); + if (input.open_twice){ + try{ + connection.open(); + results.emplace_back("second open accepted"); + }catch (InternalProgramError&){ + results.emplace_back("second open refused"); + } + } + if (input.abort_after_frame){ + aborter = thread_pool.dispatch_now_blocking([&]{ + bridge.wait_for_commands(2); + connection.abort(); + }); + } + run_commands(connection, results); + }catch (ConnectionException& e){ + bool busy = e.message().find("ERR busy") != std::string::npos; + results.emplace_back(busy ? "failed: ERR busy" : std::format("failed: {}", e.message())); + } + aborter.wait_and_ignore_exceptions(); + return results; +} + +class Test_Session : public UnitTest{ +public: + Test_Session(SessionCase test_case) + : UnitTest(std::format("Controllers::MgbaLink - fake bridge - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + ThreadPool& thread_pool = GlobalThreadPools::unlimited_normal(); + FakeBridge bridge(thread_pool, m_case.input.bridge_mode); + std::optional port = bridge.start(); + if (!port){ + return UnitTestResult(UnitTestResult::SKIPPED, "No free port for the fake bridge."); + } + std::vector results = run_session(logger, thread_pool, bridge, *port, m_case.input); + if (results != m_case.expectation.results){ + return std::format("results {}, expected {}", join_lines(results), join_lines(m_case.expectation.results)); + } + bridge.wait_for_commands(m_case.expectation.bridge_saw.size()); + std::vector saw = bridge.commands(); + if (saw != m_case.expectation.bridge_saw){ + return std::format("the bridge saw {}, expected {}", join_lines(saw), join_lines(m_case.expectation.bridge_saw)); + } + return true; + } + +private: + SessionCase m_case; +}; + + +void add_framing_and_reply_tests(UnitTestDatabase& database){ + const LineFramingCase LINE_FRAMING_CASES[] = { + {.name = "one line", .input = {{"OK\n"}, 16}, .expectation = {{"OK"}, false}}, + {.name = "CR before LF dropped", .input = {{"OK 5\r\n"}, 16}, .expectation = {{"OK 5"}, false}}, + {.name = "line split across chunks", .input = {{"FRAME 12", "0 240 160 /a b.png\nOK"}, 64}, .expectation = {{"FRAME 120 240 160 /a b.png"}, false}}, + {.name = "two lines in one chunk", .input = {{"OK\nLATE 3\n"}, 16}, .expectation = {{"OK", "LATE 3"}, false}}, + {.name = "empty line", .input = {{"\n"}, 16}, .expectation = {{""}, false}}, + {.name = "exactly max", .input = {{"aaaaaaaa\r\n"}, 8}, .expectation = {{"aaaaaaaa"}, false}}, + {.name = "over max without LF", .input = {{"aaaaaaaaaa"}, 8}, .expectation = {{}, true}}, + {.name = "over max across chunks", .input = {{"aaaaa", "aaaaa\n"}, 8}, .expectation = {{}, true}}, + }; + for (const LineFramingCase& test_case : LINE_FRAMING_CASES){ + database.add(test_case); + } + + const ReplyCase REPLY_CASES[] = { + {.name = "HELLO v1", .input = {ReplyKind::HELLO, "OK 1 BPRE bridge-0.1 paced"}, .expectation = {"BPRE|bridge-0.1|paced"}}, + {.name = "HELLO wrong version", .input = {ReplyKind::HELLO, "OK 2 BPRE bridge-0.1 paced"}, .expectation = {std::nullopt}}, + {.name = "HELLO missing mode", .input = {ReplyKind::HELLO, "OK 1 BPRE bridge-0.1"}, .expectation = {std::nullopt}}, + {.name = "HELLO extra field", .input = {ReplyKind::HELLO, "OK 1 BPRE bridge-0.1 paced x"}, .expectation = {std::nullopt}}, + {.name = "HELLO busy", .input = {ReplyKind::HELLO, "ERR busy"}, .expectation = {std::nullopt}}, + {.name = "GRANT frame", .input = {ReplyKind::OK_FRAME, "OK 4521"}, .expectation = {"4521"}}, + {.name = "bare OK", .input = {ReplyKind::OK_FRAME, "OK"}, .expectation = {std::nullopt}}, + {.name = "BYE is not a frame", .input = {ReplyKind::OK_FRAME, "OK BYE"}, .expectation = {std::nullopt}}, + {.name = "KEYS on time", .input = {ReplyKind::KEYS, "OK"}, .expectation = {"0"}}, + {.name = "KEYS late", .input = {ReplyKind::KEYS, "LATE 3"}, .expectation = {"3"}}, + {.name = "KEYS late without count", .input = {ReplyKind::KEYS, "LATE"}, .expectation = {std::nullopt}}, + {.name = "KEYS usage error", .input = {ReplyKind::KEYS, "ERR usage KEYS "}, .expectation = {std::nullopt}}, + {.name = "FRAME on 0.10.5", .input = {ReplyKind::FRAME, "FRAME 1234 240 160 /tmp/mgba-bridge-47801.png"}, .expectation = {"1234|240|160|/tmp/mgba-bridge-47801.png"}}, + {.name = "FRAME path with spaces", .input = {ReplyKind::FRAME, "FRAME 7 240 160 /Users/x/My Folder/f.png"}, .expectation = {"7|240|160|/Users/x/My Folder/f.png"}}, + {.name = "FRAME without path", .input = {ReplyKind::FRAME, "FRAME 7 240 160"}, .expectation = {std::nullopt}}, + {.name = "FRAME negative number", .input = {ReplyKind::FRAME, "FRAME -7 240 160 /f.png"}, .expectation = {std::nullopt}}, + {.name = "FRAME no game", .input = {ReplyKind::FRAME, "ERR no-game"}, .expectation = {std::nullopt}}, + }; + for (const ReplyCase& test_case : REPLY_CASES){ + database.add(test_case); + } +} + +void add_clock_tests(UnitTestDatabase& database){ + // One frame = 1000 / 59.7275 ms = 16742.7 us. + const FrameClockCase FRAME_CLOCK_CASES[] = { + {.name = "anchor frame", .input = {1000}, .expectation = {std::chrono::microseconds(0)}}, + {.name = "next frame", .input = {1001}, .expectation = {std::chrono::microseconds(16743)}}, + {.name = "60 frames", .input = {1060}, .expectation = {std::chrono::microseconds(1004562)}}, + {.name = "before the anchor (after a LOAD)", .input = {940}, .expectation = {std::chrono::microseconds(-1004562)}}, + }; + for (const FrameClockCase& test_case : FRAME_CLOCK_CASES){ + database.add(test_case); + } + + using std::chrono::milliseconds; + const StampCase STAMP_CASES[] = { + {.name = "small lag keeps the anchor", .input = {{frame_read_at(1060, milliseconds(1100))}}, .expectation = {{milliseconds(1004)}, 0}}, + {.name = "mGBA paused re-anchors at the read", .input = {{frame_read_at(1060, milliseconds(10000))}}, .expectation = {{milliseconds(10000)}, 1}}, + {.name = "mGBA running ahead is stamped at the read", .input = {{frame_read_at(1060, milliseconds(900))}}, .expectation = {{milliseconds(900)}, 0}}, + { + .name = "mGBA running ahead stays at or before each read", + .input = {{frame_read_at(1060, milliseconds(900)), frame_read_at(1120, milliseconds(1900))}}, + .expectation = {{milliseconds(900), milliseconds(1900)}, 0}, + }, + { + .name = "frame number going back keeps stamps monotonic", + .input = {{frame_read_at(1060, milliseconds(1010)), frame_read_at(1059, milliseconds(1012))}}, + .expectation = {{milliseconds(1004), milliseconds(1004)}, 0}, + }, + }; + for (const StampCase& test_case : STAMP_CASES){ + database.add(test_case); + } +} + +void add_session_tests(UnitTestDatabase& database){ + const SessionCase SESSION_CASES[] = { + { + .name = "session", + .input = {}, + .expectation = { + {"hello BPRE paced", "frame 120 /tmp/mgba bridge.png", "late 20", "late 0", "failed: MgbaLink: FLY -> ERR unknown FLY"}, + {"HELLO 1", "FRAME", "KEYS 100 1", "KEYS 130 1", "FLY", "BYE"}, + }, + }, + { + .name = "busy", + .input = {.bridge_mode = BridgeMode::BUSY}, + .expectation = {{"failed: ERR busy"}, {"(refused)"}}, + }, + { + .name = "second open refused", + .input = {.open_twice = true}, + .expectation = { + {"hello BPRE paced", "second open refused", "frame 120 /tmp/mgba bridge.png", "late 20", "late 0", "failed: MgbaLink: FLY -> ERR unknown FLY"}, + {"HELLO 1", "FRAME", "KEYS 100 1", "KEYS 130 1", "FLY", "BYE"}, + }, + }, + { + // Without abort() FRAME would wait out the 5 s timeout, then + // report "timed out"; aborted, it fails at once and skips BYE. + .name = "abort while waiting for a reply", + .input = {.bridge_mode = BridgeMode::HANGS, .abort_after_frame = true}, + .expectation = { + {"hello BPRE paced", "failed: MgbaLink: No reply to FRAME (aborted)."}, + {"HELLO 1", "FRAME"}, + }, + }, + }; + for (const SessionCase& test_case : SESSION_CASES){ + database.add(test_case); + } +} + +} // namespace + + +void add_tests(UnitTestDatabase& database){ + add_framing_and_reply_tests(database); + add_clock_tests(database); + add_session_tests(database); +} + + + +} // namespace MgbaLink +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h new file mode 100644 index 0000000000..94ce429843 --- /dev/null +++ b/SerialPrograms/Source/Controllers/MgbaLink/MgbaLink_Tests.h @@ -0,0 +1,23 @@ +/* MgbaLink Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Controllers_MgbaLink_Tests_H +#define PokemonAutomation_Controllers_MgbaLink_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace MgbaLink{ + + +// Reply parsing, line framing, the frame clock, and a connection against a +// fake bridge on 127.0.0.1. +void add_tests(UnitTestDatabase& database); + + +} // namespace MgbaLink +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index afd502a799..6dc016aff1 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -280,6 +280,12 @@ file(GLOB CORE_LIBRARY_SOURCES Source/Controllers/Joystick.cpp Source/Controllers/Joystick.h Source/Controllers/JoystickTools.h + Source/Controllers/MgbaLink/MgbaLink_Connection.cpp + Source/Controllers/MgbaLink/MgbaLink_Connection.h + Source/Controllers/MgbaLink/MgbaLink_Protocol.cpp + Source/Controllers/MgbaLink/MgbaLink_Protocol.h + Source/Controllers/MgbaLink/MgbaLink_Tests.cpp + Source/Controllers/MgbaLink/MgbaLink_Tests.h Source/Controllers/NullController.cpp Source/Controllers/NullController.h Source/Controllers/PABotBase2/PABotBase2_CommandQueueManager.cpp @@ -787,6 +793,10 @@ file(GLOB LIBRARY_SOURCES Source/CommonFramework/VideoPipeline/VideoSourceOption.h Source/CommonFramework/VideoPipeline/Sources/VideoSource_Camera.cpp Source/CommonFramework/VideoPipeline/Sources/VideoSource_Camera.h + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.cpp + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink.h + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.cpp + Source/CommonFramework/VideoPipeline/Sources/VideoSource_MgbaLink_Tests.h Source/CommonFramework/VideoPipeline/Sources/VideoSource_Null.cpp Source/CommonFramework/VideoPipeline/Sources/VideoSource_Null.h Source/CommonFramework/VideoPipeline/Sources/VideoSource_StillImage.cpp