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
Diff: lib_Transmission.h
- Revision:
- 14:9e3accc681c4
- Parent:
- 13:1e13e40b03c9
- Child:
- 15:b2da6ab01a21
--- a/lib_Transmission.h Tue Dec 15 10:29:38 2020 +0000 +++ b/lib_Transmission.h Fri Feb 12 15:29:54 2021 +0000 @@ -21,90 +21,98 @@ */ #ifndef TRANSMISSION_H #define TRANSMISSION_H +#define NDEBUG #include "mbed.h" #include "EthernetInterface.h" #include <sstream> -#define NDEBUG -#define REQUEST_TIMEOUT 100 // config client bloquante avec timeout sinon limite de transmission a 1072 octets -#define SMTP_SERVER "129.175.212.70" // IP sinon utilisation du DNS avec _eth.getHostByName("smtp.u-psud.fr") -#define NTP_SERVER "129.175.34.43" // IP sinon utilisation du DNS avec _eth.getHostByName("ntp.u-psud.fr") - +#define TRANSMISSION_DEFAULT_BUFFER_SIZE 1072 // taille des buffers de reception serial et tcp +#define TRANSMISSION_DEFAULT_SMTP_SERVER "129.175.212.70" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("smtp.u-psud.fr") +#define TRANSMISSION_DEFAULT_NTP_SERVER "129.175.34.43" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("ntp.u-psud.fr") /** Transmission class */ class Transmission { public: - typedef enum { SERIAL, TCP, HTTP, ANY } enum_trans_to; - typedef enum { WHITE, CYAN, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE } enum_trans_status; + typedef enum { SERIAL, TCP, HTTP, ANY } + enum_trans_to; + typedef enum { WHITE, CYAN, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE } + enum_trans_status; + 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; } + 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" }; /** make new Transmission instance * connected to * * @param * @param */ - #if MBED_MAJOR_VERSION > 5 - Transmission(UnbufferedSerial *serial, EthernetInterface *eth, void(*init)(void), void(*processing)(string, Transmission::enum_trans_to)); - #else - Transmission(Serial *serial, EthernetInterface *eth, void(*init)(void), void(*processing)(string, Transmission::enum_trans_to)); - #endif + Transmission( + #if MBED_MAJOR_VERSION > 5 + UnbufferedSerial *serial, + #else + Serial *serial, + #endif + EthernetInterface *eth, + void(*init)(void), + void(*processing)(string, Transmission::enum_trans_to)); /** * * @param * @param - * @returns none + * @returns */ - string set(bool SET, const char* IP="", uint16_t PORT=80); + string ip(const bool& SET, const char* IP="", const uint16_t& PORT=80, const uint16_t& TIMEOUT=100); /** * * @param * @param - * @returns none + * @returns */ - string get(void); + string ip(void); /** * * @param * @param - * @returns none + * @returns */ enum_trans_status recv(void); /** * * @param * @param - * @returns none + * @returns */ - nsapi_error_t send(const string& BUFFER, const enum_trans_to& TO); + nsapi_error_t send(const string& BUFFER="", const enum_trans_to& TO=ANY); /** * * @param * @param - * @returns none + * @returns */ - bool smtp(const char* MAIL, const char* FROM="", const char* SUBJECT="", const char* DATA=""); + bool smtp(const char* MAIL, const char* FROM="", const char* SUBJECT="", const char* DATA="", const char* SERVER=TRANSMISSION_DEFAULT_SMTP_SERVER); /** * * @param * @param - * @returns none + * @returns */ - time_t ntp(const char* ADDRESS=""); + time_t ntp(const char* SERVER=TRANSMISSION_DEFAULT_NTP_SERVER); private: - Thread _queueThread; - EventQueue _queue; #if MBED_MAJOR_VERSION > 5 UnbufferedSerial *_serial; #else Serial *_serial; #endif + TCPSocket _serverTCP, *_clientTCP = NULL; + EventQueue *_queue; EthernetInterface *_eth; - TCPSocket *_clientTCP = NULL; - TCPSocket _serverTCP; + + /* Serial */ + void serial_event(void); /* EthernetInterface */ void eth_state(void); @@ -121,6 +129,7 @@ /* Transmission */ void (*_init)(void); void (*_processing)(string, enum_trans_to); - struct typeTRANSMISSION { string serial; enum_trans_status status; bool SET; bool BREAK; bool DHCP; bool CONNECT; string IP; uint16_t PORT; } message = { "", RED_DISCONNECTED, false, false, false, false, "", 80 }; + struct { enum_trans_status status; bool SET; bool BREAK; bool DHCP; bool CONNECT; string IP; uint16_t TIMEOUT; uint16_t PORT; } + message = { RED_DISCONNECTED, false, false, false, false, "", 100, 80 }; }; #endif \ No newline at end of file