123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518 |
- /*
- * Module_Dispenser.c
- *
- * Created on: 2021/10/26
- * Author: folus
- */
- #include "Module_Dispenser.h"
- //==========================
- // Timeout constant define
- //==========================
- #define TIMEOUT_SPEC_HANDSHAKING 180
- #define TIMEOUT_SPEC_AUTH 15
- #define TIMEOUT_SPEC_HANDSHAKING_LED 185
- #define TIMEOUT_SPEC_LOGPPRINTOUT 30
- #define TIMEOUT_SPEC_PROFILE_PREPARE 60
- #define TIMEOUT_SPEC_BS_HLC_HANDSHAKE 60
- #define TIMEOUT_SPEC_EV_READY 30
- #define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET 10
- #define TIMEOUT_SPEC_CCS_HANDSHAKE 120
- #define TIMEOUT_SPEC_PWN_CHANGE 5
- #define TIMEOUT_SPEC_POWERSAVING_LCD 120
- #define TIMEOUT_SPEC_POWERSAVING_RFID 120
- #define TIMEOUT_SPEC_POWERSAVING_METER 120
- #define TIMEOUT_SPEC_POWERSAVING_LED_STATUS 120
- #define TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION 15
- #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- #define YES 1
- #define NO 0
- #define ON 1
- #define OFF 0
- #define MtdBlockSize 0x300000
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct PrimaryMcuData *ShmPrimaryMcuData;
- struct DISPENSER *ShmDispenser;
- struct timespec startTime[TMR_IDX_CNT];
- struct timespec startChargingTime;
- struct timespec endChargingTime;
- struct SysConfigData SysConfigOrg;
- /*
- *
- * @param dest
- * @param src
- * @param start
- * @param cnt
- */
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
- {
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- /*
- *
- * @param fmt
- * @return
- */
- int StoreLogMsg(const char *fmt, ...)
- {
- char Buf[4096+256];
- char buffer[4096];
- time_t CurrentTime;
- struct tm *tm;
- struct timeval tv;
- 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);
- gettimeofday(&tv, NULL); // get microseconds, 10^-6
- sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]DispenserLog",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1);
- #ifdef SystemLogMessage
- system(Buf);
- #endif
- #ifdef ConsloePrintLog
- printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
- #endif
- return rc;
- }
- /*
- *
- * @param timer
- */
- void refreshStartTimer(struct timespec *timer)
- {
- clock_gettime(CLOCK_MONOTONIC, timer);
- }
- /*
- *
- * @param clk
- * @return
- */
- int getDiffSecNow(struct timespec timer)
- {
- struct timespec timerNow;
- clock_gettime(CLOCK_MONOTONIC, &timerNow);
- return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
- }
- /*
- *
- * @param start
- * @param end
- * @return
- */
- int getDiffSecBetween(struct timespec start, struct timespec end)
- {
- return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
- }
- /*
- *
- * @param result
- */
- void getDateTimeString(char* result)
- {
- time_t CurrentTime;
- struct tm *tm;
- CurrentTime = time(NULL);
- tm=localtime(&CurrentTime);
- sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
- }
- /*
- *
- * @param mode
- * @return
- */
- char* getSystemModeName(unsigned char mode)
- {
- char* result;
- switch(mode)
- {
- case SYS_MODE_BOOTING:
- result = "booting";
- break;
- case SYS_MODE_IDLE:
- result = "idle";
- break;
- case SYS_MODE_AUTHORIZING:
- result = "authorizing";
- break;
- case SYS_MODE_PREPARING:
- result = "preparing";
- break;
- case SYS_MODE_CHARGING:
- result = "charging";
- break;
- case SYS_MODE_TERMINATING:
- result = "terminating";
- break;
- case SYS_MODE_COMPLETE:
- result = "complete";
- break;
- case SYS_MODE_ALARM:
- result = "alarm";
- break;
- case SYS_MODE_FAULT:
- result = "fault";
- break;
- case SYS_MODE_MAINTAIN:
- result = "maintain";
- break;
- case SYS_MODE_RESERVATION:
- result = "reservation";
- break;
- case SYS_MODE_BOOKING:
- result = "booking";
- break;
- case SYS_MODE_DEBUG:
- result = "debug";
- break;
- case SYS_MODE_UPDATE:
- result = "upgrade";
- break;
- default:
- result = "unknown";
- break;
- }
- return result;
- }
- /*
- *
- * @param ptr
- * @return
- */
- int LoadSysConfigAndInfo(struct SysConfigData *ptr)
- {
- int fd,wrd;
- unsigned char *buf;
- unsigned int ChkSum,ChkSumOrg;
- if((buf=malloc(MtdBlockSize))==NULL)
- {
- DEBUG_ERROR("malloc buffer NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, MtdBlockSize);
- //================================================
- // Load configuration from mtdblock10
- //================================================
- system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
- fd = open("/mnt/EvseConfig.bin", O_RDWR);
- if (fd < 0)
- {
- free(buf);
- DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- wrd=read(fd, buf, MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- free(buf);
- DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- ChkSum=0;
- for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
- {
- ChkSum+=buf[wrd];
- }
- memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
- memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
- memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
- //================================================
- // Load configuration from mtdblock11
- //================================================
- if(ChkSum!=ChkSumOrg)
- {
- DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
- system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
- fd = open("/mnt/EvseConfig.bin", O_RDWR);
- if (fd < 0)
- {
- free(buf);
- DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, MtdBlockSize);
- wrd=read(fd, buf,MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- free(buf);
- DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- ChkSum=0;
- for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
- {
- ChkSum+=buf[wrd];
- }
- memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
- //================================================
- // Load configuration from mtdblock12 (Factory default)
- //================================================
- if(ChkSum!=ChkSumOrg)
- {
- DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
- system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
- fd = open("/mnt/EvseConfig.bin", O_RDWR);
- if (fd < 0)
- {
- DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
- free(buf);
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, MtdBlockSize);
- wrd=read(fd, buf,MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
- free(buf);
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- ChkSum=0;
- for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
- {
- ChkSum+=buf[wrd];
- }
- memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
- memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
- memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
- if(ChkSum!=ChkSumOrg)
- {
- DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
- free(buf);
- system("cd /root;./Module_FactoryConfig -m");
- system("rm -f /Storage/OCPP/OCPPConfiguration");
- system("sync");
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- return FAIL;
- }
- }
- }
- //load OK
- memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
- free(buf);
- system("rm -f /mnt/EvseConfig.bin");
- // SysConfig in flash is empty (0xffffffff)
- if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
- (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
- (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
- (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
- {
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
- {
- memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
- {
- memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
- {
- memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
- }
- if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
- {
- DEBUG_INFO("Ethernet dhcp config is null.\n");
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
- {
- DEBUG_INFO("Model name over length.\n");
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
- {
- DEBUG_INFO("Model serial number over length.\n");
- }
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
- {
- DEBUG_INFO("SystemId over length.\n");
- }
- system("cd /root;./Module_FactoryConfig -m");
- sleep(3);
- system("/usr/bin/run_evse_restart.sh");
- }
- DEBUG_INFO("Load SysConfigData OK\n");
- return PASS;
- }
- /*
- *
- * @param UsrData
- * @return
- */
- int StoreUsrConfigData(struct SysConfigData *UsrData)
- {
- int result = PASS;
- int fd,wrd;
- unsigned int i,Chk;
- unsigned char *ptr, *BufTmp;
- Chk=0;
- ptr=(unsigned char *)UsrData;
- if((BufTmp=malloc(MtdBlockSize))!=NULL)
- {
- memset(BufTmp,0,MtdBlockSize);
- memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
- for(i=0;i<MtdBlockSize-4;i++)
- Chk+=*(BufTmp+i);
- memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
- // Output configuration to file.
- fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
- if (fd < 0)
- {
- DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
- free(BufTmp);
- return 0;
- }
- wrd=write(fd, BufTmp, MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
- free(BufTmp);
- return 0;
- }
- DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
- DEBUG_INFO("Erase /dev/mtd10.\n");
- runShellCmd("flash_erase /dev/mtd10 0 0");
- DEBUG_INFO("Write /dev/mtd10.\n");
- runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
- DEBUG_INFO("Erase /dev/mtd11.\n");
- runShellCmd("flash_erase /dev/mtd11 0 0");
- DEBUG_INFO("Write /dev/mtd11.\n");
- runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
- system("rm -f /mnt/EvseConfig.bin");
- DEBUG_INFO("EvseConfig write to flash OK\n");
- }
- else
- {
- DEBUG_ERROR("alloc BlockSize NG\r\n");
- result = FAIL;
- }
- if(BufTmp!=NULL)
- free(BufTmp);
- return result;
- }
- /*
- *
- * @param gun_index
- * @param mode
- * @return
- */
- unsigned char isMode(unsigned char mode)
- {
- return ((ShmDispenser->gun_info.SystemStatus == mode)?YES:NO);
- }
- /*
- *
- * @param gun_index
- * @return
- */
- unsigned char isModeChange()
- {
- unsigned char result = NO;
- if(!isMode(ShmDispenser->gun_info.PreviousSystemStatus))
- {
- result = YES;
- ShmDispenser->gun_info.PreviousSystemStatus = ShmDispenser->gun_info.SystemStatus;
- }
- return result;
- }
- /*
- *
- * @param gun_index
- * @param mode
- */
- void setChargerMode(unsigned char mode)
- {
- ShmDispenser->gun_info.PreviousSystemStatus = ShmDispenser->gun_info.SystemStatus;
- ShmDispenser->gun_info.SystemStatus = mode;
- DEBUG_INFO("System mode switch from %s to %s\n", getSystemModeName(ShmDispenser->gun_info.PreviousSystemStatus), getSystemModeName(ShmDispenser->gun_info.SystemStatus));
- }
- /*
- *
- * @return
- */
- int getRequest()
- {
- return ShmDispenser->gun_info.legacyRequest.isLegacyRequest;
- }
- /*
- *
- * @param gun_index
- * @return
- */
- int getRelay()
- {
- return ShmDispenser->gun_info.legacyRequest.isRelayOn;
- }
- /*
- *
- * @param gun_index
- * @return
- */
- int presentChargedEnergyClear()
- {
- int result = FAIL;
- ShmDispenser->gun_info.PresentChargedEnergy = 0;
- memset(ShmDispenser->gun_info.presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmDispenser->gun_info.presentChargedEnergyPeriod)*sizeof(float));
- result = PASS;
- return result;
- }
- /*
- *
- * @param gun_index
- * @return
- */
- float presentChargedEnergyTotal()
- {
- float result = 0.0f;
- for(int idx=0;idx<ARRAY_SIZE(ShmDispenser->gun_info.presentChargedEnergyPeriod);idx++)
- {
- result += ShmDispenser->gun_info.presentChargedEnergyPeriod[idx];
- }
- return result;
- }
- /*
- *
- * @param gun_index
- * @return
- */
- int presentChargedEnergyUpdate()
- {
- int result = FAIL;
- time_t CurrentTime;
- struct tm *tm;
- CurrentTime = time(NULL);
- tm=localtime(&CurrentTime);
- if(ShmDispenser->ConfigData.AcPhaseCount==1)
- {
- // Resolution: 0.0001 kwh
- ShmDispenser->gun_info.presentChargedEnergyPeriod[tm->tm_hour] += (((float)(ShmDispenser->gun_info.powerConsumptionTotal.power_consumption - ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start))/10000.0) - presentChargedEnergyTotal();
- }
- else
- {
- // Resolution: 0.0001 kwh
- ShmDispenser->gun_info.presentChargedEnergyPeriod[tm->tm_hour] += ((((float)(ShmDispenser->gun_info.powerConsumption[0].power_consumption - ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start))/10000.0) +
- (((float)(ShmDispenser->gun_info.powerConsumption[1].power_consumption - ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start))/10000.0) +
- (((float)(ShmDispenser->gun_info.powerConsumption[2].power_consumption - ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start))/10000.0))
- - presentChargedEnergyTotal();
- }
- ShmDispenser->gun_info.PresentChargedEnergy = presentChargedEnergyTotal();
- return result;
- }
- /*
- * Init all share memory
- * @return
- */
- int InitShareMemory()
- {
- int result = PASS;
- int MeterSMId;
- //Initial ShmSysConfigAndInfo
- if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
- result = FAIL;
- }
- else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
- result = FAIL;
- }
- else
- {}
- //Initial ShmStatusCodeData
- if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
- result = FAIL;
- }
- else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
- result = FAIL;
- }
- else
- {}
- //Initial ShmSDispenser
- if ((MeterSMId = shmget(ShmDispenserKey, sizeof(struct DISPENSER), 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmSDispenser NG\n");
- result = FAIL;
- }
- else if ((ShmDispenser = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmSDispenser NG\n");
- result = FAIL;
- }
- return result;
- }
- /*
- * TODO:
- * 1. Upgrade firmware
- */
- int upgrade_check()
- {
- int result = PASS;
- int fd;
- DIR *dir;
- struct dirent *file;
- char cmd[512];
- long int MaxLen=48*1024*1024;
- if ((dir = opendir ("/mnt")) != NULL)
- {
- /* print all the files and directories within directory */
- while ((file = readdir (dir)) != NULL)
- {
- if((strlen(file->d_name)>2))
- {
- // Wait for MCU upgrade finish.
- while(ShmDispenser->gun_info.mcuFlag.isMcuUpgradeReq)sleep(1);
- memset(&ShmDispenser->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
- DEBUG_INFO("New firmware file: %s\n", file->d_name);
- sprintf(ShmDispenser->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
- if((fd=open(ShmDispenser->fwUpgradeInfo.location, O_RDONLY)) >= 0)
- {
- unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
- memset(ptr, 0xFF, MaxLen); //-48 is take out the header
- read(fd, ptr, MaxLen);
- close(fd);
- ShmDispenser->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
- substr(ShmDispenser->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
- DEBUG_INFO("New firmware type: %X\n", ShmDispenser->fwUpgradeInfo.fwType);
- DEBUG_INFO("New firmware model name: %s, %s\n", ShmDispenser->fwUpgradeInfo.modelName, ShmDispenser->ConfigData.ModelName);
- if((ShmDispenser->fwUpgradeInfo.modelName[0] == ShmDispenser->ConfigData.ModelName[0]) &&
- (ShmDispenser->fwUpgradeInfo.modelName[1] == ShmDispenser->ConfigData.ModelName[1]) &&
- (ShmDispenser->fwUpgradeInfo.modelName[7] == ShmDispenser->ConfigData.ModelName[7]) &&
- (ShmDispenser->fwUpgradeInfo.modelName[8] == ShmDispenser->ConfigData.ModelName[8]) &&
- (ShmDispenser->fwUpgradeInfo.modelName[9] == ShmDispenser->ConfigData.ModelName[9]) &&
- (ShmDispenser->fwUpgradeInfo.modelName[11] == ShmDispenser->ConfigData.ModelName[11]) &&
- (ShmDispenser->fwUpgradeInfo.modelName[12] == ShmDispenser->ConfigData.ModelName[12]) &&
- (ShmDispenser->fwUpgradeInfo.modelName[13] == ShmDispenser->ConfigData.ModelName[13]) &&
- (ShmDispenser->fwUpgradeInfo.fwType>0))
- {
- switch(ShmDispenser->fwUpgradeInfo.fwType)
- {
- case CSU_MLO:
- case CSU_BOOTLOADER:
- case CSU_KERNEL_CONFIGURATION:
- case CSU_KERNEL_IMAGE:
- case CSU_ROOT_FILE_SYSTEM:
- case CSU_USER_CONFIGURATION:
- case CSU_PRIMARY_CONTROLLER:
- if(Upgrade_Flash(ShmDispenser->fwUpgradeInfo.fwType, ShmDispenser->fwUpgradeInfo.location, ShmDispenser->fwUpgradeInfo.modelName) != PASS)
- {
- result = FAIL;
- }
- else
- {
- if(ShmDispenser->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
- {
- DEBUG_INFO("Restore model name & serial number.\n");
- memcpy(&SysConfigOrg, &ShmDispenser->ConfigData, sizeof(struct SysConfigData));
- if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
- {
- DEBUG_INFO("Re-load configuration fail.\n");
- result = FAIL;
- }
- else
- {
- memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
- memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
- memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
- if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
- {
- DEBUG_INFO("Re-write configuration fail.\n");
- result = FAIL;
- }
- else
- DEBUG_INFO("Re-write configuration OK.\n");
- }
- }
- }
- sprintf(cmd, "yes|rm %s", ShmDispenser->fwUpgradeInfo.location);
- system(cmd);
- break;
- case AC_WALLMOUNT_CONTROLLER:
- ShmDispenser->gun_info.mcuFlag.isMcuUpgradeReq = ON;
- sleep(10);
- break;
- default:
- result = FAIL;
- DEBUG_WARN("Image file is unknown type.\n");
- sprintf(cmd, "yes|rm %s", ShmDispenser->fwUpgradeInfo.location);
- system(cmd);
- break;
- }
- }
- else
- {
- result = FAIL;
- DEBUG_ERROR("Model name and Firmware type error.\n");
- sprintf(cmd, "yes|rm %s", ShmDispenser->fwUpgradeInfo.location);
- system(cmd);
- }
- free(ptr);
- }
- else
- {
- result = FAIL;
- DEBUG_ERROR("New firmware open error.\n");
- }
- }
- else
- {
- if(strlen(file->d_name) >= 3)
- {
- result = FAIL;
- DEBUG_ERROR("File name error.\n");
- }
- else
- {
- DEBUG_ERROR("Searching file.\n");
- }
- }
- }
- closedir (dir);
- }
- else
- {
- result = FAIL;
- DEBUG_ERROR("/mnt does not valid.\n");
- }
- return result;
- }
- /*
- *
- * @return
- */
- int main(void)
- {
- // Initial share memory
- if(InitShareMemory() == FAIL)
- {
- DEBUG_ERROR("InitShareMemory NG\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
- }
- sleep(5);
- return 0;
- }
- ShmDispenser->gun_info.PreviousSystemStatus = 0xff;
- ShmDispenser->gun_info.primaryMcuState.rotatory_switch = 0xff;
- ShmDispenser->gun_info.mcuResetRequest.isMcuResetRequest = ON;
- ShmDispenser->gun_info.isSetBreatheLedTiming = OFF;
- ShmDispenser->gun_info.isSetLedBrightness = OFF;
- DEBUG_INFO("Module_Dispenser initialized...\n");
- for(;;)
- {
- //==========================================
- // Check initialization "PASS" or "FAIL"
- //==========================================
- if(ShmDispenser->gun_info.SystemStatus != SYS_MODE_BOOTING)
- {
- // Alarm event check
- if((ShmDispenser->gun_info.primaryMcuAlarm.InputAlarmCode > 0))
- {
- if(ShmDispenser->gun_info.SystemStatus != SYS_MODE_ALARM)
- {
- if(ShmDispenser->gun_info.SystemStatus != SYS_MODE_UPDATE)
- {
- setChargerMode(SYS_MODE_ALARM);
- }
- }
- }
- }
- //==========================================
- // Switch operative
- //==========================================
- if(ShmDispenser->gun_info.isOperactive)
- {
- if(isMode(SYS_MODE_MAINTAIN))
- {
- setChargerMode(SYS_MODE_IDLE);
- }
- }
- else
- {
- if(isMode(SYS_MODE_IDLE))
- {
- setChargerMode(SYS_MODE_MAINTAIN);
- }
- }
- //==========================================
- // Upgrade check
- //==========================================
- if(ShmDispenser->gun_info.isUpgradeReq && !ShmDispenser->gun_info.isUpgradeEnd)
- {
- if((ShmDispenser->gun_info.SystemStatus == SYS_MODE_IDLE) || (ShmDispenser->gun_info.SystemStatus == SYS_MODE_ALARM))
- {
- if((ShmDispenser->gun_info.isUpgradePASS = upgrade_check()) == PASS)
- {
- DEBUG_INFO("Upgrade process pass.\n");
- }
- else
- {
- DEBUG_ERROR("Upgrade process fail.\n");
- }
- ShmDispenser->gun_info.isUpgradeEnd = ON;
- }
- }
- //==========================================
- // Gun process
- //==========================================
- switch(ShmDispenser->gun_info.SystemStatus)
- {
- case SYS_MODE_BOOTING:
- if(isModeChange())
- {}
- if(ShmDispenser->gun_info.mcuFlag.isReadFwVerPass &&
- ShmDispenser->gun_info.mcuFlag.isSetModelNamePass &&
- ShmDispenser->gun_info.mcuFlag.isSetSerialNumberPass)
- {
- // Set max current to rating current
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ShmDispenser->gun_info.primaryMcuState.rating_current;
- // If rotate switch equal zero, the system needs to change Debug mode
- if(ShmDispenser->gun_info.primaryMcuState.rotatory_switch == 0)
- setChargerMode(SYS_MODE_DEBUG);
- else
- setChargerMode(SYS_MODE_IDLE);
- }
- break;
- case SYS_MODE_IDLE:
- if(isModeChange())
- {
- ShmDispenser->gun_info.isGunPlugged = NO;
- ShmDispenser->gun_info.systemAlarmCode.SystemAlarmCode = 0x00;
- ShmDispenser->gun_info.PresentChargedDuration = 0;
- presentChargedEnergyClear();
- ShmDispenser->gun_info.targetCurrent = 0xFF;
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_IDLE;
- ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
- ShmDispenser->gun_info.isDoEvReadyOnce = OFF;
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- if(ShmDispenser->isCcsEnable)system("pkill Module_CCS");
- }
- if(ShmDispenser->gun_info.legacyRequest.isLegacyRequest)
- {
- ShmDispenser->gun_info.isGunPlugged = NO;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
- if(ShmDispenser->isCcsEnable)
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
- }
- else
- {
- if((6 <= ShmDispenser->gun_info.targetCurrent))
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
- }
- else if((1 <= ShmDispenser->gun_info.targetCurrent) && (ShmDispenser->gun_info.targetCurrent < 6))
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 6;
- }
- else
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 100;
- }
- }
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
- setChargerMode(SYS_MODE_AUTHORIZING);
- }
- else
- {}
- break;
- case SYS_MODE_AUTHORIZING:
- if(isModeChange())
- {}
- if(ShmDispenser->isCcsEnable)
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- sleep(4);
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- system("/root/Module_CCS &");
- }
- setChargerMode(SYS_MODE_PREPARING);
- break;
- case SYS_MODE_PREPARING:
- if(isModeChange())
- {
- refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
- if(ShmDispenser->isCcsEnable)
- {
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_DUTY_5;
- }
- else
- {
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
- ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
- }
- }
- // If control pilot detect Bx, skip watch dog time out.
- if((ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_B) ||
- (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_C))
- {
- ShmDispenser->gun_info.isGunPlugged = YES;
- switch(ShmDispenser->gun_info.ccsHandshakeState)
- {
- case HANDSHAKE_DUTY_5:
- //Let CCS task start to negotiate
- ShmDispenser->gun_info.acCcsInfo.ChargingPermission = ON;
- ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_NONE;
- // Set CCS 5% PWM duty
- if(ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
- }
- break;
- case HANDSHAKE_DUTY_5_CHECK:
- if((ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF))
- {
- //2 secs timeout
- refreshStartTimer(&startTime[TMR_IDX_BS_HLC_HANDSHAKE]);
- DEBUG_INFO("HLC slac handshake start.\n");
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CCS;
- }
- break;
- case HANDSHAKE_CCS:
- //CCS handshake timeout
- if(getDiffSecNow(startTime[TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
- {
- ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
- ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
- DEBUG_INFO("HLC %d secs slac handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
- }
- if((ShmDispenser->gun_info.acCcsInfo.ChargingPermission == OFF) && (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
- {
- DEBUG_INFO("Wait CCS give up negotiagting.\n");
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
- ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
- }
- //CCS status check
- if((16 < ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus) && (ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus < 254))
- {
- ShmDispenser->gun_info.chargingMode = CHARGING_MODE_HLC;
- DEBUG_INFO("Enter HLC Mode charging.\n");
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_HLC_MODE;
- refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
- }
- break;
- case HANDSHAKE_CP_STATE_E:
- if(ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)
- {
- if(ShmDispenser->isCcsEnable)
- {
- DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
- //CP STATE E for 4 secs
- sleep(4);
- }
- //restore normal CP PWM duty
- // Determine max charging current to MCU
- DEBUG_INFO("Determine max charging current to MCU.\n");
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
- refreshStartTimer(&startTime[TMR_IDX_BS_HLC_HANDSHAKE]);
- }
- break;
- case HANDSHAKE_SET_MAX_CURRENT:
- if(ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty == OFF)
- {
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_BS_MODE;
- DEBUG_INFO("Enter BS Mode charging.\n");
- //for EV READY 30 secs didn't start charging to STATE E
- refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
- }
- break;
- case HANDSHAKE_BS_MODE:
- refreshStartTimer(&startTime[TMR_IDX_HANDSHAKING]);
- presentChargedEnergyClear();
- getDateTimeString((char*)ShmDispenser->gun_info.StartDateTime);
- ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start = ShmDispenser->gun_info.powerConsumptionTotal.power_consumption;
- ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[0].power_consumption;
- ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[1].power_consumption;
- ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[2].power_consumption;
- refreshStartTimer(&startChargingTime);
- setChargerMode(SYS_MODE_CHARGING);
- //EV READY CHECK
- /*
- if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
- {
- if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
- {
- DEBUG_INFO("EV READY STATE E 4sec.\n");
- ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
- ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
- }
- }
- if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
- {
- switch(ShmCharger->gun_info[gun_index].evReadyState)
- {
- case EV_READY_STAT_E:
- if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
- {
- sleep(4);
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
- ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
- }
- break;
- case EV_READY_STAT_C:
- if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
- {
- usleep(500000);
- DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
- if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
- if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
- {
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
- }
- }
- else
- {
- ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
- if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
- {
- ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
- }
- }
- ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
- ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
- }
- break;
- case EV_READY_SET_MAX_CURRENT:
- if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
- {
- ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
- ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
- }
- break;
- default:
- break;
- }
- }*/
- break;
- case HANDSHAKE_HLC_MODE:
- if(ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_START_MODE)
- {
- ShmDispenser->gun_info.isCCSStartTransation = ON;
- }
- if((ShmDispenser->gun_info.isCCSStartTransation == ON))
- {
- presentChargedEnergyClear();
- getDateTimeString((char*)ShmDispenser->gun_info.StartDateTime);
- ShmDispenser->gun_info.powerConsumptionTotal.power_consumption_at_start = ShmDispenser->gun_info.powerConsumptionTotal.power_consumption;
- ShmDispenser->gun_info.powerConsumption[0].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[0].power_consumption;
- ShmDispenser->gun_info.powerConsumption[1].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[1].power_consumption;
- ShmDispenser->gun_info.powerConsumption[2].power_consumption_at_start = ShmDispenser->gun_info.powerConsumption[2].power_consumption;
- ShmDispenser->gun_info.isCCSStartTransation = OFF;
- setChargerMode(SYS_MODE_CHARGING);
- refreshStartTimer(&startChargingTime);
- refreshStartTimer(&startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
- }
- //120 sec timeout
- if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
- {
- if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5)
- {
- DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
- //Cancel CCS task negotiating
- ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
- ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
- //ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
- if((ShmDispenser->gun_info.acCcsInfo.ChargingPermission == OFF) && (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
- {
- DEBUG_INFO("Wait CCS give up negotiagting.\n");
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- ShmDispenser->gun_info.ccsHandshakeState = HANDSHAKE_CP_STATE_E;
- ShmDispenser->gun_info.chargingMode = CHARGING_MODE_BS;
- }
- }
- }
- break;
- default:
- break;
- }
- }
- else if(ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEMode)
- {
- DEBUG_INFO("Enter Socket-E Mode charging.\n");
- ShmDispenser->gun_info.chargingMode = CHARGING_MODE_SOCKETE;
- refreshStartTimer(&startChargingTime);
- setChargerMode(SYS_MODE_CHARGING);
- }
- // Unplug charging gun to Idle mode
- if((ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A) && (ShmDispenser->gun_info.isGunPlugged == YES))
- {
- DEBUG_INFO("Charging gun is plugged before.\n");
- //Cancel CCS task negotiating
- ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
- ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
- ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
- }
- if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > ShmDispenser->timeoutSpec.Present_Timeout_Spec)
- {
- if(getDiffSecNow(startTime[TMR_IDX_HANDSHAKING]) > (ShmDispenser->timeoutSpec.Present_Timeout_Spec+5))
- {
- DEBUG_INFO("Handshaking timeout...\n");
- //Cancel CCS task negotiating
- ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
- ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
- ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
- }
- }
- if(((ShmDispenser->gun_info.isCCSWaitChangeDuty == ON) && ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5) ||
- !ShmDispenser->gun_info.legacyRequest.isLegacyRequest)
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- ShmDispenser->gun_info.isCCSWaitChangeDuty = OFF;
- setChargerMode(SYS_MODE_IDLE);
- }
- break;
- case SYS_MODE_CHARGING:
- if(isModeChange())
- {
- refreshStartTimer(&startTime[TMR_IDX_LOGPPRINTOUT]);
- refreshStartTimer(&startTime[TMR_IDX_PROFILE_PREPARE]);
- refreshStartTimer(&startTime[TMR_IDX_PWN_CHANGE]);
- ShmDispenser->gun_info.isChargerStopByCondition = NO;
- ShmDispenser->gun_info.resultAuthorization = DEFAULT_RFID;
- }
- //if time up, clear CCS MSG count
- if((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) &&
- (getDiffSecNow(startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
- {
- refreshStartTimer(&startTime[TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
- if(ShmDispenser->gun_info.acCcsInfo.CcsHeartBeat > 0)
- {
- ShmDispenser->gun_info.acCcsInfo.CcsHeartBeat = 0;
- }
- else
- {
- DEBUG_INFO("CCS could not get MSG from car.\n");
- //setChargerMode(SYS_MODE_TERMINATING);
- //setRelay(OFF);
- }
- }
- if(!ShmDispenser->gun_info.legacyRequest.isLegacyRequest ||
- ((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && ShmDispenser->gun_info.primaryMcuState.cp_state != CP_STATE_C) ||
- ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && !ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn) ||
- ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
- ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
- ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
- {
- setChargerMode(SYS_MODE_TERMINATING);
- //setRelay(gun_index, OFF);
- }
- else
- {
- // Charging session info calculation
- refreshStartTimer(&endChargingTime);
- ShmDispenser->gun_info.PresentChargedDuration = getDiffSecBetween(startChargingTime, endChargingTime);
- presentChargedEnergyUpdate();
- // Determine max charging current to MCU
- if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS)
- {
- if(getDiffSecNow(startTime[TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
- {
- if((6 <= ShmDispenser->gun_info.targetCurrent))
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = ((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
- }
- else if((1 <= ShmDispenser->gun_info.targetCurrent) && (ShmDispenser->gun_info.targetCurrent < 6))
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 6;
- }
- else
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = 100;
- }
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = YES;
- refreshStartTimer(&startTime[TMR_IDX_PWN_CHANGE]);
- }
- }
- else if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC)
- {
- ShmDispenser->gun_info.acCcsInfo.EVSEMaxCurrent = (float)((ShmDispenser->gun_info.targetCurrent > ShmDispenser->gun_info.primaryMcuState.rating_current)?ShmDispenser->gun_info.primaryMcuState.rating_current:ShmDispenser->gun_info.targetCurrent);
- }
- // Debug information
- if(getDiffSecNow(startTime[TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
- {
- DEBUG_INFO("================================================\n");
- DEBUG_INFO("gun_info.primaryMcuCp_Pwn_Duty.max_current: %d\n", ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current);
- DEBUG_INFO("gun_info.targetCurrent: %d\n", ShmDispenser->gun_info.targetCurrent);
- DEBUG_INFO("================================================\n");
- refreshStartTimer(&startTime[TMR_IDX_LOGPPRINTOUT]);
- getDateTimeString((char*)ShmDispenser->gun_info.StopDateTime);
- }
- }
- break;
- case SYS_MODE_TERMINATING:
- if(isModeChange())
- {
- getDateTimeString((char*)ShmDispenser->gun_info.StopDateTime);
- }
- refreshStartTimer(&endChargingTime);
- if(ShmDispenser->gun_info.PresentChargedDuration != 0)
- {
- ShmDispenser->gun_info.PresentChargedDuration = getDiffSecBetween(startChargingTime, endChargingTime);
- }
- // End authorize pass
- if(!ShmDispenser->gun_info.legacyRequest.isLegacyRequest ||
- ((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A)) ||
- ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && !ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn) ||
- ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
- {
- if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS)
- {
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- sleep(1);
- setChargerMode(SYS_MODE_COMPLETE);
- }
- else if(ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC)
- {
- //setChargerMode(gun_index, SYS_MODE_COMPLETE);
- //Cancel CCS task negotiating
- ShmDispenser->gun_info.acCcsInfo.ChargingPermission = OFF;
- ShmDispenser->gun_info.acCcsInfo.EVSENotification = NOTIFICATION_STOP;
- ShmDispenser->gun_info.isCCSWaitChangeDuty = ON;
- }
- else
- {
- setChargerMode(SYS_MODE_COMPLETE);
- }
- }
- else
- {
- // Debug information
- if(getDiffSecNow(startTime[TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
- {
- DEBUG_INFO("=============================================================\n");
- DEBUG_INFO("ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current: %d\n", ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current);
- DEBUG_INFO("ShmDispenser->gun_info.targetCurrent: %d\n", ShmDispenser->gun_info.targetCurrent);
- DEBUG_INFO("=============================================================\n");
- refreshStartTimer(&startTime[TMR_IDX_LOGPPRINTOUT]);
- }
- if(ShmDispenser->gun_info.legacyRequest.isLegacyRequest &&
- (((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_C)) || ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn)) &&
- (((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) && (ShmDispenser->gun_info.acCcsInfo.EVChargeProgress == HLC_START_MODE)) || (ShmDispenser->gun_info.chargingMode == CHARGING_MODE_BS))
- )
- {
- setChargerMode(SYS_MODE_CHARGING);
- }
- }
- if((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_HLC) &&
- (ShmDispenser->gun_info.isCCSWaitChangeDuty == ON) &&
- (ShmDispenser->gun_info.acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
- {
- DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
- ShmDispenser->gun_info.primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
- ShmDispenser->gun_info.mcuFlag.isSetCpPwmDuty = ON;
- ShmDispenser->gun_info.isCCSWaitChangeDuty = OFF;
- setChargerMode( SYS_MODE_COMPLETE);
- }
- break;
- case SYS_MODE_COMPLETE:
- if(isModeChange())
- {}
- if(((ShmDispenser->gun_info.chargingMode != CHARGING_MODE_SOCKETE) && (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_A)) ||
- ((ShmDispenser->gun_info.chargingMode == CHARGING_MODE_SOCKETE) && (!ShmDispenser->gun_info.primaryMcuState.socket_e.isSocketEPinOn)))
- {
- setChargerMode(SYS_MODE_IDLE);
- }
- break;
- case SYS_MODE_ALARM:
- if((ShmDispenser->gun_info.systemAlarmCode.SystemAlarmCode == 0))
- {
- if((ShmDispenser->gun_info.PreviousSystemStatus == SYS_MODE_CHARGING) ||
- (ShmDispenser->gun_info.PreviousSystemStatus == SYS_MODE_TERMINATING))
- {
- setChargerMode(ShmDispenser->gun_info.PreviousSystemStatus);
- }
- else
- {
- setChargerMode(SYS_MODE_IDLE);
- }
- }
- break;
- case SYS_MODE_FAULT:
- if(isModeChange())
- {}
- break;
- case SYS_MODE_MAINTAIN:
- if(isModeChange())
- {}
- if(ShmDispenser->gun_info.isOperactive)
- {
- DEBUG_INFO("Connector change to operactive.\n");
- setChargerMode(SYS_MODE_IDLE);
- }
- break;
- case SYS_MODE_UPDATE:
- if(isModeChange())
- {}
- break;
- case SYS_MODE_DEBUG:
- if(isModeChange())
- {}
- break;
- }
- usleep(100000);
- }
- return -1;
- }
|