/****************************************************************************
	Knihovna obsahuje ovladac WiFi modulu. Popis chovani modulu je uveden v
	DataSheetu a manualu k AT+i protokolu.

*****************************************************************************/

#ifdef PCSIM
	#include "stdafx.h"
#endif
#include "WiFi.h"
#include "OS.h"
#include "Applications.h"
#include "GRAPHICS.h"
#include "PhysicalLayer.h"
#include "Drivers.h"


/****************************************************************************
	Globalni promenne modulu

*****************************************************************************/
ANSWER wifiBusy = NO;							//flag Wifi pracuje
WIFI_STATE wifiState = WIFI_IDLE;				//stav ovladace
ANSWER wifiOK = NO;								//wifi je v poradku
ANSWER wifiOnline = NO;							//wifi je online
ANSWER wifiError = NO;							//nastala chyba

unsigned char wifiBuffer[WIFI_BUFFER_LENGTH];	//Prijimaci buffer
int wifiBufferTop = 0;							//vrchol bufferu

/****************************************************************************
	Vraci info, jestli prikaz probehl v poradku

	Vstup: 
		neni

	Vystup
		ANSWER:		YES/NO

*****************************************************************************/
ANSWER WifiOK(void)
{
	return wifiOK;
}

/****************************************************************************
	Vraci info, jestli modul presel do stavu online

	Vstup: 
		neni

	Vystup
		ANSWER:		YES/NO

*****************************************************************************/
ANSWER WifiOnline(void)
{
	return wifiOnline;
}

/****************************************************************************
	Vraci info, jestli byla pri potvrzeni prikazu vracena chyba

	Vstup: 
		neni

	Vystup
		ANSWER:		YES/NO

*****************************************************************************/
ANSWER WifiError(void)
{
	return wifiError;
}

/****************************************************************************
	Vnuti ovladaci informaci o resetu modulu

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
void WifiWasRebooted(void)
{
	wifiOnline = NO;
}


/****************************************************************************
	Vraci informaci jestli modul prave zpracovava prikaz

	Vstup: 
		neni

	Vystup
		ANSWER:		YES/NO

*****************************************************************************/
ANSWER WifiBusy(void)
{
	return ((wifiState==WIFI_IDLE)?NO:YES);
}

/****************************************************************************
	Posle modulu prikaz k vykonani a zahaji cekani na odpoved

	Vstup: 
		command:		text prikazu

	Vystup
		neni

*****************************************************************************/
void WifiSendCommand(unsigned char *command)
{
	WifiClearBuffer();						//Vymaze prijimaci zasobnik
	SendSerialMessage(OS_WIFI,command);		//odesle prikaz
	wifiOK = NO;							//smaze informaci o stavu wifi
	wifiState = WIFI_WAIT_FOR_RESULT;		//zahaji cekani
}

/****************************************************************************
	Vymaze prijimaci buffer

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
void WifiClearBuffer(void)
{
	clearBuffer(wifiBuffer,WIFI_BUFFER_LENGTH);		//smaze buffer
	wifiBufferTop = 0;								//resetuje pocitadla
	wifiOK = YES;									//Nastavi wifi jako OK
}


/****************************************************************************
	Funkce zpracovava prijimana data z UARTu, ktera nalezi wifi modulu
	a vyhodnocuje informace o dokonceni operaci a pripadne chyby.

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
static void ProceedSerialData(void)
{
	if (CheckSerialBuffer(OS_WIFI) == YES)													//POkud byla prijata data
	{
                if (GetSerialDataCount(OS_WIFI) + wifiBufferTop > WIFI_BUFFER_LENGTH)		//Zjisti pocet prijatych dat. POkud je jich moc
                {
                  WifiClearBuffer();														//vsechna data smaz (nejspis se jedna o chybu v modulu)
                  wifiBufferTop = 0;														//vymaz buffer
#ifndef PCSIM
                  ClearSerialBuffer(OS_WIFI);												//i wifi buffer
#endif
                  WifiSendCommand((unsigned char*)"+++");									//posli prikaz k zastaveni SerialNet modu
                  return;
                  
                }
		wifiBufferTop += GetSerialData(OS_WIFI,wifiBuffer+wifiBufferTop);					//JInak data vloz do prijimaciho  bufferu
                
		for (int i = wifiBufferTop-2; i >= 0; i--)											//Zkontroluj flagy I/OK, I/ERROR, I/ONLINE
		{
			if (wifiBuffer[i] == '/' && wifiBuffer[i-1] == 'I')					
			{
				wifiState = WIFI_IDLE;														
				
				if (wifiBuffer[i+1] == 'O' && wifiBuffer[i+2] == 'K')
				{
					wifiOK = YES;
                                        wifiBufferTop = 0;
                                        wifiError = NO;
				}else if (wifiBuffer[i+1] == 'O' && wifiBuffer[i+2] == 'N' && wifiBuffer[i+6] == 'E')
				{
					wifiOnline = YES;
					wifiOK = YES;
                    wifiBufferTop = 0;
					wifiError = NO;
				}
                else if (wifiBuffer[i+1] == 'E' && wifiBuffer[i+2] == 'R' && wifiBuffer[i+3] == 'R')
                {
					wifiBufferTop = 0;
					clearBuffer(wifiBuffer,WIFI_BUFFER_LENGTH);
					wifiError = YES;
				}
			}

		}
	}
}

/****************************************************************************
	Ridici smycka modulu wifi. Je cyklicky spoustena systemem.

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
void DrvWifi(void)
{
	switch(wifiState)
	{
	case WIFI_IDLE:
		break;
	case WIFI_WAIT_FOR_RESULT:
		ProceedSerialData();
		break;
	}
}


