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