/* ------------------------------------------------------------------------------------------------ */
/* FILE :		CGI_SSI.c																			*/
/*																									*/
/* CONTENTS :																						*/
/*				Code for the lwIP based webserver													*/
/*				This file contains the CGI, POST and SSI functions and variables					*/
/*																									*/
/*																									*/
/* Copyright (c) 2013-2014, Code-Time Technologies Inc. All rights reserved.						*/
/*																									*/
/* Code-Time Technologies retains all right, title, and interest in and to this work				*/
/*																									*/
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS							*/
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF										*/
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL							*/
/* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR								*/
/* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,							*/
/* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR							*/
/* OTHER DEALINGS IN THE SOFTWARE.																	*/
/*																									*/
/*																									*/
/*	$Revision: 1.11 $																				*/
/*	$Date: 2014/03/12 16:59:36 $																	*/
/*																									*/
/* ------------------------------------------------------------------------------------------------ */

#include "WebServer.h"

/* ------------------------------------------------------------------------------------------------ */

#define HOST_NAME	"ZedBoard"						/* Name of the WebServer platform				*/

/* ------------------------------------------------------------------------------------------------ */

static const char g_SwitchState[2][4] = {"OFF", "ON"};

#ifdef osCMSIS
  extern int G_TaskLed7off;							/* CMSIS does not have a suspend, so keep the	*/
#endif												/* LED 7 task running and tell if LED is used	*/

/* ------------------------------------------------------------------------------------------------ */
/* Control the on-board LEDs																		*/
/* CGI: already split in parameters and values														*/

const char *LEDS_CGI_Handler(int NumParam, char *Params[], char *Values[])
{
int ii;												/* Loop counter and index						*/

  #ifdef osCMSIS									/* Turn off all the LEDS first					*/
	G_TaskLed7off = 1;
  #else
	TSKsuspend(TSKgetID("LED 7"));					/* LED 7 is controlled by the LED 7 task		*/
  #endif											/* So suspend the task and then turn LED 7 off	*/

	LED_OFF_ALL();									/* Turn off all the LEDs first					*/

	for (ii=0 ; ii<NumParam ; ii++) {				/* Get param: "GET /leds.cgi?led=1&led=2..."	*/
		if (0 == strcmp(Params[ii] , "led")){
			if (0 == strcmp(Values[ii], "0")) { 
				LED_ON(MY_LED_0);
			}
			if (0 == strcmp(Values[ii], "1")) { 
				LED_ON(MY_LED_1);
			}
			if (0 == strcmp(Values[ii], "2")) { 
				LED_ON(MY_LED_2);
			}
			if (0 == strcmp(Values[ii], "3")) { 
				LED_ON(MY_LED_3);
			}
			if (0 == strcmp(Values[ii], "4")) { 
				LED_ON(MY_LED_4);
			}
			if (0 == strcmp(Values[ii], "5")) { 
				LED_ON(MY_LED_5);
			}
			if (0 == strcmp(Values[ii], "6")) { 
				LED_ON(MY_LED_6);
			}
			if (0 == strcmp(Values[ii], "7")) { 
				LED_ON(MY_LED_7);
			  #ifdef osCMSIS
				G_TaskLed7off = 0;
			  #else
				TSKresume(TSKgetID("LED 7"));
			  #endif
			}
		}
	}
	return("/LED.shtml");							/* Web page to send back to the client			*/
}

/* ------------------------------------------------------------------------------------------------ */
/* Update the 5 variables BUT0 --> BUT4 to "ON" or "OFF"											*/
/* Push Button value read of zero is ON, non-zero is OFF											*/
/* This function is attached to SSIupdate() with SSInewUpdate()										*/

void ButtonUpdate(void)
{
char *SwPtr;

	SwPtr = SSIgetVar("BUT0");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == BUTTON_GET(MY_BUTTON_0)][0]);
	}
	SwPtr = SSIgetVar("BUT1");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == BUTTON_GET(MY_BUTTON_1)][0]);
	}
	SwPtr = SSIgetVar("BUT2");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == BUTTON_GET(MY_BUTTON_2)][0]);
	}
	SwPtr = SSIgetVar("BUT3");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == BUTTON_GET(MY_BUTTON_3)][0]);
	}
	SwPtr = SSIgetVar("BUT4");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == BUTTON_GET(MY_BUTTON_4)][0]);
	}

	return;
}
/* ------------------------------------------------------------------------------------------------ */
/* Switches value read of zero is ON, non-zero is OFF												*/
/* Update the 8 variables SW0 --> SW7 to "ON" or "OFF"												*/
/* This function is attached to SSIupdate() with SSInewUpdate()										*/
/* The switch # are reversed to have the display in same order sa the board							*/

void SwitchUpdate(void)
{
char *SwPtr;

	SwPtr = SSIgetVar("SW0");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_7)][0]);
	}
	SwPtr = SSIgetVar("SW1");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_6)][0]);
	}
	SwPtr = SSIgetVar("SW2");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_5)][0]);
	}
	SwPtr = SSIgetVar("SW3");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_4)][0]);
	}
	SwPtr = SSIgetVar("SW4");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_3)][0]);
	}
	SwPtr = SSIgetVar("SW5");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_2)][0]);
	}
	SwPtr = SSIgetVar("SW6");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_1)][0]);
	}
	SwPtr = SSIgetVar("SW7");
	if (SwPtr != NULL) {
		strcpy(SwPtr, &g_SwitchState[0 == SW_GET(MY_SWITCH_0)][0]);
	}

	return;
}

/* ------------------------------------------------------------------------------------------------ */
/* Set-up all CGI handlers																			*/

void CGIinit(void)
{ 

	CGInewHandler("/leds.cgi", LEDS_CGI_Handler);

	return;
}

/* ------------------------------------------------------------------------------------------------ */
/* Set-up all the POST handlers																		*/

void POSTinit(void)
{ 
	return;
}

/* ------------------------------------------------------------------------------------------------ */
/* Set-up all server variables and variables update handlers										*/

void SSIinit(void)
{
char DemoVal[4];								/* Temp to convert from number to string			*/

												/* ------------------------------------------------ */
	SSInewVar("REMOTE_HOST", HOST_NAME, NULL);	/* Host name variable								*/

  #ifdef __ABASSI_H__
	SSInewVar("RTOS_ID", "Abassi", NULL);
  #endif
  #ifdef __MABASSI_H__
	SSInewVar("RTOS_ID", "mAbassi", NULL);
  #endif
  #ifdef __UABASSI_H__
	SSInewVar("RTOS_ID", "&micro;Abassi", NULL);
  #endif
												/* ------------------------------------------------ */
	sprintf(&DemoVal[0], "%d", OS_DEMO);		/* Demo # variable									*/
	SSInewVar("OS_DEMO", &DemoVal[0], NULL);
												/* ------------------------------------------------ */
	SSInewVar("SHOW_BUT_PAGE", "YES", NULL);	/* The ZedBoard has 5 push buttons					*/
	SSInewVar("SHOW_LCD_PAGE", "NO",  NULL);	/* The OLED display is not yet supported			*/
	SSInewVar("SHOW_LED_PAGE", "YES", NULL);	/* The ZedBoard has 8 LEDs							*/
	SSInewVar("SHOW_RTC_PAGE", "NO",  NULL);	/* The ZedBboard does not have a Real Time Clock	*/
	SSInewVar("SHOW_SW_PAGE",  "YES", NULL);	/* The ZedBoard has 8 Switches						*/
												/* ------------------------------------------------ */
	SSInewVar("LED0_PRESENT", "YES", NULL);		/* LED variables									*/
	SSInewVar("LED1_PRESENT", "YES", NULL);
	SSInewVar("LED2_PRESENT", "YES", NULL);
	SSInewVar("LED3_PRESENT", "YES", NULL);
	SSInewVar("LED4_PRESENT", "YES", NULL);
	SSInewVar("LED5_PRESENT", "YES", NULL);
	SSInewVar("LED6_PRESENT", "YES", NULL);
	SSInewVar("LED7_PRESENT", "YES", NULL);
	SSInewVar("LED8_PRESENT", "NO",  NULL);
	SSInewVar("LED9_PRESENT", "NO",  NULL);
												/* ------------------------------------------------ */
	SSInewVar("SW0",  "--", NULL);				/* Switch variables									*/
	SSInewVar("SW1",  "--", NULL);				/* Only SW0 to SW7 are updated in SwitchUpdate()	*/
	SSInewVar("SW2",  "--", NULL);				/* Set to "--" as it is not ON nor OFF right now	*/
	SSInewVar("SW3",  "--", NULL);				/* This also deal with a switch that does not exist	*/
	SSInewVar("SW4",  "--", NULL);
	SSInewVar("SW5",  "--", NULL);
	SSInewVar("SW6",  "--", NULL);
	SSInewVar("SW7",  "--", NULL);
												/* ------------------------------------------------ */
	SSInewVar("BUT0",  "--", NULL);				/* Push Button variables							*/
	SSInewVar("BUT1",  "--", NULL);				/* Only BUT0 to BUT3 are updated in ButtonUpdate()	*/
	SSInewVar("BUT2",  "--", NULL);				/* Set to "--" as it is not ON nor OFF right now	*/
	SSInewVar("BUT3",  "--", NULL);				/* This also deal with a button that does not exist	*/
	SSInewVar("BUT4",  "--", NULL);

	SSInewVar("DATE_LOCAL", " ", NULL);			/* RTC is not supported on the ZedBoard				*/

												/* ------------------------------------------------ */
	SSInewUpdate(&SwitchUpdate);				/* Add the switch refresh handler to the system		*/
	SSInewUpdate(&ButtonUpdate);				/* Add the switch refresh handler to the system		*/

	return;
}

/* EOF */
