diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index 2e567e3e..c44c2215 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -278,25 +278,26 @@ TINY_GSM_MODEM_MAINTAIN_LISTEN() bool isNetworkConnected() { RegStatus s = getRegistrationStatus(); - return (s == REG_OK_IP || s == REG_OK_TCP); - } - - bool waitForNetwork(unsigned long timeout_ms = 60000L) { - for (unsigned long start = millis(); millis() - start < timeout_ms; ) { - sendAT(GF("+CIPSTATUS")); - int res1 = waitResponse(3000, GF("busy p..."), GF("STATUS:")); - if (res1 == 2) { - int res2 = waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5")); - if (res2 == 2 || res2 == 3) { - waitResponse(); - return true; - } - } - delay(250); + if (s == REG_OK_IP || s == REG_OK_TCP) { + // with these, we're definitely connected + return true; + } + else if (s == REG_OK_NO_TCP) { + // with this, we may or may not be connected + if (getLocalIP() == "") { + return false; + } + else { + return true; + } + } + else { + return false; } - return false; } + TINY_GSM_MODEM_WAIT_FOR_NETWORK() + /* * WiFi functions */ @@ -386,8 +387,37 @@ TINY_GSM_MODEM_MAINTAIN_LISTEN() } bool modemGetConnected(uint8_t mux) { - RegStatus s = getRegistrationStatus(); - return (s == REG_OK_IP || s == REG_OK_TCP); + sendAT(GF("+CIPSTATUS")); + if (waitResponse(3000, GF("STATUS:")) != 1) return REG_UNKNOWN; + int status = + waitResponse(GFP(GSM_ERROR), GF("2"), GF("3"), GF("4"), GF("5")); + if (status != 3) { + // if the status is anything but 3, there are no connections open + waitResponse(); // Returns an OK after the status + for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { + sockets[muxNo]->sock_connected = false; + } + return false; + } + bool verified_connections[TINY_GSM_MUX_COUNT] = {0, 0, 0, 0, 0}; + for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { + uint8_t has_status = waitResponse(GF("+CIPSTATUS:"), GFP(GSM_OK), GFP(GSM_ERROR)); + if (has_status == 1) { + size_t returned_mux = stream.readStringUntil(',').toInt(); + streamSkipUntil(','); // Skip mux + streamSkipUntil(','); // Skip type + streamSkipUntil(','); // Skip remote IP + streamSkipUntil(','); // Skip remote port + streamSkipUntil(','); // Skip local port + streamSkipUntil('\n'); // Skip client/server type + verified_connections[returned_mux] = 1; + } + if (has_status == 2) break; // once we get to the ok, stop + } + for (int muxNo = 0; muxNo < TINY_GSM_MUX_COUNT; muxNo++) { + sockets[muxNo]->sock_connected = verified_connections[muxNo]; + } + return verified_connections[mux]; } public: