Skip to content

Commit 7c76317

Browse files
authored
Merge pull request #21 from mazgch/release_candidate
make MQTT read more robust, more tolerant parsing in getFileContent
2 parents 746d555 + c0ffe28 commit 7c76317

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,21 +4143,37 @@ SARA_R5_error_t SARA_R5::readMQTT(int* pQos, String* pTopic, uint8_t *readDest,
41434143
free(response);
41444144
return SARA_R5_ERROR_UNEXPECTED_RESPONSE;
41454145
}
4146+
4147+
err = SARA_R5_ERROR_SUCCESS;
41464148
searchPtr = strstr(searchPtr, "\"");
4147-
if (pTopic) {
4148-
searchPtr[topic_length+1] = '\0'; // zero terminate
4149-
*pTopic = searchPtr+1;
4150-
searchPtr[topic_length+1] = '\"'; // restore
4151-
}
4152-
searchPtr = strstr(searchPtr + topic_length + 2, "\"");
4153-
if (readDest) {
4154-
*bytesRead = (data_length > readLength) ? readLength : data_length;
4155-
memcpy(readDest, searchPtr+1, *bytesRead);
4149+
if (searchPtr!= NULL) {
4150+
if (pTopic) {
4151+
searchPtr[topic_length + 1] = '\0'; // zero terminate
4152+
*pTopic = searchPtr + 1;
4153+
searchPtr[topic_length + 1] = '\"'; // restore
4154+
}
4155+
searchPtr = strstr(searchPtr + topic_length + 2, "\"");
4156+
if (readDest && (searchPtr != NULL) && (response + responseLength >= searchPtr + data_length + 1) && (searchPtr[data_length + 1] == '"')) {
4157+
if (data_length > readLength) {
4158+
data_length = readLength;
4159+
if (_printDebug == true) {
4160+
_debugPort->print(F("readMQTT: error: trucate message"));
4161+
}
4162+
err = SARA_R5_ERROR_OUT_OF_MEMORY;
4163+
}
4164+
memcpy(readDest, searchPtr+1, data_length);
4165+
*bytesRead = data_length;
4166+
} else {
4167+
if (_printDebug == true) {
4168+
_debugPort->print(F("readMQTT: error: message end "));
4169+
}
4170+
err = SARA_R5_ERROR_UNEXPECTED_RESPONSE;
4171+
}
41564172
}
41574173
free(command);
41584174
free(response);
41594175

4160-
return (data_length > readLength) ? SARA_R5_ERROR_OUT_OF_MEMORY : SARA_R5_ERROR_SUCCESS;
4176+
return err;
41614177
}
41624178

41634179
SARA_R5_error_t SARA_R5::getMQTTprotocolError(int *error_code, int *error_code2)
@@ -4832,7 +4848,7 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, String *contents)
48324848
// A large file will completely fill the backlog buffer - but it will be pruned afterwards
48334849
// Note to self: if the file contents contain "OK\r\n" sendCommandWithResponse will return true too early...
48344850
// To try and avoid this, look for \"\r\nOK\r\n
4835-
const char fileReadTerm[] = "\"\r\nOK\r\n";
4851+
const char fileReadTerm[] = "\r\nOK\r\n"; //LARA-R6 returns "\"\r\n\r\nOK\r\n" while SARA-R5 return "\"\r\nOK\r\n";
48364852
err = sendCommandWithResponse(command, fileReadTerm,
48374853
response, (5 * SARA_R5_STANDARD_RESPONSE_TIMEOUT),
48384854
(fileSize + minimumResponseAllocation));

0 commit comments

Comments
 (0)