#ifndef __SCI_COMMUNICATION__
#define __SCI_COMMUNICATION__

#define CHECK_SUM_USE		(0)
#define MH01_DEBUG			(0)


#define RX_BUFF_SIZE		128
#define TX_BUFF_SIZE		128

#define START_CHAR			0x02
#define END_CHAR			0x03

#define WIFI_USER_UART_STX	0x2A
#define WIFI_USER_UART_ETX	0x0D

#define PACKET_DATA_POS		8

#define UART0_SEND()		TI0 = 1
#define PACKET_LENGTH(x)	x + 11

#define SCI_ACK				0x06
#define SCI_NAK				0x15

#define ACK_SEND()			Uart0_Putchar(SCI_ACK); UART0_SEND()
#define NAK_SEND()			Uart0_Putchar(SCI_NAK); UART0_SEND()
#define CHAR_SEND(x)		Uart0_Putchar(x); UART0_SEND()


enum STEP_KIND{STEP0, STEP1, STEP2, STEP3, STEP4, STEP5, STEP6, STEP_ERR};

#if 0 
enum MODE{IPRQ, IPST, TMST, MS06, MH01, SWCL, FRQ0, FRQ1, FRQ2, FRQ3, FRQ4, CTMP, ST01, ST02, ST03, ST04, ST05, ST06, ST07, ST08, ST09, ST10, ST11, ST21, ST22, PLMT, MODE_END};
#else
enum MODE{IPRQ, IPST, TMST, MS06, MH01, SWCL, FRQ0, FRQ1, FRQ2, FRQ3, FRQ4, CTMP, ST01, ST02, ST03, ST04, ST05, ST06, ST07, ST08, ST09, ST10, ST11, ST21, ST22, APID, IDPW, MODE_END};

#endif

typedef enum
{
	W_IPCONFIG = 0,
	W_CRYPTO,
	W_PSK,
	W_ASSOCIATE,
	W_ASSOCIATED,
	W_IPALLOCATED,
	W_SOCKET,
	W_CONNECTED,
	W_SENDOK,
	W_ASSOCIATED_ERR,
	W_CRYPTO_ERR,
	W_ASSOCIATED_ERR2,
	W_CLOSED,
	W_TIMEOUT,
	W_NWSTATUS,
	W_IPRELEASED,
	W_SEND_ERR2,
	W_POWERSAVE,
	W_DNSRESPONSE,
	W_MAX
}WIFI_CMD;

typedef struct
{
	unsigned int	tx_len;		// transmit length
	unsigned int	tx_ridx;	// rear index
	unsigned int	tx_fidx;	// front index
	unsigned char   *tx_ptr;	// tx buff pointer

	unsigned int	rx_len;		// receive length
	unsigned int	rx_ridx;	// rear index
	unsigned int	rx_fidx;	// front index
	unsigned char   *rx_ptr;	// rx buff pointer
}CQUEUE;


typedef struct
{
	unsigned char	mode;
	unsigned char	idx;
	unsigned char	checksum[2];
	unsigned char	buff[RX_BUFF_SIZE];
}CPACKET_STATE;


typedef struct
{
	unsigned char	stx;
	unsigned char   length[3];
	unsigned char	mode[4];
}CPACKET_HEAD;

typedef struct
{
	unsigned char year;
	unsigned char month;
	unsigned char day;
	unsigned char hour;

	unsigned long address;

	unsigned char flag		: 1;
	unsigned char option	: 1;
	unsigned char ack_flag	: 1;
	unsigned char nak_flag	: 1;
	unsigned char step	: 4;
}CMH01;


int  get_tx_queue(CQUEUE *q, unsigned char *ch);
void put_tx_queue(CQUEUE *q, unsigned char *buff, int len);
int  get_rx_queue(CQUEUE *q, unsigned char *ch);
void put_rx_queue(CQUEUE *q, unsigned char ch);

void sci_communication_init(void);
void sci_packet_analysis(void);
void sci_mode_operation(char *);
void sci_put_packet(unsigned char, unsigned char *, unsigned int);


void TMST_operation(unsigned char *);
void MS06_operation(void);
void MH01_operation(void);

void compensate_temperature(unsigned char *p);
///void Set_power_consumption(unsigned char *p);

void control_on_off(unsigned char *p);          // ST08
#if 0
void Set_Month_target(unsigned char *p)  ;  // ST01
void Set_Peak_time_day(unsigned char *p)   ; // ST02
void Set_Moment_Peak_power(unsigned char *p)   ; // ST03
void control_on_off(unsigned char *p);          // ST08
void Set_Voltage(unsigned char *p)   ; // ST09
void Set_CO2(unsigned char *p)   ; // ST10
void Set_Nation(unsigned char *p)   ; // ST11
void Set_Modulation(unsigned char *p)   ; // ST21
void Set_OSC(unsigned char *p)   ; // ST22
#endif
void WiFi_Associated_Err(U16 err_cnt);
void WiFi_Port_init(U8 Data);
void SW_Version_Response(unsigned char *p)   ; // ST23
///U8 read_idpwd_length(void);
#endif // __SCI_COMMUNICATION__
