Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Webserver.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Simple webserver application for demonstrating the RNDIS demo and TCP/IP stack. This
  29. * application will serve up a static HTTP web page when requested by the host.
  30. */
  31. #include "Webserver.h"
  32. /** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the
  33. * given location, and gives extra connection information.
  34. */
  35. const char HTTP200Header[] PROGMEM = "HTTP/1.1 200 OK\r\n"
  36. "Server: LUFA RNDIS\r\n"
  37. "Content-type: text/html\r\n"
  38. "Connection: close\r\n\r\n";
  39. /** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given
  40. * given URL is invalid, and gives extra error information.
  41. */
  42. const char HTTP404Header[] PROGMEM = "HTTP/1.1 404 Not Found\r\n"
  43. "Server: LUFA RNDIS\r\n"
  44. "Connection: close\r\n\r\n";
  45. /** 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
  46. * broken up into smaller blocks and sent as a series of packets each time the webserver application callback is run.
  47. */
  48. const char HTTPPage[] PROGMEM =
  49. "<html>"
  50. " <head>"
  51. " <title>"
  52. " LUFA Webserver Demo"
  53. " </title>"
  54. " </head>"
  55. " <body>"
  56. " <h1>Hello from your USB AVR!</h1>"
  57. " <p>"
  58. " 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."
  59. " <br /><br />"
  60. " <small>Project Information: <a href=\"http://www.lufa-lib.org\">http://www.lufa-lib.org</a>.</small>"
  61. " <hr />"
  62. " <i>LUFA Version: </i>" LUFA_VERSION_STRING
  63. " </p>"
  64. " </body>"
  65. "</html>";
  66. /** Initializes the Webserver application, opening the appropriate HTTP port in the TCP handler and registering the application
  67. * callback routine for packets sent to the HTTP protocol port.
  68. */
  69. void Webserver_Init(void)
  70. {
  71. /* Open the HTTP port in the TCP protocol so that HTTP connections to the device can be established */
  72. TCP_SetPortState(TCP_PORT_HTTP, TCP_Port_Open, Webserver_ApplicationCallback);
  73. }
  74. /** Indicates if a given request equals the given HTTP command.
  75. *
  76. * \param[in] RequestHeader HTTP request made by the host
  77. * \param[in] Command HTTP command to compare the request to
  78. *
  79. * \return Boolean \c true if the command matches the request, \c false otherwise
  80. */
  81. static bool IsHTTPCommand(uint8_t* RequestHeader,
  82. char* Command)
  83. {
  84. /* Returns true if the non null terminated string in RequestHeader matches the null terminated string Command */
  85. return (strncmp((char*)RequestHeader, Command, strlen(Command)) == 0);
  86. }
  87. /** Application callback routine, executed each time the TCP processing task runs. This callback determines what request
  88. * has been made (if any), and serves up appropriate responses.
  89. *
  90. * \param[in] ConnectionState Pointer to a TCP Connection State structure giving connection information
  91. * \param[in,out] Buffer Pointer to the application's send/receive packet buffer
  92. */
  93. void Webserver_ApplicationCallback(TCP_ConnectionState_t* const ConnectionState,
  94. TCP_ConnectionBuffer_t* const Buffer)
  95. {
  96. char* BufferDataStr = (char*)Buffer->Data;
  97. static uint8_t PageBlock = 0;
  98. /* Check to see if a packet has been received on the HTTP port from a remote host */
  99. if (TCP_APP_HAS_RECEIVED_PACKET(Buffer))
  100. {
  101. if (IsHTTPCommand(Buffer->Data, "GET"))
  102. {
  103. if (IsHTTPCommand(Buffer->Data, "GET / "))
  104. {
  105. PageBlock = 0;
  106. /* Copy the HTTP 200 response header into the packet buffer */
  107. strcpy_P(BufferDataStr, HTTP200Header);
  108. /* Send the buffer contents to the host */
  109. TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));
  110. /* Lock the buffer to Device->Host transmissions only while we send the page contents */
  111. TCP_APP_CAPTURE_BUFFER(Buffer);
  112. }
  113. else
  114. {
  115. /* Copy the HTTP 404 response header into the packet buffer */
  116. strcpy_P(BufferDataStr, HTTP404Header);
  117. /* Send the buffer contents to the host */
  118. TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));
  119. /* All data sent, close the connection */
  120. TCP_APP_CLOSECONNECTION(ConnectionState);
  121. }
  122. }
  123. else if (IsHTTPCommand(Buffer->Data, "HEAD"))
  124. {
  125. if (IsHTTPCommand(Buffer->Data, "HEAD / "))
  126. {
  127. /* Copy the HTTP response header into the packet buffer */
  128. strcpy_P(BufferDataStr, HTTP200Header);
  129. /* Send the buffer contents to the host */
  130. TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));
  131. }
  132. else
  133. {
  134. /* Copy the HTTP response header into the packet buffer */
  135. strcpy_P(BufferDataStr, HTTP404Header);
  136. /* Send the buffer contents to the host */
  137. TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));
  138. }
  139. /* All data sent, close the connection */
  140. TCP_APP_CLOSECONNECTION(ConnectionState);
  141. }
  142. else if (IsHTTPCommand(Buffer->Data, "TRACE"))
  143. {
  144. /* Echo the host's query back to the host */
  145. TCP_APP_SEND_BUFFER(Buffer, Buffer->Length);
  146. /* All data sent, close the connection */
  147. TCP_APP_CLOSECONNECTION(ConnectionState);
  148. }
  149. else
  150. {
  151. /* Unknown request, just clear the buffer (drop the packet) */
  152. TCP_APP_CLEAR_BUFFER(Buffer);
  153. }
  154. }
  155. else if (TCP_APP_HAVE_CAPTURED_BUFFER(Buffer))
  156. {
  157. uint16_t RemLength = strlen_P(&HTTPPage[PageBlock * HTTP_REPLY_BLOCK_SIZE]);
  158. uint16_t Length;
  159. /* Determine the length of the loaded block */
  160. Length = ((RemLength > HTTP_REPLY_BLOCK_SIZE) ? HTTP_REPLY_BLOCK_SIZE : RemLength);
  161. /* Copy the next buffer sized block of the page to the packet buffer */
  162. strncpy_P(BufferDataStr, &HTTPPage[PageBlock * HTTP_REPLY_BLOCK_SIZE], Length);
  163. /* Send the buffer contents to the host */
  164. TCP_APP_SEND_BUFFER(Buffer, Length);
  165. /* Check to see if the entire page has been sent */
  166. if (PageBlock++ == (sizeof(HTTPPage) / HTTP_REPLY_BLOCK_SIZE))
  167. {
  168. /* Unlock the buffer so that the host can fill it with future packets */
  169. TCP_APP_RELEASE_BUFFER(Buffer);
  170. /* Close the connection to the host */
  171. TCP_APP_CLOSECONNECTION(ConnectionState);
  172. }
  173. }
  174. }