Skip to content

Commit d650ac6

Browse files
lbernstoneme-no-dev
authored andcommitted
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
1 parent 81e0250 commit d650ac6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *_CA_cert,
103103
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_cert, const char *_cert, const char *_private_key)
104104
{
105105
int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key);
106+
_lastError = ret;
106107
if (ret < 0) {
107108
log_e("lwip_connect_r: %d", errno);
108109
stop();
@@ -187,3 +188,13 @@ void WiFiClientSecure::setPrivateKey (const char *private_key)
187188
_private_key = private_key;
188189
}
189190

191+
int WiFiClientSecure::lastError(char *buf, const size_t size)
192+
{
193+
if (!_lastError) {
194+
return 0;
195+
}
196+
char error_buf[100];
197+
mbedtls_strerror(_lastError, error_buf, 100);
198+
snprintf(buf, size, "%s", error_buf);
199+
return _lastError;
200+
}

libraries/WiFiClientSecure/src/WiFiClientSecure.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class WiFiClientSecure : public WiFiClient
2929
{
3030
protected:
3131
sslclient_context *sslclient;
32-
32+
33+
int _lastError = 0;
3334
const char *_CA_cert;
3435
const char *_cert;
3536
const char *_private_key;
@@ -55,7 +56,7 @@ class WiFiClientSecure : public WiFiClient
5556
void flush() {}
5657
void stop();
5758
uint8_t connected();
58-
59+
int lastError(char *buf, const size_t size);
5960
void setCACert(const char *rootCA);
6061
void setCertificate(const char *client_ca);
6162
void setPrivateKey (const char *private_key);

0 commit comments

Comments
 (0)