Contiki 2.5
webserver.c
1 /*
2  * Copyright (c) 2002, 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
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki desktop environment for the C64.
31  *
32  * $Id: webserver.c,v 1.1 2009/03/12 19:15:25 adamdunkels Exp $
33  *
34  */
35 
36 #include <string.h>
37 #include <stdio.h>
38 
39 #include "contiki.h"
40 #include "ctk/ctk.h"
41 
42 #include "http-strings.h"
43 #include "webserver.h"
44 #include "httpd.h"
45 
46 /* The main window. */
47 static struct ctk_window mainwindow;
48 
49 static struct ctk_label message =
50  {CTK_LABEL(0, 0, 15, 1, "Latest requests")};
51 
52 PROCESS(webserver_process, "Web server");
53 
54 AUTOSTART_PROCESSES(&webserver_process);
55 
56 #define LOG_WIDTH 38
57 #define LOG_HEIGHT 16
58 static char log[LOG_WIDTH*LOG_HEIGHT];
59 
60 static struct ctk_label loglabel =
61 {CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)};
62 /*-----------------------------------------------------------------------------------*/
63 PROCESS_THREAD(webserver_process, ev, data)
64 {
65  PROCESS_BEGIN();
66 
67  ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server");
68 
69  CTK_WIDGET_ADD(&mainwindow, &message);
70  CTK_WIDGET_ADD(&mainwindow, &loglabel);
71 
72  httpd_init();
73 
74  ctk_window_open(&mainwindow);
75 
76  while(1) {
78 
79  if(ev == ctk_signal_window_close ||
80  ev == PROCESS_EVENT_EXIT) {
81  ctk_window_close(&mainwindow);
82  process_exit(&webserver_process);
83  LOADER_UNLOAD();
84  } else if(ev == tcpip_event) {
85  httpd_appcall(data);
86  }
87  }
88 
89  PROCESS_END();
90 }
91 /*-----------------------------------------------------------------------------------*/
92 void
93 webserver_log_file(uip_ipaddr_t *requester, char *file)
94 {
95  int size;
96 
97  /* Scroll previous entries upwards */
98  memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
99 
100  /* Print out IP address of requesting host. */
101  size = sprintf(&log[LOG_WIDTH * (LOG_HEIGHT - 1)],
102  "%d.%d.%d.%d: ",
103  requester->u8[0],
104  requester->u8[1],
105  requester->u8[2],
106  requester->u8[3]);
107 
108  /* Copy filename into last line. */
109  strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1) + size], file, LOG_WIDTH - size);
110 
111  /* Update log display. */
112  CTK_WIDGET_REDRAW(&loglabel);
113 }
114 /*-----------------------------------------------------------------------------------*/
115 void
116 webserver_log(char *msg)
117 {
118  /* Scroll previous entries upwards */
119  memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
120 
121  /* Copy filename into last line. */
122  strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1)], msg, LOG_WIDTH);
123 
124  /* Update log display. */
125  CTK_WIDGET_REDRAW(&loglabel);
126 }
127 /*-----------------------------------------------------------------------------------*/