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:
Fri Dec 11 09:12:40 2020 +0000
Revision:
12:e22ff63d237c
Parent:
11:de94dcd67561
Child:
13:1e13e40b03c9
add retrocompatibility with mbed-os-5

Who changed what in which revision?

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