uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Committer:
ban4jp
Date:
Mon Jun 30 16:00:08 2014 +0000
Revision:
3:a2715e9c7737
Parent:
2:4da9ed411bdc
backported from Contiki 2.7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ban4jp 0:685224d2f66d 1 /**
ban4jp 0:685224d2f66d 2 * \addtogroup httpd
ban4jp 0:685224d2f66d 3 * @{
ban4jp 0:685224d2f66d 4 */
ban4jp 0:685224d2f66d 5
ban4jp 0:685224d2f66d 6 /**
ban4jp 0:685224d2f66d 7 * \file
ban4jp 0:685224d2f66d 8 * Web server script interface
ban4jp 0:685224d2f66d 9 * \author
ban4jp 0:685224d2f66d 10 * Adam Dunkels <adam@sics.se>
ban4jp 0:685224d2f66d 11 *
ban4jp 0:685224d2f66d 12 */
ban4jp 0:685224d2f66d 13
ban4jp 0:685224d2f66d 14 /*
ban4jp 0:685224d2f66d 15 * Copyright (c) 2001-2006, Adam Dunkels.
ban4jp 0:685224d2f66d 16 * All rights reserved.
ban4jp 0:685224d2f66d 17 *
ban4jp 0:685224d2f66d 18 * Redistribution and use in source and binary forms, with or without
ban4jp 0:685224d2f66d 19 * modification, are permitted provided that the following conditions
ban4jp 0:685224d2f66d 20 * are met:
ban4jp 0:685224d2f66d 21 * 1. Redistributions of source code must retain the above copyright
ban4jp 0:685224d2f66d 22 * notice, this list of conditions and the following disclaimer.
ban4jp 0:685224d2f66d 23 * 2. Redistributions in binary form must reproduce the above copyright
ban4jp 0:685224d2f66d 24 * notice, this list of conditions and the following disclaimer in the
ban4jp 0:685224d2f66d 25 * documentation and/or other materials provided with the distribution.
ban4jp 0:685224d2f66d 26 * 3. The name of the author may not be used to endorse or promote
ban4jp 0:685224d2f66d 27 * products derived from this software without specific prior
ban4jp 0:685224d2f66d 28 * written permission.
ban4jp 0:685224d2f66d 29 *
ban4jp 0:685224d2f66d 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
ban4jp 0:685224d2f66d 31 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
ban4jp 0:685224d2f66d 32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ban4jp 0:685224d2f66d 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
ban4jp 0:685224d2f66d 34 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ban4jp 0:685224d2f66d 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ban4jp 0:685224d2f66d 36 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ban4jp 0:685224d2f66d 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
ban4jp 0:685224d2f66d 38 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ban4jp 0:685224d2f66d 39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ban4jp 0:685224d2f66d 40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ban4jp 0:685224d2f66d 41 *
ban4jp 0:685224d2f66d 42 * This file is part of the uIP TCP/IP stack.
ban4jp 0:685224d2f66d 43 *
ban4jp 0:685224d2f66d 44 * $Id: httpd-cgi.c,v 1.2 2006/06/11 21:46:37 adam Exp $
ban4jp 0:685224d2f66d 45 *
ban4jp 0:685224d2f66d 46 */
ban4jp 0:685224d2f66d 47
ban4jp 2:4da9ed411bdc 48 extern "C" {
ban4jp 2:4da9ed411bdc 49 #include "uip.h"
ban4jp 2:4da9ed411bdc 50 #include "psock.h"
ban4jp 2:4da9ed411bdc 51 #include "httpd.h"
ban4jp 2:4da9ed411bdc 52 }
ban4jp 0:685224d2f66d 53 #include "httpd-cgi.h"
ban4jp 2:4da9ed411bdc 54 //#include "httpd-fs.h"
ban4jp 0:685224d2f66d 55
ban4jp 0:685224d2f66d 56 #include <stdio.h>
ban4jp 0:685224d2f66d 57 #include <string.h>
ban4jp 0:685224d2f66d 58
ban4jp 2:4da9ed411bdc 59 #include "TMP102.h"
ban4jp 2:4da9ed411bdc 60
ban4jp 2:4da9ed411bdc 61 TMP102 tmp102(dp5, dp27, 0x90); //A0 pin is connected to ground
ban4jp 2:4da9ed411bdc 62
ban4jp 0:685224d2f66d 63 HTTPD_CGI_CALL(file, "file-stats", file_stats);
ban4jp 0:685224d2f66d 64 HTTPD_CGI_CALL(tcp, "tcp-connections", tcp_stats);
ban4jp 0:685224d2f66d 65 HTTPD_CGI_CALL(net, "net-stats", net_stats);
ban4jp 2:4da9ed411bdc 66 HTTPD_CGI_CALL(tmp, "tmp102-stats", tmp102_stats);
ban4jp 0:685224d2f66d 67
ban4jp 2:4da9ed411bdc 68 static const struct httpd_cgi_call *calls[] = { &file, &tcp, &net, &tmp, NULL };
ban4jp 0:685224d2f66d 69
ban4jp 0:685224d2f66d 70 /*---------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 71 static
ban4jp 0:685224d2f66d 72 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
ban4jp 0:685224d2f66d 73 {
ban4jp 0:685224d2f66d 74 PSOCK_BEGIN(&s->sout);
ban4jp 0:685224d2f66d 75 PSOCK_END(&s->sout);
ban4jp 0:685224d2f66d 76 }
ban4jp 0:685224d2f66d 77 /*---------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 78 httpd_cgifunction
ban4jp 0:685224d2f66d 79 httpd_cgi(char *name)
ban4jp 0:685224d2f66d 80 {
ban4jp 0:685224d2f66d 81 const struct httpd_cgi_call **f;
ban4jp 0:685224d2f66d 82
ban4jp 0:685224d2f66d 83 /* Find the matching name in the table, return the function. */
ban4jp 0:685224d2f66d 84 for(f = calls; *f != NULL; ++f) {
ban4jp 0:685224d2f66d 85 if(strncmp((*f)->name, name, strlen((*f)->name)) == 0) {
ban4jp 0:685224d2f66d 86 return (*f)->function;
ban4jp 0:685224d2f66d 87 }
ban4jp 0:685224d2f66d 88 }
ban4jp 0:685224d2f66d 89 return nullfunction;
ban4jp 0:685224d2f66d 90 }
ban4jp 0:685224d2f66d 91 /*---------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 92 static unsigned short
ban4jp 0:685224d2f66d 93 generate_file_stats(void *arg)
ban4jp 0:685224d2f66d 94 {
ban4jp 0:685224d2f66d 95 char *f = (char *)arg;
ban4jp 1:b4d28172cacd 96 return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE, "%u", httpd_fs_count(f));
ban4jp 0:685224d2f66d 97 }
ban4jp 0:685224d2f66d 98 /*---------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 99 static
ban4jp 0:685224d2f66d 100 PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
ban4jp 0:685224d2f66d 101 {
ban4jp 0:685224d2f66d 102 PSOCK_BEGIN(&s->sout);
ban4jp 0:685224d2f66d 103
ban4jp 0:685224d2f66d 104 PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, strchr(ptr, ' ') + 1);
ban4jp 0:685224d2f66d 105
ban4jp 0:685224d2f66d 106 PSOCK_END(&s->sout);
ban4jp 0:685224d2f66d 107 }
ban4jp 0:685224d2f66d 108 /*---------------------------------------------------------------------------*/
ban4jp 2:4da9ed411bdc 109 static const char closed[] = "CLOSED";
ban4jp 2:4da9ed411bdc 110 static const char syn_rcvd[] = "SYN-RCVD";
ban4jp 2:4da9ed411bdc 111 static const char syn_sent[] = "SYN-SENT";
ban4jp 2:4da9ed411bdc 112 static const char established[] = "ESTABLISHED";
ban4jp 2:4da9ed411bdc 113 static const char fin_wait_1[] = "FIN-WAIT-1";
ban4jp 2:4da9ed411bdc 114 static const char fin_wait_2[] = "FIN-WAIT-2";
ban4jp 2:4da9ed411bdc 115 static const char closing[] = "CLOSING";
ban4jp 2:4da9ed411bdc 116 static const char time_wait[] = "TIME-WAIT";
ban4jp 2:4da9ed411bdc 117 static const char last_ack[] = "LAST-ACK";
ban4jp 0:685224d2f66d 118
ban4jp 0:685224d2f66d 119 static const char *states[] = {
ban4jp 0:685224d2f66d 120 closed,
ban4jp 0:685224d2f66d 121 syn_rcvd,
ban4jp 0:685224d2f66d 122 syn_sent,
ban4jp 0:685224d2f66d 123 established,
ban4jp 0:685224d2f66d 124 fin_wait_1,
ban4jp 0:685224d2f66d 125 fin_wait_2,
ban4jp 0:685224d2f66d 126 closing,
ban4jp 0:685224d2f66d 127 time_wait,
ban4jp 0:685224d2f66d 128 last_ack};
ban4jp 0:685224d2f66d 129
ban4jp 0:685224d2f66d 130
ban4jp 0:685224d2f66d 131 static unsigned short
ban4jp 0:685224d2f66d 132 generate_tcp_stats(void *arg)
ban4jp 0:685224d2f66d 133 {
ban4jp 0:685224d2f66d 134 struct uip_conn *conn;
ban4jp 0:685224d2f66d 135 struct httpd_state *s = (struct httpd_state *)arg;
ban4jp 0:685224d2f66d 136
ban4jp 0:685224d2f66d 137 conn = &uip_conns[s->count];
ban4jp 0:685224d2f66d 138 return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
ban4jp 1:b4d28172cacd 139 "<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",
ban4jp 3:a2715e9c7737 140 uip_htons(conn->lport),
ban4jp 3:a2715e9c7737 141 conn->ripaddr.u8[0],
ban4jp 3:a2715e9c7737 142 conn->ripaddr.u8[1],
ban4jp 3:a2715e9c7737 143 conn->ripaddr.u8[2],
ban4jp 3:a2715e9c7737 144 conn->ripaddr.u8[3],
ban4jp 3:a2715e9c7737 145 uip_htons(conn->rport),
ban4jp 0:685224d2f66d 146 states[conn->tcpstateflags & UIP_TS_MASK],
ban4jp 0:685224d2f66d 147 conn->nrtx,
ban4jp 0:685224d2f66d 148 conn->timer,
ban4jp 0:685224d2f66d 149 (uip_outstanding(conn))? '*':' ',
ban4jp 0:685224d2f66d 150 (uip_stopped(conn))? '!':' ');
ban4jp 0:685224d2f66d 151 }
ban4jp 0:685224d2f66d 152 /*---------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 153 static
ban4jp 0:685224d2f66d 154 PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
ban4jp 0:685224d2f66d 155 {
ban4jp 0:685224d2f66d 156
ban4jp 0:685224d2f66d 157 PSOCK_BEGIN(&s->sout);
ban4jp 0:685224d2f66d 158
ban4jp 0:685224d2f66d 159 for(s->count = 0; s->count < UIP_CONNS; ++s->count) {
ban4jp 0:685224d2f66d 160 if((uip_conns[s->count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
ban4jp 0:685224d2f66d 161 PSOCK_GENERATOR_SEND(&s->sout, generate_tcp_stats, s);
ban4jp 0:685224d2f66d 162 }
ban4jp 0:685224d2f66d 163 }
ban4jp 0:685224d2f66d 164
ban4jp 0:685224d2f66d 165 PSOCK_END(&s->sout);
ban4jp 0:685224d2f66d 166 }
ban4jp 0:685224d2f66d 167 /*---------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 168 static unsigned short
ban4jp 0:685224d2f66d 169 generate_net_stats(void *arg)
ban4jp 0:685224d2f66d 170 {
ban4jp 0:685224d2f66d 171 struct httpd_state *s = (struct httpd_state *)arg;
ban4jp 0:685224d2f66d 172 return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
ban4jp 1:b4d28172cacd 173 "%u\n", ((uip_stats_t *)&uip_stat)[s->count]);
ban4jp 0:685224d2f66d 174 }
ban4jp 0:685224d2f66d 175
ban4jp 0:685224d2f66d 176 static
ban4jp 0:685224d2f66d 177 PT_THREAD(net_stats(struct httpd_state *s, char *ptr))
ban4jp 0:685224d2f66d 178 {
ban4jp 0:685224d2f66d 179 PSOCK_BEGIN(&s->sout);
ban4jp 0:685224d2f66d 180
ban4jp 0:685224d2f66d 181 #if UIP_STATISTICS
ban4jp 0:685224d2f66d 182
ban4jp 0:685224d2f66d 183 for(s->count = 0; s->count < sizeof(uip_stat) / sizeof(uip_stats_t);
ban4jp 0:685224d2f66d 184 ++s->count) {
ban4jp 0:685224d2f66d 185 PSOCK_GENERATOR_SEND(&s->sout, generate_net_stats, s);
ban4jp 0:685224d2f66d 186 }
ban4jp 0:685224d2f66d 187
ban4jp 0:685224d2f66d 188 #endif /* UIP_STATISTICS */
ban4jp 0:685224d2f66d 189
ban4jp 0:685224d2f66d 190 PSOCK_END(&s->sout);
ban4jp 0:685224d2f66d 191 }
ban4jp 0:685224d2f66d 192 /*---------------------------------------------------------------------------*/
ban4jp 3:a2715e9c7737 193 static unsigned short
ban4jp 3:a2715e9c7737 194 generate_tmp102_stats(void *arg)
ban4jp 3:a2715e9c7737 195 {
ban4jp 3:a2715e9c7737 196 struct httpd_state *s = (struct httpd_state *)arg;
ban4jp 3:a2715e9c7737 197 return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
ban4jp 3:a2715e9c7737 198 "%f", tmp102.read());
ban4jp 3:a2715e9c7737 199 }
ban4jp 3:a2715e9c7737 200
ban4jp 2:4da9ed411bdc 201 static
ban4jp 2:4da9ed411bdc 202 PT_THREAD(tmp102_stats(struct httpd_state *s, char *ptr))
ban4jp 2:4da9ed411bdc 203 {
ban4jp 2:4da9ed411bdc 204 PSOCK_BEGIN(&s->sout);
ban4jp 2:4da9ed411bdc 205
ban4jp 3:a2715e9c7737 206 PSOCK_GENERATOR_SEND(&s->sout, generate_tmp102_stats, s);
ban4jp 2:4da9ed411bdc 207
ban4jp 2:4da9ed411bdc 208 PSOCK_END(&s->sout);
ban4jp 2:4da9ed411bdc 209 }
ban4jp 2:4da9ed411bdc 210 /*---------------------------------------------------------------------------*/
ban4jp 0:685224d2f66d 211 /** @} */