uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers httpd-cgi.cpp Source File

httpd-cgi.cpp

Go to the documentation of this file.
00001 /**
00002  * \addtogroup httpd
00003  * @{
00004  */
00005 
00006 /**
00007  * \file
00008  *         Web server script interface
00009  * \author
00010  *         Adam Dunkels <adam@sics.se>
00011  *
00012  */
00013 
00014 /*
00015  * Copyright (c) 2001-2006, Adam Dunkels.
00016  * All rights reserved.
00017  *
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions
00020  * are met:
00021  * 1. Redistributions of source code must retain the above copyright
00022  *    notice, this list of conditions and the following disclaimer.
00023  * 2. Redistributions in binary form must reproduce the above copyright
00024  *    notice, this list of conditions and the following disclaimer in the
00025  *    documentation and/or other materials provided with the distribution.
00026  * 3. The name of the author may not be used to endorse or promote
00027  *    products derived from this software without specific prior
00028  *    written permission.
00029  *
00030  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00031  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00032  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00034  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00036  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00037  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00038  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00039  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00040  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00041  *
00042  * This file is part of the uIP TCP/IP stack.
00043  *
00044  * $Id: httpd-cgi.c,v 1.2 2006/06/11 21:46:37 adam Exp $
00045  *
00046  */
00047 
00048 extern "C" {
00049     #include "uip.h"
00050     #include "psock.h"
00051     #include "httpd.h"
00052 }
00053 #include "httpd-cgi.h"
00054 //#include "httpd-fs.h"
00055 
00056 #include <stdio.h>
00057 #include <string.h>
00058 
00059 #include "TMP102.h"
00060 
00061 TMP102 tmp102(dp5, dp27, 0x90); //A0 pin is connected to ground
00062 
00063 HTTPD_CGI_CALL(file, "file-stats", file_stats);
00064 HTTPD_CGI_CALL(tcp, "tcp-connections", tcp_stats);
00065 HTTPD_CGI_CALL(net, "net-stats", net_stats);
00066 HTTPD_CGI_CALL(tmp, "tmp102-stats", tmp102_stats);
00067 
00068 static const struct httpd_cgi_call *calls[] = { &file, &tcp, &net, &tmp, NULL };
00069 
00070 /*---------------------------------------------------------------------------*/
00071 static
00072 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
00073 {
00074   PSOCK_BEGIN(&s->sout);
00075   PSOCK_END(&s->sout);
00076 }
00077 /*---------------------------------------------------------------------------*/
00078 httpd_cgifunction
00079 httpd_cgi(char *name)
00080 {
00081   const struct httpd_cgi_call **f;
00082 
00083   /* Find the matching name in the table, return the function. */
00084   for(f = calls; *f != NULL; ++f) {
00085     if(strncmp((*f)->name, name, strlen((*f)->name)) == 0) {
00086       return (*f)->function;
00087     }
00088   }
00089   return nullfunction;
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 static unsigned short
00093 generate_file_stats(void *arg)
00094 {
00095   char *f = (char *)arg;
00096   return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE, "%u", httpd_fs_count(f));
00097 }
00098 /*---------------------------------------------------------------------------*/
00099 static
00100 PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
00101 {
00102   PSOCK_BEGIN(&s->sout);
00103 
00104   PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, strchr(ptr, ' ') + 1);
00105   
00106   PSOCK_END(&s->sout);
00107 }
00108 /*---------------------------------------------------------------------------*/
00109 static const char closed[]      = "CLOSED";
00110 static const char syn_rcvd[]    = "SYN-RCVD";
00111 static const char syn_sent[]    = "SYN-SENT";
00112 static const char established[] = "ESTABLISHED";
00113 static const char fin_wait_1[]  = "FIN-WAIT-1";
00114 static const char fin_wait_2[]  = "FIN-WAIT-2";
00115 static const char closing[]     = "CLOSING";
00116 static const char time_wait[]   = "TIME-WAIT";
00117 static const char last_ack[]    = "LAST-ACK";
00118 
00119 static const char *states[] = {
00120   closed,
00121   syn_rcvd,
00122   syn_sent,
00123   established,
00124   fin_wait_1,
00125   fin_wait_2,
00126   closing,
00127   time_wait,
00128   last_ack};
00129   
00130 
00131 static unsigned short
00132 generate_tcp_stats(void *arg)
00133 {
00134   struct uip_conn *conn;
00135   struct httpd_state *s = (struct httpd_state *)arg;
00136     
00137   conn = &uip_conns[s->count];
00138   return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
00139          "<tr><td>%d</td><td>%u.%u.%u.%u:%u</td><td>%s</td><td>%u</td><td>%u</td><td>%c %c</td></tr>\n",
00140          uip_htons(conn->lport),
00141          conn->ripaddr.u8[0],
00142          conn->ripaddr.u8[1],
00143          conn->ripaddr.u8[2],
00144          conn->ripaddr.u8[3],
00145          uip_htons(conn->rport),
00146          states[conn->tcpstateflags & UIP_TS_MASK],
00147          conn->nrtx,
00148          conn->timer,
00149          (uip_outstanding(conn))? '*':' ',
00150          (uip_stopped(conn))? '!':' ');
00151 }
00152 /*---------------------------------------------------------------------------*/
00153 static
00154 PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
00155 {
00156   
00157   PSOCK_BEGIN(&s->sout);
00158 
00159   for(s->count = 0; s->count < UIP_CONNS; ++s->count) {
00160     if((uip_conns[s->count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
00161       PSOCK_GENERATOR_SEND(&s->sout, generate_tcp_stats, s);
00162     }
00163   }
00164 
00165   PSOCK_END(&s->sout);
00166 }
00167 /*---------------------------------------------------------------------------*/
00168 static unsigned short
00169 generate_net_stats(void *arg)
00170 {
00171   struct httpd_state *s = (struct httpd_state *)arg;
00172   return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
00173           "%u\n", ((uip_stats_t *)&uip_stat)[s->count]);
00174 }
00175 
00176 static
00177 PT_THREAD(net_stats(struct httpd_state *s, char *ptr))
00178 {
00179   PSOCK_BEGIN(&s->sout);
00180 
00181 #if UIP_STATISTICS
00182 
00183   for(s->count = 0; s->count < sizeof(uip_stat) / sizeof(uip_stats_t);
00184       ++s->count) {
00185     PSOCK_GENERATOR_SEND(&s->sout, generate_net_stats, s);
00186   }
00187   
00188 #endif /* UIP_STATISTICS */
00189   
00190   PSOCK_END(&s->sout);
00191 }
00192 /*---------------------------------------------------------------------------*/
00193 static unsigned short
00194 generate_tmp102_stats(void *arg)
00195 {
00196   struct httpd_state *s = (struct httpd_state *)arg;
00197   return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
00198           "%f", tmp102.read());
00199 }
00200 
00201 static
00202 PT_THREAD(tmp102_stats(struct httpd_state *s, char *ptr))
00203 {
00204   PSOCK_BEGIN(&s->sout);
00205 
00206   PSOCK_GENERATOR_SEND(&s->sout, generate_tmp102_stats, s);
00207   
00208   PSOCK_END(&s->sout);
00209 }
00210 /*---------------------------------------------------------------------------*/
00211 /** @} */