/**
 * @file wh_lte_7s4_v2.h
 * @author Chen Jihang (embedded@eseaoptics.com)
 * @brief WH LTE 7S4 V2驱动
 * @version 0.1
 * @date 2022-07-18
 * 
 * @copyright ESEA (c) 2020
 * 
 */
#ifndef WH_LTE_7S4_V2_H_
#define WH_LTE_7S4_V2_H_
#include "stdint.h"
// #include "config.h"
#include "uart_helper.h"
#include "uart_interface.h"
// #include "platform.h"

#define    WH_LTE_Rcv_Buf_Size  (1024)

#define WH_LTE_FLAG_MASK_BUSY ((uint32_t)1<<(uint32_t)0)
#define WH_LTE_FLAG_MASK_RECVED_SN ((uint32_t)1<<(uint32_t)1)
#define WH_LTE_FLAG_MASK_RECVED_WL ((uint32_t)1<<(uint32_t)2)
#define WH_LTE_FLAG_MASK_RECVED_DATA ((uint32_t)1<<(uint32_t)3)


typedef enum 
{
    MODULE_WH_LTE_EVENT_READY,
    MODULE_WH_LTE_EVENT_PPP,
    MODULE_WH_LTE_EVENT_A,
    MODULE_WH_LTE_EVENT_WORKMODE,
    MODULE_WH_LTE_EVENT_SOCKASL,
    MODULE_WH_LTE_EVENT_SOCKAEN,
    MODULE_WH_LTE_EVENT_SOCKA,
    MODULE_WH_LTE_EVENT_GET_TIME,
    MODULE_WH_LTE_EVENT_REBOOT,
    MODULE_WH_LTE_EVENT_INIT_OK,
    MODULE_WH_LTE_EVENT_SEND_DATA,
} MODULE_WH_LTE_EVENT_TypeDef;


typedef enum 
{
    MODULE_WH_LTE_7S4_INIT_STATE_WAIT,
    MODULE_WH_LTE_7S4_INIT_STATE_START,
    MODULE_WH_LTE_7S4_INIT_STATE_PPP,
    MODULE_WH_LTE_7S4_INIT_STATE_A,
    MODULE_WH_LTE_7S4_INIT_STATE_SET_WORKMODE,
    MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKASL,
    MODULE_WH_LTE_7S4_INIT_STATE_SOCKAEN,
    MODULE_WH_LTE_7S4_INIT_STATE_SET_SOCKA,
    MODULE_WH_LTE_7S4_INIT_STATE_GET_TIME,
    MODULE_WH_LTE_7S4_INIT_STATE_REBOOT,
    MODULE_WH_LTE_7S4_INIT_STATE_OK,
    MODULE_WH_LTE_7S4_GET_TIME,
    MODULE_WH_LTE_7S4_INIT_STATE_ERROR,
    MODULE_WH_LTE_7S4_INIT_STATE_STOP
} MODULE_WH_LTE_WORK_STATE;


typedef int (*wh_lte_7s4_send_fun)(void *send_obj,char *buf,uint32_t size);

typedef struct  
{
    // UartHelper_TypeDef *uarthelper;
    volatile MODULE_WH_LTE_WORK_STATE state;

    uint8_t receive_err_code;
    uint8_t receive_ok_flag;
    uint8_t receive_a_flag;
    uint8_t receive_para_num;
    uint32_t receive_size;
    char receive_para[8][32];
    char send_args_buf[64];
    uint8_t receive_buf[1024];
    void *send_obj;
    wh_lte_7s4_send_fun send_fun;

    uint8_t error;                 /* 接收数据完整可以处理 */
    uint8_t data_ok;                 /* 接收数据完整可以处理 */
    volatile uint16_t size_received;                  

    uint64_t timebase;
    uint32_t timeout_ms;
    uint8_t timeout_enable;

    //  f服务器信息
    char server_ip_domain[64];
    uint16_t server_port;

}WH_LTE_7S4_Typedef;

enum wh_lte_7s4_cmd_state{
    WH_LTE_7S4_CMD_STATE_OFF=0,
    WH_LTE_7S4_CMD_STATE_ON=1
};
enum wh_lte_7s4_cmd_work_mode{
    WH_LTE_7S4_CMD_WORK_MODE_NET=0,
    WH_LTE_7S4_CMD_WORK_MODE_HTTPD=1,
    WH_LTE_7S4_CMD_WORK_MODE_UDC=0,
};
enum wh_lte_7s4_cmd_uart_parity{
    WH_LTE_7S4_CMD_UART_PARITY_NONE=0,
    WH_LTE_7S4_CMD_UART_PARITY_ODD=1,
    WH_LTE_7S4_CMD_UART_PARITY_EVEN=2
};
enum wh_lte_7s4_cmd_sockasl{
    WH_LTE_7S4_CMD_SOCKASL_SHORT=0,
    WH_LTE_7S4_CMD_SOCKASL_LONG=1
};
enum wh_lte_7s4_cmd_protocol{
    WH_LTE_7S4_CMD_PROTOCOL_TCP,
    WH_LTE_7S4_CMD_PROTOCOL_UDP
};

extern WH_LTE_7S4_Typedef *wh_lte_7s4;


/* 再封装演示 */ 
typedef struct 
{
	uint8_t (*init)( );			 
	void (*port)( );	
  void  (*setstate)( MODULE_WH_LTE_WORK_STATE state);
  // MODULE_WH_LTE_WORK_STATE  (*Set_State)( MODULE_WH_LTE_WORK_STATE state );

    GPIO_TypeDef *sel_gpio;	 
    GPIO_TypeDef *dplx_gpio;	 
    GPIO_TypeDef *de_gpio;	 
    uint16_t sel_pin;
    uint16_t dplx_pin;
    uint16_t de_pin;

    Uart_Interface_Type_Typedef interface_type; /* 0: default(ttl 232), 1: 485 ,2:3160*/
    Uart_RS_Mode_TypeDef mode_232_485;       /* 0 commome  1:485*/
    Uart_Transmode_TypeDef trans_type;     /* 0 :polling, 1: IT  2: DMA*/
    Uart_Transmode_TypeDef rcv_type;     /* 0 :polling, 1: IT  2: DMA*/
    Uart_IDLE_Enable_TypeDef idle_enable_disable;     /* 0 :不启用空闲 , 1: 启用空闲*/
 
    WH_LTE_7S4_Typedef *sample; /*不是常量  不能直接初始化*/

}My_WH_LTE_TypeDef;
extern My_WH_LTE_TypeDef my_wh_lte;

void WH_LTE_Port(void);
void WH_LTE_Task(void* argument);
void  WH_LTE_Set_State(MODULE_WH_LTE_WORK_STATE state);

void WH_LTE_Set_Timeout( WH_LTE_7S4_Typedef * wh_lte_7s4,    uint16_t timeout_ms );
int WH_LTE_Get_Timeout( WH_LTE_7S4_Typedef * wh_lte_7s4 );

int WH_LTE_Transmit( WH_LTE_7S4_Typedef * wh_lte_7s4,  uint8_t * buf, uint16_t size );
int WH_LTE_Begin_Rcv( WH_LTE_7S4_Typedef * wh_lte_7s4,  uint8_t * buf, uint16_t size );

void WH_LTE_Set_GPIO_For_TC_Cplt( WH_LTE_7S4_Typedef * wh_lte_7s4 );
int WH_LTE_copy(WH_LTE_7S4_Typedef * wh_lte_7s4 ,uint16_t size);

int wh_lte_7s4_init(  );
int wh_lte_7s4_ppp(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_a(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_test(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size);
int wh_lte_7s4_inquire(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size);
int wh_lte_7s4_run(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t size);
int wh_lte_7s4_set(WH_LTE_7S4_Typedef *wh_lte_7s4,char *cmd,uint32_t cmd_size,char *args,uint32_t args_size);
int wh_lte_7s4_at_help(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_z(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_reboot(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_echo(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_echo(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state echo_state);
int wh_lte_7s4_at_entm(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_workmode(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_work_mode work_mode);
int wh_lte_7s4_at_get_workmode(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_csq(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_system_info(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_reload(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_clear(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_sn(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_iccid(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_imei(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_local_ip(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_uart(WH_LTE_7S4_Typedef *wh_lte_7s4);
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 wh_lte_7s4_at_get_uartft(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_uartft(WH_LTE_7S4_Typedef *wh_lte_7s4,uint32_t ft);
int wh_lte_7s4_at_get_uartfl(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_uartfl(WH_LTE_7S4_Typedef *wh_lte_7s4,uint32_t len);
int wh_lte_7s4_at_get_socka(WH_LTE_7S4_Typedef *wh_lte_7s4);
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 wh_lte_7s4_at_get_sockb(WH_LTE_7S4_Typedef *wh_lte_7s4);
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 wh_lte_7s4_at_get_socka_en(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_socka_en(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state state);
int wh_lte_7s4_at_get_sockb_en(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_sockb_en(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state state);
int wh_lte_7s4_at_get_sockasl(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_sockasl(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_sockasl type);
int wh_lte_7s4_at_get_sockbsl(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_sockbsl(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_sockasl type);
int wh_lte_7s4_at_get_sockalk(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_sockblk(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_get_sdpen(WH_LTE_7S4_Typedef *wh_lte_7s4);
int wh_lte_7s4_at_set_sdpen(WH_LTE_7S4_Typedef *wh_lte_7s4,enum wh_lte_7s4_cmd_state new_state);
int wh_lte_7s4_at_get_cclk(WH_LTE_7S4_Typedef *wh_lte_7s4);
void wh_lte_7s4_receive_callback(WH_LTE_7S4_Typedef *wh_lte_7s4, uint32_t size);
void wh_lte_7s4_receive_unpack(WH_LTE_7S4_Typedef *wh_lte_7s4,char *buf,uint32_t size);

int Compare_RcvBuf_And_Procedure_Table(int seq, uint8_t *buf, uint16_t size);
unsigned wh_lte_convert_u2s(unsigned integer,char *str,unsigned length);
#endif

/*
AT+CCLK? 
+CCLK: “20/06/19,20:05:19+32”

AT+LBS 
+LBS:"520C","03207502"

AT+APN=3gnet,admin,admin,0
OK

[CR][LF][+CMD][OP][PARA][CR][LF]<CR><LF>[OK]<CR><LF>

AT+ENTM  OK

AT+ID=0001

1 +++a 进入指令配置状态 
2 AT+WKMOD=UDC 工作模式设置为：UDC 
3 AT+SOCKAEN=ON 设置 socket A 为使能状态 
4 AT+SOCKA=TCP,test.usr.cn,2317 设置 socket A 为 TCP Client 
5 AT+SOCKASL=LONG 设置 socket A 为长连接 
6 AT+REGEN=ON 设置注册包使能为开 
7 AT+ID=0001 设置UDC的设备ID为0001，最大长度11位 
8 AT+Z 重启模块

1 +++a 进入指令配置状态 
2 AT+WKMOD=NET 工作模式设置为：NET 
3 AT+REGEN=ON 开启注册包功能 
4 AT+REGTP=CLOUD 设置注册包内容类型为CLOUD 
5 AT+CLOUD=01234567890123456789，12345678 设置透传云用户名和密码 
6 AT+Z 重启模块
*/