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