#include <em_device.h>									// header for all micro registers
#include "hardware.h"									// custom headers
#include "uart.h"
#include "hdc1050.h"

extern char buffer[];									// AMW037 response buffer
extern char temp[];
extern char humi[];
uint8_t data_ready;
const char url[]   = "GET /th.html ";
const char head1[] = "HTTP/1.1 200 OK\r\n";
const char head2[] = "Content-Type: text/html\r\nConnection: close\r\n\r\n";
const char resp1[] = "<html><head><title>AMW037 server</title></head><body><p>Temperature: ";
const char resp2[] = "&deg;C<br />Humidity: ";
const char resp3[] = "%</p></body></html>";
const char badr1[] = "HTTP/1.1 404 Not Found\r\n";
const char badr2[] = "Bad request";

char checkRequest(void);
void sendResponse(char);
void badRequest(char);

int main()
{
	char close[] = "close N";
	char streamNum;
	CMU_setup();										// configure clock
	GPIO_setup();										// configure GPIO ports											
	I2C0_setup();
	USART_setup();
	LETIMER_setup();

	do
	{
		CMD_AMW037("get wl n s", 1);					// wait for connection with AP
		Delay_ms(1000);
	} while (buffer[1] != '2');							// and getting IP address

	data_ready = 0;	
	while(1)											// main application loop
	{
		__WFI();										// wait for client sending request	
		if (!data_ready) continue;
		streamNum = checkRequest();
		if (streamNum >= '0')
			sendResponse(streamNum);
		else
		{
			streamNum += '0';
			badRequest(streamNum);
		}
		Delay_ms(500);
		close[6] = streamNum;
		CMD_AMW037(close, 1);
		data_ready = 0;
	}
}

char checkRequest(void)
{
	char read_request[] = "read X NN";	
	uint8_t i;
	uint8_t urlLen = sizeof(url)-1;
	char stream;
	CMD_AMW037("list", 2);								// get request stream number
	stream = buffer[3];
	read_request[5] = stream;	
	if (urlLen >= 10)
	{
		read_request[7] = (urlLen / 10) + '0';
		read_request[8] = (urlLen % 10) + '0';
	}
	else
	{
		read_request[7] = (urlLen % 10) + '0';
		read_request[8] = 0;
	}
	CMD_AMW037(read_request, 1);	
	for (i=0; i<urlLen; i++)
		if (buffer[i+1] != url[i])
			return stream-'0';
	return stream;	
}

void sendResponse(char s)
{
	char write_response[] = "write X NNN";
	uint16_t data_length;
	HDC1050_requestConversion();						// request new conversion		
	Delay_ms(50);										// wait for conversion end
	HDC1050_getTempHumi();								// read temperature from sensor		
	data_length = sizeof(head1)-1 + sizeof(head2)-1 +
			sizeof(resp1)-1 + sizeof(resp2)-1 + sizeof(resp3)-1 + 4;
	write_response[6] = s;	
	write_response[8] = (data_length / 100) + '0';
	write_response[9] = (data_length % 100) / 10 + '0';
	write_response[10] = (data_length % 10) + '0';
	WriteCR_AMW037(write_response);
	Write_AMW037(head1);	
	Write_AMW037(head2);	
	Write_AMW037(resp1);
	Write_AMW037(temp);
	Write_AMW037(resp2);
	Write_AMW037(humi);
	Write_AMW037(resp3);	
}

void badRequest(char s)
{
	char write_response[] = "write X NN";
	uint16_t data_length;	
	data_length = sizeof(badr1)-1 + sizeof(head2)-1 + sizeof(badr2)-1;
	write_response[6] = s;	
	write_response[8] = (data_length / 10) + '0';
	write_response[9] = (data_length % 10) + '0';
	WriteCR_AMW037(write_response);
	Write_AMW037(badr1);	
	Write_AMW037(head2);	
	Write_AMW037(badr2);	
}

void GPIO_EVEN_IRQHandler(void)
{
	GPIO->IFC = 1<<14;									// clear interrupt flag	
	data_ready = 1;
}
