/*===========================================================================
                    Combined Charging System (CCS): SECC
                                 CsuCommAC.h

                             initiated by Joseph
                              (since 2020/06/10)
=============================================================================
Merged from:
- GIT Server: https://git.phihong.com.tw:30000/System_Integration/CSU3_AM335x/commits/AW-CCS
- VERSION SHA: de7e1da3b9d219fbb8babf12d305e9423f1e8d94
- VERSION Date: 2020-06-12
=============================================================================*/

typedef struct Verion
{
	char Version_FW[32];
	char Version_HW[32];
}Ver;

typedef struct PRESENTINPUTVOLTAGE
{
	unsigned char inputType;	// 0x00: Line to Line       0x01: Line to Neutral
	double L1N_L12;
	double L2N_L23;
	double L3N_L31;
}PresentInputVoltage;

typedef struct PRESENTOUTPUTVOLTAGE
{
	double behindFuse_Voltage_C1;
	double behindRelay_Voltage_C1;
	double behindFuse_Voltage_C2;
	double behindRelay_Voltage_C2;
}PresentOutputVoltage;

typedef struct FANSPEED
{
	unsigned short int speed[4];
}FanSpeed;

typedef struct TEMPERATURE
{
	unsigned char point[8];
}Temperature;

typedef struct AUXPOWER
{
	unsigned char voltage[8];
}AuxPower;

typedef struct RELAY
{
	unsigned char relay_status[2][8];
}Relay;

typedef struct GFD
{
	unsigned short int adc_value_positive[2];
	unsigned short int adc_value_negative[2];
}Gfd;

typedef struct GPIO_IN
{
	unsigned char AC_Connector;
	unsigned char AC_MainBreaker;
	unsigned char SPD;
	unsigned char Door_Open;
	unsigned char GFD[2];
	unsigned char Button[2];
	unsigned char Button_Emergency;
}Gpio_in;

typedef struct GPIO_OUT
{
	unsigned char AC_Connector;
	unsigned char Button_LED[2];
	unsigned char System_LED[4];
}Gpio_out;

typedef struct ALARM_LOG
{
	unsigned char logArea;
	unsigned int alarmIndex;
	unsigned char log[8];
}Alarm_Log;

typedef struct BLE_CONFIG_DATA
{
	unsigned char isLogin:1;
	unsigned char isRequestStart:1;
	unsigned char isRequestStop:1;
}Ble_Config_Data;

typedef struct BLE_LONGIN_CENTRAL_ID
{
	unsigned char id[32];
}Ble_Login_Central_Id;

typedef struct RTC
{
	unsigned short int year;
	unsigned char month;
	unsigned char day;
	unsigned char hour;
	unsigned char min;
	unsigned char sec;
}Rtc;

typedef struct PRESENTOUTPUTCURRENT
{
	double L1N_L12[2];
	double L2N_L23[2];
	double L3N_L31[2];
}Presentoutputcurrent;

typedef struct AC_PRIMARY_MCU
{
	unsigned char cp_state;
	unsigned int  current_limit;
	float cp_voltage_positive;
	float cp_voltage_negtive;
	unsigned char locker_state;
	unsigned char relay_state;
	unsigned char shutter_state;
	unsigned char meter_state;
	unsigned char pp_state;
	unsigned char rating_current;
	unsigned char rotatory_switch;
	Relay	relayState;
}Ac_Primary_Mcu;

typedef struct AC_PRIMARY_MCU_ALARM
{
	union
	{
		unsigned long InputAlarmCode;
		struct
		{
			unsigned long OVP_L1:1;
			unsigned long UVP_L1:1;
			unsigned long OCP_L1:1;
			unsigned long OTP:1;
			unsigned long gmi_fault:1;
			unsigned long cp_fault:1;
			unsigned long ac_leak:1;
			unsigned long dc_leak:1;
			unsigned long mcu_selftest_fail:1;
			unsigned long handshaking_timeout:1;
			unsigned long emergency_stop:1;
			unsigned long relay_welding:1;
			unsigned long leak_module_fail:1;
			unsigned long shutter_fault:1;
			unsigned long locker_fault:1;
			unsigned long power_drop:1;
			unsigned long rotate_switch_fault:1;
			unsigned long short_circuit_L1:1;
			unsigned long relay_drive_fault:1;
			unsigned long comm_timeout:1;
			unsigned long OVP_L2:1;
			unsigned long UVP_L2:1;
			unsigned long OCP_L2:1;
			unsigned long OVP_L3:1;
			unsigned long UVP_L3:1;
			unsigned long OCP_L3:1;	
			unsigned long short_circuit_L2:1;
			unsigned long short_circuit_L3:1;			
		}bits;
	};
}Ac_Primary_Mcu_Alarm;

typedef struct AC_PRIMARY_MCU_LED
{
	unsigned char mode;
	unsigned long alarm_code;
}Ac_Primary_Mcu_Led;

typedef struct EVSE_ID
{
	unsigned char model_name[14];
	unsigned char serial_number[12];
}Evse_Id;

typedef struct AC_PRIMARY_MCU_CP_PWM_DUTY
{
	unsigned int max_current;
}Ac_Primary_Mcu_Cp_Pwm_Duty;

typedef struct LEGACY_REQUEST
{
	unsigned char isLegacyRequest:1;
	uint8_t isRelayOn:1;
}Legacy_Request;

typedef struct POWER_CONSUMPTION
{
	uint32_t power_consumption;
	uint32_t power_consumption_at_start;

}Power_Consumption;

typedef struct MCU_OP_FLAG
{
	unsigned char isSetModePass:1;
	unsigned char isSetSerialNumberPass:1;
	unsigned char isSetModelNamePass:1;
	unsigned char isReadFwVerPass:1;
	unsigned char isMcuUpgradeReq:1;
	unsigned char isSetCpPwmDuty:1;
}Mcu_Op_Flag;

typedef struct SYSTEM_ALARM_CODE
{
	unsigned long SystemAlarmCode;

}System_Alarm_Code;

typedef struct OTHER_ALARM_CODE
{
	unsigned long isHandshakingTimeOut:1;
	unsigned long isDcLeakage:1;
	unsigned long isACLeakage:1;
}Other_Alarm_Code;

typedef struct PILOT_VOLTAGE
{
	float PilotVoltagePositive;
	float PilotVoltageNegative;

}Pilot_Voltage;

typedef struct FW_UPGRADE_INFO
{
	int fwType;
	char modelName[17];
	char location[384];
}Fw_Upgrade_Info;

typedef struct GUN_PLUGIN_TIMES
{
	uint32_t GunPluginTimes;

}Gun_Plugin_Times;

typedef struct MCU_RESET_REQUEST
{
	unsigned char isMcuResetRequest:1;
}Mcu_Reset_Request;

typedef struct TIMEOUT_SPEC
{
	int Setting_Timeout_Spec;
	int Present_Timeout_Spec;
}Timeout_Spec;

typedef struct SET_BREATHE_LED_TIMING
{
	uint16_t set_Led_Action_Connected_Fade_In;
	uint16_t set_Led_Action_Connected_Fade_Out;
	uint16_t set_Led_Action_Authed_Fade_In;
	uint16_t set_Led_Action_Authed_Fade_Out;
	uint16_t Set_Led_Action_Chaging_Fade_In;
	uint16_t set_Led_Action_Chaging_Fade_Out;
}Set_Breathe_Led_Timing;

typedef struct SET_LED_BRIGHTNESS
{
	uint8_t sector_1;	// 0~1 AM and 1~2 AM
	uint8_t sector_2;	// 2~3 AM and 3~4 AM
	uint8_t sector_3;	// 4~5 AM and 5~6 AM
	uint8_t sector_4;	// 6~7 AM and 7~8 AM
	uint8_t sector_5;	// 8~9 AM and 9~10 AM
	uint8_t sector_6;	// 10~11 AM and 11~12 AM
	uint8_t sector_7;	// 12~13 PM and 13~14 PM
	uint8_t sector_8;	// 14~15 PM and 15~16 PM
	uint8_t sector_9;	// 16~17 PM and 17~18 PM
	uint8_t sector_10;	// 18~19 PM and 19~20 PM
	uint8_t sector_11;	// 20~21 PM and 21~22 PM
	uint8_t sector_12;	// 22~23 PM and 23~24 PM
}Set_Led_Brightness;

typedef struct CCS_INFO
{
	uint8_t		BatteryChargeType;				/*0x00: AC charging, 0x01: DC charging*/
	uint8_t 	PresentMsgFlowStatus;   
												/*  ISO15118_2018
												0: Idle(wait B2 state), 
												1: CM_SLAC_PARM.REQ, 
												2: CM_SLAC_PARM.CNF, 
												3: CM_START_ATTEN_CHAR.IND
												4: CM_MNBC_SOUND.IND, 
												5: CM_ATTEN_CHAR.IND, 
												6: CM_ATTEN_CHAR.RSP, 
												7: CM_VALIDATE.REQ
												8: CM_VALIDATE.CNF, 
												9: CM_SLAC_MATCH.REQ, 
												10: CM_SLAC_MATCH.CNF, 
												11: CM_AMP_MAP.REQ
												12: CM_AMP_MAP.CNF, 
												13: SLACC/SDP/TCP connection,
												16: SupportedAppProtocolRequest, 
												17: SupportedAppProtocolResponse, 
												18: SessionSetupRequest
												19: SessionSetupResponse, 
												20: ServiceDiscoveryRequest, 
												21: ServiceDiscoveryResponse
												22: ServiceDetailRequest, 
												23: ServiceDetailResponse
												24:ServiceAndPaymentSelectionRequest/ServiceSelectionRequest, 
												25: ServiceAndPaymentSelectionResponse/ServiceSelectionResponse
												26: PaymentDetailsRequest/IdentificationDetailsRequest;, 
												27: PaymentDetailsResponse/IdentificationDetailsResponse,
												28: AuthorizationRequest, 
												29: AuthorizationResponse,
												30: CertificateUpdateRequest, 
												31: CertificateUpdateResponse, 
												32:CertificateInstallationRequest, 
												33: CertificateInstallationResponse
												34: ChargeParameterDiscoveryRequest, 
												35: ChargeParameterDiscoveryResponse
												36: CableCheckRequest, 
												37: CableCheckResponse, 
												38: PreChargeRequest, 
												39: PreChargeResponse
												40: PowerDeliveryRequest start, 
												41: PowerDeliveryResponse start, 
												42: ChargingStatusRequest, 
												43: ChargingStatusResponse
												44:CurrentDemandRequest, 
												45:CurrentDemandResponse, 
												46:MeteringReceiptRequest, 
												47: MeteringReceiptResponse
												48: PowerDeliveryRequest end, 
												49: PowerDeliveryRequest end, 
												50: WeldingDetectionRequest, 
												51: WeldingDetectionResponse,
												52: SessionStopRequest, 
												53: SessionStopResponse
												253: Performance Timeout, 
												254: Sequence Timeout, 
												255: Fault
												*/

	float 		ConnectorTemperature1;			/*unit: 1°C*/
	float 		ConnectorTemperature2;			/*unit: 1°C*/
	uint16_t	ChargingRemainTime;				/*0x0000: 0 sec ~ 0xFFFF: 65535 sec, unit: 1 second*/
	float		TotalBatteryCapacity;			/*unit: Wh*/
	float		BatteryMaximumVoltage;			/*unit: 1 volt*/
	float		BatteryMaximumCurrent;			/*unit: 1 amp*/
	uint8_t		EVCCID[8];
	uint8_t		EVSEID[40];
	uint8_t		CCSLibRev[32];
	uint8_t		EVSEModelName[32];
	uint32_t	CSUAlarmStatusCode;
	uint16_t	CCSAlarmStatusCode;
	uint8_t		PaymentOption;					/* 0x00 EIM, 0x01 Pnc */
	float		EVSEMaxCurrent;					/*unit: 1 amp*/
	float		EVSEMinCurrent;					/*unit: 1 amp*/
	float		GridVoltage[3];				    /*unit: 1 volt*/
	uint8_t		MeterID[32];					/*unit: 1 amp*/
	float		MeterReadingValue;				/*unit: 1Wh*/
	uint8_t		EVOperation;					/*0:Charge, 1:Discharge*/
	uint8_t		EVChargeProgress;				/*0: Stop
													1: Start
													2: Renegotiate
													3: Standby */
	uint8_t		CpSetPWMDuty;					/*unit: 1%
													0: 0%
													5: 5%
													100: 100%*/
	uint8_t		CpSetStateE;					/*0:disable, 1:enable*/
	uint8_t		CpPresentPWMDuty;				/*unit:1%*/
	uint8_t		CpPresentState;					/*1: A (12V, no PWM)
													2: B1 (9V, no PWM)
													3: B2 (9V, with PWM)
													4: C (6V, with PWM)
													5: D (3V, with PWM)
													6: E (0V, no PWM)
													7: F (-12V, no PWM)
													8: G (>12V)
													9: H  (<12V)*/
	float		CpPositiveVoltage;				/*uint: 1V*/
	float		CpNegativeVoltage;				/*uint: 1V*/
	uint32_t	CcsHeartBeat;					/*unit: 1*/
	float 		EVSEPresentCurrent[3];			//unit: 1A
	float 		AvailableChargingPower;			//1KW
	uint8_t		EVSENotification;				//0:none, 1:StopCharging, 2:RenNgotiation
	uint16_t 	ChargingPermission:1;				/*0x00: Not ready yet, stay in idle mode or go into terminating process.
												  0x01: start charging process*/
	uint16_t 	ConnectorLockerStatus:1;			/*0x00: released
												  0x01: locked*/	
												 
	uint16_t	RcdStatus:1;					/* 0x00 no error
												   0x01 an error */
	uint16_t	OutputRelayStatus:1;			/*0: OFF
												1: ON*/
	uint16_t	TempFlag4:1;
	uint16_t	TempFlag5:1;
	uint16_t	TempFlag6:1;
	uint16_t	TempFlag7:1;
	uint16_t	TempFlag8:1;
	uint16_t	TempFlag9:1;
	uint16_t	TempFlag10:1;
	uint16_t	TempFlag11:1;
	uint16_t	TempFlag12:1;
	uint16_t	TempFlag13:1;
	uint16_t	TempFlag14:1;
	uint16_t	TempFlag15:1;
	
}Ac_Ccs_Info;

typedef struct GUN_INFO
{
	Ver 											ver;
	PresentInputVoltage 							inputVoltage;
	Presentoutputcurrent							outputCurrent;
	Temperature 									temperature;
	Ble_Config_Data									bleConfigData;
	Ble_Login_Central_Id							bleLoginCentralId;
	Rtc												rtc;
	Ac_Primary_Mcu									primaryMcuState;
	Ac_Primary_Mcu_Alarm							primaryMcuAlarm;
	Ac_Primary_Mcu_Led								primaryMcuLed;
	Mcu_Op_Flag										mcuFlag;
	Power_Consumption								powerConsumption;
	Legacy_Request									legacyRequest;
	System_Alarm_Code								systemAlarmCode;
	Ac_Primary_Mcu_Cp_Pwm_Duty						primaryMcuCp_Pwn_Duty;
	Other_Alarm_Code								otherAlarmCode;
	Pilot_Voltage									PilotVoltage;
	Gun_Plugin_Times								gunPluginTimes;
	Mcu_Reset_Request								mcuResetRequest;
	Set_Breathe_Led_Timing							setBreatheLedTiming;
	Set_Led_Brightness								setLedBrightness;
	Ac_Ccs_Info										acCcsInfo;
	uint8_t											ccsHandshakeState;
	uint8_t											PreviousEVChargeProgress;	
	uint8_t											chargingMode;
	uint16_t										targetCurrent;
	
	uint16_t										isAuthPassEnd:1;
	uint16_t										rfidReq:1;
	uint16_t										isGunPlugged:1;
	uint16_t										isInitialPass:1;
	uint16_t										isSetBreatheLedTiming:1;
	uint16_t										isSetLedBrightness:1;
	uint16_t										isUnlockerConnetor:1;
	uint16_t										isOperactive:1;
	uint16_t										isCCSWaitChangeDuty:1;
	uint16_t										isCCSStartTransation:1;
}Gun_Info;

//struct Charger
struct InternalCommAC
{
	Ver 					ver;
	Evse_Id					evseId;
	Gun_Info 				gun_info[2];
	Fw_Upgrade_Info			fwUpgradeInfo;
	Timeout_Spec			timeoutSpec;
	
	uint8_t					gun_selectd;
	uint8_t	 				speaker_type;
	uint8_t					isSpeakerOn:1;
	uint8_t		 			isUpdateSuccess:1;
};