httpd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /***************************************************************************//**
  2. * @file httpd.c
  3. * @brief Simple http server demo application
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. * @author onelife
  6. * @version 1.0
  7. *******************************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file
  10. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  11. *******************************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2011-07-04 onelife Derive from Energy Micro demo application
  15. ******************************************************************************/
  16. /**************************************************************************//**
  17. * @file
  18. * @brief This file is dervied from the ``httpd.c'' skeleton.
  19. * @author Energy Micro AS
  20. * @@version 0.0.4
  21. ******************************************************************************
  22. * @section License
  23. * <b>(C) Copyright 2009 Energy Micro AS, http://www.energymicro.com</b>
  24. ******************************************************************************
  25. *
  26. * This source code is the property of Energy Micro AS. The source and compiled
  27. * code may only be used on Energy Micro "EFM32" microcontrollers.
  28. *
  29. * This copyright notice may not be removed from the source code nor changed.
  30. *
  31. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  32. * obligation to support this Software. Energy Micro AS is providing the
  33. * Software "AS IS", with no express or implied warranties of any kind,
  34. * including, but not limited to, any implied warranties of merchantability
  35. * or fitness for any particular purpose or warranties against infringement
  36. * of any proprietary rights of a third party.
  37. *
  38. * Energy Micro AS will not be liable for any consequential, incidental, or
  39. * special damages, or any other relief, or for any claim by any third party,
  40. * arising from your use of this Software.
  41. *
  42. *****************************************************************************/
  43. /**
  44. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  45. * All rights reserved.
  46. *
  47. * Redistribution and use in source and binary forms, with or without modification,
  48. * are permitted provided that the following conditions are met:
  49. *
  50. * 1. Redistributions of source code must retain the above copyright notice,
  51. * this list of conditions and the following disclaimer.
  52. * 2. Redistributions in binary form must reproduce the above copyright notice,
  53. * this list of conditions and the following disclaimer in the documentation
  54. * and/or other materials provided with the distribution.
  55. * 3. The name of the author may not be used to endorse or promote products
  56. * derived from this software without specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  59. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  60. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  61. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  62. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  63. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  64. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  65. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  66. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  67. * OF SUCH DAMAGE.
  68. *
  69. * This file is part of the lwIP TCP/IP stack.
  70. *
  71. * Author: Adam Dunkels <adam@sics.se>
  72. *
  73. */
  74. /***************************************************************************//**
  75. * @addtogroup efm32_eth
  76. * @{
  77. ******************************************************************************/
  78. /* Includes ------------------------------------------------------------------*/
  79. #include "rtthread.h"
  80. #include "dev_misc.h"
  81. #if defined(RT_USING_LWIP) && defined(EFM32_USING_ETH_HTTPD)
  82. #include "lwip/tcp.h"
  83. #include "lwip/ip_addr.h"
  84. /* Private typedef -----------------------------------------------------------*/
  85. /* Private define ------------------------------------------------------------*/
  86. /* Private macro -------------------------------------------------------------*/
  87. /* Private variables ---------------------------------------------------------*/
  88. /* This is the data for the actual web page. */
  89. static int temp, vdd;
  90. static char indexdata[700];
  91. static const char indexdata1[] =
  92. "HTTP/1.0 200 OK\r\n\
  93. Content-type: text/html\r\n\
  94. Pragma: no-cache\r\n\
  95. Refresh: 5\r\n\
  96. \r\n\
  97. <html>\
  98. <head><title>EFM32 HTTPD DEMO</title><head>\
  99. <body>\
  100. <h1>This is a simple http server</h1>\
  101. <br><br><B>Ethernet controller: ENC28J60</B>\
  102. <br><br><B>Refreshing timers: ";
  103. static const char indexdata2[] =
  104. "<br><br><B>Current Vdd: ";
  105. static const char indexdata3[] =
  106. " V</B>\
  107. <br><br><B>Current temperature: ";
  108. static const char indexdata4[] =
  109. " C</B>\
  110. </body>\
  111. </html>";
  112. /* Private function prototypes -----------------------------------------------*/
  113. /* Private functions ---------------------------------------------------------*/
  114. /* This is the callback function that is called
  115. * when a TCP segment has arrived in the connection. */
  116. static err_t http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  117. {
  118. static unsigned char temp_var = 0;
  119. unsigned short counter, i;
  120. char *rq;
  121. /* If we got a NULL pbuf in p, the remote end has closed
  122. * the connection. */
  123. if (p != NULL)
  124. {
  125. /* The payload pointer in the pbuf contains the data
  126. * in the TCP segment. */
  127. rq = p->payload;
  128. /* Check if the request was an HTTP "GET / HTTP/1.1". */
  129. if (rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T')
  130. {
  131. /* Send the web page to the remote host. A zero
  132. * in the last argument means that the data should
  133. * not be copied into internal buffers. */
  134. counter = 0;
  135. for (i = 0; i < sizeof(indexdata1) - 1; i++)
  136. {
  137. indexdata[counter] = indexdata1[i];
  138. counter++;
  139. }
  140. indexdata[counter] = ' ';
  141. counter++;
  142. /*Copy page counter MSB*/
  143. indexdata[counter] = temp_var / 10 + 0x30;
  144. counter++;
  145. /*Copy page counter LSB*/
  146. indexdata[counter] = temp_var % 10 + 0x30;
  147. counter++;
  148. temp_var++;
  149. if (temp_var > 100) temp_var = 1;
  150. for (i = 0; i < sizeof(indexdata2) - 1; i++)
  151. {
  152. indexdata[counter] = indexdata2[i];
  153. counter++;
  154. }
  155. vdd = rt_hw_get_vdd();
  156. rt_sprintf(&indexdata[counter], "%1d.%02d", vdd / 100, vdd % 100);
  157. counter += 4;
  158. for (i = 0; i < sizeof(indexdata3) - 1; i++)
  159. {
  160. indexdata[counter] = indexdata3[i];
  161. counter++;
  162. }
  163. temp = rt_hw_get_temp();
  164. /*Set temperature sign*/
  165. if (temp < 0)
  166. {
  167. indexdata[counter] = '-';
  168. counter++;
  169. }
  170. rt_sprintf(&indexdata[counter], "%02d.%02d\n", temp / 100, temp % 100);
  171. counter += 5;
  172. for (i = 0; i < sizeof(indexdata4); i++)
  173. {
  174. indexdata[counter] = indexdata4[i];
  175. counter++;
  176. }
  177. tcp_write(pcb, indexdata, counter, 1);
  178. }
  179. /* Free the pbuf. */
  180. pbuf_free(p);
  181. }
  182. /* Close the connection. */
  183. tcp_close(pcb);
  184. return ERR_OK;
  185. }
  186. /* This is the callback function that is called when
  187. * a connection has been accepted. */
  188. static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
  189. {
  190. /* Set up the function http_recv() to be called when data
  191. * arrives. */
  192. tcp_recv(pcb, http_recv);
  193. return ERR_OK;
  194. }
  195. /* The initialization function. */
  196. void httpd_init(void)
  197. {
  198. struct tcp_pcb *pcb;
  199. /* Create a new TCP PCB. */
  200. pcb = tcp_new();
  201. /* Bind the PCB to TCP port 80. */
  202. tcp_bind(pcb, NULL, 80);
  203. /* Change TCP state to LISTEN. */
  204. pcb = tcp_listen(pcb);
  205. /* Set up http_accet() function to be called
  206. * when a new connection arrives. */
  207. tcp_accept(pcb, http_accept);
  208. }
  209. #endif /* defined(RT_USING_LWIP) && defined(EFM32_USING_ETH_HTTPD) */
  210. /***************************************************************************//**
  211. * @}
  212. ******************************************************************************/