diff --git a/library.properties b/library.properties index 117b2f0..3a39ee5 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun u-blox SARA-R5 Arduino Library -version=1.1.5 +version=1.1.6 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the u-blox SARA-R5 LTE-M / NB-IoT modules with secure cloud

diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp index 43ae8d5..78ee353 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp @@ -19,28 +19,28 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitTries) { #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - _softSerial = NULL; + _softSerial = nullptr; #endif - _hardSerial = NULL; + _hardSerial = nullptr; _baud = 0; _resetPin = resetPin; _powerPin = powerPin; _invertPowerPin = false; _maxInitTries = maxInitTries; - _socketListenCallback = NULL; - _socketReadCallback = NULL; - _socketReadCallbackPlus = NULL; - _socketCloseCallback = NULL; - _gpsRequestCallback = NULL; - _simStateReportCallback = NULL; - _psdActionRequestCallback = NULL; - _pingRequestCallback = NULL; - _httpCommandRequestCallback = NULL; - _mqttCommandRequestCallback = NULL; - _registrationCallback = NULL; - _epsRegistrationCallback = NULL; - _debugAtPort = NULL; - _debugPort = NULL; + _socketListenCallback = nullptr; + _socketReadCallback = nullptr; + _socketReadCallbackPlus = nullptr; + _socketCloseCallback = nullptr; + _gpsRequestCallback = nullptr; + _simStateReportCallback = nullptr; + _psdActionRequestCallback = nullptr; + _pingRequestCallback = nullptr; + _httpCommandRequestCallback = nullptr; + _mqttCommandRequestCallback = nullptr; + _registrationCallback = nullptr; + _epsRegistrationCallback = nullptr; + _debugAtPort = nullptr; + _debugPort = nullptr; _printDebug = false; _lastRemoteIP = {0, 0, 0, 0}; _lastLocalIP = {0, 0, 0, 0}; @@ -50,33 +50,33 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitTries) _bufferedPollReentrant = false; _pollReentrant = false; _saraResponseBacklogLength = 0; - _saraRXBuffer = NULL; - _pruneBuffer = NULL; - _saraResponseBacklog = NULL; + _saraRXBuffer = nullptr; + _pruneBuffer = nullptr; + _saraResponseBacklog = nullptr; } SARA_R5::~SARA_R5(void) { - if (NULL != _saraRXBuffer) { + if (nullptr != _saraRXBuffer) { delete[] _saraRXBuffer; - _saraRXBuffer = NULL; + _saraRXBuffer = nullptr; } - if (NULL != _pruneBuffer) { + if (nullptr != _pruneBuffer) { delete[] _pruneBuffer; - _pruneBuffer = NULL; + _pruneBuffer = nullptr; } - if (NULL != _saraResponseBacklog) { + if (nullptr != _saraResponseBacklog) { delete[] _saraResponseBacklog; - _saraResponseBacklog = NULL; + _saraResponseBacklog = nullptr; } } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED bool SARA_R5::begin(SoftwareSerial &softSerial, unsigned long baud) { - if (NULL == _saraRXBuffer) + if (nullptr == _saraRXBuffer) { _saraRXBuffer = new char[_RXBuffSize]; - if (NULL == _saraRXBuffer) + if (nullptr == _saraRXBuffer) { if (_printDebug == true) _debugPort->println(F("begin: not enough memory for _saraRXBuffer!")); @@ -85,10 +85,10 @@ bool SARA_R5::begin(SoftwareSerial &softSerial, unsigned long baud) } memset(_saraRXBuffer, 0, _RXBuffSize); - if (NULL == _pruneBuffer) + if (nullptr == _pruneBuffer) { _pruneBuffer = new char[_RXBuffSize]; - if (NULL == _pruneBuffer) + if (nullptr == _pruneBuffer) { if (_printDebug == true) _debugPort->println(F("begin: not enough memory for _pruneBuffer!")); @@ -97,10 +97,10 @@ bool SARA_R5::begin(SoftwareSerial &softSerial, unsigned long baud) } memset(_pruneBuffer, 0, _RXBuffSize); - if (NULL == _saraResponseBacklog) + if (nullptr == _saraResponseBacklog) { _saraResponseBacklog = new char[_RXBuffSize]; - if (NULL == _saraResponseBacklog) + if (nullptr == _saraResponseBacklog) { if (_printDebug == true) _debugPort->println(F("begin: not enough memory for _saraResponseBacklog!")); @@ -124,10 +124,10 @@ bool SARA_R5::begin(SoftwareSerial &softSerial, unsigned long baud) bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud) { - if (NULL == _saraRXBuffer) + if (nullptr == _saraRXBuffer) { _saraRXBuffer = new char[_RXBuffSize]; - if (NULL == _saraRXBuffer) + if (nullptr == _saraRXBuffer) { if (_printDebug == true) _debugPort->println(F("begin: not enough memory for _saraRXBuffer!")); @@ -136,10 +136,10 @@ bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud) } memset(_saraRXBuffer, 0, _RXBuffSize); - if (NULL == _pruneBuffer) + if (nullptr == _pruneBuffer) { _pruneBuffer = new char[_RXBuffSize]; - if (NULL == _pruneBuffer) + if (nullptr == _pruneBuffer) { if (_printDebug == true) _debugPort->println(F("begin: not enough memory for _pruneBuffer!")); @@ -148,10 +148,10 @@ bool SARA_R5::begin(HardwareSerial &hardSerial, unsigned long baud) } memset(_pruneBuffer, 0, _RXBuffSize); - if (NULL == _saraResponseBacklog) + if (nullptr == _saraResponseBacklog) { _saraResponseBacklog = new char[_RXBuffSize]; - if (NULL == _saraResponseBacklog) + if (nullptr == _saraResponseBacklog) { if (_printDebug == true) _debugPort->println(F("begin: not enough memory for _saraResponseBacklog!")); @@ -264,11 +264,11 @@ bool SARA_R5::bufferedPoll(void) char *preservedEvent; event = strtok_r(_saraRXBuffer, "\r\n", &preservedEvent); // Look for an 'event' (_saraRXBuffer contains something ending in \r\n) - if (event != NULL) + if (event != nullptr) if (_printDebug == true) _debugPort->println(F("bufferedPoll: event(s) found! ===>")); - while (event != NULL) // Keep going until all events have been processed + while (event != nullptr) // Keep going until all events have been processed { if (_printDebug == true) { @@ -279,7 +279,7 @@ bool SARA_R5::bufferedPoll(void) //Process the event bool latestHandled = processURCEvent((const char *)event); if (latestHandled) { - if ((true == _printAtDebug) && (NULL != event)) { + if ((true == _printAtDebug) && (nullptr != event)) { _debugAtPort->print(event); } handled = true; // handled will be true if latestHandled has ever been true @@ -297,12 +297,12 @@ bool SARA_R5::bufferedPoll(void) } //Walk through any remaining events - event = strtok_r(NULL, "\r\n", &preservedEvent); + event = strtok_r(nullptr, "\r\n", &preservedEvent); if (_printDebug == true) _debugPort->println(F("bufferedPoll: end of event")); //Just to denote end of processing event. - if (event == NULL) + if (event == nullptr) if (_printDebug == true) _debugPort->println(F("bufferedPoll: <=== end of event(s)!")); } @@ -320,38 +320,50 @@ bool SARA_R5::processURCEvent(const char *event) { { // URC: +UUSORD (Read Socket Data) int socket, length; - int ret = sscanf(event, "+UUSORD: %d,%d", &socket, &length); - if (ret == 2) + char *searchPtr = strstr(event, "+UUSORD:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: read socket data")); - // From the SARA_R5 AT Commands Manual: - // "For the UDP socket type the URC +UUSORD: , notifies that a UDP packet has been received, - // either when buffer is empty or after a UDP packet has been read and one or more packets are stored in the - // buffer." - // So we need to check if this is a TCP socket or a UDP socket: - // If UDP, we call parseSocketReadIndicationUDP. - // Otherwise, we call parseSocketReadIndication. - if (_lastSocketProtocol[socket] == SARA_R5_UDP) + searchPtr += strlen("+UUSORD:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + int ret = sscanf(searchPtr, "%d,%d", &socket, &length); + if (ret == 2) { if (_printDebug == true) - _debugPort->println(F("processReadEvent: received +UUSORD but socket is UDP. Calling parseSocketReadIndicationUDP")); - parseSocketReadIndicationUDP(socket, length); + _debugPort->println(F("processReadEvent: read socket data")); + // From the SARA_R5 AT Commands Manual: + // "For the UDP socket type the URC +UUSORD: , notifies that a UDP packet has been received, + // either when buffer is empty or after a UDP packet has been read and one or more packets are stored in the + // buffer." + // So we need to check if this is a TCP socket or a UDP socket: + // If UDP, we call parseSocketReadIndicationUDP. + // Otherwise, we call parseSocketReadIndication. + if (_lastSocketProtocol[socket] == SARA_R5_UDP) + { + if (_printDebug == true) + _debugPort->println(F("processReadEvent: received +UUSORD but socket is UDP. Calling parseSocketReadIndicationUDP")); + parseSocketReadIndicationUDP(socket, length); + } + else + parseSocketReadIndication(socket, length); + return true; } - else - parseSocketReadIndication(socket, length); - return true; } } { // URC: +UUSORF (Receive From command (UDP only)) int socket, length; - int ret = sscanf(event, "+UUSORF: %d,%d", &socket, &length); - if (ret == 2) + char *searchPtr = strstr(event, "+UUSORF:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: UDP receive")); - parseSocketReadIndicationUDP(socket, length); - return true; + searchPtr += strlen("+UUSORF:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + int ret = sscanf(searchPtr, "%d,%d", &socket, &length); + if (ret == 2) + { + if (_printDebug == true) + _debugPort->println(F("processReadEvent: UDP receive")); + parseSocketReadIndicationUDP(socket, length); + return true; + } } } { // URC: +UUSOLI (Set Listening Socket) @@ -364,43 +376,55 @@ bool SARA_R5::processURCEvent(const char *event) int remoteIPstore[4] = {0,0,0,0}; int localIPstore[4] = {0,0,0,0}; - int ret = sscanf(event, - "+UUSOLI: %d,\"%d.%d.%d.%d\",%u,%d,\"%d.%d.%d.%d\",%u", - &socket, - &remoteIPstore[0], &remoteIPstore[1], &remoteIPstore[2], &remoteIPstore[3], - &port, &listenSocket, - &localIPstore[0], &localIPstore[1], &localIPstore[2], &localIPstore[3], - &listenPort); - for (int i = 0; i <= 3; i++) + char *searchPtr = strstr(event, "+UUSOLI:"); + if (searchPtr != nullptr) { + searchPtr += strlen("+UUSOLI:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + int ret = sscanf(searchPtr, + "%d,\"%d.%d.%d.%d\",%u,%d,\"%d.%d.%d.%d\",%u", + &socket, + &remoteIPstore[0], &remoteIPstore[1], &remoteIPstore[2], &remoteIPstore[3], + &port, &listenSocket, + &localIPstore[0], &localIPstore[1], &localIPstore[2], &localIPstore[3], + &listenPort); + for (int i = 0; i <= 3; i++) + { + if (ret >= 5) + remoteIP[i] = (uint8_t)remoteIPstore[i]; + if (ret >= 11) + localIP[i] = (uint8_t)localIPstore[i]; + } if (ret >= 5) - remoteIP[i] = (uint8_t)remoteIPstore[i]; - if (ret >= 11) - localIP[i] = (uint8_t)localIPstore[i]; - } - if (ret >= 5) - { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: socket listen")); - parseSocketListenIndication(listenSocket, localIP, listenPort, socket, remoteIP, port); - return true; + { + if (_printDebug == true) + _debugPort->println(F("processReadEvent: socket listen")); + parseSocketListenIndication(listenSocket, localIP, listenPort, socket, remoteIP, port); + return true; + } } } { // URC: +UUSOCL (Close Socket) int socket; - int ret = sscanf(event, "+UUSOCL: %d", &socket); - if (ret == 1) + char *searchPtr = strstr(event, "+UUSOCL:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: socket close")); - if ((socket >= 0) && (socket <= 6)) + searchPtr += strlen("+UUSOCL:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + int ret = sscanf(searchPtr, "%d", &socket); + if (ret == 1) { - if (_socketCloseCallback != NULL) + if (_printDebug == true) + _debugPort->println(F("processReadEvent: socket close")); + if ((socket >= 0) && (socket <= 6)) { - _socketCloseCallback(socket); + if (_socketCloseCallback != nullptr) + { + _socketCloseCallback(socket); + } } + return true; } - return true; } } { // URC: +UULOC (Localization information - CellLocate and hybrid positioning) @@ -417,61 +441,67 @@ bool SARA_R5::processURCEvent(const char *event) // Maybe we should also scan for +UUGIND and extract the activated gnss system? // This assumes the ULOC response type is "0" or "1" - as selected by gpsRequest detailed - scanNum = sscanf(event, - "+UULOC: %d/%d/%d,%d:%d:%d.%d,%d.%[^,],%d.%[^,],%d,%lu,%u,%u,%*s", - &dateStore[0], &dateStore[1], &clck.date.year, - &dateStore[2], &dateStore[3], &dateStore[4], &clck.time.ms, - &latH, latL, &lonH, lonL, &alt, &uncertainty, - &speedU, &cogU); - clck.date.day = dateStore[0]; - clck.date.month = dateStore[1]; - clck.time.hour = dateStore[2]; - clck.time.minute = dateStore[3]; - clck.time.second = dateStore[4]; - - if (scanNum >= 13) - { - // Found a Location string! - if (_printDebug == true) + char *searchPtr = strstr(event, "+UULOC:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UULOC:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, + "%d/%d/%d,%d:%d:%d.%d,%d.%[^,],%d.%[^,],%d,%lu,%u,%u,%*s", + &dateStore[0], &dateStore[1], &clck.date.year, + &dateStore[2], &dateStore[3], &dateStore[4], &clck.time.ms, + &latH, latL, &lonH, lonL, &alt, &uncertainty, + &speedU, &cogU); + clck.date.day = dateStore[0]; + clck.date.month = dateStore[1]; + clck.time.hour = dateStore[2]; + clck.time.minute = dateStore[3]; + clck.time.second = dateStore[4]; + + if (scanNum >= 13) { - _debugPort->println(F("processReadEvent: location")); - } + // Found a Location string! + if (_printDebug == true) + { + _debugPort->println(F("processReadEvent: location")); + } - if (latH >= 0) - gps.lat = (float)latH + ((float)atol(latL) / pow(10, strlen(latL))); - else - gps.lat = (float)latH - ((float)atol(latL) / pow(10, strlen(latL))); - if (lonH >= 0) - gps.lon = (float)lonH + ((float)atol(lonL) / pow(10, strlen(lonL))); - else - gps.lon = (float)lonH - ((float)atol(lonL) / pow(10, strlen(lonL))); - gps.alt = (float)alt; - if (scanNum >= 15) // If detailed response, get speed data - { - spd.speed = (float)speedU; - spd.cog = (float)cogU; - } + if (latH >= 0) + gps.lat = (float)latH + ((float)atol(latL) / pow(10, strlen(latL))); + else + gps.lat = (float)latH - ((float)atol(latL) / pow(10, strlen(latL))); + if (lonH >= 0) + gps.lon = (float)lonH + ((float)atol(lonL) / pow(10, strlen(lonL))); + else + gps.lon = (float)lonH - ((float)atol(lonL) / pow(10, strlen(lonL))); + gps.alt = (float)alt; + if (scanNum >= 15) // If detailed response, get speed data + { + spd.speed = (float)speedU; + spd.cog = (float)cogU; + } - // if (_printDebug == true) - // { - // _debugPort->print(F("processReadEvent: location: lat: ")); - // _debugPort->print(gps.lat, 7); - // _debugPort->print(F(" lon: ")); - // _debugPort->print(gps.lon, 7); - // _debugPort->print(F(" alt: ")); - // _debugPort->print(gps.alt, 2); - // _debugPort->print(F(" speed: ")); - // _debugPort->print(spd.speed, 2); - // _debugPort->print(F(" cog: ")); - // _debugPort->println(spd.cog, 2); - // } + // if (_printDebug == true) + // { + // _debugPort->print(F("processReadEvent: location: lat: ")); + // _debugPort->print(gps.lat, 7); + // _debugPort->print(F(" lon: ")); + // _debugPort->print(gps.lon, 7); + // _debugPort->print(F(" alt: ")); + // _debugPort->print(gps.alt, 2); + // _debugPort->print(F(" speed: ")); + // _debugPort->print(spd.speed, 2); + // _debugPort->print(F(" cog: ")); + // _debugPort->println(spd.cog, 2); + // } + + if (_gpsRequestCallback != nullptr) + { + _gpsRequestCallback(clck, gps, spd, uncertainty); + } - if (_gpsRequestCallback != NULL) - { - _gpsRequestCallback(clck, gps, spd, uncertainty); + return true; } - - return true; } } { // URC: +UUSIMSTAT (SIM Status) @@ -479,21 +509,27 @@ bool SARA_R5::processURCEvent(const char *event) int scanNum; int stateStore; - scanNum = sscanf(event, "+UUSIMSTAT:%d", &stateStore); // Note: no space after the colon! - - if (scanNum == 1) + char *searchPtr = strstr(event, "+UUSIMSTAT:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: SIM status")); - - state = (SARA_R5_sim_states_t)stateStore; + searchPtr += strlen("+UUSIMSTAT:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d", &stateStore); - if (_simStateReportCallback != NULL) + if (scanNum == 1) { - _simStateReportCallback(state); - } + if (_printDebug == true) + _debugPort->println(F("processReadEvent: SIM status")); + + state = (SARA_R5_sim_states_t)stateStore; + + if (_simStateReportCallback != nullptr) + { + _simStateReportCallback(state); + } - return true; + return true; + } } } { // URC: +UUPSDA (Packet Switched Data Action) @@ -502,58 +538,76 @@ bool SARA_R5::processURCEvent(const char *event) int scanNum; int remoteIPstore[4]; - scanNum = sscanf(event, "+UUPSDA: %d,\"%d.%d.%d.%d\"", - &result, &remoteIPstore[0], &remoteIPstore[1], &remoteIPstore[2], &remoteIPstore[3]); - - if (scanNum == 5) + char *searchPtr = strstr(event, "+UUPSDA:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: packet switched data action")); + searchPtr += strlen("+UUPSDA:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,\"%d.%d.%d.%d\"", + &result, &remoteIPstore[0], &remoteIPstore[1], &remoteIPstore[2], &remoteIPstore[3]); - for (int i = 0; i <= 3; i++) + if (scanNum == 5) { - remoteIP[i] = (uint8_t)remoteIPstore[i]; - } + if (_printDebug == true) + _debugPort->println(F("processReadEvent: packet switched data action")); - if (_psdActionRequestCallback != NULL) - { - _psdActionRequestCallback(result, remoteIP); - } + for (int i = 0; i <= 3; i++) + { + remoteIP[i] = (uint8_t)remoteIPstore[i]; + } - return true; + if (_psdActionRequestCallback != nullptr) + { + _psdActionRequestCallback(result, remoteIP); + } + + return true; + } } } { // URC: +UUHTTPCR (HTTP Command Result) int profile, command, result; int scanNum; - scanNum = sscanf(event, "+UUHTTPCR: %d,%d,%d", &profile, &command, &result); - - if (scanNum == 3) + char *searchPtr = strstr(event, "+UUHTTPCR:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: HTTP command result")); + searchPtr += strlen("+UUHTTPCR:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d,%d", &profile, &command, &result); - if ((profile >= 0) && (profile < SARA_R5_NUM_HTTP_PROFILES)) + if (scanNum == 3) { - if (_httpCommandRequestCallback != NULL) + if (_printDebug == true) + _debugPort->println(F("processReadEvent: HTTP command result")); + + if ((profile >= 0) && (profile < SARA_R5_NUM_HTTP_PROFILES)) { - _httpCommandRequestCallback(profile, command, result); + if (_httpCommandRequestCallback != nullptr) + { + _httpCommandRequestCallback(profile, command, result); + } } - } - return true; + return true; + } } } - { // URC: +UUMQTTC (HTTP Command Result) - int command, result; - int scanNum; - int qos = -1; - String topic; - scanNum = sscanf(event, "+UUMQTTC:%d,%d", &command, &result); + { // URC: +UUMQTTC (MQTT Command Result) + int command, result; + int scanNum; + int qos = -1; + String topic; + + char *searchPtr = strstr(event, "+UUMQTTC:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UUMQTTC:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d", &command, &result); if ((scanNum == 2) && (command == SARA_R5_MQTT_COMMAND_SUBSCRIBE)) { char topicC[100] = ""; - scanNum = sscanf(event, "+UUMQTTC:%*d,%*d,%d,\"%[^\"]\"", &qos, topicC); + scanNum = sscanf(searchPtr, "%*d,%*d,%d,\"%[^\"]\"", &qos, topicC); topic = topicC; } if ((scanNum == 2) || (scanNum == 4)) @@ -561,13 +615,14 @@ bool SARA_R5::processURCEvent(const char *event) if (_printDebug == true) _debugPort->println(F("processReadEvent: MQTT command result")); - if (_mqttCommandRequestCallback != NULL) + if (_mqttCommandRequestCallback != nullptr) { _mqttCommandRequestCallback(command, result); } return true; } + } } { // URC: +UUPING (Ping Result) int retry = 0; @@ -577,81 +632,99 @@ bool SARA_R5::processURCEvent(const char *event) IPAddress remoteIP = {0, 0, 0, 0}; long rtt = 0; int scanNum; - const char *searchPtr = event; // Try to extract the UUPING retries and payload size - scanNum = sscanf(searchPtr, "+UUPING:%d,%d,", &retry, &p_size); - - if (scanNum == 2) + char *searchPtr = strstr(event, "+UUPING:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - { - _debugPort->println(F("processReadEvent: ping")); - } - - searchPtr = strchr(++searchPtr, '\"'); // Search to the first quote + searchPtr += strlen("+UUPING:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d,", &retry, &p_size); - // Extract the remote host name, stop at the next quote - while ((*(++searchPtr) != '\"') && (*searchPtr != '\0')) + if (scanNum == 2) { - remote_host.concat(*(searchPtr)); - } + if (_printDebug == true) + { + _debugPort->println(F("processReadEvent: ping")); + } - if (*searchPtr != '\0') // Make sure we found a quote - { - int remoteIPstore[4]; - scanNum = sscanf(searchPtr, "\",\"%d.%d.%d.%d\",%d,%ld", - &remoteIPstore[0], &remoteIPstore[1], &remoteIPstore[2], &remoteIPstore[3], &ttl, &rtt); - for (int i = 0; i <= 3; i++) + searchPtr = strchr(++searchPtr, '\"'); // Search to the first quote + + // Extract the remote host name, stop at the next quote + while ((*(++searchPtr) != '\"') && (*searchPtr != '\0')) { - remoteIP[i] = (uint8_t)remoteIPstore[i]; + remote_host.concat(*(searchPtr)); } - if (scanNum == 6) // Make sure we extracted enough data + if (*searchPtr != '\0') // Make sure we found a quote { - if (_pingRequestCallback != NULL) + int remoteIPstore[4]; + scanNum = sscanf(searchPtr, "\",\"%d.%d.%d.%d\",%d,%ld", + &remoteIPstore[0], &remoteIPstore[1], &remoteIPstore[2], &remoteIPstore[3], &ttl, &rtt); + for (int i = 0; i <= 3; i++) { - _pingRequestCallback(retry, p_size, remote_host, remoteIP, ttl, rtt); + remoteIP[i] = (uint8_t)remoteIPstore[i]; + } + + if (scanNum == 6) // Make sure we extracted enough data + { + if (_pingRequestCallback != nullptr) + { + _pingRequestCallback(retry, p_size, remote_host, remoteIP, ttl, rtt); + } } } + return true; } - return true; } } - { // URC: +A + { // URC: +CREG int status = 0; unsigned int lac = 0, ci = 0, Act = 0; - int scanNum = sscanf(event, "+CREG: %d,\"%4x\",\"%4x\",%d", &status, &lac, &ci, &Act); - if (scanNum == 4) + char *searchPtr = strstr(event, "+CREG:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: CREG")); - - if (_registrationCallback != NULL) + searchPtr += strlen("+CREG:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + int scanNum = sscanf(searchPtr, "%d,\"%4x\",\"%4x\",%d", &status, &lac, &ci, &Act); + if (scanNum == 4) { - _registrationCallback((SARA_R5_registration_status_t)status, lac, ci, Act); + if (_printDebug == true) + _debugPort->println(F("processReadEvent: CREG")); + + if (_registrationCallback != nullptr) + { + _registrationCallback((SARA_R5_registration_status_t)status, lac, ci, Act); + } + + return true; } - - return true; } } { // URC: +CEREG int status = 0; unsigned int tac = 0, ci = 0, Act = 0; - int scanNum = sscanf(event, "+CEREG: %d,\"%4x\",\"%4x\",%d", &status, &tac, &ci, &Act); - if (scanNum == 4) + char *searchPtr = strstr(event, "+CEREG:"); + if (searchPtr != nullptr) { - if (_printDebug == true) - _debugPort->println(F("processReadEvent: CEREG")); - - if (_epsRegistrationCallback != NULL) + searchPtr += strlen("+CEREG:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + int scanNum = sscanf(searchPtr, "%d,\"%4x\",\"%4x\",%d", &status, &tac, &ci, &Act); + if (scanNum == 4) { - _epsRegistrationCallback((SARA_R5_registration_status_t)status, tac, ci, Act); + if (_printDebug == true) + _debugPort->println(F("processReadEvent: CEREG")); + + if (_epsRegistrationCallback != nullptr) + { + _epsRegistrationCallback((SARA_R5_registration_status_t)status, tac, ci, Act); + } + + return true; } - - return true; } } + // NOTE: When adding new URC messages, remember to update pruneBacklog too! return false; } @@ -764,11 +837,11 @@ SARA_R5_error_t SARA_R5::setRegistrationCallback(void (*registrationCallback)(SA _registrationCallback = registrationCallback; char *command = sara_r5_calloc_char(strlen(SARA_R5_REGISTRATION_STATUS) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_REGISTRATION_STATUS, 2/*enable URC with location*/); SARA_R5_error_t err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -778,11 +851,11 @@ SARA_R5_error_t SARA_R5::setEpsRegistrationCallback(void (*registrationCallback) _epsRegistrationCallback = registrationCallback; char *command = sara_r5_calloc_char(strlen(SARA_R5_EPSREGISTRATION_STATUS) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_EPSREGISTRATION_STATUS, 2/*enable URC with location*/); SARA_R5_error_t err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -806,7 +879,7 @@ SARA_R5_error_t SARA_R5::at(void) { SARA_R5_error_t err; - err = sendCommandWithResponse(NULL, SARA_R5_RESPONSE_OK, NULL, + err = sendCommandWithResponse(nullptr, SARA_R5_RESPONSE_OK, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); return err; @@ -818,11 +891,11 @@ SARA_R5_error_t SARA_R5::enableEcho(bool enable) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_ECHO) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s%d", SARA_R5_COMMAND_ECHO, enable ? 1 : 0); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -839,7 +912,7 @@ String SARA_R5::getManufacturerID(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n%s\r\n", idResponse) != 1) + if (sscanf(response, "\r\n%15s\r\n", idResponse) != 1) { memset(idResponse, 0, 16); } @@ -851,7 +924,7 @@ String SARA_R5::getManufacturerID(void) String SARA_R5::getModelID(void) { char *response; - char idResponse[16] = {0x00}; // E.g. SARA-R510M8Q + char idResponse[32] = {0x00}; // E.g. SARA-R510M8Q SARA_R5_error_t err; response = sara_r5_calloc_char(minimumResponseAllocation); @@ -860,7 +933,7 @@ String SARA_R5::getModelID(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n%s\r\n", idResponse) != 1) + if (sscanf(response, "\r\n%31s\r\n", idResponse) != 1) { memset(idResponse, 0, 16); } @@ -881,7 +954,7 @@ String SARA_R5::getFirmwareVersion(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n%s\r\n", idResponse) != 1) + if (sscanf(response, "\r\n%15s\r\n", idResponse) != 1) { memset(idResponse, 0, 16); } @@ -893,7 +966,7 @@ String SARA_R5::getFirmwareVersion(void) String SARA_R5::getSerialNo(void) { char *response; - char idResponse[16] = {0x00}; // E.g. 357520070120767 + char idResponse[32] = {0x00}; // E.g. 357520070120767 SARA_R5_error_t err; response = sara_r5_calloc_char(minimumResponseAllocation); @@ -902,7 +975,7 @@ String SARA_R5::getSerialNo(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n%s\r\n", idResponse) != 1) + if (sscanf(response, "\r\n%31s\r\n", idResponse) != 1) { memset(idResponse, 0, 16); } @@ -914,7 +987,7 @@ String SARA_R5::getSerialNo(void) String SARA_R5::getIMEI(void) { char *response; - char imeiResponse[16] = {0x00}; // E.g. 004999010640000 + char imeiResponse[32] = {0x00}; // E.g. 004999010640000 SARA_R5_error_t err; response = sara_r5_calloc_char(minimumResponseAllocation); @@ -923,7 +996,7 @@ String SARA_R5::getIMEI(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n%s\r\n", imeiResponse) != 1) + if (sscanf(response, "\r\n%31s\r\n", imeiResponse) != 1) { memset(imeiResponse, 0, 16); } @@ -935,7 +1008,7 @@ String SARA_R5::getIMEI(void) String SARA_R5::getIMSI(void) { char *response; - char imsiResponse[16] = {0x00}; // E.g. 222107701772423 + char imsiResponse[32] = {0x00}; // E.g. 222107701772423 SARA_R5_error_t err; response = sara_r5_calloc_char(minimumResponseAllocation); @@ -944,7 +1017,7 @@ String SARA_R5::getIMSI(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n%s\r\n", imsiResponse) != 1) + if (sscanf(response, "\r\n%31s\r\n", imsiResponse) != 1) { memset(imsiResponse, 0, 16); } @@ -956,7 +1029,7 @@ String SARA_R5::getIMSI(void) String SARA_R5::getCCID(void) { char *response; - char ccidResponse[21] = {0x00}; // E.g. +CCID: 8939107900010087330 + char ccidResponse[32] = {0x00}; // E.g. +CCID: 8939107900010087330 SARA_R5_error_t err; response = sara_r5_calloc_char(minimumResponseAllocation); @@ -965,9 +1038,15 @@ String SARA_R5::getCCID(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n+CCID: %s", ccidResponse) != 1) + char *searchPtr = strstr(response, "\r\n+CCID:"); + if (searchPtr != nullptr) { - memset(ccidResponse, 0, 21); + searchPtr += strlen("\r\n+CCID:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + if (sscanf(searchPtr, "%31s", ccidResponse) != 1) + { + ccidResponse[0] = 0; + } } } free(response); @@ -986,9 +1065,15 @@ String SARA_R5::getSubscriberNo(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_10_SEC_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n+CNUM: %s", idResponse) != 1) + char *searchPtr = strstr(response, "\r\n+CNUM:"); + if (searchPtr != nullptr) { - memset(idResponse, 0, 128); + searchPtr += strlen("\r\n+CNUM:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + if (sscanf(searchPtr, "%127s", idResponse) != 1) + { + idResponse[0] = 0; + } } } free(response); @@ -1007,9 +1092,15 @@ String SARA_R5::getCapabilities(void) SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err == SARA_R5_ERROR_SUCCESS) { - if (sscanf(response, "\r\n+GCAP: %s", idResponse) != 1) + char *searchPtr = strstr(response, "\r\n+GCAP:"); + if (searchPtr != nullptr) { - memset(idResponse, 0, 128); + searchPtr += strlen("\r\n+GCAP:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + if (sscanf(searchPtr, "%127s", idResponse) != 1) + { + idResponse[0] = 0; + } } } free(response); @@ -1047,12 +1138,12 @@ String SARA_R5::clock(void) char *clockEnd; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_CLOCK) + 2); - if (command == NULL) + if (command == nullptr) return ""; sprintf(command, "%s?", SARA_R5_COMMAND_CLOCK); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return ""; @@ -1069,7 +1160,7 @@ String SARA_R5::clock(void) // Response format: \r\n+CCLK: "YY/MM/DD,HH:MM:SS-TZ"\r\n\r\nOK\r\n clockBegin = strchr(response, '\"'); // Find first quote - if (clockBegin == NULL) + if (clockBegin == nullptr) { free(command); free(response); @@ -1077,7 +1168,7 @@ String SARA_R5::clock(void) } clockBegin += 1; // Increment pointer to begin at first number clockEnd = strchr(clockBegin, '\"'); // Find last quote - if (clockEnd == NULL) + if (clockEnd == nullptr) { free(command); free(response); @@ -1105,12 +1196,12 @@ SARA_R5_error_t SARA_R5::clock(uint8_t *y, uint8_t *mo, uint8_t *d, int iy, imo, id, ih, imin, is, itz; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_CLOCK) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_COMMAND_CLOCK); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1122,10 +1213,14 @@ SARA_R5_error_t SARA_R5::clock(uint8_t *y, uint8_t *mo, uint8_t *d, // Response format (if TZ is negative): \r\n+CCLK: "YY/MM/DD,HH:MM:SS-TZ"\r\n\r\nOK\r\n if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+CCLK: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+CCLK: \"%d/%d/%d,%d:%d:%d%c%d\"\r\n", + char *searchPtr = strstr(response, "+CCLK:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+CCLK:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "\"%d/%d/%d,%d:%d:%d%c%d\"\r\n", &iy, &imo, &id, &ih, &imin, &is, &tzPlusMinus, &itz); + } if (scanNum == 8) { *y = iy; @@ -1194,12 +1289,12 @@ SARA_R5_error_t SARA_R5::setClock(String theTime) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_CLOCK) + theTime.length() + 8); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\"", SARA_R5_COMMAND_CLOCK, theTime.c_str()); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -1216,7 +1311,7 @@ SARA_R5_error_t SARA_R5::setUtimeMode(SARA_R5_utime_mode_t mode, SARA_R5_utime_s char *command; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_REQUEST_TIME) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (mode == SARA_R5_UTIME_MODE_STOP) // stop UTIME does not require a sensor sprintf(command, "%s=%d", SARA_R5_GNSS_REQUEST_TIME, mode); @@ -1224,7 +1319,7 @@ SARA_R5_error_t SARA_R5::setUtimeMode(SARA_R5_utime_mode_t mode, SARA_R5_utime_s sprintf(command, "%s=%d,%d", SARA_R5_GNSS_REQUEST_TIME, mode, sensor); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_10_SEC_TIMEOUT); + nullptr, SARA_R5_10_SEC_TIMEOUT); free(command); return err; } @@ -1239,12 +1334,12 @@ SARA_R5_error_t SARA_R5::getUtimeMode(SARA_R5_utime_mode_t *mode, SARA_R5_utime_ SARA_R5_utime_sensor_t s; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_REQUEST_TIME) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_GNSS_REQUEST_TIME); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1257,9 +1352,13 @@ SARA_R5_error_t SARA_R5::getUtimeMode(SARA_R5_utime_mode_t *mode, SARA_R5_utime_ if (err == SARA_R5_ERROR_SUCCESS) { int mStore, sStore, scanned = 0; - char *searchPtr = strstr(response, "+UTIME: "); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+UTIME: %d,%d\r\n", &mStore, &sStore); + char *searchPtr = strstr(response, "+UTIME:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UTIME:"); // Move searchPtr to first character - probably a space + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d,%d\r\n", &mStore, &sStore); + } m = (SARA_R5_utime_mode_t)mStore; s = (SARA_R5_utime_sensor_t)sStore; if (scanned == 2) @@ -1287,12 +1386,12 @@ SARA_R5_error_t SARA_R5::setUtimeIndication(SARA_R5_utime_urc_configuration_t co char *command; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_TIME_INDICATION) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_GNSS_TIME_INDICATION, config); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -1306,12 +1405,12 @@ SARA_R5_error_t SARA_R5::getUtimeIndication(SARA_R5_utime_urc_configuration_t *c SARA_R5_utime_urc_configuration_t c; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_TIME_INDICATION) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_GNSS_TIME_INDICATION); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1324,9 +1423,13 @@ SARA_R5_error_t SARA_R5::getUtimeIndication(SARA_R5_utime_urc_configuration_t *c if (err == SARA_R5_ERROR_SUCCESS) { int cStore, scanned = 0; - char *searchPtr = strstr(response, "+UTIMEIND: "); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+UTIMEIND: %d\r\n", &cStore); + char *searchPtr = strstr(response, "+UTIMEIND:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UTIMEIND:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d\r\n", &cStore); + } c = (SARA_R5_utime_urc_configuration_t)cStore; if (scanned == 1) { @@ -1347,7 +1450,7 @@ SARA_R5_error_t SARA_R5::setUtimeConfiguration(int32_t offsetNanoseconds, int32_ char *command; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_TIME_CONFIGURATION) + 48); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) sprintf(command, "%s=%d,%d", SARA_R5_GNSS_TIME_CONFIGURATION, offsetNanoseconds, offsetSeconds); @@ -1356,7 +1459,7 @@ SARA_R5_error_t SARA_R5::setUtimeConfiguration(int32_t offsetNanoseconds, int32_ #endif err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -1371,12 +1474,12 @@ SARA_R5_error_t SARA_R5::getUtimeConfiguration(int32_t *offsetNanoseconds, int32 int32_t os; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_TIME_CONFIGURATION) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_GNSS_TIME_CONFIGURATION); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1389,13 +1492,17 @@ SARA_R5_error_t SARA_R5::getUtimeConfiguration(int32_t *offsetNanoseconds, int32 if (err == SARA_R5_ERROR_SUCCESS) { int scanned = 0; - char *searchPtr = strstr(response, "+UTIMECFG: "); - if (searchPtr != NULL) + char *searchPtr = strstr(response, "+UTIMECFG:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UTIMECFG:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) - scanned = sscanf(searchPtr, "+UTIMECFG: %d,%d\r\n", &ons, &os); + scanned = sscanf(searchPtr, "%d,%d\r\n", &ons, &os); #else - scanned = sscanf(searchPtr, "+UTIMECFG: %ld,%ld\r\n", &ons, &os); + scanned = sscanf(searchPtr, "%ld,%ld\r\n", &ons, &os); #endif + } if (scanned == 2) { *offsetNanoseconds = ons; @@ -1416,12 +1523,12 @@ SARA_R5_error_t SARA_R5::autoTimeZone(bool enable) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_AUTO_TZ) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_COMMAND_AUTO_TZ, enable ? 1 : 0); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -1434,12 +1541,12 @@ int8_t SARA_R5::rssi(void) int rssi; command = sara_r5_calloc_char(strlen(SARA_R5_SIGNAL_QUALITY) + 1); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s", SARA_R5_SIGNAL_QUALITY); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1456,9 +1563,13 @@ int8_t SARA_R5::rssi(void) } int scanned = 0; - char *searchPtr = strstr(response, "+CSQ: "); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+CSQ: %d,%*d", &rssi); + char *searchPtr = strstr(response, "+CSQ:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+CSQ:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d,%*d", &rssi); + } if (scanned != 1) { rssi = -1; @@ -1477,12 +1588,12 @@ SARA_R5_registration_status_t SARA_R5::registration(bool eps) int status; const char* tag = eps ? SARA_R5_EPSREGISTRATION_STATUS : SARA_R5_REGISTRATION_STATUS; command = sara_r5_calloc_char(strlen(tag) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_REGISTRATION_INVALID; sprintf(command, "%s?", tag); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_REGISTRATION_INVALID; @@ -1499,11 +1610,13 @@ SARA_R5_registration_status_t SARA_R5::registration(bool eps) } int scanned = 0; - const char *startTag = eps ? "+CEREG: " : "+CREG: "; + const char *startTag = eps ? "+CEREG:" : "+CREG:"; char *searchPtr = strstr(response, startTag); - if (searchPtr != NULL) { - const char *format = eps ? "+CEREG: %*d,%d" : "+CREG: %*d,%d"; - scanned = sscanf(searchPtr, format, &status); + if (searchPtr != nullptr) + { + searchPtr += eps ? strlen("+CEREG:") : strlen("+CREG:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%*d,%d", &status); } if (scanned != 1) status = SARA_R5_REGISTRATION_INVALID; @@ -1572,7 +1685,7 @@ SARA_R5_error_t SARA_R5::setAPN(String apn, uint8_t cid, SARA_R5_pdp_type pdpTyp return SARA_R5_ERROR_UNEXPECTED_PARAM; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_DEF) + strlen(apn.c_str()) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; switch (pdpType) { @@ -1597,10 +1710,10 @@ SARA_R5_error_t SARA_R5::setAPN(String apn, uint8_t cid, SARA_R5_pdp_type pdpTyp return SARA_R5_ERROR_UNEXPECTED_PARAM; break; } - if (apn == NULL) + if (apn == nullptr) { if (_printDebug == true) - _debugPort->println(F("setAPN: NULL")); + _debugPort->println(F("setAPN: nullptr")); sprintf(command, "%s=%d,\"%s\",\"\"", SARA_R5_MESSAGE_PDP_DEF, cid, pdpStr); } @@ -1615,7 +1728,7 @@ SARA_R5_error_t SARA_R5::setAPN(String apn, uint8_t cid, SARA_R5_pdp_type pdpTyp cid, pdpStr, apn.c_str()); } - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -1634,12 +1747,12 @@ SARA_R5_error_t SARA_R5::getAPN(int cid, String *apn, IPAddress *ip, SARA_R5_pdp return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_DEF) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_MESSAGE_PDP_DEF); response = sara_r5_calloc_char(1024); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1661,14 +1774,15 @@ SARA_R5_error_t SARA_R5::getAPN(int cid, String *apn, IPAddress *ip, SARA_R5_pdp { int scanned = 0; // Find the first/next occurrence of +CGDCONT: - searchPtr = strstr(searchPtr, "+CGDCONT: "); - if (searchPtr != NULL) + searchPtr = strstr(searchPtr, "+CGDCONT:"); + if (searchPtr != nullptr) { char strPdpType[10]; char strApn[128]; int ipOct[4]; - searchPtr += strlen("+CGDCONT: "); // Point to the cid + searchPtr += strlen("+CGDCONT:"); // Point to the cid + while (*searchPtr == ' ') searchPtr++; // skip spaces scanned = sscanf(searchPtr, "%d,\"%[^\"]\",\"%[^\"]\",\"%d.%d.%d.%d", &rcid, strPdpType, strApn, &ipOct[0], &ipOct[1], &ipOct[2], &ipOct[3]); @@ -1687,13 +1801,13 @@ SARA_R5_error_t SARA_R5::getAPN(int cid, String *apn, IPAddress *ip, SARA_R5_pdp keepGoing = false; } } - else // We don't have a match so let's clear the APN and IP address + else // We don't have a match so let's clear the APN and IP address { - if (apn) *apn = ""; - if (pdpType) *pdpType = PDP_TYPE_INVALID; - if (ip) *ip = {0, 0, 0, 0}; - keepGoing = false; - } + if (apn) *apn = ""; + if (pdpType) *pdpType = PDP_TYPE_INVALID; + if (ip) *ip = {0, 0, 0, 0}; + keepGoing = false; + } } } else @@ -1713,11 +1827,11 @@ SARA_R5_error_t SARA_R5::getSimStatus(String* code) char *command; char *response; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_SIMPIN) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_COMMAND_SIMPIN); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1729,9 +1843,12 @@ SARA_R5_error_t SARA_R5::getSimStatus(String* code) { int scanned = 0; char c[16]; - char *searchPtr = strstr(response, "+CPIN: "); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+CPIN: %s\r\n", c); + char *searchPtr = strstr(response, "+CPIN:"); + if (searchPtr != nullptr) { + searchPtr += strlen("+CPIN:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%15s\r\n", c); + } if (scanned == 1) { if(code) @@ -1752,11 +1869,11 @@ SARA_R5_error_t SARA_R5::setSimPin(String pin) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_SIMPIN) + 4 + pin.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\"", SARA_R5_COMMAND_SIMPIN, pin.c_str()); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -1767,12 +1884,12 @@ SARA_R5_error_t SARA_R5::setSIMstateReportingMode(int mode) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_SIM_STATE) + 4); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_SIM_STATE, mode); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; } @@ -1786,12 +1903,12 @@ SARA_R5_error_t SARA_R5::getSIMstateReportingMode(int *mode) int m; command = sara_r5_calloc_char(strlen(SARA_R5_SIM_STATE) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_SIM_STATE); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1803,9 +1920,13 @@ SARA_R5_error_t SARA_R5::getSIMstateReportingMode(int *mode) if (err == SARA_R5_ERROR_SUCCESS) { int scanned = 0; - char *searchPtr = strstr(response, "+USIMSTAT: "); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+USIMSTAT: %d\r\n", &m); + char *searchPtr = strstr(response, "+USIMSTAT:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USIMSTAT:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d\r\n", &m); + } if (scanned == 1) { *mode = m; @@ -1840,7 +1961,7 @@ SARA_R5_error_t SARA_R5::enterPPP(uint8_t cid, char dialing_type_char, } command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_ENTER_PPP) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (dialing_type_char != 0) { @@ -1853,7 +1974,7 @@ SARA_R5_error_t SARA_R5::enterPPP(uint8_t cid, char dialing_type_char, dialNumber, PPP_L2P[l2p], (unsigned int)cid); } - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_CONNECT, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_CONNECT, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -1868,13 +1989,13 @@ uint8_t SARA_R5::getOperators(struct operator_stats *opRet, int maxOps) uint8_t opsSeen = 0; command = sara_r5_calloc_char(strlen(SARA_R5_OPERATOR_SELECTION) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=?", SARA_R5_OPERATOR_SELECTION); int responseSize = (maxOps + 1) * 48; response = sara_r5_calloc_char(responseSize); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -1911,10 +2032,10 @@ uint8_t SARA_R5::getOperators(struct operator_stats *opRet, int maxOps) for (; op < maxOps; op++) { opBegin = strchr(opBegin, '('); - if (opBegin == NULL) + if (opBegin == nullptr) break; opEnd = strchr(opBegin, ')'); - if (opEnd == NULL) + if (opEnd == nullptr) break; int sscanRead = sscanf(opBegin, "(%d,\"%[^\"]\",\"%[^\"]\",\"%lu\",%d)%*s", @@ -1949,12 +2070,12 @@ SARA_R5_error_t SARA_R5::registerOperator(struct operator_stats oper) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_OPERATOR_SELECTION) + 24); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=1,2,\"%lu\"", SARA_R5_OPERATOR_SELECTION, oper.numOp); // AT+COPS maximum response time is 3 minutes (180000 ms) - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_3_MIN_TIMEOUT); free(command); @@ -1967,12 +2088,12 @@ SARA_R5_error_t SARA_R5::automaticOperatorSelection() char *command; command = sara_r5_calloc_char(strlen(SARA_R5_OPERATOR_SELECTION) + 6); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=0,0", SARA_R5_OPERATOR_SELECTION); // AT+COPS maximum response time is 3 minutes (180000 ms) - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_3_MIN_TIMEOUT); free(command); @@ -1988,12 +2109,12 @@ SARA_R5_error_t SARA_R5::getOperator(String *oper) char mode; command = sara_r5_calloc_char(strlen(SARA_R5_OPERATOR_SELECTION) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_OPERATOR_SELECTION); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2005,10 +2126,11 @@ SARA_R5_error_t SARA_R5::getOperator(String *oper) if (err == SARA_R5_ERROR_SUCCESS) { - searchPtr = strstr(response, "+COPS: "); - if (searchPtr != NULL) + searchPtr = strstr(response, "+COPS:"); + if (searchPtr != nullptr) { - searchPtr += strlen("+COPS: "); // Move searchPtr to first char + searchPtr += strlen("+COPS:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces mode = *searchPtr; // Read first char -- should be mode if (mode == '2') // Check for de-register { @@ -2019,7 +2141,7 @@ SARA_R5_error_t SARA_R5::getOperator(String *oper) { *oper = ""; searchPtr = strchr(searchPtr, '\"'); // Move to first quote - if (searchPtr == NULL) + if (searchPtr == nullptr) { err = SARA_R5_ERROR_DEREGISTERED; } @@ -2050,11 +2172,11 @@ SARA_R5_error_t SARA_R5::deregisterOperator(void) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_OPERATOR_SELECTION) + 4); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=2", SARA_R5_OPERATOR_SELECTION); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_3_MIN_TIMEOUT); free(command); @@ -2067,12 +2189,12 @@ SARA_R5_error_t SARA_R5::setSMSMessageFormat(SARA_R5_message_format_t textMode) SARA_R5_error_t err; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_FORMAT) + 4); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_MESSAGE_FORMAT, (textMode == SARA_R5_MESSAGE_FORMAT_TEXT) ? 1 : 0); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -2087,16 +2209,16 @@ SARA_R5_error_t SARA_R5::sendSMS(String number, String message) SARA_R5_error_t err; numberCStr = sara_r5_calloc_char(number.length() + 2); - if (numberCStr == NULL) + if (numberCStr == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; number.toCharArray(numberCStr, number.length() + 1); command = sara_r5_calloc_char(strlen(SARA_R5_SEND_TEXT) + strlen(numberCStr) + 8); - if (command != NULL) + if (command != nullptr) { sprintf(command, "%s=\"%s\"", SARA_R5_SEND_TEXT, numberCStr); - err = sendCommandWithResponse(command, ">", NULL, + err = sendCommandWithResponse(command, ">", nullptr, SARA_R5_3_MIN_TIMEOUT); free(command); free(numberCStr); @@ -2104,7 +2226,7 @@ SARA_R5_error_t SARA_R5::sendSMS(String number, String message) return err; messageCStr = sara_r5_calloc_char(message.length() + 1); - if (messageCStr == NULL) + if (messageCStr == nullptr) { hwWrite(ASCII_CTRL_Z); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2113,7 +2235,7 @@ SARA_R5_error_t SARA_R5::sendSMS(String number, String message) messageCStr[message.length()] = ASCII_CTRL_Z; err = sendCommandWithResponse(messageCStr, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_3_MIN_TIMEOUT, minimumResponseAllocation, NOT_AT_COMMAND); + nullptr, SARA_R5_3_MIN_TIMEOUT, minimumResponseAllocation, NOT_AT_COMMAND); free(messageCStr); } @@ -2135,12 +2257,12 @@ SARA_R5_error_t SARA_R5::getPreferredMessageStorage(int *used, int *total, Strin int t; command = sara_r5_calloc_char(strlen(SARA_R5_PREF_MESSAGE_STORE) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\"", SARA_R5_PREF_MESSAGE_STORE, memory.c_str()); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2157,9 +2279,13 @@ SARA_R5_error_t SARA_R5::getPreferredMessageStorage(int *used, int *total, Strin } int scanned = 0; - char *searchPtr = strstr(response, "+CPMS: "); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+CPMS: %d,%d", &u, &t); + char *searchPtr = strstr(response, "+CPMS:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+CPMS:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d,%d", &u, &t); + } if (scanned == 2) { if (_printDebug == true) @@ -2191,12 +2317,12 @@ SARA_R5_error_t SARA_R5::readSMSmessage(int location, String *unread, String *fr char *response; command = sara_r5_calloc_char(strlen(SARA_R5_READ_TEXT_MESSAGE) + 5); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_READ_TEXT_MESSAGE, location); response = sara_r5_calloc_char(1024); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2209,11 +2335,12 @@ SARA_R5_error_t SARA_R5::readSMSmessage(int location, String *unread, String *fr { char *searchPtr = response; - // Find the first occurrence of +CGDCONT: - searchPtr = strstr(searchPtr, "+CMGR: "); - if (searchPtr != NULL) + // Find the first occurrence of +CMGR: + searchPtr = strstr(searchPtr, "+CMGR:"); + if (searchPtr != nullptr) { - searchPtr += strlen("+CMGR: "); // Point to the originator address + searchPtr += strlen("+CMGR:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces int pointer = 0; while ((*(++searchPtr) != '\"') && (*searchPtr != '\0') && (pointer < 12)) { @@ -2294,14 +2421,14 @@ SARA_R5_error_t SARA_R5::deleteSMSmessage(int location, int deleteFlag) SARA_R5_error_t err; command = sara_r5_calloc_char(strlen(SARA_R5_DELETE_MESSAGE) + 12); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (deleteFlag == 0) sprintf(command, "%s=%d", SARA_R5_DELETE_MESSAGE, location); else sprintf(command, "%s=%d,%d", SARA_R5_DELETE_MESSAGE, location, deleteFlag); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, SARA_R5_55_SECS_TIMEOUT); + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_55_SECS_TIMEOUT); free(command); return err; @@ -2328,12 +2455,12 @@ SARA_R5_error_t SARA_R5::setBaud(unsigned long baud) // Construct command command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_BAUD) + 7 + 12); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%lu", SARA_R5_COMMAND_BAUD, baud); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_SET_BAUD_TIMEOUT); + nullptr, SARA_R5_SET_BAUD_TIMEOUT); free(command); @@ -2346,12 +2473,12 @@ SARA_R5_error_t SARA_R5::setFlowControl(SARA_R5_flow_control_t value) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_FLOW_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s%d", SARA_R5_FLOW_CONTROL, value); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -2367,7 +2494,7 @@ SARA_R5_error_t SARA_R5::setGpioMode(SARA_R5_gpio_t gpio, // Example command: AT+UGPIOC=16,2 // Example command: AT+UGPIOC=23,0,1 command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_GPIO) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (mode == GPIO_OUTPUT) sprintf(command, "%s=%d,%d,%d", SARA_R5_COMMAND_GPIO, gpio, mode, value); @@ -2375,7 +2502,7 @@ SARA_R5_error_t SARA_R5::setGpioMode(SARA_R5_gpio_t gpio, sprintf(command, "%s=%d,%d", SARA_R5_COMMAND_GPIO, gpio, mode); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_10_SEC_TIMEOUT); + nullptr, SARA_R5_10_SEC_TIMEOUT); free(command); @@ -2392,12 +2519,12 @@ SARA_R5::SARA_R5_gpio_mode_t SARA_R5::getGpioMode(SARA_R5_gpio_t gpio) int gpioMode; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_GPIO) + 2); - if (command == NULL) + if (command == nullptr) return GPIO_MODE_INVALID; sprintf(command, "%s?", SARA_R5_COMMAND_GPIO); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return GPIO_MODE_INVALID; @@ -2419,7 +2546,7 @@ SARA_R5::SARA_R5_gpio_mode_t SARA_R5::getGpioMode(SARA_R5_gpio_t gpio) free(command); free(response); - if (gpioStart == NULL) + if (gpioStart == nullptr) return GPIO_MODE_INVALID; // If not found return invalid sscanf(gpioStart, "%*d,%d\r\n", &gpioMode); @@ -2435,7 +2562,7 @@ int SARA_R5::socketOpen(SARA_R5_socket_protocol_t protocol, unsigned int localPo char *responseStart; command = sara_r5_calloc_char(strlen(SARA_R5_CREATE_SOCKET) + 10); - if (command == NULL) + if (command == nullptr) return -1; if (localPort == 0) sprintf(command, "%s=%d", SARA_R5_CREATE_SOCKET, (int)protocol); @@ -2443,10 +2570,10 @@ int SARA_R5::socketOpen(SARA_R5_socket_protocol_t protocol, unsigned int localPo sprintf(command, "%s=%d,%d", SARA_R5_CREATE_SOCKET, (int)protocol, localPort); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { if (_printDebug == true) - _debugPort->println(F("socketOpen: Fail: NULL response")); + _debugPort->println(F("socketOpen: Fail: nullptr response")); free(command); return -1; } @@ -2469,8 +2596,8 @@ int SARA_R5::socketOpen(SARA_R5_socket_protocol_t protocol, unsigned int localPo return -1; } - responseStart = strstr(response, "+USOCR"); - if (responseStart == NULL) + responseStart = strstr(response, "+USOCR:"); + if (responseStart == nullptr) { if (_printDebug == true) { @@ -2483,7 +2610,9 @@ int SARA_R5::socketOpen(SARA_R5_socket_protocol_t protocol, unsigned int localPo return -1; } - sscanf(responseStart, "+USOCR: %d", &sockId); + responseStart += strlen("+USOCR:"); // Move searchPtr to first char + while (*responseStart == ' ') responseStart++; // skip spaces + sscanf(responseStart, "%d", &sockId); _lastSocketProtocol[sockId] = (int)protocol; free(command); @@ -2499,10 +2628,10 @@ SARA_R5_error_t SARA_R5::socketClose(int socket, unsigned long timeout) char *response; command = sara_r5_calloc_char(strlen(SARA_R5_CLOSE_SOCKET) + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2533,11 +2662,11 @@ SARA_R5_error_t SARA_R5::socketConnect(int socket, const char *address, char *command; command = sara_r5_calloc_char(strlen(SARA_R5_CONNECT_SOCKET) + strlen(address) + 11); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,\"%s\",%d", SARA_R5_CONNECT_SOCKET, socket, address, port); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, SARA_R5_IP_CONNECT_TIMEOUT); + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_IP_CONNECT_TIMEOUT); free(command); @@ -2548,7 +2677,7 @@ SARA_R5_error_t SARA_R5::socketConnect(int socket, IPAddress address, unsigned int port) { char *charAddress = sara_r5_calloc_char(16); - if (charAddress == NULL) + if (charAddress == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; memset(charAddress, 0, 16); sprintf(charAddress, "%d.%d.%d.%d", address[0], address[1], address[2], address[3]); @@ -2563,10 +2692,10 @@ SARA_R5_error_t SARA_R5::socketWrite(int socket, const char *str, int len) SARA_R5_error_t err; command = sara_r5_calloc_char(strlen(SARA_R5_WRITE_SOCKET) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2636,10 +2765,10 @@ SARA_R5_error_t SARA_R5::socketWriteUDP(int socket, const char *address, int por int dataLen = len == -1 ? strlen(str) : len; command = sara_r5_calloc_char(64); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2677,7 +2806,7 @@ SARA_R5_error_t SARA_R5::socketWriteUDP(int socket, const char *address, int por SARA_R5_error_t SARA_R5::socketWriteUDP(int socket, IPAddress address, int port, const char *str, int len) { char *charAddress = sara_r5_calloc_char(16); - if (charAddress == NULL) + if (charAddress == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; memset(charAddress, 0, 16); sprintf(charAddress, "%d.%d.%d.%d", address[0], address[1], address[2], address[3]); @@ -2705,7 +2834,7 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest, int int bytesToRead; // Set *bytesRead to zero - if (bytesRead != NULL) + if (bytesRead != nullptr) *bytesRead = 0; // Check if length is zero @@ -2718,14 +2847,14 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest, int // Allocate memory for the command command = sara_r5_calloc_char(strlen(SARA_R5_READ_SOCKET) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; // Allocate memory for the response // We only need enough to read _saraR5maxSocketRead bytes - not the whole thing int responseLength = _saraR5maxSocketRead + strlen(SARA_R5_READ_SOCKET) + minimumResponseAllocation; response = sara_r5_calloc_char(responseLength); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2759,10 +2888,14 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest, int } // Extract the data - char *searchPtr = strstr(response, "+USORD: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USORD: %d,%d", + char *searchPtr = strstr(response, "+USORD:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USORD:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d", &socketStore, &readLength); + } if (scanNum != 2) { if (_printDebug == true) @@ -2802,7 +2935,7 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest, int // Find the first double-quote: strBegin = strchr(searchPtr, '\"'); - if (strBegin == NULL) + if (strBegin == nullptr) { free(command); free(response); @@ -2822,7 +2955,7 @@ SARA_R5_error_t SARA_R5::socketRead(int socket, int length, char *readDest, int _debugPort->println(F("socketRead: success")); // Update *bytesRead - if (bytesRead != NULL) + if (bytesRead != nullptr) *bytesRead = readIndexTotal; // How many bytes are left to read? @@ -2856,12 +2989,12 @@ SARA_R5_error_t SARA_R5::socketReadAvailable(int socket, int *length) int socketStore = 0; command = sara_r5_calloc_char(strlen(SARA_R5_READ_SOCKET) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,0", SARA_R5_READ_SOCKET, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2872,10 +3005,14 @@ SARA_R5_error_t SARA_R5::socketReadAvailable(int socket, int *length) if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USORD: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USORD: %d,%d", + char *searchPtr = strstr(response, "+USORD:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USORD:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d", &socketStore, &readLength); + } if (scanNum != 2) { if (_printDebug == true) @@ -2915,7 +3052,7 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest, int bytesToRead; // Set *bytesRead to zero - if (bytesRead != NULL) + if (bytesRead != nullptr) *bytesRead = 0; // Check if length is zero @@ -2928,14 +3065,14 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest, // Allocate memory for the command command = sara_r5_calloc_char(strlen(SARA_R5_READ_UDP_SOCKET) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; // Allocate memory for the response // We only need enough to read _saraR5maxSocketRead bytes - not the whole thing int responseLength = _saraR5maxSocketRead + strlen(SARA_R5_READ_UDP_SOCKET) + minimumResponseAllocation; response = sara_r5_calloc_char(responseLength); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -2969,11 +3106,15 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest, } // Extract the data - char *searchPtr = strstr(response, "+USORF: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USORF: %d,\"%d.%d.%d.%d\",%d,%d", + char *searchPtr = strstr(response, "+USORF:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USORF:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,\"%d.%d.%d.%d\",%d,%d", &socketStore, &remoteIPstore[0], &remoteIPstore[1], &remoteIPstore[2], &remoteIPstore[3], &portStore, &readLength); + } if (scanNum != 7) { if (_printDebug == true) @@ -3015,7 +3156,7 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest, strBegin = strchr(strBegin + 1, '\"'); strBegin = strchr(strBegin + 1, '\"'); - if (strBegin == NULL) + if (strBegin == nullptr) { free(command); free(response); @@ -3031,8 +3172,8 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest, readIndexThisRead++; } - // If remoteIPaddress is not NULL, copy the remote IP address - if (remoteIPAddress != NULL) + // If remoteIPaddress is not nullptr, copy the remote IP address + if (remoteIPAddress != nullptr) { IPAddress tempAddress; for (int i = 0; i <= 3; i++) @@ -3042,8 +3183,8 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest, *remoteIPAddress = tempAddress; } - // If remotePort is not NULL, copy the remote port - if (remotePort != NULL) + // If remotePort is not nullptr, copy the remote port + if (remotePort != nullptr) { *remotePort = portStore; } @@ -3052,7 +3193,7 @@ SARA_R5_error_t SARA_R5::socketReadUDP(int socket, int length, char *readDest, _debugPort->println(F("socketReadUDP: success")); // Update *bytesRead - if (bytesRead != NULL) + if (bytesRead != nullptr) *bytesRead = readIndexTotal; // How many bytes are left to read? @@ -3086,12 +3227,12 @@ SARA_R5_error_t SARA_R5::socketReadAvailableUDP(int socket, int *length) int socketStore = 0; command = sara_r5_calloc_char(strlen(SARA_R5_READ_UDP_SOCKET) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,0", SARA_R5_READ_UDP_SOCKET, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3102,10 +3243,14 @@ SARA_R5_error_t SARA_R5::socketReadAvailableUDP(int socket, int *length) if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USORF: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USORF: %d,%d", + char *searchPtr = strstr(response, "+USORF:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USORF:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d", &socketStore, &readLength); + } if (scanNum != 2) { if (_printDebug == true) @@ -3133,11 +3278,11 @@ SARA_R5_error_t SARA_R5::socketListen(int socket, unsigned int port) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_LISTEN_SOCKET) + 9); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d", SARA_R5_LISTEN_SOCKET, socket, port); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3150,11 +3295,11 @@ SARA_R5_error_t SARA_R5::socketDirectLinkMode(int socket) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_DIRECT_LINK) + 8); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_SOCKET_DIRECT_LINK, socket); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_CONNECT, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_CONNECT, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3171,11 +3316,11 @@ SARA_R5_error_t SARA_R5::socketDirectLinkTimeTrigger(int socket, unsigned long t char *command; command = sara_r5_calloc_char(strlen(SARA_R5_UD_CONFIGURATION) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=5,%d,%ld", SARA_R5_UD_CONFIGURATION, socket, timerTrigger); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3192,11 +3337,11 @@ SARA_R5_error_t SARA_R5::socketDirectLinkDataLengthTrigger(int socket, int dataL char *command; command = sara_r5_calloc_char(strlen(SARA_R5_UD_CONFIGURATION) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=6,%d,%d", SARA_R5_UD_CONFIGURATION, socket, dataLengthTrigger); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3213,11 +3358,11 @@ SARA_R5_error_t SARA_R5::socketDirectLinkCharacterTrigger(int socket, int charac char *command; command = sara_r5_calloc_char(strlen(SARA_R5_UD_CONFIGURATION) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=7,%d,%d", SARA_R5_UD_CONFIGURATION, socket, characterTrigger); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3234,11 +3379,11 @@ SARA_R5_error_t SARA_R5::socketDirectLinkCongestionTimer(int socket, unsigned lo char *command; command = sara_r5_calloc_char(strlen(SARA_R5_UD_CONFIGURATION) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=8,%d,%ld", SARA_R5_UD_CONFIGURATION, socket, congestionTimer); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3255,12 +3400,12 @@ SARA_R5_error_t SARA_R5::querySocketType(int socket, SARA_R5_socket_protocol_t * int paramVal; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,0", SARA_R5_SOCKET_CONTROL, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3271,10 +3416,14 @@ SARA_R5_error_t SARA_R5::querySocketType(int socket, SARA_R5_socket_protocol_t * if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOCTL: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USOCTL: %d,0,%d", + char *searchPtr = strstr(response, "+USOCTL:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOCTL:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,0,%d", &socketStore, ¶mVal); + } if (scanNum != 2) { if (_printDebug == true) @@ -3307,12 +3456,12 @@ SARA_R5_error_t SARA_R5::querySocketLastError(int socket, int *error) int paramVal; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,1", SARA_R5_SOCKET_CONTROL, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3323,10 +3472,14 @@ SARA_R5_error_t SARA_R5::querySocketLastError(int socket, int *error) if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOCTL: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USOCTL: %d,1,%d", + char *searchPtr = strstr(response, "+USOCTL:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOCTL:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,1,%d", &socketStore, ¶mVal); + } if (scanNum != 2) { if (_printDebug == true) @@ -3358,12 +3511,12 @@ SARA_R5_error_t SARA_R5::querySocketTotalBytesSent(int socket, uint32_t *total) long unsigned int paramVal; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,2", SARA_R5_SOCKET_CONTROL, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3374,10 +3527,14 @@ SARA_R5_error_t SARA_R5::querySocketTotalBytesSent(int socket, uint32_t *total) if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOCTL: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USOCTL: %d,2,%lu", + char *searchPtr = strstr(response, "+USOCTL:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOCTL:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,2,%lu", &socketStore, ¶mVal); + } if (scanNum != 2) { if (_printDebug == true) @@ -3409,12 +3566,12 @@ SARA_R5_error_t SARA_R5::querySocketTotalBytesReceived(int socket, uint32_t *tot long unsigned int paramVal; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,3", SARA_R5_SOCKET_CONTROL, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3425,10 +3582,14 @@ SARA_R5_error_t SARA_R5::querySocketTotalBytesReceived(int socket, uint32_t *tot if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOCTL: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USOCTL: %d,3,%lu", + char *searchPtr = strstr(response, "+USOCTL:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOCTL:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,3,%lu", &socketStore, ¶mVal); + } if (scanNum != 2) { if (_printDebug == true) @@ -3460,12 +3621,12 @@ SARA_R5_error_t SARA_R5::querySocketRemoteIPAddress(int socket, IPAddress *addre int paramVals[5]; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,4", SARA_R5_SOCKET_CONTROL, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3476,12 +3637,16 @@ SARA_R5_error_t SARA_R5::querySocketRemoteIPAddress(int socket, IPAddress *addre if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOCTL: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USOCTL: %d,4,\"%d.%d.%d.%d\",%d", + char *searchPtr = strstr(response, "+USOCTL:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOCTL:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,4,\"%d.%d.%d.%d\",%d", &socketStore, ¶mVals[0], ¶mVals[1], ¶mVals[2], ¶mVals[3], ¶mVals[4]); + } if (scanNum != 6) { if (_printDebug == true) @@ -3516,12 +3681,12 @@ SARA_R5_error_t SARA_R5::querySocketStatusTCP(int socket, SARA_R5_tcp_socket_sta int paramVal; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,10", SARA_R5_SOCKET_CONTROL, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3532,10 +3697,14 @@ SARA_R5_error_t SARA_R5::querySocketStatusTCP(int socket, SARA_R5_tcp_socket_sta if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOCTL: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USOCTL: %d,10,%d", + char *searchPtr = strstr(response, "+USOCTL:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOCTL:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,10,%d", &socketStore, ¶mVal); + } if (scanNum != 2) { if (_printDebug == true) @@ -3567,12 +3736,12 @@ SARA_R5_error_t SARA_R5::querySocketOutUnackData(int socket, uint32_t *total) long unsigned int paramVal; command = sara_r5_calloc_char(strlen(SARA_R5_SOCKET_CONTROL) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,11", SARA_R5_SOCKET_CONTROL, socket); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3583,10 +3752,14 @@ SARA_R5_error_t SARA_R5::querySocketOutUnackData(int socket, uint32_t *total) if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOCTL: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+USOCTL: %d,11,%lu", + char *searchPtr = strstr(response, "+USOCTL:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOCTL:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,11,%lu", &socketStore, ¶mVal); + } if (scanNum != 2) { if (_printDebug == true) @@ -3617,11 +3790,11 @@ int SARA_R5::socketGetLastError() int errorCode = -1; command = sara_r5_calloc_char(64); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3634,9 +3807,13 @@ int SARA_R5::socketGetLastError() if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+USOER: "); - if (searchPtr != NULL) - sscanf(searchPtr, "+USOER: %d", &errorCode); + char *searchPtr = strstr(response, "+USOER:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+USOER:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + sscanf(searchPtr, "%d", &errorCode); + } } free(command); @@ -3659,11 +3836,11 @@ SARA_R5_error_t SARA_R5::resetHTTPprofile(int profile) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_HTTP_PROFILE, profile); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3679,12 +3856,12 @@ SARA_R5_error_t SARA_R5::setHTTPserverIPaddress(int profile, IPAddress address) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 64); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%d.%d.%d.%d\"", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_SERVER_IP, address[0], address[1], address[2], address[3]); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3700,12 +3877,12 @@ SARA_R5_error_t SARA_R5::setHTTPserverName(int profile, String server) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 12 + server.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_SERVER_NAME, server.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3721,12 +3898,12 @@ SARA_R5_error_t SARA_R5::setHTTPusername(int profile, String username) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 12 + username.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_USERNAME, username.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3742,12 +3919,12 @@ SARA_R5_error_t SARA_R5::setHTTPpassword(int profile, String password) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 12 + password.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_PASSWORD, password.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3763,12 +3940,12 @@ SARA_R5_error_t SARA_R5::setHTTPauthentication(int profile, bool authenticate) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,%d", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_AUTHENTICATION, authenticate); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3784,12 +3961,12 @@ SARA_R5_error_t SARA_R5::setHTTPserverPort(int profile, int port) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,%d", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_SERVER_PORT, port); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3805,12 +3982,12 @@ SARA_R5_error_t SARA_R5::setHTTPcustomHeader(int profile, String header) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 12 + header.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_ADD_CUSTOM_HEADERS, header.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3826,7 +4003,7 @@ SARA_R5_error_t SARA_R5::setHTTPsecure(int profile, bool secure, int secprofile) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROFILE) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (secprofile == -1) sprintf(command, "%s=%d,%d,%d", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_SECURE, @@ -3834,7 +4011,7 @@ SARA_R5_error_t SARA_R5::setHTTPsecure(int profile, bool secure, int secprofile) else sprintf(command, "%s=%d,%d,%d,%d", SARA_R5_HTTP_PROFILE, profile, SARA_R5_HTTP_OP_CODE_SECURE, secure, secprofile); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3849,12 +4026,12 @@ SARA_R5_error_t SARA_R5::ping(String remote_host, int retry, int p_size, command = sara_r5_calloc_char(strlen(SARA_R5_PING_COMMAND) + 48 + remote_host.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\",%d,%d,%ld,%d", SARA_R5_PING_COMMAND, remote_host.c_str(), retry, p_size, timeout, ttl); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3871,12 +4048,12 @@ SARA_R5_error_t SARA_R5::sendHTTPGET(int profile, String path, String responseFi command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_COMMAND) + 24 + path.length() + responseFilename.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\",\"%s\"", SARA_R5_HTTP_COMMAND, profile, SARA_R5_HTTP_COMMAND_GET, path.c_str(), responseFilename.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3894,12 +4071,12 @@ SARA_R5_error_t SARA_R5::sendHTTPPOSTdata(int profile, String path, String respo command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_COMMAND) + 24 + path.length() + responseFilename.length() + data.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\",\"%s\",\"%s\",%d", SARA_R5_HTTP_COMMAND, profile, SARA_R5_HTTP_COMMAND_POST_DATA, path.c_str(), responseFilename.c_str(), data.c_str(), httpContentType); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3917,12 +4094,12 @@ SARA_R5_error_t SARA_R5::sendHTTPPOSTfile(int profile, String path, String respo command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_COMMAND) + 24 + path.length() + responseFilename.length() + requestFile.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\",\"%s\",\"%s\",%d", SARA_R5_HTTP_COMMAND, profile, SARA_R5_HTTP_COMMAND_POST_FILE, path.c_str(), responseFilename.c_str(), requestFile.c_str(), httpContentType); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -3938,12 +4115,12 @@ SARA_R5_error_t SARA_R5::getHTTPprotocolError(int profile, int *error_class, int int rprofile, eclass, ecode; command = sara_r5_calloc_char(strlen(SARA_R5_HTTP_PROTOCOL_ERROR) + 4); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_HTTP_PROTOCOL_ERROR, profile); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -3956,9 +4133,13 @@ SARA_R5_error_t SARA_R5::getHTTPprotocolError(int profile, int *error_class, int { int scanned = 0; char *searchPtr = strstr(response, "+UHTTPER:"); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+UHTTPER:%d,%d,%d\r\n", + if (searchPtr != nullptr) + { + searchPtr += strlen("+UHTTPER:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d,%d,%d\r\n", &rprofile, &eclass, &ecode); + } if (scanned == 3) { *error_class = eclass; @@ -3978,10 +4159,10 @@ SARA_R5_error_t SARA_R5::nvMQTT(SARA_R5_mqtt_nv_parameter_t parameter) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_NVM) + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_MQTT_NVM, parameter); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -3992,10 +4173,10 @@ SARA_R5_error_t SARA_R5::setMQTTclientId(String clientId) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_PROFILE) + clientId.length() + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,\"%s\"", SARA_R5_MQTT_PROFILE, SARA_R5_MQTT_PROFILE_CLIENT_ID, clientId.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4006,10 +4187,10 @@ SARA_R5_error_t SARA_R5::setMQTTserver(String serverName, int port) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_PROFILE) + serverName.length() + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,\"%s\",%d", SARA_R5_MQTT_PROFILE, SARA_R5_MQTT_PROFILE_SERVERNAME, serverName.c_str(), port); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4020,11 +4201,11 @@ SARA_R5_error_t SARA_R5::setMQTTsecure(bool secure, int secprofile) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_PROFILE) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (secprofile == -1) sprintf(command, "%s=%d,%d", SARA_R5_MQTT_PROFILE, SARA_R5_MQTT_PROFILE_SECURE, secure); else sprintf(command, "%s=%d,%d,%d", SARA_R5_MQTT_PROFILE, SARA_R5_MQTT_PROFILE_SECURE, secure, secprofile); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4035,10 +4216,10 @@ SARA_R5_error_t SARA_R5::connectMQTT(void) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_COMMAND) + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_MQTT_COMMAND, SARA_R5_MQTT_COMMAND_LOGIN); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4049,10 +4230,10 @@ SARA_R5_error_t SARA_R5::disconnectMQTT(void) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_COMMAND) + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_MQTT_COMMAND, SARA_R5_MQTT_COMMAND_LOGOUT); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4063,10 +4244,10 @@ SARA_R5_error_t SARA_R5::subscribeMQTTtopic(int max_Qos, String topic) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_COMMAND) + 16 + topic.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_MQTT_COMMAND, SARA_R5_MQTT_COMMAND_SUBSCRIBE, max_Qos, topic.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4077,10 +4258,10 @@ SARA_R5_error_t SARA_R5::unsubscribeMQTTtopic(String topic) SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_COMMAND) + 16 + topic.length()); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,\"%s\"", SARA_R5_MQTT_COMMAND, SARA_R5_MQTT_COMMAND_UNSUBSCRIBE, topic.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4095,18 +4276,18 @@ SARA_R5_error_t SARA_R5::readMQTT(int* pQos, String* pTopic, uint8_t *readDest, int total_length, topic_length, data_length; // Set *bytesRead to zero - if (bytesRead != NULL) + if (bytesRead != nullptr) *bytesRead = 0; // Allocate memory for the command command = sara_r5_calloc_char(strlen(SARA_R5_MQTT_COMMAND) + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; // Allocate memory for the response int responseLength = readLength + minimumResponseAllocation; response = sara_r5_calloc_char(responseLength); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -4134,8 +4315,12 @@ SARA_R5_error_t SARA_R5::readMQTT(int* pQos, String* pTopic, uint8_t *readDest, // Extract the data char *searchPtr = strstr(response, "+UMQTTC:"); int cmd = 0; - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+UMQTTC:%d,%d,%d,%d,\"%*[^\"]\",%d,\"", &cmd, pQos, &total_length, &topic_length, &data_length); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UMQTTC:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d,%d,%d,\"%*[^\"]\",%d,\"", &cmd, pQos, &total_length, &topic_length, &data_length); + } if ((scanNum != 5) || (cmd != SARA_R5_MQTT_COMMAND_READ)) { if (_printDebug == true) @@ -4150,14 +4335,14 @@ SARA_R5_error_t SARA_R5::readMQTT(int* pQos, String* pTopic, uint8_t *readDest, err = SARA_R5_ERROR_SUCCESS; searchPtr = strstr(searchPtr, "\""); - if (searchPtr!= NULL) { + if (searchPtr!= nullptr) { 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 (readDest && (searchPtr != nullptr) && (response + responseLength >= searchPtr + data_length + 1) && (searchPtr[data_length + 1] == '"')) { if (data_length > readLength) { data_length = readLength; if (_printDebug == true) { @@ -4188,7 +4373,7 @@ SARA_R5_error_t SARA_R5::getMQTTprotocolError(int *error_code, int *error_code2) int code, code2; response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { return SARA_R5_ERROR_OUT_OF_MEMORY; } @@ -4200,9 +4385,13 @@ SARA_R5_error_t SARA_R5::getMQTTprotocolError(int *error_code, int *error_code2) { int scanned = 0; char *searchPtr = strstr(response, "+UMQTTER:"); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+UMQTTER:%d,%d\r\n", + if (searchPtr != nullptr) + { + searchPtr += strlen("+UMQTTER:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d,%d\r\n", &code, &code2); + } if (scanned == 2) { *error_code = code; @@ -4222,12 +4411,12 @@ SARA_R5_error_t SARA_R5::resetSecurityProfile(int secprofile) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_SEC_PROFILE) + 6); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_SEC_PROFILE, secprofile); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -4240,10 +4429,10 @@ SARA_R5_error_t SARA_R5::configSecurityProfile(int secprofile, SARA_R5_sec_profi char *command; command = sara_r5_calloc_char(strlen(SARA_R5_SEC_PROFILE) + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,%d", SARA_R5_SEC_PROFILE, secprofile,parameter,value); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4254,10 +4443,10 @@ SARA_R5_error_t SARA_R5::configSecurityProfileString(int secprofile, SARA_R5_sec SARA_R5_error_t err; char *command; command = sara_r5_calloc_char(strlen(SARA_R5_SEC_PROFILE) + value.length() + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_SEC_PROFILE, secprofile,parameter,value.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); return err; @@ -4270,10 +4459,10 @@ SARA_R5_error_t SARA_R5::setSecurityManager(SARA_R5_sec_manager_opcode_t opcode, SARA_R5_error_t err; command = sara_r5_calloc_char(strlen(SARA_R5_SEC_MANAGER) + name.length() + 20); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -4321,12 +4510,12 @@ SARA_R5_error_t SARA_R5::setPDPconfiguration(int profile, SARA_R5_pdp_configurat return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_CONFIG) + 24); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,%d", SARA_R5_MESSAGE_PDP_CONFIG, profile, parameter, value); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -4347,12 +4536,12 @@ SARA_R5_error_t SARA_R5::setPDPconfiguration(int profile, SARA_R5_pdp_configurat return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_CONFIG) + 64); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%s\"", SARA_R5_MESSAGE_PDP_CONFIG, profile, parameter, value.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -4368,12 +4557,12 @@ SARA_R5_error_t SARA_R5::setPDPconfiguration(int profile, SARA_R5_pdp_configurat return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_CONFIG) + 64); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d,\"%d.%d.%d.%d\"", SARA_R5_MESSAGE_PDP_CONFIG, profile, parameter, value[0], value[1], value[2], value[3]); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -4389,11 +4578,11 @@ SARA_R5_error_t SARA_R5::performPDPaction(int profile, SARA_R5_pdp_actions_t act return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_ACTION) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d", SARA_R5_MESSAGE_PDP_ACTION, profile, action); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -4409,14 +4598,14 @@ SARA_R5_error_t SARA_R5::activatePDPcontext(bool status, int cid) return SARA_R5_ERROR_ERROR; command = sara_r5_calloc_char(strlen(SARA_R5_MESSAGE_PDP_CONTEXT_ACTIVATE) + 32); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (cid == -1) sprintf(command, "%s=%d", SARA_R5_MESSAGE_PDP_CONTEXT_ACTIVATE, status); else sprintf(command, "%s=%d,%d", SARA_R5_MESSAGE_PDP_CONTEXT_ACTIVATE, status, cid); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -4434,12 +4623,12 @@ SARA_R5_error_t SARA_R5::getNetworkAssignedIPAddress(int profile, IPAddress *add int paramVals[4]; command = sara_r5_calloc_char(strlen(SARA_R5_NETWORK_ASSIGNED_DATA) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d,%d", SARA_R5_NETWORK_ASSIGNED_DATA, profile, paramTag); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -4450,11 +4639,15 @@ SARA_R5_error_t SARA_R5::getNetworkAssignedIPAddress(int profile, IPAddress *add if (err == SARA_R5_ERROR_SUCCESS) { - char *searchPtr = strstr(response, "+UPSND: "); - if (searchPtr != NULL) - scanNum = sscanf(searchPtr, "+UPSND: %d,%d,\"%d.%d.%d.%d\"", + char *searchPtr = strstr(response, "+UPSND:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UPSND:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanNum = sscanf(searchPtr, "%d,%d,\"%d.%d.%d.%d\"", &profileStore, ¶mTag, ¶mVals[0], ¶mVals[1], ¶mVals[2], ¶mVals[3]); + } if (scanNum != 6) { if (_printDebug == true) @@ -4486,12 +4679,12 @@ bool SARA_R5::isGPSon(void) bool on = false; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_POWER) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_GNSS_POWER); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -4505,10 +4698,10 @@ bool SARA_R5::isGPSon(void) // Example response: "+UGPS: 0" for off "+UGPS: 1,0,1" for on // Search for a ':' followed by a '1' or ' 1' char *pch1 = strchr(response, ':'); - if (pch1 != NULL) + if (pch1 != nullptr) { char *pch2 = strchr(response, '1'); - if ((pch2 != NULL) && ((pch2 == pch1 + 1) || (pch2 == pch1 + 2))) + if ((pch2 != nullptr) && ((pch2 == pch1 + 1) || (pch2 == pch1 + 2))) on = true; } } @@ -4534,7 +4727,7 @@ SARA_R5_error_t SARA_R5::gpsPower(bool enable, gnss_system_t gnss_sys, gnss_aidi // GPS power management command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_POWER) + 32); // gnss_sys could be up to three digits - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (enable) { @@ -4545,7 +4738,7 @@ SARA_R5_error_t SARA_R5::gpsPower(bool enable, gnss_system_t gnss_sys, gnss_aidi sprintf(command, "%s=0", SARA_R5_GNSS_POWER); } - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, 10000); + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, 10000); free(command); return err; @@ -4627,11 +4820,11 @@ SARA_R5_error_t SARA_R5::gpsEnableRmc(bool enable) // } command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_GPRMC) + 3); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_GNSS_GPRMC, enable ? 1 : 0); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, SARA_R5_10_SEC_TIMEOUT); + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_10_SEC_TIMEOUT); free(command); return err; @@ -4646,12 +4839,12 @@ SARA_R5_error_t SARA_R5::gpsGetRmc(struct PositionData *pos, struct SpeedData *s char *rmcBegin; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_GPRMC) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_GNSS_GPRMC); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -4662,7 +4855,7 @@ SARA_R5_error_t SARA_R5::gpsGetRmc(struct PositionData *pos, struct SpeedData *s { // Fast-forward response string to $GPRMC starter rmcBegin = strstr(response, "$GPRMC"); - if (rmcBegin == NULL) + if (rmcBegin == nullptr) { err = SARA_R5_ERROR_UNEXPECTED_RESPONSE; } @@ -4712,7 +4905,7 @@ SARA_R5_error_t SARA_R5::gpsRequest(unsigned int timeout, uint32_t accuracy, accuracy = 999999; command = sara_r5_calloc_char(strlen(SARA_R5_GNSS_REQUEST_LOCATION) + 24); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) sprintf(command, "%s=2,%d,%d,%d,%d", SARA_R5_GNSS_REQUEST_LOCATION, @@ -4722,7 +4915,7 @@ SARA_R5_error_t SARA_R5::gpsRequest(unsigned int timeout, uint32_t accuracy, sensor, detailed ? 1 : 0, timeout, accuracy); #endif - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, SARA_R5_10_SEC_TIMEOUT); + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_10_SEC_TIMEOUT); free(command); return err; @@ -4736,14 +4929,14 @@ SARA_R5_error_t SARA_R5::gpsAidingServerConf(const char *primaryServer, const ch char *command; command = sara_r5_calloc_char(strlen(SARA_R5_AIDING_SERVER_CONFIGURATION) + 256); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\",\"%s\",\"%s\",%d,%d,%d,%d,%d,%d", SARA_R5_AIDING_SERVER_CONFIGURATION, primaryServer, secondaryServer, authToken, days, period, resolution, gnssTypes, mode, dataType); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -4759,10 +4952,10 @@ SARA_R5_error_t SARA_R5::appendFileContents(String filename, const char *str, in SARA_R5_error_t err; command = sara_r5_calloc_char(strlen(SARA_R5_FILE_SYSTEM_DOWNLOAD_FILE) + filename.length() + 10); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -4833,12 +5026,12 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, String *contents) } command = sara_r5_calloc_char(strlen(SARA_R5_FILE_SYSTEM_READ_FILE) + filename.length() + 8); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\"", SARA_R5_FILE_SYSTEM_READ_FILE, filename.c_str()); response = sara_r5_calloc_char(fileSize + minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { if (_printDebug == true) { @@ -4872,8 +5065,8 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, String *contents) // Response format: \r\n+URDFILE: "filename",36,"these bytes are the data of the file"\r\n\r\nOK\r\n int scanned = 0; int readFileSize = 0; - char *searchPtr = strstr(response, "+URDFILE: "); - if (searchPtr != NULL) + char *searchPtr = strstr(response, "+URDFILE:"); + if (searchPtr != nullptr) { searchPtr = strchr(searchPtr, '\"'); // Find the first quote searchPtr = strchr(++searchPtr, '\"'); // Find the second quote @@ -4883,7 +5076,7 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, String *contents) { searchPtr = strchr(++searchPtr, '\"'); // Find the third quote - if (searchPtr == NULL) + if (searchPtr == nullptr) { if (_printDebug == true) { @@ -4955,12 +5148,12 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, char *contents) } command = sara_r5_calloc_char(strlen(SARA_R5_FILE_SYSTEM_READ_FILE) + filename.length() + 8); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\"", SARA_R5_FILE_SYSTEM_READ_FILE, filename.c_str()); response = sara_r5_calloc_char(fileSize + minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { if (_printDebug == true) { @@ -4994,8 +5187,8 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, char *contents) // Response format: \r\n+URDFILE: "filename",36,"these bytes are the data of the file"\r\n\r\nOK\r\n int scanned = 0; int readFileSize = 0; - char *searchPtr = strstr(response, "+URDFILE: "); - if (searchPtr != NULL) + char *searchPtr = strstr(response, "+URDFILE:"); + if (searchPtr != nullptr) { searchPtr = strchr(searchPtr, '\"'); // Find the first quote searchPtr = strchr(++searchPtr, '\"'); // Find the second quote @@ -5005,7 +5198,7 @@ SARA_R5_error_t SARA_R5::getFileContents(String filename, char *contents) { searchPtr = strchr(++searchPtr, '\"'); // Find the third quote - if (searchPtr == NULL) + if (searchPtr == nullptr) { if (_printDebug == true) { @@ -5060,12 +5253,12 @@ SARA_R5_error_t SARA_R5::getFileSize(String filename, int *size) char *response; command = sara_r5_calloc_char(strlen(SARA_R5_FILE_SYSTEM_LIST_FILES) + filename.length() + 8); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=2,\"%s\"", SARA_R5_FILE_SYSTEM_LIST_FILES, filename.c_str()); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -5087,8 +5280,8 @@ SARA_R5_error_t SARA_R5::getFileSize(String filename, int *size) return err; } - char *responseStart = strstr(response, "+ULSTFILE: "); - if (responseStart == NULL) + char *responseStart = strstr(response, "+ULSTFILE:"); + if (responseStart == nullptr) { if (_printDebug == true) { @@ -5102,7 +5295,9 @@ SARA_R5_error_t SARA_R5::getFileSize(String filename, int *size) } int fileSize; - sscanf(responseStart, "+ULSTFILE: %d", &fileSize); + responseStart += strlen("+ULSTFILE:"); // Move searchPtr to first char + while (*responseStart == ' ') responseStart++; // skip spaces + sscanf(responseStart, "%d", &fileSize); *size = fileSize; free(command); @@ -5116,11 +5311,11 @@ SARA_R5_error_t SARA_R5::deleteFile(String filename) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_FILE_SYSTEM_DELETE_FILE) + filename.length() + 8); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=\"%s\"", SARA_R5_FILE_SYSTEM_DELETE_FILE, filename.c_str()); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); if (err != SARA_R5_ERROR_SUCCESS) { @@ -5141,12 +5336,12 @@ SARA_R5_error_t SARA_R5::modulePowerOff(void) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_POWER_OFF) + 6); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s", SARA_R5_COMMAND_POWER_OFF); - err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, NULL, + err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, nullptr, SARA_R5_POWER_OFF_TIMEOUT); free(command); @@ -5350,12 +5545,12 @@ SARA_R5_error_t SARA_R5::functionality(SARA_R5_functionality_t function) char *command; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_FUNC) + 16); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s=%d", SARA_R5_COMMAND_FUNC, function); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_3_MIN_TIMEOUT); + nullptr, SARA_R5_3_MIN_TIMEOUT); free(command); @@ -5368,7 +5563,7 @@ SARA_R5_error_t SARA_R5::setMNOprofile(mobile_network_operator_t mno, bool autoR char *command; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_MNO) + 9); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; if (mno == MNO_SIM_ICCID) // Only add autoReset and urcNotification if mno is MNO_SIM_ICCID sprintf(command, "%s=%d,%d,%d", SARA_R5_COMMAND_MNO, (uint8_t)mno, (uint8_t)autoReset, (uint8_t)urcNotification); @@ -5376,7 +5571,7 @@ SARA_R5_error_t SARA_R5::setMNOprofile(mobile_network_operator_t mno, bool autoR sprintf(command, "%s=%d", SARA_R5_COMMAND_MNO, (uint8_t)mno); err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK_OR_ERROR, - NULL, SARA_R5_STANDARD_RESPONSE_TIMEOUT); + nullptr, SARA_R5_STANDARD_RESPONSE_TIMEOUT); free(command); @@ -5395,12 +5590,12 @@ SARA_R5_error_t SARA_R5::getMNOprofile(mobile_network_operator_t *mno) int oStore; command = sara_r5_calloc_char(strlen(SARA_R5_COMMAND_MNO) + 2); - if (command == NULL) + if (command == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; sprintf(command, "%s?", SARA_R5_COMMAND_MNO); response = sara_r5_calloc_char(minimumResponseAllocation); - if (response == NULL) + if (response == nullptr) { free(command); return SARA_R5_ERROR_OUT_OF_MEMORY; @@ -5416,9 +5611,13 @@ SARA_R5_error_t SARA_R5::getMNOprofile(mobile_network_operator_t *mno) } int scanned = 0; - char *searchPtr = strstr(response, "+UMNOPROF: "); - if (searchPtr != NULL) - scanned = sscanf(searchPtr, "+UMNOPROF: %d,%d,%d,%d", &oStore, &d, &r, &u); + char *searchPtr = strstr(response, "+UMNOPROF:"); + if (searchPtr != nullptr) + { + searchPtr += strlen("+UMNOPROF:"); // Move searchPtr to first char + while (*searchPtr == ' ') searchPtr++; // skip spaces + scanned = sscanf(searchPtr, "%d,%d,%d,%d", &oStore, &d, &r, &u); + } o = (mobile_network_operator_t)oStore; if (scanned >= 1) @@ -5456,7 +5655,7 @@ SARA_R5_error_t SARA_R5::waitForResponse(const char *expectedResponse, const cha while ((!found) && ((timeIn + timeout) > millis())) { - if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL + if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is nullptr { char c = readChar(); // if (_printDebug == true) @@ -5493,7 +5692,7 @@ SARA_R5_error_t SARA_R5::waitForResponse(const char *expectedResponse, const cha //that came in while waiting for response. To be processed later within bufferedPoll(). //Note: the expectedResponse or expectedError will also be added to the backlog. //The backlog is only used by bufferedPoll to process the URCs - which are all readable. - //bufferedPoll uses strtok - which does not like NULL characters. + //bufferedPoll uses strtok - which does not like nullptr characters. //So let's make sure no NULLs end up in the backlog! if (_saraResponseBacklogLength < _RXBuffSize) // Don't overflow the buffer { @@ -5537,8 +5736,9 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse( unsigned int charsRead = 0; int responseLen = 0; int errorLen = 0; - const char* expectedError= NULL; - //bool printedSomething = false; + const char* expectedError= nullptr; + bool printResponse = false; // Change to true to print the full response + bool printedSomething = false; if (_printDebug == true) { @@ -5559,19 +5759,19 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse( while ((!found) && ((timeIn + commandTimeout) > millis())) { - if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL + if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is nullptr { char c = readChar(); - // if (_printDebug == true) - // { - // if (printedSomething == false) - // { - // _debugPort->print(F("sendCommandWithResponse: Response: ")); - // printedSomething = true; - // } - // _debugPort->write(c); - // } - if (responseDest != NULL) + if ((printResponse = true) && (_printDebug == true)) + { + if (printedSomething == false) + { + _debugPort->print(F("sendCommandWithResponse: Response: ")); + printedSomething = true; + } + _debugPort->write(c); + } + if (responseDest != nullptr) { if (destIndex < destSize) // Only add this char to response if there is room for it responseDest[destIndex] = c; @@ -5580,11 +5780,11 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse( { if (_printDebug == true) { - // if (printedSomething) - // _debugPort->println(); + if ((printResponse = true) && (printedSomething)) + _debugPort->println(); _debugPort->print(F("sendCommandWithResponse: Panic! responseDest is full!")); - // if (printedSomething) - // _debugPort->print(F("sendCommandWithResponse: Ignored response: ")); + if ((printResponse = true) && (printedSomething)) + _debugPort->print(F("sendCommandWithResponse: Ignored response: ")); } } } @@ -5630,16 +5830,16 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse( } } - // if (_printDebug == true) - // if (printedSomething) - // _debugPort->println(); + if (_printDebug == true) + if ((printResponse = true) && (printedSomething)) + _debugPort->println(); pruneBacklog(); // Prune any incoming non-actionable URC's and responses/errors from the backlog if (found) { - if ((true == _printAtDebug) && ((NULL != responseDest) || (NULL != expectedResponse))) { - _debugAtPort->print((NULL != responseDest) ? responseDest : expectedResponse); + if ((true == _printAtDebug) && ((nullptr != responseDest) || (nullptr != expectedResponse))) { + _debugAtPort->print((nullptr != responseDest) ? responseDest : expectedResponse); } return error ? SARA_R5_ERROR_ERROR : SARA_R5_ERROR_SUCCESS; } @@ -5649,7 +5849,7 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse( } else { - if ((true == _printAtDebug) && (NULL != responseDest)) { + if ((true == _printAtDebug) && (nullptr != responseDest)) { _debugAtPort->print(responseDest); } return SARA_R5_ERROR_UNEXPECTED_RESPONSE; @@ -5721,12 +5921,12 @@ SARA_R5_error_t SARA_R5::parseSocketReadIndication(int socket, int length) return SARA_R5_ERROR_UNEXPECTED_RESPONSE; } - // Return now if both callbacks pointers are NULL - otherwise the data will be read and lost! - if ((_socketReadCallback == NULL) && (_socketReadCallbackPlus == NULL)) + // Return now if both callbacks pointers are nullptr - otherwise the data will be read and lost! + if ((_socketReadCallback == nullptr) && (_socketReadCallbackPlus == nullptr)) return SARA_R5_ERROR_INVALID; readDest = sara_r5_calloc_char(length + 1); - if (readDest == NULL) + if (readDest == nullptr) return SARA_R5_ERROR_OUT_OF_MEMORY; int bytesRead; @@ -5737,7 +5937,7 @@ SARA_R5_error_t SARA_R5::parseSocketReadIndication(int socket, int length) return err; } - if (_socketReadCallback != NULL) + if (_socketReadCallback != nullptr) { String dataAsString = ""; // Create an empty string // Copy the data from readDest into the String in a binary-compatible way @@ -5749,7 +5949,7 @@ SARA_R5_error_t SARA_R5::parseSocketReadIndication(int socket, int length) _socketReadCallback(socket, dataAsString); } - if (_socketReadCallbackPlus != NULL) + if (_socketReadCallbackPlus != nullptr) { IPAddress dummyAddress = { 0, 0, 0, 0 }; int dummyPort = 0; @@ -5772,12 +5972,12 @@ SARA_R5_error_t SARA_R5::parseSocketReadIndicationUDP(int socket, int length) return SARA_R5_ERROR_UNEXPECTED_RESPONSE; } - // Return now if both callbacks pointers are NULL - otherwise the data will be read and lost! - if ((_socketReadCallback == NULL) && (_socketReadCallbackPlus == NULL)) + // Return now if both callbacks pointers are nullptr - otherwise the data will be read and lost! + if ((_socketReadCallback == nullptr) && (_socketReadCallbackPlus == nullptr)) return SARA_R5_ERROR_INVALID; readDest = sara_r5_calloc_char(length + 1); - if (readDest == NULL) + if (readDest == nullptr) { return SARA_R5_ERROR_OUT_OF_MEMORY; } @@ -5790,7 +5990,7 @@ SARA_R5_error_t SARA_R5::parseSocketReadIndicationUDP(int socket, int length) return err; } - if (_socketReadCallback != NULL) + if (_socketReadCallback != nullptr) { String dataAsString = ""; // Create an empty string // Important Note: some implementations of concat, like the one on ESP32, are binary-compatible. @@ -5801,7 +6001,7 @@ SARA_R5_error_t SARA_R5::parseSocketReadIndicationUDP(int socket, int length) _socketReadCallback(socket, dataAsString); } - if (_socketReadCallbackPlus != NULL) + if (_socketReadCallbackPlus != nullptr) { _socketReadCallbackPlus(socket, (const char *)readDest, bytesRead, remoteAddress, remotePort); } @@ -5815,7 +6015,7 @@ SARA_R5_error_t SARA_R5::parseSocketListenIndication(int listeningSocket, IPAddr _lastLocalIP = localIP; _lastRemoteIP = remoteIP; - if (_socketListenCallback != NULL) + if (_socketListenCallback != nullptr) { _socketListenCallback(listeningSocket, localIP, listeningPort, socket, remoteIP, port); } @@ -5828,12 +6028,14 @@ SARA_R5_error_t SARA_R5::parseSocketCloseIndication(String *closeIndication) int search; int socket; - search = closeIndication->indexOf("UUSOCL: ") + strlen("UUSOCL: "); + search = closeIndication->indexOf("+UUSOCL:"); + search += strlen("+UUSOCL:"); + while (closeIndication->charAt(search) == ' ') search ++; // skip spaces // Socket will be first integer, should be single-digit number between 0-6: socket = closeIndication->substring(search, search + 1).toInt(); - if (_socketCloseCallback != NULL) + if (_socketCloseCallback != nullptr) { _socketCloseCallback(socket); } @@ -5843,15 +6045,15 @@ SARA_R5_error_t SARA_R5::parseSocketCloseIndication(String *closeIndication) size_t SARA_R5::hwPrint(const char *s) { - if ((true == _printAtDebug) && (NULL != s)) { + if ((true == _printAtDebug) && (nullptr != s)) { _debugAtPort->print(s); } - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { return _hardSerial->print(s); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { return _softSerial->print(s); } @@ -5862,15 +6064,15 @@ size_t SARA_R5::hwPrint(const char *s) size_t SARA_R5::hwWriteData(const char *buff, int len) { - if ((true == _printAtDebug) && (NULL != buff) && (0 < len) ) { + if ((true == _printAtDebug) && (nullptr != buff) && (0 < len) ) { _debugAtPort->write(buff,len); } - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { return _hardSerial->write((const uint8_t *)buff, len); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { return _softSerial->write((const uint8_t *)buff, len); } @@ -5883,12 +6085,12 @@ size_t SARA_R5::hwWrite(const char c) if (true == _printAtDebug) { _debugAtPort->write(c); } - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { return _hardSerial->write(c); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { return _softSerial->write(c); } @@ -5901,17 +6103,17 @@ int SARA_R5::readAvailable(char *inString) { int len = 0; - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { while (_hardSerial->available()) { char c = (char)_hardSerial->read(); - if (inString != NULL) + if (inString != nullptr) { inString[len++] = c; } } - if (inString != NULL) + if (inString != nullptr) { inString[len] = 0; } @@ -5919,17 +6121,17 @@ int SARA_R5::readAvailable(char *inString) // _debugPort->println(inString); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { while (_softSerial->available()) { char c = (char)_softSerial->read(); - if (inString != NULL) + if (inString != nullptr) { inString[len++] = c; } } - if (inString != NULL) + if (inString != nullptr) { inString[len] = 0; } @@ -5943,12 +6145,12 @@ char SARA_R5::readChar(void) { char ret = 0; - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { ret = (char)_hardSerial->read(); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { ret = (char)_softSerial->read(); } @@ -5959,12 +6161,12 @@ char SARA_R5::readChar(void) int SARA_R5::hwAvailable(void) { - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { return _hardSerial->available(); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { return _softSerial->available(); } @@ -5976,13 +6178,13 @@ int SARA_R5::hwAvailable(void) void SARA_R5::beginSerial(unsigned long baud) { delay(100); - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { _hardSerial->end(); _hardSerial->begin(baud); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { _softSerial->end(); _softSerial->begin(baud); @@ -5993,12 +6195,12 @@ void SARA_R5::beginSerial(unsigned long baud) void SARA_R5::setTimeout(unsigned long timeout) { - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { _hardSerial->setTimeout(timeout); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { _softSerial->setTimeout(timeout); } @@ -6008,12 +6210,12 @@ void SARA_R5::setTimeout(unsigned long timeout) bool SARA_R5::find(char *target) { bool found = false; - if (_hardSerial != NULL) + if (_hardSerial != nullptr) { found = _hardSerial->find(target); } #ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED - else if (_softSerial != NULL) + else if (_softSerial != nullptr) { found = _softSerial->find(target); } @@ -6071,28 +6273,28 @@ void SARA_R5::pruneBacklog() char *preservedEvent; event = strtok_r(_saraResponseBacklog, "\r\n", &preservedEvent); // Look for an 'event' - something ending in \r\n - while (event != NULL) //If event is actionable, add it to pruneBuffer. + while (event != nullptr) //If event is actionable, add it to pruneBuffer. { // These are the events we want to keep so they can be processed by poll / bufferedPoll - if ((strstr(event, "+UUSORD:") != NULL) - || (strstr(event, "+UUSORF:") != NULL) - || (strstr(event, "+UUSOLI:") != NULL) - || (strstr(event, "+UUSOCL:") != NULL) - || (strstr(event, "+UULOC:") != NULL) - || (strstr(event, "+UUSIMSTAT:") != NULL) - || (strstr(event, "+UUPSDA:") != NULL) - || (strstr(event, "+UUPING:") != NULL) - || (strstr(event, "+UUMQTTC:") != NULL) - || (strstr(event, "+UUCREG:") != NULL) - || (strstr(event, "+UUCEREG:") != NULL) - || (strstr(event, "+UUHTTPCR:") != NULL)) + if ((strstr(event, "+UUSORD:") != nullptr) + || (strstr(event, "+UUSORF:") != nullptr) + || (strstr(event, "+UUSOLI:") != nullptr) + || (strstr(event, "+UUSOCL:") != nullptr) + || (strstr(event, "+UULOC:") != nullptr) + || (strstr(event, "+UUSIMSTAT:") != nullptr) + || (strstr(event, "+UUPSDA:") != nullptr) + || (strstr(event, "+UUHTTPCR:") != nullptr) + || (strstr(event, "+UUMQTTC:") != nullptr) + || (strstr(event, "+UUPING:") != nullptr) + || (strstr(event, "+CREG:") != nullptr) + || (strstr(event, "+CEREG:") != nullptr)) { strcat(_pruneBuffer, event); // The URCs are all readable text so using strcat is OK strcat(_pruneBuffer, "\r\n"); // strtok blows away delimiter, but we want that for later. _saraResponseBacklogLength += strlen(event) + 2; // Add the length of this event to _saraResponseBacklogLength } - event = strtok_r(NULL, "\r\n", &preservedEvent); // Walk though any remaining events + event = strtok_r(nullptr, "\r\n", &preservedEvent); // Walk though any remaining events } memset(_saraResponseBacklog, 0, _RXBuffSize); //Clear out backlog buffer. @@ -6127,7 +6329,7 @@ char *SARA_R5::readDataUntil(char *destination, unsigned int destSize, strEnd = strchr(source, delimiter); - if (strEnd != NULL) + if (strEnd != nullptr) { len = strEnd - source; memset(destination, 0, destSize); @@ -6158,7 +6360,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find time: search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); // Next comma should be present and not the next position - if ((search != NULL) && (search != ptr)) + if ((search != nullptr) && (search != ptr)) { pos->utc = atof(tempData); // Extract hhmmss.ss as float tTemp = pos->utc; // Convert to unsigned long (discard the digits beyond the decimal point) @@ -6181,7 +6383,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find status character: search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); // Should be a single character: V = Data invalid, A = Data valid - if ((search != NULL) && (search == ptr + 1)) + if ((search != nullptr) && (search == ptr + 1)) { pos->status = *ptr; // Assign char at ptr to status } @@ -6193,7 +6395,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find latitude: search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search != ptr)) + if ((search != nullptr) && (search != ptr)) { pos->lat = atof(tempData); // Extract ddmm.mmmmm as float unsigned long lat_deg = pos->lat / 100; // Extract the degrees @@ -6209,7 +6411,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find latitude hemishpere search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search == ptr + 1)) + if ((search != nullptr) && (search == ptr + 1)) { if (*ptr == 'S') // Is the latitude South pos->lat *= -1.0; // Make lat negative @@ -6218,7 +6420,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find longitude: search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search != ptr)) + if ((search != nullptr) && (search != ptr)) { pos->lon = atof(tempData); // Extract dddmm.mmmmm as float unsigned long lon_deg = pos->lon / 100; // Extract the degrees @@ -6234,7 +6436,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find longitude hemishpere search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search == ptr + 1)) + if ((search != nullptr) && (search == ptr + 1)) { if (*ptr == 'W') // Is the longitude West pos->lon *= -1.0; // Make lon negative @@ -6243,7 +6445,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find speed search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search != ptr)) + if ((search != nullptr) && (search != ptr)) { spd->speed = atof(tempData); // Extract speed over ground in knots spd->speed *= 0.514444; // Convert to m/s @@ -6256,7 +6458,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find course over ground search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search != ptr)) + if ((search != nullptr) && (search != ptr)) { spd->cog = atof(tempData); } @@ -6268,7 +6470,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find date search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search != ptr)) + if ((search != nullptr) && (search != ptr)) { tTemp = atol(tempData); clk->date.day = tTemp / 10000; @@ -6287,7 +6489,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find magnetic variation in degrees: search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search != ptr)) + if ((search != nullptr) && (search != ptr)) { spd->magVar = atof(tempData); } @@ -6299,7 +6501,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Find magnetic variation direction search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ','); - if ((search != NULL) && (search == ptr + 1)) + if ((search != nullptr) && (search == ptr + 1)) { if (*ptr == 'W') // Is the magnetic variation West spd->magVar *= -1.0; // Make magnetic variation negative @@ -6310,7 +6512,7 @@ bool SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos, // Possible values for posMode: N = No fix, E = Estimated/Dead reckoning fix, A = Autonomous GNSS fix, // D = Differential GNSS fix, F = RTK float, R = RTK fixed search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, '*'); - if ((search != NULL) && (search = ptr + 1)) + if ((search != nullptr) && (search = ptr + 1)) { pos->mode = *ptr; } diff --git a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h index 1a6dd7b..ed4f743 100644 --- a/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h +++ b/src/SparkFun_u-blox_SARA-R5_Arduino_Library.h @@ -98,7 +98,7 @@ const char SARA_R5_COMMAND_MANU_ID[] = "+CGMI"; // Manufacturer identification const char SARA_R5_COMMAND_MODEL_ID[] = "+CGMM"; // Model identification const char SARA_R5_COMMAND_FW_VER_ID[] = "+CGMR"; // Firmware version identification const char SARA_R5_COMMAND_SERIAL_NO[] = "+CGSN"; // Product serial number -const char SARA_R5_COMMAND_IMEI[] = "+CSN"; // IMEI identification +const char SARA_R5_COMMAND_IMEI[] = "+GSN"; // IMEI identification const char SARA_R5_COMMAND_IMSI[] = "+CIMI"; // IMSI identification const char SARA_R5_COMMAND_CCID[] = "+CCID"; // SIM CCID const char SARA_R5_COMMAND_REQ_CAP[] = "+GCAP"; // Request capabilities list @@ -189,7 +189,7 @@ const char SARA_R5_SEC_MANAGER[] = "+USECMNG"; const char SARA_R5_RESPONSE_OK[] = "\nOK\r\n"; const char SARA_R5_RESPONSE_ERROR[] = "\nERROR\r\n"; const char SARA_R5_RESPONSE_CONNECT[] = "\r\nCONNECT\r\n"; -#define SARA_R5_RESPONSE_OK_OR_ERROR NULL +#define SARA_R5_RESPONSE_OK_OR_ERROR nullptr // CTRL+Z and ESC ASCII codes for SMS message sends const char ASCII_CTRL_Z = 0x1A; @@ -681,7 +681,7 @@ class SARA_R5 : public Print PDP_TYPE_IPV6 = 3 } SARA_R5_pdp_type; SARA_R5_error_t setAPN(String apn, uint8_t cid = 1, SARA_R5_pdp_type pdpType = PDP_TYPE_IP); // Set the Access Point Name - SARA_R5_error_t getAPN(int cid, String *apn, IPAddress *ip, SARA_R5_pdp_type* pdpType = NULL); // Return the apn and IP address for the chosen context identifier + SARA_R5_error_t getAPN(int cid, String *apn, IPAddress *ip, SARA_R5_pdp_type* pdpType = nullptr); // Return the apn and IP address for the chosen context identifier SARA_R5_error_t getSimStatus(String* code); SARA_R5_error_t setSimPin(String pin); @@ -798,15 +798,15 @@ class SARA_R5 : public Print // Call socketReadAvailable first to determine how much data is available - or use the callbacks (triggered by URC's) // Works for both TCP and UDP - but socketReadUDP is preferred for UDP as it records the remote IP Address and port // bytesRead - if provided - will be updated with the number of bytes actually read. This could be less than length! - SARA_R5_error_t socketRead(int socket, int length, char *readDest, int *bytesRead = NULL); + SARA_R5_error_t socketRead(int socket, int length, char *readDest, int *bytesRead = nullptr); // Return the number of bytes available (waiting to be read) on the chosen socket // Uses +USORD. Valid for both TCP and UDP sockets - but socketReadAvailableUDP is preferred for UDP SARA_R5_error_t socketReadAvailable(int socket, int *length); // Read data from the specified UDP port // Call socketReadAvailableUDP first to determine how much data is available - or use the callbacks (triggered by URC's) - // The remote IP Address and port are returned via *remoteIPAddress and *remotePort (if not NULL) + // The remote IP Address and port are returned via *remoteIPAddress and *remotePort (if not nullptr) // bytesRead - if provided - will be updated with the number of bytes actually read. This could be less than length! - SARA_R5_error_t socketReadUDP(int socket, int length, char *readDest, IPAddress *remoteIPAddress = NULL, int *remotePort = NULL, int *bytesRead = NULL); + SARA_R5_error_t socketReadUDP(int socket, int length, char *readDest, IPAddress *remoteIPAddress = nullptr, int *remotePort = nullptr, int *bytesRead = nullptr); // Return the number of bytes available (waiting to be read) on the chosen UDP socket SARA_R5_error_t socketReadAvailableUDP(int socket, int *length); // Start listening for a connection on the specified port. The connection is reported via the socket listen callback