WiFiClientSecure::lastError() method (#945)

* Added a lastError method to WiFiClientSecure so that a connection error from mbedTLS can be retrieved if connection fails (and then presented to a user).

* Changed to dos CRLF

* Made buffer size a const\nMore cleanup to match source
This commit is contained in:
lbernstone 2018-01-17 16:03:56 -07:00 committed by Me No Dev
parent 81e0250983
commit d650ac6c3c
2 changed files with 14 additions and 2 deletions

View File

@ -103,6 +103,7 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *_CA_cert,
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_cert, const char *_cert, const char *_private_key)
{
int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key);
_lastError = ret;
if (ret < 0) {
log_e("lwip_connect_r: %d", errno);
stop();
@ -187,3 +188,13 @@ void WiFiClientSecure::setPrivateKey (const char *private_key)
_private_key = private_key;
}
int WiFiClientSecure::lastError(char *buf, const size_t size)
{
if (!_lastError) {
return 0;
}
char error_buf[100];
mbedtls_strerror(_lastError, error_buf, 100);
snprintf(buf, size, "%s", error_buf);
return _lastError;
}

View File

@ -30,6 +30,7 @@ class WiFiClientSecure : public WiFiClient
protected:
sslclient_context *sslclient;
int _lastError = 0;
const char *_CA_cert;
const char *_cert;
const char *_private_key;
@ -55,7 +56,7 @@ public:
void flush() {}
void stop();
uint8_t connected();
int lastError(char *buf, const size_t size);
void setCACert(const char *rootCA);
void setCertificate(const char *client_ca);
void setPrivateKey (const char *private_key);