Contiki 2.5
httpd-cgi.c
1 /*
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. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the uIP TCP/IP stack.
30  *
31  * $Id: httpd-cgi.c,v 1.13 2010/12/23 19:41:07 dak664 Exp $
32  *
33  */
34 /* Line endings in git repository are LF instead of CR-LF ? */
35 /*
36  * This file includes functions that are called by the web server
37  * scripts. The functions takes no argument, and the return value is
38  * interpreted as follows. A zero means that the function did not
39  * complete and should be invoked for the next packet as well. A
40  * non-zero value indicates that the function has completed and that
41  * the web server should move along to the next script line.
42  *
43  */
44 
45 #include <stdio.h>
46 #include <string.h>
47 
48 #include "contiki-net.h"
49 #include "httpd.h"
50 #include "httpd-cgi.h"
51 #include "httpd-fs.h"
52 #include "httpd-fsdata.h"
53 #include "lib/petsciiconv.h"
54 
55 #include "sensors.h"
56 
57 #define DEBUGLOGIC 0 //See httpd.c, if 1 must also set it there!
58 #if DEBUGLOGIC
59 #define uip_mss(...) 512
60 #define uip_appdata TCPBUF
61 extern char TCPBUF[512];
62 #endif
63 
64 /* RADIOSTATS must also be set in clock.c and the radio driver */
65 #if RF230BB
66 #define RADIOSTATS 1
67 #endif
68 
69 static struct httpd_cgi_call *calls = NULL;
70 
71 /*cgi function names*/
72 #if HTTPD_FS_STATISTICS
73 static const char file_name[] HTTPD_STRING_ATTR = "file-stats";
74 #endif
75 static const char tcp_name[] HTTPD_STRING_ATTR = "tcp-connections";
76 static const char proc_name[] HTTPD_STRING_ATTR = "processes";
77 static const char sensor_name[] HTTPD_STRING_ATTR = "sensors";
78 static const char adrs_name[] HTTPD_STRING_ATTR = "addresses";
79 static const char nbrs_name[] HTTPD_STRING_ATTR = "neighbors";
80 static const char rtes_name[] HTTPD_STRING_ATTR = "routes";
81 
82 /*Process states for processes cgi*/
83 static const char closed[] HTTPD_STRING_ATTR = "CLOSED";
84 static const char syn_rcvd[] HTTPD_STRING_ATTR = "SYN-RCVD";
85 static const char syn_sent[] HTTPD_STRING_ATTR = "SYN-SENT";
86 static const char established[] HTTPD_STRING_ATTR = "ESTABLISHED";
87 static const char fin_wait_1[] HTTPD_STRING_ATTR = "FIN-WAIT-1";
88 static const char fin_wait_2[] HTTPD_STRING_ATTR = "FIN-WAIT-2";
89 static const char closing[] HTTPD_STRING_ATTR = "CLOSING";
90 static const char time_wait[] HTTPD_STRING_ATTR = "TIME-WAIT";
91 static const char last_ack[] HTTPD_STRING_ATTR = "LAST-ACK";
92 static const char none[] HTTPD_STRING_ATTR = "NONE";
93 static const char running[] HTTPD_STRING_ATTR = "RUNNING";
94 static const char called[] HTTPD_STRING_ATTR = "CALLED";
95 static const char *states[] = {
96  closed,
97  syn_rcvd,
98  syn_sent,
99  established,
100  fin_wait_1,
101  fin_wait_2,
102  closing,
103  time_wait,
104  last_ack,
105  none,
106  running,
107  called};
108 
109  static char sensor_temperature[12]="Not Enabled";
110  static char sensor_extvoltage[12]="Not Enabled";
111  static unsigned long last_tempupdate,last_extvoltageupdate;
112  extern unsigned long seconds, sleepseconds;
113 #if RADIOSTATS
114  extern unsigned long radioontime;
115  static unsigned long savedradioontime;
116  extern uint8_t RF230_radio_on, rf230_last_rssi;
117  extern uint16_t RF230_sendpackets,RF230_receivepackets,RF230_sendfail,RF230_receivefail;
118 #endif
119 
120 
121 void
122 web_set_temp(char *s)
123 {
124  strcpy(sensor_temperature, s);
125 // printf_P(PSTR("got temp"));
126  last_tempupdate=seconds;
127 }
128 void
129 web_set_voltage(char *s)
130 {
131  strcpy(sensor_extvoltage, s);
132 // printf_P(PSTR("got volts"));
133  last_extvoltageupdate=seconds;
134 }
135 
136 /*---------------------------------------------------------------------------*/
137 static
138 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
139 {
140  PSOCK_BEGIN(&s->sout);
141  PSOCK_END(&s->sout);
142 }
143 /*---------------------------------------------------------------------------*/
144 httpd_cgifunction
145 httpd_cgi(char *name)
146 {
147  struct httpd_cgi_call *f;
148 
149  /* Find the matching name in the table, return the function. */
150  for(f = calls; f != NULL; f = f->next) {
151  if(httpd_strncmp(name, f->name, httpd_strlen(f->name)) == 0) {
152  return f->function;
153  }
154  }
155  return nullfunction;
156 }
157 
158 #if HTTPD_FS_STATISTICS
159 static char *thisfilename;
160 /*---------------------------------------------------------------------------*/
161 static unsigned short
162 generate_file_stats(void *arg)
163 {
164  static const char httpd_cgi_filestat1[] HTTPD_STRING_ATTR = "<p class=right><br><br><i>This page has been sent %u times</i></div></body></html>";
165  static const char httpd_cgi_filestat2[] HTTPD_STRING_ATTR = "<tr><td><a href=\"%s\">%s</a></td><td>%d</td>";
166  static const char httpd_cgi_filestat3[] HTTPD_STRING_ATTR = "%5u";
167  char tmp[20];
168  struct httpd_fsdata_file_noconst *f,fram;
169  u16_t i;
170  unsigned short numprinted;
171 
172  /* Transfer arg from whichever flash that contains the html file to RAM */
173  httpd_fs_cpy(&tmp, (char *)arg, 20);
174 
175  /* Count for this page, with common page footer */
176  if (tmp[0]=='.') {
177  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat1, httpd_fs_open(thisfilename, 0));
178 
179  /* Count for all files */
180  /* Note buffer will overflow if there are too many files! */
181  } else if (tmp[0]=='*') {
182  i=0;numprinted=0;
183  for(f = (struct httpd_fsdata_file_noconst *)httpd_fs_get_root();
184  f != NULL;
185  f = (struct httpd_fsdata_file_noconst *)fram.next) {
186 
187  /* Get the linked list file entry into RAM from from wherever it is*/
188  httpd_memcpy(&fram,f,sizeof(fram));
189 
190  /* Get the file name from whatever memory it is in */
191  httpd_fs_cpy(&tmp, fram.name, sizeof(tmp));
192 #if HTTPD_FS_STATISTICS==1
193  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, f->count);
194 #elif HTTPD_FS_STATISTICS==2
195  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, httpd_filecount[i]);
196 #endif
197  i++;
198  }
199 
200  /* Count for specified file */
201  } else {
202  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat3, httpd_fs_open(tmp, 0));
203  }
204 #if DEBUGLOGIC
205  return 0;
206 #endif
207  return numprinted;
208 }
209 /*---------------------------------------------------------------------------*/
210 static
211 PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
212 {
213 
214  PSOCK_BEGIN(&s->sout);
215 
216  thisfilename=&s->filename[0]; //temporary way to pass filename to generate_file_stats
217 
218  PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) ptr);
219 
220  PSOCK_END(&s->sout);
221 }
222 #endif /*HTTPD_FS_STATISTICS*/
223 /*---------------------------------------------------------------------------*/
224 static unsigned short
225 make_tcp_stats(void *arg)
226 {
227  static const char httpd_cgi_tcpstat1[] HTTPD_STRING_ATTR = "<tr align=\"center\"><td>%d</td><td>";
228  static const char httpd_cgi_tcpstat2[] HTTPD_STRING_ATTR = "-%u</td><td>%s</td><td>%u</td><td>%u</td><td>%c %c</td></tr>\r\n";
229  struct uip_conn *conn;
230  struct httpd_state *s = (struct httpd_state *)arg;
231  char tstate[20];
232  uint16_t numprinted;
233 
234  conn = &uip_conns[s->u.count];
235 
236  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_tcpstat1, uip_htons(conn->lport));
237  numprinted += httpd_cgi_sprint_ip6(conn->ripaddr, uip_appdata + numprinted);
238  httpd_strcpy(tstate,states[conn->tcpstateflags & UIP_TS_MASK]);
239  numprinted += httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted,
240  httpd_cgi_tcpstat2,
241  uip_htons(conn->rport),
242  tstate,
243  conn->nrtx,
244  conn->timer,
245  (uip_outstanding(conn))? '*':' ',
246  (uip_stopped(conn))? '!':' ');
247 
248  return numprinted;
249 }
250 /*---------------------------------------------------------------------------*/
251 static
252 PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
253 {
254 
255  PSOCK_BEGIN(&s->sout);
256 
257  for(s->u.count = 0; s->u.count < UIP_CONNS; ++s->u.count) {
258  if((uip_conns[s->u.count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
259  PSOCK_GENERATOR_SEND(&s->sout, make_tcp_stats, s);
260  }
261  }
262 
263  PSOCK_END(&s->sout);
264 }
265 /*---------------------------------------------------------------------------*/
266 static unsigned short
267 make_processes(void *p)
268 {
269  static const char httpd_cgi_proc[] HTTPD_STRING_ATTR = "<tr align=\"center\"><td>%p</td><td>%s</td><td>%p</td><td>%s</td></tr>\r\n";
270  char name[40],tstate[20];
271 
272  strncpy(name, PROCESS_NAME_STRING((struct process *)p), 40);
273  petsciiconv_toascii(name, 40);
274  httpd_strcpy(tstate,states[9 + ((struct process *)p)->state]);
275  return httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_proc, p, name,
276  *(char *)(&(((struct process *)p)->thread)),
277 
278  tstate);
279 }
280 /*---------------------------------------------------------------------------*/
281 static
282 PT_THREAD(processes(struct httpd_state *s, char *ptr))
283 {
284  PSOCK_BEGIN(&s->sout);
285  for(s->u.ptr = PROCESS_LIST(); s->u.ptr != NULL; s->u.ptr = ((struct process *)s->u.ptr)->next) {
286  PSOCK_GENERATOR_SEND(&s->sout, make_processes, s->u.ptr);
287  }
288  PSOCK_END(&s->sout);
289 }
290 /*---------------------------------------------------------------------------*/
291 static const char httpd_cgi_addrh[] HTTPD_STRING_ATTR = "<code>";
292 static const char httpd_cgi_addrf[] HTTPD_STRING_ATTR = "</code>[Room for %u more]";
293 static const char httpd_cgi_addrb[] HTTPD_STRING_ATTR = "<br>";
294 static const char httpd_cgi_addrn[] HTTPD_STRING_ATTR = "(none)<br>";
297 extern uip_ds6_netif_t uip_ds6_if;
298 
299 static unsigned short
300 make_addresses(void *p)
301 {
302 uint8_t i,j=0;
303 uint16_t numprinted;
304  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
305  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
306  if (uip_ds6_if.addr_list[i].isused) {
307  j++;
308  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, uip_appdata + numprinted);
309  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrb);
310  }
311  }
312 //if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
313  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf, UIP_DS6_ADDR_NB-j);
314  return numprinted;
315 }
316 /*---------------------------------------------------------------------------*/
317 static
318 PT_THREAD(addresses(struct httpd_state *s, char *ptr))
319 {
320  PSOCK_BEGIN(&s->sout);
321 
322  PSOCK_GENERATOR_SEND(&s->sout, make_addresses, s->u.ptr);
323 
324  PSOCK_END(&s->sout);
325 }
326 /*---------------------------------------------------------------------------*/
327 static unsigned short
328 make_neighbors(void *p)
329 {
330 uint8_t i,j=0;
331 uint16_t numprinted;
332  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
333  for (i=0; i<UIP_DS6_NBR_NB;i++) {
334  if (uip_ds6_nbr_cache[i].isused) {
335  j++;
336  numprinted += httpd_cgi_sprint_ip6(uip_ds6_nbr_cache[i].ipaddr, uip_appdata + numprinted);
337  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrb);
338  }
339  }
340 //if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
341  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf,UIP_DS6_NBR_NB-j);
342  return numprinted;
343 }
344 /*---------------------------------------------------------------------------*/
345 static
346 PT_THREAD(neighbors(struct httpd_state *s, char *ptr))
347 {
348  PSOCK_BEGIN(&s->sout);
349 
350  PSOCK_GENERATOR_SEND(&s->sout, make_neighbors, s->u.ptr);
351 
352  PSOCK_END(&s->sout);
353 }
354 /*---------------------------------------------------------------------------*/
355 static unsigned short
356 make_routes(void *p)
357 {
358 static const char httpd_cgi_rtes1[] HTTPD_STRING_ATTR = "(%u (via ";
359 static const char httpd_cgi_rtes2[] HTTPD_STRING_ATTR = ") %lus<br>";
360 static const char httpd_cgi_rtes3[] HTTPD_STRING_ATTR = ")<br>";
361 uint8_t i,j=0;
362 uint16_t numprinted;
363  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
364  for (i=0; i<UIP_DS6_ROUTE_NB;i++) {
365  if (uip_ds6_routing_table[i].isused) {
366  j++;
367  numprinted += httpd_cgi_sprint_ip6(uip_ds6_routing_table[i].ipaddr, uip_appdata + numprinted);
368  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes1, uip_ds6_routing_table[i].length);
369  numprinted += httpd_cgi_sprint_ip6(uip_ds6_routing_table[i].nexthop, uip_appdata + numprinted);
370  if(uip_ds6_routing_table[i].state.lifetime < 3600) {
371  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes2, uip_ds6_routing_table[i].state.lifetime);
372  } else {
373  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes3);
374  }
375  }
376  }
377  if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
378  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf,UIP_DS6_ROUTE_NB-j);
379  return numprinted;
380 }
381 /*---------------------------------------------------------------------------*/
382 static
383 PT_THREAD(routes(struct httpd_state *s, char *ptr))
384 {
385  PSOCK_BEGIN(&s->sout);
386 
387  PSOCK_GENERATOR_SEND(&s->sout, make_routes, s->u.ptr);
388 
389  PSOCK_END(&s->sout);
390 }
391 /*---------------------------------------------------------------------------*/
392 static unsigned short
393 generate_sensor_readings(void *arg)
394 {
395  uint16_t numprinted;
396  uint16_t h,m,s;
397  uint8_t p1;
398  static const char httpd_cgi_sensor0[] HTTPD_STRING_ATTR = "[Updated %d seconds ago]<br><br>";
399 // static const char httpd_cgi_sensor1[] HTTPD_STRING_ATTR = "<em>Temperature:</em> %s<br>";
400 // static const char httpd_cgi_sensor2[] HTTPD_STRING_ATTR = "<em>Battery:</em> %s<br>";
401  static const char httpd_cgi_sensr12[] HTTPD_STRING_ATTR = "<em>Temperature:</em> %s <em>Battery:<em> %s<br>";
402  static const char httpd_cgi_sensor3[] HTTPD_STRING_ATTR = "<em>Elapsed timer :</em> %02d:%02d:%02d<br>";
403  static const char httpd_cgi_sensor4[] HTTPD_STRING_ATTR = "<em>Sleeping time :</em> %02d:%02d:%02d (%d%%)<br>";
404 
405  numprinted=0;
406  if (last_tempupdate) {
407  numprinted =httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_sensor0,seconds-last_tempupdate);
408  }
409 
410 // numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor1, sensor_temperature);
411  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensr12, sensor_temperature,sensor_extvoltage);
412 
413 #if 0
414 //Measuring AVcc might be useful to check on battery condition but on ext power it's always 3v3
415  ADMUX =0x1E; //Select AREF as reference, measure 1.1 volt bandgap reference.
416 //ADMUX =0x5E; //Select AVCC as reference, measure 1.1 volt bandgap reference.
417  ADCSRA=0x07; //Enable ADC, not free running, interrupt disabled, clock divider 128 (62 KHz@ 8 MHz)
418  ADCSRA|=1<<ADSC; //Start throwaway conversion
419  while (ADCSRA&(1<<ADSC)); //Wait till done
420  ADCSRA|=1<<ADSC; //Start another conversion
421  while (ADCSRA&(1<<ADSC)); //Wait till done
422  h=1131632UL/ADC; //Get supply voltage
423 #endif
424 
425  // numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor2, sensor_extvoltage);
426 #if RADIOSTATS
427  /* Remember radioontime for display below - slow connection might make it report longer than cpu ontime! */
428  savedradioontime = radioontime;
429 #endif
430  h=seconds/3600;
431  s=seconds-h*3600;
432  m=s/60;
433  s=s-m*60;
434  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor3, h,m,s);
435  if (sleepseconds) {
436  p1=100UL*sleepseconds/seconds;
437  h=sleepseconds/3600;
438  s=sleepseconds-h*3600;
439  m=s/60;
440  s=s-m*60;
441  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor4, h,m,s,p1);
442  }
443  return numprinted;
444 }
445 #if RADIOSTATS
446 /*---------------------------------------------------------------------------*/
447 static unsigned short
448 generate_radio_stats(void *arg)
449 {
450  uint16_t numprinted;
451  uint16_t h,m,s;
452  uint8_t p1,p2;
453  static const char httpd_cgi_sensor10[] HTTPD_STRING_ATTR = "<em>Radio on time :</em> %02d:%02d:%02d (%d.%02d%%)<br>";
454  static const char httpd_cgi_sensor11[] HTTPD_STRING_ATTR = "<em>Packets:</em> Tx=%5d Rx=%5d TxL=%5d RxL=%5d RSSI=%2ddBm\n";
455 
456  s=(10000UL*savedradioontime)/seconds;
457  p1=s/100;
458  p2=s-p1*100;
459  h=savedradioontime/3600;
460  s=savedradioontime-h*3600;
461  m=s/60;
462  s=s-m*60;
463 
464  numprinted =httpd_snprintf((char *)uip_appdata , uip_mss() , httpd_cgi_sensor10,\
465  h,m,s,p1,p2);
466 
467 #if RF230BB
468  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor11,\
469  RF230_sendpackets,RF230_receivepackets,RF230_sendfail,RF230_receivefail,-92+rf230_last_rssi);
470 #else
471  p1=0;
473  p1 = -91*3(p1-1);
474  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor11,\
475  RF230_sendpackets,RF230_receivepackets,RF230_sendfail,RF230_receivefail,p1);
476 #endif
477 
478  return numprinted;
479 }
480 #endif
481 /*---------------------------------------------------------------------------*/
482 static
483 PT_THREAD(sensor_readings(struct httpd_state *s, char *ptr))
484 {
485  PSOCK_BEGIN(&s->sout);
486 
487  PSOCK_GENERATOR_SEND(&s->sout, generate_sensor_readings, s);
488 #if RADIOSTATS
489  PSOCK_GENERATOR_SEND(&s->sout, generate_radio_stats, s);
490 #endif
491 
492 
493  PSOCK_END(&s->sout);
494 }
495 /*---------------------------------------------------------------------------*/
496 void
497 httpd_cgi_add(struct httpd_cgi_call *c)
498 {
499  struct httpd_cgi_call *l;
500 
501  c->next = NULL;
502  if(calls == NULL) {
503  calls = c;
504  } else {
505  for(l = calls; l->next != NULL; l = l->next);
506  l->next = c;
507  }
508 }
509 /*---------------------------------------------------------------------------*/
510 
511 #if HTTPD_FS_STATISTICS
512 HTTPD_CGI_CALL( file, file_name, file_stats);
513 #endif
514 HTTPD_CGI_CALL( tcp, tcp_name, tcp_stats );
515 HTTPD_CGI_CALL( proc, proc_name, processes );
516 HTTPD_CGI_CALL( adrs, adrs_name, addresses );
517 HTTPD_CGI_CALL( nbrs, nbrs_name, neighbors );
518 HTTPD_CGI_CALL( rtes, rtes_name, routes );
519 HTTPD_CGI_CALL(sensors, sensor_name, sensor_readings);
520 
521 void
522 httpd_cgi_init(void)
523 {
524 #if HTTPD_FS_STATISTICS
525  httpd_cgi_add( &file);
526 #endif
527  httpd_cgi_add( &tcp);
528  httpd_cgi_add( &proc);
529  httpd_cgi_add( &adrs);
530  httpd_cgi_add( &nbrs);
531  httpd_cgi_add( &rtes);
532  httpd_cgi_add(&sensors);
533 }
534 /*---------------------------------------------------------------------------*/
535 
536 uint8_t httpd_cgi_sprint_ip6(uip_ip6addr_t addr, char * result)
537  {
538  unsigned char zerocnt = 0;
539  unsigned char numprinted = 0;
540  char * starting = result;
541 
542  unsigned char i = 0;
543 
544  while (numprinted < 8)
545  {
546  //Address is zero, have we used our ability to
547  //replace a bunch with : yet?
548  if ((addr.u16[i] == 0) && (zerocnt == 0))
549  {
550  //How mant zeros?
551  zerocnt = 0;
552  while(addr.u16[zerocnt + i] == 0)
553  zerocnt++;
554 
555  //just one, don't waste our zeros...
556  if (zerocnt == 1)
557  {
558  *result++ = '0';
559  numprinted++;
560  break;
561  }
562 
563  //Cool - can replace a bunch of zeros
564  i += zerocnt;
565  numprinted += zerocnt;
566  }
567  //Normal address, just print it
568  else
569  {
570  result += sprintf(result, "%x", (unsigned int)(uip_ntohs(addr.u16[i])));
571  i++;
572  numprinted++;
573  }
574 
575  //Don't print : on last one
576  if (numprinted != 8)
577  *result++ = ':';
578  }
579 
580  return (result - starting);
581  }
582