SAMV71 Xplained Ultra Software Package 1.3

httpd.c

00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2010, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /* lwIP Demo */
00031 /* A simple HTTP/1.0 server directly interfacing the stack. */
00032 
00033 #include <board.h>
00034 #include <liblwip.h>
00035 
00036 #include "lwip/opt.h"
00037 #include "lwip/tcp.h"
00038 
00039 #include "httpd.h"
00040 
00041 #define COUNTER_DISP_INDEX      119
00042 
00043 /* This is the data for the actual web page. */
00044 static char indexdata[] =
00045 "HTTP/1.1 200 OK\r\n\
00046 Content-type: text/html\r\n\
00047 \r\n\
00048 <html> \
00049 <head><title>lwIP test page</title></head> \
00050 <body> \
00051 Small test page. \
00052 </body> \
00053 </html>";
00054 
00055 /**
00056  * This is the callback function that is called
00057  * when a TCP segment has arrived in the connection.
00058  */
00059 static err_t
00060 httpd_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
00061 {
00062     char *rq;
00063     arg = arg;
00064 
00065     //printf("CB recv %x, %x, %x, %x\n\r", arg, pcb, p, err);
00066     if (err != ERR_OK) {
00067         return err;
00068     }
00069 
00070     /* If we got a NULL pbuf in p, the remote end has closed
00071     the connection. */
00072     if(p != NULL) {
00073 
00074         /* The payload pointer in the pbuf contains the data
00075         in the TCP segment. */
00076         rq = p->payload;
00077 
00078         /* Check if the request was an HTTP "GET /\r\n".
00079                                     or HTTP "GET / HTTP/1.1\r\n" */
00080         if(rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T' &&
00081            rq[3] == ' ' && rq[4] == '/' &&
00082            ((rq[ 5] == '\r' && rq[ 6] == '\n') ||
00083             (rq[14] == '\r' && rq[15] == '\n')) ) {
00084             /* Send the web page to the remote host. A zero
00085             in the last argument means that the data should
00086             not be copied into internal buffers. */
00087             
00088             tcp_write(pcb, indexdata, sizeof(indexdata), 0);
00089         }
00090 
00091         /* Free the pbuf. */
00092         pbuf_free(p);
00093     }
00094     /* Close the connection. */
00095     tcp_close(pcb);
00096 
00097     return ERR_OK;
00098 }
00099 
00100 /**
00101  * This is the callback function that is called when
00102  * a connection has been accepted.
00103  */
00104 static err_t
00105 httpd_accept(void *arg, struct tcp_pcb *pcb, err_t err)
00106 {
00107     arg = arg;
00108     if (err != ERR_OK) {
00109         return err;
00110     }
00111 
00112     /* Set up the function httpd_recv() to be called when data
00113     arrives. */
00114     tcp_recv(pcb, httpd_recv);
00115     return ERR_OK;
00116 }
00117 
00118 /**
00119  * The initialization function.
00120  */
00121 err_t
00122 httpd_init(void)
00123 {
00124     struct tcp_pcb *pcb;
00125     err_t  err;
00126     /* Create a new TCP PCB. */
00127     pcb = tcp_new();
00128     if (pcb == NULL) {
00129         printf("F: Fail to create PCB\n\r");
00130         return ERR_BUF;
00131     }
00132     /* Bind the PCB to TCP port 80. */
00133     err = tcp_bind(pcb, NULL, 80);
00134     if (err != ERR_OK) {
00135         printf("E: tcp_bind %x\n\r", err);
00136         return err;
00137     }
00138     /* Change TCP state to LISTEN. */
00139     pcb = tcp_listen(pcb);
00140     if (pcb == NULL) {
00141         printf("E: tcp_listen\n\r");
00142         return ERR_BUF;
00143     }
00144     /* Set up httpd_accet() function to be called
00145     when a new connection arrives. */
00146     tcp_accept(pcb, httpd_accept);
00147 
00148     return ERR_OK;
00149 }
00150 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines