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