Skip to content

Commit 8027c97

Browse files
committed
Add support for newlines in SMS messages
1 parent 8727c3c commit 8027c97

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/ArduinoCellular.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,11 @@ std::vector<String> splitStringByLines(const String& input, char delimiter = '\n
293293
if (endIndex == -1)
294294
endIndex = input.length();
295295
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);
296+
// Remove trailing \r if it exists
297+
if (line.endsWith("\r")) {
298+
line.remove(line.length() - 1);
302299
}
300+
lines.push_back(line);
303301
startIndex = endIndex + 1;
304302
}
305303
return lines;
@@ -309,19 +307,30 @@ std::vector<String> splitStringByLines(const String& input, char delimiter = '\n
309307
std::vector<SMS> parseSMSData(const String& data) {
310308
std::vector<SMS> smsList = std::vector<SMS>();
311309
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();
310+
311+
// Remove first line if it's empty
312+
if (lines.size() > 0 && lines[0] == "") {
313+
lines.erase(lines.begin());
316314
}
317315

318-
for(int i = 0; i < lines.size(); i += 2){
316+
// Remove last 2 lines if second to last line is "OK"
317+
if (lines.size() >= 2 && lines.back() == "OK") {
318+
lines.erase(lines.end() - 2, lines.end());
319+
}
320+
321+
for(int i = 0; i < lines.size(); i ++){
319322
if (lines[i].startsWith("+CMGL:")) {
323+
String entry = lines[i];
320324
String message = "";
321-
if(i + 1 < lines.size()){
322-
message = lines[i + 1];
325+
// Loop through the lines until the next +CMGL line and extract the message by concatenating the lines
326+
for (int j = i + 1; j < lines.size(); j++) {
327+
if (lines[j].startsWith("+CMGL:")) {
328+
i = j - 1;
329+
break;
330+
}
331+
message += lines[j] + "\n";
323332
}
324-
SMS sms = parseSMSEntry(lines[i], message);
333+
SMS sms = parseSMSEntry(entry, message);
325334
smsList.push_back(sms);
326335
}
327336
}

0 commit comments

Comments
 (0)