Skip to content

Commit c630ac0

Browse files
committed
Increased the alloc in getAPN. getAPN now scans multiple apn's if required.
1 parent 8749577 commit c630ac0

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,20 +1099,20 @@ SARA_R5_error_t SARA_R5::setAPN(String apn, uint8_t cid, SARA_R5_pdp_type pdpTyp
10991099
return err;
11001100
}
11011101

1102+
// Return the first non-zero-length apn
11021103
SARA_R5_error_t SARA_R5::getAPN(String *apn, IPAddress *ip)
11031104
{
11041105
SARA_R5_error_t err;
11051106
char *command;
11061107
char *response;
1107-
char *searchPtr;
11081108
int ipOctets[4];
11091109

11101110
command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_DEF) + 3);
11111111
if (command == NULL)
11121112
return SARA_R5_ERROR_OUT_OF_MEMORY;
11131113
sprintf(command, "%s?", SARA_R5_MESSAGE_PDP_DEF);
11141114

1115-
response = sara_r5_calloc_char(128);
1115+
response = sara_r5_calloc_char(1024);
11161116
if (response == NULL)
11171117
{
11181118
free(command);
@@ -1124,43 +1124,60 @@ SARA_R5_error_t SARA_R5::getAPN(String *apn, IPAddress *ip)
11241124

11251125
if (err == SARA_R5_ERROR_SUCCESS)
11261126
{
1127-
// Example: +CGDCONT: 1,"IP","hologram","10.170.241.191",0,0,0,0
1128-
searchPtr = strstr(response, "+CGDCONT: ");
1127+
// Example:
1128+
// +CGDCONT: 0,"IP","","0.0.0.0",0,0,0,0,0,0,0,0,0,0
1129+
// +CGDCONT: 1,"IP","payandgo.o2.co.uk.mnc010.mcc234.gprs","10.160.182.234",0,0,0,2,0,0,0,0,0,0
1130+
1131+
char *searchPtr = response;
1132+
1133+
boolean keepGoing = true;
1134+
while (keepGoing == true)
1135+
{
1136+
int apnLen = 0;
1137+
int scanned = 0;
1138+
// Find the first/next occurrence of +CGDCONT:
1139+
searchPtr = strstr(searchPtr, "+CGDCONT: ");
11291140
if (searchPtr != NULL)
11301141
{
1131-
searchPtr += strlen("+CGDCONT: ");
1132-
// Search to the third double-quote
1133-
for (int i = 0; i < 3; i++)
1134-
{
1135-
searchPtr = strchr(++searchPtr, '\"');
1136-
}
1137-
if (searchPtr != NULL)
1138-
{
1139-
// Fill in the APN:
1140-
searchPtr = strchr(searchPtr, '\"'); // Move to first quote
1141-
while ((*(++searchPtr) != '\"') && (*searchPtr != '\0'))
1142-
{
1143-
apn->concat(*(searchPtr));
1144-
}
1145-
// Now get the IP:
1146-
if (searchPtr != NULL)
1147-
{
1148-
int scanned = sscanf(searchPtr, "\",\"%d.%d.%d.%d\"",
1149-
&ipOctets[0], &ipOctets[1], &ipOctets[2], &ipOctets[3]);
1150-
if (scanned == 4)
1151-
{
1152-
for (int octet = 0; octet < 4; octet++)
1153-
{
1154-
(*ip)[octet] = (uint8_t)ipOctets[octet];
1155-
}
1156-
}
1157-
}
1158-
}
1142+
searchPtr += strlen("+CGDCONT: "); // Point to the cid
1143+
// Search to the third double-quote
1144+
for (int i = 0; i < 3; i++)
1145+
{
1146+
searchPtr = strchr(++searchPtr, '\"');
1147+
}
1148+
if (searchPtr != NULL)
1149+
{
1150+
// Fill in the APN:
1151+
//searchPtr = strchr(searchPtr, '\"'); // Move to first quote
1152+
while ((*(++searchPtr) != '\"') && (*searchPtr != '\0'))
1153+
{
1154+
apn->concat(*(searchPtr));
1155+
apnLen++;
1156+
}
1157+
// Now get the IP:
1158+
if (searchPtr != NULL)
1159+
{
1160+
scanned = sscanf(searchPtr, "\",\"%d.%d.%d.%d\"",
1161+
&ipOctets[0], &ipOctets[1], &ipOctets[2], &ipOctets[3]);
1162+
if (scanned == 4)
1163+
{
1164+
for (int octet = 0; octet < 4; octet++)
1165+
{
1166+
(*ip)[octet] = (uint8_t)ipOctets[octet];
1167+
}
1168+
}
1169+
}
1170+
}
11591171
}
1160-
else
1172+
if ((apnLen > 0) || (scanned != 4) || (searchPtr == NULL) || (*searchPtr == '\0')) // Stop searching
11611173
{
1162-
err = SARA_R5_ERROR_UNEXPECTED_RESPONSE;
1174+
keepGoing = false;
11631175
}
1176+
}
1177+
}
1178+
else
1179+
{
1180+
err = SARA_R5_ERROR_UNEXPECTED_RESPONSE;
11641181
}
11651182

11661183
free(command);
@@ -1438,7 +1455,7 @@ SARA_R5_error_t SARA_R5::deregisterOperator(void)
14381455
sprintf(command, "%s=2", SARA_R5_OPERATOR_SELECTION);
14391456

14401457
err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK, NULL,
1441-
SARA_R5_STANDARD_RESPONSE_TIMEOUT);
1458+
SARA_R5_3_MIN_TIMEOUT);
14421459

14431460
free(command);
14441461
return err;

0 commit comments

Comments
 (0)