this will take a image from C328 serial camera and store those images in mbed flash as html and this html page is uploaded into the server at ip:192.168.1.2

Dependencies:   mbed

Committer:
mitesh2patel
Date:
Wed Dec 15 15:01:56 2010 +0000
Revision:
0:e1a0471e5ffb

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mitesh2patel 0:e1a0471e5ffb 1 // HTTPLinkStatus.h
mitesh2patel 0:e1a0471e5ffb 2 // HTTP Server Logger class
mitesh2patel 0:e1a0471e5ffb 3 //
mitesh2patel 0:e1a0471e5ffb 4 // Provides:
mitesh2patel 0:e1a0471e5ffb 5 // 1. ETH port link state monitor
mitesh2patel 0:e1a0471e5ffb 6 // - lights LED for link
mitesh2patel 0:e1a0471e5ffb 7 // - default: logs link state to stdout;
mitesh2patel 0:e1a0471e5ffb 8 // - default: creates URL file on local filesystem (can be used to open browser to URL address)
mitesh2patel 0:e1a0471e5ffb 9 // 2. HTTP activity monitor (does not react to non-http traffic)
mitesh2patel 0:e1a0471e5ffb 10 // - blinks LED for activity / HTTP requests
mitesh2patel 0:e1a0471e5ffb 11 // - optional: log activity to stdout
mitesh2patel 0:e1a0471e5ffb 12 // - optional: log activity to file (file is opened for each log entry, so local file system can still be accessed via USB)
mitesh2patel 0:e1a0471e5ffb 13 //
mitesh2patel 0:e1a0471e5ffb 14
mitesh2patel 0:e1a0471e5ffb 15 #ifndef HTTPLINKSTATUS_H
mitesh2patel 0:e1a0471e5ffb 16 #define HTTPLINKSTATUS_H
mitesh2patel 0:e1a0471e5ffb 17
mitesh2patel 0:e1a0471e5ffb 18 #include "HTTPServer.h"
mitesh2patel 0:e1a0471e5ffb 19
mitesh2patel 0:e1a0471e5ffb 20 // FIXME: should we be able to get this from *con?
mitesh2patel 0:e1a0471e5ffb 21 extern Ethernet eth; // eth is defined elsewhere, avoid compiler error.
mitesh2patel 0:e1a0471e5ffb 22
mitesh2patel 0:e1a0471e5ffb 23 namespace mbed {
mitesh2patel 0:e1a0471e5ffb 24
mitesh2patel 0:e1a0471e5ffb 25 class DigitalOutDelayed : public DigitalOut {
mitesh2patel 0:e1a0471e5ffb 26 public:
mitesh2patel 0:e1a0471e5ffb 27 DigitalOutDelayed(PinName pin, const char* name = NULL) : DigitalOut(pin, name) {}
mitesh2patel 0:e1a0471e5ffb 28 void write(int value, float delay=0.0) {
mitesh2patel 0:e1a0471e5ffb 29 _timeout.detach();
mitesh2patel 0:e1a0471e5ffb 30 if (delay > 0.0) {
mitesh2patel 0:e1a0471e5ffb 31 _delay_value = value;
mitesh2patel 0:e1a0471e5ffb 32 _timeout.attach(this, &DigitalOutDelayed::_write_delayed, delay);
mitesh2patel 0:e1a0471e5ffb 33 } else {
mitesh2patel 0:e1a0471e5ffb 34 DigitalOut::write(value);
mitesh2patel 0:e1a0471e5ffb 35 }
mitesh2patel 0:e1a0471e5ffb 36 }
mitesh2patel 0:e1a0471e5ffb 37 void write_us(int value, unsigned int delay_us=0) {
mitesh2patel 0:e1a0471e5ffb 38 _timeout.detach();
mitesh2patel 0:e1a0471e5ffb 39 if (delay_us > 0) {
mitesh2patel 0:e1a0471e5ffb 40 _delay_value = value;
mitesh2patel 0:e1a0471e5ffb 41 _timeout.attach_us(this, &DigitalOutDelayed::_write_delayed, delay_us);
mitesh2patel 0:e1a0471e5ffb 42 } else {
mitesh2patel 0:e1a0471e5ffb 43 DigitalOut::write(value);
mitesh2patel 0:e1a0471e5ffb 44 }
mitesh2patel 0:e1a0471e5ffb 45 }
mitesh2patel 0:e1a0471e5ffb 46 #ifdef MBED_OPERATORS
mitesh2patel 0:e1a0471e5ffb 47 using DigitalOut::operator = ;
mitesh2patel 0:e1a0471e5ffb 48 using DigitalOut::operator int;
mitesh2patel 0:e1a0471e5ffb 49 #endif
mitesh2patel 0:e1a0471e5ffb 50
mitesh2patel 0:e1a0471e5ffb 51 protected:
mitesh2patel 0:e1a0471e5ffb 52 void _write_delayed(void) { DigitalOut::write(_delay_value); }
mitesh2patel 0:e1a0471e5ffb 53 Timeout _timeout;
mitesh2patel 0:e1a0471e5ffb 54 int _delay_value;
mitesh2patel 0:e1a0471e5ffb 55 };
mitesh2patel 0:e1a0471e5ffb 56
mitesh2patel 0:e1a0471e5ffb 57 } // namespace mbed
mitesh2patel 0:e1a0471e5ffb 58
mitesh2patel 0:e1a0471e5ffb 59 class HTTPLinkStatus : public HTTPHandler {
mitesh2patel 0:e1a0471e5ffb 60 public:
mitesh2patel 0:e1a0471e5ffb 61 HTTPLinkStatus(const char *prefix, PinName link_led=NC, PinName act_led=NC, float poll_t = 0.1,
mitesh2patel 0:e1a0471e5ffb 62 bool do_urlfile = true, bool do_link_printf = true, bool do_log_printf = false, const char* log_file = NULL
mitesh2patel 0:e1a0471e5ffb 63 ) :
mitesh2patel 0:e1a0471e5ffb 64 HTTPHandler(prefix),
mitesh2patel 0:e1a0471e5ffb 65 _link_led(link_led),
mitesh2patel 0:e1a0471e5ffb 66 _act_led(act_led),
mitesh2patel 0:e1a0471e5ffb 67 _linkstate(-1),
mitesh2patel 0:e1a0471e5ffb 68 _first(true),
mitesh2patel 0:e1a0471e5ffb 69 _our_ip(0),
mitesh2patel 0:e1a0471e5ffb 70 _do_urlfile(do_urlfile),
mitesh2patel 0:e1a0471e5ffb 71 _do_link_printf(do_link_printf),
mitesh2patel 0:e1a0471e5ffb 72 _do_log_printf(do_log_printf),
mitesh2patel 0:e1a0471e5ffb 73 _log_file(log_file)
mitesh2patel 0:e1a0471e5ffb 74 {
mitesh2patel 0:e1a0471e5ffb 75 if (poll_t > 0) _ticker.attach(this, &HTTPLinkStatus::_link_poll, poll_t);
mitesh2patel 0:e1a0471e5ffb 76 }
mitesh2patel 0:e1a0471e5ffb 77 HTTPLinkStatus(HTTPServer *server, const char *prefix, PinName link_led=NC, PinName act_led=NC, float poll_t = 0.1,
mitesh2patel 0:e1a0471e5ffb 78 bool do_urlfile = true, bool do_link_printf = true, bool do_log_printf = false, const char* log_file = NULL
mitesh2patel 0:e1a0471e5ffb 79 ) :
mitesh2patel 0:e1a0471e5ffb 80 HTTPHandler(prefix),
mitesh2patel 0:e1a0471e5ffb 81 _link_led(link_led),
mitesh2patel 0:e1a0471e5ffb 82 _act_led(act_led),
mitesh2patel 0:e1a0471e5ffb 83 _linkstate(-1),
mitesh2patel 0:e1a0471e5ffb 84 _first(true),
mitesh2patel 0:e1a0471e5ffb 85 _our_ip(0),
mitesh2patel 0:e1a0471e5ffb 86 _do_urlfile(do_urlfile),
mitesh2patel 0:e1a0471e5ffb 87 _do_link_printf(do_link_printf),
mitesh2patel 0:e1a0471e5ffb 88 _do_log_printf(do_log_printf),
mitesh2patel 0:e1a0471e5ffb 89 _log_file(log_file)
mitesh2patel 0:e1a0471e5ffb 90 {
mitesh2patel 0:e1a0471e5ffb 91 server->addHandler(this);
mitesh2patel 0:e1a0471e5ffb 92 if (poll_t > 0) _ticker.attach(this, &HTTPLinkStatus::_link_poll, poll_t);
mitesh2patel 0:e1a0471e5ffb 93 }
mitesh2patel 0:e1a0471e5ffb 94 virtual ~HTTPLinkStatus() {
mitesh2patel 0:e1a0471e5ffb 95 _ticker.detach(); // Needeed?
mitesh2patel 0:e1a0471e5ffb 96 }
mitesh2patel 0:e1a0471e5ffb 97 void set_do_urlfile(bool val) { _do_urlfile = val; }
mitesh2patel 0:e1a0471e5ffb 98 void set_link_stdout(bool val) { _do_link_printf = val; }
mitesh2patel 0:e1a0471e5ffb 99 void set_log_stdout(bool val) { _do_log_printf = val; }
mitesh2patel 0:e1a0471e5ffb 100 void set_log_file(const char *file) {
mitesh2patel 0:e1a0471e5ffb 101 _log_file = file;
mitesh2patel 0:e1a0471e5ffb 102 if (_log_file) {
mitesh2patel 0:e1a0471e5ffb 103 FILE *fp = fopen(_log_file, "a");
mitesh2patel 0:e1a0471e5ffb 104 if (fp) {
mitesh2patel 0:e1a0471e5ffb 105 fprintf(fp, "======== HTTPLinkStatus NEW LOG OPENED ========\r\n");
mitesh2patel 0:e1a0471e5ffb 106 fclose(fp);
mitesh2patel 0:e1a0471e5ffb 107 } else {
mitesh2patel 0:e1a0471e5ffb 108 _log_file = NULL; // Error opening file. Reset file name so we won't waste our time trying to log to it.
mitesh2patel 0:e1a0471e5ffb 109 }
mitesh2patel 0:e1a0471e5ffb 110 }
mitesh2patel 0:e1a0471e5ffb 111 }
mitesh2patel 0:e1a0471e5ffb 112 private:
mitesh2patel 0:e1a0471e5ffb 113 void _link_poll() {
mitesh2patel 0:e1a0471e5ffb 114 int new_linkstate = eth.link();
mitesh2patel 0:e1a0471e5ffb 115 if (new_linkstate) {
mitesh2patel 0:e1a0471e5ffb 116 // From http://mbed.org/forum/post/909/
mitesh2patel 0:e1a0471e5ffb 117 NetServer *net = NetServer::get();
mitesh2patel 0:e1a0471e5ffb 118 struct ip_addr ip = net->getIPAddr();
mitesh2patel 0:e1a0471e5ffb 119 // struct ip_addr gw = net->getGateway();
mitesh2patel 0:e1a0471e5ffb 120 // struct ip_addr nm = net->getNetmask();
mitesh2patel 0:e1a0471e5ffb 121 // struct ip_addr dns = net->getDNS1();
mitesh2patel 0:e1a0471e5ffb 122 if (ip.addr != _our_ip) {
mitesh2patel 0:e1a0471e5ffb 123 if (!_first &&_do_link_printf) {
mitesh2patel 0:e1a0471e5ffb 124 printf("IP: %hhu.%hhu.%hhu.%hhu\r\n",
mitesh2patel 0:e1a0471e5ffb 125 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF);
mitesh2patel 0:e1a0471e5ffb 126 }
mitesh2patel 0:e1a0471e5ffb 127 _first = false;
mitesh2patel 0:e1a0471e5ffb 128 if (_do_urlfile) {
mitesh2patel 0:e1a0471e5ffb 129 // Create a link file to our IP.
mitesh2patel 0:e1a0471e5ffb 130 FILE *fp = fopen("/local/Start.url", "w"); // Create a link to own IP
mitesh2patel 0:e1a0471e5ffb 131 if (fp) {
mitesh2patel 0:e1a0471e5ffb 132 fprintf(fp, "[InternetShortcut]\r\nURL=http://%hhu.%hhu.%hhu.%hhu/\r\n",
mitesh2patel 0:e1a0471e5ffb 133 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF);
mitesh2patel 0:e1a0471e5ffb 134 fclose(fp);
mitesh2patel 0:e1a0471e5ffb 135 }
mitesh2patel 0:e1a0471e5ffb 136 }
mitesh2patel 0:e1a0471e5ffb 137 _our_ip = ip.addr;
mitesh2patel 0:e1a0471e5ffb 138 }
mitesh2patel 0:e1a0471e5ffb 139 }
mitesh2patel 0:e1a0471e5ffb 140 else {
mitesh2patel 0:e1a0471e5ffb 141 if (_do_link_printf && _linkstate != new_linkstate) {
mitesh2patel 0:e1a0471e5ffb 142 printf("IP: <link down>\r\n");
mitesh2patel 0:e1a0471e5ffb 143 }
mitesh2patel 0:e1a0471e5ffb 144 }
mitesh2patel 0:e1a0471e5ffb 145 _link_led = new_linkstate;
mitesh2patel 0:e1a0471e5ffb 146 _linkstate = new_linkstate;
mitesh2patel 0:e1a0471e5ffb 147 }
mitesh2patel 0:e1a0471e5ffb 148
mitesh2patel 0:e1a0471e5ffb 149 virtual HTTPHandle action(HTTPConnection *con) const {
mitesh2patel 0:e1a0471e5ffb 150 _act_led = 1;
mitesh2patel 0:e1a0471e5ffb 151 // struct ip_addr ip = con->_pcb()->remote_ip;
mitesh2patel 0:e1a0471e5ffb 152 struct ip_addr ip = con->get_remote_ip(); // This requires a patch to TCPConnection.h file in lwip/Core
mitesh2patel 0:e1a0471e5ffb 153 if (_do_log_printf) {
mitesh2patel 0:e1a0471e5ffb 154 printf("HTTPStatus IP: %hhu.%hhu.%hhu.%hhu %s %s\r\n",
mitesh2patel 0:e1a0471e5ffb 155 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF,
mitesh2patel 0:e1a0471e5ffb 156 (con->getType() == POST? "POST" : "GET "), con->getURL()
mitesh2patel 0:e1a0471e5ffb 157 );
mitesh2patel 0:e1a0471e5ffb 158 }
mitesh2patel 0:e1a0471e5ffb 159 if (_log_file) {
mitesh2patel 0:e1a0471e5ffb 160 FILE *fp = fopen(_log_file, "a");
mitesh2patel 0:e1a0471e5ffb 161 if (fp) {
mitesh2patel 0:e1a0471e5ffb 162 fprintf(fp, "HTTPStatus IP: %hhu.%hhu.%hhu.%hhu %s %s\r\n",
mitesh2patel 0:e1a0471e5ffb 163 (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF,
mitesh2patel 0:e1a0471e5ffb 164 (con->getType() == POST? "POST" : "GET "), con->getURL()
mitesh2patel 0:e1a0471e5ffb 165 );
mitesh2patel 0:e1a0471e5ffb 166 fclose(fp);
mitesh2patel 0:e1a0471e5ffb 167 }
mitesh2patel 0:e1a0471e5ffb 168 }
mitesh2patel 0:e1a0471e5ffb 169 _act_led.write(0, 0.050); // Delayed write
mitesh2patel 0:e1a0471e5ffb 170 return HTTP_AddFields;
mitesh2patel 0:e1a0471e5ffb 171 }
mitesh2patel 0:e1a0471e5ffb 172
mitesh2patel 0:e1a0471e5ffb 173 DigitalOut _link_led; // Link status LED
mitesh2patel 0:e1a0471e5ffb 174 mutable DigitalOutDelayed _act_led; // Link activity LED. Need "mutable" keyword to let it be changed from within action() const function.
mitesh2patel 0:e1a0471e5ffb 175 Ticker _ticker; // State polling timer
mitesh2patel 0:e1a0471e5ffb 176 int _linkstate; // Last state of eth link
mitesh2patel 0:e1a0471e5ffb 177 bool _first; // Avoid duplicate IP report on the very first pass
mitesh2patel 0:e1a0471e5ffb 178 unsigned int _our_ip; // Our last IP address (used for _do_urlfile)
mitesh2patel 0:e1a0471e5ffb 179 bool _do_urlfile; // True for creating url (link) to self on local file system /local/Start.url
mitesh2patel 0:e1a0471e5ffb 180 bool _do_link_printf; // True for printing to stdout
mitesh2patel 0:e1a0471e5ffb 181 bool _do_log_printf; // True for printing activity log to stdout
mitesh2patel 0:e1a0471e5ffb 182 const char *_log_file; // Optional file for activity logging
mitesh2patel 0:e1a0471e5ffb 183 };
mitesh2patel 0:e1a0471e5ffb 184
mitesh2patel 0:e1a0471e5ffb 185 #endif