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:
Tue Jun 29 13:01:17 2021 +0000
Revision:
22:e99892e6fd8d
Parent:
21:59c0adcdfe9b
Child:
23:457f1242007d
add requeste to specific ip and port with get function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:2fc6fc3b5e15 1 #include "lib_Transmission.h"
YSI 18:15778c8a97a1 2
YSI 18:15778c8a97a1 3 Transmission::Transmission(
YSI 18:15778c8a97a1 4 #if MBED_MAJOR_VERSION > 5
YSI 18:15778c8a97a1 5 UnbufferedSerial *serial,
YSI 18:15778c8a97a1 6 #else
YSI 18:15778c8a97a1 7 Serial *serial,
YSI 18:15778c8a97a1 8 #endif
YSI 18:15778c8a97a1 9 USBCDC *usb,
YSI 18:15778c8a97a1 10 EthernetInterface *eth,
YSI 18:15778c8a97a1 11 string (*processing)(string),
YSI 18:15778c8a97a1 12 void (*ethup)(void),
YSI 18:15778c8a97a1 13 bool caseIgnore)
YSI 18:15778c8a97a1 14 :_serial(serial), _usb(usb), _eth(eth), _processing(processing), _ethup(ethup), _caseIgnore(caseIgnore)
YSI 18:15778c8a97a1 15 {
YSI 18:15778c8a97a1 16 if(_serial) _serial->attach(callback(this, &Transmission::serial_event));
YSI 22:e99892e6fd8d 17 _evenThread = new Thread(osPriorityNormal, TRANSMISSION_DEFAULT_THREAD_SIZE);
YSI 22:e99892e6fd8d 18 _evenThread->start(callback(&_queue, &EventQueue::dispatch_forever));
YSI 18:15778c8a97a1 19 }
YSI 18:15778c8a97a1 20
YSI 18:15778c8a97a1 21 Transmission::Transmission(
YSI 18:15778c8a97a1 22 #if MBED_MAJOR_VERSION > 5
YSI 18:15778c8a97a1 23 UnbufferedSerial *serial,
YSI 18:15778c8a97a1 24 #else
YSI 18:15778c8a97a1 25 Serial *serial,
YSI 18:15778c8a97a1 26 #endif
YSI 18:15778c8a97a1 27 USBCDC *usb,
YSI 18:15778c8a97a1 28 string (*processing)(string),
YSI 18:15778c8a97a1 29 bool caseIgnore)
YSI 18:15778c8a97a1 30 :_serial(serial), _usb(usb), _processing(processing), _caseIgnore(caseIgnore)
YSI 18:15778c8a97a1 31 {
YSI 18:15778c8a97a1 32 if(_serial) _serial->attach(callback(this, &Transmission::serial_event));
YSI 22:e99892e6fd8d 33 _evenThread = new Thread(osPriorityNormal, TRANSMISSION_DEFAULT_THREAD_SIZE);
YSI 22:e99892e6fd8d 34 _evenThread->start(callback(&_queue, &EventQueue::dispatch_forever));
YSI 18:15778c8a97a1 35 }
YSI 0:2fc6fc3b5e15 36
YSI 14:9e3accc681c4 37 Transmission::Transmission(
YSI 14:9e3accc681c4 38 #if MBED_MAJOR_VERSION > 5
YSI 15:b2da6ab01a21 39 UnbufferedSerial *serial,
YSI 14:9e3accc681c4 40 #else
YSI 15:b2da6ab01a21 41 Serial *serial,
YSI 14:9e3accc681c4 42 #endif
YSI 15:b2da6ab01a21 43 EthernetInterface *eth,
YSI 15:b2da6ab01a21 44 string (*processing)(string),
YSI 18:15778c8a97a1 45 void (*ethup)(void),
YSI 15:b2da6ab01a21 46 bool caseIgnore)
YSI 18:15778c8a97a1 47 :_serial(serial), _eth(eth), _processing(processing), _ethup(ethup), _caseIgnore(caseIgnore)
YSI 0:2fc6fc3b5e15 48 {
YSI 16:3ef69ffede76 49 if(_serial) _serial->attach(callback(this, &Transmission::serial_event));
YSI 22:e99892e6fd8d 50 _evenThread = new Thread(osPriorityNormal, TRANSMISSION_DEFAULT_THREAD_SIZE);
YSI 22:e99892e6fd8d 51 _evenThread->start(callback(&_queue, &EventQueue::dispatch_forever));
YSI 18:15778c8a97a1 52 }
YSI 18:15778c8a97a1 53
YSI 18:15778c8a97a1 54 Transmission::Transmission(
YSI 15:b2da6ab01a21 55 #if MBED_MAJOR_VERSION > 5
YSI 15:b2da6ab01a21 56 UnbufferedSerial *serial,
YSI 15:b2da6ab01a21 57 #else
YSI 15:b2da6ab01a21 58 Serial *serial,
YSI 15:b2da6ab01a21 59 #endif
YSI 15:b2da6ab01a21 60 string (*processing)(string),
YSI 15:b2da6ab01a21 61 bool caseIgnore)
YSI 15:b2da6ab01a21 62 :_serial(serial), _processing(processing), _caseIgnore(caseIgnore)
YSI 15:b2da6ab01a21 63 {
YSI 16:3ef69ffede76 64 if(_serial) _serial->attach(callback(this, &Transmission::serial_event));
YSI 22:e99892e6fd8d 65 _evenThread = new Thread(osPriorityNormal, TRANSMISSION_DEFAULT_THREAD_SIZE);
YSI 22:e99892e6fd8d 66 _evenThread->start(callback(&_queue, &EventQueue::dispatch_forever));
YSI 21:59c0adcdfe9b 67 }
YSI 21:59c0adcdfe9b 68
YSI 21:59c0adcdfe9b 69 Transmission::Transmission(
YSI 21:59c0adcdfe9b 70 USBCDC *usb,
YSI 21:59c0adcdfe9b 71 EthernetInterface *eth,
YSI 21:59c0adcdfe9b 72 string (*processing)(string),
YSI 21:59c0adcdfe9b 73 void (*ethup)(void),
YSI 21:59c0adcdfe9b 74 bool caseIgnore)
YSI 21:59c0adcdfe9b 75 :_usb(usb), _eth(eth), _processing(processing), _ethup(ethup), _caseIgnore(caseIgnore)
YSI 21:59c0adcdfe9b 76 {
YSI 22:e99892e6fd8d 77 _evenThread = new Thread(osPriorityNormal, TRANSMISSION_DEFAULT_THREAD_SIZE);
YSI 22:e99892e6fd8d 78 _evenThread->start(callback(&_queue, &EventQueue::dispatch_forever));
YSI 15:b2da6ab01a21 79 }
YSI 15:b2da6ab01a21 80
YSI 15:b2da6ab01a21 81 Transmission::Transmission(
YSI 15:b2da6ab01a21 82 EthernetInterface *eth,
YSI 15:b2da6ab01a21 83 string (*processing)(string),
YSI 18:15778c8a97a1 84 void (*ethup)(void),
YSI 15:b2da6ab01a21 85 bool caseIgnore)
YSI 18:15778c8a97a1 86 :_eth(eth), _processing(processing), _ethup(ethup), _caseIgnore(caseIgnore)
YSI 15:b2da6ab01a21 87 {
YSI 22:e99892e6fd8d 88 _evenThread = new Thread(osPriorityNormal, TRANSMISSION_DEFAULT_THREAD_SIZE);
YSI 22:e99892e6fd8d 89 _evenThread->start(callback(&_queue, &EventQueue::dispatch_forever));
YSI 15:b2da6ab01a21 90 }
YSI 15:b2da6ab01a21 91
YSI 15:b2da6ab01a21 92 Transmission::Transmission(
YSI 15:b2da6ab01a21 93 USBCDC *usb,
YSI 15:b2da6ab01a21 94 string (*processing)(string),
YSI 15:b2da6ab01a21 95 bool caseIgnore)
YSI 18:15778c8a97a1 96 :_usb(usb), _processing(processing), _caseIgnore(caseIgnore)
YSI 18:15778c8a97a1 97 {}
YSI 15:b2da6ab01a21 98
YSI 22:e99892e6fd8d 99 string Transmission::ip(string ip)
YSI 21:59c0adcdfe9b 100 {
YSI 21:59c0adcdfe9b 101 if(!_eth) return "0.0.0.0";
YSI 21:59c0adcdfe9b 102 ostringstream address;
YSI 21:59c0adcdfe9b 103 SocketAddress socket;
YSI 21:59c0adcdfe9b 104 _eth->get_ip_address(&socket);
YSI 21:59c0adcdfe9b 105 address << (socket.get_ip_address()?socket.get_ip_address():"0.0.0.0") << ":" << message.PORT;
YSI 22:e99892e6fd8d 106 if(ip == address.str())
YSI 21:59c0adcdfe9b 107 {
YSI 21:59c0adcdfe9b 108 _eth->get_netmask(&socket);
YSI 21:59c0adcdfe9b 109 address << " " << (socket.get_ip_address()?socket.get_ip_address():"0.0.0.0");
YSI 21:59c0adcdfe9b 110 _eth->get_gateway(&socket);
YSI 21:59c0adcdfe9b 111 address << " " << (socket.get_ip_address()?socket.get_ip_address():"0.0.0.0");
YSI 21:59c0adcdfe9b 112 }
YSI 21:59c0adcdfe9b 113 return address.str();
YSI 21:59c0adcdfe9b 114 }
YSI 21:59c0adcdfe9b 115
YSI 22:e99892e6fd8d 116 string Transmission::ip(const bool set, const char* ip, const uint16_t port, const char* mask, const char* gateway, const uint16_t timeout)
YSI 3:7e15bf0a71f4 117 {
YSI 16:3ef69ffede76 118 if(!_eth) return "00:00:00:00:00:00";
YSI 22:e99892e6fd8d 119 if(message.SET && set)
YSI 3:7e15bf0a71f4 120 {
YSI 22:e99892e6fd8d 121 if(message.PORT != port)
YSI 3:7e15bf0a71f4 122 {
YSI 3:7e15bf0a71f4 123 message.CONNECT = false;
YSI 3:7e15bf0a71f4 124 _serverTCP.sigio(NULL);
YSI 3:7e15bf0a71f4 125 eth_error("serverTCP_close", _serverTCP.close());
YSI 3:7e15bf0a71f4 126 }
YSI 3:7e15bf0a71f4 127 eth_error("Ethernet_disconnect", _eth->disconnect());
YSI 3:7e15bf0a71f4 128 }
YSI 22:e99892e6fd8d 129 message.SET = set;
YSI 22:e99892e6fd8d 130 message.IP = ip;
YSI 22:e99892e6fd8d 131 message.PORT = port;
YSI 22:e99892e6fd8d 132 message.MASK = mask;
YSI 22:e99892e6fd8d 133 message.GATEWAY = gateway;
YSI 22:e99892e6fd8d 134 message.TIMEOUT = timeout;
YSI 3:7e15bf0a71f4 135 message.DHCP = message.IP.empty();
YSI 10:25e049353db5 136 eth_connect();
YSI 14:9e3accc681c4 137 return _eth->get_mac_address()?_eth->get_mac_address():"00:00:00:00:00:00";
YSI 3:7e15bf0a71f4 138 }
YSI 3:7e15bf0a71f4 139
YSI 0:2fc6fc3b5e15 140 bool Transmission::eth_connect(void)
YSI 0:2fc6fc3b5e15 141 {
YSI 16:3ef69ffede76 142 if(!_eth) return false;
YSI 10:25e049353db5 143 if(message.SET)
YSI 0:2fc6fc3b5e15 144 {
YSI 0:2fc6fc3b5e15 145 switch(_eth->get_connection_status())
YSI 0:2fc6fc3b5e15 146 {
YSI 0:2fc6fc3b5e15 147 case NSAPI_STATUS_DISCONNECTED:
YSI 14:9e3accc681c4 148 eth_error("Ethernet_blocking", _eth->set_blocking(false));
YSI 14:9e3accc681c4 149 eth_error("Ethernet_dhcp", _eth->set_dhcp(message.DHCP));
YSI 21:59c0adcdfe9b 150 if(!message.DHCP) eth_error("Ethernet_static", _eth->set_network(SocketAddress(message.IP.c_str()), SocketAddress(message.MASK.c_str()), SocketAddress(message.GATEWAY.c_str())));
YSI 14:9e3accc681c4 151 _eth->attach(callback(this, &Transmission::eth_event));
YSI 15:b2da6ab01a21 152 eth_error("Ethernet_connect", _eth->connect()); break;
YSI 14:9e3accc681c4 153 case NSAPI_STATUS_GLOBAL_UP: return message.CONNECT;break;
YSI 14:9e3accc681c4 154 default: break;
YSI 0:2fc6fc3b5e15 155 }
YSI 0:2fc6fc3b5e15 156 }
YSI 0:2fc6fc3b5e15 157 else if(_eth->get_connection_status() != NSAPI_STATUS_DISCONNECTED) eth_error("Ethernet_disconnect", _eth->disconnect());
YSI 0:2fc6fc3b5e15 158 return false;
YSI 0:2fc6fc3b5e15 159 }
YSI 0:2fc6fc3b5e15 160
YSI 0:2fc6fc3b5e15 161 void Transmission::eth_event(nsapi_event_t status, intptr_t param)
YSI 0:2fc6fc3b5e15 162 {
YSI 0:2fc6fc3b5e15 163 eth_status("Ethernet_event", param);
YSI 0:2fc6fc3b5e15 164 switch(param)
YSI 0:2fc6fc3b5e15 165 {
YSI 15:b2da6ab01a21 166 case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break;
YSI 4:9a4ab4f406ab 167 case NSAPI_STATUS_CONNECTING:if(message.status == BLUE_CLIENT) eth_error("clientTCP_disconnect", _clientTCP->close());
YSI 15:b2da6ab01a21 168 message.status = YELLOW_CONNECTING; break;
YSI 3:7e15bf0a71f4 169 case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP;
YSI 15:b2da6ab01a21 170 if(!message.CONNECT) serverTCP_connect();
YSI 15:b2da6ab01a21 171 else serverTCP_event(); break;
YSI 15:b2da6ab01a21 172 default: break;
YSI 1:27f6baabb15e 173 }
YSI 1:27f6baabb15e 174 }
YSI 1:27f6baabb15e 175
YSI 1:27f6baabb15e 176 bool Transmission::serverTCP_connect(void)
YSI 1:27f6baabb15e 177 {
YSI 3:7e15bf0a71f4 178 if(!message.CONNECT)
YSI 3:7e15bf0a71f4 179 if(eth_error("serverTCP_open", _serverTCP.open(_eth)) == NSAPI_ERROR_OK)
YSI 3:7e15bf0a71f4 180 if(eth_error("serverTCP_bind", _serverTCP.bind(message.PORT)) == NSAPI_ERROR_OK)
YSI 3:7e15bf0a71f4 181 if(eth_error("serverTCP_listen", _serverTCP.listen()) == NSAPI_ERROR_OK)
YSI 1:27f6baabb15e 182 {
YSI 3:7e15bf0a71f4 183 _serverTCP.set_blocking(false);
YSI 3:7e15bf0a71f4 184 _serverTCP.sigio(callback(this, &Transmission::serverTCP_event));
YSI 3:7e15bf0a71f4 185 message.CONNECT = true;
YSI 21:59c0adcdfe9b 186 if(_ethup) _queue.call(_ethup);
YSI 1:27f6baabb15e 187 }
YSI 3:7e15bf0a71f4 188 return message.CONNECT;
YSI 1:27f6baabb15e 189 }
YSI 1:27f6baabb15e 190
YSI 1:27f6baabb15e 191 void Transmission::serverTCP_event(void)
YSI 1:27f6baabb15e 192 {
YSI 21:59c0adcdfe9b 193 _queue.call(this, &Transmission::serverTCP_accept);
YSI 1:27f6baabb15e 194 }
YSI 1:27f6baabb15e 195
YSI 1:27f6baabb15e 196 void Transmission::serverTCP_accept(void)
YSI 1:27f6baabb15e 197 {
YSI 1:27f6baabb15e 198 if(message.status == GREEN_GLOBAL_UP)
YSI 1:27f6baabb15e 199 {
YSI 1:27f6baabb15e 200 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK;
YSI 1:27f6baabb15e 201 message.status = MAGENTA_ACCEPT;
YSI 3:7e15bf0a71f4 202 _clientTCP = _serverTCP.accept(&ack);
YSI 1:27f6baabb15e 203 switch(ack)
YSI 1:27f6baabb15e 204 {
YSI 1:27f6baabb15e 205 case NSAPI_ERROR_OK:
YSI 14:9e3accc681c4 206 _clientTCP->set_timeout(message.TIMEOUT); // config client bloquante avec timeout sinon limite de transmission a 1072 octets
YSI 1:27f6baabb15e 207 message.status = BLUE_CLIENT;
YSI 1:27f6baabb15e 208 break;
YSI 1:27f6baabb15e 209 case NSAPI_ERROR_NO_CONNECTION:
YSI 4:9a4ab4f406ab 210 eth_state();
YSI 3:7e15bf0a71f4 211 serverTCP_event();
YSI 1:27f6baabb15e 212 break;
YSI 1:27f6baabb15e 213 default:
YSI 4:9a4ab4f406ab 214 eth_state();
YSI 4:9a4ab4f406ab 215 if(ack < NSAPI_ERROR_WOULD_BLOCK) eth_error("serverTCP_accept", ack);
YSI 1:27f6baabb15e 216 break;
YSI 1:27f6baabb15e 217 }
YSI 1:27f6baabb15e 218 }
YSI 1:27f6baabb15e 219 }
YSI 1:27f6baabb15e 220
YSI 4:9a4ab4f406ab 221 void Transmission::eth_state(void)
YSI 4:9a4ab4f406ab 222 {
YSI 4:9a4ab4f406ab 223 switch(_eth->get_connection_status())
YSI 4:9a4ab4f406ab 224 {
YSI 4:9a4ab4f406ab 225 case NSAPI_STATUS_DISCONNECTED: message.status = RED_DISCONNECTED; break;
YSI 4:9a4ab4f406ab 226 case NSAPI_STATUS_CONNECTING: message.status = YELLOW_CONNECTING; break;
YSI 4:9a4ab4f406ab 227 case NSAPI_STATUS_GLOBAL_UP: message.status = GREEN_GLOBAL_UP; break;
YSI 15:b2da6ab01a21 228 default: break;
YSI 4:9a4ab4f406ab 229 }
YSI 4:9a4ab4f406ab 230 }
YSI 4:9a4ab4f406ab 231
YSI 14:9e3accc681c4 232 void Transmission::serial_event(void)
YSI 14:9e3accc681c4 233 {
YSI 21:59c0adcdfe9b 234 char caractere;
YSI 14:9e3accc681c4 235 static uint16_t size = 0;
YSI 21:59c0adcdfe9b 236 static char buffer[TRANSMISSION_DEFAULT_BUFFER_SIZE] = {0};
YSI 14:9e3accc681c4 237 _serial->read(&caractere, 1);
YSI 21:59c0adcdfe9b 238 if((caractere == '\n') || (size == (TRANSMISSION_DEFAULT_BUFFER_SIZE-1)))
YSI 14:9e3accc681c4 239 {
YSI 14:9e3accc681c4 240 buffer[size] = '\0';
YSI 14:9e3accc681c4 241 size = 0;
YSI 21:59c0adcdfe9b 242 if(_processing) _queue.call(this, &Transmission::preprocessing, buffer, SERIAL_DELIVERY);
YSI 14:9e3accc681c4 243 }
YSI 21:59c0adcdfe9b 244 else if((caractere > 31) && (caractere < 127) && (size < (TRANSMISSION_DEFAULT_BUFFER_SIZE-1))) buffer[size++] = caractere;
YSI 14:9e3accc681c4 245 }
YSI 14:9e3accc681c4 246
YSI 11:de94dcd67561 247 Transmission::enum_trans_status Transmission::recv(void)
YSI 1:27f6baabb15e 248 {
YSI 16:3ef69ffede76 249 if(_usb)
YSI 15:b2da6ab01a21 250 {
YSI 15:b2da6ab01a21 251 if(_usb->ready())
YSI 15:b2da6ab01a21 252 {
YSI 20:2672d4aea6b7 253 uint8_t buffer[TRANSMISSION_DEFAULT_BUFFER_SIZE] = {0};
YSI 18:15778c8a97a1 254 uint32_t ack = 0, size = 0;
YSI 18:15778c8a97a1 255 do{
YSI 20:2672d4aea6b7 256 ack = NSAPI_ERROR_OK;
YSI 18:15778c8a97a1 257 _usb->receive_nb(&buffer[size], 1, &ack); // un peu plus rapide sur les petits transferts
YSI 18:15778c8a97a1 258 size += ack;
YSI 20:2672d4aea6b7 259 }while((ack > NSAPI_ERROR_OK) && (size < TRANSMISSION_DEFAULT_BUFFER_SIZE-1) && (buffer[size-ack] != '\n'));
YSI 19:6c5777719ece 260 if(size && _processing) preprocessing((char *)buffer, USB_DELIVERY);
YSI 19:6c5777719ece 261 } else _usb->connect();
YSI 15:b2da6ab01a21 262 }
YSI 14:9e3accc681c4 263 if(eth_connect() && (message.status == BLUE_CLIENT))
YSI 1:27f6baabb15e 264 {
YSI 14:9e3accc681c4 265 char buffer[TRANSMISSION_DEFAULT_BUFFER_SIZE] = {0};
YSI 20:2672d4aea6b7 266 nsapi_error_t ack = 0, size = 0;
YSI 20:2672d4aea6b7 267 do{
YSI 20:2672d4aea6b7 268 ack = _clientTCP->recv(&buffer[0], TRANSMISSION_DEFAULT_BUFFER_SIZE-size);
YSI 20:2672d4aea6b7 269 if(ack > NSAPI_ERROR_OK) size += ack;
YSI 20:2672d4aea6b7 270 }while((ack == 536) && (size < TRANSMISSION_DEFAULT_BUFFER_SIZE));
YSI 10:25e049353db5 271 if(ack < NSAPI_ERROR_WOULD_BLOCK) eth_error("clientTCP_recv", ack);
YSI 20:2672d4aea6b7 272 if((ack == NSAPI_ERROR_OK) || (ack == NSAPI_ERROR_NO_CONNECTION))
YSI 18:15778c8a97a1 273 {
YSI 18:15778c8a97a1 274 eth_error("clientTCP_disconnect", _clientTCP->close());
YSI 18:15778c8a97a1 275 eth_state();
YSI 18:15778c8a97a1 276 serverTCP_event();
YSI 18:15778c8a97a1 277 }
YSI 19:6c5777719ece 278 if(size && _processing) preprocessing(buffer, TCP_DELIVERY);
YSI 20:2672d4aea6b7 279 }
YSI 20:2672d4aea6b7 280 else if(!_usb) for(int i = 0; ((i < 10) && (message.status != BLUE_CLIENT)); i++) ThisThread::sleep_for(10ms);
YSI 1:27f6baabb15e 281 return message.status;
YSI 1:27f6baabb15e 282 }
YSI 1:27f6baabb15e 283
YSI 16:3ef69ffede76 284 void Transmission::preprocessing(char *buffer, const enum_trans_delivery delivery)
YSI 16:3ef69ffede76 285 {
YSI 16:3ef69ffede76 286 string cmd(buffer);
YSI 17:9b7100c31466 287 for(char &c : cmd) if(_caseIgnore && (c >= 'a') && (c <= 'z')) c += 'A'-'a';
YSI 16:3ef69ffede76 288 if((cmd.find("HOST: ") != string::npos) || (cmd.find("Host: ") != string::npos))
YSI 21:59c0adcdfe9b 289 send(_processing(cmd), HTTP_DELIVERY);
YSI 18:15778c8a97a1 290 else if(!cmd.empty() && (cmd[0] != 22))
YSI 16:3ef69ffede76 291 {
YSI 17:9b7100c31466 292 for(char &c : cmd) if(c == '\n') c = ';';
YSI 17:9b7100c31466 293 istringstream srecv(cmd);
YSI 16:3ef69ffede76 294 string ssend;
YSI 16:3ef69ffede76 295 while(getline(srecv, cmd, ';'))
YSI 16:3ef69ffede76 296 {
YSI 17:9b7100c31466 297 string process = _processing(cmd);
YSI 17:9b7100c31466 298 if(!process.empty()) ssend += (ssend.size() > 0)?(' '+process):process;
YSI 16:3ef69ffede76 299 }
YSI 16:3ef69ffede76 300 send(ssend, delivery);
YSI 16:3ef69ffede76 301 }
YSI 16:3ef69ffede76 302 }
YSI 16:3ef69ffede76 303
YSI 22:e99892e6fd8d 304 nsapi_error_t Transmission::send(const string& buffer, const enum_trans_delivery& delivery)
YSI 1:27f6baabb15e 305 {
YSI 1:27f6baabb15e 306 nsapi_error_t ack = NSAPI_ERROR_WOULD_BLOCK;
YSI 22:e99892e6fd8d 307 string ssend(buffer+"\n");
YSI 22:e99892e6fd8d 308 if(_usb && !buffer.empty() && ((delivery == USB_DELIVERY) || (delivery == ANY_DELIVERY)))
YSI 18:15778c8a97a1 309 {
YSI 18:15778c8a97a1 310 _usb->connect();
YSI 18:15778c8a97a1 311 if(_usb->ready()) _usb->send((uint8_t*)ssend.c_str(), ssend.size());
YSI 18:15778c8a97a1 312 }
YSI 22:e99892e6fd8d 313 if(_serial && !buffer.empty() && ((delivery == SERIAL_DELIVERY) || (delivery == ANY_DELIVERY)))
YSI 11:de94dcd67561 314 ack = _serial->write(ssend.c_str(), ssend.length());
YSI 19:6c5777719ece 315 if(_eth && ((delivery == TCP_DELIVERY) || (delivery == HTTP_DELIVERY) || (delivery == ANY_DELIVERY)))
YSI 1:27f6baabb15e 316 {
YSI 22:e99892e6fd8d 317 if((message.status == BLUE_CLIENT) && !buffer.empty())
YSI 4:9a4ab4f406ab 318 eth_error("clientTCP_send", ack = _clientTCP->send(ssend.c_str(), ssend.size()));
YSI 19:6c5777719ece 319 if(delivery == HTTP_DELIVERY)
YSI 4:9a4ab4f406ab 320 {
YSI 4:9a4ab4f406ab 321 eth_error("clientTCP_disconnect", _clientTCP->close());
YSI 4:9a4ab4f406ab 322 eth_state();
YSI 4:9a4ab4f406ab 323 serverTCP_event();
YSI 4:9a4ab4f406ab 324 }
YSI 1:27f6baabb15e 325 }
YSI 1:27f6baabb15e 326 return ack;
YSI 1:27f6baabb15e 327 }
YSI 1:27f6baabb15e 328
YSI 22:e99892e6fd8d 329 string Transmission::get(const string& ssend, const string& server, const int& port)
YSI 22:e99892e6fd8d 330 {
YSI 22:e99892e6fd8d 331 char buffer[256] = {0};
YSI 22:e99892e6fd8d 332 if(!server.empty())
YSI 22:e99892e6fd8d 333 {
YSI 22:e99892e6fd8d 334 TCPSocket clientTCP;
YSI 22:e99892e6fd8d 335 clientTCP.set_timeout(2000);
YSI 22:e99892e6fd8d 336 if(eth_error("clientTCP_open", clientTCP.open(_eth)) == NSAPI_ERROR_OK)
YSI 22:e99892e6fd8d 337 {
YSI 22:e99892e6fd8d 338 if(eth_error("clientTCP_connect", clientTCP.connect(SocketAddress(server.c_str(), port))) == NSAPI_ERROR_OK)
YSI 22:e99892e6fd8d 339 {
YSI 22:e99892e6fd8d 340 eth_error("clientTCP_send", clientTCP.send(ssend.c_str(), ssend.size()));
YSI 22:e99892e6fd8d 341 eth_error("clientTCP_recv", clientTCP.recv(buffer, 256));
YSI 22:e99892e6fd8d 342 }
YSI 22:e99892e6fd8d 343 }
YSI 22:e99892e6fd8d 344 eth_error("clientTCP_close", clientTCP.close());
YSI 22:e99892e6fd8d 345 }
YSI 22:e99892e6fd8d 346 return buffer;
YSI 22:e99892e6fd8d 347 }
YSI 22:e99892e6fd8d 348
YSI 22:e99892e6fd8d 349 bool Transmission::smtp(const char* mail, const char* from, const char* subject, const char* data, const char* server)
YSI 1:27f6baabb15e 350 {
YSI 16:3ef69ffede76 351 if(!_eth) return false;
YSI 10:25e049353db5 352 if((!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP)) return false;
YSI 1:27f6baabb15e 353 TCPSocket clientSMTP;
YSI 14:9e3accc681c4 354 clientSMTP.set_timeout(2000);
YSI 22:e99892e6fd8d 355 string sMAIL(mail), sFROM(from), sSUBJECT(subject), sDATA(data), sTO(sMAIL.substr(0, sMAIL.find("@")));
YSI 15:b2da6ab01a21 356 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 15:b2da6ab01a21 357 { "", "HELO Mbed\r\n", "MAIL FROM: <Mbed>\r\n","RCPT TO: <" + sMAIL + ">\r\n", "QUIT\r\n" }};
YSI 1:27f6baabb15e 358 string code;
YSI 1:27f6baabb15e 359 if(eth_error("clientSMTP_open", clientSMTP.open(_eth)) == NSAPI_ERROR_OK)
YSI 1:27f6baabb15e 360 {
YSI 14:9e3accc681c4 361 for(const string& ssend : smtpParams[(sFROM.empty())?1:0])
YSI 1:27f6baabb15e 362 {
YSI 1:27f6baabb15e 363 char buffer[256] = {0};
YSI 22:e99892e6fd8d 364 if(code.empty()) { if(eth_error("clientSMTP_connect", clientSMTP.connect(SocketAddress(server, 25))) < NSAPI_ERROR_OK) break; }
YSI 1:27f6baabb15e 365 else if(eth_error("clientSMTP_send", clientSMTP.send(ssend.c_str(), ssend.size())) < NSAPI_ERROR_OK) break;
YSI 1:27f6baabb15e 366 if(eth_error("clientSMTP_recv", clientSMTP.recv(buffer, 256)) < NSAPI_ERROR_OK) break;
YSI 1:27f6baabb15e 367 buffer[3] = 0;
YSI 1:27f6baabb15e 368 code += buffer;
YSI 1:27f6baabb15e 369 if(ssend == "QUIT\r\n") break;
YSI 1:27f6baabb15e 370 }
YSI 1:27f6baabb15e 371 eth_error("clientSMTP_close", clientSMTP.close());
YSI 1:27f6baabb15e 372 }
YSI 1:27f6baabb15e 373 if(sFROM.empty()) return code == "220250250250221";
YSI 12:e22ff63d237c 374 #if MBED_MAJOR_VERSION > 5
YSI 22:e99892e6fd8d 375 else if(code != "220250250250354250221") _queue.call_in(60s, this, &Transmission::smtp, mail, from, subject, data, server);
YSI 12:e22ff63d237c 376 #else
YSI 22:e99892e6fd8d 377 else if(code != "220250250250354250221") _queue.call_in(60000, this, &Transmission::smtp, mail, from, subject, data, server);
YSI 12:e22ff63d237c 378 #endif
YSI 1:27f6baabb15e 379 return code == "220250250250354250221";
YSI 0:2fc6fc3b5e15 380 }
YSI 0:2fc6fc3b5e15 381
YSI 22:e99892e6fd8d 382 time_t Transmission::ntp(const char* server)
YSI 6:d6a07fd1548a 383 {
YSI 16:3ef69ffede76 384 if(!_eth) return 0;
YSI 10:25e049353db5 385 if((!message.DHCP) || (_eth->get_connection_status() != NSAPI_STATUS_GLOBAL_UP)) return 0;
YSI 6:d6a07fd1548a 386 time_t timeStamp = 0;
YSI 6:d6a07fd1548a 387 UDPSocket clientNTP;
YSI 14:9e3accc681c4 388 clientNTP.set_timeout(2000);
YSI 6:d6a07fd1548a 389 if(eth_error("clientNTP_open", clientNTP.open(_eth)) == NSAPI_ERROR_OK)
YSI 6:d6a07fd1548a 390 {
YSI 6:d6a07fd1548a 391 uint32_t buffer[12] = { 0b11011, 0 }; // VN = 3 & Mode = 3
YSI 22:e99892e6fd8d 392 SocketAddress aSERVER(server, 123);
YSI 22:e99892e6fd8d 393 string sSERVER(server);
YSI 22:e99892e6fd8d 394 if(sSERVER != TRANSMISSION_DEFAULT_NTP_SERVER) eth_error("eth_gethostbyname", _eth->gethostbyname(sSERVER.c_str(), &aSERVER));
YSI 22:e99892e6fd8d 395 if(eth_error("clientNTP_send", clientNTP.sendto(aSERVER, (void*)buffer, sizeof(buffer))) > NSAPI_ERROR_OK)
YSI 6:d6a07fd1548a 396 {
YSI 6:d6a07fd1548a 397 if(eth_error("clientNTP_recv", clientNTP.recvfrom(NULL, (void*)buffer, sizeof(buffer))) > NSAPI_ERROR_OK)
YSI 6:d6a07fd1548a 398 {
YSI 6:d6a07fd1548a 399 timeStamp = ((buffer[10] & 0xFF) << 24) | ((buffer[10] & 0xFF00) << 8) | ((buffer[10] & 0xFF0000UL) >> 8) | ((buffer[10] & 0xFF000000UL) >> 24);
YSI 6:d6a07fd1548a 400 timeStamp -= 2208985200U; // 01/01/1970 Europe
YSI 6:d6a07fd1548a 401 struct tm * tmTimeStamp = localtime(&timeStamp);
YSI 10:25e049353db5 402 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 403 timeStamp += 3600; // DST starts last Sunday of March; 2am (1am UTC), DST ends last Sunday of october; 3am (2am UTC)
YSI 6:d6a07fd1548a 404 }
YSI 6:d6a07fd1548a 405 }
YSI 6:d6a07fd1548a 406 eth_error("clientNTP_close", clientNTP.close());
YSI 6:d6a07fd1548a 407 }
YSI 6:d6a07fd1548a 408 return timeStamp;
YSI 6:d6a07fd1548a 409 }
YSI 6:d6a07fd1548a 410
YSI 0:2fc6fc3b5e15 411 intptr_t Transmission::eth_status(const string& source, const intptr_t& code)
YSI 0:2fc6fc3b5e15 412 {
YSI 14:9e3accc681c4 413 #ifndef NDEBUG
YSI 14:9e3accc681c4 414 ostringstream message;
YSI 0:2fc6fc3b5e15 415 message << "\n" << source << "[" << code;
YSI 0:2fc6fc3b5e15 416 switch(code)
YSI 0:2fc6fc3b5e15 417 {
YSI 0:2fc6fc3b5e15 418 case NSAPI_STATUS_LOCAL_UP: message << "] NSAPI_STATUS_LOCAL_UP < local IP address set >"; break;
YSI 0:2fc6fc3b5e15 419 case NSAPI_STATUS_GLOBAL_UP: message << "] NSAPI_STATUS_GLOBAL_UP < global IP address set >"; break;
YSI 0:2fc6fc3b5e15 420 case NSAPI_STATUS_DISCONNECTED: message << "] NSAPI_STATUS_DISCONNECTED < no connection to network >"; break;
YSI 0:2fc6fc3b5e15 421 case NSAPI_STATUS_CONNECTING: message << "] NSAPI_STATUS_CONNECTING < connecting to network >"; break;
YSI 0:2fc6fc3b5e15 422 case NSAPI_STATUS_ERROR_UNSUPPORTED: message << "] NSAPI_STATUS_ERROR_UNSUPPORTED < unsupported functionality >";break;
YSI 0:2fc6fc3b5e15 423 }
YSI 1:27f6baabb15e 424 _serial->write(message.str().c_str(), message.str().size());
YSI 1:27f6baabb15e 425 #endif
YSI 0:2fc6fc3b5e15 426 return code;
YSI 0:2fc6fc3b5e15 427 }
YSI 0:2fc6fc3b5e15 428
YSI 0:2fc6fc3b5e15 429 nsapi_error_t Transmission::eth_error(const string& source, const nsapi_error_t& code)
YSI 0:2fc6fc3b5e15 430 {
YSI 14:9e3accc681c4 431 #ifndef NDEBUG
YSI 14:9e3accc681c4 432 ostringstream message;
YSI 0:2fc6fc3b5e15 433 message << "\n" << source << "[" << code;
YSI 0:2fc6fc3b5e15 434 switch(code)
YSI 0:2fc6fc3b5e15 435 {
YSI 0:2fc6fc3b5e15 436 case NSAPI_ERROR_OK: message << "] NSAPI_ERROR_OK < no error >"; break;
YSI 0:2fc6fc3b5e15 437 case NSAPI_ERROR_WOULD_BLOCK: message << "] NSAPI_ERROR_WOULD_BLOCK < no data is not available but call is non-blocking >";break;
YSI 0:2fc6fc3b5e15 438 case NSAPI_ERROR_UNSUPPORTED: message << "] NSAPI_ERROR_UNSUPPORTED < unsupported functionality >"; break;
YSI 0:2fc6fc3b5e15 439 case NSAPI_ERROR_PARAMETER: message << "] NSAPI_ERROR_PARAMETER < invalid configuration >"; break;
YSI 0:2fc6fc3b5e15 440 case NSAPI_ERROR_NO_CONNECTION: message << "] NSAPI_ERROR_NO_CONNECTION < not connected to a network >"; break;
YSI 0:2fc6fc3b5e15 441 case NSAPI_ERROR_NO_SOCKET: message << "] NSAPI_ERROR_NO_SOCKET < socket not available for use >"; break;
YSI 0:2fc6fc3b5e15 442 case NSAPI_ERROR_NO_ADDRESS: message << "] NSAPI_ERROR_NO_ADDRESS < IP address is not known >"; break;
YSI 0:2fc6fc3b5e15 443 case NSAPI_ERROR_NO_MEMORY: message << "] NSAPI_ERROR_NO_MEMORY < memory resource not available >"; break;
YSI 0:2fc6fc3b5e15 444 case NSAPI_ERROR_NO_SSID: message << "] NSAPI_ERROR_NO_SSID < ssid not found >"; break;
YSI 0:2fc6fc3b5e15 445 case NSAPI_ERROR_DNS_FAILURE: message << "] NSAPI_ERROR_DNS_FAILURE < DNS failed to complete successfully >"; break;
YSI 0:2fc6fc3b5e15 446 case NSAPI_ERROR_DHCP_FAILURE: message << "] NSAPI_ERROR_DHCP_FAILURE < DHCP failed to complete successfully >"; break;
YSI 0:2fc6fc3b5e15 447 case NSAPI_ERROR_AUTH_FAILURE: message << "] NSAPI_ERROR_AUTH_FAILURE < connection to access point failed >"; break;
YSI 0:2fc6fc3b5e15 448 case NSAPI_ERROR_DEVICE_ERROR: message << "] NSAPI_ERROR_DEVICE_ERROR < failure interfacing with the network processor >"; break;
YSI 0:2fc6fc3b5e15 449 case NSAPI_ERROR_IN_PROGRESS: message << "] NSAPI_ERROR_IN_PROGRESS < operation (eg connect) in progress >"; break;
YSI 0:2fc6fc3b5e15 450 case NSAPI_ERROR_ALREADY: message << "] NSAPI_ERROR_ALREADY < operation (eg connect) already in progress >"; break;
YSI 0:2fc6fc3b5e15 451 case NSAPI_ERROR_IS_CONNECTED: message << "] NSAPI_ERROR_IS_CONNECTED < socket is already connected >"; break;
YSI 0:2fc6fc3b5e15 452 case NSAPI_ERROR_CONNECTION_LOST: message << "] NSAPI_ERROR_CONNECTION_LOST < connection lost >"; break;
YSI 0:2fc6fc3b5e15 453 case NSAPI_ERROR_CONNECTION_TIMEOUT: message << "] NSAPI_ERROR_CONNECTION_TIMEOUT < connection timed out >"; break;
YSI 0:2fc6fc3b5e15 454 case NSAPI_ERROR_ADDRESS_IN_USE: message << "] NSAPI_ERROR_ADDRESS_IN_USE < Address already in use >"; break;
YSI 0:2fc6fc3b5e15 455 case NSAPI_ERROR_TIMEOUT: message << "] NSAPI_ERROR_TIMEOUT < operation timed out >"; break;
YSI 0:2fc6fc3b5e15 456 case NSAPI_ERROR_BUSY: message << "] NSAPI_ERROR_BUSY < device is busy and cannot accept new operation >"; break;
YSI 0:2fc6fc3b5e15 457 default: message << "] NSAPI_ERROR < unknow code >"; break;
YSI 0:2fc6fc3b5e15 458 }
YSI 1:27f6baabb15e 459 if(code < NSAPI_ERROR_OK) _serial->write(message.str().c_str(), message.str().size());
YSI 1:27f6baabb15e 460 #endif
YSI 0:2fc6fc3b5e15 461 return code;
YSI 0:2fc6fc3b5e15 462 }