Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.76 KB | None | 0 0
  1. /*****************************************************************************
  2. *
  3. * \file
  4. *
  5. * \brief Basic WEB Server for AVR32 UC3.
  6. *
  7. * Copyright (c) 2009-2012 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. *    this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. *    this list of conditions and the following disclaimer in the documentation
  21. *    and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. *    from this software without specific prior written permission.
  25. *
  26. * 4. This software may only be redistributed and used in connection with an
  27. *    Atmel microcontroller product.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  32. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  33. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * \asf_license_stop
  42. *
  43. *****************************************************************************/
  44.  
  45.  
  46. /*
  47. Implements a simplistic WEB server.  Every time a connection is made and
  48. data is received a dynamic page that shows the current FreeRTOS.org kernel
  49. statistics is generated and returned.  The connection is then closed.
  50.  
  51. This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
  52. party.
  53. */
  54.  
  55. #if (HTTP_USED == 1)
  56.  
  57.  
  58. /* Standard includes. */
  59. #include <stdio.h>
  60. #include <string.h>
  61.  
  62. #include "conf_eth.h"
  63. #include "dns.h"
  64.  
  65. /* Scheduler includes. */
  66. #include "FreeRTOS.h"
  67. #include "task.h"
  68. #include "semphr.h"
  69. #include "partest.h"
  70. #include "Webpage.h"
  71.  
  72. /* Demo includes. */
  73. /* Demo app includes. */
  74. #include "portmacro.h"
  75.  
  76. /* lwIP includes. */
  77. #include "lwip/api.h"
  78. #include "lwip/tcpip.h"
  79. #include "lwip/memp.h"
  80. #include "lwip/stats.h"
  81. #include "lwip/init.h"
  82. #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) )
  83. #include "netif/loopif.h"
  84. #endif
  85.  
  86. /* ethernet includes */
  87. #include "ethernet.h"
  88.  
  89. /*! The size of the buffer in which the dynamic WEB page is created. */
  90. #define webMAX_PAGE_SIZE    512
  91.  
  92. /*! Standard GET response. */
  93. #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
  94.  
  95. /*! The port on which we listen. */
  96. #define webHTTP_PORT        ( 80 )
  97.  
  98. /*! Delay on close error. */
  99. #define webSHORT_DELAY      ( 10 )
  100.  
  101. /*! Format of the dynamic page that is returned on each connection. */
  102. #define webHTML_START \
  103. "<html>\
  104. <head>\
  105. </head>\
  106. <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\" bgcolor=\"#FFFFFF\" text=\"#2477E6\">\
  107. \r\nPage Hits = "
  108.  
  109. #define webHTML_END \
  110. "\r\n</pre>\
  111. \r\n</font></BODY>\
  112. </html>"
  113.  
  114. portCHAR cDynamicPage[ webMAX_PAGE_SIZE ];
  115. char *host;
  116. char *end;
  117. char address[16];
  118.  
  119. struct ip_addr resolved;
  120.  
  121.  
  122. /*! Function to process the current connection */
  123. static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon );
  124.  
  125.  
  126. /*! \brief WEB server main task
  127. *         check for incoming connection and process it
  128. *
  129. *  \param pvParameters   Input. Not Used.
  130. *
  131. */
  132. portTASK_FUNCTION( vBasicWEBServer, pvParameters );
  133. portTASK_FUNCTION( vBasicWEBServer, pvParameters )
  134. {
  135.     struct netconn *pxHTTPListener, *pxNewConnection;
  136.  
  137.     /* Create a new tcp connection handle */
  138.     pxHTTPListener = netconn_new( NETCONN_TCP );
  139.     netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
  140.     netconn_listen( pxHTTPListener );
  141.  
  142.     /* Loop forever */
  143.     for( ;; )
  144.     {
  145.         #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) )
  146.         /* Wait for a first connection. */
  147.         pxNewConnection = netconn_accept(pxHTTPListener);
  148.         #else
  149.         while(netconn_accept(pxHTTPListener, &pxNewConnection) != ERR_OK)
  150.         {
  151.             vTaskDelay( webSHORT_DELAY );
  152.         }
  153.         #endif
  154.         vParTestSetLED(webCONN_LED, pdTRUE);
  155.  
  156.         if(pxNewConnection != NULL)
  157.         {
  158.             prvweb_ParseHTMLRequest(pxNewConnection);
  159.         }/* end if new connection */
  160.  
  161.         vParTestSetLED(webCONN_LED, pdFALSE);
  162.  
  163.     } /* end infinite loop */
  164. }
  165.        
  166.  
  167.  
  168.         /*! \brief parse the incoming request
  169.         *         parse the HTML request and send file
  170.         *
  171.         *  \param pxNetCon   Input. The netconn to use to send and receive data.
  172.         *
  173.         */
  174. static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon )
  175. {
  176.             struct netbuf *pxRxBuffer;
  177.             portCHAR *pcRxString;
  178.             unsigned portSHORT usLength;
  179.  
  180.  
  181.             #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) )
  182.             /* We expect to immediately get data. */
  183.             pxRxBuffer = netconn_recv( pxNetCon );
  184.             #else
  185.             while(netconn_recv( pxNetCon, &pxRxBuffer) != ERR_OK)
  186.             {
  187.                 vTaskDelay( webSHORT_DELAY );
  188.             }
  189.             #endif
  190.            
  191.             if( pxRxBuffer != NULL )
  192.             {
  193.                 /* Where is the data? */
  194.                 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
  195.  
  196.                 /* Is this a GET?  We don't handle anything else. */
  197.                 if(( NULL != pcRxString)&& ( !strncmp( pcRxString, "GET", 3 ) ))
  198.                 {
  199.                     /* Write out the HTTP OK header. */
  200.                     netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
  201.                    
  202.                     /* Send HTML Page */
  203.                    
  204.                 }
  205.                
  206.                 if(( NULL != pcRxString) && ( !strncmp( pcRxString, "POST", 4 )))
  207.                 {
  208.                     printf("\r\n %s", pcRxString);
  209.                    
  210.                     while(!strncmp( pcRxString, "Keep-Alive", 10 ))
  211.                     {
  212.                         /* Write out the HTTP OK header. */
  213.                         netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
  214.                    
  215.                         #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) )
  216.                             /* We expect to immediately get data. */
  217.                             pxRxBuffer = netconn_recv( pxNetCon );
  218.                         #else
  219.                             while(netconn_recv( pxNetCon, &pxRxBuffer) != ERR_OK)
  220.                             {
  221.                                 vTaskDelay( webSHORT_DELAY );
  222.                             }
  223.                         #endif
  224.                    
  225.                         netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
  226.                         printf("\r\n %s", pcRxString);
  227.                     }
  228.                    
  229.                 }
  230.                 netbuf_delete( pxRxBuffer);
  231.         }
  232.            
  233.             netconn_close( pxNetCon );
  234.             netconn_delete( pxNetCon );
  235.  
  236. }
  237.  
  238.  
  239.         #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement