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