Skip to content

Commit

Permalink
Fix issue with parsing SMS
Browse files Browse the repository at this point in the history
  • Loading branch information
sebromero committed Apr 10, 2024
1 parent 4e7f9d3 commit 28f425c
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions src/ArduinoCellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ Time parseTimestamp(const String &timestampStr) {
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);
Expand All @@ -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<String> splitStringByLines(const String& input, char delimiter = '\n') {
std::vector<String> 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<SMS> parseSMSData(const String& data) {
std::vector<SMS> 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<SMS> smsList = std::vector<SMS>();
std::vector<String> 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<SMS> ArduinoCellular::getReadSMS(){
String rawMessages = sendATCommand("+CMGL=\"REC READ\"");
if(rawMessages.indexOf("OK") == -1){
Expand Down

0 comments on commit 28f425c

Please sign in to comment.