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@21:59c0adcdfe9b, 2021-06-23 (annotated)
- Committer:
- YSI
- Date:
- Wed Jun 23 12:35:34 2021 +0000
- Revision:
- 21:59c0adcdfe9b
- Parent:
- 19:6c5777719ece
- Child:
- 22:e99892e6fd8d
remove share event queue to specific event thread
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 | 14:9e3accc681c4 | 46 | #define NDEBUG |
YSI | 0:2fc6fc3b5e15 | 47 | |
YSI | 0:2fc6fc3b5e15 | 48 | #include "mbed.h" |
YSI | 15:b2da6ab01a21 | 49 | #include "USBCDC.h" |
YSI | 0:2fc6fc3b5e15 | 50 | #include "EthernetInterface.h" |
YSI | 0:2fc6fc3b5e15 | 51 | #include <sstream> |
YSI | 0:2fc6fc3b5e15 | 52 | |
YSI | 15:b2da6ab01a21 | 53 | #define TRANSMISSION_DEFAULT_BUFFER_SIZE 1072 // taille des buffers de reception |
YSI | 14:9e3accc681c4 | 54 | #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 | 55 | #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 | 56 | |
YSI | 0:2fc6fc3b5e15 | 57 | /** Transmission class |
YSI | 0:2fc6fc3b5e15 | 58 | */ |
YSI | 0:2fc6fc3b5e15 | 59 | class Transmission |
YSI | 0:2fc6fc3b5e15 | 60 | { |
YSI | 0:2fc6fc3b5e15 | 61 | public: |
YSI | 15:b2da6ab01a21 | 62 | /** |
YSI | 15:b2da6ab01a21 | 63 | * |
YSI | 15:b2da6ab01a21 | 64 | * @param |
YSI | 15:b2da6ab01a21 | 65 | * @param |
YSI | 15:b2da6ab01a21 | 66 | * @returns |
YSI | 15:b2da6ab01a21 | 67 | */ |
YSI | 19:6c5777719ece | 68 | typedef enum { USB_DELIVERY, SERIAL_DELIVERY, TCP_DELIVERY, HTTP_DELIVERY, ANY_DELIVERY } |
YSI | 15:b2da6ab01a21 | 69 | enum_trans_delivery; |
YSI | 15:b2da6ab01a21 | 70 | /** |
YSI | 15:b2da6ab01a21 | 71 | * |
YSI | 15:b2da6ab01a21 | 72 | * @param |
YSI | 15:b2da6ab01a21 | 73 | * @param |
YSI | 15:b2da6ab01a21 | 74 | * @returns |
YSI | 15:b2da6ab01a21 | 75 | */ |
YSI | 19:6c5777719ece | 76 | typedef enum { WHITE_STATUS, CYAN_STATUS, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE } |
YSI | 14:9e3accc681c4 | 77 | enum_trans_status; |
YSI | 15:b2da6ab01a21 | 78 | /** |
YSI | 15:b2da6ab01a21 | 79 | * |
YSI | 15:b2da6ab01a21 | 80 | * @param |
YSI | 15:b2da6ab01a21 | 81 | * @param |
YSI | 15:b2da6ab01a21 | 82 | * @returns |
YSI | 15:b2da6ab01a21 | 83 | */ |
YSI | 14:9e3accc681c4 | 84 | 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 | 85 | 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 | 86 | /** make new Transmission instance |
YSI | 0:2fc6fc3b5e15 | 87 | * connected to |
YSI | 0:2fc6fc3b5e15 | 88 | * |
YSI | 0:2fc6fc3b5e15 | 89 | * @param |
YSI | 0:2fc6fc3b5e15 | 90 | * @param |
YSI | 0:2fc6fc3b5e15 | 91 | */ |
YSI | 14:9e3accc681c4 | 92 | Transmission( |
YSI | 14:9e3accc681c4 | 93 | #if MBED_MAJOR_VERSION > 5 |
YSI | 14:9e3accc681c4 | 94 | UnbufferedSerial *serial, |
YSI | 14:9e3accc681c4 | 95 | #else |
YSI | 14:9e3accc681c4 | 96 | Serial *serial, |
YSI | 14:9e3accc681c4 | 97 | #endif |
YSI | 18:15778c8a97a1 | 98 | USBCDC *usb, |
YSI | 15:b2da6ab01a21 | 99 | EthernetInterface *eth, |
YSI | 18:15778c8a97a1 | 100 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 101 | void (*ethup)(void) = NULL, |
YSI | 18:15778c8a97a1 | 102 | bool caseIgnore = true); |
YSI | 18:15778c8a97a1 | 103 | /** make new Transmission instance |
YSI | 18:15778c8a97a1 | 104 | * connected to |
YSI | 18:15778c8a97a1 | 105 | * |
YSI | 18:15778c8a97a1 | 106 | * @param |
YSI | 18:15778c8a97a1 | 107 | * @param |
YSI | 18:15778c8a97a1 | 108 | */ |
YSI | 18:15778c8a97a1 | 109 | Transmission( |
YSI | 18:15778c8a97a1 | 110 | #if MBED_MAJOR_VERSION > 5 |
YSI | 18:15778c8a97a1 | 111 | UnbufferedSerial *serial, |
YSI | 18:15778c8a97a1 | 112 | #else |
YSI | 18:15778c8a97a1 | 113 | Serial *serial, |
YSI | 18:15778c8a97a1 | 114 | #endif |
YSI | 15:b2da6ab01a21 | 115 | USBCDC *usb, |
YSI | 15:b2da6ab01a21 | 116 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 117 | bool caseIgnore = true); |
YSI | 18:15778c8a97a1 | 118 | /** make new Transmission instance |
YSI | 18:15778c8a97a1 | 119 | * connected to |
YSI | 18:15778c8a97a1 | 120 | * |
YSI | 18:15778c8a97a1 | 121 | * @param |
YSI | 18:15778c8a97a1 | 122 | * @param |
YSI | 18:15778c8a97a1 | 123 | */ |
YSI | 18:15778c8a97a1 | 124 | Transmission( |
YSI | 18:15778c8a97a1 | 125 | #if MBED_MAJOR_VERSION > 5 |
YSI | 18:15778c8a97a1 | 126 | UnbufferedSerial *serial, |
YSI | 18:15778c8a97a1 | 127 | #else |
YSI | 18:15778c8a97a1 | 128 | Serial *serial, |
YSI | 18:15778c8a97a1 | 129 | #endif |
YSI | 18:15778c8a97a1 | 130 | EthernetInterface *eth, |
YSI | 18:15778c8a97a1 | 131 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 132 | void (*ethup)(void) = NULL, |
YSI | 18:15778c8a97a1 | 133 | bool caseIgnore = true); |
YSI | 18:15778c8a97a1 | 134 | /** make new Transmission instance |
YSI | 18:15778c8a97a1 | 135 | * connected to |
YSI | 18:15778c8a97a1 | 136 | * |
YSI | 18:15778c8a97a1 | 137 | * @param |
YSI | 18:15778c8a97a1 | 138 | * @param |
YSI | 18:15778c8a97a1 | 139 | */ |
YSI | 18:15778c8a97a1 | 140 | Transmission( |
YSI | 18:15778c8a97a1 | 141 | USBCDC *usb, |
YSI | 18:15778c8a97a1 | 142 | EthernetInterface *eth, |
YSI | 18:15778c8a97a1 | 143 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 144 | void (*ethup)(void) = NULL, |
YSI | 15:b2da6ab01a21 | 145 | bool caseIgnore = true); |
YSI | 15:b2da6ab01a21 | 146 | /** make new Transmission instance |
YSI | 15:b2da6ab01a21 | 147 | * connected to |
YSI | 15:b2da6ab01a21 | 148 | * |
YSI | 15:b2da6ab01a21 | 149 | * @param |
YSI | 15:b2da6ab01a21 | 150 | * @param |
YSI | 15:b2da6ab01a21 | 151 | */ |
YSI | 15:b2da6ab01a21 | 152 | Transmission( |
YSI | 15:b2da6ab01a21 | 153 | #if MBED_MAJOR_VERSION > 5 |
YSI | 15:b2da6ab01a21 | 154 | UnbufferedSerial *serial, |
YSI | 15:b2da6ab01a21 | 155 | #else |
YSI | 15:b2da6ab01a21 | 156 | Serial *serial, |
YSI | 15:b2da6ab01a21 | 157 | #endif |
YSI | 15:b2da6ab01a21 | 158 | string (*processing)(string), |
YSI | 15:b2da6ab01a21 | 159 | bool caseIgnore = true); |
YSI | 15:b2da6ab01a21 | 160 | /** make new Transmission instance |
YSI | 15:b2da6ab01a21 | 161 | * connected to |
YSI | 15:b2da6ab01a21 | 162 | * |
YSI | 15:b2da6ab01a21 | 163 | * @param |
YSI | 15:b2da6ab01a21 | 164 | * @param |
YSI | 15:b2da6ab01a21 | 165 | */ |
YSI | 15:b2da6ab01a21 | 166 | Transmission( |
YSI | 15:b2da6ab01a21 | 167 | EthernetInterface *eth, |
YSI | 15:b2da6ab01a21 | 168 | string (*processing)(string), |
YSI | 18:15778c8a97a1 | 169 | void (*ethup)(void) = NULL, |
YSI | 15:b2da6ab01a21 | 170 | bool caseIgnore = true); |
YSI | 15:b2da6ab01a21 | 171 | /** make new Transmission instance |
YSI | 15:b2da6ab01a21 | 172 | * connected to |
YSI | 15:b2da6ab01a21 | 173 | * |
YSI | 15:b2da6ab01a21 | 174 | * @param |
YSI | 15:b2da6ab01a21 | 175 | * @param |
YSI | 15:b2da6ab01a21 | 176 | */ |
YSI | 15:b2da6ab01a21 | 177 | Transmission( |
YSI | 15:b2da6ab01a21 | 178 | USBCDC *usb, |
YSI | 15:b2da6ab01a21 | 179 | string (*processing)(string), |
YSI | 15:b2da6ab01a21 | 180 | bool caseIgnore = true); |
YSI | 0:2fc6fc3b5e15 | 181 | /** |
YSI | 0:2fc6fc3b5e15 | 182 | * |
YSI | 0:2fc6fc3b5e15 | 183 | * @param |
YSI | 0:2fc6fc3b5e15 | 184 | * @param |
YSI | 14:9e3accc681c4 | 185 | * @returns |
YSI | 4:9a4ab4f406ab | 186 | */ |
YSI | 21:59c0adcdfe9b | 187 | 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 | 4:9a4ab4f406ab | 188 | /** |
YSI | 4:9a4ab4f406ab | 189 | * |
YSI | 4:9a4ab4f406ab | 190 | * @param |
YSI | 4:9a4ab4f406ab | 191 | * @param |
YSI | 14:9e3accc681c4 | 192 | * @returns |
YSI | 4:9a4ab4f406ab | 193 | */ |
YSI | 21:59c0adcdfe9b | 194 | string ip(string IP=""); |
YSI | 4:9a4ab4f406ab | 195 | /** |
YSI | 4:9a4ab4f406ab | 196 | * |
YSI | 4:9a4ab4f406ab | 197 | * @param |
YSI | 4:9a4ab4f406ab | 198 | * @param |
YSI | 14:9e3accc681c4 | 199 | * @returns |
YSI | 4:9a4ab4f406ab | 200 | */ |
YSI | 11:de94dcd67561 | 201 | enum_trans_status recv(void); |
YSI | 4:9a4ab4f406ab | 202 | /** |
YSI | 4:9a4ab4f406ab | 203 | * |
YSI | 4:9a4ab4f406ab | 204 | * @param |
YSI | 4:9a4ab4f406ab | 205 | * @param |
YSI | 14:9e3accc681c4 | 206 | * @returns |
YSI | 4:9a4ab4f406ab | 207 | */ |
YSI | 19:6c5777719ece | 208 | nsapi_error_t send(const string& BUFFER="", const enum_trans_delivery& DELIVERY=ANY_DELIVERY); |
YSI | 15:b2da6ab01a21 | 209 | /** |
YSI | 15:b2da6ab01a21 | 210 | * |
YSI | 15:b2da6ab01a21 | 211 | * @param |
YSI | 15:b2da6ab01a21 | 212 | * @param |
YSI | 15:b2da6ab01a21 | 213 | * @returns |
YSI | 15:b2da6ab01a21 | 214 | */ |
YSI | 14:9e3accc681c4 | 215 | bool smtp(const char* MAIL, const char* FROM="", const char* SUBJECT="", const char* DATA="", const char* SERVER=TRANSMISSION_DEFAULT_SMTP_SERVER); |
YSI | 4:9a4ab4f406ab | 216 | /** |
YSI | 4:9a4ab4f406ab | 217 | * |
YSI | 4:9a4ab4f406ab | 218 | * @param |
YSI | 4:9a4ab4f406ab | 219 | * @param |
YSI | 14:9e3accc681c4 | 220 | * @returns |
YSI | 4:9a4ab4f406ab | 221 | */ |
YSI | 14:9e3accc681c4 | 222 | time_t ntp(const char* SERVER=TRANSMISSION_DEFAULT_NTP_SERVER); |
YSI | 0:2fc6fc3b5e15 | 223 | |
YSI | 0:2fc6fc3b5e15 | 224 | private: |
YSI | 12:e22ff63d237c | 225 | #if MBED_MAJOR_VERSION > 5 |
YSI | 15:b2da6ab01a21 | 226 | UnbufferedSerial *_serial = NULL; |
YSI | 12:e22ff63d237c | 227 | #else |
YSI | 15:b2da6ab01a21 | 228 | Serial *_serial = NULL; |
YSI | 12:e22ff63d237c | 229 | #endif |
YSI | 14:9e3accc681c4 | 230 | TCPSocket _serverTCP, *_clientTCP = NULL; |
YSI | 21:59c0adcdfe9b | 231 | Thread _eventThread; |
YSI | 21:59c0adcdfe9b | 232 | EventQueue _queue; |
YSI | 15:b2da6ab01a21 | 233 | EthernetInterface *_eth = NULL; |
YSI | 15:b2da6ab01a21 | 234 | USBCDC *_usb = NULL; |
YSI | 15:b2da6ab01a21 | 235 | bool _caseIgnore = false; |
YSI | 14:9e3accc681c4 | 236 | |
YSI | 14:9e3accc681c4 | 237 | void serial_event(void); |
YSI | 0:2fc6fc3b5e15 | 238 | |
YSI | 8:7193327bed34 | 239 | void eth_state(void); |
YSI | 8:7193327bed34 | 240 | bool eth_connect(void); |
YSI | 8:7193327bed34 | 241 | void eth_event(nsapi_event_t, intptr_t); |
YSI | 8:7193327bed34 | 242 | intptr_t eth_status(const string&, const intptr_t&); |
YSI | 8:7193327bed34 | 243 | nsapi_error_t eth_error(const string& SOURCE, const nsapi_error_t& CODE); |
YSI | 8:7193327bed34 | 244 | |
YSI | 8:7193327bed34 | 245 | bool serverTCP_connect(void); |
YSI | 8:7193327bed34 | 246 | void serverTCP_accept(void); |
YSI | 8:7193327bed34 | 247 | void serverTCP_event(void); |
YSI | 8:7193327bed34 | 248 | |
YSI | 18:15778c8a97a1 | 249 | void (*_ethup)(void); |
YSI | 15:b2da6ab01a21 | 250 | string (*_processing)(string); |
YSI | 21:59c0adcdfe9b | 251 | void preprocessing(char *, const enum_trans_delivery); |
YSI | 21:59c0adcdfe9b | 252 | 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 | 253 | message = { RED_DISCONNECTED, false, false, false, "192.168.1.1", 80, "255.255.255.0", "192.168.1.0", 100 }; |
YSI | 8:7193327bed34 | 254 | }; |
YSI | 0:2fc6fc3b5e15 | 255 | #endif |