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

#include 	<unistd.h>
#include 	<stdarg.h>
#include    <stdio.h>      /*�зǿ�J��X�w�q*/
#include    <stdlib.h>     /*�зǨ�Ʈw�w�q*/
#include    <unistd.h>     /*Unix �зǨ�Ʃw�q*/
#include    <fcntl.h>      /*�ɱ���w�q*/
#include    <termios.h>    /*PPSIX �׺ݱ���w�q*/
#include    <errno.h>      /*���~���w�q*/
#include 	<errno.h>
#include 	<string.h>
#include	<time.h>
#include	<ctype.h>
#include 	<ifaddrs.h>
#include	"../../define.h"
#include 	"Module_EvComm.h"

#define ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
#define PASS				1
#define FAIL				-1
#define START				1
#define STOP				0
#define YES					1
#define NO					0

struct SysConfigAndInfo			*ShmSysConfigAndInfo;
struct StatusCodeData 			*ShmStatusCodeData;
struct FanModuleData			*ShmFanModuleData;
struct CHAdeMOData				*ShmCHAdeMOData;
struct CcsData					*ShmCcsData;

byte gun_count;
int chargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];

// ����̤j�R�q�q���A�]�����P type �j�u�ӭ���
// Chademo : 500V, CCS : 950V
float maxChargingVol[2] = { 10000, 9500 };			// ����̤j�R�q�q���A�p�̷ӼҶ��h��W 0
// ����̤j�R�q�q�y�P��q�z�L Web
float maxChargingCur[2] = { 0, 0 };					// ����̤j�R�q�q�y�A�p�̷ӼҶ��h��W 0
float maxChargingPow = 0;							// ����̤j�R�q��q�A�p�̷ӼҶ��h��W 0

// �j��T
struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];

struct Ev_Board_Cmd Ev_Cmd={
		0,
		0x00000200,
		0x00000400,
		0x00000500,
		0x00000600,
		0x00000700,
		0x00000800,
		0x00000900,
		0x00000A00,
		0x00000C00,
		0x00000D00,

		0x00000E00,
		0x00000F00,
		0x00001000,
		0x00001100,

		0x00001200,
		0x00001400,
		0x00001500,
};

unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

void PRINTF_FUNC(char *string, ...);

void GetMaxVolAndCurMethod(byte index, float *vol, float *cur);
void GetMaxPowerMethod(float *pow);
unsigned long GetTimeoutValue(struct timeval _sour_time);

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

unsigned long GetTimeoutValue(struct timeval _sour_time)
{
	struct timeval _end_time;
	gettimeofday(&_end_time, NULL);

	return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
}

int StoreLogMsg(const char *fmt, ...)
{
	char Buf[4096+256];
	char buffer[4096];
	time_t CurrentTime;
	struct tm *tm;
	va_list args;

	va_start(args, fmt);
	int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
	va_end(args);

	memset(Buf,0,sizeof(Buf));
	CurrentTime = time(NULL);
	tm=localtime(&CurrentTime);
	sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
			buffer,
			tm->tm_year+1900,tm->tm_mon+1);
	system(Buf);

	return rc;
}

int DiffTimeb(struct timeb ST, struct timeb ET)
{
	//return milli-second
	unsigned int StartTime,StopTime;

	StartTime=(unsigned int)ST.time;
	StopTime=(unsigned int)ET.time;
	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
}

void PRINTF_FUNC(char *string, ...)
{
	va_list args;
	char buffer[4096];
	va_start(args, string);
	vsnprintf(buffer, sizeof(buffer), string, args);
	va_end(args);

	if (DEBUG)
		printf("%s \n", buffer);
	else
		DEBUG_INFO("%s \n", buffer);
}

//=================================
// Common routine
//=================================
void getTimeString(char *buff)
{
	time_t timep;
	struct tm *p;
	time(&timep);
	p=gmtime(&timep);

	sprintf(buff, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
}

bool CheckUniqNumber(byte value)
{
	for (byte index = 0; index < gun_count; index++)
	{
		if (_chargingData[index]->Evboard_id == value)
		{
			struct timeval _end_time;
			gettimeofday(&_end_time, NULL);
			unsigned long diff = 1000000 *	(_end_time.tv_sec - _id_assign_time.tv_sec) + _end_time.tv_usec - _id_assign_time.tv_usec;
			if (diff >= 3000000)
			{
				gettimeofday(&_id_assign_time, NULL);
				return true;
			}
			else
			{
				return false;
			}
		}
	}

	gettimeofday(&_id_assign_time, NULL);
	return true;
}

//==========================================
// Init all share memory
//==========================================
int InitShareMemory()
{
	int result = PASS;
	int MeterSMId;

	//initial ShmSysConfigAndInfo
	if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo),  0777)) < 0)
    {
		#ifdef SystemLogMessage
		DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
		#endif
		result = FAIL;
	}
    else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
    {
    	#ifdef SystemLogMessage
    	DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
		#endif
    	result = FAIL;
   	 }
    else
    {}

   	 //initial ShmStatusCodeData
   	 if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData),  0777)) < 0)
    {
		#ifdef SystemLogMessage
   		DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
		#endif
   		result = FAIL;
	}
    else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
    {
    	#ifdef SystemLogMessage
    	DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
		#endif
    	result = FAIL;
   	}
    else
    {}

   	if(CHAdeMO_QUANTITY > 0)
   	{
   		if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),	IPC_CREAT | 0777)) < 0)
   		{
   			#ifdef SystemLogMessage
   		   	DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
   			#endif
   			return FAIL;
   		}
   		else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
   		{
   			#ifdef SystemLogMessage
   		   	DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
   			#endif
   			return FAIL;
   		}
   		else
   		{}
   	}

   	if(CCS_QUANTITY > 0)
   	{
   		if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),	IPC_CREAT | 0777)) < 0)
   		{
   			#ifdef SystemLogMessage
   			DEBUG_ERROR("shmget ShmCcsData NG \n");
   			#endif
   			return FAIL;
   		}
   		else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
   			#ifdef SystemLogMessage
   		   	DEBUG_ERROR("shmat ShmCcsData NG \n");
   			#endif
   			return FAIL;
   		}
   		else
   		{}
   	}

    return result;
}

//================================================
// initial can-bus
//================================================
int InitCanBus()
{
	int 					s0,nbytes;
	struct timeval			tv;
	struct ifreq 			ifr0;
	struct sockaddr_can		addr0;

	system("/sbin/ip link set can0 down");
	system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
	system("/sbin/ip link set can0 up");

	s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);

	tv.tv_sec = 0;
	tv.tv_usec = 10000;
   	if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct	timeval)) < 0)
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("Set SO_RCVTIMEO NG");
		#endif
	}
	nbytes=40960;
	if (setsockopt(s0, SOL_SOCKET,  SO_RCVBUF, &nbytes, sizeof(int)) < 0)
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("Set SO_RCVBUF NG");
		#endif
	}
	nbytes=40960;
	if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("Set SO_SNDBUF NG");
		#endif
	}

   	strcpy(ifr0.ifr_name, "can0" );
	ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
	addr0.can_family = AF_CAN;
	addr0.can_ifindex = ifr0.ifr_ifindex;
	bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
	return s0;
}

//================================================
//================================================
// CANBUS receive task
//================================================
//================================================
bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
{
	for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
	{
		if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
		{
			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
			return true;
		}
	}

	for (byte index = 0; index < CCS_QUANTITY; index++)
	{
		if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
		{
			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
			return true;
		}
	}

	for (byte index = 0; index < GB_QUANTITY; index++)
	{
		if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
		{
			chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
			return true;
		}
	}

	return false;
}

void AddrAssignment(byte *data)
{
	byte target_number[8];
	byte index = 0x00;

	memcpy(target_number, data, sizeof(target_number));
	index = *(data + 4);

	if (gun_count == 1)
		index = 0x01;
	if (CheckUniqNumber(index))
	{
		PRINTF_FUNC("EV board id = %x \n", index);
//		PRINTF_FUNC("target_number[0] = %x \n", target_number[0]);
//		PRINTF_FUNC("target_number[1] = %x \n", target_number[1]);
//		PRINTF_FUNC("target_number[2] = %x \n", target_number[2]);
//		PRINTF_FUNC("target_number[3] = %x \n", target_number[3]);
//		PRINTF_FUNC("target_number[4] = %x \n", target_number[4]);

		PRINTF_FUNC("SetTargetAddr = %d, type = %d \n", index, _chargingData[index - 1]->Type);
		SetTargetAddr(target_number, index);
	}
}

void ClearAbnormalStatus_Chademo(byte gun_index)
{
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvCommFail = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryMalfun = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoNoPermission = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryIncompatibility = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOVP = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryUVP = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOTP = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryCurrentDiff = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryVoltageDiff = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoShiftPosition = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOtherFault = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargingSystemError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvNormalStop = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoTempSensorBroken = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoConnectorLockFail = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoD1OnNoReceive = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJTimeout = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeAllowTimeout = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoWaitGfdTimeout = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayTimeout = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsReqCurrentTimeout = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJOffTimeout = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayOffTimeout = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan10V = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan20V = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeBeforeStop = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetNormalStop = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoIsolationResultFail = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoMissLinkWithMotherBoard = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoOutputVolMoreThanLimit = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReqCurrentMoreThanLimit = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReCapBmsEqrCurrentExceed = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargeRemainCountDown = 0x00;
}

void ClearAbnormalStatus_CCS(byte gun_index)
{
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsRESTemperatureInhibit = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVShiftPosition = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargerConnectorLockFault = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVRESSMalfunction = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingCurrentdifferential = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingVoltageOutOfRange = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingSystemIncompatibility = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEmergencyEvent = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsBreaker = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoData = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_A = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_B = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_C = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_1 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_2 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_3 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_1 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_2 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_3 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_4 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_5 = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequenceError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSignatureError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUnknownSession = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceIDInvalid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPaymentSelectionInvalid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsIdentificationSelectionInvalid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceSelectionInvalid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateExpired = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotYetValid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoCertificateAvailable = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertChainError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertValidationError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertVerificationError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractCanceled = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChallengeInvalid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongEnergyTransferMode = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongChargeParameter = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingProfileInvalid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTariffSelectionInvalid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEPresentVoltageToLow = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryNotApplied = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMeteringSignatureNotValid = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoChargeServiceSelected = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContactorError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotAllowedAtThisEVSE = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsGAChargeStop = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAlignmentError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsACDError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAssociationError = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEChargeAbort = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoSupportedAppProtocol = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractNotAccepted = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMOUnknown = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_Prov_CertificateRevoke = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA1_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA2_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_RootCA_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_Prov_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA1_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA2_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_RootCA_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_Prov_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA1_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA2_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_RootCA_CertificateRevoked = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_SLAC_init = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_match_response = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_sequence = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_match_MNBC = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_avg_atten_calc = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_response = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_match_session = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_assoc_session = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_vald_toggle = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUDP_TT_match_join = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTCP_TT_match_join = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_amp_map_exchange = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_link_ready_notification = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSupportedAppProtocolRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionSetupRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceDiscoveryRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServicePaymentSelectionRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractAuthenticationRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargeParameterDiscoveryRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheckRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPreChargeRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCurrentDemandRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWeldingDetectionRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionStopRes = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequence_Time = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsReadyToCharge_Performance_Time = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCommunicationSetup_Performance_Time = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheck_Performance_Time = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPState_Detection_Time = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPOscillator_Retain_Time = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_TARGET_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_TARGET_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_BATTERY_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_BATTERY_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EV_STOP_EVENT = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EV_STOP_EVENT = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_STOP_EVENT = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_STOP_EVENT = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_MISC_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_MISC_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_REQUEST = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_REQUEST = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_START_BLOCK_TRANSFER = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_START_BLOCK_TRANSFER = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DATA_TRANSFER = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DATA_TRANSFER = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_FINISH = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_FINISH = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_ISOLATION_STATUS = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_ISOLATION_STATUS = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_CONNECTOR_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_CONNECTOR_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_RTC_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_RTC_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_PRECHARGE_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_PRECHARGE_INFO = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMSG_Sequence = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCAN_MSG_Unrecognized_CMD_ID = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Decode_Error = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Encode_Error = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Decode_Error = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Encode_Error = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Decode_Error = 0x00;
	ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Encode_Error = 0x00;

}

void AbnormalStopAnalysis(byte gun_index, byte *errCode)
{
	char string[7];
	sprintf(string, "%d%d%d%d%d%d", *(errCode + 5), *(errCode + 4), *(errCode + 3), *(errCode + 2), *(errCode + 1), *(errCode + 0));

	PRINTF_FUNC("NOTIFICATION_EV_STOP : Err Code = %s \n", string);

	if (strcmp(string, "023700") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvCommFail = 0x01;
	if (strcmp(string, "023701") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEvCommFail = 0x01;
	if (strcmp(string, "023702") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.GbEvCommFail = 0x01;
	if (strcmp(string, "023703") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = 0x01;
	if (strcmp(string, "023704") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryMalfun = 0x01;
	if (strcmp(string, "023705") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoNoPermission = 0x01;
	if (strcmp(string, "023706") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryIncompatibility = 0x01;
	if (strcmp(string, "023707") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOVP = 0x01;
	if (strcmp(string, "023708") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryUVP = 0x01;
	if (strcmp(string, "023709") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOTP = 0x01;
	if (strcmp(string, "023710") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryCurrentDiff = 0x01;
	if (strcmp(string, "023711") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryVoltageDiff = 0x01;
	if (strcmp(string, "023712") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoShiftPosition = 0x01;
	if (strcmp(string, "023713") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBatteryOtherFault = 0x01;
	if (strcmp(string, "023714") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargingSystemError = 0x01;
	if (strcmp(string, "023715") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoEvNormalStop = 0x01;
	if (strcmp(string, "023716") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoTempSensorBroken = 0x01;
	if (strcmp(string, "023717") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoConnectorLockFail = 0x01;
	if (strcmp(string, "023718") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoD1OnNoReceive = 0x01;
	if (strcmp(string, "023719") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJTimeout = 0x01;
	if (strcmp(string, "023720") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeAllowTimeout = 0x01;
	if (strcmp(string, "023721") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoWaitGfdTimeout = 0x01;
	if (strcmp(string, "023722") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayTimeout = 0x01;
	if (strcmp(string, "023723") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsReqCurrentTimeout = 0x01;
	if (strcmp(string, "023724") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsKtoJOffTimeout = 0x01;
	if (strcmp(string, "023725") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsEvRelayOffTimeout = 0x01;
	if (strcmp(string, "023726") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan10V = 0x01;
	if (strcmp(string, "023727") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoAdcMoreThan20V = 0x01;
	if (strcmp(string, "023728") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoBmsChargeBeforeStop = 0x01;
	if (strcmp(string, "023729") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetNormalStop = 0x01;
	if (strcmp(string, "023730") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = 0x01;
	if (strcmp(string, "023731") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoIsolationResultFail = 0x01;
	if (strcmp(string, "023732") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoMissLinkWithMotherBoard = 0x01;
	if (strcmp(string, "023733") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoOutputVolMoreThanLimit = 0x01;
	if (strcmp(string, "023734") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReqCurrentMoreThanLimit = 0x01;
	if (strcmp(string, "023735") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoReCapBmsEqrCurrentExceed = 0x01;
	if (strcmp(string, "023736") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargeRemainCountDown = 0x01;

	if (strcmp(string, "23737") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsRESTemperatureInhibit = 0x01;
	if (strcmp(string, "23738") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVShiftPosition = 0x01;
	if (strcmp(string, "23739") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargerConnectorLockFault = 0x01;
	if (strcmp(string, "23740") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVRESSMalfunction = 0x01;
	if (strcmp(string, "23741") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingCurrentdifferential = 0x01;
	if (strcmp(string, "23742") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingVoltageOutOfRange = 0x01;
	if (strcmp(string, "23743") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingSystemIncompatibility = 0x01;
	if (strcmp(string, "23744") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEmergencyEvent = 0x01;
	if (strcmp(string, "23745") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsBreaker = 0x01;
	if (strcmp(string, "23746") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoData = 0x01;
	if (strcmp(string, "23747") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_A = 0x01;
	if (strcmp(string, "23748") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_B = 0x01;
	if (strcmp(string, "23749") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_C = 0x01;
	if (strcmp(string, "23750") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_1 = 0x01;
	if (strcmp(string, "23751") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_2 = 0x01;
	if (strcmp(string, "23752") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_3 = 0x01;
	if (strcmp(string, "23753") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_1 = 0x01;
	if (strcmp(string, "23754") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_2 = 0x01;
	if (strcmp(string, "23755") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_3 = 0x01;
	if (strcmp(string, "23756") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_4 = 0x01;
	if (strcmp(string, "23757") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_5 = 0x01;
	if (strcmp(string, "23758") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequenceError = 0x01;
	if (strcmp(string, "23759") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSignatureError = 0x01;
	if (strcmp(string, "23760") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUnknownSession = 0x01;
	if (strcmp(string, "23761") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceIDInvalid = 0x01;
	if (strcmp(string, "23762") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPaymentSelectionInvalid = 0x01;
	if (strcmp(string, "23763") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsIdentificationSelectionInvalid = 0x01;
	if (strcmp(string, "23764") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceSelectionInvalid = 0x01;
	if (strcmp(string, "23765") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateExpired = 0x01;
	if (strcmp(string, "23766") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotYetValid = 0x01;
	if (strcmp(string, "23767") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateRevoked = 0x01;
	if (strcmp(string, "23768") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoCertificateAvailable = 0x01;
	if (strcmp(string, "23769") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertChainError = 0x01;
	if (strcmp(string, "23770") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertValidationError = 0x01;
	if (strcmp(string, "23771") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertVerificationError = 0x01;
	if (strcmp(string, "23772") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractCanceled = 0x01;
	if (strcmp(string, "23773") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChallengeInvalid = 0x01;
	if (strcmp(string, "23774") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongEnergyTransferMode = 0x01;
	if (strcmp(string, "23775") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongChargeParameter = 0x01;
	if (strcmp(string, "23776") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingProfileInvalid = 0x01;
	if (strcmp(string, "23777") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTariffSelectionInvalid = 0x01;
	if (strcmp(string, "23778") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEPresentVoltageToLow = 0x01;
	if (strcmp(string, "23779") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryNotApplied = 0x01;
	if (strcmp(string, "23780") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMeteringSignatureNotValid = 0x01;
	if (strcmp(string, "23781") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoChargeServiceSelected = 0x01;
	if (strcmp(string, "23782") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContactorError = 0x01;
	if (strcmp(string, "23783") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotAllowedAtThisEVSE = 0x01;
	if (strcmp(string, "23784") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsGAChargeStop = 0x01;
	if (strcmp(string, "23785") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAlignmentError = 0x01;
	if (strcmp(string, "23786") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsACDError = 0x01;
	if (strcmp(string, "23787") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAssociationError = 0x01;
	if (strcmp(string, "23788") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEChargeAbort = 0x01;
	if (strcmp(string, "23789") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoSupportedAppProtocol = 0x01;
	if (strcmp(string, "23790") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractNotAccepted = 0x01;
	if (strcmp(string, "23791") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMOUnknown = 0x01;
	if (strcmp(string, "23792") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_Prov_CertificateRevoke = 0x01;
	if (strcmp(string, "23793") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA1_CertificateRevoked = 0x01;
	if (strcmp(string, "23794") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA2_CertificateRevoked = 0x01;
	if (strcmp(string, "23795") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_RootCA_CertificateRevoked = 0x01;
	if (strcmp(string, "23796") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_Prov_CertificateRevoked = 0x01;
	if (strcmp(string, "23797") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA1_CertificateRevoked = 0x01;
	if (strcmp(string, "23798") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA2_CertificateRevoked = 0x01;
	if (strcmp(string, "23799") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_RootCA_CertificateRevoked = 0x01;
	if (strcmp(string, "23800") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_Prov_CertificateRevoked = 0x01;
	if (strcmp(string, "23801") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA1_CertificateRevoked = 0x01;
	if (strcmp(string, "23802") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA2_CertificateRevoked = 0x01;
	if (strcmp(string, "23803") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_RootCA_CertificateRevoked = 0x01;
	if (strcmp(string, "23809") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_SLAC_init = 0x01;
	if (strcmp(string, "23810") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_match_response = 0x01;
	if (strcmp(string, "23811") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_sequence = 0x01;
	if (strcmp(string, "23812") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_match_MNBC = 0x01;
	if (strcmp(string, "23813") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_avg_atten_calc = 0x01;
	if (strcmp(string, "23814") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_response = 0x01;
	if (strcmp(string, "23815") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_match_session = 0x01;
	if (strcmp(string, "23816") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_assoc_session = 0x01;
	if (strcmp(string, "23817") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_vald_toggle = 0x01;
	if (strcmp(string, "23823") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUDP_TT_match_join = 0x01;
	if (strcmp(string, "23824") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTCP_TT_match_join = 0x01;
	if (strcmp(string, "23825") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_amp_map_exchange = 0x01;
	if (strcmp(string, "23826") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_link_ready_notification = 0x01;
	if (strcmp(string, "23832") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSupportedAppProtocolRes = 0x01;
	if (strcmp(string, "23833") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionSetupRes = 0x01;
	if (strcmp(string, "23834") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceDiscoveryRes = 0x01;
	if (strcmp(string, "23835") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServicePaymentSelectionRes = 0x01;
	if (strcmp(string, "23836") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractAuthenticationRes = 0x01;
	if (strcmp(string, "23837") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargeParameterDiscoveryRes = 0x01;
	if (strcmp(string, "23838") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryRes = 0x01;
	if (strcmp(string, "23839") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheckRes = 0x01;
	if (strcmp(string, "23840") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPreChargeRes = 0x01;
	if (strcmp(string, "23841") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCurrentDemandRes = 0x01;
	if (strcmp(string, "23842") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWeldingDetectionRes = 0x01;
	if (strcmp(string, "23843") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionStopRes = 0x01;
	if (strcmp(string, "23844") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequence_Time = 0x01;
	if (strcmp(string, "23845") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsReadyToCharge_Performance_Time = 0x01;
	if (strcmp(string, "23846") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCommunicationSetup_Performance_Time = 0x01;
	if (strcmp(string, "23847") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheck_Performance_Time = 0x01;
	if (strcmp(string, "23848") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPState_Detection_Time = 0x01;
	if (strcmp(string, "23849") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPOscillator_Retain_Time = 0x01;
	if (strcmp(string, "23855") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_TARGET_INFO = 0x01;
	if (strcmp(string, "23856") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_TARGET_INFO = 0x01;
	if (strcmp(string, "23857") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_BATTERY_INFO = 0x01;
	if (strcmp(string, "23858") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_BATTERY_INFO = 0x01;
	if (strcmp(string, "23859") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EV_STOP_EVENT = 0x01;
	if (strcmp(string, "23860") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EV_STOP_EVENT = 0x01;
	if (strcmp(string, "23861") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_STOP_EVENT = 0x01;
	if (strcmp(string, "23862") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_STOP_EVENT = 0x01;
	if (strcmp(string, "23863") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_MISC_INFO = 0x01;
	if (strcmp(string, "23864") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_MISC_INFO = 0x01;
	if (strcmp(string, "23865") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_REQUEST = 0x01;
	if (strcmp(string, "23866") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_REQUEST = 0x01;
	if (strcmp(string, "23867") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_START_BLOCK_TRANSFER = 0x01;
	if (strcmp(string, "23868") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_START_BLOCK_TRANSFER = 0x01;
	if (strcmp(string, "23869") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DATA_TRANSFER = 0x01;
	if (strcmp(string, "23870") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DATA_TRANSFER = 0x01;
	if (strcmp(string, "23871") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_FINISH = 0x01;
	if (strcmp(string, "23872") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_FINISH = 0x01;
	if (strcmp(string, "23873") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_ISOLATION_STATUS = 0x01;
	if (strcmp(string, "23874") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_ISOLATION_STATUS = 0x01;
	if (strcmp(string, "23875") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_CONNECTOR_INFO = 0x01;
	if (strcmp(string, "23876") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_CONNECTOR_INFO = 0x01;
	if (strcmp(string, "23877") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_RTC_INFO = 0x01;
	if (strcmp(string, "23878") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_RTC_INFO = 0x01;
	if (strcmp(string, "23879") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_PRECHARGE_INFO = 0x01;
	if (strcmp(string, "23880") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_PRECHARGE_INFO = 0x01;
	if (strcmp(string, "23881") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMSG_Sequence = 0x01;
	if (strcmp(string, "23882") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCAN_MSG_Unrecognized_CMD_ID = 0x01;
	if (strcmp(string, "23883") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Decode_Error = 0x01;
	if (strcmp(string, "23884") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Encode_Error = 0x01;
	if (strcmp(string, "23885") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Decode_Error = 0x01;
	if (strcmp(string, "23886") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Encode_Error = 0x01;
	if (strcmp(string, "23887") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Decode_Error = 0x01;
	if (strcmp(string, "23888") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Encode_Error = 0x01;
}

void CANReceiver()
{
	pid_t canRecPid;

	canRecPid = fork();

	if(canRecPid > 0)
	{
		int nbytes;
		struct can_frame frame;
		int intCmd;
		// �j��T
		struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
		struct timeval _cmd_ack_timeout[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];

		bool isPass = false;
		gun_count = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;

		while(!isPass)
		{
			isPass = true;
			for (byte _index = 0; _index < gun_count; _index++)
			{
				if (!FindChargingInfoData(_index, &_chargingData[0]))
				{
					DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
					isPass = false;
					break;
				}
			}
		}

		for (byte _index = 0; _index < gun_count; _index++)
			gettimeofday(&_cmd_ack_timeout[_index], NULL);

		while (1)
		{
			memset(&frame, 0, sizeof(struct can_frame));
			nbytes = read(CanFd, &frame, sizeof(struct can_frame));

			for (byte _index = 0; _index < gun_count; _index++)
			{
				if (GetTimeoutValue(_cmd_ack_timeout[_index]) >= 5000000)
				{
					// ACK timeout
					//PRINTF_FUNC("gun = %x, ack timeout \n", _index);
				}
			}

			if (nbytes > 0)
			{
				byte target;
				byte targetGun = 0x00;
				intCmd = (int) (frame.can_id & CAN_EFF_MASK);

				if (intCmd == ADDRESS_REQ)
				{
					AddrAssignment(frame.data);
					continue;
				}

				intCmd = (int) (frame.can_id & CAN_EFF_MASK & 0xFFFFFF00);
				target = ((byte) (frame.can_id & 0x000000FF));		// 0x01 or 0x02

				for (byte _index = 0; _index < gun_count; _index++)
				{
					if (_chargingData[_index]->Evboard_id == target)
					{
						targetGun = _index;
						break;
					}
				}

				if(targetGun < 0 || targetGun >= CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY)
				{
					PRINTF_FUNC("EvComm (CANReceiver) : Target index = %x is < 0 or > QUANTITY \n", targetGun);
					continue;
				}
				if(intCmd == 256)
				{
					continue;
				}

				gettimeofday(&_cmd_ack_timeout[targetGun], NULL);
				switch (intCmd)
				{
					case NOTIFICATION_EV_STATUS:
					{
						_chargingData[targetGun]->ConnectorPlugIn = frame.data[0];
						_chargingData[targetGun]->PilotVoltage = frame.data[1];

						//PRINTF_FUNC("index = %d, ConnectorPlugIn = %x, data[0] = %x \n", targetGun, _chargingData[targetGun]->ConnectorPlugIn, frame.data[0]);
						//PRINTF_FUNC("ConnectorPlugIn = %x \n", (-120 + frame.data[1]) / 10);
					}
						break;
					case ACK_EV_FW_VERSION:
					{
						if (_chargingData[targetGun]->Type == _Type_Chademo)
						{
							memcpy(ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].version, frame.data, ARRAY_SIZE(frame.data));
							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].SelfTest_Comp = PASS;
							PRINTF_FUNC("chademo ver. : %s\n", ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].version);
						}
						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
						{
							if (ShmCcsData->CommProtocol == 0x01)
							{
								memcpy(&ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].version, frame.data, ARRAY_SIZE(frame.data));
								ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].SelfTest_Comp = PASS;
								PRINTF_FUNC("CCS FW = %s \n", ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].version);
							}
						}
					}
						break;
					case ACK_EV_HW_VERSION:
					{
						//PRINTF_FUNC("Get EV HW = %s \n", frame.data);
					}
						break;
					case ACK_GET_OUTPUT_REQ:
					{
						_chargingData[targetGun]->EvBatterySoc = frame.data[1];
						_chargingData[targetGun]->EvBatterytargetVoltage = ((short) frame.data[3] << 8) + (short) frame.data[2];
						_chargingData[targetGun]->EvBatterytargetCurrent = ((short) frame.data[5] << 8) + (short) frame.data[4];
						_chargingData[targetGun]->PresentChargedDuration = ((short) frame.data[7] << 8) + (short) frame.data[6];

						if (_chargingData[targetGun]->Type == _Type_Chademo)
						{
							if (ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].EvDetection != frame.data[0])
							{
								ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].PresentMsgFlowStatus = frame.data[0];
							}

							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].EvDetection = frame.data[0];
							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].SOC = _chargingData[targetGun]->EvBatterySoc;
							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].TargetBatteryVoltage = _chargingData[targetGun]->EvBatterytargetVoltage;
							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].ChargingCurrentRequest = _chargingData[targetGun]->EvBatterytargetCurrent;
						}
						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
						{
							if(ShmCcsData->CommProtocol == 0x01)
							{
								ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index].PresentMsgFlowStatus = frame.data[0];
							}
						}

						//PRINTF_FUNC("EvBatterytargetVoltage = %f \n", _chargingData[targetGun]->EvBatterytargetVoltage);
						//PRINTF_FUNC("EvBatterytargetCurrent = %f \n", _chargingData[targetGun]->EvBatterytargetCurrent);
						//PRINTF_FUNC("BatteryVoltage = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].TargetBatteryVoltage);
						//PRINTF_FUNC("CurrentRequest = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].ChargingCurrentRequest);
					}
						break;
					case ACK_GET_EV_BATTERY_INFO:
					{
						//_chargingData[target].EvACorDCcharging = frame.data[0];
						//_chargingData[target]->TotalBatteryCap = ((float) frame.data[4] << 8) + (short) frame.data[3];
						_chargingData[targetGun]->EvBatteryMaxVoltage = ((short) frame.data[4] << 8) + (short) frame.data[3];
						//_chargingData[target]->EvBatteryMaxCurrent = ((float) frame.data[4] << 8) + (short) frame.data[3];
						//_chargingData[target].MaxiBatteryCurrent = ((short) frame.data[6] << 8) + (short) frame.data[5];
						if (_chargingData[targetGun]->Type == _Type_Chademo)
						{
							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].TotalBatteryCapacity = ((short) frame.data[2] << 8) + (short) frame.data[1];
							ShmCHAdeMOData->ev[_chargingData[targetGun]->type_index].MaxiBatteryVoltage = _chargingData[targetGun]->EvBatteryMaxVoltage;

							//PRINTF_FUNC("EvBatteryMaxVoltage = %f \n", _chargingData[target]->EvBatteryMaxVoltage);
							//PRINTF_FUNC("TotalBatteryCapacity = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].TotalBatteryCapacity);
							//PRINTF_FUNC("MaxiBatteryVoltage = %d \n", ShmCHAdeMOData->ev[_chargingData[target]->type_index].MaxiBatteryVoltage);
						}
						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
						{

						}
					}
						break;
					case ACK_GET_MISCELLANEOUS_INFO:
					{
						if (_chargingData[targetGun]->Type == _Type_Chademo)
						{
							_chargingData[targetGun]->GunLocked = frame.data[0];
							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureP = frame.data[1];
							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureN = frame.data[2];
							_chargingData[targetGun]->PilotVoltage = (float)(-120 + frame.data[3]) / 10;
							ShmCHAdeMOData->evse[_chargingData[targetGun]->type_index].EvboardStatus = frame.data[7];
						}
						else if (_chargingData[targetGun]->Type == _Type_CCS_2)
						{
							if (ShmCcsData->CommProtocol == 0x01)
							{
								_chargingData[targetGun]->GunLocked = frame.data[0];
								//ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index]. .ConnectorTemperatureP = frame.data[1];
								//ShmCcsData->V2GMessage_DIN70121[_chargingData[targetGun]->type_index]. .ConnectorTemperatureN = frame.data[2];
								_chargingData[targetGun]->PilotVoltage = (float)(-120 + frame.data[3]) / 10;
							}
						}

						//PRINTF_FUNC("EvboardStatus = %x \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].EvboardStatus);
						//PRINTF_FUNC("ConnectorPlug locked = %x \n", frame.data[0]);
						//PRINTF_FUNC("ConnectorTemp 0= %d \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].ConnectorTemperatureP);
						//PRINTF_FUNC("ConnectorTemp 1= %d \n", ShmCHAdeMOData->evse[_chargingData[target]->type_index].ConnectorTemperatureN);
						//PRINTF_FUNC("PilotVoltage = %x \n", (-120 + frame.data[3]) / 10);
					}
						break;
					case ACK_EVSE_ISOLATION_STATUS:	{}
						break;
					case ACK_EVSE_PRECHAGE_INFO:
					{
						_chargingData[targetGun]->PrechargeStatus = frame.data[0];
					}
						break;
					case NOTIFICATION_EV_STOP:
					{
						// ���ݭn�D����
						// frame.data[0] : 0x01 => normal stop, 0x02 => ev emergency stop
						PRINTF_FUNC("NOTIFICATION_EV_STOP err level = %d-----------------------------\n", frame.data[0]);
						if (frame.data[0] == 0x02)
						{
							AbnormalStopAnalysis(targetGun, frame.data + 1);
						}
						_chargingData[targetGun]->StopChargeFlag = YES;
					}
						break;
					default:
						PRINTF_FUNC("EV board = %d, Ack none defined. intCmd = %d  \n", targetGun, intCmd);
						break;
				}
			}
			usleep(10000);
		}
	}
}

//================================================
// Main process
//================================================
// �ˬd Byte ���Y�� Bit ����
// _byte : �����ܪ� byte
// _bit : �� byte ���ĴX�� bit
unsigned char EvDetectionStatus(unsigned char _byte, unsigned char _bit)
{
	return ( _byte & mask_table[_bit] ) != 0x00;
}

bool IsConnectorPlugIn(struct ChargingInfoData *chargingData)
{
	return (chargingData->ConnectorPlugIn == 0x01) ? true : false;
}

void SetPresentChargingOutputPower(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
{
	float vol1 = 0, cur1 = 0;
	float vol2 = 0, cur2 = 0;

	//PRINTF_FUNC("f vol - 0 = %f \n", chargingData_1->FireChargingVoltage);
	//PRINTF_FUNC("f cur - 0 = %f \n", chargingData_1->PresentChargingCurrent);
	//PRINTF_FUNC("***********************f vol - 1 = %f \n", chargingData_2->FireChargingVoltage);
	//PRINTF_FUNC("***********************f cur - 1 = %f \n", chargingData_2->PresentChargingCurrent);

	vol1 = chargingData_1->FireChargingVoltage;
	cur1 = chargingData_1->PresentChargingCurrent;

	vol2 = chargingData_2->FireChargingVoltage;
	cur2 = chargingData_2->PresentChargingCurrent;

	SetPresentOutputPower(vol1, cur1, vol2, cur2);
}

void SetPresentChargingOutputCap(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
{
	float pow1 = 0, cur1 = 0;
	float pow2 = 0, cur2 = 0;
	float vol = 0;

	pow1 = chargingData_1->AvailableChargingPower;
	cur1 = chargingData_1->AvailableChargingCurrent;

	vol = chargingData_1->MaximumChargingVoltage;
	GetMaxVolAndCurMethod(chargingData_1->Index, &vol, &cur1);
	GetMaxPowerMethod(&pow1);

	pow2 = chargingData_2->AvailableChargingPower;
	cur2 = chargingData_2->AvailableChargingCurrent;
	vol = chargingData_2->MaximumChargingVoltage;

	GetMaxVolAndCurMethod(chargingData_2->Index, &vol, &cur2);
	GetMaxPowerMethod(&pow2);

	PRINTF_FUNC("To EV Power_1 = %f, Cur_1 = %f, Power_2 = %f, Cur_2 = %f \n", pow1, cur1, pow2, cur2);
	SetPresentOutputCapacity(pow1, cur1, pow2, cur2);
}

void Initialization()
{
	bool isPass = false;
	while(!isPass)
	{
		isPass = true;
		for (byte _index = 0; _index < gun_count; _index++)
		{
			if (!FindChargingInfoData(_index, &_chargingData[0]))
			{
				DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
				isPass = false;
				break;
			}
		}
	}
}

void GetMaxVolAndCurMethod(byte index, float *vol, float *cur)
{
	if (maxChargingVol[index] != 0 && maxChargingVol[index] <= *vol)
		*vol = maxChargingVol[index];

	if (maxChargingCur[index] != 0 && maxChargingCur[index] <= *cur)
		*cur = maxChargingCur[index];
}

void GetMaxPowerMethod(float *pow)
{
	if (maxChargingPow != 0 && maxChargingPow <= *pow)
		*pow = maxChargingPow;
}

time_t GetRtcInfoForEpoch()
{
	struct timeb csuTime;
	struct tm *tmCSU;
	struct tm t;
	time_t result;

	ftime(&csuTime);
	tmCSU = localtime(&csuTime.time);

	t.tm_year = tmCSU->tm_year;
	t.tm_mon = tmCSU->tm_mon;
	t.tm_mday = tmCSU->tm_mday;
	t.tm_hour = tmCSU->tm_hour;
	t.tm_min = tmCSU->tm_min;
	t.tm_sec = tmCSU->tm_sec;
	t.tm_isdst = -1;
	result = mktime(&t);

	return result;
}

byte GetStopChargingReasonByEvse(byte gunIndex, byte *reason)
{
	byte result = NO;

	if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == 0x01)
	{
		// 012251
		*(reason + 5)  = 0;
		*(reason + 4)  = 1;
		*(reason + 3)  = 2;
		*(reason + 2)  = 2;
		*(reason + 1)  = 5;
		*(reason + 0)  = 1;
		result = YES;
	}

	if (_chargingData[gunIndex]->Type == _Type_Chademo)
	{
		if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
		{
			// 011012
			*(reason + 5) = 0;
			*(reason + 4) = 1;
			*(reason + 3) = 1;
			*(reason + 2) = 0;
			*(reason + 1) = 1;
			*(reason + 0) = 2;
			result = YES;
		}
		else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail == YES)
		{
			// 012289
			*(reason + 5) = 0;
			*(reason + 4) = 1;
			*(reason + 3) = 2;
			*(reason + 2) = 2;
			*(reason + 1) = 8;
			*(reason + 0) = 9;
			result = YES;
		}
	}
	else if (_chargingData[gunIndex]->Type == _Type_CCS_2)
	{
		if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
		{
			// 011014
			*(reason + 5) = 0;
			*(reason + 4) = 1;
			*(reason + 3) = 1;
			*(reason + 2) = 0;
			*(reason + 1) = 1;
			*(reason + 0) = 4;
			result = YES;
		}
		else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail == YES)
		{
			// 012288
			*(reason + 5) = 0;
			*(reason + 4) = 1;
			*(reason + 3) = 2;
			*(reason + 2) = 2;
			*(reason + 1) = 8;
			*(reason + 0) = 8;
			result = YES;
		}
	}

	return result;
}

int main(int argc, char *argv[])
{
	if(InitShareMemory() == FAIL)
	{
		#ifdef SystemLogMessage
		DEBUG_ERROR("InitShareMemory NG\n");
		#endif
		if(ShmStatusCodeData != NULL)
		{
			ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
		}
		sleep(5);
		return 0;
	}

	gun_count = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
	Initialization();
	CanFd = InitCanBus();
	CANReceiver();

	byte priorityLow = 1;
	time_t rtc = GetRtcInfoForEpoch();
	while(CanFd)
	{
		for(byte _index = 0; _index < gun_count; _index++)
		{
			if (priorityLow == 1)
			{
				// �u���v���C - �u�n���^���Y���|�A�߰�
				if (_chargingData[_index]->Type == _Type_Chademo &&
						ShmCHAdeMOData->evse[_chargingData[_index]->type_index].SelfTest_Comp != PASS)
				{
					SyncRtcInfo(_index, _chargingData[_index]->Evboard_id, (int)rtc);
					GetFirmwareVersion(_index, _chargingData[_index]->Evboard_id);
					GetHardwareVersion(_index, _chargingData[_index]->Evboard_id);
				}
				else if (_chargingData[_index]->Type == _Type_CCS_2)
				{
					if (ShmCcsData->CommProtocol == 0x01 &&
						ShmCcsData->V2GMessage_DIN70121[_chargingData[_index]->type_index].SelfTest_Comp != PASS)
					{
						SyncRtcInfo(_index, _chargingData[_index]->Evboard_id, (int)rtc);
						GetFirmwareVersion(_index, _chargingData[_index]->Evboard_id);
						GetHardwareVersion(_index, _chargingData[_index]->Evboard_id);
					}
				}

				// �T�w�n���o����T : 1.�j�ꪬ�A, 2."Connector 1" �ū�, 3."Connector 2" �ū�, 4.Pilot Voltage
				//PRINTF_FUNC("GetMiscellaneousInfo. index = %d, Eid = %d \n", _index, _chargingData[_index]->Evboard_id);
				GetMiscellaneousInfo(_index, _chargingData[_index]->Evboard_id);
			}

			switch (_chargingData[_index]->SystemStatus)
			{
				case S_IDLE:
				case S_RESERVATION:
					_chargingData[_index]->PresentChargedEnergy = 0;
					_chargingData[_index]->PresentChargingPower = 0;
					_chargingData[_index]->GroundFaultStatus = GFD_WAIT;
					_chargingData[_index]->StopChargeFlag = NO;
					chargingTime[_index] = 0;

					if (_chargingData[_index]->Type == _Type_Chademo)
					{
						ClearAbnormalStatus_Chademo(_index);
					}
					else if (_chargingData[_index]->Type == _Type_CCS_2)
					{
						ClearAbnormalStatus_CCS(_index);
					}

					if (priorityLow == 1)
					{
						maxChargingCur[_index] = ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent * 10;
						maxChargingPow = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower * 10);
					}
					break;
				case S_PREPARNING:
				{
					// �]�w���e��X
					if (gun_count == 1)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
					else if (gun_count == 2)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);
				}
					break;
				case S_PREPARING_FOR_EV:
				{
					// �}�l�T�{���ݬO�_�P�N�}�l�R�q : 1.SOC, 2.Target Vol, 3.Target Cur, 4.Charging remaining time
					GetOutputReq(_index, _chargingData[_index]->Evboard_id);

//					PRINTF_FUNC("PresentChargingVoltage = %f \n", _chargingData[_index]->PresentChargingVoltage);
//					PRINTF_FUNC("PresentChargingCurrent = %f \n", _chargingData[_index]->PresentChargingCurrent);
//					PRINTF_FUNC("AvailableChargingPower = %f \n", _chargingData[_index]->AvailableChargingPower);
//					PRINTF_FUNC("AvailableChargingCurrent = %f \n", _chargingData[_index]->AvailableChargingCurrent);
//					PRINTF_FUNC("MaximumChargingVoltage = %f \n", _chargingData[_index]->MaximumChargingVoltage);

					// �]�w���e��X
					if (gun_count == 1)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
					else if (gun_count == 2)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);

					if (priorityLow == 1)
					{
						float maxVol, maxCur;
						// �κݿ�X��O
						maxVol = _chargingData[_index]->MaximumChargingVoltage;
						maxCur = _chargingData[_index]->AvailableChargingCurrent;

						GetMaxVolAndCurMethod(_index, &maxVol, &maxCur);

						PRINTF_FUNC("To EV_%d Max_Vol = %f, Cap_Cur = %f, Cap_Pow = %f \n", _index, maxVol, maxCur, _chargingData[_index]->AvailableChargingPower);
						SetChargingPermission(_index, START,
						_chargingData[_index]->AvailableChargingPower,
								maxCur,
								maxVol,
								_chargingData[_index]->Evboard_id);

						// ���o���ݹq����T : 1.AC or DC ? 2.Total battery cap, 3.Max battery vol, 4.Max battery cur
						GetEvBatteryInfo(_index, _chargingData[_index]->Evboard_id);
					}
				}
					break;
				case S_PREPARING_FOR_EVSE:
				case S_CCS_PRECHARGE_ST0:
				case S_CCS_PRECHARGE_ST1:
				{
					// �}�l�T�{���ݬO�_�P�N�}�l�R�q
					GetOutputReq(_index, _chargingData[_index]->Evboard_id);

					// �]�w���e��X
					if (gun_count == 1)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
					else if (gun_count == 2)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);

					if (priorityLow % 5 == 1)
					{
						// �κݿ�X��O����
						if (gun_count == 1)
							SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
						else if (gun_count == 2)
							SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
					}

					// ����q�� Isolation ���ժ��A
					if (priorityLow == 1)
					{
						// �� 500 V �p�G�b�@������ GFD ���ŦX�h PASS
						if(_chargingData[_index]->GroundFaultStatus != GFD_WAIT)
						{
							SetIsolationStatus(_index, _chargingData[_index]->GroundFaultStatus, _chargingData[_index]->Evboard_id);
						}

						if(_chargingData[_index]->SystemStatus == S_CCS_PRECHARGE_ST0 &&
							_chargingData[_index]->PrechargeStatus == PRECHARGE_READY)
						{
							SetEvsePrechargeInfo(_index, PRECHARGE_PRERELAY_PASS, _chargingData[_index]->Evboard_id);
						}
					}
				}
					break;
				case S_CHARGING:
				{
					// �p�� Power
					_chargingData[_index]->PresentChargingPower = ((float)((_chargingData[_index]->PresentChargingVoltage / 10) * (_chargingData[_index]->PresentChargingCurrent / 10)) / 1000);

					if (chargingTime[_index] == 0)
					{
						chargingTime[_index] = _chargingData[_index]->RemainChargingDuration;
					}
					else
					{
						int passTime = _chargingData[_index]->RemainChargingDuration - chargingTime[_index];

						if (passTime > 0)
						{
							_chargingData[_index]->PresentChargedEnergy += (_chargingData[_index]->PresentChargingPower) * passTime / 3600;
							chargingTime[_index] = _chargingData[_index]->RemainChargingDuration;
						}
					}

					// �}�l�T�{���ݬO�_�P�N�}�l�R�q
					GetOutputReq(_index, _chargingData[_index]->Evboard_id);

					// �]�w���e��X
					if (gun_count == 1)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
					else if (gun_count == 2)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);

					// for test end
					if (priorityLow % 5 == 0)
					{
						// �κݿ�X��O����
						if (gun_count == 1)
							SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
						else if (gun_count == 2)
							SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
					}

					// GFD ���ѦA�q��
					if (priorityLow == 1)
					{
						if(_chargingData[_index]->GroundFaultStatus == GFD_FAIL)
						{
							SetIsolationStatus(_index, _chargingData[_index]->GroundFaultStatus, _chargingData[_index]->Evboard_id);
						}

						if(_chargingData[_index]->Type == _Type_CCS_2 &&
							_chargingData[_index]->PrechargeStatus == PRECHARGE_READY)
						{
							SetEvsePrechargeInfo(_index, PRECHARGE_CHARELAY_PASS, _chargingData[_index]->Evboard_id);
						}
					}
				}
					break;
				case S_TERMINATING:
				{
					// �]�w���e��X
					if (gun_count == 1)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
					else if (gun_count == 2)
						SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);

					// �j���٦b�A�h�N���O�κݭn�D������
					if (_chargingData[_index]->GunLocked == START ||
							_chargingData[_index]->Type == _Type_CCS_2)
					{
						byte normalStop = 0x01;
						byte stopReason[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

						if (GetStopChargingReasonByEvse(_index, stopReason))
						{
							normalStop = 0x02;
						}

						EvseStopChargingEvent(normalStop, stopReason, _chargingData[_index]->Evboard_id);
					}
					GetOutputReq(_index, _chargingData[_index]->Evboard_id);
				}
					break;
				case S_COMPLETE:
				{
					if (priorityLow == 1)
					{
						float maxVol, maxCur;

						// �κݿ�X��O
						maxVol = _chargingData[_index]->MaximumChargingVoltage;
						maxCur = _chargingData[_index]->AvailableChargingCurrent;

						GetMaxVolAndCurMethod(_index, &maxVol, &maxCur);
						SetChargingPermission(_index, STOP,
									_chargingData[_index]->AvailableChargingPower,
									maxCur,
									maxVol,
									_chargingData[_index]->Evboard_id);
					}
				}
					break;
			}
		}
		priorityLow >= 20 ? priorityLow = 1 : priorityLow++;
		usleep(45000); //EV �p�O�q�T (50 ms)
	}
	DEBUG_INFO("Module_EvComm : Can-bus port = %d \n", CanFd);
	return FAIL;
}