/**
 * @file wh_lte_7s4_v2.c
 * @author Chen Jihang (embedded@eseaoptics.com)
 * @brief WH LTE 7S4 V2驱动
 * @version 0.1
 * @date 2022-07-18
 * 
 * @copyright ESEA (c) 2020
 * 相关模块
 */
#include <string.h>
#include "wh_lte_7s4_v2.h"
#include "FreeRTOS.h"
#include "cmsis_os2.h"
#include "usart.h"
#include "elog.h"
 
My_WH_LTE_TypeDef my_wh_lte=
{
    wh_lte_7s4_init,
    WH_LTE_Port,
    WH_LTE_Set_State,
        NULL,
        NULL,
        NULL,
        0,
        0,
        0,
        Uart_Interface_Default, /* 接口类型 0: common, 1: 485 ,2:3160*/
        Uart_RSMODE_232,        /* mode_232_485 0 commome  1:485*/
        Uart_Trans_DMA,         /* trans_type0 :polling, 1: IT  2: DMA*/
        Uart_Trans_DMA,         /* rcv_type 0 :polling, 1: IT  2: DMA*/
        Uart_IDLE_IT_ENABLE,    /* idle_enable_disable 0 :不启用空闲 , 1: 启用空闲*/
       NULL, /* pointer huart*/
};

const static char *wh_lte_7s4_cmd_state_table[]={"OFF","ON"};
const static char *wh_lte_7s4_cmd_work_mode_table[]={"NET","HTTPD","UDC"};
const static char *wh_lte_7s4_cmd_uart_parity_table[]={"NONE","ODD","EVEN"};
const static char *wh_lte_7s4_cmd_sockasl_table[]={"SHORT","LONG"};
const static char *wh_lte_7s4_cmd_protocol_table[]={"TCP","UDP"};
const static char *wh_lte_7s4_procedure_table[]={
    "+++",  "a",    "WKMOD",    "SOCKASL", "SOCKAEN",   "SOCKA","CCLK", "Z" };
const static char *wh_lte_7s4_procedure_rcv_table[]={
    "a",  "+ok",  "OK",       "OK",       "OK",       "OK",   "+CCLK:", "OK" };

WH_LTE_7S4_Typedef *wh_lte_7s4;

UART_HandleTypeDef *pWH_LTE_Uart = &huart2;
#define WH_LTE_Uart_IRQHandler USART2_IRQHandler

 uint8_t WH_LTE_Rcv_Buf[WH_LTE_Rcv_Buf_Size] ={0};
 uint8_t Time_Buf[32] = {0};
 uint8_t Back_Buf[16] = {0};
// static uint8_t WH_LTE_Rcv_Buf[WH_LTE_Rcv_Buf_Size] ={0};

osThreadId_t wh_lteHandle;
const osThreadAttr_t WH_LTE_attributes = {
    .name = "wh_lte",
    .stack_size = 1024,
    .priority = (osPriority_t)osPriorityBelowNormal,
};



int wh_lte_7s4_init( )
{
    wh_lte_7s4 = (WH_LTE_7S4_Typedef *)malloc(sizeof(WH_LTE_7S4_Typedef));

    /*  设置超时*/
    WH_LTE_Set_Timeout(wh_lte_7s4, 2000);

    // wh_lte_7s4->send_fun = send_fun;
    // wh_lte_7s4->send_obj = send_obj;
    wh_lte_7s4->receive_a_flag=0;
    wh_lte_7s4->receive_err_code=0;
    wh_lte_7s4->receive_ok_flag=0;
    wh_lte_7s4->receive_para_num=0;

    wh_lte_7s4->size_received=0;
    wh_lte_7s4->timebase=0;
    wh_lte_7s4->state=MODULE_WH_LTE_7S4_INIT_STATE_WAIT;
    return 0;
}

void  WH_LTE_Set_State(MODULE_WH_LTE_WORK_STATE state)
{
    wh_lte_7s4->state =state;
}

void WH_LTE_Uart_IRQHandler(void)
{
    uint16_t count = 0;
    uint16_t data_length =0;
    if (__HAL_UART_GET_FLAG(pWH_LTE_Uart, UART_FLAG_TC) != RESET)
    {
        log_i("WH_LTE_.... TC IT ...");
        // WH_LTE_Set_GPIO_For_TC_Cplt(wh_lte_7s4);
        pWH_LTE_Uart->gState = HAL_UART_STATE_READY;
        __HAL_UART_CLEAR_FLAG(pWH_LTE_Uart, UART_FLAG_TC);
    }

    if (__HAL_UART_GET_FLAG(pWH_LTE_Uart, UART_FLAG_IDLE) != RESET )
    {
        log_i("WH_LTE_....  IDLE  IT ....  ");
        // TODO ...处理空闲 ，最后一位  datalength-1

        count = __HAL_DMA_GET_COUNTER(pWH_LTE_Uart->hdmarx);
        data_length = WH_LTE_Rcv_Buf_Size - count;
        log_i("   IDLE  IT ....  %d ", data_length);
        if (data_length > 0)
        {
            log_i("WH_LTE_. IDLE..data_length  %d  %2x",   data_length, WH_LTE_Rcv_Buf[0]);
            WH_LTE_copy(wh_lte_7s4, data_length);
            // wh_lte_7s4_receive_callback(wh_lte_7s4, data_length);
            // log_i("WH_LTE_. IDLE..res  %02X %d %02X",  wh_lte_7s4->uarthelper ->receive_buf[0],  wh_lte_7s4->uarthelper ->receive_buf[2],  wh_lte_7s4->uarthelper ->receive_buf[data_length-1]);
        }
 
        __HAL_UART_CLEAR_IDLEFLAG(pWH_LTE_Uart);
    }

    if (__HAL_UART_GET_FLAG(pWH_LTE_Uart, UART_FLAG_ORE) != RESET)
    {
        log_i("WH_LTE_....ORE");

        __HAL_UART_CLEAR_FLAG(pWH_LTE_Uart, UART_FLAG_ORE);
    }
    if (__HAL_UART_GET_FLAG(pWH_LTE_Uart, UART_FLAG_FE) != RESET)
    {
        log_i("WH_LTE_....FE");

        __HAL_UART_CLEAR_FLAG(pWH_LTE_Uart, UART_FLAG_FE);
    }
    if (__HAL_UART_GET_FLAG(pWH_LTE_Uart, UART_FLAG_PE) != RESET)
    {
        log_i("WH_LTE_....PE");

        __HAL_UART_CLEAR_FLAG(pWH_LTE_Uart, UART_FLAG_PE);
    }
    HAL_UART_IRQHandler(pWH_LTE_Uart);
}


int WH_LTE_copy(WH_LTE_7S4_Typedef * wh_lte_7s4 ,uint16_t size)
{
    log_i("WH_LTE_copy..size  %d   ",   size );
    wh_lte_7s4->size_received = size;
}

void WH_LTE_Port( )
{
    wh_lteHandle = osThreadNew(WH_LTE_Task, (void*)(&wh_lte_7s4->state), &WH_LTE_attributes);
    // wh_lteEventHandle = osEventFlagsNew ( &wh_lteEvent_attributes );
  	// wh_lteTimerHandle = osTimerNew(wh_lteTimer_Callback, osTimerOnce, NULL, &wh_lteTimer_attributes);
}


void WH_LTE_Task(void* argument)
{
    static int err_con=0;  
    int st; // static  ！！！
    MODULE_WH_LTE_WORK_STATE *state = argument;
    // FLAG   发送后进入 busy,  接收sn后就rcved_flag
    for ( ;  ;  )
    {
        switch (*state)
        {
            case MODULE_WH_LTE_7S4_INIT_STATE_WAIT:
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_START:
                log_i("pppp .. wh_lte_7s4->timeout_ms %d",wh_lte_7s4->timeout_ms);
                wh_lte_7s4->error=0;
                wh_lte_7s4->size_received = 0;
                memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                wh_lte_7s4->timebase = osKernelGetTickCount();
                wh_lte_7s4_ppp( &wh_lte_7s4 );
                __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;
 
                *state = MODULE_WH_LTE_7S4_INIT_STATE_PPP;
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_PPP:
                log_i(" a .. ok... %d",  wh_lte_7s4->size_received);
                if (wh_lte_7s4->size_received >=1)
                {
                    if (Compare_RcvBuf_And_Procedure_Table(0, WH_LTE_Rcv_Buf, wh_lte_7s4->size_received) == 0)
                    {
                        HAL_UART_DMAStop(pWH_LTE_Uart );
                        wh_lte_7s4->receive_a_flag = 1;  

                        err_con=0;
                        wh_lte_7s4->error=0;
                        wh_lte_7s4->size_received = 0;
                        memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                        wh_lte_7s4->timebase = osKernelGetTickCount();

                        wh_lte_7s4_a(&wh_lte_7s4);

                        __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                        HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;                    
                        *state=MODULE_WH_LTE_7S4_INIT_STATE_A;               
                    }
                }
                           
                // if( wh_lte_7s4->receive_a_flag==1 )
                // {

                //     wh_lte_7s4->error=0;
                //     wh_lte_7s4->receive_a_flag=0;
                //     wh_lte_7s4->receive_ok_flag=0;
                //     wh_lte_7s4->size_received = 0;
                //     memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                //     wh_lte_7s4->timebase = osKernelGetTickCount();

                //     wh_lte_7s4_a(&wh_lte_7s4);

                //     __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                //     HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;                    
                //     *state=MODULE_WH_LTE_7S4_INIT_STATE_A;
                // }
                if( (osKernelGetTickCount() - wh_lte_7s4->timebase) >=wh_lte_7s4->timeout_ms){
                    log_w( "WH LTE ppp Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("WH LTE Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        wh_lte_7s4->error = 1;
                        *state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
                        break;
                    } 
                    *state=MODULE_WH_LTE_7S4_INIT_STATE_START;
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_A:
                log_i(" INIT_STATE_A ...");
                if (wh_lte_7s4->size_received >=2)
                {
                    if (Compare_RcvBuf_And_Procedure_Table(1, WH_LTE_Rcv_Buf, wh_lte_7s4->size_received) == 0)
                    {
                        HAL_UART_DMAStop(pWH_LTE_Uart );

                        err_con=0;
                        wh_lte_7s4->error=0;
                        wh_lte_7s4->receive_ok_flag=0;
                        wh_lte_7s4->size_received = 0;
                        memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                        wh_lte_7s4->timebase = osKernelGetTickCount();

                        wh_lte_7s4_at_set_workmode( &wh_lte_7s4,WH_LTE_7S4_CMD_WORK_MODE_NET );

                        __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                        HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;   
                        *state = MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE;                  
                    }
                }
                // if( wh_lte_7s4->receive_ok_flag==1 )
                // {
                //     wh_lte_7s4->error=0;
                //     wh_lte_7s4->receive_ok_flag=0;
                //     wh_lte_7s4->size_received = 0;
                //     memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                //     wh_lte_7s4->timebase = osKernelGetTickCount();

                //     wh_lte_7s4_at_set_workmode( &wh_lte_7s4,WH_LTE_7S4_CMD_WORK_MODE_NET );

                //     __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                //     HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;   
                //     *state = MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE;
                // }
                // to  error 重试
                break;    
            case MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE:
                log_i(" STATE_SET_WORKMODE ...");
                if (wh_lte_7s4->size_received >=2)
                {
                    if (Compare_RcvBuf_And_Procedure_Table(2, WH_LTE_Rcv_Buf, wh_lte_7s4->size_received) == 0)
                    {
                        HAL_UART_DMAStop(pWH_LTE_Uart );

                        err_con=0;
                        wh_lte_7s4->error=0;
                        wh_lte_7s4->receive_ok_flag=0;
                        wh_lte_7s4->size_received = 0;
                        memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                        wh_lte_7s4->timebase = osKernelGetTickCount();

                        wh_lte_7s4_at_set_workmode( &wh_lte_7s4,WH_LTE_7S4_CMD_WORK_MODE_NET );

                        __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                        HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;   
                        *state = MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL;                  
                    }
                }
                // if(wh_lte_7s4->receive_ok_flag==1)
                // {
                //     osTimerStart(wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
                //     osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_WORKMODE);
                //     wh_lte_7s4->error=0;
                //     // wh_lte_7s4->timebase =osKernelGetTickCount();
                //     // clock=time_clock_get_clock();
                //     // wh_lte_7s4->receive_ok_flag=0;
                //     wh_lte_7s4_at_set_sockasl( &wh_lte_7s4, WH_LTE_7S4_CMD_SOCKASL_LONG );
                //     *state = MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL;
                // }
                break;        
            case MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL:
                log_i(" INIT_STATE_SET_SOCKASL .。。 ");
                if (wh_lte_7s4->size_received >=2)
                {
                    if (Compare_RcvBuf_And_Procedure_Table(3, WH_LTE_Rcv_Buf, wh_lte_7s4->size_received) == 0)
                    {
                        HAL_UART_DMAStop(pWH_LTE_Uart );

                        err_con=0;
                        wh_lte_7s4->error=0;
                        wh_lte_7s4->receive_ok_flag=0;
                        wh_lte_7s4->size_received = 0;
                        memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                        wh_lte_7s4->timebase = osKernelGetTickCount();

                        wh_lte_7s4_at_set_socka_en(&wh_lte_7s4,WH_LTE_7S4_CMD_STATE_ON);

                        __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                        HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;   
                        *state = MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN;                  
                    }
                } 
                // if(wh_lte_7s4->receive_ok_flag==1)
                // {
                //     osTimerStart( wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
                //     osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_SOCKAEN);
                //     wh_lte_7s4->error=0;
                //     wh_lte_7s4_at_set_socka_en(&wh_lte_7s4,WH_LTE_7S4_CMD_STATE_ON);
                //     *state=MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN;
                // }
                break;        
            case MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN:
                if (wh_lte_7s4->size_received >=2)
                {
                    if (Compare_RcvBuf_And_Procedure_Table(4, WH_LTE_Rcv_Buf, wh_lte_7s4->size_received) == 0)
                    {
                        HAL_UART_DMAStop(pWH_LTE_Uart );

                        err_con=0;
                        wh_lte_7s4->error=0;
                        wh_lte_7s4->receive_ok_flag=0;
                        wh_lte_7s4->size_received = 0;
                        memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                        wh_lte_7s4->timebase = osKernelGetTickCount();

                        wh_lte_7s4_at_set_socka(&wh_lte_7s4,WH_LTE_7S4_CMD_PROTOCOL_TCP,wh_lte_7s4->server_ip_domain,strlen(wh_lte_7s4->server_ip_domain),wh_lte_7s4->server_port);

                        __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                        HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;   
                        *state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA;
                    }
                } 
                // if( wh_lte_7s4->receive_ok_flag == 1)
                // {
                //     wh_lte_7s4->error=0;
                //     // wh_lte_7s4->timebase =osKernelGetTickCount();
                //     // clock=time_clock_get_clock();
                //     wh_lte_7s4->receive_ok_flag = 0;
                //     wh_lte_7s4_at_set_socka(&wh_lte_7s4,WH_LTE_7S4_CMD_PROTOCOL_TCP,wh_lte_7s4->server_ip_domain,strlen(wh_lte_7s4->server_ip_domain),wh_lte_7s4->server_port);
                //     // wh_lte_7s4_at_set_socka(&wh_lte_7s4,WH_LTE_7S4_CMD_PROTOCOL_TCP,config.tcp.server_ipv4,strlen(config.tcp.server_ipv4),config.tcp.server_port);
                //     err_con=0;
                //     *state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA;
                // }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA:
                if (wh_lte_7s4->size_received >=2)
                {
                    if (Compare_RcvBuf_And_Procedure_Table(5, WH_LTE_Rcv_Buf, wh_lte_7s4->size_received) == 0)
                    {
                        HAL_UART_DMAStop(pWH_LTE_Uart );

                        err_con=0;
                        wh_lte_7s4->error=0;
                        wh_lte_7s4->receive_ok_flag=0;
                        wh_lte_7s4->size_received = 0;
                        memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                        wh_lte_7s4->timebase = osKernelGetTickCount();

                        wh_lte_7s4_at_get_cclk(&wh_lte_7s4);

                        __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                        HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;   
                        *state = MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME;
                    }
                } 
                // if( wh_lte_7s4->receive_ok_flag == 1 )
                // {
                //     osTimerStart( wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
                //     osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_GET_TIME );
                //     wh_lte_7s4->error=0;
                //     wh_lte_7s4_at_get_cclk(&wh_lte_7s4);
                //     *state=MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME;
                // }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME:
                if (wh_lte_7s4->size_received >=2)
                {
                    if (Compare_RcvBuf_And_Procedure_Table(6, WH_LTE_Rcv_Buf, wh_lte_7s4->size_received) == 0)
                    {
                        HAL_UART_DMAStop(pWH_LTE_Uart );

                        err_con=0;
                        wh_lte_7s4->error=0;
                        wh_lte_7s4->receive_ok_flag=0;
                        wh_lte_7s4->size_received = 0;
                        memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                        wh_lte_7s4->timebase = osKernelGetTickCount();

                        wh_lte_7s4_at_z(&wh_lte_7s4);

                        __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                        HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;   
                        *state = MODULE_WH_LTE_7S4_INIT_STATE_REBOOT;
                    }
                } 
                // if(wh_lte_7s4->receive_para_num!=0)
                // {
                //     log_d("Module WH-LTE-7S4 Receive Time");
                //     osTimerStart( wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
                //     osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_REBOOT );
                //     wh_lte_7s4->error=0;
                //     wh_lte_7s4_at_z(&wh_lte_7s4);
                //     *state=MODULE_WH_LTE_7S4_INIT_STATE_REBOOT;
                // }
                break;  
            case MODULE_WH_LTE_7S4_INIT_STATE_REBOOT:
                //  TODO
                // if(wh_let_7s4_get_work_state()==0)
                if( 0==0)
                {
                    log_i("Module WH-LTE-7S4 Init Succeed");
                    err_con=0;
                    *state=MODULE_WH_LTE_7S4_INIT_STATE_OK;
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_OK:
                // TODO: 切换传送模式  polling  - DMA
                break;

            case MODULE_WH_LTE_7S4_GET_TIME:
                log_i("pppp .. wh_lte_7s4->timeout_ms %d", wh_lte_7s4->timeout_ms);
                wh_lte_7s4->error=0;
                wh_lte_7s4->size_received = 0;
                memset(  WH_LTE_Rcv_Buf, 0, sizeof(WH_LTE_Rcv_Buf) );
                wh_lte_7s4->timebase = osKernelGetTickCount();

                wh_lte_7s4_at_get_cclk(&wh_lte_7s4);

                __HAL_UART_ENABLE_IT( pWH_LTE_Uart, UART_IT_IDLE );
                HAL_UART_Receive_DMA( pWH_LTE_Uart,  WH_LTE_Rcv_Buf, sizeof(WH_LTE_Rcv_Buf) )  ;
 
                *state = MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME;
                break;
            
            case MODULE_WH_LTE_7S4_INIT_STATE_ERROR:
                if(wh_lte_7s4->error!=0){
                    wh_lte_7s4->error=0;
                    log_e("Module WH-LTE-7S4 Init Error");
                }
                break;
            case MODULE_WH_LTE_7S4_INIT_STATE_STOP:
                break;
            default:
                break;
        }
        osDelay(80);
    }

}

void WH_LTE_Set_GPIO_For_TC_Cplt( WH_LTE_7S4_Typedef * wh_lte_7s4 )
{
//    UartHelper_Set_GPIO_For_Trans_Cplt(wh_lte_7s4->uarthelper );
}



int wh_lte_7s4_ppp(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    // WH_LTE_Transmit( wh_lte_7s4,"+++",3);
    WH_LTE_Transmit( wh_lte_7s4, (uint8_t*)"+++",3);
    return 0;
}
int wh_lte_7s4_a(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"a",1);
    return 0;
}
int wh_lte_7s4_at(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"AT\r",3);
    return 0;
}
int wh_lte_7s4_test(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size)
{
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"AT+",3);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)cmd,size);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"?\r\n",3);
    return 0;
}
int wh_lte_7s4_inquire(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size)
{
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"AT+",3);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)cmd,size);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"=?\r\n",4);
    return 0;
}
int wh_lte_7s4_run(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size)
{
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"AT+",3);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)cmd,size);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"\r\n",2);
    return 0;
}
int wh_lte_7s4_set(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t cmd_size,char *args,uint32_t args_size)
{
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"AT+",3);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)cmd, cmd_size);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"=",1);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)args,args_size);
    WH_LTE_Transmit( wh_lte_7s4,(uint8_t*)"\r\n",2);
    return 0;
}

int wh_lte_7s4_at_help(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"H",1);
}
int wh_lte_7s4_at_z(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"Z",1);
}
int wh_lte_7s4_at_reboot(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"REBOOT",6);
}
int wh_lte_7s4_at_get_echo(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_test(wh_lte_7s4,"E",1);
}
int wh_lte_7s4_at_set_echo(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state echo_state)
{
    return wh_lte_7s4_set(wh_lte_7s4,"E",1,(char*)wh_lte_7s4_cmd_state_table[echo_state],strlen(wh_lte_7s4_cmd_state_table[echo_state]));
}
int wh_lte_7s4_at_entm(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_test(wh_lte_7s4,"ENTM",4);
}
int wh_lte_7s4_at_set_workmode(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_work_mode work_mode)
{
    return wh_lte_7s4_set(wh_lte_7s4,"WKMOD",5,(char*)wh_lte_7s4_cmd_work_mode_table[work_mode],strlen(wh_lte_7s4_cmd_work_mode_table[work_mode]));
}
int wh_lte_7s4_at_get_workmode(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_test(wh_lte_7s4,"WKMOD",5);
}
int wh_lte_7s4_at_get_csq(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"CSQ",3);
}
int wh_lte_7s4_at_get_system_info(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SYSINFO",7);
}
int wh_lte_7s4_at_reload(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"RELD",4);
}
int wh_lte_7s4_at_clear(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"CLEAR",5);
}
int wh_lte_7s4_at_get_sn(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SN",2);
}
int wh_lte_7s4_at_get_iccid(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"ICCID",5);
}
int wh_lte_7s4_at_get_imei(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"IMEI",4);
}
int wh_lte_7s4_at_get_local_ip(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"LOCIP",5);
}
int wh_lte_7s4_at_get_uart(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"UART",4);
}
int wh_lte_7s4_at_set_uart(WH_LTE_7S4_Typedef *wh_lte_7s4,uint32_t buad,uint8_t data,uint8_t stop,enum wh_lte_7s4_cmd_uart_parity parity)
{
    int i=0;
    i+=convert_u2s(buad,&wh_lte_7s4->send_args_buf[i],64-i);
    wh_lte_7s4->send_args_buf[i++]=',';
    i+=convert_u2s(data,&wh_lte_7s4->send_args_buf[i],64-i);
    wh_lte_7s4->send_args_buf[i++]=',';
    i+=convert_u2s(stop,&wh_lte_7s4->send_args_buf[i],64-i);
    wh_lte_7s4->send_args_buf[i++]=',';
    strcpy(&wh_lte_7s4->send_args_buf[i],wh_lte_7s4_cmd_uart_parity_table[parity]);
    i+=strlen(wh_lte_7s4_cmd_uart_parity_table[parity]);
    wh_lte_7s4->send_args_buf[i++]=',';
    strcpy(&wh_lte_7s4->send_args_buf[i],"NFC");
    i+=3;
    return wh_lte_7s4_set(wh_lte_7s4,"UART",4,wh_lte_7s4->send_args_buf,i);
}
int wh_lte_7s4_at_get_uartft(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"UARTFT",6);
}
int wh_lte_7s4_at_set_uartft(WH_LTE_7S4_Typedef *wh_lte_7s4,uint32_t ft)
{
    int i=0;
    i+=convert_u2s(ft,&wh_lte_7s4->send_args_buf[i],64-i);
    return wh_lte_7s4_set(wh_lte_7s4,"UARTFT",6,wh_lte_7s4->send_args_buf,i);
}
int wh_lte_7s4_at_get_uartfl(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"UARTFL",6);
}
int wh_lte_7s4_at_set_uartfl(WH_LTE_7S4_Typedef *wh_lte_7s4,uint32_t len)
{
    int i=0;
    i+=convert_u2s(len,&wh_lte_7s4->send_args_buf[i],64-i);
    return wh_lte_7s4_set(wh_lte_7s4,"UARTFL",6,wh_lte_7s4->send_args_buf,i);
}
int wh_lte_7s4_at_get_socka(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKA",5);
}
int wh_lte_7s4_at_set_socka(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_protocol protocol,char *remote_ip,uint32_t remote_ip_size,uint16_t port)
{
    int i=0;
    memcpy(&wh_lte_7s4->send_args_buf[i],wh_lte_7s4_cmd_protocol_table[protocol],3);
    i+=3;
    wh_lte_7s4->send_args_buf[i++]=',';
    memcpy(&wh_lte_7s4->send_args_buf[i],remote_ip,remote_ip_size);
    i+=remote_ip_size;
    wh_lte_7s4->send_args_buf[i++]=',';
    i+=wh_lte_convert_u2s(port,&wh_lte_7s4->send_args_buf[i],64-i);
    return wh_lte_7s4_set(wh_lte_7s4,"SOCKA",5,wh_lte_7s4->send_args_buf,i);
}
int wh_lte_7s4_at_get_sockb(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKB",5);
}
int wh_lte_7s4_at_set_sockb(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_protocol protocol,char *remote_ip,uint32_t remote_ip_size,uint16_t port)
{
    int i=0;
    memcpy(&wh_lte_7s4->send_args_buf[i],wh_lte_7s4_cmd_protocol_table[protocol],3);
    i+=3;
    wh_lte_7s4->send_args_buf[i++]=',';
    memcpy(&wh_lte_7s4->send_args_buf[i],remote_ip,remote_ip_size);
    i+=remote_ip_size;
    wh_lte_7s4->send_args_buf[i++]=',';
    i+=wh_lte_convert_u2s(port,&wh_lte_7s4->send_args_buf[i],64-i);
    return wh_lte_7s4_set(wh_lte_7s4,"SOCKB",5,wh_lte_7s4->send_args_buf,i);
}
int wh_lte_7s4_at_get_socka_en(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKAEN",7);
}
int wh_lte_7s4_at_set_socka_en(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state state)
{
    return wh_lte_7s4_set(wh_lte_7s4,"SOCKAEN",7,(char*)wh_lte_7s4_cmd_state_table[state],strlen(wh_lte_7s4_cmd_state_table[state]));
}
int wh_lte_7s4_at_get_sockb_en(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKBEN",7);
}
int wh_lte_7s4_at_set_sockb_en(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state state)
{
    return wh_lte_7s4_set(wh_lte_7s4,"SOCKBEN",7,(char*)wh_lte_7s4_cmd_state_table[state],strlen(wh_lte_7s4_cmd_state_table[state]));
}
int wh_lte_7s4_at_get_sockasl(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKASL",7);
}
int wh_lte_7s4_at_set_sockasl(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_sockasl type)
{
    return wh_lte_7s4_set(wh_lte_7s4,"SOCKASL",7,(char*)wh_lte_7s4_cmd_sockasl_table[type],strlen(wh_lte_7s4_cmd_sockasl_table[type]));
}
int wh_lte_7s4_at_get_sockbsl(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKBSL",7);
}
int wh_lte_7s4_at_set_sockbsl(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_sockasl type)
{
    return wh_lte_7s4_set(wh_lte_7s4,"SOCKBSL",7,(char*)wh_lte_7s4_cmd_sockasl_table[type],strlen(wh_lte_7s4_cmd_sockasl_table[type]));
}
int wh_lte_7s4_at_get_sockalk(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKALK",7);
}
int wh_lte_7s4_at_get_sockblk(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKBLK",7);
}
int wh_lte_7s4_at_get_sdpen(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SDPEN",5);
}
int wh_lte_7s4_at_set_sdpen(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state new_state)
{
    return wh_lte_7s4_set(wh_lte_7s4,"SDPEN",5,(char*)wh_lte_7s4_cmd_state_table[new_state],strlen(wh_lte_7s4_cmd_state_table[new_state]));
}
int wh_lte_7s4_at_get_cclk(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"CCLK",4);
}

void WH_LTE_Set_Timeout( WH_LTE_7S4_Typedef * wh_lte_7s4,    uint16_t timeout_ms )
{
    wh_lte_7s4->timeout_ms =timeout_ms;
    wh_lte_7s4->timeout_enable = 1;
}

int WH_LTE_Get_Timeout( WH_LTE_7S4_Typedef * wh_lte_7s4 )
{
    if ((osKernelGetTickCount() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout_ms)
        return 0;
    return -1;
}

int WH_LTE_Transmit( WH_LTE_7S4_Typedef * wh_lte_7s4,  uint8_t * buf, uint16_t size )
{
    if (wh_lte_7s4->timeout_enable == 1)
    {
        wh_lte_7s4->timebase = osKernelGetTickCount();
    }
    return  HAL_UART_Transmit_DMA(pWH_LTE_Uart , buf, size);
    // return UartHelper_Transmit(wh_lte_7s4->uarthelper , buf, size);
}
int WH_LTE_Begin_Rcv( WH_LTE_7S4_Typedef * wh_lte_7s4,  uint8_t * buf, uint16_t size )
{
    return HAL_UART_Receive_DMA(pWH_LTE_Uart , buf, size);
    // return UartHelper_Begin_Rcv(wh_lte_7s4->uarthelper , buf, size);
}

int Compare_RcvBuf_And_Procedure_Table(int seq, uint8_t *buf, uint16_t size)
{
    log_w( "  compare   seq %d  ", seq  );
    int i = 0;
    int j = 0;
    int para_flag = 0;
    // int16_t len = sizeof(*wh_lte_7s4_procedure_rcv_table[seq]);
    // log_w( "  compare   len %d ", len);

    // 处理接收到数据， 定义每包不超过16byte, 依据接收类型处理
    if (wh_lte_7s4->state == MODULE_WH_LTE_7S4_INIT_STATE_OK )
    {
        /* 获取时间  需要调用外部函数 */
        memset(Back_Buf, 0, sizeof(Back_Buf) ) ;
        for (  i = 0; i < size; i++)
        {
            Back_Buf[i] ==buf[i];
        }
        return 0;
    }

    //  获取时间， 调用外部设置时间函数
    if (wh_lte_7s4->state == MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME )
    {
        /* 获取时间  需要调用外部函数 */
        memset(Time_Buf, 0, sizeof(Time_Buf) ) ;
        for (  i = 0; i < size; i++)
        {
            Time_Buf[i] == buf[i];
        }
        // 判断是否包含 +CCLK:

        // TODO 外部设置时间函数
        return 0;
    }

    // 处理设置参数
    for (  i = 0; i < size; i++)
    {
        if( buf[i]=='\r' ) continue;
        if( buf[i]=='\n' ) continue;
        if( buf[i]!='\r' && buf[i]!='\n' )
        if(buf[i] != wh_lte_7s4_procedure_rcv_table[seq][j]) return -1;
        j++;
    }
    // log_w( "  compare  pass " );
    
    return 0;
}
void wh_lte_7s4_receive_callback( WH_LTE_7S4_Typedef *wh_lte_7s4, uint32_t size )
{
    // uint32_t evt_flags = osEventFlagsGet(wh_lteEventHandle ) & MODULE_WH_LTE_EVENT_READY;
    // uint32_t i;
    // uint32_t j;
    // if ( evt_flags != 0 )
    // {
    //     // while ( i < size )
    //     // {
    //     //     if ( wh_lte_7s4->uarthelper->receive_buf != "\r") 
    //     //     {
    //     //         i++;
    //     //         continue;
    //     //     }
    //     //     if ( wh_lte_7s4->uarthelper->receive_buf != "\n")
    //     //     {
    //     //         i++;
    //     //         continue;                
    //     //     }
    //     //     if ( j >= sizeof(wh_lte_7s4_procedure_rcv_table[evt_flags]) ) 
    //     //     {
    //     //         break;  /* 如果需要提取数据， 在任务处理获取 */
    //     //     }
    //     //     if ( wh_lte_7s4_procedure_rcv_table[evt_flags-1][j] !=wh_lte_7s4->uarthelper->receive_buf[i] )
    //     //     {
    //     //         return;
    //     //     }

    //     //     i++;
    //     //     j++;
    //     // }
    //     osEventFlagsClear(wh_lteEventHandle, evt_flags);
    // }
    return ;
}

void wh_lte_7s4_receive_unpack(WH_LTE_7S4_Typedef *wh_lte_7s4, char *buf, uint32_t size)
{
    int state;
    int i=0;
    int j=0;
    while(i<size){
        state=0;
        uint32_t size_tmp=size+wh_lte_7s4->receive_size>1024?1024-wh_lte_7s4->receive_size:size;
        memcpy(&wh_lte_7s4->receive_buf[wh_lte_7s4->receive_size],buf,size_tmp);
        wh_lte_7s4->receive_size+=size_tmp;
        while(i<size){
            switch(state){
            case 0:
                if(buf[i]=='a'){
                    wh_lte_7s4->receive_a_flag=1;
                    state=0;
                    i++;
                    continue;
                }
                if(buf[i]=='\r'){
                    state=1;
                    break;
                }
                if(buf[i]=='+'){
                    state=9;
                    break;
                }
                if(buf[i]=='A'){
                    state=11;
                    break;
                }
                break;
            case 1:
                if(buf[i]=='\n'){
                    state=2;
                    break;
                }
                i--;
                state=0;
                break;
            case 2:
                if(buf[i]=='O'){
                    state=3;
                    break;
                }
                if(buf[i]=='+'){
                    state=6;
                    break;
                }
                i--;
                state=0;
                break;
            case 3:
                if(buf[i]=='K'){
                    state=4;
                    break;
                }
                i--;
                state=0;
                break;
            case 4:
                if(buf[i]=='\r'){
                    state=5;
                    break;
                }
                i--;
                state=0;
                break;
            case 5:
                if(buf[i]=='\n'){
                    wh_lte_7s4->receive_ok_flag=1;
                    state=0;
                    i++;
                    continue;
                }
                i--;
                state=0;
                break;
            case 6:
                if(buf[i]==':'){
                    state=7;
                    break;
                }
                break;
            case 7:
                if(buf[i]==','){
                    j=0;
                    wh_lte_7s4->receive_para[wh_lte_7s4->receive_para_num][j++]='\0';
                    wh_lte_7s4->receive_para_num++;
                    break;
                }
                if(buf[i]=='\r')
                {
                    wh_lte_7s4->receive_para_num++;
                    state=8;
                    break;
                }
                if(j>=32 || wh_lte_7s4->receive_para_num>=8){
                    wh_lte_7s4->receive_para_num=0;
                    state=0;
                    break;
                }
                wh_lte_7s4->receive_para[wh_lte_7s4->receive_para_num][j++]=buf[i];
                break;
            case 8:
                if(buf[i]=='\n')
                {
                    state=0;
                    break;
                }
                wh_lte_7s4->receive_para_num=0;
                break;
            case 9:
                if(buf[i]=='o'){
                    state=10;
                    break;
                }
                i--;
                state=0;
                break;
            case 10:
                if(buf[i]=='k'){
                    wh_lte_7s4->receive_ok_flag=1;
                    state=0;
                    i++;
                    continue;
                }
                i--;
                state=0;
                break;
            case 11:
                if(buf[i]=='T'){
                    state=2;
                    break;
                }
                i--;
                state=0;
                break;
            }
            i++;
        }
    }
    
}

/**
 * @brief 将unsigned数据转换为字符串
 * 
 * @param integer 要转换的数据
 * @param str 字符串指针
 * @param length 字符串长度
 * @return unsigned 转换的字符串长度
 */
unsigned wh_lte_convert_u2s(unsigned integer,char *str,unsigned length)
{
    unsigned i;
    unsigned j=length-1;
    while(j<length){
        str[j]=integer%10+'0';
        integer/=10;
        if(integer==0)
            break;
        j--;
    }
    if(integer!=0)
        return 0;
    for(i=0;j<length;i++,j++){
        str[i]=str[j];
    }
    return i;
}



    // switch(*state)
    // {
    //     case MODULE_WH_LTE_7S4_INIT_STATE_WAIT:
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_START:
    //         osTimerStart(wh_lteTimerHandle, 1000 );
    //         osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_PPP);
    //         wh_lte_7s4->error=0;
    //         // wh_lte_7s4->timebase =osKernelGetTickCount();
    //         // clock=time_clock_get_clock();
    //         // wh_lte_7s4->receive_a_flag=0;
    //         wh_lte_7s4_ppp(&wh_lte_7s4);
    //         *state = MODULE_WH_LTE_7S4_INIT_STATE_PPP;
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_PPP:
    //         if( wh_lte_7s4->receive_a_flag==1 )
    //         {
    //             osTimerStart(wh_lteTimerHandle, 1000 );
    //             osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_A);
    //             wh_lte_7s4->error=0;
    //             // clock=time_clock_get_clock();A
    //             wh_lte_7s4->receive_a_flag=0;
    //             wh_lte_7s4->receive_ok_flag=0;
    //             wh_lte_7s4_a(&wh_lte_7s4);
    //             *state=MODULE_WH_LTE_7S4_INIT_STATE_A;
    //         }
    //         // if(time_clock_get_clock_offset_ms(clock)>=1000){
    //         //     // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //         //     log_w("Module WH-LTE-7S4 AT Mode Failed %d,Retrying...",err_con);
    //         //     err_con++;
    //         //     if(err_con>60){
    //         //         *state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
    //         //         break;
    //         //     }
    //         //     *state=MODULE_WH_LTE_7S4_INIT_STATE_START;
    //         // }
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_A:
    //         if( wh_lte_7s4->receive_ok_flag==1 )
    //         {
    //             osTimerStart(wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
    //             osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_WORKMODE);
    //             wh_lte_7s4->error=0;
    //             // wh_lte_7s4->timebase =osKernelGetTickCount();
    //             // // clock=time_clock_get_clock();
    //             // wh_lte_7s4->receive_ok_flag=0;
    //             wh_lte_7s4_at_set_workmode(&wh_lte_7s4,WH_LTE_7S4_CMD_WORK_MODE_NET);
    //             *state=MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE;
    //         }
    //         // if(time_clock_get_clock_offset_ms(clock)>=1000){
    //         //     // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //         //     log_w("Module WH-LTE-7S4 AT Mode Failed %d,Retrying...",err_con);
    //         //     err_con++;
    //         //     if(err_con>30){
    //         //         *state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
    //         //         break;
    //         //     }
    //         //     *state=MODULE_WH_LTE_7S4_INIT_STATE_START;
    //         // }
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE:
    //         if(wh_lte_7s4->receive_ok_flag==1)
    //         {
    //             osTimerStart(wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
    //             osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_WORKMODE);
    //             wh_lte_7s4->error=0;
    //             // wh_lte_7s4->timebase =osKernelGetTickCount();
    //             // clock=time_clock_get_clock();
    //             // wh_lte_7s4->receive_ok_flag=0;
    //             wh_lte_7s4_at_set_sockasl( &wh_lte_7s4, WH_LTE_7S4_CMD_SOCKASL_LONG );
    //             *state = MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL;
    //         }
    //         // if(time_clock_get_clock_offset_ms(clock)>=1000){
    //         //     // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //         //     log_w("Module WH-LTE-7S4 Set Work Mode Failed %d,Retrying...",err_con);
    //         //     err_con++;
    //         //     if(err_con>30){
    //         //         *state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
    //         //         break;
    //         //     }
    //         //     clock=time_clock_get_clock();
    //         //     wh_lte_7s4_at_set_workmode(&wh_lte_7s4,WH_LTE_7S4_CMD_WORK_MODE_NET);
    //         //     *state=MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE;
    //         // }
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL:
    //         if(wh_lte_7s4->receive_ok_flag==1)
    //         {
    //             osTimerStart( wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
    //             osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_SOCKAEN);
    //             wh_lte_7s4->error=0;
    //             // wh_lte_7s4->timebase =osKernelGetTickCount();
    //             // clock=time_clock_get_clock();
    //             // wh_lte_7s4->receive_ok_flag=0;
    //             wh_lte_7s4_at_set_socka_en(&wh_lte_7s4,WH_LTE_7S4_CMD_STATE_ON);
    //               *state=MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN;
    //         }
    //     //     if(time_clock_get_clock_offset_ms(clock)>=1000){
    //     //         // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //     //         log_w("Module WH-LTE-7S4 Set Socket ASL Failed %d,Retrying...",err_con);
    //     //         err_con++;
    //     //         if(err_con>30){
    //     //             *state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
    //     //             break;
    //     //         }
    //     // wh_lte_7s4->timebase =osKernelGetTickCount();
    //     //         // clock=time_clock_get_clock();
    //     //         wh_lte_7s4_at_set_sockasl(&wh_lte_7s4,WH_LTE_7S4_CMD_SOCKASL_LONG);
    //     //         *state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL;
    //     //     }
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN:
    //         if(wh_lte_7s4->receive_ok_flag==1)
    //         {
    //             osTimerStart( wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
    //             osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_SOCKA);
    //             wh_lte_7s4->error=0;
    //             // wh_lte_7s4->timebase =osKernelGetTickCount();
    //             // clock=time_clock_get_clock();
    //             wh_lte_7s4->receive_ok_flag=0;
    //             wh_lte_7s4_at_set_socka(&wh_lte_7s4,WH_LTE_7S4_CMD_PROTOCOL_TCP,config.tcp.server_ipv4,strlen(config.tcp.server_ipv4),config.tcp.server_port);
    //             err_con=0;
    //             *state=MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA;
    //         }
    //         // TODO
    //         // if(time_clock_get_clock_offset_ms(clock)>=1000){
    //         //     // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //         //     log_w("Module WH-LTE-7S4 Enable Socket Failed %d,Retrying...",err_con);
    //         //     err_con++;
    //         //     if(err_con>30){
    //         //         *state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
    //         //         break;
    //         //     }
    //             // wh_lte_7s4->timebase =osKernelGetTickCount();
    //             // // clock=time_clock_get_clock();
    //             // wh_lte_7s4_at_set_socka_en(&wh_lte_7s4,WH_LTE_7S4_CMD_STATE_ON);
    //             // *state=MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN;
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA:
    //         if(wh_lte_7s4->receive_ok_flag==1)
    //         {
    //             osTimerStart( wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
    //             osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_GET_TIME );
    //             wh_lte_7s4->error=0;
    //             // wh_lte_7s4->timebase =osKernelGetTickCount();
    //             // clock=time_clock_get_clock();
    //             // time_clock_get_time(&time_set_base);
    //             // wh_lte_7s4->receive_para_num=0;
    //             wh_lte_7s4_at_get_cclk(&wh_lte_7s4);
    //             // operating_state.err_code&=(~ERROR_CODE_MASK_WH_LTE_7S4);
    //             *state=MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME;
    //         }
    //         //  TODO

    //         // if(time_clock_get_clock_offset_ms(clock)>=1000){
    //         //     // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //         //     log_w("Module WH-LTE-7S4 Set Socket Failed %d,Retrying...",err_con);
    //         //     err_con++;
    //         //     if(err_con>30){
    //         //         *state=MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
    //         //         break;
    //         //     }
    //         //     clock=time_clock_get_clock();
    //         //     wh_lte_7s4_at_set_socka(&wh_lte_7s4,WH_LTE_7S4_CMD_PROTOCOL_TCP,config.tcp.server_ipv4,strlen(config.tcp.server_ipv4),config.tcp.server_port);
    //         // }
    //         break;
    //     case MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME:
    //         if(wh_lte_7s4->receive_para_num!=0)
    //         {
    //             log_d("Module WH-LTE-7S4 Receive Time");
    //             osTimerStart( wh_lteTimerHandle, wh_lte_7s4->timeout_ms );
    //             osEventFlagsSet(wh_lteEventHandle, MODULE_WH_LTE_EVENT_READY|MODULE_WH_LTE_EVENT_REBOOT );
    //             wh_lte_7s4->error=0;
    //             //  TODO
    //             // if(set_time(&time_set_base,wh_lte_7s4->receive_para[0])!=0){
    //             //     wh_lte_7s4->receive_para_num=0;
    //             //     break;
    //             // }
    //             // wh_lte_7s4->timebase =osKernelGetTickCount();
    //             // clock=time_clock_get_clock();
    //             // wh_lte_7s4->receive_para_num=0;
    //             wh_lte_7s4_at_z(&wh_lte_7s4);
    //             // operating_state.err_code&=(~ERROR_CODE_MASK_WH_LTE_7S4);
    //             // TODO
    //             // operating_state.flag_write_back=1;
    //             *state=MODULE_WH_LTE_7S4_INIT_STATE_REBOOT;
    //         }

    //         //  TODO
    //     //     if(time_clock_get_clock_offset_ms(clock)>=1000){
    //     //         // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //     //         log_w("Module WH-LTE-7S4 Get Time Failed %d,Retrying...",err_con);
    //     //         err_con++;
    //     //         if(err_con>120){
    //     // wh_lte_7s4->timebase =osKernelGetTickCount();
    //     //             // wh_lte_7s4_at_z(&wh_lte_7s4);
    //     //             log_e("Module WH-LTE-7S4 Get Time Error");
    //     //             err_con=0;
    //     //             // operating_state.err_code|=ERROR_CODE_MASK_WH_LTE_7S4;
    //     //             *state=MODULE_WH_LTE_7S4_INIT_STATE_REBOOT;
    //     //             break;
    //     //         }
    //     // wh_lte_7s4->timebase =osKernelGetTickCount();
    //     //         // time_clock_get_time(&time_set_base);
    //     //         // clock=time_clock_get_clock();
    //     //         wh_lte_7s4->receive_para_num=0;
    //     //         wh_lte_7s4_at_get_cclk(&wh_lte_7s4);
    //     //     }
    //         break;
    //     // case MODULE_WH_LTE_7S4_INIT_STATE_REBOOT:
    //     //     //  TODO
    //     //     if(wh_let_7s4_get_work_state()==0)
    //     //     {
    //     //         log_i("Module WH-LTE-7S4 Init Succeed");
    //     //         //  TODO
    //     //         // operating_state.flag_error[ERROR_CODE_4G]=0;
    //     //         // operating_state.net_state=1;
    //     //         err_con=0;
    //     //         *state=MODULE_WH_LTE_7S4_INIT_STATE_OK;
    //     //     }

    //     //     //  TODO 算时间差
    //     // //     if(time_clock_get_clock_offset_ms(clock)>=20000){
    //     // // wh_lte_7s4->timebase =osKernelGetTickCount();
    //     // //         // clock=time_clock_get_clock();
    //     // //         log_w("Module WH-LTE-7S4 Connect Failed,Retrying...");
    //     // //     }
    //     //     break;
    //     // case MODULE_WH_LTE_7S4_INIT_STATE_OK:
    //     //     break;
    //     // case MODULE_WH_LTE_7S4_INIT_STATE_ERROR:
    //     //     if(err_con!=0){
    //     //         err_con=0;
    //     //         // TODO
    //     //         // operating_state.flag_error[ERROR_CODE_4G]=1;
    //     //         log_e("Module WH-LTE-7S4 Init Error");
    //     //     }
    //     //     break;
    //     // case MODULE_WH_LTE_7S4_INIT_STATE_STOP:
    //     //     break;
    //     default:
    //         *state=MODULE_WH_LTE_7S4_INIT_STATE_WAIT;
    //         break;
    //     }
    // }
    


    // osEventFlagsId_t wh_lteEventHandle;
// const osEventFlagsAttr_t wh_lteEvent_attributes = {
//   .name = "wh_lteEvent"
// };

// //  处理过程中的超时
// osTimerId_t wh_lteTimerHandle;  
// const osTimerAttr_t wh_lteTimer_attributes = {
//     .name = "wh_ltetTimer"
// };
// __weak void wh_lteTimer_Callback(void *argument)
// {
//     log_w("Module WH-LTE-7S4 AT Mode Failed %d,Retrying...", wh_lte_7s4->error);
//     wh_lte_7s4->error++;
//     // if(wh_lte_7s4->error>60){
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_ERROR;
//     //     return ;
//     // }
//     // // TODO: 状态切换初始状态
//     // uint32_t evt_flags = osEventFlagsGet(wh_lteEventHandle );
//     // if (evt_flags & MODULE_WH_LTE_EVENT_PPP != 0 )
//     // {
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_START;
//     //     return ;
//     // }
//     // if (evt_flags & MODULE_WH_LTE_EVENT_A != 0 )
//     // {
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_START;
//     //     return ;
//     // }

//     // if (evt_flags & MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE != 0 )
//     // {
//     //     osTimerStop( wh_lteTimerHandle );
//     //     osTimerStart( wh_lteTimerHandle, 1000 );
//     //     wh_lte_7s4_at_set_workmode(&wh_lte_7s4,WH_LTE_7S4_CMD_WORK_MODE_NET);
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_START;
//     //     return ;
//     // }

//     // if (evt_flags & MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL != 0 )
//     // {
//     //     osTimerStop( wh_lteTimerHandle );
//     //     osTimerStart( wh_lteTimerHandle, 1000 );
//     //     wh_lte_7s4_at_set_sockasl( &wh_lte_7s4, WH_LTE_7S4_CMD_SOCKASL_LONG );
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL;
//     //     return ;
//     // }

//     // if (evt_flags & MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN != 0 )
//     // {
//     //     osTimerStop( wh_lteTimerHandle );
//     //     osTimerStart( wh_lteTimerHandle, 1000 );
//     //     wh_lte_7s4_at_set_socka_en(&wh_lte_7s4,WH_LTE_7S4_CMD_STATE_ON);
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN;
//     //     return ;
//     // }

//     // if (evt_flags & MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA != 0 )
//     // {
//     //     osTimerStop( wh_lteTimerHandle );
//     //     osTimerStart( wh_lteTimerHandle, 1000 );
//     //     // wh_lte_7s4_at_set_socka(&wh_lte_7s4,WH_LTE_7S4_CMD_PROTOCOL_TCP,config.tcp.server_ipv4,strlen(config.tcp.server_ipv4),config.tcp.server_port);
//     //     wh_lte_7s4_at_set_socka(&wh_lte_7s4,WH_LTE_7S4_CMD_PROTOCOL_TCP,wh_lte_7s4->server_ip_domain,strlen(wh_lte_7s4->server_ip_domain),wh_lte_7s4->server_port);
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA;
//     //     return ;
//     // }

//     // if (evt_flags & MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME != 0 )
//     // {
//     //     osTimerStop( wh_lteTimerHandle );
//     //     osTimerStart( wh_lteTimerHandle, 1000 );
//     //     wh_lte_7s4_at_get_cclk(&wh_lte_7s4);
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME;
//     //     return ;
//     // }

//     // if (evt_flags & MODULE_WH_LTE_7S4_INIT_STATE_REBOOT != 0 )
//     // {
//     //     osTimerStop( wh_lteTimerHandle );
//     //     osTimerStart( wh_lteTimerHandle, 1000 );
//     //     wh_lte_7s4_at_z(&wh_lte_7s4);
//     //     wh_lte_7s4->state = MODULE_WH_LTE_7S4_INIT_STATE_REBOOT;
//     //     return ;
//     // }

// }
