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