/**
  ******************************************************************************
  * @file    USART/Interrupt/main.c
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "wifi9113.h"
#include "wifi_gpio_conf.h"
#include "ds1302.h"
#include "Delay.h"

#define	FALSE false
#define	TRUE true

char bAtSentBuff[255];
char DoByteBuffer[255];
char bAtRecBuff[255];
unsigned char TxCounterWifi = 0x00;
unsigned char RxCounterWifi = 0x00;
unsigned char WifiDataToXmt = 0x00;

//unsigned long wifi_start_time;
unsigned char Mac_Addr[6];
unsigned char Mac_Temp_Buff[17];
unsigned char Wifi_Rec_Buff[255];

unsigned char WifiCheckStep = 0;
unsigned char WifiProcessStep = 0;
unsigned char WifiConnectStep = 0;
unsigned char WifiConnectNext = 0;

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

unsigned char TOTAL_MAC_LENGTH = 0;
bool WifiHaveRecFlag = false;
unsigned char WifiDatCome = 0;
unsigned char TotalRecvBytes = 0; //REC  NUMBER
unsigned char SERVER_IP_ADDRESS[] = SERVER_IP_ADDRESS_DEFAULT;

unsigned char WIFI_LED_FLAG;
unsigned int WIFI_ERROR_NUM;
//unsigned char WIFI_ERROR_TIME;

char sent_data_buf[255];
u16 sent_data_size = 0;


static uint8_t outBuffer[256];
u8 Packaging_wifi_sendbuffer ( u8 type, u8 *inBuffer, u8 inSize )
{
    uint8_t i, check_num;

    outBuffer[0] = 0x4B;
    outBuffer[1] = 0x52;
    outBuffer[2] = inSize + 3;
    outBuffer[3] = type;

    for ( i = 0; i < inSize; i++ )
    {
        outBuffer[4 + i] = inBuffer[i];
    }

    check_num = outBuffer[2];

    for ( i = 3; i < 4 + inSize; i++ )
    {
        check_num ^= outBuffer[i];
    }

    outBuffer[4 + inSize] = check_num;
    outBuffer[4 + inSize + 1] = 0x55;

    memcpy ( inBuffer, outBuffer, inSize + 6 );
    return ( inSize + 6 );
}


void AtSent ( char *SendBuff, unsigned int BuffCount )
{
#if 0
    unsigned char i;

    for ( i = 0; i < BuffCount; i++ )
    {
        USART_SendData ( USART9113, SendBuff[i] );

        //  while( USART_GetFlagStatus(USART9113,USART_FLAG_TC)!= 1);
        while ( USART_GetFlagStatus ( USART9113, USART_FLAG_TC ) == RESET );
    }

#else

    // Wait for previous xmt to complete
    while ( TxCounterWifi < WifiDataToXmt );

    // Setup next XMT
    memcpy ( bAtSentBuff, SendBuff, BuffCount );
    TxCounterWifi = 0;
    WifiDataToXmt = BuffCount;

    //USART_ITConfig(USART9113, USART_IT_RXNE, DISABLE); 	  //发送前，关接收中断
    USART_ITConfig ( Wifi_UART, USART_IT_TXE, ENABLE );

#endif
}

int HighBytetoASCII ( unsigned char Data_Byte_in )
{
    unsigned char Data_Temp1;
    //	byte Data_Temp2;
    Data_Temp1 = Data_Byte_in >> 4 & 0x0f;

    //	Data_temp2=Data_Byte_in&0x0f;
    if ( Data_Temp1 > 9 )
    {
        Data_Temp1 = ( Data_Temp1 - 10 ) + 0x61;
    }
    else
        Data_Temp1 = Data_Temp1 + 0x30;

    return Data_Temp1;
}

int LowBytetoASCII ( unsigned char Data_Byte_in )
{
    //	byte Data_Temp1;
    unsigned char Data_temp2;
    //	Data_Temp1=Data_Byte_in>>4&0x0f;
    Data_temp2 = Data_Byte_in & 0x0f;

    if ( Data_temp2 > 9 )
    {
        Data_temp2 = ( Data_temp2 - 10 ) + 0x61;
    }
    else
        Data_temp2 = Data_temp2 + 0x30;

    return Data_temp2;
}


void clear_bAtRecBuff ( void )
{
    memset ( bAtRecBuff, 0, start );
    start = 0;
}
void clear_bAtSentBuff ( void )
{
    memset ( bAtSentBuff, 0, WifiDataToXmt );
}

/*
	查找字符串位置
	<=255bit
*/
char *FindStrPos ( char *socStr, u8 socSize, char *findStr, u8 findSize )
{
    u8 i, j, t;

    for ( i = 0; i < socSize; i++ )
    {
        for ( j = 0, t = 0; j < findSize; j++ )
        {
            if ( findStr[j] == socStr[i + j] )
            {
                t++;

                if ( t == findSize )
                {
                    return ( &socStr[i] );
                }
            }
            else
            {
                break;
            }
        }

    }

    return NULL;

}

char *FindATcommd ( char *findStr )
{
    char *p = NULL;
    p = FindStrPos ( bAtRecBuff, start, findStr, strlen ( findStr ) );

    return p;
}


bool CheckOK ( char *Cvalue )
{
#if 0

    if ( ( ( NULL != strstr ( bAtRecBuff, AT_REPLY_OK ) ) || ( NULL != strstr ( bAtRecBuff, AT_REPLY_NO_CHANGE ) ) ) )
    {
        return true;
    }
    else
    {
        return false;
    }

#else

    if ( FindATcommd ( AT_REPLY_OK ) )
    {
        return true;
    }
    else
    {
        return false;
    }

#endif
}

bool CheckERROR ( char *Cvalue )
{
    char *p;
#if 0

    if ( NULL != ( p = strstr ( bAtRecBuff, AT_REPLY_ERROR ) ) )
    {
        WIFI_ERROR_NUM = ( * ( p + 5 ) ) | ( * ( p + 5 + 1 ) << 8 );
        return true;
    }
    else
    {
        return false;
    }

#else

    if ( p = FindATcommd ( AT_REPLY_ERROR ) )
    {
        WIFI_ERROR_NUM = ( * ( p + 5 ) ) | ( * ( p + 5 + 1 ) << 8 );
        return true;
    }
    else
    {
        return false;
    }

#endif
}

bool AtCmp ( char *str0, char *str1 )
{
    unsigned char i;

    for ( i = 0; i < 100; i++ )
    {

        if ( str0[i] != str1[i] )
        {
            return true;
        }
        else
        {
            if ( str0[i] == 0 )
            {
                return false;
            }
        }

    }

    return true;

}

bool ABCcompare ( char *Cvalue0, unsigned char *Cvalue1 )
{
    unsigned char i;

    for ( i = 0; i < 100; i++ )
    {
        if ( Cvalue1[i] != 0 )
        {
            if ( Cvalue0[i] != Cvalue1[i] )
                return false;
        }
    }

    return true;
}

int iByteStuffing = 0;
int DoByteStuffing ( char *data_out, char *data_in, int data_in_size )
{
    int i = 0, j = 0;
    int TotalSize = 0;
    int iSet0x0A = 0;
    unsigned char bValue = 0;

    for ( i = 0; i < data_in_size; i++ )
    {
        //if ((data_in[i] == 0x0D) && (data_in_size != (i - 2)))
        if ( data_in[i] == 0x0D )
        {
            if ( data_in[i + 1] == 0x0A )
            {
                bValue = 0xDB;
                iSet0x0A = 1;
            }
            else
            {
                bValue = data_in[i];
            }
        }
        else if ( ( data_in[i] == 0x0A ) && ( iSet0x0A == 1 ) )
        {
            bValue = 0xDC;
            iSet0x0A = 0;

            if ( iByteStuffing < 65535 )
                iByteStuffing++;
        }
        else if ( data_in[i] == 0xDB )
        {
            data_out[j++] = 0xDB;
            bValue = 0xDD;

            if ( iByteStuffing < 65535 )
                iByteStuffing++;
        }
        else
        {
            bValue = data_in[i];
        }

        data_out[j++] = bValue;
    }

#if 0

    if ( i != j )
    {
        if ( iByteStuffing < 65535 )
            iByteStuffing++;
    }

#endif
    TotalSize = j;
    return TotalSize;
}

int DoByteStrSize = 0;
void AT_command_send ( char *str, int handle, unsigned char n )
{
    int DataSize, iTotal;
    int i;
#if 0
    DataSize = n;
    sprintf ( ( char * ) bAtSentBuff, "%#s%#d,%#d,0,0,", AT_COMMAND_SND_CMD,
              handle, DataSize );
    iTotal = strlen ( bAtSentBuff );

    for ( i = 0; i < DataSize; i++ )
    {
        bAtSentBuff[iTotal + i] = str[i];
    }

    bAtSentBuff[DataSize + iTotal] = 0x0D;
    bAtSentBuff[DataSize + iTotal + 1] = 0x0A;
    bAtSentBuff[DataSize + iTotal + 2] = 0x00;
    iTotal += DataSize + 2;
    AtSent ( bAtSentBuff, iTotal );
#else
    // 发送处理
    memset ( DoByteBuffer, 0, DoByteStrSize );
    DoByteStrSize = DoByteStuffing ( DoByteBuffer, str, n );

    DataSize = DoByteStrSize;
    sprintf ( ( char * ) bAtSentBuff, "%#s%#d,%#d,0,0,", AT_COMMAND_SND_CMD,
              handle, DataSize );
    iTotal = strlen ( bAtSentBuff );

    for ( i = 0; i < DataSize; i++ )
    {
        bAtSentBuff[iTotal + i] = DoByteBuffer[i];
    }

    bAtSentBuff[DataSize + iTotal] = 0x0D;
    bAtSentBuff[DataSize + iTotal + 1] = 0x0A;
    bAtSentBuff[DataSize + iTotal + 2] = 0x00;
    iTotal += DataSize + 2;
    AtSent ( bAtSentBuff, iTotal );
#endif

}



void wifi_sent ( void )
{
    char sendstring[50];
    unsigned char i;

    for ( i = 0; i < 6; i++ )
    {
        sendstring[i] = Mac_Addr[i];
    }

    sendstring[6] = 0x00;
    AT_command_send ( sendstring, 1, 6 );
}


char KeyString[100] = {0};
char *pStr = NULL;
void wifi_connect ( void )
{


    switch ( WifiConnectStep )
    {
    case 0:
        clear_bAtRecBuff();
        WifiSoftReset();
        WifiConnectStep = 1;
        break;

    case 1:
        if ( FindATcommd ( CHECK_REPLY_Ready ) )
        {
            //net close
            WifiConnectStep = 2;
            WifiTimeReset = 0;
        }
        else if ( ( WifiTimeReset % 3000 ) == 0 )	//需要1241
        {
            WifiConnectStep = 0;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 6000 ) )
        {
            WifiHardReset();		//硬件没做
            WifiProcessStep = WIFI_STEP_CHECK;
            WifiCheckStep = 0;

            WifiTimeReset = 0;
        }

        break;

    case 2:
        clear_bAtRecBuff();
        strcpy ( bAtSentBuff, CONNECT_CWMODE );
        AtSent ( bAtSentBuff, strlen ( bAtSentBuff ) );
        WifiConnectStep = 3;
        break;

    case 3:
        if ( CheckOK ( bAtRecBuff ) ) //AtCmp(bAtRecBuff,AT_COMMAND_OK_ONLY)==0
        {
            WifiConnectStep = 4;
            WifiTimeReset = 0;
        }
        else if ( ( WifiTimeReset % 1000 ) == 0 )
        {
            WifiConnectStep = 2;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 2000 ) ) //0x3fff//需要3
        {
            WifiConnectStep = 0;
            WifiTimeReset = 0;
        }

        break;

    case 4:
        clear_bAtRecBuff();
        strcpy ( bAtSentBuff, CONNECT_CWJAP );
        AtSent ( bAtSentBuff, strlen ( bAtSentBuff ) );
        WifiConnectStep = 5;
        break;

    case 5:
        if ( CheckOK ( bAtRecBuff ) )
        {
            WifiConnectStep = 6;
            WifiTimeReset = 0;
        }
        else if ( ( WifiTimeReset % 5000 ) == 0 )
        {
            WifiConnectStep = 4;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 10000 ) ) //0x3fff//需要3043
        {
            WifiConnectStep = 0;
            WifiTimeReset = 0;
        }

        break;

    case 6:
        clear_bAtRecBuff();
        strcpy ( bAtSentBuff, CONNECT_CWJAP_CX );
        AtSent ( bAtSentBuff, strlen ( bAtSentBuff ) );
        WifiConnectStep = 7;
        break;

    case 7:
        if ( CheckOK ( bAtRecBuff ) ) //NULL != strstr(bAtRecBuff,CONNECT_CWJAP_CX_REPLY)
        {
            WIFI_LED_FLAG = WIFI_JOIN_ROUTE_OK_FLAG;
            WifiConnectStep = 8;
            WifiTimeReset = 0;
        }
        else if ( ( WifiTimeReset % 1000 ) == 0 ) //1500
        {
            WifiConnectStep = 6;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 2000 ) ) //0x17ffff//需要1132
        {
            WifiConnectStep = 0;
            WifiTimeReset = 0;
        }

        break;

    case 8:
        clear_bAtRecBuff();
        strcpy ( bAtSentBuff, CONNECT_CIPMUX );
        AtSent ( bAtSentBuff, strlen ( bAtSentBuff ) ); //AT_COMMAND_PSK_LENGTH+10
        //GetDis(bAtSentBuff);
        WifiConnectStep = 9;
        break;

    case 9:
        if ( CheckOK ( bAtRecBuff ) )
        {
            WifiConnectStep = 10;
            WifiTimeReset = 0;
        }
        else if ( ( WifiTimeReset % 1000 ) == 0 )
        {
            WifiConnectStep = 8;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 2000 ) ) //0xffff//需要11
        {
            WifiConnectStep = 0;
            WifiTimeReset = 0;
        }

        break;

    case 10:
        clear_bAtRecBuff();
        strcpy ( bAtSentBuff, CONNECT_CIPSTART );
        AtSent ( bAtSentBuff, strlen ( bAtSentBuff ) ); //USB_AT_COMMAND_JOIN_LENGTH+14
        //GetDis(bAtSentBuff);
        WifiConnectStep = 11;
        break;

    case 11:
        if ( FindATcommd ( CONNECT_CIPSTART_Port ) && \
                ( pStr = FindATcommd ( CONNECT_CIPSTART_NOWis ) ) )
        {
            Refresh_DS1302_Time ( pStr );
            WifiConnectStep = 20;
            WifiTimeReset = 0;
        }
        else if ( ( WifiTimeReset % 6000 ) == 0 ) //7500//8244
        {
            WifiConnectStep = 10;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 12000 ) ) //0x1fffff//需要7127
        {
            WifiConnectStep = 0;
            WifiTimeReset = 0;
        }

        break;
        /*
        //作为服务端
        12.配置IP
        14.打开端口
        */

    case 20:
        clear_bAtRecBuff();
        strcpy ( bAtSentBuff, CONNECT_CIFSR );
        AtSent ( bAtSentBuff, sizeof ( CONNECT_CIFSR ) );
        //	GetDis(bAtSentBuff);
        WifiConnectStep = 21;
        break;

    case 21:

        //if(NULL != (p = strstr(bAtRecBuff,CONNECT_STAMAC)))
        if ( NULL != ( pStr = FindATcommd ( CONNECT_STAMAC ) ) )
        {
            memcpy ( Mac_Temp_Buff, &pStr[strlen ( CONNECT_STAMAC )], sizeof ( Mac_Temp_Buff ) );
            WifiConnectStep = 22;
            WifiTimeReset = 0;
        }
        else if ( ( WifiTimeReset % 4000 ) == 0 ) //7500//8244
        {
            WifiConnectStep = 20;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 8000 ) ) //0x1fffff//需要7127
        {
            WifiConnectStep = 0;
            WifiTimeReset = 0;
        }

        break;

    case 22:
        clear_bAtRecBuff();
        reg_server0824 ( REGMY_ID, Mac_Temp_Buff, sizeof ( Mac_Temp_Buff ) );
        //GetDis(bAtSentBuff);
        WifiConnectStep = 23;
        break;

    case 23:
        if ( FindATcommd ( "OK:reg" ) )
        {
            WIFI_LED_FLAG = WIFI_LOAD_DONE_FLAG;
            WifiProcessStep = WIFI_STEP_CHAT;
            WifiTimeReset = 0;
            clear_bAtRecBuff();
        }
        else if ( ( WifiTimeReset % 4000 ) == 0 ) //7500//8244
        {
            WifiConnectStep = 22;
        }
        else if ( CheckERROR ( bAtRecBuff ) || ( WifiTimeReset > 8000 ) ) //0x1fffff//需要7127
        {
            WifiConnectStep = 0;
            WifiTimeReset = 0;
        }

        break;

    default:
        WifiConnectStep = 0;
        break;

    }

}

char get_mac ( char *wifi_data )
{
    char *str_p;

    if ( NULL != ( str_p = strstr ( wifi_data, CONNECT_STAMAC ) ) )
    {
        memcpy ( Mac_Temp_Buff, &str_p[8], sizeof ( Mac_Temp_Buff ) );
        return 1;
    }
    else
    {
        return 0;
    }
}
unsigned char scan_wifi ( char *wifi_data )
{
    unsigned char count;
    count = 0;

    //if(wifi_data[0]!= 0xab || wifi_data[0] != 0xaa)
    //	return 0;
    for ( count = 0; count < 60; count++ )
    {
        if ( wifi_data[count] == 0x00 )
        {
            break;
        }
    }

    if ( count == 60 )
        return 0;

    return count;
}



void receive_deal ( char * Rdata, char Totalbytes )
{

}

void wifi_check ( void )
{
    //char commd;
    switch ( WifiCheckStep )
    {
    case 0:
        WifiCheckStep = 1;
        WifiTimeReset = 0;
        WifiLedOff;
        break;

    case 1:
        if ( WifiTimeReset > 100 )	//等待100ms
        {
            WifiCheckStep = 2;
        }

        break;

    case 2:
        if ( WifiHaveRecFlag )
        {
            WifiHaveRecFlag = false;

            if ( FindATcommd ( CHECK_REPLY_Ready ) )
            {
                WifiCheckStep = 3;
            }
            else if ( CheckERROR ( bAtRecBuff ) )
            {
                WifiCheckStep = 3;
            }
        }
        else
        {
            if ( WifiTimeReset > 1000 ) //8000
            {
                WifiCheckStep = 3;
            }
        }

        break;

    case 3:			//正常启动，或超时启动
        WifiTimeReset = 0;
        WifiProcessStep = WIFI_STEP_CONNECT;
        WifiConnectStep = 2;
        clear_bAtRecBuff();
        break;

    default:
        WifiCheckStep = 0;
        break;
    }
}

/*
	Now is: 2015-07-26 17:37:35
	去掉Now is: 20
	15-07-26 17:37:35

	星期计算:
	最著名的是蔡勒（Zeller）公式。
	即w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1

	以2049年10月1日（100周年国庆）为例，用蔡勒（Zeller）公式进行计算，过程如下：
	蔡勒（Zeller）公式：w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
	=49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1
	=49+[12.25]+5-40+[28.6]
	=49+12+5-40+28
	=54 (除以7余5)
	即2049年10月1日（100周年国庆）是星期5。

*/
#define CalDay(y,c,m,d) ((y+y/4+c/4-2*c+26*(m+1)/10+d-1)%7)

void Refresh_DS1302_Time ( char data[] )
{
    char *p;
    char dataBuff[18] = {0};
    memcpy ( dataBuff, &data[10], sizeof ( dataBuff ) - 1 );
    p = strtok ( dataBuff, "-" );
    DS1302_DateTime.Year = atoi ( dataBuff );

    p = strtok ( NULL, "-" );
    DS1302_DateTime.Month = atoi ( p );

    p = strtok ( NULL, " " );
    DS1302_DateTime.Date = atoi ( p );

    p = strtok ( NULL, ":" );
    DS1302_DateTime.Hour = atoi ( p );

    p = strtok ( NULL, ":" );
    DS1302_DateTime.Minute = atoi ( p );

    p = strtok ( NULL, " " );
    DS1302_DateTime.Second = atoi ( p );

    DS1302_DateTime.Day = GetDayFromDate ( DS1302_DateTime.Year, DS1302_DateTime.Month, DS1302_DateTime.Date );
    DS1302_DateTime.HourMode = _24_hours;
    DS1302_DateTime.AM_PM = pm;

    sys_rtc_time = Date_TO_Sec ( DS1302_DateTime.Year, DS1302_DateTime.Month, DS1302_DateTime.Date, DS1302_DateTime.Hour, DS1302_DateTime.Minute, DS1302_DateTime.Second );
	
#if DS1302_def
    DS1302_SetDateTime();
#endif
}


void esp8266_send ( void )
{
    u8 delayTime;

#ifndef CONNECT_Multiple_DEF
    sprintf ( bAtSentBuff, "%s%d\r\n", CONNECT_CIPSEND_N, sent_data_size );
#else
    sprintf ( bAtSentBuff, "%s,%d\r\n", CONNECT_CIPSEND_N, sent_data_size );
#endif
    AtSent ( bAtSentBuff, strlen ( bAtSentBuff ) );

    if ( sent_data_size < 64 )			delayTime = 5;
    else if ( sent_data_size < 128 )		delayTime = 10;
    else if ( sent_data_size < 256 )		delayTime = 15;
    else if ( sent_data_size < 512 )		delayTime = 20;
    else if ( sent_data_size < 1024 )	delayTime = 25;

    delay_ms ( delayTime );

    AtSent ( sent_data_buf, sent_data_size );
}

void reg_server0824 ( u8 regID, u8 *inBuffer, u8 inSize )
{
    memset ( sent_data_buf, 0, 255 );
    memset ( DoByteBuffer, 0, 255 );
    //实际数据
    sent_data_buf[0] = regID;
    sent_data_buf[1] = regID;
    memcpy ( &sent_data_buf[2], inBuffer, inSize );
    sent_data_size = Packaging_wifi_sendbuffer ( COMMD_REG, ( u8 * ) sent_data_buf, inSize + 2 );

    //特殊处理
    memcpy ( DoByteBuffer, sent_data_buf, sent_data_size );
    sent_data_size = DoByteStuffing ( sent_data_buf, DoByteBuffer, sent_data_size );

    //加后缀
    sent_data_buf[sent_data_size + 0] = 0x0D;
    sent_data_buf[sent_data_size + 1] = 0x0A;

    sent_data_size = sent_data_size + 2;

    //发送
    esp8266_send();
}

void sendto_server0824 ( u8 aimID, u8 myID, u8 *inBuffer, u8 inSize )
{
    memset ( sent_data_buf, 0, 255 );
    memset ( DoByteBuffer, 0, 255 );
    //实际数据
    sent_data_buf[0] = aimID;
    sent_data_buf[1] = myID;
    memcpy ( &sent_data_buf[2], inBuffer, inSize );
    sent_data_size = Packaging_wifi_sendbuffer ( COMMD_TRANSMIT, ( u8 * ) sent_data_buf, inSize + 2 );

    //特殊处理
    memcpy ( DoByteBuffer, sent_data_buf, sent_data_size );
    sent_data_size = DoByteStuffing ( sent_data_buf, DoByteBuffer, sent_data_size );

    //加后缀
    sent_data_buf[sent_data_size + 0] = 0x0D;
    sent_data_buf[sent_data_size + 1] = 0x0A;

    sent_data_size = sent_data_size + 2;

    //发送
    esp8266_send();
}



void wifi_chat ( void )
{
    char *p;

    if ( WifiHaveRecFlag )
    {
        WifiHaveRecFlag = false;

        //if(NULL !=  FindATcommd(AT_COMMAND_READ))
        //符合krain格式，则进入处理
        if ( NULL != ( p = FindATcommd ( "KR" ) ) )
        {
            TotalRecvBytes = p[2] + 3;

            if ( 0x55 == p[TotalRecvBytes - 1] )
            {
                memcpy ( Wifi_Rec_Buff, p, TotalRecvBytes );
                WifiDatCome = 1;
                WIFI_LED_FLAG = WIFI_DATA_COME_FLAG;
            }
        }
        else if ( FindATcommd ( AT_COMMAND_CLOSE ) || \
                  FindATcommd ( AT_COMMAND_LINK_ERROR ) || \
                  FindATcommd ( AT_COMMAND_REGID_ERROR )
                )
        {
            WifiProcessStep = WIFI_STEP_CONNECT;
            WifiConnectStep = 10;
        }

        clear_bAtRecBuff();
        WifiTimeLed = 0;
        WifiTimeReset = 0;

    }

    /*else if((WifiTimeReset > 20000) && (WifiTimeReset%2000==0))
    {
    	 WIFI_LED_FLAG = WIFI_CLOSE_FLAG;
    	 sendto_server0824(AIM_ID, REGMY_ID, "Msg Test！", 10);
    }*/


}

void LED_Flash_State ( void )
{
    switch ( WIFI_LED_FLAG )
    {

    case WIFI_JOIN_ROUTE_OK_FLAG:
        if ( WifiTimeLed % 2000 == 0 )		   WifiLedOn; //50ms
        else if ( WifiTimeLed % 2000 == 1000 )   WifiLedOff; //150ms

        break;

    case WIFI_LOAD_DONE_FLAG:
        WifiLedOn;
        WIFI_LED_FLAG = WIFI_MAX_FLAG;
        break;

    case WIFI_LTCP_CONNECT_FLAG:
        if ( WifiTimeLed < 50 )			WifiLedOn; //50ms
        else if ( WifiTimeLed < 200 )	WifiLedOff; //150ms
        else if ( WifiTimeLed < 250 )	WifiLedOn; //50ms
        else if ( WifiTimeLed < 550 )	WifiLedOff; //300ms
        else if ( WifiTimeLed < 600 )	WifiLedOn; //50ms
        else if ( WifiTimeLed < 750 )	WifiLedOff; //150ms
        else if ( WifiTimeLed < 800 )	WifiLedOn; //50ms
        else if ( WifiTimeLed < 850 )	WIFI_LED_FLAG = WIFI_LOAD_DONE_FLAG; //50ms

        break;

    case WIFI_DATA_COME_FLAG:
        if ( WifiTimeLed < 50 )			WifiLedOn; //50ms
        else if ( WifiTimeLed < 200 )	WifiLedOff; //150ms
        else if ( WifiTimeLed < 250 )	WifiLedOn; //50ms
        else if ( WifiTimeLed < 300 )	WIFI_LED_FLAG = WIFI_LOAD_DONE_FLAG; //50ms

        break;

    case WIFI_CLOSE_FLAG:
        WifiLedOff;
        WIFI_LED_FLAG = WIFI_MAX_FLAG;
        break;

    default:
        break;
    }
}

void wifi_main ( void )
{
#if WIFI_Uart1_def

    switch ( WifiProcessStep )
    {
    case WIFI_STEP_CHECK:
        wifi_check();
        break;

    case WIFI_STEP_CONNECT:
        wifi_connect();
        break;

    case WIFI_STEP_CHAT:
        wifi_chat();
        break;

    default:
        WifiProcessStep = WIFI_STEP_CONNECT;
        break;
    }

    LED_Flash_State();
#endif
}

