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

Committer:
YSI
Date:
Wed Jun 24 14:30:34 2020 +0000
Revision:
0:2fc6fc3b5e15
Child:
1:27f6baabb15e
lib_Transmission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:2fc6fc3b5e15 1 #include "lib_Transmission.h"
YSI 0:2fc6fc3b5e15 2
YSI 0:2fc6fc3b5e15 3 Transmission::Transmission(UnbufferedSerial *serial, EthernetInterface *eth, EventQueue *queue, void(*com_init)(void), void(*com_processing)(string, const enumCOM&))
YSI 0:2fc6fc3b5e15 4 {
YSI 0:2fc6fc3b5e15 5 _serial = serial;
YSI 0:2fc6fc3b5e15 6 _eth = eth;
YSI 0:2fc6fc3b5e15 7 _queue = queue;
YSI 0:2fc6fc3b5e15 8 fn_com_init = com_init;
YSI 0:2fc6fc3b5e15 9 fn_com_processing = com_processing;
YSI 0:2fc6fc3b5e15 10 }
YSI 0:2fc6fc3b5e15 11
YSI 0:2fc6fc3b5e15 12 bool Transmission::eth_connect(void)
YSI 0:2fc6fc3b5e15 13 {
YSI 0:2fc6fc3b5e15 14 if(message.TCP)
YSI 0:2fc6fc3b5e15 15 {
YSI 0:2fc6fc3b5e15 16 switch(_eth->get_connection_status())
YSI 0:2fc6fc3b5e15 17 {
YSI 0:2fc6fc3b5e15 18 case NSAPI_STATUS_DISCONNECTED:
YSI 0:2fc6fc3b5e15 19 if(statusCOLOR == RED_DISCONNECTED)
YSI 0:2fc6fc3b5e15 20 {
YSI 0:2fc6fc3b5e15 21 eth_error("Ethernet_blocking", _eth->set_blocking(false));
YSI 0:2fc6fc3b5e15 22 eth_error("Ethernet_dhcp", _eth->set_dhcp(TCP_SET_DHCP));
YSI 0:2fc6fc3b5e15 23 if(!TCP_SET_DHCP) eth_error("Ethernet_static", _eth->set_network(SocketAddress(TCP_IP), SocketAddress(TCP_NETMASK), SocketAddress(TCP_GATEWAY)));
YSI 0:2fc6fc3b5e15 24 //{ SocketAddress ip(TCP_IP); SocketAddress mask(TCP_NETMASK); SocketAddress gateway(TCP_GATEWAY); eth_error("Ethernet_static", eth_error("Ethernet_static", _eth->set_network(ip, mask, gateway))); }
YSI 0:2fc6fc3b5e15 25 eth_error("Ethernet_connect", _eth->connect());
YSI 0:2fc6fc3b5e15 26 }
YSI 0:2fc6fc3b5e15 27 break;
YSI 0:2fc6fc3b5e15 28 case NSAPI_STATUS_CONNECTING:
YSI 0:2fc6fc3b5e15 29 if(statusCOLOR == RED_DISCONNECTED)
YSI 0:2fc6fc3b5e15 30 {
YSI 0:2fc6fc3b5e15 31 eth_status("Ethernet_connect", NSAPI_STATUS_CONNECTING);
YSI 0:2fc6fc3b5e15 32 statusCOLOR = YELLOW_CONNECTING;
YSI 0:2fc6fc3b5e15 33 _eth->attach(callback(this, &Transmission::eth_event));
YSI 0:2fc6fc3b5e15 34 }
YSI 0:2fc6fc3b5e15 35 break;
YSI 0:2fc6fc3b5e15 36 case NSAPI_STATUS_GLOBAL_UP: return serverTCP_connect(); break;
YSI 0:2fc6fc3b5e15 37 default: break;
YSI 0:2fc6fc3b5e15 38 }
YSI 0:2fc6fc3b5e15 39 }
YSI 0:2fc6fc3b5e15 40 else if(_eth->get_connection_status() != NSAPI_STATUS_DISCONNECTED) eth_error("Ethernet_disconnect", _eth->disconnect());
YSI 0:2fc6fc3b5e15 41 return false;
YSI 0:2fc6fc3b5e15 42 }
YSI 0:2fc6fc3b5e15 43
YSI 0:2fc6fc3b5e15 44 void Transmission::eth_event(nsapi_event_t status, intptr_t param)
YSI 0:2fc6fc3b5e15 45 {
YSI 0:2fc6fc3b5e15 46 eth_status("Ethernet_event", param);
YSI 0:2fc6fc3b5e15 47 switch(param)
YSI 0:2fc6fc3b5e15 48 {
YSI 0:2fc6fc3b5e15 49 case NSAPI_STATUS_CONNECTING: statusCOLOR = YELLOW_CONNECTING; break;
YSI 0:2fc6fc3b5e15 50 case NSAPI_STATUS_DISCONNECTED: statusCOLOR = RED_DISCONNECTED; break;
YSI 0:2fc6fc3b5e15 51 case NSAPI_STATUS_GLOBAL_UP: statusCOLOR = GREEN_GLOBAL_UP; break;
YSI 0:2fc6fc3b5e15 52 default: break;
YSI 0:2fc6fc3b5e15 53 }
YSI 0:2fc6fc3b5e15 54 }
YSI 0:2fc6fc3b5e15 55
YSI 0:2fc6fc3b5e15 56 intptr_t Transmission::eth_status(const string& source, const intptr_t& code)
YSI 0:2fc6fc3b5e15 57 {
YSI 0:2fc6fc3b5e15 58 stringstream message;
YSI 0:2fc6fc3b5e15 59 message << "\n" << source << "[" << code;
YSI 0:2fc6fc3b5e15 60 switch(code)
YSI 0:2fc6fc3b5e15 61 {
YSI 0:2fc6fc3b5e15 62 case NSAPI_STATUS_LOCAL_UP: message << "] NSAPI_STATUS_LOCAL_UP < local IP address set >"; break;
YSI 0:2fc6fc3b5e15 63 case NSAPI_STATUS_GLOBAL_UP: message << "] NSAPI_STATUS_GLOBAL_UP < global IP address set >"; break;
YSI 0:2fc6fc3b5e15 64 case NSAPI_STATUS_DISCONNECTED: message << "] NSAPI_STATUS_DISCONNECTED < no connection to network >"; break;
YSI 0:2fc6fc3b5e15 65 case NSAPI_STATUS_CONNECTING: message << "] NSAPI_STATUS_CONNECTING < connecting to network >"; break;
YSI 0:2fc6fc3b5e15 66 case NSAPI_STATUS_ERROR_UNSUPPORTED: message << "] NSAPI_STATUS_ERROR_UNSUPPORTED < unsupported functionality >";break;
YSI 0:2fc6fc3b5e15 67 }
YSI 0:2fc6fc3b5e15 68 _serial->write(message.str().c_str(), message.str().size());//debug("%s", message.str().c_str());
YSI 0:2fc6fc3b5e15 69 return code;
YSI 0:2fc6fc3b5e15 70 }
YSI 0:2fc6fc3b5e15 71
YSI 0:2fc6fc3b5e15 72 nsapi_error_t Transmission::eth_error(const string& source, const nsapi_error_t& code)
YSI 0:2fc6fc3b5e15 73 {
YSI 0:2fc6fc3b5e15 74 stringstream message;
YSI 0:2fc6fc3b5e15 75 message << "\n" << source << "[" << code;
YSI 0:2fc6fc3b5e15 76 switch(code)
YSI 0:2fc6fc3b5e15 77 {
YSI 0:2fc6fc3b5e15 78 case NSAPI_ERROR_OK: message << "] NSAPI_ERROR_OK < no error >"; break;
YSI 0:2fc6fc3b5e15 79 case NSAPI_ERROR_WOULD_BLOCK: message << "] NSAPI_ERROR_WOULD_BLOCK < no data is not available but call is non-blocking >";break;
YSI 0:2fc6fc3b5e15 80 case NSAPI_ERROR_UNSUPPORTED: message << "] NSAPI_ERROR_UNSUPPORTED < unsupported functionality >"; break;
YSI 0:2fc6fc3b5e15 81 case NSAPI_ERROR_PARAMETER: message << "] NSAPI_ERROR_PARAMETER < invalid configuration >"; break;
YSI 0:2fc6fc3b5e15 82 case NSAPI_ERROR_NO_CONNECTION: message << "] NSAPI_ERROR_NO_CONNECTION < not connected to a network >"; break;
YSI 0:2fc6fc3b5e15 83 case NSAPI_ERROR_NO_SOCKET: message << "] NSAPI_ERROR_NO_SOCKET < socket not available for use >"; break;
YSI 0:2fc6fc3b5e15 84 case NSAPI_ERROR_NO_ADDRESS: message << "] NSAPI_ERROR_NO_ADDRESS < IP address is not known >"; break;
YSI 0:2fc6fc3b5e15 85 case NSAPI_ERROR_NO_MEMORY: message << "] NSAPI_ERROR_NO_MEMORY < memory resource not available >"; break;
YSI 0:2fc6fc3b5e15 86 case NSAPI_ERROR_NO_SSID: message << "] NSAPI_ERROR_NO_SSID < ssid not found >"; break;
YSI 0:2fc6fc3b5e15 87 case NSAPI_ERROR_DNS_FAILURE: message << "] NSAPI_ERROR_DNS_FAILURE < DNS failed to complete successfully >"; break;
YSI 0:2fc6fc3b5e15 88 case NSAPI_ERROR_DHCP_FAILURE: message << "] NSAPI_ERROR_DHCP_FAILURE < DHCP failed to complete successfully >"; break;
YSI 0:2fc6fc3b5e15 89 case NSAPI_ERROR_AUTH_FAILURE: message << "] NSAPI_ERROR_AUTH_FAILURE < connection to access point failed >"; break;
YSI 0:2fc6fc3b5e15 90 case NSAPI_ERROR_DEVICE_ERROR: message << "] NSAPI_ERROR_DEVICE_ERROR < failure interfacing with the network processor >"; break;
YSI 0:2fc6fc3b5e15 91 case NSAPI_ERROR_IN_PROGRESS: message << "] NSAPI_ERROR_IN_PROGRESS < operation (eg connect) in progress >"; break;
YSI 0:2fc6fc3b5e15 92 case NSAPI_ERROR_ALREADY: message << "] NSAPI_ERROR_ALREADY < operation (eg connect) already in progress >"; break;
YSI 0:2fc6fc3b5e15 93 case NSAPI_ERROR_IS_CONNECTED: message << "] NSAPI_ERROR_IS_CONNECTED < socket is already connected >"; break;
YSI 0:2fc6fc3b5e15 94 case NSAPI_ERROR_CONNECTION_LOST: message << "] NSAPI_ERROR_CONNECTION_LOST < connection lost >"; break;
YSI 0:2fc6fc3b5e15 95 case NSAPI_ERROR_CONNECTION_TIMEOUT: message << "] NSAPI_ERROR_CONNECTION_TIMEOUT < connection timed out >"; break;
YSI 0:2fc6fc3b5e15 96 case NSAPI_ERROR_ADDRESS_IN_USE: message << "] NSAPI_ERROR_ADDRESS_IN_USE < Address already in use >"; break;
YSI 0:2fc6fc3b5e15 97 case NSAPI_ERROR_TIMEOUT: message << "] NSAPI_ERROR_TIMEOUT < operation timed out >"; break;
YSI 0:2fc6fc3b5e15 98 case NSAPI_ERROR_BUSY: message << "] NSAPI_ERROR_BUSY < device is busy and cannot accept new operation >"; break;
YSI 0:2fc6fc3b5e15 99 default: message << "] NSAPI_ERROR < unknow code >"; break;
YSI 0:2fc6fc3b5e15 100 }
YSI 0:2fc6fc3b5e15 101 if(code < NSAPI_ERROR_OK) _serial->write(message.str().c_str(), message.str().size());//debug("%s", message.str().c_str());
YSI 0:2fc6fc3b5e15 102 return code;
YSI 0:2fc6fc3b5e15 103 }
YSI 0:2fc6fc3b5e15 104
YSI 0:2fc6fc3b5e15 105 bool Transmission::serverTCP_connect(void)
YSI 0:2fc6fc3b5e15 106 {
YSI 0:2fc6fc3b5e15 107 static bool serverTCP_connect = false;
YSI 0:2fc6fc3b5e15 108 if(!serverTCP_connect)
YSI 0:2fc6fc3b5e15 109 if (eth_error("serverTCP_open", serverTCP.open(_eth)) == NSAPI_ERROR_OK)
YSI 0:2fc6fc3b5e15 110 if (eth_error("serverTCP_bind", serverTCP.bind(TCP_PORT)) == NSAPI_ERROR_OK)
YSI 0:2fc6fc3b5e15 111 if (eth_error("serverTCP_listen", serverTCP.listen()) == NSAPI_ERROR_OK)
YSI 0:2fc6fc3b5e15 112 {
YSI 0:2fc6fc3b5e15 113 serverTCP.set_blocking(false);
YSI 0:2fc6fc3b5e15 114 serverTCP.sigio(callback(this, &Transmission::serverTCP_event));
YSI 0:2fc6fc3b5e15 115 serverTCP_connect = true;
YSI 0:2fc6fc3b5e15 116 fn_com_init();
YSI 0:2fc6fc3b5e15 117 }
YSI 0:2fc6fc3b5e15 118 return serverTCP_connect;
YSI 0:2fc6fc3b5e15 119 }
YSI 0:2fc6fc3b5e15 120
YSI 0:2fc6fc3b5e15 121 void Transmission::serverTCP_event(void)
YSI 0:2fc6fc3b5e15 122 {
YSI 0:2fc6fc3b5e15 123 _queue->call(this, &Transmission::serverTCP_accept);
YSI 0:2fc6fc3b5e15 124 }
YSI 0:2fc6fc3b5e15 125
YSI 0:2fc6fc3b5e15 126 void Transmission::serverTCP_accept(void)
YSI 0:2fc6fc3b5e15 127 {
YSI 0:2fc6fc3b5e15 128 if(statusCOLOR == GREEN_GLOBAL_UP)
YSI 0:2fc6fc3b5e15 129 {
YSI 0:2fc6fc3b5e15 130 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK;
YSI 0:2fc6fc3b5e15 131 statusCOLOR = MAGENTA_ACCEPT;
YSI 0:2fc6fc3b5e15 132 clientTCP = serverTCP.accept(&ack);
YSI 0:2fc6fc3b5e15 133 switch(ack)
YSI 0:2fc6fc3b5e15 134 {
YSI 0:2fc6fc3b5e15 135 case NSAPI_ERROR_OK:
YSI 0:2fc6fc3b5e15 136 clientTCP->set_timeout(TCP_CLIENT_TIMEOUT);
YSI 0:2fc6fc3b5e15 137 statusCOLOR = BLUE_CLIENT;
YSI 0:2fc6fc3b5e15 138 break;
YSI 0:2fc6fc3b5e15 139 case NSAPI_ERROR_WOULD_BLOCK:
YSI 0:2fc6fc3b5e15 140 statusCOLOR = GREEN_GLOBAL_UP;
YSI 0:2fc6fc3b5e15 141 break;
YSI 0:2fc6fc3b5e15 142 case NSAPI_ERROR_NO_CONNECTION:
YSI 0:2fc6fc3b5e15 143 statusCOLOR = GREEN_GLOBAL_UP;
YSI 0:2fc6fc3b5e15 144 _queue->call(this, &Transmission::serverTCP_accept);
YSI 0:2fc6fc3b5e15 145 break;
YSI 0:2fc6fc3b5e15 146 default:
YSI 0:2fc6fc3b5e15 147 statusCOLOR = GREEN_GLOBAL_UP;
YSI 0:2fc6fc3b5e15 148 eth_error("serverTCP_accept", ack);
YSI 0:2fc6fc3b5e15 149 break;
YSI 0:2fc6fc3b5e15 150 }
YSI 0:2fc6fc3b5e15 151 }
YSI 0:2fc6fc3b5e15 152 }
YSI 0:2fc6fc3b5e15 153
YSI 0:2fc6fc3b5e15 154 nsapi_error_t Transmission::send(const string& buff, const enumCOM& type)
YSI 0:2fc6fc3b5e15 155 {
YSI 0:2fc6fc3b5e15 156 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK;
YSI 0:2fc6fc3b5e15 157 string ssend(buff+"\n");
YSI 0:2fc6fc3b5e15 158 switch(type)
YSI 0:2fc6fc3b5e15 159 {
YSI 0:2fc6fc3b5e15 160 case TCP:
YSI 0:2fc6fc3b5e15 161 if(message.BREAK)
YSI 0:2fc6fc3b5e15 162 {
YSI 0:2fc6fc3b5e15 163 message.BREAK = false;
YSI 0:2fc6fc3b5e15 164 eth_error("clientTCP_disconnect", clientTCP->close());
YSI 0:2fc6fc3b5e15 165 switch(_eth->get_connection_status())
YSI 0:2fc6fc3b5e15 166 {
YSI 0:2fc6fc3b5e15 167 case NSAPI_STATUS_DISCONNECTED: statusCOLOR = RED_DISCONNECTED; break;
YSI 0:2fc6fc3b5e15 168 case NSAPI_STATUS_CONNECTING: statusCOLOR = YELLOW_CONNECTING; break;
YSI 0:2fc6fc3b5e15 169 case NSAPI_STATUS_GLOBAL_UP: statusCOLOR = GREEN_GLOBAL_UP; break;
YSI 0:2fc6fc3b5e15 170 default: break;
YSI 0:2fc6fc3b5e15 171 }
YSI 0:2fc6fc3b5e15 172 _queue->call(this, &Transmission::serverTCP_accept);
YSI 0:2fc6fc3b5e15 173 }
YSI 0:2fc6fc3b5e15 174 else if(!buff.empty())
YSI 0:2fc6fc3b5e15 175 {
YSI 0:2fc6fc3b5e15 176 ack = clientTCP->send(ssend.c_str(), ssend.size());
YSI 0:2fc6fc3b5e15 177 if(message.HTTP)
YSI 0:2fc6fc3b5e15 178 {
YSI 0:2fc6fc3b5e15 179 message.HTTP = false;
YSI 0:2fc6fc3b5e15 180 eth_error("clientTCP_disconnect", clientTCP->close());
YSI 0:2fc6fc3b5e15 181 switch(_eth->get_connection_status())
YSI 0:2fc6fc3b5e15 182 {
YSI 0:2fc6fc3b5e15 183 case NSAPI_STATUS_DISCONNECTED: statusCOLOR = RED_DISCONNECTED; break;
YSI 0:2fc6fc3b5e15 184 case NSAPI_STATUS_CONNECTING: statusCOLOR = YELLOW_CONNECTING; break;
YSI 0:2fc6fc3b5e15 185 case NSAPI_STATUS_GLOBAL_UP: statusCOLOR = GREEN_GLOBAL_UP; break;
YSI 0:2fc6fc3b5e15 186 default: break;
YSI 0:2fc6fc3b5e15 187 }
YSI 0:2fc6fc3b5e15 188 _queue->call(this, &Transmission::serverTCP_accept);
YSI 0:2fc6fc3b5e15 189 }
YSI 0:2fc6fc3b5e15 190 }
YSI 0:2fc6fc3b5e15 191 break;
YSI 0:2fc6fc3b5e15 192 case SERIAL:
YSI 0:2fc6fc3b5e15 193 if(!buff.empty()) ack = _serial->write(ssend.c_str(), ssend.length());
YSI 0:2fc6fc3b5e15 194 break;
YSI 0:2fc6fc3b5e15 195 }
YSI 0:2fc6fc3b5e15 196 return ack;
YSI 0:2fc6fc3b5e15 197 }
YSI 0:2fc6fc3b5e15 198
YSI 0:2fc6fc3b5e15 199 enumCOLOR Transmission::recv(void)
YSI 0:2fc6fc3b5e15 200 {
YSI 0:2fc6fc3b5e15 201 if(eth_connect())
YSI 0:2fc6fc3b5e15 202 {
YSI 0:2fc6fc3b5e15 203 char buffer[1024] = {0};
YSI 0:2fc6fc3b5e15 204 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK;
YSI 0:2fc6fc3b5e15 205 if(statusCOLOR == BLUE_CLIENT)
YSI 0:2fc6fc3b5e15 206 if((ack = clientTCP->recv(buffer, 1024)) < NSAPI_ERROR_WOULD_BLOCK)
YSI 0:2fc6fc3b5e15 207 eth_error("clientTCP_recv", ack);
YSI 0:2fc6fc3b5e15 208 switch(ack)
YSI 0:2fc6fc3b5e15 209 {
YSI 0:2fc6fc3b5e15 210 case NSAPI_ERROR_NO_CONNECTION:
YSI 0:2fc6fc3b5e15 211 case NSAPI_ERROR_OK:
YSI 0:2fc6fc3b5e15 212 message.BREAK = true; break;
YSI 0:2fc6fc3b5e15 213 default: break;
YSI 0:2fc6fc3b5e15 214 }
YSI 0:2fc6fc3b5e15 215 for(int i = 0; i < ack; i++) if(buffer[i] == '\n') buffer[i] = ';';
YSI 0:2fc6fc3b5e15 216 fn_com_processing(message.buffer[TCP] = buffer, TCP);
YSI 0:2fc6fc3b5e15 217 message.buffer[TCP].clear();
YSI 0:2fc6fc3b5e15 218 }
YSI 0:2fc6fc3b5e15 219 if(_serial->readable())
YSI 0:2fc6fc3b5e15 220 {
YSI 0:2fc6fc3b5e15 221 char caractere;
YSI 0:2fc6fc3b5e15 222 _serial->read(&caractere, 1);
YSI 0:2fc6fc3b5e15 223 switch(caractere)
YSI 0:2fc6fc3b5e15 224 {
YSI 0:2fc6fc3b5e15 225 case '\n':
YSI 0:2fc6fc3b5e15 226 fn_com_processing(message.buffer[SERIAL], SERIAL);
YSI 0:2fc6fc3b5e15 227 message.buffer[SERIAL].clear();
YSI 0:2fc6fc3b5e15 228 break;
YSI 0:2fc6fc3b5e15 229 default:
YSI 0:2fc6fc3b5e15 230 if((caractere > 31) && (caractere < 127)) message.buffer[SERIAL] += caractere;
YSI 0:2fc6fc3b5e15 231 break;
YSI 0:2fc6fc3b5e15 232 }
YSI 0:2fc6fc3b5e15 233 }
YSI 0:2fc6fc3b5e15 234 return statusCOLOR;
YSI 0:2fc6fc3b5e15 235 }
YSI 0:2fc6fc3b5e15 236
YSI 0:2fc6fc3b5e15 237 bool Transmission::sendSMTP(const char* MAIL, const char* FROM, const char* SUBJECT, const char* DATA)
YSI 0:2fc6fc3b5e15 238 {
YSI 0:2fc6fc3b5e15 239 TCPSocket clientSMTP;
YSI 0:2fc6fc3b5e15 240 clientSMTP.set_timeout(5000);
YSI 0:2fc6fc3b5e15 241 string code;
YSI 0:2fc6fc3b5e15 242 if(eth_error("clientSMTP_open", clientSMTP.open(_eth)) == NSAPI_ERROR_OK)
YSI 0:2fc6fc3b5e15 243 {
YSI 0:2fc6fc3b5e15 244 const string sMAIL(MAIL), sFROM(FROM), sSUBJECT(SUBJECT), sDATA(DATA),
YSI 0:2fc6fc3b5e15 245 smtpSend[] = { "", "HELO Mbed " + sFROM + "\r\n", "MAIL FROM: <Mbed." + sFROM + "@U-PSUD.FR>\r\n", "RCPT TO: <" + sMAIL + ">\r\n", "DATA\r\n",
YSI 0:2fc6fc3b5e15 246 "From: \"Mbed " + sFROM + "\" <Mbed." + sFROM + "@U-PSUD.FR>\r\nTo: \"DESTINATAIRE\" <" + sMAIL + ">\r\nSubject:" + sSUBJECT + "\r\n" + sDATA + "\r\n.\r\n", "QUIT\r\n" };
YSI 0:2fc6fc3b5e15 247 for(const string ssend : smtpSend)
YSI 0:2fc6fc3b5e15 248 {
YSI 0:2fc6fc3b5e15 249 char buffer[256] = {0};
YSI 0:2fc6fc3b5e15 250 if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(SMTP_SERVER, 25))) < NSAPI_ERROR_OK) break; }
YSI 0:2fc6fc3b5e15 251 else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break;
YSI 0:2fc6fc3b5e15 252 if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 256)) < NSAPI_ERROR_OK) break;
YSI 0:2fc6fc3b5e15 253 buffer[3] = 0;
YSI 0:2fc6fc3b5e15 254 code += buffer;
YSI 0:2fc6fc3b5e15 255 }
YSI 0:2fc6fc3b5e15 256 eth_error("clientSMTP_close", clientSMTP.close());
YSI 0:2fc6fc3b5e15 257 }
YSI 0:2fc6fc3b5e15 258 if(code != "220250250250354250221")
YSI 0:2fc6fc3b5e15 259 {
YSI 0:2fc6fc3b5e15 260 _queue->call_in(60s, this, &Transmission::sendSMTP, MAIL, FROM, SUBJECT, DATA);
YSI 0:2fc6fc3b5e15 261 return false;
YSI 0:2fc6fc3b5e15 262 }
YSI 0:2fc6fc3b5e15 263 return true;
YSI 0:2fc6fc3b5e15 264 }
YSI 0:2fc6fc3b5e15 265
YSI 0:2fc6fc3b5e15 266 bool Transmission::checkSMTP(const char* MAIL)
YSI 0:2fc6fc3b5e15 267 {
YSI 0:2fc6fc3b5e15 268 TCPSocket clientSMTP;
YSI 0:2fc6fc3b5e15 269 clientSMTP.set_timeout(5000);
YSI 0:2fc6fc3b5e15 270 string code(MAIL);
YSI 0:2fc6fc3b5e15 271 if(eth_error("clientSMTP_open", clientSMTP.open(_eth)) == NSAPI_ERROR_OK)
YSI 0:2fc6fc3b5e15 272 {
YSI 0:2fc6fc3b5e15 273 const string smtpSend[] = { "", "HELO Mbed\r\n", "MAIL FROM: <Mbed>\r\n","RCPT TO: <" + code + ">\r\n", "QUIT\r\n" };
YSI 0:2fc6fc3b5e15 274 code.clear();
YSI 0:2fc6fc3b5e15 275 for(const string ssend : smtpSend)
YSI 0:2fc6fc3b5e15 276 {
YSI 0:2fc6fc3b5e15 277 char buffer[256] = {0};
YSI 0:2fc6fc3b5e15 278 if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(SMTP_SERVER, 25))) < NSAPI_ERROR_OK) break; }
YSI 0:2fc6fc3b5e15 279 else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break;
YSI 0:2fc6fc3b5e15 280 if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 256)) < NSAPI_ERROR_OK) break;
YSI 0:2fc6fc3b5e15 281 buffer[3] = 0;
YSI 0:2fc6fc3b5e15 282 code += buffer;
YSI 0:2fc6fc3b5e15 283 }
YSI 0:2fc6fc3b5e15 284 eth_error("clientSMTP_close", clientSMTP.close());
YSI 0:2fc6fc3b5e15 285 }
YSI 0:2fc6fc3b5e15 286 return code == "220250250250221";
YSI 0:2fc6fc3b5e15 287 }