adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 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. |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 14 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 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 | * |
adamdunkels | 796d647 | 2004-09-13 23:21:36 +0000 | [diff] [blame] | 32 | * $Id: webserver.c,v 1.4 2004/09/13 23:21:36 adamdunkels Exp $ |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 33 | * |
| 34 | */ |
| 35 | |
| 36 | |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 37 | #include "contiki.h" |
adamdunkels | 796d647 | 2004-09-13 23:21:36 +0000 | [diff] [blame] | 38 | #include "ctk.h" |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 39 | #include "http-strings.h" |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 40 | |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 41 | #include "petsciiconv.h" |
| 42 | |
| 43 | #include "loader.h" |
| 44 | |
| 45 | #include "webserver.h" |
| 46 | #include "httpd.h" |
| 47 | |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 48 | #include <string.h> |
| 49 | #include <stdio.h> |
| 50 | |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 51 | /* The main window. */ |
| 52 | static struct ctk_window mainwindow; |
| 53 | |
| 54 | static struct ctk_label message = |
| 55 | {CTK_LABEL(0, 0, 15, 1, "Latest requests")}; |
| 56 | |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 57 | |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 58 | EK_EVENTHANDLER(eventhandler, ev, data); |
| 59 | EK_PROCESS(p, "Web server", EK_PRIO_NORMAL, |
| 60 | eventhandler, NULL, NULL); |
| 61 | static ek_id_t id = EK_ID_NONE; |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | #define LOG_WIDTH 30 |
| 65 | #define LOG_HEIGHT 20 |
| 66 | static char log[LOG_WIDTH*LOG_HEIGHT]; |
| 67 | |
| 68 | static struct ctk_label loglabel = |
| 69 | {CTK_LABEL(0, 1, LOG_WIDTH, LOG_HEIGHT, log)}; |
| 70 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | eb4ecc6 | 2003-08-25 12:42:41 +0000 | [diff] [blame] | 71 | LOADER_INIT_FUNC(webserver_init, arg) |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 72 | { |
adamdunkels | eb4ecc6 | 2003-08-25 12:42:41 +0000 | [diff] [blame] | 73 | arg_free(arg); |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 74 | if(id == EK_ID_NONE) { |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 75 | id = ek_start(&p); |
| 76 | } else { |
| 77 | ctk_window_open(&mainwindow); |
| 78 | } |
| 79 | } |
| 80 | /*-----------------------------------------------------------------------------------*/ |
| 81 | EK_EVENTHANDLER(eventhandler, ev, data) |
| 82 | { |
| 83 | if(ev == EK_EVENT_INIT) { |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 84 | ctk_window_new(&mainwindow, LOG_WIDTH, LOG_HEIGHT+1, "Web server"); |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 85 | |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 86 | |
| 87 | CTK_WIDGET_ADD(&mainwindow, &message); |
| 88 | CTK_WIDGET_ADD(&mainwindow, &loglabel); |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 89 | |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 90 | /* Attach listeners to signals. */ |
| 91 | /* dispatcher_listen(ctk_signal_button_activate);*/ |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 92 | |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 93 | httpd_init(); |
adamdunkels | 55637d9 | 2004-08-09 22:22:47 +0000 | [diff] [blame] | 94 | |
| 95 | ctk_window_open(&mainwindow); |
| 96 | } else if(ev == tcpip_event) { |
| 97 | httpd_appcall(data); |
adamdunkels | 3023dee | 2003-07-04 10:54:51 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | /*-----------------------------------------------------------------------------------*/ |
| 101 | void |
| 102 | webserver_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 | /*-----------------------------------------------------------------------------------*/ |