@@ -67,9 +67,9 @@ bool ArduinoCellular::connect(String apn, String gprsUser, String gprsPass, Stri
67
67
} else {
68
68
return false ;
69
69
}
70
- } else {
71
- return false ;
72
70
}
71
+
72
+ return false ;
73
73
}
74
74
75
75
@@ -193,7 +193,7 @@ bool ArduinoCellular::unlockSIM(const char * pin){
193
193
if (this ->debugStream != nullptr ){
194
194
this ->debugStream ->println (" Unlocking SIM..." );
195
195
}
196
- modem.simUnlock (pin);
196
+ return modem.simUnlock (pin);
197
197
}
198
198
199
199
bool ArduinoCellular::awaitNetworkRegistration (){
@@ -226,7 +226,7 @@ bool ArduinoCellular::enableGPS(bool assisted){
226
226
227
227
// modem.waitResponse();
228
228
229
- modem.enableGPS ();
229
+ return modem.enableGPS ();
230
230
// delay(10000);
231
231
}
232
232
@@ -264,7 +264,7 @@ Time parseTimestamp(const String ×tampStr) {
264
264
return Time (year, month, day, hour, minute, second, offset);
265
265
}
266
266
// Parses a single SMS entry from the data
267
- SMS parseSMSEntry (const String& entry) {
267
+ SMS parseSMSEntry (const String& entry, const String& message ) {
268
268
SMS sms;
269
269
int firstQuoteIndex = entry.indexOf (' "' );
270
270
int secondQuoteIndex = entry.indexOf (' "' , firstQuoteIndex + 1 );
@@ -278,38 +278,57 @@ SMS parseSMSEntry(const String& entry) {
278
278
279
279
// Parse the timestamp
280
280
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;
288
283
return sms;
289
284
}
290
285
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
+
291
308
// Splits the entire message string into individual SMS entries and parses them
292
309
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
+ }
307
328
308
- smsList.erase (smsList.begin ());
309
329
return smsList;
310
330
}
311
331
312
-
313
332
std::vector<SMS> ArduinoCellular::getReadSMS (){
314
333
String rawMessages = sendATCommand (" +CMGL=\" REC READ\" " );
315
334
if (rawMessages.indexOf (" OK" ) == -1 ){
0 commit comments