Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: lib_Transmission_Serial_example 2022_TICE_Electrolyse lib_Transmission_TCP_example
lib_Transmission.h
00001 /** Transmission class 00002 * 00003 * @purpose library for Transmission 00004 * 00005 * Use to Transmission 00006 * 00007 * Example: 00008 * @code 00009 * #define MBED_PROJECT "Transmission" 00010 * 00011 * #include "lib_Transmission.h" 00012 * 00013 * #if MBED_MAJOR_VERSION > 5 00014 * UnbufferedSerial pc(USBTX, USBRX, 230400); 00015 * #else 00016 * Serial pc(USBTX, USBRX, 230400); 00017 * #endif 00018 * 00019 * string transmission_processing(string); 00020 * Transmission transmission(&pc, &transmission_processing); 00021 * 00022 * int main(void) 00023 * { 00024 * while(1) ThisThread::sleep_for(200ms); 00025 * } 00026 * 00027 * string transmission_processing(string cmd) 00028 * { 00029 * ostringstream ssend; 00030 * ssend << fixed; 00031 * ssend.precision(2); 00032 * if(cmd.empty()); 00033 * else if(cmd == "*IDN?") 00034 * ssend << MBED_PROJECT << ", Mbed OS " << MBED_VERSION << ", Version dated, " << __DATE__ << ", " << __TIME__; 00035 * else if(cmd[cmd.size()-1] == '?') 00036 * ssend << "incorrect requeste [" << cmd << "]"; 00037 * return ssend.str(); 00038 * } 00039 * @endcode 00040 * @file lib_Transmission.h 00041 * @date Jun 2020 00042 * @author Yannic Simon 00043 */ 00044 #ifndef TRANSMISSION_H 00045 #define TRANSMISSION_H 00046 #define NDEBUG 00047 00048 #include <sstream> 00049 #include "mbed.h" 00050 #include "USBCDC.h" 00051 #include "EthernetInterface.h" 00052 00053 #define TRANSMISSION_BUFFER_SIZE 1072 // taille des buffers de reception 00054 #define TRANSMISSION_THREAD_SIZE OS_STACK_SIZE // taille du thread transmission 00055 #define TRANSMISSION_SMTP_SERVER "129.175.212.70" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("smtp.u-psud.fr") 00056 #define TRANSMISSION_NTP_SERVER "129.175.34.43" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("ntp.u-psud.fr") 00057 00058 /** Transmission class 00059 */ 00060 class Transmission 00061 { 00062 public: 00063 /** enumerator of the different possible delivery of the transmission library 00064 */ 00065 typedef enum { USB_DELIVERY, SERIAL_DELIVERY, TCP_DELIVERY, HTTP_DELIVERY, ANY_DELIVERY } 00066 enum_trans_delivery; 00067 /** enumerator of the different ethernet connexion status of the transmission library 00068 */ 00069 typedef enum { WHITE_STATUS, CYAN_STATUS, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE } 00070 enum_trans_status; 00071 /** List of http headers returns 00072 */ 00073 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; } 00074 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" }; 00075 /** make new Transmission instance 00076 * 00077 * @param 00078 */ 00079 Transmission( 00080 #if MBED_MAJOR_VERSION > 5 00081 UnbufferedSerial *serial, 00082 #else 00083 Serial *serial, 00084 #endif 00085 USBCDC *usb, 00086 EthernetInterface *eth, 00087 string (*processing)(string) = NULL, 00088 void (*ethup)(void) = NULL, 00089 bool caseIgnore = true); 00090 /** make new Transmission instance 00091 * 00092 * @param 00093 */ 00094 Transmission( 00095 #if MBED_MAJOR_VERSION > 5 00096 UnbufferedSerial *serial, 00097 #else 00098 Serial *serial, 00099 #endif 00100 USBCDC *usb, 00101 string (*processing)(string) = NULL, 00102 bool caseIgnore = true); 00103 /** make new Transmission instance 00104 * 00105 * @param 00106 */ 00107 Transmission( 00108 #if MBED_MAJOR_VERSION > 5 00109 UnbufferedSerial *serial, 00110 #else 00111 Serial *serial, 00112 #endif 00113 EthernetInterface *eth, 00114 string (*processing)(string) = NULL, 00115 void (*ethup)(void) = NULL, 00116 bool caseIgnore = true); 00117 /** make new Transmission instance 00118 * 00119 * @param 00120 */ 00121 Transmission( 00122 USBCDC *usb, 00123 EthernetInterface *eth, 00124 string (*processing)(string) = NULL, 00125 void (*ethup)(void) = NULL, 00126 bool caseIgnore = true); 00127 /** make new Transmission instance 00128 * 00129 * @param 00130 */ 00131 Transmission( 00132 #if MBED_MAJOR_VERSION > 5 00133 UnbufferedSerial *serial, 00134 #else 00135 Serial *serial, 00136 #endif 00137 string (*processing)(string) = NULL, 00138 bool caseIgnore = true); 00139 /** make new Transmission instance 00140 * 00141 * @param 00142 */ 00143 Transmission( 00144 EthernetInterface *eth, 00145 string (*processing)(string) = NULL, 00146 void (*ethup)(void) = NULL, 00147 bool caseIgnore = true); 00148 /** make new Transmission instance 00149 * 00150 * @param 00151 */ 00152 Transmission( 00153 USBCDC *usb, 00154 string (*processing)(string) = NULL, 00155 bool caseIgnore = true); 00156 /** Configure the TCP connection 00157 * 00158 * @param set enable for the tcp connection 00159 * @returns MAC adress 00160 */ 00161 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); 00162 /** Return ip config 00163 * 00164 * @param ip="" If the specified ip is the one configured gives the complete ip configuration 00165 * @returns ip config 00166 */ 00167 string ip(string ip=""); 00168 /** Return client config 00169 * 00170 * @returns ip:port config 00171 */ 00172 string client(void); 00173 /** scans the reception buffers of transmission TCP and USB 00174 * 00175 * @returns enumerator of the different ethernet connexion status of the transmission library 00176 */ 00177 enum_trans_status recv(void); 00178 /** send the buffer to the specified transmission delivery 00179 * 00180 * @param buffer sent over transmission 00181 * @param delivery of the transmission 00182 * @returns 00183 */ 00184 nsapi_error_t send(const string& buffer="", const enum_trans_delivery& delivery=ANY_DELIVERY); 00185 /** send a request to a server at the specified port 00186 * 00187 * @param request sent to server 00188 * @param server ip 00189 * @param server port 00190 * @returns server response 00191 */ 00192 string get(const string& buffer, const string& server, const int& port=80); 00193 /** send an email to an smtp server 00194 * 00195 * @param mail is the recipient's email 00196 * @param from="" this is the sender's email 00197 * @param subject="" this is the subject of the email 00198 * @param data="" this is the content of the email 00199 * @param server="129.175.212.70" this is an ip from an smtp server 00200 * @returns indicates if the smtp transaction was successful 00201 */ 00202 bool smtp(string mail, string from="", string subject="", string data="", const char* server=TRANSMISSION_SMTP_SERVER); 00203 /** time request to an ntp server 00204 * 00205 * @param server="129.175.34.43" this is an ip from an ntp server 00206 * @returns time 00207 */ 00208 time_t ntp(const char* server=TRANSMISSION_NTP_SERVER); 00209 00210 private: 00211 #if MBED_MAJOR_VERSION > 5 00212 UnbufferedSerial *_serial = NULL; 00213 #else 00214 Serial *_serial = NULL; 00215 #endif 00216 TCPSocket _serverTCP, *_clientTCP = NULL; 00217 Thread *_evenThread = NULL; 00218 EventQueue _queue; 00219 EthernetInterface *_eth = NULL; 00220 USBCDC *_usb = NULL; 00221 bool _caseIgnore = false; 00222 00223 void serial_event(void); 00224 00225 void eth_state(void); 00226 bool eth_connect(void); 00227 void eth_event(nsapi_event_t, intptr_t); 00228 intptr_t eth_status(const string&, const intptr_t&); 00229 nsapi_error_t eth_error(const string& SOURCE, const nsapi_error_t& CODE); 00230 00231 bool serverTCP_connect(void); 00232 void serverTCP_accept(void); 00233 void serverTCP_event(void); 00234 00235 void (*_ethup)(void); 00236 string (*_processing)(string); 00237 void preprocessing(char *, const enum_trans_delivery); 00238 struct { enum_trans_status status; bool SET; bool DHCP; bool CONNECT; string IP; uint16_t PORT; string MASK; string GATEWAY; uint16_t TIMEOUT; } 00239 message = { RED_DISCONNECTED, false, false, false, "192.168.1.1", 80, "255.255.255.0", "192.168.1.0", 100 }; 00240 }; 00241 #endif
Generated on Wed Jul 13 2022 20:40:22 by
1.7.2