From 63f915d9d6d434a9083f72f64c93930304fbb05d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 13:23:05 +0200
Subject: [PATCH 01/17] Removed virtual + moved socketOptions ot read/write

---
 libraries/WiFi/src/WiFiClient.cpp | 53 +++++++++++++++++++++++--------
 libraries/WiFi/src/WiFiClient.h   |  5 +--
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index 69a691995bd..f56cac2e7c8 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -310,22 +310,25 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
     return res;
 }
 
-int WiFiClient::setTimeout(uint32_t seconds)
+void WiFiClient::setTimeout(uint32_t seconds)
 {
+    _lastReadTimeout = Client::getTimeout();
+    _lastWriteTimeout = _lastReadTimeout;
     Client::setTimeout(seconds * 1000); // This should be here?
     _timeout = seconds * 1000;
-    if(fd() >= 0) {
-        struct timeval tv;
-        tv.tv_sec = seconds;
-        tv.tv_usec = 0;
-        if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
-            return -1;
-        }
-        return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
-    }
-    else {
-        return 0;
-    }
+    
+    // if(fd() >= 0) {
+    //     struct timeval tv;
+    //     tv.tv_sec = seconds;
+    //     tv.tv_usec = 0;
+    //     if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
+    //         return -1;
+    //     }
+    //     return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
+    // }
+    // else {
+    //     return 0;
+    // }
 }
 
 int WiFiClient::setOption(int option, int *value)
@@ -400,6 +403,18 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
         tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US;
         retry--;
 
+        if(_lastWriteTimeout != _timeout){
+            if(fd() >= 0){
+                struct timeval tv;
+                tv.tv_sec = _timeout/1000;
+                tv.tv_usec = 0;
+                if(setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
+                {
+                    _lastWriteTimeout = _timeout;
+                }
+            }
+        }
+
         if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) {
             return 0;
         }
@@ -459,6 +474,18 @@ size_t WiFiClient::write(Stream &stream)
 
 int WiFiClient::read(uint8_t *buf, size_t size)
 {
+    if(_lastReadTimeout != _timeout){
+        if(fd() >= 0){
+            struct timeval tv;
+            tv.tv_sec = _timeout/1000;
+            tv.tv_usec = 0;
+            if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
+            {
+                _lastReadTimeout = _timeout;
+            }
+        }
+    }
+
     int res = -1;
     if (_rxBuffer) {
         res = _rxBuffer->read(buf, size);
diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h
index 5aaa6ba115e..a2b7e3267a4 100644
--- a/libraries/WiFi/src/WiFiClient.h
+++ b/libraries/WiFi/src/WiFiClient.h
@@ -33,7 +33,6 @@ class ESPLwIPClient : public Client
 public:
         virtual int connect(IPAddress ip, uint16_t port, int32_t timeout) = 0;
         virtual int connect(const char *host, uint16_t port, int32_t timeout) = 0;
-        virtual int setTimeout(uint32_t seconds) = 0;
 };
 
 class WiFiClient : public ESPLwIPClient
@@ -43,6 +42,8 @@ class WiFiClient : public ESPLwIPClient
     std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
     bool _connected;
     int _timeout;
+    int _lastWriteTimeout;
+    int _lastReadTimeout;
 
 public:
     WiFiClient *next;
@@ -89,7 +90,7 @@ class WiFiClient : public ESPLwIPClient
     int setSocketOption(int option, char* value, size_t len);
     int setOption(int option, int *value);
     int getOption(int option, int *value);
-    int setTimeout(uint32_t seconds);
+    void setTimeout(uint32_t seconds);
     int setNoDelay(bool nodelay);
     bool getNoDelay();
 

From 4d8e7de40fbad1e435d26d580c60163262ea0621 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 13:34:40 +0200
Subject: [PATCH 02/17] Removed no needed code + edit

---
 libraries/WiFi/src/WiFiClient.cpp | 30 ++++++++----------------------
 1 file changed, 8 insertions(+), 22 deletions(-)

diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index f56cac2e7c8..2fd362bd40d 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -314,21 +314,7 @@ void WiFiClient::setTimeout(uint32_t seconds)
 {
     _lastReadTimeout = Client::getTimeout();
     _lastWriteTimeout = _lastReadTimeout;
-    Client::setTimeout(seconds * 1000); // This should be here?
     _timeout = seconds * 1000;
-    
-    // if(fd() >= 0) {
-    //     struct timeval tv;
-    //     tv.tv_sec = seconds;
-    //     tv.tv_usec = 0;
-    //     if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
-    //         return -1;
-    //     }
-    //     return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
-    // }
-    // else {
-    //     return 0;
-    // }
 }
 
 int WiFiClient::setOption(int option, int *value)
@@ -405,10 +391,10 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
 
         if(_lastWriteTimeout != _timeout){
             if(fd() >= 0){
-                struct timeval tv;
-                tv.tv_sec = _timeout/1000;
-                tv.tv_usec = 0;
-                if(setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
+                struct timeval timeout_tv;
+                timeout_tv.tv_sec = _timeout/1000;
+                timeout_tv.tv_usec = 0;
+                if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
                 {
                     _lastWriteTimeout = _timeout;
                 }
@@ -476,10 +462,10 @@ int WiFiClient::read(uint8_t *buf, size_t size)
 {
     if(_lastReadTimeout != _timeout){
         if(fd() >= 0){
-            struct timeval tv;
-            tv.tv_sec = _timeout/1000;
-            tv.tv_usec = 0;
-            if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
+            struct timeval timeout_tv;
+            timeout_tv.tv_sec = _timeout/1000;
+            timeout_tv.tv_usec = 0;
+            if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
             {
                 _lastReadTimeout = _timeout;
             }

From b5d52193a465df5963531b8ec3c438adc9349da4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 13:43:03 +0200
Subject: [PATCH 03/17] removed Client::getTimeout

---
 libraries/WiFi/src/WiFiClient.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index 2fd362bd40d..2a729edf63b 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -312,8 +312,8 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
 
 void WiFiClient::setTimeout(uint32_t seconds)
 {
-    _lastReadTimeout = Client::getTimeout();
-    _lastWriteTimeout = _lastReadTimeout;
+    _lastReadTimeout = _timeout;
+    _lastWriteTimeout = _timeout;
     _timeout = seconds * 1000;
 }
 

From 38c8ab4cd265cee15192f7896c15806062ca0b5b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 14:55:17 +0200
Subject: [PATCH 04/17] removed setTimeout from WifiClient - read/write
 timeouts in constructor now

---
 libraries/WiFi/src/WiFiClient.cpp | 9 ++-------
 libraries/WiFi/src/WiFiClient.h   | 1 -
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index 2a729edf63b..f622d184ab7 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -204,6 +204,8 @@ void WiFiClient::stop()
     clientSocketHandle = NULL;
     _rxBuffer = NULL;
     _connected = false;
+    _lastReadTimeout = 0;
+    _lastWriteTimeout = 0;
 }
 
 int WiFiClient::connect(IPAddress ip, uint16_t port)
@@ -310,13 +312,6 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
     return res;
 }
 
-void WiFiClient::setTimeout(uint32_t seconds)
-{
-    _lastReadTimeout = _timeout;
-    _lastWriteTimeout = _timeout;
-    _timeout = seconds * 1000;
-}
-
 int WiFiClient::setOption(int option, int *value)
 {
     int res = setsockopt(fd(), IPPROTO_TCP, option, (char *) value, sizeof(int));
diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h
index a2b7e3267a4..8fc67950f04 100644
--- a/libraries/WiFi/src/WiFiClient.h
+++ b/libraries/WiFi/src/WiFiClient.h
@@ -90,7 +90,6 @@ class WiFiClient : public ESPLwIPClient
     int setSocketOption(int option, char* value, size_t len);
     int setOption(int option, int *value);
     int getOption(int option, int *value);
-    void setTimeout(uint32_t seconds);
     int setNoDelay(bool nodelay);
     bool getNoDelay();
 

From 25b2062c0a63666a0e99eb9bd23d6a319bf9c149 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 15:01:33 +0200
Subject: [PATCH 05/17] Changed seconds to miliseconds in other classes relaed
 + examples

---
 libraries/HTTPClient/src/HTTPClient.cpp                       | 4 ++--
 .../HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino | 2 +-
 libraries/WebServer/src/WebServer.cpp                         | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp
index 7e967be9f35..8de020d2cf6 100644
--- a/libraries/HTTPClient/src/HTTPClient.cpp
+++ b/libraries/HTTPClient/src/HTTPClient.cpp
@@ -495,7 +495,7 @@ void HTTPClient::setTimeout(uint16_t timeout)
 {
     _tcpTimeout = timeout;
     if(connected()) {
-        _client->setTimeout((timeout + 500) / 1000);
+        _client->setTimeout(timeout + 500); /* / 1000 removed, WifiClient setTimeout changed to ms */
     }
 }
 
@@ -1151,7 +1151,7 @@ bool HTTPClient::connect(void)
     }
 
     // set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
-    _client->setTimeout((_tcpTimeout + 500) / 1000);	
+    _client->setTimeout(_tcpTimeout + 500); /* / 1000 removed, WifiClient setTimeout changed to ms */	
 
     log_d(" connected to %s:%u", _host.c_str(), _port);
 
diff --git a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino
index 1c85ace02a5..1e26fbcee5a 100644
--- a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino
+++ b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino
@@ -96,7 +96,7 @@ void loop() {
     client.setCACert(rootCACertificate);
 
     // Reading data over SSL may be slow, use an adequate timeout
-    client.setTimeout(12000 / 1000); // timeout argument is defined in seconds for setTimeout
+    client.setTimeout(12000); // timeout argument is defined in miliseconds for setTimeout
 
     // The line below is optional. It can be used to blink the LED on the board during flashing
     // The LED will be on during download of one buffer of data from the network. The LED will
diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp
index dbb3a4fe553..4dc0754c4c0 100644
--- a/libraries/WebServer/src/WebServer.cpp
+++ b/libraries/WebServer/src/WebServer.cpp
@@ -312,7 +312,7 @@ void WebServer::handleClient() {
         if (_parseRequest(_currentClient)) {
           // because HTTP_MAX_SEND_WAIT is expressed in milliseconds,
           // it must be divided by 1000
-          _currentClient.setTimeout(HTTP_MAX_SEND_WAIT / 1000);
+          _currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
           _contentLength = CONTENT_LENGTH_NOT_SET;
           _handleRequest();
 

From 28e727d471d4a61cb01936ad9fbcc30c38b1c62a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 15:06:36 +0200
Subject: [PATCH 06/17] Applied same changes for WifiClientSecure

---
 .../WiFiClientSecure/src/WiFiClientSecure.cpp | 39 +++++++++++--------
 .../WiFiClientSecure/src/WiFiClientSecure.h   |  1 -
 2 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
index cf22082af06..0983409d092 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
@@ -185,6 +185,16 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
     if (!_connected) {
         return 0;
     }
+    if(_lastWriteTimeout != _timeout){
+        struct timeval timeout_tv;
+        timeout_tv.tv_sec = _timeout/1000;
+        timeout_tv.tv_usec = 0;
+        if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
+        {
+            _lastWriteTimeout = _timeout;
+        }
+    }
+
     int res = send_ssl_data(sslclient, buf, size);
     if (res < 0) {
         stop();
@@ -195,6 +205,18 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
 
 int WiFiClientSecure::read(uint8_t *buf, size_t size)
 {
+    if(_lastReadTimeout != _timeout){
+        if(fd() >= 0){
+            struct timeval timeout_tv;
+            timeout_tv.tv_sec = _timeout/1000;
+            timeout_tv.tv_usec = 0;
+            if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
+            {
+                _lastReadTimeout = _timeout;
+            }
+        }
+    }
+
     int peeked = 0;
     int avail = available();
     if ((!buf && size) || avail <= 0) {
@@ -360,22 +382,7 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
 {
     _alpn_protos = alpn_protos;
 }
-int WiFiClientSecure::setTimeout(uint32_t seconds)
-{
-    _timeout = seconds * 1000;
-    if (sslclient->socket >= 0) {
-        struct timeval tv;
-        tv.tv_sec = seconds;
-        tv.tv_usec = 0;
-        if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
-            return -1;
-        }
-        return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
-    }
-    else {
-        return 0;
-    }
-}
+
 int WiFiClientSecure::setSocketOption(int option, char* value, size_t len)
 {
     int res = setsockopt(sslclient->socket, SOL_SOCKET, option, value, len);
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.h b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
index 9ea0c04eb4e..f2a61ccdf00 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.h
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
@@ -79,7 +79,6 @@ class WiFiClientSecure : public WiFiClient
     void setAlpnProtocols(const char **alpn_protos);
     const mbedtls_x509_crt* getPeerCertificate() { return mbedtls_ssl_get_peer_cert(&sslclient->ssl_ctx); };
     bool getFingerprintSHA256(uint8_t sha256_result[32]) { return get_peer_fingerprint(sslclient, sha256_result); };
-    int setTimeout(uint32_t seconds);
     int setSocketOption(int option, char* value, size_t len);
 
     operator bool()

From 6f5886baf8bb8ffa18cf4b8c83614c653abf2db1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 15:10:37 +0200
Subject: [PATCH 07/17] Added 0 init values to constructor

---
 libraries/WiFiClientSecure/src/WiFiClientSecure.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
index 0983409d092..68f94f14686 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
@@ -54,6 +54,8 @@ WiFiClientSecure::WiFiClientSecure(int sock)
 {
     _connected = false;
     _timeout = 30000; // Same default as ssl_client
+    _lastReadTimeout = 0;
+    _lastWriteTimeout = 0;
 
     sslclient = new sslclient_context;
     ssl_init(sslclient);
@@ -94,6 +96,8 @@ void WiFiClientSecure::stop()
         sslclient->socket = -1;
         _connected = false;
         _peek = -1;
+        _lastReadTimeout = 0;
+        _lastWriteTimeout = 0;
     }
     stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
 }

From 1c7116bb2306bda8198f289a4a7523b47858fc2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 15:30:11 +0200
Subject: [PATCH 08/17] Seconds are not rounded now

---
 libraries/WiFi/src/WiFiClient.cpp                   | 8 ++++----
 libraries/WiFiClientSecure/src/WiFiClientSecure.cpp | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index f622d184ab7..7a2fa6d3847 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -387,8 +387,8 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
         if(_lastWriteTimeout != _timeout){
             if(fd() >= 0){
                 struct timeval timeout_tv;
-                timeout_tv.tv_sec = _timeout/1000;
-                timeout_tv.tv_usec = 0;
+                timeout_tv.tv_sec = _timeout / 1000;
+                timeout_tv.tv_usec = (_timeout % 1000) * 1000;
                 if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
                 {
                     _lastWriteTimeout = _timeout;
@@ -458,8 +458,8 @@ int WiFiClient::read(uint8_t *buf, size_t size)
     if(_lastReadTimeout != _timeout){
         if(fd() >= 0){
             struct timeval timeout_tv;
-            timeout_tv.tv_sec = _timeout/1000;
-            timeout_tv.tv_usec = 0;
+            timeout_tv.tv_sec = _timeout / 1000;
+            timeout_tv.tv_usec = (_timeout % 1000) * 1000;
             if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
             {
                 _lastReadTimeout = _timeout;
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
index 68f94f14686..fb708393791 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
@@ -191,8 +191,8 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
     }
     if(_lastWriteTimeout != _timeout){
         struct timeval timeout_tv;
-        timeout_tv.tv_sec = _timeout/1000;
-        timeout_tv.tv_usec = 0;
+        timeout_tv.tv_sec = _timeout / 1000;
+        timeout_tv.tv_usec = (_timeout % 1000) * 1000;
         if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
         {
             _lastWriteTimeout = _timeout;
@@ -212,8 +212,8 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size)
     if(_lastReadTimeout != _timeout){
         if(fd() >= 0){
             struct timeval timeout_tv;
-            timeout_tv.tv_sec = _timeout/1000;
-            timeout_tv.tv_usec = 0;
+            timeout_tv.tv_sec = _timeout / 1000;
+            timeout_tv.tv_usec = (_timeout % 1000) * 1000;
             if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
             {
                 _lastReadTimeout = _timeout;

From 17adb9d92b0aadee3cf42719088b87551e404e69 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 2 May 2022 16:40:28 +0200
Subject: [PATCH 09/17] removed +500 for previous rounding

+ unnecessary comments removed.
---
 libraries/HTTPClient/src/HTTPClient.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp
index 8de020d2cf6..9115c43f62f 100644
--- a/libraries/HTTPClient/src/HTTPClient.cpp
+++ b/libraries/HTTPClient/src/HTTPClient.cpp
@@ -495,7 +495,7 @@ void HTTPClient::setTimeout(uint16_t timeout)
 {
     _tcpTimeout = timeout;
     if(connected()) {
-        _client->setTimeout(timeout + 500); /* / 1000 removed, WifiClient setTimeout changed to ms */
+        _client->setTimeout(timeout);
     }
 }
 
@@ -1151,7 +1151,7 @@ bool HTTPClient::connect(void)
     }
 
     // set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
-    _client->setTimeout(_tcpTimeout + 500); /* / 1000 removed, WifiClient setTimeout changed to ms */	
+    _client->setTimeout(_tcpTimeout);	
 
     log_d(" connected to %s:%u", _host.c_str(), _port);
 

From 9a3461fbc2c913f08fb443955e4a0e0d5402e2e2 Mon Sep 17 00:00:00 2001
From: Tomas Pilny <tomas.pilny@espressif.com>
Date: Wed, 27 Jul 2022 11:03:27 +0200
Subject: [PATCH 10/17] Initial commit

---
 boards.txt   | 8 ++++++++
 platform.txt | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/boards.txt b/boards.txt
index 9b11c39428f..2047f4262ba 100644
--- a/boards.txt
+++ b/boards.txt
@@ -17,6 +17,7 @@ menu.LoRaWanDebugLevel=LoRaWan Debug Level
 menu.LoopCore=Arduino Runs On
 menu.EventsCore=Events Run On
 menu.MemoryType=Memory Type
+menu.Wipe=Erase Flash
 
 ##############################################################
 ### DO NOT PUT BOARDS ABOVE THE OFFICIAL ESPRESSIF BOARDS! ###
@@ -715,6 +716,13 @@ esp32.menu.DebugLevel.debug.build.code_debug=4
 esp32.menu.DebugLevel.verbose=Verbose
 esp32.menu.DebugLevel.verbose.build.code_debug=5
 
+esp32.menu.Wipe.none=Only Sketch
+esp32.menu.Wipe.none.upload.erase_cmd=
+#esp32.menu.Wipe.sdk=Sketch + WiFi Settings
+#esp32.menu.Wipe.sdk.upload.erase_cmd=erase_region "{build.rfcal_addr}" 0x4000
+esp32.menu.Wipe.all=All Flash Contents
+esp32.menu.Wipe.all.upload.erase_cmd=erase_flash
+
 ##############################################################
 
 esp32da.name=ESP32-WROOM-DA Module
diff --git a/platform.txt b/platform.txt
index 3cb29298791..ae2091e1307 100644
--- a/platform.txt
+++ b/platform.txt
@@ -211,7 +211,7 @@ pluggable_monitor.required.serial=builtin:serial-monitor
 tools.esptool_py.upload.protocol=serial
 tools.esptool_py.upload.params.verbose=
 tools.esptool_py.upload.params.quiet=
-tools.esptool_py.upload.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default_reset --after hard_reset write_flash -z --flash_mode {build.flash_mode} --flash_freq {build.flash_freq} --flash_size {build.flash_size} {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" {upload.extra_flags}
+tools.esptool_py.upload.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default_reset --after hard_reset {upload.erase_cmd} write_flash -z --flash_mode {build.flash_mode} --flash_freq {build.flash_freq} --flash_size {build.flash_size} {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" {upload.extra_flags}
 tools.esptool_py.upload.pattern="{path}/{cmd}" {upload.pattern_args}
 tools.esptool_py.upload.pattern.linux=python3 "{path}/{cmd}" {upload.pattern_args}
 

From 53d0e35013d2222a128441db2aded2bfd15540e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Wed, 27 Jul 2022 11:27:22 +0200
Subject: [PATCH 11/17] Erase command change + added cmd to most common
 variants

---
 boards.txt   | 27 ++++++++++++++++++++-------
 platform.txt |  2 +-
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/boards.txt b/boards.txt
index 2047f4262ba..02162c194ce 100644
--- a/boards.txt
+++ b/boards.txt
@@ -17,7 +17,7 @@ menu.LoRaWanDebugLevel=LoRaWan Debug Level
 menu.LoopCore=Arduino Runs On
 menu.EventsCore=Events Run On
 menu.MemoryType=Memory Type
-menu.Wipe=Erase Flash
+menu.EraseFlash=Erase Flash
 
 ##############################################################
 ### DO NOT PUT BOARDS ABOVE THE OFFICIAL ESPRESSIF BOARDS! ###
@@ -225,6 +225,11 @@ esp32s3.menu.DebugLevel.debug.build.code_debug=4
 esp32s3.menu.DebugLevel.verbose=Verbose
 esp32s3.menu.DebugLevel.verbose.build.code_debug=5
 
+esp32s3.menu.EraseFlash.none=Only Sketch
+esp32s3.menu.EraseFlash.none.upload.erase_cmd=
+esp32s3.menu.EraseFlash.all=All Flash Contents
+esp32s3.menu.EraseFlash.all.upload.erase_cmd=-e
+
 ##############################################################
 
 esp32c3.name=ESP32C3 Dev Module
@@ -375,6 +380,11 @@ esp32c3.menu.DebugLevel.debug.build.code_debug=4
 esp32c3.menu.DebugLevel.verbose=Verbose
 esp32c3.menu.DebugLevel.verbose.build.code_debug=5
 
+esp32c3.menu.EraseFlash.none=Only Sketch
+esp32c3.menu.EraseFlash.none.upload.erase_cmd=
+esp32c3.menu.EraseFlash.all=All Flash Contents
+esp32c3.menu.EraseFlash.all.upload.erase_cmd=-e
+
 ##############################################################
 
 esp32s2.name=ESP32S2 Dev Module
@@ -551,6 +561,11 @@ esp32s2.menu.DebugLevel.debug.build.code_debug=4
 esp32s2.menu.DebugLevel.verbose=Verbose
 esp32s2.menu.DebugLevel.verbose.build.code_debug=5
 
+esp32s2.menu.EraseFlash.none=Only Sketch
+esp32s2.menu.EraseFlash.none.upload.erase_cmd=
+esp32s2.menu.EraseFlash.all=All Flash Contents
+esp32s2.menu.EraseFlash.all.upload.erase_cmd=-e
+
 ##############################################################
 
 esp32.name=ESP32 Dev Module
@@ -716,12 +731,10 @@ esp32.menu.DebugLevel.debug.build.code_debug=4
 esp32.menu.DebugLevel.verbose=Verbose
 esp32.menu.DebugLevel.verbose.build.code_debug=5
 
-esp32.menu.Wipe.none=Only Sketch
-esp32.menu.Wipe.none.upload.erase_cmd=
-#esp32.menu.Wipe.sdk=Sketch + WiFi Settings
-#esp32.menu.Wipe.sdk.upload.erase_cmd=erase_region "{build.rfcal_addr}" 0x4000
-esp32.menu.Wipe.all=All Flash Contents
-esp32.menu.Wipe.all.upload.erase_cmd=erase_flash
+esp32.menu.EraseFlash.none=Only Sketch
+esp32.menu.EraseFlash.none.upload.erase_cmd=
+esp32.menu.EraseFlash.all=All Flash Contents
+esp32.menu.EraseFlash.all.upload.erase_cmd=-e
 
 ##############################################################
 
diff --git a/platform.txt b/platform.txt
index ae2091e1307..90861499458 100644
--- a/platform.txt
+++ b/platform.txt
@@ -211,7 +211,7 @@ pluggable_monitor.required.serial=builtin:serial-monitor
 tools.esptool_py.upload.protocol=serial
 tools.esptool_py.upload.params.verbose=
 tools.esptool_py.upload.params.quiet=
-tools.esptool_py.upload.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default_reset --after hard_reset {upload.erase_cmd} write_flash -z --flash_mode {build.flash_mode} --flash_freq {build.flash_freq} --flash_size {build.flash_size} {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" {upload.extra_flags}
+tools.esptool_py.upload.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default_reset --after hard_reset write_flash {upload.erase_cmd} -z --flash_mode {build.flash_mode} --flash_freq {build.flash_freq} --flash_size {build.flash_size} {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" {upload.extra_flags}
 tools.esptool_py.upload.pattern="{path}/{cmd}" {upload.pattern_args}
 tools.esptool_py.upload.pattern.linux=python3 "{path}/{cmd}" {upload.pattern_args}
 

From caacfe633416f9221edc296814dee0b1fd78415f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Wed, 3 Aug 2022 11:46:23 +0200
Subject: [PATCH 12/17] Updated docs with erase flash option

---
 docs/source/guides/tools_menu.rst | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/docs/source/guides/tools_menu.rst b/docs/source/guides/tools_menu.rst
index 432511c191e..8b80a3b783b 100644
--- a/docs/source/guides/tools_menu.rst
+++ b/docs/source/guides/tools_menu.rst
@@ -196,6 +196,14 @@ Events Run On
 
 This function is also used to select the core that runs the Arduino events. This is only valid if the target SoC has 2 cores.
 
+Erase Flash
+***********
+
+This function is used to select if the flash memory should be erased before uploading the sketch.
+
+* **Only Sketch** - Upload the sketch without erasing all flash contents. (default)
+* **All Flash Contents** - Erase all flash contents before uploading the sketch.
+
 Port
 ****
 

From 8161e789406141132efb751c73981858b2c6ef84 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Wed, 3 Aug 2022 13:54:51 +0200
Subject: [PATCH 13/17] docs update + tool menu name

---
 boards.txt                        | 2 +-
 docs/source/guides/tools_menu.rst | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/boards.txt b/boards.txt
index 676b910884c..2fc84b2012d 100644
--- a/boards.txt
+++ b/boards.txt
@@ -17,7 +17,7 @@ menu.LoRaWanDebugLevel=LoRaWan Debug Level
 menu.LoopCore=Arduino Runs On
 menu.EventsCore=Events Run On
 menu.MemoryType=Memory Type
-menu.EraseFlash=Erase Flash
+menu.EraseFlash=Erase Flash (on upload)
 
 ##############################################################
 ### DO NOT PUT BOARDS ABOVE THE OFFICIAL ESPRESSIF BOARDS! ###
diff --git a/docs/source/guides/tools_menu.rst b/docs/source/guides/tools_menu.rst
index 8b80a3b783b..53fafcbe136 100644
--- a/docs/source/guides/tools_menu.rst
+++ b/docs/source/guides/tools_menu.rst
@@ -199,7 +199,7 @@ This function is also used to select the core that runs the Arduino events. This
 Erase Flash
 ***********
 
-This function is used to select if the flash memory should be erased before uploading the sketch.
+This option selects the flash memory region to be erased before uploading the new sketch.
 
 * **Only Sketch** - Upload the sketch without erasing all flash contents. (default)
 * **All Flash Contents** - Erase all flash contents before uploading the sketch.

From bbf8d482f9426168c2e6f6b34c1d058032802c70 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Tue, 9 Aug 2022 19:20:42 +0200
Subject: [PATCH 14/17] Edit typo in boards.txt + docs

---
 boards.txt                        | 18 +++++++++---------
 docs/source/guides/tools_menu.rst |  8 ++++----
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/boards.txt b/boards.txt
index 336876e1ba7..3bf574ca45a 100644
--- a/boards.txt
+++ b/boards.txt
@@ -17,7 +17,7 @@ menu.LoRaWanDebugLevel=LoRaWan Debug Level
 menu.LoopCore=Arduino Runs On
 menu.EventsCore=Events Run On
 menu.MemoryType=Memory Type
-menu.EraseFlash=Erase Flash (on upload)
+menu.EraseFlash=Erase All Flash Before Sketch Upload
 
 ##############################################################
 ### DO NOT PUT BOARDS ABOVE THE OFFICIAL ESPRESSIF BOARDS! ###
@@ -225,9 +225,9 @@ esp32s3.menu.DebugLevel.debug.build.code_debug=4
 esp32s3.menu.DebugLevel.verbose=Verbose
 esp32s3.menu.DebugLevel.verbose.build.code_debug=5
 
-esp32s3.menu.EraseFlash.none=Only Sketch
+esp32s3.menu.EraseFlash.none=Disabled
 esp32s3.menu.EraseFlash.none.upload.erase_cmd=
-esp32s3.menu.EraseFlash.all=All Flash Contents
+esp32s3.menu.EraseFlash.all=Enabled
 esp32s3.menu.EraseFlash.all.upload.erase_cmd=-e
 
 ##############################################################
@@ -380,9 +380,9 @@ esp32c3.menu.DebugLevel.debug.build.code_debug=4
 esp32c3.menu.DebugLevel.verbose=Verbose
 esp32c3.menu.DebugLevel.verbose.build.code_debug=5
 
-esp32c3.menu.EraseFlash.none=Only Sketch
+esp32c3.menu.EraseFlash.none=Disabled
 esp32c3.menu.EraseFlash.none.upload.erase_cmd=
-esp32c3.menu.EraseFlash.all=All Flash Contents
+esp32c3.menu.EraseFlash.all=Enabled
 esp32c3.menu.EraseFlash.all.upload.erase_cmd=-e
 
 ##############################################################
@@ -561,9 +561,9 @@ esp32s2.menu.DebugLevel.debug.build.code_debug=4
 esp32s2.menu.DebugLevel.verbose=Verbose
 esp32s2.menu.DebugLevel.verbose.build.code_debug=5
 
-esp32s2.menu.EraseFlash.none=Only Sketch
+esp32s2.menu.EraseFlash.none=Disabled
 esp32s2.menu.EraseFlash.none.upload.erase_cmd=
-esp32s2.menu.EraseFlash.all=All Flash Contents
+esp32s2.menu.EraseFlash.all=Enabled
 esp32s2.menu.EraseFlash.all.upload.erase_cmd=-e
 
 ##############################################################
@@ -731,9 +731,9 @@ esp32.menu.DebugLevel.debug.build.code_debug=4
 esp32.menu.DebugLevel.verbose=Verbose
 esp32.menu.DebugLevel.verbose.build.code_debug=5
 
-esp32.menu.EraseFlash.none=Only Sketch
+esp32.menu.EraseFlash.none=Disabled
 esp32.menu.EraseFlash.none.upload.erase_cmd=
-esp32.menu.EraseFlash.all=All Flash Contents
+esp32.menu.EraseFlash.all=Enabled
 esp32.menu.EraseFlash.all.upload.erase_cmd=-e
 
 ##############################################################
diff --git a/docs/source/guides/tools_menu.rst b/docs/source/guides/tools_menu.rst
index 53fafcbe136..96b3bcc3674 100644
--- a/docs/source/guides/tools_menu.rst
+++ b/docs/source/guides/tools_menu.rst
@@ -196,13 +196,13 @@ Events Run On
 
 This function is also used to select the core that runs the Arduino events. This is only valid if the target SoC has 2 cores.
 
-Erase Flash
-***********
+Erase All Flash Before Sketch Upload
+************************************
 
 This option selects the flash memory region to be erased before uploading the new sketch.
 
-* **Only Sketch** - Upload the sketch without erasing all flash contents. (default)
-* **All Flash Contents** - Erase all flash contents before uploading the sketch.
+* **Disabled** - Upload the sketch without erasing all flash contents. (Default)
+* **Enabled** - Erase all flash contents before uploading the sketch.
 
 Port
 ****

From 55d2a29b73943c12ad2a0388c642a2c213d60f62 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Wed, 10 Aug 2022 08:19:40 +0200
Subject: [PATCH 15/17] Revert "Merge branch 'erase_flash' of
 https://github.com/PilnyTomas/arduino-esp32 into pr/7043"

This reverts commit 31d0b73a05e0bc8dca4de62521059d449729f542, reversing
changes made to 3f47a0c4b4b75c04d3bf644945ed46b225cc91fc.
---
 libraries/WiFi/src/WiFiClient.cpp                 | 15 +++++++--------
 libraries/WiFi/src/WiFiClient.h                   |  1 -
 .../WiFiClientSecure/src/WiFiClientSecure.cpp     |  9 ++-------
 libraries/WiFiClientSecure/src/WiFiClientSecure.h |  1 -
 4 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index 9657d73243c..7a2fa6d3847 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -305,21 +305,20 @@ int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout)
 
 int WiFiClient::setSocketOption(int option, char* value, size_t len)
 {
-    return setSocketOption(SOL_SOCKET, option, (const void*)value, len);
-}
-
-int WiFiClient::setSocketOption(int level, int option, const void* value, size_t len)
-{
-    int res = setsockopt(fd(), level, option, value, len);
+    int res = setsockopt(fd(), SOL_SOCKET, option, value, len);
     if(res < 0) {
-        log_e("fail on %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
+        log_e("%X : %d", option, errno);
     }
     return res;
 }
 
 int WiFiClient::setOption(int option, int *value)
 {
-    return setSocketOption(IPPROTO_TCP, option, (const void*)value, sizeof(int));
+    int res = setsockopt(fd(), IPPROTO_TCP, option, (char *) value, sizeof(int));
+    if(res < 0) {
+        log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
+    }
+    return res;
 }
 
 int WiFiClient::getOption(int option, int *value)
diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h
index caa1f5e0ae2..8fc67950f04 100644
--- a/libraries/WiFi/src/WiFiClient.h
+++ b/libraries/WiFi/src/WiFiClient.h
@@ -88,7 +88,6 @@ class WiFiClient : public ESPLwIPClient
     int fd() const;
 
     int setSocketOption(int option, char* value, size_t len);
-    int setSocketOption(int level, int option, const void* value, size_t len);
     int setOption(int option, int *value);
     int getOption(int option, int *value);
     int setNoDelay(bool nodelay);
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
index f7038223dde..fb708393791 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
@@ -389,14 +389,9 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
 
 int WiFiClientSecure::setSocketOption(int option, char* value, size_t len)
 {
-    return setSocketOption(SOL_SOCKET, option, (const void*)value, len);
-}
-
-int WiFiClientSecure::setSocketOption(int level, int option, const void* value, size_t len)
-{
-    int res = setsockopt(sslclient->socket, level, option, value, len);
+    int res = setsockopt(sslclient->socket, SOL_SOCKET, option, value, len);
     if(res < 0) {
-        log_e("fail on %d, errno: %d, \"%s\"", sslclient->socket, errno, strerror(errno));
+        log_e("%X : %d", option, errno);
     }
     return res;
 }
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.h b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
index e12e7d43fca..f2a61ccdf00 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.h
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
@@ -80,7 +80,6 @@ class WiFiClientSecure : public WiFiClient
     const mbedtls_x509_crt* getPeerCertificate() { return mbedtls_ssl_get_peer_cert(&sslclient->ssl_ctx); };
     bool getFingerprintSHA256(uint8_t sha256_result[32]) { return get_peer_fingerprint(sslclient, sha256_result); };
     int setSocketOption(int option, char* value, size_t len);
-    int setSocketOption(int level, int option, const void* value, size_t len);
 
     operator bool()
     {

From 5d97507087d55b916220e510a690cbaaaf6f9acf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Wed, 10 Aug 2022 08:27:44 +0200
Subject: [PATCH 16/17] Revert "Merge commit 'refs/pull/6676/head' of
 https://github.com/espressif/arduino-esp32 into pr/7043"

This reverts commit 6a584ef4f008cde308b72e6e4437b7b448377835, reversing
changes made to 8161e789406141132efb751c73981858b2c6ef84.
---
 libraries/HTTPClient/src/HTTPClient.cpp       |  4 +-
 .../httpUpdateSecure/httpUpdateSecure.ino     |  2 +-
 libraries/WebServer/src/WebServer.cpp         |  2 +-
 libraries/WiFi/src/WiFiClient.cpp             | 44 ++++++++-----------
 libraries/WiFi/src/WiFiClient.h               |  4 +-
 .../WiFiClientSecure/src/WiFiClientSecure.cpp | 43 +++++++-----------
 .../WiFiClientSecure/src/WiFiClientSecure.h   |  1 +
 7 files changed, 41 insertions(+), 59 deletions(-)

diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp
index 9115c43f62f..7e967be9f35 100644
--- a/libraries/HTTPClient/src/HTTPClient.cpp
+++ b/libraries/HTTPClient/src/HTTPClient.cpp
@@ -495,7 +495,7 @@ void HTTPClient::setTimeout(uint16_t timeout)
 {
     _tcpTimeout = timeout;
     if(connected()) {
-        _client->setTimeout(timeout);
+        _client->setTimeout((timeout + 500) / 1000);
     }
 }
 
@@ -1151,7 +1151,7 @@ bool HTTPClient::connect(void)
     }
 
     // set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
-    _client->setTimeout(_tcpTimeout);	
+    _client->setTimeout((_tcpTimeout + 500) / 1000);	
 
     log_d(" connected to %s:%u", _host.c_str(), _port);
 
diff --git a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino
index 1e26fbcee5a..1c85ace02a5 100644
--- a/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino
+++ b/libraries/HTTPUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino
@@ -96,7 +96,7 @@ void loop() {
     client.setCACert(rootCACertificate);
 
     // Reading data over SSL may be slow, use an adequate timeout
-    client.setTimeout(12000); // timeout argument is defined in miliseconds for setTimeout
+    client.setTimeout(12000 / 1000); // timeout argument is defined in seconds for setTimeout
 
     // The line below is optional. It can be used to blink the LED on the board during flashing
     // The LED will be on during download of one buffer of data from the network. The LED will
diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp
index 015e453300c..2acc8acb9e4 100644
--- a/libraries/WebServer/src/WebServer.cpp
+++ b/libraries/WebServer/src/WebServer.cpp
@@ -312,7 +312,7 @@ void WebServer::handleClient() {
         if (_parseRequest(_currentClient)) {
           // because HTTP_MAX_SEND_WAIT is expressed in milliseconds,
           // it must be divided by 1000
-          _currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
+          _currentClient.setTimeout(HTTP_MAX_SEND_WAIT / 1000);
           _contentLength = CONTENT_LENGTH_NOT_SET;
           _handleRequest();
 
diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index 7a2fa6d3847..69a691995bd 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -204,8 +204,6 @@ void WiFiClient::stop()
     clientSocketHandle = NULL;
     _rxBuffer = NULL;
     _connected = false;
-    _lastReadTimeout = 0;
-    _lastWriteTimeout = 0;
 }
 
 int WiFiClient::connect(IPAddress ip, uint16_t port)
@@ -312,6 +310,24 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
     return res;
 }
 
+int WiFiClient::setTimeout(uint32_t seconds)
+{
+    Client::setTimeout(seconds * 1000); // This should be here?
+    _timeout = seconds * 1000;
+    if(fd() >= 0) {
+        struct timeval tv;
+        tv.tv_sec = seconds;
+        tv.tv_usec = 0;
+        if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
+            return -1;
+        }
+        return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
+    }
+    else {
+        return 0;
+    }
+}
+
 int WiFiClient::setOption(int option, int *value)
 {
     int res = setsockopt(fd(), IPPROTO_TCP, option, (char *) value, sizeof(int));
@@ -384,18 +400,6 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
         tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US;
         retry--;
 
-        if(_lastWriteTimeout != _timeout){
-            if(fd() >= 0){
-                struct timeval timeout_tv;
-                timeout_tv.tv_sec = _timeout / 1000;
-                timeout_tv.tv_usec = (_timeout % 1000) * 1000;
-                if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
-                {
-                    _lastWriteTimeout = _timeout;
-                }
-            }
-        }
-
         if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) {
             return 0;
         }
@@ -455,18 +459,6 @@ size_t WiFiClient::write(Stream &stream)
 
 int WiFiClient::read(uint8_t *buf, size_t size)
 {
-    if(_lastReadTimeout != _timeout){
-        if(fd() >= 0){
-            struct timeval timeout_tv;
-            timeout_tv.tv_sec = _timeout / 1000;
-            timeout_tv.tv_usec = (_timeout % 1000) * 1000;
-            if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
-            {
-                _lastReadTimeout = _timeout;
-            }
-        }
-    }
-
     int res = -1;
     if (_rxBuffer) {
         res = _rxBuffer->read(buf, size);
diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h
index 8fc67950f04..5aaa6ba115e 100644
--- a/libraries/WiFi/src/WiFiClient.h
+++ b/libraries/WiFi/src/WiFiClient.h
@@ -33,6 +33,7 @@ class ESPLwIPClient : public Client
 public:
         virtual int connect(IPAddress ip, uint16_t port, int32_t timeout) = 0;
         virtual int connect(const char *host, uint16_t port, int32_t timeout) = 0;
+        virtual int setTimeout(uint32_t seconds) = 0;
 };
 
 class WiFiClient : public ESPLwIPClient
@@ -42,8 +43,6 @@ class WiFiClient : public ESPLwIPClient
     std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
     bool _connected;
     int _timeout;
-    int _lastWriteTimeout;
-    int _lastReadTimeout;
 
 public:
     WiFiClient *next;
@@ -90,6 +89,7 @@ class WiFiClient : public ESPLwIPClient
     int setSocketOption(int option, char* value, size_t len);
     int setOption(int option, int *value);
     int getOption(int option, int *value);
+    int setTimeout(uint32_t seconds);
     int setNoDelay(bool nodelay);
     bool getNoDelay();
 
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
index fb708393791..cf22082af06 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
@@ -54,8 +54,6 @@ WiFiClientSecure::WiFiClientSecure(int sock)
 {
     _connected = false;
     _timeout = 30000; // Same default as ssl_client
-    _lastReadTimeout = 0;
-    _lastWriteTimeout = 0;
 
     sslclient = new sslclient_context;
     ssl_init(sslclient);
@@ -96,8 +94,6 @@ void WiFiClientSecure::stop()
         sslclient->socket = -1;
         _connected = false;
         _peek = -1;
-        _lastReadTimeout = 0;
-        _lastWriteTimeout = 0;
     }
     stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
 }
@@ -189,16 +185,6 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
     if (!_connected) {
         return 0;
     }
-    if(_lastWriteTimeout != _timeout){
-        struct timeval timeout_tv;
-        timeout_tv.tv_sec = _timeout / 1000;
-        timeout_tv.tv_usec = (_timeout % 1000) * 1000;
-        if(setSocketOption(SO_SNDTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
-        {
-            _lastWriteTimeout = _timeout;
-        }
-    }
-
     int res = send_ssl_data(sslclient, buf, size);
     if (res < 0) {
         stop();
@@ -209,18 +195,6 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
 
 int WiFiClientSecure::read(uint8_t *buf, size_t size)
 {
-    if(_lastReadTimeout != _timeout){
-        if(fd() >= 0){
-            struct timeval timeout_tv;
-            timeout_tv.tv_sec = _timeout / 1000;
-            timeout_tv.tv_usec = (_timeout % 1000) * 1000;
-            if(setSocketOption(SO_RCVTIMEO, (char *)&timeout_tv, sizeof(struct timeval)) >= 0)
-            {
-                _lastReadTimeout = _timeout;
-            }
-        }
-    }
-
     int peeked = 0;
     int avail = available();
     if ((!buf && size) || avail <= 0) {
@@ -386,7 +360,22 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
 {
     _alpn_protos = alpn_protos;
 }
-
+int WiFiClientSecure::setTimeout(uint32_t seconds)
+{
+    _timeout = seconds * 1000;
+    if (sslclient->socket >= 0) {
+        struct timeval tv;
+        tv.tv_sec = seconds;
+        tv.tv_usec = 0;
+        if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
+            return -1;
+        }
+        return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
+    }
+    else {
+        return 0;
+    }
+}
 int WiFiClientSecure::setSocketOption(int option, char* value, size_t len)
 {
     int res = setsockopt(sslclient->socket, SOL_SOCKET, option, value, len);
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.h b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
index f2a61ccdf00..9ea0c04eb4e 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.h
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
@@ -79,6 +79,7 @@ class WiFiClientSecure : public WiFiClient
     void setAlpnProtocols(const char **alpn_protos);
     const mbedtls_x509_crt* getPeerCertificate() { return mbedtls_ssl_get_peer_cert(&sslclient->ssl_ctx); };
     bool getFingerprintSHA256(uint8_t sha256_result[32]) { return get_peer_fingerprint(sslclient, sha256_result); };
+    int setTimeout(uint32_t seconds);
     int setSocketOption(int option, char* value, size_t len);
 
     operator bool()

From c1d88592cd267f84871a811be43b5a8b3d6eac56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Wed, 10 Aug 2022 08:36:49 +0200
Subject: [PATCH 17/17] Revert "Revert "Merge branch 'erase_flash' of
 https://github.com/PilnyTomas/arduino-esp32 into pr/7043""

This reverts commit 55d2a29b73943c12ad2a0388c642a2c213d60f62.
---
 libraries/WiFi/src/WiFiClient.cpp                 | 15 ++++++++-------
 libraries/WiFi/src/WiFiClient.h                   |  1 +
 .../WiFiClientSecure/src/WiFiClientSecure.cpp     |  9 +++++++--
 libraries/WiFiClientSecure/src/WiFiClientSecure.h |  1 +
 4 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/libraries/WiFi/src/WiFiClient.cpp b/libraries/WiFi/src/WiFiClient.cpp
index 69a691995bd..6e56e94ac53 100644
--- a/libraries/WiFi/src/WiFiClient.cpp
+++ b/libraries/WiFi/src/WiFiClient.cpp
@@ -303,9 +303,14 @@ int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout)
 
 int WiFiClient::setSocketOption(int option, char* value, size_t len)
 {
-    int res = setsockopt(fd(), SOL_SOCKET, option, value, len);
+    return setSocketOption(SOL_SOCKET, option, (const void*)value, len);
+}
+
+int WiFiClient::setSocketOption(int level, int option, const void* value, size_t len)
+{
+    int res = setsockopt(fd(), level, option, value, len);
     if(res < 0) {
-        log_e("%X : %d", option, errno);
+        log_e("fail on %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
     }
     return res;
 }
@@ -330,11 +335,7 @@ int WiFiClient::setTimeout(uint32_t seconds)
 
 int WiFiClient::setOption(int option, int *value)
 {
-    int res = setsockopt(fd(), IPPROTO_TCP, option, (char *) value, sizeof(int));
-    if(res < 0) {
-        log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
-    }
-    return res;
+    return setSocketOption(IPPROTO_TCP, option, (const void*)value, sizeof(int));
 }
 
 int WiFiClient::getOption(int option, int *value)
diff --git a/libraries/WiFi/src/WiFiClient.h b/libraries/WiFi/src/WiFiClient.h
index 5aaa6ba115e..f36c51102e7 100644
--- a/libraries/WiFi/src/WiFiClient.h
+++ b/libraries/WiFi/src/WiFiClient.h
@@ -87,6 +87,7 @@ class WiFiClient : public ESPLwIPClient
     int fd() const;
 
     int setSocketOption(int option, char* value, size_t len);
+    int setSocketOption(int level, int option, const void* value, size_t len);
     int setOption(int option, int *value);
     int getOption(int option, int *value);
     int setTimeout(uint32_t seconds);
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
index cf22082af06..8862ed7ce07 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
@@ -378,9 +378,14 @@ int WiFiClientSecure::setTimeout(uint32_t seconds)
 }
 int WiFiClientSecure::setSocketOption(int option, char* value, size_t len)
 {
-    int res = setsockopt(sslclient->socket, SOL_SOCKET, option, value, len);
+    return setSocketOption(SOL_SOCKET, option, (const void*)value, len);
+}
+
+int WiFiClientSecure::setSocketOption(int level, int option, const void* value, size_t len)
+{
+    int res = setsockopt(sslclient->socket, level, option, value, len);
     if(res < 0) {
-        log_e("%X : %d", option, errno);
+        log_e("fail on %d, errno: %d, \"%s\"", sslclient->socket, errno, strerror(errno));
     }
     return res;
 }
diff --git a/libraries/WiFiClientSecure/src/WiFiClientSecure.h b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
index 9ea0c04eb4e..5070d424743 100644
--- a/libraries/WiFiClientSecure/src/WiFiClientSecure.h
+++ b/libraries/WiFiClientSecure/src/WiFiClientSecure.h
@@ -81,6 +81,7 @@ class WiFiClientSecure : public WiFiClient
     bool getFingerprintSHA256(uint8_t sha256_result[32]) { return get_peer_fingerprint(sslclient, sha256_result); };
     int setTimeout(uint32_t seconds);
     int setSocketOption(int option, char* value, size_t len);
+    int setSocketOption(int level, int option, const void* value, size_t len);
 
     operator bool()
     {