From eb9c1f77865f3543a5b556c49f732c81dbb742af Mon Sep 17 00:00:00 2001 From: Fabrice Fontaine Date: Fri, 5 Mar 2021 16:47:09 +0100 Subject: [PATCH 01/12] src/TinyGsmClientSequansMonarch.h: fix connection handling There is an issue in the handling of +SQNSS answer: the sock->sock_connected state will always be set to the state of the last socket. This will prevent any communication. Fix this issue by checking that muxNo is equal to mux before updating. Signed-off-by: Fabrice Fontaine --- src/TinyGsmClientSequansMonarch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TinyGsmClientSequansMonarch.h b/src/TinyGsmClientSequansMonarch.h index 855c166..dc782ee 100644 --- a/src/TinyGsmClientSequansMonarch.h +++ b/src/TinyGsmClientSequansMonarch.h @@ -597,7 +597,7 @@ class TinyGsmSequansMonarch // SOCK_INCOMING = 5, // SOCK_OPENING = 6, GsmClientSequansMonarch* sock = sockets[mux % TINY_GSM_MUX_COUNT]; - if (sock) { + if (sock && muxNo == mux) { sock->sock_connected = ((status != SOCK_CLOSED) && (status != SOCK_INCOMING) && (status != SOCK_OPENING)); From f20293905f6b5c9125a6a7c34ab22dd32f0687c7 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Tue, 9 Mar 2021 12:27:42 +0100 Subject: [PATCH 02/12] SIM7000: added methods to get some connection preferences, plus the current connection technology --- src/TinyGsmClientSIM7000.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 0934bf4..68a57d3 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -262,6 +262,7 @@ class TinyGsmSim7000 : public TinyGsmModem, public: String getNetworkModes() { + // Get the help string, not the setting value sendAT(GF("+CNMP=?")); if (waitResponse(GF(GSM_NL "+CNMP:")) != 1) { return ""; } String res = stream.readStringUntil('\n'); @@ -269,6 +270,14 @@ class TinyGsmSim7000 : public TinyGsmModem, return res; } + bool getNetworkMode(int16_t & mode) { + sendAT(GF("+CNMP?")); + if (waitResponse(GF(GSM_NL "+CNMP:")) != 1) { return false; } + mode = streamGetIntBefore('\n'); + waitResponse(); + return true; + } + String setNetworkMode(uint8_t mode) { sendAT(GF("+CNMP="), mode); if (waitResponse(GF(GSM_NL "+CNMP:")) != 1) { return "OK"; } @@ -278,6 +287,7 @@ class TinyGsmSim7000 : public TinyGsmModem, } String getPreferredModes() { + // Get the help string, not the setting value sendAT(GF("+CMNB=?")); if (waitResponse(GF(GSM_NL "+CMNB:")) != 1) { return ""; } String res = stream.readStringUntil('\n'); @@ -285,6 +295,14 @@ class TinyGsmSim7000 : public TinyGsmModem, return res; } + bool getPreferredMode(int16_t & mode) { + sendAT(GF("+CMNB?")); + if (waitResponse(GF(GSM_NL "+CMNB:")) != 1) { return false; } + mode = streamGetIntBefore('\n'); + waitResponse(); + return true; + } + String setPreferredMode(uint8_t mode) { sendAT(GF("+CMNB="), mode); if (waitResponse(GF(GSM_NL "+CMNB:")) != 1) { return "OK"; } @@ -293,6 +311,26 @@ class TinyGsmSim7000 : public TinyGsmModem, return res; } + bool getNetworkSystemMode(bool & n, int16_t & stat) { + // n: whether to automatically report the system mode info + // stat: the current service. 0 if it not connected + sendAT(GF("+CNSMOD?")); + if (waitResponse(GF(GSM_NL "+CNSMOD:")) != 1) { return false; } + n = streamGetIntBefore(',') != 0; + stat = streamGetIntBefore('\n'); + waitResponse(); + return true; + } + + String setNetworkSystemMode(bool n) { + // n: whether to automatically report the system mode info + sendAT(GF("+CNSMOD="), int8_t(n)); + if (waitResponse(GF(GSM_NL "+CNSMOD:")) != 1) { return "OK"; } + String res = stream.readStringUntil('\n'); + waitResponse(); + return res; + } + String getLocalIPImpl() { sendAT(GF("+CIFSR;E0")); String res; From 81f583a80fb7132d97f40fc551ecffd6023a90d7 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Fri, 12 Mar 2021 10:11:58 +0100 Subject: [PATCH 03/12] SIM7000: TLS + some refinements --- src/TinyGsmClient.h | 2 +- src/TinyGsmClientSIM7000.h | 260 +++++++++++++++++++++---------------- 2 files changed, 151 insertions(+), 111 deletions(-) diff --git a/src/TinyGsmClient.h b/src/TinyGsmClient.h index cec5d68..39bfd0b 100644 --- a/src/TinyGsmClient.h +++ b/src/TinyGsmClient.h @@ -30,7 +30,7 @@ typedef TinyGsmSim800::GsmClientSim800 TinyGsmClient; #include "TinyGsmClientSIM7000.h" typedef TinyGsmSim7000 TinyGsm; typedef TinyGsmSim7000::GsmClientSim7000 TinyGsmClient; -// typedef TinyGsmSim7000::GsmClientSecureSim7000 TinyGsmClientSecure; TODO! +typedef TinyGsmSim7000::GsmClientSecureSIM7000 TinyGsmClientSecure; #elif defined(TINY_GSM_MODEM_SIM5320) || defined(TINY_GSM_MODEM_SIM5360) || \ defined(TINY_GSM_MODEM_SIM5300) || defined(TINY_GSM_MODEM_SIM7100) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 68a57d3..5bbd63b 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -12,7 +12,7 @@ // #define TINY_GSM_DEBUG Serial // #define TINY_GSM_USE_HEX -#define TINY_GSM_MUX_COUNT 8 +#define TINY_GSM_MUX_COUNT 2 #define TINY_GSM_BUFFER_READ_AND_CHECK_SIZE #include "TinyGsmBattery.tpp" @@ -99,7 +99,7 @@ class TinyGsmSim7000 : public TinyGsmModem, void stop(uint32_t maxWaitMs) { dumpModemBuffer(maxWaitMs); - at->sendAT(GF("+CIPCLOSE="), mux); + at->sendAT(GF("+CACLOSE="), mux); sock_connected = false; at->waitResponse(); } @@ -118,17 +118,14 @@ class TinyGsmSim7000 : public TinyGsmModem, * Inner Secure Client */ - /*TODO(?)) - class GsmClientSecureSIM7000 : public GsmClientSim7000 - { - public: - GsmClientSecure() {} + class GsmClientSecureSIM7000 : public GsmClientSim7000 { + public: + GsmClientSecureSIM7000() {} - GsmClientSecure(TinyGsmSim7000& modem, uint8_t mux = 0) - : public GsmClient(modem, mux) - {} + GsmClientSecureSIM7000(TinyGsmSim7000& modem, uint8_t mux = 0) + : GsmClientSim7000(modem, mux) {} - public: + public: int connect(const char* host, uint16_t port, int timeout_s) override { stop(); TINY_GSM_YIELD(); @@ -138,7 +135,6 @@ class TinyGsmSim7000 : public TinyGsmModem, } TINY_GSM_CLIENT_CONNECT_OVERRIDES }; - */ /* * Constructor @@ -280,10 +276,8 @@ class TinyGsmSim7000 : public TinyGsmModem, String setNetworkMode(uint8_t mode) { sendAT(GF("+CNMP="), mode); - if (waitResponse(GF(GSM_NL "+CNMP:")) != 1) { return "OK"; } - String res = stream.readStringUntil('\n'); waitResponse(); - return res; + return "OK"; } String getPreferredModes() { @@ -305,10 +299,8 @@ class TinyGsmSim7000 : public TinyGsmModem, String setPreferredMode(uint8_t mode) { sendAT(GF("+CMNB="), mode); - if (waitResponse(GF(GSM_NL "+CMNB:")) != 1) { return "OK"; } - String res = stream.readStringUntil('\n'); waitResponse(); - return res; + return "OK"; } bool getNetworkSystemMode(bool & n, int16_t & stat) { @@ -325,19 +317,16 @@ class TinyGsmSim7000 : public TinyGsmModem, String setNetworkSystemMode(bool n) { // n: whether to automatically report the system mode info sendAT(GF("+CNSMOD="), int8_t(n)); - if (waitResponse(GF(GSM_NL "+CNSMOD:")) != 1) { return "OK"; } - String res = stream.readStringUntil('\n'); waitResponse(); - return res; + return "OK"; } String getLocalIPImpl() { - sendAT(GF("+CIFSR;E0")); - String res; - if (waitResponse(10000L, res) != 1) { return ""; } - res.replace(GSM_NL "OK" GSM_NL, ""); - res.replace(GSM_NL, ""); - res.trim(); + sendAT(GF("+CNACT?")); + if (waitResponse(GF(GSM_NL "+CNACT:")) != 1) { return ""; } + streamSkipUntil('\"'); + String res = stream.readStringUntil('\"'); + waitResponse(); return res; } @@ -349,6 +338,10 @@ class TinyGsmSim7000 : public TinyGsmModem, const char* pwd = NULL) { gprsDisconnect(); + // Open data connection + sendAT(GF("+CNACT=1,\""), apn, GF("\"")); + if (waitResponse(60000L) != 1) { return false; } + // Set the Bearer for the IP sendAT(GF( "+SAPBR=3,1,\"Contype\",\"GPRS\"")); // Set the connection type to GPRS @@ -387,37 +380,20 @@ class TinyGsmSim7000 : public TinyGsmModem, // TODO(?): wait AT+CGATT? - // Set to multi-IP - sendAT(GF("+CIPMUX=1")); - if (waitResponse() != 1) { return false; } - - // Put in "quick send" mode (thus no extra "Send OK") - sendAT(GF("+CIPQSEND=1")); - if (waitResponse() != 1) { return false; } - - // Set to get data manually - sendAT(GF("+CIPRXGET=1")); - if (waitResponse() != 1) { return false; } - - // Start Task and Set APN, USER NAME, PASSWORD - sendAT(GF("+CSTT=\""), apn, GF("\",\""), user, GF("\",\""), pwd, GF("\"")); - if (waitResponse(60000L) != 1) { return false; } - - // Bring Up Wireless Connection with GPRS or CSD - sendAT(GF("+CIICR")); - if (waitResponse(60000L) != 1) { return false; } + // Check data connection - // Get Local IP Address, only assigned after connection - sendAT(GF("+CIFSR;E0")); - if (waitResponse(10000L) != 1) { return false; } + sendAT(GF("+CNACT?")); + if (waitResponse(GF(GSM_NL "+CNACT:")) != 1) { return false; } + int res = streamGetIntBefore(','); + waitResponse(); - return true; + return res == 1; } bool gprsDisconnectImpl() { // Shut the TCP/IP connection - // CIPSHUT will close *all* open connections - sendAT(GF("+CIPSHUT")); + // CNACT will close *all* open connections + sendAT(GF("+CNACT=0")); if (waitResponse(60000L) != 1) { return false; } sendAT(GF("+CGATT=0")); // Deactivate the bearer context @@ -569,101 +545,151 @@ class TinyGsmSim7000 : public TinyGsmModem, protected: bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false, int timeout_s = 75) { - if (ssl) { DBG("SSL not yet supported on this module!"); } - uint32_t timeout_ms = ((uint32_t)timeout_s) * 1000; - sendAT(GF("+CIPSTART="), mux, ',', GF("\"TCP"), GF("\",\""), host, - GF("\","), port); - return (1 == - waitResponse(timeout_ms, GF("CONNECT OK" GSM_NL), - GF("CONNECT FAIL" GSM_NL), - GF("ALREADY CONNECT" GSM_NL), GF("ERROR" GSM_NL), - GF("CLOSE OK" GSM_NL))); + + sendAT(GF("+CACID="), mux); + if (waitResponse(timeout_ms) != 1) return false; + + if (ssl) { + sendAT(GF("+CSSLCFG=\"sslversion\",0,3")); // TLS 1.2 + waitResponse(); + + sendAT(GF("+CSSLCFG=\"ctxindex\",0")); + waitResponse(); + } + + sendAT(GF("+CASSLCFG="), mux, ',', GF("ssl,"), ssl); + waitResponse(); + + sendAT(GF("+CASSLCFG="), mux, ',', GF("protocol,0")); + waitResponse(); + + sendAT(GF("+CAOPEN="), mux, ',', GF("\""), host, GF("\","), port); + + if (waitResponse(timeout_ms, GF(GSM_NL "+CAOPEN:")) != 1) { return 0; } + streamSkipUntil(','); // Skip mux + + int8_t res = streamGetIntBefore('\n'); + + return 0 == res; } int16_t modemSend(const void* buff, size_t len, uint8_t mux) { - sendAT(GF("+CIPSEND="), mux, ',', (uint16_t)len); - if (waitResponse(GF(">")) != 1) { return 0; } + sendAT(GF("+CASEND="), mux, ',', (uint16_t)len); + if (waitResponse(GF(">")) != 1) { + Serial.println("SEND ERROR: 0"); + return 0; + } + stream.write(reinterpret_cast(buff), len); stream.flush(); - if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) { return 0; } - streamSkipUntil(','); // Skip mux + + if (waitResponse(GF(GSM_NL "+CASEND:")) != 1) { + Serial.println("SEND ERROR: 1"); + return 0; + } + streamSkipUntil(','); // Skip mux + if (streamGetIntBefore(',') != 0) { + Serial.println("SEND ERROR: 2"); + return 0; + } // If result != success + Serial.println("SEND SUCCESS"); return streamGetIntBefore('\n'); } size_t modemRead(size_t size, uint8_t mux) { if (!sockets[mux]) return 0; -#ifdef TINY_GSM_USE_HEX - sendAT(GF("+CIPRXGET=3,"), mux, ',', (uint16_t)size); - if (waitResponse(GF("+CIPRXGET:")) != 1) { return 0; } -#else - sendAT(GF("+CIPRXGET=2,"), mux, ',', (uint16_t)size); - if (waitResponse(GF("+CIPRXGET:")) != 1) { return 0; } -#endif - streamSkipUntil(','); // Skip Rx mode 2/normal or 3/HEX - streamSkipUntil(','); // Skip mux - int16_t len_requested = streamGetIntBefore(','); - // ^^ Requested number of data bytes (1-1460 bytes)to be read - int16_t len_confirmed = streamGetIntBefore('\n'); - // ^^ Confirmed number of data bytes to be read, which may be less than - // requested. 0 indicates that no data can be read. - // SRGD NOTE: Contrary to above (which is copied from AT command manual) - // this is actually be the number of bytes that will be remaining in the - // buffer after the read. - for (int i = 0; i < len_requested; i++) { + sockets[mux]->sock_available = 0; + + sendAT(GF("+CARECV="), mux, ',', (uint16_t)size); + + if (waitResponse(GF("+CARECV:")) != 1) { + return 0; + } + + const int16_t len_confirmed = streamGetIntBefore(','); + if (len_confirmed <= 0) { + streamSkipUntil('\n'); + return 0; + } + + for (int i = 0; i < len_confirmed; i++) { uint32_t startMillis = millis(); -#ifdef TINY_GSM_USE_HEX - while (stream.available() < 2 && - (millis() - startMillis < sockets[mux]->_timeout)) { - TINY_GSM_YIELD(); - } - char buf[4] = { - 0, - }; - buf[0] = stream.read(); - buf[1] = stream.read(); - char c = strtol(buf, NULL, 16); -#else while (!stream.available() && - (millis() - startMillis < sockets[mux]->_timeout)) { + (millis() - startMillis < sockets[mux]->_timeout)) { TINY_GSM_YIELD(); } char c = stream.read(); -#endif sockets[mux]->rx.put(c); } // DBG("### READ:", len_requested, "from", mux); // sockets[mux]->sock_available = modemGetAvailable(mux); sockets[mux]->sock_available = len_confirmed; waitResponse(); - return len_requested; + return len_confirmed; } size_t modemGetAvailable(uint8_t mux) { if (!sockets[mux]) return 0; - sendAT(GF("+CIPRXGET=4,"), mux); + + if (!sockets[mux]->sock_connected) { + sockets[mux]->sock_connected = modemGetConnected(mux); + } + if (!sockets[mux]->sock_connected) return 0; + + sendAT(GF("+CARECV?")); + + int8_t readMux = -1; size_t result = 0; - if (waitResponse(GF("+CIPRXGET:")) == 1) { - streamSkipUntil(','); // Skip mode 4 - streamSkipUntil(','); // Skip mux + while (readMux != mux) { + if (waitResponse(GF("+CARECV:")) != 1) { + sockets[mux]->sock_connected = modemGetConnected(mux); + return 0; + }; + readMux = streamGetIntBefore(','); result = streamGetIntBefore('\n'); - waitResponse(); } - // DBG("### Available:", result, "on", mux); - if (!result) { sockets[mux]->sock_connected = modemGetConnected(mux); } + waitResponse(); + return result; } bool modemGetConnected(uint8_t mux) { - sendAT(GF("+CIPSTATUS="), mux); - waitResponse(GF("+CIPSTATUS")); - int8_t res = waitResponse(GF(",\"CONNECTED\""), GF(",\"CLOSED\""), - GF(",\"CLOSING\""), GF(",\"REMOTE CLOSING\""), - GF(",\"INITIAL\"")); + sendAT(GF("+CASTATE?")); + int8_t readMux = -1; + while (readMux != mux) { + if (waitResponse(GF("+CASTATE:")) != 1) return 0; + readMux = streamGetIntBefore(','); + } + int8_t res = streamGetIntBefore('\n'); waitResponse(); return 1 == res; } + public: + bool modemGetConnected(const char* host, uint16_t port, uint8_t mux) { + sendAT(GF("+CAOPEN?")); + int8_t readMux = -1; + while (readMux != mux) { + if (waitResponse(GF("+CAOPEN:")) != 1) return 0; + readMux = streamGetIntBefore(','); + } + streamSkipUntil('\"'); + + size_t hostLen = strlen(host); + + char buffer[hostLen]; + stream.readBytesUntil('\"', buffer, hostLen); + streamSkipUntil(','); + uint16_t connectedPort = streamGetIntBefore('\n'); + waitResponse(); + bool samePort = connectedPort == port; + bool sameHost = memcmp(buffer, host, hostLen) == 0; + sockets[mux]->sock_connected = sameHost && samePort; + + return sockets[mux]->sock_connected; + } + /* * Utilities */ @@ -727,6 +753,20 @@ class TinyGsmSim7000 : public TinyGsmModem, } else { data += mode; } + } else if (data.endsWith(GF(GSM_NL "+CARECV:"))) { + int8_t mux = streamGetIntBefore(','); + if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) { + sockets[mux]->got_data = true; + } + data = ""; + // DBG("### Got Data:", mux); + } else if (data.endsWith(GF(GSM_NL "+CADATAIND:"))) { + int8_t mux = streamGetIntBefore('\n'); + if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) { + sockets[mux]->got_data = true; + } + data = ""; + // DBG("### Got Data:", mux); } else if (data.endsWith(GF(GSM_NL "+RECEIVE:"))) { int8_t mux = streamGetIntBefore(','); int16_t len = streamGetIntBefore('\n'); From 0be9622e9c7402c645df06676012da9391460f80 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Fri, 12 Mar 2021 10:38:47 +0100 Subject: [PATCH 04/12] Removed some debug print --- src/TinyGsmClientSIM7000.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 5bbd63b..9dcf2c2 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -577,7 +577,6 @@ class TinyGsmSim7000 : public TinyGsmModem, int16_t modemSend(const void* buff, size_t len, uint8_t mux) { sendAT(GF("+CASEND="), mux, ',', (uint16_t)len); if (waitResponse(GF(">")) != 1) { - Serial.println("SEND ERROR: 0"); return 0; } @@ -585,15 +584,12 @@ class TinyGsmSim7000 : public TinyGsmModem, stream.flush(); if (waitResponse(GF(GSM_NL "+CASEND:")) != 1) { - Serial.println("SEND ERROR: 1"); return 0; } streamSkipUntil(','); // Skip mux if (streamGetIntBefore(',') != 0) { - Serial.println("SEND ERROR: 2"); return 0; } // If result != success - Serial.println("SEND SUCCESS"); return streamGetIntBefore('\n'); } From 7cf19beb440c230c04a67555beec18419815208d Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Fri, 12 Mar 2021 15:26:35 +0100 Subject: [PATCH 05/12] A deep debug of sim7000 --- src/TinyGsmClientSIM7000.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 9dcf2c2..756f0d3 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -276,7 +276,7 @@ class TinyGsmSim7000 : public TinyGsmModem, String setNetworkMode(uint8_t mode) { sendAT(GF("+CNMP="), mode); - waitResponse(); + if (waitResponse() != 1) return ""; return "OK"; } @@ -299,7 +299,7 @@ class TinyGsmSim7000 : public TinyGsmModem, String setPreferredMode(uint8_t mode) { sendAT(GF("+CMNB="), mode); - waitResponse(); + if (waitResponse() != 1) return ""; return "OK"; } @@ -317,7 +317,7 @@ class TinyGsmSim7000 : public TinyGsmModem, String setNetworkSystemMode(bool n) { // n: whether to automatically report the system mode info sendAT(GF("+CNSMOD="), int8_t(n)); - waitResponse(); + if (waitResponse() != 1) return ""; return "OK"; } @@ -595,17 +595,18 @@ class TinyGsmSim7000 : public TinyGsmModem, size_t modemRead(size_t size, uint8_t mux) { if (!sockets[mux]) return 0; - sockets[mux]->sock_available = 0; sendAT(GF("+CARECV="), mux, ',', (uint16_t)size); if (waitResponse(GF("+CARECV:")) != 1) { + sockets[mux]->sock_available = 0; return 0; } const int16_t len_confirmed = streamGetIntBefore(','); if (len_confirmed <= 0) { - streamSkipUntil('\n'); + sockets[mux]->sock_available = 0; + waitResponse(); return 0; } @@ -620,7 +621,9 @@ class TinyGsmSim7000 : public TinyGsmModem, } // DBG("### READ:", len_requested, "from", mux); // sockets[mux]->sock_available = modemGetAvailable(mux); - sockets[mux]->sock_available = len_confirmed; + auto diff = int64_t(size) - int64_t(len_confirmed); + if (diff < 0) diff = 0; + sockets[mux]->sock_available = diff; waitResponse(); return len_confirmed; } @@ -763,6 +766,17 @@ class TinyGsmSim7000 : public TinyGsmModem, } data = ""; // DBG("### Got Data:", mux); + } else if (data.endsWith(GF(GSM_NL "+CASTATE:"))) { + int8_t mux = streamGetIntBefore(','); + int8_t state = streamGetIntBefore('\n'); + if (mux >= 0 && mux < TINY_GSM_MUX_COUNT && sockets[mux]) { + if (state != 1) + { + sockets[mux]->sock_connected = false; + DBG("### Closed: ", mux); + } + } + data = ""; } else if (data.endsWith(GF(GSM_NL "+RECEIVE:"))) { int8_t mux = streamGetIntBefore(','); int16_t len = streamGetIntBefore('\n'); From 57514b7c4d80d2116078a7917abf19a9c755d6cb Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Fri, 12 Mar 2021 15:27:57 +0100 Subject: [PATCH 06/12] Supported factory reset for sim7000 --- src/TinyGsmClientSIM7000.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 756f0d3..7512bbb 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -201,7 +201,18 @@ class TinyGsmSim7000 : public TinyGsmModem, } bool factoryDefaultImpl() { // these commands aren't supported - return false; + sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write + waitResponse(); + sendAT(GF("+IPR=0")); // Auto-baud + waitResponse(); + sendAT(GF("+IFC=0,0")); // No Flow Control + waitResponse(); + sendAT(GF("+ICF=3,3")); // 8 data 0 parity 1 stop + waitResponse(); + sendAT(GF("+CSCLK=0")); // Disable Slow Clock + waitResponse(); + sendAT(GF("&W")); // Write configuration + return waitResponse() == 1; } /* From 57895868f27b093a75baeb4d573e7546c2abcd35 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Wed, 17 Mar 2021 08:36:09 +0100 Subject: [PATCH 07/12] Added missing waitResponse() --- src/TinyGsmClientSIM7000.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 7512bbb..2b2b7da 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -581,6 +581,7 @@ class TinyGsmSim7000 : public TinyGsmModem, streamSkipUntil(','); // Skip mux int8_t res = streamGetIntBefore('\n'); + waitResponse(); return 0 == res; } From b8a54d37e05be050fc2ef53af9d90e340f801955 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Wed, 17 Mar 2021 14:27:43 +0100 Subject: [PATCH 08/12] Some improvements to sim7000 --- src/TinyGsmClientSIM7000.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 2b2b7da..e0375ce 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -101,7 +101,7 @@ class TinyGsmSim7000 : public TinyGsmModem, dumpModemBuffer(maxWaitMs); at->sendAT(GF("+CACLOSE="), mux); sock_connected = false; - at->waitResponse(); + at->waitResponse(3000); } void stop() override { stop(15000L); @@ -614,7 +614,14 @@ class TinyGsmSim7000 : public TinyGsmModem, sockets[mux]->sock_available = 0; return 0; } - +/* + stream.read(); + if (stream.peek() == '0') { + waitResponse(); + sockets[mux]->sock_available = 0; + return 0; + } +*/ const int16_t len_confirmed = streamGetIntBefore(','); if (len_confirmed <= 0) { sockets[mux]->sock_available = 0; @@ -669,7 +676,9 @@ class TinyGsmSim7000 : public TinyGsmModem, sendAT(GF("+CASTATE?")); int8_t readMux = -1; while (readMux != mux) { - if (waitResponse(GF("+CASTATE:")) != 1) return 0; + if (waitResponse(GF("+CASTATE:"),GF(GSM_OK)) != 1) { + return 0; + } readMux = streamGetIntBefore(','); } int8_t res = streamGetIntBefore('\n'); From f0abeef22d78f31ab6b4102ba50229287a49a119 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Wed, 17 Mar 2021 15:17:13 +0100 Subject: [PATCH 09/12] Supported root certificates --- src/TinyGsmClientSIM7000.h | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index e0375ce..9b32f37 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -126,6 +126,10 @@ class TinyGsmSim7000 : public TinyGsmModem, : GsmClientSim7000(modem, mux) {} public: + bool setCertificate(const String & certificateName) { + return at->setCertificate(certificateName, mux); + } + int connect(const char* host, uint16_t port, int timeout_s) override { stop(); TINY_GSM_YIELD(); @@ -140,7 +144,10 @@ class TinyGsmSim7000 : public TinyGsmModem, * Constructor */ public: - explicit TinyGsmSim7000(Stream& stream) : stream(stream) { + explicit TinyGsmSim7000(Stream& stream): + stream(stream), + certificates() + { memset(sockets, 0, sizeof(sockets)); } @@ -262,6 +269,13 @@ class TinyGsmSim7000 : public TinyGsmModem, } protected: + bool setCertificate(const String & certificateName, const uint8_t mux = 0) { + if (mux >= TINY_GSM_MUX_COUNT) return false; + certificates[mux] = certificateName; + return true; + } + + bool isNetworkConnectedImpl() { RegStatus s = getRegistrationStatus(); return (s == REG_OK_HOME || s == REG_OK_ROAMING); @@ -389,8 +403,6 @@ class TinyGsmSim7000 : public TinyGsmModem, sendAT(GF("+CGATT=1")); if (waitResponse(60000L) != 1) { return false; } - // TODO(?): wait AT+CGATT? - // Check data connection sendAT(GF("+CNACT?")); @@ -563,10 +575,16 @@ class TinyGsmSim7000 : public TinyGsmModem, if (ssl) { sendAT(GF("+CSSLCFG=\"sslversion\",0,3")); // TLS 1.2 - waitResponse(); + if (waitResponse() != 1) return false; sendAT(GF("+CSSLCFG=\"ctxindex\",0")); - waitResponse(); + if (waitResponse() != 1) return false; + + if (certificates[mux] != "") + { + sendAT(GF("+CASSLCFG="), mux, ",CACERT,\"", certificates[mux].c_str(),"\""); + if (waitResponse() != 1) return false; + } } sendAT(GF("+CASSLCFG="), mux, ',', GF("ssl,"), ssl); @@ -877,6 +895,7 @@ class TinyGsmSim7000 : public TinyGsmModem, protected: GsmClientSim7000* sockets[TINY_GSM_MUX_COUNT]; + String certificates[TINY_GSM_MUX_COUNT]; const char* gsmNL = GSM_NL; }; From 28f7e7b2986732fb21b633af607c20921831a509 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Wed, 17 Mar 2021 15:19:04 +0100 Subject: [PATCH 10/12] Some improvements --- src/TinyGsmClientSIM7000.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 9b32f37..e9c7da0 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -694,7 +694,7 @@ class TinyGsmSim7000 : public TinyGsmModem, sendAT(GF("+CASTATE?")); int8_t readMux = -1; while (readMux != mux) { - if (waitResponse(GF("+CASTATE:"),GF(GSM_OK)) != 1) { + if (waitResponse(3000, GF("+CASTATE:"),GF(GSM_OK)) != 1) { return 0; } readMux = streamGetIntBefore(','); From 7f19c695548c330700616f33e3f2383c04accfce Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Thu, 18 Mar 2021 08:06:14 +0100 Subject: [PATCH 11/12] sim7000: added fix on read --- src/TinyGsmClientSIM7000.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index e9c7da0..86a49ad 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -632,14 +632,14 @@ class TinyGsmSim7000 : public TinyGsmModem, sockets[mux]->sock_available = 0; return 0; } -/* + stream.read(); if (stream.peek() == '0') { waitResponse(); sockets[mux]->sock_available = 0; return 0; } -*/ + const int16_t len_confirmed = streamGetIntBefore(','); if (len_confirmed <= 0) { sockets[mux]->sock_available = 0; From c1d6abaff7562d9930fdb1c4d7056952da45c454 Mon Sep 17 00:00:00 2001 From: Francesco Stefanni Date: Wed, 7 Apr 2021 11:24:39 +0200 Subject: [PATCH 12/12] Supported SNI (fix by kyriazis) --- src/TinyGsmClientSIM7000.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/TinyGsmClientSIM7000.h b/src/TinyGsmClientSIM7000.h index 86a49ad..fadaedb 100644 --- a/src/TinyGsmClientSIM7000.h +++ b/src/TinyGsmClientSIM7000.h @@ -593,6 +593,9 @@ class TinyGsmSim7000 : public TinyGsmModem, sendAT(GF("+CASSLCFG="), mux, ',', GF("protocol,0")); waitResponse(); + sendAT(GF("+CSSLCFG=\"sni\","), mux, ',', GF("\""), host, GF("\"")); + waitResponse(); + sendAT(GF("+CAOPEN="), mux, ',', GF("\""), host, GF("\","), port); if (waitResponse(timeout_ms, GF(GSM_NL "+CAOPEN:")) != 1) { return 0; }