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
lib_Transmission.cpp@11:de94dcd67561, 2020-12-10 (annotated)
- Committer:
- YSI
- Date:
- Thu Dec 10 09:49:22 2020 +0000
- Revision:
- 11:de94dcd67561
- Parent:
- 10:25e049353db5
- Child:
- 12:e22ff63d237c
add if(NULL) to Transmission constructor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YSI | 0:2fc6fc3b5e15 | 1 | #include "lib_Transmission.h" |
YSI | 0:2fc6fc3b5e15 | 2 | |
YSI | 11:de94dcd67561 | 3 | Transmission::Transmission(UnbufferedSerial *serial, EthernetInterface *eth, void(*init)(void), void(*processing)(string, Transmission::enum_trans_to)) |
YSI | 0:2fc6fc3b5e15 | 4 | { |
YSI | 8:7193327bed34 | 5 | _queueThread.start(callback(&_queue, &EventQueue::dispatch_forever)); |
YSI | 0:2fc6fc3b5e15 | 6 | _serial = serial; |
YSI | 0:2fc6fc3b5e15 | 7 | _eth = eth; |
YSI | 8:7193327bed34 | 8 | _init = init; |
YSI | 8:7193327bed34 | 9 | _processing = processing; |
YSI | 0:2fc6fc3b5e15 | 10 | } |
YSI | 0:2fc6fc3b5e15 | 11 | |
YSI | 10:25e049353db5 | 12 | string Transmission::set(bool SET, const char* IP, uint16_t PORT) |
YSI | 3:7e15bf0a71f4 | 13 | { |
YSI | 11:de94dcd67561 | 14 | if(_eth == NULL) return "00:00:00:00:00:00"; |
YSI | 10:25e049353db5 | 15 | if(message.SET && SET) |
YSI | 3:7e15bf0a71f4 | 16 | { |
YSI | 3:7e15bf0a71f4 | 17 | if(message.PORT != PORT) |
YSI | 3:7e15bf0a71f4 | 18 | { |
YSI | 3:7e15bf0a71f4 | 19 | message.CONNECT = false; |
YSI | 3:7e15bf0a71f4 | 20 | _serverTCP.sigio(NULL); |
YSI | 3:7e15bf0a71f4 | 21 | eth_error("serverTCP_close", _serverTCP.close()); |
YSI | 3:7e15bf0a71f4 | 22 | } |
YSI | 3:7e15bf0a71f4 | 23 | eth_error("Ethernet_disconnect", _eth->disconnect()); |
YSI | 3:7e15bf0a71f4 | 24 | } |
YSI | 10:25e049353db5 | 25 | message.SET = SET; |
YSI | 3:7e15bf0a71f4 | 26 | message.IP = IP; |
YSI | 3:7e15bf0a71f4 | 27 | message.PORT = PORT; |
YSI | 3:7e15bf0a71f4 | 28 | message.DHCP = message.IP.empty(); |
YSI | 10:25e049353db5 | 29 | eth_connect(); |
YSI | 10:25e049353db5 | 30 | return _eth->get_mac_address(); |
YSI | 3:7e15bf0a71f4 | 31 | } |
YSI | 3:7e15bf0a71f4 | 32 | |
YSI | 3:7e15bf0a71f4 | 33 | string Transmission::get(void) |
YSI | 3:7e15bf0a71f4 | 34 | { |
YSI | 11:de94dcd67561 | 35 | if(_eth == NULL) return "0.0.0.0"; |
YSI | 3:7e15bf0a71f4 | 36 | SocketAddress ip; |
YSI | 3:7e15bf0a71f4 | 37 | _eth->get_ip_address(&ip); |
YSI | 3:7e15bf0a71f4 | 38 | string address(ip.get_ip_address()?ip.get_ip_address():"0.0.0.0"); |
YSI | 3:7e15bf0a71f4 | 39 | address += ":" + to_string(message.PORT); |
YSI | 3:7e15bf0a71f4 | 40 | return address; |
YSI | 3:7e15bf0a71f4 | 41 | } |
YSI | 3:7e15bf0a71f4 | 42 | |
YSI | 0:2fc6fc3b5e15 | 43 | bool Transmission::eth_connect(void) |
YSI | 0:2fc6fc3b5e15 | 44 | { |
YSI | 11:de94dcd67561 | 45 | if(_eth == NULL) return false; |
YSI | 10:25e049353db5 | 46 | if(message.SET) |
YSI | 0:2fc6fc3b5e15 | 47 | { |
YSI | 0:2fc6fc3b5e15 | 48 | switch(_eth->get_connection_status()) |
YSI | 0:2fc6fc3b5e15 | 49 | { |
YSI | 0:2fc6fc3b5e15 | 50 | case NSAPI_STATUS_DISCONNECTED: |
YSI | 1:27f6baabb15e | 51 | if(message.status == RED_DISCONNECTED) |
YSI | 0:2fc6fc3b5e15 | 52 | { |
YSI | 0:2fc6fc3b5e15 | 53 | eth_error("Ethernet_blocking", _eth->set_blocking(false)); |
YSI | 3:7e15bf0a71f4 | 54 | eth_error("Ethernet_dhcp", _eth->set_dhcp(message.DHCP)); |
YSI | 3:7e15bf0a71f4 | 55 | 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 | 0:2fc6fc3b5e15 | 56 | eth_error("Ethernet_connect", _eth->connect()); |
YSI | 0:2fc6fc3b5e15 | 57 | } |
YSI | 0:2fc6fc3b5e15 | 58 | break; |
YSI | 0:2fc6fc3b5e15 | 59 | case NSAPI_STATUS_CONNECTING: |
YSI | 1:27f6baabb15e | 60 | if(message.status == RED_DISCONNECTED) |
YSI | 0:2fc6fc3b5e15 | 61 | { |
YSI | 0:2fc6fc3b5e15 | 62 | eth_status("Ethernet_connect", NSAPI_STATUS_CONNECTING); |
YSI | 1:27f6baabb15e | 63 | message.status = YELLOW_CONNECTING; |
YSI | 0:2fc6fc3b5e15 | 64 | _eth->attach(callback(this, &Transmission::eth_event)); |
YSI | 0:2fc6fc3b5e15 | 65 | } |
YSI | 0:2fc6fc3b5e15 | 66 | break; |
YSI | 4:9a4ab4f406ab | 67 | case NSAPI_STATUS_GLOBAL_UP: return message.CONNECT; break; |
YSI | 4:9a4ab4f406ab | 68 | default: break; |
YSI | 0:2fc6fc3b5e15 | 69 | } |
YSI | 0:2fc6fc3b5e15 | 70 | } |
YSI | 0:2fc6fc3b5e15 | 71 | else if(_eth->get_connection_status() != NSAPI_STATUS_DISCONNECTED) eth_error("Ethernet_disconnect", _eth->disconnect()); |
YSI | 0:2fc6fc3b5e15 | 72 | return false; |
YSI | 0:2fc6fc3b5e15 | 73 | } |
YSI | 0:2fc6fc3b5e15 | 74 | |
YSI | 0:2fc6fc3b5e15 | 75 | void Transmission::eth_event(nsapi_event_t status, intptr_t param) |
YSI | 0:2fc6fc3b5e15 | 76 | { |
YSI | 0:2fc6fc3b5e15 | 77 | eth_status("Ethernet_event", param); |
YSI | 0:2fc6fc3b5e15 | 78 | switch(param) |
YSI | 0:2fc6fc3b5e15 | 79 | { |
YSI | 1:27f6baabb15e | 80 | case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break; |
YSI | 4:9a4ab4f406ab | 81 | case NSAPI_STATUS_CONNECTING:if(message.status == BLUE_CLIENT) eth_error("clientTCP_disconnect", _clientTCP->close()); |
YSI | 1:27f6baabb15e | 82 | message.status = YELLOW_CONNECTING; break; |
YSI | 3:7e15bf0a71f4 | 83 | case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP; |
YSI | 3:7e15bf0a71f4 | 84 | if(message.CONNECT) |
YSI | 3:7e15bf0a71f4 | 85 | serverTCP_event(); |
YSI | 3:7e15bf0a71f4 | 86 | else serverTCP_connect(); break; |
YSI | 1:27f6baabb15e | 87 | default: break; |
YSI | 1:27f6baabb15e | 88 | } |
YSI | 1:27f6baabb15e | 89 | } |
YSI | 1:27f6baabb15e | 90 | |
YSI | 1:27f6baabb15e | 91 | bool Transmission::serverTCP_connect(void) |
YSI | 1:27f6baabb15e | 92 | { |
YSI | 3:7e15bf0a71f4 | 93 | if(!message.CONNECT) |
YSI | 3:7e15bf0a71f4 | 94 | if(eth_error("serverTCP_open", _serverTCP.open(_eth)) == NSAPI_ERROR_OK) |
YSI | 3:7e15bf0a71f4 | 95 | if(eth_error("serverTCP_bind", _serverTCP.bind(message.PORT)) == NSAPI_ERROR_OK) |
YSI | 3:7e15bf0a71f4 | 96 | if(eth_error("serverTCP_listen", _serverTCP.listen()) == NSAPI_ERROR_OK) |
YSI | 1:27f6baabb15e | 97 | { |
YSI | 3:7e15bf0a71f4 | 98 | _serverTCP.set_blocking(false); |
YSI | 3:7e15bf0a71f4 | 99 | _serverTCP.sigio(callback(this, &Transmission::serverTCP_event)); |
YSI | 3:7e15bf0a71f4 | 100 | message.CONNECT = true; |
YSI | 11:de94dcd67561 | 101 | if(_init != NULL) _queue.call(_init); |
YSI | 1:27f6baabb15e | 102 | } |
YSI | 3:7e15bf0a71f4 | 103 | return message.CONNECT; |
YSI | 1:27f6baabb15e | 104 | } |
YSI | 1:27f6baabb15e | 105 | |
YSI | 1:27f6baabb15e | 106 | void Transmission::serverTCP_event(void) |
YSI | 1:27f6baabb15e | 107 | { |
YSI | 8:7193327bed34 | 108 | _queue.call(this, &Transmission::serverTCP_accept); |
YSI | 1:27f6baabb15e | 109 | } |
YSI | 1:27f6baabb15e | 110 | |
YSI | 1:27f6baabb15e | 111 | void Transmission::serverTCP_accept(void) |
YSI | 1:27f6baabb15e | 112 | { |
YSI | 1:27f6baabb15e | 113 | if(message.status == GREEN_GLOBAL_UP) |
YSI | 1:27f6baabb15e | 114 | { |
YSI | 1:27f6baabb15e | 115 | nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK; |
YSI | 1:27f6baabb15e | 116 | message.status = MAGENTA_ACCEPT; |
YSI | 3:7e15bf0a71f4 | 117 | _clientTCP = _serverTCP.accept(&ack); |
YSI | 1:27f6baabb15e | 118 | switch(ack) |
YSI | 1:27f6baabb15e | 119 | { |
YSI | 1:27f6baabb15e | 120 | case NSAPI_ERROR_OK: |
YSI | 6:d6a07fd1548a | 121 | _clientTCP->set_timeout(REQUEST_TIMEOUT); |
YSI | 1:27f6baabb15e | 122 | message.status = BLUE_CLIENT; |
YSI | 1:27f6baabb15e | 123 | break; |
YSI | 1:27f6baabb15e | 124 | case NSAPI_ERROR_NO_CONNECTION: |
YSI | 4:9a4ab4f406ab | 125 | eth_state(); |
YSI | 3:7e15bf0a71f4 | 126 | serverTCP_event(); |
YSI | 1:27f6baabb15e | 127 | break; |
YSI | 1:27f6baabb15e | 128 | default: |
YSI | 4:9a4ab4f406ab | 129 | eth_state(); |
YSI | 4:9a4ab4f406ab | 130 | if(ack < NSAPI_ERROR_WOULD_BLOCK) eth_error("serverTCP_accept", ack); |
YSI | 1:27f6baabb15e | 131 | break; |
YSI | 1:27f6baabb15e | 132 | } |
YSI | 1:27f6baabb15e | 133 | } |
YSI | 1:27f6baabb15e | 134 | } |
YSI | 1:27f6baabb15e | 135 | |
YSI | 4:9a4ab4f406ab | 136 | void Transmission::eth_state(void) |
YSI | 4:9a4ab4f406ab | 137 | { |
YSI | 4:9a4ab4f406ab | 138 | switch(_eth->get_connection_status()) |
YSI | 4:9a4ab4f406ab | 139 | { |
YSI | 4:9a4ab4f406ab | 140 | case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break; |
YSI | 4:9a4ab4f406ab | 141 | case NSAPI_STATUS_CONNECTING: message.status = YELLOW_CONNECTING; break; |
YSI | 4:9a4ab4f406ab | 142 | case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP; break; |
YSI | 10:25e049353db5 | 143 | default: break; |
YSI | 4:9a4ab4f406ab | 144 | } |
YSI | 4:9a4ab4f406ab | 145 | } |
YSI | 4:9a4ab4f406ab | 146 | |
YSI | 11:de94dcd67561 | 147 | Transmission::enum_trans_status Transmission::recv(void) |
YSI | 1:27f6baabb15e | 148 | { |
YSI | 1:27f6baabb15e | 149 | if(eth_connect()) |
YSI | 1:27f6baabb15e | 150 | { |
YSI | 10:25e049353db5 | 151 | char buffer[1072] = {0}; |
YSI | 10:25e049353db5 | 152 | nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK, size = 0; |
YSI | 10:25e049353db5 | 153 | if(message.status == BLUE_CLIENT) while((ack = _clientTCP->recv(&buffer[size], 1072-size)) > NSAPI_ERROR_OK) size += ack; |
YSI | 10:25e049353db5 | 154 | if(ack < NSAPI_ERROR_WOULD_BLOCK) eth_error("clientTCP_recv", ack); |
YSI | 10:25e049353db5 | 155 | if(!size) message.BREAK = ((ack == NSAPI_ERROR_OK) || (ack == NSAPI_ERROR_NO_CONNECTION)); |
YSI | 10:25e049353db5 | 156 | for(int i = 0; i < size; i++) if(buffer[i] == '\n') buffer[i] = ';'; |
YSI | 11:de94dcd67561 | 157 | if(_processing != NULL) _processing(buffer, TCP); |
YSI | 0:2fc6fc3b5e15 | 158 | } |
YSI | 11:de94dcd67561 | 159 | if(_serial != NULL) |
YSI | 1:27f6baabb15e | 160 | { |
YSI | 11:de94dcd67561 | 161 | if(_serial->readable()) |
YSI | 1:27f6baabb15e | 162 | { |
YSI | 11:de94dcd67561 | 163 | char caractere; |
YSI | 11:de94dcd67561 | 164 | _serial->read(&caractere, 1); |
YSI | 11:de94dcd67561 | 165 | if((caractere == '\n') || (caractere == '\r')) |
YSI | 11:de94dcd67561 | 166 | { |
YSI | 11:de94dcd67561 | 167 | if(_processing != NULL) _processing(message.serial, SERIAL); |
YSI | 11:de94dcd67561 | 168 | message.serial.clear(); |
YSI | 11:de94dcd67561 | 169 | } |
YSI | 11:de94dcd67561 | 170 | else if((caractere > 31) && (caractere < 127)) message.serial += caractere; |
YSI | 1:27f6baabb15e | 171 | } |
YSI | 1:27f6baabb15e | 172 | } |
YSI | 1:27f6baabb15e | 173 | return message.status; |
YSI | 1:27f6baabb15e | 174 | } |
YSI | 1:27f6baabb15e | 175 | |
YSI | 11:de94dcd67561 | 176 | nsapi_error_t Transmission::send(const string& buff, const enum_trans_to& type) |
YSI | 1:27f6baabb15e | 177 | { |
YSI | 1:27f6baabb15e | 178 | nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK; |
YSI | 1:27f6baabb15e | 179 | string ssend(buff+"\n"); |
YSI | 11:de94dcd67561 | 180 | if((type != TCP) && (type != HTTP) && !buff.empty()) |
YSI | 11:de94dcd67561 | 181 | { |
YSI | 11:de94dcd67561 | 182 | if(_serial == NULL) return NSAPI_ERROR_NO_CONNECTION; |
YSI | 11:de94dcd67561 | 183 | ack = _serial->write(ssend.c_str(), ssend.length()); |
YSI | 11:de94dcd67561 | 184 | } |
YSI | 4:9a4ab4f406ab | 185 | if(type != SERIAL) |
YSI | 1:27f6baabb15e | 186 | { |
YSI | 11:de94dcd67561 | 187 | if(_eth == NULL) return NSAPI_ERROR_NO_CONNECTION; |
YSI | 5:4d87504e9257 | 188 | if(!message.BREAK && !buff.empty() && (message.status == BLUE_CLIENT)) |
YSI | 4:9a4ab4f406ab | 189 | eth_error("clientTCP_send", ack = _clientTCP->send(ssend.c_str(), ssend.size())); |
YSI | 10:25e049353db5 | 190 | if(message.BREAK || (type == HTTP)) |
YSI | 4:9a4ab4f406ab | 191 | { |
YSI | 10:25e049353db5 | 192 | message.BREAK = false; |
YSI | 4:9a4ab4f406ab | 193 | eth_error("clientTCP_disconnect", _clientTCP->close()); |
YSI | 4:9a4ab4f406ab | 194 | eth_state(); |
YSI | 4:9a4ab4f406ab | 195 | serverTCP_event(); |
YSI | 4:9a4ab4f406ab | 196 | } |
YSI | 1:27f6baabb15e | 197 | } |
YSI | 1:27f6baabb15e | 198 | return ack; |
YSI | 1:27f6baabb15e | 199 | } |
YSI | 1:27f6baabb15e | 200 | |
YSI | 1:27f6baabb15e | 201 | bool Transmission::smtp(const char* MAIL, const char* FROM, const char* SUBJECT, const char* DATA) |
YSI | 1:27f6baabb15e | 202 | { |
YSI | 11:de94dcd67561 | 203 | if(_eth == NULL) return false; |
YSI | 10:25e049353db5 | 204 | if((!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP)) return false; |
YSI | 1:27f6baabb15e | 205 | TCPSocket clientSMTP; |
YSI | 8:7193327bed34 | 206 | clientSMTP.set_timeout(REQUEST_TIMEOUT*20); |
YSI | 1:27f6baabb15e | 207 | const string sMAIL(MAIL), sFROM(FROM), sSUBJECT(SUBJECT), sDATA(DATA); |
YSI | 10:25e049353db5 | 208 | const 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 + "@U-PSUD.FR>\r\nTo: \"DESTINATAIRE\" <" + sMAIL + ">\r\nSubject:" + sSUBJECT + "\r\n" + sDATA + "\r\n.\r\n", "QUIT\r\n" }, |
YSI | 1:27f6baabb15e | 209 | { "", "HELO Mbed\r\n", "MAIL FROM: <Mbed>\r\n","RCPT TO: <" + sMAIL + ">\r\n", "QUIT\r\n" }}; |
YSI | 1:27f6baabb15e | 210 | string code; |
YSI | 1:27f6baabb15e | 211 | if(eth_error("clientSMTP_open", clientSMTP.open(_eth)) == NSAPI_ERROR_OK) |
YSI | 1:27f6baabb15e | 212 | { |
YSI | 1:27f6baabb15e | 213 | for(const string ssend : smtpParams[(sFROM.empty())?1:0]) |
YSI | 1:27f6baabb15e | 214 | { |
YSI | 1:27f6baabb15e | 215 | char buffer[256] = {0}; |
YSI | 1:27f6baabb15e | 216 | if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(SMTP_SERVER, 25))) < NSAPI_ERROR_OK) break; } |
YSI | 1:27f6baabb15e | 217 | else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break; |
YSI | 1:27f6baabb15e | 218 | if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 256)) < NSAPI_ERROR_OK) break; |
YSI | 1:27f6baabb15e | 219 | buffer[3] = 0; |
YSI | 1:27f6baabb15e | 220 | code += buffer; |
YSI | 1:27f6baabb15e | 221 | if(ssend == "QUIT\r\n") break; |
YSI | 1:27f6baabb15e | 222 | } |
YSI | 1:27f6baabb15e | 223 | eth_error("clientSMTP_close", clientSMTP.close()); |
YSI | 1:27f6baabb15e | 224 | } |
YSI | 1:27f6baabb15e | 225 | if(sFROM.empty()) return code == "220250250250221"; |
YSI | 8:7193327bed34 | 226 | else if(code != "220250250250354250221") _queue.call_in(60s, this, &Transmission::smtp, MAIL, FROM, SUBJECT, DATA); |
YSI | 1:27f6baabb15e | 227 | return code == "220250250250354250221"; |
YSI | 0:2fc6fc3b5e15 | 228 | } |
YSI | 0:2fc6fc3b5e15 | 229 | |
YSI | 9:abd4a4944399 | 230 | time_t Transmission::ntp(const char* ADDRESS) |
YSI | 6:d6a07fd1548a | 231 | { |
YSI | 11:de94dcd67561 | 232 | if(_eth == NULL) return 0; |
YSI | 10:25e049353db5 | 233 | if((!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP)) return 0; |
YSI | 6:d6a07fd1548a | 234 | time_t timeStamp = 0; |
YSI | 6:d6a07fd1548a | 235 | UDPSocket clientNTP; |
YSI | 8:7193327bed34 | 236 | clientNTP.set_timeout(REQUEST_TIMEOUT*20); |
YSI | 6:d6a07fd1548a | 237 | if(eth_error("clientNTP_open", clientNTP.open(_eth)) == NSAPI_ERROR_OK) |
YSI | 6:d6a07fd1548a | 238 | { |
YSI | 9:abd4a4944399 | 239 | string sADDRESS(ADDRESS); |
YSI | 6:d6a07fd1548a | 240 | uint32_t buffer[12] = { 0b11011, 0 }; // VN = 3 & Mode = 3 |
YSI | 9:abd4a4944399 | 241 | SocketAddress address(NTP_SERVER, 123); |
YSI | 9:abd4a4944399 | 242 | if(!sADDRESS.empty()) eth_error("eth_gethostbyname", _eth->gethostbyname(sADDRESS.c_str(), &address)); |
YSI | 9:abd4a4944399 | 243 | if(eth_error("clientNTP_send", clientNTP.sendto(address, (void*)buffer, sizeof(buffer))) > NSAPI_ERROR_OK) |
YSI | 6:d6a07fd1548a | 244 | { |
YSI | 6:d6a07fd1548a | 245 | if(eth_error("clientNTP_recv", clientNTP.recvfrom(NULL, (void*)buffer, sizeof(buffer))) > NSAPI_ERROR_OK) |
YSI | 6:d6a07fd1548a | 246 | { |
YSI | 6:d6a07fd1548a | 247 | timeStamp = ((buffer[10] & 0xFF) << 24) | ((buffer[10] & 0xFF00) << 8) | ((buffer[10] & 0xFF0000UL) >> 8) | ((buffer[10] & 0xFF000000UL) >> 24); |
YSI | 6:d6a07fd1548a | 248 | timeStamp -= 2208985200U; // 01/01/1970 Europe |
YSI | 6:d6a07fd1548a | 249 | struct tm * tmTimeStamp = localtime(&timeStamp); |
YSI | 10:25e049353db5 | 250 | 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 | 251 | timeStamp += 3600; // DST starts last Sunday of March; 2am (1am UTC), DST ends last Sunday of october; 3am (2am UTC) |
YSI | 6:d6a07fd1548a | 252 | } |
YSI | 6:d6a07fd1548a | 253 | } |
YSI | 6:d6a07fd1548a | 254 | eth_error("clientNTP_close", clientNTP.close()); |
YSI | 6:d6a07fd1548a | 255 | } |
YSI | 6:d6a07fd1548a | 256 | return timeStamp; |
YSI | 6:d6a07fd1548a | 257 | } |
YSI | 6:d6a07fd1548a | 258 | |
YSI | 0:2fc6fc3b5e15 | 259 | intptr_t Transmission::eth_status(const string& source, const intptr_t& code) |
YSI | 0:2fc6fc3b5e15 | 260 | { |
YSI | 0:2fc6fc3b5e15 | 261 | stringstream message; |
YSI | 0:2fc6fc3b5e15 | 262 | message << "\n" << source << "[" << code; |
YSI | 0:2fc6fc3b5e15 | 263 | switch(code) |
YSI | 0:2fc6fc3b5e15 | 264 | { |
YSI | 0:2fc6fc3b5e15 | 265 | case NSAPI_STATUS_LOCAL_UP: message << "] NSAPI_STATUS_LOCAL_UP < local IP address set >"; break; |
YSI | 0:2fc6fc3b5e15 | 266 | case NSAPI_STATUS_GLOBAL_UP: message << "] NSAPI_STATUS_GLOBAL_UP < global IP address set >"; break; |
YSI | 0:2fc6fc3b5e15 | 267 | case NSAPI_STATUS_DISCONNECTED: message << "] NSAPI_STATUS_DISCONNECTED < no connection to network >"; break; |
YSI | 0:2fc6fc3b5e15 | 268 | case NSAPI_STATUS_CONNECTING: message << "] NSAPI_STATUS_CONNECTING < connecting to network >"; break; |
YSI | 0:2fc6fc3b5e15 | 269 | case NSAPI_STATUS_ERROR_UNSUPPORTED: message << "] NSAPI_STATUS_ERROR_UNSUPPORTED < unsupported functionality >";break; |
YSI | 0:2fc6fc3b5e15 | 270 | } |
YSI | 1:27f6baabb15e | 271 | #ifndef NDEBUG |
YSI | 1:27f6baabb15e | 272 | _serial->write(message.str().c_str(), message.str().size()); |
YSI | 1:27f6baabb15e | 273 | #endif |
YSI | 0:2fc6fc3b5e15 | 274 | return code; |
YSI | 0:2fc6fc3b5e15 | 275 | } |
YSI | 0:2fc6fc3b5e15 | 276 | |
YSI | 0:2fc6fc3b5e15 | 277 | nsapi_error_t Transmission::eth_error(const string& source, const nsapi_error_t& code) |
YSI | 0:2fc6fc3b5e15 | 278 | { |
YSI | 0:2fc6fc3b5e15 | 279 | stringstream message; |
YSI | 0:2fc6fc3b5e15 | 280 | message << "\n" << source << "[" << code; |
YSI | 0:2fc6fc3b5e15 | 281 | switch(code) |
YSI | 0:2fc6fc3b5e15 | 282 | { |
YSI | 0:2fc6fc3b5e15 | 283 | case NSAPI_ERROR_OK: message << "] NSAPI_ERROR_OK < no error >"; break; |
YSI | 0:2fc6fc3b5e15 | 284 | case NSAPI_ERROR_WOULD_BLOCK: message << "] NSAPI_ERROR_WOULD_BLOCK < no data is not available but call is non-blocking >";break; |
YSI | 0:2fc6fc3b5e15 | 285 | case NSAPI_ERROR_UNSUPPORTED: message << "] NSAPI_ERROR_UNSUPPORTED < unsupported functionality >"; break; |
YSI | 0:2fc6fc3b5e15 | 286 | case NSAPI_ERROR_PARAMETER: message << "] NSAPI_ERROR_PARAMETER < invalid configuration >"; break; |
YSI | 0:2fc6fc3b5e15 | 287 | case NSAPI_ERROR_NO_CONNECTION: message << "] NSAPI_ERROR_NO_CONNECTION < not connected to a network >"; break; |
YSI | 0:2fc6fc3b5e15 | 288 | case NSAPI_ERROR_NO_SOCKET: message << "] NSAPI_ERROR_NO_SOCKET < socket not available for use >"; break; |
YSI | 0:2fc6fc3b5e15 | 289 | case NSAPI_ERROR_NO_ADDRESS: message << "] NSAPI_ERROR_NO_ADDRESS < IP address is not known >"; break; |
YSI | 0:2fc6fc3b5e15 | 290 | case NSAPI_ERROR_NO_MEMORY: message << "] NSAPI_ERROR_NO_MEMORY < memory resource not available >"; break; |
YSI | 0:2fc6fc3b5e15 | 291 | case NSAPI_ERROR_NO_SSID: message << "] NSAPI_ERROR_NO_SSID < ssid not found >"; break; |
YSI | 0:2fc6fc3b5e15 | 292 | case NSAPI_ERROR_DNS_FAILURE: message << "] NSAPI_ERROR_DNS_FAILURE < DNS failed to complete successfully >"; break; |
YSI | 0:2fc6fc3b5e15 | 293 | case NSAPI_ERROR_DHCP_FAILURE: message << "] NSAPI_ERROR_DHCP_FAILURE < DHCP failed to complete successfully >"; break; |
YSI | 0:2fc6fc3b5e15 | 294 | case NSAPI_ERROR_AUTH_FAILURE: message << "] NSAPI_ERROR_AUTH_FAILURE < connection to access point failed >"; break; |
YSI | 0:2fc6fc3b5e15 | 295 | case NSAPI_ERROR_DEVICE_ERROR: message << "] NSAPI_ERROR_DEVICE_ERROR < failure interfacing with the network processor >"; break; |
YSI | 0:2fc6fc3b5e15 | 296 | case NSAPI_ERROR_IN_PROGRESS: message << "] NSAPI_ERROR_IN_PROGRESS < operation (eg connect) in progress >"; break; |
YSI | 0:2fc6fc3b5e15 | 297 | case NSAPI_ERROR_ALREADY: message << "] NSAPI_ERROR_ALREADY < operation (eg connect) already in progress >"; break; |
YSI | 0:2fc6fc3b5e15 | 298 | case NSAPI_ERROR_IS_CONNECTED: message << "] NSAPI_ERROR_IS_CONNECTED < socket is already connected >"; break; |
YSI | 0:2fc6fc3b5e15 | 299 | case NSAPI_ERROR_CONNECTION_LOST: message << "] NSAPI_ERROR_CONNECTION_LOST < connection lost >"; break; |
YSI | 0:2fc6fc3b5e15 | 300 | case NSAPI_ERROR_CONNECTION_TIMEOUT: message << "] NSAPI_ERROR_CONNECTION_TIMEOUT < connection timed out >"; break; |
YSI | 0:2fc6fc3b5e15 | 301 | case NSAPI_ERROR_ADDRESS_IN_USE: message << "] NSAPI_ERROR_ADDRESS_IN_USE < Address already in use >"; break; |
YSI | 0:2fc6fc3b5e15 | 302 | case NSAPI_ERROR_TIMEOUT: message << "] NSAPI_ERROR_TIMEOUT < operation timed out >"; break; |
YSI | 0:2fc6fc3b5e15 | 303 | case NSAPI_ERROR_BUSY: message << "] NSAPI_ERROR_BUSY < device is busy and cannot accept new operation >"; break; |
YSI | 0:2fc6fc3b5e15 | 304 | default: message << "] NSAPI_ERROR < unknow code >"; break; |
YSI | 0:2fc6fc3b5e15 | 305 | } |
YSI | 1:27f6baabb15e | 306 | #ifndef NDEBUG |
YSI | 1:27f6baabb15e | 307 | if(code < NSAPI_ERROR_OK) _serial->write(message.str().c_str(), message.str().size()); |
YSI | 1:27f6baabb15e | 308 | #endif |
YSI | 0:2fc6fc3b5e15 | 309 | return code; |
YSI | 0:2fc6fc3b5e15 | 310 | } |