Skip to content

Commit

Permalink
Fix NTP time issue #298.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Aug 27, 2023
1 parent 283c1fa commit 1520fa5
Show file tree
Hide file tree
Showing 27 changed files with 203 additions and 2,398 deletions.
3 changes: 2 additions & 1 deletion examples/SMTP/External_Client/GSMClient/GSMClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
* IMAP_Data imap_data;
*/

// To allow TinyGSM library integration, the following macro should be defined in src/ESP_Mail_FS.h.
// To allow TinyGSM library integration, the following macro should be defined in src/ESP_Mail_FS.h or
// your custom config file src/Custom_ESP_Mail_FS.h.
// #define TINY_GSM_MODEM_SIM7600

#define TINY_GSM_MODEM_SIM7600 // SIMA7670 Compatible with SIM7600 AT instructions
Expand Down
17 changes: 14 additions & 3 deletions examples/SMTP/Set_Time/Set_Time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
* Github: https://github.com/mobizt/ESP-Mail-Client
*
* Copyright (c) 2023 mobizt
*/
*/

// This example showes how to set the library and/or device time manually.

/** Note for library update from v2.x.x to v3.x.x.
*
*
* Struct data names changed
*
* "ESP_Mail_Session" changes to "Session_Config"
Expand Down Expand Up @@ -145,7 +145,7 @@ void setup()
// Get current time from RTC
RTC.getTime(currentTime);

float gmtOffset = 3.0; // GMT offset in hour
float gmtOffset = 3.0; // GMT offset in hour
smtp.setSystemTime(currentTime.getUnixTime(), gmtOffset);

#endif
Expand Down Expand Up @@ -173,6 +173,17 @@ void setup()

config.login.user_domain = F("127.0.0.1");

/**
* Once the system time or device time was set before calling smtp.connect, the following config will
* not take effect when NTP time is enabled.
*
* config.time.ntp_server
* config.time.gmt_offset
* config.time.day_light_offset
*
* To reset the reference time and use config.time instead, call smtp.setSystemTime(0) whenever you want.
*/

SMTP_Message message;

/* Set the message headers */
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP Mail Client",
"version": "3.4.6",
"version": "3.4.7",
"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": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP Mail Client

version=3.4.6
version=3.4.7

author=Mobizt

Expand Down
14 changes: 9 additions & 5 deletions src/ESP_Mail_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Arduino devices.
*
* Created August 20, 2023
* Created August 27, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -503,7 +503,7 @@ void ESP_Mail_Client::getTimezone(const char *TZ_file, MB_String &out)
#endif
}

void ESP_Mail_Client::setTime(float gmt_offset, float day_light_offset, const char *ntp_server, const char *TZ_Var, const char *TZ_file, bool wait)
void ESP_Mail_Client::setTime(const char *TZ_Var, const char *TZ_file, bool wait)
{

_clockReady = Time.clockReady();
Expand All @@ -517,7 +517,7 @@ void ESP_Mail_Client::setTime(float gmt_offset, float day_light_offset, const ch

if (WiFI_CONNECTED)
{
Time.setClock(gmt_offset, day_light_offset, ntp_server);
Time.setClock();
if (wait)
{
unsigned long waitMs = millis();
Expand Down Expand Up @@ -1347,6 +1347,10 @@ bool ESP_Mail_Client::prepareTime(Session_Config *session_config, T sessionPtr)

if (session_config->time.ntp_server.length() > 0 || timeShouldBeValid)
{

if (session_config->time.ntp_server.length() > 0)
Time.begin(session_config->time.gmt_offset, session_config->time.day_light_offset, session_config->time.ntp_server.c_str());

if (!Time.clockReady())
{
if (sessionPtr->client.type() == esp_mail_client_type_external_gsm_client)
Expand All @@ -1368,7 +1372,7 @@ bool ESP_Mail_Client::prepareTime(Session_Config *session_config, T sessionPtr)
if (sessionPtr->_debug && !isResponseCB<T>(sessionPtr))
esp_mail_debug_print_tag(esp_mail_dbg_str_21 /* "Reading time from NTP server" */, esp_mail_debug_tag_type_client, true);
#endif
setTime(session_config->time.gmt_offset, session_config->time.day_light_offset, session_config->time.ntp_server.c_str(), session_config->time.timezone_env_string.c_str(), session_config->time.timezone_file.c_str(), true);
setTime(session_config->time.timezone_env_string.c_str(), session_config->time.timezone_file.c_str(), true);
#endif
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ESP_Mail_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#define ESP_MAIL_CLIENT_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Arduino devices.
*
* Created August 20, 2023
* Created August 27, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -1214,7 +1214,7 @@ class ESP_Mail_Client

// Set or sync device system time with NTP server
// Do not modify or remove
void setTime(float gmt_offset, float day_light_offset, const char *ntp_server, const char *TZ_Var, const char *TZ_file, bool wait);
void setTime(const char *TZ_Var, const char *TZ_file, bool wait);

// Set the device time zone via TZ environment variable
void setTimezone(const char *TZ_Var, const char *TZ_file);
Expand Down
4 changes: 2 additions & 2 deletions src/ESP_Mail_Client_Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#ifndef ESP_MAIL_VERSION

#define ESP_MAIL_VERSION "3.4.6"
#define ESP_MAIL_VERSION_NUM 30406
#define ESP_MAIL_VERSION "3.4.7"
#define ESP_MAIL_VERSION_NUM 30407

/* The inconsistent file version checking to prevent mixed versions compilation. */
#define VALID_VERSION_CHECK(ver) (ver == ESP_MAIL_VERSION_NUM)
Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define ESP_MAIL_CONST_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define ESP_MAIL_ERROR_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define ESP_MAIL_CONFIG_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

Expand Down
7 changes: 5 additions & 2 deletions src/ESP_Mail_IMAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#define ESP_MAIL_IMAP_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Espressif's ESP32 and ESP8266, Raspberry Pi RP2040 Pico, and SAMD21 with u-blox NINA-W102 WiFi/Bluetooth module
*
* Created August 20, 2023
* Created August 27, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -449,6 +449,9 @@ bool ESP_Mail_Client::readMail(IMAPSession *imap, bool closeSession)
}
else
{
// If time config changed, we will update time
MailClient.prepareTime<IMAPSession *>(imap->_session_cfg, imap);

// reuse session
for (size_t i = 0; i < imap->_headers.size(); i++)
imap->_headers[i].part_headers.clear();
Expand Down
8 changes: 5 additions & 3 deletions src/ESP_Mail_SMTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#define ESP_MAIL_SMTP_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Espressif's ESP32 and ESP8266, Raspberry Pi RP2040 Pico, and SAMD21 with u-blox NINA-W102 WiFi/Bluetooth module
*
* Created August 20, 2023
* Created August 27, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -476,6 +476,8 @@ bool ESP_Mail_Client::mSendMail(SMTPSession *smtp, SMTP_Message *msg, bool close
}
else
{
// If time config changed, we will update time
MailClient.prepareTime<SMTPSession *>(smtp->_session_cfg, smtp);
// reuse session
#if !defined(SILENT_MODE)
PGM_P p1 = smtp->_sentSuccessCount || smtp->_sentFailedCount ? esp_mail_cb_str_10 /* "Sending next Email..." */ : esp_mail_cb_str_9 /* "Sending Email..." */;
Expand Down Expand Up @@ -823,7 +825,7 @@ void ESP_Mail_Client::altSendCallback(SMTPSession *smtp, PGM_P cbMsg, PGM_P dbMs
else if (imap && !calDataLen)
{
#if defined(ENABLE_IMAP)
printDebug<IMAPSession*>(imap, cbMsg, dbMsg, type, prependCRLF, success);
printDebug<IMAPSession *>(imap, cbMsg, dbMsg, type, prependCRLF, success);
#endif
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/ESP_Mail_TCPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* The Network Upgradable Arduino Secure TCP Client Class, ESP_Mail_TCPClient.h v1.0.1
*
* Created August 20, 2023
* Created August 27, 2023
*
* The MIT License (MIT)
* Copyright (c) 2023 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -30,7 +30,7 @@
#define ESP_MAIL_TCPCLIENT_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30406)
#if !VALID_VERSION_CHECK(30407)
#error "Mixed versions compilation."
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/SSLClient/ESP_SSLClient.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
*
* The ESP SSL Client Class, ESP_SSLClient.h v2.1.4
* The ESP SSL Client Class, ESP_SSLClient.h v2.1.6
*
* Created August 16, 2023
* Created August 27, 2023
*
* The MIT License (MIT)
* Copyright (c) 2023 K. Suwatchai (Mobizt)
Expand Down
3 changes: 0 additions & 3 deletions src/SSLClient/client/BSSL_CertStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
#if __has_include(<FS.h>) && defined(ESP_SSLCLIENT_USE_FILESYSTEM)
#include <FS.h>
#define ESP_SSL_FS_SUPPORTED
#if !defined(TEENSYDUINO)
using namespace fs;
#endif
#endif
#endif

Expand Down
39 changes: 36 additions & 3 deletions src/SSLClient/client/BSSL_SSL_Client.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* BSSL_SSL_Client library v1.0.9 for Arduino devices.
* BSSL_SSL_Client library v1.0.11 for Arduino devices.
*
* Created August 13, 2003
* Created August 27, 2003
*
* This work contains codes based on WiFiClientSecure from Earle F. Philhower and SSLClient from OSU OPEnS Lab.
*
Expand Down Expand Up @@ -194,6 +194,16 @@ uint8_t BSSL_SSL_Client::connected()
return c_con && br_con;
}

void BSSL_SSL_Client::validate(const char *host, uint16_t port)
{
mConnectionValidate(host, IPAddress(), port);
}

void BSSL_SSL_Client::validate(IPAddress ip, uint16_t port)
{
mConnectionValidate(nullptr, ip, port);
}

int BSSL_SSL_Client::available()
{
if (!mIsClientInitialized(false))
Expand Down Expand Up @@ -428,6 +438,9 @@ int BSSL_SSL_Client::connectSSL(IPAddress ip, uint16_t port)
if (!_basic_client->connected() && !mConnectBasicClient(nullptr, ip, port))
return 0;

_ip = ip;
_port = port;

return mConnectSSL(nullptr);
}

Expand All @@ -440,6 +453,9 @@ int BSSL_SSL_Client::connectSSL(const char *host, uint16_t port)
if (!_basic_client->connected() && !mConnectBasicClient(host, IPAddress(), port))
return 0;

_host = host;
_port = port;

return mConnectSSL(host);
}

Expand Down Expand Up @@ -1374,7 +1390,8 @@ int BSSL_SSL_Client::mIsClientInitialized(bool notify)

int BSSL_SSL_Client::mConnectBasicClient(const char *host, IPAddress ip, uint16_t port)
{
if (!mIsClientInitialized(true))

if (!mConnectionValidate(host, ip, port))
return 0;

if (!(host ? _basic_client->connect(host, port) : _basic_client->connect(ip, port)))
Expand Down Expand Up @@ -1570,6 +1587,22 @@ int BSSL_SSL_Client::mConnectSSL(const char *host)
return 1;
}

bool BSSL_SSL_Client::mConnectionValidate(const char *host, IPAddress ip, uint16_t port)
{
if (!mIsClientInitialized(true))
return false;

if (_basic_client && _basic_client->connected() &&
host
? (strcasecmp(host, _host.c_str()) != 0 || port != _port)
: (ip != _ip || port != _port))
{
_basic_client->stop();
}

return true;
}

int BSSL_SSL_Client::mRunUntil(const unsigned target, unsigned long timeout)
{
unsigned lastState = 0;
Expand Down
Loading

0 comments on commit 1520fa5

Please sign in to comment.