Skip to content

Commit 28f425c

Browse files
committed
Fix issue with parsing SMS
1 parent 4e7f9d3 commit 28f425c

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

Diff for: src/ArduinoCellular.cpp

+43-24
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Time parseTimestamp(const String &timestampStr) {
264264
return Time(year, month, day, hour, minute, second, offset);
265265
}
266266
// Parses a single SMS entry from the data
267-
SMS parseSMSEntry(const String& entry) {
267+
SMS parseSMSEntry(const String& entry, const String& message) {
268268
SMS sms;
269269
int firstQuoteIndex = entry.indexOf('"');
270270
int secondQuoteIndex = entry.indexOf('"', firstQuoteIndex + 1);
@@ -278,38 +278,57 @@ SMS parseSMSEntry(const String& entry) {
278278

279279
// Parse the timestamp
280280
sms.timestamp = parseTimestamp(rawTimestamp);
281-
282-
// Extracting the message content
283-
int messageStartIndex = entry.indexOf('\n') + 1; // Assuming message starts after the newline
284-
if (messageStartIndex != -1) {
285-
sms.message = entry.substring(messageStartIndex, entry.indexOf('\n'));
286-
}
287-
281+
282+
sms.message = message;
288283
return sms;
289284
}
290285

286+
// Function to split a string into lines based on a delimiter character
287+
// Filters out empty lines
288+
std::vector<String> splitStringByLines(const String& input, char delimiter = '\n') {
289+
std::vector<String> lines;
290+
int startIndex = 0;
291+
while (startIndex < input.length()) {
292+
int endIndex = input.indexOf(delimiter, startIndex);
293+
if (endIndex == -1)
294+
endIndex = input.length();
295+
String line = input.substring(startIndex, endIndex);
296+
if(line.length() > 0 && line != "\r" && line != "\n" && line != "\r\n"){
297+
// Remove trailing \r if it exists
298+
if (line.endsWith("\r")) {
299+
line.remove(line.length() - 1);
300+
}
301+
lines.push_back(line);
302+
}
303+
startIndex = endIndex + 1;
304+
}
305+
return lines;
306+
}
307+
291308
// Splits the entire message string into individual SMS entries and parses them
292309
std::vector<SMS> parseSMSData(const String& data) {
293-
std::vector<SMS> smsList;
294-
int start = 0;
295-
int end = data.indexOf("\n+CMGL: ", start);
296-
297-
while (end != -1) {
298-
String entry = data.substring(start, end);
299-
smsList.push_back(parseSMSEntry(entry));
300-
start = end + 1;
301-
end = data.indexOf("\n+CMGL: ", start);
302-
}
303-
// Adding the last SMS entry, if there's any remaining part
304-
if (start < data.length()) {
305-
smsList.push_back(parseSMSEntry(data.substring(start)));
306-
}
310+
std::vector<SMS> smsList = std::vector<SMS>();
311+
std::vector<String> lines = splitStringByLines(data);
312+
313+
// Remove last line if it's "OK"
314+
if (lines.size() > 0 && lines[lines.size() - 1] == "OK") {
315+
lines.pop_back();
316+
}
317+
318+
for(int i = 0; i < lines.size(); i += 2){
319+
if (lines[i].startsWith("+CMGL:")) {
320+
String message = "";
321+
if(i + 1 < lines.size()){
322+
message = lines[i + 1];
323+
}
324+
SMS sms = parseSMSEntry(lines[i], message);
325+
smsList.push_back(sms);
326+
}
327+
}
307328

308-
smsList.erase(smsList.begin());
309329
return smsList;
310330
}
311331

312-
313332
std::vector<SMS> ArduinoCellular::getReadSMS(){
314333
String rawMessages = sendATCommand("+CMGL=\"REC READ\"");
315334
if(rawMessages.indexOf("OK") == -1){

0 commit comments

Comments
 (0)