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');