/*
 * Config.h
 *
 *  Created on: 2020年01月15日
 *      Author: Eason Yang
 */

#ifndef CONFIG_MAIN_H_
#define CONFIG_MAIN_H_

#include    <sys/types.h>
#include    <sys/stat.h>
#include 	<sys/time.h>
#include 	<sys/timeb.h>
#include 	<sys/types.h>
#include 	<sys/ioctl.h>
#include 	<sys/socket.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/mman.h>
#include 	<linux/wireless.h>
#include 	<arpa/inet.h>
#include 	<netinet/in.h>
#include	<dirent.h>

#include 	<unistd.h>
#include 	<stdarg.h>
#include    <stdio.h>      /*標準輸入輸出定義*/
#include    <stdlib.h>     /*標準函數庫定義*/
#include    <unistd.h>     /*Unix 標準函數定義*/
#include    <fcntl.h>      /*檔控制定義*/
#include    <termios.h>    /*PPSIX 終端控制定義*/
#include    <errno.h>      /*錯誤號定義*/
#include 	<errno.h>
#include 	<string.h>
#include	<time.h>
#include	<ctype.h>
#include 	<ifaddrs.h>
#include	<stdbool.h>
#include	<stddef.h>
#include	<stdint.h>
#include	<sqlite3.h>


//===================================
//	Define CP State constant
//===================================
#define CP_STATE_UNKNOWN			0
#define CP_STATE_A					1
#define CP_STATE_B					2
#define CP_STATE_C					3
#define CP_STATE_D					4
#define CP_STATE_E					5
#define CP_STATE_F					6

//===================================
// Define start mode constant
//===================================
#define START_METHOD_FREE	 		0
#define START_METHOD_RFID	 		1
#define START_METHOD_BACKEND 		2
#define START_METHOD_BLE	 		3

//===================================
// Define Speaker type constant
//===================================
#define SPEAKER_STOP				0
#define SPEAKER_ALWAYS_ON			1
#define SPEAKER_SHORT				2
#define SPEAKER_LONG				3
#define SPEAKER_INTERVAL_SHORT		4
#define SPEAKER_INTERVAL_LONG		5
#define SPEAKER_INTERVAL_3COUNT		6

//===================================
// Define Alarm code constant
//===================================
#define ALARM_OVER_VOLTAGE          0x000001
#define ALARM_UNDER_VOLTAGE         0x000002
#define ALARM_OVER_CURRENT          0x000004
#define ALARM_OVER_TEMPERATURE      0x000008
#define ALARM_GROUND_FAIL           0x000010
#define ALARM_CP_ERROR              0x000020
#define ALARM_CURRENT_LEAK_AC       0x000040
#define ALARM_CURRENT_LEAK_DC       0x000080
#define ALARM_MCU_TESTFAIL          0x000100
#define ALARM_HANDSHAKE_TIMEOUT     0x000200
#define ALARM_EMERGENCY_STOP        0x000400
#define ALARM_RELAY_WELDING         0x000800
#define ALARM_LEAK_MODULE_FAIL      0x001000
#define ALARM_SHUTTER_FAULT         0x002000
#define ALARM_LOCKER_FAULT          0x004000
#define ALARM_POWER_DROP            0x008000
#define ALARM_CURRENT_SHORT         0x010000
#define ALARM_ROTATORY_SWITCH_FAULT 0x020000
#define ALARM_RELAY_DRIVE_FAULT     0x040000

//===================================
// Define Led constant
//===================================
#define LED_ACTION_INIT             	0
#define LED_ACTION_IDLE             	1
#define LED_ACTION_AUTHED           	2
#define LED_ACTION_CONNECTED        	3
#define LED_ACTION_CHARGING         	4
#define LED_ACTION_STOP             	5
#define LED_ACTION_ALARM            	6
#define LED_ACTION_MAINTAIN         	7
#define LED_ACTION_RFID_PASS        	8
#define LED_ACTION_RFID_FAIL        	9
#define LED_ACTION_BLE_CONNECT      	10
#define LED_ACTION_BLE_DISABLE      	11
#define LED_ACTION_DEBUG            	12
#define LED_ACTION_ALL_OFF          	13
#define LED_RELAY_ON               	 	14
#define LED_RELAY_OFF               	15
#define LED_ACTION_HANDSHAKE_FAIL   	16
#define LED_ACTION_INTERNET_DISCONNECT	17


#define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
#define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)

extern int StoreLogMsg(const char *fmt, ...);


//============================================================
// Private shared memory key define
//============================================================
#define ShmChargerKey		2001

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;
}Ac_Primary_Mcu;

typedef struct AC_PRIMARY_MCU_ALARM
{
	union
	{
		unsigned long InputAlarmCode;
		struct
		{
			unsigned long OVP:1;
			unsigned long UVP:1;
			unsigned long OCP: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 circuit_short:1;
			unsigned long set_circuit:1;
			unsigned long relay_drive_fault:1;
			unsigned long comm_timeout: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 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;
	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										isChargerStopByCondition:1;
}Gun_Info;

struct Charger
{
	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;
};

#endif /* CONFIG_MAIN_H_ */