
#include "webpages.h"
#include "lwipopts.h"

#if (HTTP_USED == 1)

/* Standard includes. */
#include <stdio.h>
#include <string.h>

#include "conf_eth.h"

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "portmacro.h"

/* lwIP includes. */
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "netif/loopif.h"

/* ethernet includes */
#include "ethernet.h"

/*! The size of the buffer in which the dynamic WEB page is created. */
#define webMAX_PAGE_SIZE	512

/*! Standard GET response. */
#define webHTTP_OK	"HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"

/*! The port on which we listen. */
#define webHTTP_PORT		( 80 )

/*! Delay on close error. */
#define webSHORT_DELAY		( 10 )


#define webHTML_OLED \
"<META content=\"MSHTML 6.00.2900.5726\" name=GENERATOR></HEAD>\
\r\n <BODY>\
\r\n <FORM action=oled.shtml method=get>\
\r\n <TABLE class=tbl_text height=\"32%\" cellSpacing=1 cellPadding=1 width=\"40%\"\"align=center>\"\
\r\n <TBODY>\
\r\n <form name=\"input\" method=\"get\">String to Display on OLED:\
\r\n <input type=\"text\" maxlength=19 name=\" INPUTSTRING \" />\
\r\n <input type=\"submit\" value=\"Submit\" />\
\r\n </form>\
\r\n <form>\
\r\n <input type = \"Button\" value = \"Home\" onclick = \"window.location.href='index.html'\">\
\r\n </form>\
\r\n </TBODY></TABLE></FORM></BODY></HTML>"



#define webHTML_END \
"\r\n</pre>\
\r\n</font></BODY>\
</html>"

char uip_appdata[1200];               /* The uip_appdata pointer points to */

 extern unsigned long                real_voltage_value;
 extern unsigned long                     real_current_value;
 extern unsigned long                     real_temperature_value_tc;
 extern unsigned long                     real_temperature_value_tk;
 extern unsigned long                     real_temperature_value_tf;

portCHAR cDynamicPage[ webMAX_PAGE_SIZE ];
portCHAR cPageHits[ 11 ];

extern xSemaphoreHandle webSemHndl;
extern volatile unsigned char inWebTask;


/*! Function to process the current connection */
static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon );


/*! \brief WEB server main task
 *         check for incoming connection and process it
 *
 *  \param pvParameters   Input. Not Used.
 *
 */
portTASK_FUNCTION( vBasicWEBServer, pvParameters )
{
struct netconn *pxHTTPListener, *pxNewConnection;

//printf("In WebServer Task\n\r");
/* Create a new tcp connection handle */
	pxHTTPListener = netconn_new( NETCONN_TCP );
//printf("In WebServer netCoon\n\r");
	netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
//printf("In WebServer Bind\n\r");
	netconn_listen( pxHTTPListener );
//printf("In WebServer Listen\n\r");
	/* Loop forever */
    for(;;)
    {
//		printf("In WebServer In Loop\n\r");
		   /* Wait for a first connection. */
		   pxNewConnection = netconn_accept(pxHTTPListener);
		   //vParTestSetLED(webCONN_LED, pdTRUE);

		   if(pxNewConnection != NULL)
		   {
//			   printf("In WebServer New Connections\n\r");
			   prvweb_ParseHTMLRequest(pxNewConnection);
		   }/* end if new connection */
	} /* end infinite loop */
}


/*! \brief parse the incoming request
 *         parse the HTML request and send file
 *
 *  \param pxNetCon   Input. The netconn to use to send and receive data.
 *
 */
static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon )
{
	  char *tempStrPtr;
	  unsigned char c,i;
	  unsigned char oled_string[20];
	struct netbuf *pxRxBuffer;
portCHAR *pcRxString;
unsigned portSHORT usLength;
static unsigned portLONG ulPageHits = 0;

	/* We expect to immediately get data. */
	pxRxBuffer = netconn_recv( pxNetCon );
	//printf("In WebServer Parse Request\n\r");
	if( pxRxBuffer != NULL )
	{
		/* Where is the data? */
		netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
		//printf("In WebServer Rx Buff is OK\n\r");
		/* Is this a GET?  We don't handle anything else. */
		if( !strncmp( pcRxString, "GET /index.htm", 10 ) || !strncmp( pcRxString, "GET / HTTP/1.1", 14 ))
		{
			pcRxString = cDynamicPage;
			//printf("In WebServer GET\n\r");
			/* Update the hit count. */
			ulPageHits++;
			sprintf( cPageHits, "%d", (int)ulPageHits );

			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );

			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, data_index_htm, sizeof(data_index_htm), NETCONN_COPY );
		}
		else if( !strncmp( pcRxString, "GET /b.gif", 10) )
		{
			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, data_b_gif, sizeof( data_b_gif), NETCONN_COPY );
		}
		else if( !strncmp( pcRxString, "GET /oled.shtml", 15 ) )
		{
			
			snprintf((char *)uip_appdata, 1200,

			"<META content=\"MSHTML 6.00.2900.5726\" name=GENERATOR></HEAD>"
			"<BODY>"
			"<FORM action=oled.shtml method=get>"
			"<TABLE class=tbl_text height=\"32%\" cellSpacing=1 cellPadding=1 width=\"40%\""
			"align=center>"
			  "<TBODY>"
			"<form name=\"input\" method=\"get\">"
			"String to Display on TextTerminal:"
			"<input type=\"text\" maxlength=19 name=\" INPUTSTRING \" />"
			"<input type=\"submit\" value=\"Submit\" />"
			"</form>"
			"<form>"
			"<input type = \"Button\" value = \"Home\" onclick = \"window.location.href='index.html'\">"
			"</form>"
			"</TBODY></TABLE></FORM></BODY></HTML> \n"
			);

			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, uip_appdata, sizeof(uip_appdata), NETCONN_COPY );
			
			/* OLED STRING PRINTING */
			if( !strncmp( pcRxString, "GET /oled.shtml?+INPUTSTRING+=", 30 ))
			{
				tempStrPtr = pcRxString + 30;
				    for (i = 0; i < 20; i++)
				    {
					c = *tempStrPtr++;
					if (c == ' ')
					break;
					if (c == '+')
					{
					    c = ' ';
					}
					else if (c == '%') {
						unsigned char temp1,temp2;
						temp1=(*tempStrPtr++);
						temp2=(*tempStrPtr++);
					    c = hex_digits_to_byte(temp1,temp2);
					}
					oled_string[i] = c;
				    }
				    oled_string[i] = '\0';
                    iprintf("Submitted string is %s\n\r",oled_string);
#if 0
				write_data.string1 = "Submitted string   ";
			    OLED_write_data(&write_data,FIRST_LINE);

			    write_data.string2 = (char *)oled_string;
			    OLED_write_data(&write_data,SECOND_LINE);
#endif
			}
		}
		else if( !strncmp( pcRxString, "GET /google_srearch.htm", 23 ) )
		{
			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, data_google_srearch_htm, sizeof( data_google_srearch_htm), NETCONN_COPY );
		}

		else if( !strncmp( pcRxString, "GET /Gadgets.htm", 16) )
		{
			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, data_Gadgets_htm, sizeof( data_Gadgets_htm), NETCONN_COPY );
		}

		else if( !strncmp( pcRxString, "GET /Stock_ticker.htm", 21) )
		{
			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, data_Stock_ticker_htm, sizeof( data_Stock_ticker_htm), NETCONN_COPY );
		}
		else if( !strncmp( pcRxString, "GET /realdata.shtml", 19 ) )
		{
			ace_task();
			snprintf((char *)uip_appdata, 1200,
					"<HTML><HEAD><TITLE>Actel SmartFusion Webserver</TITLE>"
			"<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">"
			"<META http-equiv=Refresh content=2>"
			"<BODY>"
			"<FORM action=realdata.shtm method=get>"
			"<TABLE class=tbl_text cellSpacing=1 cellPadding=1 width=\"800%\" align=center>"
			"<TBODY>"
			"  <TR>"
			"    <TD align=middle colSpan=2 td <></TD>"
			"  <TR>"
			"    <TD align=middle colSpan=2><B>Real Time Data"
			"  Display<B></B></B></TD></TR></TBODY></TABLE>"
			"<TABLE class=tbl_text width=\"400%\" align=center border=1>"
			  "<TBODY>"
			  "<TR>"
			    "<TD align=middle width=\"70%\"><FONT"
			    "size=-1><B>Channel/Quantity</B></FONT></TD>"
			    "<TD align=middle><FONT size=-1><B>Value</B></FONT></TD></TD></TR>"
			  "<TR>"
			   " <TD>Potentiometer Voltage</TD>"
			    "<TD align=middle>"
					"%.2d V"
					"</TD></TR>"
			  "<TR>"
			   " <TD>Potentiometer Current</TD>"
			    "<TD align=middle>"
					"%.2d mA"
					"</TD></TR>"
			  "<TR>"
			   " <TD>External Temperature in Celcius</TD>"
			    "<TD align=middle>"
			    "%.2d C"
				  "</TD></TR>"
			  "<TR>"
			   " <TD>External Temperature in Fahrenheit</TD>"
			    "<TD align=middle>"
			    "%.2d F"
			  "</TD></TR>"
			  "<TR>"
			   " <TD>External Temperature in Kelvin</TD>"
			   "<TD align=middle>"
			   "%.2d K"
			   "</TD></TR></TBODY></TABLE>"
			"<TABLE align=center>"
			 " <TBODY>"
			"<form>"
			"<input type = \"Button\" value = \"Home\" onclick = \"window.location.href='index.html'\">"
			"</form>"
			"</table></form>\n"
			"</body></html>\n", real_voltage_value, real_current_value, real_temperature_value_tc,real_temperature_value_tf,real_temperature_value_tk);
			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, uip_appdata, sizeof(uip_appdata), NETCONN_COPY );
		}

		else if( !strncmp( pcRxString, "GET /mul.shtml", 14 ) )
		{
			ace_task();
			snprintf((char *)uip_appdata, 1200,


			"<html><head><title>Smartfusion Embedded Webserver</title></head>\n"
			"<table width=\"800%\" cellpadding=\"1\" cellspacing=\"1\" align=\"center\" class=\"tbl_text\">"
			"<tr><td colspan=\"2\" align=\"center\" <img src=\"/b?\">"
			"</td></tr>"
			"<tr><td colspan=\"2\" align=\"center\"><b>Multimeter Mode</b>"
			"</td></tr></table>"
			"<table width=\"400%\" border=\"1px\" align=\"center\" class=\"tbl_text\">"
			"<tr><td width=\"50%\" align=\"center\"><font size=\"-1\"><b>Channel</b></font></td>"
			"<td align=\"center\"><font size=\"-1\"><b>Value</b></font></td>"
			"</td></tr>"
			"<tr><td>Potentiometer Voltage</td><td align=\"center\">"
			" %.2d V"
			"</td></tr>"
			"<tr><td>Potentiometer Current </td><td align=\"center\">"
			" %.2d mA"
			"</td></tr>"
					  "<TR>"
					   " <TD>External Temperature in Celcius</TD>"
					    "<TD align=middle>"
					    "%.2d C"
						  "</TD></TR>"
					  "<TR>"
					   " <TD>External Temperature in Fahrenheit</TD>"
					    "<TD align=middle>"
					    "%.2d F"
					  "</TD></TR>"
					  "<TR>"
					   " <TD>External Temperature in Kelvin</TD>"
					   "<TD align=middle>"
					   "%.2d K"
			"</td></tr></table>"
			"<table width=\"40%\" cellpadding=\"1\" cellspacing=\"1\" align=\"center\">"
			"<form>"
					"<input type = \"Button\" value = \"Home\" onclick = \"window.location.href='index.html'\">"
					"</form>"
			"</table></form>\n"
			"</body></html>\n", real_voltage_value, real_current_value, real_temperature_value_tc,real_temperature_value_tf,real_temperature_value_tk);




			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			/* Write out the dynamically generated page. */
			netconn_write( pxNetCon, uip_appdata, sizeof(uip_appdata), NETCONN_COPY );
		}
		else
		{
			pcRxString = cDynamicPage;

			/* Write out the HTTP OK header. */
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );

			/* Write out the dynamically generated page. */
			//netconn_write( pxNetCon, data_index_htm, sizeof(data_index_htm), NETCONN_COPY );

		}
		netbuf_delete( pxRxBuffer );
	}

	netconn_close( pxNetCon );
	netconn_delete( pxNetCon );
}

#endif

