Added const modifiers to device mode class drivers.
[pub/USBasp.git] / Demos / Device / ClassDriver / RNDISEthernet / Lib / Webserver.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 webserver application for demonstrating the RNDIS demo and TCP/IP stack. This
34 * application will serve up a static HTTP webpage when requested by the host.
35 */
36
37 #include "Webserver.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 HTTPHeader[] = "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 page to serve to the host when a HTTP request is made. This page is too long for a single response, thus it is automatically
48 * broken up into smaller blocks and sent as a series of packets each time the webserver application callback is run.
49 */
50 char PROGMEM HTTPPage[] =
51 "<html>"
52 " <head>"
53 " <title>"
54 " LUFA Webserver Demo"
55 " </title>"
56 " </head>"
57 " <body>"
58 " <h1>Hello from your USB AVR!</h1>"
59 " <p>"
60 " Hello! Welcome to the LUFA RNDIS Demo Webserver test page, running on your USB AVR via the LUFA library. This demonstrates the HTTP webserver, TCP/IP stack and RNDIS demo all running atop the LUFA USB stack."
61 " <br /><br />"
62 " <small>Project Information: <a href=\"http://www.fourwalledcubicle.com/LUFA.php\">http://www.fourwalledcubicle.com/LUFA.php</a>.</small>"
63 " <hr />"
64 " <i>LUFA Version: </i>" LUFA_VERSION_STRING
65 " </p>"
66 " </body>"
67 "</html>";
68
69
70 /** Initializes the Webserver application, opening the appropriate HTTP port in the TCP handler and registering the application
71 * callback routine for packets sent to the HTTP protocol port.
72 */
73 void Webserver_Init(void)
74 {
75 /* Open the HTTP port in the TCP protocol so that HTTP connections to the device can be established */
76 TCP_SetPortState(TCP_PORT_HTTP, TCP_Port_Open, Webserver_ApplicationCallback);
77 }
78
79 /** Indicates if a given request equals the given HTTP command.
80 *
81 * \param[in] RequestHeader HTTP request made by the host
82 * \param[in] Command HTTP command to compare the request to
83 *
84 * \return Boolean true if the command matches the request, false otherwise
85 */
86 static bool IsHTTPCommand(uint8_t* RequestHeader, char* Command)
87 {
88 /* Returns true if the non null terminated string in RequestHeader matches the null terminated string Command */
89 return (strncmp((char*)RequestHeader, Command, strlen(Command)) == 0);
90 }
91
92 /** Application callback routine, executed each time the TCP processing task runs. This callback determines what request
93 * has been made (if any), and serves up appropriate responses.
94 *
95 * \param[in] ConnectionState Pointer to a TCP Connection State structure giving connection information
96 * \param[in,out] Buffer Pointer to the application's send/receive packet buffer
97 */
98 void Webserver_ApplicationCallback(TCP_ConnectionState_t* ConnectionState, TCP_ConnectionBuffer_t* Buffer)
99 {
100 char* BufferDataStr = (char*)Buffer->Data;
101 static uint8_t PageBlock = 0;
102
103 /* Check to see if a packet has been received on the HTTP port from a remote host */
104 if (TCP_APP_HAS_RECEIVED_PACKET(Buffer))
105 {
106 if (IsHTTPCommand(Buffer->Data, "GET"))
107 {
108 PageBlock = 0;
109
110 /* Copy the HTTP response header into the packet buffer */
111 strcpy_P(BufferDataStr, HTTPHeader);
112
113 /* Send the buffer contents to the host */
114 TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));
115
116 /* Lock the buffer to Device->Host transmissions only while we send the page contents */
117 TCP_APP_CAPTURE_BUFFER(Buffer);
118 }
119 else if (IsHTTPCommand(Buffer->Data, "HEAD"))
120 {
121 /* Copy the HTTP response header into the packet buffer */
122 strcpy_P(BufferDataStr, HTTPHeader);
123
124 /* Send the buffer contents to the host */
125 TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));
126 }
127 else if (IsHTTPCommand(Buffer->Data, "TRACE"))
128 {
129 /* Echo the host's query back to the host */
130 TCP_APP_SEND_BUFFER(Buffer, Buffer->Length);
131 }
132 else
133 {
134 /* Unknown request, just clear the buffer (drop the packet) */
135 TCP_APP_CLEAR_BUFFER(Buffer);
136 }
137 }
138 else if (TCP_APP_HAVE_CAPTURED_BUFFER(Buffer))
139 {
140 uint16_t RemLength = strlen_P(&HTTPPage[PageBlock * HTTP_REPLY_BLOCK_SIZE]);
141 uint16_t Length;
142
143 /* Determine the length of the loaded block */
144 Length = ((RemLength > HTTP_REPLY_BLOCK_SIZE) ? HTTP_REPLY_BLOCK_SIZE : RemLength);
145
146 /* Copy the next buffer sized block of the page to the packet buffer */
147 strncpy_P(BufferDataStr, &HTTPPage[PageBlock * HTTP_REPLY_BLOCK_SIZE], Length);
148
149 /* Send the buffer contents to the host */
150 TCP_APP_SEND_BUFFER(Buffer, Length);
151
152 /* Check to see if the entire page has been sent */
153 if (PageBlock++ == (sizeof(HTTPPage) / HTTP_REPLY_BLOCK_SIZE))
154 {
155 /* Unlock the buffer so that the host can fill it with future packets */
156 TCP_APP_RELEASE_BUFFER(Buffer);
157
158 /* Close the connection to the host */
159 TCP_APP_CLOSECONNECTION(ConnectionState);
160 }
161 }
162 }