From 207b939d01f05400e6727196dbbbc6a6adf4e023 Mon Sep 17 00:00:00 2001 From: Bascy Date: Sat, 29 Jan 2022 16:20:15 +0100 Subject: [PATCH 1/3] Fixed modemGetAvailable() and modemGetConnected() when socket[0] is not connected Both methods update status of all connections, but guards at the beginning would return 0 when socket[0] was not connected --- src/TinyGsmClientSIM7000SSL.h | 123 +++++++++++++++++----------------- 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/src/TinyGsmClientSIM7000SSL.h b/src/TinyGsmClientSIM7000SSL.h index 2f88e893..4a2b958e 100644 --- a/src/TinyGsmClientSIM7000SSL.h +++ b/src/TinyGsmClientSIM7000SSL.h @@ -485,63 +485,74 @@ class TinyGsmSim7000SSL } size_t modemGetAvailable(uint8_t mux) { - // If the socket doesn't exist, just return - if (!sockets[mux]) { return 0; } - // We need to check if there are any connections open *before* checking for - // available characters. The SIM7000 *will crash* if you ask about data - // when there are no open connections. - if (!modemGetConnected(mux)) { return 0; } - // NOTE: This gets how many characters are available on all connections that - // have data. It does not return all the connections, just those with data. - sendAT(GF("+CARECV?")); - for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { - // after the last connection, there's an ok, so we catch it right away - int res = waitResponse(3000, GF("+CARECV:"), GFP(GSM_OK), GFP(GSM_ERROR)); - // if we get the +CARECV: response, read the mux number and the number of - // characters available - if (res == 1) { - int ret_mux = streamGetIntBefore(','); - size_t result = streamGetIntBefore('\n'); - GsmClientSim7000SSL* sock = sockets[ret_mux]; - if (sock) { sock->sock_available = result; } - // if the first returned mux isn't 0 (or is higher than expected) - // we need to fill in the missing muxes - if (ret_mux > muxNo) { - for (int extra_mux = muxNo; extra_mux < ret_mux; extra_mux++) { - GsmClientSim7000SSL* isock = sockets[extra_mux]; - if (isock) { isock->sock_available = 0; } + + bool anySocketConnected = false; + // Reset sock_available on all sockets + for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { + GsmClientSim7000SSL* isock = sockets[muxNo]; + if (isock) { + isock->sock_available = 0; + anySocketConnected |= isock->sock_connected; + } + } + + if (!anySocketConnected) { + log_w("No sockets connected"); + return 0; + } + + // NOTE: This gets how many characters are available on all connections that + // have data. It does not return all the connections, just those with data. + sendAT(GF("+CARECV?")); + for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { + // after the last connection, there's an ok, so we catch it right away + int res = waitResponse(3000, GF("+CARECV:"), GFP(GSM_OK), GFP(GSM_ERROR)); + // if we get the +CARECV: response, read the mux number and the number of + // characters available + if (res == 1) { + int ret_mux = streamGetIntBefore(','); + size_t result = streamGetIntBefore('\n'); + GsmClientSim7000SSL* sock = sockets[ret_mux]; + if (sock) { + sock->sock_available = result; } - muxNo = ret_mux; + } else if (res == 3) { + // if we got an error, give up + break; } - } else if (res == 2) { - // if we get an OK, we've reached the last socket with available data - // so we set any we haven't gotten to yet to 0 - for (int extra_mux = muxNo; extra_mux < TINY_GSM_MUX_COUNT; - extra_mux++) { - GsmClientSim7000SSL* isock = sockets[extra_mux]; - if (isock) { isock->sock_available = 0; } + // Should be a final OK at the end. + // If every connection was returned, catch the OK here. + // If only a portion were returned, catch it above. + if (muxNo == TINY_GSM_MUX_COUNT - 1) { + waitResponse(); } - break; - } else { - // if we got an error, give up - break; } - // Should be a final OK at the end. - // If every connection was returned, catch the OK here. - // If only a portion were returned, catch it above. - if (muxNo == TINY_GSM_MUX_COUNT - 1) { waitResponse(); } + modemGetConnected(0); // check the state of all connections, discard result of mux 0 (arbitrary) + if (!sockets[mux]) { + return 0; + } + return sockets[mux]->sock_available; } + modemGetConnected(mux); // check the state of all connections if (!sockets[mux]) { return 0; } return sockets[mux]->sock_available; } - bool modemGetConnected(uint8_t mux) { + bool modemGetConnected(uint8_t mux) { + + // Reset sock_connected on all sockets + for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { + GsmClientSim7000SSL* isock = sockets[muxNo]; + if (isock) { + isock->sock_connected = false; + } + } // NOTE: This gets the state of all connections that have been opened // since the last connection sendAT(GF("+CASTATE?")); - for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { + for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { // after the last connection, there's an ok, so we catch it right away int res = waitResponse(3000, GF("+CASTATE:"), GFP(GSM_OK), GFP(GSM_ERROR)); @@ -553,33 +564,19 @@ class TinyGsmSim7000SSL // 1: Connected to remote server // 2: Listening (server mode) GsmClientSim7000SSL* sock = sockets[ret_mux]; - if (sock) { sock->sock_connected = (status == 1); } - // if the first returned mux isn't 0 (or is higher than expected) - // we need to fill in the missing muxes - if (ret_mux > muxNo) { - for (int extra_mux = muxNo; extra_mux < ret_mux; extra_mux++) { - GsmClientSim7000SSL* isock = sockets[extra_mux]; - if (isock) { isock->sock_connected = false; } - } - muxNo = ret_mux; - } - } else if (res == 2) { - // if we get an OK, we've reached the last socket with available data - // so we set any we haven't gotten to yet to 0 - for (int extra_mux = muxNo; extra_mux < TINY_GSM_MUX_COUNT; - extra_mux++) { - GsmClientSim7000SSL* isock = sockets[extra_mux]; - if (isock) { isock->sock_connected = false; } + if (sock) { + sock->sock_connected = (status == 1); } - break; - } else { + } else if (res == 3) { // if we got an error, give up break; } // Should be a final OK at the end. // If every connection was returned, catch the OK here. // If only a portion were returned, catch it above. - if (muxNo == TINY_GSM_MUX_COUNT - 1) { waitResponse(); } + if (muxNo == TINY_GSM_MUX_COUNT - 1) { + waitResponse(); + } } return sockets[mux]->sock_connected; } From 3e29ebee90166c0398ae6bfa9c79cc852c0fdea4 Mon Sep 17 00:00:00 2001 From: Bascy Date: Sat, 29 Jan 2022 16:23:04 +0100 Subject: [PATCH 2/3] Removed log_w() --- src/TinyGsmClientSIM7000SSL.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TinyGsmClientSIM7000SSL.h b/src/TinyGsmClientSIM7000SSL.h index 4a2b958e..a2069937 100644 --- a/src/TinyGsmClientSIM7000SSL.h +++ b/src/TinyGsmClientSIM7000SSL.h @@ -497,7 +497,6 @@ class TinyGsmSim7000SSL } if (!anySocketConnected) { - log_w("No sockets connected"); return 0; } From d70c14dc71402559412b526d43fb690ebde274dd Mon Sep 17 00:00:00 2001 From: Bascy Date: Sat, 29 Jan 2022 16:43:04 +0100 Subject: [PATCH 3/3] Removed socket connected check --- src/TinyGsmClientSIM7000SSL.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/TinyGsmClientSIM7000SSL.h b/src/TinyGsmClientSIM7000SSL.h index a2069937..47fbab10 100644 --- a/src/TinyGsmClientSIM7000SSL.h +++ b/src/TinyGsmClientSIM7000SSL.h @@ -486,20 +486,14 @@ class TinyGsmSim7000SSL size_t modemGetAvailable(uint8_t mux) { - bool anySocketConnected = false; // Reset sock_available on all sockets for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { GsmClientSim7000SSL* isock = sockets[muxNo]; if (isock) { isock->sock_available = 0; - anySocketConnected |= isock->sock_connected; } } - if (!anySocketConnected) { - return 0; - } - // NOTE: This gets how many characters are available on all connections that // have data. It does not return all the connections, just those with data. sendAT(GF("+CARECV?"));