From 7bbe045c78702ddc8a90c7206911b21172f3790a Mon Sep 17 00:00:00 2001 From: Sara Damiano Date: Wed, 8 May 2019 18:25:49 -0400 Subject: [PATCH] Trying to work better with the "async" close on the UBLOX SARA R410 --- src/TinyGsmClientA6.h | 2 +- src/TinyGsmClientBG96.h | 2 +- src/TinyGsmClientESP8266.h | 4 ++-- src/TinyGsmClientM590.h | 2 +- src/TinyGsmClientM95.h | 2 +- src/TinyGsmClientMC60.h | 2 +- src/TinyGsmClientSIM7000.h | 2 +- src/TinyGsmClientSIM800.h | 2 +- src/TinyGsmClientUBLOX.h | 29 ++++++++++++++++++++--------- 9 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/TinyGsmClientA6.h b/src/TinyGsmClientA6.h index 6f892a5..b90d1c2 100644 --- a/src/TinyGsmClientA6.h +++ b/src/TinyGsmClientA6.h @@ -75,7 +75,7 @@ public: if (sock_connected) { mux = newMux; at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems } return sock_connected; diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index e24d718..281689c 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -76,7 +76,7 @@ public: sock_connected = at->modemConnect(host, port, mux); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } diff --git a/src/TinyGsmClientESP8266.h b/src/TinyGsmClientESP8266.h index a18dfbd..9f8e333 100644 --- a/src/TinyGsmClientESP8266.h +++ b/src/TinyGsmClientESP8266.h @@ -73,7 +73,7 @@ public: sock_connected = at->modemConnect(host, port, mux); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } @@ -191,7 +191,7 @@ public: sock_connected = at->modemConnect(host, port, mux, true); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } diff --git a/src/TinyGsmClientM590.h b/src/TinyGsmClientM590.h index 7eeb917..c16b9c0 100644 --- a/src/TinyGsmClientM590.h +++ b/src/TinyGsmClientM590.h @@ -73,7 +73,7 @@ public: sock_connected = at->modemConnect(host, port, mux); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } diff --git a/src/TinyGsmClientM95.h b/src/TinyGsmClientM95.h index 170f764..9027215 100644 --- a/src/TinyGsmClientM95.h +++ b/src/TinyGsmClientM95.h @@ -76,7 +76,7 @@ public: sock_connected = at->modemConnect(host, port, mux); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } diff --git a/src/TinyGsmClientMC60.h b/src/TinyGsmClientMC60.h index 583a9b7..591355a 100644 --- a/src/TinyGsmClientMC60.h +++ b/src/TinyGsmClientMC60.h @@ -80,7 +80,7 @@ public: sock_connected = at->modemConnect(host, port, mux); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 90634aa..fd0cbd6 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -85,7 +85,7 @@ public: sock_connected = at->modemConnect(host, port, mux); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } diff --git a/src/TinyGsmClientSIM800.h b/src/TinyGsmClientSIM800.h index 13b94b4..e613d0d 100644 --- a/src/TinyGsmClientSIM800.h +++ b/src/TinyGsmClientSIM800.h @@ -82,7 +82,7 @@ public: sock_connected = at->modemConnect(host, port, mux); // sock_connected = at->modemConnect(host, port, &mux); // at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems return sock_connected; } diff --git a/src/TinyGsmClientUBLOX.h b/src/TinyGsmClientUBLOX.h index 4d4e0f9..9fefc3d 100644 --- a/src/TinyGsmClientUBLOX.h +++ b/src/TinyGsmClientUBLOX.h @@ -75,7 +75,13 @@ public: // If we're creating a new connection on the same client, we need to wait // until the async close has finished on Cat-M modems. // After close has completed, the +UUSOCL should appear. - if (at->isCatM && sock_connected) { + // Without this wait, there might be unexpected behaviors when the same + // client instance attempts to move from one socket to another. + // This is only a problem for the LTE-M modules that take painfully long + // to open and close sockets. For those modules, when connecting to multple + // locations, remember to create multiple clients with different mux numbers. + // TODO: Re-evaluate this! + if (at->isCatM && at->modemGetConnected(mux)) { DBG("Waiting for +UUSOCL URC on", mux); for (unsigned long start = millis(); millis() - start < 120000L; ) { at->maintain(); @@ -87,7 +93,7 @@ public: // sock_connected = at->modemConnect(host, port, mux); sock_connected = at->modemConnect(host, port, &mux); at->sockets[mux] = this; - // ^^ TODO: attach the socet after attempting connection or above at init? + // ^^ TODO: attach the socket after attempting connection or above at init? // Currently done inconsistently between modems at->maintain(); return sock_connected; @@ -120,9 +126,6 @@ public: at->maintain(); } at->modemDisconnect(mux); - // We don't actually know if the CatM modem has finished closing because - // we're using an "asynchronous" close - if (!at->isCatM) sock_connected = false; } virtual size_t write(const uint8_t *buf, size_t size) { @@ -780,16 +783,24 @@ protected: bool modemDisconnect(uint8_t mux) { TINY_GSM_YIELD(); - if (!modemGetConnected(mux)) return true; + if (!modemGetConnected(mux)) { + sockets[mux]->sock_connected = false; + return true; + } + bool success; if (isCatM) { // These modems allow a faster "asynchronous" close sendAT(GF("+USOCL="), mux, GF(",1")); - int rsp = waitResponse(120000L); - return (1 == rsp); // but it still can take up to 120s to get a response + success = 1 == waitResponse(120000L); // but it still can take up to 120s to get a response + // TODO: Evaluate whether the speed bump by allowing the async close is worth it } else { // no async close sendAT(GF("+USOCL="), mux); - return (1 == waitResponse()); + success = 1 == waitResponse(); // others should return within 1s + if (success) { + sockets[mux]->sock_connected = false; + } } + return success; } int16_t modemSend(const void* buff, size_t len, uint8_t mux) {