tesbed webserver sur SD card ( htm lile) usb serial LCD + analog in and led out from mbed
Embed:
(wiki syntax)
Show/hide line numbers
HTTPLinkStatus.h
00001 // HTTPLinkStatus.h 00002 // HTTP Server Logger class 00003 // 00004 // Provides: 00005 // 1. ETH port link state monitor 00006 // - lights LED for link 00007 // - default: logs link state to stdout; 00008 // - default: creates URL file on local filesystem (can be used to open browser to URL address) 00009 // 2. HTTP activity monitor (does not react to non-http traffic) 00010 // - blinks LED for activity / HTTP requests 00011 // - optional: log activity to stdout 00012 // - optional: log activity to file (file is opened for each log entry, so local file system can still be accessed via USB) 00013 // 00014 00015 #ifndef HTTPLINKSTATUS_H 00016 #define HTTPLINKSTATUS_H 00017 #include "HTTPServer.h" 00018 00019 00020 // FIXME: should we be able to get this from *con? 00021 extern Ethernet eth; // eth is defined elsewhere, avoid compiler error. 00022 00023 namespace mbed { 00024 00025 class DigitalOutDelayed : public DigitalOut { 00026 public: 00027 DigitalOutDelayed(PinName pin, const char* name = NULL) : DigitalOut(pin, name) {} 00028 void write(int value, float delay=0.0) { 00029 _timeout.detach(); 00030 if (delay > 0.0) { 00031 _delay_value = value; 00032 _timeout.attach(this, &DigitalOutDelayed::_write_delayed, delay); 00033 } else { 00034 DigitalOut::write(value); 00035 } 00036 } 00037 void write_us(int value, unsigned int delay_us=0) { 00038 _timeout.detach(); 00039 if (delay_us > 0) { 00040 _delay_value = value; 00041 _timeout.attach_us(this, &DigitalOutDelayed::_write_delayed, delay_us); 00042 } else { 00043 DigitalOut::write(value); 00044 } 00045 } 00046 #ifdef MBED_OPERATORS 00047 using DigitalOut::operator = ; 00048 using DigitalOut::operator int; 00049 #endif 00050 00051 protected: 00052 void _write_delayed(void) { DigitalOut::write(_delay_value); } 00053 Timeout _timeout; 00054 int _delay_value; 00055 }; 00056 00057 } // namespace mbed 00058 00059 class HTTPLinkStatus : public HTTPHandler { 00060 public: 00061 HTTPLinkStatus(const char *prefix, PinName link_led=NC, PinName act_led=NC, float poll_t = 0.1, 00062 bool do_urlfile = true, bool do_link_printf = true, bool do_log_printf = false, const char* log_file = NULL 00063 ) : 00064 HTTPHandler(prefix), 00065 _link_led(link_led), 00066 _act_led(act_led), 00067 _linkstate(-1), 00068 _first(true), 00069 _our_ip(0), 00070 _do_urlfile(do_urlfile), 00071 _do_link_printf(do_link_printf), 00072 _do_log_printf(do_log_printf), 00073 _log_file(log_file) 00074 { 00075 if (poll_t > 0) _ticker.attach(this, &HTTPLinkStatus::_link_poll, poll_t); 00076 } 00077 HTTPLinkStatus(HTTPServer *server, const char *prefix, PinName link_led=NC, PinName act_led=NC, float poll_t = 0.1, 00078 bool do_urlfile = true, bool do_link_printf = true, bool do_log_printf = false, const char* log_file = NULL 00079 ) : 00080 HTTPHandler(prefix), 00081 _link_led(link_led), 00082 _act_led(act_led), 00083 _linkstate(-1), 00084 _first(true), 00085 _our_ip(0), 00086 _do_urlfile(do_urlfile), 00087 _do_link_printf(do_link_printf), 00088 _do_log_printf(do_log_printf), 00089 _log_file(log_file) 00090 { 00091 server->addHandler(this); 00092 if (poll_t > 0) _ticker.attach(this, &HTTPLinkStatus::_link_poll, poll_t); 00093 } 00094 virtual ~HTTPLinkStatus() { 00095 _ticker.detach(); // Needeed? 00096 } 00097 void set_do_urlfile(bool val) { _do_urlfile = val; } 00098 void set_link_stdout(bool val) { _do_link_printf = val; } 00099 void set_log_stdout(bool val) { _do_log_printf = val; } 00100 void set_log_file(const char *file) { 00101 _log_file = file; 00102 if (_log_file) { 00103 FILE *fp = fopen(_log_file, "a"); 00104 if (fp) { 00105 fprintf(fp, "======== HTTPLinkStatus NEW LOG OPENED ========\r\n"); 00106 fclose(fp); 00107 } else { 00108 _log_file = NULL; // Error opening file. Reset file name so we won't waste our time trying to log to it. 00109 } 00110 } 00111 } 00112 private: 00113 void _link_poll() { 00114 int new_linkstate = eth.link(); 00115 if (new_linkstate) { 00116 // From http://mbed.org/forum/post/909/ 00117 NetServer *net = NetServer::get(); 00118 struct ip_addr ip = net->getIPAddr(); 00119 // struct ip_addr gw = net->getGateway(); 00120 // struct ip_addr nm = net->getNetmask(); 00121 // struct ip_addr dns = net->getDNS1(); 00122 if (ip.addr != _our_ip) { 00123 if (!_first &&_do_link_printf) { 00124 printf("IP: %hhu.%hhu.%hhu.%hhu\r\n", 00125 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF); 00126 00127 } 00128 00129 _first = false; 00130 if (_do_urlfile) { 00131 // Create a link file to our IP. 00132 FILE *fp = fopen("/local/Start.url", "w"); // Create a link to own IP 00133 if (fp) { 00134 fprintf(fp, "[InternetShortcut]\r\nURL=http://%hhu.%hhu.%hhu.%hhu/\r\n", 00135 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF); 00136 00137 fclose(fp); 00138 } 00139 } 00140 _our_ip = ip.addr; 00141 } 00142 } 00143 else { 00144 if (_do_link_printf && _linkstate != new_linkstate) { 00145 printf("IP: <link down>\r\n"); 00146 } 00147 } 00148 _link_led = new_linkstate; 00149 _linkstate = new_linkstate; 00150 } 00151 00152 virtual HTTPHandle action(HTTPConnection *con) const { 00153 _act_led = 1; 00154 00155 // struct ip_addr ip = con->_pcb()->remote_ip; 00156 struct ip_addr ip = con->get_remote_ip(); // This requires a patch to TCPConnection.h file in lwip/Core 00157 if (_do_log_printf) { 00158 printf("HTTPStatus IP: %hhu.%hhu.%hhu.%hhu %s %s\r\n", 00159 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF, 00160 (con->getType() == POST? "POST" : "GET "), con->getURL() 00161 ); 00162 } 00163 if (_log_file) { 00164 FILE *fp = fopen(_log_file, "a"); 00165 if (fp) { 00166 fprintf(fp, "HTTPStatus IP: %hhu.%hhu.%hhu.%hhu %s %s\r\n", 00167 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF, 00168 (con->getType() == POST? "POST" : "GET "), con->getURL() 00169 ); 00170 fclose(fp); 00171 } 00172 } 00173 _act_led.write(0, 0.050); // Delayed write 00174 return HTTP_AddFields; 00175 } 00176 00177 DigitalOut _link_led; // Link status LED 00178 mutable DigitalOutDelayed _act_led; // Link activity LED. Need "mutable" keyword to let it be changed from within action() const function. 00179 Ticker _ticker; // State polling timer 00180 int _linkstate; // Last state of eth link 00181 bool _first; // Avoid duplicate IP report on the very first pass 00182 unsigned int _our_ip; // Our last IP address (used for _do_urlfile) 00183 bool _do_urlfile; // True for creating url (link) to self on local file system /local/Start.url 00184 bool _do_link_printf; // True for printing to stdout 00185 bool _do_log_printf; // True for printing activity log to stdout 00186 const char *_log_file; // Optional file for activity logging 00187 }; 00188 00189 #endif
Generated on Tue Jul 12 2022 16:12:19 by
1.7.2