Skip to content

Commit 8727c3c

Browse files
authored
Merge pull request #8 from arduino-libraries/fix-sms-parsing
Fix issue with parsing SMS
2 parents e7bd4ee + 473c232 commit 8727c3c

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

src/ArduinoCellular.cpp

+47-28
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ bool ArduinoCellular::connect(String apn, String gprsUser, String gprsPass, Stri
6767
} else {
6868
return false;
6969
}
70-
} else {
71-
return false;
7270
}
71+
72+
return false;
7373
}
7474

7575

@@ -193,7 +193,7 @@ bool ArduinoCellular::unlockSIM(const char * pin){
193193
if(this->debugStream != nullptr){
194194
this->debugStream->println("Unlocking SIM...");
195195
}
196-
modem.simUnlock(pin);
196+
return modem.simUnlock(pin);
197197
}
198198

199199
bool ArduinoCellular::awaitNetworkRegistration(){
@@ -226,7 +226,7 @@ bool ArduinoCellular::enableGPS(bool assisted){
226226

227227
//modem.waitResponse();
228228

229-
modem.enableGPS();
229+
return modem.enableGPS();
230230
//delay(10000);
231231
}
232232

@@ -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)