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 /*
34  * This file includes functions that are called by the web server
35  * scripts. The functions takes no argument, and the return value is
36  * interpreted as follows. A zero means that the function did not
37  * complete and should be invoked for the next packet as well. A
38  * non-zero value indicates that the function has completed and that
39  * the web server should move along to the next script line.
40  *
41  */
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "contiki-net.h"
48 #include "httpd.h"
49 #include "httpd-cgi.h"
50 #include "httpd-fs.h"
51 #include "httpd-fsdata.h"
52 #include "lib/petsciiconv.h"
53 #include "net/nbr-table.h"
54 
55 #include "sensors.h"
56 
57 /* RADIOSTATS must also be set in clock.c and the radio driver */
58 #if RF230BB
59 #define RADIOSTATS 1
60 #endif
61 #if CONTIKI_TARGET_REDBEE_ECONOTAG
62 #include "adc.h"
63 #endif
64 short sleepcount;
65 #if WEBSERVER_CONF_CGI
66 static struct httpd_cgi_call *calls = NULL;
67 /*cgi function names*/
68 #if WEBSERVER_CONF_HEADER
69 static const char hdr_name[] HTTPD_STRING_ATTR = "header";
70 #endif
71 #if WEBSERVER_CONF_FILESTATS
72 static const char file_name[] HTTPD_STRING_ATTR = "file-stats";
73 #endif
74 #if WEBSERVER_CONF_TCPSTATS
75 static const char tcp_name[] HTTPD_STRING_ATTR = "tcp-connections";
76 #endif
77 #if WEBSERVER_CONF_PROCESSES
78 static const char proc_name[] HTTPD_STRING_ATTR = "processes";
79 #endif
80 #if WEBSERVER_CONF_SENSORS
81 static const char sensor_name[] HTTPD_STRING_ATTR = "sensors";
82 #endif
83 #if WEBSERVER_CONF_ADDRESSES
84 static const char adrs_name[] HTTPD_STRING_ATTR = "addresses";
85 #endif
86 #if WEBSERVER_CONF_NEIGHBORS
87 static const char nbrs_name[] HTTPD_STRING_ATTR = "neighbors";
88 #endif
89 #if WEBSERVER_CONF_ROUTES
90 static const char rtes_name[] HTTPD_STRING_ATTR = "routes";
91 #endif
92 #if WEBSERVER_CONF_TICTACTOE
93 static const char tictac_name[] HTTPD_STRING_ATTR = "tictac";
94 #endif
95 #if WEBSERVER_CONF_AJAX
96 static const char ajax_name[] HTTPD_STRING_ATTR = "ajaxdata";
97 #endif
98 #endif
99 
100 /*Process states for processes cgi*/
101 
102 #if WEBSERVER_CONF_PROCESSES || WEBSERVER_CONF_TCPSTATS
103 static const char closed[] HTTPD_STRING_ATTR = "CLOSED";
104 static const char syn_rcvd[] HTTPD_STRING_ATTR = "SYN-RCVD";
105 static const char syn_sent[] HTTPD_STRING_ATTR = "SYN-SENT";
106 static const char established[] HTTPD_STRING_ATTR = "ESTABLISHED";
107 static const char fin_wait_1[] HTTPD_STRING_ATTR = "FIN-WAIT-1";
108 static const char fin_wait_2[] HTTPD_STRING_ATTR = "FIN-WAIT-2";
109 static const char closing[] HTTPD_STRING_ATTR = "CLOSING";
110 static const char time_wait[] HTTPD_STRING_ATTR = "TIME-WAIT";
111 static const char last_ack[] HTTPD_STRING_ATTR = "LAST-ACK";
112 static const char none[] HTTPD_STRING_ATTR = "NONE";
113 static const char running[] HTTPD_STRING_ATTR = "RUNNING";
114 static const char called[] HTTPD_STRING_ATTR = "CALLED";
115 static const char *states[] = {
116  closed,
117  syn_rcvd,
118  syn_sent,
119  established,
120  fin_wait_1,
121  fin_wait_2,
122  closing,
123  time_wait,
124  last_ack,
125  none,
126  running,
127  called};
128 #endif
129 
130 #if WEBSERVER_CONF_SENSORS
131  static char sensor_temperature[14]="Not Available";
132  static char sensor_extvoltage[14]="Not Available";
133  static unsigned long last_tempupdate;//,last_extvoltageupdate;
134 // extern unsigned long seconds, sleepseconds;
135 #if RADIOSTATS
136  extern unsigned long radioontime;
137  static unsigned long savedradioontime;
138  extern uint8_t RF230_radio_on, rf230_last_rssi;
139  extern uint16_t RF230_sendpackets,RF230_receivepackets,RF230_sendfail,RF230_receivefail;
140 #endif
141 #endif
142 
143 #if 0
144 void
145 web_set_temp(char *s)
146 {
147  strcpy(sensor_temperature, s);
148 // printf_P(PSTR("got temp"));
149  last_tempupdate=seconds;
150 }
151 void
152 web_set_voltage(char *s)
153 {
154  strcpy(sensor_extvoltage, s);
155 // printf_P(PSTR("got volts"));
156  last_extvoltageupdate=seconds;
157 }
158 #endif
159 
160 #if WEBSERVER_CONF_CGI
161 /*---------------------------------------------------------------------------*/
162 static
163 PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
164 {
165  PSOCK_BEGIN(&s->sout);
166  PSOCK_END(&s->sout);
167 }
168 /*---------------------------------------------------------------------------*/
169 httpd_cgifunction
170 httpd_cgi(char *name)
171 {
172  struct httpd_cgi_call *f;
173 
174  /* Find the matching name in the table, return the function. */
175  for(f = calls; f != NULL; f = f->next) {
176  if(httpd_strncmp(name, f->name, httpd_strlen(f->name)) == 0) {
177  return f->function;
178  }
179  }
180  return nullfunction;
181 }
182 #if WEBSERVER_CONF_HEADER
183 /*---------------------------------------------------------------------------*/
184 static unsigned short
185 generate_header(void *arg)
186 {
187  unsigned short numprinted=0;
188 
189 #if WEBSERVER_CONF_HEADER_W3C
190 #define _MSS1 100
191  static const char httpd_cgi_headerw[] HTTPD_STRING_ATTR = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
192  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerw);
193 #endif
194 
195 #if WEBSERVER_CONF_HEADER_ICON
196 #define _MSS2 105
197 #ifdef WEBSERVER_CONF_PAGETITLE
198  static const char httpd_cgi_header1[] HTTPD_STRING_ATTR = "<html><head><title>%s</title><link rel=\"icon\" href=\"favicon.gif\" type=\"image/gif\"></head><body>";
199 #else
200  static const char httpd_cgi_header1[] HTTPD_STRING_ATTR = "<html><head><title>Contiki-Nano</title><link rel=\"icon\" href=\"favicon.gif\" type=\"image/gif\"></head><body>";
201 #endif
202 #else
203 #define _MSS2 52
204 #ifdef WEBSERVER_CONF_PAGETITLE
205  static const char httpd_cgi_header1[] HTTPD_STRING_ATTR = "<html><head><title>%s</title></head><body>";
206 #else
207  static const char httpd_cgi_header1[] HTTPD_STRING_ATTR = "<html><head><title>Contiki-Nano</title></head><body>";
208 #endif
209 #endif
210 
211 #ifdef WEBSERVER_CONF_PAGETITLE
212 #define WAD ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr.u8
213 { char buf[40];
214  WEBSERVER_CONF_PAGETITLE;
215  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_header1,buf);
216 }
217 #else
218  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_header1);
219 #endif
220 
221 #if WEBSERVER_CONF_HEADER_MENU
222 #define _MSS3 32
223  static const char httpd_cgi_headerm1[] HTTPD_STRING_ATTR = "<pre><a href=\"/\">Front page</a>";
224  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm1);
225 #if WEBSERVER_CONF_SENSORS
226 #define _MSS4 34
227  static const char httpd_cgi_headerm2[] HTTPD_STRING_ATTR = "|<a href=\"status.shtml\">Status</a>";
228  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm2);
229 #endif
230 #if WEBSERVER_CONF_TCPSTATS
231 #define _MSS5 44
232  static const char httpd_cgi_headerm3[] HTTPD_STRING_ATTR = "|<a href=\"tcp.shtml\">Network connections</a>";
233  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm3);
234 #endif
235 #if WEBSERVER_CONF_PROCESSES
236 #define _MSS6 46
237  static const char httpd_cgi_headerm4[] HTTPD_STRING_ATTR = "|<a href=\"processes.shtml\">System processes</a>";
238  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm4);
239 #endif
240 #if WEBSERVER_CONF_FILESTATS
241 #define _MSS7 45
242  static const char httpd_cgi_headerm5[] HTTPD_STRING_ATTR = "|<a href=\"files.shtml\">File statistics</a>";
243  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm5);
244 #endif
245 #if WEBSERVER_CONF_TICTACTOE
246 #define _MSS8 44
247  static const char httpd_cgi_headerm6[] HTTPD_STRING_ATTR = "|<a href=\"/ttt/ttt.shtml\">TicTacToe</a>";
248  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm6);
249 #endif
250 #if WEBSERVER_CONF_AJAX
251 #define _MSS9 30
252  static const char httpd_cgi_headerm7[] HTTPD_STRING_ATTR = "|<a href=\"ajax.shtml\">Ajax</a>";
253  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerm7);
254 #endif
255  static const char httpd_cgi_headerme[] HTTPD_STRING_ATTR = "</pre>";
256  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_headerme);
257 #endif /* WEBSERVER_CONF_MENU */
258 
259 #if UIP_RECEIVE_WINDOW < _MSS1+_MSS2+_MSS3_+MSS4_+MSS5_MSS6+_MSS7+_MSS8+_MSS9
260 #warning ************************************************************
261 #warning UIP_RECEIVE_WINDOW not large enough for header cgi output.
262 #warning Web pages will not render properly!
263 #warning ************************************************************
264 #endif
265  return numprinted;
266 }
267 /*---------------------------------------------------------------------------*/
268 static
269 PT_THREAD(header(struct httpd_state *s, char *ptr))
270 {
271 
272  PSOCK_BEGIN(&s->sout);
273 
274  PSOCK_GENERATOR_SEND(&s->sout, generate_header, (void *) ptr);
275 
276  PSOCK_END(&s->sout);
277 }
278 #endif /* WEBSERVER_CONF_HEADER */
279 
280 #if WEBSERVER_CONF_FILESTATS
281 /*---------------------------------------------------------------------------*/
282 static unsigned short
283 generate_file_stats(void *arg)
284 {
285  struct httpd_state *s = (struct httpd_state *)arg;
286 #if WEBSERVER_CONF_LOADTIME
287  static const char httpd_cgi_filestat1[] HTTPD_STRING_ATTR = "<p align=\"right\"><br><br><i>This page has been sent %u times (%1u.%02u sec)</i></body></html>";
288 #else
289  static const char httpd_cgi_filestat1[] HTTPD_STRING_ATTR = "<p align=\"right\"><br><br><i>This page has been sent %u times</i></body></html>";
290 #endif
291  static const char httpd_cgi_filestat2[] HTTPD_STRING_ATTR = "<tr><td><a href=\"%s\">%s</a></td><td>%d</td>";
292  static const char httpd_cgi_filestat3[] HTTPD_STRING_ATTR = "%5u";
293  char tmp[20];
294  struct httpd_fsdata_file_noconst *f,fram;
295  uint16_t i;
296  unsigned short numprinted;
297  /* Transfer arg from whichever flash that contains the html file to RAM */
298  httpd_fs_cpy(&tmp, s->u.ptr, 20);
299 
300  /* Count for this page, with common page footer */
301  if (tmp[0]=='.') {
302 #if WEBSERVER_CONF_LOADTIME
303  s->pagetime = clock_time() - s->pagetime;
304  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat1, httpd_fs_open(s->filename, 0),
305  (unsigned int)s->pagetime/CLOCK_SECOND,(100*((unsigned int)s->pagetime%CLOCK_SECOND))/CLOCK_SECOND);
306 #else
307  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat1, httpd_fs_open(s->filename, 0));
308 #endif
309 
310  /* Count for all files */
311  /* Note buffer will overflow if there are too many files! */
312  } else if (tmp[0]=='*') {
313  i=0;numprinted=0;
314  for(f = (struct httpd_fsdata_file_noconst *)httpd_fs_get_root();
315  f != NULL;
316  f = (struct httpd_fsdata_file_noconst *)fram.next) {
317 
318  /* Get the linked list file entry into RAM from from wherever it is*/
319  httpd_memcpy(&fram,f,sizeof(fram));
320  /* Get the file name from whatever memory it is in */
321  httpd_fs_cpy(&tmp, fram.name, sizeof(tmp));
322 #if WEBSERVER_CONF_FILESTATS==2
323  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, f->count);
324 #else
325  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, httpd_filecount[i]);
326 #endif
327  i++;
328  }
329 
330  /* Count for specified file */
331  } else {
332  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat3, httpd_fs_open(tmp, 0));
333  }
334  return numprinted;
335 }
336 /*---------------------------------------------------------------------------*/
337 static
338 PT_THREAD(file_stats(struct httpd_state *s, char *ptr))
339 {
340 
341  PSOCK_BEGIN(&s->sout);
342 
343  /* Pass string after cgi invocation to the generator */
344  s->u.ptr = ptr;
345  PSOCK_GENERATOR_SEND(&s->sout, generate_file_stats, s);
346 
347  PSOCK_END(&s->sout);
348 }
349 #endif /* WEBSERVER_CONF_FILESTATS*/
350 
351 #if WEBSERVER_CONF_TCPSTATS
352 /*---------------------------------------------------------------------------*/
353 static unsigned short
354 make_tcp_stats(void *arg)
355 {
356  static const char httpd_cgi_tcpstat1[] HTTPD_STRING_ATTR = "<tr align=\"center\"><td>%d</td><td>";
357  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";
358  static const char httpd_cgi_tcpstat3[] HTTPD_STRING_ATTR = "[Room for %d more]";
359 
360  struct uip_conn *conn;
361  struct httpd_state *s = (struct httpd_state *)arg;
362  char tstate[20];
363  uint16_t numprinted;
364 
365  if (s->u.count==UIP_CONNS){
366  for(numprinted = 0; numprinted < UIP_CONNS; numprinted++ ) {
367  if((uip_conns[numprinted].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) s->u.count--;
368  }
369  return(httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_tcpstat3, s->u.count));
370  }
371 
372  conn = &uip_conns[s->u.count];
373 
374  numprinted = httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_tcpstat1, uip_htons(conn->lport));
375  numprinted += httpd_cgi_sprint_ip6(conn->ripaddr, uip_appdata + numprinted);
376  httpd_strcpy(tstate,states[conn->tcpstateflags & UIP_TS_MASK]);
377  numprinted += httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted,
378  httpd_cgi_tcpstat2,
379  uip_htons(conn->rport),
380  tstate,
381  conn->nrtx,
382  conn->timer,
383  (uip_outstanding(conn))? '*':' ',
384  (uip_stopped(conn))? '!':' ');
385 
386  return numprinted;
387 }
388 /*---------------------------------------------------------------------------*/
389 static
390 PT_THREAD(tcp_stats(struct httpd_state *s, char *ptr))
391 {
392 
393  PSOCK_BEGIN(&s->sout);
394 
395  s->u.count=UIP_CONNS;
396  PSOCK_GENERATOR_SEND(&s->sout, make_tcp_stats, s);
397 
398  for(s->u.count = 0; s->u.count < UIP_CONNS; ++s->u.count) {
399  if((uip_conns[s->u.count].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
400  PSOCK_GENERATOR_SEND(&s->sout, make_tcp_stats, s);
401  }
402  }
403 
404  PSOCK_END(&s->sout);
405 }
406 #endif /* WEBSERVER_CONF_TCPSTATS */
407 
408 #if WEBSERVER_CONF_PROCESSES
409 /*---------------------------------------------------------------------------*/
410 static unsigned short
411 make_processes(void *p)
412 {
413  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";
414  char name[40],tstate[20];
415 
416  strncpy(name, PROCESS_NAME_STRING((struct process *)p), 40);
417  petsciiconv_toascii(name, 40);
418  httpd_strcpy(tstate,states[9 + ((struct process *)p)->state]);
419  return httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_proc, p, name,
420 // *((char **) &(((struct process *)p)->thread)),
421  * (char **)(&(((struct process *)p)->thread)), //minimal net
422  tstate);
423 }
424 /*---------------------------------------------------------------------------*/
425 static
426 PT_THREAD(processes(struct httpd_state *s, char *ptr))
427 {
428  PSOCK_BEGIN(&s->sout);
429  for(s->u.ptr = PROCESS_LIST(); s->u.ptr != NULL; s->u.ptr = ((struct process *)s->u.ptr)->next) {
430  PSOCK_GENERATOR_SEND(&s->sout, make_processes, s->u.ptr);
431  }
432  PSOCK_END(&s->sout);
433 }
434 #endif /* WEBSERVER_CONF_PROCESSES */
435 
436 #if WEBSERVER_CONF_ADDRESSES || WEBSERVER_CONF_NEIGHBORS || WEBSERVER_CONF_ROUTES
437 #if WEBSERVER_CONF_SHOW_ROOM
438 static const char httpd_cgi_addrf[] HTTPD_STRING_ATTR = "[Room for %u more]\n";
439 #else
440 static const char httpd_cgi_addrf[] HTTPD_STRING_ATTR = "[Table is full]\n";
441 #endif
442 static const char httpd_cgi_addrn[] HTTPD_STRING_ATTR = "[None]\n";
443 #endif
444 
445 #if WEBSERVER_CONF_ADDRESSES
446 /*---------------------------------------------------------------------------*/
447 extern uip_ds6_netif_t uip_ds6_if;
448 
449 static unsigned short
450 make_addresses(void *p)
451 {
452 uint8_t i,j=0;
453 uint16_t numprinted = 0;
454  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
455  if (uip_ds6_if.addr_list[i].isused) {
456  j++;
457  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, uip_appdata + numprinted);
458  *((char *)uip_appdata+numprinted++) = '\n';
459  }
460  }
461 #if WEBSERVER_CONF_SHOW_ROOM
462  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf, UIP_DS6_ADDR_NB-j);
463 #else
464  if(UIP_DS6_ADDR_NB == j) {
465  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf);
466  }
467 #endif
468  return numprinted;
469 }
470 /*---------------------------------------------------------------------------*/
471 static
472 PT_THREAD(addresses(struct httpd_state *s, char *ptr))
473 {
474  PSOCK_BEGIN(&s->sout);
475 
476  PSOCK_GENERATOR_SEND(&s->sout, make_addresses, s->u.ptr);
477 
478  PSOCK_END(&s->sout);
479 }
480 #endif /* WEBSERVER_CONF_ADDRESSES */
481 
482 #if WEBSERVER_CONF_NEIGHBORS
483 /*---------------------------------------------------------------------------*/
484 static unsigned short
485 make_neighbors(void *p)
486 {
487 uint8_t i,j;
488 uint16_t numprinted=0;
489 struct httpd_state *s=p;
490 static uip_ds6_nbr_t *nbr;
491  /* Span generator calls over tcp segments */
492  /* Note retransmissions will execute thise code multiple times for a segment */
493  i=s->starti;j=s->startj;
494  for(nbr = nbr_table_head(ds6_neighbors);
495  nbr != NULL;
496  nbr = nbr_table_next(ds6_neighbors, nbr), i++) {
497  j++;
498 
499 #if WEBSERVER_CONF_NEIGHBOR_STATUS
500 static const char httpd_cgi_nbrs1[] HTTPD_STRING_ATTR = " INCOMPLETE";
501 static const char httpd_cgi_nbrs2[] HTTPD_STRING_ATTR = " REACHABLE";
502 static const char httpd_cgi_nbrs3[] HTTPD_STRING_ATTR = " STALE";
503 static const char httpd_cgi_nbrs4[] HTTPD_STRING_ATTR = " DELAY";
504 static const char httpd_cgi_nbrs5[] HTTPD_STRING_ATTR = " NBR_PROBE";
505 {uint16_t k=numprinted+25;
506  numprinted += httpd_cgi_sprint_ip6(nbr->ipaddr, uip_appdata + numprinted);
507  while (numprinted < k) {*((char *)uip_appdata+numprinted++) = ' ';}
508  switch (nbr->state) {
509  case NBR_INCOMPLETE: numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_nbrs1);break;
510  case NBR_REACHABLE: numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_nbrs2);break;
511  case NBR_STALE: numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_nbrs3);break;
512  case NBR_DELAY: numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_nbrs4);break;
513  case NBR_PROBE: numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_nbrs5);break;
514  }
515 }
516 #else
517  numprinted += httpd_cgi_sprint_ip6(nbr->ipaddr, uip_appdata + numprinted);
518 #endif
519  *((char *)uip_appdata+numprinted++) = '\n';
520 
521  /* If buffer near full, send it and wait for the next call. Could be a retransmission, or the next segment */
522  if(numprinted > (uip_mss() - 50)) {
523  s->savei=i;s->savej=j;
524  return numprinted;
525  }
526  }
527 #if WEBSERVER_CONF_SHOW_ROOM
528  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf,NBR_TABLE_MAX_NEIGHBORS-j);
529 #else
530  if(NBR_TABLE_MAX_NEIGHBORS == j) {
531  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf);
532  }
533 #endif
534 
535  /* Signal that this was the last segment */
536  s->savei = 0;
537  return numprinted;
538 }
539 /*---------------------------------------------------------------------------*/
540 static
541 PT_THREAD(neighbors(struct httpd_state *s, char *ptr))
542 {
543  PSOCK_BEGIN(&s->sout);
544 
545  /* Send as many TCP segments as needed for the neighbor table */
546  /* Move to next seqment after each successful transmission */
547  s->starti=s->startj=0;
548  do {
549  PSOCK_GENERATOR_SEND(&s->sout, make_neighbors, (void *)s);
550  s->starti=s->savei+1;s->startj=s->savej;
551  } while(s->savei);
552 
553  PSOCK_END(&s->sout);
554 }
555 #endif
556 
557 #if WEBSERVER_CONF_ROUTES
558 #if WEBSERVER_CONF_ROUTE_LINKS
559 static const char httpd_cgi_rtesl1[] HTTPD_STRING_ATTR = "<a href=http://[";
560 static const char httpd_cgi_rtesl2[] HTTPD_STRING_ATTR = "]/status.shtml>";
561 static const char httpd_cgi_rtesl3[] HTTPD_STRING_ATTR = "</a>";
562 #endif
563 /*---------------------------------------------------------------------------*/
564 static unsigned short
565 make_routes(void *p)
566 {
567 static const char httpd_cgi_rtes1[] HTTPD_STRING_ATTR = "/%u (via ";
568 static const char httpd_cgi_rtes2[] HTTPD_STRING_ATTR = ") %lus\n";
569 static const char httpd_cgi_rtes3[] HTTPD_STRING_ATTR = ")\n";
570 uint8_t i,j;
571 uint16_t numprinted=0;
572 struct httpd_state *s=p;
573 uip_ds6_route_t *r;
574  /* Span generator calls over tcp segments */
575  /* Note retransmissions will execute thise code multiple times for a segment */
576  i=s->starti;j=s->startj;
577  for(r = uip_ds6_route_head();
578  r != NULL;
579  r = uip_ds6_route_next(r)) {
580  j++;
581 
582 #if WEBSERVER_CONF_ROUTE_LINKS
583  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtesl1);
584  numprinted += httpd_cgi_sprint_ip6(r->ipaddr, uip_appdata + numprinted);
585  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtesl2);
586  numprinted += httpd_cgi_sprint_ip6(r->ipaddr, uip_appdata + numprinted);
587  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtesl3);
588 #else
589  numprinted += httpd_cgi_sprint_ip6(r->ipaddr, uip_appdata + numprinted);
590 #endif
591 
592  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes1, r->length);
593  numprinted += httpd_cgi_sprint_ip6(uip_ds6_route_nexthop(r), uip_appdata + numprinted);
594  if(1 || r->state.lifetime < 3600) {
595  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes2, (long unsigned int)r->state.lifetime);
596  } else {
597  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtes3);
598  }
599  /* If buffer near full, send it and wait for the next call. Could be a retransmission, or the next segment */
600  if(numprinted > (uip_mss() - 200)) {
601  s->savei=i;s->savej=j;
602  return numprinted;
603  }
604  }
605  }
606  if (j==0) numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrn);
607 #if WEBSERVER_CONF_SHOW_ROOM
608  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf,UIP_DS6_ROUTE_NB-j);
609 #else
610  if(UIP_DS6_ROUTE_NB == j) {
611  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_addrf);
612  }
613 #endif
614 {
615  static const char httpd_cgi_defr1[] HTTPD_STRING_ATTR = "\n<big><b>RPL Parent</b></big>\n";
616  static const char httpd_cgi_defr2[] HTTPD_STRING_ATTR = " (%u sec)\n";
617 #if 0
618  uip_ip6addr_t *nexthop = uip_ds6_defrt_choose();
619  if (nexthop) {
620  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_defr1);
621  numprinted += httpd_cgi_sprint_ip6(*nexthop, uip_appdata + numprinted);
622  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_defr2,nexthop->lifetime.start+nexthop->lifetime.interval-clock_seconds());
623  }
624 #else
625 uip_ds6_defrt_t *locdefrt;
626 extern uip_ds6_defrt_t uip_ds6_defrt_list[UIP_DS6_DEFRT_NB];
627  for(locdefrt = uip_ds6_defrt_list;
628  locdefrt < uip_ds6_defrt_list + UIP_DS6_DEFRT_NB; locdefrt++) {
629  if(locdefrt->isused) {
630  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_defr1);
631 
632 #if WEBSERVER_CONF_ROUTE_LINKS && 0
633  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtesl1);
634  numprinted += httpd_cgi_sprint_ip6(locdefrt->ipaddr, uip_appdata + numprinted);
635  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtesl2);
636  numprinted += httpd_cgi_sprint_ip6(locdefrt->ipaddr, uip_appdata + numprinted);
637  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_rtesl3);
638 #else
639  numprinted += httpd_cgi_sprint_ip6(locdefrt->ipaddr, uip_appdata + numprinted);
640 #endif
641  numprinted += httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_defr2,locdefrt->lifetime.start+locdefrt->lifetime.interval-clock_seconds());
642  // break;
643  }
644  }
645 
646 #endif
647 }
648  /* Signal that this was the last segment */
649  s->savei = 0;
650  return numprinted;
651 }
652 /*---------------------------------------------------------------------------*/
653 static
654 PT_THREAD(routes(struct httpd_state *s, char *ptr))
655 {
656  PSOCK_BEGIN(&s->sout);
657 
658  /* Send as many TCP segments as needed for the route table */
659  /* Move to next seqment after each successful transmission */
660  s->starti=s->startj=0;
661  do {
662  PSOCK_GENERATOR_SEND(&s->sout, make_routes, s);
663  s->starti=s->savei+1;s->startj=s->savej;
664  } while(s->savei);
665 
666  PSOCK_END(&s->sout);
667 }
668 #endif /* WEBSERVER_CONF_ROUTES */
669 
670 #if WEBSERVER_CONF_SENSORS
671 /*---------------------------------------------------------------------------*/
672 static unsigned short
673 generate_sensor_readings(void *arg)
674 {
675  uint16_t numprinted=0;
676  uint16_t days,h,m,s;
677  unsigned long seconds=clock_seconds();
678  static const char httpd_cgi_sensor0[] HTTPD_STRING_ATTR = "[Updated %d seconds ago]\n";
679  static const char httpd_cgi_sensor1[] HTTPD_STRING_ATTR = "<em>Temperature:</em> %s\n";
680  static const char httpd_cgi_sensor2[] HTTPD_STRING_ATTR = "<em>Battery :</em> %s\n";
681  static const char httpd_cgi_sensor3[] HTTPD_STRING_ATTR = "<em>Uptime :</em> %02d:%02d:%02d\n";
682  static const char httpd_cgi_sensor3d[] HTTPD_STRING_ATTR = "<em>Uptime :</em> %u days %02u:%02u:%02u\n";
683 
684  /* Generate temperature and voltage strings for each platform */
685 #if CONTIKI_TARGET_AVR_ATMEGA128RFA1
686 {uint8_t i;
687  BATMON = 16; //give BATMON time to stabilize at highest range and lowest voltage
688 
689 /* Measure internal temperature sensor, see atmega128rfa1 datasheet */
690 /* This code disabled by default for safety.
691  Selecting an internal reference will short it to anything connected to the AREF pin
692  */
693 #if 0
694  ADCSRB|=1<<MUX5; //this bit buffered till ADMUX written to!
695  ADMUX =0xc9; // Select internal 1.6 volt ref, temperature sensor ADC channel
696  ADCSRA=0x85; //Enable ADC, not free running, interrupt disabled, clock divider 32 (250 KHz@ 8 MHz)
697 // while ((ADCSRB&(1<<AVDDOK))==0); //wait for AVDD ok
698 // while ((ADCSRB&(1<<REFOK))==0); //wait for ref ok
699  ADCSRA|=1<<ADSC; //Start throwaway conversion
700  while (ADCSRA&(1<<ADSC)); //Wait till done
701  ADCSRA|=1<<ADSC; //Start another conversion
702  while (ADCSRA&(1<<ADSC)); //Wait till done
703  h=ADC; //Read adc
704  h=11*h-2728+(h>>2); //Convert to celcius*10 (should be 11.3*h, approximate with 11.25*h)
705  ADCSRA=0; //disable ADC
706  ADMUX=0; //turn off internal vref
707  m=h/10;s=h-10*m;
708  static const char httpd_cgi_sensor1_printf[] HTTPD_STRING_ATTR = "%d.%d C";
709  httpd_snprintf(sensor_temperature,sizeof(sensor_temperature),httpd_cgi_sensor1_printf,m,s);
710 #endif
711 
712 /* Bandgap can't be measured against supply voltage in this chip. */
713 /* Use BATMON register instead */
714  for ( i=16; i<31; i++) {
715  BATMON = i;
716  if ((BATMON&(1<<BATMON_OK))==0) break;
717  }
718  h=2550-75*16-75+75*i; //-75 to take the floor of the 75 mv transition window
719  static const char httpd_cgi_sensor2_printf[] HTTPD_STRING_ATTR = "%u mv";
720  httpd_snprintf(sensor_extvoltage,sizeof(sensor_extvoltage),httpd_cgi_sensor2_printf,h);
721 }
722 #elif CONTIKI_TARGET_AVR_RAVEN
723 {
724 #if 1
725 /* Usual way to get AVR supply voltage, measure 1.1v bandgap using Vcc as reference.
726  * This connects the bandgap to the AREF pin, so enable only if there is no external AREF!
727  * A capacitor may be connected to this pin to reduce reference noise.
728  */
729  ADMUX =0x5E; //Select AVCC as reference, measure 1.1 volt bandgap reference.
730  ADCSRA=0x87; //Enable ADC, not free running, interrupt disabled, clock divider 128 (62 KHz@ 8 MHz)
731  ADCSRA|=1<<ADSC; //Start throwaway conversion
732  while (ADCSRA&(1<<ADSC)); //Wait till done
733  ADCSRA|=1<<ADSC; //Start another conversion
734  while (ADCSRA&(1<<ADSC)); //Wait till done
735 //h=1126400UL/ADC; //Get supply voltage (factor nominally 1100*1024)
736  h=1198070UL/ADC; //My Raven
737  ADCSRA=0; //disable ADC
738  ADMUX=0; //turn off internal vref
739 
740  static const char httpd_cgi_sensor2_printf[] HTTPD_STRING_ATTR = "%u mv";
741  httpd_snprintf(sensor_extvoltage,sizeof(sensor_extvoltage),httpd_cgi_sensor2_printf,h);
742 #endif
743 }
744 #elif CONTIKI_TARGET_REDBEE_ECONOTAG
745 //#include "adc.h"
746 {
747 #if 0
748 /* Scan ADC channels if not already being done elsewhere */
749 uint8_t c;
750  adc_reading[8]=0;
751  adc_init();
752  while (adc_reading[8]==0) adc_service();
753 //for (c=0; c<NUM_ADC_CHAN; c++) printf("%u %04u\r\n", c, adc_reading[c]);
754  adc_disable();
755 #endif
756 
757  static const char httpd_cgi_sensorv[] HTTPD_STRING_ATTR = "<em>ADC chans :</em> %u %u %u %u %u %u %u %u \n";
758  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensorv,
759  adc_reading[0],adc_reading[1],adc_reading[2],adc_reading[3],adc_reading[4],adc_reading[5],adc_reading[6],adc_reading[7]);
760 
761 }
762 #endif
763 
764  if (last_tempupdate) {
765  numprinted =httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_sensor0,(unsigned int) (seconds-last_tempupdate));
766  }
767  if (sensor_temperature[0]!='N') {
768  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor1, sensor_temperature);
769  }
770 
771 #if CONTIKI_TARGET_REDBEE_ECONOTAG
772 /* Econotag at 3v55 with 10 ohms to LiFePO4 battery: 3680mv usb 3573 2 Fresh alkaline AAs. Take 3590 as threshold for USB connected */
773  static const char httpd_cgi_sensor2u[] HTTPD_STRING_ATTR = "<em>Vcc (USB) :</em> %s\n";
774  if(adc_reading[8]<1368) {
775  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor2u, sensor_extvoltage);
776  } else {
777  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor2, sensor_extvoltage);
778  }
779 #else
780  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor2, sensor_extvoltage);
781 #endif
782 
783 #if RADIOSTATS
784  /* Remember radioontime for display below - slow connection might make it report longer than cpu ontime! */
785  savedradioontime = radioontime;
786 #endif
787  h=seconds/3600;s=seconds-h*3600;m=s/60;s=s-m*60;
788  days=h/24;
789  if (days == 0) {
790  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor3, h,m,s);
791  } else {
792  h=h-days*24;
793  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor3d, days,h,m,s);
794  }
795  return numprinted;
796 }
797 #if WEBSERVER_CONF_STATISTICS
798 /*---------------------------------------------------------------------------*/
799 static unsigned short
800 generate_stats(void *arg)
801 {
802  uint16_t numprinted;
803  uint16_t h,m,s;
804  uint8_t p1,p2;
805  uint32_t seconds=clock_seconds();
806 
807  static const char httpd_cgi_stats[] HTTPD_STRING_ATTR = "\n<big><b>Statistics</b></big>\n";
808  numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_stats);
809 
810 #if ENERGEST_CONF_ON
811 {uint8_t p1,p2;
812  uint32_t sl;
813 #if 0
814 /* Update all the timers to get current values */
815  for (p1=1;p1<ENERGEST_TYPE_MAX;p1++) {
816  if (energest_current_mode[p1]) {
817  ENERGEST_OFF(p1);
818  ENERGEST_ON(p1);
819  }
820  }
821 #else
822  energest_flush();
823 #endif
824  static const char httpd_cgi_sensor4[] HTTPD_STRING_ATTR = "<em>CPU time (ENERGEST):</em> %02u:%02u:%02u (%u.%02u%%)\n";
825  static const char httpd_cgi_sensor10[] HTTPD_STRING_ATTR = "<em>Radio (ENERGEST):</em> Tx %02u:%02u:%02u (%u.%02u%%) ";
826  static const char httpd_cgi_sensor11[] HTTPD_STRING_ATTR = "Rx %02u:%02u:%02u (%u.%02u%%)\n";
827  sl=energest_total_time[ENERGEST_TYPE_CPU].current/RTIMER_ARCH_SECOND;
828  h=(10000UL*sl)/seconds;p1=h/100;p2=h-p1*100;h=sl/3600;s=sl-h*3600;m=s/60;s=s-m*60;
829  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor4, h,m,s,p1,p2);
830 
831  sl=energest_total_time[ENERGEST_TYPE_TRANSMIT].current/RTIMER_ARCH_SECOND;
832  h=(10000UL*sl)/seconds;p1=h/100;p2=h-p1*100;h=sl/3600;s=sl-h*3600;m=s/60;s=s-m*60;
833  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor10, h,m,s,p1,p2);
834 
835  sl=energest_total_time[ENERGEST_TYPE_LISTEN].current/RTIMER_ARCH_SECOND;
836  h=(10000UL*sl)/seconds;p1=h/100;p2=h-p1*100;h=sl/3600;s=sl-h*3600;m=s/60;s=s-m*60;
837  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor11, h,m,s,p1,p2);
838 }
839 #endif /* ENERGEST_CONF_ON */
840 
841 #if CONTIKIMAC_CONF_COMPOWER
842 #include "sys/compower.h"
843 {uint8_t p1,p2;
844  // extern struct compower_activity current_packet;
845  static const char httpd_cgi_sensor31[] HTTPD_STRING_ATTR = "<em>ContikiMAC (COMPOWER):</em> Tx %02u:%02u:%02u (%u.%02u%%) ";
846  static const char httpd_cgi_sensor32[] HTTPD_STRING_ATTR = "Rx %02u:%02u:%02u (%u.%02u%%)\n";
847 
848  s=compower_idle_activity.transmit/RTIMER_ARCH_SECOND;
849  h=((10000UL*compower_idle_activity.transmit)/RTIMER_ARCH_SECOND)/seconds;
850  p1=h/100;p2=h-p1*100;h=s/3600;s=s-h*3600;m=s/60;s=s-m*60;
851  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor31, h,m,s,p1,p2);
852 
853  s=compower_idle_activity.listen/RTIMER_ARCH_SECOND;
854  h=((10000UL*compower_idle_activity.listen)/RTIMER_ARCH_SECOND)/seconds;
855  p1=h/100;p2=h-p1*100;h=s/3600;s=s-h*3600;m=s/60;s=s-m*60;
856  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor32, h,m,s,p1,p2);
857 
858 }
859 #endif
860 
861 #if RIMESTATS_CONF_ENABLED
862 #include "net/rime/rimestats.h"
863  static const char httpd_cgi_sensor21[] HTTPD_STRING_ATTR = "<em>Packets (RIMESTATS):</em> Tx=%5lu Rx=%5lu TxL=%4lu RxL=%4lu\n";
864  numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_sensor21,
865  RIMESTATS_GET(tx),RIMESTATS_GET(rx),RIMESTATS_GET(lltx)-RIMESTATS_GET(tx),RIMESTATS_GET(llrx)-RIMESTATS_GET(rx));
866 #endif
867 
868 #if RADIOSTATS
869  /* From RF230 statistics */
870  static const char httpd_cgi_sensor10[] HTTPD_STRING_ATTR = "<em>Radio on (RF230BB) :</em> %02d:%02d:%02d (%d.%02d%%)\n";
871  static const char httpd_cgi_sensor11[] HTTPD_STRING_ATTR = "<em>Packets: (RF230BB) :</em> Tx=%5d Rx=%5d TxL=%5d RxL=%5d RSSI=%2ddBm\n";
872 
873  s=(10000UL*savedradioontime)/seconds;
874  p1=s/100;
875  p2=s-p1*100;
876  h=savedradioontime/3600;
877  s=savedradioontime-h*3600;
878  m=s/60;
879  s=s-m*60;
880 
881  numprinted =httpd_snprintf((char *)uip_appdata , uip_mss() , httpd_cgi_sensor10,\
882  h,m,s,p1,p2);
883 
884 #if RF230BB
885  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor11,\
886  RF230_sendpackets,RF230_receivepackets,RF230_sendfail,RF230_receivefail,-92+rf230_last_rssi);
887 #else
888  p1=0;
890  p1 = -91*3(p1-1);
891  numprinted+=httpd_snprintf((char *)uip_appdata + numprinted, uip_mss() - numprinted, httpd_cgi_sensor11,\
892  RF230_sendpackets,RF230_receivepackets,RF230_sendfail,RF230_receivefail,p1);
893 #endif
894 #endif /* RADIOSTATS */
895  return numprinted;
896 }
897 #endif
898 /*---------------------------------------------------------------------------*/
899 static
900 PT_THREAD(sensor_readings(struct httpd_state *s, char *ptr))
901 {
902  PSOCK_BEGIN(&s->sout);
903 
904  PSOCK_GENERATOR_SEND(&s->sout, generate_sensor_readings, s);
905 #if WEBSERVER_CONF_STATISTICS
906  PSOCK_GENERATOR_SEND(&s->sout, generate_stats, s);
907 #endif
908 
909  PSOCK_END(&s->sout);
910 }
911 #endif /* WEBSERVER_CONF_SENSORS */
912 
913 #if WEBSERVER_CONF_TICTACTOE
914 /*---------------------------------------------------------------------------*/
915 static uint8_t whowon(char x) {
916 
917  if (httpd_query[0]==x) {
918  if(((httpd_query[1]==x)&&(httpd_query[2]==x))
919  || ((httpd_query[4]==x)&&(httpd_query[8]==x))
920  || ((httpd_query[3]==x)&&(httpd_query[6]==x))){
921  return 1;
922  }
923  }
924  if (httpd_query[1]==x) {
925  if ((httpd_query[4]==x)&&(httpd_query[7]==x)) {
926  return 1;
927  }
928  }
929  if (httpd_query[2]==x) {
930  if(((httpd_query[4]==x)&&(httpd_query[6]==x))
931  || ((httpd_query[5]==x)&&(httpd_query[8]==x))){
932  return 1;
933  }
934  }
935  if (httpd_query[3]==x) {
936  if ((httpd_query[4]==x)&&(httpd_query[5]==x)) {
937  return 1;
938  }
939  }
940  if (httpd_query[6]==x) {
941  if ((httpd_query[7]==x)&&(httpd_query[8]==x)) {
942  return 1;
943  }
944  }
945  return 0;
946 }
947 
948 /*---------------------------------------------------------------------------*/
949 static unsigned short
950 make_tictactoe(void *p)
951 {
952  uint8_t i,newgame,iwon,uwon,nx,no;
953  char me,you,locater;
954  unsigned short numprinted=0;
955 
956  /* If no query string restart game, else put into proper form */
957  newgame=0;httpd_query[9]=0;
958  if ((httpd_query[0]==0)||(httpd_query[0]==' ')) {
959  newgame=1;
960  for (i=0;i<9;i++) httpd_query[i]='b';
961  } else for (i=0;i<9;i++) {
962  if (!((httpd_query[i]=='x')||(httpd_query[i]=='o'))) {
963  httpd_query[i]='b';
964  }
965  }
966 
967  /* I am x if I move first, or if number of x's is <= number of o's */
968  for (nx=0,no=0,i=0;i<9;i++) {
969  if (httpd_query[i]=='x') nx++;
970  else if (httpd_query[i]=='o') no++;
971  }
972  if ((no>=nx)&&!newgame) {me='x';you='o';}
973  else {me='o';you='x';};
974 
975  iwon=whowon(me);
976  uwon=whowon(you);
977 
978  if (newgame||iwon||uwon||(nx+no)>=9) goto showboard;
979 
980  /* Make a move */
981  if (me=='x') nx++;else no++;
982  if (httpd_query[4]=='b') httpd_query[4]=me;
983  else if (httpd_query[0]=='b') httpd_query[0]=me;
984  else if (httpd_query[2]=='b') httpd_query[2]=me;
985  else if (httpd_query[6]=='b') httpd_query[6]=me;
986  else if (httpd_query[8]=='b') httpd_query[8]=me;
987  else if (httpd_query[1]=='b') httpd_query[1]=me;
988  else if (httpd_query[3]=='b') httpd_query[3]=me;
989  else if (httpd_query[5]=='b') httpd_query[5]=me;
990  else if (httpd_query[7]=='b') httpd_query[7]=me;
991 
992  /* Did I win? */
993  iwon=whowon(me);
994 
995  showboard:
996  for (i=0;i<9;i++) {
997 
998  if (i==4) locater='c';
999  else if ((i==1)||(i==7)) locater='v';
1000  else if ((i==3)||(i==5)) locater='h';
1001  else locater=0;
1002 
1003  if ((httpd_query[i]=='b')&&(!(iwon||uwon))) {
1004  httpd_query[i]=you;
1005  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<a href=ttt.shtml?%s><img src=b",httpd_query);
1006  httpd_query[i]='b';
1007  } else {
1008  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<img src=%c",httpd_query[i]);
1009  }
1010  if (locater) {
1011  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "%c",locater);
1012  }
1013  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, ".gif>");
1014  if (httpd_query[i]=='b') {
1015  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "</a>");
1016  }
1017  if ((i==2)||(i==5)) {
1018  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<br>");
1019  }
1020  }
1021 
1022  if ((nx>(no+1))||(no>(nx+1))) {
1023  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<br><h2>You cheated!!!</h2>");
1024  } else if (iwon) {
1025  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<br><h2>I Win!</h2>");
1026  } else if (uwon) {
1027  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<br><h2>You Win!</h2>");
1028  } else if ((nx+no)==9) {
1029  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<br><h2>Draw!</h2>");
1030  }
1031  if (iwon||uwon||((nx+no)==9)) {
1032  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<br><a href=ttt.shtml>Play Again</a>");
1033  }
1034 
1035  /* If new game give option for me to start */
1036  if ((nx==0)&&(no==0)) {
1037  numprinted+=snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, "<br><br><a href=ttt.shtml?bbbbbbbb>Let computer move first</a>");
1038  }
1039  httpd_query[0]=0; //zero the query string
1040  return numprinted;
1041 
1042 }
1043 /*---------------------------------------------------------------------------*/
1044 static
1045 PT_THREAD(tictactoe(struct httpd_state *s, char *ptr))
1046 {
1047  PSOCK_BEGIN(&s->sout);
1048  PSOCK_GENERATOR_SEND(&s->sout, make_tictactoe, s);
1049  PSOCK_END(&s->sout);
1050 }
1051 #endif /* WEBSERVER_CONF_TICTACTOE */
1052 
1053 #if WEBSERVER_CONF_AJAX
1054 /*---------------------------------------------------------------------------*/
1055 static
1056 PT_THREAD(ajax_call(struct httpd_state *s, char *ptr))
1057 {
1058  static struct timer t;
1059  static int iter;
1060  static char buf[128];
1061  static uint8_t numprinted;
1062  PSOCK_BEGIN(&s->sout);
1063 
1064 #if WEBSERVER_CONF_PASSQUERY
1065 /* Get update time from ? string, if present */
1066 {
1067  uint8_t i;uint16_t dt=0;
1068  for (i=0;i<WEBSERVER_CONF_PASSQUERY;i++) {
1069  if (httpd_query[i] < '0' || httpd_query[i] > '9') break;
1070  dt = dt*10 + httpd_query[i]-'0';
1071  }
1072  if (dt < 1) dt = 1;
1073  // printf("dt %u\n",dt);
1074  timer_set(&t, dt*CLOCK_SECOND);
1075  httpd_query[i]=0; //necessary?
1076  if (dt > WEBSERVER_CONF_TIMEOUT/2) s->ajax_timeout = 2*dt; else s->ajax_timeout = WEBSERVER_CONF_TIMEOUT;
1077 }
1078 #else
1079  timer_set(&t, 2*CLOCK_SECOND);
1080 #endif
1081 
1082  iter = 0;
1083 
1084  while(1) {
1085  iter++;
1086 
1087 #if CONTIKI_TARGET_SKY
1088  SENSORS_ACTIVATE(sht11_sensor);
1089  SENSORS_ACTIVATE(light_sensor);
1090  numprinted = snprintf(buf, sizeof(buf),
1091  "t(%d);h(%d);l1(%d);l2(%d);",
1092  sht11_sensor.value(SHT11_SENSOR_TEMP),
1093  sht11_sensor.value(SHT11_SENSOR_HUMIDITY),
1094  light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC),
1095  light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR));
1096  SENSORS_DEACTIVATE(sht11_sensor);
1097  SENSORS_DEACTIVATE(light_sensor);
1098 
1099 #elif CONTIKI_TARGET_MB851
1100  SENSORS_ACTIVATE(acc_sensor);
1101  numprinted = snprintf(buf, sizeof(buf),"t(%d);ax(%d);ay(%d);az(%d);",
1102  temperature_sensor.value(0),
1103  acc_sensor.value(ACC_X_AXIS),
1104  acc_sensor.value(ACC_Y_AXIS),
1105  acc_sensor.value(ACC_Z_AXIS));
1106  SENSORS_DEACTIVATE(acc_sensor);
1107 
1108 #elif CONTIKI_TARGET_REDBEE_ECONOTAG
1109 #if 0
1110 /* Scan ADC channels if not already done elsewhere */
1111 { uint8_t c;
1112  adc_reading[8]=0;
1113  adc_init();
1114  while (adc_reading[8]==0) adc_service();
1115  adc_disable();
1116 #endif
1117 
1118 
1119 #if 1
1120  numprinted = snprintf(buf, sizeof(buf),"b(%u);adc(%u,%u,%u,%u,%u,%u,%u,%u);",
1121  1200*0xfff/adc_reading[8],adc_reading[0],adc_reading[1],adc_reading[2],adc_reading[3],adc_reading[4],adc_reading[5],adc_reading[6],adc_reading[7]);
1122 #else
1123  // numprinted = snprintf(buf, sizeof(buf),"b(%u);",1200*0xfff/adc_reading[8]);
1124  numprinted = snprintf(buf, sizeof(buf),"b(%u);adc(%u,%u,%u);",1200*0xfff/adc_reading[8],adc_reading[1],adc_reading[7],adc_reading[8]);
1125 #endif
1126 }
1127  if (iter<3) {
1128  static const char httpd_cgi_ajax11[] HTTPD_STRING_ATTR = "wt('Econtag [";
1129  static const char httpd_cgi_ajax12[] HTTPD_STRING_ATTR = "]');";
1130  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax11);
1131 #if WEBSERVER_CONF_PRINTADDR
1132 /* Note address table is filled from the end down */
1133 {int i;
1134  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
1135  if (uip_ds6_if.addr_list[i].isused) {
1136  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, buf + numprinted);
1137  break;
1138  }
1139  }
1140 }
1141 #endif
1142  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax12);
1143  }
1144 #elif CONTIKI_TARGET_MINIMAL_NET
1145 static uint16_t c0=0x3ff,c1=0x3ff,c2=0x3ff,c3=0x3ff,c4=0x3ff,c5=0x3ff,c6=0x3ff,c7=0x3ff;
1146  numprinted = snprintf(buf, sizeof(buf), "t(%d);b(%u);v(%u);",273+(rand()&0x3f),3300-iter/10,iter);
1147  numprinted += snprintf(buf+numprinted, sizeof(buf)-numprinted,"adc(%u,%u,%u,%u,%u,%u,%u,%u);",c0,c1,c2,c3,c4,c5,c6,c7);
1148  c0+=(rand()&0xf)-8;
1149  c1+=(rand()&0xf)-8;
1150  c2+=(rand()&0xf)-7;
1151  c3+=(rand()&0x1f)-15;
1152  c4+=(rand()&0x3)-1;
1153  c5+=(rand()&0xf)-8;
1154  c6+=(rand()&0xf)-8;
1155  c7+=(rand()&0xf)-8;
1156  if (iter==1) {
1157  static const char httpd_cgi_ajax11[] HTTPD_STRING_ATTR = "wt('Minimal-net ";
1158  static const char httpd_cgi_ajax12[] HTTPD_STRING_ATTR = "');";
1159  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax11);
1160 #if WEBSERVER_CONF_PRINTADDR
1161 /* Note address table is filled from the end down */
1162 {int i;
1163  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
1164  if (uip_ds6_if.addr_list[i].isused) {
1165  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, buf + numprinted);
1166  break;
1167  }
1168  }
1169 }
1170 #endif
1171  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax12);
1172  }
1173 
1174 #elif CONTIKI_TARGET_AVR_ATMEGA128RFA1
1175 { uint8_t i;int16_t tmp,bat;
1176  BATMON = 16; //give BATMON time to stabilize at highest range and lowest voltage
1177 /* Measure internal temperature sensor, see atmega128rfa1 datasheet */
1178 /* This code disabled by default for safety.
1179  Selecting an internal reference will short it to anything connected to the AREF pin
1180  */
1181 #if 1
1182  ADCSRB|=1<<MUX5; //this bit buffered till ADMUX written to!
1183  ADMUX =0xc9; // Select internal 1.6 volt ref, temperature sensor ADC channel
1184  ADCSRA=0x85; //Enable ADC, not free running, interrupt disabled, clock divider 32 (250 KHz@ 8 MHz)
1185 // while ((ADCSRB&(1<<AVDDOK))==0); //wait for AVDD ok
1186 // while ((ADCSRB&(1<<REFOK))==0); //wait for ref ok
1187  ADCSRA|=1<<ADSC; //Start throwaway conversion
1188  while (ADCSRA&(1<<ADSC)); //Wait till done
1189  ADCSRA|=1<<ADSC; //Start another conversion
1190  while (ADCSRA&(1<<ADSC)); //Wait till done
1191  tmp=ADC; //Read adc
1192  tmp=11*tmp-2728+(tmp>>2); //Convert to celcius*10 (should be 11.3*h, approximate with 11.25*h)
1193  ADCSRA=0; //disable ADC
1194  ADMUX=0; //turn off internal vref
1195 #endif
1196 /* Bandgap can't be measured against supply voltage in this chip. */
1197 /* Use BATMON register instead */
1198  for ( i=16; i<31; i++) {
1199  BATMON = i;
1200  if ((BATMON&(1<<BATMON_OK))==0) break;
1201  }
1202  bat=2550-75*16-75+75*i; //-75 to take the floor of the 75 mv transition window
1203  static const char httpd_cgi_ajax10[] HTTPD_STRING_ATTR ="t(%u),b(%u);adc(%d,%d,%u,%u,%u,%u,%u,%lu);";
1204  numprinted = httpd_snprintf(buf, sizeof(buf),httpd_cgi_ajax10,tmp,bat,iter,tmp,bat,sleepcount,OCR2A,0,clock_time(),clock_seconds());
1205  if (iter==1) {
1206  static const char httpd_cgi_ajax11[] HTTPD_STRING_ATTR = "wt('128rfa1 [";
1207  static const char httpd_cgi_ajax12[] HTTPD_STRING_ATTR = "]');";
1208  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax11);
1209 #if WEBSERVER_CONF_PRINTADDR
1210 /* Note address table is filled from the end down */
1211 {int i;
1212  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
1213  if (uip_ds6_if.addr_list[i].isused) {
1214  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, buf + numprinted);
1215  break;
1216  }
1217  }
1218 }
1219 #endif
1220  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax12);
1221  }
1222 }
1223 #elif CONTIKI_TARGET_AVR_RAVEN
1224 { int16_t tmp,bat;
1225 #if 1
1226 /* Usual way to get AVR supply voltage, measure 1.1v bandgap using Vcc as reference.
1227  * This connects the bandgap to the AREF pin, so enable only if there is no external AREF!
1228  * A capacitor may be connected to this pin to reduce reference noise.
1229  */
1230  ADMUX =0x5E; //Select AVCC as reference, measure 1.1 volt bandgap reference.
1231  ADCSRA=0x87; //Enable ADC, not free running, interrupt disabled, clock divider 128 (62 KHz@ 8 MHz)
1232  ADCSRA|=1<<ADSC; //Start throwaway conversion
1233  while (ADCSRA&(1<<ADSC)); //Wait till done
1234  ADCSRA|=1<<ADSC; //Start another conversion
1235  while (ADCSRA&(1<<ADSC)); //Wait till done
1236 //bat=1126400UL/ADC; //Get supply voltage (factor nominally 1100*1024)
1237  bat=1198070UL/ADC; //My Raven
1238  ADCSRA=0; //disable ADC
1239  ADMUX=0; //turn off internal vref
1240 #else
1241  bat=3300;
1242 #endif
1243 
1244  tmp=420;
1245 
1246  static const char httpd_cgi_ajax10[] HTTPD_STRING_ATTR ="t(%u),b(%u);adc(%d,%d,%u,%u,%u,%u,%u,%lu);";
1247  numprinted = httpd_snprintf(buf, sizeof(buf),httpd_cgi_ajax10,tmp,bat,iter,tmp,bat,sleepcount,OCR2A,0,clock_time(),clock_seconds());
1248  if (iter<3) {
1249  static const char httpd_cgi_ajax11[] HTTPD_STRING_ATTR = "wt('Raven [";
1250  static const char httpd_cgi_ajax12[] HTTPD_STRING_ATTR = "]');";
1251  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax11);
1252 #if WEBSERVER_CONF_PRINTADDR
1253 /* Note address table is filled from the end down */
1254 {int i;
1255  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
1256  if (uip_ds6_if.addr_list[i].isused) {
1257  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, buf + numprinted);
1258  break;
1259  }
1260  }
1261 }
1262 #endif
1263  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax12);
1264  }
1265 }
1266 
1267 //#elif CONTIKI_TARGET_IS_SOMETHING_ELSE
1268 #else
1269 {
1270  static const char httpd_cgi_ajax10[] HTTPD_STRING_ATTR ="v(%u);";
1271  numprinted = httpd_snprintf(buf, sizeof(buf),httpd_cgi_ajax10,iter);
1272  if (iter==1) {
1273  static const char httpd_cgi_ajax11[] HTTPD_STRING_ATTR = "wt('Contiki Ajax ";
1274  static const char httpd_cgi_ajax12[] HTTPD_STRING_ATTR = "');";
1275  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax11);
1276 #if WEBSERVER_CONF_PRINTADDR
1277 /* Note address table is filled from the end down */
1278 {int i;
1279  for (i=0; i<UIP_DS6_ADDR_NB;i++) {
1280  if (uip_ds6_if.addr_list[i].isused) {
1281  numprinted += httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr, buf + numprinted);
1282  break;
1283  }
1284  }
1285 }
1286 #endif
1287  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajax12);
1288  }
1289 }
1290 #endif
1291 
1292 #if CONTIKIMAC_CONF_COMPOWER
1293 #include "sys/compower.h"
1294 {
1295 //sl=compower_idle_activity.transmit/RTIMER_ARCH_SECOND;
1296 //sl=compower_idle_activity.listen/RTIMER_ARCH_SECOND;
1297 }
1298 #endif
1299 
1300 #if RIMESTATS_CONF_ENABLED
1301 
1302 #include "net/rime/rimestats.h"
1303  static const char httpd_cgi_ajaxr1[] HTTPD_STRING_ATTR ="rime(%lu,%lu,%lu,%lu);";
1304  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajaxr1,
1305  RIMESTATS_GET(tx),RIMESTATS_GET(rx),RIMESTATS_GET(lltx)-RIMESTATS_GET(tx),RIMESTATS_GET(llrx)-RIMESTATS_GET(rx));
1306 #endif
1307 
1308 #if ENERGEST_CONF_ON
1309 {
1310 #if 1
1311 /* Send on times in percent since last update. Handle 16 bit rtimer wraparound.
1312  * Javascript must convert based on platform cpu, tx, rx power.
1313  * e.g. for cpu drawing 1.2ma@3v3, cp = 1.2*3.3*(% on time)/100 = 0.0396*c;
1314  * Low power mode l is essentially zero for cpus that sleep.
1315  * function p(c,l,t,r){cp=0.013*c;lt=0.465*t;lr=0.400*r;n=cp+lt+lr;//128rfa1@3v3,3dBm
1316  * function p(c,l,t,r){cp=0.013*c;lt=0.317*t;lr=0.400*r;n=cp+lt+lr;//128rfa1@3v3,0dBm
1317  * function p(c,l,t,r){cp=0.109*c;lt=0.848*t;lr=0.617*r;n=cp+lt+lr;//mc1322x@3v3,0dBm
1318  * Precision can be increased by multiplying by another 100 before sending.
1319  */
1320  static rtimer_clock_t last_send;
1321  rtimer_clock_t delta_time;
1322  static unsigned long last_cpu, last_lpm, last_listen, last_transmit;
1323  energest_flush();
1324  delta_time=RTIMER_NOW()-last_send;
1325  if (RTIMER_CLOCK_LT(RTIMER_NOW(),last_send)) delta_time+=RTIMER_ARCH_SECOND;
1326  last_send=RTIMER_NOW();
1327  static const char httpd_cgi_ajaxe1[] HTTPD_STRING_ATTR = "p(%lu,%lu,%lu,%lu);";
1328  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajaxe1,
1329  (10000UL*(energest_total_time[ENERGEST_TYPE_CPU].current - last_cpu))/delta_time,
1330  (10000UL*(energest_total_time[ENERGEST_TYPE_LPM].current - last_lpm))/delta_time,
1331  (10000UL*(energest_total_time[ENERGEST_TYPE_TRANSMIT].current - last_transmit))/delta_time,
1332  (10000UL*(energest_total_time[ENERGEST_TYPE_LISTEN].current - last_listen))/delta_time);
1333  last_cpu = energest_total_time[ENERGEST_TYPE_CPU].current;
1334  last_lpm = energest_total_time[ENERGEST_TYPE_LPM].current;
1335  last_transmit = energest_total_time[ENERGEST_TYPE_TRANSMIT].current;
1336  last_listen = energest_total_time[ENERGEST_TYPE_LISTEN].current;
1337 #endif
1338 #if 1
1339 /* Send cumulative on times in percent*100 */
1340  uint16_t cpp,txp,rxp;
1341  uint32_t sl,clockseconds=clock_seconds();
1342 // energest_flush();
1343 // sl=((10000UL*energest_total_time[ENERGEST_TYPE_CPU].current)/RTIMER_ARCH_SECOND)/clockseconds;
1344  sl=energest_total_time[ENERGEST_TYPE_CPU].current/RTIMER_ARCH_SECOND;
1345  cpp=(10000UL*sl)/clockseconds;
1346 // txp=((10000UL*energest_total_time[ENERGEST_TYPE_TRANSMIT].current)/RTIMER_ARCH_SECOND)/clockseconds;
1347  sl=energest_total_time[ENERGEST_TYPE_TRANSMIT].current/RTIMER_ARCH_SECOND;
1348  txp=(10000UL*sl)/clockseconds;
1349 
1350  // rxp=((10000UL*energest_total_time[ENERGEST_TYPE_LISTEN].current)/RTIMER_ARCH_SECOND)/clockseconds;
1351  sl=energest_total_time[ENERGEST_TYPE_LISTEN].current/RTIMER_ARCH_SECOND;
1352  rxp=(10000UL*sl)/clockseconds;
1353 
1354  static const char httpd_cgi_ajaxe2[] HTTPD_STRING_ATTR = "ener(%u,%u,%u);";
1355  numprinted += httpd_snprintf(buf+numprinted, sizeof(buf)-numprinted,httpd_cgi_ajaxe2,cpp,txp,rxp);
1356 #endif
1357 }
1358 #endif /* ENERGEST_CONF_ON */
1359 
1360  PSOCK_SEND_STR(&s->sout, buf);
1361  /* Can do fixed intervals or fixed starting points */
1362 #if FIXED_INTERVALS
1363  timer_restart(&t);
1364  PSOCK_WAIT_UNTIL(&s->sout, timer_expired(&t));
1365 #else
1366  PSOCK_WAIT_UNTIL(&s->sout, timer_expired(&t));
1367  timer_reset(&t);
1368 #endif
1369 }
1370  PSOCK_END(&s->sout);
1371 }
1372 #endif /* WEBSERVER_CONF_AJAX */
1373 /*---------------------------------------------------------------------------*/
1374 void
1375 httpd_cgi_add(struct httpd_cgi_call *c)
1376 {
1377  struct httpd_cgi_call *l;
1378 
1379  c->next = NULL;
1380  if(calls == NULL) {
1381  calls = c;
1382  } else {
1383  for(l = calls; l->next != NULL; l = l->next);
1384  l->next = c;
1385  }
1386 }
1387 /*---------------------------------------------------------------------------*/
1388 #if WEBSERVER_CONF_HEADER
1389 HTTPD_CGI_CALL( hdr, hdr_name, header );
1390 #endif
1391 #if WEBSERVER_CONF_FILESTATS
1392 HTTPD_CGI_CALL( file, file_name, file_stats );
1393 #endif
1394 #if WEBSERVER_CONF_TCPSTATS
1395 HTTPD_CGI_CALL( tcp, tcp_name, tcp_stats );
1396 #endif
1397 #if WEBSERVER_CONF_PROCESSES
1398 HTTPD_CGI_CALL( proc, proc_name, processes );
1399 #endif
1400 #if WEBSERVER_CONF_ADDRESSES
1401 HTTPD_CGI_CALL( adrs, adrs_name, addresses );
1402 #endif
1403 #if WEBSERVER_CONF_NEIGHBORS
1404 HTTPD_CGI_CALL( nbrs, nbrs_name, neighbors );
1405 #endif
1406 #if WEBSERVER_CONF_ROUTES
1407 HTTPD_CGI_CALL( rtes, rtes_name, routes );
1408 #endif
1409 #if WEBSERVER_CONF_SENSORS
1410 HTTPD_CGI_CALL(sensors, sensor_name, sensor_readings);
1411 #endif
1412 #if WEBSERVER_CONF_TICTACTOE
1413 HTTPD_CGI_CALL( tictac, tictac_name, tictactoe );
1414 #endif
1415 #if WEBSERVER_CONF_AJAX
1416 HTTPD_CGI_CALL( ajax, ajax_name, ajax_call );
1417 #endif
1418 
1419 void
1420 httpd_cgi_init(void)
1421 {
1422 #if WEBSERVER_CONF_HEADER
1423  httpd_cgi_add( &hdr);
1424 #endif
1425 #if WEBSERVER_CONF_FILESTATS
1426  httpd_cgi_add( &file);
1427 #endif
1428 #if WEBSERVER_CONF_TCPSTATS
1429  httpd_cgi_add( &tcp);
1430 #endif
1431 #if WEBSERVER_CONF_PROCESSES
1432  httpd_cgi_add( &proc);
1433 #endif
1434 #if WEBSERVER_CONF_ADDRESSES
1435  httpd_cgi_add( &adrs);
1436 #endif
1437 #if WEBSERVER_CONF_NEIGHBORS
1438  httpd_cgi_add( &nbrs);
1439 #endif
1440 #if WEBSERVER_CONF_ROUTES
1441  httpd_cgi_add( &rtes);
1442 #endif
1443 #if WEBSERVER_CONF_SENSORS
1444  httpd_cgi_add(&sensors);
1445 #endif
1446 #if WEBSERVER_CONF_TICTACTOE
1447  httpd_cgi_add( &tictac);
1448 #endif
1449 #if WEBSERVER_CONF_AJAX
1450  httpd_cgi_add( &ajax);
1451 #endif
1452 }
1453 #endif /* WEBSERVER_CONF_CGI */
1454 
1455 #if WEBSERVER_CONF_PRINTADDR
1456 /*---------------------------------------------------------------------------*/
1457 uint8_t httpd_cgi_sprint_ip6(uip_ip6addr_t addr, char * result)
1458 {
1459  unsigned char zerocnt = 0;
1460  unsigned char numprinted = 0;
1461  char * starting = result;
1462  unsigned char i = 0;
1463  while (numprinted < 8)
1464  {
1465  //Address is zero, have we used our ability to
1466  //replace a bunch with : yet?
1467  if ((addr.u16[i] == 0) && (zerocnt == 0))
1468  {
1469  //How mant zeros?
1470  zerocnt = 0;
1471  while(addr.u16[zerocnt + i] == 0)
1472  zerocnt++;
1473 
1474  //just one, don't waste our zeros...
1475  if (zerocnt == 1)
1476  {
1477  *result++ = '0';
1478  numprinted++;
1479  break;
1480  }
1481 
1482  //Cool - can replace a bunch of zeros
1483  i += zerocnt;
1484  numprinted += zerocnt;
1485  //all zeroes ?
1486  if (zerocnt > 7) {*result++ = ':';*result++ = ':';}
1487  }
1488  //Normal address, just print it
1489  else
1490  {
1491  result += sprintf(result, "%x", (unsigned int)(uip_ntohs(addr.u16[i])));
1492  i++;
1493  numprinted++;
1494  }
1495 
1496  //Don't print : on last one
1497  if (numprinted != 8)
1498  *result++ = ':';
1499  }
1500 
1501  return (result - starting);
1502  }
1503 #endif /* WEBSERVER_CONF_PRINTADDR */
#define UIP_CONNS
The maximum number of simultaneously open TCP connections.
Definition: uipopt.h:419
An entry in the routing table.
A timer.
Definition: timer.h:86
radio_status_t radio_get_rssi_value(uint8_t *rssi)
This function returns the Received Signal Strength Indication.
Definition: radio.c:485
struct sensors_sensor temperature_sensor
NOTE: For the temperature measurement, the ADC extended range mode is needed; but this is inaccurate ...
void timer_restart(struct timer *t)
Restart the timer from the current point in time.
Definition: timer.c:104
#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
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
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 NBR_INCOMPLETE
Possible states for the nbr cache entries.
Definition: uip-ds6-nbr.h:61
struct compower_activity compower_idle_activity
The default idle communication activity.
Definition: compower.c:50
Header file for the communication power accounting module
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
#define PSOCK_WAIT_UNTIL(psock, condition)
Wait until a condition is true.
Definition: psock.h:395
uint16_t rport
The local remote TCP port, in network byte order.
Definition: uip.h:1340
#define PSOCK_SEND_STR(psock, str)
Send a null-terminated string.
Definition: psock.h:197
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
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:57
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:133
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
Header file for Rime statistics
uip_ipaddr_t ripaddr
The IP address of the remote host.
Definition: uip.h:1337
void adc_init(void)
Initializes the ADC controller.
Definition: adc.c:50
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:121
An entry in the default router list.
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
void timer_reset(struct timer *t)
Reset the timer with the same interval.
Definition: timer.c:84
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
An entry in the nbr cache.
Definition: uip-ds6-nbr.h:70