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.h@22:e99892e6fd8d, 2021-06-29 (annotated)
- Committer:
- YSI
- Date:
- Tue Jun 29 13:01:17 2021 +0000
- Revision:
- 22:e99892e6fd8d
- Parent:
- 21:59c0adcdfe9b
- Child:
- 24:a925173e1143
add requeste to specific ip and port with get function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YSI | 0:2fc6fc3b5e15 | 1 | /** Transmission class |
YSI | 0:2fc6fc3b5e15 | 2 | * |
YSI | 0:2fc6fc3b5e15 | 3 | * @purpose library for Transmission |
YSI | 0:2fc6fc3b5e15 | 4 | * |
YSI | 0:2fc6fc3b5e15 | 5 | * Use to Transmission |
YSI | 0:2fc6fc3b5e15 | 6 | * |
YSI | 0:2fc6fc3b5e15 | 7 | * Example: |
YSI | 0:2fc6fc3b5e15 | 8 | * @code |
YSI | 21:59c0adcdfe9b | 9 | * #define MBED_PROJECT "Transmission" |
YSI | 21:59c0adcdfe9b | 10 | * |
YSI | 21:59c0adcdfe9b | 11 | * #include "lib_Transmission.h" |
YSI | 21:59c0adcdfe9b | 12 | * |
YSI | 21:59c0adcdfe9b | 13 | * #if MBED_MAJOR_VERSION > 5 |
YSI | 21:59c0adcdfe9b | 14 | * UnbufferedSerial pc(USBTX, USBRX, 230400); |
YSI | 21:59c0adcdfe9b | 15 | * #else |
YSI | 21:59c0adcdfe9b | 16 | * Serial pc(USBTX, USBRX, 230400); |
YSI | 21:59c0adcdfe9b | 17 | * #endif |
YSI | 21:59c0adcdfe9b | 18 | * |
YSI | 21:59c0adcdfe9b | 19 | * string transmission_processing(string); |
YSI | 21:59c0adcdfe9b | 20 | * Transmission transmission(&pc, &transmission_processing); |
YSI | 21:59c0adcdfe9b | 21 | * |
YSI | 21:59c0adcdfe9b | 22 | * int main(void) |
YSI | 0:2fc6fc3b5e15 | 23 | * { |
YSI | 21:59c0adcdfe9b | 24 | * while(1) ThisThread::sleep_for(200ms); |
YSI | 21:59c0adcdfe9b | 25 | * } |
YSI | 21:59c0adcdfe9b | 26 | * |
YSI | 21:59c0adcdfe9b | 27 | * string transmission_processing(string cmd) |
YSI | 21:59c0adcdfe9b | 28 | * { |
YSI | 21:59c0adcdfe9b | 29 | * ostringstream ssend; |
YSI | 21:59c0adcdfe9b | 30 | * ssend << fixed; |
YSI | 21:59c0adcdfe9b | 31 | * ssend.precision(2); |
YSI | 21:59c0adcdfe9b | 32 | * if(cmd.empty()); |
YSI | 21:59c0adcdfe9b | 33 | * else if(cmd == "*IDN?") |
YSI | 21:59c0adcdfe9b | 34 | * ssend << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__; |
YSI | 21:59c0adcdfe9b | 35 | * else if(cmd[cmd.size()-1] == '?') |
YSI | 21:59c0adcdfe9b | 36 | * ssend << "incorrect requeste [" << cmd << "]"; |
YSI | 21:59c0adcdfe9b | 37 | * return ssend.str(); |
YSI | 0:2fc6fc3b5e15 | 38 | * } |
YSI | 0:2fc6fc3b5e15 | 39 | * @endcode |
YSI | 0:2fc6fc3b5e15 | 40 | * @file lib_Transmission.h |
YSI | 0:2fc6fc3b5e15 | 41 | * @date Jun 2020 |
YSI | 0:2fc6fc3b5e15 | 42 | * @author Yannic Simon |
YSI | 0:2fc6fc3b5e15 | 43 | */ |
YSI | 4:9a4ab4f406ab | 44 | #ifndef TRANSMISSION_H |
YSI | 4:9a4ab4f406ab | 45 | #define TRANSMISSION_H |
YSI | 22:e99892e6fd8d | 46 | //#define NDEBUG |
YSI | 0:2fc6fc3b5e15 | 47 | |
YSI | 22:e99892e6fd8d | 48 | #include <sstream> |
YSI | 0:2fc6fc3b5e15 | 49 | #include "mbed.h" |
YSI | 15:b2da6ab01a21 | 50 | #include "USBCDC.h" |
YSI | 0:2fc6fc3b5e15 | 51 | #include "EthernetInterface.h" |
YSI | 0:2fc6fc3b5e15 | 52 | |
YSI | 15:b2da6ab01a21 | 53 | #define TRANSMISSION_DEFAULT_BUFFER_SIZE 1072 // taille des buffers de reception |
YSI | 22:e99892e6fd8d | 54 | #define TRANSMISSION_DEFAULT_THREAD_SIZE OS_STACK_SIZE // taille du thread transmission |
YSI | 14:9e3accc681c4 | 55 | #define TRANSMISSION_DEFAULT_SMTP_SERVER "129.175.212.70" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("smtp.u-psud.fr") |
YSI | 14:9e3accc681c4 | 56 | #define TRANSMISSION_DEFAULT_NTP_SERVER "129.175.34.43" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("ntp.u-psud.fr") |
YSI | 0:2fc6fc3b5e15 | 57 | |
YSI | 0:2fc6fc3b5e15 | 58 | /** Transmission class |
YSI | 0:2fc6fc3b5e15 | 59 | */ |
YSI | 0:2fc6fc3b5e15 | 60 | class Transmission |
YSI | 0:2fc6fc3b5e15 | 61 | { |
YSI | 0:2fc6fc3b5e15 | 62 | public: |
YSI | 22:e99892e6fd8d | 63 | /** enumerator of the different possible delivery of the transmission library |
YSI | 15:b2da6ab01a21 | 64 | */ |
YSI | 19:6c5777719ece | 65 | typedef enum { USB_DELIVERY, SERIAL_DELIVERY, TCP_DELIVERY, HTTP_DELIVERY, ANY_DELIVERY } |
YSI | 15:b2da6ab01a21 | 66 | enum_trans_delivery; |
YSI | 22:e99892e6fd8d | 67 | /** enumerator of the different ethernet connexion status of the transmission library |
YSI | 15:b2da6ab01a21 | 68 | */ |
YSI | 19:6c5777719ece | 69 | typedef enum { WHITE_STATUS, CYAN_STATUS, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE } |
YSI | 14:9e3accc681c4 | 70 | enum_trans_status; |
YSI | 22:e99892e6fd8d | 71 | /** List of http headers returns |
YSI | 15:b2da6ab01a21 | 72 | */ |
YSI | 14:9e3accc681c4 | 73 | struct { const char *RETURN_OK; const char *RETURN_NO_CONTENT; const char *RETURN_MOVED; const char *RETURN_FOUND; const char *RETURN_SEE_OTHER; const char *RETURN_REDIRECT; const char *RETURN_NOT_FOUND; } |
YSI | 14:9e3accc681c4 | 74 | http = { "HTTP/1.1 200 OK\r\n", "HTTP/1.1 204 No Content\r\n", "HTTP/1.1 301 Moved Permanently\r\n", "HTTP/1.1 302 Found\r\n", "HTTP/1.1 303 See Other\r\n", "HTTP/1.1 307 Temporary Redirect\r\n", "HTTP/1.1 404 Not Found\r\n" }; |
YSI | 0:2fc6fc3b5e15 | 75 | /** make new Transmission instance |
YSI | 0:2fc6fc3b5e15 | 76 | * |
YSI | 0:2fc6fc3b5e15 | 77 | * @param |
YSI | 0:2fc6fc3b5e15 | 78 | */ |
YSI | 14:9e3accc681c4 | 79 | Transmission( |
YSI | 14:9e3accc681c4 | 80 | #if MBED_MAJOR_VERSION > 5 |
YSI | 14:9e3accc681c4 | 81 | UnbufferedSerial *serial, |
YSI | 14:9e3accc681c4 | 82 | #else |
YSI | 14:9e3accc681c4 | 83 | Serial *serial, |
YSI | 14:9e3accc681c4 | 84 | #endif |
YSI | 18:15778c8a97a1 | 85 | USBCDC *usb, |
YSI | 15:b2da6ab01a21 | 86 | EthernetInterface *eth, |
YSI | 18:15778c8a97a1 | 87 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 88 | void (*ethup)(void) = NULL, |
YSI | 18:15778c8a97a1 | 89 | bool caseIgnore = true); |
YSI | 18:15778c8a97a1 | 90 | /** make new Transmission instance |
YSI | 18:15778c8a97a1 | 91 | * |
YSI | 18:15778c8a97a1 | 92 | * @param |
YSI | 18:15778c8a97a1 | 93 | */ |
YSI | 18:15778c8a97a1 | 94 | Transmission( |
YSI | 18:15778c8a97a1 | 95 | #if MBED_MAJOR_VERSION > 5 |
YSI | 18:15778c8a97a1 | 96 | UnbufferedSerial *serial, |
YSI | 18:15778c8a97a1 | 97 | #else |
YSI | 18:15778c8a97a1 | 98 | Serial *serial, |
YSI | 18:15778c8a97a1 | 99 | #endif |
YSI | 15:b2da6ab01a21 | 100 | USBCDC *usb, |
YSI | 15:b2da6ab01a21 | 101 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 102 | bool caseIgnore = true); |
YSI | 18:15778c8a97a1 | 103 | /** make new Transmission instance |
YSI | 18:15778c8a97a1 | 104 | * |
YSI | 18:15778c8a97a1 | 105 | * @param |
YSI | 18:15778c8a97a1 | 106 | */ |
YSI | 18:15778c8a97a1 | 107 | Transmission( |
YSI | 18:15778c8a97a1 | 108 | #if MBED_MAJOR_VERSION > 5 |
YSI | 18:15778c8a97a1 | 109 | UnbufferedSerial *serial, |
YSI | 18:15778c8a97a1 | 110 | #else |
YSI | 18:15778c8a97a1 | 111 | Serial *serial, |
YSI | 18:15778c8a97a1 | 112 | #endif |
YSI | 18:15778c8a97a1 | 113 | EthernetInterface *eth, |
YSI | 18:15778c8a97a1 | 114 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 115 | void (*ethup)(void) = NULL, |
YSI | 18:15778c8a97a1 | 116 | bool caseIgnore = true); |
YSI | 18:15778c8a97a1 | 117 | /** make new Transmission instance |
YSI | 18:15778c8a97a1 | 118 | * |
YSI | 18:15778c8a97a1 | 119 | * @param |
YSI | 18:15778c8a97a1 | 120 | */ |
YSI | 18:15778c8a97a1 | 121 | Transmission( |
YSI | 18:15778c8a97a1 | 122 | USBCDC *usb, |
YSI | 18:15778c8a97a1 | 123 | EthernetInterface *eth, |
YSI | 18:15778c8a97a1 | 124 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 125 | void (*ethup)(void) = NULL, |
YSI | 15:b2da6ab01a21 | 126 | bool caseIgnore = true); |
YSI | 15:b2da6ab01a21 | 127 | /** make new Transmission instance |
YSI | 15:b2da6ab01a21 | 128 | * |
YSI | 15:b2da6ab01a21 | 129 | * @param |
YSI | 15:b2da6ab01a21 | 130 | */ |
YSI | 15:b2da6ab01a21 | 131 | Transmission( |
YSI | 15:b2da6ab01a21 | 132 | #if MBED_MAJOR_VERSION > 5 |
YSI | 15:b2da6ab01a21 | 133 | UnbufferedSerial *serial, |
YSI | 15:b2da6ab01a21 | 134 | #else |
YSI | 15:b2da6ab01a21 | 135 | Serial *serial, |
YSI | 15:b2da6ab01a21 | 136 | #endif |
YSI | 15:b2da6ab01a21 | 137 | string (*processing)(string), |
YSI | 15:b2da6ab01a21 | 138 | bool caseIgnore = true); |
YSI | 15:b2da6ab01a21 | 139 | /** make new Transmission instance |
YSI | 15:b2da6ab01a21 | 140 | * |
YSI | 15:b2da6ab01a21 | 141 | * @param |
YSI | 15:b2da6ab01a21 | 142 | */ |
YSI | 15:b2da6ab01a21 | 143 | Transmission( |
YSI | 15:b2da6ab01a21 | 144 | EthernetInterface *eth, |
YSI | 15:b2da6ab01a21 | 145 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 146 | void (*ethup)(void) = NULL, |
YSI | 15:b2da6ab01a21 | 147 | bool caseIgnore = true); |
YSI | 15:b2da6ab01a21 | 148 | /** make new Transmission instance |
YSI | 15:b2da6ab01a21 | 149 | * |
YSI | 15:b2da6ab01a21 | 150 | * @param |
YSI | 15:b2da6ab01a21 | 151 | */ |
YSI | 15:b2da6ab01a21 | 152 | Transmission( |
YSI | 15:b2da6ab01a21 | 153 | USBCDC *usb, |
YSI | 15:b2da6ab01a21 | 154 | string (*processing)(string), |
YSI | 15:b2da6ab01a21 | 155 | bool caseIgnore = true); |
YSI | 22:e99892e6fd8d | 156 | /** Configure the TCP connection |
YSI | 0:2fc6fc3b5e15 | 157 | * |
YSI | 22:e99892e6fd8d | 158 | * @param set enable for the tcp connection |
YSI | 22:e99892e6fd8d | 159 | * @returns MAC adress |
YSI | 4:9a4ab4f406ab | 160 | */ |
YSI | 22:e99892e6fd8d | 161 | string ip(const bool set, const char* ip="", const uint16_t port=80, const char* mask="255.255.255.0", const char* gateway="192.168.1.1", const uint16_t timeout=100); |
YSI | 22:e99892e6fd8d | 162 | /** Return ip config |
YSI | 4:9a4ab4f406ab | 163 | * |
YSI | 22:e99892e6fd8d | 164 | * @param ip="" If the specified ip is the one configured gives the complete ip configuration |
YSI | 22:e99892e6fd8d | 165 | * @returns ip config |
YSI | 4:9a4ab4f406ab | 166 | */ |
YSI | 22:e99892e6fd8d | 167 | string ip(string ip=""); |
YSI | 22:e99892e6fd8d | 168 | /** scans the reception buffers of transmission TCP and USB |
YSI | 4:9a4ab4f406ab | 169 | * |
YSI | 22:e99892e6fd8d | 170 | * @returns enumerator of the different ethernet connexion status of the transmission library |
YSI | 4:9a4ab4f406ab | 171 | */ |
YSI | 11:de94dcd67561 | 172 | enum_trans_status recv(void); |
YSI | 22:e99892e6fd8d | 173 | /** send the buffer to the specified transmission delivery |
YSI | 4:9a4ab4f406ab | 174 | * |
YSI | 22:e99892e6fd8d | 175 | * @param buffer sent over transmission |
YSI | 22:e99892e6fd8d | 176 | * @param delivery of the transmission |
YSI | 14:9e3accc681c4 | 177 | * @returns |
YSI | 4:9a4ab4f406ab | 178 | */ |
YSI | 22:e99892e6fd8d | 179 | nsapi_error_t send(const string& buffer="", const enum_trans_delivery& delivery=ANY_DELIVERY); |
YSI | 22:e99892e6fd8d | 180 | /** send a request to a server at the specified port |
YSI | 15:b2da6ab01a21 | 181 | * |
YSI | 22:e99892e6fd8d | 182 | * @param request sent to server |
YSI | 22:e99892e6fd8d | 183 | * @param server ip |
YSI | 22:e99892e6fd8d | 184 | * @param server port |
YSI | 22:e99892e6fd8d | 185 | * @returns server response |
YSI | 15:b2da6ab01a21 | 186 | */ |
YSI | 22:e99892e6fd8d | 187 | string get(const string& buffer, const string& server="", const int& port=80); |
YSI | 22:e99892e6fd8d | 188 | /** send an email to an smtp server |
YSI | 4:9a4ab4f406ab | 189 | * |
YSI | 22:e99892e6fd8d | 190 | * @param mail is the recipient's email |
YSI | 22:e99892e6fd8d | 191 | * @param from="" this is the sender's email |
YSI | 22:e99892e6fd8d | 192 | * @param subject="" this is the subject of the email |
YSI | 22:e99892e6fd8d | 193 | * @param data="" this is the content of the email |
YSI | 22:e99892e6fd8d | 194 | * @param server="" this is an ip from an smtp server |
YSI | 22:e99892e6fd8d | 195 | * @returns indicates if the smtp transaction was successful |
YSI | 4:9a4ab4f406ab | 196 | */ |
YSI | 22:e99892e6fd8d | 197 | bool smtp(const char* mail, const char* from="", const char* subject="", const char* data="", const char* server=TRANSMISSION_DEFAULT_SMTP_SERVER); |
YSI | 22:e99892e6fd8d | 198 | /** time request to an ntp server |
YSI | 22:e99892e6fd8d | 199 | * |
YSI | 22:e99892e6fd8d | 200 | * @param server="" this is an ip from an ntp server |
YSI | 22:e99892e6fd8d | 201 | * @returns time |
YSI | 22:e99892e6fd8d | 202 | */ |
YSI | 22:e99892e6fd8d | 203 | time_t ntp(const char* server=TRANSMISSION_DEFAULT_NTP_SERVER); |
YSI | 0:2fc6fc3b5e15 | 204 | |
YSI | 0:2fc6fc3b5e15 | 205 | private: |
YSI | 12:e22ff63d237c | 206 | #if MBED_MAJOR_VERSION > 5 |
YSI | 15:b2da6ab01a21 | 207 | UnbufferedSerial *_serial = NULL; |
YSI | 12:e22ff63d237c | 208 | #else |
YSI | 15:b2da6ab01a21 | 209 | Serial *_serial = NULL; |
YSI | 12:e22ff63d237c | 210 | #endif |
YSI | 14:9e3accc681c4 | 211 | TCPSocket _serverTCP, *_clientTCP = NULL; |
YSI | 22:e99892e6fd8d | 212 | Thread *_evenThread = NULL; |
YSI | 21:59c0adcdfe9b | 213 | EventQueue _queue; |
YSI | 15:b2da6ab01a21 | 214 | EthernetInterface *_eth = NULL; |
YSI | 15:b2da6ab01a21 | 215 | USBCDC *_usb = NULL; |
YSI | 15:b2da6ab01a21 | 216 | bool _caseIgnore = false; |
YSI | 14:9e3accc681c4 | 217 | |
YSI | 14:9e3accc681c4 | 218 | void serial_event(void); |
YSI | 0:2fc6fc3b5e15 | 219 | |
YSI | 8:7193327bed34 | 220 | void eth_state(void); |
YSI | 8:7193327bed34 | 221 | bool eth_connect(void); |
YSI | 8:7193327bed34 | 222 | void eth_event(nsapi_event_t, intptr_t); |
YSI | 8:7193327bed34 | 223 | intptr_t eth_status(const string&, const intptr_t&); |
YSI | 8:7193327bed34 | 224 | nsapi_error_t eth_error(const string& SOURCE, const nsapi_error_t& CODE); |
YSI | 8:7193327bed34 | 225 | |
YSI | 8:7193327bed34 | 226 | bool serverTCP_connect(void); |
YSI | 8:7193327bed34 | 227 | void serverTCP_accept(void); |
YSI | 8:7193327bed34 | 228 | void serverTCP_event(void); |
YSI | 8:7193327bed34 | 229 | |
YSI | 18:15778c8a97a1 | 230 | void (*_ethup)(void); |
YSI | 15:b2da6ab01a21 | 231 | string (*_processing)(string); |
YSI | 21:59c0adcdfe9b | 232 | void preprocessing(char *, const enum_trans_delivery); |
YSI | 21:59c0adcdfe9b | 233 | struct { enum_trans_status status; bool SET; bool DHCP; bool CONNECT; string IP; uint16_t PORT; string MASK; string GATEWAY; uint16_t TIMEOUT; } |
YSI | 21:59c0adcdfe9b | 234 | message = { RED_DISCONNECTED, false, false, false, "192.168.1.1", 80, "255.255.255.0", "192.168.1.0", 100 }; |
YSI | 8:7193327bed34 | 235 | }; |
YSI | 0:2fc6fc3b5e15 | 236 | #endif |