Contiki 3.x
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  *
33  */
34 
35 #include <string.h>
36 #include <stdio.h>
37 
38 #include "contiki.h"
39 #include "ctk/ctk.h"
40 
41 #include "http-strings.h"
42 #include "webserver.h"
43 #include "httpd.h"
44 
45 /* The main window. */
46 static struct ctk_window mainwindow;
47 
48 static struct ctk_label message =
49  {CTK_LABEL(0, 0, 15, 1, "Latest requests")};
50 
51 PROCESS(webserver_process, "Web server");
52 
53 AUTOSTART_PROCESSES(&webserver_process);
54 
55 #define LOG_WIDTH 38
56 #define LOG_HEIGHT 16
57 static char log[LOG_WIDTH*LOG_HEIGHT];
58 
59 static struct ctk_label loglabel =
60 {CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)};
61 /*-----------------------------------------------------------------------------------*/
62 PROCESS_THREAD(webserver_process, ev, data)
63 {
64  PROCESS_BEGIN();
65 
66  ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server");
67 
68  CTK_WIDGET_ADD(&mainwindow, &message);
69  CTK_WIDGET_ADD(&mainwindow, &loglabel);
70 
71  httpd_init();
72 
73  ctk_window_open(&mainwindow);
74 
75  while(1) {
77 
78  if(ev == ctk_signal_window_close ||
79  ev == PROCESS_EVENT_EXIT) {
80  ctk_window_close(&mainwindow);
81  process_exit(&webserver_process);
82  LOADER_UNLOAD();
83  } else if(ev == tcpip_event) {
84  httpd_appcall(data);
85  }
86  }
87 
88  PROCESS_END();
89 }
90 /*-----------------------------------------------------------------------------------*/
91 void
92 webserver_log_file(uip_ipaddr_t *requester, char *file)
93 {
94  int size;
95 
96  /* Scroll previous entries upwards */
97  memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
98 
99  /* Print out IP address of requesting host. */
100  size = sprintf(&log[LOG_WIDTH * (LOG_HEIGHT - 1)],
101  "%d.%d.%d.%d: ",
102  requester->u8[0],
103  requester->u8[1],
104  requester->u8[2],
105  requester->u8[3]);
106 
107  /* Copy filename into last line. */
108  strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1) + size], file, LOG_WIDTH - size);
109 
110  /* Update log display. */
111  CTK_WIDGET_REDRAW(&loglabel);
112 }
113 /*-----------------------------------------------------------------------------------*/
114 void
115 webserver_log(char *msg)
116 {
117  /* Scroll previous entries upwards */
118  memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
119 
120  /* Copy filename into last line. */
121  strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1)], msg, LOG_WIDTH);
122 
123  /* Update log display. */
124  CTK_WIDGET_REDRAW(&loglabel);
125 }
126 /*-----------------------------------------------------------------------------------*/
#define CTK_WIDGET_ADD(win, widg)
Add a widget to a window.
Definition: ctk.h:752
process_event_t ctk_signal_window_close
Emitted when a window is closed.
Definition: ctk.c:155
void ctk_window_open(CC_REGISTER_ARG struct ctk_window *w)
Open a window, or bring window to front if already open.
Definition: ctk.c:347
void ctk_window_close(struct ctk_window *w)
Close a window if it is open.
Definition: ctk.c:400
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define CTK_LABEL(x, y, w, h, text)
Instantiating macro for the ctk_label widget.
Definition: ctk.h:171
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void ctk_window_new(struct ctk_window *window, unsigned char w, unsigned char h, char *title)
Create a new window.
Definition: ctk.c:732
#define LOADER_UNLOAD()
Unload a program from memory.
Definition: loader.h:104
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:75
Representation of a CTK window.
Definition: ctk.h:506
#define CTK_WIDGET_REDRAW(widg)
Add a widget to the redraw queue.
Definition: ctk.h:771
CTK header file.
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202