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

/* 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 float   real_voltage_value;
extern float   real_current_value;
extern float   real_temperature_value_tc;
extern float   real_temperature_value_tk;
extern float   real_temperature_value_tf;

char pageBuffer[ webMAX_PAGE_SIZE ];


static void parseHTMLRequest( struct netconn *pxNetCon );


portTASK_FUNCTION( vBasicWEBServer, pvParameters )
{
    struct netconn *pxHTTPListener, *pxNewConnection;

	pxHTTPListener = netconn_new( NETCONN_TCP );

	netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );

	netconn_listen( pxHTTPListener );

	/* Loop forever */
    for(;;)
    {
		   pxNewConnection = netconn_accept(pxHTTPListener);

		   if(pxNewConnection != NULL)
		   {
			   parseHTMLRequest(pxNewConnection);
		   }/* end if new connection */
	} /* end infinite loop */
}

static void parseHTMLRequest( struct netconn *pxNetCon )
{
    char *tempStrPtr;
    unsigned char c,i;
    unsigned char oled_string[20];
	struct netbuf *pxRxBuffer;
    char *pcRxString;
    unsigned portSHORT usLength;

	pxRxBuffer = netconn_recv( pxNetCon );

	if( pxRxBuffer != NULL )
	{
		netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
		if( !strncmp( pcRxString, "GET /index.htm", 10 ) || !strncmp( pcRxString, "GET / HTTP/1.1", 14 ))
		{
			pcRxString = pageBuffer;
			/* 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, 900/*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);
			}
		}
		else if( !strncmp( pcRxString, "GET /google_srearch.htm", 23 ) )
		{
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			netconn_write( pxNetCon, data_google_srearch_htm, sizeof( data_google_srearch_htm), NETCONN_COPY );
		}

		else if( !strncmp( pcRxString, "GET /Gadgets.htm", 16) )
		{
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			netconn_write( pxNetCon, data_Gadgets_htm, sizeof( data_Gadgets_htm), NETCONN_COPY );
		}

		else if( !strncmp( pcRxString, "GET /Stock_ticker.htm", 21) )
		{
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			netconn_write( pxNetCon, data_Stock_ticker_htm, sizeof( data_Stock_ticker_htm), NETCONN_COPY );
		}
		else if( !strncmp( pcRxString, "GET /realdata.shtml", 19 ) )
		{
			ace_task(NULL);
			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>"
					"%.2f V"
					"</TD></TR>"
			  "<TR>"
			   " <TD>Potentiometer Current</TD>"
			    "<TD align=middle>"
					"%.2f uA"
					"</TD></TR>"
			  "<TR>"
			   " <TD>External Temperature in Celcius</TD>"
			    "<TD align=middle>"
			    "%.2f °C"
				  "</TD></TR>"
			  "<TR>"
			   " <TD>External Temperature in Fahrenheit</TD>"
			    "<TD align=middle>"
			    "%.2f F"
			  "</TD></TR>"
			  "<TR>"
			   " <TD>External Temperature in Kelvin</TD>"
			   "<TD align=middle>"
			   "%.2f 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);			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\">"
			" %.2f V"
			"</td></tr>"
			"<tr><td>Potentiometer Current </td><td align=\"center\">"
			" %.2f uA"
			"</td></tr>"
					  "<TR>"
					   " <TD>External Temperature in Celcius</TD>"
					    "<TD align=middle>"
					    "%.2f °C"
						  "</TD></TR>"
					  "<TR>"
					   " <TD>External Temperature in Fahrenheit</TD>"
					    "<TD align=middle>"
					    "%.2f F"
					  "</TD></TR>"
					  "<TR>"
					   " <TD>External Temperature in Kelvin</TD>"
					   "<TD align=middle>"
					   "%.2f 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);			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
			netconn_write( pxNetCon, uip_appdata, sizeof(uip_appdata), NETCONN_COPY );
		}
		else
		{
			pcRxString = pageBuffer;
			netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
		}
		netbuf_delete( pxRxBuffer );
	}

	netconn_close( pxNetCon );
	netconn_delete( pxNetCon );
}
