/**
 * @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 "wh_lte_7s4_v2.h"
// #include "convert.h"
#include "string.h"
#include "usart.h"

WH_LTE_7S4_Typedef *wh_lte_7s4;


void wh_lte_7s4_port( )
{
    wh_lte_7s4 = wh_lte_7s4_init();
    // Oscar_Set_myuart( wh_lte_7s4, NULL );
    // Oscar_Set_Timeout( oscar, 1000 );
    // Oscar_Set_Sendbuf( oscar, Oscar_sendbuf, sizeof(Oscar_sendbuf) );
    // Oscar_Set_Rcvbuf( oscar, Oscar_rcvbuf, sizeof(Oscar_rcvbuf) );

    wh_lte_7s4->myuart =   Myuart_Init();
    // MYUART_TypeDef *myuart_handle = Myuart_Init();
    Myuart_Set_Huart( wh_lte_7s4->myuart, &huart3);
    Myuart_Set_Sel_GPIO_Pin( wh_lte_7s4->myuart, SEL_232_485_GPIO_Port, SEL_232_485_Pin );
    Myuart_Set_DE485_GPIO_Pin( wh_lte_7s4->myuart, DE_485_GPIO_Port, DE_485_Pin );
    Myuart_Set_Dplx_GPIO_Pin( wh_lte_7s4->myuart, DUPLEX_GPIO_Port, DUPLEX_Pin );
    log_i( "myuart->sel : %d   interface  %d ",wh_lte_7s4->myuart->sel , wh_lte_7s4->myuart->interface_type );
    Myuart_Set_232( wh_lte_7s4->myuart );

    /* DMA 模式*/
    Myuart_Setup_Transmod( wh_lte_7s4->myuart, 2);

    /* ppp */
    wh_lte_7s4_install( wh_lte_7s4);

}

/*  定义要发送的命令，及返回值长度不固定  ?????? */
void wh_lte_7s4_install_task( )
{
    /* Polling 模式*/
    Myuart_Setup_Transmod( wh_lte_7s4->myuart, 2);
    wh_lte_7s4->step = 0;

    /* 超时  回退上一步状态
    ppp             a      
    a               +ok   
    workmode        AT+WKMOD=NET\r\n (网络透传模式)   OK\r\n
    sockasl         AT+SOCKASL=LONG\r\n  SocketA 设置为长连接   OK\r\n
    sockeaen        AT+SOCKAEN=ON\r\n SocketA 使能      OK\r\n
    socka           AT+SOCKA=TCP,test.usr.cn,2317 设置 SocketA 的远程 IP 和端口  OK\r\n
    reboot          AT+Z\r\n 重启模块，参数生效  OK\r\n
    可选:
    gettime         AT+CCLK\r\n 
    apn
    */

    wh_lte_7s4_ppp( wh_lte_7s4);
    __wh_lte_7s4_receive( wh_lte_7s4, wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;

    for ( ; ; )
    {  
        /*  ppp  */
        if ( wh_lte_7s4->step == 0 )
        {
            wh_lte_7s4_ppp( wh_lte_7s4);
            __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
            wh_lte_7s4->timebase = HAL_GetTick();
            wh_lte_7s4->step++;
            continue;
        };

        /*  ppp -> a  */
        if ( wh_lte_7s4->step == 1 &&   wh_lte_7s4->receive_a_flag == 0 )
        {
            /* 未收到a , 是否超时 */
            if ( (HAL_GetTick() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout)
            {
                // wh_lte_7s4_ppp( wh_lte_7s4 ,wh_lte_7s4);
                // __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
                wh_lte_7s4->step == 0;
                wh_lte_7s4->timebase = HAL_GetTick();
            }
            continue;
        }
        if ( wh_lte_7s4->step == 1 &&  wh_lte_7s4->receive_a_flag == 1)
        {
            /* 已经接收到a */
            wh_lte_7s4_a( wh_lte_7s4 );
            __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
            wh_lte_7s4->receive_a_flag == 0;
            wh_lte_7s4->step++;
            continue;
        }

        /*  +ok -> workmode  */
        if ( wh_lte_7s4->step == 2 &&  wh_lte_7s4->receive_ok_flag == 0 )
        {
            /* 未收到+ok  , 是否超时 */
            if ( (HAL_GetTick() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout)
            {
                /* 超时，从ppp 开始*/
                // wh_lte_7s4_ppp( wh_lte_7s4 );
                // __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
                wh_lte_7s4->step == 0;
                wh_lte_7s4->timebase = HAL_GetTick();
            }
            continue;
        }
        if ( wh_lte_7s4->step == 2 &&  wh_lte_7s4->receive_ok_flag == 1)
        {
            /* 已经接收到+ok */
            wh_lte_7s4_at_set_workmode(wh_lte_7s4 ,WH_LTE_7S4_CMD_WORK_MODE_NET);
            __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
            wh_lte_7s4->step++;
            continue;
        }

        /*  OK -> Sockasl set long  */
        if ( wh_lte_7s4->step == 3 &&  wh_lte_7s4->receive_ok_flag == 0 )
        {
            /* 未收到OK, 是否超时  */
            if ( (HAL_GetTick() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout)
            {
                // wh_lte_7s4_a( wh_lte_7s4 );
                wh_lte_7s4_at_set_workmode(wh_lte_7s4 ,WH_LTE_7S4_CMD_WORK_MODE_NET);
                __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
                wh_lte_7s4->timebase = HAL_GetTick();
            }
            continue;
        }
        if ( wh_lte_7s4->step == 3 &&  wh_lte_7s4->receive_ok_flag == 1)
        {
            /* 已经接收到 OK */
            wh_lte_7s4_at_set_sockasl( wh_lte_7s4 ,WH_LTE_7S4_CMD_SOCKASL_LONG );
            __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
            wh_lte_7s4->step++;
            continue;
        }

        /*  OK -> SockaEN , socketa enable  */
        if ( wh_lte_7s4->step == 4 &&  wh_lte_7s4->receive_ok_flag == 0 )
        {
            /* 未收到OK, 是否超时  */
            if ( (HAL_GetTick() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout)
            {
                wh_lte_7s4_at_set_workmode(wh_lte_7s4 ,WH_LTE_7S4_CMD_WORK_MODE_NET);
                __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
                wh_lte_7s4->timebase = HAL_GetTick();
            }
            continue;
        }
        if ( wh_lte_7s4->step == 4 &&  wh_lte_7s4->receive_ok_flag == 1)
        {
            /* 已经接收到 OK */
            wh_lte_7s4_at_set_socka_en(wh_lte_7s4 ,WH_LTE_7S4_CMD_STATE_ON);
            __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
            wh_lte_7s4->step++;
            continue;
        }

        /*  OK -> Socka ip port , socketa enable  */
        if ( wh_lte_7s4->step == 5 &&  wh_lte_7s4->receive_ok_flag == 0 )
        {
            /* 未收到OK, 是否超时  */
            if ( (HAL_GetTick() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout)
            {
                wh_lte_7s4_at_set_socka_en(wh_lte_7s4 ,WH_LTE_7S4_CMD_STATE_ON);
                __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
                wh_lte_7s4->timebase = HAL_GetTick();
            }
            continue;
        }
        if ( wh_lte_7s4->step == 5 &&  wh_lte_7s4->receive_ok_flag == 1)
        {
            /* 已经接收到 OK */

            // wh_lte_7s4_at_set_socka(&wh_lte_7s4_buf,WH_LTE_7S4_CMD_PROTOCOL_TCP,buf,len,config.tcp.server_port);
            __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
            wh_lte_7s4->step++;
            continue;
        }

        /*  OK -> other   APN  CCLk .... wh_lte_7s4_at_get_cclk(); */
        /*  OK -> other   APN  CCLk ....  */

        /*  OK -> Socka reboot z , socketa enable  */
        if ( wh_lte_7s4->step == 6 &&  wh_lte_7s4->receive_ok_flag == 0 )
        {
            /* 未收到OK, 是否超时  */
            if ( (HAL_GetTick() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout)
            {

                // wh_lte_7s4_at_set_socka(&wh_lte_7s4_buf,WH_LTE_7S4_CMD_PROTOCOL_TCP,buf,len,config.tcp.server_port);
                __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
                wh_lte_7s4->timebase = HAL_GetTick();
            }
            continue;
        }
        if ( wh_lte_7s4->step == 6 &&  wh_lte_7s4->receive_ok_flag == 1)
        {
            /* 已经接收到 OK */
            wh_lte_7s4_at_z(wh_lte_7s4);
            __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
            wh_lte_7s4->step++;
            continue;
        }

        /*  OK -> Socka reboot z , socketa enable  */
        if ( wh_lte_7s4->step == 6 &&  wh_lte_7s4->receive_ok_flag == 0 )
        {
            /* 未收到OK, 是否超时  */
            if ( (HAL_GetTick() - wh_lte_7s4->timebase) > wh_lte_7s4->timeout)
            {
                wh_lte_7s4_at_z(wh_lte_7s4);
                __wh_lte_7s4_receive( wh_lte_7s4 ,wh_lte_7s4->receive_buf, sizeof(wh_lte_7s4->receive_buf) ) ;
                wh_lte_7s4->timebase = HAL_GetTick();
            }
            continue;
        }
        if ( wh_lte_7s4->step == 7 &&  wh_lte_7s4->receive_ok_flag == 1)
        {
            /* 已经接收到 OK */
            wh_lte_7s4->step++;
            break;
        }

        /*  OK -> error  */
        if ( wh_lte_7s4->receive_err_code == 1 )
        {
            wh_lte_7s4->step = 0;
            wh_lte_7s4->receive_ok_flag = 0;
            break;
        }
        osDelay(20);
    }
}

/*   发送字节数 >0  发送 */
void wh_lte_7s4_task( )
{
    Myuart_Setup_Transmod( wh_lte_7s4->myuart, 2 );   /* 2  DMA */
    for ( ; ; )
    {  
        osDelay(2000);
    }
}

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"};

WH_LTE_7S4_Typedef *wh_lte_7s4_init( )
{
    WH_LTE_7S4_Typedef *Handle = (WH_LTE_7S4_Typedef *)malloc(sizeof(WH_LTE_7S4_Typedef));
    if (Handle == NULL)
    {
        return NULL;
    }
    Handle->receive_a_flag=0;
    Handle->receive_err_code=0;
    Handle->receive_ok_flag=0;
    Handle->receive_para_num=0;
    return Handle;
}

int __wh_lte_7s4_send(WH_LTE_7S4_Typedef *wh_lte_7s4, uint8_t *buf, uint16_t size)
{
    return Myuart_Send( wh_lte_7s4->myuart, buf, size);
    // return 0;
}

int __wh_lte_7s4_receive(WH_LTE_7S4_Typedef *wh_lte_7s4, uint8_t *buf, uint16_t size)
{
    return Myuart_Start_Rcv( wh_lte_7s4->myuart, buf, size);
    // return 0;
}

void  wh_lte_7s4_receive_callback( WH_LTE_7S4_Typedef *wh_lte_7s4 )
{
    /*接收回调 判断 a */
    if( wh_lte_7s4->receive_buf[0] == 'a' )
    {
        wh_lte_7s4->receive_a_flag = 1;
    }
    if( wh_lte_7s4->receive_buf[0] == '+' )
    {
        if( wh_lte_7s4->receive_buf[1] == 'o' &&   wh_lte_7s4->receive_buf[2] == 'k' )
            wh_lte_7s4->receive_ok_flag = 1;
    }
    if( wh_lte_7s4->receive_buf[0] == 'O' )
    {
        if( wh_lte_7s4->receive_buf[1] == 'K' )
            wh_lte_7s4->receive_ok_flag = 1;
    }
    // return Myuart_Start_Rcv( wh_lte_7s4->myuart, buf, size);
    // return 0;
}

void  wh_lte_7s4_receive_error_callback(WH_LTE_7S4_Typedef *wh_lte_7s4 )
{
    /*接收发生错误回调*/
    wh_lte_7s4->receive_err_code = 1;
}

int wh_lte_7s4_ppp(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    __wh_lte_7s4_send(wh_lte_7s4,"+++",3);
    return 0;
}
int wh_lte_7s4_a(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    __wh_lte_7s4_send(wh_lte_7s4,"a",1);
    return 0;
}
int wh_lte_7s4_at(WH_LTE_7S4_Typedef *wh_lte_7s4)
{
    __wh_lte_7s4_send(wh_lte_7s4,"AT\r",3);
    return 0;
}
int wh_lte_7s4_test(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size)
{
    __wh_lte_7s4_send(wh_lte_7s4,"AT+",3);
    __wh_lte_7s4_send(wh_lte_7s4,cmd,size);
    __wh_lte_7s4_send(wh_lte_7s4,"?\r\n",3);
    return 0;
}
int wh_lte_7s4_inquire(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size)
{
    __wh_lte_7s4_send(wh_lte_7s4,"AT+",3);
    __wh_lte_7s4_send(wh_lte_7s4,cmd,size);
    __wh_lte_7s4_send(wh_lte_7s4,"=?\r\n",4);
    return 0;
}
int wh_lte_7s4_run(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size)
{
    __wh_lte_7s4_send(wh_lte_7s4,"AT+",3);
    __wh_lte_7s4_send(wh_lte_7s4,cmd,size);
    __wh_lte_7s4_send(wh_lte_7s4,"\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_7s4_send(wh_lte_7s4,"AT+",3);
    __wh_lte_7s4_send(wh_lte_7s4,cmd,cmd_size);
    __wh_lte_7s4_send(wh_lte_7s4,"=",1);
    __wh_lte_7s4_send(wh_lte_7s4,args,args_size);
    __wh_lte_7s4_send(wh_lte_7s4,"\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 baud,uint8_t data,uint8_t stop,enum wh_lte_7s4_cmd_uart_parity parity)
{
    int i=0;
    i+=convert_u_2_s(baud,&wh_lte_7s4->send_args_buf[i],64-i);
    wh_lte_7s4->send_args_buf[i++]=',';
    i+=convert_u_2_s(data,&wh_lte_7s4->send_args_buf[i],64-i);
    wh_lte_7s4->send_args_buf[i++]=',';
    i+=convert_u_2_s(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_u_2_s(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_u_2_s(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);
}
/**
 * @brief 
 * 
 * @param wh_lte_7s4 
 * @param protocol 
 * @param remote_ip  char * 
 * @param remote_ip_size 
 * @param port 
 * @return int 
 */
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+=convert_u_2_s(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);
}
/**
 * @brief 
 * 
 * @param wh_lte_7s4 
 * @param protocol 
 * @param remote_ip 
 * @param remote_ip_size 
 * @param port 
 * @return int 
 */
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+=convert_u_2_s(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_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 convert_u_2_s(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;
}