From c8afbe68b535b5f76201b0f78320336682f8edd3 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Tue, 9 Sep 2025 06:34:38 -0500 Subject: [PATCH] Use char buffer for probeResponse (#7870) * Use char buffer for probeResponse * \Update src/gps/GPS.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Revert "\Update src/gps/GPS.cpp" This reverts commit 54d64e19f710c2971347507bff5e506b2209602f. * Remove string --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/gps/GPS.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index 88984a890..d4e9076d9 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -1,5 +1,4 @@ #include // Include for strstr -#include #include #include "configuration.h" @@ -1370,34 +1369,42 @@ GnssModel_t GPS::probe(int serialSpeed) GnssModel_t GPS::getProbeResponse(unsigned long timeout, const std::vector &responseMap) { - String response = ""; + char response[256] = {0}; // Fixed buffer instead of String + uint16_t responseLen = 0; unsigned long start = millis(); while (millis() - start < timeout) { if (_serial_gps->available()) { - response += (char)_serial_gps->read(); + char c = _serial_gps->read(); - if (response.endsWith(",") || response.endsWith("\r\n")) { + // Add char to buffer if there's space + if (responseLen < sizeof(response) - 1) { + response[responseLen++] = c; + response[responseLen] = '\0'; + } + + if (c == ',' || (responseLen >= 2 && response[responseLen - 2] == '\r' && response[responseLen - 1] == '\n')) { #ifdef GPS_DEBUG - LOG_DEBUG(response.c_str()); + LOG_DEBUG(response); #endif // check if we can see our chips for (const auto &chipInfo : responseMap) { - if (strstr(response.c_str(), chipInfo.detectionString.c_str()) != nullptr) { + if (strstr(response, chipInfo.detectionString.c_str()) != nullptr) { LOG_INFO("%s detected", chipInfo.chipName.c_str()); return chipInfo.driver; } } } - if (response.endsWith("\r\n")) { - response.trim(); - response = ""; // Reset the response string for the next potential message + if (responseLen >= 2 && response[responseLen - 2] == '\r' && response[responseLen - 1] == '\n') { + // Reset the response buffer for the next potential message + responseLen = 0; + response[0] = '\0'; } } } #ifdef GPS_DEBUG - LOG_DEBUG(response.c_str()); + LOG_DEBUG(response); #endif - return GNSS_MODEL_UNKNOWN; // Return empty string on timeout + return GNSS_MODEL_UNKNOWN; // Return unknown on timeout } GPS *GPS::createGps()