Mitesh Patel / Mbed 2 deprecated camera_online_server

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPLinkStatus.h Source File

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 
00018 #include "HTTPServer.h"
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                 _first = false;
00128                 if (_do_urlfile) {
00129                     // Create a link file to our IP.
00130                     FILE *fp = fopen("/local/Start.url", "w");  // Create a link to own IP
00131                     if (fp) {
00132                         fprintf(fp, "[InternetShortcut]\r\nURL=http://%hhu.%hhu.%hhu.%hhu/\r\n",
00133                           (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF);
00134                         fclose(fp);
00135                     }
00136                 }
00137                 _our_ip = ip.addr;
00138             }
00139         }
00140         else {
00141             if (_do_link_printf && _linkstate != new_linkstate) {
00142               printf("IP: <link down>\r\n");
00143             }
00144         }
00145         _link_led  = new_linkstate;
00146         _linkstate = new_linkstate;
00147     }
00148 
00149     virtual HTTPHandle action(HTTPConnection *con) const {
00150       _act_led = 1;
00151 //      struct ip_addr ip = con->_pcb()->remote_ip;
00152       struct ip_addr ip = con->get_remote_ip();    // This requires a patch to TCPConnection.h file in lwip/Core
00153       if (_do_log_printf) {
00154         printf("HTTPStatus IP: %hhu.%hhu.%hhu.%hhu %s %s\r\n",
00155           (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF, 
00156           (con->getType() == POST? "POST" : "GET "), con->getURL()
00157         );
00158       }
00159       if (_log_file) {
00160         FILE *fp = fopen(_log_file, "a");
00161         if (fp) {
00162             fprintf(fp, "HTTPStatus IP: %hhu.%hhu.%hhu.%hhu %s %s\r\n",
00163               (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF, 
00164               (con->getType() == POST? "POST" : "GET "), con->getURL()
00165             );
00166             fclose(fp);
00167         }
00168       }
00169       _act_led.write(0, 0.050);    // Delayed write
00170       return HTTP_AddFields;
00171     }
00172     
00173     DigitalOut _link_led;           // Link status LED
00174     mutable DigitalOutDelayed _act_led;    // Link activity LED. Need "mutable" keyword to let it be changed from within action() const function.
00175     Ticker _ticker;                 // State polling timer
00176     int _linkstate;                 // Last state of eth link
00177     bool _first;                    // Avoid duplicate IP report on the very first pass
00178     unsigned int _our_ip;           // Our last IP address (used for _do_urlfile)
00179     bool _do_urlfile;               // True for creating url (link) to self on local file system /local/Start.url
00180     bool _do_link_printf;           // True for printing to stdout 
00181     bool _do_log_printf;            // True for printing activity log to stdout 
00182     const char *_log_file;          // Optional file for activity logging
00183 };
00184 
00185 #endif