Skip to content

Commit a2134bc

Browse files
committed
Store the NTRIP credentials etc. in allocated memory - to avoid stack problems on Mbed platforms
1 parent e6fc72b commit a2134bc

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

examples/SARA-R5_Example15_GNSS_NTRIP_Caster_With_Callbacks/SARA-R5_NTRIP_Client.ino

+36-17
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
1414
Serial.print(F("beginClient: Socket is already open"));
1515
if (*connectionIsOpen)
1616
{
17-
Serial.print(F(" and the connection is open!"));
17+
Serial.println(F(" and the connection is open!"));
1818
}
1919
else
2020
{
21-
Serial.print(F("!"));
21+
Serial.println(F("!"));
2222
}
2323
return (false);
2424
}
@@ -56,23 +56,28 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
5656

5757
// Set up the server request (GET)
5858
const int SERVER_BUFFER_SIZE = 512;
59-
char serverRequest[SERVER_BUFFER_SIZE];
59+
char *serverRequest = new char[SERVER_BUFFER_SIZE];
60+
memset(serverRequest, 0, SERVER_BUFFER_SIZE);
6061
snprintf(serverRequest,
6162
SERVER_BUFFER_SIZE,
6263
"GET /%s HTTP/1.0\r\nUser-Agent: NTRIP SparkFun u-blox Client v1.0\r\n",
6364
mountPoint);
6465

6566
// Set up the credentials
66-
char credentials[512];
67+
const int CREDENTIALS_BUFFER_SIZE = 512;
68+
char *credentials = new char[CREDENTIALS_BUFFER_SIZE];
69+
memset(credentials, 0, CREDENTIALS_BUFFER_SIZE);
6770
if (strlen(casterUser) == 0)
6871
{
69-
strncpy(credentials, "Accept: */*\r\nConnection: close\r\n", sizeof(credentials));
72+
strncpy(credentials, "Accept: */*\r\nConnection: close\r\n", CREDENTIALS_BUFFER_SIZE);
7073
}
7174
else
7275
{
7376
//Pass base64 encoded user:pw
74-
char userCredentials[sizeof(casterUser) + sizeof(casterUserPW) + 1]; //The ':' takes up a spot
75-
snprintf(userCredentials, sizeof(userCredentials), "%s:%s", casterUser, casterUserPW);
77+
int userCredentialsSize = sizeof(casterUser) + sizeof(casterUserPW) + 1; //The ':' takes up a spot
78+
char *userCredentials = new char[userCredentialsSize];
79+
memset(userCredentials, 0, userCredentialsSize);
80+
snprintf(userCredentials, userCredentialsSize, "%s:%s", casterUser, casterUserPW);
7681

7782
Serial.print(F("beginClient: Sending credentials: "));
7883
Serial.println(userCredentials);
@@ -81,20 +86,27 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
8186
//Encode with ESP32 built-in library
8287
base64 b;
8388
String strEncodedCredentials = b.encode(userCredentials);
84-
char encodedCredentials[strEncodedCredentials.length() + 1];
85-
strEncodedCredentials.toCharArray(encodedCredentials, sizeof(encodedCredentials)); //Convert String to char array
89+
int encodedCredentialsSize = strEncodedCredentials.length() + 1;
90+
char *encodedCredentials = new char[encodedCredentialsSize];
91+
memset(encodedCredentials, 0, encodedCredentialsSize);
92+
strEncodedCredentials.toCharArray(encodedCredentials, encodedCredentialsSize); //Convert String to char array
8693
#elif defined(ARDUINO_ARCH_APOLLO3) || defined(ARDUINO_ARDUINO_NANO33BLE)
87-
char encodedCredentials[sizeof(userCredentials) * 8];
94+
int encodedCredentialsSize = userCredentialsSize * 8;
95+
char *encodedCredentials = new char[encodedCredentialsSize];
96+
memset(encodedCredentials, 0, encodedCredentialsSize);
8897
size_t olen;
89-
mbedtls_base64_encode((unsigned char *)encodedCredentials, sizeof(userCredentials) * 8, &olen, (const unsigned char *)userCredentials, strlen(userCredentials));
98+
mbedtls_base64_encode((unsigned char *)encodedCredentials, encodedCredentialsSize, &olen, (const unsigned char *)userCredentials, strlen(userCredentials));
9099
#else
91100
//Encode with nfriendly library
92-
int encodedLen = base64_enc_len(strlen(userCredentials));
93-
char encodedCredentials[encodedLen]; //Create array large enough to house encoded data
101+
int encodedLen = base64_enc_len(userCredentialsSize);
102+
char *encodedCredentials = new char[encodedLen]; //Create array large enough to house encoded data
103+
memset(encodedCredentials, 0, encodedLen);
94104
base64_encode(encodedCredentials, userCredentials, strlen(userCredentials)); //Note: Input array is consumed
95105
#endif
96106

97-
snprintf(credentials, sizeof(credentials), "Authorization: Basic %s\r\n", encodedCredentials);
107+
snprintf(credentials, CREDENTIALS_BUFFER_SIZE, "Authorization: Basic %s\r\n", encodedCredentials);
108+
delete[] userCredentials;
109+
delete[] encodedCredentials;
98110
}
99111

100112
// Add the encoded credentials to the server request
@@ -104,7 +116,7 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
104116
Serial.print(F("beginClient: serverRequest size: "));
105117
Serial.print(strlen(serverRequest));
106118
Serial.print(F(" of "));
107-
Serial.print(sizeof(serverRequest));
119+
Serial.print(SERVER_BUFFER_SIZE);
108120
Serial.println(F(" bytes available"));
109121

110122
// Send the server request
@@ -123,6 +135,8 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
123135
{
124136
Serial.println(F("beginClient: Caster timed out!"));
125137
closeConnection(theSocket, connectionIsOpen);
138+
delete[] serverRequest;
139+
delete[] credentials;
126140
return (false);
127141
}
128142
delay(100);
@@ -134,12 +148,12 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
134148

135149
//Check reply
136150
int connectionResult = 0;
137-
char response[512 * 4];
151+
char *response = new char[512 * 4];
138152
memset(response, 0, 512 * 4);
139153
size_t responseSpot = 0;
140154
while ((availableLength > 0) && (connectionResult == 0)) // Read bytes from the caster and store them
141155
{
142-
if ((responseSpot + availableLength) >= (sizeof(response) - 1)) // Exit the loop if we get too much data
156+
if ((responseSpot + availableLength) >= ((512 * 4) - 1)) // Exit the loop if we get too much data
143157
break;
144158

145159
mySARA.socketRead(*theSocket, availableLength, &response[responseSpot]);
@@ -172,6 +186,11 @@ bool beginClient(int *theSocket, bool *connectionIsOpen)
172186

173187
//Serial.print(F("beginClient: Caster responded with: ")); Serial.println(response); // Uncomment this line to see the full response
174188

189+
// Free the memory allocated for serverRequest, credentials and response
190+
delete[] serverRequest;
191+
delete[] credentials;
192+
delete[] response;
193+
175194
if (connectionResult != 200)
176195
{
177196
Serial.print(F("beginClient: Failed to connect to "));

0 commit comments

Comments
 (0)