Skip to content

Commit b357cae

Browse files
committed
Update pruneBacklog and getAPN
1 parent 1ce11ba commit b357cae

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

+20-18
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ bool SARA_R5::bufferedPoll(void)
219219
}
220220
memcpy(_saraRXBuffer + avail, _saraResponseBacklog, _saraResponseBacklogLength);
221221
avail += _saraResponseBacklogLength;
222-
memset(_saraResponseBacklog, 0, _RXBuffSize); // Clear the backlog making sure it is nullptr-terminated
222+
memset(_saraResponseBacklog, 0, _RXBuffSize); // Clear the backlog making sure it is NULL-terminated
223223
_saraResponseBacklogLength = 0;
224224
}
225225

@@ -235,13 +235,13 @@ bool SARA_R5::bufferedPoll(void)
235235

236236
while (((millis() - timeIn) < _rxWindowMillis) && (avail < _RXBuffSize))
237237
{
238-
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is nullptr
238+
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL
239239
{
240240
c = readChar();
241241
// bufferedPoll is only interested in the URCs.
242242
// The URCs are all readable.
243-
// strtok does not like nullptr characters.
244-
// So we need to make sure no nullptr characters are added to _saraRXBuffer
243+
// strtok does not like NULL characters.
244+
// So we need to make sure no NULL characters are added to _saraRXBuffer
245245
if (c == '\0')
246246
c = '0'; // Convert any NULLs to ASCII Zeros
247247
_saraRXBuffer[avail++] = c;
@@ -254,9 +254,9 @@ bool SARA_R5::bufferedPoll(void)
254254
// _saraRXBuffer now contains the backlog (if any) and the new serial data (if any)
255255

256256
// A health warning about strtok:
257-
// strtok will convert any delimiters it finds ("\r\n" in our case) into nullptr characters.
257+
// strtok will convert any delimiters it finds ("\r\n" in our case) into NULL characters.
258258
// Also, be very careful that you do not use strtok within an strtok while loop.
259-
// The next call of strtok(nullptr, ...) in the outer loop will use the pointer saved from the inner loop!
259+
// The next call of strtok(NULL, ...) in the outer loop will use the pointer saved from the inner loop!
260260
// In our case, strtok is also used in pruneBacklog, which is called by waitForRespone or sendCommandWithResponse,
261261
// which is called by the parse functions called by processURCEvent...
262262
// The solution is to use strtok_r - the reentrant version of strtok
@@ -678,7 +678,7 @@ bool SARA_R5::processURCEvent(const char *event)
678678
}
679679
}
680680
}
681-
{ // URC: +A
681+
{ // URC: +CREG
682682
int status = 0;
683683
unsigned int lac = 0, ci = 0, Act = 0;
684684
char *searchPtr = strstr(event, "+CREG:");
@@ -724,6 +724,7 @@ bool SARA_R5::processURCEvent(const char *event)
724724
}
725725
}
726726
}
727+
// NOTE: When adding new URC messages, remember to update pruneBacklog too!
727728

728729
return false;
729730
}
@@ -744,11 +745,11 @@ bool SARA_R5::poll(void)
744745

745746
memset(_saraRXBuffer, 0, _RXBuffSize); // Clear _saraRXBuffer
746747

747-
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is nullptr
748+
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL
748749
{
749750
while (c != '\n') // Copy characters into _saraRXBuffer. Stop at the first new line
750751
{
751-
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is nullptr
752+
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL
752753
{
753754
c = readChar();
754755
_saraRXBuffer[avail++] = c;
@@ -1781,6 +1782,7 @@ SARA_R5_error_t SARA_R5::getAPN(int cid, String *apn, IPAddress *ip, SARA_R5_pdp
17811782
int ipOct[4];
17821783

17831784
searchPtr += strlen("+CGDCONT:"); // Point to the cid
1785+
while (*searchPtr == ' ') searchPtr++; // skip spaces
17841786
scanned = sscanf(searchPtr, "%d,\"%[^\"]\",\"%[^\"]\",\"%d.%d.%d.%d",
17851787
&rcid, strPdpType, strApn,
17861788
&ipOct[0], &ipOct[1], &ipOct[2], &ipOct[3]);
@@ -5814,7 +5816,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
58145816
//that came in while waiting for response. To be processed later within bufferedPoll().
58155817
//Note: the expectedResponse or expectedError will also be added to the backlog
58165818
//The backlog is only used by bufferedPoll to process the URCs - which are all readable.
5817-
//bufferedPoll uses strtok - which does not like nullptr characters.
5819+
//bufferedPoll uses strtok - which does not like NULL characters.
58185820
//So let's make sure no NULLs end up in the backlog!
58195821
if (_saraResponseBacklogLength < _RXBuffSize) // Don't overflow the buffer
58205822
{
@@ -5873,20 +5875,20 @@ void SARA_R5::sendCommand(const char *command, bool at)
58735875
// At 115200 baud, hwAvailable takes ~120 * 10 / 115200 = 10.4 millis before it indicates that data is being received.
58745876

58755877
unsigned long timeIn = millis();
5876-
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is nullptr
5878+
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL
58775879
{
58785880
while (((millis() - timeIn) < _rxWindowMillis) && (_saraResponseBacklogLength < _RXBuffSize)) //May need to escape on newline?
58795881
{
5880-
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is nullptr
5882+
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL
58815883
{
58825884
//_saraResponseBacklog is a global array that holds the backlog of any events
58835885
//that came in while waiting for response. To be processed later within bufferedPoll().
58845886
//Note: the expectedResponse or expectedError will also be added to the backlog
58855887
//The backlog is only used by bufferedPoll to process the URCs - which are all readable.
5886-
//bufferedPoll uses strtok - which does not like nullptr characters.
5888+
//bufferedPoll uses strtok - which does not like NULL characters.
58875889
//So let's make sure no NULLs end up in the backlog!
58885890
char c = readChar();
5889-
if (c == '\0') // Make sure no nullptr characters end up in the backlog! Change them to ASCII Zeros
5891+
if (c == '\0') // Make sure no NULL characters end up in the backlog! Change them to ASCII Zeros
58905892
c = '0';
58915893
_saraResponseBacklog[_saraResponseBacklogLength++] = c;
58925894
timeIn = millis();
@@ -6281,11 +6283,11 @@ void SARA_R5::pruneBacklog()
62816283
|| (strstr(event, "+UULOC:") != nullptr)
62826284
|| (strstr(event, "+UUSIMSTAT:") != nullptr)
62836285
|| (strstr(event, "+UUPSDA:") != nullptr)
6284-
|| (strstr(event, "+UUPING:") != nullptr)
6286+
|| (strstr(event, "+UUHTTPCR:") != nullptr)
62856287
|| (strstr(event, "+UUMQTTC:") != nullptr)
6286-
|| (strstr(event, "+UUCREG:") != nullptr)
6287-
|| (strstr(event, "+UUCEREG:") != nullptr)
6288-
|| (strstr(event, "+UUHTTPCR:") != nullptr))
6288+
|| (strstr(event, "+UUPING:") != nullptr)
6289+
|| (strstr(event, "+CREG:") != nullptr)
6290+
|| (strstr(event, "+CEREG:") != nullptr))
62896291
{
62906292
strcat(_pruneBuffer, event); // The URCs are all readable text so using strcat is OK
62916293
strcat(_pruneBuffer, "\r\n"); // strtok blows away delimiter, but we want that for later.

0 commit comments

Comments
 (0)