#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>      /*標準輸入輸出定義*/
#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 "../../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 GBTData                  *ShmGBTData;
struct CcsData                  *ShmCcsData;

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

float _pow_1 = 0;
float _cur_1 = 0;
float _pow_2 = 0;
float _cur_2 = 0;

// 限制最大充電電壓,因應不同 type 槍線來限制
// Chademo : 500V, GB : 750, CCS : 950V
float maxChargingVol[2] = { 9500, 9500 };           // 限制最大充電電壓,如依照模塊則填上 0
// 限制最大充電電流與能量透過 Web
float maxChargingCur[2] = { 0, 0 };                 // 限制最大充電電流,如依照模塊則填上 0
float maxChargingPow = 0;                           // 限制最大充電能量,如依照模塊則填上 0

// 槍資訊
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 (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
        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(GB_QUANTITY > 0)
    {
        if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData),  IPC_CREAT | 0777)) < 0)
        {
            #ifdef SystemLogMessage
            DEBUG_ERROR("[shmget ShmGBTData NG \n");
            #endif
            return FAIL;
        }
        else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
        {
            #ifdef SystemLogMessage
            DEBUG_ERROR("shmat ShmGBTData 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_GB(byte gun_index)
{
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_LOS_CC1 = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CONNECTOR_LOCK_FAIL = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BATTERY_INCOMPATIBLE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BMS_BROAA_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CSU_PRECHARGE_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BMS_PRESENT_VOLTAGE_FAULT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BMS_VOLTAGE_OVER_RANGE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BSM_CHARGE_ALLOW_00_10MIN_COUUNTDONE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_WAIT_GROUNDFAULT_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_ADC_MORE_THAN_10V = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_ADC_MORE_THAN_60V = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CHARGER_GET_NORMAL_STOP_CMD = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CHARGER_GET_EMERGENCY_STOP_CMD = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_ISOLATION_RESULT_FAIL = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_MOTHER_BOARD_MISS_LINK = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_OUTPUT_VOLTAGE_MORE_THAN_LIMIT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_REQ_CURRENT_MORE_THAN_LIMIT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_OUTPUT_VOLTAGE_MORE_THAN_10_PERCENT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_OUTPUT_VOLTAGE_DIFF_BCS_5_PERCENT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_STOP_ADC_MORE_THAN_10V = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BHM_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BRM_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BCP_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BRO_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BCL_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BCS_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BSM_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BST_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BSD_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BEM_OTHER_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CRM_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CRMAA_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CTS_CML_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CRO_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CCS_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CST_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CSD_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_BEM_OTHER_TIMEOUT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_SOC_GOAL = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_TOTAL_VOLTAGE_GOAL = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CELL_VOLTAGE_GOAL = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_GET_CST = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_ISOLATION = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_OUTPUT_CONNECTOR_OTP = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_COMPONENT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CHARGE_CONNECTOR = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_OTP = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_OTHER = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_HIGH_V = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CC2 = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CURRENT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_VOLTAGE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GET_BST_NO_REASON = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_CELL_OVER_VOLTAGE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_CELL_UNDER_VOLTAGE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_OVER_SOC = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_UNDER_SOC = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_CURRENT = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_TEMPERATURE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_ISOLATE = 0x00;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_OUTPUT_CONNECTOR = 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;
    ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCpStatus_Error = 0x00;
}

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

    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, "023737") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsRESTemperatureInhibit = 0x01;
    if (strcmp(string, "023738") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVShiftPosition = 0x01;
    if (strcmp(string, "023739") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargerConnectorLockFault = 0x01;
    if (strcmp(string, "023740") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVRESSMalfunction = 0x01;
    if (strcmp(string, "023741") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingCurrentdifferential = 0x01;
    if (strcmp(string, "023742") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingVoltageOutOfRange = 0x01;
    if (strcmp(string, "023743") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingSystemIncompatibility = 0x01;
    if (strcmp(string, "023744") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEmergencyEvent = 0x01;
    if (strcmp(string, "023745") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsBreaker = 0x01;
    if (strcmp(string, "023746") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoData = 0x01;
    if (strcmp(string, "023747") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_A = 0x01;
    if (strcmp(string, "023748") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_B = 0x01;
    if (strcmp(string, "023749") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_DIN_C = 0x01;
    if (strcmp(string, "023750") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_1 = 0x01;
    if (strcmp(string, "023751") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_2 = 0x01;
    if (strcmp(string, "023752") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_ISO_3 = 0x01;
    if (strcmp(string, "023753") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_1 = 0x01;
    if (strcmp(string, "023754") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_2 = 0x01;
    if (strcmp(string, "023755") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_3 = 0x01;
    if (strcmp(string, "023756") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_4 = 0x01;
    if (strcmp(string, "023757") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.Ccsreserved_by_OEM_5 = 0x01;
    if (strcmp(string, "023758") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequenceError = 0x01;
    if (strcmp(string, "023759") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSignatureError = 0x01;
    if (strcmp(string, "023760") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUnknownSession = 0x01;
    if (strcmp(string, "023761") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceIDInvalid = 0x01;
    if (strcmp(string, "023762") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPaymentSelectionInvalid = 0x01;
    if (strcmp(string, "023763") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsIdentificationSelectionInvalid = 0x01;
    if (strcmp(string, "023764") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceSelectionInvalid = 0x01;
    if (strcmp(string, "023765") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateExpired = 0x01;
    if (strcmp(string, "023766") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotYetValid = 0x01;
    if (strcmp(string, "023767") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateRevoked = 0x01;
    if (strcmp(string, "023768") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoCertificateAvailable = 0x01;
    if (strcmp(string, "023769") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertChainError = 0x01;
    if (strcmp(string, "023770") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertValidationError = 0x01;
    if (strcmp(string, "023771") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertVerificationError = 0x01;
    if (strcmp(string, "023772") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractCanceled = 0x01;
    if (strcmp(string, "023773") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChallengeInvalid = 0x01;
    if (strcmp(string, "023774") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongEnergyTransferMode = 0x01;
    if (strcmp(string, "023775") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWrongChargeParameter = 0x01;
    if (strcmp(string, "023776") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargingProfileInvalid = 0x01;
    if (strcmp(string, "023777") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTariffSelectionInvalid = 0x01;
    if (strcmp(string, "023778") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEPresentVoltageToLow = 0x01;
    if (strcmp(string, "023779") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryNotApplied = 0x01;
    if (strcmp(string, "023780") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMeteringSignatureNotValid = 0x01;
    if (strcmp(string, "023781") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoChargeServiceSelected = 0x01;
    if (strcmp(string, "023782") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContactorError = 0x01;
    if (strcmp(string, "023783") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCertificateNotAllowedAtThisEVSE = 0x01;
    if (strcmp(string, "023784") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsGAChargeStop = 0x01;
    if (strcmp(string, "023785") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAlignmentError = 0x01;
    if (strcmp(string, "023786") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsACDError = 0x01;
    if (strcmp(string, "023787") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsAssociationError = 0x01;
    if (strcmp(string, "023788") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsEVSEChargeAbort = 0x01;
    if (strcmp(string, "023789") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsNoSupportedAppProtocol = 0x01;
    if (strcmp(string, "023790") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractNotAccepted = 0x01;
    if (strcmp(string, "023791") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMOUnknown = 0x01;
    if (strcmp(string, "023792") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_Prov_CertificateRevoke = 0x01;
    if (strcmp(string, "023793") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA1_CertificateRevoked = 0x01;
    if (strcmp(string, "023794") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_SubCA2_CertificateRevoked = 0x01;
    if (strcmp(string, "023795") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsOEM_RootCA_CertificateRevoked = 0x01;
    if (strcmp(string, "023796") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_Prov_CertificateRevoked = 0x01;
    if (strcmp(string, "023797") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA1_CertificateRevoked = 0x01;
    if (strcmp(string, "023798") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_SubCA2_CertificateRevoked = 0x01;
    if (strcmp(string, "023799") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMO_RootCA_CertificateRevoked = 0x01;
    if (strcmp(string, "023800") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_Prov_CertificateRevoked = 0x01;
    if (strcmp(string, "023801") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA1_CertificateRevoked = 0x01;
    if (strcmp(string, "023802") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_SubCA2_CertificateRevoked = 0x01;
    if (strcmp(string, "023803") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPS_RootCA_CertificateRevoked = 0x01;
    if (strcmp(string, "023809") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_SLAC_init = 0x01;
    if (strcmp(string, "023810") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_match_response = 0x01;
    if (strcmp(string, "023811") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_sequence = 0x01;
    if (strcmp(string, "023812") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_match_MNBC = 0x01;
    if (strcmp(string, "023813") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_avg_atten_calc = 0x01;
    if (strcmp(string, "023814") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_match_response = 0x01;
    if (strcmp(string, "023815") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_match_session = 0x01;
    if (strcmp(string, "023816") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_assoc_session = 0x01;
    if (strcmp(string, "023817") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_vald_toggle = 0x01;
    if (strcmp(string, "023823") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsUDP_TT_match_join = 0x01;
    if (strcmp(string, "023824") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTCP_TT_match_join = 0x01;
    if (strcmp(string, "023825") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_amp_map_exchange = 0x01;
    if (strcmp(string, "023826") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_link_ready_notification = 0x01;
    if (strcmp(string, "023832") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSupportedAppProtocolRes = 0x01;
    if (strcmp(string, "023833") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionSetupRes = 0x01;
    if (strcmp(string, "023834") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServiceDiscoveryRes = 0x01;
    if (strcmp(string, "023835") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsServicePaymentSelectionRes = 0x01;
    if (strcmp(string, "023836") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsContractAuthenticationRes = 0x01;
    if (strcmp(string, "023837") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsChargeParameterDiscoveryRes = 0x01;
    if (strcmp(string, "023838") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPowerDeliveryRes = 0x01;
    if (strcmp(string, "023839") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheckRes = 0x01;
    if (strcmp(string, "023840") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsPreChargeRes = 0x01;
    if (strcmp(string, "023841") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCurrentDemandRes = 0x01;
    if (strcmp(string, "023842") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsWeldingDetectionRes = 0x01;
    if (strcmp(string, "023843") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSessionStopRes = 0x01;
    if (strcmp(string, "023844") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSequence_Time = 0x01;
    if (strcmp(string, "023845") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsReadyToCharge_Performance_Time = 0x01;
    if (strcmp(string, "023846") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCommunicationSetup_Performance_Time = 0x01;
    if (strcmp(string, "023847") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCableCheck_Performance_Time = 0x01;
    if (strcmp(string, "023848") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPState_Detection_Time = 0x01;
    if (strcmp(string, "023849") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCPOscillator_Retain_Time = 0x01;
    if (strcmp(string, "023855") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_TARGET_INFO = 0x01;
    if (strcmp(string, "023856") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_TARGET_INFO = 0x01;
    if (strcmp(string, "023857") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_EV_BATTERY_INFO = 0x01;
    if (strcmp(string, "023858") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_EV_BATTERY_INFO = 0x01;
    if (strcmp(string, "023859") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EV_STOP_EVENT = 0x01;
    if (strcmp(string, "023860") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EV_STOP_EVENT = 0x01;
    if (strcmp(string, "023861") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_STOP_EVENT = 0x01;
    if (strcmp(string, "023862") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_STOP_EVENT = 0x01;
    if (strcmp(string, "023863") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_GET_MISC_INFO = 0x01;
    if (strcmp(string, "023864") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_GET_MISC_INFO = 0x01;
    if (strcmp(string, "023865") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_REQUEST = 0x01;
    if (strcmp(string, "023866") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_REQUEST = 0x01;
    if (strcmp(string, "023867") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_START_BLOCK_TRANSFER = 0x01;
    if (strcmp(string, "023868") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_START_BLOCK_TRANSFER = 0x01;
    if (strcmp(string, "023869") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DATA_TRANSFER = 0x01;
    if (strcmp(string, "023870") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DATA_TRANSFER = 0x01;
    if (strcmp(string, "023871") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_DOWNLOAD_FINISH = 0x01;
    if (strcmp(string, "023872") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_DOWNLOAD_FINISH = 0x01;
    if (strcmp(string, "023873") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_ISOLATION_STATUS = 0x01;
    if (strcmp(string, "023874") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_ISOLATION_STATUS = 0x01;
    if (strcmp(string, "023875") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_CONNECTOR_INFO = 0x01;
    if (strcmp(string, "023876") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_CONNECTOR_INFO = 0x01;
    if (strcmp(string, "023877") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_RTC_INFO = 0x01;
    if (strcmp(string, "023878") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_RTC_INFO = 0x01;
    if (strcmp(string, "023879") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTP_EVSE_PRECHARGE_INFO = 0x01;
    if (strcmp(string, "023880") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsTT_EVSE_PRECHARGE_INFO = 0x01;
    if (strcmp(string, "023881") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsMSG_Sequence = 0x01;
    if (strcmp(string, "023882") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCAN_MSG_Unrecognized_CMD_ID = 0x01;
    if (strcmp(string, "023883") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Decode_Error = 0x01;
    if (strcmp(string, "023884") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsDIN_Msg_Encode_Error = 0x01;
    if (strcmp(string, "023885") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Decode_Error = 0x01;
    if (strcmp(string, "023886") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO1_Msg_Encode_Error = 0x01;
    if (strcmp(string, "023887") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Decode_Error = 0x01;
    if (strcmp(string, "023888") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsISO2_Msg_Encode_Error = 0x01;
    if (strcmp(string, "023889") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsCpStatus_Error = 0x01;

    if (strcmp(string, "023900") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_LOS_CC1 = 0x01;
    if (strcmp(string, "023901") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CONNECTOR_LOCK_FAIL = 0x01;
    if (strcmp(string, "023902") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BATTERY_INCOMPATIBLE = 0x01;
    if (strcmp(string, "023903") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BMS_BROAA_TIMEOUT = 0x01;
    if (strcmp(string, "023904") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CSU_PRECHARGE_TIMEOUT = 0x01;
    if (strcmp(string, "023905") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BMS_PRESENT_VOLTAGE_FAULT = 0x01;
    if (strcmp(string, "023906") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BMS_VOLTAGE_OVER_RANGE = 0x01;
    if (strcmp(string, "023907") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_BSM_CHARGE_ALLOW_00_10MIN_COUUNTDONE = 0x01;
    if (strcmp(string, "023908") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_WAIT_GROUNDFAULT_TIMEOUT = 0x01;
    if (strcmp(string, "023909") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_ADC_MORE_THAN_10V = 0x01;
    if (strcmp(string, "023910") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_ADC_MORE_THAN_60V = 0x01;
    if (strcmp(string, "023911") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CHARGER_GET_NORMAL_STOP_CMD = 0x01;
    if (strcmp(string, "023912") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_CHARGER_GET_EMERGENCY_STOP_CMD = 0x01;
    if (strcmp(string, "023913") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_ISOLATION_RESULT_FAIL = 0x01;
    if (strcmp(string, "023914") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_MOTHER_BOARD_MISS_LINK = 0x01;
    if (strcmp(string, "023915") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_OUTPUT_VOLTAGE_MORE_THAN_LIMIT = 0x01;
    if (strcmp(string, "023916") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_REQ_CURRENT_MORE_THAN_LIMIT = 0x01;
    if (strcmp(string, "023917") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_OUTPUT_VOLTAGE_MORE_THAN_10_PERCENT = 0x01;
    if (strcmp(string, "023918") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_OUTPUT_VOLTAGE_DIFF_BCS_5_PERCENT = 0x01;
    if (strcmp(string, "023919") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GBT_STOP_ADC_MORE_THAN_10V = 0x01;
    if (strcmp(string, "023930") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BHM_TIMEOUT = 0x01;
    if (strcmp(string, "023931") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BRM_TIMEOUT = 0x01;
    if (strcmp(string, "023932") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BCP_TIMEOUT = 0x01;
    if (strcmp(string, "023933") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BRO_TIMEOUT = 0x01;
    if (strcmp(string, "023934") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BCL_TIMEOUT = 0x01;
    if (strcmp(string, "023935") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BCS_TIMEOUT = 0x01;
    if (strcmp(string, "023936") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BSM_TIMEOUT = 0x01;
    if (strcmp(string, "023937") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BST_TIMEOUT = 0x01;
    if (strcmp(string, "023938") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BSD_TIMEOUT = 0x01;
    if (strcmp(string, "023939") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_CEM_BEM_OTHER_TIMEOUT = 0x01;
    if (strcmp(string, "023940") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CRM_TIMEOUT = 0x01;
    if (strcmp(string, "023941") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CRMAA_TIMEOUT = 0x01;
    if (strcmp(string, "023942") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CTS_CML_TIMEOUT = 0x01;
    if (strcmp(string, "023943") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CRO_TIMEOUT = 0x01;
    if (strcmp(string, "023944") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CCS_TIMEOUT = 0x01;
    if (strcmp(string, "023945") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CST_TIMEOUT = 0x01;
    if (strcmp(string, "023946") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_CSD_TIMEOUT = 0x01;
    if (strcmp(string, "023947") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BEM_BEM_OTHER_TIMEOUT = 0x01;
    if (strcmp(string, "023950") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_SOC_GOAL = 0x01;
    if (strcmp(string, "023951") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_TOTAL_VOLTAGE_GOAL = 0x01;
    if (strcmp(string, "023952") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CELL_VOLTAGE_GOAL = 0x01;
    if (strcmp(string, "023953") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_GET_CST = 0x01;
    if (strcmp(string, "023954") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_ISOLATION = 0x01;
    if (strcmp(string, "023955") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_OUTPUT_CONNECTOR_OTP = 0x01;
    if (strcmp(string, "023956") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_COMPONENT = 0x01;
    if (strcmp(string, "023957") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CHARGE_CONNECTOR = 0x01;
    if (strcmp(string, "023958") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_OTP = 0x01;
    if (strcmp(string, "023959") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_OTHER = 0x01;
    if (strcmp(string, "023960") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_HIGH_V = 0x01;
    if (strcmp(string, "023961") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CC2 = 0x01;
    if (strcmp(string, "023962") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_CURRENT = 0x01;
    if (strcmp(string, "023963") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BST_VOLTAGE = 0x01;
    if (strcmp(string, "023964") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_GET_BST_NO_REASON = 0x01;
    if (strcmp(string, "023970") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_CELL_OVER_VOLTAGE = 0x01;
    if (strcmp(string, "023971") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_CELL_UNDER_VOLTAGE = 0x01;
    if (strcmp(string, "023972") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_OVER_SOC = 0x01;
    if (strcmp(string, "023973") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_UNDER_SOC = 0x01;
    if (strcmp(string, "023974") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_CURRENT = 0x01;
    if (strcmp(string, "023975") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_TEMPERATURE = 0x01;
    if (strcmp(string, "023976") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_ISOLATE = 0x01;
    if (strcmp(string, "023977") == 0) ShmStatusCodeData->InfoCode.InfoEvents.bits.ERROR_CODE_BSM_OUTPUT_CONNECTOR = 0x01;
}

void CANReceiver()
{
    pid_t canRecPid;

    canRecPid = fork();

    if(canRecPid > 0)
    {
        int nbytes;
        struct can_frame frame;
        int intCmd;
        // 槍資訊
        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_GB)
                        {
                            memcpy(ShmGBTData->evse[_chargingData[targetGun]->type_index].version, frame.data, ARRAY_SIZE(frame.data));
                            ShmGBTData->evse[_chargingData[targetGun]->type_index].SelfTest_Comp = PASS;
                            PRINTF_FUNC("gbt ver. : %s\n", ShmGBTData->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_GB)
                        {
                            if (ShmGBTData->ev[_chargingData[targetGun]->type_index].EvDetection != frame.data[0])
                            {
                                ShmGBTData->ev[_chargingData[targetGun]->type_index].PresentMsgFlowStatus = frame.data[0];
                            }

                            ShmGBTData->ev[_chargingData[targetGun]->type_index].EvDetection = frame.data[0];
                            ShmGBTData->ev[_chargingData[targetGun]->type_index].SOC = _chargingData[targetGun]->EvBatterySoc;
                            ShmGBTData->ev[_chargingData[targetGun]->type_index].TargetBatteryVoltage = _chargingData[targetGun]->EvBatterytargetVoltage;
                            ShmGBTData->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_GB)
                        {
                            ShmGBTData->ev[_chargingData[targetGun]->type_index].TotalBatteryCapacity = ((short) frame.data[2] << 8) + (short) frame.data[1];
                            ShmGBTData->ev[_chargingData[targetGun]->type_index].MaxiBatteryVoltage = _chargingData[targetGun]->EvBatteryMaxVoltage;
                        }
                        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_GB)
                        {
                            _chargingData[targetGun]->GunLocked = frame.data[0];
                            ShmGBTData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureP = frame.data[1];
                            ShmGBTData->evse[_chargingData[targetGun]->type_index].ConnectorTemperatureN = frame.data[2];
                            _chargingData[targetGun]->PilotVoltage = (float)(-120 + frame.data[3]) / 10;
                            ShmGBTData->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:
                    {
                        // 車端要求停止
                        // 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
//================================================
// 檢查 Byte 中某個 Bit 的值
// _byte : 欲改變的 byte
// _bit : 該 byte 的第幾個 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);

    if (_pow_1 != pow1 ||
        _cur_1 != cur1 ||
        _pow_2 != pow2 ||
        _cur_2 != cur2)
    {
        PRINTF_FUNC("To EV Power_1 = %f, Cur_1 = %f, Power_2 = %f, Cur_2 = %f \n",
                pow1, cur1, pow2, cur2);
        _pow_1 = pow1; _cur_1 = cur1; _pow_2 = pow2; _cur_2 = 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_GB)
    {
        if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
        {
            // 012290
            *(reason + 5) = 0;
            *(reason + 4) = 1;
            *(reason + 3) = 2;
            *(reason + 2) = 2;
            *(reason + 1) = 9;
            *(reason + 0) = 0;
            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)
            {
                // 優先權較低 - 只要有回應即不會再詢問
                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);
                }
                else if (_chargingData[_index]->Type == _Type_GB &&
                        ShmGBTData->evse[_chargingData[_index]->type_index].SelfTest_Comp != PASS)
                {
                    SyncRtcInfo(_index, _chargingData[_index]->Evboard_id, (int)rtc);
                    GetFirmwareVersion(_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);
                    }
                }

                // 固定要取得的資訊 : 1.槍鎖狀態, 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]->RelayK1K2Status,
                        _chargingData[_index]->PresentChargedEnergy,
                        (_chargingData[_index]->PresentChargingVoltage * 10),
                        _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_GB)
                    {
                        ClearAbnormalStatus_GB(_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:
                {
                    // 設定當前輸出
                    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:
                {
                    // 開始確認車端是否同意開始充電 : 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);

                    // 設定當前輸出
                    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;
                        // 樁端輸出能力
                        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);

                        // 取得車端電池資訊 : 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:
                {
                    // 開始確認車端是否同意開始充電
                    GetOutputReq(_index, _chargingData[_index]->Evboard_id);

                    // 設定當前輸出
                    if (gun_count == 1)
                        SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
                    else if (gun_count == 2)
                        SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);

                    if (priorityLow % 5 == 1)
                    {
                        // 樁端輸出能力改變
                        if (gun_count == 1)
                            SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
                        else if (gun_count == 2)
                            SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
                    }

                    // 持續通知 Isolation 測試狀態
                    if (priorityLow == 1)
                    {
                        // 拉 500 V 如果在一秒鐘內 GFD 都符合則 PASS
//                      if (_chargingData[_index]->FireChargingVoltage >= 3500)
//                          _chargingData[_index]->GroundFaultStatus = GFD_PASS;

                        //PRINTF_FUNC("To EV_%d GFD = %d \n",   _index, _chargingData[_index]->GroundFaultStatus);
                        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:
                {
                    // 計算 Power
                    _chargingData[_index]->PresentChargingPower = ((float)((_chargingData[_index]->PresentChargingVoltage) * (_chargingData[_index]->PresentChargingCurrent)) / 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;
                        }
                    }

                    // 開始確認車端是否同意開始充電
                    GetOutputReq(_index, _chargingData[_index]->Evboard_id);

                    // 設定當前輸出
                    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)
                    {
                        // 樁端輸出能力改變
                        if (gun_count == 1)
                            SetPresentChargingOutputCap(_chargingData[0], _chargingData[0]);
                        else if (gun_count == 2)
                            SetPresentChargingOutputCap(_chargingData[0], _chargingData[1]);
                    }

                    // GFD 失敗再通知
                    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:
                {
                    // 設定當前輸出
                    if (gun_count == 1)
                        SetPresentChargingOutputPower(_chargingData[0], _chargingData[0]);
                    else if (gun_count == 2)
                        SetPresentChargingOutputPower(_chargingData[0], _chargingData[1]);

                    // 槍鎖還在,則代表是樁端要求的停止
                    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;

                        // 樁端輸出能力
                        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 小板通訊 (50 ms)
    }
    DEBUG_INFO("Module_EvComm : Can-bus port = %d \n", CanFd);
    return FAIL;
}