From b1cb92cf09d5a94e79dcdd36bc3bcb0c9137cfce Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 15 Sep 2023 10:35:57 +0700 Subject: [PATCH] Fix filesystems, native Ethernet and Ethernet integration issues. --- README.md | 30 +++++++++++- .../EthernetClient/ESP32/ESP32.ino | 6 +++ .../EthernetClient/ESP8266/ESP8266.ino | 6 +++ library.json | 2 +- library.properties | 2 +- src/ESP_Mail_Client.cpp | 2 +- src/ESP_Mail_Client.h | 2 +- src/ESP_Mail_Client_Version.h | 4 +- src/ESP_Mail_Const.h | 2 +- src/ESP_Mail_Error.h | 2 +- src/ESP_Mail_FS.h | 13 +++++- src/ESP_Mail_IMAP.h | 2 +- src/ESP_Mail_SMTP.h | 2 +- src/ESP_Mail_TCPClient.h | 29 +++++++----- src/extras/MB_FS.h | 31 +------------ src/extras/MB_Time.h | 2 +- src/extras/Networks.h | 46 +++++++++++++++---- src/extras/RFC2047.cpp | 2 +- src/extras/RFC2047.h | 2 +- 19 files changed, 123 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index d141e4ac..5f10c9ea 100644 --- a/README.md +++ b/README.md @@ -381,16 +381,33 @@ ENABLE_SMTP // For SMTP class compilation ENABLE_NTP_TIME // For enabling the device or library time setup from NTP server ENABLE_ERROR_STRING // For enabling the error string from error reason ESP_MAIL_USE_PSRAM // For enabling PSRAM support -ESP_MAIL_DEFAULT_FLASH_FS // For enabling flash filesystem support -ESP_MAIL_DEFAULT_SD_FS // For enabling SD filesystem support +ESP_MAIL_DEFAULT_FLASH_FS // For enabling Flash filesystem support +ESP_MAIL_DEFAULT_SD_FS // For enabling SD filesystem support +ESP_MAIL_CARD_TYPE_SD or ESP_MAIL_CARD_TYPE_SD_MMC // The SD card type for SD filesystem ``` +The Flash and SD filesystems are predefined. + +SD is the default SD filesystem for all devices. + +For ESP8266 and Arduino Pico, LittleFS is the default flash filesystem. + +For ESP32 since v2.0.x, LittleFS is the default flash filesystem otherwise SPIFFS is the default flash filesystem. + +In otherr devices, SPIFFS is the default flash filesystem. + +User can change `ESP_MAIL_DEFAULT_FLASH_FS` and `ESP_MAIL_DEFAULT_SD_FS` with `ESP_MAIL_CARD_TYPE_SD` or `ESP_MAIL_CARD_TYPE_SD_MMC` defined values for other filesystems. + + ### Optional Options The following options are not yet defined in [**ESP_Mail_FS.h**](src/ESP_Mail_FS.h) and can be assigned by user. ```cpp SILENT_MODE // For silent operation (no debug printing and callback) +ESP_MAIL_ETHERNET_MODULE_LIB `"EthernetLibrary.h"` // For the Ethernet library to work with external Ethernet module +ESP_MAIL_ETHERNET_MODULE_CLASS EthernetClass // For the Ethernet class object of Ethernet library to work with external Ethernet module +ESP_MAIL_ETHERNET_MODULE_TIMEOUT 2000 // For the time out in milliseconds to wait external Ethernet module to connect to network ENABLE_ESP8266_ENC28J60_ETH // For ENC28J60 Ethernet module support in ESP8266 ENABLE_ESP8266_W5500_ETH // For W5500 Ethernet module support in ESP8266 ENABLE_ESP8266_W5100_ETH // For W5100 Ethernet module support in ESP8266 @@ -415,6 +432,15 @@ build_flags = -D ESP_MAIL_DEBUG_PORT=Serial -D ESP_MAIL_DISABLE_ONBOARD_WIFI ``` +For external Ethernet module integation used with function `setEthernetClient`, both `ESP_MAIL_ETHERNET_MODULE_LIB` and `ESP_MAIL_ETHERNET_MODULE_CLASS` should be defined. + +`ESP_MAIL_ETHERNET_MODULE_LIB` is the Ethernet library name with extension (.h) and should be inside `""` or `<>` e.g. `"Ethernet.h"`. + +`ESP_MAIL_ETHERNET_MODULE_CLASS` is the name of static object defined from class e.g. `Ethernet`. + +`ESP_MAIL_ETHERNET_MODULE_TIMEOUT` is the time out in milliseconds to wait network connection. + + For disabling predefined options instead of editing the [**ESP_Mail_FS.h**](src/ESP_Mail_FS.h) or using `#undef` in `Custom_ESP_Mail_FS.h`, you can define these build flags with these names or macros in `Custom_ESP_Mail_FS.h`. ```cpp diff --git a/examples/SMTP/External_Client/EthernetClient/ESP32/ESP32.ino b/examples/SMTP/External_Client/EthernetClient/ESP32/ESP32.ino index 52aeae4b..bb1cef0d 100644 --- a/examples/SMTP/External_Client/EthernetClient/ESP32/ESP32.ino +++ b/examples/SMTP/External_Client/EthernetClient/ESP32/ESP32.ino @@ -42,8 +42,14 @@ #include +// https://github.com/arduino-libraries/Ethernet #include +// For using other Ethernet library that works with other Ethernet module, +// the following build flags or macros should be assigned in src/ESP_Mail_FS.h or your Custom_ESP_Mail_FS.h. +// ESP_MAIL_ETHERNET_MODULE_LIB and ESP_MAIL_ETHERNET_MODULE_CLASS +// See src/ESP_Mail_FS.h for detail. + #define SMTP_HOST "smtp.gmail.com" #define SMTP_PORT 587 diff --git a/examples/SMTP/External_Client/EthernetClient/ESP8266/ESP8266.ino b/examples/SMTP/External_Client/EthernetClient/ESP8266/ESP8266.ino index 77452b37..a5ce6017 100644 --- a/examples/SMTP/External_Client/EthernetClient/ESP8266/ESP8266.ino +++ b/examples/SMTP/External_Client/EthernetClient/ESP8266/ESP8266.ino @@ -44,8 +44,14 @@ #include +// https://github.com/arduino-libraries/Ethernet #include +// For using other Ethernet library that works with other Ethernet module, +// the following build flags or macros should be assigned in src/ESP_Mail_FS.h or your Custom_ESP_Mail_FS.h. +// ESP_MAIL_ETHERNET_MODULE_LIB and ESP_MAIL_ETHERNET_MODULE_CLASS +// See src/ESP_Mail_FS.h for detail. + #define SMTP_HOST "smtp.gmail.com" #define SMTP_PORT 587 diff --git a/library.json b/library.json index e62dd868..d08d44f3 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP Mail Client", - "version": "3.4.13", + "version": "3.4.14", "keywords": "communication, email, imap, smtp, esp32, esp8266, samd, arduino", "description": "Arduino E-Mail Client Library to send, read and get incoming email notification for ESP32, ESP8266 and SAMD21 devices. The library also supported other Arduino Devices using Clients interfaces e.g. WiFiClient, EthernetClient, and GSMClient.", "repository": { diff --git a/library.properties b/library.properties index e02dacdf..a2902a40 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP Mail Client -version=3.4.13 +version=3.4.14 author=Mobizt diff --git a/src/ESP_Mail_Client.cpp b/src/ESP_Mail_Client.cpp index 95e33869..827b047e 100644 --- a/src/ESP_Mail_Client.cpp +++ b/src/ESP_Mail_Client.cpp @@ -4,7 +4,7 @@ #pragma GCC diagnostic ignored "-Wunused-but-set-variable" #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_Client.h b/src/ESP_Mail_Client.h index 22d62e1a..94703fab 100644 --- a/src/ESP_Mail_Client.h +++ b/src/ESP_Mail_Client.h @@ -2,7 +2,7 @@ #define ESP_MAIL_CLIENT_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_Client_Version.h b/src/ESP_Mail_Client_Version.h index a840e4a9..9fed60c5 100644 --- a/src/ESP_Mail_Client_Version.h +++ b/src/ESP_Mail_Client_Version.h @@ -3,8 +3,8 @@ #ifndef ESP_MAIL_VERSION -#define ESP_MAIL_VERSION "3.4.13" -#define ESP_MAIL_VERSION_NUM 30413 +#define ESP_MAIL_VERSION "3.4.14" +#define ESP_MAIL_VERSION_NUM 30414 /* The inconsistent file version checking to prevent mixed versions compilation. */ #define VALID_VERSION_CHECK(ver) (ver == ESP_MAIL_VERSION_NUM) diff --git a/src/ESP_Mail_Const.h b/src/ESP_Mail_Const.h index 15101468..ee75d21a 100644 --- a/src/ESP_Mail_Const.h +++ b/src/ESP_Mail_Const.h @@ -6,7 +6,7 @@ #define ESP_MAIL_CONST_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_Error.h b/src/ESP_Mail_Error.h index 39e16be4..997c455d 100644 --- a/src/ESP_Mail_Error.h +++ b/src/ESP_Mail_Error.h @@ -7,7 +7,7 @@ #define ESP_MAIL_ERROR_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_FS.h b/src/ESP_Mail_FS.h index f84851f0..1ff6d197 100644 --- a/src/ESP_Mail_FS.h +++ b/src/ESP_Mail_FS.h @@ -6,7 +6,7 @@ #define ESP_MAIL_CONFIG_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif @@ -136,6 +136,17 @@ * * 🏷️ For silent operation (no debug printing and callback) * #define SILENT_MODE + * + * 🏷️ For external Ethernet module support. + * - Should define both library name and class object name. + * - ESP_MAIL_ETHERNET_MODULE_LIB is the Ethernet library name with extension (.h) and + * should be inside "" or <> e.g. "Ethernet.h". + * - ESP_MAIL_ETHERNET_MODULE_CLASS is the name of static object defined from class e.g. Ethernet. + * - ESP_MAIL_ETHERNET_MODULE_TIMEOUT is the time out in milliseconds to wait network connection. + * + * #define ESP_MAIL_ETHERNET_MODULE_LIB "EthernetLib.h" + * #define ESP_MAIL_ETHERNET_MODULE_CLASS EthernetClass + * #define ESP_MAIL_ETHERNET_MODULE_TIMEOUT 2000 * * 🏷️ For ENC28J60 Ethernet module support in ESP8266 * #define ENABLE_ESP8266_ENC28J60_ETH diff --git a/src/ESP_Mail_IMAP.h b/src/ESP_Mail_IMAP.h index 8dc3dfd8..ea60197e 100644 --- a/src/ESP_Mail_IMAP.h +++ b/src/ESP_Mail_IMAP.h @@ -3,7 +3,7 @@ #define ESP_MAIL_IMAP_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_SMTP.h b/src/ESP_Mail_SMTP.h index d095563d..88e7df36 100644 --- a/src/ESP_Mail_SMTP.h +++ b/src/ESP_Mail_SMTP.h @@ -3,7 +3,7 @@ #define ESP_MAIL_SMTP_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/ESP_Mail_TCPClient.h b/src/ESP_Mail_TCPClient.h index 57abfa73..d6c2308d 100644 --- a/src/ESP_Mail_TCPClient.h +++ b/src/ESP_Mail_TCPClient.h @@ -1,8 +1,8 @@ /** * - * The Network Upgradable Arduino Secure TCP Client Class, ESP_Mail_TCPClient.h v1.0.3 + * The Network Upgradable Arduino Secure TCP Client Class, ESP_Mail_TCPClient.h v1.0.4 * - * Created September 13, 2023 + * Created September 14, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -30,7 +30,7 @@ #define ESP_MAIL_TCPCLIENT_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif @@ -1030,7 +1030,7 @@ class ESP_Mail_TCPClient #if defined(ESP_MAIL_ETHERNET_MODULE_IS_AVAILABLE) if (_ethernet_cs_pin > -1) - Ethernet.init(_ethernet_cs_pin); + ETH_MODULE_CLASS.init(_ethernet_cs_pin); if (_ethernet_reset_pin > -1) { @@ -1057,18 +1057,18 @@ class ESP_Mail_TCPClient { if (_static_ip->optional == false) - Ethernet.begin(_ethernet_mac, _static_ip->ipAddress, _static_ip->dnsServer, _static_ip->defaultGateway, _static_ip->netMask); - else if (!Ethernet.begin(_ethernet_mac)) + ETH_MODULE_CLASS.begin(_ethernet_mac, _static_ip->ipAddress, _static_ip->dnsServer, _static_ip->defaultGateway, _static_ip->netMask); + else if (!ETH_MODULE_CLASS.begin(_ethernet_mac)) { - Ethernet.begin(_ethernet_mac, _static_ip->ipAddress, _static_ip->dnsServer, _static_ip->defaultGateway, _static_ip->netMask); + ETH_MODULE_CLASS.begin(_ethernet_mac, _static_ip->ipAddress, _static_ip->dnsServer, _static_ip->defaultGateway, _static_ip->netMask); } } else - Ethernet.begin(_ethernet_mac); + ETH_MODULE_CLASS.begin(_ethernet_mac); unsigned long to = millis(); - while (Ethernet.linkStatus() == LinkOFF && millis() - to < 2000) + while (ETH_MODULE_CLASS.linkStatus() == LinkOFF && millis() - to < ESP_MAIL_ETHERNET_MODULE_TIMEOUT) { delay(100); } @@ -1081,7 +1081,7 @@ class ESP_Mail_TCPClient if (ret) { esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Connected with IP "), esp_mail_debug_tag_type_info, false); - ESP_MAIL_DEFAULT_DEBUG_PORT.println(Ethernet.localIP()); + ESP_MAIL_DEFAULT_DEBUG_PORT.println(ETH_MODULE_CLASS.localIP()); } else esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Can't connect"), esp_mail_debug_tag_type_error, true); @@ -1096,7 +1096,14 @@ class ESP_Mail_TCPClient bool ethernetConnected() { #if defined(ESP_MAIL_ETHERNET_MODULE_IS_AVAILABLE) - _network_status = Ethernet.linkStatus() == LinkON; + _network_status = ETH_MODULE_CLASS.linkStatus() == LinkON && validIP(ETH_MODULE_CLASS.localIP()); + + if (!_network_status) + { + delay(ESP_MAIL_ETHERNET_MODULE_TIMEOUT); + _network_status = ETH_MODULE_CLASS.linkStatus() == LinkON && validIP(ETH_MODULE_CLASS.localIP()); + } + #endif return _network_status; } diff --git a/src/extras/MB_FS.h b/src/extras/MB_FS.h index 96740627..4c52e414 100644 --- a/src/extras/MB_FS.h +++ b/src/extras/MB_FS.h @@ -1,9 +1,9 @@ /** - * The MB_FS, filesystems wrapper class v1.0.16 + * The MB_FS, filesystems wrapper class v1.0.17 * * This wrapper class is for SD and Flash filesystems interface which supports SdFat (//https://github.com/greiman/SdFat) * - * Created June 14, 2023 + * Created September 13, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -46,23 +46,6 @@ #include "SPI.h" #endif -#if defined(ESP32) && __has_include() -#ifdef _LITTLEFS_H_ -#define MB_FS_USE_POSIX_STAT -#include -namespace mb_fs_ns -{ - inline bool exists(const char *mountPoint, const char *filename) - { - MB_String path = mountPoint; - path += filename; - struct stat st; - return stat(path.c_str(), &st) == 0; - } -}; -#endif -#endif - using namespace mb_string; #if defined(BOARD_HAS_PSRAM) && defined(MB_STRING_USE_PSRAM) @@ -661,17 +644,7 @@ class MB_FS #if defined(MBFS_FLASH_FS) if (type == mbfs_flash) - { - -// The workaround for ESP32 LittleFS when calling vfs_api.cpp open() issue. -// See https://github.com/espressif/arduino-esp32/issues/7615 -#if defined(MB_FS_USE_POSIX_STAT) - return mb_fs_ns::exists("/littlefs", filename.c_str()); -#else return MBFS_FLASH_FS.exists(filename.c_str()); -#endif - } - #endif #if defined(MBFS_SD_FS) diff --git a/src/extras/MB_Time.h b/src/extras/MB_Time.h index b3ebc286..3a3996ab 100644 --- a/src/extras/MB_Time.h +++ b/src/extras/MB_Time.h @@ -2,7 +2,7 @@ #define MB_Time_H #include "./ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/extras/Networks.h b/src/extras/Networks.h index 9d1ebb7e..ea4a987c 100644 --- a/src/extras/Networks.h +++ b/src/extras/Networks.h @@ -4,11 +4,10 @@ #include "./ESP_Mail_FS.h" #include "./ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif - // Renesas devices #if defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_MINIMA) || defined(ARDUINO_PORTENTA_C33) #define ESP_MAIL_STRSEP strsepImpl @@ -75,7 +74,7 @@ #endif -#if defined(INC_ENC28J60_LWIP) && defined(INC_W5100_LWIP) && defined(INC_W5500_LWIP) +#if defined(INC_ENC28J60_LWIP) || defined(INC_W5100_LWIP) || defined(INC_W5500_LWIP) #define ESP_MAIL_ETH_IS_AVAILABLE #endif @@ -83,23 +82,54 @@ #endif -#if __has_include() +#if __has_include() || (defined(ESP_MAIL_ETHERNET_MODULE_LIB) && defined(ESP_MAIL_ETHERNET_MODULE_CLASS)) + #if defined(ESP8266) #undef MAX_SOCK_NUM +#endif -#if defined(ESP_MAIL_DISABLE_NATIVE_ETHERNET) +#if (defined(ESP8266) && defined(ESP_MAIL_DISABLE_NATIVE_ETHERNET)) || (!defined(ESP8266) && !defined(ARDUINO_NANO_RP2040_CONNECT)) + + +#if defined(ESP_MAIL_ETHERNET_MODULE_LIB) && defined(ESP_MAIL_ETHERNET_MODULE_CLASS) +#if __has_include(ESP_MAIL_ETHERNET_MODULE_LIB) +#include ESP_MAIL_ETHERNET_MODULE_LIB +#define ETH_MODULE_CLASS ESP_MAIL_ETHERNET_MODULE_CLASS +#elif __has_include() +#include +#define ETH_MODULE_CLASS Ethernet +#endif +#else #include +#define ETH_MODULE_CLASS Ethernet +#endif + +#if defined(ETH_MODULE_CLASS) + #define ESP_MAIL_ETHERNET_MODULE_IS_AVAILABLE + +#if !defined(ESP_MAIL_ETHERNET_MODULE_TIMEOUT) +#define ESP_MAIL_ETHERNET_MODULE_TIMEOUT 2000 +#elif ESP_MAIL_ETHERNET_MODULE_TIMEOUT <= 0 || ESP_MAIL_ETHERNET_MODULE_TIMEOUT > 120 * 1000 +#undef ESP_MAIL_ETHERNET_MODULE_TIMEOUT +#define ESP_MAIL_ETHERNET_MODULE_TIMEOUT 2000 #endif + +#endif + + +#endif + + +#if defined(ESP8266) #undef MAX_SOCK_NUM -#elif !defined(ARDUINO_NANO_RP2040_CONNECT) -#include -#define ESP_MAIL_ETHERNET_MODULE_IS_AVAILABLE #endif + #endif + #if defined(ESP32) || defined(ESP8266) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || \ defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_PORTENTA_C33) || \ defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || \ diff --git a/src/extras/RFC2047.cpp b/src/extras/RFC2047.cpp index 81aea944..9c5f4242 100644 --- a/src/extras/RFC2047.cpp +++ b/src/extras/RFC2047.cpp @@ -2,7 +2,7 @@ #define RFC2047_CPP #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif diff --git a/src/extras/RFC2047.h b/src/extras/RFC2047.h index 219a978e..8aaa5617 100644 --- a/src/extras/RFC2047.h +++ b/src/extras/RFC2047.h @@ -4,7 +4,7 @@ #define RFC2047_H #include "ESP_Mail_Client_Version.h" -#if !VALID_VERSION_CHECK(30413) +#if !VALID_VERSION_CHECK(30414) #error "Mixed versions compilation." #endif