Add DHCP server to the Webserver demo for automatic network configuration. Correct...
[pub/lufa.git] / Projects / Webserver / Lib / WebserverApp.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Simple HTTP Webserver Application. When connected to the uIP stack,
34 * this will serve out files to HTTP clients.
35 */
36
37 #include "WebserverApp.h"
38
39 /** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the
40 * given location, and gives extra connection information.
41 */
42 char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n"
43 "Server: LUFA RNDIS\r\n"
44 "Content-type: text/html\r\n"
45 "Connection: close\r\n\r\n";
46
47 /** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given
48 * given URL is invalid, and gives extra error information.
49 */
50 char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"
51 "Server: LUFA RNDIS\r\n"
52 "Connection: close\r\n\r\n";
53
54 /** Static HTTP page to serve to the host when a HTTP request is made from a host. */
55 char PROGMEM HTTPPage[] =
56 "<html>"
57 " <head>"
58 " <title>"
59 " LUFA Webserver Demo"
60 " </title>"
61 " </head>"
62 " <body>"
63 " <h1>Hello from your USB AVR!</h1>"
64 " <p>"
65 " Hello! Welcome to the LUFA RNDIS Demo Webserver test page, running on your USB AVR via the LUFA library and uIP TCP/IP network stack. This"
66 " demonstrates a simple HTTP webserver serving out pages to HTTP clients."
67 " <br /><br />"
68 " <small>Project Information: <a href=\"http://www.fourwalledcubicle.com/LUFA.php\">http://www.fourwalledcubicle.com/LUFA.php</a>.</small>"
69 " <hr />"
70 " <i>LUFA Version: </i>" LUFA_VERSION_STRING
71 " </p>"
72 " </body>"
73 "</html>";
74
75 /** Initialization function for the simple HTTP webserver. */
76 void WebserverApp_Init(void)
77 {
78 /* Listen on port 80 for HTTP connections from hosts */
79 uip_listen(HTONS(HTTP_SERVER_PORT));
80 }
81
82 /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
83 * TCP/IP stack needs a TCP packet to be processed.
84 */
85 void WebserverApp_Callback(void)
86 {
87 uip_tcp_appstate_t* const AppState = &uip_conn->appstate;
88 char* AppData = (char*)uip_appdata;
89 uint16_t AppDataSize = 0;
90
91 if (uip_closed() || uip_aborted() || uip_timedout())
92 {
93 /* Terminated or completed connection - don't send any new data */
94 return;
95 }
96 else if (uip_connected())
97 {
98 /* New connection - initialize connection state and data pointer to the appropriate HTTP header */
99 AppState->SendPos = HTTP200Header;
100 AppState->CurrentState = WEBSERVER_STATE_SendHeaders;
101 }
102
103 /* Calculate the maximum segment size and remaining data size */
104 uint16_t BytesRemaining = strlen_P(AppState->SendPos);
105 uint16_t MaxSegSize = uip_mss();
106
107 /* No more bytes remaining in the current data being sent - progress to next data chunk or
108 * terminate the connection once all chunks are sent */
109 if (!(BytesRemaining))
110 {
111 /* Check which data chunk we are currently sending (header or data) */
112 if (AppState->CurrentState == WEBSERVER_STATE_SendHeaders)
113 {
114 AppState->SendPos = HTTPPage;
115 AppState->CurrentState = WEBSERVER_STATE_SendData;
116 }
117 else if (AppState->CurrentState == WEBSERVER_STATE_SendData)
118 {
119 uip_close();
120 AppState->CurrentState = WEBSERVER_STATE_Closed;
121 }
122
123 return;
124 }
125 else if (BytesRemaining > MaxSegSize)
126 {
127 /* More bytes remaining to send than the maximum segment size, send next chunk */
128 AppDataSize = MaxSegSize;
129 }
130 else
131 {
132 /* Less bytes than the segment size remaining, send all remaining bytes in the one packet */
133 AppDataSize = BytesRemaining;
134 }
135
136 /* Copy over the next data segment to the application buffer, advance send position pointer */
137 strncpy_P(AppData, AppState->SendPos, AppDataSize);
138 AppState->SendPos += AppDataSize;
139
140 /* Send the data to the requesting host */
141 uip_send(AppData, AppDataSize);
142 }