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@14:9e3accc681c4, 2021-02-12 (annotated)
- Committer:
- YSI
- Date:
- Fri Feb 12 15:29:54 2021 +0000
- Revision:
- 14:9e3accc681c4
- Parent:
- 13:1e13e40b03c9
- Child:
- 15:b2da6ab01a21
improve connecting
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 | 0:2fc6fc3b5e15 | 27 | #include "EthernetInterface.h" |
YSI | 0:2fc6fc3b5e15 | 28 | #include <sstream> |
YSI | 0:2fc6fc3b5e15 | 29 | |
YSI | 14:9e3accc681c4 | 30 | #define TRANSMISSION_DEFAULT_BUFFER_SIZE 1072 // taille des buffers de reception serial et tcp |
YSI | 14:9e3accc681c4 | 31 | #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 | 32 | #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 | 33 | |
YSI | 0:2fc6fc3b5e15 | 34 | /** Transmission class |
YSI | 0:2fc6fc3b5e15 | 35 | */ |
YSI | 0:2fc6fc3b5e15 | 36 | class Transmission |
YSI | 0:2fc6fc3b5e15 | 37 | { |
YSI | 0:2fc6fc3b5e15 | 38 | public: |
YSI | 14:9e3accc681c4 | 39 | typedef enum { SERIAL, TCP, HTTP, ANY } |
YSI | 14:9e3accc681c4 | 40 | enum_trans_to; |
YSI | 14:9e3accc681c4 | 41 | typedef enum { WHITE, CYAN, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE } |
YSI | 14:9e3accc681c4 | 42 | enum_trans_status; |
YSI | 14:9e3accc681c4 | 43 | 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 | 44 | 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 | 45 | /** make new Transmission instance |
YSI | 0:2fc6fc3b5e15 | 46 | * connected to |
YSI | 0:2fc6fc3b5e15 | 47 | * |
YSI | 0:2fc6fc3b5e15 | 48 | * @param |
YSI | 0:2fc6fc3b5e15 | 49 | * @param |
YSI | 0:2fc6fc3b5e15 | 50 | */ |
YSI | 14:9e3accc681c4 | 51 | Transmission( |
YSI | 14:9e3accc681c4 | 52 | #if MBED_MAJOR_VERSION > 5 |
YSI | 14:9e3accc681c4 | 53 | UnbufferedSerial *serial, |
YSI | 14:9e3accc681c4 | 54 | #else |
YSI | 14:9e3accc681c4 | 55 | Serial *serial, |
YSI | 14:9e3accc681c4 | 56 | #endif |
YSI | 14:9e3accc681c4 | 57 | EthernetInterface *eth, |
YSI | 14:9e3accc681c4 | 58 | void(*init)(void), |
YSI | 14:9e3accc681c4 | 59 | void(*processing)(string, Transmission::enum_trans_to)); |
YSI | 0:2fc6fc3b5e15 | 60 | |
YSI | 0:2fc6fc3b5e15 | 61 | /** |
YSI | 0:2fc6fc3b5e15 | 62 | * |
YSI | 0:2fc6fc3b5e15 | 63 | * @param |
YSI | 0:2fc6fc3b5e15 | 64 | * @param |
YSI | 14:9e3accc681c4 | 65 | * @returns |
YSI | 4:9a4ab4f406ab | 66 | */ |
YSI | 14:9e3accc681c4 | 67 | string ip(const bool& SET, const char* IP="", const uint16_t& PORT=80, const uint16_t& TIMEOUT=100); |
YSI | 4:9a4ab4f406ab | 68 | /** |
YSI | 4:9a4ab4f406ab | 69 | * |
YSI | 4:9a4ab4f406ab | 70 | * @param |
YSI | 4:9a4ab4f406ab | 71 | * @param |
YSI | 14:9e3accc681c4 | 72 | * @returns |
YSI | 4:9a4ab4f406ab | 73 | */ |
YSI | 14:9e3accc681c4 | 74 | string ip(void); |
YSI | 4:9a4ab4f406ab | 75 | /** |
YSI | 4:9a4ab4f406ab | 76 | * |
YSI | 4:9a4ab4f406ab | 77 | * @param |
YSI | 4:9a4ab4f406ab | 78 | * @param |
YSI | 14:9e3accc681c4 | 79 | * @returns |
YSI | 4:9a4ab4f406ab | 80 | */ |
YSI | 11:de94dcd67561 | 81 | enum_trans_status recv(void); |
YSI | 4:9a4ab4f406ab | 82 | /** |
YSI | 4:9a4ab4f406ab | 83 | * |
YSI | 4:9a4ab4f406ab | 84 | * @param |
YSI | 4:9a4ab4f406ab | 85 | * @param |
YSI | 14:9e3accc681c4 | 86 | * @returns |
YSI | 4:9a4ab4f406ab | 87 | */ |
YSI | 14:9e3accc681c4 | 88 | nsapi_error_t send(const string& BUFFER="", const enum_trans_to& TO=ANY); |
YSI | 4:9a4ab4f406ab | 89 | /** |
YSI | 4:9a4ab4f406ab | 90 | * |
YSI | 4:9a4ab4f406ab | 91 | * @param |
YSI | 4:9a4ab4f406ab | 92 | * @param |
YSI | 14:9e3accc681c4 | 93 | * @returns |
YSI | 4:9a4ab4f406ab | 94 | */ |
YSI | 14:9e3accc681c4 | 95 | bool smtp(const char* MAIL, const char* FROM="", const char* SUBJECT="", const char* DATA="", const char* SERVER=TRANSMISSION_DEFAULT_SMTP_SERVER); |
YSI | 4:9a4ab4f406ab | 96 | /** |
YSI | 4:9a4ab4f406ab | 97 | * |
YSI | 4:9a4ab4f406ab | 98 | * @param |
YSI | 4:9a4ab4f406ab | 99 | * @param |
YSI | 14:9e3accc681c4 | 100 | * @returns |
YSI | 4:9a4ab4f406ab | 101 | */ |
YSI | 14:9e3accc681c4 | 102 | time_t ntp(const char* SERVER=TRANSMISSION_DEFAULT_NTP_SERVER); |
YSI | 0:2fc6fc3b5e15 | 103 | |
YSI | 0:2fc6fc3b5e15 | 104 | private: |
YSI | 12:e22ff63d237c | 105 | #if MBED_MAJOR_VERSION > 5 |
YSI | 2:ec88f3f8b619 | 106 | UnbufferedSerial *_serial; |
YSI | 12:e22ff63d237c | 107 | #else |
YSI | 12:e22ff63d237c | 108 | Serial *_serial; |
YSI | 12:e22ff63d237c | 109 | #endif |
YSI | 14:9e3accc681c4 | 110 | TCPSocket _serverTCP, *_clientTCP = NULL; |
YSI | 14:9e3accc681c4 | 111 | EventQueue *_queue; |
YSI | 2:ec88f3f8b619 | 112 | EthernetInterface *_eth; |
YSI | 14:9e3accc681c4 | 113 | |
YSI | 14:9e3accc681c4 | 114 | /* Serial */ |
YSI | 14:9e3accc681c4 | 115 | void serial_event(void); |
YSI | 0:2fc6fc3b5e15 | 116 | |
YSI | 8:7193327bed34 | 117 | /* EthernetInterface */ |
YSI | 8:7193327bed34 | 118 | void eth_state(void); |
YSI | 8:7193327bed34 | 119 | bool eth_connect(void); |
YSI | 8:7193327bed34 | 120 | void eth_event(nsapi_event_t, intptr_t); |
YSI | 8:7193327bed34 | 121 | intptr_t eth_status(const string&, const intptr_t&); |
YSI | 8:7193327bed34 | 122 | nsapi_error_t eth_error(const string& SOURCE, const nsapi_error_t& CODE); |
YSI | 8:7193327bed34 | 123 | |
YSI | 8:7193327bed34 | 124 | /* serverTCP */ |
YSI | 8:7193327bed34 | 125 | bool serverTCP_connect(void); |
YSI | 8:7193327bed34 | 126 | void serverTCP_accept(void); |
YSI | 8:7193327bed34 | 127 | void serverTCP_event(void); |
YSI | 8:7193327bed34 | 128 | |
YSI | 10:25e049353db5 | 129 | /* Transmission */ |
YSI | 10:25e049353db5 | 130 | void (*_init)(void); |
YSI | 11:de94dcd67561 | 131 | void (*_processing)(string, enum_trans_to); |
YSI | 14:9e3accc681c4 | 132 | struct { enum_trans_status status; bool SET; bool BREAK; bool DHCP; bool CONNECT; string IP; uint16_t TIMEOUT; uint16_t PORT; } |
YSI | 14:9e3accc681c4 | 133 | message = { RED_DISCONNECTED, false, false, false, false, "", 100, 80 }; |
YSI | 8:7193327bed34 | 134 | }; |
YSI | 0:2fc6fc3b5e15 | 135 | #endif |