From 893fc5f7e5198eebb17f67f2ab768ffda735d834 Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Fri, 6 May 2022 09:19:38 +0200 Subject: [PATCH 1/9] allow begin to be outside the libray - this allows custom pins to be used. - do just change baudrate instead of repetitive begins --- src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp | 8 +++++--- src/SparkFun_u-blox_SARA-R5_Arduino_Library.h | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp index 077e7a3..dac0b73 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp @@ -122,7 +122,7 @@ bool SARA_R5::begin(SoftwareSerial &softSerial, unsigned long baud) } #endif -bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud) +bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud, bool doBegin) { if (NULL == _saraRXBuffer) { @@ -163,7 +163,9 @@ bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud) SARA_R5_error_t err; _hardSerial = &hardSerial; - + if (doBegin) { + _hardSerial->begin(baud); + } err = init(baud); if (err == SARA_R5_ERROR_SUCCESS) { @@ -5958,7 +5960,7 @@ void SARA_R5::beginSerial(unsigned long baud) delay(100); if (_hardSerial != NULL) { - _hardSerial->begin(baud); + _hardSerial->updateBaudRate(baud); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED else if (_softSerial != NULL) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h index 8ac323d..482a025 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h @@ -207,7 +207,7 @@ const char ASCII_ESC = 0x1B; #define SARA_R5_NUM_SOCKETS 6 -#define NUM_SUPPORTED_BAUD 6 +#define NUM_SUPPORTED_BAUD 9 const unsigned long SARA_R5_SUPPORTED_BAUD[NUM_SUPPORTED_BAUD] = { 115200, @@ -215,7 +215,10 @@ const unsigned long SARA_R5_SUPPORTED_BAUD[NUM_SUPPORTED_BAUD] = 19200, 38400, 57600, - 230400}; + 230400, + 460800, + 921600, + 3000000}; #define SARA_R5_DEFAULT_BAUD_RATE 115200 // Flow control definitions for AT&K @@ -582,7 +585,7 @@ class SARA_R5 : public Print #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED bool begin(SoftwareSerial &softSerial, unsigned long baud = 9600); #endif - bool begin(HardwareSerial &hardSerial, unsigned long baud = 9600); + bool begin(HardwareSerial &hardSerial, unsigned long baud = 9600, bool doBegin = true); // Debug prints void enableDebugging(Print &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used. From cdef232cec261afba003ee865541176206b3b9db Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Sun, 8 May 2022 20:46:54 +0200 Subject: [PATCH 2/9] better parsing of response --- ...parkFun_u-blox_SARA-R5_Arduino_Library.cpp | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp index dac0b73..2c8fa89 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp @@ -4145,21 +4145,37 @@ SARA_R5_error_t SARA_R5::readMQTT(int* pQos, String* pTopic, uint8_t *readDest, free(response); return SARA_R5_ERROR_UNEXPECTED_RESPONSE; } + + err = SARA_R5_ERROR_SUCCESS; searchPtr = strstr(searchPtr, "\""); - if (pTopic) { - searchPtr[topic_length+1] = '\0'; // zero terminate - *pTopic = searchPtr+1; - searchPtr[topic_length+1] = '\"'; // restore - } - searchPtr = strstr(searchPtr + topic_length + 2, "\""); - if (readDest) { - *bytesRead = (data_length > readLength) ? readLength : data_length; - memcpy(readDest, searchPtr+1, *bytesRead); + if (searchPtr!= NULL) { + if (pTopic) { + searchPtr[topic_length + 1] = '\0'; // zero terminate + *pTopic = searchPtr + 1; + searchPtr[topic_length + 1] = '\"'; // restore + } + searchPtr = strstr(searchPtr + topic_length + 2, "\""); + if (readDest && (searchPtr != NULL) && (response + responseLength >= searchPtr + data_length + 1) && (searchPtr[data_length + 1] == '"')) { + if (data_length > readLength) { + data_length = readLength; + if (_printDebug == true) { + _debugPort->print(F("readMQTT: error: trucate message")); + } + err = SARA_R5_ERROR_OUT_OF_MEMORY; + } + memcpy(readDest, searchPtr+1, data_length); + *bytesRead = data_length; + } else { + if (_printDebug == true) { + _debugPort->print(F("readMQTT: error: message end ")); + } + err = SARA_R5_ERROR_UNEXPECTED_RESPONSE; + } } free(command); free(response); - return (data_length > readLength) ? SARA_R5_ERROR_OUT_OF_MEMORY : SARA_R5_ERROR_SUCCESS; + return err; } SARA_R5_error_t SARA_R5::getMQTTprotocolError(int *error_code, int *error_code2) @@ -4834,7 +4850,7 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, String *contents) // A large file will completely fill the backlog buffer - but it will be pruned afterwards // Note to self: if the file contents contain "OK\r\n" sendCommandWithResponse will return true too early... // To try and avoid this, look for \"\r\nOK\r\n - const char fileReadTerm[] = "\"\r\nOK\r\n"; + const char fileReadTerm[] = "\r\nOK\r\n"; //"\"\r\n\r\nOK\r\n"; err = sendCommandWithResponse(command, fileReadTerm, response, (5 * SARA_R5_STANDARD_RESPONSE_TIMEOUT), (fileSize + minimumResponseAllocation)); From febfd295051dfed1236aa5f2d70fe20ce005897f Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Tue, 10 May 2022 18:41:59 +0200 Subject: [PATCH 3/9] less baud rates --- src/SparkFun_u-blox_SARA-R5_Arduino_Library.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h index 482a025..84eeb44 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h @@ -207,7 +207,7 @@ const char ASCII_ESC = 0x1B; #define SARA_R5_NUM_SOCKETS 6 -#define NUM_SUPPORTED_BAUD 9 +#define NUM_SUPPORTED_BAUD 6 const unsigned long SARA_R5_SUPPORTED_BAUD[NUM_SUPPORTED_BAUD] = { 115200, @@ -215,10 +215,7 @@ const unsigned long SARA_R5_SUPPORTED_BAUD[NUM_SUPPORTED_BAUD] = 19200, 38400, 57600, - 230400, - 460800, - 921600, - 3000000}; + 230400}; #define SARA_R5_DEFAULT_BAUD_RATE 115200 // Flow control definitions for AT&K From 2cbe2fbed15b20ec0e26551d3de98e8ecc799194 Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Tue, 10 May 2022 18:44:44 +0200 Subject: [PATCH 4/9] clarify comments --- src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp index 2c8fa89..b69d827 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp @@ -4850,7 +4850,7 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, String *contents) // A large file will completely fill the backlog buffer - but it will be pruned afterwards // Note to self: if the file contents contain "OK\r\n" sendCommandWithResponse will return true too early... // To try and avoid this, look for \"\r\nOK\r\n - const char fileReadTerm[] = "\r\nOK\r\n"; //"\"\r\n\r\nOK\r\n"; + const char fileReadTerm[] = "\r\nOK\r\n"; //LARA-R6 returns "\"\r\n\r\nOK\r\n" while SARA-R5 return "\"\r\nOK\r\n"; err = sendCommandWithResponse(command, fileReadTerm, response, (5 * SARA_R5_STANDARD_RESPONSE_TIMEOUT), (fileSize + minimumResponseAllocation)); From c1ed11dff77c43334de50b572b4dd04a2786994b Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Tue, 10 May 2022 20:33:43 +0200 Subject: [PATCH 5/9] Update SparkFun_u-blox_SARA-R5_Arduino_Library.h --- src/SparkFun_u-blox_SARA-R5_Arduino_Library.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h index 84eeb44..8ac323d 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h @@ -582,7 +582,7 @@ class SARA_R5 : public Print #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED bool begin(SoftwareSerial &softSerial, unsigned long baud = 9600); #endif - bool begin(HardwareSerial &hardSerial, unsigned long baud = 9600, bool doBegin = true); + bool begin(HardwareSerial &hardSerial, unsigned long baud = 9600); // Debug prints void enableDebugging(Print &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used. From 2a1eeb209be4fe2ddacc4b41c92caa5cd468ee3b Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Tue, 10 May 2022 20:35:19 +0200 Subject: [PATCH 6/9] Update SparkFun_u-blox_SARA-R5_Arduino_Library.cpp --- src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp index b69d827..9551bc7 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp @@ -122,7 +122,7 @@ bool SARA_R5::begin(SoftwareSerial &softSerial, unsigned long baud) } #endif -bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud, bool doBegin) +bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud) { if (NULL == _saraRXBuffer) { @@ -163,9 +163,9 @@ bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud, bool doBegin SARA_R5_error_t err; _hardSerial = &hardSerial; - if (doBegin) { - _hardSerial->begin(baud); - } + + _hardSerial->begin(baud) + err = init(baud); if (err == SARA_R5_ERROR_SUCCESS) { @@ -5976,7 +5976,7 @@ void SARA_R5::beginSerial(unsigned long baud) delay(100); if (_hardSerial != NULL) { - _hardSerial->updateBaudRate(baud); + _hardSerial->begin(baud); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED else if (_softSerial != NULL) From c0ffe285f30125b685031ee31774007daa067c3e Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Tue, 10 May 2022 20:37:27 +0200 Subject: [PATCH 7/9] Update SparkFun_u-blox_SARA-R5_Arduino_Library.cpp --- src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp index 9551bc7..ac70253 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp @@ -164,8 +164,6 @@ bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud) _hardSerial = &hardSerial; - _hardSerial->begin(baud) - err = init(baud); if (err == SARA_R5_ERROR_SUCCESS) { From 65dd415aff4225311cb3d283454d7bc187a4c6a5 Mon Sep 17 00:00:00 2001 From: Michael Ammann Date: Tue, 10 May 2022 21:53:15 +0200 Subject: [PATCH 8/9] allow customisation of beginSerial - make beginSerial virtual so that we can override and assign custom pins and configuration - end the serial port before new begin to avoid crash on ESP32 when data is received while begin is called --- src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp | 1 + src/SparkFun_u-blox_SARA-R5_Arduino_Library.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp index ac70253..f33a83c 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp @@ -5974,6 +5974,7 @@ void SARA_R5::beginSerial(unsigned long baud) delay(100); if (_hardSerial != NULL) { + _hardSerial->end(); _hardSerial->begin(baud); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h index 8ac323d..1a6dd7b 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h @@ -1026,7 +1026,7 @@ class SARA_R5 : public Print int readAvailable(char *inString); char readChar(void); int hwAvailable(void); - void beginSerial(unsigned long baud); + virtual void beginSerial(unsigned long baud); void setTimeout(unsigned long timeout); bool find(char *target); From 529b4436adcf07a368ce4e76efa56dff39b6c7db Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 18 May 2022 10:54:43 +0100 Subject: [PATCH 9/9] v1.1.3 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 49ad7bb..016a1ce 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun u-blox SARA-R5 Arduino Library -version=1.1.2 +version=1.1.3 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the u-blox SARA-R5 LTE-M / NB-IoT modules with secure cloud