diff --git a/README.md b/README.md index 490bdf4..89ab706 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,20 @@ * [Use Arduino Library Manager](#use-arduino-library-manager) * [Manual Install](#manual-install) * [VS Code & PlatformIO](#vs-code--platformio) +* [Note for Platform IO using ESP32 LittleFS](#note-for-platform-io-using-esp32-littlefs) +* [HOWTO Fix `Multiple Definitions` Linker Error](#howto-fix-multiple-definitions-linker-error) +* [Note for Platform IO using ESP32 LittleFS](#note-for-platform-io-using-esp32-littlefs) +* [HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)](#howto-use-analogread-with-esp32-running-wifi-andor-bluetooth-btble) + * [1. ESP32 has 2 ADCs, named ADC1 and ADC2](#1--esp32-has-2-adcs-named-adc1-and-adc2) + * [2. ESP32 ADCs functions](#2-esp32-adcs-functions) + * [3. ESP32 WiFi uses ADC2 for WiFi functions](#3-esp32-wifi-uses-adc2-for-wifi-functions) * [Orignal documentation](#Orignal-documentation) * [AsyncSSLClient](#AsyncSSLClient) * [Debug Terminal Output Samples](#debug-terminal-output-samples) * [1. AsyncHTTPSRequest_ESP on ESP32_DEV](#1-AsyncHTTPSRequest_ESP-on-ESP32_DEV) * [2. AsyncHTTPSRequest_ESP on ESP32S2_DEV](#2-AsyncHTTPSRequest_ESP-on-ESP32S2_DEV) + * [3. AsyncHTTPSRequest_ESP on ESP32C3_DEV](#3-AsyncHTTPSRequest_ESP-on-ESP32C3_DEV) + * [4. AsyncHTTPSRequest_ESP_WiFiManager on ESP32_DEV](#4-AsyncHTTPSRequest_ESP_WiFiManager-on-ESP32_DEV) * [Debug](#debug) * [Troubleshooting](#troubleshooting) * [Issues](#issues) @@ -114,6 +123,105 @@ Another way to install is to: --- +### Note for Platform IO using ESP32 LittleFS + +In Platform IO, to fix the error when using [`LittleFS_esp32 v1.0`](https://github.com/lorol/LITTLEFS) for ESP32-based boards with ESP32 core v1.0.4- (ESP-IDF v3.2-), uncomment the following line + +from + +``` +//#define CONFIG_LITTLEFS_FOR_IDF_3_2 /* For old IDF - like in release 1.0.4 */ +``` + +to + +``` +#define CONFIG_LITTLEFS_FOR_IDF_3_2 /* For old IDF - like in release 1.0.4 */ +``` + +It's advisable to use the latest [`LittleFS_esp32 v1.0.5+`](https://github.com/lorol/LITTLEFS) to avoid the issue. + +Thanks to [Roshan](https://github.com/solroshan) to report the issue in [Error esp_littlefs.c 'utime_p'](https://github.com/khoih-prog/ESPAsync_WiFiManager/issues/28) + +--- +--- + +### HOWTO Fix `Multiple Definitions` Linker Error + +The current library implementation, using xyz-Impl.h instead of standard xyz.cpp, possibly creates certain `Multiple Definitions` Linker error in certain use cases. Although it's simple to just modify several lines of code, either in the library or in the application, the library is adding a separate source directory, named src_cpp, besides the standard src directory. + +To use the old standard cpp way, just + +1. **Rename the h-only src directory into src_h.** +2. **Then rename the cpp src_cpp directory into src.** +3. Close then reopen the application code in Arduino IDE, etc. to recompile from scratch. + +--- +--- + +### Note for Platform IO using ESP32 LittleFS + +In Platform IO, to fix the error when using [`LittleFS_esp32 v1.0`](https://github.com/lorol/LITTLEFS) for ESP32-based boards with ESP32 core v1.0.4- (ESP-IDF v3.2-), uncomment the following line + +from + +``` +//#define CONFIG_LITTLEFS_FOR_IDF_3_2 /* For old IDF - like in release 1.0.4 */ +``` + +to + +``` +#define CONFIG_LITTLEFS_FOR_IDF_3_2 /* For old IDF - like in release 1.0.4 */ +``` + +It's advisable to use the latest [`LittleFS_esp32 v1.0.5+`](https://github.com/lorol/LITTLEFS) to avoid the issue. + +Thanks to [Roshan](https://github.com/solroshan) to report the issue in [Error esp_littlefs.c 'utime_p'](https://github.com/khoih-prog/ESPAsync_WiFiManager/issues/28) + +--- +--- + +### HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE) + +Please have a look at [**ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example**](https://github.com/khoih-prog/ESP_WiFiManager/issues/39) to have more detailed description and solution of the issue. + +#### 1. ESP32 has 2 ADCs, named ADC1 and ADC2 + +#### 2. ESP32 ADCs functions + +- ADC1 controls ADC function for pins **GPIO32-GPIO39** +- ADC2 controls ADC function for pins **GPIO0, 2, 4, 12-15, 25-27** + +#### 3.. ESP32 WiFi uses ADC2 for WiFi functions + +Look in file [**adc_common.c**](https://github.com/espressif/esp-idf/blob/master/components/driver/adc_common.c#L61) + +> In ADC2, there're two locks used for different cases: +> 1. lock shared with app and Wi-Fi: +> ESP32: +> When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed. +> ESP32S2: +> The controller's control over the ADC is determined by the arbiter. There is no need to control by lock. +> +> 2. lock shared between tasks: +> when several tasks sharing the ADC2, we want to guarantee +> all the requests will be handled. +> Since conversions are short (about 31us), app returns the lock very soon, +> we use a spinlock to stand there waiting to do conversions one by one. +> +> adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock. + + +- In order to use ADC2 for other functions, we have to **acquire complicated firmware locks and very difficult to do** +- So, it's not advisable to use ADC2 with WiFi/BlueTooth (BT/BLE). +- Use ADC1, and pins GPIO32-GPIO39 +- If somehow it's a must to use those pins serviced by ADC2 (**GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27**), use the **fix mentioned at the end** of [**ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example**](https://github.com/khoih-prog/ESP_WiFiManager/issues/39) to work with ESP32 WiFi/BlueTooth (BT/BLE). + +--- +--- + + ## Orignal documentation For ESP32, check [AsyncTCP Library](https://github.com/me-no-dev/AsyncTCP) @@ -132,11 +240,11 @@ The base classes on which everything else is built. They expose all possible sce #### 1. AsyncHTTPSRequest_ESP on ESP32_DEV -Following is the debug terminal when running example [AsyncHTTPSRequest_ESP](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/tree/main/examples/AsyncHTTPSRequest_ESP) on ESP32_DEV to demonstrate the operation of SSL Async HTTPS request, based on this [AsyncTCP_SSL Library](https://github.com/khoih-prog/AsyncTCP_SSL). +Following is the debug terminal when running example [AsyncHTTPSRequest_ESP](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/tree/main/examples/AsyncHTTPSRequest_ESP) on ESP32_DEV to demonstrate the operation of SSL Async HTTPS request, using [AsyncTCP_SSL Library](https://github.com/khoih-prog/AsyncTCP_SSL). ``` Starting AsyncHTTPSRequest_ESP using ESP32_DEV -AsyncTCP_SSL v1.0.0 +AsyncTCP_SSL v1.1.0 AsyncHTTPSRequest_Generic v1.0.0 Connecting to WiFi SSID: HueNet1 ....... @@ -182,12 +290,11 @@ week_number: 42 #### 2. AsyncHTTPSRequest_ESP on ESP32S2_DEV -Following is the debug terminal when running example [AsyncHTTPSRequest_ESP](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/tree/main/examples/AsyncHTTPSRequest_ESP) on ESP32S2_DEV to demonstrate the operation of SSL Async HTTPS request, based on this [AsyncTCP_SSL Library](https://github.com/khoih-prog/AsyncTCP_SSL). +Following is the debug terminal when running example [AsyncHTTPSRequest_ESP](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/tree/main/examples/AsyncHTTPSRequest_ESP) on ESP32S2_DEV to demonstrate the operation of SSL Async HTTPS request, using [AsyncTCP_SSL Library](https://github.com/khoih-prog/AsyncTCP_SSL). ``` - Starting AsyncHTTPSRequest_ESP using ESP32S2_DEV -AsyncTCP_SSL v1.0.0 +AsyncTCP_SSL v1.1.0 AsyncHTTPSRequest_Generic v1.0.0 Connecting to WiFi SSID: HueNet1 ....... @@ -234,7 +341,75 @@ utc_datetime: 2021-10-22T03:19:43.835205+00:00 utc_offset: -04:00 week_number: 42 ************************************** +``` + +--- + +#### 3. AsyncHTTPSRequest_ESP on ESP32C3_DEV + +Following is the debug terminal when running example [AsyncHTTPSRequest_ESP](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/tree/main/examples/AsyncHTTPSRequest_ESP) on ESP32C3_DEV to demonstrate the operation of SSL Async HTTPS request, using [AsyncTCP_SSL Library](https://github.com/khoih-prog/AsyncTCP_SSL). + +``` +Starting AsyncHTTPSRequest_ESP using ESP32C3_DEV +AsyncTCP_SSL v1.1.0 +AsyncHTTPSRequest_Generic v1.0.0 +Connecting to WiFi SSID: HueNet1 +......... +AsyncHTTPSRequest @ IP : 192.168.2.80 + +************************************** +abbreviation: EDT +client_ip: aaa.bbb.ccc.ddd +datetime: 2021-10-22T02:00:44.009661-04:00 +day_of_week: 5 +day_of_year: 295 +dst: true +dst_from: 2021-03-14T07:00:00+00:00 +dst_offset: 3600 +dst_until: 2021-11-07T06:00:00+00:00 +raw_offset: -18000 +timezone: America/Toronto +unixtime: 1634882444 +utc_datetime: 2021-10-22T06:00:44.009661+00:00 +utc_offset: -04:00 +week_number: 42 +************************************** +``` +--- + +#### 4. AsyncHTTPSRequest_ESP_WiFiManager on ESP32_DEV + +Following is the debug terminal when running example [AsyncHTTPSRequest_ESP_WiFiManager](https://github.com/khoih-prog/AsyncHTTPSRequest_Generic/tree/main/examples/AsyncHTTPSRequest_ESP_WiFiManager) on ESP32_DEV to demonstrate the operation of SSL Async HTTPS request, using [AsyncTCP_SSL Library](https://github.com/khoih-prog/AsyncTCP_SSL), and [ESPAsync_WiFiManager Library](https://github.com/khoih-prog/ESPAsync_WiFiManager) + +``` +Starting AsyncHTTPSRequest_ESP_WiFiManager using LittleFS on ESP32_DEV +ESPAsync_WiFiManager v1.9.4 +AsyncTCP_SSL v1.1.0 +AsyncHTTPSRequest_Generic v1.0.0 +Stored: SSID = HueNet1, Pass = 12345678 +Got stored Credentials. Timeout 120s +ConnectMultiWiFi in setup +After waiting 11.38 secs more in setup(), connection result is connected. Local IP: 192.168.2.232 +H +************************************** +abbreviation: EDT +client_ip: 216.154.33.167 +datetime: 2021-10-22T02:38:12.722777-04:00 +day_of_week: 5 +day_of_year: 295 +dst: true +dst_from: 2021-03-14T07:00:00+00:00 +dst_offset: 3600 +dst_until: 2021-11-07T06:00:00+00:00 +raw_offset: -18000 +timezone: America/Toronto +unixtime: 1634884692 +utc_datetime: 2021-10-22T06:38:12.722777+00:00 +utc_offset: -04:00 +week_number: 42 +************************************** +H ``` --- diff --git a/changelog.md b/changelog.md index 4df9a2e..c34a610 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.1.0](#Releases-v110) * [Initial Releases v1.0.0](#Initial-Releases-v100) --- @@ -18,6 +19,10 @@ ## Changelog +### Releases v1.1.0 + +1. Fix duplication bug when using `src_h` +2. Enable coexistence with AsyncTCP ### Initial Releases v1.0.0 diff --git a/library.json b/library.json index 938720a..a2fd58f 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name":"AsyncTCP_SSL", - "version": "1.0.0", + "version": "1.1.0", "keywords":"communication, async, tcp, ssl, tls, mbed, free-rtos", "description":"Asynchronous SSL TCP Library for ESP32. This library is the base for future and more advanced Async SSL libraries, such as AsyncSSLWebServer, AsyncHTTPSRequest", "authors": diff --git a/library.properties b/library.properties index 9427889..5d5ab9c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AsyncTCP_SSL -version=1.0.0 +version=1.1.0 author=Hristo Gochkov, Maarten Fremouw, Thorsten von Eicken, Khoi Hoang maintainer=Khoi Hoang sentence=Asynchronous SSL TCP Library for ESP32. diff --git a/src/AsyncTCP_SSL.h b/src/AsyncTCP_SSL.h index 83a5924..85e82ac 100644 --- a/src/AsyncTCP_SSL.h +++ b/src/AsyncTCP_SSL.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ /* @@ -50,7 +51,7 @@ #error This AsyncTCP_SSL library is supporting only ESP32 #endif -#define ASYNC_TCP_SSL_VERSION "AsyncTCP_SSL v1.0.0" +#define ASYNC_TCP_SSL_VERSION "AsyncTCP_SSL v1.1.0" #define ASYNC_TCP_SSL_ENABLED true @@ -85,12 +86,21 @@ class AsyncSSLClient; #define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react. #define SSL_HANDSHAKE_TIMEOUT 5000 // timeout to complete SSL handshake +#if ASYNC_TCP_SSL_ENABLED +typedef std::function AcConnectHandlerSSL; +typedef std::function AcAckHandlerSSL; +typedef std::function AcErrorHandlerSSL; +typedef std::function AcDataHandlerSSL; +typedef std::function AcPacketHandlerSSL; +typedef std::function AcTimeoutHandlerSSL; +#else typedef std::function AcConnectHandler; typedef std::function AcAckHandler; typedef std::function AcErrorHandler; typedef std::function AcDataHandler; typedef std::function AcPacketHandler; typedef std::function AcTimeoutHandler; +#endif struct tcp_pcb; struct ip_addr; @@ -166,6 +176,16 @@ class AsyncSSLClient IPAddress localIP(); uint16_t localPort(); +#if ASYNC_TCP_SSL_ENABLED + void onConnect(AcConnectHandlerSSL cb, void* arg = 0); //on successful connect + void onDisconnect(AcConnectHandlerSSL cb, void* arg = 0); //disconnected + void onAck(AcAckHandlerSSL cb, void* arg = 0); //ack received + void onError(AcErrorHandlerSSL cb, void* arg = 0); //unsuccessful connect or error + void onData(AcDataHandlerSSL cb, void* arg = 0); //data received (called if onPacket is not used) + void onPacket(AcPacketHandlerSSL cb, void* arg = 0); //data received + void onTimeout(AcTimeoutHandlerSSL cb, void* arg = 0); //ack timeout + void onPoll(AcConnectHandlerSSL cb, void* arg = 0); //every 125ms when connected +#else void onConnect(AcConnectHandler cb, void* arg = 0); //on successful connect void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected void onAck(AcAckHandler cb, void* arg = 0); //ack received @@ -174,6 +194,7 @@ class AsyncSSLClient void onPacket(AcPacketHandler cb, void* arg = 0); //data received void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected +#endif void ackPacket(struct pbuf * pb);//ack pbuf from onPacket size_t ack(size_t len); //ack data that you have not acked using the method below @@ -221,6 +242,24 @@ class AsyncSSLClient std::string _hostname; int8_t _closed_slot; +#if ASYNC_TCP_SSL_ENABLED + AcConnectHandlerSSL _connect_cb; + void* _connect_cb_arg; + AcConnectHandlerSSL _discard_cb; + void* _discard_cb_arg; + AcAckHandlerSSL _sent_cb; + void* _sent_cb_arg; + AcErrorHandlerSSL _error_cb; + void* _error_cb_arg; + AcDataHandlerSSL _recv_cb; + void* _recv_cb_arg; + AcPacketHandlerSSL _pb_cb; + void* _pb_cb_arg; + AcTimeoutHandlerSSL _timeout_cb; + void* _timeout_cb_arg; + AcConnectHandlerSSL _poll_cb; + void* _poll_cb_arg; +#else AcConnectHandler _connect_cb; void* _connect_cb_arg; AcConnectHandler _discard_cb; @@ -237,6 +276,7 @@ class AsyncSSLClient void* _timeout_cb_arg; AcConnectHandler _poll_cb; void* _poll_cb_arg; +#endif bool _pcb_busy; uint32_t _pcb_sent_at; @@ -282,23 +322,27 @@ class AsyncSSLClient }; #if ASYNC_TCP_SSL_ENABLED -typedef std::function AcSSlFileHandler; +typedef std::function AcSSlFileHandlerSSL; #endif ///////////////////////////////////////////////// -class AsyncServer +class AsyncSSLServer { public: - AsyncServer(IPAddress addr, uint16_t port); - AsyncServer(uint16_t port); - ~AsyncServer(); - void onClient(AcConnectHandler cb, void* arg); + AsyncSSLServer(IPAddress addr, uint16_t port); + AsyncSSLServer(uint16_t port); + ~AsyncSSLServer(); + //void onClient(AcConnectHandler cb, void* arg); #if ASYNC_TCP_SSL_ENABLED + void onClient(AcConnectHandlerSSL cb, void* arg); + // Dummy, so it compiles with ESP Async WebServer library enabled. - void onSslFileRequest(AcSSlFileHandler cb, void* arg) {}; + void onSslFileRequest(AcSSlFileHandlerSSL cb, void* arg) {}; void beginSecure(const char *cert, const char *private_key_file, const char *password) {}; +#else + void onClient(AcConnectHandler cb, void* arg); #endif void begin(); @@ -317,7 +361,13 @@ class AsyncServer bool _noDelay; tcp_pcb* _pcb; + +#if ASYNC_TCP_SSL_ENABLED + AcConnectHandlerSSL _connect_cb; +#else AcConnectHandler _connect_cb; +#endif + void* _connect_cb_arg; int8_t _accept(tcp_pcb* newpcb, int8_t err); diff --git a/src/AsyncTCP_SSL.hpp b/src/AsyncTCP_SSL.hpp index 7b98925..c42d57b 100644 --- a/src/AsyncTCP_SSL.hpp +++ b/src/AsyncTCP_SSL.hpp @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ /* @@ -145,7 +146,7 @@ static xQueueHandle _async_queue; static TaskHandle_t _async_service_task_handle = NULL; -SemaphoreHandle_t _slots_lock; +static SemaphoreHandle_t _slots_lock; const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP; static int _closed_slots[_number_of_closed_slots]; @@ -289,7 +290,7 @@ static void _handle_async_event(lwip_event_packet_t * e) else if (e->event == LWIP_TCP_ACCEPT) { ATCP_HEXLOGINFO2("_handle_async_event: LWIP_TCP_ACCEPT =", (uint32_t) e->arg, (uint32_t) e->accept.client); - AsyncServer::_s_accepted(e->arg, e->accept.client); + AsyncSSLServer::_s_accepted(e->arg, e->accept.client); } else if (e->event == LWIP_TCP_DNS) { @@ -968,49 +969,81 @@ AsyncSSLClient & AsyncSSLClient::operator+=(const AsyncSSLClient &other) Callback Setters * */ +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onConnect(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onConnect(AcConnectHandler cb, void* arg) +#endif { _connect_cb = cb; _connect_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onDisconnect(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onDisconnect(AcConnectHandler cb, void* arg) +#endif { _discard_cb = cb; _discard_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onAck(AcAckHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onAck(AcAckHandler cb, void* arg) +#endif { _sent_cb = cb; _sent_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onError(AcErrorHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onError(AcErrorHandler cb, void* arg) +#endif { _error_cb = cb; _error_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onData(AcDataHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onData(AcDataHandler cb, void* arg) +#endif { _recv_cb = cb; _recv_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onPacket(AcPacketHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onPacket(AcPacketHandler cb, void* arg) +#endif { _pb_cb = cb; _pb_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onTimeout(AcTimeoutHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onTimeout(AcTimeoutHandler cb, void* arg) +#endif { _timeout_cb = cb; _timeout_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onPoll(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onPoll(AcConnectHandler cb, void* arg) +#endif { _poll_cb = cb; _poll_cb_arg = arg; @@ -1995,7 +2028,7 @@ void AsyncSSLClient::_s_ssl_error(void *arg, struct tcp_pcb *tcp, int8_t err) Async TCP Server */ -AsyncServer::AsyncServer(IPAddress addr, uint16_t port) +AsyncSSLServer::AsyncSSLServer(IPAddress addr, uint16_t port) : _port(port) , _addr(addr) , _noDelay(false) @@ -2004,7 +2037,7 @@ AsyncServer::AsyncServer(IPAddress addr, uint16_t port) , _connect_cb_arg(0) {} -AsyncServer::AsyncServer(uint16_t port) +AsyncSSLServer::AsyncSSLServer(uint16_t port) : _port(port) , _addr((uint32_t) IPADDR_ANY) , _noDelay(false) @@ -2013,18 +2046,22 @@ AsyncServer::AsyncServer(uint16_t port) , _connect_cb_arg(0) {} -AsyncServer::~AsyncServer() +AsyncSSLServer::~AsyncSSLServer() { end(); } -void AsyncServer::onClient(AcConnectHandler cb, void* arg) +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLServer::onClient(AcConnectHandlerSSL cb, void* arg) +#else +void AsyncSSLServer::onClient(AcConnectHandler cb, void* arg) +#endif { _connect_cb = cb; _connect_cb_arg = arg; } -void AsyncServer::begin() +void AsyncSSLServer::begin() { if (_pcb) { @@ -2076,7 +2113,7 @@ void AsyncServer::begin() tcp_accept(_pcb, &_s_accept); } -void AsyncServer::end() +void AsyncSSLServer::end() { if (_pcb) { @@ -2093,7 +2130,7 @@ void AsyncServer::end() } //runs on LwIP thread -int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err) +int8_t AsyncSSLServer::_accept(tcp_pcb* pcb, int8_t err) { ATCP_HEXLOGDEBUG1("_accept: pcb =", (uint32_t) pcb); @@ -2118,7 +2155,7 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err) return ERR_OK; } -int8_t AsyncServer::_accepted(AsyncSSLClient* client) +int8_t AsyncSSLServer::_accepted(AsyncSSLClient* client) { if (_connect_cb) { @@ -2128,17 +2165,17 @@ int8_t AsyncServer::_accepted(AsyncSSLClient* client) return ERR_OK; } -void AsyncServer::setNoDelay(bool nodelay) +void AsyncSSLServer::setNoDelay(bool nodelay) { _noDelay = nodelay; } -bool AsyncServer::getNoDelay() +bool AsyncSSLServer::getNoDelay() { return _noDelay; } -uint8_t AsyncServer::status() +uint8_t AsyncSSLServer::status() { if (!_pcb) { @@ -2148,14 +2185,14 @@ uint8_t AsyncServer::status() return _pcb->state; } -int8_t AsyncServer::_s_accept(void * arg, tcp_pcb * pcb, int8_t err) +int8_t AsyncSSLServer::_s_accept(void * arg, tcp_pcb * pcb, int8_t err) { - return reinterpret_cast(arg)->_accept(pcb, err); + return reinterpret_cast(arg)->_accept(pcb, err); } -int8_t AsyncServer::_s_accepted(void *arg, AsyncSSLClient* client) +int8_t AsyncSSLServer::_s_accepted(void *arg, AsyncSSLClient* client) { - return reinterpret_cast(arg)->_accepted(client); + return reinterpret_cast(arg)->_accepted(client); } #endif /* ASYNCTCP_SSL_HPP_ */ diff --git a/src/AsyncTCP_SSL_Debug.h b/src/AsyncTCP_SSL_Debug.h index 956c67c..a8dc87a 100644 --- a/src/AsyncTCP_SSL_Debug.h +++ b/src/AsyncTCP_SSL_Debug.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #pragma once diff --git a/src/tcp_mbedtls.c b/src/tcp_mbedtls.c index d6bcc12..09f474c 100644 --- a/src/tcp_mbedtls.c +++ b/src/tcp_mbedtls.c @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #ifndef _ASYNC_TCP_SSL_LOGLEVEL_ diff --git a/src/tcp_mbedtls.h b/src/tcp_mbedtls.h index 912bd7a..982312d 100644 --- a/src/tcp_mbedtls.h +++ b/src/tcp_mbedtls.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #ifndef LWIPR_MBEDTLS_H diff --git a/src_cpp/AsyncTCP_SSL.cpp b/src_cpp/AsyncTCP_SSL.cpp index bd6136c..4218cc0 100644 --- a/src_cpp/AsyncTCP_SSL.cpp +++ b/src_cpp/AsyncTCP_SSL.cpp @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ /* @@ -142,7 +143,7 @@ static xQueueHandle _async_queue; static TaskHandle_t _async_service_task_handle = NULL; -SemaphoreHandle_t _slots_lock; +static SemaphoreHandle_t _slots_lock; const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP; static int _closed_slots[_number_of_closed_slots]; @@ -286,7 +287,7 @@ static void _handle_async_event(lwip_event_packet_t * e) else if (e->event == LWIP_TCP_ACCEPT) { ATCP_HEXLOGINFO2("_handle_async_event: LWIP_TCP_ACCEPT =", (uint32_t) e->arg, (uint32_t) e->accept.client); - AsyncServer::_s_accepted(e->arg, e->accept.client); + AsyncSSLServer::_s_accepted(e->arg, e->accept.client); } else if (e->event == LWIP_TCP_DNS) { @@ -965,49 +966,81 @@ AsyncSSLClient & AsyncSSLClient::operator+=(const AsyncSSLClient &other) Callback Setters * */ +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onConnect(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onConnect(AcConnectHandler cb, void* arg) +#endif { _connect_cb = cb; _connect_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onDisconnect(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onDisconnect(AcConnectHandler cb, void* arg) +#endif { _discard_cb = cb; _discard_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onAck(AcAckHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onAck(AcAckHandler cb, void* arg) +#endif { _sent_cb = cb; _sent_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onError(AcErrorHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onError(AcErrorHandler cb, void* arg) +#endif { _error_cb = cb; _error_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onData(AcDataHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onData(AcDataHandler cb, void* arg) +#endif { _recv_cb = cb; _recv_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onPacket(AcPacketHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onPacket(AcPacketHandler cb, void* arg) +#endif { _pb_cb = cb; _pb_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onTimeout(AcTimeoutHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onTimeout(AcTimeoutHandler cb, void* arg) +#endif { _timeout_cb = cb; _timeout_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onPoll(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onPoll(AcConnectHandler cb, void* arg) +#endif { _poll_cb = cb; _poll_cb_arg = arg; @@ -1992,7 +2025,7 @@ void AsyncSSLClient::_s_ssl_error(void *arg, struct tcp_pcb *tcp, int8_t err) Async TCP Server */ -AsyncServer::AsyncServer(IPAddress addr, uint16_t port) +AsyncSSLServer::AsyncSSLServer(IPAddress addr, uint16_t port) : _port(port) , _addr(addr) , _noDelay(false) @@ -2001,7 +2034,7 @@ AsyncServer::AsyncServer(IPAddress addr, uint16_t port) , _connect_cb_arg(0) {} -AsyncServer::AsyncServer(uint16_t port) +AsyncSSLServer::AsyncSSLServer(uint16_t port) : _port(port) , _addr((uint32_t) IPADDR_ANY) , _noDelay(false) @@ -2010,18 +2043,22 @@ AsyncServer::AsyncServer(uint16_t port) , _connect_cb_arg(0) {} -AsyncServer::~AsyncServer() +AsyncSSLServer::~AsyncSSLServer() { end(); } -void AsyncServer::onClient(AcConnectHandler cb, void* arg) +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLServer::onClient(AcConnectHandlerSSL cb, void* arg) +#else +void AsyncSSLServer::onClient(AcConnectHandler cb, void* arg) +#endif { _connect_cb = cb; _connect_cb_arg = arg; } -void AsyncServer::begin() +void AsyncSSLServer::begin() { if (_pcb) { @@ -2073,7 +2110,7 @@ void AsyncServer::begin() tcp_accept(_pcb, &_s_accept); } -void AsyncServer::end() +void AsyncSSLServer::end() { if (_pcb) { @@ -2090,7 +2127,7 @@ void AsyncServer::end() } //runs on LwIP thread -int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err) +int8_t AsyncSSLServer::_accept(tcp_pcb* pcb, int8_t err) { ATCP_HEXLOGDEBUG1("_accept: pcb =", (uint32_t) pcb); @@ -2115,7 +2152,7 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err) return ERR_OK; } -int8_t AsyncServer::_accepted(AsyncSSLClient* client) +int8_t AsyncSSLServer::_accepted(AsyncSSLClient* client) { if (_connect_cb) { @@ -2125,17 +2162,17 @@ int8_t AsyncServer::_accepted(AsyncSSLClient* client) return ERR_OK; } -void AsyncServer::setNoDelay(bool nodelay) +void AsyncSSLServer::setNoDelay(bool nodelay) { _noDelay = nodelay; } -bool AsyncServer::getNoDelay() +bool AsyncSSLServer::getNoDelay() { return _noDelay; } -uint8_t AsyncServer::status() +uint8_t AsyncSSLServer::status() { if (!_pcb) { @@ -2145,12 +2182,12 @@ uint8_t AsyncServer::status() return _pcb->state; } -int8_t AsyncServer::_s_accept(void * arg, tcp_pcb * pcb, int8_t err) +int8_t AsyncSSLServer::_s_accept(void * arg, tcp_pcb * pcb, int8_t err) { - return reinterpret_cast(arg)->_accept(pcb, err); + return reinterpret_cast(arg)->_accept(pcb, err); } -int8_t AsyncServer::_s_accepted(void *arg, AsyncSSLClient* client) +int8_t AsyncSSLServer::_s_accepted(void *arg, AsyncSSLClient* client) { - return reinterpret_cast(arg)->_accepted(client); + return reinterpret_cast(arg)->_accepted(client); } diff --git a/src_cpp/AsyncTCP_SSL.h b/src_cpp/AsyncTCP_SSL.h index dbac853..173fa31 100644 --- a/src_cpp/AsyncTCP_SSL.h +++ b/src_cpp/AsyncTCP_SSL.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ /* @@ -50,7 +51,7 @@ #error This AsyncTCP_SSL library is supporting only ESP32 #endif -#define ASYNC_TCP_SSL_VERSION "AsyncTCP_SSL v1.0.0" +#define ASYNC_TCP_SSL_VERSION "AsyncTCP_SSL v1.1.0" #define ASYNC_TCP_SSL_ENABLED true @@ -85,12 +86,21 @@ class AsyncSSLClient; #define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react. #define SSL_HANDSHAKE_TIMEOUT 5000 // timeout to complete SSL handshake +#if ASYNC_TCP_SSL_ENABLED +typedef std::function AcConnectHandlerSSL; +typedef std::function AcAckHandlerSSL; +typedef std::function AcErrorHandlerSSL; +typedef std::function AcDataHandlerSSL; +typedef std::function AcPacketHandlerSSL; +typedef std::function AcTimeoutHandlerSSL; +#else typedef std::function AcConnectHandler; typedef std::function AcAckHandler; typedef std::function AcErrorHandler; typedef std::function AcDataHandler; typedef std::function AcPacketHandler; typedef std::function AcTimeoutHandler; +#endif struct tcp_pcb; struct ip_addr; @@ -166,6 +176,16 @@ class AsyncSSLClient IPAddress localIP(); uint16_t localPort(); +#if ASYNC_TCP_SSL_ENABLED + void onConnect(AcConnectHandlerSSL cb, void* arg = 0); //on successful connect + void onDisconnect(AcConnectHandlerSSL cb, void* arg = 0); //disconnected + void onAck(AcAckHandlerSSL cb, void* arg = 0); //ack received + void onError(AcErrorHandlerSSL cb, void* arg = 0); //unsuccessful connect or error + void onData(AcDataHandlerSSL cb, void* arg = 0); //data received (called if onPacket is not used) + void onPacket(AcPacketHandlerSSL cb, void* arg = 0); //data received + void onTimeout(AcTimeoutHandlerSSL cb, void* arg = 0); //ack timeout + void onPoll(AcConnectHandlerSSL cb, void* arg = 0); //every 125ms when connected +#else void onConnect(AcConnectHandler cb, void* arg = 0); //on successful connect void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected void onAck(AcAckHandler cb, void* arg = 0); //ack received @@ -174,6 +194,7 @@ class AsyncSSLClient void onPacket(AcPacketHandler cb, void* arg = 0); //data received void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected +#endif void ackPacket(struct pbuf * pb);//ack pbuf from onPacket size_t ack(size_t len); //ack data that you have not acked using the method below @@ -221,6 +242,24 @@ class AsyncSSLClient std::string _hostname; int8_t _closed_slot; +#if ASYNC_TCP_SSL_ENABLED + AcConnectHandlerSSL _connect_cb; + void* _connect_cb_arg; + AcConnectHandlerSSL _discard_cb; + void* _discard_cb_arg; + AcAckHandlerSSL _sent_cb; + void* _sent_cb_arg; + AcErrorHandlerSSL _error_cb; + void* _error_cb_arg; + AcDataHandlerSSL _recv_cb; + void* _recv_cb_arg; + AcPacketHandlerSSL _pb_cb; + void* _pb_cb_arg; + AcTimeoutHandlerSSL _timeout_cb; + void* _timeout_cb_arg; + AcConnectHandlerSSL _poll_cb; + void* _poll_cb_arg; +#else AcConnectHandler _connect_cb; void* _connect_cb_arg; AcConnectHandler _discard_cb; @@ -237,6 +276,7 @@ class AsyncSSLClient void* _timeout_cb_arg; AcConnectHandler _poll_cb; void* _poll_cb_arg; +#endif bool _pcb_busy; uint32_t _pcb_sent_at; @@ -282,23 +322,27 @@ class AsyncSSLClient }; #if ASYNC_TCP_SSL_ENABLED -typedef std::function AcSSlFileHandler; +typedef std::function AcSSlFileHandlerSSL; #endif ///////////////////////////////////////////////// -class AsyncServer +class AsyncSSLServer { public: - AsyncServer(IPAddress addr, uint16_t port); - AsyncServer(uint16_t port); - ~AsyncServer(); - void onClient(AcConnectHandler cb, void* arg); + AsyncSSLServer(IPAddress addr, uint16_t port); + AsyncSSLServer(uint16_t port); + ~AsyncSSLServer(); + //void onClient(AcConnectHandler cb, void* arg); #if ASYNC_TCP_SSL_ENABLED + void onClient(AcConnectHandlerSSL cb, void* arg); + // Dummy, so it compiles with ESP Async WebServer library enabled. - void onSslFileRequest(AcSSlFileHandler cb, void* arg) {}; + void onSslFileRequest(AcSSlFileHandlerSSL cb, void* arg) {}; void beginSecure(const char *cert, const char *private_key_file, const char *password) {}; +#else + void onClient(AcConnectHandler cb, void* arg); #endif void begin(); @@ -317,7 +361,13 @@ class AsyncServer bool _noDelay; tcp_pcb* _pcb; + +#if ASYNC_TCP_SSL_ENABLED + AcConnectHandlerSSL _connect_cb; +#else AcConnectHandler _connect_cb; +#endif + void* _connect_cb_arg; int8_t _accept(tcp_pcb* newpcb, int8_t err); diff --git a/src_cpp/AsyncTCP_SSL_Debug.h b/src_cpp/AsyncTCP_SSL_Debug.h index 956c67c..a8dc87a 100644 --- a/src_cpp/AsyncTCP_SSL_Debug.h +++ b/src_cpp/AsyncTCP_SSL_Debug.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #pragma once diff --git a/src_cpp/tcp_mbedtls.c b/src_cpp/tcp_mbedtls.c index d6bcc12..09f474c 100644 --- a/src_cpp/tcp_mbedtls.c +++ b/src_cpp/tcp_mbedtls.c @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #ifndef _ASYNC_TCP_SSL_LOGLEVEL_ diff --git a/src_cpp/tcp_mbedtls.h b/src_cpp/tcp_mbedtls.h index 912bd7a..982312d 100644 --- a/src_cpp/tcp_mbedtls.h +++ b/src_cpp/tcp_mbedtls.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #ifndef LWIPR_MBEDTLS_H diff --git a/src_h/AsyncTCP_SSL.h b/src_h/AsyncTCP_SSL.h index 83a5924..85e82ac 100644 --- a/src_h/AsyncTCP_SSL.h +++ b/src_h/AsyncTCP_SSL.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ /* @@ -50,7 +51,7 @@ #error This AsyncTCP_SSL library is supporting only ESP32 #endif -#define ASYNC_TCP_SSL_VERSION "AsyncTCP_SSL v1.0.0" +#define ASYNC_TCP_SSL_VERSION "AsyncTCP_SSL v1.1.0" #define ASYNC_TCP_SSL_ENABLED true @@ -85,12 +86,21 @@ class AsyncSSLClient; #define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react. #define SSL_HANDSHAKE_TIMEOUT 5000 // timeout to complete SSL handshake +#if ASYNC_TCP_SSL_ENABLED +typedef std::function AcConnectHandlerSSL; +typedef std::function AcAckHandlerSSL; +typedef std::function AcErrorHandlerSSL; +typedef std::function AcDataHandlerSSL; +typedef std::function AcPacketHandlerSSL; +typedef std::function AcTimeoutHandlerSSL; +#else typedef std::function AcConnectHandler; typedef std::function AcAckHandler; typedef std::function AcErrorHandler; typedef std::function AcDataHandler; typedef std::function AcPacketHandler; typedef std::function AcTimeoutHandler; +#endif struct tcp_pcb; struct ip_addr; @@ -166,6 +176,16 @@ class AsyncSSLClient IPAddress localIP(); uint16_t localPort(); +#if ASYNC_TCP_SSL_ENABLED + void onConnect(AcConnectHandlerSSL cb, void* arg = 0); //on successful connect + void onDisconnect(AcConnectHandlerSSL cb, void* arg = 0); //disconnected + void onAck(AcAckHandlerSSL cb, void* arg = 0); //ack received + void onError(AcErrorHandlerSSL cb, void* arg = 0); //unsuccessful connect or error + void onData(AcDataHandlerSSL cb, void* arg = 0); //data received (called if onPacket is not used) + void onPacket(AcPacketHandlerSSL cb, void* arg = 0); //data received + void onTimeout(AcTimeoutHandlerSSL cb, void* arg = 0); //ack timeout + void onPoll(AcConnectHandlerSSL cb, void* arg = 0); //every 125ms when connected +#else void onConnect(AcConnectHandler cb, void* arg = 0); //on successful connect void onDisconnect(AcConnectHandler cb, void* arg = 0); //disconnected void onAck(AcAckHandler cb, void* arg = 0); //ack received @@ -174,6 +194,7 @@ class AsyncSSLClient void onPacket(AcPacketHandler cb, void* arg = 0); //data received void onTimeout(AcTimeoutHandler cb, void* arg = 0); //ack timeout void onPoll(AcConnectHandler cb, void* arg = 0); //every 125ms when connected +#endif void ackPacket(struct pbuf * pb);//ack pbuf from onPacket size_t ack(size_t len); //ack data that you have not acked using the method below @@ -221,6 +242,24 @@ class AsyncSSLClient std::string _hostname; int8_t _closed_slot; +#if ASYNC_TCP_SSL_ENABLED + AcConnectHandlerSSL _connect_cb; + void* _connect_cb_arg; + AcConnectHandlerSSL _discard_cb; + void* _discard_cb_arg; + AcAckHandlerSSL _sent_cb; + void* _sent_cb_arg; + AcErrorHandlerSSL _error_cb; + void* _error_cb_arg; + AcDataHandlerSSL _recv_cb; + void* _recv_cb_arg; + AcPacketHandlerSSL _pb_cb; + void* _pb_cb_arg; + AcTimeoutHandlerSSL _timeout_cb; + void* _timeout_cb_arg; + AcConnectHandlerSSL _poll_cb; + void* _poll_cb_arg; +#else AcConnectHandler _connect_cb; void* _connect_cb_arg; AcConnectHandler _discard_cb; @@ -237,6 +276,7 @@ class AsyncSSLClient void* _timeout_cb_arg; AcConnectHandler _poll_cb; void* _poll_cb_arg; +#endif bool _pcb_busy; uint32_t _pcb_sent_at; @@ -282,23 +322,27 @@ class AsyncSSLClient }; #if ASYNC_TCP_SSL_ENABLED -typedef std::function AcSSlFileHandler; +typedef std::function AcSSlFileHandlerSSL; #endif ///////////////////////////////////////////////// -class AsyncServer +class AsyncSSLServer { public: - AsyncServer(IPAddress addr, uint16_t port); - AsyncServer(uint16_t port); - ~AsyncServer(); - void onClient(AcConnectHandler cb, void* arg); + AsyncSSLServer(IPAddress addr, uint16_t port); + AsyncSSLServer(uint16_t port); + ~AsyncSSLServer(); + //void onClient(AcConnectHandler cb, void* arg); #if ASYNC_TCP_SSL_ENABLED + void onClient(AcConnectHandlerSSL cb, void* arg); + // Dummy, so it compiles with ESP Async WebServer library enabled. - void onSslFileRequest(AcSSlFileHandler cb, void* arg) {}; + void onSslFileRequest(AcSSlFileHandlerSSL cb, void* arg) {}; void beginSecure(const char *cert, const char *private_key_file, const char *password) {}; +#else + void onClient(AcConnectHandler cb, void* arg); #endif void begin(); @@ -317,7 +361,13 @@ class AsyncServer bool _noDelay; tcp_pcb* _pcb; + +#if ASYNC_TCP_SSL_ENABLED + AcConnectHandlerSSL _connect_cb; +#else AcConnectHandler _connect_cb; +#endif + void* _connect_cb_arg; int8_t _accept(tcp_pcb* newpcb, int8_t err); diff --git a/src_h/AsyncTCP_SSL.hpp b/src_h/AsyncTCP_SSL.hpp index 7b98925..c42d57b 100644 --- a/src_h/AsyncTCP_SSL.hpp +++ b/src_h/AsyncTCP_SSL.hpp @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ /* @@ -145,7 +146,7 @@ static xQueueHandle _async_queue; static TaskHandle_t _async_service_task_handle = NULL; -SemaphoreHandle_t _slots_lock; +static SemaphoreHandle_t _slots_lock; const int _number_of_closed_slots = CONFIG_LWIP_MAX_ACTIVE_TCP; static int _closed_slots[_number_of_closed_slots]; @@ -289,7 +290,7 @@ static void _handle_async_event(lwip_event_packet_t * e) else if (e->event == LWIP_TCP_ACCEPT) { ATCP_HEXLOGINFO2("_handle_async_event: LWIP_TCP_ACCEPT =", (uint32_t) e->arg, (uint32_t) e->accept.client); - AsyncServer::_s_accepted(e->arg, e->accept.client); + AsyncSSLServer::_s_accepted(e->arg, e->accept.client); } else if (e->event == LWIP_TCP_DNS) { @@ -968,49 +969,81 @@ AsyncSSLClient & AsyncSSLClient::operator+=(const AsyncSSLClient &other) Callback Setters * */ +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onConnect(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onConnect(AcConnectHandler cb, void* arg) +#endif { _connect_cb = cb; _connect_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onDisconnect(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onDisconnect(AcConnectHandler cb, void* arg) +#endif { _discard_cb = cb; _discard_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onAck(AcAckHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onAck(AcAckHandler cb, void* arg) +#endif { _sent_cb = cb; _sent_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onError(AcErrorHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onError(AcErrorHandler cb, void* arg) +#endif { _error_cb = cb; _error_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onData(AcDataHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onData(AcDataHandler cb, void* arg) +#endif { _recv_cb = cb; _recv_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onPacket(AcPacketHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onPacket(AcPacketHandler cb, void* arg) +#endif { _pb_cb = cb; _pb_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onTimeout(AcTimeoutHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onTimeout(AcTimeoutHandler cb, void* arg) +#endif { _timeout_cb = cb; _timeout_cb_arg = arg; } +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLClient::onPoll(AcConnectHandlerSSL cb, void* arg) +#else void AsyncSSLClient::onPoll(AcConnectHandler cb, void* arg) +#endif { _poll_cb = cb; _poll_cb_arg = arg; @@ -1995,7 +2028,7 @@ void AsyncSSLClient::_s_ssl_error(void *arg, struct tcp_pcb *tcp, int8_t err) Async TCP Server */ -AsyncServer::AsyncServer(IPAddress addr, uint16_t port) +AsyncSSLServer::AsyncSSLServer(IPAddress addr, uint16_t port) : _port(port) , _addr(addr) , _noDelay(false) @@ -2004,7 +2037,7 @@ AsyncServer::AsyncServer(IPAddress addr, uint16_t port) , _connect_cb_arg(0) {} -AsyncServer::AsyncServer(uint16_t port) +AsyncSSLServer::AsyncSSLServer(uint16_t port) : _port(port) , _addr((uint32_t) IPADDR_ANY) , _noDelay(false) @@ -2013,18 +2046,22 @@ AsyncServer::AsyncServer(uint16_t port) , _connect_cb_arg(0) {} -AsyncServer::~AsyncServer() +AsyncSSLServer::~AsyncSSLServer() { end(); } -void AsyncServer::onClient(AcConnectHandler cb, void* arg) +#if ASYNC_TCP_SSL_ENABLED +void AsyncSSLServer::onClient(AcConnectHandlerSSL cb, void* arg) +#else +void AsyncSSLServer::onClient(AcConnectHandler cb, void* arg) +#endif { _connect_cb = cb; _connect_cb_arg = arg; } -void AsyncServer::begin() +void AsyncSSLServer::begin() { if (_pcb) { @@ -2076,7 +2113,7 @@ void AsyncServer::begin() tcp_accept(_pcb, &_s_accept); } -void AsyncServer::end() +void AsyncSSLServer::end() { if (_pcb) { @@ -2093,7 +2130,7 @@ void AsyncServer::end() } //runs on LwIP thread -int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err) +int8_t AsyncSSLServer::_accept(tcp_pcb* pcb, int8_t err) { ATCP_HEXLOGDEBUG1("_accept: pcb =", (uint32_t) pcb); @@ -2118,7 +2155,7 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err) return ERR_OK; } -int8_t AsyncServer::_accepted(AsyncSSLClient* client) +int8_t AsyncSSLServer::_accepted(AsyncSSLClient* client) { if (_connect_cb) { @@ -2128,17 +2165,17 @@ int8_t AsyncServer::_accepted(AsyncSSLClient* client) return ERR_OK; } -void AsyncServer::setNoDelay(bool nodelay) +void AsyncSSLServer::setNoDelay(bool nodelay) { _noDelay = nodelay; } -bool AsyncServer::getNoDelay() +bool AsyncSSLServer::getNoDelay() { return _noDelay; } -uint8_t AsyncServer::status() +uint8_t AsyncSSLServer::status() { if (!_pcb) { @@ -2148,14 +2185,14 @@ uint8_t AsyncServer::status() return _pcb->state; } -int8_t AsyncServer::_s_accept(void * arg, tcp_pcb * pcb, int8_t err) +int8_t AsyncSSLServer::_s_accept(void * arg, tcp_pcb * pcb, int8_t err) { - return reinterpret_cast(arg)->_accept(pcb, err); + return reinterpret_cast(arg)->_accept(pcb, err); } -int8_t AsyncServer::_s_accepted(void *arg, AsyncSSLClient* client) +int8_t AsyncSSLServer::_s_accepted(void *arg, AsyncSSLClient* client) { - return reinterpret_cast(arg)->_accepted(client); + return reinterpret_cast(arg)->_accepted(client); } #endif /* ASYNCTCP_SSL_HPP_ */ diff --git a/src_h/AsyncTCP_SSL_Debug.h b/src_h/AsyncTCP_SSL_Debug.h index 956c67c..a8dc87a 100644 --- a/src_h/AsyncTCP_SSL_Debug.h +++ b/src_h/AsyncTCP_SSL_Debug.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #pragma once diff --git a/src_h/tcp_mbedtls.c b/src_h/tcp_mbedtls.c index d6bcc12..09f474c 100644 --- a/src_h/tcp_mbedtls.c +++ b/src_h/tcp_mbedtls.c @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #ifndef _ASYNC_TCP_SSL_LOGLEVEL_ diff --git a/src_h/tcp_mbedtls.h b/src_h/tcp_mbedtls.h index 912bd7a..982312d 100644 --- a/src_h/tcp_mbedtls.h +++ b/src_h/tcp_mbedtls.h @@ -15,11 +15,12 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - Version: 1.0.0 + Version: 1.1.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 21/10/2021 Initial coding to support only ESP32 + 1.1.0 K Hoang 22/10/2021 Fix bug. Enable coexistence with AsyncTCP *****************************************************************************************************************************/ #ifndef LWIPR_MBEDTLS_H