#ifndef _WIFI9113_H_
#define _WIFI9113_H_

#include "main.h"

#define WiFi0824		0

#define RS9113			0
#define RS9110			1
#define ESP8266			2

#define WIFI_AS_SERVER	0
#define WIFI_AS_CLIENT	1

//#define CONNECT_Multiple_DEF	//多路连接

#define WIFIroute		WiFi0824
#define WIFImodule		ESP8266

#if(WIFIroute==WiFi0824)
#define RouteName		"WiFi0824-02"
#define RoutePSk		"1234567890"
#endif

#define AT_REPLY_OK     	("OK")
#define AT_REPLY_ERROR  	("ERROR")
#define AT_REPLY_NO_CHANGE  ("no change")


//启动
#if(WIFImodule == ESP8266)
#define CHECK_REPLY_Ready		("ready")//("System Ready")
#endif

#if(WIFIroute==WiFi0824)
#define SERVER_IP_ADDRESS_DEFAULT		"\"server0824.nat123.net\",7512"
#endif

//连接
#if(WIFImodule == ESP8266)
#define CONNECT_DOGOFF		("AT+CSYSWDTDISABLE")
#define CONNECT_DOGON		("AT+CSYSWDTENABLE")

#define CONNECT_RESET		("AT+RST\r\n")
#define CONNECT_CWMODE		("AT+CWMODE=3\r\n")
#define CONNECT_CWLAP		("AT+CWLAP\r\n")
#define CONNECT_CWJAP		("AT+CWJAP=\""##RouteName##"\",\""##RoutePSk##"\"\r\n")
#define CONNECT_CWJAP_CX	("AT+CWJAP?\r\n")
#define CONNECT_CWJAP_CX_REPLY	("+CWJAP:\""##RouteName##"\"")

#ifndef CONNECT_Multiple_DEF
#define CONNECT_CIPMUX		("AT+CIPMUX=0\r\n")//0.单路连接
#define CONNECT_CIPSTART	("AT+CIPSTART=\"TCP\","##SERVER_IP_ADDRESS_DEFAULT##"\r\n")
#define CONNECT_CIPSEND		("AT+CIPSEND=15\r\n")
#define CONNECT_CIPSEND_N	("AT+CIPSEND=")
#else
#define CONNECT_CIPMUX		("AT+CIPMUX=1\r\n")//1.0-4路连接
#define CONNECT_CIPSTART	("AT+CIPSTART=4,\"TCP\","##SERVER_IP_ADDRESS_DEFAULT##"\r\n")
#define CONNECT_CIPSEND		("AT+CIPSEND=4,15\r\n")
#define CONNECT_CIPSEND_N	("AT+CIPSEND=4")
#endif

#define CONNECT_CIPSTART_Welcome	("Welcome to the server")
#define CONNECT_CIPSTART_Port		("Port:7512")
#define CONNECT_CIPSTART_NOWis		("Now is:")

#define CONNECT_CIFSR		("AT+CIFSR\r\n")
#define CONNECT_APIP		("+CIFSR:APIP,\"")
#define CONNECT_APMAC		("+CIFSR:APMAC,\"")
#define CONNECT_STAIP		("+CIFSR:STAIP,\"")
#define CONNECT_STAMAC		("+CIFSR:STAMAC,\"")

#define CONNECT_CIPCLOSE	("AT+ CIPCLOSE=4\r\n")

#define CONNECT_SEND_EG		("I Can Hear You！\r\n")
#endif

//通讯
#if(WIFImodule == ESP8266)
#define AT_COMMAND_READ				("+IPD")
#define AT_COMMAND_LTCP_CONNECT		("AT+RSI_LTCP_CONNECT")
#define AT_COMMAND_CLOSE			("CLOSED")
#define AT_COMMAND_LINK_ERROR		("link is not valid")
#define AT_COMMAND_REGID_ERROR		("ERROR:Don't find your ID!")

#define CONNECT_REPLY_CONNECT			("CONNECT")
#define CONNECT_REPLY_ALREAY_CONNECT	("ALREADY CONNECTED")

#define CHAT_REPLY4
#endif


//#define	USB_AT_COMMAND_MAC			        "at+rsi_mac?\r\n"
//#define	USB_AT_COMMAND_MAC_LENGTH			13

#define AT_COMMAND_FWVERSION                "at+rsi_fwversion?"
#define	AT_COMMAND_FWVERSION_LENGTH			17

#define AT_KRAIN_SERVER_IP              	"KRAIN_SERVER_IP="

#if(WIFImodule == ESP8266)
#define	AT_COMMAND_SND_CMD					"AT+CIPSEND=4,15\r\n"
#define	AT_COMMAND_SND_CMD_LENGTH			16
#endif


enum wifi_led_flag_
{
    WIFI_CLOSE_FLAG,
    WIFI_JOIN_ROUTE_OK_FLAG,
    WIFI_LOAD_DONE_FLAG,
    WIFI_LTCP_CONNECT_FLAG,
    WIFI_DATA_COME_FLAG,
    WIFI_DATA_GO_FLAG,
    WIFI_MAX_FLAG
};


enum wifi_step_
{
    WIFI_STEP_CHECK,
    WIFI_STEP_CONNECT,
    WIFI_STEP_CHAT,
    WIFI_STEP_MAX
};

#define COMMD_DATA_RETURN 	0x00
#define COMMD_CONSOLE		0x01
#define COMMD_REG			0x02
#define COMMD_TRANSMIT		0x03
#define COMMD_AUTHORIZE		0x04

extern char bAtSentBuff[255];
extern char DoByteBuffer[255];
extern char bAtRecBuff[255];
extern unsigned char TxCounterWifi;
extern unsigned char RxCounterWifi;
extern unsigned char WifiDataToXmt;

extern unsigned char Mac_Addr[];
extern unsigned char Mac_Temp_Buff[];
extern unsigned char Wifi_Rec_Buff[];

extern unsigned char WifiProcessStep;
extern unsigned char WifiCheckStep;
extern unsigned char WifiConnectStep;
extern unsigned char WifiConnectNext;

extern unsigned long  WifiTimeReset;// Rec9113_time=0;
extern unsigned long  WifiTimeLed;// Rec9113_time=0;
extern unsigned char  start;

extern unsigned char TOTAL_MAC_LENGTH;
extern bool WifiHaveRecFlag;
extern unsigned char WifiDatCome;
extern unsigned char TotalRecvBytes;//REC  NUMBER
extern unsigned char SERVER_IP_ADDRESS[];

extern unsigned char WIFI_LED_FLAG;

extern bool if_sent_data;
extern char sent_data_buf[255];

extern void AtSent ( char *SendBuff, unsigned int BuffCount );
extern void wifi_connect ( void );
extern void wifi_main ( void );
extern void AT_command_send ( char *str, int handle, unsigned char n );

extern char *FindStrPos ( char *socStr, u8 socSize, char *findStr, u8 findSize );
extern char *FindATcommd ( char *findStr );

extern void reg_server0824 ( u8 regID, u8 *inBuffer, u8 inSize );
extern void sendto_server0824 ( u8 aimID, u8 myID, u8 *inBuffer, u8 inSize );

extern void Refresh_DS1302_Time ( char data[] );

#endif

