blob: a12efa0dd29341b8de560590fba617e7dd4baa92 [file] [log] [blame]
adamdunkels3023dee2003-07-04 10:54:51 +00001/*
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.
adamdunkels55637d92004-08-09 22:22:47 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkels3023dee2003-07-04 10:54:51 +000015 * 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 *
adamdunkels796d6472004-09-13 23:21:36 +000032 * $Id: webserver.c,v 1.4 2004/09/13 23:21:36 adamdunkels Exp $
adamdunkels3023dee2003-07-04 10:54:51 +000033 *
34 */
35
36
adamdunkels55637d92004-08-09 22:22:47 +000037#include "contiki.h"
adamdunkels796d6472004-09-13 23:21:36 +000038#include "ctk.h"
adamdunkels3023dee2003-07-04 10:54:51 +000039#include "http-strings.h"
adamdunkels55637d92004-08-09 22:22:47 +000040
adamdunkels3023dee2003-07-04 10:54:51 +000041#include "petsciiconv.h"
42
43#include "loader.h"
44
45#include "webserver.h"
46#include "httpd.h"
47
adamdunkels55637d92004-08-09 22:22:47 +000048#include <string.h>
49#include <stdio.h>
50
adamdunkels3023dee2003-07-04 10:54:51 +000051/* The main window. */
52static struct ctk_window mainwindow;
53
54static struct ctk_label message =
55 {CTK_LABEL(0, 0, 15, 1, "Latest requests")};
56
adamdunkels3023dee2003-07-04 10:54:51 +000057
adamdunkels55637d92004-08-09 22:22:47 +000058EK_EVENTHANDLER(eventhandler, ev, data);
59EK_PROCESS(p, "Web server", EK_PRIO_NORMAL,
60 eventhandler, NULL, NULL);
61static ek_id_t id = EK_ID_NONE;
adamdunkels3023dee2003-07-04 10:54:51 +000062
63
64#define LOG_WIDTH 30
65#define LOG_HEIGHT 20
66static char log[LOG_WIDTH*LOG_HEIGHT];
67
68static struct ctk_label loglabel =
69{CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)};
70/*-----------------------------------------------------------------------------------*/
adamdunkelseb4ecc62003-08-25 12:42:41 +000071LOADER_INIT_FUNC(webserver_init, arg)
adamdunkels3023dee2003-07-04 10:54:51 +000072{
adamdunkelseb4ecc62003-08-25 12:42:41 +000073 arg_free(arg);
adamdunkels3023dee2003-07-04 10:54:51 +000074 if(id == EK_ID_NONE) {
adamdunkels55637d92004-08-09 22:22:47 +000075 id = ek_start(&p);
76 } else {
77 ctk_window_open(&mainwindow);
78 }
79}
80/*-----------------------------------------------------------------------------------*/
81EK_EVENTHANDLER(eventhandler, ev, data)
82{
83 if(ev == EK_EVENT_INIT) {
adamdunkels3023dee2003-07-04 10:54:51 +000084 ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server");
adamdunkels55637d92004-08-09 22:22:47 +000085
adamdunkels3023dee2003-07-04 10:54:51 +000086
87 CTK_WIDGET_ADD(&mainwindow, &message);
88 CTK_WIDGET_ADD(&mainwindow, &loglabel);
adamdunkels55637d92004-08-09 22:22:47 +000089
adamdunkels3023dee2003-07-04 10:54:51 +000090 /* Attach listeners to signals. */
91 /* dispatcher_listen(ctk_signal_button_activate);*/
adamdunkels55637d92004-08-09 22:22:47 +000092
adamdunkels3023dee2003-07-04 10:54:51 +000093 httpd_init();
adamdunkels55637d92004-08-09 22:22:47 +000094
95 ctk_window_open(&mainwindow);
96 } else if(ev == tcpip_event) {
97 httpd_appcall(data);
adamdunkels3023dee2003-07-04 10:54:51 +000098 }
99}
100/*-----------------------------------------------------------------------------------*/
101void
102webserver_log_file(u16_t *requester, char *file)
103{
104 int size;
105
106 /* Scroll previous entries upwards */
107 memcpy(log, &log[LOG_WIDTH], LOG_WIDTH * (LOG_HEIGHT - 1));
108
109 /* Print out IP address of requesting host. */
110 size = sprintf(&log[LOG_WIDTH * (LOG_HEIGHT - 1)],
111 "%d.%d.%d.%d: ",
112 htons(requester[0]) >> 8,
113 htons(requester[0]) & 0xff,
114 htons(requester[1]) >> 8,
115 htons(requester[1]) & 0xff);
116
117 /* Copy filename into last line. */
118 strncpy(&log[LOG_WIDTH * (LOG_HEIGHT - 1) + size], file, LOG_WIDTH - size);
119
120 /* Update log display. */
121 CTK_WIDGET_REDRAW(&loglabel);
122}
123/*-----------------------------------------------------------------------------------*/