mirror of
https://github.com/vshymanskyy/TinyGSM.git
synced 2026-05-15 04:06:10 +00:00
Merge branch 'georgeman93-bg96ssl-dev'
This commit is contained in:
@@ -79,16 +79,11 @@ typedef TinyGsmSaraR5::GsmClientSecureR5 TinyGsmClientSecure;
|
||||
typedef TinyGsmM95 TinyGsm;
|
||||
typedef TinyGsmM95::GsmClientM95 TinyGsmClient;
|
||||
|
||||
#elif defined(TINY_GSM_MODEM_BG96)
|
||||
#elif defined(TINY_GSM_MODEM_BG96) || defined(TINY_GSM_MODEM_BG95SSL)
|
||||
#include "TinyGsmClientBG96.h"
|
||||
typedef TinyGsmBG96 TinyGsm;
|
||||
typedef TinyGsmBG96::GsmClientBG96 TinyGsmClient;
|
||||
// typedef TinyGsmBG96::GsmClientSecureBG96 TinyGsmClientSecure;
|
||||
|
||||
#elif defined(TINY_GSM_MODEM_BG95SSL)
|
||||
#include "TinyGsmClientBG95SSL.h"
|
||||
typedef TinyGsmBG95 TinyGsm;
|
||||
typedef TinyGsmBG95::GsmClientSecureBG95 TinyGsmClientSecure;
|
||||
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"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* @file TinyGsmClientBG96.h
|
||||
* @author Volodymyr Shymanskyy
|
||||
* @author Volodymyr Shymanskyy and Aurelien BOUIN (SSL)
|
||||
* @license LGPL-3.0
|
||||
* @copyright Copyright (c) 2016 Volodymyr Shymanskyy
|
||||
* @date Apr 2018
|
||||
* @date Apr 2018, Aug 2023 (SSL)
|
||||
*/
|
||||
|
||||
#ifndef SRC_TINYGSMCLIENTBG96_H_
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "TinyGsmModem.tpp"
|
||||
#include "TinyGsmSMS.tpp"
|
||||
#include "TinyGsmTCP.tpp"
|
||||
#include "TinyGsmSSL.tpp"
|
||||
#include "TinyGsmTemperature.tpp"
|
||||
#include "TinyGsmTime.tpp"
|
||||
#include "TinyGsmNTP.tpp"
|
||||
@@ -72,6 +73,7 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
|
||||
GsmClientBG96() {}
|
||||
|
||||
explicit GsmClientBG96(TinyGsmBG96& modem, uint8_t mux = 0) {
|
||||
ssl_sock = false;
|
||||
init(&modem, mux);
|
||||
}
|
||||
|
||||
@@ -92,17 +94,16 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
virtual int connect(const char* host, uint16_t port, int timeout_s) {
|
||||
stop();
|
||||
TINY_GSM_YIELD();
|
||||
rx.clear();
|
||||
sock_connected = at->modemConnect(host, port, mux, false, timeout_s);
|
||||
sock_connected = at->modemConnect(host, port, mux, timeout_s);
|
||||
return sock_connected;
|
||||
}
|
||||
TINY_GSM_CLIENT_CONNECT_OVERRIDES
|
||||
|
||||
void stop(uint32_t maxWaitMs) {
|
||||
virtual void stop(uint32_t maxWaitMs) {
|
||||
uint32_t startMillis = millis();
|
||||
dumpModemBuffer(maxWaitMs);
|
||||
at->sendAT(GF("+QICLOSE="), mux);
|
||||
@@ -118,34 +119,36 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
|
||||
*/
|
||||
|
||||
String remoteIP() TINY_GSM_ATTR_NOT_IMPLEMENTED;
|
||||
|
||||
protected:
|
||||
bool ssl_sock;
|
||||
};
|
||||
|
||||
/*
|
||||
* Inner Secure Client
|
||||
*/
|
||||
public:
|
||||
class GsmClientSecureBG96 : public GsmClientBG96 {
|
||||
public:
|
||||
GsmClientSecureBG96() {}
|
||||
|
||||
/*
|
||||
class GsmClientSecureBG96 : public GsmClientBG96
|
||||
{
|
||||
public:
|
||||
GsmClientSecure() {}
|
||||
|
||||
GsmClientSecure(TinyGsmBG96& modem, uint8_t mux = 0)
|
||||
: public GsmClient(modem, mux)
|
||||
{}
|
||||
|
||||
|
||||
public:
|
||||
int connect(const char* host, uint16_t port, int timeout_s) override {
|
||||
stop();
|
||||
TINY_GSM_YIELD();
|
||||
rx.clear();
|
||||
sock_connected = at->modemConnect(host, port, mux, true, timeout_s);
|
||||
return sock_connected;
|
||||
explicit GsmClientSecureBG96(TinyGsmBG96& modem, uint8_t mux = 0)
|
||||
: GsmClientBG96(modem, mux) {
|
||||
ssl_sock = true;
|
||||
}
|
||||
|
||||
bool setCertificate(const String& certificateName) {
|
||||
return at->setCertificate(certificateName, mux);
|
||||
}
|
||||
|
||||
void stop(uint32_t maxWaitMs) override {
|
||||
uint32_t startMillis = millis();
|
||||
dumpModemBuffer(maxWaitMs);
|
||||
at->sendAT(GF("+QSSLCLOSE="), mux);
|
||||
sock_connected = false;
|
||||
at->waitResponse((maxWaitMs - (millis() - startMillis)));
|
||||
}
|
||||
TINY_GSM_CLIENT_CONNECT_OVERRIDES
|
||||
};
|
||||
*/
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
@@ -224,9 +227,10 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
|
||||
return waitResponse() == 1;
|
||||
}
|
||||
|
||||
bool setPhoneFunctionalityImpl(uint8_t fun, bool reset = false) {
|
||||
bool setPhoneFunctionalityImpl(uint8_t fun, bool reset = false,
|
||||
uint32_t timeout_ms = 15500L) {
|
||||
sendAT(GF("+CFUN="), fun, reset ? ",1" : "");
|
||||
return waitResponse(10000L, GF("OK")) == 1;
|
||||
return waitResponse(timeout_ms, GF("OK")) == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -252,6 +256,16 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
|
||||
return (s == REG_OK_HOME || s == REG_OK_ROAMING);
|
||||
}
|
||||
|
||||
/*
|
||||
* Secure socket layer functions
|
||||
*/
|
||||
protected:
|
||||
bool setCertificate(const String& certificateName, const uint8_t mux = 0) {
|
||||
if (mux >= TINY_GSM_MUX_COUNT) return false;
|
||||
certificates[mux] = certificateName;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* GPRS functions
|
||||
*/
|
||||
@@ -569,39 +583,121 @@ 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!"); }
|
||||
|
||||
int timeout_s = 150) {
|
||||
uint32_t timeout_ms = ((uint32_t)timeout_s) * 1000;
|
||||
bool ssl = sockets[mux]->ssl_sock;
|
||||
|
||||
// <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) {
|
||||
// set the ssl version
|
||||
// AT+QSSLCFG="sslversion",<ctxindex>,<sslversion>
|
||||
// <ctxindex> PDP context identifier
|
||||
// <sslversion> 0: QAPI_NET_SSL_3.0
|
||||
// 1: QAPI_NET_SSL_PROTOCOL_TLS_1_0
|
||||
// 2: QAPI_NET_SSL_PROTOCOL_TLS_1_1
|
||||
// 3: QAPI_NET_SSL_PROTOCOL_TLS_1_2
|
||||
// 4: ALL
|
||||
// NOTE: despite docs using caps, "sslversion" must be in lower case
|
||||
sendAT(GF("+QSSLCFG=\"sslversion\",0,3")); // TLS 1.2
|
||||
if (waitResponse(5000L) != 1) return false;
|
||||
// set the ssl cipher_suite
|
||||
// AT+QSSLCFG="ciphersuite",<ctxindex>,<cipher_suite>
|
||||
// <ctxindex> PDP context identifier
|
||||
// <cipher_suite> 0: TODO
|
||||
// 1: TODO
|
||||
// 0X0035: TLS_RSA_WITH_AES_256_CBC_SHA
|
||||
// 0XFFFF: ALL
|
||||
// NOTE: despite docs using caps, "sslversion" must be in lower case
|
||||
sendAT(GF(
|
||||
"+QSSLCFG=\"ciphersuite\",0,0X0035")); // TLS_RSA_WITH_AES_256_CBC_SHA
|
||||
if (waitResponse(5000L) != 1) return false;
|
||||
// set the ssl sec level
|
||||
// AT+QSSLCFG="seclevel",<ctxindex>,<sec_level>
|
||||
// <ctxindex> PDP context identifier
|
||||
// <sec_level> 0: TODO
|
||||
// 1: TODO
|
||||
// 0X0035: TLS_RSA_WITH_AES_256_CBC_SHA
|
||||
// 0XFFFF: ALL
|
||||
// NOTE: despite docs using caps, "sslversion" must be in lower case
|
||||
sendAT(GF("+QSSLCFG=\"seclevel\",0,1"));
|
||||
if (waitResponse(5000L) != 1) return false;
|
||||
|
||||
if (waitResponse(timeout_ms, GF(AT_NL "+QIOPEN:")) != 1) { return false; }
|
||||
|
||||
if (streamGetIntBefore(',') != mux) { return false; }
|
||||
if (certificates[mux] != "") {
|
||||
// apply the correct certificate to the connection
|
||||
// AT+QSSLCFG="cacert",<ctxindex>,<caname>
|
||||
// <ctxindex> PDP context identifier
|
||||
// <certname> certificate name
|
||||
|
||||
// sendAT(GF("+CASSLCFG="), mux, ",CACERT,\"",
|
||||
// certificates[mux].c_str(),
|
||||
// "\"");
|
||||
sendAT(GF("+QSSLCFG=\"cacert\",0,\""), certificates[mux].c_str(),
|
||||
GF("\""));
|
||||
if (waitResponse(5000L) != 1) return false;
|
||||
}
|
||||
|
||||
// <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)
|
||||
// may need previous AT+QSSLCFG
|
||||
sendAT(GF("+QSSLOPEN=1,1,"), mux, GF(",\""), host, GF("\","), port,
|
||||
GF(",0"));
|
||||
waitResponse();
|
||||
|
||||
if (waitResponse(timeout_ms, GF(AT_NL "+QSSLOPEN:")) != 1) {
|
||||
return false;
|
||||
}
|
||||
// 20230629 -> +QSSLOPEN: <clientID>,<err>
|
||||
// clientID is mux
|
||||
// err must be 0
|
||||
if (streamGetIntBefore(',') != mux) { return false; }
|
||||
// Read status
|
||||
return (0 == streamGetIntBefore('\n'));
|
||||
} 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'));
|
||||
}
|
||||
|
||||
int16_t modemSend(const void* buff, size_t len, uint8_t mux) {
|
||||
sendAT(GF("+QISEND="), mux, ',', (uint16_t)len);
|
||||
bool ssl = sockets[mux]->ssl_sock;
|
||||
if (ssl) {
|
||||
sendAT(GF("+QSSLSEND="), mux, ',', (uint16_t)len);
|
||||
} else {
|
||||
sendAT(GF("+QISEND="), mux, ',', (uint16_t)len);
|
||||
}
|
||||
if (waitResponse(GF(">")) != 1) { return 0; }
|
||||
stream.write(reinterpret_cast<const uint8_t*>(buff), len);
|
||||
stream.flush();
|
||||
if (waitResponse(GF(AT_NL "SEND OK")) != 1) { return 0; }
|
||||
// TODO(?): Wait for ACK? AT+QISEND=id,0
|
||||
// TODO(?): Wait for ACK? (AT+QISEND=id,0 or AT+QSSLSEND=id,0)
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t modemRead(size_t size, uint8_t mux) {
|
||||
if (!sockets[mux]) return 0;
|
||||
sendAT(GF("+QIRD="), mux, ',', (uint16_t)size);
|
||||
if (waitResponse(GF("+QIRD:")) != 1) { return 0; }
|
||||
bool ssl = sockets[mux]->ssl_sock;
|
||||
if (ssl) {
|
||||
sendAT(GF("+QSSLRECV="), mux, ',', (uint16_t)size);
|
||||
if (waitResponse(GF("+QSSLRECV:")) != 1) {
|
||||
DBG("### READ: For unknown reason close");
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
sendAT(GF("+QIRD="), mux, ',', (uint16_t)size);
|
||||
if (waitResponse(GF("+QIRD:")) != 1) { return 0; }
|
||||
}
|
||||
int16_t len = streamGetIntBefore('\n');
|
||||
|
||||
for (int i = 0; i < len; i++) { moveCharFromStreamToFifo(mux); }
|
||||
@@ -613,36 +709,68 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
|
||||
|
||||
size_t modemGetAvailable(uint8_t mux) {
|
||||
if (!sockets[mux]) return 0;
|
||||
sendAT(GF("+QIRD="), mux, GF(",0"));
|
||||
bool ssl = sockets[mux]->ssl_sock;
|
||||
size_t result = 0;
|
||||
if (waitResponse(GF("+QIRD:")) == 1) {
|
||||
streamSkipUntil(','); // Skip total received
|
||||
streamSkipUntil(','); // Skip have read
|
||||
result = streamGetIntBefore('\n');
|
||||
if (result) { DBG("### DATA AVAILABLE:", result, "on", mux); }
|
||||
waitResponse();
|
||||
if (ssl) {
|
||||
sendAT(GF("+QSSLRECV="), mux, GF(",0"));
|
||||
if (waitResponse(GF("+QSSLRECV:")) == 1) {
|
||||
streamSkipUntil(','); // Skip total received
|
||||
streamSkipUntil(','); // Skip have read
|
||||
result = streamGetIntBefore('\n');
|
||||
if (result) { DBG("### DATA AVAILABLE:", result, "on", mux); }
|
||||
waitResponse();
|
||||
}
|
||||
} else {
|
||||
sendAT(GF("+QIRD="), mux, GF(",0"));
|
||||
if (waitResponse(GF("+QIRD:")) == 1) {
|
||||
streamSkipUntil(','); // Skip total received
|
||||
streamSkipUntil(','); // Skip have read
|
||||
result = streamGetIntBefore('\n');
|
||||
if (result) { DBG("### DATA AVAILABLE:", result, "on", mux); }
|
||||
waitResponse();
|
||||
}
|
||||
}
|
||||
if (!result) { sockets[mux]->sock_connected = modemGetConnected(mux); }
|
||||
return result;
|
||||
}
|
||||
|
||||
bool modemGetConnected(uint8_t mux) {
|
||||
sendAT(GF("+QISTATE=1,"), mux);
|
||||
// +QISTATE: 0,"TCP","151.139.237.11",80,5087,4,1,0,0,"uart1"
|
||||
bool ssl = sockets[mux]->ssl_sock;
|
||||
if (ssl) {
|
||||
sendAT(GF("+QSSLSTATE=1,"), mux);
|
||||
// +QSSLSTATE: 0,"TCP","151.139.237.11",80,5087,4,1,0,0,"uart1"
|
||||
|
||||
if (waitResponse(GF("+QISTATE:")) != 1) { return false; }
|
||||
if (waitResponse(GF("+QSSLSTATE:")) != 1) { return false; }
|
||||
|
||||
streamSkipUntil(','); // Skip mux
|
||||
streamSkipUntil(','); // Skip socket type
|
||||
streamSkipUntil(','); // Skip remote ip
|
||||
streamSkipUntil(','); // Skip remote port
|
||||
streamSkipUntil(','); // Skip local port
|
||||
int8_t res = streamGetIntBefore(','); // socket state
|
||||
streamSkipUntil(','); // Skip clientID
|
||||
streamSkipUntil(','); // Skip "SSLClient"
|
||||
streamSkipUntil(','); // Skip remote ip
|
||||
streamSkipUntil(','); // Skip remote port
|
||||
streamSkipUntil(','); // Skip local port
|
||||
int8_t res = streamGetIntBefore(','); // socket state
|
||||
|
||||
waitResponse();
|
||||
waitResponse();
|
||||
|
||||
// 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing
|
||||
return 2 == res;
|
||||
// 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing
|
||||
return 2 == res;
|
||||
} else {
|
||||
sendAT(GF("+QISTATE=1,"), mux);
|
||||
// +QISTATE: 0,"TCP","151.139.237.11",80,5087,4,1,0,0,"uart1"
|
||||
|
||||
if (waitResponse(GF("+QISTATE:")) != 1) { return false; }
|
||||
|
||||
streamSkipUntil(','); // Skip mux
|
||||
streamSkipUntil(','); // Skip socket type
|
||||
streamSkipUntil(','); // Skip remote ip
|
||||
streamSkipUntil(','); // Skip remote port
|
||||
streamSkipUntil(','); // Skip local port
|
||||
int8_t res = streamGetIntBefore(','); // socket state
|
||||
|
||||
waitResponse();
|
||||
|
||||
// 0 Initial, 1 Opening, 2 Connected, 3 Listening, 4 Closing
|
||||
return 2 == res;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -680,6 +808,7 @@ class TinyGsmBG96 : public TinyGsmModem<TinyGsmBG96>,
|
||||
|
||||
protected:
|
||||
GsmClientBG96* sockets[TINY_GSM_MUX_COUNT];
|
||||
String certificates[TINY_GSM_MUX_COUNT];
|
||||
};
|
||||
|
||||
#endif // SRC_TINYGSMCLIENTBG96_H_
|
||||
|
||||
Reference in New Issue
Block a user