#ifndef __CAT_ONE_TASK_H
#define	__CAT_ONE_TASK_H
#include "main.h"
#include "Public.h"

#define	CAT_ONE_REV_TIMEOUT_MAX			(10 * 60 * 1000)
#define	CAT_ONE_REV_LEN_MAX					512

#define	CAT_ONE_POW_ON()						CAT1_POW_ON()
#define	CAT_ONE_POW_OFF()						CAT1_POW_OFF()

#define	CAT_ONE_LINKA_GET()					CAT1_LINKA_GET()
#define	CAT_ONE_LINKB_GET()					CAT1_LINKB_GET()

#define	CAT_ONE_DELAY_1MS(X)

// ETH状态机相关宏定义
#define ETH_RESET_DELAY_TIME_MAX		(5 * 60 * 1000)
#define ETH_LINK_WAIT_TIME_MAX			(10 * 1000)
#define ETH_REG_TIMEOUT_MAX				(30 * 1000)
#define ETH_SEND_TIMEOUT_MAX			(5 * 1000)
#define ETH_RECV_TIMEOUT_MAX			(60 * 1000)
#define ETH_SEND_RETRY_MAX				3
#define ETH_FIRST_REG_DELAY				(5 * 1000)

typedef enum {
	CAT_ONE_AT_NULL,
	CAT_ONE_AT,					//开机检测
	CAT_ONE_ATE0,				//关回显
	CAT_ONE_CPIN,				//识卡
	CAT_ONE_IMEI,
	CAT_ONE_LCCID,			//读卡
	CAT_ONE_CEREG,			//查询注册状态
	CAT_ONE_CGPADDR,		//查询IP地址
	CAT_ONE_CSQ,				//查询信号强度
	CAT_ONE_LDNSGIP,		//域名解析
	CAT_ONE_LBS,				//获取基站定位信息
	CAT_ONE_LIPOPEN,		//设置TCP服务器地址和端口
	CAT_ONE_LIPSEND,		//发送数据
	CAT_ONE_LTPCLOSE,		//断开连接
	CAT_ONE_LBSPARA,
	CAT_ONE_CCLK,					//获取时间
	CAT_ONE_AT_CMD_END,
}CatOneATCMD_m;

typedef enum {
	CAT_ONE_IDEL,
	CAT_ONE_INIT,
	CAT_ONE_TCP_CONN,
	CAT_ONE_WAIT_CONN,
	CAT_ONE_SEND_DATA,
	CAT_ONE_WAIT_SEND,
	CAT_ONE_REV_DATA,
	CAT_ONE_REG_SVR,
	CAT_ONE_WAIT_REV,
	CAT_ONE_RESET,
	CAT_ONE_OFF,
	CAT_ONE_CLOSE_TCP,
}CatOneStatus_m;

typedef enum {
	ETH_IDEL,
	ETH_RESET,
	ETH_WAIT_LINK,
	ETH_REG_SVR,
	ETH_WAIT_SEND,
	ETH_SEND_DATA,
	ETH_WAIT_RECV,
	ETH_OFF,
}EthStatus_m;

typedef enum {
	CAT_ONE_RET_NULL,
	CAT_ONE_RET_OK,
	CAT_ONE_RET_ERR,
	CAT_ONE_RET_TIMEOUT,
}CatOneRetStatus_m;

typedef enum {
	CAT_ONE_CMD_MODE,
	CAT_ONE_PT_MODE,
}CatOneMode_m;

typedef enum {
	CAT_ONE_2G,
	CAT_ONE_ATE,
}CatOneNetType_m;

typedef struct {
	CatOneStatus_m Cat1Status;
	CatOneStatus_m NextStatus;
	CatOneRetStatus_m ATCmdRet;
	uint32_t CatOne1mSDelayCnt;
	uint32_t ResetDelayCnt;
	uint32_t RegisterDelayCnt;
	uint8_t AtCmdResendCnt;
	uint8_t AtCmdIdx;
	CatOneATCMD_m AtCmd;
	bool RxFlag;
	uint16_t CSQ;
	char IMEI[20];
	char SIM[32];
	CatOneNetType_m  NetType;
	bool TcpConnFlag;
	
	bool LBSFlag;
	int Longitude;
	int Latitude;
	char CatOneSendBuff[1024];
	
	GateWayPara GateWay;	
	DataRevCallBack CatOneRevCallBack;
	
	// ETH状态机相关字段
	EthStatus_m EthStatus;
	uint32_t EthResetDelayCnt;
	uint32_t EthLinkWaitCnt;
	uint32_t EthRecvTimeoutCnt;
	uint8_t EthSendRetryCnt;
	bool EthFirstRegFlag;
}CatOne_t, *pCatOne_t;

CatOneRetStatus_m CatOneSend(uint8_t *sData, uint16_t sLen);
void CatOneStart(void);
void CatOneStop(void);
void CatOneReset(void);
void ETHStop(void);
void ETHReset(void);
void Cat1DBGOnOff(bool OnOff);
void CatOneGetLocationInfo(int *Longitude, int *Latitude);
void CatOneGetIMEIAndSIM(char *Imei, char *Sim);
bool CatOneGetStatus(void);
bool CatOneEthSendQueue(uint8_t *sData, uint16_t sLen);
void CatOneTriggerRegister(void);
void ETHTriggerRegister(void);
void CatOne_Eth_Thread_Entry(void *parameter);
void EthRxOverhandler(void);
void CatReadImeiOrSim(char *Imei, char *Sim);
#endif
