#ifndef _ESP8266_H_
#define _ESP8266_H_
#include <stdint.h>
//this header compatible with firmware 1.3v TI ESP8266
#include "USART.h"

#define ESP_Sends(str)		        USART_PutS(str)
#define ESP_Sendn(n)		          USART_PutN(n)
#define ESP_SendData(data,length)	USART_PutData(data,length)


#define ESP_BufferSize		        128
#define ESP_ReceiveBufferSize       64


#define ESP_Station			1
#define ESP_AccessPoint	2
#define ESP_AP_STA			3

#define ESP_Echo_Off		0
#define ESP_Echo_On		  1

#define ESP_MultiConnection_On  1
#define ESP_MultiConnection_Off 0

//define this for use function with delay
#define ESP_Delay

//static Answers
#define ESP_NumOfAnswers	9
extern const char ESP_Answer_OK[];
extern const char ESP_Answer_Fail[];
extern const char ESP_Answer_Error[];
extern const char ESP_Answer_Diconnected[];
extern const char ESP_Answer_Connected[];
extern const char ESP_Answer_ReceiveData[];
extern const char ESP_Answer_GotIP[];
extern const char ESP_Answer_TCP_Connect[];
extern const char ESP_Answer_TCP_Close[];

typedef struct _ESP_StrFinderDesc{
	flash const char* 	    Strs[ESP_NumOfAnswers];
	uint8_t 			Length;
}ESP_StrFinderDesc;

typedef enum _ESP_Answer{
	ESP_Nothing = 0xff,
	ESP_Ok = 0,
	ESP_Fail = 1,
	ESP_Error = 2,
	ESP_Disconnected = 3,
	ESP_Connected = 4,
	ESP_ReceiveDataAnswer = 5,
	ESP_GotIP = 6,
	ESP_TCP_Connect = 7,
	ESP_TCP_Close	= 8
}ESP_Answer;

typedef struct _ESP_Flags{
	uint8_t WIFI_Connected  : 1;
	uint8_t WIFI_GotIP			: 1;
	uint8_t Answer_Received : 1;
	uint8_t MultiConnection : 1;
	uint8_t WaitingForAnswer: 1;
	uint8_t TCPConnected		: 1;
	uint8_t DataReceiving		: 1; // in receive data process
	uint8_t DataReceived		: 1; // data receiving process done
}ESP_flags;


extern ESP_flags  ESP_Flags;
extern ESP_Answer ESP_LastAnswer;
extern ESP_StrFinderDesc ESP_StaticStrs;
extern uint8_t  ESP_ReceiveData[ESP_ReceiveBufferSize];
extern uint16_t ESP_ReceiveData_Index;
extern uint16_t ESP_SearchIndex;

//Basic Functions
void ESP_Init(void);
void ESP_Restart(void);
void ESP_Version(void);
void ESP_Echo(uint8_t State);
void ESP_Check(void);
void ESP_AppendChar(char c);
//WiFi Functions
void ESP_WiFiMode(uint8_t Mode);
void ESP_Connect(char* SSID, char* Password);
void ESP_Disconnect(void);
void ESP_setAccessPoint(char* SSID, char* Password);
void ESP_setMAC_Station(char* MAC);
void ESP_setMAC_AccessPoint(char* MAC);
void ESP_setIP_Station(char* IP);
void ESP_setIP_AccessPoint(char* IP);
//TCP Functions
void ESP_TCPConnect(char* RemoteIP,uint16_t RemotePort);
void ESP_TCPSendData(uint8_t* Data, uint16_t Length);
void ESP_TCPClose(void);
void ESP_TCPServer(uint16_t Port);
void ESP_MultiConnection(uint8_t State);
void ESP_ReleaseReceiveBuffer(void);
//we need timer for this function
void ESP_CheckAnswer(void);

#endif //_ESP8266_H_

