From 5f54d9075e29e6108ff71b867ab361ba01360882 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 23 Jun 2025 10:51:24 +0200 Subject: [PATCH] fix(modem): Fix get_network_registration_state() to accept two params Co-authored-by: Beernaert Robbe Closes https://github.com/espressif/esp-protocols/issues/826 --- .../src/esp_modem_command_library.cpp | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/components/esp_modem/src/esp_modem_command_library.cpp b/components/esp_modem/src/esp_modem_command_library.cpp index 024008fcf..9d69bbe80 100644 --- a/components/esp_modem/src/esp_modem_command_library.cpp +++ b/components/esp_modem/src/esp_modem_command_library.cpp @@ -635,17 +635,40 @@ command_result get_network_registration_state(CommandableIf *t, int &state) } constexpr std::string_view pattern = "+CEREG: "; - int state_pos_start; - int state_pos_end; - if (out.find(pattern) == std::string::npos || (state_pos_start = out.find(',')) == std::string::npos) { + + // Check if pattern exists and find its position + size_t pattern_pos = out.find(pattern); + if (pattern_pos == std::string::npos) { return command_result::FAIL; } - if (out.find(pattern) == std::string::npos || (state_pos_end = out.find(',', state_pos_start)) == std::string::npos) { - if (std::from_chars(out.data() + state_pos_start, out.data() + out.size(), state).ec == std::errc::invalid_argument) { - return command_result::FAIL; + // Find the first comma after the pattern + size_t state_pos_start = out.find(',', pattern_pos); + if (state_pos_start == std::string::npos) { + return command_result::FAIL; + } + + // Find the end of the state value - either a second comma or end of line + size_t state_pos_end = out.find(',', state_pos_start + 1); + if (state_pos_end == std::string::npos) { + // No second comma found, look for end of line characters + state_pos_end = out.find('\r', state_pos_start); + if (state_pos_end == std::string::npos) { + state_pos_end = out.find('\n', state_pos_start); } - } else if (std::from_chars(out.data() + state_pos_start, out.data() + state_pos_end, state).ec == std::errc::invalid_argument) { + if (state_pos_end == std::string::npos) { + // No end delimiter found, use end of string + state_pos_end = out.size(); + } + } + + // Validate that we have a valid range to parse + if (state_pos_start + 1 >= state_pos_end) { + return command_result::FAIL; + } + + // Extract state value (skip the comma) + if (std::from_chars(out.data() + state_pos_start + 1, out.data() + state_pos_end, state).ec == std::errc::invalid_argument) { return command_result::FAIL; }