diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index fdca6918..7d5baa5b 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -449,12 +449,12 @@ class TinyGsmSim7000 : public TinyGsmSim70xx, size_t modemGetAvailable(uint8_t mux) { - // Reset sock_available on all sockets - for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { - GsmClientSim7000* isock = sockets[muxNo]; - if (isock) { - isock->sock_available = 0; - } + if (sockets[mux]) { + sockets[mux]->sock_connected = modemGetConnected(mux); + } + + if (!sockets[mux]->sock_connected) { + return 0; } sendAT(GF("+CIPRXGET=4,"), mux); @@ -470,9 +470,7 @@ class TinyGsmSim7000 : public TinyGsmSim70xx, break; } // DBG("### Available:", result, "on", mux); - if (!result) { - sockets[mux]->sock_connected = modemGetConnected(mux); - } + sockets[mux]->sock_available = result; return result; } diff --git a/src/TinyGsmClientSIM7000SSL.h b/src/TinyGsmClientSIM7000SSL.h index 91dbf790..2594dee7 100644 --- a/src/TinyGsmClientSIM7000SSL.h +++ b/src/TinyGsmClientSIM7000SSL.h @@ -570,68 +570,35 @@ class TinyGsmSim7000SSL } size_t modemGetAvailable(uint8_t mux) { - // 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; - } - - // Reset sock_available on all sockets + // Reset all sock_available values to 0 for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { - GsmClientSim7000SSL* isock = sockets[muxNo]; - if (isock) { - isock->sock_available = 0; + GsmClientSim7000SSL* sock = sockets[muxNo]; + if (sock) { + sock->sock_available = 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. + + // Request awaiting data, will return zero or more +CARECV: answers, ending with an OK 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; - } - } - 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_available = 0; + + while (int result = waitResponse(3000, GF("+CARECV:"), GFP(GSM_OK), GFP(GSM_ERROR))) { + if (result == 1) { + // if we get the +CARECV: response, read the mux number and the number of + // characters available + int ret_mux = streamGetIntBefore(','); + size_t charsAvailable = streamGetIntBefore('\n'); + if (0 <= ret_mux && ret_mux < TINY_GSM_MUX_COUNT) { + GsmClientSim7000SSL* sock = sockets[ret_mux]; + if (sock) { + sock->sock_available = charsAvailable; } } - break; } else { - // if we got an error, give up + // Ok, timeout or Error -> we're done 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(mux); // check the state of all connections if (!sockets[mux]) { return 0;