Contiki 2.5
httpd-simple-avr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
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. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $Id: httpd-simple-avr.c,v 1.8 2010/12/04 21:32:35 dak664 Exp $
30  */
31 
32 /**
33  * \file
34  * A simple web server forwarding page generation to a protothread
35  * \author
36  * Adam Dunkels <adam@sics.se>
37  * Niclas Finne <nfi@sics.se>
38  * Joakim Eriksson <joakime@sics.se>
39  * David Kopf <dak664@embarqmail.com> (AVR adaptation)
40  */
41 
42 #include <stdio.h>
43 #include <string.h>
44 #include <avr/pgmspace.h>
45 #include "contiki-net.h"
46 
47 #ifndef WEBSERVER_CONF_CFS_PATHLEN
48 #define HTTPD_PATHLEN 2
49 #else
50 #define HTTPD_PATHLEN WEBSERVER_CONF_CFS_PATHLEN
51 #endif
52 
53 struct httpd_state;
54 typedef char (* httpd_simple_script_t)(struct httpd_state *s);
55 
56 struct httpd_state {
57  struct timer timer;
58  struct psock sin, sout;
59  struct pt outputpt;
60  char inputbuf[HTTPD_PATHLEN + 30];
61  char outputbuf[UIP_TCP_MSS];
62  char filename[HTTPD_PATHLEN];
63  httpd_simple_script_t script;
64  char state;
65 };
66 
67 /* DEBUGLOGIC is a convenient way to debug in a simulator without a tcp/ip connection.
68  * Break the program in the process loop and step from the entry in httpd_appcall.
69  * The input file is forced to /index.html and the output directed to uip_aligned_buf.
70  * If cgi's are invoked define it in httpd-cgi.c as well!
71  * psock_generator_send in /core/net/psock.c must also be modified as follows:
72  * ...
73  * // Wait until all data is sent and acknowledged.
74  * if (!s->sendlen) break; //<---add this line
75  * PT_YIELD_UNTIL(&s->psockpt, uip_acked() || uip_rexmit());
76  * ...
77  */
78 #define DEBUGLOGIC 0
79 #if DEBUGLOGIC
80 struct httpd_state *sg;
81 #define uip_mss(...) sizeof(uip_aligned_buf)
82 #define uip_appdata (char *) &uip_aligned_buf
83 #endif
84 
85 #ifndef WEBSERVER_CONF_CFS_CONNS
86 #define CONNS UIP_CONNS
87 #else /* WEBSERVER_CONF_CFS_CONNS */
88 #define CONNS WEBSERVER_CONF_CFS_CONNS
89 #endif /* WEBSERVER_CONF_CFS_CONNS */
90 
91 #ifndef WEBSERVER_CONF_CFS_URLCONV
92 #define URLCONV 0
93 #else /* WEBSERVER_CONF_CFS_URLCONV */
94 #define URLCONV WEBSERVER_CONF_CFS_URLCONV
95 #endif /* WEBSERVER_CONF_CFS_URLCONV */
96 
97 #define STATE_WAITING 0
98 #define STATE_OUTPUT 1
99 
100 MEMB(conns, struct httpd_state, CONNS);
101 
102 #define webserver_log_file(...)
103 
104 #define ISO_nl 0x0a
105 #define ISO_space 0x20
106 #define ISO_period 0x2e
107 #define ISO_slash 0x2f
108 
109 /*---------------------------------------------------------------------------*/
110 static unsigned short
111 generate_string(void *sstr)
112 {
113  uint8_t slen=strlen((char *)sstr);
114  memcpy(uip_appdata, (char *)sstr, slen);
115 
116 #if DEBUGLOGIC
117  return 0;
118 #else
119  return slen;
120 #endif
121 }
122 /*---------------------------------------------------------------------------*/
123 static unsigned short
124 generate_string_P(void *sstr)
125 {
126  uint8_t slen=strlen_P((char *)sstr);
127  memcpy_P(uip_appdata, (char *)sstr, slen);
128 
129 #if DEBUGLOGIC
130  return 0;
131 #else
132  return slen;
133 #endif
134 }
135 /*---------------------------------------------------------------------------*/
136 #if FIND_THE_SCRIPT
137 /* Needed if more than one script is implemented.
138  * The generate_routes RPL page is hard coded at present
139  */
140 static
141 PT_THREAD(send_string_P(struct httpd_state *s, char *str))
142 {
143  PSOCK_BEGIN(&s->sout);
144  PSOCK_GENERATOR_SEND(&s->sout, generate_string_P, str);
145  PSOCK_END(&s->sout);
146 }
147 #endif
148 /*---------------------------------------------------------------------------*/
149 char http_content_type_html[] PROGMEM = "Content-type: text/html\r\n\r\n";
150 static
151 PT_THREAD(send_headers(struct httpd_state *s, char *statushdr))
152 {
153  PSOCK_BEGIN(&s->sout);
154  PSOCK_GENERATOR_SEND(&s->sout, generate_string_P, statushdr);
155  PSOCK_GENERATOR_SEND(&s->sout, generate_string_P, http_content_type_html);
156  PSOCK_END(&s->sout);
157 }
158 /*---------------------------------------------------------------------------*/
159 const char http_index_html[] PROGMEM = "/index.html";
160 const char http_referer[] PROGMEM = "Referer:";
161 const char http_get[] PROGMEM = "GET ";
162 static
163 PT_THREAD(handle_input(struct httpd_state *s))
164 {
165  PSOCK_BEGIN(&s->sin);
166 
167  PSOCK_READTO(&s->sin, ISO_space);
168 
169  if(strncmp_P(s->inputbuf, http_get, 4) != 0) {
170  PSOCK_CLOSE_EXIT(&s->sin);
171  }
172  PSOCK_READTO(&s->sin, ISO_space);
173 
174  if(s->inputbuf[0] != ISO_slash) {
175  PSOCK_CLOSE_EXIT(&s->sin);
176  }
177 
178 #if URLCONV
179  s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
180  urlconv_tofilename(s->filename, s->inputbuf, sizeof(s->filename));
181 #else /* URLCONV */
182  if(s->inputbuf[1] == ISO_space) {
183  strncpy_P(s->filename, http_index_html, sizeof(s->filename));
184  } else {
185  s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
186  strncpy(s->filename, s->inputbuf, sizeof(s->filename));
187  }
188 #endif /* URLCONV */
189 
190  webserver_log_file(&uip_conn->ripaddr, s->filename);
191 
192  s->state = STATE_OUTPUT;
193 
194  while(1) {
195  PSOCK_READTO(&s->sin, ISO_nl);
196 
197  // if(strncmp_P(s->inputbuf, http_referer, 8) == 0) {
198  // s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
199  // webserver_log(s->inputbuf);
200  // }
201  }
202 
203  PSOCK_END(&s->sin);
204 }
205 /*---------------------------------------------------------------------------*/
206 void
207 httpd_init(void)
208 {
209  tcp_listen(UIP_HTONS(80));
210  memb_init(&conns);
211 }
212 
213 /*---------------------------------------------------------------------------*/
214 /* Only one single web request at time, MSS is 48 to save RAM */
215 static char buf[48];
216 static uint8_t blen;
217 #define ADD(FORMAT,args...) do { \
218  blen += snprintf_P(&buf[blen], sizeof(buf) - blen, PSTR(FORMAT),##args); \
219  } while(0)
220 /*---------------------------------------------------------------------------*/
221 static void
222 ipaddr_add(const uip_ipaddr_t *addr)
223 {
224  uint16_t a;
225  int i, f;
226  for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
227  a = (addr->u8[i] << 8) + addr->u8[i + 1];
228  if(a == 0 && f >= 0) {
229  if(f++ == 0 && sizeof(buf) - blen >= 2) {
230  buf[blen++] = ':';
231  buf[blen++] = ':';
232  }
233  } else {
234  if(f > 0) {
235  f = -1;
236  } else if(i > 0 && blen < sizeof(buf)) {
237  buf[blen++] = ':';
238  }
239  ADD("%x", a);
240  }
241  }
242 }
243 /*---------------------------------------------------------------------------*/
244 char TOP1[] PROGMEM = "<html><head><title>ContikiRPL(Jackdaw)";
245 char TOP2[] PROGMEM = "</title></head><body>";
246 char BOTTOM[] PROGMEM = "</body></html>";
247 #if UIP_CONF_IPV6
250 #endif
251 
252 static
253 PT_THREAD(generate_routes(struct httpd_state *s))
254 {
255  uint8_t i=0;
256  PSOCK_BEGIN(&s->sout);
257 
258  PSOCK_GENERATOR_SEND(&s->sout, generate_string_P, TOP1);
259  PSOCK_GENERATOR_SEND(&s->sout, generate_string_P, TOP2);
260 
261 #if UIP_CONF_IPV6 //allow ip4 builds
262  blen = 0;
263  ADD("<h2>Neighbors [%u max]</h2>",UIP_DS6_NBR_NB);
264  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
265  blen = 0;
266  for(i = 0; i < UIP_DS6_NBR_NB; i++) {
267  if(uip_ds6_nbr_cache[i].isused) {
268  ipaddr_add(&uip_ds6_nbr_cache[i].ipaddr);
269  ADD("<br>");
270 // if(blen > sizeof(buf) - 45) {
271  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
272  blen = 0;
273 // }
274  }
275  }
276 
277  ADD("<h2>Routes [%u max]</h2>",UIP_DS6_ROUTE_NB);
278  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
279  blen = 0;
280  for(i = 0; i < UIP_DS6_ROUTE_NB; i++) {
281  if(uip_ds6_routing_table[i].isused) {
282  ipaddr_add(&uip_ds6_routing_table[i].ipaddr);
283  ADD("/%u (via ", uip_ds6_routing_table[i].length);
284  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
285  blen=0;
286  ipaddr_add(&uip_ds6_routing_table[i].nexthop);
287  if(uip_ds6_routing_table[i].state.lifetime < 600) {
288  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
289  blen=0;
290  ADD(") %lus<br>", uip_ds6_routing_table[i].state.lifetime);
291  } else {
292  ADD(")<br>");
293  }
294  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
295  blen = 0;
296  }
297  }
298  if(blen > 0) {
299  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
300  blen = 0;
301  }
302 #else /* UIP_CONF_IPV6 */
303  blen = 0;i++;
304  ADD("<h2>Hey, you got ip4 working!</h2>");
305  PSOCK_GENERATOR_SEND(&s->sout, generate_string, buf);
306 #endif /* UIP_CONF_IPV6 */
307 
308  PSOCK_GENERATOR_SEND(&s->sout, generate_string_P, BOTTOM);
309 
310  PSOCK_END(&s->sout);
311 }
312 
313 /*---------------------------------------------------------------------------*/
314 httpd_simple_script_t
315 httpd_simple_get_script(const char *name)
316 {
317  return generate_routes;
318 }
319 /*---------------------------------------------------------------------------*/
320 char http_header_200[] PROGMEM = "HTTP/1.0 200 OK\r\nServer: Jackdaw\r\nConnection: close\r\n";
321 char http_header_404[] PROGMEM = "HTTP/1.0 404 Not found\r\nServer: Jackdaw\r\nConnection: close\r\n";
322 char NOT_FOUND[] PROGMEM = "<html><body bgcolor=\"white\"><center><h1>404 - file not found</h1></center></body></html>";
323 static
324 PT_THREAD(handle_output(struct httpd_state *s))
325 {
326  PT_BEGIN(&s->outputpt);
327 
328 #if DEBUGLOGIC
329  strcpy_P(s->filename,PSTR("/x"));
330 #endif
331 #if FIND_THE_SCRIPT
332  s->script = httpd_simple_get_script(&s->filename[1]);
333  if(s->script == NULL) {
334  printf_P(PSTR("not found!"));
335  strcpy_P(s->filename, PSTR("/notfound.html"));
336 
337  PT_WAIT_THREAD(&s->outputpt,
338  send_headers(s, http_header_404));
339  PT_WAIT_THREAD(&s->outputpt,
340  send_string_P(s, NOT_FOUND));
341  uip_close();
342 
343  PT_EXIT(&s->outputpt);
344  } else {
345 #else
346  s->script = generate_routes;
347  if (1) {
348 #endif
349 
350  PT_WAIT_THREAD(&s->outputpt,
351  send_headers(s, http_header_200));
352  PT_WAIT_THREAD(&s->outputpt, s->script(s));
353  }
354  s->script = NULL;
355  PSOCK_CLOSE(&s->sout);
356  PT_END(&s->outputpt);
357 }
358 /*---------------------------------------------------------------------------*/
359 static void
360 handle_connection(struct httpd_state *s)
361 {
362 #if DEBUGLOGIC
363  handle_output(s);
364 #else
365  handle_input(s);
366  if(s->state == STATE_OUTPUT) {
367  handle_output(s);
368  }
369 #endif
370 }
371 /*---------------------------------------------------------------------------*/
372 void
373 httpd_appcall(void *state)
374 {
375 #if DEBUGLOGIC
376  struct httpd_state *s; //Enter here for debugging with output directed to TCPBUF
377  s = sg = (struct httpd_state *)memb_alloc(&conns); //put ram watch on sg
378  if (1) {
379 #else
380  struct httpd_state *s = (struct httpd_state *)state;
381 
382  if(uip_closed() || uip_aborted() || uip_timedout()) {
383  if(s != NULL) {
384  s->script = NULL;
385  memb_free(&conns, s);
386  }
387  } else if(uip_connected()) {
388  s = (struct httpd_state *)memb_alloc(&conns);
389  if(s == NULL) {
390  uip_abort();
391  webserver_log_file(&uip_conn->ripaddr, "reset (no memory block)");
392  return;
393  }
394 #endif
395  tcp_markconn(uip_conn, s);
396  PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
397  PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf, sizeof(s->inputbuf) - 1);
398  PT_INIT(&s->outputpt);
399  s->script = NULL;
400  s->state = STATE_WAITING;
401  timer_set(&s->timer, CLOCK_SECOND * 10);
402  handle_connection(s);
403  } else if(s != NULL) {
404  if(uip_poll()) {
405  if(timer_expired(&s->timer)) {
406  uip_abort();
407  s->script = NULL;
408  memb_free(&conns, s);
409  webserver_log_file(&uip_conn->ripaddr, "reset (timeout)");
410  }
411  } else {
412  timer_restart(&s->timer);
413  }
414  handle_connection(s);
415  } else {
416  uip_abort();
417  }
418 }
419 /*---------------------------------------------------------------------------*/
420 PROCESS(httpd_process, "httpd");
421 PROCESS_THREAD(httpd_process, ev, data)
422 {
423  PROCESS_BEGIN();
424 
425  httpd_init();
426 
427  while(1) {
429  httpd_appcall(data);
430  }
431 
432  PROCESS_END();
433 }
434