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:
Tue Mar 09 14:50:24 2021 +0000
Revision:
17:9b7100c31466
Parent:
16:3ef69ffede76
Child:
18:15778c8a97a1
Corrected '\n' processing bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:2fc6fc3b5e15 1 #include "lib_Transmission.h"
YSI 16:3ef69ffede76 2 #include <string>
YSI 0:2fc6fc3b5e15 3
YSI 14:9e3accc681c4 4 Transmission::Transmission(
YSI 14:9e3accc681c4 5 #if MBED_MAJOR_VERSION > 5
YSI 15:b2da6ab01a21 6 UnbufferedSerial *serial,
YSI 14:9e3accc681c4 7 #else
YSI 15:b2da6ab01a21 8 Serial *serial,
YSI 14:9e3accc681c4 9 #endif
YSI 15:b2da6ab01a21 10 EthernetInterface *eth,
YSI 15:b2da6ab01a21 11 USBCDC *usb,
YSI 15:b2da6ab01a21 12 string (*processing)(string),
YSI 15:b2da6ab01a21 13 void (*startuped)(void),
YSI 15:b2da6ab01a21 14 bool caseIgnore)
YSI 15:b2da6ab01a21 15 :_serial(serial), _eth(eth), _usb(usb), _processing(processing), _startuped(startuped), _caseIgnore(caseIgnore)
YSI 0:2fc6fc3b5e15 16 {
YSI 16:3ef69ffede76 17 if(_serial) _serial->attach(callback(this, &Transmission::serial_event));
YSI 14:9e3accc681c4 18 _queue = mbed_event_queue();
YSI 0:2fc6fc3b5e15 19 }
YSI 0:2fc6fc3b5e15 20
YSI 15:b2da6ab01a21 21 Transmission::Transmission(
YSI 15:b2da6ab01a21 22 #if MBED_MAJOR_VERSION > 5
YSI 15:b2da6ab01a21 23 UnbufferedSerial *serial,
YSI 15:b2da6ab01a21 24 #else
YSI 15:b2da6ab01a21 25 Serial *serial,
YSI 15:b2da6ab01a21 26 #endif
YSI 15:b2da6ab01a21 27 string (*processing)(string),
YSI 15:b2da6ab01a21 28 bool caseIgnore)
YSI 15:b2da6ab01a21 29 :_serial(serial), _processing(processing), _caseIgnore(caseIgnore)
YSI 15:b2da6ab01a21 30 {
YSI 16:3ef69ffede76 31 if(_serial) _serial->attach(callback(this, &Transmission::serial_event));
YSI 15:b2da6ab01a21 32 _queue = mbed_event_queue();
YSI 15:b2da6ab01a21 33 }
YSI 15:b2da6ab01a21 34
YSI 15:b2da6ab01a21 35 Transmission::Transmission(
YSI 15:b2da6ab01a21 36 EthernetInterface *eth,
YSI 15:b2da6ab01a21 37 string (*processing)(string),
YSI 15:b2da6ab01a21 38 void (*startuped)(void),
YSI 15:b2da6ab01a21 39 bool caseIgnore)
YSI 15:b2da6ab01a21 40 :_eth(eth), _processing(processing), _startuped(startuped), _caseIgnore(caseIgnore)
YSI 15:b2da6ab01a21 41 {
YSI 15:b2da6ab01a21 42 _queue = mbed_event_queue();
YSI 15:b2da6ab01a21 43 }
YSI 15:b2da6ab01a21 44
YSI 15:b2da6ab01a21 45 Transmission::Transmission(
YSI 15:b2da6ab01a21 46 USBCDC *usb,
YSI 15:b2da6ab01a21 47 string (*processing)(string),
YSI 15:b2da6ab01a21 48 bool caseIgnore)
YSI 15:b2da6ab01a21 49 :_usb(usb), _processing(processing), _caseIgnore(caseIgnore){}
YSI 15:b2da6ab01a21 50
YSI 14:9e3accc681c4 51 string Transmission::ip(const bool& SET, const char* IP, const uint16_t& PORT, const uint16_t& TIMEOUT)
YSI 3:7e15bf0a71f4 52 {
YSI 16:3ef69ffede76 53 if(!_eth) return "00:00:00:00:00:00";
YSI 10:25e049353db5 54 if(message.SET && SET)
YSI 3:7e15bf0a71f4 55 {
YSI 3:7e15bf0a71f4 56 if(message.PORT != PORT)
YSI 3:7e15bf0a71f4 57 {
YSI 3:7e15bf0a71f4 58 message.CONNECT = false;
YSI 3:7e15bf0a71f4 59 _serverTCP.sigio(NULL);
YSI 3:7e15bf0a71f4 60 eth_error("serverTCP_close", _serverTCP.close());
YSI 3:7e15bf0a71f4 61 }
YSI 3:7e15bf0a71f4 62 eth_error("Ethernet_disconnect", _eth->disconnect());
YSI 3:7e15bf0a71f4 63 }
YSI 10:25e049353db5 64 message.SET = SET;
YSI 3:7e15bf0a71f4 65 message.IP = IP;
YSI 3:7e15bf0a71f4 66 message.PORT = PORT;
YSI 14:9e3accc681c4 67 message.TIMEOUT = TIMEOUT;
YSI 3:7e15bf0a71f4 68 message.DHCP = message.IP.empty();
YSI 10:25e049353db5 69 eth_connect();
YSI 14:9e3accc681c4 70 return _eth->get_mac_address()?_eth->get_mac_address():"00:00:00:00:00:00";
YSI 3:7e15bf0a71f4 71 }
YSI 3:7e15bf0a71f4 72
YSI 14:9e3accc681c4 73 string Transmission::ip(void)
YSI 3:7e15bf0a71f4 74 {
YSI 16:3ef69ffede76 75 if(!_eth) return "0.0.0.0";
YSI 3:7e15bf0a71f4 76 SocketAddress ip;
YSI 3:7e15bf0a71f4 77 _eth->get_ip_address(&ip);
YSI 3:7e15bf0a71f4 78 string address(ip.get_ip_address()?ip.get_ip_address():"0.0.0.0");
YSI 3:7e15bf0a71f4 79 address += ":" + to_string(message.PORT);
YSI 3:7e15bf0a71f4 80 return address;
YSI 3:7e15bf0a71f4 81 }
YSI 3:7e15bf0a71f4 82
YSI 0:2fc6fc3b5e15 83 bool Transmission::eth_connect(void)
YSI 0:2fc6fc3b5e15 84 {
YSI 16:3ef69ffede76 85 if(!_eth) return false;
YSI 10:25e049353db5 86 if(message.SET)
YSI 0:2fc6fc3b5e15 87 {
YSI 0:2fc6fc3b5e15 88 switch(_eth->get_connection_status())
YSI 0:2fc6fc3b5e15 89 {
YSI 0:2fc6fc3b5e15 90 case NSAPI_STATUS_DISCONNECTED:
YSI 14:9e3accc681c4 91 eth_error("Ethernet_blocking", _eth->set_blocking(false));
YSI 14:9e3accc681c4 92 eth_error("Ethernet_dhcp", _eth->set_dhcp(message.DHCP));
YSI 14:9e3accc681c4 93 if(!message.DHCP) eth_error("Ethernet_static", _eth->set_network(SocketAddress(message.IP.c_str()), SocketAddress("255.255.255.0"), SocketAddress("192.168.1.1")));
YSI 14:9e3accc681c4 94 _eth->attach(callback(this, &Transmission::eth_event));
YSI 15:b2da6ab01a21 95 eth_error("Ethernet_connect", _eth->connect()); break;
YSI 14:9e3accc681c4 96 case NSAPI_STATUS_GLOBAL_UP: return message.CONNECT;break;
YSI 14:9e3accc681c4 97 default: break;
YSI 0:2fc6fc3b5e15 98 }
YSI 0:2fc6fc3b5e15 99 }
YSI 0:2fc6fc3b5e15 100 else if(_eth->get_connection_status() != NSAPI_STATUS_DISCONNECTED) eth_error("Ethernet_disconnect", _eth->disconnect());
YSI 0:2fc6fc3b5e15 101 return false;
YSI 0:2fc6fc3b5e15 102 }
YSI 0:2fc6fc3b5e15 103
YSI 0:2fc6fc3b5e15 104 void Transmission::eth_event(nsapi_event_t status, intptr_t param)
YSI 0:2fc6fc3b5e15 105 {
YSI 0:2fc6fc3b5e15 106 eth_status("Ethernet_event", param);
YSI 0:2fc6fc3b5e15 107 switch(param)
YSI 0:2fc6fc3b5e15 108 {
YSI 15:b2da6ab01a21 109 case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break;
YSI 4:9a4ab4f406ab 110 case NSAPI_STATUS_CONNECTING:if(message.status == BLUE_CLIENT) eth_error("clientTCP_disconnect", _clientTCP->close());
YSI 15:b2da6ab01a21 111 message.status = YELLOW_CONNECTING; break;
YSI 3:7e15bf0a71f4 112 case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP;
YSI 15:b2da6ab01a21 113 if(!message.CONNECT) serverTCP_connect();
YSI 15:b2da6ab01a21 114 else serverTCP_event(); break;
YSI 15:b2da6ab01a21 115 default: break;
YSI 1:27f6baabb15e 116 }
YSI 1:27f6baabb15e 117 }
YSI 1:27f6baabb15e 118
YSI 1:27f6baabb15e 119 bool Transmission::serverTCP_connect(void)
YSI 1:27f6baabb15e 120 {
YSI 3:7e15bf0a71f4 121 if(!message.CONNECT)
YSI 3:7e15bf0a71f4 122 if(eth_error("serverTCP_open", _serverTCP.open(_eth)) == NSAPI_ERROR_OK)
YSI 3:7e15bf0a71f4 123 if(eth_error("serverTCP_bind", _serverTCP.bind(message.PORT)) == NSAPI_ERROR_OK)
YSI 3:7e15bf0a71f4 124 if(eth_error("serverTCP_listen", _serverTCP.listen()) == NSAPI_ERROR_OK)
YSI 1:27f6baabb15e 125 {
YSI 3:7e15bf0a71f4 126 _serverTCP.set_blocking(false);
YSI 3:7e15bf0a71f4 127 _serverTCP.sigio(callback(this, &Transmission::serverTCP_event));
YSI 3:7e15bf0a71f4 128 message.CONNECT = true;
YSI 16:3ef69ffede76 129 if(_startuped) _queue->call(_startuped);
YSI 1:27f6baabb15e 130 }
YSI 3:7e15bf0a71f4 131 return message.CONNECT;
YSI 1:27f6baabb15e 132 }
YSI 1:27f6baabb15e 133
YSI 1:27f6baabb15e 134 void Transmission::serverTCP_event(void)
YSI 1:27f6baabb15e 135 {
YSI 14:9e3accc681c4 136 _queue->call(this, &Transmission::serverTCP_accept);
YSI 1:27f6baabb15e 137 }
YSI 1:27f6baabb15e 138
YSI 1:27f6baabb15e 139 void Transmission::serverTCP_accept(void)
YSI 1:27f6baabb15e 140 {
YSI 1:27f6baabb15e 141 if(message.status == GREEN_GLOBAL_UP)
YSI 1:27f6baabb15e 142 {
YSI 1:27f6baabb15e 143 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK;
YSI 1:27f6baabb15e 144 message.status = MAGENTA_ACCEPT;
YSI 3:7e15bf0a71f4 145 _clientTCP = _serverTCP.accept(&ack);
YSI 1:27f6baabb15e 146 switch(ack)
YSI 1:27f6baabb15e 147 {
YSI 1:27f6baabb15e 148 case NSAPI_ERROR_OK:
YSI 14:9e3accc681c4 149 _clientTCP->set_timeout(message.TIMEOUT); // config client bloquante avec timeout sinon limite de transmission a 1072 octets
YSI 1:27f6baabb15e 150 message.status = BLUE_CLIENT;
YSI 1:27f6baabb15e 151 break;
YSI 1:27f6baabb15e 152 case NSAPI_ERROR_NO_CONNECTION:
YSI 4:9a4ab4f406ab 153 eth_state();
YSI 3:7e15bf0a71f4 154 serverTCP_event();
YSI 1:27f6baabb15e 155 break;
YSI 1:27f6baabb15e 156 default:
YSI 4:9a4ab4f406ab 157 eth_state();
YSI 4:9a4ab4f406ab 158 if(ack < NSAPI_ERROR_WOULD_BLOCK) eth_error("serverTCP_accept", ack);
YSI 1:27f6baabb15e 159 break;
YSI 1:27f6baabb15e 160 }
YSI 1:27f6baabb15e 161 }
YSI 1:27f6baabb15e 162 }
YSI 1:27f6baabb15e 163
YSI 4:9a4ab4f406ab 164 void Transmission::eth_state(void)
YSI 4:9a4ab4f406ab 165 {
YSI 4:9a4ab4f406ab 166 switch(_eth->get_connection_status())
YSI 4:9a4ab4f406ab 167 {
YSI 4:9a4ab4f406ab 168 case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break;
YSI 4:9a4ab4f406ab 169 case NSAPI_STATUS_CONNECTING: message.status = YELLOW_CONNECTING; break;
YSI 4:9a4ab4f406ab 170 case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP; break;
YSI 15:b2da6ab01a21 171 default: break;
YSI 4:9a4ab4f406ab 172 }
YSI 4:9a4ab4f406ab 173 }
YSI 4:9a4ab4f406ab 174
YSI 14:9e3accc681c4 175 void Transmission::serial_event(void)
YSI 14:9e3accc681c4 176 {
YSI 14:9e3accc681c4 177 static char buffer[TRANSMISSION_DEFAULT_BUFFER_SIZE] = {0};
YSI 14:9e3accc681c4 178 static uint16_t size = 0;
YSI 14:9e3accc681c4 179 char caractere;
YSI 14:9e3accc681c4 180 _serial->read(&caractere, 1);
YSI 14:9e3accc681c4 181 if((caractere == '\n') || (caractere == '\r'))
YSI 14:9e3accc681c4 182 {
YSI 14:9e3accc681c4 183 buffer[size] = '\0';
YSI 14:9e3accc681c4 184 size = 0;
YSI 16:3ef69ffede76 185 if(_processing) _queue->call(this, &Transmission::preprocessing, buffer, SERIAL);
YSI 14:9e3accc681c4 186 }
YSI 14:9e3accc681c4 187 else if((caractere > 31) && (caractere < 127) && (size < (TRANSMISSION_DEFAULT_BUFFER_SIZE-2))) buffer[size++] = caractere;
YSI 14:9e3accc681c4 188 }
YSI 14:9e3accc681c4 189
YSI 11:de94dcd67561 190 Transmission::enum_trans_status Transmission::recv(void)
YSI 1:27f6baabb15e 191 {
YSI 16:3ef69ffede76 192 if(_usb)
YSI 15:b2da6ab01a21 193 {
YSI 15:b2da6ab01a21 194 _usb->connect();
YSI 15:b2da6ab01a21 195 if(_usb->ready())
YSI 15:b2da6ab01a21 196 {
YSI 15:b2da6ab01a21 197 uint32_t size = 0;
YSI 15:b2da6ab01a21 198 uint8_t buffer[TRANSMISSION_DEFAULT_BUFFER_SIZE] = {0};
YSI 15:b2da6ab01a21 199 _usb->receive_nb(buffer, TRANSMISSION_DEFAULT_BUFFER_SIZE, &size);
YSI 16:3ef69ffede76 200 if(size && _processing) preprocessing((char *)buffer, USB);
YSI 15:b2da6ab01a21 201 }
YSI 15:b2da6ab01a21 202 }
YSI 14:9e3accc681c4 203 if(eth_connect() && (message.status == BLUE_CLIENT))
YSI 1:27f6baabb15e 204 {
YSI 14:9e3accc681c4 205 char buffer[TRANSMISSION_DEFAULT_BUFFER_SIZE] = {0};
YSI 10:25e049353db5 206 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK, size = 0;
YSI 14:9e3accc681c4 207 while((ack = _clientTCP->recv(&buffer[size], TRANSMISSION_DEFAULT_BUFFER_SIZE-size)) > NSAPI_ERROR_OK) size += ack;
YSI 10:25e049353db5 208 if(ack < NSAPI_ERROR_WOULD_BLOCK) eth_error("clientTCP_recv", ack);
YSI 10:25e049353db5 209 if(!size) message.BREAK = ((ack == NSAPI_ERROR_OK) || (ack == NSAPI_ERROR_NO_CONNECTION));
YSI 16:3ef69ffede76 210 if(_processing) preprocessing(buffer, TCP);
YSI 0:2fc6fc3b5e15 211 }
YSI 1:27f6baabb15e 212 return message.status;
YSI 1:27f6baabb15e 213 }
YSI 1:27f6baabb15e 214
YSI 16:3ef69ffede76 215 void Transmission::preprocessing(char *buffer, const enum_trans_delivery delivery)
YSI 16:3ef69ffede76 216 {
YSI 16:3ef69ffede76 217 string cmd(buffer);
YSI 17:9b7100c31466 218 for(char &c : cmd) if(_caseIgnore && (c >= 'a') && (c <= 'z')) c += 'A'-'a';
YSI 16:3ef69ffede76 219 if((cmd.find("HOST: ") != string::npos) || (cmd.find("Host: ") != string::npos))
YSI 16:3ef69ffede76 220 send(_processing(cmd), Transmission::HTTP);
YSI 16:3ef69ffede76 221 else
YSI 16:3ef69ffede76 222 {
YSI 17:9b7100c31466 223 for(char &c : cmd) if(c == '\n') c = ';';
YSI 17:9b7100c31466 224 istringstream srecv(cmd);
YSI 16:3ef69ffede76 225 string ssend;
YSI 16:3ef69ffede76 226 while(getline(srecv, cmd, ';'))
YSI 16:3ef69ffede76 227 {
YSI 17:9b7100c31466 228 string process = _processing(cmd);
YSI 17:9b7100c31466 229 if(!process.empty()) ssend += (ssend.size() > 0)?(' '+process):process;
YSI 16:3ef69ffede76 230 }
YSI 16:3ef69ffede76 231 send(ssend, delivery);
YSI 16:3ef69ffede76 232 }
YSI 16:3ef69ffede76 233 }
YSI 16:3ef69ffede76 234
YSI 15:b2da6ab01a21 235 nsapi_error_t Transmission::send(const string& buff, const enum_trans_delivery& delivery)
YSI 1:27f6baabb15e 236 {
YSI 1:27f6baabb15e 237 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK;
YSI 1:27f6baabb15e 238 string ssend(buff+"\n");
YSI 16:3ef69ffede76 239 if(_usb && !buff.empty() && ((delivery == USB) || (delivery == ANY)))
YSI 15:b2da6ab01a21 240 _usb->send((uint8_t*)ssend.c_str(), ssend.size());
YSI 16:3ef69ffede76 241 if(_serial && !buff.empty() && ((delivery == SERIAL) || (delivery == ANY)))
YSI 11:de94dcd67561 242 ack = _serial->write(ssend.c_str(), ssend.length());
YSI 16:3ef69ffede76 243 if(_eth && ((delivery == TCP) || (delivery == HTTP) || (delivery == ANY)))
YSI 1:27f6baabb15e 244 {
YSI 15:b2da6ab01a21 245 if(!message.BREAK && (message.status == BLUE_CLIENT) && !buff.empty())
YSI 4:9a4ab4f406ab 246 eth_error("clientTCP_send", ack = _clientTCP->send(ssend.c_str(), ssend.size()));
YSI 15:b2da6ab01a21 247 if(message.BREAK || (delivery == HTTP))
YSI 4:9a4ab4f406ab 248 {
YSI 10:25e049353db5 249 message.BREAK = false;
YSI 4:9a4ab4f406ab 250 eth_error("clientTCP_disconnect", _clientTCP->close());
YSI 4:9a4ab4f406ab 251 eth_state();
YSI 4:9a4ab4f406ab 252 serverTCP_event();
YSI 4:9a4ab4f406ab 253 }
YSI 1:27f6baabb15e 254 }
YSI 1:27f6baabb15e 255 return ack;
YSI 1:27f6baabb15e 256 }
YSI 1:27f6baabb15e 257
YSI 14:9e3accc681c4 258 bool Transmission::smtp(const char* MAIL, const char* FROM, const char* SUBJECT, const char* DATA, const char* SERVER)
YSI 1:27f6baabb15e 259 {
YSI 16:3ef69ffede76 260 if(!_eth) return false;
YSI 10:25e049353db5 261 if((!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP)) return false;
YSI 1:27f6baabb15e 262 TCPSocket clientSMTP;
YSI 14:9e3accc681c4 263 clientSMTP.set_timeout(2000);
YSI 14:9e3accc681c4 264 string sMAIL(MAIL), sFROM(FROM), sSUBJECT(SUBJECT), sDATA(DATA), sTO(sMAIL.substr(0, sMAIL.find("@")));
YSI 15:b2da6ab01a21 265 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" },
YSI 15:b2da6ab01a21 266 { "", "HELO Mbed\r\n", "MAIL FROM: <Mbed>\r\n","RCPT TO: <" + sMAIL + ">\r\n", "QUIT\r\n" }};
YSI 1:27f6baabb15e 267 string code;
YSI 1:27f6baabb15e 268 if(eth_error("clientSMTP_open", clientSMTP.open(_eth)) == NSAPI_ERROR_OK)
YSI 1:27f6baabb15e 269 {
YSI 14:9e3accc681c4 270 for(const string& ssend : smtpParams[(sFROM.empty())?1:0])
YSI 1:27f6baabb15e 271 {
YSI 1:27f6baabb15e 272 char buffer[256] = {0};
YSI 14:9e3accc681c4 273 if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(SERVER, 25))) < NSAPI_ERROR_OK) break; }
YSI 1:27f6baabb15e 274 else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break;
YSI 1:27f6baabb15e 275 if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 256)) < NSAPI_ERROR_OK) break;
YSI 1:27f6baabb15e 276 buffer[3] = 0;
YSI 1:27f6baabb15e 277 code += buffer;
YSI 1:27f6baabb15e 278 if(ssend == "QUIT\r\n") break;
YSI 1:27f6baabb15e 279 }
YSI 1:27f6baabb15e 280 eth_error("clientSMTP_close", clientSMTP.close());
YSI 1:27f6baabb15e 281 }
YSI 1:27f6baabb15e 282 if(sFROM.empty()) return code == "220250250250221";
YSI 12:e22ff63d237c 283 #if MBED_MAJOR_VERSION > 5
YSI 14:9e3accc681c4 284 else if(code != "220250250250354250221") _queue->call_in(60s, this, &Transmission::smtp, MAIL, FROM, SUBJECT, DATA, SERVER);
YSI 12:e22ff63d237c 285 #else
YSI 14:9e3accc681c4 286 else if(code != "220250250250354250221") _queue->call_in(60000, this, &Transmission::smtp, MAIL, FROM, SUBJECT, DATA, SERVER);
YSI 12:e22ff63d237c 287 #endif
YSI 1:27f6baabb15e 288 return code == "220250250250354250221";
YSI 0:2fc6fc3b5e15 289 }
YSI 0:2fc6fc3b5e15 290
YSI 14:9e3accc681c4 291 time_t Transmission::ntp(const char* SERVER)
YSI 6:d6a07fd1548a 292 {
YSI 16:3ef69ffede76 293 if(!_eth) return 0;
YSI 10:25e049353db5 294 if((!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP)) return 0;
YSI 6:d6a07fd1548a 295 time_t timeStamp = 0;
YSI 6:d6a07fd1548a 296 UDPSocket clientNTP;
YSI 14:9e3accc681c4 297 clientNTP.set_timeout(2000);
YSI 6:d6a07fd1548a 298 if(eth_error("clientNTP_open", clientNTP.open(_eth)) == NSAPI_ERROR_OK)
YSI 6:d6a07fd1548a 299 {
YSI 6:d6a07fd1548a 300 uint32_t buffer[12] = { 0b11011, 0 }; // VN = 3 & Mode = 3
YSI 14:9e3accc681c4 301 SocketAddress server(SERVER, 123);
YSI 14:9e3accc681c4 302 string sSERVER(SERVER);
YSI 14:9e3accc681c4 303 if(sSERVER != TRANSMISSION_DEFAULT_NTP_SERVER) eth_error("eth_gethostbyname", _eth->gethostbyname(sSERVER.c_str(), &server));
YSI 14:9e3accc681c4 304 if(eth_error("clientNTP_send", clientNTP.sendto(server, (void*)buffer, sizeof(buffer))) > NSAPI_ERROR_OK)
YSI 6:d6a07fd1548a 305 {
YSI 6:d6a07fd1548a 306 if(eth_error("clientNTP_recv", clientNTP.recvfrom(NULL, (void*)buffer, sizeof(buffer))) > NSAPI_ERROR_OK)
YSI 6:d6a07fd1548a 307 {
YSI 6:d6a07fd1548a 308 timeStamp = ((buffer[10] & 0xFF) << 24) | ((buffer[10] & 0xFF00) << 8) | ((buffer[10] & 0xFF0000UL) >> 8) | ((buffer[10] & 0xFF000000UL) >> 24);
YSI 6:d6a07fd1548a 309 timeStamp -= 2208985200U; // 01/01/1970 Europe
YSI 6:d6a07fd1548a 310 struct tm * tmTimeStamp = localtime(&timeStamp);
YSI 10:25e049353db5 311 if (((tmTimeStamp->tm_mon > 2) && (tmTimeStamp->tm_mon < 10)) || ((tmTimeStamp->tm_mon == 2) && ((tmTimeStamp->tm_mday - tmTimeStamp->tm_wday) > 24)) || ((tmTimeStamp->tm_mon == 9) && ((tmTimeStamp->tm_mday - tmTimeStamp->tm_wday) < 25)))
YSI 7:98b12722e9e2 312 timeStamp += 3600; // DST starts last Sunday of March; 2am (1am UTC), DST ends last Sunday of october; 3am (2am UTC)
YSI 6:d6a07fd1548a 313 }
YSI 6:d6a07fd1548a 314 }
YSI 6:d6a07fd1548a 315 eth_error("clientNTP_close", clientNTP.close());
YSI 6:d6a07fd1548a 316 }
YSI 6:d6a07fd1548a 317 return timeStamp;
YSI 6:d6a07fd1548a 318 }
YSI 6:d6a07fd1548a 319
YSI 0:2fc6fc3b5e15 320 intptr_t Transmission::eth_status(const string& source, const intptr_t& code)
YSI 0:2fc6fc3b5e15 321 {
YSI 14:9e3accc681c4 322 #ifndef NDEBUG
YSI 14:9e3accc681c4 323 ostringstream message;
YSI 0:2fc6fc3b5e15 324 message << "\n" << source << "[" << code;
YSI 0:2fc6fc3b5e15 325 switch(code)
YSI 0:2fc6fc3b5e15 326 {
YSI 0:2fc6fc3b5e15 327 case NSAPI_STATUS_LOCAL_UP: message << "] NSAPI_STATUS_LOCAL_UP < local IP address set >"; break;
YSI 0:2fc6fc3b5e15 328 case NSAPI_STATUS_GLOBAL_UP: message << "] NSAPI_STATUS_GLOBAL_UP < global IP address set >"; break;
YSI 0:2fc6fc3b5e15 329 case NSAPI_STATUS_DISCONNECTED: message << "] NSAPI_STATUS_DISCONNECTED < no connection to network >"; break;
YSI 0:2fc6fc3b5e15 330 case NSAPI_STATUS_CONNECTING: message << "] NSAPI_STATUS_CONNECTING < connecting to network >"; break;
YSI 0:2fc6fc3b5e15 331 case NSAPI_STATUS_ERROR_UNSUPPORTED: message << "] NSAPI_STATUS_ERROR_UNSUPPORTED < unsupported functionality >";break;
YSI 0:2fc6fc3b5e15 332 }
YSI 1:27f6baabb15e 333 _serial->write(message.str().c_str(), message.str().size());
YSI 1:27f6baabb15e 334 #endif
YSI 0:2fc6fc3b5e15 335 return code;
YSI 0:2fc6fc3b5e15 336 }
YSI 0:2fc6fc3b5e15 337
YSI 0:2fc6fc3b5e15 338 nsapi_error_t Transmission::eth_error(const string& source, const nsapi_error_t& code)
YSI 0:2fc6fc3b5e15 339 {
YSI 14:9e3accc681c4 340 #ifndef NDEBUG
YSI 14:9e3accc681c4 341 ostringstream message;
YSI 0:2fc6fc3b5e15 342 message << "\n" << source << "[" << code;
YSI 0:2fc6fc3b5e15 343 switch(code)
YSI 0:2fc6fc3b5e15 344 {
YSI 0:2fc6fc3b5e15 345 case NSAPI_ERROR_OK: message << "] NSAPI_ERROR_OK < no error >"; break;
YSI 0:2fc6fc3b5e15 346 case NSAPI_ERROR_WOULD_BLOCK: message << "] NSAPI_ERROR_WOULD_BLOCK < no data is not available but call is non-blocking >";break;
YSI 0:2fc6fc3b5e15 347 case NSAPI_ERROR_UNSUPPORTED: message << "] NSAPI_ERROR_UNSUPPORTED < unsupported functionality >"; break;
YSI 0:2fc6fc3b5e15 348 case NSAPI_ERROR_PARAMETER: message << "] NSAPI_ERROR_PARAMETER < invalid configuration >"; break;
YSI 0:2fc6fc3b5e15 349 case NSAPI_ERROR_NO_CONNECTION: message << "] NSAPI_ERROR_NO_CONNECTION < not connected to a network >"; break;
YSI 0:2fc6fc3b5e15 350 case NSAPI_ERROR_NO_SOCKET: message << "] NSAPI_ERROR_NO_SOCKET < socket not available for use >"; break;
YSI 0:2fc6fc3b5e15 351 case NSAPI_ERROR_NO_ADDRESS: message << "] NSAPI_ERROR_NO_ADDRESS < IP address is not known >"; break;
YSI 0:2fc6fc3b5e15 352 case NSAPI_ERROR_NO_MEMORY: message << "] NSAPI_ERROR_NO_MEMORY < memory resource not available >"; break;
YSI 0:2fc6fc3b5e15 353 case NSAPI_ERROR_NO_SSID: message << "] NSAPI_ERROR_NO_SSID < ssid not found >"; break;
YSI 0:2fc6fc3b5e15 354 case NSAPI_ERROR_DNS_FAILURE: message << "] NSAPI_ERROR_DNS_FAILURE < DNS failed to complete successfully >"; break;
YSI 0:2fc6fc3b5e15 355 case NSAPI_ERROR_DHCP_FAILURE: message << "] NSAPI_ERROR_DHCP_FAILURE < DHCP failed to complete successfully >"; break;
YSI 0:2fc6fc3b5e15 356 case NSAPI_ERROR_AUTH_FAILURE: message << "] NSAPI_ERROR_AUTH_FAILURE < connection to access point failed >"; break;
YSI 0:2fc6fc3b5e15 357 case NSAPI_ERROR_DEVICE_ERROR: message << "] NSAPI_ERROR_DEVICE_ERROR < failure interfacing with the network processor >"; break;
YSI 0:2fc6fc3b5e15 358 case NSAPI_ERROR_IN_PROGRESS: message << "] NSAPI_ERROR_IN_PROGRESS < operation (eg connect) in progress >"; break;
YSI 0:2fc6fc3b5e15 359 case NSAPI_ERROR_ALREADY: message << "] NSAPI_ERROR_ALREADY < operation (eg connect) already in progress >"; break;
YSI 0:2fc6fc3b5e15 360 case NSAPI_ERROR_IS_CONNECTED: message << "] NSAPI_ERROR_IS_CONNECTED < socket is already connected >"; break;
YSI 0:2fc6fc3b5e15 361 case NSAPI_ERROR_CONNECTION_LOST: message << "] NSAPI_ERROR_CONNECTION_LOST < connection lost >"; break;
YSI 0:2fc6fc3b5e15 362 case NSAPI_ERROR_CONNECTION_TIMEOUT: message << "] NSAPI_ERROR_CONNECTION_TIMEOUT < connection timed out >"; break;
YSI 0:2fc6fc3b5e15 363 case NSAPI_ERROR_ADDRESS_IN_USE: message << "] NSAPI_ERROR_ADDRESS_IN_USE < Address already in use >"; break;
YSI 0:2fc6fc3b5e15 364 case NSAPI_ERROR_TIMEOUT: message << "] NSAPI_ERROR_TIMEOUT < operation timed out >"; break;
YSI 0:2fc6fc3b5e15 365 case NSAPI_ERROR_BUSY: message << "] NSAPI_ERROR_BUSY < device is busy and cannot accept new operation >"; break;
YSI 0:2fc6fc3b5e15 366 default: message << "] NSAPI_ERROR < unknow code >"; break;
YSI 0:2fc6fc3b5e15 367 }
YSI 1:27f6baabb15e 368 if(code < NSAPI_ERROR_OK) _serial->write(message.str().c_str(), message.str().size());
YSI 1:27f6baabb15e 369 #endif
YSI 0:2fc6fc3b5e15 370 return code;
YSI 0:2fc6fc3b5e15 371 }