From 9ca9633332f876c0a5f01b0e5c6cb29e3b36381c Mon Sep 17 00:00:00 2001 From: georgeman93 Date: Wed, 8 Dec 2021 13:41:17 +1100 Subject: [PATCH 001/100] not working yet --- src/TinyGsmClient.h | 1 + src/TinyGsmClientBG96.h | 183 +++++++++++++++++++++++++++++++++++----- 2 files changed, 164 insertions(+), 20 deletions(-) diff --git a/src/TinyGsmClient.h b/src/TinyGsmClient.h index a8ae763..6b85bc7 100644 --- a/src/TinyGsmClient.h +++ b/src/TinyGsmClient.h @@ -77,6 +77,7 @@ typedef TinyGsmM95::GsmClientM95 TinyGsmClient; #include "TinyGsmClientBG96.h" typedef TinyGsmBG96 TinyGsm; typedef TinyGsmBG96::GsmClientBG96 TinyGsmClient; +typedef TinyGsmBG96::GsmClientSecureBG96 TinyGsmClientSecure; #elif defined(TINY_GSM_MODEM_A6) || defined(TINY_GSM_MODEM_A7) #include "TinyGsmClientA6.h" diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index e696404..656f85d 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -96,7 +96,26 @@ class TinyGsmBG96 : public TinyGsmModem, return true; } - public: + int8_t uploadFile(const char *fileName, const char *content) { + // AT+QFDEL="file_name.pem" + at->sendAT(GF("+QFDEL="), GF("\""), fileName, + GF("\"")); + at->waitResponse(); + + at->sendAT(GF("+QFUPL="), GF("\""), fileName, + GF("\","), strlen(content), GF(","), 100); + if (at->waitResponse(GF("CONNECT")) != 1) + return 0; + at->stream.write(content, strlen(content)); + at->stream.flush(); + if (at->waitResponse() != 1) + return 0; + if (at->waitResponse(5000, GF(GSM_NL "+QFUPL:")) != 1) + return 0; + at->stream.readStringUntil('\n'); + return at->waitResponse(); + } + virtual int connect(const char* host, uint16_t port, int timeout_s) { stop(); TINY_GSM_YIELD(); @@ -117,6 +136,10 @@ class TinyGsmBG96 : public TinyGsmModem, stop(15000L); } + // we need to be able to access the inner secure client function definitions from inside TinyGsmTCP + // eg modemSend, modemRead, modemGetConnected and modemGetAvailable, how? + + /* * Extended API */ @@ -127,17 +150,13 @@ class TinyGsmBG96 : public TinyGsmModem, /* * Inner Secure Client */ - - /* - class GsmClientSecureBG96 : public GsmClientBG96 - { +public: + class GsmClientSecureBG96 : public GsmClientBG96 { public: - GsmClientSecure() {} - - GsmClientSecure(TinyGsmBG96& modem, uint8_t mux = 0) - : public GsmClient(modem, mux) - {} + GsmClientSecureBG96() {} + explicit GsmClientSecureBG96(TinyGsmBG96& modem, uint8_t mux = 0) + : GsmClientBG96(modem, mux) {} public: int connect(const char* host, uint16_t port, int timeout_s) override { @@ -148,8 +167,118 @@ class TinyGsmBG96 : public TinyGsmModem, return sock_connected; } TINY_GSM_CLIENT_CONNECT_OVERRIDES + + void stop(uint32_t maxWaitMs) { + uint32_t startMillis = millis(); + dumpModemBuffer(maxWaitMs); + at->sendAT(GF("+QSSLCLOSE=0")); + sock_connected = false; + at->waitResponse((maxWaitMs - (millis() - startMillis))); + } + void stop() { + stop(15000L); + } + + int8_t sslSetcacert(uint8_t context, const char *fileName) { + at->sendAT(GF("+QSSLCFG="), GF("\""), GF("cacert"), + GF("\","), context, GF(",\""), fileName, GF("\"")); + at->waitResponse(); + + at->sendAT(GF("+QSSLCFG="), GF("\""), GF("negotiatetime"), + GF("\","), context, GF(",300")); + at->waitResponse(); + at->sendAT(GF("+QSSLCFG="), GF("\""), GF("ignorelocaltime"), + GF("\","), context, GF(",0")); + return at->waitResponse(); + } + + protected: + int16_t modemSend(const void* buff, size_t len, uint8_t mux) { + at->sendAT(GF("+QSSLSEND=0,"), (uint16_t)len); + if (at->waitResponse(GF(">")) != 1) { return 0; } + at->stream.write(reinterpret_cast(buff), len); + at->stream.flush(); + if (at->waitResponse(GF(GSM_NL "SEND OK")) != 1) { return 0; } + return len; + } + + size_t modemRead(size_t size, uint8_t mux) { + if (!at->sockets[mux]) return 0; + at->sendAT(GF("+QSSLRECV=0,"), (uint16_t)size); + if (at->waitResponse(GF("+QSSLRECV:")) != 1) { return 0; } + int16_t len = at->streamGetIntBefore('\n'); + + for (int i = 0; i < len; i++) { at->moveCharFromStreamToFifo(mux); } + at->waitResponse(); + // DBG("### READ:", len, "from", mux); + // sockets[mux]->sock_available = modemGetAvailable(mux); + return len; + } + + size_t modemGetAvailable(uint8_t mux) { + if (!at->sockets[mux]) return 0; + at->sendAT(GF("+QSSLRECV=0,1500")); + size_t result = 0; + if (at->waitResponse(GF("+QSSLRECV:")) == 1) { + result = at->streamGetIntBefore('\n'); + if (result) { DBG("### DATA AVAILABLE:", result, "on", mux); } + at->waitResponse(); + } + if (!result) { at->sockets[mux]->sock_connected = modemGetConnected(mux); } + return result; + } + + bool modemGetConnected(uint8_t mux) { + at->sendAT(GF("+QSSLSTATE=0")); + // +QSSLSTATE: 4,"SSLClient","220.180.239.201",8010,65344,2,1,4,0,"usbmodem",1 + + if (at->waitResponse(GF("+QSSLSTATE:")) != 1) { return false; } + + at->streamSkipUntil(','); // Skip clientID + at->streamSkipUntil(','); // Skip "SSLClient" + at->streamSkipUntil(','); // Skip remote ip + at->streamSkipUntil(','); // Skip remote port + at->streamSkipUntil(','); // Skip local port + int8_t res = at->streamGetIntBefore(','); // socket state + + at->waitResponse(); + + // 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing + return 2 == res; + } + + bool gprsConnectImpl(const char* apn, const char* user = NULL, + const char* pwd = NULL) { + at->gprsDisconnect(); + + // Configure the TCPIP Context + at->sendAT(GF("+QICSGP=1,1,\""), apn, GF("\",\""), user, GF("\",\""), pwd, + GF("\"")); + if (at->waitResponse() != 1) { return false; } + + // Activate GPRS/CSD Context + at->sendAT(GF("+QIACT=1")); + if (at->waitResponse(150000L) != 1) { return false; } + + at->sendAT(GF("+QSSLCFG="), GF("\"sslversion"), + GF("\","), 0, GF(",4")); + if (!at->waitResponse()) + return false; + + at->sendAT(GF("+QSSLCFG="), GF("\"ciphersuite"), + GF("\","), 0, GF(",0xffff")); + if (!at->waitResponse()) + return false; + + at->sendAT(GF("+QSSLCFG="), GF("\"seclevel"), + GF("\","), 0, GF(",1")); + if (!at->waitResponse()) + return false; + + return true; + } + }; - */ /* * Constructor @@ -531,22 +660,36 @@ class TinyGsmBG96 : public TinyGsmModem, protected: bool modemConnect(const char* host, uint16_t port, uint8_t mux, bool ssl = false, int timeout_s = 150) { - if (ssl) { DBG("SSL not yet supported on this module!"); } uint32_t timeout_ms = ((uint32_t)timeout_s) * 1000; - // (1-16), (0-11), - // "TCP/UDP/TCP LISTENER/UDPSERVICE", "/", - // ,,(0-2; 0=buffer) - sendAT(GF("+QIOPEN=1,"), mux, GF(",\""), GF("TCP"), GF("\",\""), host, - GF("\","), port, GF(",0,0")); - waitResponse(); + if (ssl) { + //AT+QSSLOPEN=1,0,0,"220.180.239.201",8010,0 + sendAT(GF("+QSSLOPEN=1,"), mux, GF(",0,\""), host, + GF("\","), port, GF(",0")); + waitResponse(); - if (waitResponse(timeout_ms, GF(GSM_NL "+QIOPEN:")) != 1) { return false; } + if (waitResponse(timeout_ms, GF(GSM_NL "+QSSLOPEN:")) != 1) { return false; } - if (streamGetIntBefore(',') != mux) { return false; } + if (streamGetIntBefore(',') != 0) { return false; } //make sure client_id is 0 + + } else { + // AT+QIOPEN=1,0,"TCP","220.180.239.212",8009,0,0 + // (1-16), (0-11), + // "TCP/UDP/TCP LISTENER/UDPSERVICE", "/", + // ,,(0-2; 0=buffer) + sendAT(GF("+QIOPEN=1,"), mux, GF(",\""), GF("TCP"), GF("\",\""), host, + GF("\","), port, GF(",0,0")); + waitResponse(); + + if (waitResponse(timeout_ms, GF(GSM_NL "+QIOPEN:")) != 1) { return false; } + + if (streamGetIntBefore(',') != mux) { return false; } + + } // Read status return (0 == streamGetIntBefore('\n')); + } int16_t modemSend(const void* buff, size_t len, uint8_t mux) { From 69b8c400e88c3f8277dc5e10e85f7b266d21f0e0 Mon Sep 17 00:00:00 2001 From: Gonzalo Brusco Date: Mon, 4 Apr 2022 15:54:48 -0300 Subject: [PATCH 002/100] Fix NTPServerSync --- src/TinyGsmNTP.tpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/TinyGsmNTP.tpp b/src/TinyGsmNTP.tpp index aaeda7e..7d0ca0d 100644 --- a/src/TinyGsmNTP.tpp +++ b/src/TinyGsmNTP.tpp @@ -32,7 +32,7 @@ class TinyGsmNTP { return true; } - byte NTPServerSync(String server = "pool.ntp.org", byte TimeZone = 3) { + byte NTPServerSync(String server = "pool.ntp.org", int TimeZone = 0) { return thisModem().NTPServerSyncImpl(server, TimeZone); } String ShowNTPError(byte error) { @@ -54,7 +54,7 @@ class TinyGsmNTP { * NTP server functions */ protected: - byte NTPServerSyncImpl(String server = "pool.ntp.org", byte TimeZone = 3) { + byte NTPServerSyncImpl(String server = "pool.ntp.org", int TimeZone = 0) { // Set GPRS bearer profile to associate with NTP sync // this may fail, it's not supported by all modules thisModem().sendAT(GF("+CNTPCID=1")); @@ -68,6 +68,11 @@ class TinyGsmNTP { thisModem().sendAT(GF("+CNTP")); if (thisModem().waitResponse(10000L, GF("+CNTP:"))) { String result = thisModem().stream.readStringUntil('\n'); + // Check for ',' in case the modules appends the time nexto to the return value. + int index = result.indexOf(','); + if(index > 0) { + result.remove(index); + } result.trim(); if (TinyGsmIsValidNumber(result)) { return result.toInt(); } } else { From 7f25e735e6bb91c3dd1ff10d02b793a2f7bf55cb Mon Sep 17 00:00:00 2001 From: Gonzalo Brusco Date: Mon, 4 Apr 2022 16:03:04 -0300 Subject: [PATCH 003/100] Fix typo --- src/TinyGsmNTP.tpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TinyGsmNTP.tpp b/src/TinyGsmNTP.tpp index 7d0ca0d..6ae23cc 100644 --- a/src/TinyGsmNTP.tpp +++ b/src/TinyGsmNTP.tpp @@ -68,7 +68,7 @@ class TinyGsmNTP { thisModem().sendAT(GF("+CNTP")); if (thisModem().waitResponse(10000L, GF("+CNTP:"))) { String result = thisModem().stream.readStringUntil('\n'); - // Check for ',' in case the modules appends the time nexto to the return value. + // Check for ',' in case the module appends the time next to the return code. Eg: +CNTP: [,