/* Copyright (c) 2012 VECTARE * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. * - Neither the name of VECTARE nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * VECTARE. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE * */ /** * */ #include "lwip/opt.h" #include "lwip/arch.h" #include "lwip/api.h" #include "efmboard.h" #include "efmboard_utils.h" #include "archer.h" #include "archer_rtc.h" #include "webfunctions.h" #include #include //#include #if LWIP_NETCONN void http_server(void *pdata); void http_server_serve(struct netconn *conn); // defined in efmboard.c extern struct tm currentTime[1]; void http_server_serve(struct netconn *conn) { struct netbuf *inbuf; char *buf; u16_t buflen,j; int i; FIL fsrc; u16_t more=0; webinfo webdata; u16_t numwebparams; char indexdata[BUFFERSIZEMAX]; char webvalue[15]; u16_t bytesread=0; char * ptr; //ARCHER_RTC_gettime(currentTime[0]); //indexdata = ctime(currentTime[0]); /* Read the data from the port, blocking if nothing yet there. We assume the request (the part we care about) is in one netbuf */ netconn_recv(conn, &inbuf); if (netconn_err(conn) == ERR_OK) { netbuf_data(inbuf, (void **)&buf, &buflen); /* Is this an HTTP GET command? (only check the first 5 chars, since there are other formats for GET, and we're keeping it very simple )*/ if (buflen>=5 && buf[0]=='G' && buf[1]=='E' && buf[2]=='T' && buf[3]==' ' && buf[4]=='/' ) { // Respond with HTTP Acknowledgement bzero(webdata.filename, MAXFILENAMESIZE); getDecode(buf, buflen, &webdata); // Decode the GET statement if(open_file_on_SDCard(&fsrc, webdata.filename, FA_READ) == SDCARD_SUCCESS) { // If the file exist webdata.filesize = f_size(&fsrc); if (webdata.filesize <= 0) goto ERROR404; // Since the fat system creates a file when openning it up // We will jump to the 404 if the filesize is 0; bytesread = webSendAck(indexdata, BUFFERSIZE, &webdata, getCurrentRfc868()); // Generate the HTTP ACK Message // Write the ACK Message out if(netconn_write(conn, indexdata, bytesread, NETCONN_COPY) != ERR_OK) { } //debug GET //if(netconn_write(conn, buf, buflen, NETCONN_COPY) != ERR_OK) { } numwebparams = 0; numwebparams = getWebParam(buf, buflen, &webdata); // Pull paramaters that are passed webParamProcess(&webdata); // Process the parameters that are passed in the GET while(f_read(&fsrc,indexdata,BUFFERSIZE,&bytesread) == FR_OK) { if (bytesread == BUFFERSIZE) more=1; else more=0; if (webdata.filetype == 0) { // If file type is zero, then parse for (i=0; i< MAXWEBREPLACE; i++) { ptr=strstr(indexdata,webreplace[i]); if (ptr != NULL) { // Found the variable switch (i) { case NODETIME: { // NODETIME if ((j=webLTOA(getCurrentRfc868(), webvalue, 15))) { // Note: (ptr-indexdata) will return the position of the beginning of the string bytesread = webReplaceVar(indexdata, bytesread, webvalue, j, webreplace[i],(ptr-indexdata)); } break; } case LED0: { // LED0 if (efmboard_Led_get(0)) strcpy(webvalue,"\"OFF\""); else strcpy(webvalue,"\"ON\""); j = strlen(webvalue); bytesread = webReplaceVar(indexdata, bytesread, webvalue, j, webreplace[i],(ptr-indexdata)); break; } case LED1: { // LED1 if (efmboard_Led_get(1)) strcpy(webvalue,"\"OFF\""); else strcpy(webvalue,"\"ON\""); j = strlen(webvalue); bytesread = webReplaceVar(indexdata, bytesread, webvalue, j, webreplace[i],(ptr-indexdata)); break; } } // end of switch } } //end for } //end if if(netconn_write(conn, indexdata, bytesread, NETCONN_COPY) != ERR_OK) { break; } if (!more) break; } // end while //} close_file_on_SDCard(&fsrc); } else { ERROR404: // Not found, send 404 header bytesread = webSend404(indexdata, BUFFERSIZE); // Write the 404 Message out if(netconn_write(conn, indexdata, bytesread, NETCONN_COPY) != ERR_OK) { } close_file_on_SDCard(&fsrc); } } } // end if netconn /* Close the connection (server closes in HTTP) */ netconn_close(conn); /* Delete the buffer (netconn_recv gives us ownership, so we have to make sure to deallocate the buffer) */ netbuf_delete(inbuf); } void http_server(void *pdata) { //struct netconn *conn, *newconn; struct netconn *conn; struct netconn *newconn; /* Create a new TCP connection handle */ conn = netconn_new(NETCONN_TCP); // LWIP_ERROR("http_server: invalid conn", (conn != NULL), return -1;); /* Bind to port 80 (HTTP) with default IP address */ netconn_bind(conn, NULL, 80); /* Put the connection into LISTEN state */ netconn_listen(conn); while(1) { netconn_accept(conn, &newconn); http_server_serve(newconn); netconn_delete(newconn); } } #endif /* LWIP_NETCONN*/