/**
 * \addtogroup httpd
 * @{
 */

/**
 * \file
 *         Web server script interface
 * \author
 *         Adam Dunkels <adam@sics.se>
 *
 */

/*
 * Copyright (c) 2001-2006, Adam Dunkels.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 *
 * This file is part of the uIP TCP/IP stack.
 *
 * $Id: httpd-cgi.c,v 1.2 2006/06/11 21:46:37 adam Exp $
 *
 */

#include "uip.h"
#include "psock.h"
#include "httpd.h"
#include "httpd-cgi.h"
#include "httpd-fs.h"
#include "AT91SAM7X512.h"

#include <stdio.h>
#include <string.h>

unsigned char logged=0;


HTTPD_CGI_CALL(io, "led-io", led_io );
HTTPD_CGI_CALL(config, "config-io", config_io );
HTTPD_CGI_CALL(login, "login-io", login_io );

static const struct httpd_cgi_call *calls[] = { &io, &config, &login, NULL };

/*---------------------------------------------------------------------------*/
static
PT_THREAD(nullfunction(struct httpd_state *s, char *ptr))
{
  PSOCK_BEGIN(&s->sout);
  PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
httpd_cgifunction
httpd_cgi(char *name)
{
  const struct httpd_cgi_call **f;

  /* Find the matching name in the table, return the function. */
  for(f = calls; *f != NULL; ++f) {
    if(strncmp((*f)->name, name, strlen((*f)->name)) == 0) {
      return (*f)->function;
    }
  }
  return nullfunction;
}

char *pcStatus1, *pcStatus2, *pcStatus3;
unsigned char outstat[3];
unsigned long ulString;
extern unsigned long uxParTextGetLED( unsigned long uxLED );

	/* Get output status, io.shtml */

static unsigned short generate_io_state( void *arg )
{
	if( uxParTestGetLED(3) )
	{
		pcStatus3 = "checked";
		outstat[2]=1;

	}
	else
	{
		pcStatus3 = "";
		outstat[2]=0;
	}
	if( uxParTestGetLED(2) )
	{
		pcStatus2 = "checked";
		outstat[1]=1;
	}
	else
	{
		pcStatus2 = "";
		outstat[1]=0;
	}
	if( uxParTestGetLED(1) )
		{
			pcStatus1 = "checked";
			outstat[0]=1;
		}
		else
		{
			pcStatus1 = "";
			outstat[0]=0;
		}

	sprintf( uip_appdata,
			"<input type=\"checkbox\" name=\"LED0\" value=\"1\" %s>Output 1<p><input type=\"checkbox\" name=\"LED1\" value=\"1\" %s>Output 2<p><input type=\"checkbox\" name=\"LED2\" value=\"1\" %s>Output 3<p>"\
			"<input type=\"submit\" name=\"update\" value=\"Change state\"><p>",
			pcStatus3,
			pcStatus2,
			pcStatus1);

	/* Write output status */
	at45db_write( 0x00, 0x04, 0x00, outstat, 3);
	return strlen( uip_appdata );
}

static PT_THREAD(led_io(struct httpd_state *s, char *ptr))
{
  PSOCK_BEGIN(&s->sout);
  PSOCK_GENERATOR_SEND(&s->sout, generate_io_state, NULL);
  PSOCK_END(&s->sout);
}

/*---------------------------------------------------------------------------*/
/* Set IP address and Mask, config.shtml */

static unsigned short config_io_state( void *arg )
{
	uip_ipaddr_t xIPAddr;
	uip_ipaddr_t xMAddr;

	uip_gethostaddr( xIPAddr);
	uip_getnetmask( xMAddr);

	sprintf( uip_appdata,
			"IP address:&nbsp;" \
			"<input type=\"text\" name=\"A0\" size=\"3\" maxlength=\"3\" value=\"%d\">."\
			"<input type=\"text\" name=\"A1\" size=\"3\" maxlength=\"3\" value=\"%d\">."\
			"<input type=\"text\" name=\"A2\" size=\"3\" maxlength=\"3\" value=\"%d\">."\
			"<input type=\"text\" name=\"A3\" size=\"3\" maxlength=\"3\" value=\"%d\"><p>"\
			"Net mask:&nbsp;&nbsp;&nbsp;" \
			"<input type=\"text\" name=\"M0\" size=\"3\" maxlength=\"3\" value=\"%d\">."\
			"<input type=\"text\" name=\"M1\" size=\"3\" maxlength=\"3\" value=\"%d\">."\
			"<input type=\"text\" name=\"M2\" size=\"3\" maxlength=\"3\" value=\"%d\">."\
			"<input type=\"text\" name=\"M3\" size=\"3\" maxlength=\"3\" value=\"%d\"><p>"\
			"<input type=\"submit\" name=\"Reboot\" value=\"Reboot\">"
     		"<input type=\"submit\" name=\"storeIP\" value=\"Store IP parameters\"><p>",
			((unsigned char *)xIPAddr)[0],
			((unsigned char *)xIPAddr)[1],
			((unsigned char *)xIPAddr)[2],
			((unsigned char *)xIPAddr)[3],
   			((unsigned char *)xMAddr)[0],
			((unsigned char *)xMAddr)[1],
			((unsigned char *)xMAddr)[2],
			((unsigned char *)xMAddr)[3]
			);

	return strlen( uip_appdata );
}

static PT_THREAD(config_io(struct httpd_state *s, char *ptr))
{
  PSOCK_BEGIN(&s->sout);
  PSOCK_GENERATOR_SEND(&s->sout, config_io_state, NULL);
  PSOCK_END(&s->sout);
}

/* Check password, login.shtml */
static unsigned short login_io_state( void *arg )
{
	if (logged == 1)
	{
		sprintf( uip_appdata,
		"<a href=\"login.shtml\">Login</a> <b>|</b> <a href=\"io.shtml\">IO</a><b>|</b> <a href=\"config.shtml\">Config</a>"
		);
	}
	else
	{
		sprintf( uip_appdata,
			"Login first"
			);
	}
	return strlen( uip_appdata );
}

static PT_THREAD(login_io(struct httpd_state *s, char *ptr))
{
  PSOCK_BEGIN(&s->sout);
  PSOCK_GENERATOR_SEND(&s->sout, login_io_state, NULL);
  PSOCK_END(&s->sout);
}


/** @} */






