Skip to content

Commit

Permalink
Code optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Aug 20, 2023
1 parent e5450c7 commit b71fe4f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
13 changes: 2 additions & 11 deletions src/ESP_Mail_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,6 @@ void ESP_Mail_Client::getTimezone(const char *TZ_file, MB_String &out)
#endif
}

void ESP_Mail_Client::idle()
{
#if defined(ARDUINO_ESP8266_MAJOR) && defined(ARDUINO_ESP8266_MINOR) && defined(ARDUINO_ESP8266_REVISION) && ((ARDUINO_ESP8266_MAJOR == 3 && ARDUINO_ESP8266_MINOR >= 1) || ARDUINO_ESP8266_MAJOR > 3)
esp_yield();
#else
delay(0);
#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)
{

Expand All @@ -533,7 +524,7 @@ void ESP_Mail_Client::setTime(float gmt_offset, float day_light_offset, const ch
unsigned long waitMs = millis();
while (!Time.clockReady() && millis() - waitMs < 10000)
{
idle();
yield_impl();
}
}
}
Expand Down Expand Up @@ -1015,7 +1006,7 @@ int ESP_Mail_Client::readLine(ESP_Mail_TCPClient *client, char *buf, int bufLen,
break;
}

idle();
yield_impl();

ret = client->read();
if (ret > -1)
Expand Down
2 changes: 0 additions & 2 deletions src/ESP_Mail_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -1322,8 +1322,6 @@ class ESP_Mail_Client
// Get operation config based on port and its protocol
void getPortFunction(uint16_t port, struct esp_mail_ports_functions &ports_functions, bool &secure, bool &secureMode, bool &ssl, bool &starttls);

void idle();

#endif

#if defined(ENABLE_SMTP)
Expand Down
16 changes: 9 additions & 7 deletions src/ESP_Mail_Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -3435,15 +3435,21 @@ appendDebugTag(MB_String &buf, esp_mail_debug_tag_type type, bool clear, PGM_P t
buf += text;
}

// Print debug message w/wo new line to debug port
static void __attribute__((used))
esp_mail_debug_print(PGM_P msg = "", bool newLine = true)
yield_impl()
{
#if defined(ARDUINO_ESP8266_MAJOR) && defined(ARDUINO_ESP8266_MINOR) && defined(ARDUINO_ESP8266_REVISION) && ((ARDUINO_ESP8266_MAJOR == 3 && ARDUINO_ESP8266_MINOR >= 1) || ARDUINO_ESP8266_MAJOR > 3)
esp_yield();
#else
delay(0);
#endif
}

// Print debug message w/wo new line to debug port
static void __attribute__((used))
esp_mail_debug_print(PGM_P msg = "", bool newLine = true)
{
yield_impl();
if (newLine)
ESP_MAIL_DEFAULT_DEBUG_PORT.println(msg);
else
Expand All @@ -3453,11 +3459,7 @@ esp_mail_debug_print(PGM_P msg = "", bool newLine = true)
static void __attribute__((used))
esp_mail_debug_print_tag(PGM_P msg, esp_mail_debug_tag_type type, bool newLine = true, bool showTag = true)
{
#if defined(ARDUINO_ESP8266_MAJOR) && defined(ARDUINO_ESP8266_MINOR) && defined(ARDUINO_ESP8266_REVISION) && ((ARDUINO_ESP8266_MAJOR == 3 && ARDUINO_ESP8266_MINOR >= 1) || ARDUINO_ESP8266_MAJOR > 3)
esp_yield();
#else
delay(0);
#endif
yield_impl();

MB_String s;
if (showTag)
Expand Down
18 changes: 9 additions & 9 deletions src/ESP_Mail_IMAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ bool ESP_Mail_Client::readMail(IMAPSession *imap, bool closeSession)
if (!handleIMAPResponse(imap, IMAP_STATUS_IMAP_RESPONSE_FAILED, closeSession))
return false;

idle();
yield_impl();
}
}
else
Expand Down Expand Up @@ -1635,7 +1635,7 @@ int ESP_Mail_Client::parseSearchResponse(IMAPSession *imap, esp_mail_imap_respon

while (imap->client.available() > 0 && idx < bufLen)
{
idle();
yield_impl();

ret = imap->client.read();

Expand Down Expand Up @@ -2631,7 +2631,7 @@ bool ESP_Mail_Client::handleIMAPResponse(IMAPSession *imap, int errCode, bool cl
return false;
}
res.chunkBufSize = imap->client.available();
idle();
yield_impl();
}

res.dataTime = millis();
Expand Down Expand Up @@ -2660,7 +2660,7 @@ bool ESP_Mail_Client::handleIMAPResponse(IMAPSession *imap, int errCode, bool cl

while (!res.completedResponse) // looking for operation finishing
{
idle();
yield_impl();

if (imap->_imap_cmd == esp_mail_imap_cmd_append || (imap->_imap_custom_cmd == esp_mail_imap_cmd_append && imap->_imap_cmd == esp_mail_imap_cmd_custom && imap->_customCmdResCallback))
{
Expand Down Expand Up @@ -4044,7 +4044,7 @@ bool ESP_Mail_Client::parseAttachmentResponse(IMAPSession *imap, char *buf, esp_
return true;
}

idle();
yield_impl();

if (res.octetLength == 0)
return true;
Expand Down Expand Up @@ -4105,7 +4105,7 @@ bool ESP_Mail_Client::parseAttachmentResponse(IMAPSession *imap, char *buf, esp_
write = mbfs->write(mbfs_type imap->_imap_data->storage.type, (uint8_t *)decoded, olen);
}

idle();
yield_impl();
// release memory
freeMem(&decoded);

Expand Down Expand Up @@ -4143,7 +4143,7 @@ bool ESP_Mail_Client::parseAttachmentResponse(IMAPSession *imap, char *buf, esp_
write = mbfs->write(mbfs_type imap->_imap_data->storage.type, (uint8_t *)buf, bufLen);
}

idle();
yield_impl();

write_error = write != bufLen;

Expand Down Expand Up @@ -4285,7 +4285,7 @@ void ESP_Mail_Client::decodeText(IMAPSession *imap, esp_mail_imap_response_data
}
}

idle();
yield_impl();

if (res.octetLength == 0)
return;
Expand Down Expand Up @@ -4884,7 +4884,7 @@ bool IMAPSession::connect(bool &ssl)
unsigned long dataMs = millis();
while (client.connected() && client.available() == 0 && millis() - dataMs < 2000)
{
MailClient.idle();
yield_impl();
}

int chunkBufSize = client.available();
Expand Down
6 changes: 3 additions & 3 deletions src/ESP_Mail_SMTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -2648,7 +2648,7 @@ bool ESP_Mail_Client::handleSMTPResponse(SMTPSession *smtp, esp_mail_smtp_comman
return false;
}
chunkBufSize = smtp->client.available();
idle();
yield_impl();
}

dataTime = millis();
Expand All @@ -2657,7 +2657,7 @@ bool ESP_Mail_Client::handleSMTPResponse(SMTPSession *smtp, esp_mail_smtp_comman
{
while (!completedResponse)
{
idle();
yield_impl();

if (!reconnect(smtp, dataTime))
return false;
Expand Down Expand Up @@ -2713,7 +2713,7 @@ bool ESP_Mail_Client::handleSMTPResponse(SMTPSession *smtp, esp_mail_smtp_comman
chunkBufSize = 0;
while (chunkBufSize == 0)
{
idle();
yield_impl();
if (!reconnect(smtp, dataTime))
return false;
chunkBufSize = smtp->client.available();
Expand Down

0 comments on commit b71fe4f

Please sign in to comment.