From 28f425c4f8462417b71d8c37aa9397870e475b9d Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Wed, 10 Apr 2024 02:26:15 +0200 Subject: [PATCH 1/2] Fix issue with parsing SMS --- src/ArduinoCellular.cpp | 67 ++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index 55f12f3..d7193b9 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -264,7 +264,7 @@ Time parseTimestamp(const String ×tampStr) { return Time(year, month, day, hour, minute, second, offset); } // Parses a single SMS entry from the data -SMS parseSMSEntry(const String& entry) { +SMS parseSMSEntry(const String& entry, const String& message) { SMS sms; int firstQuoteIndex = entry.indexOf('"'); int secondQuoteIndex = entry.indexOf('"', firstQuoteIndex + 1); @@ -278,38 +278,57 @@ SMS parseSMSEntry(const String& entry) { // Parse the timestamp sms.timestamp = parseTimestamp(rawTimestamp); - - // Extracting the message content - int messageStartIndex = entry.indexOf('\n') + 1; // Assuming message starts after the newline - if (messageStartIndex != -1) { - sms.message = entry.substring(messageStartIndex, entry.indexOf('\n')); - } - + + sms.message = message; return sms; } +// Function to split a string into lines based on a delimiter character +// Filters out empty lines +std::vector splitStringByLines(const String& input, char delimiter = '\n') { + std::vector lines; + int startIndex = 0; + while (startIndex < input.length()) { + int endIndex = input.indexOf(delimiter, startIndex); + if (endIndex == -1) + endIndex = input.length(); + String line = input.substring(startIndex, endIndex); + if(line.length() > 0 && line != "\r" && line != "\n" && line != "\r\n"){ + // Remove trailing \r if it exists + if (line.endsWith("\r")) { + line.remove(line.length() - 1); + } + lines.push_back(line); + } + startIndex = endIndex + 1; + } + return lines; +} + // Splits the entire message string into individual SMS entries and parses them std::vector parseSMSData(const String& data) { - std::vector smsList; - int start = 0; - int end = data.indexOf("\n+CMGL: ", start); - - while (end != -1) { - String entry = data.substring(start, end); - smsList.push_back(parseSMSEntry(entry)); - start = end + 1; - end = data.indexOf("\n+CMGL: ", start); - } - // Adding the last SMS entry, if there's any remaining part - if (start < data.length()) { - smsList.push_back(parseSMSEntry(data.substring(start))); - } + std::vector smsList = std::vector(); + std::vector lines = splitStringByLines(data); + + // Remove last line if it's "OK" + if (lines.size() > 0 && lines[lines.size() - 1] == "OK") { + lines.pop_back(); + } + + for(int i = 0; i < lines.size(); i += 2){ + if (lines[i].startsWith("+CMGL:")) { + String message = ""; + if(i + 1 < lines.size()){ + message = lines[i + 1]; + } + SMS sms = parseSMSEntry(lines[i], message); + smsList.push_back(sms); + } + } - smsList.erase(smsList.begin()); return smsList; } - std::vector ArduinoCellular::getReadSMS(){ String rawMessages = sendATCommand("+CMGL=\"REC READ\""); if(rawMessages.indexOf("OK") == -1){ From 473c232d162fecb4d90dcde9f5402e065e37e009 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Wed, 10 Apr 2024 02:33:17 +0200 Subject: [PATCH 2/2] Fix compiler warnings --- src/ArduinoCellular.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index d7193b9..ad709fc 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -67,9 +67,9 @@ bool ArduinoCellular::connect(String apn, String gprsUser, String gprsPass, Stri } else { return false; } - } else { - return false; } + + return false; } @@ -193,7 +193,7 @@ bool ArduinoCellular::unlockSIM(const char * pin){ if(this->debugStream != nullptr){ this->debugStream->println("Unlocking SIM..."); } - modem.simUnlock(pin); + return modem.simUnlock(pin); } bool ArduinoCellular::awaitNetworkRegistration(){ @@ -226,7 +226,7 @@ bool ArduinoCellular::enableGPS(bool assisted){ //modem.waitResponse(); - modem.enableGPS(); + return modem.enableGPS(); //delay(10000); }