#include "PROJECT_INCLUDES.H"



xdata volatile CQUEUE		 Qa;
xdata volatile CPACKET_STATE ps;
xdata volatile unsigned char rx_buff[RX_BUFF_SIZE];
xdata volatile unsigned char tx_buff[TX_BUFF_SIZE];

xdata volatile CMH01		 mh01;
xdata volatile unsigned char          packet_buff[TX_BUFF_SIZE];
xdata volatile unsigned char          packet_buff_wifi[TX_BUFF_SIZE];
xdata U8 rcv_packet[RX_BUFF_SIZE];
xdata U8 wifi_buff[RX_BUFF_SIZE];
xdata U8 temp_wifi_rssi_level[8];
xdata U8 wifi_rssi_level[4];
xdata volatile unsigned char content_buff[100];

xdata U8 WiFi_MyIP_buff[15];
xdata U8 DNSSVR_IP[15];
unsigned char idpwd_length;
unsigned char pwd_length;
U8 wifi_id[20];
U8 req_data[20];

///xdata volatile CPOWERS_AUG	 p_aug;
xdata volatile char			 sci_mode_str[MODE_END][5];
///xdata volatile char			 wifi_tabl[12][15];
xdata volatile char			 sci_time;
volatile char			 flag_tmst_set;

U8 Finish_wifi_setting; 
U8 assc_cnt;
U8 err_associate_cnt;
U8 find_crypto;
U8 fix_associate_cnt;
U8 wifi_data_send_ok;
U8 cmd_start;
U8 iprq_send;
U8 current_state;

U8	hWiFi_Send_Event_Timer;
U8	hWiFi_Send_Event_Timer1;

// extern variable
///extern volatile CPOWERS		 power;
///extern volatile CHEAD		 ph;

U8 ipallocation_cnt;
/*xdata unsigned char *sci_mode_str[]=
{
	"IPRQ", "MS06", "MD01", "MH01"
};*/
///extern TIER_LEVEL_FRAM_STRUCT volatile	Cost;
///extern void phyWrite (U8 reg, U8 value);   // cmlee
#if 1 
extern U8 phyRead (U8 reg);   // cmlee
//extern void   setTRxFrequency (U32 frequency);
//extern U32   divideWithRounding (U32 value, U32 divisor);
//extern void setTxPower (U8 power);
//extern void setTxFrequencyDeviation (U32 deviation);
//extern void setTxDataRate (U32 dataRate);
void int_to_str(unsigned int x, unsigned char *p, char len);
void Crypto_type(U8 cnt);
void wifi_put_packet(unsigned char mode, unsigned char *p, unsigned int len);

#endif
///////////////////////////////////////////////////////////////
// QUEUE 관련
int get_tx_queue(CQUEUE *q, unsigned char *ch)
{
	if(q->tx_fidx == q->tx_ridx)
		return 0;

	*ch = q->tx_ptr[q->tx_ridx];
	q->tx_ridx++;

	if(q->tx_ridx >= TX_BUFF_SIZE)
		q->tx_ridx = 0;

	return 1;
}

void put_tx_queue(CQUEUE *q, unsigned char *buff, int len)
{
	int i;

	if(!len || !q->tx_ptr)return;

	for(i = 0; i < len; i++)
	{
		q->tx_ptr[q->tx_fidx] = buff[i];
		q->tx_fidx++;

		if(q->tx_fidx >= TX_BUFF_SIZE)
			q->tx_fidx = 0;
	}
}

int get_rx_queue(CQUEUE *q, unsigned char *ch)
{
	if(q->rx_fidx == q->rx_ridx)
		return 0;

	*ch = q->rx_ptr[q->rx_ridx];
	q->rx_ridx++;

	if(q->rx_ridx >= RX_BUFF_SIZE)
		q->rx_ridx = 0;

	return 1;
}

void put_rx_queue(CQUEUE *q, unsigned char ch)
{
	q->rx_ptr[q->rx_fidx]= ch;
	q->rx_fidx++;

	if(q->rx_fidx >= RX_BUFF_SIZE)
		q->rx_fidx = 0;
}
///////////////////////////////////////////////////////////////
void WiFi_Port_init(U8 Data)
{
    //Write_GPIO0(Data);
    WIFI_RSTN = Data;
}

U8 Is_WiFi_Set(void)
{
    if(event1.wifi_pwd_size == 0) return 0;
    else return 1;
}

U8 Wifi_Table[15];
void wifi_table_init(U16 cnt, U8 *wtbl)
{
	U8 i;
#if 1

      for(i=0; i < 15; i++) Wifi_Table[i] = ' ';

	switch (cnt)
	{
		case 0:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'D';
			Wifi_Table[2] = 'E';
			Wifi_Table[3] = 'V';
			Wifi_Table[4] = 'I';
			Wifi_Table[5] = 'C';
			Wifi_Table[6] = 'E';
			Wifi_Table[7] = 'R';
			Wifi_Table[8] = 'E';
			Wifi_Table[9] = 'A';
			Wifi_Table[10] = 'D';
			Wifi_Table[11] = 'Y';
			break;
		case 1:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'I';
			Wifi_Table[2] = 'P';
			Wifi_Table[3] = 'C';
			Wifi_Table[4] = 'O';
			Wifi_Table[5] = 'N';
			Wifi_Table[6] = 'F';
			Wifi_Table[7] = 'I';
			Wifi_Table[8] = 'G';
			Wifi_Table[9] = ':';
			Wifi_Table[10] = 'O';
			Wifi_Table[11] = 'K';
			break;
		case 2:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'C';
			Wifi_Table[2] = 'R';
			Wifi_Table[3] = 'Y';
			Wifi_Table[4] = 'P';
			Wifi_Table[5] = 'T';
			Wifi_Table[6] = 'O';
			Wifi_Table[7] = ':';
			Wifi_Table[8] = 'O';
			Wifi_Table[9] = 'K';
			break;
		case 3:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'P';
			Wifi_Table[2] = 'S';
			Wifi_Table[3] = 'K';
			Wifi_Table[4] = ':';
			Wifi_Table[5] = 'O';
			Wifi_Table[6] = 'K';
			break;
		case 4:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'A';
			Wifi_Table[2] = 'S';
			Wifi_Table[3] = 'S';
			Wifi_Table[4] = 'O';
			Wifi_Table[5] = 'C';
			Wifi_Table[6] = 'I';
			Wifi_Table[7] = 'A';
			Wifi_Table[8] = 'T';
			Wifi_Table[9] = 'E';
			Wifi_Table[10] = 'D';
			Wifi_Table[11] = ':';
			Wifi_Table[12] = '0';
			break;
		case 5:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'I';
			Wifi_Table[2] = 'P';
			Wifi_Table[3] = 'A';
			Wifi_Table[4] = 'L';
			Wifi_Table[5] = 'L';
			Wifi_Table[6] = 'O';
			Wifi_Table[7] = 'C';
			Wifi_Table[8] = 'A';
			Wifi_Table[9] = 'T';
			Wifi_Table[10] = 'E';
			Wifi_Table[11] = 'D';
			break;
		case 6:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'S';
			Wifi_Table[2] = 'O';
			Wifi_Table[3] = 'C';
			Wifi_Table[4] = 'K';
			Wifi_Table[5] = 'E';
			Wifi_Table[6] = 'T';
			Wifi_Table[7] = ':';
			Wifi_Table[8] = 'O';
			Wifi_Table[9] = 'K';
			break;
		case 7:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'C';
			Wifi_Table[2] = 'O';
			Wifi_Table[3] = 'N';
			Wifi_Table[4] = 'N';
			Wifi_Table[5] = 'E';
			Wifi_Table[6] = 'C';
			Wifi_Table[7] = 'T';
			Wifi_Table[8] = 'E';
			Wifi_Table[9] = 'D';
			Wifi_Table[10] = ':';
			Wifi_Table[11] = '0';
			break;
		case 8:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'S';
			Wifi_Table[2] = 'E';
			Wifi_Table[3] = 'N';
			Wifi_Table[4] = 'D';
			Wifi_Table[5] = ':';
			Wifi_Table[6] = 'O';
			Wifi_Table[7] = 'K';
			break;
		case 9:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'D';
			Wifi_Table[2] = 'I';
			Wifi_Table[3] = 'S';
			Wifi_Table[4] = 'A';
			Wifi_Table[5] = 'S';
			Wifi_Table[6] = 'S';
			Wifi_Table[7] = 'O';
			Wifi_Table[8] = 'C';
			Wifi_Table[9] = 'I';
			Wifi_Table[10] = 'A';
			Wifi_Table[11] = 'T';
			Wifi_Table[12] = 'E';
			Wifi_Table[13] = 'D';
			break;
		case 10:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'C';
			Wifi_Table[2] = 'R';
			Wifi_Table[3] = 'Y';
			Wifi_Table[4] = 'P';
			Wifi_Table[5] = 'T';
			Wifi_Table[6] = 'O';
			Wifi_Table[7] = ':';
			Wifi_Table[8] = 'E';
			Wifi_Table[9] = 'R';
			Wifi_Table[10] = 'R';
			Wifi_Table[11] = 'O';
			Wifi_Table[12] = 'R';
			break;
		case 11:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'A';
			Wifi_Table[2] = 'S';
			Wifi_Table[3] = 'S';
			Wifi_Table[4] = 'O';
			Wifi_Table[5] = 'C';
			Wifi_Table[6] = 'I';
			Wifi_Table[7] = 'A';
			Wifi_Table[8] = 'T';
			Wifi_Table[9] = 'E';
			Wifi_Table[10] = 'D';
			Wifi_Table[11] = ':';
			Wifi_Table[12] = '2';
			break;
    		case 12:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'C';
			Wifi_Table[2] = 'L';
			Wifi_Table[3] = 'O';
			Wifi_Table[4] = 'S';
			Wifi_Table[5] = 'E';
			Wifi_Table[6] = 'D';
			break;
            case 13:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'T';
			Wifi_Table[2] = 'I';
			Wifi_Table[3] = 'M';
			Wifi_Table[4] = 'E';
			Wifi_Table[5] = 'O';
			Wifi_Table[6] = 'U';
			Wifi_Table[7] = 'T';
			break;
		case 14:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'N';
			Wifi_Table[2] = 'W';
			Wifi_Table[3] = 'S';
			Wifi_Table[4] = 'T';
			Wifi_Table[5] = 'A';
			Wifi_Table[6] = 'T';
			Wifi_Table[7] = 'U';
			Wifi_Table[8] = 'S';
			Wifi_Table[9] = ':';
			Wifi_Table[10] = 'O';
			Wifi_Table[11] = 'K';
			break;
             case 15:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'I';
			Wifi_Table[2] = 'P';
			Wifi_Table[3] = 'R';
			Wifi_Table[4] = 'E';
			Wifi_Table[5] = 'L';
			Wifi_Table[6] = 'E';
			Wifi_Table[7] = 'A';
			Wifi_Table[8] = 'S';
			Wifi_Table[9] = 'E';
			Wifi_Table[10] = 'D';
			break;
             case 16:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'S';
			Wifi_Table[2] = 'E';
			Wifi_Table[3] = 'N';
			Wifi_Table[4] = 'D';
			Wifi_Table[5] = ':';
			Wifi_Table[6] = 'E';
			Wifi_Table[7] = 'R';
			Wifi_Table[8] = 'R';
			Wifi_Table[9] = 'O';
			Wifi_Table[10] = 'R';
			break;
             case 17:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'h';
			Wifi_Table[2] = 'w';
			Wifi_Table[3] = 'p';
			Wifi_Table[4] = 's';
			Wifi_Table[5] = 'i';
			Wifi_Table[6] = 'n';
			Wifi_Table[7] = 'd';
			Wifi_Table[8] = ':';
			break;
             case 18:
			Wifi_Table[0] = '*';
			Wifi_Table[1] = 'D';
			Wifi_Table[2] = 'N';
			Wifi_Table[3] = 'S';
			Wifi_Table[4] = 'R';
			Wifi_Table[5] = 'E';
			Wifi_Table[6] = 'S';
			Wifi_Table[7] = 'P';
			Wifi_Table[8] = 'O';
			Wifi_Table[9] = 'N';
			Wifi_Table[10] = 'S';
			Wifi_Table[11] = 'E';
			break;
             case 19:
			Wifi_Table[0] = 'M';
			Wifi_Table[1] = 'A';
			Wifi_Table[2] = 'X';
			break;
		default:
			break;
	}
#endif	
}
U8 test_min;
void sci_communication_init(void)
{
    int i = 0;
    test_min = 0;
	memset((void *)rx_buff, 0, sizeof(rx_buff));
	memset((void *)tx_buff, 0, sizeof(tx_buff));
	memset((void *)&ps, 0, sizeof(ps));
	memset((void *)&Qa, 0, sizeof(Qa));
	memset((void *)&packet_buff, 0, sizeof(packet_buff));
	memset((void *)&packet_buff_wifi, 0, sizeof(packet_buff_wifi));
	memset((void *)&rcv_packet, 0, sizeof(rcv_packet));
	memset((void *)&wifi_buff, 0, sizeof(wifi_buff));

	sci_time = 0;
      flag_tmst_set = 0;
	  	
	Finish_wifi_setting = 0;
	err_associate_cnt = 0;
	iprq_send = 0;
	assc_cnt = 0;
	cmd_start =0;
      fix_associate_cnt = 0;
      wifi_data_send_ok = 0;
      ipallocation_cnt = 0;
      current_state = 0;
	
///	Crypto_type(err_associate_cnt);

	Qa.rx_ptr = rx_buff;
	Qa.tx_ptr = tx_buff;

	sci_mode_str[0][0] = 'I';
	sci_mode_str[0][1] = 'P';
	sci_mode_str[0][2] = 'R';
	sci_mode_str[0][3] = 'Q';
	sci_mode_str[0][4] = 0;

	sci_mode_str[1][0] = 'I';
	sci_mode_str[1][1] = 'P';
	sci_mode_str[1][2] = 'S';
	sci_mode_str[1][3] = 'T';
	sci_mode_str[1][4] = 0;

	sci_mode_str[2][0] = 'T';
	sci_mode_str[2][1] = 'M';
	sci_mode_str[2][2] = 'S';
	sci_mode_str[2][3] = 'T';
	sci_mode_str[2][4] = 0;

	sci_mode_str[3][0] = 'M';
	sci_mode_str[3][1] = 'S';
	sci_mode_str[3][2] = '0';
	sci_mode_str[3][3] = '6';
	sci_mode_str[3][4] = 0;

	sci_mode_str[4][0] = 'M';
	sci_mode_str[4][1] = 'H';
	sci_mode_str[4][2] = '0';
	sci_mode_str[4][3] = '1';
	sci_mode_str[4][4] = 0;

	sci_mode_str[5][0] = 'S';
	sci_mode_str[5][1] = 'W';
	sci_mode_str[5][2] = 'C';
	sci_mode_str[5][3] = 'L';
	sci_mode_str[5][4] = 0;
// inserted by cmlee to change frequency, deviation, data rate, so on
#if 0
	sci_mode_str[6][0] = 'F';
	sci_mode_str[6][1] = 'R';
	sci_mode_str[6][2] = 'Q';
	sci_mode_str[6][3] = '0';
	sci_mode_str[6][4] = 0;

	sci_mode_str[7][0] = 'F';
	sci_mode_str[7][1] = 'R';
	sci_mode_str[7][2] = 'Q';
	sci_mode_str[7][3] = '1';
	sci_mode_str[7][4] = 0;

	sci_mode_str[8][0] = 'F';
	sci_mode_str[8][1] = 'R';
	sci_mode_str[8][2] = 'Q';
	sci_mode_str[8][3] = '2';
	sci_mode_str[8][4] = 0;

	sci_mode_str[9][0] = 'F';
	sci_mode_str[9][1] = 'R';
	sci_mode_str[9][2] = 'Q';
	sci_mode_str[9][3] = '3';
	sci_mode_str[9][4] = 0;

	sci_mode_str[10][0] = 'F';
	sci_mode_str[10][1] = 'R';
	sci_mode_str[10][2] = 'Q';
	sci_mode_str[10][3] = '4';
	sci_mode_str[10][4] = 0;
#endif
	sci_mode_str[11][0] = 'C';
	sci_mode_str[11][1] = 'T';
	sci_mode_str[11][2] = 'M';
	sci_mode_str[11][3] = 'P';
	sci_mode_str[11][4] = 0;

	sci_mode_str[12][0] = 'S';
	sci_mode_str[12][1] = 'T';
	sci_mode_str[12][2] = '0';
	sci_mode_str[12][3] = '1';
	sci_mode_str[12][4] = 0;

	sci_mode_str[13][0] = 'S';
	sci_mode_str[13][1] = 'T';
	sci_mode_str[13][2] = '0';
	sci_mode_str[13][3] = '2';
	sci_mode_str[13][4] = 0;

	sci_mode_str[14][0] = 'S';
	sci_mode_str[14][1] = 'T';
	sci_mode_str[14][2] = '0';
	sci_mode_str[14][3] = '3';
	sci_mode_str[14][4] = 0;

	sci_mode_str[15][0] = 'S';
	sci_mode_str[15][1] = 'T';
	sci_mode_str[15][2] = '0';
	sci_mode_str[15][3] = '4';
	sci_mode_str[15][4] = 0;

	sci_mode_str[16][0] = 'S';
	sci_mode_str[16][1] = 'T';
	sci_mode_str[16][2] = '0';
	sci_mode_str[16][3] = '5';
	sci_mode_str[16][4] = 0;

	sci_mode_str[17][0] = 'S';
	sci_mode_str[17][1] = 'T';
	sci_mode_str[17][2] = '0';
	sci_mode_str[17][3] = '6';
	sci_mode_str[17][4] = 0;

	sci_mode_str[18][0] = 'S';
	sci_mode_str[18][1] = 'T';
	sci_mode_str[18][2] = '0';
	sci_mode_str[18][3] = '7';
	sci_mode_str[18][4] = 0;

	sci_mode_str[19][0] = 'S';
	sci_mode_str[19][1] = 'T';
	sci_mode_str[19][2] = '0';
	sci_mode_str[19][3] = '8';
	sci_mode_str[19][4] = 0;

	sci_mode_str[20][0] = 'S';
	sci_mode_str[20][1] = 'T';
	sci_mode_str[20][2] = '0';
	sci_mode_str[20][3] = '9';
	sci_mode_str[20][4] = 0;

	sci_mode_str[21][0] = 'S';
	sci_mode_str[21][1] = 'T';
	sci_mode_str[21][2] = '1';
	sci_mode_str[21][3] = '0';
	sci_mode_str[21][4] = 0;

	sci_mode_str[22][0] = 'S';
	sci_mode_str[22][1] = 'T';
	sci_mode_str[22][2] = '1';
	sci_mode_str[22][3] = '1';
	sci_mode_str[22][4] = 0;
//==================New attached for manufacturing 
	sci_mode_str[23][0] = 'S';
	sci_mode_str[23][1] = 'T';
	sci_mode_str[23][2] = '2';
	sci_mode_str[23][3] = '1';
	sci_mode_str[23][4] = 0;

	sci_mode_str[24][0] = 'S';
	sci_mode_str[24][1] = 'T';
	sci_mode_str[24][2] = '2';
	sci_mode_str[24][3] = '2';
	sci_mode_str[24][4] = 0;

	sci_mode_str[25][0] = 'A';
	sci_mode_str[25][1] = 'P';
	sci_mode_str[25][2] = 'I';
	sci_mode_str[25][3] = 'D';
	sci_mode_str[25][4] = 0;

	sci_mode_str[26][0] = 'I';
	sci_mode_str[26][1] = 'D';
	sci_mode_str[26][2] = 'P';
	sci_mode_str[26][3] = 'W';
	sci_mode_str[26][4] = 0;
#if 0
	sci_mode_str[25][0] = 'P';
	sci_mode_str[25][1] = 'L';
	sci_mode_str[25][2] = 'M';
	sci_mode_str[25][3] = 'T';
	sci_mode_str[25][4] = 0;

	sci_mode_str[25][0] = 'S';
	sci_mode_str[25][1] = 'T';
	sci_mode_str[25][2] = '2';
	sci_mode_str[25][3] = '3';
	sci_mode_str[25][4] = 0;
#endif
	// cmlee end
#if 1
	packet_buff_wifi[0]= 'A';
	packet_buff_wifi[1]= 'T';
	packet_buff_wifi[2]= '*';
	packet_buff_wifi[3]= 'I';
	packet_buff_wifi[4]= 'C';
	packet_buff_wifi[5]= 'T';
	packet_buff_wifi[6]= '*';
#endif

	hWiFi_Send_Event_Timer = timerSecHandleAssign();
	timerSecTimerSet(hWiFi_Send_Event_Timer, 10);

    hWiFi_Send_Event_Timer1 = timerSecHandleAssign();
	timerSecTimerSet(hWiFi_Send_Event_Timer1, 120);

	if(0)//event1.device_info [0]==255)
	{  
            event1.wifi_id_size = 0;
            event1.wifi_pwd_size =0;
            memset(event1.wifi_id, 0, 30);
            memset(event1.wifi_pwd, 0, 30);
            memset(event1.my_ip, 0, 20);

         
            event1.wifi_id[0] = 'H'; event1.wifi_id[1] = 'J'; event1.wifi_id[2] = 'H';
            //event1.wifi_id[0] = 'k'; event1.wifi_id[1] = 'o'; event1.wifi_id[2] = 'r';event1.wifi_id[3] = 'i'; event1.wifi_id[4] = 'n'; event1.wifi_id[5] = 's';event1.wifi_id[6] = '_'; event1.wifi_id[7] = 'I'; event1.wifi_id[8] = 'n';event1.wifi_id[9] = 'c';
            event1.wifi_id_size =  3; // 10;//
            event1.wifi_pwd[0] = '1';event1.wifi_pwd[1] = '2';event1.wifi_pwd[2] = '3';event1.wifi_pwd[3] = '1';event1.wifi_pwd[4] = '2';event1.wifi_pwd[5] = '3';event1.wifi_pwd[6] = '1';event1.wifi_pwd[7] = '2';event1.wifi_pwd[8] = '3';
            //event1.wifi_pwd[0] = 's';event1.wifi_pwd[1] = 'o';event1.wifi_pwd[2] = 'l';event1.wifi_pwd[3] = 'a';event1.wifi_pwd[4] = 'r';event1.wifi_pwd[5] = 's';event1.wifi_pwd[6] = 'o';event1.wifi_pwd[7] = 'l';event1.wifi_pwd[8] = 'a';event1.wifi_pwd[9] = 'r';
            event1.wifi_pwd_size =  9; //10;//

            System_Parameter_Init(SYSTEM_SETING);
 
            event1.wifi_id_size = 0;
            event1.wifi_pwd_size =0;
            event1.device_info_size = 0;
            memset(event1.wifi_id, 0, 30);
            memset(event1.wifi_pwd, 0, 30);
            memset(event1.device_info, 0, 90);
            memset(event1.my_ip, 0, 20);
#if 0            
            #if 1
            event1.device_info[i++]='0';	event1.device_info[i++]='0'; event1.device_info[i++]='0';	
            event1.device_info[i++]='5';	event1.device_info[i++]='4'; event1.device_info[i++]='7';
            //MAC
            event1.device_info[i++]='1';event1.device_info[i++]='0';event1.device_info[i++]='.';
            event1.device_info[i++]='1';event1.device_info[i++]='2';event1.device_info[i++]='.';
            event1.device_info[i++]='1';event1.device_info[i++]='8';event1.device_info[i++]='.';
            event1.device_info[i++]='0';event1.device_info[i++]='0';event1.device_info[i++]='.';
            event1.device_info[i++]='0';event1.device_info[i++]='2';event1.device_info[i++]='.';
            event1.device_info[i++]='0';event1.device_info[i++]='3';
#if 0
            //서버IP
            event1.device_info[i++]='1';event1.device_info[i++]='8';event1.device_info[i++]='2';event1.device_info[i++]='.';
            event1.device_info[i++]='1';event1.device_info[i++]='6';event1.device_info[i++]='3';event1.device_info[i++]='.';
            event1.device_info[i++]='2';event1.device_info[i++]='3';event1.device_info[i++]='4';event1.device_info[i++]='.';
            event1.device_info[i++]='1';event1.device_info[i++]='7';event1.device_info[i++]='1';
#else
                        //서버JP IP
            event1.device_info[i++]='1';event1.device_info[i++]='8';event1.device_info[i++]='2';event1.device_info[i++]='.';
            event1.device_info[i++]='1';event1.device_info[i++]='6';event1.device_info[i++]='3';event1.device_info[i++]='.';
            event1.device_info[i++]='2';event1.device_info[i++]='0';event1.device_info[i++]='1';event1.device_info[i++]='.';
            event1.device_info[i++]='0';event1.device_info[i++]='8';event1.device_info[i++]='2';
#endif
            //My IP
            event1.device_info[i++]='1';event1.device_info[i++]='9';event1.device_info[i++]='2';event1.device_info[i++]='.';
            event1.device_info[i++]='1';event1.device_info[i++]='6';event1.device_info[i++]='8';event1.device_info[i++]='.';
            event1.device_info[i++]='0';event1.device_info[i++]='0';event1.device_info[i++]='0';event1.device_info[i++]='.';
            event1.device_info[i++]='0';event1.device_info[i++]='5';event1.device_info[i++]='7';

            event1.device_info[i++]='4';event1.device_info[i++]='7';event1.device_info[i++]='0';event1.device_info[i++]='0';
#endif                    
            event1.wifi_id[0] = 'H'; event1.wifi_id[1] = 'J'; event1.wifi_id[2] = 'H';
            //event1.wifi_id[0] = 'k'; event1.wifi_id[1] = 'o'; event1.wifi_id[2] = 'r';event1.wifi_id[3] = 'i'; event1.wifi_id[4] = 'n'; event1.wifi_id[5] = 's';event1.wifi_id[6] = '_'; event1.wifi_id[7] = 'I'; event1.wifi_id[8] = 'n';event1.wifi_id[9] = 'c';
            event1.wifi_id_size =  3; // 10;//
            event1.wifi_pwd[0] = '1';event1.wifi_pwd[1] = '2';event1.wifi_pwd[2] = '3';event1.wifi_pwd[3] = '1';event1.wifi_pwd[4] = '2';event1.wifi_pwd[5] = '3';event1.wifi_pwd[6] = '1';event1.wifi_pwd[7] = '2';event1.wifi_pwd[8] = '3';
            //event1.wifi_pwd[0] = 's';event1.wifi_pwd[1] = 'o';event1.wifi_pwd[2] = 'l';event1.wifi_pwd[3] = 'a';event1.wifi_pwd[4] = 'r';event1.wifi_pwd[5] = 's';event1.wifi_pwd[6] = 'o';event1.wifi_pwd[7] = 'l';event1.wifi_pwd[8] = 'a';event1.wifi_pwd[9] = 'r';
            event1.wifi_pwd_size =  9; //10;//
            #endif

                System_Parameter_Init(SYSTEM_SETING);
        }
}

void IDPW_mode_tx_set(void)
{
	U8 jj=0,i;
	U8 chksum,msb,lsb;	

	wifi_id[jj++] = START_CHAR;
	wifi_id[jj++] = '0';
	wifi_id[jj++] = '0';
	wifi_id[jj++] = '0';
	wifi_id[jj++] = 'I';
	wifi_id[jj++] = 'D';;
	wifi_id[jj++] = 'P';
	wifi_id[jj++] = 'W';
	
	chksum=0;
	for(i=1;i<jj;i++) chksum ^=wifi_id[i];	
	msb=(chksum/0x10) & 0x0F;
	lsb=chksum & 0x0F;
	
	if(msb > 9)     msb+=0x37;
	else 			msb+='0';
	
	if(lsb > 9)   	lsb+=0x37;
	else 			lsb+='0';
	wifi_id[jj++]=msb;
	wifi_id[jj++]=lsb;		
	wifi_id[jj++]=END_CHAR;

	sci_mode_operation(wifi_id);
}

void IPRQ_mode_req_set(void)
{
	U8 jj=0,i;
	U8 chksum,msb,lsb;	

	req_data[jj++] = START_CHAR;
	req_data[jj++] = '0';
	req_data[jj++] = '0';
	req_data[jj++] = '0';
	req_data[jj++] = 'I';
	req_data[jj++] = 'P';;
	req_data[jj++] = 'R';
	req_data[jj++] = 'Q';
	
	chksum=0;
	for(i=1;i<jj;i++) chksum ^=req_data[i];	
	msb=(chksum/0x10) & 0x0F;
	lsb=chksum & 0x0F;
	
	if(msb > 9)     msb+=0x37;
	else 			msb+='0';
	
	if(lsb > 9)   	lsb+=0x37;
	else 			lsb+='0';
	req_data[jj++]=msb;
	req_data[jj++]=lsb;		
	req_data[jj++]=END_CHAR;

	sci_mode_operation(req_data);
}

void MS06_mode_req_set(void)
{
	U8 jj=0,i;
	U8 chksum,msb,lsb;	

	req_data[jj++] = START_CHAR;
	req_data[jj++] = '0';
	req_data[jj++] = '0';
	req_data[jj++] = '0';
	req_data[jj++] = 'M';
	req_data[jj++] = 'S';;
	req_data[jj++] = '0';
	req_data[jj++] = '6';
	
	chksum=0;
	for(i=1;i<jj;i++) chksum ^=req_data[i];	
	msb=(chksum/0x10) & 0x0F;
	lsb=chksum & 0x0F;
	
	if(msb > 9)     msb+=0x37;
	else 			msb+='0';
	
	if(lsb > 9)   	lsb+=0x37;
	else 			lsb+='0';
	req_data[jj++]=msb;
	req_data[jj++]=lsb;		
	req_data[jj++]=END_CHAR;

	sci_mode_operation(req_data);
}

void WiFi_Set_Cmd(unsigned char *w_cmd, unsigned char *w_param )
{
	int i, jj;
	int CmdLen, ParamLen;
	jj = 7;

	#if 1
	CmdLen = strlen( (char*) w_cmd);
	ParamLen = strlen( (char*) w_param);

	for(i=0; i < CmdLen; i++)
	{
		
		if( *w_cmd==' ')
		{ 
			;//
		}
		else
		{			
			packet_buff_wifi[jj++]= w_cmd[i];
		}
	}

	if(w_param != NULL)
	{
		for(i=0; i < ParamLen; i++)
		{
			packet_buff_wifi[jj++]= w_param[i];
		}
	}

	packet_buff_wifi[jj++]=WIFI_USER_UART_ETX;
	#endif
	put_tx_queue(&Qa, packet_buff_wifi, jj);
	UART0_SEND();
}
#if 1
//#define ASSC_ERR_TABLE 25
//char WiFi_Associate_tabl[ASSC_ERR_TABLE][5]=

U8 Crypto_Table[5];
void Crypto_type(U8 cnt)
{
	U8 Cyripto[5];

    #if 1
	switch(cnt)
	{
		case 0:
                    Crypto_Table[0]='1';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='1';
                    break;
		case 1:
                    Crypto_Table[0]='0';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 2:
                    Crypto_Table[0]='3';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='1';
			break;
		case 3:
                    Crypto_Table[0]='3';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 4:
			Crypto_Table[0]='3';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 5:
			Crypto_Table[0]='3';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 6:
			Crypto_Table[0]='3';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 7:
			Crypto_Table[0]='3';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='1';
			break;
		case 8:
			Crypto_Table[0]='1';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 9:
			Crypto_Table[0]='1';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 10:
			Crypto_Table[0]='0';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 11:
			Crypto_Table[0]='1';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 12:
			Crypto_Table[0]='2';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 13:
			Crypto_Table[0]='2';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='1';
			break;
		case 14:
			Crypto_Table[0]='2';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 15:
			Crypto_Table[0]='2';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 16:
			Crypto_Table[0]='2';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='1';
			break;
		case 17:
			Crypto_Table[0]='2';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 18:
                    Crypto_Table[0]='0';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 19:
			Crypto_Table[0]='1';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='1';
			break;
		case 20:
			Crypto_Table[0]='1';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='0';
			break;
		case 22:
			Crypto_Table[0]='0';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='2';
			break;
		case 23:
			Crypto_Table[0]='0';Crypto_Table[1]=' ';Crypto_Table[2]='0';Crypto_Table[3]=' ';Crypto_Table[4]='1';
			break;
		case 24:
			Crypto_Table[0]='0';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='1';
			break;
		case 25:
			Crypto_Table[0]='M';Crypto_Table[1]=' ';Crypto_Table[2]='A';Crypto_Table[3]=' ';Crypto_Table[4]='X';
			break;
		default:
			break;
	}
    #else
    Crypto_Table[0]='3';Crypto_Table[1]=' ';Crypto_Table[2]='1';Crypto_Table[3]=' ';Crypto_Table[4]='1';
    #endif
}
#endif
U8 WiFi_Command_check()
{
	U8 *WifBuffer;
	U16 i, jj, k, n, cmd, cnt;
	cmd = 0;
	cnt = 0;

	for(i=0;i< W_MAX + 1;i++)
	{	
		//UIprintf("\r\n ===>[[[  i = %d    %s  %s ]]]", i, &UART_WiFi_tabl[i][0], RxBuffer);
		wifi_table_init(i, &WifBuffer[0]);
		for(jj=0;jj<15;jj++)
		{
			if(Wifi_Table[jj] == rcv_packet[jj] && rcv_packet[jj] != ' ')
			{
				if(jj == 0)
				cnt = 0;
				else
				cnt++;
				
				cmd=i;
			}
		}

		if( (cnt == 6 &&( i==3 || i==12)) || (cnt == 7 && ( i == 8 || i==13)) || (cnt == 8 && ( i == 17)) || (cnt == 9 &&( i==2 || i==6) ) || (cnt == 10 &&( i==15 || i==16) ) || 
		   (cnt == 11 && (i == 0 || i==1 || i==5 || i==7 || i == 14 || i == 18)) || (cnt == 12 && ( i == 10 ||  i == 11)) || (cnt == 13 && ( i == 9)))
		{
			cnt = 0;

			if(i == 5)
			{
			      for(n=0; n < 15; n++) WiFi_MyIP_buff[n]=0;
                  
				for(k=0; k < 15; k ++)
				{
					if(rcv_packet[13 + k] == ' ') return cmd;
                    
					WiFi_MyIP_buff[k] = rcv_packet[13 + k];
				}
			}
			
			if(i == 14)
			{
				for(k=0; k < 8; k ++)
				{
					temp_wifi_rssi_level[k] = rcv_packet[31 + k];
				}
			}

			if(i == 18)
			{
			      for(n=0; n < 15; n++) DNSSVR_IP[n]=0;
                  
				for(k=0; k < 15; k ++)
				{
					if(rcv_packet[13 + k] == ' ') return cmd;
                    
					DNSSVR_IP[k] = rcv_packet[13 + k];
				}
			}
            
			 return cmd;
		}
		else
		{
			cmd = W_MAX; // MAX
			cnt = 0;
		}

	}

	//UIprintf("\r\n ===>[[[    %d ]]]", cmd);

	return cmd;
}

// Sel = 0 : WiFi_MyIP_buff
// Sel = 1 : DNSSVR_IP
void MyIP_Set(U8 Sel)
{
	int i, j, k, m, mm, nn;
	U8 Temp[4];
	U8 MyIPAll[15];
	U8 RESIP[15];

	if(Sel == 0)
	memcpy(RESIP, WiFi_MyIP_buff, 15);
	else
	memcpy(RESIP, DNSSVR_IP, 15);
	
	for(k = 0; k < 15; k++)
	{
		mm = (k+1) % 4;
		if(!mm)
		{
			MyIPAll[k] = '.';
		}
		else
		{
			MyIPAll[k] = '0';
		}
	}

	m = 0; nn = 0; j = 0;

	for(i = 0; i < 16; i++)
	{
		if(RESIP[i] != '.' && (i != 15) && (RESIP[i] != NULL))
		{
			Temp[j++] = RESIP[i];
			m++;
		}
		else
		{
			if(nn == 0)
			{
				if(m == 1)
				MyIPAll[2] = Temp[0];
				else if(m == 2)
				{
					MyIPAll[1] = Temp[0];
					MyIPAll[2] = Temp[1];
				}
				else if(m == 3)
				{
					MyIPAll[0] = Temp[0];
					MyIPAll[1] = Temp[1];
					MyIPAll[2] = Temp[2];
				}
			}
			else if(nn == 1)
			{
				if(m == 1)
				MyIPAll[6] = Temp[0];
				else if(m == 2)
				{
					MyIPAll[5] = Temp[0];
					MyIPAll[6] = Temp[1];
				}
				else if(m == 3)
				{
					MyIPAll[4] = Temp[0];
					MyIPAll[5] = Temp[1];
					MyIPAll[6] = Temp[2];
				}
			}
			else if(nn == 2)
			{
				if(m == 1)
				MyIPAll[10] = Temp[0];
				else if(m == 2)
				{
					MyIPAll[9] = Temp[0];
					MyIPAll[10] = Temp[1];
				}
				else if(m == 3)
				{
					MyIPAll[8] = Temp[0];
					MyIPAll[9] = Temp[1];
					MyIPAll[10] = Temp[2];
				}
			}
			else if(nn == 3)
			{
				if(m == 1)
				MyIPAll[14] = Temp[0];
				else if(m == 2)
				{
					MyIPAll[13] = Temp[0];
					MyIPAll[14] = Temp[1];
				}
				else if(m == 3)
				{
					MyIPAll[12] = Temp[0];
					MyIPAll[13] = Temp[1];
					MyIPAll[14] = Temp[2];
				}

				if(Sel == 0)
				for(i = 0; i < 15; i++) WiFi_MyIP_buff[i] = MyIPAll[i];
				else
				for(i = 0; i < 15; i++) DNSSVR_IP[i] = MyIPAll[i];

				return;
			}
			j = 0;
			m = 0;
			nn++;
		}
	}
}

void WiFi_Associated_Err(U16 err_cnt)
{
    Crypto_type(err_cnt);
    WiFi_Set_Cmd("CRYPTO=", Crypto_Table);
}

///#define SVRIP "0 182.163.234.171 4700"
#define DNSQUERY  "www.mywatt.jp"
#define SVRIP  "0 182.163.201.082 4700"

U8 WiFi_Connect(void)
{
    U8 send_cmd=W_MAX;
    U8 Cmd[5];
    U8 * crytodata;
    U16  i, k, m, n, rssi;
	
    send_cmd = WiFi_Command_check();

	switch(send_cmd)
	{
		case W_IPCONFIG:
			current_state = send_cmd;
                    ///timerSecTimerSet(hWiFi_Send_Event_Timer, 7);
			WiFi_Set_Cmd("IPCONFIG=", "1");
			//LCD_Display_WiFi_RSSI_Level(0, 0);
			break;
		case W_CRYPTO:
			current_state = send_cmd;
                    timerSecTimerSet(hWiFi_Send_Event_Timer, 7);
			if(event1.find_crypto == 0)
			WiFi_Set_Cmd("CRYPTO=", Crypto_Table);
			else
			WiFi_Associated_Err(event1.find_crypto);
			//LCD_Display_WiFi_RSSI_Level(4, LCD_INVERSE_DISPLAY_bit);
			break;
		case W_PSK:
			current_state = send_cmd;
			timerSecTimerSet(hWiFi_Send_Event_Timer, 5);
			IDPW_mode_tx_set();
			WiFi_Set_Cmd("PSK=", event1.wifi_pwd);//"123123123");///
			//LCD_Display_WiFi_RSSI_Level(0, 0);
			break;
		case W_ASSOCIATE:
			current_state = send_cmd;
			WiFi_Set_Cmd("ASSOCIATE=", event1.wifi_id); //"HJH");//
			timerSecTimerSet(hWiFi_Send_Event_Timer, 10);
			//LCD_Display_WiFi_RSSI_Level(0, 0);
			///Finish_wifi_setting = 1;
			break;
		case W_ASSOCIATED:
			current_state = send_cmd;
                    timerSecTimerSet(hWiFi_Send_Event_Timer, 10);
			memset(WiFi_MyIP_buff,NULL,sizeof(WiFi_MyIP_buff));
			//LCD_Display_WiFi_RSSI_Level(2, 0);
			ipallocation_cnt = 0;
			break;
		case W_IPALLOCATED:
			current_state = send_cmd;
			timerSecTimerSet(hWiFi_Send_Event_Timer, 10);
			MyIP_Set(0);
			WiFi_Set_Cmd("SOCKET=", "1");
			break;
		case W_SOCKET:
#if 1
			current_state = send_cmd;
			timerSecTimerSet(hWiFi_Send_Event_Timer, 10);
			WiFi_Set_Cmd("CONNECT=", SVRIP);
			///Finish_wifi_setting = 1;
			//LCD_Display_WiFi_RSSI_Level(2, 0);
#else
			current_state = send_cmd;
			timerSecTimerSet(hWiFi_Send_Event_Timer, 10);
			WiFi_Set_Cmd("DNSQUERY=", DNSQUERY);
#endif
			break;
			
		case W_DNSRESPONSE:
			current_state = send_cmd;
			timerSecTimerSet(hWiFi_Send_Event_Timer, 10);
			MyIP_Set(1);
			WiFi_Set_Cmd("CONNECT=", SVRIP);///DNSSVR_IP);
			break;
		case W_CONNECTED:
			current_state = send_cmd;
			//Finish_wifi_setting = 1;
			timerSecTimerSet(hWiFi_Send_Event_Timer, 20);
			//LCD_Display_WiFi_RSSI_Level(3, 0);
			break;
		case W_SENDOK:
			current_state = send_cmd;
			Finish_wifi_setting = 1;
            
			if(event1.find_crypto == 0)
			{
        			event1.find_crypto = err_associate_cnt;
        			System_Parameter_Init(SYSTEM_SETING);
			}
            
                    ///if(wifi_data_send_ok == 1)
                    ///WiFi_Set_Cmd("NWSTATUS=", "?");
			break;
		case W_CRYPTO_ERR:
                  Finish_wifi_setting = 0;
		case W_ASSOCIATED_ERR2:
		case W_ASSOCIATED_ERR:
                    if(event1.find_crypto == 0)
                    WiFi_Associated_Err(err_associate_cnt++);
                    else
                    WiFi_Associated_Err(event1.find_crypto);
                    
                    current_state = send_cmd;
                    fix_associate_cnt = 0;
			timerSecTimerSet(hWiFi_Send_Event_Timer, 5);
                    WiFi_Port_init(0);
                    WiFi_Port_init(1);
                        
                   //LCD_Display_WiFi_RSSI_Level(1, 0);
                   //LCD_Display_WiFi_RSSI_Level(4, LCD_INVERSE_DISPLAY_bit);
			break;
             ///case W_CLOSED:
             case W_SEND_ERR2:
		case W_TIMEOUT: 
             case W_IPRELEASED:
                    current_state = send_cmd;
                    fix_associate_cnt = 0;
                    WiFi_Port_init(0);
                    WiFi_Port_init(1);
                    //LCD_Display_WiFi_RSSI_Level(2, 0);
                    //LCD_Display_WiFi_RSSI_Level(4, LCD_INVERSE_DISPLAY_bit);
                    timerSecTimerSet(hWiFi_Send_Event_Timer, 60 * 1);
                   break;
            case W_POWERSAVE: 
			current_state = send_cmd;
			WiFi_Set_Cmd("hwps:5");
            case W_NWSTATUS:
                    k = 0; m = 0; n = 0;

                    wifi_rssi_level[0] = '0';
                    wifi_rssi_level[1] = '0';
                    wifi_rssi_level[2] = '0';
                    wifi_rssi_level[3] = '0';
                    
                    for(i = 0; i < 8; i++)
                    {
                        if(k == 1)
                        {
                            if(temp_wifi_rssi_level[i] == ' ') break;
                            wifi_rssi_level[m++] = temp_wifi_rssi_level[i];
                        }

                        if(temp_wifi_rssi_level[i] == ' ')
                        {
                            n++;
                        }
                        
                        ///if(temp_wifi_rssi_level[i] == '-' || n == 2)
                        if(n == 2)
                        {
                            k = 1;
                        }
                    }

                    if(wifi_rssi_level[0] == '0')
                    {
                        rssi = 0;//((wifi_rssi_level[0] - '0') * 100) + ((wifi_rssi_level[1] - '0') * 10) + (wifi_rssi_level[2] - '0');
                    }
                    else if(wifi_rssi_level[1] == '0')
                    {
                        rssi = (wifi_rssi_level[0] - '0');/// + ((wifi_rssi_level[1] - '0') * 10) + (wifi_rssi_level[2] - '0');
                    }
                    else if(wifi_rssi_level[2] == '0')
                    {
                        rssi = ((wifi_rssi_level[0] - '0') * 10) + (wifi_rssi_level[1] - '0');/// + (wifi_rssi_level[2] - '0');
                    }
                    else if(wifi_rssi_level[3] == '0')
                    {
                        rssi = ((wifi_rssi_level[0] - '0') * 100) + ((wifi_rssi_level[1] - '0') * 10) + (wifi_rssi_level[2] - '0');
                    }
                    else
                        rssi = 0;
                    #if 0
                    if(rssi <= 65)
                        LCD_Display_WiFi_RSSI_Level(3, 0);
                    else if(rssi <= 70)
                        LCD_Display_WiFi_RSSI_Level(2, 0);
                    else if(rssi <= 75)
                        LCD_Display_WiFi_RSSI_Level(1, 0);
                    else if(rssi <= 80)
                    {
                        LCD_Display_WiFi_RSSI_Level(1, 0);
                        LCD_Display_WiFi_RSSI_Level(4, LCD_INVERSE_DISPLAY_bit);
                    }
                    else
                        LCD_Display_WiFi_RSSI_Level(0, 0);
                    #endif
                break;
		default :
			break;
	}
	return send_cmd;
}

void wifi_callback(void)
{
    if((current_state < 4 || current_state == 11 ) && Finish_wifi_setting == 0)
    {
        //LCD_Display_WiFi_RSSI_Level(4, LCD_INVERSE_DISPLAY_bit);
        if(event1.find_crypto == 0)
        {
            if(err_associate_cnt < 26)
            {
                WiFi_Associated_Err(err_associate_cnt);
                timerSecTimerSet(hWiFi_Send_Event_Timer, 3);
                err_associate_cnt++;
            }
            else
            {
                err_associate_cnt = 0;
                //WiFi_Associated_Err(err_associate_cnt);
                timerSecTimerSet(hWiFi_Send_Event_Timer, 7);
            }
        }
        else
            timerSecTimerSet(hWiFi_Send_Event_Timer, 7);
            
    }
    else if(Finish_wifi_setting == 1)// && fix_associate_cnt == 1)
    {
        if(iprq_send < 2)
        {
            //LCD_Display_WiFi_RSSI_Level(4, LCD_INVERSE_DISPLAY_bit);
            IPRQ_mode_req_set();
        }
        else
        {
            wifi_data_send_ok = 1;
            //LCD_Display_WiFi_RSSI_Level(4, LCD_NORMAL_DISPLAY_bit);
            MS06_mode_req_set();
        }
        timerSecTimerSet(hWiFi_Send_Event_Timer, 60);
    }
    else if(current_state >= 4)
    {
        iprq_send = 0;
        //LCD_Display_WiFi_RSSI_Level(4, LCD_INVERSE_DISPLAY_bit);
        if(event1.find_crypto == 0)
        WiFi_Associated_Err(err_associate_cnt);
        else
        WiFi_Associated_Err(event1.find_crypto);
        timerSecTimerSet(hWiFi_Send_Event_Timer, 7);
    }
}

void sci_packet_analysis(void)
{
	U8 ch, i, k, mode, checksum;

	///WiFi_Connect(&wifi_test[0][0]);

	while(get_rx_queue(&Qa, &ch))
	{
		if(ch == SCI_ACK)
			mh01.ack_flag = 1;
		else if(ch == SCI_NAK)
			mh01.nak_flag = 1;

		switch(ch)
		{
			case START_CHAR:
				memset((void *)&ps, 0, sizeof(ps));
				memset(rcv_packet, 0, sizeof(rcv_packet));
				ps.mode = START_CHAR;
				ps.buff[ps.idx++] = ch;
				continue;

			case END_CHAR:
				ps.mode = END_CHAR;
				break;
				
			case WIFI_USER_UART_STX://START_CHAR:
			    if(Is_WiFi_Set() == 0) break;
                
				memset((void *)&ps, 0, sizeof(ps));
				memset(rcv_packet, 0, sizeof(rcv_packet));
				ps.mode = WIFI_USER_UART_STX;
				ps.buff[ps.idx++] = ch;
				break;

			case WIFI_USER_UART_ETX://END_CHAR:
			    if(Is_WiFi_Set() == 0) break;
                
				ps.mode = WIFI_USER_UART_ETX;
				break;

			default:
				break;
		}

		switch(ps.mode)
		{
			case START_CHAR:
				if(ps.idx < RX_BUFF_SIZE-2)
					ps.buff[ps.idx++] = ch;
				break;

			case END_CHAR:
				if(ps.idx < 8)break;

				// check sum 확인
				checksum = 0;
				for(i = 0; i < ps.idx-2; i++)
					checksum ^= ps.buff[i];

				ps.checksum[0] = (checksum >> 4)    + 0x30;
				ps.checksum[1] = (checksum &  0x0f) + 0x30;

#if CHECK_SUM_USE
				if( ps.checksum[0] == ps.buff[ps.idx-2] &&
					ps.checksum[1] == ps.buff[ps.idx-1])
				{
					sci_mode_operation(ps.buff);
					ps.mode = 0;
				}else
				{
					NAK_SEND();
				}
#else
				for(i = 0; i < ps.idx; i++)
				{
					if(ps.buff[i] == 0x02)
					{
						k = 0;
						rcv_packet[k++] = ps.buff[i];
					}
					else
					{
						rcv_packet[k++] = ps.buff[i];
					}
				}
				sci_mode_operation(&rcv_packet[0]);///(ps.buff);
				ps.mode = 0;
#endif
				break;

			case WIFI_USER_UART_STX://START_CHAR:
			    if(Is_WiFi_Set == 0) break;
                
				if(ps.idx < RX_BUFF_SIZE-2)
					ps.buff[ps.idx++] = ch;
				break;

			case WIFI_USER_UART_ETX://END_CHAR:
			    if(Is_WiFi_Set == 0) break;
                
				if(ps.idx < 8)break;

				// check sum E￠cAI
				checksum = 0;
				for(i = 0; i < ps.idx-2; i++)
					checksum ^= ps.buff[i];

				ps.checksum[0] = (checksum >> 4)    + 0x30;
				ps.checksum[1] = (checksum &  0x0f) + 0x30;

#if CHECK_SUM_USE
					
				if( ps.checksum[0] == ps.buff[ps.idx-2] && ps.checksum[1] == ps.buff[ps.idx-1])
				{
					sci_mode_operation(&ps.buff[5]);
					ps.mode = 0;
				}
				else
				{
					NAK_SEND();
				}
#else
				//if(mode != W_MAX)
				for(i=0; i < 100;i++)
				{
					/*if(ps.buff[1+i] != ' ') */rcv_packet[i] = ps.buff[i+1];
					///else break;
				}
				mode=WiFi_Connect();
				///mode=WiFi_Connect(&wifi_test[0][0]);
				//else
				//sci_mode_operation(ps.buff);
				ps.mode = 0;
#endif
				break;
			default :	break;
		}
	}

	if(mh01.flag)
		MH01_operation();

	 if(timeSecTimeExpired(hWiFi_Send_Event_Timer) == TIMER_EXPIRED)
	 {
            timerSecTimerSet(hWiFi_Send_Event_Timer1, 180);
            wifi_callback();
	 }

     if(timeSecTimeExpired(hWiFi_Send_Event_Timer1) == TIMER_EXPIRED)
	 {
	    timerSecTimerSet(hWiFi_Send_Event_Timer1, 180);
            wifi_callback();
        }
     
}
#if 0
U8 read_idpwd_length(void)
{
    idpwd_length = 0;
    pwd_length = 0;

    sflash_fast_read(WIFI_IDPWD_ADDRESS, content_buff, 4);

    idpwd_length = ((content_buff[0] - '0') * 10) + (content_buff[1] - '0');
    pwd_length = ((content_buff[2] - '0') * 10) + (content_buff[3] - '0');

    if(pwd_length == 229) return 0;
    else return 1;
}
#endif
void sci_mode_operation(char *packet)
{
	unsigned char mode = MODE_END;
	unsigned char *p;
	unsigned char i=0;
	
	//U32 deviation1;              // cmlee
	
	CPACKET_HEAD *ph;

	ph = (CPACKET_HEAD *)packet;

	for(mode = 0; mode < MODE_END; mode++)
	{
		if( ph->mode[0] == sci_mode_str[mode][0] && \
			ph->mode[1] == sci_mode_str[mode][1] && \
			ph->mode[2] == sci_mode_str[mode][2] && \
			ph->mode[3] == sci_mode_str[mode][3])
			break;
	}

	switch(mode)
	{
		case APID:
			
			ACK_SEND();
			event1.find_crypto=0;
			event1.wifi_id_size = ((packet[PACKET_DATA_POS] - '0') * 10) + (packet[PACKET_DATA_POS+1] - '0');
			event1.wifi_pwd_size = ((packet[PACKET_DATA_POS+2] - '0') * 10) + (packet[PACKET_DATA_POS+3] - '0');

			memset(event1.wifi_id, 0, 30);
			memset(event1.wifi_pwd, 0, 30);
            
			memcpy(event1.wifi_id, &packet[PACKET_DATA_POS + 4], event1.wifi_id_size);
			memcpy(event1.wifi_pwd, &packet[PACKET_DATA_POS + 4 + event1.wifi_id_size], event1.wifi_pwd_size);
			
			///event1.set_apidpwd = 1;
			System_Parameter_Init(SYSTEM_SETING);
			break;

		case IDPW:
			int_to_str(event1.wifi_id_size, content_buff, 2);
			int_to_str(event1.wifi_pwd_size, &content_buff[2], 2);
			for(i = 4; i < event1.wifi_id_size + 4; i++) content_buff[i] = event1.wifi_id[i-4];
			for(i = 4 + event1.wifi_id_size; i < event1.wifi_id_size + event1.wifi_pwd_size + 4; i++) content_buff[i] = event1.wifi_pwd[i - event1.wifi_id_size - 4];

			sci_put_packet(IDPW, content_buff, 4 + event1.wifi_id_size + event1.wifi_pwd_size);
			break;
			
		case IPRQ:
			// 장비번호: "000000"
		//	ACK_SEND();
#if 0		//  cmlee when manufacturing 1 	
			//TEST START(양산시삭제 TEST END부분까지)
			//장비번호
			content_buff[i++]='0';	content_buff[i++]='0'; content_buff[i++]='0';	content_buff[i++]='0';	content_buff[i++]='2';	content_buff[i++]='8';
			//MAC
			content_buff[i++]='1';content_buff[i++]='0';content_buff[i++]='.';
			content_buff[i++]='1';content_buff[i++]='2';content_buff[i++]='.';
			content_buff[i++]='1';content_buff[i++]='8';content_buff[i++]='.';
			content_buff[i++]='0';content_buff[i++]='0';content_buff[i++]='.';
			content_buff[i++]='0';content_buff[i++]='0';content_buff[i++]='.';
			content_buff[i++]='3';content_buff[i++]='C';
			#if 1
			//서버IP
			content_buff[i++]='1';content_buff[i++]='8';content_buff[i++]='2';content_buff[i++]='.';
			content_buff[i++]='1';content_buff[i++]='6';content_buff[i++]='3';content_buff[i++]='.';
			content_buff[i++]='2';content_buff[i++]='3';content_buff[i++]='4';content_buff[i++]='.';
			content_buff[i++]='1';content_buff[i++]='7';content_buff[i++]='1';
			#else
			content_buff[i++]='1';content_buff[i++]='9';content_buff[i++]='2';content_buff[i++]='.';
			content_buff[i++]='1';content_buff[i++]='6';content_buff[i++]='8';content_buff[i++]='.';
			content_buff[i++]='0';content_buff[i++]='0';content_buff[i++]='0';content_buff[i++]='.';
			content_buff[i++]='0';content_buff[i++]='0';content_buff[i++]='2';
			#endif
			//My IP
			content_buff[i++]='0';content_buff[i++]='1';content_buff[i++]='0';content_buff[i++]='.';
			content_buff[i++]='0';content_buff[i++]='0';content_buff[i++]='1';content_buff[i++]='.';
			content_buff[i++]='0';content_buff[i++]='0';content_buff[i++]='1';content_buff[i++]='.';
			content_buff[i++]='0';content_buff[i++]='9';content_buff[i++]='6';
			
			content_buff[i++]='4';content_buff[i++]='7';content_buff[i++]='0';content_buff[i++]='0';
			
			//TEST END--------------------------------------------------------
//#else 			
			
#endif 	
			if(Read_GPIO1())
			{                         
				iprq_send++;
				fix_associate_cnt = 1;
				
				for(i = 0; i < 15; i++) event1.device_info[38 + i] = WiFi_MyIP_buff[i];
				
				wifi_put_packet(IPRQ, event1.device_info, 57);
				timerSecTimerSet(hWiFi_Send_Event_Timer, 20);
				System_Parameter_Init(SYSTEM_SETING);
			}
			else
			{
				sci_put_packet(IPRQ, event1.device_info, 57);
			}
			break;

		case IPST:
			// 장비번호: "000000"				(6)
			// mac     : "00.00,00,00,00,00"	(17)
			// 서버IP  : "000,000,000,000"		(15)
			// MYIP    : "000,000,000,000"		(15)
			// PORT    : "0000"					(4)  TOTAL : 57
			ACK_SEND();
			memset(event1.device_info, 0, 90);
			event1.device_info_size = 57;
			memcpy(&event1.device_info[0], &packet[PACKET_DATA_POS], event1.device_info_size);
			System_Parameter_Init(SYSTEM_SETING);
			break;

		case TMST:
			// 년도(4) + 월(2) + 일(2) + 시(2) + 일(2)
			//sci_put_packet(TMST, &packet[PACKET_DATA_POS], 12); 
			TMST_operation(&packet[PACKET_DATA_POS]);

			ACK_SEND();
			break;

		case MS06:
			// 장비번호: "000000" 
			#if 0
			///085MS0623201506011621000000000000000000000000000000000000000000000000000000000000
			///WiFi_Set_Cmd("SEND=", "0 182.163.234.171 4700 85 085MS062720150601162500000000000000000000000000000000000000000000000000000000000024");
			#else
			ACK_SEND();
			MS06_operation();
			#endif
			///IPRQ_mode_req_set();
			break;

		case MH01:
			// 년도(4) + 월(2) + 일(2) + 시(2) + 전송옵션 
			// 전송 옵션 '0': 1건 전송, '1':현재시점 까지.
			p = &packet[PACKET_DATA_POS];

			//mh01.year   = (p[0] - '0') * 1000;
			//mh01.year  += (p[1] - '0') * 100;
			
			mh01.year  = (p[2] - '0') * 10;
			mh01.year  += (p[3] - '0');

			mh01.month  = (p[4] - '0') * 10;
			mh01.month += (p[5] - '0');

			mh01.day    = (p[6] - '0') * 10;
			mh01.day   += (p[7] - '0');

			mh01.hour   = (p[8] - '0') * 10;
			mh01.hour  += (p[9] - '0');

			if(p[10] >= '0' && p[10] <= '3')
			{
				mh01.option = p[10] - '0';

				mh01.flag = 1;
				mh01.step = STEP1;

				ACK_SEND();

				// 전송 중지
				if(mh01.option == 3)
				{
					mh01.flag = 0;
					mh01.step = STEP0;
				}
			}
			break;

		case SWCL:
					SW_Version_Response(&packet[PACKET_DATA_POS]); 
					ACK_SEND();
					break;
		#if 1 // cmlee inserted for frequency Setting
		case FRQ0 :          // frequency Setting
					ACK_SEND();
			                                                     // 77
					break;
		case FRQ1 :          // deviation Setting
					#if 1 // cmlee  must process 
					ACK_SEND();
					#endif
					break;
		case FRQ2:         // data rate   setting 
					ACK_SEND();
					break;
		case FRQ3: 	
			// power level 0~7
					ACK_SEND();
					break;
		case FRQ4:   break;
		case CTMP:  
					compensate_temperature(&packet[PACKET_DATA_POS]);  
					ACK_SEND();	
					break;
#if 0					
		case PLMT:  
					Set_power_consumption(&packet[PACKET_DATA_POS]);  
					ACK_SEND();	
					break;
#endif					
		case  ST01: 
					//Set_Month_target(&packet[PACKET_DATA_POS]);
					ACK_SEND();	
					break;
		case ST02: 
					//Set_Peak_time_day(&packet[PACKET_DATA_POS]);
					ACK_SEND();	
					break;
		case ST03:   
					//Set_Moment_Peak_power(&packet[PACKET_DATA_POS]);
					ACK_SEND();
					
					break;

		case ST04: 
					ACK_SEND();
					break;

		case ST05: 
					ACK_SEND();
					break;
		case ST06: 
					ACK_SEND();
					break;
		case ST07: 
					ACK_SEND();
					break;
		case  ST08: 
			             control_on_off(&packet[PACKET_DATA_POS]);
					//sci_put_packet(ST08, content_buff, 57);   // testing
					ACK_SEND();
					break;
		#if 0
		case  ST09: 
			             Set_Voltage(&packet[PACKET_DATA_POS]);
					//sci_put_packet(ST08, content_buff, 57);   // testing
					ACK_SEND();
					break;
		case  ST10: 
			             Set_CO2(&packet[PACKET_DATA_POS]);
					//sci_put_packet(ST08, content_buff, 57);   // testing
					ACK_SEND();
					break;
		case  ST11: 
			             Set_Nation(&packet[PACKET_DATA_POS]);
					//sci_put_packet(ST08, content_buff, 57);   // testing
					ACK_SEND();
					break;
		
		case ST21: 
			            Set_Modulation(&packet[PACKET_DATA_POS]); 
					ACK_SEND();
					break;
		case ST22:  Set_OSC(&packet[PACKET_DATA_POS]);
					ACK_SEND();
					break;
		//case ST23:  SW_Version_Response(&packet[PACKET_DATA_POS]); 
		//			ACK_SEND();
					break;
		#endif
	      #endif
		default: 		break;
	}
}


///WiFi_Set_Cmd("SEND=", "0 182.163.234.171 4700 68 068IPRQ00002810.12.18.00.00.3C182.163.234.171010.001.001.05947007F");
///WiFi_Set_Cmd("SEND=", "0 182.163.234.171 4700 86 086MS0627020150601162500000000000000000000000000000000000000000000000000000000000024");
/// #define SVRIP  "0 182.163.201.082 4700"

void wifi_put_packet(unsigned char mode, unsigned char *p, unsigned int len)
{
	unsigned int  tlen;
	unsigned char i, k;
	unsigned int checksum = 0;

	k = 0;

	if(mode >= MODE_END)return;

	memset(wifi_buff, NULL, sizeof(wifi_buff));

	//tlen = len + 11;
	tlen = PACKET_LENGTH(len);

	wifi_buff[k++] = '0';
	wifi_buff[k++] = ' ';
	wifi_buff[k++] = '1';
	wifi_buff[k++] = '8';
	wifi_buff[k++] = '2';
	wifi_buff[k++] = '.';
	wifi_buff[k++] = '1';
	wifi_buff[k++] = '6';
	wifi_buff[k++] = '3';
	wifi_buff[k++] = '.';
#if 0
	wifi_buff[k++] = '2';
	wifi_buff[k++] = '3';
	wifi_buff[k++] = '4';
	wifi_buff[k++] = '.';
	wifi_buff[k++] = '1';
	wifi_buff[k++] = '7';
	wifi_buff[k++] = '1';
#else
	wifi_buff[k++] = '2';
	wifi_buff[k++] = '0';
	wifi_buff[k++] = '1';
	wifi_buff[k++] = '.';
	wifi_buff[k++] = '0';
	wifi_buff[k++] = '8';
	wifi_buff[k++] = '2';
#endif
	wifi_buff[k++] = ' ';
	wifi_buff[k++] = '4';
	wifi_buff[k++] = '7';
	wifi_buff[k++] = '0';
	wifi_buff[k++] = '0';
	wifi_buff[k++] = ' ';
	if(mode == 0)
	{
		wifi_buff[k++] = '6';
		wifi_buff[k++] = '8';
	}
	else
	{
		wifi_buff[k++] = '8';
		wifi_buff[k++] = '6';
	}
	wifi_buff[k++] = ' ';	
	
	wifi_buff[k++] = START_CHAR;

	// length
	wifi_buff[k++] = (tlen/100) + '0'; 
	tlen %= 100;
	wifi_buff[k++] = (tlen/10)  + '0';
	wifi_buff[k++] = (tlen%10)  + '0';

	// mode
	wifi_buff[k++] = sci_mode_str[mode][0];
	wifi_buff[k++] = sci_mode_str[mode][1];
	wifi_buff[k++] = sci_mode_str[mode][2];
	wifi_buff[k++] = sci_mode_str[mode][3];

	for(i = 0; i < len; i++)
		wifi_buff[k + i] = p[i];

	for(i = k-8; i < len + 8; i++)
		checksum ^= wifi_buff[i];

	wifi_buff[len+k] = (checksum>>4)  + '0';
	wifi_buff[len+k+1] = (checksum&0xf) + '0';
	wifi_buff[len+k+2] = END_CHAR;

	WiFi_Set_Cmd("SEND=", wifi_buff);

	//put_tx_queue(&Qa, wifi_buff, PACKET_LENGTH(len));
	//UART0_SEND();
}

void sci_put_packet(unsigned char mode, unsigned char *p, unsigned int len)
{
	unsigned int  tlen;
	unsigned char i, checksum = 0;
#if 1
	if(mode >= MODE_END)return;

	//tlen = len + 11;
	tlen = PACKET_LENGTH(len);
	
	packet_buff[0] = START_CHAR;

	// length
	packet_buff[1] = (tlen/100) + '0'; 
	tlen %= 100;
	packet_buff[2] = (tlen/10)  + '0';
	packet_buff[3] = (tlen%10)  + '0';

	// mode
	packet_buff[4] = sci_mode_str[mode][0];
	packet_buff[5] = sci_mode_str[mode][1];
	packet_buff[6] = sci_mode_str[mode][2];
	packet_buff[7] = sci_mode_str[mode][3];

	for(i = 0; i < len; i++)
		packet_buff[8 + i] = p[i];

	for(i = 0; i < (len+8); i++)
		checksum ^= packet_buff[i];

	packet_buff[len+8] = (checksum>>4)  + '0';
	packet_buff[len+9] = (checksum&0xf) + '0';
	packet_buff[len+10] = END_CHAR;

	put_tx_queue(&Qa, packet_buff, PACKET_LENGTH(len));
	UART0_SEND();
#endif
}


void TMST_operation(unsigned char *p)
{
	unsigned int  temp, i;
      unsigned char tmstdata[12];

///      flag_tmst_set = 1;

      for(i = 0; i < 12; i++) tmstdata[i] = p[i];
	
//--------- 추가  -------------------
	RTC_Timer_Stop();
//----------------------------------	
	// year
	temp  = (tmstdata[0] - '0') * 1000;
	temp += (tmstdata[1] - '0') * 100;
	temp += (tmstdata[2] - '0') * 10;
	temp += (tmstdata[3] - '0');
	event1.years = temp;

	// months
	temp  = (tmstdata[4] - '0') * 10;
	temp += (tmstdata[5] - '0');
	event1.output_month = temp;

	// days
	temp  = (tmstdata[6] - '0') * 10;
	temp += (tmstdata[7] - '0');
	event1.output_days = temp;

	// hours
	temp  = (tmstdata[8] - '0') * 10;
	temp += (tmstdata[9] - '0');
	event1.hours = temp;

	// minutes
	temp  = (tmstdata[10] - '0') * 10;
	temp += (tmstdata[11] - '0');
	event1.minutes = temp;


//	Default_Set_Time_And_Day();  // 시간 디폴트  설정 

	Compute_365_Total_Day(event1.years, event1.output_month, event1.output_days,  & event1.days );
	
	event1.State =ID_ONCE_INITIALIZE_WRITE;

	FLASH_Page_Erase(System_Parameter_BaseAddr, SCRATCHPAD_FLASH_MEMORY); 
	
	Buff_To_Flash_Memory(System_Parameter_BaseAddr, &event1.State, sizeof(event1) );

//--------- 추가  ------------------------
	MINUTE_INIT=(U8)event1.minutes;   
	HOUR_INIT=(U8)event1.hours;   
   	event1.seconds = SECOND_INIT = 0;  // 50

	RTC_SetTimer(TIME_INIT);
	RTC_Timer_Run();		
//--------------------------------------
}
#if 0
void FRQ0_operation(unsigned char *p)
{
	unsigned int  temp, temp1;
	unsigned char freq_no;     // cmlee 
	unsigned long f; 
	U8 test; 
	
	// frq 0 ~ 21
	temp  = (p[0] - '0') * 10;
	temp += (p[1] - '0') ;
	
	freq_no = temp;

	temp1 = (p[2]-'0');
	
	switch(freq_no) {
							case 1 : f=    424700000L ;      	break;
							case 2 : f=    424712500L  ;   	break;
							case 3 : f= 	424725000L	;	break;
							case 4 : f= 	424737500L	;	break;
							case 5 : f= 	424775000L	;	break;
							case 6 : f= 	424762500L	;	break;
							case 7 : f= 	424775000L	;	break;
							case 8 : f= 	424787500L	;	break;
							case 9 : f= 	424800000L	;	break;
							case 10 : f= 	424812500L	;	break;
							case 11 : f= 	424825000L	;	break;
							case 12 : f= 	424837500L	;	break;
							case 13 : f= 	424850000L	;	break;
							case 14 : f= 	424862500L	;	break;
							case 15 : f= 	424875000L;		break;
							case 16 : f= 	424887500L	;	break;
							case 17 : f= 	424900000L	;	break;
							case 18 : f= 	424912500L	;	break;
							case 19 : f= 	424925000L	;	break;
							case 20 : f= 	424937500L	;	break;
							case 21 : f= 	424950000L	;	break;
							default : break;
						}
		if(temp1==0) 	{	// 수신기 설정 		
					setTRxFrequency (f); 
					test=event1.frequency_no;
					event1.frequency_no=freq_no;
					System_Parameter_Init(SYSTEM_SETING);
		}
		else if(temp1==1){   // 송신기 설정 
		               // while(1){
					RF_Send_Command(RF_FREQ_SET_CMD, Display_Channel, (U16)freq_no);	
		              //  	}
			}
}

void FRQ1_operation(unsigned char *p)     // 2800
{
	unsigned int  temp, temp1;
	unsigned int deviation;     // cmlee 
		
	// frq 0 ~ 21
	temp  = (p[0] - '0') * 1000;
	temp += (p[1] - '0') * 100;
	temp  += (p[2] - '0') * 10;
	temp += (p[3] - '0') ;

	temp1= (p[4] - '0') ;
	deviation = temp;
	if(temp1==0) {
					setTxFrequencyDeviation (deviation); 
					event1.deviation=deviation;
					System_Parameter_Init(SYSTEM_SETING);
					}
	else if(temp1==1) {
					RF_Send_Command(DEVIATION_SET_CMD, Display_Channel, (U16)deviation);	
					}
}

void FRQ2_operation(unsigned char *p)     //
{
	unsigned int  temp, temp1;
	U32 dataRate ;     // cmlee 
		
	// frq 0 ~ 21
	temp  = (p[0] - '0') * 1000;
	temp += (p[1] - '0') * 100;
	temp  += (p[2] - '0') * 10;
	temp += (p[3] - '0') ;
	temp1= (p[4] - '0') ;
	dataRate = temp;
	if(temp1==0) {
				setTxDataRate (dataRate); 
				event1.data_rate=dataRate;
				System_Parameter_Init(SYSTEM_SETING);
				}
	else if(temp1==1) {
				RF_Send_Command(DATA_RATE_CMD, Display_Channel, (U16)dataRate);	
					}
}

void FRQ3_operation(unsigned char *p)    // 0~7 level
{
	unsigned int  temp, temp1;
	U8 power_level;     // cmlee 
		
	// frq 0 ~ 21
	
	temp = (p[0] - '0') ;
	temp1= (p[1] - '0') ;
	power_level = (U8) temp;
	if(temp1==0) {
			setTxPower (power_level);
			event1.power_level=power_level;
			System_Parameter_Init(SYSTEM_SETING);
		}
	else if(temp1==1) {
				RF_Send_Command(RF_POWER_LEVEL_CMD, Display_Channel, (U16)power_level);	
					}
}

void compensate_temperature(unsigned char *p)    // 0~7 level
{
	unsigned int   temp2;
	U8 temp, temp1;
	//U8 power_level;     // cmlee 
	#if 0	
	temp = (p[0] - '0') ;

	temp1  = (p[1] - '0') * 10;
	temp1 += (p[2] - '0') ;

	event1.plus_minus=temp;
	event1.compensate_temp= temp1;
	System_Parameter_Init(SYSTEM_SETING);
	#else
      temp = (p[0] - '0') ;   // request 0    set 1
      temp1 = (p[1] - '0') ;   // +/-
	temp2  = (p[2] - '0') * 10;
	temp2 += (p[3] - '0') ;

	if(temp ==0) 
		{
					int_to_str(event1.plus_minus, content_buff, 1);   
					int_to_str(event1.compensate_temp, content_buff+1, 2);       // from 0 
								                    //int_to_str(event1.years, &content_buff[2], 4);
				sci_put_packet(CTMP, content_buff, 20);     // length 는 재확인    
		
		}
	else
		{
		event1.plus_minus=temp1;
		event1.compensate_temp= temp2;
		System_Parameter_Init(SYSTEM_SETING);
		}
	#endif
}
#endif
#if 0
void Set_power_consumption(unsigned char *p)
{
	//unsigned int   temp2;
	U8 temp, temp1;

      temp = (p[0] - '0') ;   // request 0    set 1
      temp1 = (p[1] - '0'); 

	if(temp ==0) 
	{
		int_to_str(event1.Home_Building_Indu, content_buff, 1);   
		sci_put_packet(PLMT, content_buff, 2);
	}
	else
	{
		event1.Home_Building_Indu = event1.Limit_Pwr_Consumption=temp1;
		event1.fLimit_Pwr_Consumption=1;
		
		System_Parameter_Init(SYSTEM_SETING);
	}
}


void Set_Month_target(unsigned char *p)    // ST01
{
	unsigned long  temp;
	//U8 ch_id;     // cmlee 
		
	// frq 0 ~ 21
	
	temp  = (p[0] - '0') * 1000;
	temp += (p[1] - '0') * 100;
	temp  += (p[2] - '0') * 10;
	temp += (p[3] - '0') ;
	
	event1.alarm_month_target=temp;
	System_Parameter_Init(SYSTEM_SETING);
	}

void Set_Peak_time_day(unsigned char *p)    // ST02
{
	unsigned int  temp, temp1;
	U8 class_level;     // cmlee 
		
	// frq 0 ~ 21
	
	temp  = (p[0] - '0') ;
	class_level = temp;
					// start hour
	temp1  = (p[1] - '0') * 10;
	temp1 += (p[2] - '0');
	event1.alarm_hour[class_level] = temp1;
	temp1  = (p[3] - '0') * 10;
	temp1 += (p[4] - '0');
	event1.alarm_minute[class_level] =temp1;
	temp1 = (p[9] - '0');
	event1.alarm_which_peak[class_level] =temp1;
	
	System_Parameter_Init(SYSTEM_SETING);
	}
void Set_Moment_Peak_power(unsigned char *p)    // ST03
{
	unsigned long  temp;
	//U8 class_level;     // cmlee 
		
	// frq 0 ~ 21
	
	temp  = (p[0] - '0')  * 10000;
	temp  += (p[1] - '0') * 1000;
	temp += (p[2] - '0') * 100;
	temp  += (p[3] - '0') * 10;
	temp += (p[4] - '0');
	
	event1.alarm_moment_peak=temp;
	System_Parameter_Init(SYSTEM_SETING);
}

void Set_Night_Peak_power(unsigned char *p)    // ST03
{
	unsigned long  temp;
	//U8 class_level;     // cmlee 
		
	// frq 0 ~ 21
	
	temp  = (p[0] - '0')  * 10000;
	temp  += (p[1] - '0') * 1000;
	temp += (p[2] - '0') * 100;
	temp  += (p[3] - '0') * 10;
	temp += (p[4] - '0');
	
	event1.alarm_night_peak=temp;
	System_Parameter_Init(SYSTEM_SETING);
	}

void Set_6level_Billing_Base(unsigned char *p)   // ST05
{
	unsigned int  temp;
	//temp  = (p[0] - '0');
	temp = (p[1] - '0')*  10000;
	temp += (p[2] - '0') * 1000;
	temp += (p[3] - '0')*  100;
	temp += (p[4] - '0') * 10;
	temp += (p[5] - '0');
	Cost.Base_Cost[0]=temp;
	event1.Base_Cost[0]=temp;
	//temp  = (p[6] - '0');
	temp = (p[7] - '0')*  10000;
	temp += (p[8] - '0') * 1000;
	temp += (p[9] - '0')*  100;
	temp += (p[10] - '0') * 10;
	temp += (p[11] - '0');
	Cost.Base_Cost[1]=temp;
	event1.Base_Cost[1]=temp;
//temp  = (p[12] - '0');
	temp = (p[13] - '0')*  10000;
	temp += (p[14] - '0') * 1000;
	temp += (p[15] - '0')*  100;
	temp += (p[16] - '0') * 10;
	temp += (p[17] - '0');
	Cost.Base_Cost[2]=temp;
	event1.Base_Cost[2]=temp;
//temp  = (p[18] - '0');
	temp = (p[19] - '0')*  10000;
	temp += (p[20] - '0') * 1000;
	temp += (p[21] - '0')*  100;
	temp += (p[22] - '0') * 10;
	temp += (p[23] - '0');
	Cost.Base_Cost[3]=temp;
	event1.Base_Cost[3]=temp;
//temp  = (p[24] - '0');
	temp = (p[25] - '0')*  10000;
	temp += (p[26] - '0') * 1000;
	temp += (p[27] - '0')*  100;
	temp += (p[28] - '0') * 10;
	temp += (p[29] - '0');
	Cost.Base_Cost[4]=temp;
	event1.Base_Cost[4]=temp;
//temp  = (p[30] - '0');
	temp = (p[31] - '0')*  10000;
	temp += (p[32] - '0') * 1000;
	temp += (p[33] - '0')*  100;
	temp += (p[34] - '0') * 10;
	temp += (p[35] - '0');
	Cost.Base_Cost[5]=temp;	
	event1.Base_Cost[5]=temp;	
	System_Parameter_Init(SYSTEM_SETING);
	}
void Set_6level_Billing_Threshold(unsigned char *p)   // ST05
{
	unsigned int  temp;

	//temp  = (p[0] - '0');
	temp = (p[1] - '0')*  10000;
	temp += (p[2] - '0') * 1000;
	temp += (p[3] - '0')*  100;
	temp += (p[4] - '0') * 10;
	temp += (p[5] - '0');
	Cost.Treshold[0]=temp;
	event1.Treshold[0]=temp;
	//temp  = (p[6] - '0');
	temp = (p[7] - '0')*  10000;
	temp += (p[8] - '0') * 1000;
	temp += (p[9] - '0')*  100;
	temp += (p[10] - '0') * 10;
	temp += (p[11] - '0');
	Cost.Treshold[1]=temp;
	event1.Treshold[1]=temp;
//temp  = (p[12] - '0');
	temp = (p[13] - '0')*  10000;
	temp += (p[14] - '0') * 1000;
	temp += (p[15] - '0')*  100;
	temp += (p[16] - '0') * 10;
	temp += (p[17] - '0');
	Cost.Treshold[2]=temp;
	event1.Treshold[2]=temp;
//temp  = (p[18] - '0');
	temp = (p[19] - '0')*  10000;
	temp += (p[20] - '0') * 1000;
	temp += (p[21] - '0')*  100;
	temp += (p[22] - '0') * 10;
	temp += (p[23] - '0');
	Cost.Treshold[3]=temp;
	event1.Treshold[3]=temp;
//temp  = (p[24] - '0');
	temp = (p[25] - '0')*  10000;
	temp += (p[26] - '0') * 1000;
	temp += (p[27] - '0')*  100;
	temp += (p[28] - '0') * 10;
	temp += (p[29] - '0');
	Cost.Treshold[4]=temp;
	event1.Treshold[4]=temp;
//temp  = (p[30] - '0');
	temp = (p[31] - '0')*  10000;
	temp += (p[32] - '0') * 1000;
	temp += (p[33] - '0')*  100;
	temp += (p[34] - '0') * 10;
	temp += (p[35] - '0');
	Cost.Treshold[5]=temp;
	event1.Treshold[5]=temp;	
	System_Parameter_Init(SYSTEM_SETING);

	}
void Set_6level_Billing_Rate(unsigned char *p)   // ST05
{
	//unsigned int   base1;
	float temp1;
	temp1 = (p[0] - '0') * 1000;
	temp1 += (p[1] - '0')*  100;
	temp1 += (p[2] - '0') * 10;
	temp1 += (p[3] - '0');
	temp1 += (p[4] - '0') /10;    // floating point
		
	temp1 += (p[5] - '0') /100;
	Cost.Billing_Rate[0]=temp1;
	event1.Billing_Rate[0]=temp1;
	temp1 = (p[6] - '0') * 1000;
	temp1 += (p[7] - '0')*  100;
	temp1 += (p[8] - '0') * 10;
	temp1 += (p[9] - '0');
	temp1 += (p[10] - '0') / 10;    // floating point
		
	temp1 += (p[11] - '0') / 100;
	Cost.Billing_Rate[1]=temp1;
	event1.Billing_Rate[1]=temp1;
	temp1 = (p[12] - '0') * 1000;
	temp1 += (p[13] - '0')*  100;
	temp1 += (p[14] - '0') * 10;
	temp1 += (p[15] - '0');
	temp1 += (p[16] - '0') / 10;    // floating point
		
	temp1+= (p[17] - '0') / 100;
	Cost.Billing_Rate[2]=temp1;
	event1.Billing_Rate[2]=temp1;
	temp1 = (p[18] - '0') * 1000;
	temp1 += (p[19] - '0')*  100;
	temp1 += (p[20] - '0') * 10;
	temp1 += (p[21] - '0');
	temp1 += (p[22] - '0') /10;    // floating point
		
	temp1 += (p[23] - '0') /100;
	Cost.Billing_Rate[3]=temp1;
	event1.Billing_Rate[3]=temp1;
	temp1 = (p[24] - '0') * 1000;
	temp1 += (p[25] - '0')*  100;
	temp1 += (p[26] - '0') * 10;
	temp1 += (p[27] - '0');
	temp1 += (p[28] - '0') /10;    // floating point
		
	temp1 += (p[29] - '0') /100;
	Cost.Billing_Rate[4]=temp1;
	event1.Billing_Rate[4]=temp1;
	temp1 = (p[30] - '0') * 1000;
	temp1 += (p[31] - '0')*  100;
	temp1 += (p[32] - '0') * 10;
	temp1 += (p[33] - '0');
	temp1 += (p[34] - '0') /10;    // floating point
		
	temp1 += (p[35] - '0') /100;
	Cost.Billing_Rate[5]=temp1;
	event1.Billing_Rate[5]=temp1;
	System_Parameter_Init(SYSTEM_SETING);
	}

void control_on_off(unsigned char *p)    // ST08
{
	unsigned int  temp, temp1;
	U8 ch_id, i,j, upper_MAC, lower_MAC;     // cmlee 
	U16 MAC_class, temp_MAC; 	
	// frq 0 ~ 21
	
	temp = (p[0] - '0') * 10 ;
	temp+= (p[1] - '0') ;
	
	ch_id = (U8)temp;
	
	MAC_class=event1.Channel_ID[ch_id-1] ;
	upper_MAC=(U8) (MAC_class >> 8);
	lower_MAC=(U8) MAC_class; 

	temp_MAC=(U16) (lower_MAC << 8);
	
	
	temp1 = (p[2] - '0') ;     // 1 btye

	temp_MAC=(temp_MAC | (U16)temp1); 
	
	for (i=0; i<10; i++)     
		 {// 
			RF_Send_Command(CONTROL_ON_OFF_CMD, upper_MAC, temp_MAC);	 // on 0ff 제어 
			for(j=0;j<50;j++){};
		}
					
	}

void Set_Voltage(unsigned char *p)    // ST08
{
	unsigned int  temp, temp1;
	U8 ch_id;     // cmlee 
		
	// frq 0 ~ 21
	
	temp = (p[0] - '0') * 10 ;
	temp+= (p[1] - '0') ;
	
	ch_id = (U8) temp;

	temp1 = (p[2] - '0') *100;
	temp1 += (p[3] - '0') *10;
	temp1 += (p[4] - '0') ;

	
	//RF_Send_Command(CONTROL_ON_OFF_CMD, ch_id, (U16)temp1);	 // on 0ff 제어 
					
	}
#endif
#if 0
void Set_CO2(unsigned char *p)    // ST08
{
	float  temp, temp1,temp2,temp3;
	//float co2_value;
	//U8 ch_id;     // cmlee 
		
	// frq 0 ~ 21
	
	temp = (p[0] - '0') << 32;
	temp1= (p[1] - '0') << 16 ;
	temp2 = (p[2] - '0')<< 8;
	temp3 =(p[3] - '0');
	
	//co2_value = temp | temp1 | temp2 | temp3;
	
	//RF_Send_Command(CONTROL_ON_OFF_CMD, ch_id, (U16)temp1);	 // on 0ff 제어 
					
	}
#endif
#if 0
void Set_Nation(unsigned char *p)    // ST08
{
	unsigned int  temp, temp1;
	unsigned int nation_code, class_nation;	
	temp = (p[0] - '0') *10;
	temp += (p[1] - '0');
	nation_code = temp;
	temp1 = (p[2] - '0') *10;
	temp1 +=(p[3] - '0') ;
	class_nation = temp1; 
	
	}
#endif
#if 0
void Set_Modulation(unsigned char *p)  // ST21
{
	unsigned int  temp, temp1;
	unsigned int ch_id, i,j,MAC_class, temp_MAC;	
	U8 Terminal , modulation_on_off,status,  upper_MAC, lower_MAC;

	Terminal = (p[0] - '0');     // 1 : SEM3000      2: Transmitter    3: PIT   
	temp = (p[1] - '0') *10;
	temp += (p[2] - '0');

	ch_id = temp; 
	modulation_on_off =  (p[3] - '0');
	

	if(Terminal==1) 
		{ 
		if(modulation_on_off==1)    // 1 : 무변조        2: 변조 
			{
		  // Modulation get the code of certification , need not ch_id
		setTRxFrequency (424712500L);	
		  setTxPower(4);   
		setTxFrequencyDeviation(2800L);                  // Full power
		setTxDataRate (2400L);
		phyWrite(0x09, event1.osc_value);	    // cap value
		
 		phyWrite(EZRADIOPRO_MODULATION_MODE_CONTROL_2, 0x00);

		//#ifdef ENABLE_RF_SWITCH
   		// RF Switch enabled by default
   		phyWrite(EZRADIOPRO_GPIO1_CONFIGURATION, 0x12);
  		phyWrite(EZRADIOPRO_GPIO2_CONFIGURATION, 0x15);
		 // enable transmitter
   		phyWrite(EZRADIOPRO_OPERATING_AND_FUNCTION_CONTROL_1,(EZRADIOPRO_TXON|EZRADIOPRO_XTON));
		     // ONE_MS
	 	status = phyRead(EZRADIOPRO_DEVICE_STATUS);
			}
		else if(modulation_on_off==2)
			{
		setTRxFrequency (424712500L);	
		setTxPower(4);   
		setTxFrequencyDeviation(2800L);                  // Full power
		setTxDataRate (2400L);
		phyWrite(0x09, event1.osc_value);	    // cap value
		// Set modulation control register for FSK or GFSK spectrum.
   		// Read/Modify/write used to preserve txscale
   		status = phyRead(EZRADIOPRO_MODULATION_MODE_CONTROL_2);
   		status |= MODULATION_MODE;
  		//phyWrite(EZRADIOPRO_MODULATION_MODE_CONTROL_2, status);
   		phyWrite(EZRADIOPRO_MODULATION_MODE_CONTROL_2, 0x33);
		//#ifdef ENABLE_RF_SWITCH
   		// RF Switch enabled by default
  		phyWrite(EZRADIOPRO_GPIO1_CONFIGURATION, 0x12);
   		phyWrite(EZRADIOPRO_GPIO2_CONFIGURATION, 0x15);
		// enable transmitter
   		phyWrite(EZRADIOPRO_OPERATING_AND_FUNCTION_CONTROL_1,(EZRADIOPRO_TXON|EZRADIOPRO_XTON));
      		}
	}
      else   // send value to transmitter or PIT  designated ch_id
      	{
	
		MAC_class=event1.Channel_ID[ch_id-1] ;
		upper_MAC=(U8) (MAC_class >> 8);
		lower_MAC=(U8) MAC_class; 

		temp_MAC=(U16) (lower_MAC << 8);
		temp1 = modulation_on_off; 
		
		temp_MAC=(temp_MAC | (U16)temp1); 
	#if 1
	for (i=0; i<10; i++)     
		 {// 
			RF_Send_Command(CONTROL_MODULATION, upper_MAC, temp_MAC);	 // on 0ff 제어 
			for(j=0;j<50;j++){};
		}
	#endif
      	}
	//RF_Send_Command(CONTROL_ON_OFF_CMD, ch_id, (U16)temp1);	 // on 0ff 제어 
					
	}
#endif
#if 0
void Set_OSC(unsigned char *p)  // ST22
{
	unsigned int  temp;
	unsigned int ch_id, i,j, MAC_class, temp_MAC;	
	U8 Terminal , req_control, upper_MAC, lower_MAC, temp1;

	Terminal = (p[0] - '0');     // 1 : SEM3000      2: Transmitter    3: PIT   
	temp = (p[1] - '0') *10;
	temp += (p[2] - '0');

	ch_id = temp; 
	req_control =  (p[3] - '0');

	temp1 = (p[4] - '0') *100;
	temp1 += (p[5] - '0') *10;
	temp1 += (p[6] - '0');

	if(Terminal==1) 
		{ 
			if(req_control==1)    // 1: request        2: Control
				{    // 요청시 응답 
				#if 1
				int_to_str(event1.osc_value, content_buff, 3);       // from 0 
								                    //int_to_str(event1.years, &content_buff[2], 4);
				sci_put_packet(ST22, content_buff, 20);     // length 는 재확인    
				#endif
				}
			else if(req_control==2)   //  설정시 
				{
		  		// set osc value 
		  		//set  Crystal Oscillator Load Capacitance register
		  		 event1.osc_value=temp1;
		 		 phyWrite(0x09, temp1);	    // cap value
		 		 System_Parameter_Init(SYSTEM_SETING);
				}
		  
		}
      else   // send value to transmitter or PIT  designated ch_id
      	{
		MAC_class=event1.Channel_ID[ch_id-1] ;
		upper_MAC=(U8) (MAC_class >> 8);
		lower_MAC=(U8) MAC_class; 

		temp_MAC=(U16) (lower_MAC << 8);
		//temp1 = req_control; 
		if(req_control==1)     // 1 : request      2: set
			temp1 &= 0x7f;
		if(req_control==2)
			temp1 |= 0x80;
		temp_MAC=(temp_MAC | (U16)temp1); 
	#if 1
	for (i=0; i<10; i++)     
		 {// 
			RF_Send_Command(CONTROL_OSC, upper_MAC, temp_MAC);	 // on 0ff 제어 
			for(j=0;j<50;j++){};
		}
	#endif
	//RF_Send_Command(CONTROL_ON_OFF_CMD, ch_id, (U16)temp1);	
      	}
	
	}
#endif
void SW_Version_Response(unsigned char *p)  // ST23
{
	unsigned int  temp;
	U8 Request ;

	Request = (p[0] - '0');     // always SW version 
	temp=event1.SW_Version;
	int_to_str(event1.SW_Version, content_buff, 3);       // from 0 
	sci_put_packet(SWCL, content_buff, 15);     // length 는 재확인    
			
	}
void int_to_str(unsigned int x, unsigned char *p, char len)
{
	char idx = 0;

	if(len >= 5)
	{
		p[idx++] = (x / 10000) + '0';
		x %= 10000;
	}
	if(len >= 4)
	{
		p[idx++] = (x / 1000) + '0';
		x %= 1000;
	}
	if(len >= 3)
	{
		p[idx++] = (x / 100) + '0';
		x %= 100;
	}
	if(len >= 2)
	{
		p[idx++] = (x / 10) + '0';
		x %= 10;
	}
	p[idx++] = (x % 10) + '0';
	p[idx] = 0;
}

void long_to_str(unsigned long x, unsigned char *p, char len)
{
	char idx = 0;

	x %= 1000000UL;
	if(len >= 7)
	{
		p[idx++] = (x / 1000000UL) + '0';
		x %= 1000000UL;
	}
	if(len >= 6)
	{
		p[idx++] = (x / 100000UL) + '0';
		x %= 100000UL;
	}
	if(len >= 5)
	{
		p[idx++] = (x / 10000UL) + '0';
		x %= 10000UL;
	}
	if(len >= 4)
	{
		p[idx++] = (x / 1000UL) + '0';
		x %= 1000UL;
	}
	if(len >= 3)
	{
		p[idx++] = (x / 100UL) + '0';
		x %= 100UL;
	}
	if(len >= 2)
	{
		p[idx++] = (x / 10UL) + '0';
		x %= 10UL;
	}
	p[idx++] = (x % 10UL) + '0';
	p[idx] = 0;
}
int idata  Current_Temperature ; // imsi
#if 1
void MS06_operation(void)
{
    char i;
    unsigned long x;
    U16 Compare_temp;
    
    content_buff[0] = '0'; content_buff[1] = '0'; content_buff[2] = '0';

	Current_Temperature=111;
    // 14 byte
    Compare_temp = Current_Temperature; //cmlee

    //event1.years = 2016;
    //event1.output_month = 4;
    //event1.output_days =23;
    //event1.hours = 15;
    //if(test_min > 59)event1.minutes = test_min=0;
    //else
    //event1.minutes = test_min++;
    
    int_to_str(Compare_temp, content_buff, 3);
    int_to_str(event1.years, &content_buff[3], 4);
    int_to_str(event1.output_month, &content_buff[7], 2);
    int_to_str(event1.output_days, &content_buff[9], 2);
    int_to_str(event1.hours, &content_buff[11], 2);
    int_to_str(event1.minutes, &content_buff[13], 2);
        
    // 60 byte
    for(i = 0; i < MAX_CHANNEL; i++)
    {
	    if(i == 0) x = Minute_Total_Power;
	    else x = 0;
	    long_to_str(x, &content_buff[15+(i*6)], 6);
    }

	if(Read_GPIO1())
		wifi_put_packet(MS06, content_buff, 75);
	else
		sci_put_packet(MS06, content_buff, 75);
}
#else
void MS06_operation(void)
{
    char i;
    unsigned long x;
    U16 Compare_temp;
    
    content_buff[0] = '0'; content_buff[1] = '0'; content_buff[2] = '0';

	Current_Temperature=111;
    // 14 byte
    Compare_temp = Current_Temperature; //cmlee

    //event1.years = 2016;
    //event1.output_month = 4;
    //event1.output_days =23;
    //event1.hours = 15;
    //if(test_min > 59)event1.minutes = test_min=0;
    //else
    //event1.minutes = test_min++;

    if(Current_Temperature < 0) content_buff[0] = '-';
    
    int_to_str(Compare_temp, content_buff, 3);
    int_to_str(event1.years, &content_buff[3], 4);
    int_to_str(event1.output_month, &content_buff[7], 2);
    int_to_str(event1.output_days, &content_buff[9], 2);
    int_to_str(event1.hours, &content_buff[11], 2);
    int_to_str(event1.minutes, &content_buff[13], 2);
        
    // 60 byte
    for(i = 0; i < MAX_CHANNEL; i++)
    {
    x = 123;///Get_RealTime_PowerData_send(i); //ckahn???
    long_to_str(x, &content_buff[15+(i*6)], 6);
    }

	if(Read_GPIO1())
		wifi_put_packet(MS06, content_buff, 75);
	else
		sci_put_packet(MS06, content_buff, 75);
}
#endif
void MH01_operation(void)
{
#if 0
	char i;
#if 1
	unsigned char year1, month1, day1;
	unsigned char year2,month2,day2;
	unsigned long addr1;
	unsigned char test;
#endif
#if !MH01_DEBUG
	unsigned char buff[4];
	unsigned long x;
#endif
    test=mh01.step;
	switch(mh01.step)
	{
		case STEP0:
			break;

		case STEP1:
#if	MH01_DEBUG
			mh01.step = STEP3;
			break;
#endif 

			// 전체 전송.
			if(mh01.option == 2)
			{
				mh01.address = DATA_BASE_ADD;
				mh01.step = STEP3;
				break;
			}

			if(mh01.month > 12 || mh01.hour >= 24)
			{
				mh01.step = STEP_ERR;
				break;
			}
			p_aug.year 	= mh01.year;
			p_aug.month = mh01.month;
			p_aug.day 	= mh01.day;
			p_aug.hour 	= mh01.hour;
			p_aug.channel_num = 0;
#if 1
			year1=ph.cur.year ;
			month1=ph.cur.month; 
			day1=ph.cur.day;
			year2=mh01.year ;
			month2=mh01.month;
			day2=mh01.day	;
#endif			
			if( ph.cur.year  == mh01.year  &&	\
				ph.cur.month == mh01.month &&	\
				ph.cur.day   == mh01.day)
			{
				mh01.address = ph.cur.point;
			}
			else
			{
				mh01.address = get_eeprom_address(&p_aug, DAY_K);
				
				addr1=mh01.address; // lcm

				if(!mh01.address)
				{
					mh01.step = STEP_ERR;
					break;
				}
			}

			mh01.step = STEP3;
			break;

		case STEP2:
			mh01.address += POWER_DATA_LENGTH;
			addr1=	mh01.address;
			addr1=	ph.cur.point;
			if(mh01.address > ph.cur.point)
			{
				#if 1
				if(mh01.address>0x001ffd00)mh01.address=0x00002000;     // 뒤로 갈 경우  
				else 
					{
				mh01.step = STEP_ERR;
				break;
				}
				#else
				mh01.step = STEP_ERR;
				break;
				#endif
			}
			mh01.step = STEP3;
			break;

		case STEP3:
#if !MH01_DEBUG
			sflash_fast_read(mh01.address+1, buff, 3);
			test=buff[0];
			// check for invalid value from frash memory
			if(buff[0]==0xff || buff[1]>12 || buff[2]>31 ){
				mh01.step = STEP3;
				mh01.hour++;
				if(mh01.hour > 23)
				{
					mh01.hour = 0;
					mh01.step = STEP2;
					}
				break;
				}
			// 10 byte
			int_to_str(buff[0]+2000, content_buff, 4);         // 년 
			int_to_str(buff[1], &content_buff[4], 2);           // 월 
			int_to_str(buff[2], &content_buff[6], 2);           // 일 
			int_to_str(mh01.hour, &content_buff[8], 2);     // 시 
	
			// 60 byte
			for(i = 0; i < MAX_CHANNEL; i++)                   // 6*10  
			{
				sflash_fast_read(mh01.address + 64 + (mh01.hour*40) +  (i * 4), \
					            (unsigned char *)&x, 4);
				
				if(mh01.address  == ph.cur.point)  
				if(mh01.hour > event1.hours)  
				{
					mh01.step = STEP_ERR;
					return;
				} 
				if(x==0xffffffff)x=0;     // ffff 에도 값이 들어간다. 그래서 0으로 처리 
				long_to_str(x, &content_buff[10+(i*6)], 6);
			}
#else
			content_buff[0] = '2';
			content_buff[1] = '0';
			content_buff[2] = '1';
			content_buff[3] = '4';

			content_buff[4] = '0';
			content_buff[5] = '6';

			content_buff[6] = '2';
			content_buff[7] = '3';

			int_to_str(mh01.hour, &content_buff[8], 2);
			for(i = 0; i < MAX_CHANNEL; i++)
			{
				long_to_str(i+1, &content_buff[10+(i*6)], 6);
			}
#endif
			sci_put_packet(MH01, content_buff, 70);
			#if 0     // cmlee    if you use mh01.ack_flag = 0;, conditional if  if(mh01.ack_flag)   does not run  in STEP4
			mh01.ack_flag = 0;
			#endif
			mh01.nak_flag = 0;
			sci_time = 24;     //10;	// 1s     //   if ACK is not received from PC, time interval .....Which time interval is.....
			mh01.step = STEP4;
			break;

		case STEP4:
			if(mh01.option == 0)
			{
				mh01.step = STEP0;
				mh01.flag = 0;
				break;
			}
#if 1    // cmlee          // when sci_time use , change to 0
			// ack timeout
			if(!sci_time)
			{
				mh01.step = STEP0;
				mh01.flag = 0;
				sci_put_packet(MH01, "TIMEOUT", 7);
				break;
			}
#endif
			if(mh01.nak_flag)
			{
				mh01.step = STEP3;
				break;
			}
#if 1		
			test=mh01.ack_flag;
			 if(mh01.ack_flag)   {// Original      When ACK received           //  if(!mh01.ack_flag)   when stored data send continuously
#else
			if(!sci_time)      {     //  when send data every sci_time interval  
#endif
	   	if(Qa.tx_fidx == Qa.tx_ridx)
			{
				mh01.step = STEP3;
				mh01.hour++;
				if(mh01.hour > 23)
				{
					mh01.hour = 0;
					mh01.step = STEP2;
#if	MH01_DEBUG
					mh01.step = STEP_ERR;
					break;
#endif
				}
			    }
			#if 1     // cmlee   
			mh01.ack_flag = 0;
			#endif
		       }
			break;

		case STEP_ERR:
			sci_put_packet(MH01, "NONE", 4);
			mh01.step = STEP0;
			mh01.flag = 0;
			break;
		default : break;
	}
#endif             
}
