Simplify using of UnbufferedSerial(Serial), USBCDC, TCP, SMTP, NTP Fork : https://github.com/YSI-LPS/lib_Transmission
Dependents: lib_Transmission_Serial_example 2022_TICE_Electrolyse lib_Transmission_TCP_example
Revision 28:24f7e0ddf6f5, committed 2021-09-17
- Comitter:
- YSI
- Date:
- Fri Sep 17 08:53:40 2021 +0000
- Parent:
- 27:87c2a2a23d06
- Child:
- 29:0d321d298d37
- Commit message:
- improvement of smtp error management.; Transmission processing function is now optional when dont receive transmissions
Changed in this revision
lib_Transmission.cpp | Show annotated file Show diff for this revision Revisions of this file |
lib_Transmission.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/lib_Transmission.cpp Wed Sep 08 06:41:38 2021 +0000 +++ b/lib_Transmission.cpp Fri Sep 17 08:53:40 2021 +0000 @@ -361,37 +361,42 @@ return buffer; } -bool Transmission::smtp(const char* mail, const char* from, const char* subject, const char* data, const char* server) +bool Transmission::smtp(string mail, string from, string subject, string data, const char* server) { - string sMAIL(mail); - if((!_eth) || (!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) || sMAIL.empty()) return false; + if((!_eth) || (!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) || mail.empty()) return false; + string code, smtpParams[][7] = {{ "", "HELO Mbed " + from + "\r\n", "MAIL FROM:<Mbed." + from + "@UNIVERSITE-PARIS-SACLAY.FR>\r\n", "RCPT TO:<" + mail + ">\r\n", "DATA\r\n", + "From:\"Mbed " + from + "\" <Mbed." + from + "@UNIVERSITE-PARIS-SACLAY.FR>\r\nTo:<" + mail + ">\r\nSubject:" + subject + "\r\nMessage-Id:<" + mail.substr(0, mail.find("@")) + "." + to_string(time(NULL)) + mail.substr(mail.find("@")) + ">\r\n" + data + "\r\n.\r\n", "QUIT\r\n" }, + { "", "HELO Mbed\r\n", "MAIL FROM: <Mbed>\r\n","RCPT TO: <" + mail + ">\r\n", "QUIT\r\n" }}; TCPSocket clientSMTP; clientSMTP.set_timeout(message.TIMEOUT); - string sFROM(from), sSUBJECT(subject), sDATA(data), sTO(sMAIL.substr(0, sMAIL.find("@"))); - string smtpParams[][7] = { { "", "HELO Mbed " + sFROM + "\r\n", "MAIL FROM: <Mbed." + sFROM + "@UNIVERSITE-PARIS-SACLAY.FR>\r\n", "RCPT TO: <" + sMAIL + ">\r\n", "DATA\r\n", "From: \"Mbed " + sFROM + "\" <Mbed." + sFROM + "@UNIVERSITE-PARIS-SACLAY.FR>\r\nTo: \"" + sTO + "\" <" + sMAIL + ">\r\nSubject:" + sSUBJECT + "\r\n" + sDATA + "\r\n\r\n.\r\n", "QUIT\r\n" }, - { "", "HELO Mbed\r\n", "MAIL FROM: <Mbed>\r\n","RCPT TO: <" + sMAIL + ">\r\n", "QUIT\r\n" }}; - string code; if(eth_error("clientSMTP_open", clientSMTP.open(_eth)) == NSAPI_ERROR_OK) { - for(const string& ssend : smtpParams[(sFROM.empty())?1:0]) + for(const string& ssend : smtpParams[from.empty()?1:0]) { + char buffer[64] = {0}; - if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(server, 25))) < NSAPI_ERROR_OK) break; } - else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break; - if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 64)) < NSAPI_ERROR_OK) break; + if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(server, 25))) < NSAPI_ERROR_OK) break; } + else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break; + if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 64)) < NSAPI_ERROR_OK) break; buffer[3] = 0; code += buffer; if(ssend == "QUIT\r\n") break; } eth_error("clientSMTP_close", clientSMTP.close()); } - if(sFROM.empty()) return code == "220250250250221"; - #if MBED_MAJOR_VERSION > 5 - else if(code != "220250250250354250221") _queue.call_in(60s, this, &Transmission::smtp, mail, from, subject, data, server); - #else - else if(code != "220250250250354250221") _queue.call_in(60000, this, &Transmission::smtp, mail, from, subject, data, server); - #endif - return code == "220250250250354250221"; + static int smtp_error = 0; + if(from.empty()) return code == "220250250250221"; + else if((code == "220250250250354250221") || (code == "220250250250354")) smtp_error = 0; + else if(smtp_error < 5) + { + smtp_error++; + #if MBED_MAJOR_VERSION > 5 + _queue.call_in(60s, this, &Transmission::smtp, mail, from, subject, data, server); + #else + _queue.call_in(60000, this, &Transmission::smtp, mail, from, subject, data, server); + #endif + } + return !smtp_error; } time_t Transmission::ntp(const char* server)
--- a/lib_Transmission.h Wed Sep 08 06:41:38 2021 +0000 +++ b/lib_Transmission.h Fri Sep 17 08:53:40 2021 +0000 @@ -84,7 +84,7 @@ #endif USBCDC *usb, EthernetInterface *eth, - string (*processing)(string), + string (*processing)(string) = NULL, void (*ethup)(void) = NULL, bool caseIgnore = true); /** make new Transmission instance @@ -98,7 +98,7 @@ Serial *serial, #endif USBCDC *usb, - string (*processing)(string), + string (*processing)(string) = NULL, bool caseIgnore = true); /** make new Transmission instance * @@ -111,7 +111,7 @@ Serial *serial, #endif EthernetInterface *eth, - string (*processing)(string), + string (*processing)(string) = NULL, void (*ethup)(void) = NULL, bool caseIgnore = true); /** make new Transmission instance @@ -121,7 +121,7 @@ Transmission( USBCDC *usb, EthernetInterface *eth, - string (*processing)(string), + string (*processing)(string) = NULL, void (*ethup)(void) = NULL, bool caseIgnore = true); /** make new Transmission instance @@ -134,7 +134,7 @@ #else Serial *serial, #endif - string (*processing)(string), + string (*processing)(string) = NULL, bool caseIgnore = true); /** make new Transmission instance * @@ -142,7 +142,7 @@ */ Transmission( EthernetInterface *eth, - string (*processing)(string), + string (*processing)(string) = NULL, void (*ethup)(void) = NULL, bool caseIgnore = true); /** make new Transmission instance @@ -151,7 +151,7 @@ */ Transmission( USBCDC *usb, - string (*processing)(string), + string (*processing)(string) = NULL, bool caseIgnore = true); /** Configure the TCP connection * @@ -189,20 +189,20 @@ * @param server port * @returns server response */ - string get(const string& buffer, const string& server="", const int& port=80); + string get(const string& buffer, const string& server, const int& port=80); /** send an email to an smtp server * * @param mail is the recipient's email * @param from="" this is the sender's email * @param subject="" this is the subject of the email * @param data="" this is the content of the email - * @param server="" this is an ip from an smtp server + * @param server="129.175.212.70" this is an ip from an smtp server * @returns indicates if the smtp transaction was successful */ - bool smtp(const char* mail, const char* from="", const char* subject="", const char* data="", const char* server=TRANSMISSION_SMTP_SERVER); + bool smtp(string mail, string from="", string subject="", string data="", const char* server=TRANSMISSION_SMTP_SERVER); /** time request to an ntp server * - * @param server="" this is an ip from an ntp server + * @param server="129.175.34.43" this is an ip from an ntp server * @returns time */ time_t ntp(const char* server=TRANSMISSION_NTP_SERVER);