#ifndef __COMMANDDEFINE_H
#define __COMMANDDEFINE_H
#include "sys.h"
#include "CommandsConfig.h"

#define BOOT_VERSION_FLASH_ADDR_BASE  0x0200

//#define COMPPOR_RECEIVED_BUFFER_SIZE (1024*16)

#define PACKAGE_HEADER_BYTE1	0x55
#define PACKAGE_HEADER_BYTE2	0xaa
#define PACKAGE_HEADER_BYTE3	0xbb

#define PACKAGE_TAIL_BYTE1	0x0d
#define PACKAGE_TAIL_BYTE2	0x0a
#define PACKAGE_HEAD_BYTES	6  // Header_byte1 + Header_byte2 + Header_byte3 + Header_cmd + Len_Bytes * 2 
#define PACKAGE_MIN_BYTES		(PACKAGE_HEAD_BYTES + PACKAGE_TAIL_BYTES) // Header_byte1 + Header_byte2 + Header_cmd + Len_Bytes * 2 + 0x0d + 0x0a
#define PACKAGE_TAIL_BYTES 2

#define LMH43711_ADDR_GAIN 0x00
#define LMH6401_ADDR_GAIN 0x02
#define LMH6401_ADDR_TEMPCORRECT_GAIN 0x04
#define LMH6401_ADDR_TEMPCORRECT_FREQ 0x05

#define BYTES_MASTER_STARTTIME 20 //format=2023-08-09 17:45:58
//返回命令无效
#define COMMAND_INVALID 0xEE
//返回命令成功
#define COMMAND_OK 0x66

#define RUNNING_AT_APP 0x77 // App

#define RUNNING_AT_BOOT 0x11 // Boot


#define Version_DateLength  14
#define Version_ModifierNameMaxLength 16
#define Version_ModelNameMaxLength  32
#define Version_Comment_MaxLength 64

#pragma pack(1)
typedef struct
{
	u8 Version_MainNo;
	u8 Version_SubNo;
	u8 Version_ModifyNo;
	u8 Version_Revision_Number;
	char Version_Date[Version_DateLength];
	char Version_ModifierName[Version_ModifierNameMaxLength];
	char Version_ModelName[Version_ModelNameMaxLength];
	char Version_Comment[Version_Comment_MaxLength];
}VersionInfo;


typedef struct
{
	u8 Head1;
	u8 Head2;
	u8 Head3;
	u8 Cmd;
  u16 Bytes;		
}CmdPackageHeader;

enum RequestCmds
{
	CMD0x01_Request_RunningAtWhere=0x01,
	CMD0x02_Request_ReadMcuVersion=0x02,
	CMD0x03_Request_ReadMcuBootVersion=0x03,
	CMD0x04_Request_ReadUpdateTimeStamp=0x04,
	
	CMD0x05_Request_UpdateStart=0x05,
	CMD0x06_Request_RegisterAppStartTime=0x06,
	CMD0x07_Request_ReadbackAppStartTime=0x07,
	
	CMD0x08_Request_UpdateSend=0x08,
	CMD0x0B_Request_UpdateVerifyStage1=0x0B,	
	CMD0x0C_Request_UpdateSwitch=0x0C,
	CMD0x0D_Request_UpdateVerifyStage2=0x0D,	
	CMD0x11_Request_UpdateFinished=0x11,	
	CMD0x15_Request_WriteUpdateTimeStamp=0x15,
	
	CMD0x20_Request_PowerCtrl=0x20,
	CMD0x21_Request_RefVlotage=0x21,
	CMD0x22_Request_AnalogChannelSet=0x22,
	CMD0x23_Request_ExternalChannelSet=0x23,
 
	CMD0x29_Request_SetGainTempCorrection=0x29,
	CMD0x30_Request_ReadTemperature=0x30,
	CMD0x31_Request_ReadBack_ProbeStatus=0x31,	
	CMD0x32_Request_CtrlProbeDifferenceOrSingle=0x32,
	CMD0x33_Request_ReadProbeFactoryInfo=0x33,
	CMD0x34_Request_CtrlProbeLed=0x34,
	
	CMD0x40_Request_AnalogChannelCalcOffset=0x40,
	CMD0x41_Request_AnalogChannelOffset=0x41,
	CMD0x42_Request_AnalogChannelGain=0x42,
	CMD0x43_Request_CtrlChannel4094=0x43,
	CMD0x48_Request_CtrlOuterLed=0x48,
	CMD0x50_Request_WriteProbeFactoryInfo=0x50,
	CMD0xA1_Request_ReadMcuRunInfo=0xA1,
	CMD0xB0_Request_GetErrorCode=0xB0,	
	CMD0xC0_Request_CmdIsOK=0xC0,//used response		
	CMD0xC1_Request_CommunicateReset=0xC1,		
	CMD0xE0_Request_ThrowHDError=0xE0,
	CMD0xE1_Request_ThrowStuckError=0xE1,
	CMD0xE3_Request_GetDebugData=0xE3,		
	CMD0xE4_Request_TestCommunicate=0xE4,
};

enum ReadbackErrorCode
{
	Error0x01_CannotRunAtApp=0x01,
	Error0x02_CannotRunAtBoot=0x02,
	Error0x03_AlreadyAtBoot=0x03,
	Error0x04_AlreadyAtApp=0x04,

	Error0x05_CMD_ParaLentghError=0x05,
	Error0x06_CMD_UpdateNotStart=0x06,
	
	Error0x90_CMD_ThisVersionNotSupport=0x90,	
	Error0xFF_General=0xff,
};

enum DebugDataTypes
{
	DebugDataType_AnalogChannelSetData=0x00,
	DebugDataType_CommunicateTimesInfo=0x01,
	DebugDataType_Analog4094Word=0x02,
};
/*
extern short ComPortReceivedBytes;
extern u8 bPackageTailFound;
extern u8 ComPortReceivedBuffer[COMPPOR_RECEIVED_BUFFER_SIZE];
*/
extern void Command_ProcessRequestCmds(void);

//初始化命令
//cmdLen命令总长度
//cmdCode命令号
//dataLen数据长度
extern int Command_FormatCmdBuffer(u8 cmdCode,u8 *pData,int dataLen,u8* resultBuffer);
extern int Command_BytesCheck(u8 datas[],u16 bytesLen);
//// Request_UpdateSend 相关
extern u8 UpdateWhat;
extern u8 Updating;
extern u32 UpdateRecvedContentBytes;
extern u8  UpdateVerifyStage1OK;
extern u8  UpdateVerifyStage2OK;
//// Request_UpdateFinished 相关

extern void Command_SendbackErrorMessage(u8 errorCode,u8 bIsContentBitInverted_SendBack);
extern void Command_SendbackOKMessage(void);
extern void Command_SendbackAppStartTime(void);

extern void Timer_ReadBack_ProbeStatus(u8 checkChanged,u8 bIsContentBitInverted_SendBack);
extern short Last_Probe_ConnectState;
extern short Last_Probe_ButtonState;

extern u32 RecvedPackageCount;
extern u32 SendBackPackageCount;
extern u8  RegisteredMatserStartTime[BYTES_MASTER_STARTTIME];
extern VersionInfo* GetVersion(void);
#endif 
