Contiki 3.x
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  *
32  */
33 #include <util/delay.h>
34 #define delay_us( us ) ( _delay_us( ( us ) ) )
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 #if 0
121 void
122 web_set_temp(char *s)
123 {
124  strcpy(sensor_temperature, s);
125  last_tempupdate=seconds;
126 }
127 void
128 web_set_voltage(char *s)
129 {
130  strcpy(sensor_extvoltage, s);
131  last_extvoltageupdate=seconds;
132 }
133 #endif
134 /*---------------------------------------------------------------------------*/
135 static
136 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
137 {
138  PSOCK_BEGIN(&s->sout);
139  PSOCK_END(&s->sout);
140 }
141 /*---------------------------------------------------------------------------*/
142 httpd_cgifunction
143 httpd_cgi(char *name)
144 {
145  struct httpd_cgi_call *f;
146 
147  /* Find the matching name in the table, return the function. */
148  for(f = calls; f != NULL; f = f->next) {
149  if(httpd_strncmp(name, f->name, httpd_strlen(f->name)) == 0) {
150  return f->function;
151  }
152  }
153  return nullfunction;
154 }
155 
156 #if HTTPD_FS_STATISTICS
157 static char *thisfilename;
158 /*---------------------------------------------------------------------------*/
159 static unsigned short
160 generate_file_stats(void *arg)
161 {
162  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>";
163  static const char httpd_cgi_filestat2[] HTTPD_STRING_ATTR = "<tr><td><a href=\"%s\">%s</a></td><td>%d</td>";
164  static const char httpd_cgi_filestat3[] HTTPD_STRING_ATTR = "%5u";
165  char tmp[20];
166  struct httpd_fsdata_file_noconst *f,fram;
167  uint16_t i;
168  unsigned short numprinted;
169 
170  /* Transfer arg from whichever flash that contains the html file to RAM */
171  httpd_fs_cpy(&tmp, (char *)arg, 20);
172 
173  /* Count for this page, with common page footer */
174  if (tmp[0]=='.') {
175  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat1, httpd_fs_open(thisfilename, 0));
176 
177  /* Count for all files */
178  /* Note buffer will overflow if there are too many files! */
179  } else if (tmp[0]=='*') {
180  i=0;numprinted=0;
181  for(f = (struct httpd_fsdata_file_noconst *)httpd_fs_get_root();
182  f != NULL;
183  f = (struct httpd_fsdata_file_noconst *)fram.next) {
184 
185  /* Get the linked list file entry into RAM from from wherever it is*/
186  httpd_memcpy(&fram,f,sizeof(fram));
187 
188  /* Get the file name from whatever memory it is in */
189  httpd_fs_cpy(&tmp, fram.name, sizeof(tmp));
190 #if HTTPD_FS_STATISTICS==1
191  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, f->count);
192 #elif HTTPD_FS_STATISTICS==2
193  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, httpd_filecount[i]);
194 #endif
195  i++;
196  }
197 
198  /* Count for specified file */
199  } else {
200  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat3, httpd_fs_open(tmp, 0));
201  }
202 #if DEBUGLOGIC
203  return 0;
204 #endif
205  return numprinted;
206 }
207 /*---------------------------------------------------------------------------*/
208 static
209 PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
210 {
211 
212  PSOCK_BEGIN(&s->sout);
213 
214  thisfilename=&s->filename[0]; //temporary way to pass filename to generate_file_stats
215 
216  PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, (void *) ptr);
217 
218  PSOCK_END(&s->sout);
219 }
220 #endif /*HTTPD_FS_STATISTICS*/
221 /*---------------------------------------------------------------------------*/
222 static unsigned short
223 make_tcp_stats(void *arg)
224 {
225  static const char httpd_cgi_tcpstat1[] HTTPD_STRING_ATTR = "<tr align=\"center\"><td>%d</td><td>";
226  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";
227  struct uip_conn *conn;
228  struct httpd_state *s = (struct httpd_state *)arg;
229  char tstate[20];
230  uint16_t numprinted;
231 
232  conn = &uip_conns[s->u.count];
233 
234  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_tcpstat1, uip_htons(conn->lport));
235  numprinted += httpd_cgi_sprint_ip6(conn->ripaddr, uip_appdata + numprinted);
236  httpd_strcpy(tstate,states[conn->tcpstateflags & UIP_TS_MASK]);
237  numprinted += httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted,
238  httpd_cgi_tcpstat2,
239  uip_htons(conn->rport),
240  tstate,
241  conn->nrtx,
242  conn->timer,
243  (uip_outstanding(conn))? '*':' ',
244  (uip_stopped(conn))? '!':' ');
245 
246  return numprinted;
247 }
248 /*---------------------------------------------------------------------------*/
249 static
250 PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
251 {
252 
253  PSOCK_BEGIN(&s->sout);
254 
255  for(s->u.count = 0; s->u.count < UIP_CONNS; ++s->u.count) {
256  if((uip_conns[s->u.count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
257  PSOCK_GENERATOR_SEND(&s->sout, make_tcp_stats, s);
258  }
259  }
260 
261  PSOCK_END(&s->sout);
262 }
263 /*---------------------------------------------------------------------------*/
264 static unsigned short
265 make_processes(void *p)
266 {
267  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";
268  char name[40],tstate[20];
269 
270  strncpy(name, PROCESS_NAME_STRING((struct process *)p), 40);
271  petsciiconv_toascii(name, 40);
272  httpd_strcpy(tstate,states[9 + ((struct process *)p)->state]);
273  return httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_proc, p, name,
274  *(char *)(&(((struct process *)p)->thread)),
275 
276  tstate);
277 }
278 /*---------------------------------------------------------------------------*/
279 static
280 PT_THREAD(processes(struct httpd_state *s, char *ptr))
281 {
282  PSOCK_BEGIN(&s->sout);
283  for(s->u.ptr = PROCESS_LIST(); s->u.ptr != NULL; s->u.ptr = ((struct process *)s->u.ptr)->next) {
284  PSOCK_GENERATOR_SEND(&s->sout, make_processes, s->u.ptr);
285  }
286  PSOCK_END(&s->sout);
287 }
288 /*---------------------------------------------------------------------------*/
289 static const char httpd_cgi_addrh[] HTTPD_STRING_ATTR = "<code>";
290 static const char httpd_cgi_addrf[] HTTPD_STRING_ATTR = "</code>[Room for %u more]";
291 static const char httpd_cgi_addrb[] HTTPD_STRING_ATTR = "<br>";
292 static const char httpd_cgi_addrn[] HTTPD_STRING_ATTR = "(none)<br>";
293 extern uip_ds6_netif_t uip_ds6_if;
294 
295 static unsigned short
296 make_addresses(void *p)
297 {
298 uint8_t i,j=0;
299 uint16_t numprinted;
300  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
301  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
302  if (uip_ds6_if.addr_list[i].isused) {
303  j++;
304  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, uip_appdata + numprinted);
305  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrb);
306  }
307  }
308 //if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
309  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf, UIP_DS6_ADDR_NB-j);
310  return numprinted;
311 }
312 /*---------------------------------------------------------------------------*/
313 static
314 PT_THREAD(addresses(struct httpd_state *s, char *ptr))
315 {
316  PSOCK_BEGIN(&s->sout);
317 
318  PSOCK_GENERATOR_SEND(&s->sout, make_addresses, s->u.ptr);
319 
320  PSOCK_END(&s->sout);
321 }
322 /*---------------------------------------------------------------------------*/
323 static unsigned short
324 make_neighbors(void *p)
325 {
326 uint8_t i,j=0;
327 uint16_t numprinted;
328  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
329  uip_ds6_nbr_t *nbr;
330  for(nbr = nbr_table_head(ds6_neighbors);
331  nbr != NULL;
332  nbr = nbr_table_next(ds6_neighbors, nbr)) {
333  j++;
334  numprinted += httpd_cgi_sprint_ip6(nbr->ipaddr, uip_appdata + numprinted);
335  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrb);
336  }
337 //if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
338  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf,NBR_TABLE_MAX_NEIGHBORS-j);
339  return numprinted;
340 }
341 /*---------------------------------------------------------------------------*/
342 static
343 PT_THREAD(neighbors(struct httpd_state *s, char *ptr))
344 {
345  PSOCK_BEGIN(&s->sout);
346 
347  PSOCK_GENERATOR_SEND(&s->sout, make_neighbors, s->u.ptr);
348 
349  PSOCK_END(&s->sout);
350 }
351 /*---------------------------------------------------------------------------*/
352 static unsigned short
353 make_routes(void *p)
354 {
355 static const char httpd_cgi_rtes1[] HTTPD_STRING_ATTR = "(%u (via ";
356 static const char httpd_cgi_rtes2[] HTTPD_STRING_ATTR = ") %lus<br>";
357 static const char httpd_cgi_rtes3[] HTTPD_STRING_ATTR = ")<br>";
358 uint8_t i,j=0;
359 uint16_t numprinted;
360 uip_ds6_route_t *r;
361  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(),httpd_cgi_addrh);
362  for(r = uip_ds6_route_head();
363  r != NULL;
364  r = uip_ds6_route_next(r)) {
365  j++;
366  numprinted += httpd_cgi_sprint_ip6(r->ipaddr, uip_appdata + numprinted);
367  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes1, r->length);
368  numprinted += httpd_cgi_sprint_ip6(uip_ds6_route_nexthop(r), uip_appdata + numprinted);
369  if(r->state.lifetime < 3600) {
370  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes2, r->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 
#define UIP_CONNS
The maximum number of simultaneously open TCP connections.
Definition: uipopt.h:419
An entry in the routing table.
radio_status_t radio_get_rssi_value(uint8_t *rssi)
This function returns the Received Signal Strength Indication.
Definition: radio.c:485
#define PSOCK_BEGIN(psock)
Start the protosocket protothread in a function.
Definition: psock.h:164
Representation of a uIP TCP connection.
Definition: uip.h:1336
uint8_t nrtx
The number of retransmissions for the last segment sent.
Definition: uip.h:1359
#define uip_mss()
Get the current maximum segment size that can be sent on the current connection.
Definition: uip.h:838
uint16_t lport
The local TCP port, in network byte order.
Definition: uip.h:1339
#define NULL
The null pointer.
uint8_t timer
The retransmission timer.
Definition: uip.h:1358
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
uint16_t rport
The local remote TCP port, in network byte order.
Definition: uip.h:1340
PETSCII/ASCII conversion functions.
#define PSOCK_GENERATOR_SEND(psock, generator, arg)
Generate data with a function and send it.
Definition: psock.h:225
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298
#define PSOCK_END(psock)
Declare the end of a protosocket&#39;s protothread.
Definition: psock.h:348
uint8_t tcpstateflags
TCP state and flags.
Definition: uip.h:1357
Interface structure (contains all the interface variables)
Definition: uip-ds6.h:212
#define uip_stopped(conn)
Find out if the current connection has been previously stopped with uip_stop().
Definition: uip.h:700
uip_ipaddr_t ripaddr
The IP address of the remote host.
Definition: uip.h:1337
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
An entry in the nbr cache.
Definition: uip-ds6-nbr.h:70