httpd.c 7.7 KB

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