#ifndef WH_LTE_7S4_V2_H_
#define WH_LTE_7S4_V2_H_

#ifdef __cplusplus
	extern "C" {
#endif

#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"
 
#include "elog.h"


#include <string.h>
#include "bsp_uart.h"
 
#define WH_LTE_USE_MAX485 0
#define WH_LTE_USE_MAX3160_232 0
#define WH_LTE_USE_MAX3160_485 1
 
#define  WH_LTE_Rcv_Buf_Size   (3+244+2) 
// #define  WH_LTE_Command_Number       1

static void WH_LTE_MAX3160_232(void) 
{
  HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, RESET);
  HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
}

#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;


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 UART_HELPER_TypeDef *WH_LTE_uart_helper;

typedef struct 
{
    int (*init)(void);
    void (*port)(void);
    int (*test)(void);
    void (*start)(void);
    void (*stop)(void);

    osMessageQueueId_t transQueue;  // 队列可选

    volatile MODULE_WH_LTE_WORK_STATE state; 
    uint8_t data_ok;                 /* 接收数据完整可以处理 */

    uint8_t receive_buf[1024];
    uint16_t size_received;

    uint64_t  timebase_ticks;
    uint64_t  timeout_ticks;

    uint8_t		event_flag;
	uint8_t		wavelength_ok;

    Record_Trans_Rcv_TypeDef transRecord[3];

    char receive_para[8][32];
    char send_args_buf[64];
    uint8_t WH_LTE_Data_Buf[3][ WH_LTE_Rcv_Buf_Size ];

	uint8_t receive_err_code;
    uint8_t receive_ok_flag;
    uint8_t receive_a_flag;
    uint8_t receive_para_num;
    uint32_t receive_size;
	uint8_t error;

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


}WH_LTE_TypeDef;

extern WH_LTE_TypeDef wh_lte ;

int WH_LTE_Init( void );
void WH_LTE_Task( void);
void WH_LTE_Port( void);
int WH_LTE_Test( );
void WH_LTE_Start( );
void WH_LTE_Stop( );

void WH_LTE_Wavelength( );

void WH_LTE_Set_Sendbuf(   uint8_t * buf, uint16_t size );
void WH_LTE_Set_Rcvbuf(   uint8_t * buf, uint16_t size );
void WH_LTE_Set_Timeout_ms(    uint64_t ms_ticks );
int WH_LTE_Validate(   );


int WH_LTE_Transmit();
int WH_LTE_Receive();

void WH_LTE_Trans_GPIO(void) ;

void WH_LTE_Trans_Cplt_GPIO(void) ;

int WH_LTE_CallBack( WH_LTE_TypeDef *pWHLTE , uint8_t *buf, uint16_t size );

unsigned wh_lte_convert_u2s(unsigned integer,char *str,unsigned length);
unsigned wh_let_7s4_get_work_state(void);
#endif
