#include "wh_lte_7s4_v2.h"
#include "wh_lte_7s4_v2.h"

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

extern int WH_LTE_Send( WH_LTE_TypeDef * wh_lte_7s4,  uint8_t * buf, uint16_t size );
extern int wh_lte_7s4_ppp(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_a(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_test(WH_LTE_TypeDef *wh_lte_7s4,char *cmd,uint32_t size);
extern int wh_lte_7s4_inquire(WH_LTE_TypeDef *wh_lte_7s4,char *cmd,uint32_t size);
extern int wh_lte_7s4_run(WH_LTE_TypeDef *wh_lte_7s4,char *cmd,uint32_t size);
extern int wh_lte_7s4_set(WH_LTE_TypeDef *wh_lte_7s4,char *cmd,uint32_t cmd_size,char *args,uint32_t args_size);
extern int wh_lte_7s4_at_help(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_z(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_reboot(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_echo(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_echo(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_state echo_state);
extern int wh_lte_7s4_at_entm(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_workmode(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_work_mode work_mode);
extern int wh_lte_7s4_at_get_workmode(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_csq(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_system_info(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_reload(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_clear(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_sn(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_iccid(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_imei(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_local_ip(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_uart(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_uart(WH_LTE_TypeDef *wh_lte_7s4,uint32_t buad,uint8_t data,uint8_t stop,enum wh_lte_7s4_cmd_uart_parity parity);
extern int wh_lte_7s4_at_get_uartft(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_uartft(WH_LTE_TypeDef *wh_lte_7s4,uint32_t ft);
extern int wh_lte_7s4_at_get_uartfl(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_uartfl(WH_LTE_TypeDef *wh_lte_7s4,uint32_t len);
extern int wh_lte_7s4_at_get_socka(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_socka(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_protocol protocol,char *remote_ip,uint32_t remote_ip_size,uint16_t port);
extern int wh_lte_7s4_at_get_sockb(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_sockb(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_protocol protocol,char *remote_ip,uint32_t remote_ip_size,uint16_t port);
extern int wh_lte_7s4_at_get_socka_en(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_socka_en(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_state state);
extern int wh_lte_7s4_at_get_sockb_en(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_sockb_en(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_state state);
extern int wh_lte_7s4_at_get_sockasl(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_sockasl(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_sockasl type);
extern int wh_lte_7s4_at_get_sockbsl(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_sockbsl(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_sockasl type);
extern int wh_lte_7s4_at_get_sockalk(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_sockblk(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_get_sdpen(WH_LTE_TypeDef *wh_lte_7s4);
extern int wh_lte_7s4_at_set_sdpen(WH_LTE_TypeDef *wh_lte_7s4,enum wh_lte_7s4_cmd_state new_state);
extern int wh_lte_7s4_at_get_cclk(WH_LTE_TypeDef *wh_lte_7s4);

int WH_LTE_Send( WH_LTE_TypeDef * wh_lte_7s4,  uint8_t * buf, uint16_t size )
{
    return UART_HELPER_Trans(WH_LTE_uart_helper, buf, size);
}

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

int wh_lte_7s4_at_help(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"H",1);
}
int wh_lte_7s4_at_z(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"Z",1);
}
int wh_lte_7s4_at_reboot(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"REBOOT",6);
}
int wh_lte_7s4_at_get_echo(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_test(wh_lte_7s4,"E",1);
}
int wh_lte_7s4_at_set_echo(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_test(wh_lte_7s4,"ENTM",4);
}
int wh_lte_7s4_at_set_workmode(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_test(wh_lte_7s4,"WKMOD",5);
}
int wh_lte_7s4_at_get_csq(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"CSQ",3);
}
int wh_lte_7s4_at_get_system_info(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SYSINFO",7);
}
int wh_lte_7s4_at_reload(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"RELD",4);
}
int wh_lte_7s4_at_clear(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"CLEAR",5);
}
int wh_lte_7s4_at_get_sn(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SN",2);
}
int wh_lte_7s4_at_get_iccid(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"ICCID",5);
}
int wh_lte_7s4_at_get_imei(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"IMEI",4);
}
int wh_lte_7s4_at_get_local_ip(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"LOCIP",5);
}
int wh_lte_7s4_at_get_uart(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"UART",4);
}
int wh_lte_7s4_at_set_uart(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"UARTFT",6);
}
int wh_lte_7s4_at_set_uartft(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"UARTFL",6);
}
int wh_lte_7s4_at_set_uartfl(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKA",5);
}
int wh_lte_7s4_at_set_socka(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKB",5);
}
int wh_lte_7s4_at_set_sockb(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKAEN",7);
}
int wh_lte_7s4_at_set_socka_en(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKBEN",7);
}
int wh_lte_7s4_at_set_sockb_en(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKASL",7);
}
int wh_lte_7s4_at_set_sockasl(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKBSL",7);
}
int wh_lte_7s4_at_set_sockbsl(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKALK",7);
}
int wh_lte_7s4_at_get_sockblk(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SOCKBLK",7);
}
int wh_lte_7s4_at_get_sdpen(WH_LTE_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"SDPEN",5);
}
int wh_lte_7s4_at_set_sdpen(WH_LTE_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_TypeDef *wh_lte_7s4)
{
    return wh_lte_7s4_run(wh_lte_7s4,"CCLK",4);
}