Merge branch 'bg96ssl-dev' of https://github.com/georgeman93/TinyGSM into georgeman93-bg96ssl-dev

Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
This commit is contained in:
Sara Damiano
2024-05-20 11:01:07 -04:00
2 changed files with 164 additions and 26 deletions

View File

@@ -81,9 +81,9 @@ typedef TinyGsmM95::GsmClientM95 TinyGsmClient;
#elif defined(TINY_GSM_MODEM_BG96)
#include "TinyGsmClientBG96.h"
typedef TinyGsmBG96 TinyGsm;
typedef TinyGsmBG96::GsmClientBG96 TinyGsmClient;
// typedef TinyGsmBG96::GsmClientSecureBG96 TinyGsmClientSecure;
typedef TinyGsmBG96 TinyGsm;
typedef TinyGsmBG96::GsmClientBG96 TinyGsmClient;
typedef TinyGsmBG96::GsmClientSecureBG96 TinyGsmClientSecure;
#elif defined(TINY_GSM_MODEM_BG95SSL)
#include "TinyGsmClientBG95SSL.h"

View File

@@ -92,7 +92,22 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
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(AT_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();
@@ -113,6 +128,11 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
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
*/
@@ -123,19 +143,15 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
/*
* Inner Secure Client
*/
public:
class GsmClientSecureBG96 : public GsmClientBG96 {
public:
GsmClientSecureBG96() {}
/*
class GsmClientSecureBG96 : public GsmClientBG96
{
public:
GsmClientSecure() {}
explicit GsmClientSecureBG96(TinyGsmBG96& modem, uint8_t mux = 0)
: GsmClientBG96(modem, mux) {}
GsmClientSecure(TinyGsmBG96& modem, uint8_t mux = 0)
: public GsmClient(modem, mux)
{}
public:
public:
int connect(const char* host, uint16_t port, int timeout_s) override {
stop();
TINY_GSM_YIELD();
@@ -144,8 +160,115 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
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<const uint8_t*>(buff), len);
at->stream.flush();
if (at->waitResponse(GF(AT_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
@@ -570,20 +693,35 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
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;
// <PDPcontextID>(1-16), <connectID>(0-11),
// "TCP/UDP/TCP LISTENER/UDPSERVICE", "<IP_address>/<domain_name>",
// <remote_port>,<local_port>,<access_mode>(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(AT_NL "+QIOPEN:")) != 1) { return false; }
if (waitResponse(timeout_ms, GF(AT_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
// <PDPcontextID>(1-16), <connectID>(0-11),
// "TCP/UDP/TCP LISTENER/UDPSERVICE", "<IP_address>/<domain_name>",
// <remote_port>,<local_port>,<access_mode>(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(AT_NL "+QIOPEN:")) != 1) { return false; }
if (streamGetIntBefore(',') != mux) { return false; }
}
// Read status
return (0 == streamGetIntBefore('\n'));
}