diff --git a/testing/SimHarness.cpp b/testing/SimHarness.cpp index 7f716d67f3..1817469294 100644 --- a/testing/SimHarness.cpp +++ b/testing/SimHarness.cpp @@ -75,6 +75,63 @@ uint8_t SimHarness::CycleTime() const { return millis_per_cycle_; } +// Serial support implementation +void SimHarness::ProcessSerialInput() { + // This will be called by RunCycle() to process any pending serial input + // The actual processing is handled by the virtual HardwareSerial implementation +} + +void SimHarness::SendSerialData(const uint8_t *data, size_t length) { + // Get direct access to the virtual device's serial port + auto &serial = Serial; + serial.injectInput(data, length); +} + +std::vector SimHarness::GetSerialOutput() { + // Get direct access to the virtual device's serial port + auto &serial = Serial; + return serial.getOutputBuffer(); +} + +// Focus protocol helper implementations +std::string SimHarness::SendFocusCommand(const std::string &command) { + // Ensure command ends with newline + std::string cmd = command; + if (cmd.empty() || cmd.back() != '\n') { + cmd += '\n'; + } + + // Send command + SendString(cmd); + + // Run cycles until we get a complete response + // (ends with \r\n.\r\n) + std::string response; + size_t max_cycles = 100; // Prevent infinite loops + size_t cycles = 0; + + while (cycles++ < max_cycles) { + RunCycle(); + response = GetSerialOutputAsString(); + if (IsFocusResponse(response)) break; + } + + return StripFocusTerminator(response); +} + +bool SimHarness::IsFocusResponse(const std::string &response) { + static const std::string terminator = "\r\n.\r\n"; + return response.length() >= terminator.length() && + response.substr(response.length() - terminator.length()) == terminator; +} + +std::string SimHarness::StripFocusTerminator(const std::string &response) { + static const std::string terminator = "\r\n.\r\n"; + if (IsFocusResponse(response)) { + return response.substr(0, response.length() - terminator.length()); + } + return response; +} } // namespace testing } // namespace kaleidoscope diff --git a/testing/SimHarness.h b/testing/SimHarness.h index 424a43cab1..f070b19102 100644 --- a/testing/SimHarness.h +++ b/testing/SimHarness.h @@ -18,6 +18,8 @@ #include // for size_t #include // for uint8_t +#include +#include #include "kaleidoscope/KeyAddr.h" // for KeyAddr #include "testing/gtest.h" // IWYU pragma: keep @@ -37,6 +39,23 @@ class SimHarness { void SetCycleTime(uint8_t millis); uint8_t CycleTime() const; + // Serial support + void ProcessSerialInput(); + void SendString(const std::string &str) { + SendSerialData(reinterpret_cast(str.c_str()), str.length()); + } + void SendSerialData(const uint8_t *data, size_t length); + std::vector GetSerialOutput(); + std::string GetSerialOutputAsString() { + auto output = GetSerialOutput(); + return std::string(output.begin(), output.end()); + } + + // Focus protocol helpers + std::string SendFocusCommand(const std::string &command); + static bool IsFocusResponse(const std::string &response); + static std::string StripFocusTerminator(const std::string &response); + private: uint8_t millis_per_cycle_ = 1; };