blob: f9cc3f17e6641acd409342a36c902c9b78e6ff41 [file] [log] [blame]
adamdunkels9fcf9d62003-09-04 19:46:32 +00001/*
2 * Copyright (c) 2001, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Adam Dunkels.
16 * 4. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * This file is part of the uIP TCP/IP stack.
33 *
34 * $Id: httpd-cgi.c,v 1.1 2003/09/04 19:46:33 adamdunkels Exp $
35 *
36 */
37
38/*
39 * This file includes functions that are called by the web server
40 * scripts. The functions takes no argument, and the return value is
41 * interpreted as follows. A zero means that the function did not
42 * complete and should be invoked for the next packet as well. A
43 * non-zero value indicates that the function has completed and that
44 * the web server should move along to the next script line.
45 *
46 */
47
48#include "uip.h"
49#include "httpd.h"
50#include "httpd-cgi.h"
51#include "httpd-fs.h"
52
53#include "petsciiconv.h"
54
55#ifdef __CBM__
56#include <cbm.h>
57#include <c64.h>
58#endif /* __CBM__ */
59
60#include <stdio.h>
61#include <string.h>
62
63#include "sensors.h"
64
65static u8_t sensors(void);
66static u8_t tcp_stats(void);
67static u8_t processes(void);
68
69const httpd_cgifunction httpd_cgitab[3] = {
70 sensors, /* CGI function "a" */
71 tcp_stats, /* CGI function "b" */
72 processes, /* CGI function "c" */
73};
74
75static const char closed[] = /* "CLOSED",*/
76{0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
77static const char syn_rcvd[] = /* "SYN-RCVD",*/
78{0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,
79 0x44, 0};
80static const char syn_sent[] = /* "SYN-SENT",*/
81{0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,
82 0x54, 0};
83static const char established[] = /* "ESTABLISHED",*/
84{0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49, 0x53, 0x48,
85 0x45, 0x44, 0};
86static const char fin_wait_1[] = /* "FIN-WAIT-1",*/
87{0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
88 0x54, 0x2d, 0x31, 0};
89static const char fin_wait_2[] = /* "FIN-WAIT-2",*/
90{0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
91 0x54, 0x2d, 0x32, 0};
92static const char closing[] = /* "CLOSING",*/
93{0x43, 0x4c, 0x4f, 0x53, 0x49,
94 0x4e, 0x47, 0};
95static const char time_wait[] = /* "TIME-WAIT,"*/
96{0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,
97 0x49, 0x54, 0};
98static const char last_ack[] = /* "LAST-ACK"*/
99{0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,
100 0x4b, 0};
101
102static const char * const states[] = {
103 closed,
104 syn_rcvd,
105 syn_sent,
106 established,
107 fin_wait_1,
108 fin_wait_2,
109 closing,
110 time_wait,
111 last_ack};
112
113
114/*-----------------------------------------------------------------------------------*/
115static u8_t
116sensors(void)
117{
118 if(uip_acked()) {
119 return 1;
120 }
121 sprintf((char *)uip_appdata,
122 "<li>Microphone 0x%x<br><li>Battery 0x%x<br><li>Temp %d<br><li>Extern 0x%x<br>",
123 sensors_mic, sensors_battery, sensors_temp, sensors_extern);
124 uip_send(uip_appdata, strlen((char *)uip_appdata));
125 return 0;
126}
127/*-----------------------------------------------------------------------------------*/
128static u8_t
129tcp_stats(void)
130{
131 struct uip_conn *conn;
132
133 if(uip_acked()) {
134 /* If the previously sent data has been acknowledged, we move
135 forward one connection. */
136 if(++hs->count == UIP_CONNS) {
137 /* If all connections has been printed out, we are done and
138 return 1. */
139 return 1;
140 }
141 }
142
143 conn = &uip_conns[hs->count];
144 while((conn->tcpstateflags & TS_MASK) == CLOSED) {
145 if(++hs->count == UIP_CONNS) {
146 /* If all connections has been printed out, we are done and
147 return 1. */
148 return 1;
149 }
150 conn = &uip_conns[hs->count];
151 }
152
153 sprintf((char *)uip_appdata,
154 "<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>\r\n",
155 htons(conn->lport),
156 htons(conn->ripaddr[0]) >> 8,
157 htons(conn->ripaddr[0]) & 0xff,
158 htons(conn->ripaddr[1]) >> 8,
159 htons(conn->ripaddr[1]) & 0xff,
160 htons(conn->rport),
161 states[conn->tcpstateflags & TS_MASK],
162 conn->nrtx,
163 conn->timer,
164 (uip_outstanding(conn))? '*':' ',
165 (uip_stopped(conn))? '!':' ');
166 uip_send(uip_appdata, strlen((char *)uip_appdata));
167
168 return 0;
169}
170/*-----------------------------------------------------------------------------------*/
171static u8_t
172processes(void)
173{
174 u8_t i;
175 struct dispatcher_proc *p;
176
177
178 p = DISPATCHER_PROCS();
179 for(i = 0; i < hs->count; ++i) {
180 if(p != NULL) {
181 p = p->next;
182 }
183 }
184
185 if(uip_acked()) {
186 /* If the previously sent data has been acknowledged, we move
187 forward one connection. */
188 ++hs->count;
189 if(p != NULL) {
190 p = p->next;
191 }
192 if(p == NULL) {
193 /* If all processes have been printed out, we are done and
194 return 1. */
195 return 1;
196 }
197 }
198
199 sprintf((char *)uip_appdata,
200 "<tr align=\"center\"><td>%3d</td><td>%s</td><td>0x%04x</td><td>0x%04x</td><td>0x%04x</td></tr>\r\n",
201 p->id, p->name,
202 p->idle, p->signalhandler, p->uiphandler);
203 uip_send(uip_appdata, strlen((char *)uip_appdata));
204 return 0;
205}
206/*-----------------------------------------------------------------------------------*/
207/*-----------------------------------------------------------------------------------*/