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@8:7193327bed34, 2020-10-13 (annotated)
- Committer:
- YSI
- Date:
- Tue Oct 13 12:08:04 2020 +0000
- Revision:
- 8:7193327bed34
- Parent:
- 7:98b12722e9e2
- Child:
- 9:abd4a4944399
add Transmission internaly 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 | 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 | 0:2fc6fc3b5e15 | 24 | |
YSI | 0:2fc6fc3b5e15 | 25 | #include "mbed.h" |
YSI | 0:2fc6fc3b5e15 | 26 | #include "EthernetInterface.h" |
YSI | 0:2fc6fc3b5e15 | 27 | #include <sstream> |
YSI | 0:2fc6fc3b5e15 | 28 | |
YSI | 8:7193327bed34 | 29 | #define NDEBUG |
YSI | 8:7193327bed34 | 30 | #define REQUEST_TIMEOUT 100 // config client bloquante avec timeout sinon limite de transmission a 1072 octets |
YSI | 8:7193327bed34 | 31 | #define SMTP_SERVER "129.175.212.70" // IP sinon utilisation du DNS avec eth.getHostByName("smtp.u-psud.fr") |
YSI | 8:7193327bed34 | 32 | #define NTP_SERVER "129.175.34.43" // IP sinon utilisation du DNS avec eth.getHostByName("ntp.u-psud.fr") |
YSI | 0:2fc6fc3b5e15 | 33 | |
YSI | 4:9a4ab4f406ab | 34 | enum enumTRANSMISSION { TCP, SERIAL, BOTH }; |
YSI | 3:7e15bf0a71f4 | 35 | enum enumTRANSTATUS { WHITE, CYAN, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE }; |
YSI | 0:2fc6fc3b5e15 | 36 | |
YSI | 0:2fc6fc3b5e15 | 37 | /** Transmission class |
YSI | 0:2fc6fc3b5e15 | 38 | */ |
YSI | 0:2fc6fc3b5e15 | 39 | class Transmission |
YSI | 0:2fc6fc3b5e15 | 40 | { |
YSI | 0:2fc6fc3b5e15 | 41 | public: |
YSI | 0:2fc6fc3b5e15 | 42 | /** make new Transmission instance |
YSI | 0:2fc6fc3b5e15 | 43 | * connected to |
YSI | 0:2fc6fc3b5e15 | 44 | * |
YSI | 0:2fc6fc3b5e15 | 45 | * @param |
YSI | 0:2fc6fc3b5e15 | 46 | * @param |
YSI | 0:2fc6fc3b5e15 | 47 | */ |
YSI | 8:7193327bed34 | 48 | Transmission(UnbufferedSerial *serial, EthernetInterface *eth, void(*init)(void), void(*processing)(string, const enumTRANSMISSION&)); |
YSI | 0:2fc6fc3b5e15 | 49 | |
YSI | 0:2fc6fc3b5e15 | 50 | /** |
YSI | 0:2fc6fc3b5e15 | 51 | * |
YSI | 0:2fc6fc3b5e15 | 52 | * @param |
YSI | 0:2fc6fc3b5e15 | 53 | * @param |
YSI | 0:2fc6fc3b5e15 | 54 | * @returns none |
YSI | 4:9a4ab4f406ab | 55 | */ |
YSI | 4:9a4ab4f406ab | 56 | void set(bool TCP, const char* IP="", uint16_t PORT=80); |
YSI | 4:9a4ab4f406ab | 57 | /** |
YSI | 4:9a4ab4f406ab | 58 | * |
YSI | 4:9a4ab4f406ab | 59 | * @param |
YSI | 4:9a4ab4f406ab | 60 | * @param |
YSI | 4:9a4ab4f406ab | 61 | * @returns none |
YSI | 4:9a4ab4f406ab | 62 | */ |
YSI | 4:9a4ab4f406ab | 63 | string get(void); |
YSI | 4:9a4ab4f406ab | 64 | /** |
YSI | 4:9a4ab4f406ab | 65 | * |
YSI | 4:9a4ab4f406ab | 66 | * @param |
YSI | 4:9a4ab4f406ab | 67 | * @param |
YSI | 4:9a4ab4f406ab | 68 | * @returns none |
YSI | 4:9a4ab4f406ab | 69 | */ |
YSI | 4:9a4ab4f406ab | 70 | enumTRANSTATUS recv(void); |
YSI | 4:9a4ab4f406ab | 71 | /** |
YSI | 4:9a4ab4f406ab | 72 | * |
YSI | 4:9a4ab4f406ab | 73 | * @param |
YSI | 4:9a4ab4f406ab | 74 | * @param |
YSI | 4:9a4ab4f406ab | 75 | * @returns none |
YSI | 4:9a4ab4f406ab | 76 | */ |
YSI | 4:9a4ab4f406ab | 77 | nsapi_error_t send(const string& BUFFER, const enumTRANSMISSION& TYPE); |
YSI | 4:9a4ab4f406ab | 78 | /** |
YSI | 4:9a4ab4f406ab | 79 | * |
YSI | 4:9a4ab4f406ab | 80 | * @param |
YSI | 4:9a4ab4f406ab | 81 | * @param |
YSI | 4:9a4ab4f406ab | 82 | * @returns none |
YSI | 4:9a4ab4f406ab | 83 | */ |
YSI | 4:9a4ab4f406ab | 84 | bool smtp(const char* MAIL, const char* FROM="", const char* SUBJECT="", const char* DATA=""); |
YSI | 4:9a4ab4f406ab | 85 | /** |
YSI | 4:9a4ab4f406ab | 86 | * |
YSI | 4:9a4ab4f406ab | 87 | * @param |
YSI | 4:9a4ab4f406ab | 88 | * @param |
YSI | 4:9a4ab4f406ab | 89 | * @returns none |
YSI | 4:9a4ab4f406ab | 90 | */ |
YSI | 6:d6a07fd1548a | 91 | time_t ntp(void); |
YSI | 6:d6a07fd1548a | 92 | /** |
YSI | 6:d6a07fd1548a | 93 | * |
YSI | 6:d6a07fd1548a | 94 | * @param |
YSI | 6:d6a07fd1548a | 95 | * @param |
YSI | 6:d6a07fd1548a | 96 | * @returns none |
YSI | 6:d6a07fd1548a | 97 | */ |
YSI | 4:9a4ab4f406ab | 98 | void http(void); |
YSI | 0:2fc6fc3b5e15 | 99 | |
YSI | 0:2fc6fc3b5e15 | 100 | private: |
YSI | 8:7193327bed34 | 101 | Thread _queueThread; |
YSI | 8:7193327bed34 | 102 | EventQueue _queue; |
YSI | 2:ec88f3f8b619 | 103 | UnbufferedSerial *_serial; |
YSI | 2:ec88f3f8b619 | 104 | EthernetInterface *_eth; |
YSI | 3:7e15bf0a71f4 | 105 | TCPSocket *_clientTCP = NULL; |
YSI | 3:7e15bf0a71f4 | 106 | TCPSocket _serverTCP; |
YSI | 0:2fc6fc3b5e15 | 107 | |
YSI | 4:9a4ab4f406ab | 108 | /* Transmission */ |
YSI | 8:7193327bed34 | 109 | void (*_init)(void); |
YSI | 8:7193327bed34 | 110 | void (*_processing)(string, const enumTRANSMISSION&); |
YSI | 8:7193327bed34 | 111 | |
YSI | 8:7193327bed34 | 112 | /* EthernetInterface */ |
YSI | 8:7193327bed34 | 113 | void eth_state(void); |
YSI | 8:7193327bed34 | 114 | bool eth_connect(void); |
YSI | 8:7193327bed34 | 115 | void eth_event(nsapi_event_t, intptr_t); |
YSI | 8:7193327bed34 | 116 | intptr_t eth_status(const string&, const intptr_t&); |
YSI | 8:7193327bed34 | 117 | nsapi_error_t eth_error(const string& SOURCE, const nsapi_error_t& CODE); |
YSI | 8:7193327bed34 | 118 | |
YSI | 8:7193327bed34 | 119 | /* serverTCP */ |
YSI | 8:7193327bed34 | 120 | bool serverTCP_connect(void); |
YSI | 8:7193327bed34 | 121 | void serverTCP_accept(void); |
YSI | 8:7193327bed34 | 122 | void serverTCP_event(void); |
YSI | 8:7193327bed34 | 123 | |
YSI | 8:7193327bed34 | 124 | struct typeTRANSMISSION { string buffer[2]; enumTRANSTATUS status; bool TCP; bool HTTP; bool BREAK; bool DHCP; bool CONNECT; string IP; uint16_t PORT; } |
YSI | 8:7193327bed34 | 125 | message = { {"", ""}, RED_DISCONNECTED, false, false, false, false, false, "", 80 }; |
YSI | 8:7193327bed34 | 126 | }; |
YSI | 8:7193327bed34 | 127 | |
YSI | 8:7193327bed34 | 128 | #endif/** Transmission class |
YSI | 8:7193327bed34 | 129 | * |
YSI | 8:7193327bed34 | 130 | * @purpose library for Transmission |
YSI | 8:7193327bed34 | 131 | * |
YSI | 8:7193327bed34 | 132 | * Use to Transmission |
YSI | 8:7193327bed34 | 133 | * |
YSI | 8:7193327bed34 | 134 | * Example: |
YSI | 8:7193327bed34 | 135 | * @code |
YSI | 8:7193327bed34 | 136 | * #include "mbed.h" |
YSI | 8:7193327bed34 | 137 | * |
YSI | 8:7193327bed34 | 138 | * int main() |
YSI | 8:7193327bed34 | 139 | * { |
YSI | 8:7193327bed34 | 140 | * while(1) |
YSI | 8:7193327bed34 | 141 | * { |
YSI | 8:7193327bed34 | 142 | * } |
YSI | 8:7193327bed34 | 143 | * } |
YSI | 8:7193327bed34 | 144 | * @endcode |
YSI | 8:7193327bed34 | 145 | * @file lib_Transmission.h |
YSI | 8:7193327bed34 | 146 | * @date Jun 2020 |
YSI | 8:7193327bed34 | 147 | * @author Yannic Simon |
YSI | 8:7193327bed34 | 148 | */ |
YSI | 8:7193327bed34 | 149 | #ifndef TRANSMISSION_H |
YSI | 8:7193327bed34 | 150 | #define TRANSMISSION_H |
YSI | 8:7193327bed34 | 151 | |
YSI | 8:7193327bed34 | 152 | #include "mbed.h" |
YSI | 8:7193327bed34 | 153 | #include "EthernetInterface.h" |
YSI | 8:7193327bed34 | 154 | #include <sstream> |
YSI | 8:7193327bed34 | 155 | |
YSI | 8:7193327bed34 | 156 | #define NDEBUG |
YSI | 8:7193327bed34 | 157 | #define REQUEST_TIMEOUT 100 // config client bloquante avec timeout sinon limite de transmission a 1072 octets |
YSI | 8:7193327bed34 | 158 | #define SMTP_SERVER "129.175.212.70" // IP sinon utilisation du DNS avec eth.getHostByName("smtp.u-psud.fr") |
YSI | 8:7193327bed34 | 159 | #define NTP_SERVER "129.175.34.43" // IP sinon utilisation du DNS avec eth.getHostByName("ntp.u-psud.fr") |
YSI | 8:7193327bed34 | 160 | |
YSI | 8:7193327bed34 | 161 | enum enumTRANSMISSION { TCP, SERIAL, BOTH }; |
YSI | 8:7193327bed34 | 162 | enum enumTRANSTATUS { WHITE, CYAN, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE }; |
YSI | 8:7193327bed34 | 163 | |
YSI | 8:7193327bed34 | 164 | /** Transmission class |
YSI | 8:7193327bed34 | 165 | */ |
YSI | 8:7193327bed34 | 166 | class Transmission |
YSI | 8:7193327bed34 | 167 | { |
YSI | 8:7193327bed34 | 168 | public: |
YSI | 8:7193327bed34 | 169 | /** make new Transmission instance |
YSI | 8:7193327bed34 | 170 | * connected to |
YSI | 8:7193327bed34 | 171 | * |
YSI | 8:7193327bed34 | 172 | * @param |
YSI | 8:7193327bed34 | 173 | * @param |
YSI | 8:7193327bed34 | 174 | */ |
YSI | 8:7193327bed34 | 175 | Transmission(UnbufferedSerial *serial, EthernetInterface *eth, void(*init)(void), void(*processing)(string, const enumTRANSMISSION&)); |
YSI | 8:7193327bed34 | 176 | |
YSI | 8:7193327bed34 | 177 | /** |
YSI | 8:7193327bed34 | 178 | * |
YSI | 8:7193327bed34 | 179 | * @param |
YSI | 8:7193327bed34 | 180 | * @param |
YSI | 8:7193327bed34 | 181 | * @returns none |
YSI | 8:7193327bed34 | 182 | */ |
YSI | 8:7193327bed34 | 183 | void set(bool TCP, const char* IP="", uint16_t PORT=80); |
YSI | 8:7193327bed34 | 184 | /** |
YSI | 8:7193327bed34 | 185 | * |
YSI | 8:7193327bed34 | 186 | * @param |
YSI | 8:7193327bed34 | 187 | * @param |
YSI | 8:7193327bed34 | 188 | * @returns none |
YSI | 8:7193327bed34 | 189 | */ |
YSI | 8:7193327bed34 | 190 | string get(void); |
YSI | 8:7193327bed34 | 191 | /** |
YSI | 8:7193327bed34 | 192 | * |
YSI | 8:7193327bed34 | 193 | * @param |
YSI | 8:7193327bed34 | 194 | * @param |
YSI | 8:7193327bed34 | 195 | * @returns none |
YSI | 8:7193327bed34 | 196 | */ |
YSI | 8:7193327bed34 | 197 | enumTRANSTATUS recv(void); |
YSI | 8:7193327bed34 | 198 | /** |
YSI | 8:7193327bed34 | 199 | * |
YSI | 8:7193327bed34 | 200 | * @param |
YSI | 8:7193327bed34 | 201 | * @param |
YSI | 8:7193327bed34 | 202 | * @returns none |
YSI | 8:7193327bed34 | 203 | */ |
YSI | 8:7193327bed34 | 204 | nsapi_error_t send(const string& BUFFER, const enumTRANSMISSION& TYPE); |
YSI | 8:7193327bed34 | 205 | /** |
YSI | 8:7193327bed34 | 206 | * |
YSI | 8:7193327bed34 | 207 | * @param |
YSI | 8:7193327bed34 | 208 | * @param |
YSI | 8:7193327bed34 | 209 | * @returns none |
YSI | 8:7193327bed34 | 210 | */ |
YSI | 8:7193327bed34 | 211 | bool smtp(const char* MAIL, const char* FROM="", const char* SUBJECT="", const char* DATA=""); |
YSI | 8:7193327bed34 | 212 | /** |
YSI | 8:7193327bed34 | 213 | * |
YSI | 8:7193327bed34 | 214 | * @param |
YSI | 8:7193327bed34 | 215 | * @param |
YSI | 8:7193327bed34 | 216 | * @returns none |
YSI | 8:7193327bed34 | 217 | */ |
YSI | 8:7193327bed34 | 218 | time_t ntp(void); |
YSI | 8:7193327bed34 | 219 | /** |
YSI | 8:7193327bed34 | 220 | * |
YSI | 8:7193327bed34 | 221 | * @param |
YSI | 8:7193327bed34 | 222 | * @param |
YSI | 8:7193327bed34 | 223 | * @returns none |
YSI | 8:7193327bed34 | 224 | */ |
YSI | 8:7193327bed34 | 225 | void http(void); |
YSI | 8:7193327bed34 | 226 | |
YSI | 8:7193327bed34 | 227 | private: |
YSI | 8:7193327bed34 | 228 | Thread _queueThread; |
YSI | 8:7193327bed34 | 229 | EventQueue _queue; |
YSI | 8:7193327bed34 | 230 | UnbufferedSerial *_serial; |
YSI | 8:7193327bed34 | 231 | EthernetInterface *_eth; |
YSI | 8:7193327bed34 | 232 | TCPSocket *_clientTCP = NULL; |
YSI | 8:7193327bed34 | 233 | TCPSocket _serverTCP; |
YSI | 8:7193327bed34 | 234 | |
YSI | 8:7193327bed34 | 235 | /* Transmission */ |
YSI | 8:7193327bed34 | 236 | void (*_init)(void); |
YSI | 8:7193327bed34 | 237 | void (*_processing)(string, const enumTRANSMISSION&); |
YSI | 0:2fc6fc3b5e15 | 238 | |
YSI | 0:2fc6fc3b5e15 | 239 | /* EthernetInterface */ |
YSI | 4:9a4ab4f406ab | 240 | void eth_state(void); |
YSI | 4:9a4ab4f406ab | 241 | bool eth_connect(void); |
YSI | 4:9a4ab4f406ab | 242 | void eth_event(nsapi_event_t, intptr_t); |
YSI | 4:9a4ab4f406ab | 243 | intptr_t eth_status(const string&, const intptr_t&); |
YSI | 4:9a4ab4f406ab | 244 | nsapi_error_t eth_error(const string& SOURCE, const nsapi_error_t& CODE); |
YSI | 0:2fc6fc3b5e15 | 245 | |
YSI | 0:2fc6fc3b5e15 | 246 | /* serverTCP */ |
YSI | 4:9a4ab4f406ab | 247 | bool serverTCP_connect(void); |
YSI | 4:9a4ab4f406ab | 248 | void serverTCP_accept(void); |
YSI | 4:9a4ab4f406ab | 249 | void serverTCP_event(void); |
YSI | 4:9a4ab4f406ab | 250 | |
YSI | 4:9a4ab4f406ab | 251 | struct typeTRANSMISSION { string buffer[2]; enumTRANSTATUS status; bool TCP; bool HTTP; bool BREAK; bool DHCP; bool CONNECT; string IP; uint16_t PORT; } |
YSI | 4:9a4ab4f406ab | 252 | message = { {"", ""}, RED_DISCONNECTED, false, false, false, false, false, "", 80 }; |
YSI | 0:2fc6fc3b5e15 | 253 | }; |
YSI | 0:2fc6fc3b5e15 | 254 | |
YSI | 0:2fc6fc3b5e15 | 255 | #endif |