/*
 * http.c
 *
 *  Created on: Feb 23, 2012
 *      Author: RomanM
 */

#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include "../../uip.h"
#include "http.h"

#include "ServicePort/setting.h"
#include "drivers/DoorLock/DoorLock.h"
#include "drivers/Periph/ADC/ADC.h"

#define BUFFER_APPDATA ((uint8_t*)uip_appdata)
struct app_state *s;

enum {
	HTTP_NOGET,
	HTTP_200,
	HTTP_401,
	HTTP_404,
	HTTP_CLOSE
};

const char http200[] = "HTTP/1.0 200 OK\r\n";
const char http404[] = "HTTP/1.0 404 Not Found\r\n";
const char http401[] = "HTTP/1.0 401 Access Denied\r\n";

const char httpHead[] =
		"Content-Type: text/html\r\n\r\n"
		"<html>"
		"<head>"
		"<meta http-equiv=\"refresh\" content=\"5\" />"
		"<title>RFID terminal</title>"
		"<body>";

const char httpEnd[] =
		"</body>"
		"</head>"
		"</html>";

const char pageStatus[] =
		"<h1>%s</h1>"
		"<p>RFID terminal is working</p>";

const char pageForbidden[] =
		"<h1>Access denied</h1>"
		"<p>You don't have permission to access RFID terminal.</p>";



void uip_http_init(void) {
	uip_listen(HTONS(HTTP_PORT));
}

void uip_http_call(void) {
	char toSend[64];

	s = &(uip_conn->appstate);

	if (uip_connected()) {
		s->state = HTTP_NOGET;
		s->part = 0;
		return;
	}
	else if (uip_timedout()) {
		uip_abort();
	}
	else if (uip_newdata() && s->state == HTTP_NOGET) {
		if (!memcmp(uip_conn->ripaddr, setting.serverAddress, sizeof(uip_ipaddr_t))) {
			if (!memcmp(BUFFER_APPDATA, "GET / ", 6)) {
				uip_send(http200, sizeof(http200));
				s->state = HTTP_200;
			}
			else {
				uip_send(http404, sizeof(http404));
				s->state = HTTP_404;
			}
		}
		else {
			uip_send(http401, sizeof(http401));
			s->state = HTTP_401;
		}
	}
	else if (uip_acked()) {
		switch (s->state) {
		case HTTP_200:
			switch (s->part) {
			case 0:
				uip_send(httpHead, sizeof(httpHead));
				break;
			case 1:
				//uip_send(pageStatus, sizeof(pageStatus));
				sprintf(toSend, pageStatus, setting.devName);
				uip_send(toSend, strlen(toSend));
				break;
			case 2:
				if (DoorLock_GetDoorLoop()) {
					uip_send("Door: <font color=\"green\">Closed</font><br />", 46);
				}
				else {
					uip_send("Door: <font color=\"red\">Opened</font><br />", 44);
				}
				break;
			case 3:
				if (DoorLock_GetLockLoop()) {
					uip_send("Door lock: <font color=\"green\">OK</font><br />", 47);
				}
				else {
					uip_send("Door lock: <font color=\"red\">Unconnected</font><br />", 54);
				}
				break;
			case 4:
				if (ADC1_GetValueFilt(SENSOR1) >= setting.calibCh1) {
					uip_send("Sensor 1: <font color=\"green\">OK</font><br />", 45);
				}
				else {
					uip_send("Sensor 1: <font color=\"red\">ERR</font><br />", 44);
				}
				break;
			case 5:
				if (ADC1_GetValueFilt(SENSOR2) >= setting.calibCh2) {
					uip_send("Sensor 2: <font color=\"green\">OK</font><br />", 45);
				}
				else {
					uip_send("Sensor 2: <font color=\"red\">ERR</font><br />", 44);
				}
				break;
			case 6:
				sprintf(toSend, "Temperature: %+0.1f °C<br />", ADC1_Temperature());
				uip_send(toSend, strlen(toSend));
				break;
			case 7:
				uip_send(httpEnd, sizeof(httpEnd));
				s->state = HTTP_CLOSE;
				break;
			}
			break;
		case HTTP_401:
			switch (s->part) {
			case 0:
				uip_send(httpHead, sizeof(httpHead));
				break;
			case 1:
				uip_send(pageForbidden, sizeof(pageForbidden));
				break;
			case 2:
				uip_send(httpEnd, sizeof(httpEnd));
				s->state = HTTP_CLOSE;
				break;
			}
			break;
		case HTTP_404:
			uip_close();
			break;
		case HTTP_CLOSE:
			uip_close();
			break;
		}
		s->part++;
	}
}
