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@3:7e15bf0a71f4, 2020-07-08 (annotated)
- Committer:
- YSI
- Date:
- Wed Jul 08 12:26:35 2020 +0000
- Revision:
- 3:7e15bf0a71f4
- Parent:
- 2:ec88f3f8b619
- Child:
- 4:9a4ab4f406ab
add get & set ip:port conf
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 | 1:27f6baabb15e | 3 | Transmission::Transmission(UnbufferedSerial *serial, EthernetInterface *eth, EventQueue *queue, void(*_init)(void), void(*_processing)(string, const enumTRANSMISSION&)) |
YSI | 0:2fc6fc3b5e15 | 4 | { |
YSI | 0:2fc6fc3b5e15 | 5 | _serial = serial; |
YSI | 0:2fc6fc3b5e15 | 6 | _eth = eth; |
YSI | 0:2fc6fc3b5e15 | 7 | _queue = queue; |
YSI | 1:27f6baabb15e | 8 | fn_init = _init; |
YSI | 1:27f6baabb15e | 9 | fn_processing = _processing; |
YSI | 0:2fc6fc3b5e15 | 10 | } |
YSI | 0:2fc6fc3b5e15 | 11 | |
YSI | 3:7e15bf0a71f4 | 12 | void Transmission::set(bool TCP, const char* IP, uint16_t PORT) |
YSI | 3:7e15bf0a71f4 | 13 | { |
YSI | 3:7e15bf0a71f4 | 14 | if(message.TCP && TCP) |
YSI | 3:7e15bf0a71f4 | 15 | { |
YSI | 3:7e15bf0a71f4 | 16 | if(message.PORT != PORT) |
YSI | 3:7e15bf0a71f4 | 17 | { |
YSI | 3:7e15bf0a71f4 | 18 | message.CONNECT = false; |
YSI | 3:7e15bf0a71f4 | 19 | _serverTCP.sigio(NULL); |
YSI | 3:7e15bf0a71f4 | 20 | eth_error("serverTCP_close", _serverTCP.close()); |
YSI | 3:7e15bf0a71f4 | 21 | } |
YSI | 3:7e15bf0a71f4 | 22 | eth_error("Ethernet_disconnect", _eth->disconnect()); |
YSI | 3:7e15bf0a71f4 | 23 | } |
YSI | 3:7e15bf0a71f4 | 24 | message.TCP = TCP; |
YSI | 3:7e15bf0a71f4 | 25 | message.IP = IP; |
YSI | 3:7e15bf0a71f4 | 26 | message.PORT = PORT; |
YSI | 3:7e15bf0a71f4 | 27 | message.DHCP = message.IP.empty(); |
YSI | 3:7e15bf0a71f4 | 28 | } |
YSI | 3:7e15bf0a71f4 | 29 | |
YSI | 3:7e15bf0a71f4 | 30 | string Transmission::get(void) |
YSI | 3:7e15bf0a71f4 | 31 | { |
YSI | 3:7e15bf0a71f4 | 32 | SocketAddress ip; |
YSI | 3:7e15bf0a71f4 | 33 | _eth->get_ip_address(&ip); |
YSI | 3:7e15bf0a71f4 | 34 | string address(ip.get_ip_address()?ip.get_ip_address():"0.0.0.0"); |
YSI | 3:7e15bf0a71f4 | 35 | address += ":" + to_string(message.PORT); |
YSI | 3:7e15bf0a71f4 | 36 | return address; |
YSI | 3:7e15bf0a71f4 | 37 | } |
YSI | 3:7e15bf0a71f4 | 38 | |
YSI | 0:2fc6fc3b5e15 | 39 | bool Transmission::eth_connect(void) |
YSI | 0:2fc6fc3b5e15 | 40 | { |
YSI | 0:2fc6fc3b5e15 | 41 | if(message.TCP) |
YSI | 0:2fc6fc3b5e15 | 42 | { |
YSI | 0:2fc6fc3b5e15 | 43 | switch(_eth->get_connection_status()) |
YSI | 0:2fc6fc3b5e15 | 44 | { |
YSI | 0:2fc6fc3b5e15 | 45 | case NSAPI_STATUS_DISCONNECTED: |
YSI | 1:27f6baabb15e | 46 | if(message.status == RED_DISCONNECTED) |
YSI | 0:2fc6fc3b5e15 | 47 | { |
YSI | 0:2fc6fc3b5e15 | 48 | eth_error("Ethernet_blocking", _eth->set_blocking(false)); |
YSI | 3:7e15bf0a71f4 | 49 | eth_error("Ethernet_dhcp", _eth->set_dhcp(message.DHCP)); |
YSI | 3:7e15bf0a71f4 | 50 | 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 | 51 | eth_error("Ethernet_connect", _eth->connect()); |
YSI | 0:2fc6fc3b5e15 | 52 | } |
YSI | 0:2fc6fc3b5e15 | 53 | break; |
YSI | 0:2fc6fc3b5e15 | 54 | case NSAPI_STATUS_CONNECTING: |
YSI | 1:27f6baabb15e | 55 | if(message.status == RED_DISCONNECTED) |
YSI | 0:2fc6fc3b5e15 | 56 | { |
YSI | 0:2fc6fc3b5e15 | 57 | eth_status("Ethernet_connect", NSAPI_STATUS_CONNECTING); |
YSI | 1:27f6baabb15e | 58 | message.status = YELLOW_CONNECTING; |
YSI | 0:2fc6fc3b5e15 | 59 | _eth->attach(callback(this, &Transmission::eth_event)); |
YSI | 0:2fc6fc3b5e15 | 60 | } |
YSI | 0:2fc6fc3b5e15 | 61 | break; |
YSI | 3:7e15bf0a71f4 | 62 | case NSAPI_STATUS_GLOBAL_UP: return message.CONNECT; break; |
YSI | 0:2fc6fc3b5e15 | 63 | default: break; |
YSI | 0:2fc6fc3b5e15 | 64 | } |
YSI | 0:2fc6fc3b5e15 | 65 | } |
YSI | 0:2fc6fc3b5e15 | 66 | else if(_eth->get_connection_status() != NSAPI_STATUS_DISCONNECTED) eth_error("Ethernet_disconnect", _eth->disconnect()); |
YSI | 0:2fc6fc3b5e15 | 67 | return false; |
YSI | 0:2fc6fc3b5e15 | 68 | } |
YSI | 0:2fc6fc3b5e15 | 69 | |
YSI | 0:2fc6fc3b5e15 | 70 | void Transmission::eth_event(nsapi_event_t status, intptr_t param) |
YSI | 0:2fc6fc3b5e15 | 71 | { |
YSI | 0:2fc6fc3b5e15 | 72 | eth_status("Ethernet_event", param); |
YSI | 0:2fc6fc3b5e15 | 73 | switch(param) |
YSI | 0:2fc6fc3b5e15 | 74 | { |
YSI | 1:27f6baabb15e | 75 | case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break; |
YSI | 1:27f6baabb15e | 76 | case NSAPI_STATUS_CONNECTING: |
YSI | 3:7e15bf0a71f4 | 77 | if(message.status == BLUE_CLIENT) eth_error("clientTCP_disconnect", _clientTCP->close()); |
YSI | 1:27f6baabb15e | 78 | message.status = YELLOW_CONNECTING; break; |
YSI | 3:7e15bf0a71f4 | 79 | case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP; |
YSI | 3:7e15bf0a71f4 | 80 | if(message.CONNECT) |
YSI | 3:7e15bf0a71f4 | 81 | serverTCP_event(); |
YSI | 3:7e15bf0a71f4 | 82 | else serverTCP_connect(); break; |
YSI | 1:27f6baabb15e | 83 | default: break; |
YSI | 1:27f6baabb15e | 84 | } |
YSI | 1:27f6baabb15e | 85 | } |
YSI | 1:27f6baabb15e | 86 | |
YSI | 1:27f6baabb15e | 87 | bool Transmission::serverTCP_connect(void) |
YSI | 1:27f6baabb15e | 88 | { |
YSI | 3:7e15bf0a71f4 | 89 | if(!message.CONNECT) |
YSI | 3:7e15bf0a71f4 | 90 | if(eth_error("serverTCP_open", _serverTCP.open(_eth)) == NSAPI_ERROR_OK) |
YSI | 3:7e15bf0a71f4 | 91 | if(eth_error("serverTCP_bind", _serverTCP.bind(message.PORT)) == NSAPI_ERROR_OK) |
YSI | 3:7e15bf0a71f4 | 92 | if(eth_error("serverTCP_listen", _serverTCP.listen()) == NSAPI_ERROR_OK) |
YSI | 1:27f6baabb15e | 93 | { |
YSI | 3:7e15bf0a71f4 | 94 | _PORT = message.PORT; |
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 | 3:7e15bf0a71f4 | 98 | _queue->call(fn_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 | 1:27f6baabb15e | 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 | 3:7e15bf0a71f4 | 118 | _clientTCP->set_timeout(TCP_CLIENT_TIMEOUT); |
YSI | 1:27f6baabb15e | 119 | message.status = BLUE_CLIENT; |
YSI | 1:27f6baabb15e | 120 | break; |
YSI | 1:27f6baabb15e | 121 | case NSAPI_ERROR_PARAMETER: |
YSI | 1:27f6baabb15e | 122 | case NSAPI_ERROR_WOULD_BLOCK: |
YSI | 1:27f6baabb15e | 123 | message.status = GREEN_GLOBAL_UP; |
YSI | 1:27f6baabb15e | 124 | break; |
YSI | 1:27f6baabb15e | 125 | case NSAPI_ERROR_NO_CONNECTION: |
YSI | 1:27f6baabb15e | 126 | message.status = GREEN_GLOBAL_UP; |
YSI | 3:7e15bf0a71f4 | 127 | serverTCP_event(); |
YSI | 1:27f6baabb15e | 128 | break; |
YSI | 1:27f6baabb15e | 129 | default: |
YSI | 1:27f6baabb15e | 130 | message.status = GREEN_GLOBAL_UP; |
YSI | 1:27f6baabb15e | 131 | eth_error("serverTCP_accept", ack); |
YSI | 1:27f6baabb15e | 132 | break; |
YSI | 1:27f6baabb15e | 133 | } |
YSI | 1:27f6baabb15e | 134 | } |
YSI | 1:27f6baabb15e | 135 | } |
YSI | 1:27f6baabb15e | 136 | |
YSI | 2:ec88f3f8b619 | 137 | enumTRANSTATUS Transmission::recv(void) |
YSI | 1:27f6baabb15e | 138 | { |
YSI | 1:27f6baabb15e | 139 | if(eth_connect()) |
YSI | 1:27f6baabb15e | 140 | { |
YSI | 1:27f6baabb15e | 141 | char buffer[1024] = {0}; |
YSI | 1:27f6baabb15e | 142 | nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK; |
YSI | 1:27f6baabb15e | 143 | if(message.status == BLUE_CLIENT) |
YSI | 3:7e15bf0a71f4 | 144 | if((ack = _clientTCP->recv(buffer, 1024)) < NSAPI_ERROR_WOULD_BLOCK) |
YSI | 1:27f6baabb15e | 145 | eth_error("clientTCP_recv", ack); |
YSI | 1:27f6baabb15e | 146 | switch(ack) |
YSI | 1:27f6baabb15e | 147 | { |
YSI | 1:27f6baabb15e | 148 | case NSAPI_ERROR_NO_CONNECTION: |
YSI | 1:27f6baabb15e | 149 | case NSAPI_ERROR_OK: |
YSI | 1:27f6baabb15e | 150 | message.BREAK = true; break; |
YSI | 1:27f6baabb15e | 151 | default: break; |
YSI | 1:27f6baabb15e | 152 | } |
YSI | 1:27f6baabb15e | 153 | for(int i = 0; i < ack; i++) if(buffer[i] == '\n') buffer[i] = ';'; |
YSI | 1:27f6baabb15e | 154 | fn_processing(message.buffer[TCP] = buffer, TCP); |
YSI | 1:27f6baabb15e | 155 | message.buffer[TCP].clear(); |
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 | 1:27f6baabb15e | 161 | switch(caractere) |
YSI | 1:27f6baabb15e | 162 | { |
YSI | 1:27f6baabb15e | 163 | case '\n': |
YSI | 1:27f6baabb15e | 164 | fn_processing(message.buffer[SERIAL], SERIAL); |
YSI | 1:27f6baabb15e | 165 | message.buffer[SERIAL].clear(); |
YSI | 1:27f6baabb15e | 166 | break; |
YSI | 1:27f6baabb15e | 167 | default: |
YSI | 1:27f6baabb15e | 168 | if((caractere > 31) && (caractere < 127)) message.buffer[SERIAL] += caractere; |
YSI | 1:27f6baabb15e | 169 | break; |
YSI | 1:27f6baabb15e | 170 | } |
YSI | 1:27f6baabb15e | 171 | } |
YSI | 1:27f6baabb15e | 172 | return message.status; |
YSI | 1:27f6baabb15e | 173 | } |
YSI | 1:27f6baabb15e | 174 | |
YSI | 1:27f6baabb15e | 175 | nsapi_error_t Transmission::send(const string& buff, const enumTRANSMISSION& type) |
YSI | 1:27f6baabb15e | 176 | { |
YSI | 1:27f6baabb15e | 177 | nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK; |
YSI | 1:27f6baabb15e | 178 | string ssend(buff+"\n"); |
YSI | 1:27f6baabb15e | 179 | switch(type) |
YSI | 1:27f6baabb15e | 180 | { |
YSI | 1:27f6baabb15e | 181 | case TCP: |
YSI | 1:27f6baabb15e | 182 | if(message.BREAK) |
YSI | 1:27f6baabb15e | 183 | { |
YSI | 1:27f6baabb15e | 184 | message.BREAK = false; |
YSI | 3:7e15bf0a71f4 | 185 | eth_error("clientTCP_disconnect", _clientTCP->close()); |
YSI | 1:27f6baabb15e | 186 | switch(_eth->get_connection_status()) |
YSI | 1:27f6baabb15e | 187 | { |
YSI | 1:27f6baabb15e | 188 | case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break; |
YSI | 1:27f6baabb15e | 189 | case NSAPI_STATUS_CONNECTING: message.status = YELLOW_CONNECTING; break; |
YSI | 1:27f6baabb15e | 190 | case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP; break; |
YSI | 1:27f6baabb15e | 191 | default: break; |
YSI | 1:27f6baabb15e | 192 | } |
YSI | 3:7e15bf0a71f4 | 193 | serverTCP_event(); |
YSI | 1:27f6baabb15e | 194 | } |
YSI | 1:27f6baabb15e | 195 | else if(!buff.empty()) |
YSI | 1:27f6baabb15e | 196 | { |
YSI | 3:7e15bf0a71f4 | 197 | eth_error("clientTCP_send", ack = _clientTCP->send(ssend.c_str(), ssend.size())); |
YSI | 1:27f6baabb15e | 198 | if(message.HTTP) |
YSI | 1:27f6baabb15e | 199 | { |
YSI | 1:27f6baabb15e | 200 | message.HTTP = false; |
YSI | 3:7e15bf0a71f4 | 201 | eth_error("clientTCP_disconnect", _clientTCP->close()); |
YSI | 1:27f6baabb15e | 202 | switch(_eth->get_connection_status()) |
YSI | 1:27f6baabb15e | 203 | { |
YSI | 3:7e15bf0a71f4 | 204 | case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break; |
YSI | 3:7e15bf0a71f4 | 205 | case NSAPI_STATUS_CONNECTING: message.status = YELLOW_CONNECTING; break; |
YSI | 3:7e15bf0a71f4 | 206 | case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP; break; |
YSI | 3:7e15bf0a71f4 | 207 | default: break; |
YSI | 1:27f6baabb15e | 208 | } |
YSI | 3:7e15bf0a71f4 | 209 | serverTCP_event(); |
YSI | 1:27f6baabb15e | 210 | } |
YSI | 1:27f6baabb15e | 211 | } |
YSI | 1:27f6baabb15e | 212 | break; |
YSI | 1:27f6baabb15e | 213 | case SERIAL: |
YSI | 1:27f6baabb15e | 214 | if(!buff.empty()) ack = _serial->write(ssend.c_str(), ssend.length()); |
YSI | 1:27f6baabb15e | 215 | break; |
YSI | 1:27f6baabb15e | 216 | } |
YSI | 1:27f6baabb15e | 217 | return ack; |
YSI | 1:27f6baabb15e | 218 | } |
YSI | 1:27f6baabb15e | 219 | |
YSI | 1:27f6baabb15e | 220 | bool Transmission::smtp(const char* MAIL, const char* FROM, const char* SUBJECT, const char* DATA) |
YSI | 1:27f6baabb15e | 221 | { |
YSI | 3:7e15bf0a71f4 | 222 | if(!message.DHCP) return false; |
YSI | 1:27f6baabb15e | 223 | TCPSocket clientSMTP; |
YSI | 1:27f6baabb15e | 224 | clientSMTP.set_timeout(5000); |
YSI | 1:27f6baabb15e | 225 | const string sMAIL(MAIL), sFROM(FROM), sSUBJECT(SUBJECT), sDATA(DATA); |
YSI | 1:27f6baabb15e | 226 | const string smtpParams[][7] = {{ "", "HELO Mbed " + sFROM + "\r\n", "MAIL FROM: <Mbed." + sFROM + "@U-PSUD.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 | 227 | { "", "HELO Mbed\r\n", "MAIL FROM: <Mbed>\r\n","RCPT TO: <" + sMAIL + ">\r\n", "QUIT\r\n" }}; |
YSI | 1:27f6baabb15e | 228 | string code; |
YSI | 1:27f6baabb15e | 229 | if(eth_error("clientSMTP_open", clientSMTP.open(_eth)) == NSAPI_ERROR_OK) |
YSI | 1:27f6baabb15e | 230 | { |
YSI | 1:27f6baabb15e | 231 | for(const string ssend : smtpParams[(sFROM.empty())?1:0]) |
YSI | 1:27f6baabb15e | 232 | { |
YSI | 1:27f6baabb15e | 233 | char buffer[256] = {0}; |
YSI | 1:27f6baabb15e | 234 | if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(SMTP_SERVER, 25))) < NSAPI_ERROR_OK) break; } |
YSI | 1:27f6baabb15e | 235 | else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break; |
YSI | 1:27f6baabb15e | 236 | if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 256)) < NSAPI_ERROR_OK) break; |
YSI | 1:27f6baabb15e | 237 | buffer[3] = 0; |
YSI | 1:27f6baabb15e | 238 | code += buffer; |
YSI | 1:27f6baabb15e | 239 | if(ssend == "QUIT\r\n") break; |
YSI | 1:27f6baabb15e | 240 | } |
YSI | 1:27f6baabb15e | 241 | eth_error("clientSMTP_close", clientSMTP.close()); |
YSI | 1:27f6baabb15e | 242 | } |
YSI | 1:27f6baabb15e | 243 | if(sFROM.empty()) return code == "220250250250221"; |
YSI | 1:27f6baabb15e | 244 | else if(code != "220250250250354250221") _queue->call_in(60s, this, &Transmission::smtp, MAIL, FROM, SUBJECT, DATA); |
YSI | 1:27f6baabb15e | 245 | return code == "220250250250354250221"; |
YSI | 0:2fc6fc3b5e15 | 246 | } |
YSI | 0:2fc6fc3b5e15 | 247 | |
YSI | 3:7e15bf0a71f4 | 248 | void Transmission::http(void) |
YSI | 3:7e15bf0a71f4 | 249 | { |
YSI | 3:7e15bf0a71f4 | 250 | message.HTTP = true; |
YSI | 3:7e15bf0a71f4 | 251 | } |
YSI | 3:7e15bf0a71f4 | 252 | |
YSI | 0:2fc6fc3b5e15 | 253 | intptr_t Transmission::eth_status(const string& source, const intptr_t& code) |
YSI | 0:2fc6fc3b5e15 | 254 | { |
YSI | 0:2fc6fc3b5e15 | 255 | stringstream message; |
YSI | 0:2fc6fc3b5e15 | 256 | message << "\n" << source << "[" << code; |
YSI | 0:2fc6fc3b5e15 | 257 | switch(code) |
YSI | 0:2fc6fc3b5e15 | 258 | { |
YSI | 0:2fc6fc3b5e15 | 259 | case NSAPI_STATUS_LOCAL_UP: message << "] NSAPI_STATUS_LOCAL_UP < local IP address set >"; break; |
YSI | 0:2fc6fc3b5e15 | 260 | case NSAPI_STATUS_GLOBAL_UP: message << "] NSAPI_STATUS_GLOBAL_UP < global IP address set >"; break; |
YSI | 0:2fc6fc3b5e15 | 261 | case NSAPI_STATUS_DISCONNECTED: message << "] NSAPI_STATUS_DISCONNECTED < no connection to network >"; break; |
YSI | 0:2fc6fc3b5e15 | 262 | case NSAPI_STATUS_CONNECTING: message << "] NSAPI_STATUS_CONNECTING < connecting to network >"; break; |
YSI | 0:2fc6fc3b5e15 | 263 | case NSAPI_STATUS_ERROR_UNSUPPORTED: message << "] NSAPI_STATUS_ERROR_UNSUPPORTED < unsupported functionality >";break; |
YSI | 0:2fc6fc3b5e15 | 264 | } |
YSI | 1:27f6baabb15e | 265 | #ifndef NDEBUG |
YSI | 1:27f6baabb15e | 266 | _serial->write(message.str().c_str(), message.str().size()); |
YSI | 1:27f6baabb15e | 267 | #endif |
YSI | 0:2fc6fc3b5e15 | 268 | return code; |
YSI | 0:2fc6fc3b5e15 | 269 | } |
YSI | 0:2fc6fc3b5e15 | 270 | |
YSI | 0:2fc6fc3b5e15 | 271 | nsapi_error_t Transmission::eth_error(const string& source, const nsapi_error_t& code) |
YSI | 0:2fc6fc3b5e15 | 272 | { |
YSI | 0:2fc6fc3b5e15 | 273 | stringstream message; |
YSI | 0:2fc6fc3b5e15 | 274 | message << "\n" << source << "[" << code; |
YSI | 0:2fc6fc3b5e15 | 275 | switch(code) |
YSI | 0:2fc6fc3b5e15 | 276 | { |
YSI | 0:2fc6fc3b5e15 | 277 | case NSAPI_ERROR_OK: message << "] NSAPI_ERROR_OK < no error >"; break; |
YSI | 0:2fc6fc3b5e15 | 278 | case NSAPI_ERROR_WOULD_BLOCK: message << "] NSAPI_ERROR_WOULD_BLOCK < no data is not available but call is non-blocking >";break; |
YSI | 0:2fc6fc3b5e15 | 279 | case NSAPI_ERROR_UNSUPPORTED: message << "] NSAPI_ERROR_UNSUPPORTED < unsupported functionality >"; break; |
YSI | 0:2fc6fc3b5e15 | 280 | case NSAPI_ERROR_PARAMETER: message << "] NSAPI_ERROR_PARAMETER < invalid configuration >"; break; |
YSI | 0:2fc6fc3b5e15 | 281 | case NSAPI_ERROR_NO_CONNECTION: message << "] NSAPI_ERROR_NO_CONNECTION < not connected to a network >"; break; |
YSI | 0:2fc6fc3b5e15 | 282 | case NSAPI_ERROR_NO_SOCKET: message << "] NSAPI_ERROR_NO_SOCKET < socket not available for use >"; break; |
YSI | 0:2fc6fc3b5e15 | 283 | case NSAPI_ERROR_NO_ADDRESS: message << "] NSAPI_ERROR_NO_ADDRESS < IP address is not known >"; break; |
YSI | 0:2fc6fc3b5e15 | 284 | case NSAPI_ERROR_NO_MEMORY: message << "] NSAPI_ERROR_NO_MEMORY < memory resource not available >"; break; |
YSI | 0:2fc6fc3b5e15 | 285 | case NSAPI_ERROR_NO_SSID: message << "] NSAPI_ERROR_NO_SSID < ssid not found >"; break; |
YSI | 0:2fc6fc3b5e15 | 286 | case NSAPI_ERROR_DNS_FAILURE: message << "] NSAPI_ERROR_DNS_FAILURE < DNS failed to complete successfully >"; break; |
YSI | 0:2fc6fc3b5e15 | 287 | case NSAPI_ERROR_DHCP_FAILURE: message << "] NSAPI_ERROR_DHCP_FAILURE < DHCP failed to complete successfully >"; break; |
YSI | 0:2fc6fc3b5e15 | 288 | case NSAPI_ERROR_AUTH_FAILURE: message << "] NSAPI_ERROR_AUTH_FAILURE < connection to access point failed >"; break; |
YSI | 0:2fc6fc3b5e15 | 289 | case NSAPI_ERROR_DEVICE_ERROR: message << "] NSAPI_ERROR_DEVICE_ERROR < failure interfacing with the network processor >"; break; |
YSI | 0:2fc6fc3b5e15 | 290 | case NSAPI_ERROR_IN_PROGRESS: message << "] NSAPI_ERROR_IN_PROGRESS < operation (eg connect) in progress >"; break; |
YSI | 0:2fc6fc3b5e15 | 291 | case NSAPI_ERROR_ALREADY: message << "] NSAPI_ERROR_ALREADY < operation (eg connect) already in progress >"; break; |
YSI | 0:2fc6fc3b5e15 | 292 | case NSAPI_ERROR_IS_CONNECTED: message << "] NSAPI_ERROR_IS_CONNECTED < socket is already connected >"; break; |
YSI | 0:2fc6fc3b5e15 | 293 | case NSAPI_ERROR_CONNECTION_LOST: message << "] NSAPI_ERROR_CONNECTION_LOST < connection lost >"; break; |
YSI | 0:2fc6fc3b5e15 | 294 | case NSAPI_ERROR_CONNECTION_TIMEOUT: message << "] NSAPI_ERROR_CONNECTION_TIMEOUT < connection timed out >"; break; |
YSI | 0:2fc6fc3b5e15 | 295 | case NSAPI_ERROR_ADDRESS_IN_USE: message << "] NSAPI_ERROR_ADDRESS_IN_USE < Address already in use >"; break; |
YSI | 0:2fc6fc3b5e15 | 296 | case NSAPI_ERROR_TIMEOUT: message << "] NSAPI_ERROR_TIMEOUT < operation timed out >"; break; |
YSI | 0:2fc6fc3b5e15 | 297 | case NSAPI_ERROR_BUSY: message << "] NSAPI_ERROR_BUSY < device is busy and cannot accept new operation >"; break; |
YSI | 0:2fc6fc3b5e15 | 298 | default: message << "] NSAPI_ERROR < unknow code >"; break; |
YSI | 0:2fc6fc3b5e15 | 299 | } |
YSI | 1:27f6baabb15e | 300 | #ifndef NDEBUG |
YSI | 1:27f6baabb15e | 301 | if(code < NSAPI_ERROR_OK) _serial->write(message.str().c_str(), message.str().size()); |
YSI | 1:27f6baabb15e | 302 | #endif |
YSI | 0:2fc6fc3b5e15 | 303 | return code; |
YSI | 0:2fc6fc3b5e15 | 304 | } |