main.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include <stdbool.h>
  30. #include <dirent.h>
  31. #include <signal.h>
  32. #include "../Config.h"
  33. #include "main.h"
  34. #include "../common.h"
  35. #include "../timeout.h"
  36. #include "../Log/log.h"
  37. #include "../DataBase/DataBase.h"
  38. #include "../Define/define.h"
  39. #include "../ShareMemory/shmMem.h"
  40. #include "../SelectGun/SelectGun.h"
  41. //------------------------------------------------------------------------------
  42. static struct SysInfoData *pSysInfo = NULL;
  43. static struct SysConfigData *pSysConfig = NULL;
  44. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  45. static struct AlarmCodeData *pAlarmCode = NULL;
  46. static struct FaultCodeData *pFaultCode = NULL;
  47. static struct InfoCodeData *pInfoCode = NULL;
  48. static struct PsuData *ShmPsuData = NULL;
  49. static struct CHAdeMOData *ShmCHAdeMOData = NULL;
  50. static struct GBTData *ShmGBTData = NULL;
  51. static struct CcsData *ShmCcsData = NULL;
  52. static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
  53. static struct FanModuleData *ShmFanModuleData = NULL;
  54. static struct RelayModuleData *ShmRelayModuleData = NULL;
  55. static struct LedModuleData *ShmLedModuleData = NULL;
  56. static struct OCPP16Data *ShmOCPP16Data = NULL;
  57. static DcCommonInfo *ShmDcCommonData = NULL;
  58. static struct ChargingInfoData *pDcChargingInfo = NULL;
  59. static struct ChargingInfoData *pAcChargingInfo = NULL;
  60. static struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  61. static struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  62. static SelectGunInfo *ShmSelectGunInfo = NULL; //Jerry add
  63. static EvBoardErrMsg gEvBoardErr = {0};
  64. static ChillerTempErr gChillerTempErr = {0};
  65. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  66. struct StatusCodeData *ShmStatusCodeData;
  67. // for initial index to check EV board type is correct
  68. uint8_t bd0_1_status = 0;
  69. uint8_t bd0_2_status = 0;
  70. uint8_t bd1_1_status = 0;
  71. uint8_t bd1_2_status = 0;
  72. char *fwVersion = "V2.00.00.0000.00"; // Phihong version
  73. char* DebugVersion = "v2.00.00"; // Software debug version
  74. //sqlite3 *localDb;
  75. bool isDb_ready;
  76. //------------------------------------------------------------------------------
  77. void ClearDetectPluginFlag();
  78. long long DiffTimebWithNow(struct timeb ST);
  79. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit);
  80. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value);
  81. unsigned long GetTimeoutValue(struct timeval _sour_time);
  82. void GetClockTime(struct timespec *_now_time, void *null);
  83. unsigned long GetClockTimeoutValue(struct timespec _start_time);
  84. void gpio_set_value(unsigned int gpio, unsigned int value);
  85. void InformOcppErrOccur(uint8_t codeType);
  86. void RecordAlarmCode(uint8_t gunIndex, char *code);
  87. void ReleaseAlarmCode(uint8_t gunIndex);
  88. void ResetChargerAlarmCode(uint8_t gunIndex, char *code);
  89. void AdjustChargerCurrent(void);
  90. void UpdateErrorCodeToOcpp(uint8_t index);
  91. //------------------------------------------------------------------------------
  92. //Primary.c
  93. extern void ChkPrimaryStatus(void);
  94. //RFID.c
  95. extern void CreateRfidFork(void);
  96. extern void ScannerCardProcess(void);
  97. extern bool RfidStopCharging(void);
  98. extern bool GetIsCardScan(void);
  99. extern void SetIsCardScan(bool value);
  100. //SelfTest.c
  101. extern void SelfTestRun(void);
  102. //UpgradeFW.c
  103. /*extern void CheckFwUpdateFunction(void);*/
  104. //Ethernet.c
  105. extern void InitEthernet(void);
  106. extern void GetMacAddress(void);
  107. //WatchDog.c
  108. extern void CreateWatchdog(void);
  109. extern void TryCloseWatchdog(void);
  110. extern void TryFeedWatchdog(void);
  111. //ZipFile.c
  112. extern void zipLogFiles(void);
  113. //------------------------------------------------------------------------------
  114. //--- share memory value ---
  115. //------------------------------------------------------------------------------
  116. static void changeLcmPage(uint8_t index)
  117. {
  118. pSysInfo->SystemPage = index;
  119. }
  120. static uint8_t getCurLcmPage(void)
  121. {
  122. return pSysInfo->SystemPage;
  123. }
  124. static void systemPageRestoreInit(void)
  125. {
  126. pSysInfo->SystemPage = _LCM_VIEW;
  127. }
  128. //------------------------------------------------------------------------------
  129. void destroySelGun(uint8_t curGun)
  130. {
  131. uint8_t i = 0;
  132. uint8_t totalGun = pSysConfig->TotalConnectorCount;
  133. //for status timeout
  134. if (curGun == DESTROY_ALL_SEL) {
  135. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  136. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  137. log_info("destroy all gun = %d, %d",
  138. ShmSelectGunInfo->SelGunInfo.LeftGun,
  139. ShmSelectGunInfo->SelGunInfo.RightGun);
  140. for (i = 0; i < totalGun; i++) {
  141. StopGunInfoTimeoutDet(i);
  142. memset(&ShmSelectGunInfo->PricesInfo[i], 0, sizeof(PricesInfo));
  143. }
  144. pSysInfo->CurGunSelected = 0;
  145. strcpy((char *)pSysConfig->UserId, "");
  146. //changeLcmPage(_LCM_VIEW);
  147. return;
  148. }
  149. //for charging timeout or complete
  150. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  151. if (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  152. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  153. changeLcmPage(_LCM_VIEW);
  154. }
  155. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  156. StopGunInfoTimeoutDet(LEFT_GUN_NUM);
  157. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  158. ClearAuthorizedFlag();
  159. }
  160. log_info("destroy left gun, cur page = %d", getCurLcmPage());
  161. if (getCurLcmPage() == pSysInfo->SystemPage) {
  162. log_info("clear left balance");
  163. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  164. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  165. }
  166. }
  167. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  168. if (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  169. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  170. changeLcmPage(_LCM_VIEW);
  171. }
  172. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  173. StopGunInfoTimeoutDet(RIGHT_GUN_NUM);
  174. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  175. ClearAuthorizedFlag();
  176. }
  177. log_info("destroy right gun, cur page = %d", getCurLcmPage());
  178. if (getCurLcmPage() == pSysInfo->SystemPage) {
  179. log_info("clear right balance");
  180. memset(&ShmSelectGunInfo->PricesInfo[curGun], 0, sizeof(PricesInfo));
  181. ShmSelectGunInfo->PricesInfo[curGun].Balance = FAIL_BALANCE_PRICES;
  182. }
  183. }
  184. }
  185. static int waitRightGunPlugIt(uint8_t curGun)
  186. {
  187. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  188. return PASS;
  189. }
  190. return FAIL;
  191. }
  192. static int waitLeftGunPlugIt(uint8_t curGun)
  193. {
  194. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  195. return PASS;
  196. }
  197. return FAIL;
  198. }
  199. void setSelGunWaitToAuthor(uint8_t curSel)
  200. {
  201. if (curSel == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM) {
  202. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_ATHOR;
  203. //printf("setSelGunWaitToAuthor left");
  204. StopGunInfoTimeoutDet(curSel);
  205. } else if (curSel == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM) {
  206. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_ATHOR;
  207. //printf("setSelGunWaitToAuthor right");
  208. StopGunInfoTimeoutDet(curSel);
  209. }
  210. }
  211. int getConfirmSelectedGun(uint8_t curSel)
  212. {
  213. if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  214. ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  215. return PASS;
  216. }
  217. return FAIL;
  218. }
  219. void confirmSelGun(uint8_t selGun)
  220. {
  221. if (selGun == LEFT_GUN_NUM) {
  222. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  223. //printf("confirmSelGun left");
  224. } else if (selGun == RIGHT_GUN_NUM) {
  225. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  226. //printf("confirmSelGun right");
  227. }
  228. changeLcmPage(_LCM_START_SCAN);
  229. //StartGunInfoTimeoutDet(selGun, Timeout_SelectGun);
  230. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  231. }
  232. static void GetFirmwareVersion(void)
  233. {
  234. // Get CSU root file system version
  235. sprintf((char *)pSysInfo->CsuRootFsFwRev, fwVersion);
  236. uint8_t count = 0, chademo = 0, ccs = 0, gb = 0;
  237. for (uint8_t idx = 0; idx < 3; idx++) {
  238. if (pSysConfig->ModelName[7 + idx] == 'J') {
  239. chademo++;
  240. count++;
  241. } else if (pSysConfig->ModelName[7 + idx] == 'G') {
  242. gb++;
  243. count++;
  244. } else if (pSysConfig->ModelName[7 + idx] == 'U' ||
  245. pSysConfig->ModelName[7 + idx] == 'V' ||
  246. pSysConfig->ModelName[7 + idx] == 'E') {
  247. ccs++;
  248. count++;
  249. }
  250. }
  251. if (count == 1) {
  252. if (chademo > 0) {
  253. pSysInfo->CsuRootFsFwRev[7] = '1';
  254. } else if (ccs > 0) {
  255. pSysInfo->CsuRootFsFwRev[7] = '2';
  256. } else if (gb > 0) {
  257. pSysInfo->CsuRootFsFwRev[7] = '3';
  258. }
  259. } else {
  260. if (chademo > 0 && ccs > 0) {
  261. pSysInfo->CsuRootFsFwRev[7] = '4';
  262. } else if (chademo > 0 && gb > 0) {
  263. pSysInfo->CsuRootFsFwRev[7] = '5';
  264. } else if (ccs > 0 && gb > 0) {
  265. pSysInfo->CsuRootFsFwRev[7] = '6';
  266. }
  267. }
  268. // Get network option from model name
  269. switch (pSysConfig->ModelName[10]) {
  270. case 'B':
  271. case 'U':
  272. //Blue tooth
  273. pSysInfo->CsuRootFsFwRev[9] = '3';
  274. break;
  275. case 'W':
  276. // WIFI
  277. pSysInfo->CsuRootFsFwRev[9] = '1';
  278. break;
  279. case 'T':
  280. // 3G/4G
  281. pSysInfo->CsuRootFsFwRev[9] = '2';
  282. break;
  283. case 'D': //DS60-120 add
  284. pSysInfo->CsuRootFsFwRev[9] = '5';
  285. break;
  286. default:
  287. // LAN
  288. pSysInfo->CsuRootFsFwRev[9] = '0';
  289. break;
  290. }
  291. // Get rating power from model name
  292. memcpy(&pSysInfo->CsuRootFsFwRev[10], &pSysConfig->ModelName[4], 0x03);
  293. // Get IEC or UL
  294. char _buf[3] = {0};
  295. memcpy(_buf, &pSysConfig->ModelName[2], 2);
  296. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL) {
  297. pSysInfo->ChargerType = _CHARGER_TYPE_IEC;
  298. log_info("IEC model");
  299. } else if (strcmp(_buf, "WU") == EQUAL) {
  300. pSysInfo->ChargerType = _CHARGER_TYPE_UL;
  301. log_info("UL model");
  302. }
  303. }
  304. static void checkGunOTPState(uint8_t gunIndex)
  305. {
  306. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  307. //水冷機溫度檢測
  308. if (((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x80) >> 7) == YES) {
  309. if (pDcChargingInfo->ChillerTemp != UNDEFINED_TEMP) {
  310. if (pDcChargingInfo->ChillerTemp >= GUN_OTP_VALUE) {
  311. if (((gunIndex == 0) &&
  312. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  313. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  314. ||
  315. ((gunIndex == 1) &&
  316. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  317. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  318. ) {
  319. RecordAlarmCode(gunIndex, "012323");
  320. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = YES;
  321. }
  322. else
  323. {
  324. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  325. }
  326. } else if (pDcChargingInfo->ChillerTemp != 0 &&
  327. pDcChargingInfo->ChillerTemp < GUN_OTP_RECOVERY) {
  328. //ResetChargerAlarmCode(gunIndex, "012323");
  329. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  330. }
  331. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  332. } else {
  333. // 沒接上 Sensor or 異常
  334. //RecordAlarmCode(gunIndex, "011038");
  335. //ResetChargerAlarmCode(gunIndex, "012323");
  336. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  337. if (((gunIndex == 0) &&
  338. ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  339. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)))
  340. ||
  341. ((gunIndex == 1) &&
  342. ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  343. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)))
  344. ) {
  345. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = YES;
  346. }
  347. else
  348. {
  349. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  350. }
  351. }
  352. if(pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault == YES)
  353. {
  354. RecordAlarmCode(gunIndex, "011037");
  355. }
  356. }
  357. switch (pDcChargingInfo->Type) {
  358. case _Type_Chademo:
  359. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  360. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  361. RecordAlarmCode(gunIndex, "012229");
  362. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = YES;
  363. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  364. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  365. //ResetChargerAlarmCode(gunIndex, "012229");
  366. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  367. }
  368. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  369. } else {
  370. // 沒接上 Sensor or 異常
  371. //RecordAlarmCode(gunIndex, "011018");
  372. //ResetChargerAlarmCode(gunIndex, "012229");
  373. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  374. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = YES;
  375. }
  376. break;
  377. case _Type_CCS_2:
  378. // CCS 不管甚麼輸出都會有槍溫偵測!!~
  379. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  380. //ResetChargerAlarmCode(gunIndex, "011019");
  381. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  382. RecordAlarmCode(gunIndex, "012230");
  383. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = YES;
  384. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  385. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  386. //ResetChargerAlarmCode(gunIndex, "012230");
  387. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  388. }
  389. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  390. //ResetChargerAlarmCode(gunIndex, "011019");
  391. } else {
  392. // 沒接上 Sensor or 異常
  393. //RecordAlarmCode(gunIndex, "011019");
  394. //ResetChargerAlarmCode(gunIndex, "012230");
  395. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  396. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = YES;
  397. }
  398. break;
  399. case _Type_GB:
  400. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  401. if (pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) {
  402. RecordAlarmCode(gunIndex, "012231");
  403. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = YES;
  404. } else if (pDcChargingInfo->ConnectorTemp != 0 &&
  405. pDcChargingInfo->ConnectorTemp < GUN_OTP_RECOVERY) {
  406. //ResetChargerAlarmCode(gunIndex, "012231");
  407. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  408. }
  409. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  410. } else {
  411. // 沒接上 Sensor or 異常
  412. //RecordAlarmCode(gunIndex, "011020");
  413. //ResetChargerAlarmCode(gunIndex, "012231");
  414. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  415. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = YES;
  416. }
  417. break;
  418. }
  419. }
  420. static void collectError(uint8_t gunIndex)
  421. {
  422. gEvBoardErr.GunErrMessage |= ShmDcCommonData->ConnectErrList[gunIndex].GunErrMessage;
  423. gChillerTempErr.TempErrMsg |= ShmDcCommonData->ChillerTempErr[gunIndex].TempErrMsg;
  424. }
  425. static void checkGBTAlarmState(uint8_t gunType)
  426. {
  427. // GFD Trip
  428. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  429. pAlarmCode->AlarmEvents.bits.GbGfdTrip = YES;
  430. } else {
  431. pAlarmCode->AlarmEvents.bits.GbGfdTrip = NO;
  432. }
  433. // UVP
  434. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  435. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = YES;
  436. } else {
  437. pAlarmCode->AlarmEvents.bits.GbtOutputUVPFail = NO;
  438. }
  439. // OTP
  440. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  441. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = YES;
  442. } else {
  443. pAlarmCode->AlarmEvents.bits.GbConnectorOTP = NO;
  444. }
  445. // OVP
  446. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  447. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = YES;
  448. } else {
  449. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  450. }
  451. // GFD Warning
  452. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  453. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = YES;
  454. } else {
  455. pAlarmCode->AlarmEvents.bits.GbGroundfaultWarning = NO;
  456. }
  457. // Relay Welding
  458. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  459. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = YES;
  460. } else {
  461. pFaultCode->FaultEvents.bits.GbOutputRelayWelding = NO;
  462. }
  463. // Relay Driving
  464. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  465. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  466. } else {
  467. pFaultCode->FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  468. }
  469. // Connect temp Sensor broken
  470. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  471. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = YES;
  472. } else {
  473. pFaultCode->FaultEvents.bits.GbConnectorTempSensorBroken = NO;
  474. }
  475. }
  476. static void checkCCSAlarmState(uint8_t gunType)
  477. {
  478. // GFD Trip
  479. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  480. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = YES;
  481. } else {
  482. pAlarmCode->AlarmEvents.bits.CcsGfdTrip = NO;
  483. }
  484. // UVP
  485. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  486. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = YES;
  487. } else {
  488. pAlarmCode->AlarmEvents.bits.CcsOutputUVPFail = NO;
  489. }
  490. // OTP
  491. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  492. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = YES;
  493. } else {
  494. pAlarmCode->AlarmEvents.bits.CcsConnectorOTP = NO;
  495. }
  496. // OVP
  497. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  498. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = YES;
  499. } else {
  500. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  501. }
  502. // GFD Warning
  503. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  504. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = YES;
  505. } else {
  506. pAlarmCode->AlarmEvents.bits.CcsGroundfaultWarning = NO;
  507. }
  508. // Relay Welding
  509. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  510. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = YES;
  511. } else {
  512. pFaultCode->FaultEvents.bits.CcsOutputRelayWelding = NO;
  513. }
  514. // Relay Driving
  515. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  516. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  517. } else {
  518. pFaultCode->FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  519. }
  520. // Connect temp Sensor broken
  521. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  522. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = YES;
  523. } else {
  524. pFaultCode->FaultEvents.bits.CcsConnectorTempSensorBroken = NO;
  525. }
  526. }
  527. static void checkChaDeMoAlarmState(uint8_t gunType)
  528. {
  529. // GFD Trip
  530. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 0)) {
  531. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = YES;
  532. } else {
  533. pAlarmCode->AlarmEvents.bits.ChademoGfdTrip = NO;
  534. }
  535. // UVP
  536. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 1)) {
  537. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = YES;
  538. } else {
  539. pAlarmCode->AlarmEvents.bits.ChademoOutputUVPFail = NO;
  540. }
  541. // OTP
  542. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 2)) {
  543. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = YES;
  544. } else {
  545. pAlarmCode->AlarmEvents.bits.ChademoConnectorOTP = NO;
  546. }
  547. // OVP
  548. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 3)) {
  549. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = YES;
  550. } else {
  551. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  552. }
  553. // GFD Warning
  554. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 4)) {
  555. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = YES;
  556. } else {
  557. pAlarmCode->AlarmEvents.bits.ChademoGroundWarning = NO;
  558. }
  559. // Relay Welding
  560. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 5)) {
  561. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = YES;
  562. } else {
  563. pFaultCode->FaultEvents.bits.ChademoOutputRelayWelding = NO;
  564. }
  565. // Relay Driving
  566. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 6)) {
  567. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  568. } else {
  569. pFaultCode->FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  570. }
  571. // Connect temp Sensor broken
  572. if (DetectBitValue(gEvBoardErr.GunErrMessage >> (8 * gunType), 7)) {
  573. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = YES;
  574. } else {
  575. pFaultCode->FaultEvents.bits.ChademoConnectorTempSensorBroken = NO;
  576. }
  577. }
  578. static void checkChillerAlarmState(void)
  579. {
  580. if (DetectBitValue(gChillerTempErr.TempErrMsg, 0)) {
  581. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = YES;
  582. } else {
  583. pAlarmCode->AlarmEvents.bits.SystemChillerOTP = NO;
  584. }
  585. if (DetectBitValue(gChillerTempErr.TempErrMsg, 1)) {
  586. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = YES;
  587. } else {
  588. pFaultCode->FaultEvents.bits.ChillerTempSensorBroken = NO;
  589. }
  590. }
  591. static void checkEvBoardAlarmState(uint8_t gunType)
  592. {
  593. switch (gunType) {
  594. case _Type_Chademo:
  595. checkChaDeMoAlarmState(gunType);
  596. break;
  597. case _Type_CCS_2:
  598. checkCCSAlarmState(gunType);
  599. break;
  600. case _Type_GB:
  601. checkGBTAlarmState(gunType);
  602. break;
  603. }
  604. }
  605. unsigned long GetTimeoutValue(struct timeval _sour_time)
  606. {
  607. struct timeval _end_time;
  608. gettimeofday(&_end_time, NULL);
  609. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  610. }
  611. void GetClockTime(struct timespec *_now_time, void *null)
  612. {
  613. clock_gettime(CLOCK_MONOTONIC, _now_time);
  614. }
  615. // return value unit: 1us
  616. unsigned long GetClockTimeoutValue(struct timespec _start_time)
  617. {
  618. struct timespec ts_end;
  619. unsigned long ret = 0;
  620. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  621. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec/ 1000)));
  622. return ret;
  623. }
  624. int DiffTimeb(struct timeb ST, struct timeb ET)
  625. {
  626. //return milli-second
  627. unsigned int StartTime, StopTime;
  628. StartTime = (unsigned int)ST.time;
  629. StopTime = (unsigned int)ET.time;
  630. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  631. return (StopTime - StartTime);
  632. }
  633. bool CheckTimeOut(struct timeb ST)
  634. {
  635. struct timeb ET;
  636. unsigned int StartTime, StopTime;
  637. ftime(&ET);
  638. StartTime = (unsigned int) ST.time;
  639. StopTime = (unsigned int) ET.time;
  640. return (StopTime > StartTime) ? YES : NO;
  641. }
  642. void setChargerMode(uint8_t gunIndex, uint8_t mode)
  643. {
  644. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  645. pDcChargingInfo->SystemStatus = mode;
  646. }
  647. long long DiffTimebWithNow(struct timeb ST)
  648. {
  649. //return milli-second
  650. struct timeb ET;
  651. long long StartTime, StopTime;
  652. ftime(&ET);
  653. StartTime = (long long)ST.time;
  654. StopTime = (long long)ET.time;
  655. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  656. }
  657. //==========================================
  658. // Log
  659. //==========================================
  660. void CheckFwSlotStatusLog(void)
  661. {
  662. if (bd0_1_status == 0 && bd0_2_status == 1) {
  663. log_info("Connector 1 : Chademo");
  664. } else if (bd0_1_status == 1 && bd0_2_status == 0) {
  665. log_info("Connector 1 : CCS");
  666. } else if (bd0_1_status == 1 && bd0_2_status == 1) {
  667. log_info("Connector 1 : GB");
  668. }
  669. if (bd1_1_status == 0 && bd1_2_status == 1) {
  670. log_info("Connector 2 : Chademo");
  671. } else if (bd1_1_status == 1 && bd1_2_status == 0) {
  672. log_info("Connector 2 : CCS");
  673. } else if (bd1_1_status == 1 && bd1_2_status == 1) {
  674. log_info("Connector 2 : GB");
  675. }
  676. }
  677. void CheckHwSlotStatusLog(uint8_t index)
  678. {
  679. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  680. if (pDcChargingInfo->Type == _Type_Chademo) {
  681. log_info("Hw check : Connector %d, Type : Chademo, Evboard_id = %d ",
  682. index, pDcChargingInfo->Evboard_id);
  683. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  684. log_info("Hw check : Connector %d, Type : CCS, Evboard_id = %d ",
  685. index, pDcChargingInfo->Evboard_id);
  686. } else if (pDcChargingInfo->Type == _Type_GB) {
  687. log_info("Hw check : Connector %d, Type : GB, Evboard_id = %d ",
  688. index, pDcChargingInfo->Evboard_id);
  689. }
  690. }
  691. //=================================
  692. // LCM Page
  693. //=================================
  694. void ChangeLcmByIndex(uint8_t page_index)
  695. {
  696. if (pSysWarning->Level != WARN_LV_ER ||
  697. page_index == _LCM_SUMMARY ||
  698. page_index == _LCM_ERROR
  699. ) {
  700. pSysInfo->PageIndex = page_index;
  701. //log_info("LCM index:%d",pSysInfo->PageIndex);
  702. }
  703. }
  704. //======================================================
  705. // Peripheral initial
  706. //======================================================
  707. void InitGPIO()
  708. {
  709. /*****************0~3, 4 bank, bank x 32+ num*********************/
  710. /***************************************************************/
  711. /*************** GPIO 0 ***************************************/
  712. /***************************************************************/
  713. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  714. system("echo 22 > /sys/class/gpio/export");
  715. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  716. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  717. system("echo 23 > /sys/class/gpio/export");
  718. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  719. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  720. system("echo 26 > /sys/class/gpio/export");
  721. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  722. system("echo 1 > /sys/class/gpio/gpio26/value");
  723. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  724. system("echo 27 > /sys/class/gpio/export");
  725. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  726. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  727. system("echo 29 > /sys/class/gpio/export");
  728. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  729. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  730. system("echo 19 > /sys/class/gpio/export");
  731. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  732. system("echo 1 > /sys/class/gpio/gpio19/value");
  733. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  734. system("echo 20 > /sys/class/gpio/export");
  735. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  736. /***************************************************************/
  737. /*************** GPIO 1 ***************************************/
  738. /***************************************************************/
  739. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  740. system("echo 44 > /sys/class/gpio/export");
  741. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  742. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  743. system("echo 45 > /sys/class/gpio/export");
  744. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  745. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  746. system("echo 46 > /sys/class/gpio/export");
  747. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  748. system("echo 0 > /sys/class/gpio/gpio46/value");
  749. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  750. system("echo 47 > /sys/class/gpio/export");
  751. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  752. /***************************************************************/
  753. /*************** GPIO 2 ***************************************/
  754. /***************************************************************/
  755. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  756. system("echo 89 > /sys/class/gpio/export");
  757. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  758. system("echo 1 > /sys/class/gpio/gpio89/value");
  759. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  760. system("echo 87 > /sys/class/gpio/export");
  761. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  762. system("echo 0 > /sys/class/gpio/gpio87/value");
  763. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  764. system("echo 88 > /sys/class/gpio/export");
  765. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  766. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  767. system("echo 86 > /sys/class/gpio/export");
  768. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  769. /***************************************************************/
  770. /*************** GPIO 3 ***************************************/
  771. /***************************************************************/
  772. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  773. system("echo 111 > /sys/class/gpio/export");
  774. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  775. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  776. system("echo 114 > /sys/class/gpio/export");
  777. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  778. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  779. system("echo 113 > /sys/class/gpio/export");
  780. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  781. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  782. system("echo 110 > /sys/class/gpio/export");
  783. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  784. system("echo 0 > /sys/class/gpio/gpio110/value");
  785. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  786. system("echo 115 > /sys/class/gpio/export");
  787. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  788. system("echo 0 > /sys/class/gpio/gpio115/value");
  789. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  790. system("echo 112 > /sys/class/gpio/export");
  791. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  792. system("echo 1 > /sys/class/gpio/gpio112/value");
  793. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  794. system("echo 116 > /sys/class/gpio/export");
  795. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  796. system("echo 0 > /sys/class/gpio/gpio116/value");
  797. log_info("Initial GPIO OK");
  798. return ;
  799. }
  800. int LoadSysConfigAndInfo()
  801. {
  802. int fd, wrd;
  803. unsigned char *buf;
  804. unsigned int ChkSum, ChkSumOrg;
  805. if ((buf = malloc(MtdBlockSize)) == NULL) {
  806. log_error("malloc buffer NG,rebooting..");
  807. if (pAlarmCode != NULL) {
  808. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  809. }
  810. sleep(5);
  811. system("reboot -f");
  812. sleep(5);
  813. system("reboot -f");
  814. }
  815. memset(buf, 0, MtdBlockSize);
  816. //================================================
  817. // Load configuration from mtdblock10
  818. //================================================
  819. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  820. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  821. if (fd < 0) {
  822. free(buf);
  823. log_error("open mtdblock10 NG,rebooting..");
  824. if (pAlarmCode != NULL) {
  825. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  826. }
  827. sleep(5);
  828. system("reboot -f");
  829. sleep(5);
  830. system("reboot -f");
  831. }
  832. wrd = read(fd, buf, MtdBlockSize);
  833. close(fd);
  834. if (wrd < MtdBlockSize) {
  835. free(buf);
  836. log_error("read SysConfigData data NG,rebooting..");
  837. if (pAlarmCode != NULL) {
  838. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  839. }
  840. sleep(5);
  841. system("reboot -f");
  842. sleep(5);
  843. system("reboot -f");
  844. }
  845. ChkSum = 0;
  846. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  847. ChkSum += buf[wrd];
  848. }
  849. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  850. memcpy(&pSysConfig->ModelName, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), ARRAY_SIZE(pSysConfig->ModelName));
  851. memcpy(&pSysConfig->SerialNumber, buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), ARRAY_SIZE(pSysConfig->SerialNumber));
  852. //================================================
  853. // Load configuration from mtdblock11
  854. //================================================
  855. if (ChkSum != ChkSumOrg) {
  856. log_error("Primary SysConfigData checksum NG, read backup");
  857. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  858. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  859. if (fd < 0) {
  860. free(buf);
  861. log_error("open mtdblock11 (backup) NG,rebooting..");
  862. if (pAlarmCode != NULL) {
  863. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  864. }
  865. sleep(5);
  866. system("reboot -f");
  867. sleep(5);
  868. system("reboot -f");
  869. }
  870. memset(buf, 0, MtdBlockSize);
  871. wrd = read(fd, buf, MtdBlockSize);
  872. close(fd);
  873. if (wrd < MtdBlockSize) {
  874. free(buf);
  875. log_error("read backup SysConfigData data NG,rebooting..");
  876. if (pAlarmCode != NULL) {
  877. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  878. }
  879. sleep(5);
  880. system("reboot -f");
  881. sleep(5);
  882. system("reboot -f");
  883. }
  884. ChkSum = 0;
  885. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  886. ChkSum += buf[wrd];
  887. }
  888. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  889. //================================================
  890. // Load configuration from mtdblock12 (Factory default)
  891. //================================================
  892. if (ChkSum != ChkSumOrg) {
  893. log_warn("backup SysConfigData checksum NG, read Factory default");
  894. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  895. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  896. if (fd < 0) {
  897. log_error("open mtdblock12 (Factory default) NG,rebooting..");
  898. free(buf);
  899. if (pAlarmCode != NULL) {
  900. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  901. }
  902. sleep(5);
  903. system("reboot -f");
  904. sleep(5);
  905. system("reboot -f");
  906. }
  907. memset(buf, 0, MtdBlockSize);
  908. wrd = read(fd, buf, MtdBlockSize);
  909. close(fd);
  910. if (wrd < MtdBlockSize) {
  911. log_error("read factory default SysConfigData data NG,rebooting..");
  912. free(buf);
  913. if (pAlarmCode != NULL) {
  914. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  915. }
  916. sleep(5);
  917. system("reboot -f");
  918. sleep(5);
  919. system("reboot -f");
  920. }
  921. ChkSum = 0;
  922. for (wrd = ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++) {
  923. ChkSum += buf[wrd];
  924. }
  925. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  926. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev)), &pSysConfig->ModelName, ARRAY_SIZE(pSysConfig->ModelName));
  927. memcpy(buf + (ARRAY_SIZE(pSysConfig->CsuBootLoadFwRev) + ARRAY_SIZE(pSysConfig->ModelName) + ARRAY_SIZE(pSysConfig->AcModelName)), &pSysConfig->SerialNumber, ARRAY_SIZE(pSysConfig->SerialNumber));
  928. if (ChkSum != ChkSumOrg) {
  929. log_warn("factory default SysConfigData checksum NG, restore factory default");
  930. free(buf);
  931. system("cd /root;./FactoryConfig -m");
  932. system("rm -f /Storage/OCPP/OCPPConfiguration");
  933. system("sync");
  934. sleep(5);
  935. system("reboot -f");
  936. sleep(5);
  937. system("reboot -f");
  938. return FAIL;
  939. }
  940. }
  941. }
  942. //load OK
  943. memcpy(pSysConfig, buf, sizeof(struct SysConfigData));
  944. free(buf);
  945. system("rm -f /mnt/EvseConfig.bin");
  946. // SysConfig in flash is empty (0xffffffff)
  947. if ((strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) ||
  948. (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) ||
  949. (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) ||
  950. (pSysConfig->Eth0Interface.EthDhcpClient == 0xff)) {
  951. if (strlen((char *)pSysConfig->ModelName) > ARRAY_SIZE(pSysConfig->ModelName)) {
  952. memset(pSysConfig->ModelName, 0x00, ARRAY_SIZE(pSysConfig->ModelName));
  953. }
  954. if (strlen((char *)pSysConfig->SerialNumber) > ARRAY_SIZE(pSysConfig->SerialNumber)) {
  955. memset(pSysConfig->SerialNumber, 0x00, ARRAY_SIZE(pSysConfig->SerialNumber));
  956. }
  957. if (strlen((char *)pSysConfig->SystemId) > ARRAY_SIZE(pSysConfig->SystemId)) {
  958. memset(pSysConfig->SystemId, 0x00, ARRAY_SIZE(pSysConfig->SystemId));
  959. }
  960. if (pSysConfig->Eth0Interface.EthDhcpClient == 0xff) {
  961. log_info("Ethernet dhcp config is null.");
  962. }
  963. if (strlen((char *)pSysConfig->ModelName) == 0x00) {
  964. log_info("Model name over length.");
  965. }
  966. if (strlen((char *)pSysConfig->SerialNumber) == 0x00) {
  967. log_info("Model serial number over length.");
  968. }
  969. if (strlen((char *)pSysConfig->SystemId) == 0x00) {
  970. log_info("SystemId over length.");
  971. }
  972. system("cd /root;./FactoryConfig -m");
  973. sleep(3);
  974. system("/usr/bin/run_evse_restart.sh");
  975. }
  976. log_info("Load SysConfigData OK");
  977. return PASS;
  978. }
  979. int Initialization(void)
  980. {
  981. uint8_t count = 0;
  982. uint8_t pinOut[2] = {116, 115};
  983. // 初始化卡號驗證的 Flag
  984. ClearAuthorizedFlag();
  985. // 初始化插槍驗證的 Flag
  986. ClearDetectPluginFlag();
  987. for (count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  988. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  989. pDcChargingInfo->RemoteStartFlag = NO;
  990. if (pDcChargingInfo->Type == _Type_Chademo) {
  991. gpio_set_value(pinOut[count], 0x00);
  992. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  993. } else if (pDcChargingInfo->Type == _Type_GB) {
  994. gpio_set_value(pinOut[count], 0x00);
  995. ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  996. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  997. //if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) { //DS60-120 remove
  998. if (pSysConfig->TotalConnectorCount == 1) {
  999. gpio_set_value(pinOut[1], 0x01);
  1000. } else {
  1001. gpio_set_value(pinOut[count], 0x01);
  1002. }
  1003. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp = NO;
  1004. //}
  1005. }
  1006. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1007. }
  1008. for (count = 0; count < pSysConfig->AcConnectorCount; count++) {
  1009. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  1010. pAcChargingInfo->RemoteStartFlag = NO;
  1011. if (pAcChargingInfo->Type == _Type_AC) {
  1012. pAcChargingInfo->SelfTest_Comp = NO;
  1013. strcpy((char *)ShmOCPP16Data->StatusNotification[count + pSysConfig->TotalConnectorCount].ErrorCode, "NoError");
  1014. }
  1015. }
  1016. return PASS;
  1017. }
  1018. bool InitialSystemDefaultConfig()
  1019. {
  1020. bool result = true;
  1021. LoadSysConfigAndInfo();
  1022. InitGPIO();
  1023. InitEthernet();
  1024. GetMacAddress();
  1025. return result;
  1026. }
  1027. //顯示自檢錯誤原因,觸發對應的flag
  1028. bool DisplaySelfTestFailReason()
  1029. {
  1030. bool result = false;
  1031. uint8_t index = 0;
  1032. // RB、FB、407、EV 小板中有些板子無回應
  1033. if (ShmRelayModuleData->SelfTest_Comp == NO) {
  1034. pAlarmCode->AlarmEvents.bits.RelayboardStestFail = true;
  1035. }
  1036. if (ShmFanModuleData->SelfTest_Comp == NO) {
  1037. pAlarmCode->AlarmEvents.bits.FanboardStestFail = true;
  1038. }
  1039. if (ShmPrimaryMcuData->SelfTest_Comp == NO) {
  1040. pAlarmCode->AlarmEvents.bits.PrimaryStestFail = true;
  1041. }
  1042. if (ShmLedModuleData->SelfTest_Comp == NO) {
  1043. pAlarmCode->AlarmEvents.bits.LedboardStestFail = true;
  1044. }
  1045. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  1046. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1047. if (pDcChargingInfo->Type == _Type_Chademo) {
  1048. if (ShmCHAdeMOData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1049. pAlarmCode->AlarmEvents.bits.ChademoboardStestFail = true;
  1050. }
  1051. } else if (pDcChargingInfo->Type == _Type_GB) {
  1052. if (ShmGBTData->evse[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1053. pAlarmCode->AlarmEvents.bits.GbtboardStestFail = true;
  1054. }
  1055. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1056. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1057. if (ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].SelfTest_Comp == NO) {
  1058. pAlarmCode->AlarmEvents.bits.CCSboardStestFail = true;
  1059. }
  1060. }
  1061. }
  1062. }
  1063. for (index = 0; index < pSysConfig->AcConnectorCount; index++) {
  1064. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(index);
  1065. // 先借 GBT 顯示
  1066. if (pAcChargingInfo->SelfTest_Comp == NO) {
  1067. pAlarmCode->AlarmEvents.bits.AcConnectorStestFail = true;
  1068. }
  1069. }
  1070. if (pSysInfo->AcContactorStatus == NO) {
  1071. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox &&!defined DD360UCar
  1072. // AC Contact 未搭上
  1073. pAlarmCode->AlarmEvents.bits.AcContactStestFail = true;
  1074. result = true;
  1075. #endif // !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  1076. }
  1077. //else if (pAlarmCode->AlarmEvents.bits.PsuDipSwitchStestFail == YES) { //DS60-120 add
  1078. // result = true;
  1079. //}
  1080. else if (ShmPsuData->SystemAvailablePower <= 0 &&
  1081. ShmPsuData->SystemAvailableCurrent <= 0) {
  1082. // PSU 通訊問題
  1083. pAlarmCode->AlarmEvents.bits.PsuModuleStestFail = true;
  1084. result = true;
  1085. }
  1086. return result;
  1087. }
  1088. //===============================================
  1089. // Common Detect Chk - Stop Charging ?
  1090. //===============================================
  1091. int isEvBoardStopChargeFlag(uint8_t gunIndex)
  1092. {
  1093. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1094. //printf("StopChargeFlag = %d", chargingInfo[gunIndex]->StopChargeFlag);
  1095. return pDcChargingInfo->StopChargeFlag;
  1096. }
  1097. bool isEvBoardNormalStopChargeFlag(uint8_t gunIndex)
  1098. {
  1099. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1100. return pDcChargingInfo->NormalStopChargeFlag;
  1101. }
  1102. //===============================================
  1103. // 掃描插槍狀況
  1104. //===============================================
  1105. void ClearDetectPluginFlag(void)
  1106. {
  1107. uint8_t gunIndex = 0;
  1108. pSysInfo->WaitForPlugit = NO;
  1109. //DS60-120 add
  1110. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1111. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1112. if (pDcChargingInfo->RemoteStartFlag == YES) {
  1113. pDcChargingInfo->RemoteStartFlag = NO;
  1114. }
  1115. }
  1116. if (pSysInfo->OrderCharging != NO_DEFINE) {
  1117. pSysInfo->OrderCharging = NO_DEFINE;
  1118. }
  1119. }
  1120. void DetectPluginStart()
  1121. {
  1122. pSysInfo->WaitForPlugit = YES;
  1123. }
  1124. bool isDetectPlugin()
  1125. {
  1126. if (pSysInfo->WaitForPlugit == YES) {
  1127. return true;
  1128. }
  1129. return false;
  1130. }
  1131. //===============================================
  1132. // Common Detect Chk - Chademo
  1133. //===============================================
  1134. bool isEvGunLocked_chademo(uint8_t gunIndex)
  1135. {
  1136. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1137. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1138. }
  1139. bool isEvContactorWelding_chademo(uint8_t gunIndex)
  1140. {
  1141. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1142. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 3);
  1143. }
  1144. bool isEvStopReq_chademo(uint8_t gunIndex)
  1145. {
  1146. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1147. return DetectBitValue(ShmCHAdeMOData->ev[pDcChargingInfo->type_index].EvDetection, 4);
  1148. }
  1149. bool isEvStopCharging_chademo(uint8_t gunIndex)
  1150. {
  1151. if (isEvGunLocked_chademo(gunIndex) == NO) {
  1152. // 無鎖槍 = 停止
  1153. log_info("gun locked none (%d) ", gunIndex);
  1154. return YES;
  1155. }
  1156. return NO;
  1157. }
  1158. uint8_t isPrechargeStatus_chademo(uint8_t gunIndex)
  1159. {
  1160. uint8_t result = 0x00;
  1161. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1162. result = ShmCHAdeMOData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1163. return result;
  1164. }
  1165. //===============================================
  1166. // Common Detect Chk - GB
  1167. //===============================================
  1168. bool isEvGunLocked_gb(uint8_t gunIndex)
  1169. {
  1170. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1171. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1172. }
  1173. bool isEvStopCharging_gb(uint8_t gunIndex)
  1174. {
  1175. if (isEvGunLocked_gb(gunIndex) == NO) {
  1176. // 無鎖槍 = 停止
  1177. log_info("gun locked none. ");
  1178. return YES;
  1179. }
  1180. return NO;
  1181. }
  1182. uint8_t isPrechargeStatus_gb(uint8_t gunIndex)
  1183. {
  1184. uint8_t result = 0x00;
  1185. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1186. result = ShmGBTData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1187. return result;
  1188. }
  1189. //===============================================
  1190. // Common Detect Chk - CCS
  1191. //===============================================
  1192. bool isEvGunLocked_ccs(uint8_t gunIndex)
  1193. {
  1194. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1195. return (DetectBitValue(pDcChargingInfo->GunLocked , 0) == 0) ? NO : YES;
  1196. }
  1197. uint8_t isPrechargeStatus_ccs(uint8_t gunIndex)
  1198. {
  1199. uint8_t result = 0x00;
  1200. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1201. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1202. result = ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].PresentMsgFlowStatus;
  1203. }
  1204. return result;
  1205. }
  1206. bool isEvStopCharging_ccs(uint8_t gunIndex)
  1207. {
  1208. if (isEvGunLocked_ccs(gunIndex) == NO) {
  1209. // 無鎖槍 = 停止
  1210. log_info("gun locked none. ");
  1211. return YES;
  1212. }
  1213. return NO;
  1214. }
  1215. //===============================================
  1216. // Callback
  1217. //===============================================
  1218. void DisplayChargingInfo()
  1219. {
  1220. uint8_t i = 0;
  1221. log_info("*********** DisplayChargingInfo *********** ");
  1222. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1223. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1224. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1225. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  1226. ChangeGunSelectByIndex(i);
  1227. return;
  1228. }
  1229. }
  1230. if (pSysConfig->AcConnectorCount > 0 &&
  1231. pSysInfo->CurGunSelectedByAc == NO_DEFINE)
  1232. {
  1233. pAcChargingInfo = (struct ChargingInfoData*)GetAcChargingInfoData(0);
  1234. if (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  1235. pAcChargingInfo->SystemStatus <= S_COMPLETE)
  1236. {
  1237. pSysInfo->CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1238. }
  1239. }
  1240. usleep(50000);
  1241. systemPageRestoreInit();
  1242. }
  1243. void _AutoReturnTimeout(void)
  1244. {
  1245. log_info("*********** _AutoReturnTimeout(%d) *********** ", pSysInfo->PageIndex);
  1246. if (pSysInfo->PageIndex == _LCM_PRECHARGE) {
  1247. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  1248. pDcChargingInfo->SystemStatus = S_ALARM;
  1249. //pSysInfo->SystemPage = _LCM_ERROR;
  1250. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1251. ClearDetectPluginFlag();
  1252. } else if (pSysInfo->SystemPage ==_LCM_START_AUTHORIZE_FAIL) {
  1253. pSysInfo->SystemPage = _LCM_START_SCAN;
  1254. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1255. strcpy((char *)pSysConfig->UserId, "");
  1256. setChargerMode(pSysInfo->CurGunSelected,MODE_AUTHORIZING);
  1257. }
  1258. }
  1259. void _SelfTestTimeout(void)
  1260. {
  1261. if (pSysInfo->BootingStatus != BOOT_COMPLETE) {
  1262. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1263. setChargerMode(gunIndex, MODE_ALARM);
  1264. }
  1265. }
  1266. ShmPsuData->Work_Step = _NO_WORKING;
  1267. pSysInfo->SelfTestSeq = _STEST_FAIL;
  1268. log_info("Self test timeout. ");
  1269. }
  1270. void _AuthorizedTimeout(void)
  1271. {
  1272. //if (IsAuthorizingMode()) {
  1273. log_info("*********** _AuthorizedTimeout *********** ");
  1274. StopSystemTimeoutDet();
  1275. //isCardScan = false;
  1276. SetIsCardScan(false);
  1277. pSysInfo->SystemPage = _LCM_START_AUTHORIZE_FAIL;
  1278. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1279. strcpy((char *)pSysConfig->UserId, "");
  1280. ClearAuthorizedFlag();
  1281. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1282. //}
  1283. }
  1284. void _DetectPlugInTimeout(void)
  1285. {
  1286. log_info("*********** _DetectPlugInTimeout *********** ");
  1287. strcpy((char *)pSysConfig->UserId, "");
  1288. StopSystemTimeoutDet();
  1289. pSysInfo->SystemPage = _LCM_LINK_ERROR;
  1290. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  1291. ClearDetectPluginFlag();
  1292. //setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1293. //systemPageRestoreInit();
  1294. }
  1295. void _DetectEvChargingEnableTimeout(uint8_t gunIndex)
  1296. {
  1297. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1298. if (pDcChargingInfo->Type == _Type_Chademo) {
  1299. if (!isEvGunLocked_chademo(gunIndex)) {
  1300. log_info("*********** _DetectEvChargingEnableTimeout (chademo) ***********");
  1301. }
  1302. } else if (pDcChargingInfo->Type == _Type_GB) {
  1303. if (!isEvGunLocked_ccs(gunIndex)) {
  1304. log_info("*********** _DetectEvChargingEnableTimeout (gb) ***********");
  1305. }
  1306. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1307. if (!isEvGunLocked_ccs(gunIndex)) {
  1308. log_info("*********** _DetectEvChargingEnableTimeout (ccs) ***********");
  1309. }
  1310. }
  1311. //pSysInfo->SystemPage = _LCM_ERROR;
  1312. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1313. ChargingTerminalProcess(gunIndex);
  1314. _AutoReturnTimeout();
  1315. }
  1316. void _DetectEvseChargingEnableTimeout(uint8_t gunIndex)
  1317. {
  1318. log_info("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********");
  1319. setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1320. //pSysInfo->SystemPage = _LCM_ERROR;
  1321. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1322. _AutoReturnTimeout();
  1323. }
  1324. void _PrepareTimeout(uint8_t gunIndex)
  1325. {
  1326. log_info("*********** _PrepareTimeout ***********");
  1327. setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1328. pAlarmCode->AlarmEvents.bits.PsuNoResource = YES;
  1329. //pSysInfo->SystemPage = _LCM_ERROR;
  1330. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1331. _AutoReturnTimeout();
  1332. }
  1333. void _CcsPrechargeTimeout(uint8_t gunIndex)
  1334. {
  1335. log_info("*********** _CcsPrechargeTimeout ***********");
  1336. setChargerMode(pSysInfo->CurGunSelected, S_TERMINATING);
  1337. //pSysInfo->SystemPage = _LCM_ERROR;
  1338. pSysInfo->SystemPage = _LCM_MAINTAIN;
  1339. }
  1340. void _LinkErrorTimeout(uint8_t gunIndex)
  1341. {
  1342. log_info("*********** _LinkErrorTimeout ***********");
  1343. setChargerMode(pSysInfo->CurGunSelected, S_IDLE);
  1344. pSysInfo->SystemPage = _LCM_VIEW;
  1345. }
  1346. //===============================================
  1347. // 取得卡號與卡號驗證
  1348. //===============================================
  1349. void AuthorizingStart(void)
  1350. {
  1351. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1352. pSysInfo->AuthorizeFlag = YES;
  1353. }
  1354. void ClearAuthorizedFlag(void)
  1355. {
  1356. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1357. pSysInfo->AuthorizeFlag = NO;
  1358. }
  1359. bool isAuthorizedComplete(void)
  1360. {
  1361. if (pSysInfo->AuthorizeFlag == YES) {
  1362. return false;
  1363. }
  1364. return true;
  1365. }
  1366. bool IsAuthorizingMode()
  1367. {
  1368. if (pSysInfo->AuthorizeFlag == NO) {
  1369. return false;
  1370. }
  1371. return true;
  1372. }
  1373. //===============================================
  1374. // 紀錄 Alarm Code
  1375. //===============================================
  1376. void ResetChargerAlarmCode(uint8_t gunIndex, char *code)
  1377. {
  1378. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1379. if (strcmp(code, "012234") == EQUAL) {
  1380. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1381. } else if (strcmp(code, "012235") == EQUAL) {
  1382. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1383. } else if (strcmp(code, "012236") == EQUAL) {
  1384. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1385. } else if (strcmp(code, "012288") == EQUAL) {
  1386. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1387. } else if (strcmp(code, "012289") == EQUAL) {
  1388. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1389. } else if (strcmp(code, "012290") == EQUAL) {
  1390. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1391. } else if (strcmp(code, "012229") == EQUAL) {
  1392. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOTP = NO;
  1393. } else if (strcmp(code, "012230") == EQUAL) {
  1394. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOTP = NO;
  1395. } else if (strcmp(code, "012231") == EQUAL) {
  1396. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOTP = NO;
  1397. } else if (strcmp(code, "011011") == EQUAL) {
  1398. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1399. } else if (strcmp(code, "011013") == EQUAL) {
  1400. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1401. } else if (strcmp(code, "011015") == EQUAL) {
  1402. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1403. } else if (strcmp(code, "011012") == EQUAL) {
  1404. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1405. } else if (strcmp(code, "011014") == EQUAL) {
  1406. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1407. } else if (strcmp(code, "011016") == EQUAL) {
  1408. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1409. } else if (strcmp(code, "011018") == EQUAL) {
  1410. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectTempSensorFail = NO;
  1411. } else if (strcmp(code, "011019") == EQUAL) {
  1412. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectTempSensorFail = NO;
  1413. } else if (strcmp(code, "011020") == EQUAL) {
  1414. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectTempSensorFail = NO;
  1415. } else if (strcmp(code, "012323") == EQUAL) {
  1416. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerOTP = NO;
  1417. } else if (strcmp(code, "011038") == EQUAL) {
  1418. ShmDcCommonData->ChillerTempErr[gunIndex].StatusBit.ChillerTempSensorFail = NO;
  1419. }
  1420. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012229", 6) == EQUAL ||
  1421. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012230", 6) == EQUAL ||
  1422. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012231", 6) == EQUAL ||
  1423. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011011", 6) == EQUAL ||
  1424. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011013", 6) == EQUAL ||
  1425. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011015", 6) == EQUAL ||
  1426. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011012", 6) == EQUAL ||
  1427. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011014", 6) == EQUAL ||
  1428. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011016", 6) == EQUAL ||
  1429. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011018", 6) == EQUAL ||
  1430. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011019", 6) == EQUAL ||
  1431. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011020", 6) == EQUAL ||
  1432. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "012323", 6) == EQUAL ||
  1433. strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "011038", 6) == EQUAL) {
  1434. strncpy((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1435. }
  1436. }
  1437. void RecordAlarmCode(uint8_t gunIndex, char *code)
  1438. {
  1439. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1440. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1441. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1442. {
  1443. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1444. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1445. }
  1446. if (pDcChargingInfo->StopChargeFlag == NO)
  1447. {
  1448. pDcChargingInfo->StopChargeFlag = YES;
  1449. }
  1450. }
  1451. }
  1452. void ReleaseAlarmCode(uint8_t gunIndex)
  1453. {
  1454. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1455. //if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1456. // return;
  1457. //}
  1458. switch (pDcChargingInfo->Type) {
  1459. case _Type_Chademo:
  1460. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  1461. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaUvpFail = NO;
  1462. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP = NO;
  1463. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  1464. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = NO;
  1465. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = NO;
  1466. break;
  1467. case _Type_CCS_2:
  1468. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  1469. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSUvpFail = NO;
  1470. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP = NO;
  1471. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  1472. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = NO;
  1473. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = NO;
  1474. break;
  1475. case _Type_GB:
  1476. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  1477. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTUvpFail = NO;
  1478. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP = NO;
  1479. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  1480. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = NO;
  1481. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = NO;
  1482. break;
  1483. }
  1484. memcpy(pDcChargingInfo->ConnectorAlarmCode, "", 6);
  1485. }
  1486. //===============================================
  1487. // EmergencyStop and Charging Stop
  1488. //===============================================
  1489. void ChargingTerminalProcess(uint8_t gunIndex)
  1490. {
  1491. setChargerMode(gunIndex, MODE_TERMINATING);
  1492. }
  1493. void ChargingAlarmProcess(uint8_t gunIndex)
  1494. {
  1495. UpdateErrorCodeToOcpp(gunIndex);
  1496. setChargerMode(gunIndex, MODE_ALARM);
  1497. }
  1498. void AcChargingTerminalProcess(void)
  1499. {
  1500. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  1501. pAcChargingInfo->SystemStatus = MODE_TERMINATING;
  1502. }
  1503. void StopChargingProcessByString(uint8_t level)
  1504. {
  1505. if (level > pSysWarning->Level) {
  1506. pSysWarning->Level = level;
  1507. }
  1508. }
  1509. void ReleaseChargingProcessByString(uint8_t level)
  1510. {
  1511. if (level >= pSysWarning->Level) {
  1512. pSysWarning->Level = WARN_LV_NL;
  1513. }
  1514. }
  1515. // 一般錯誤停止充電處理函式
  1516. void BoardErrOccurByString(uint8_t index, char *code)
  1517. {
  1518. uint8_t level = 1;
  1519. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1520. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1521. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1522. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1523. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1524. if (strncmp(code, "023730", 6) == EQUAL &&
  1525. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == NO
  1526. ) {
  1527. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  1528. }
  1529. ChargingTerminalProcess(index);
  1530. }
  1531. StopChargingProcessByString(level);
  1532. }
  1533. void ReleaseBoardErrOccurByString(uint8_t index, char *code)
  1534. {
  1535. bool isTrigger = false;
  1536. uint8_t level = 1;
  1537. if (strncmp(code, "023730", 6) == 0 &&
  1538. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop == YES) {
  1539. isTrigger = true;
  1540. pInfoCode->InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1541. }
  1542. if (isTrigger) {
  1543. ReleaseChargingProcessByString(level);
  1544. }
  1545. }
  1546. // 急停狀況的停止充電處理函式
  1547. void EmcOccureByString(char *code)
  1548. {
  1549. uint8_t level = 2;
  1550. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  1551. // 其錯誤等級為 2
  1552. //DS60-120 remove
  1553. if (strncmp(code, "012251", 6) == EQUAL ||
  1554. strncmp(code, "012252", 6) == EQUAL ||
  1555. strncmp(code, "012238", 6) == EQUAL ||
  1556. strncmp(code, "042251", 6) == EQUAL ||
  1557. strncmp(code, "042252", 6) == EQUAL ||
  1558. strncmp(code, "012304", 6) == EQUAL ||
  1559. strncmp(code, "042200", 6) == EQUAL ||
  1560. strncmp(code, "042201", 6) == EQUAL ||
  1561. strncmp(code, "042202", 6) == EQUAL ||
  1562. strncmp(code, "042267", 6) == EQUAL) {
  1563. for (uint8_t gun = 0; gun < pSysConfig->TotalConnectorCount; gun++) {
  1564. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  1565. //strncpy((char *)ShmOCPP16Data->StatusNotification[gun].VendorErrorCode, code, 6);
  1566. if ((pDcChargingInfo->SystemStatus > S_IDLE &&
  1567. pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  1568. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1569. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  1570. //ChargingTerminalProcess(gun);
  1571. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1572. memcpy(pDcChargingInfo->ConnectorAlarmCode, code, 6);
  1573. }
  1574. ChargingAlarmProcess(gun);
  1575. }
  1576. }
  1577. StopChargingProcessByString(level);
  1578. InformOcppErrOccur(4);
  1579. }
  1580. }
  1581. void ReleaseEmsOccureByString(uint8_t index, char *code)
  1582. {
  1583. bool isTrigger = false;
  1584. uint8_t level = 2;
  1585. if (strncmp(code, "042251", 6) == 0 ) {
  1586. isTrigger = true;
  1587. } else if (strncmp(code, "012251", 6) == 0 &&
  1588. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1589. isTrigger = true;
  1590. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip = NO;
  1591. } else if (strncmp(code, "012252", 6) == 0 &&
  1592. pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1593. isTrigger = true;
  1594. pAlarmCode->AlarmEvents.bits.DoorOpen = NO;
  1595. } else if (strncmp(code, "012237", 6) == 0 &&
  1596. pAlarmCode->AlarmEvents.bits.SpdTrip == YES) {
  1597. isTrigger = true;
  1598. pAlarmCode->AlarmEvents.bits.SpdTrip = NO;
  1599. } else if (strncmp(code, "012238", 6) == 0 &&
  1600. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1601. isTrigger = true;
  1602. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1603. }
  1604. if (isTrigger) {
  1605. ReleaseChargingProcessByString(level);
  1606. InformOcppErrOccur(6);
  1607. }
  1608. }
  1609. static void checkOvpState(uint8_t gunIndex, uint8_t gunType)
  1610. {
  1611. switch(gunType) {
  1612. case _Type_Chademo:
  1613. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaConnectOVP == YES)
  1614. RecordAlarmCode(gunIndex, "012217");
  1615. break;
  1616. case _Type_CCS_2:
  1617. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSConnectOVP == YES)
  1618. RecordAlarmCode(gunIndex, "012219");
  1619. break;
  1620. case _Type_GB:
  1621. if(ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTConnectOVP == YES)
  1622. RecordAlarmCode(gunIndex, "012221");
  1623. break;
  1624. }
  1625. }
  1626. //===============================================
  1627. // 確認各小板偵測的錯誤狀況
  1628. //===============================================
  1629. void CheckErrorOccurStatus(uint8_t index)
  1630. {
  1631. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  1632. // RB
  1633. if (pSysConfig->PhaseLossPolicy == YES) {
  1634. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES ||
  1635. pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES ||
  1636. pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1637. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1638. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  1639. StopChargingProcessByString(2);
  1640. InformOcppErrOccur(13);
  1641. }
  1642. //DS60-120 add -----
  1643. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1644. if (pAlarmCode->AlarmEvents.bits.SystemL1InputUVP == YES) {
  1645. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012203", 6);
  1646. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputUVP == YES) {
  1647. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012204", 6);
  1648. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputUVP == YES) {
  1649. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012205", 6);
  1650. }
  1651. }
  1652. //log_info("1 CheckErrorOccurStatus");
  1653. pDcChargingInfo->StopChargeFlag = YES;
  1654. //------------------------------------------------------------------
  1655. } else {
  1656. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1657. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1658. ReleaseChargingProcessByString(2);
  1659. InformOcppErrOccur(6);
  1660. }
  1661. }
  1662. } else {
  1663. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  1664. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1665. ReleaseChargingProcessByString(2);
  1666. InformOcppErrOccur(6);
  1667. }
  1668. }
  1669. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES ||
  1670. pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES ||
  1671. pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1672. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  1673. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  1674. StopChargingProcessByString(2);
  1675. InformOcppErrOccur(14);
  1676. }
  1677. //DS60-120 -----
  1678. if (strncmp((char *)pDcChargingInfo->ConnectorAlarmCode, "", 6) == EQUAL) {
  1679. if (pAlarmCode->AlarmEvents.bits.SystemL1InputOVP == YES) {
  1680. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012200", 6);
  1681. } else if (pAlarmCode->AlarmEvents.bits.SystemL2InputOVP == YES) {
  1682. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012201", 6);
  1683. } else if (pAlarmCode->AlarmEvents.bits.SystemL3InputOVP == YES) {
  1684. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012202", 6);
  1685. }
  1686. }
  1687. //log_info("2 CheckErrorOccurStatus");
  1688. pDcChargingInfo->StopChargeFlag = YES;
  1689. //----------------------------------------------------------------------
  1690. } else {
  1691. if (pSysWarning->ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP) {
  1692. pSysWarning->ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  1693. ReleaseChargingProcessByString(2);
  1694. InformOcppErrOccur(6);
  1695. }
  1696. }
  1697. checkOvpState(index, pDcChargingInfo->Type);
  1698. //--------------------------------------------------------------------------
  1699. if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == 0) {
  1700. //Primary
  1701. if (pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES) {
  1702. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012251", 6);
  1703. } else if (pAlarmCode->AlarmEvents.bits.DoorOpen == YES) {
  1704. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012252", 6);
  1705. } else if (pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  1706. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012238", 6);
  1707. } else if (pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES) {
  1708. memcpy(pDcChargingInfo->ConnectorAlarmCode, "012304", 6);
  1709. }
  1710. //Chiller temperature
  1711. //else if (pAlarmCode->AlarmEvents.bits.SystemChillerOTP == YES) {
  1712. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "012323", 6);
  1713. //} else if (pFaultCode->FaultEvents.bits.ChillerTempSensorBroken == YES) {
  1714. // memcpy(pDcChargingInfo->ConnectorAlarmCode, "011038", 6);
  1715. //}
  1716. }
  1717. }
  1718. //===============================================
  1719. // 確認 GPIO 狀態
  1720. //===============================================
  1721. void gpio_set_value(unsigned int gpio, unsigned int value)
  1722. {
  1723. int fd;
  1724. char buf[MAX_BUF];
  1725. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1726. fd = open(buf, O_WRONLY);
  1727. if (fd < 0) {
  1728. perror("gpio/set-value");
  1729. return;
  1730. }
  1731. if (value) {
  1732. write(fd, "1", 2);
  1733. } else {
  1734. write(fd, "0", 2);
  1735. }
  1736. close(fd);
  1737. }
  1738. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1739. {
  1740. int fd;
  1741. char buf[MAX_BUF];
  1742. char ch;
  1743. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1744. fd = open(buf, O_RDONLY);
  1745. if (fd < 0) {
  1746. perror("gpio/get-value");
  1747. return fd;
  1748. }
  1749. read(fd, &ch, 1);
  1750. if (ch != '0') {
  1751. *value = 1;
  1752. } else {
  1753. *value = 0;
  1754. }
  1755. close(fd);
  1756. return 0;
  1757. }
  1758. void CheckGunTypeFromHw()
  1759. {
  1760. uint8_t i = 0;
  1761. int pinIn[4] = {22, 23, 44, 45};
  1762. unsigned int gpioValue = 0;
  1763. uint8_t tmp[2] = {0};
  1764. log_info("ModelName = %s", pSysConfig->ModelName);
  1765. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1766. gpio_get_value(pinIn[i], &gpioValue);
  1767. switch (pinIn[i]) {
  1768. //right slot
  1769. case 22:
  1770. bd1_1_status = gpioValue;
  1771. break;
  1772. case 23:
  1773. bd1_2_status = gpioValue;
  1774. break;
  1775. //left slot
  1776. case 44:
  1777. bd0_1_status = gpioValue;
  1778. break;
  1779. case 45:
  1780. bd0_2_status = gpioValue;
  1781. break;
  1782. }
  1783. }
  1784. //BD1(Left-CCS-CND1-SMR2-左槍), BD2(Right-CHADEMO-CND2-SMR1-右槍), CCS: 10 , CHAdeMO: 01 , GBT: 11
  1785. //CcsChargingData [0至1] 分別為 Right至Left
  1786. //model name 槍順序左至右分別為Right至Left
  1787. tmp[1] = (bd0_1_status << 4 | bd0_2_status);
  1788. tmp[0] = (bd1_1_status << 4 | bd1_2_status);
  1789. for (i = 0; i < 2; i++) {
  1790. switch (tmp[i]) {
  1791. case 0x01:
  1792. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CHAdeMO");
  1793. break;
  1794. case 0x10:
  1795. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "CCS");
  1796. break;
  1797. case 0x11:
  1798. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "GBT");
  1799. break;
  1800. case 0x00:
  1801. log_info("BD%d(%s) = %s ", i + 1, i == 0 ? "Left" : "Right", "None");
  1802. break;
  1803. }
  1804. }
  1805. }
  1806. void CheckGpioInStatus()
  1807. {
  1808. int i = 0;
  1809. int pinIn[2] = { 27, 47 };//{IO BD1_2, IO BD2_2}
  1810. unsigned int gpioValue = 0;
  1811. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1812. gpio_get_value(pinIn[i], &gpioValue);
  1813. if (gpioValue == 0x01) {
  1814. switch (pinIn[i]) {
  1815. // 小板緊急停止
  1816. case 47:
  1817. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1818. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1819. if (pDcChargingInfo->slotsIndex == 1) {
  1820. if (pDcChargingInfo->Type == _Type_Chademo) {
  1821. BoardErrOccurByString(i, "023730");
  1822. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1823. BoardErrOccurByString(i, "013627");
  1824. }
  1825. break;
  1826. }
  1827. }
  1828. break;
  1829. case 27:
  1830. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1831. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1832. if (pDcChargingInfo->slotsIndex == 3) {
  1833. if (pDcChargingInfo->Type == _Type_Chademo) {
  1834. BoardErrOccurByString(i, "023730");
  1835. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1836. BoardErrOccurByString(i, "013627");
  1837. }
  1838. break;
  1839. }
  1840. }
  1841. break;
  1842. }
  1843. } else {
  1844. switch (pinIn[i]) {
  1845. // 小板解除緊急停止
  1846. case 47:
  1847. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1848. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1849. if (pDcChargingInfo->slotsIndex == 1) {
  1850. if (pDcChargingInfo->Type == _Type_Chademo) {
  1851. ReleaseBoardErrOccurByString(i, "023730");
  1852. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1853. ReleaseBoardErrOccurByString(i, "013627");
  1854. }
  1855. break;
  1856. }
  1857. }
  1858. break;
  1859. case 27:
  1860. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  1861. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1862. if (pDcChargingInfo->slotsIndex == 3) {
  1863. if (pDcChargingInfo->Type == _Type_Chademo) {
  1864. ReleaseBoardErrOccurByString(i, "023730");
  1865. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  1866. ReleaseBoardErrOccurByString(i, "013627");
  1867. }
  1868. break;
  1869. }
  1870. }
  1871. break;
  1872. }
  1873. }
  1874. }
  1875. }
  1876. //===============================================
  1877. // Main process
  1878. //===============================================
  1879. // 檢查 uint8_t 中某個 Bit 的值
  1880. // _byte : 欲改變的 byte
  1881. // _bit : 該 byte 的第幾個 bit
  1882. uint8_t DetectBitValue(uint8_t _byte, uint8_t _bit)
  1883. {
  1884. uint8_t mask_table[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
  1885. return ( _byte & mask_table[_bit] ) != 0x00;
  1886. }
  1887. // 設定 Byte 中某個 Bit的值
  1888. // _byte : 欲改變的 byte
  1889. // _bit : 該 byte 的第幾個 bit
  1890. // value : 修改的值為 0 or 1
  1891. void SetBitValue(uint8_t *_byte, uint8_t _bit, uint8_t value)
  1892. {
  1893. if (value == 1) {
  1894. *_byte |= (1 << _bit);
  1895. } else if (value == 0) {
  1896. *_byte ^= (1 << _bit);
  1897. }
  1898. }
  1899. uint8_t isModeChange(uint8_t gunIndex)
  1900. {
  1901. uint8_t result = NO;
  1902. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1903. if (pDcChargingInfo->SystemStatus == pDcChargingInfo->PreviousSystemStatus) {
  1904. return result;
  1905. }
  1906. pDcChargingInfo->PreviousSystemStatus = pDcChargingInfo->SystemStatus;
  1907. ShmDcCommonData->SystemModeChange[gunIndex] = YES; //for Module_EvRxComm print temperature message
  1908. return YES;
  1909. }
  1910. static int checkSlotGpioMappingGunType(uint8_t gunIndex, uint8_t gunType)
  1911. {
  1912. uint8_t slot1 = 0, slot2 = 0;
  1913. if (gunIndex == 0) {
  1914. slot1 = bd0_1_status;
  1915. slot2 = bd0_2_status;
  1916. } else if (gunIndex == 1) {
  1917. slot1 = bd1_1_status;
  1918. slot2 = bd1_2_status;
  1919. }
  1920. log_info("slot %d gpio1 = %d, gpio2 = %d, type = %d",
  1921. gunIndex,
  1922. slot1,
  1923. slot2,
  1924. gunType);
  1925. switch (gunType) {
  1926. case _Type_Chademo:
  1927. if (slot1 != NO && slot2 != YES) {
  1928. return FAIL;
  1929. }
  1930. break;
  1931. case _Type_CCS_2:
  1932. if (slot1 != YES && slot2 != NO) {
  1933. return FAIL;
  1934. }
  1935. break;
  1936. case _Type_GB:
  1937. if (slot1 != YES && slot2 != YES) {
  1938. return FAIL;
  1939. }
  1940. break;
  1941. }
  1942. return PASS;
  1943. }
  1944. bool CheckConnectorTypeStatus(void)
  1945. {
  1946. bool result = true;
  1947. uint8_t gunIndex = 0;
  1948. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  1949. struct ChargingInfoData *pDcChargingInfo = NULL;
  1950. if (!MappingGunChargingInfo("CSU Task")) {
  1951. log_error("CheckConnectorTypeStatus MappingGunChargingInfo failed");
  1952. return false;
  1953. }
  1954. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  1955. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  1956. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  1957. if (checkSlotGpioMappingGunType(gunIndex, pDcChargingInfo->Type) == FAIL) {
  1958. return false;
  1959. }
  1960. pDcChargingInfo->SystemStatus = S_BOOTING;
  1961. switch (gunIndex) {
  1962. case 0:
  1963. if (pSysConfig->TotalConnectorCount == 1) {
  1964. if ((bd0_1_status == YES || bd0_2_status == YES) &&
  1965. (bd1_1_status == NO && bd1_2_status == NO)) {
  1966. pDcChargingInfo->Evboard_id = 0x01;
  1967. }
  1968. if ((bd0_1_status == NO && bd0_2_status == NO) &&
  1969. (bd1_1_status == YES || bd1_2_status == YES)
  1970. ) {
  1971. pDcChargingInfo->Evboard_id = 0x02;
  1972. }
  1973. break;
  1974. }
  1975. pDcChargingInfo->Evboard_id = 0x01;
  1976. break;
  1977. case 1:
  1978. pDcChargingInfo->Evboard_id = 0x02;
  1979. break;
  1980. }
  1981. CheckHwSlotStatusLog(gunIndex);
  1982. if (pDcChargingInfo->Evboard_id == 0x00) {
  1983. return false;
  1984. }
  1985. }
  1986. AdjustChargerCurrent();
  1987. return result;
  1988. }
  1989. int SpawnTask(void)
  1990. {
  1991. if(SIMULATION)
  1992. system("/root/simulation &");
  1993. sleep(2);
  1994. system("/root/Module_EventLogging &");
  1995. system("/root/Module_PrimaryComm &");
  1996. system("/root/Module_EvComm &");
  1997. system("/root/Module_LcmControl &");
  1998. system("/root/Module_InternalComm &");
  1999. system("/root/Module_ProduceUtils &");
  2000. system("/root/Module_UpdateFW &");
  2001. system("/root/Module_DoComm &");
  2002. return PASS;
  2003. }
  2004. void KillTask(void)
  2005. {
  2006. //ChangeLcmByIndex(_LCM_ERROR);
  2007. ChangeLcmByIndex(_LCM_MAINTAIN);
  2008. system("killall Module_EventLogging");
  2009. system("killall Module_PrimaryComm");
  2010. system("killall Module_EvComm");
  2011. system("killall Module_LcmControl");
  2012. system("killall Module_InternalComm");
  2013. system("killall Module_UpdateFW");
  2014. system("killall Module_ChkSysTask");
  2015. //system("killall Module_DoComm");
  2016. return ;
  2017. }
  2018. void KillTaskExceptPrimary(void)
  2019. {
  2020. //ChangeLcmByIndex(_LCM_ERROR);
  2021. ChangeLcmByIndex(_LCM_MAINTAIN);
  2022. system("killall Module_EvComm");
  2023. system("killall Module_InternalComm");
  2024. return;
  2025. }
  2026. void KillAllTask(void)
  2027. {
  2028. //ChangeLcmByIndex(_LCM_ERROR);
  2029. ChangeLcmByIndex(_LCM_MAINTAIN);
  2030. system("killall Module_EventLogging");
  2031. system("killall Module_PrimaryComm");
  2032. system("killall Module_EvComm");
  2033. system("killall Module_LcmControl");
  2034. system("killall Module_InternalComm");
  2035. system("killall Module_UpdateFW");
  2036. system("killall Module_ChkSysTask");
  2037. system("killall Module_DoComm");
  2038. return ;
  2039. }
  2040. void StartSystemTimeoutDet(uint8_t flag)
  2041. {
  2042. if (pSysInfo->SystemTimeoutFlag != flag) {
  2043. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2044. }
  2045. pSysInfo->SystemTimeoutFlag = flag;
  2046. }
  2047. void StopSystemTimeoutDet(void)
  2048. {
  2049. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  2050. pSysInfo->SystemTimeoutFlag = Timeout_None;
  2051. }
  2052. void StartGunInfoTimeoutDet(uint8_t gunIndex, uint8_t flag)
  2053. {
  2054. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2055. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2056. if (pDcChargingInfo->TimeoutFlag != flag) {
  2057. gettimeofday(&pDcChargingInfo->TimeoutTimer, NULL);
  2058. }
  2059. pDcChargingInfo->TimeoutFlag = flag;
  2060. }
  2061. }
  2062. void StopGunInfoTimeoutDet(uint8_t gunIndex)
  2063. {
  2064. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2065. if (gunIndex < pSysConfig->TotalConnectorCount) {
  2066. pDcChargingInfo->TimeoutFlag = Timeout_None;
  2067. }
  2068. }
  2069. void CheckConnectionTimeout(void)
  2070. {
  2071. if (ShmSelectGunInfo->RemoteSetup.ConnectionTimeout != 0) { //Jerry add
  2072. _connectionTimeout = ShmSelectGunInfo->RemoteSetup.ConnectionTimeout;
  2073. } else {
  2074. _connectionTimeout = CONN_PLUG_TIMEOUT;
  2075. }
  2076. return;
  2077. }
  2078. void _evccidlinktimeout(uint8_t gunIndex)
  2079. {
  2080. log_info("Getting EVCCID Timeout");
  2081. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2082. pDcChargingInfo->isEVCCIDVerify = true;
  2083. StopGunInfoTimeoutDet(gunIndex);
  2084. destroySelGun(pSysInfo->CurGunSelected);
  2085. systemPageRestoreInit();
  2086. }
  2087. void CreateTimeoutFork(void)
  2088. {
  2089. pid_t timeoutPid;
  2090. timeoutPid = fork();
  2091. if (timeoutPid == 0) {
  2092. gettimeofday(&_cmdSubPriority_time, NULL);
  2093. CheckConnectionTimeout();
  2094. //log_info("Timeout Fork Child's PID is %d", getpid());
  2095. while (1) {
  2096. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000) {
  2097. CheckConnectionTimeout();
  2098. gettimeofday(&_cmdSubPriority_time, NULL);
  2099. }
  2100. //if (pSysInfo->SystemTimeoutFlag != 0)
  2101. //log_info("Timeout ***********SystemTimeoutFlag = %d(%d) ********",pSysInfo->SystemTimeoutFlag,
  2102. // GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL);
  2103. // 系統
  2104. switch (pSysInfo->SystemTimeoutFlag) {
  2105. case Timeout_SelftestChk:
  2106. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT) {
  2107. _SelfTestTimeout();
  2108. StopSystemTimeoutDet();
  2109. destroySelGun(DESTROY_ALL_SEL); //jerry add
  2110. }
  2111. break;
  2112. case Timeout_ReturnViewPage:
  2113. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_VIEWPAGE_TIMEOUT) {
  2114. StopSystemTimeoutDet();
  2115. pSysInfo->SystemPage = _LCM_VIEW;
  2116. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2117. if (pDcChargingInfo->SystemStatus == S_AUTHORIZING || pDcChargingInfo->SystemStatus == S_COMPLETE )
  2118. setChargerMode(pSysInfo->CurGunSelected, MODE_IDLE);
  2119. }
  2120. break;
  2121. case Timeout_Authorizing:
  2122. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT) {
  2123. log_error("Authorizing Timeout");
  2124. _AuthorizedTimeout();
  2125. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2126. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2127. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2128. log_error("Author timeout restart DoComm");
  2129. system("killall Module_DoComm");
  2130. }
  2131. }
  2132. break;
  2133. case Timeout_WaitBalance:
  2134. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= BALANCE_TIMEOUT) {
  2135. log_error("Wait Balance timeout");
  2136. _AuthorizedTimeout();
  2137. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2138. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2139. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2140. log_error("Author timeout restart DoComm");
  2141. system("killall Module_DoComm");
  2142. }
  2143. }
  2144. break;
  2145. case Timeout_VerifyFail:
  2146. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT) {
  2147. if (pSysInfo->SystemPage ==_LCM_START_AUTHORIZE_FAIL ) {
  2148. StopSystemTimeoutDet();
  2149. _AutoReturnTimeout();
  2150. //destroySelGun(pSysInfo->CurGunSelected);
  2151. /*
  2152. if (ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] == YES) { //DoComm no ask cabinet balance
  2153. ShmSelectGunInfo->AuthorStateFromCabinet[pSysInfo->CurGunSelected] = NO;
  2154. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = ABNORMAL;
  2155. log_error("Author timeout restart DoComm");
  2156. system("killall Module_DoComm");
  2157. //sleep(1);
  2158. //system("/root/Module_DoComm &");
  2159. }*/
  2160. } else if (pSysInfo->SystemPage ==_LCM_STOP_RFID_FAIL ) {
  2161. StopSystemTimeoutDet();
  2162. pSysInfo->SystemPage = _LCM_VIEW;
  2163. }
  2164. }
  2165. break;
  2166. case Timeout_WaitPlug:
  2167. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout) {
  2168. _DetectPlugInTimeout();
  2169. //destroySelGun(pSysInfo->CurGunSelected);
  2170. }
  2171. break;
  2172. /*
  2173. case Timeout_ReturnToChargingGunDet:
  2174. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE) {
  2175. if (getCurLcmPage() != _LCM_VIEW &&
  2176. getCurLcmPage() != _LCM_DETAIL_VIEW ) {
  2177. destroySelGun(pSysInfo->CurGunSelected); //jerry add
  2178. }
  2179. DisplayChargingInfo();
  2180. StopSystemTimeoutDet();
  2181. }
  2182. break;
  2183. */
  2184. case Timeout_AuthorizingForStop:
  2185. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT) {
  2186. strcpy((char *)pSysConfig->UserId, "");
  2187. ClearAuthorizedFlag();
  2188. StopSystemTimeoutDet();
  2189. }
  2190. break;
  2191. case Timeout_Terminating:
  2192. if (GetClockTimeoutValue(pSysInfo->SystemTimeoutTimer) / uSEC_VAL >= TERMINATING_TIMEOUT) {
  2193. StopSystemTimeoutDet();
  2194. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2195. log_info("Terminating timeout");
  2196. setChargerMode(pSysInfo->CurGunSelected, S_COMPLETE);
  2197. }
  2198. break;
  2199. }
  2200. // 各槍
  2201. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2202. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2203. /*
  2204. if (pDcChargingInfo->TimeoutFlag != 0)
  2205. log_info("Timeout ***********GunTimeoutFlag = %d(%d) ********",pDcChargingInfo->TimeoutFlag,
  2206. GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL);
  2207. */
  2208. switch (pDcChargingInfo->TimeoutFlag) {
  2209. case Timeout_EVCCID_Link:
  2210. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= EVCCID_LINK_TIMEOUT) {
  2211. _evccidlinktimeout(gunIndex);
  2212. }
  2213. break;
  2214. case Timeout_LinkError:
  2215. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= LINKERROR_TIMEOUT) {
  2216. _LinkErrorTimeout(gunIndex);
  2217. StopGunInfoTimeoutDet(gunIndex);
  2218. destroySelGun(gunIndex);
  2219. }
  2220. break;
  2221. case Timeout_Preparing:
  2222. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT) {
  2223. _PrepareTimeout(gunIndex);
  2224. StopGunInfoTimeoutDet(gunIndex);
  2225. destroySelGun(gunIndex); //jerry add
  2226. }
  2227. break;
  2228. case Timeout_EvChargingDet:
  2229. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT) {
  2230. _DetectEvChargingEnableTimeout(gunIndex);
  2231. StopGunInfoTimeoutDet(gunIndex);
  2232. destroySelGun(gunIndex); //jerry add
  2233. }
  2234. break;
  2235. case Timeout_EvseChargingDet:
  2236. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT) {
  2237. _DetectEvseChargingEnableTimeout(gunIndex);
  2238. StopGunInfoTimeoutDet(gunIndex);
  2239. destroySelGun(gunIndex); //jerry add
  2240. }
  2241. break;
  2242. case Timeout_EvseCompleteDet:
  2243. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT) {
  2244. StopGunInfoTimeoutDet(gunIndex);
  2245. destroySelGun(gunIndex); //jerry add
  2246. }
  2247. break;
  2248. case Timeout_ForCcsPrechargeDet:
  2249. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT) {
  2250. _CcsPrechargeTimeout(gunIndex);
  2251. StopGunInfoTimeoutDet(gunIndex);
  2252. destroySelGun(gunIndex); //jerry add
  2253. }
  2254. break;
  2255. /*
  2256. case Timeout_SelectGun: //Jerry add
  2257. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= SEL_GUN_TIMEOUT) {
  2258. log_info("Timeout_SelectGun");
  2259. StopGunInfoTimeoutDet(gunIndex);
  2260. destroySelGun(gunIndex); //jerry add
  2261. }
  2262. break;
  2263. */
  2264. case Timeout_DetailView:
  2265. if (GetTimeoutValue(pDcChargingInfo->TimeoutTimer) / uSEC_VAL >= DETAILVIEW_TIMEOUT) {
  2266. StopGunInfoTimeoutDet(gunIndex);
  2267. pSysInfo->SystemPage = _LCM_VIEW;
  2268. }
  2269. break;
  2270. }
  2271. }
  2272. sleep(1);
  2273. }
  2274. }
  2275. }
  2276. void CheckFactoryConfigFunction(void)
  2277. {
  2278. char Buf[256] = {0};
  2279. if (pSysInfo->FactoryConfiguration) {
  2280. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  2281. pSysConfig->ModelName,
  2282. pSysConfig->SerialNumber);
  2283. system(Buf);
  2284. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2285. system("sync");
  2286. sleep(5);
  2287. system("reboot -f");
  2288. sleep(5);
  2289. system("reboot -f");
  2290. }
  2291. }
  2292. //===============================================
  2293. // Check reservation date is expired
  2294. //===============================================
  2295. int isReservationExpired(uint8_t gunIndex)
  2296. {
  2297. int result = NO;
  2298. struct tm expiredDate;
  2299. struct timeb expiredTime;
  2300. if (sscanf((char *) ShmOCPP16Data->ReserveNow[gunIndex].ExpiryDate,
  2301. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  2302. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  2303. &expiredDate.tm_min, &expiredDate.tm_sec) == 6) {
  2304. expiredDate.tm_year -= 1900;
  2305. expiredDate.tm_mon -= 1;
  2306. expiredTime.time = mktime(&expiredDate);
  2307. if (!CheckTimeOut(expiredTime)) {
  2308. result = YES;
  2309. }
  2310. }
  2311. return result;
  2312. }
  2313. //===============================================
  2314. // OCPP
  2315. //===============================================
  2316. void CheckOcppStatus(void)
  2317. {
  2318. bool canReset = true;
  2319. //bool canHardReset = true; //DS60-120 add
  2320. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES) {
  2321. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  2322. }
  2323. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES) {
  2324. if (pSysWarning->Level != WARN_LV_ER) {
  2325. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2326. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2327. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2328. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  2329. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  2330. canReset = false;
  2331. if (pDcChargingInfo->SystemStatus >= S_REASSIGN &&
  2332. pDcChargingInfo->SystemStatus < S_TERMINATING) {
  2333. //canHardReset = false;
  2334. ChargingTerminalProcess(_index);
  2335. //restartFlag = 1;
  2336. }
  2337. }
  2338. }
  2339. }
  2340. if (canReset) {
  2341. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  2342. sprintf((char *)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2343. if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL) {
  2344. log_error("****** Hard Reboot ****** ");
  2345. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2346. sleep(3);
  2347. system("reboot -f");
  2348. } else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL) {
  2349. log_error("****** Soft Reboot ****** ");
  2350. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  2351. sleep(3);
  2352. system("killall OcppBackend &");
  2353. KillAllTask();
  2354. TryCloseWatchdog();
  2355. system("/usr/bin/run_evse_restart.sh");
  2356. }
  2357. }
  2358. }
  2359. }
  2360. void OcppStopTransation(uint8_t gunIndex)
  2361. {
  2362. uint8_t _OcppGunIndex = gunIndex;
  2363. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2364. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  2365. if (pSysConfig->AcConnectorCount == 1 && gunIndex == 1) {
  2366. _OcppGunIndex = 2;
  2367. }
  2368. if (strcmp((char *)pDcChargingInfo->StartUserId, "") == EQUAL) {
  2369. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2370. } else {
  2371. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)pDcChargingInfo->StartUserId);
  2372. }
  2373. log_info("IdTag = %s ", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  2374. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  2375. }
  2376. bool OcppRemoteStop(uint8_t gunIndex)
  2377. {
  2378. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2379. // 有 AC 槍的話
  2380. if (acDirIndex > 0 && gunIndex > 0) {
  2381. gunIndex += acDirIndex;
  2382. }
  2383. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  2384. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES) {
  2385. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  2386. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  2387. }
  2388. return result;
  2389. }
  2390. bool WifiScheduleStop(uint8_t gunIndex)
  2391. {
  2392. bool result = false;
  2393. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2394. result = pDcChargingInfo->schedule.isTriggerStop;
  2395. pDcChargingInfo->schedule.isTriggerStop = NO;
  2396. return result;
  2397. }
  2398. void OcppRemoteStartChk()
  2399. {
  2400. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  2401. } else if (!isDetectPlugin()) {
  2402. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  2403. uint8_t acDirIndex = pSysConfig->AcConnectorCount;
  2404. for (uint8_t ac_index = 0; ac_index < pSysConfig->AcConnectorCount; ac_index++) {
  2405. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(ac_index);
  2406. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES) {
  2407. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2408. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2409. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2410. pAcChargingInfo->RemoteStartFlag = YES;
  2411. pSysInfo->OrderCharging = YES;
  2412. //pSysInfo->OrderCharging = DEFAULT_AC_INDEX;
  2413. ShmOCPP16Data->CsMsg.bits[pSysConfig->TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  2414. DetectPluginStart();
  2415. return;
  2416. }
  2417. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  2418. }
  2419. }
  2420. uint8_t threeGunIndex = 0;
  2421. uint8_t dcIndex = 0;
  2422. bool isGunUsingStatus = false;
  2423. for (uint8_t _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  2424. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2425. // 如果有 AC 槍,且 DC 槍也有兩把
  2426. if (acDirIndex == 1 && _index == 1) {
  2427. threeGunIndex = 1;
  2428. }
  2429. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES) {
  2430. dcIndex = _index;
  2431. }
  2432. if (pDcChargingInfo->SystemStatus != S_IDLE) {
  2433. isGunUsingStatus = true;
  2434. }
  2435. }
  2436. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  2437. if (isGunUsingStatus && pSysInfo->IsAlternatvieConf) {
  2438. if (dcIndex == 0) {
  2439. threeGunIndex = 0;
  2440. }
  2441. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2442. return;
  2443. }
  2444. if (dcIndex == 0) {
  2445. threeGunIndex = 0;
  2446. }
  2447. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(dcIndex);
  2448. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES) {
  2449. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2450. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2451. pDcChargingInfo->RemoteStartFlag = YES;
  2452. pSysInfo->OrderCharging = YES;
  2453. //pSysInfo->OrderCharging = gunIndex;
  2454. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2455. DetectPluginStart();
  2456. setSelGunWaitToAuthor(dcIndex);
  2457. }
  2458. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  2459. }
  2460. }
  2461. }
  2462. void ChkOcppStatus(uint8_t gunIndex)
  2463. {
  2464. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2465. if (pDcChargingInfo->SystemStatus == S_IDLE &&
  2466. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES) {
  2467. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  2468. if (isReservationExpired(gunIndex)) {
  2469. log_error("***************ChkOcppStatus : OcppReservedStatus******************** ");
  2470. pDcChargingInfo->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  2471. pDcChargingInfo->SystemStatus = S_RESERVATION;
  2472. }
  2473. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  2474. }
  2475. if (pDcChargingInfo->SystemStatus == S_RESERVATION &&
  2476. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES) {
  2477. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  2478. if (isReservationExpired(gunIndex)) {
  2479. log_error("***************ChkOcppStatus : Cancel OcppReservedStatus******************** ");
  2480. pDcChargingInfo->ReservationId = 0;
  2481. pDcChargingInfo->SystemStatus = S_IDLE;
  2482. }
  2483. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  2484. }
  2485. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES) {
  2486. log_error("***************ChkOcppStatus : OcppChangeAvailability******************** ");
  2487. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  2488. if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL) {
  2489. if (isDb_ready) {
  2490. DB_Update_Operactive(gunIndex, true);
  2491. }
  2492. pDcChargingInfo->IsAvailable = YES;
  2493. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2494. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2495. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2496. setChargerMode(gunIndex, MODE_IDLE);
  2497. }
  2498. } else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL) {
  2499. if (isDb_ready) {
  2500. DB_Update_Operactive(gunIndex, false);
  2501. }
  2502. pDcChargingInfo->IsAvailable = NO;
  2503. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2504. pDcChargingInfo->SystemStatus == S_RESERVATION ||
  2505. pDcChargingInfo->SystemStatus == S_MAINTAIN) {
  2506. setChargerMode(gunIndex, MODE_MAINTAIN);
  2507. }
  2508. }
  2509. }
  2510. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES) {
  2511. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  2512. if (pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK && // DS60-120 add ||
  2513. pDcChargingInfo->SystemStatus <= S_CHARGING) {
  2514. // 充電中,需停止充電
  2515. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  2516. ChargingTerminalProcess(gunIndex);
  2517. }
  2518. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  2519. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  2520. }
  2521. }
  2522. bool CheckBackendChargingTimeout(uint8_t gunIndex)
  2523. {
  2524. bool result = false;
  2525. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2526. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2527. if (pSysConfig->MaxChargingDuration > 0) {
  2528. if (pDcChargingInfo->PresentChargedDuration > (pSysConfig->MaxChargingDuration * 60)) {
  2529. result = true;
  2530. }
  2531. }
  2532. }
  2533. return result;
  2534. }
  2535. bool CheckBackendChargingEnergy(uint8_t gunIndex)
  2536. {
  2537. bool result = false;
  2538. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2539. if (pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE) {
  2540. if (pSysConfig->MaxChargingEnergy > 0) {
  2541. if (pDcChargingInfo->PresentChargedEnergy > pSysConfig->MaxChargingEnergy) {
  2542. result = true;
  2543. }
  2544. }
  2545. }
  2546. return result;
  2547. }
  2548. void InformOcppErrOccur(uint8_t codeType)
  2549. {
  2550. char _error[25];
  2551. switch (codeType) {
  2552. case 4:
  2553. strcpy(_error, "InternalError");
  2554. break;
  2555. case 6:
  2556. strcpy(_error, "NoError");
  2557. break;
  2558. case 7:
  2559. strcpy(_error, "OtherError");
  2560. break;
  2561. case 13:
  2562. strcpy(_error, "UnderVoltage");
  2563. break;
  2564. case 14:
  2565. strcpy(_error, "OverVoltage");
  2566. break;
  2567. }
  2568. for (uint8_t gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  2569. strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].ErrorCode, _error);
  2570. }
  2571. }
  2572. //===============================================
  2573. // Config process
  2574. //===============================================
  2575. /*
  2576. void AddPlugInTimes(uint8_t gunIndex)
  2577. {
  2578. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2579. if (pDcChargingInfo->Type == _Type_Chademo) {
  2580. pSysConfig->ChademoPlugInTimes += 1;
  2581. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2582. pSysConfig->Ccs2PlugInTimes += 1;
  2583. } else if (pDcChargingInfo->Type == _Type_GB) {
  2584. pSysConfig->GbPlugInTimes += 1;
  2585. }
  2586. }*/
  2587. void ChangeStartOrStopDateTime(uint8_t isStart, uint8_t gunIndex)
  2588. {
  2589. char cmdBuf[32];
  2590. struct timeb csuTime;
  2591. struct tm *tmCSU;
  2592. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2593. ftime(&csuTime);
  2594. tmCSU = localtime(&csuTime.time);
  2595. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  2596. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2597. tmCSU->tm_sec);
  2598. if (isStart) {
  2599. strcpy((char *)pDcChargingInfo->StartDateTime, cmdBuf);
  2600. } else {
  2601. strcpy((char *)pDcChargingInfo->StopDateTime, cmdBuf);
  2602. }
  2603. }
  2604. void ChangeGunSelectByIndex(uint8_t sel)
  2605. {
  2606. pSysInfo->CurGunSelected = sel;
  2607. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  2608. }
  2609. void CheckIsAlternatvieByModelName()
  2610. {
  2611. if (pSysConfig->ModelName[1] == 'W') { //DS60-120
  2612. // 壁掛
  2613. pSysInfo->IsAlternatvieConf = YES;
  2614. } else {
  2615. pSysInfo->IsAlternatvieConf = NO;
  2616. }
  2617. }
  2618. void StopProcessingLoop()
  2619. {
  2620. for (;;) {
  2621. CheckFactoryConfigFunction();
  2622. //CheckFwUpdateFunction();
  2623. if (pSysWarning->Level == WARN_LV_ER) {
  2624. ChkPrimaryStatus();
  2625. if (pSysWarning->Level == WARN_LV_NL) {
  2626. log_info("Soft reboot for retry self-tets (Primary). ");
  2627. KillAllTask();
  2628. sleep(3);
  2629. TryCloseWatchdog();
  2630. system("/usr/bin/run_evse_restart.sh");
  2631. return;
  2632. }
  2633. }
  2634. sleep(1);
  2635. TryFeedWatchdog();
  2636. }
  2637. }
  2638. bool IsConnectorWholeIdle()
  2639. {
  2640. bool result = true;
  2641. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2642. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2643. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2644. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  2645. result = false;
  2646. break;
  2647. }
  2648. }
  2649. for (uint8_t count = 0; count < pSysConfig->AcConnectorCount; count++) {
  2650. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(count);
  2651. if (pAcChargingInfo->SystemStatus != S_IDLE &&
  2652. pAcChargingInfo->IsErrorOccur == NO) {
  2653. result = false;
  2654. break;
  2655. }
  2656. }
  2657. return result;
  2658. }
  2659. void ClearAlarmCodeWhenAcOff()
  2660. {
  2661. if (!pSysInfo->AcContactorStatus) {
  2662. pAlarmCode->AlarmEvents.bits.PsuNoResource = NO;
  2663. }
  2664. }
  2665. //==========================================
  2666. // Check Smart Charging Profile
  2667. //==========================================
  2668. int GetStartScheduleTime(uint8_t *time)
  2669. {
  2670. int result = -1;
  2671. struct tm tmScheduleStart;
  2672. struct timeb tbScheduleStart;
  2673. if ((sscanf((char *)time, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6)) {
  2674. tmScheduleStart.tm_year -= 1900;
  2675. tmScheduleStart.tm_mon -= 1;
  2676. tbScheduleStart.time = mktime(&tmScheduleStart);
  2677. tbScheduleStart.millitm = 0;
  2678. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  2679. }
  2680. return result;
  2681. }
  2682. void CheckSmartChargeProfile(uint8_t _index)
  2683. {
  2684. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  2685. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == YES) {
  2686. // Get Charging Profile
  2687. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  2688. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL &&
  2689. ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileId == YES //DS60-120 add
  2690. ) {
  2691. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  2692. uint8_t _startCount = NO_DEFINE;
  2693. uint8_t _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  2694. for (uint8_t _count = 0; _count < _maxCount; _count++) {
  2695. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  2696. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod) {
  2697. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  2698. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR) {
  2699. _startCount = _count;
  2700. }
  2701. }
  2702. }
  2703. log_info("_startCount = %d ", _startCount);
  2704. if (_startCount < _maxCount) {
  2705. //DS60-120 add
  2706. log_info("Profile Limit = %f ", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  2707. pDcChargingInfo->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  2708. if (pDcChargingInfo->EvBatterytargetVoltage > 0 && pDcChargingInfo->PresentChargingVoltage > 0) {
  2709. pDcChargingInfo->ChargingProfileCurrent = (pDcChargingInfo->ChargingProfilePower / pDcChargingInfo->PresentChargingVoltage) * 10;
  2710. } else {
  2711. pDcChargingInfo->ChargingProfileCurrent = 0;
  2712. }
  2713. } else {
  2714. pDcChargingInfo->ChargingProfilePower = -1;
  2715. pDcChargingInfo->ChargingProfileCurrent = -1;
  2716. }
  2717. } else {
  2718. pDcChargingInfo->ChargingProfilePower = -1;
  2719. pDcChargingInfo->ChargingProfileCurrent = -1;
  2720. }
  2721. log_info("ChargingProfilePower = %f ", pDcChargingInfo->ChargingProfilePower);
  2722. log_info("ChargingProfileCurrent = %f ", pDcChargingInfo->ChargingProfileCurrent);
  2723. }
  2724. }
  2725. void ChargingProfileFlat(uint8_t _index)
  2726. {
  2727. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO) {
  2728. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO) {
  2729. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  2730. }
  2731. }
  2732. }
  2733. /*
  2734. void CheckReturnToChargingConn()
  2735. {
  2736. if ((pSysConfig->TotalConnectorCount + pSysConfig->AcConnectorCount) > 1 &&
  2737. pSysInfo->PageIndex != _LCM_START_AUTHORIZING &&
  2738. pSysInfo->PageIndex != _LCM_START_AUTHORIZE_FAIL &&
  2739. pSysInfo->PageIndex != _LCM_WAIT_PLUGIN) {
  2740. bool isReturnTimeout = false;
  2741. for (uint8_t count = 0; count < pSysConfig->TotalConnectorCount; count++) {
  2742. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(count);
  2743. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  2744. if (count == pSysInfo->CurGunSelected) {
  2745. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2746. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2747. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2748. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2749. ) {
  2750. isReturnTimeout = false;
  2751. break;
  2752. }
  2753. } else if (count != pSysInfo->CurGunSelected) {
  2754. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2755. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2756. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2757. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  2758. ) {
  2759. isReturnTimeout = true;
  2760. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2761. }
  2762. }
  2763. }
  2764. // AC 槍
  2765. if (!isReturnTimeout && pSysConfig->AcConnectorCount > 0) {
  2766. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  2767. pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  2768. // 沒有選中 AC,且 AC 在充電中
  2769. if (pSysInfo->CurGunSelectedByAc == NO_DEFINE &&
  2770. (pAcChargingInfo->SystemStatus >= S_PREPARNING &&
  2771. pAcChargingInfo->SystemStatus <= S_COMPLETE)) {
  2772. // 當前 DC 充電槍在 idle 狀態
  2773. if (pDcChargingInfo->SystemStatus == S_IDLE ||
  2774. pDcChargingInfo->SystemStatus == S_RESERVATION) {
  2775. isReturnTimeout = true;
  2776. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2777. }
  2778. } else if (pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2779. ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK &&
  2780. pDcChargingInfo->SystemStatus <= S_COMPLETE) ||
  2781. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2782. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))) {
  2783. // 當前 DC 充電槍在 idle 狀態
  2784. if (pAcChargingInfo->SystemStatus == S_IDLE ||
  2785. pAcChargingInfo->SystemStatus == S_RESERVATION) {
  2786. isReturnTimeout = true;
  2787. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  2788. }
  2789. }
  2790. }
  2791. if (!isReturnTimeout) {
  2792. StopSystemTimeoutDet();
  2793. }
  2794. }
  2795. }
  2796. bool GetStartChargingByAlterMode(uint8_t _gun)
  2797. {
  2798. bool result = true;
  2799. if (pSysConfig->TotalConnectorCount == 2 &&
  2800. pSysInfo->IsAlternatvieConf == YES) {
  2801. for (uint8_t _select = 0; _select < pSysConfig->TotalConnectorCount; _select++) {
  2802. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_select);
  2803. if (_select != _gun) {
  2804. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  2805. pDcChargingInfo->SystemStatus != S_RESERVATION) {
  2806. result = false;
  2807. break;
  2808. }
  2809. }
  2810. }
  2811. }
  2812. return result;
  2813. }
  2814. */
  2815. void TheEndCharging(uint8_t gunIndex)
  2816. {
  2817. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2818. ftime(&endChargingTime[gunIndex]);
  2819. if (pDcChargingInfo->PresentChargedDuration != 0) {
  2820. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  2821. }
  2822. pDcChargingInfo->isRemoteStart = NO;
  2823. StopGunInfoTimeoutDet(gunIndex);
  2824. //StartGunInfoTimeoutDet(gunIndex, Timeout_EvseCompleteDet);
  2825. ChangeStartOrStopDateTime(NO, gunIndex);
  2826. DB_Insert_Record(gunIndex);
  2827. }
  2828. void UpdateErrorCodeToOcpp(uint8_t index)
  2829. {
  2830. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  2831. //log_info("%d = ConnectorAlarmCode = %s", index, pDcChargingInfo->ConnectorAlarmCode);
  2832. //log_info("%d = EvConnAlarmCode = %s", index, pDcChargingInfo->EvConnAlarmCode);
  2833. if (strcmp((char *)pDcChargingInfo->ConnectorAlarmCode, "") != EQUAL) {
  2834. //if (strlen((char *)pDcChargingInfo->ConnectorAlarmCode) == EQUAL) {
  2835. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  2836. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->ConnectorAlarmCode);
  2837. } else if (strcmp((char *)pDcChargingInfo->EvConnAlarmCode, "") != EQUAL) {
  2838. //} else if (strlen((char *)pDcChargingInfo->EvConnAlarmCode) == EQUAL) {
  2839. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  2840. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)pDcChargingInfo->EvConnAlarmCode);
  2841. }
  2842. //log_info("Gun %d = VendorErrorCode = %s", index, (char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  2843. }
  2844. void AdjustChargerCurrent()
  2845. {
  2846. pSysConfig->RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  2847. // 設定的電流~ 如超過可輸出的電流,則 bypass
  2848. if (pSysConfig->RatingCurrent < pSysConfig->MaxChargingCurrent ||
  2849. pSysConfig->RatingCurrent == 0) {
  2850. pSysConfig->MaxChargingCurrent = 0;
  2851. }
  2852. log_info("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  2853. ShmPsuData->SystemAvailablePower / 10,
  2854. ShmPsuData->SystemAvailableCurrent / 10
  2855. );
  2856. log_info("Config : ChargingPower = %d, ChargingCurrent = %d",
  2857. pSysConfig->MaxChargingPower,
  2858. pSysConfig->MaxChargingCurrent
  2859. );
  2860. }
  2861. void ResetDetAlarmStatus(uint8_t gun)
  2862. {
  2863. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gun);
  2864. if (pDcChargingInfo->Type == _Type_Chademo) {
  2865. if (pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  2866. pAlarmCode->AlarmEvents.bits.SystemChademoOutputOVP = NO;
  2867. }
  2868. } else if (pDcChargingInfo->Type == _Type_GB) {
  2869. if (pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP == YES) {
  2870. pAlarmCode->AlarmEvents.bits.SystemGbOutputOVP = NO;
  2871. }
  2872. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2873. if (pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  2874. pAlarmCode->AlarmEvents.bits.SystemCcsOutputOVP = NO;
  2875. }
  2876. }
  2877. }
  2878. static void autoStartCharging(uint8_t gunIndex)
  2879. {
  2880. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2881. if ( (pSysInfo->SystemPage >= _LCM_START_AUTHORIZING && pSysInfo->SystemPage <= _LCM_LINK_ERROR) ||
  2882. pDcChargingInfo->isEVCCIDVerify && strcmp( (char *)pSysConfig->UserId , "" ) != EQUAL) {
  2883. return;
  2884. }
  2885. if ((pDcChargingInfo->ConnectorPlugIn == YES) &&
  2886. (pDcChargingInfo->SystemStatus == S_IDLE )
  2887. ) {
  2888. if(!pSysConfig->AuthorisationMode) {
  2889. if (pSysConfig->isAuthrizeByEVCCID && !pDcChargingInfo->isEVCCIDVerify &&
  2890. pDcChargingInfo->Type == _Type_CCS_2) {
  2891. // LCM show linking pic. & timeout 2 min
  2892. if( pSysInfo->CurGunSelected == gunIndex ) {
  2893. systemPageRestoreInit();
  2894. pSysInfo->SystemPage = _LCM_PRECHARGE;
  2895. }
  2896. StartGunInfoTimeoutDet(gunIndex,Timeout_EVCCID_Link);
  2897. // Getting EVCCID
  2898. if( strcmp( (char *)pDcChargingInfo->EVCCID, "" ) != EQUAL ) {
  2899. log_info("Authorizing EVCCID");
  2900. StopSystemTimeoutDet();
  2901. strcpy((char *)pSysConfig->UserId, (char *)pDcChargingInfo->EVCCID);
  2902. ChangeGunSelectByIndex(gunIndex);
  2903. confirmSelGun(gunIndex);
  2904. pDcChargingInfo->isEVCCIDVerify = true;
  2905. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  2906. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  2907. setChargerMode(gunIndex, MODE_AUTHORIZING);
  2908. }
  2909. } else
  2910. return;
  2911. } else {
  2912. strcpy((char *)&pSysConfig->UserId, "AutoStartCharging");
  2913. ChangeGunSelectByIndex(gunIndex);
  2914. confirmSelGun(gunIndex);
  2915. pSysInfo->SystemPage = _LCM_START_AUTHORIZING;
  2916. setChargerMode(gunIndex, MODE_AUTHORIZING);
  2917. log_info("Get User(%d) ID:%s",gunIndex,pSysConfig->UserId);
  2918. }
  2919. }
  2920. }
  2921. static bool PrecheckIsPass(uint8_t gunIndex)
  2922. {
  2923. bool result = true;
  2924. // relay welding or driving 是反向
  2925. result = !ShmDcCommonData->GunRelayWeldingOccur[gunIndex];
  2926. return result;
  2927. }
  2928. static void ReviewCriticalAlarm(void)
  2929. {
  2930. if (
  2931. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip == YES ||
  2932. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip == YES ||
  2933. pAlarmCode->AlarmEvents.bits.DoorOpen == YES ||
  2934. pSysWarning->ExtraErrProcess != _EXTRA_ERR_PROCESS_NONE ||
  2935. pAlarmCode->AlarmEvents.bits.PsuFailureAlarm == YES ||
  2936. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == YES ||
  2937. //Power cabinet alarm status
  2938. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop == YES ||
  2939. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen == YES ||
  2940. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP == YES ||
  2941. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP == YES ||
  2942. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP == YES ||
  2943. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP == YES ||
  2944. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP == YES ||
  2945. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure == YES
  2946. ) {
  2947. pSysWarning->Level = WARN_LV_ER;
  2948. } else {
  2949. pSysWarning->Level = WARN_LV_NL;
  2950. }
  2951. }
  2952. static void CheckRelayWeldingOrDrivingFault(uint8_t gunIndex)
  2953. {
  2954. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  2955. // relay welding fault then stop the charging process.
  2956. uint8_t faultCode = RELAY_STATUS_ERROR_NONE;
  2957. //static uint8_t drivingCount = 0;
  2958. if (gunIndex == 0) {
  2959. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  2960. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  2961. faultCode = RELAY_STATUS_ERROR_WELDING;
  2962. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  2963. ShmDcCommonData->CheckRelayStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  2964. faultCode = RELAY_STATUS_ERROR_DRIVING;
  2965. }
  2966. } else if (gunIndex == 1) {
  2967. if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  2968. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  2969. faultCode = RELAY_STATUS_ERROR_WELDING;
  2970. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  2971. ShmDcCommonData->CheckRelayStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  2972. faultCode = RELAY_STATUS_ERROR_DRIVING;
  2973. }
  2974. }
  2975. if (pSysConfig->TotalConnectorCount >= 2 &&
  2976. !pSysInfo->IsAlternatvieConf) {
  2977. // 橋接
  2978. if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  2979. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  2980. faultCode = RELAY_STATUS_ERROR_WELDING;
  2981. } else if (ShmDcCommonData->CheckRelayStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  2982. ShmDcCommonData->CheckRelayStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  2983. faultCode = RELAY_STATUS_ERROR_DRIVING;
  2984. }
  2985. }
  2986. if (faultCode == RELAY_STATUS_ERROR_WELDING) {
  2987. // welding
  2988. if (pDcChargingInfo->Type == _Type_Chademo) {
  2989. RecordAlarmCode(gunIndex, "011011");
  2990. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayWeldingFault = YES;
  2991. } else if (pDcChargingInfo->Type == _Type_GB) {
  2992. RecordAlarmCode(gunIndex, "011015");
  2993. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayWeldingFault = YES;
  2994. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  2995. RecordAlarmCode(gunIndex, "011013");
  2996. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayWeldingFault = YES;
  2997. }
  2998. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = YES;
  2999. //EmcOccureByString("");
  3000. } else if (faultCode == RELAY_STATUS_ERROR_DRIVING) {
  3001. //if (drivingCount++ != 2) {
  3002. // return;
  3003. //}
  3004. // driving
  3005. if (pDcChargingInfo->Type == _Type_Chademo) {
  3006. RecordAlarmCode(gunIndex, "011012");
  3007. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaRelayDrivingFault = YES;
  3008. } else if (pDcChargingInfo->Type == _Type_GB) {
  3009. RecordAlarmCode(gunIndex, "011016");
  3010. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTRelayDrivingFault = YES;
  3011. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3012. RecordAlarmCode(gunIndex, "011014");
  3013. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSRelayDrivingFault = YES;
  3014. }
  3015. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = YES;
  3016. //EmcOccureByString("");
  3017. } else {
  3018. ShmDcCommonData->GunRelayWeldingOccur[gunIndex] = NO;
  3019. ShmDcCommonData->GunRelayDrivingOccur[gunIndex] = NO;
  3020. //drivingCount = 0;
  3021. }
  3022. }
  3023. static void isChargingAverageState(void)
  3024. {
  3025. if (pSysInfo->MainChargingMode != _MAIN_CHARGING_MODE_AVER) {
  3026. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3027. return;
  3028. }
  3029. // 均充 -> 最大充
  3030. if (pSysInfo->BridgeRelayStatus == NO) {
  3031. if (pSysInfo->ReAssignedFlag == _REASSIGNED_NONE) {
  3032. log_info("=============Smart Charging============= Step 11 ");
  3033. pSysInfo->ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  3034. }
  3035. } else if (pSysInfo->ReAssignedFlag != _REASSIGNED_COMP &&
  3036. pSysInfo->ReAssignedFlag != _REASSIGNED_WAITING) {
  3037. log_info("=============Smart Charging============= Step 14 ");
  3038. pSysInfo->ReAssignedFlag = _REASSIGNED_WAITING;
  3039. } else if (pSysInfo->ReAssignedFlag == _REASSIGNED_COMP) {
  3040. pSysInfo->MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  3041. pSysInfo->ReAssignedFlag = _REASSIGNED_NONE;
  3042. }
  3043. }
  3044. bool Taskconutstring(char *src, char *taskname)
  3045. {
  3046. bool result = false;
  3047. if (src == NULL || strlen(src) == 0)
  3048. return result;
  3049. if (strstr(src, taskname) != NULL &&
  3050. strstr(src, "grep") == NULL &&
  3051. strstr(src, "[") == NULL)
  3052. {
  3053. result = true;
  3054. }
  3055. return result;
  3056. }
  3057. int GetProcessCount(char *procName)
  3058. {
  3059. int result = 0;
  3060. FILE *fp;
  3061. char cmd[256];
  3062. char buf[256];
  3063. sprintf(cmd, "ps -ef |grep %s", procName);
  3064. fp = popen(cmd, "r");
  3065. if(fp != NULL)
  3066. {
  3067. while(fgets(buf, sizeof(buf), fp) != NULL)
  3068. {
  3069. if (Taskconutstring(buf, procName))
  3070. result++;
  3071. }
  3072. }
  3073. pclose(fp);
  3074. return result;
  3075. }
  3076. void CheckTaskAlive()
  3077. {
  3078. pid_t Pid = fork();
  3079. if ( Pid == 0 ) {
  3080. //log_info("CheckTaskAlive fork Child's PID is %d", getpid());
  3081. while(1) {
  3082. sleep(3);
  3083. unsigned char count = GetProcessCount("Module_ChkSysTask");
  3084. if( count != 1 ) {
  3085. system("pkill Module_ChkSysTask");
  3086. sleep(10);
  3087. log_info("Create Check System Task");
  3088. system("/root/Module_ChkSysTask &");
  3089. }
  3090. }
  3091. return;
  3092. }
  3093. log_info("Create Check Systam fork:%d",Pid);
  3094. return;
  3095. }
  3096. /**
  3097. * [checkPileEndGfdResult 確認充電樁GFD狀態]
  3098. * @Author Jerry
  3099. * @DateTime 2021-07-01
  3100. * @param gunIndex [當前的槍]
  3101. * @param gunType [槍的類型]
  3102. * @param sysStatus [槍的系統狀態]
  3103. */
  3104. static void checkPileEndGfdResult(uint8_t gunIndex, uint8_t gunType, uint8_t sysStatus)
  3105. {
  3106. switch (gunType) {
  3107. case _Type_Chademo:
  3108. // 檢查樁端的 GFD 結果
  3109. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3110. isPrechargeStatus_chademo(gunIndex) > 6 &&
  3111. isPrechargeStatus_chademo(gunIndex) < 8
  3112. ) {
  3113. // 當前操作的槍號,進入 Charging
  3114. setChargerMode(gunIndex, MODE_CHARGING);
  3115. }
  3116. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3117. // GFD 錯誤停止
  3118. RecordAlarmCode(gunIndex, "012234");
  3119. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = YES;
  3120. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3121. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3122. // GFD 警告
  3123. //RecordAlarmCode(gunIndex, "012296");
  3124. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3125. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = YES;
  3126. }
  3127. else {
  3128. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdTrip = NO;
  3129. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.ChaGfdWarning = NO;
  3130. }
  3131. break;
  3132. case _Type_GB:
  3133. // 檢查樁端的 GFD 結果
  3134. //if (isPrechargeStatus_gb(gunIndex) > 5 && isPrechargeStatus_gb(gunIndex) < 9) {
  3135. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3136. isPrechargeStatus_gb(gunIndex) > 6) {
  3137. setChargerMode(gunIndex, MODE_CHARGING);
  3138. }
  3139. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3140. // GFD 錯誤停止
  3141. RecordAlarmCode(gunIndex, "012236");
  3142. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = YES;
  3143. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3144. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3145. // GFD 警告
  3146. //RecordAlarmCode(gunIndex, "012298");
  3147. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3148. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = YES;
  3149. }
  3150. else {
  3151. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdTrip = NO;
  3152. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.GBTGfdWarning = NO;
  3153. }
  3154. break;
  3155. case _Type_CCS_2:
  3156. // 檢查樁端的 GFD 結果
  3157. if (sysStatus == S_PREPARING_FOR_EVSE &&
  3158. (pDcChargingInfo->GroundFaultStatus == GFD_PASS ||
  3159. pDcChargingInfo->GroundFaultStatus == GFD_WARNING)
  3160. ) {
  3161. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP0);
  3162. }
  3163. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3164. // GFD 錯誤停止
  3165. RecordAlarmCode(gunIndex, "012235");
  3166. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3167. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3168. } else if (pDcChargingInfo->GroundFaultStatus == GFD_WARNING) {
  3169. // GFD 警告
  3170. //RecordAlarmCode(gunIndex, "012297");
  3171. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3172. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = YES;
  3173. }
  3174. else {
  3175. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = NO;
  3176. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdWarning = NO;
  3177. }
  3178. break;
  3179. }
  3180. }
  3181. static void checkEvBoardReqStop(uint8_t sysStatus, uint8_t gunIndex)
  3182. {
  3183. uint8_t evBoardStopState = 0;
  3184. if ((evBoardStopState = isEvBoardStopChargeFlag(gunIndex)) > 0) {
  3185. // 板端要求停止 (錯誤)
  3186. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3187. sysStatus != S_CCS_PRECHARGE_ST1) {
  3188. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3189. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3190. }
  3191. }
  3192. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  3193. log_info("EV Board Stop Charging with some error");
  3194. ChargingAlarmProcess(gunIndex);
  3195. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  3196. log_info("Power Cabient Stop Charging");
  3197. ChargingTerminalProcess(gunIndex);
  3198. }
  3199. } else if (isEvBoardNormalStopChargeFlag(gunIndex) == YES) {
  3200. // 板端要求停止 (正常)
  3201. if (sysStatus != S_CCS_PRECHARGE_ST0 &&
  3202. sysStatus != S_CCS_PRECHARGE_ST1) {
  3203. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3204. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "EVDisconnected");
  3205. }
  3206. }
  3207. log_info("EV Board Normal Stop Charging");
  3208. ChargingTerminalProcess(gunIndex);
  3209. } else if (OcppRemoteStop(gunIndex) == YES ||
  3210. WifiScheduleStop(gunIndex)) {
  3211. // 後臺要求停止
  3212. log_info("OCPP Stop Charging");
  3213. ChargingTerminalProcess(gunIndex);
  3214. }
  3215. }
  3216. static void checkOCPPReqStop(uint8_t gunIndex)
  3217. {
  3218. if (OcppRemoteStop(gunIndex) == YES ||
  3219. WifiScheduleStop(gunIndex) ||
  3220. CheckBackendChargingTimeout(gunIndex) ||
  3221. CheckBackendChargingEnergy(gunIndex) ||
  3222. strcmp((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  3223. // 後臺要求停止
  3224. ChargingTerminalProcess(gunIndex);
  3225. }
  3226. }
  3227. void ResetIdleData(uint8_t gunIndex)
  3228. {
  3229. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3230. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3231. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3232. pDcChargingInfo->PresentChargedDuration = 0;
  3233. pDcChargingInfo->RemainChargingDuration = 0;
  3234. pDcChargingInfo->PresentChargingVoltage = 0;//DS60-120 add
  3235. pDcChargingInfo->PresentChargingCurrent = 0;//DS60-120 add
  3236. strcpy((char *)pDcChargingInfo->StartDateTime, "");
  3237. strcpy((char *)pDcChargingInfo->StopDateTime, "");
  3238. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3239. strcpy((char *)pSysConfig->UserId, "");
  3240. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "");
  3241. ClearDetectPluginFlag();
  3242. //Jerry add
  3243. memset(&ShmSelectGunInfo->PricesInfo[gunIndex], 0, sizeof(PricesInfo));
  3244. ShmSelectGunInfo->PricesInfo[gunIndex].Balance = FAIL_BALANCE_PRICES;
  3245. destroySelGun(gunIndex);
  3246. ResetDetAlarmStatus(gunIndex); //recovery OVP status code
  3247. if (ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] == YES) {
  3248. ShmSelectGunInfo->AuthorStateFromCabinet[gunIndex] = NO;
  3249. }
  3250. //strcpy((char *)ShmOCPP16Data->StatusNotification[gunIndex].VendorErrorCode, "");
  3251. ReleaseAlarmCode(gunIndex);
  3252. pSysInfo->SystemPage = _LCM_VIEW;
  3253. }
  3254. void CheckErrorCode(uint8_t gunIndex)
  3255. {
  3256. struct ChargingInfoData *pSelectedDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  3257. if (gunIndex == pSysInfo->CurGunSelected) {
  3258. //pSysInfo->SystemPage = _LCM_ERROR;
  3259. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3260. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton)
  3261. pSysInfo->SystemPage = _LCM_EMERGENCY;
  3262. } else if (pSelectedDcChargingInfo->SystemStatus != S_IDLE &&
  3263. pSelectedDcChargingInfo->SystemStatus != S_RESERVATION &&
  3264. pSelectedDcChargingInfo->SystemStatus != S_MAINTAIN &&
  3265. pSelectedDcChargingInfo->SystemStatus != S_FAULT) {
  3266. if (pSelectedDcChargingInfo->SystemStatus == S_CHARGING) {
  3267. pSysInfo->SystemPage = _LCM_SUMMARY;
  3268. } else {
  3269. systemPageRestoreInit();
  3270. }
  3271. }
  3272. ClearDetectPluginFlag();
  3273. if (pDcChargingInfo->SystemStatus != S_FAULT)
  3274. {
  3275. UpdateErrorCodeToOcpp(gunIndex);
  3276. setChargerMode(gunIndex, MODE_FAULT);
  3277. }
  3278. }
  3279. void AuthorizeStopCharging(uint8_t gunIndex)
  3280. {
  3281. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3282. if ( pSysInfo->SystemPage == _LCM_STOP_APP ) {
  3283. checkOCPPReqStop(gunIndex);
  3284. }
  3285. else if (pSysInfo->SystemPage == _LCM_STOP_RFID ){
  3286. if (!RfidStopCharging() && strlen((char *)pSysConfig->UserId) > 0) {
  3287. // Different Card
  3288. log_info("RFID Authorize fail.");
  3289. strcpy((char *)pSysConfig->UserId, "");
  3290. StopSystemTimeoutDet();
  3291. pSysInfo->SystemPage = _LCM_STOP_RFID_FAIL;
  3292. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  3293. }
  3294. }
  3295. }
  3296. void showversion()
  3297. {
  3298. uint8_t index = 0;
  3299. log_info("\t=========== Dispenser info =========== ");
  3300. log_info("\tSW Version = %s", fwVersion);
  3301. log_info("\tDebug Version = %s",DebugVersion);
  3302. log_info("\tModelName = %s", pSysConfig->ModelName);
  3303. log_info("\tRelay Board FW Rev = %s", pSysInfo->RelayModuleFwRev);
  3304. log_info("\tFan Board FW Rev = %s", pSysInfo->FanModuleFwRev);
  3305. log_info("\tPrimary FW Rev = %s", pSysInfo->CsuPrimFwRev);
  3306. log_info("\tLCM HW Rev = %s", pSysInfo->LcmHwRev);
  3307. log_info("\tLED FW Rev = %s", pSysInfo->LedModuleFwRev);
  3308. for (index = 0; index < pSysConfig->TotalConnectorCount; index++) {
  3309. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index);
  3310. if (pDcChargingInfo->Type == _Type_Chademo) {
  3311. log_info("\tCHAdeMO[%d] FW Rev = %s",
  3312. pDcChargingInfo->type_index,
  3313. ShmCHAdeMOData->evse[pDcChargingInfo->type_index].version);
  3314. } else if (pDcChargingInfo->Type == _Type_GB) {
  3315. log_info("\tGBT[%d] FW Rev = %s",
  3316. pDcChargingInfo->type_index,
  3317. ShmGBTData->evse[pDcChargingInfo->type_index].version);
  3318. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3319. log_info("\tCCS[%d] FW Rev = %s",
  3320. pDcChargingInfo->type_index,
  3321. ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].version);
  3322. }
  3323. }
  3324. log_info("\t==================================== ");
  3325. }
  3326. int main(void)
  3327. {
  3328. bool isModelNameMatch = true;
  3329. uint8_t _ocppProfileChkFlag;
  3330. uint8_t gunIndex = 0;
  3331. if (CreateAllCsuShareMemory() == FAIL) {
  3332. log_error("create share memory error");
  3333. return FAIL;
  3334. }
  3335. ClearAllShmMemParameter();
  3336. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  3337. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  3338. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  3339. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  3340. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  3341. pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  3342. ShmPsuData = (struct PsuData *)GetShmPsuData();
  3343. ShmCHAdeMOData = (struct CHAdeMOData *)GetShmCHAdeMOData();
  3344. ShmGBTData = (struct GBTData *)GetShmGBTData();
  3345. ShmCcsData = (struct CcsData *)GetShmCcsData();
  3346. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  3347. ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();
  3348. ShmRelayModuleData = (struct RelayModuleData *)GetShmRelayModuleData();
  3349. ShmLedModuleData = (struct LedModuleData *)GetShmLedModuleData();
  3350. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  3351. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  3352. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  3353. ShmSysConfigAndInfo = (struct SysConfigAndInfo *)GetShmSysConfigAndInfo();
  3354. ShmStatusCodeData = (struct StatusCodeData *)GetShmStatusCodeData();
  3355. log_info(" ********************************************************");
  3356. log_info(" ****************** FileSystem Boot up *****************");
  3357. log_info(" ********************************************************");
  3358. log_info(" ****************** Project:DD360UCar *****************");
  3359. log_info(" ********************************************************");
  3360. if (!InitialSystemDefaultConfig()) {
  3361. log_error("InitialSystemDefaultConfig NG ");
  3362. //StopProcessingLoop();
  3363. sleep(5);
  3364. system("reboot -f");
  3365. }
  3366. CheckGunTypeFromHw();
  3367. CheckIsAlternatvieByModelName();
  3368. InitialShareMemoryInfo();
  3369. GetFirmwareVersion();
  3370. if (!CheckConnectorTypeStatus()) {
  3371. isModelNameMatch = false;
  3372. }
  3373. Initialization();
  3374. SpawnTask();
  3375. log_info("Spawned all Task");
  3376. if (!isModelNameMatch) {
  3377. pAlarmCode->AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  3378. //ChangeLcmByIndex(_LCM_ERROR);
  3379. ChangeLcmByIndex(_LCM_MAINTAIN);
  3380. // Module Name 與硬體對應不正確
  3381. log_error("Module Name & HW info none match. ");
  3382. sleep(3);
  3383. KillAllTask();
  3384. StopProcessingLoop();
  3385. }
  3386. CreateTimeoutFork();
  3387. log_info("Start self test... ");
  3388. SelfTestRun();
  3389. StopSystemTimeoutDet();
  3390. log_info("Self test finished : SelfTestSeq = %d, Work_Step = %d ",
  3391. pSysInfo->SelfTestSeq,
  3392. ShmPsuData->Work_Step);
  3393. if (pSysInfo->SelfTestSeq == _STEST_FAIL ||
  3394. ShmPsuData->Work_Step == _NO_WORKING ||
  3395. pInfoCode->InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES //DS60-120 add
  3396. ) {
  3397. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3398. setChargerMode(gunIndex, MODE_MAINTAIN);
  3399. }
  3400. ChangeLcmByIndex(_LCM_MAINTAIN);
  3401. } else {
  3402. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3403. setChargerMode(gunIndex, MODE_IDLE);
  3404. }
  3405. }
  3406. // Local DB
  3407. if (DB_Open() != PASS) {
  3408. log_info("DB_Open fail. ");
  3409. isDb_ready = false;
  3410. } else {
  3411. isDb_ready = true;
  3412. for (int _index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  3413. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  3414. pDcChargingInfo->IsAvailable = DB_Get_Operactive(_index);
  3415. }
  3416. DB_Reboot_Record();
  3417. }
  3418. log_info("===== Create DB End ===== ");
  3419. ChangeLcmByIndex(_LCM_VIEW);
  3420. sleep(1);
  3421. //***** 須新增的偵測 *****//
  3422. // 1. Thernal - 控制風扇轉速
  3423. // 2. ouput fuse - 控制風扇轉速
  3424. CreateRfidFork();
  3425. // Main loop
  3426. showversion();
  3427. CheckFwSlotStatusLog();
  3428. //AdjustChargerCurrent();
  3429. gettimeofday(&_cmdMainPriority_time, NULL);
  3430. GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  3431. CheckTaskAlive();
  3432. CreateWatchdog();
  3433. for (;;) {
  3434. CheckOcppStatus();
  3435. ChkPrimaryStatus();
  3436. if ((IsConnectorWholeIdle() ||
  3437. //(pSysInfo->PageIndex == _LCM_ERROR)) &&
  3438. (pSysInfo->PageIndex == _LCM_MAINTAIN)) &&
  3439. (pSysInfo->SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  3440. ) {
  3441. CheckFactoryConfigFunction();
  3442. //CheckFwUpdateFunction();
  3443. }
  3444. // OCPP 邏輯
  3445. OcppRemoteStartChk();
  3446. // 當 AC 沒有搭上時,清除一些錯誤碼
  3447. ClearAlarmCodeWhenAcOff();
  3448. /*
  3449. if (pGunIndexInfo->AcGunIndex > 0 && isDetectPlugin() && !GetIsCardScan()) {
  3450. // AUDI_LCM_CHANGE
  3451. pSysInfo->SystemPage = _LCM_WAIT_PLUGIN;
  3452. }*/
  3453. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000) {
  3454. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3455. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3456. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3457. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3458. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3459. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3460. if (pDcChargingInfo->SystemStatus == S_CHARGING &&
  3461. _ocppProfileChkFlag == 12) {
  3462. ChargingProfileFlat(gunIndex);
  3463. _ocppProfileChkFlag = 0;
  3464. } else if (pDcChargingInfo->SystemStatus != S_CHARGING) {
  3465. ChargingProfileFlat(gunIndex);
  3466. _ocppProfileChkFlag = 0;
  3467. } else {
  3468. _ocppProfileChkFlag++;
  3469. }
  3470. }
  3471. checkGunOTPState(gunIndex); //check gun OTP
  3472. }
  3473. gettimeofday(&_cmdMainPriority_time, NULL);
  3474. }
  3475. gEvBoardErr.GunErrMessage = 0; //清除系統執行中的錯誤訊息
  3476. gChillerTempErr.TempErrMsg = 0;//清除系統執行中的錯誤訊息
  3477. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3478. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3479. // 重新收集各槍的錯誤狀態
  3480. collectError(gunIndex);
  3481. checkEvBoardAlarmState(pDcChargingInfo->Type);
  3482. }
  3483. checkChillerAlarmState();
  3484. // 確認當前錯誤 Level = 2 ?
  3485. ReviewCriticalAlarm();
  3486. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  3487. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  3488. CheckGpioInStatus();
  3489. CheckErrorOccurStatus(gunIndex);
  3490. // 確認 Relay Welding or Driving Fault
  3491. CheckRelayWeldingOrDrivingFault(gunIndex);
  3492. ChkOcppStatus(gunIndex);
  3493. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE &&
  3494. pDcChargingInfo->SystemStatus <= S_CHARGING) ||
  3495. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  3496. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  3497. CheckSmartChargeProfile(gunIndex);
  3498. }
  3499. switch (pDcChargingInfo->SystemStatus) {
  3500. case S_IDLE:
  3501. if (isModeChange(gunIndex)) {
  3502. log_info("============================= S_IDLE(%x) ============================= ", gunIndex);
  3503. ResetIdleData(gunIndex);
  3504. }
  3505. isChargingAverageState();
  3506. goto CheckStatus;
  3507. break;
  3508. case S_RESERVATION:
  3509. if (isModeChange(gunIndex)) {
  3510. log_info("============================= S_RESERVATION(%x) ============================= ", gunIndex);
  3511. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  3512. }
  3513. if (!isReservationExpired(gunIndex)) {
  3514. pDcChargingInfo->SystemStatus = S_IDLE;
  3515. }
  3516. goto CheckStatus;
  3517. break;
  3518. case S_FAULT:
  3519. if (isModeChange(gunIndex)) {
  3520. if(pDcChargingInfo->SystemStatus == S_FAULT) {
  3521. log_info("============================= S_FAULT(%x) ============================= ", gunIndex);
  3522. //pSysInfo->SystemPage = _LCM_ERROR;
  3523. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3524. }
  3525. }
  3526. CheckStatus:
  3527. if (pDcChargingInfo->IsAvailable == NO) {
  3528. setChargerMode(gunIndex, MODE_MAINTAIN);
  3529. break;
  3530. }
  3531. // Refresh Error Status
  3532. if (pSysWarning->Level == WARN_LV_ER) {
  3533. CheckErrorCode(gunIndex);
  3534. break;;
  3535. }
  3536. if (PrecheckIsPass(gunIndex) == false) {
  3537. log_error("Relay welding");
  3538. setChargerMode(gunIndex, MODE_FAULT);
  3539. if (gunIndex == pSysInfo->CurGunSelected) {
  3540. //pSysInfo->SystemPage = _LCM_ERROR;
  3541. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3542. }
  3543. break;
  3544. }
  3545. if (pDcChargingInfo->SystemStatus == S_FAULT) {
  3546. systemPageRestoreInit();
  3547. setChargerMode(gunIndex, MODE_IDLE);
  3548. }
  3549. break;
  3550. case S_AUTHORIZING:
  3551. if (isModeChange(gunIndex)) {
  3552. log_info("============================= S_AUTHORIZING(%x) ============================= ", gunIndex);
  3553. }
  3554. // 讀卡邏輯
  3555. ScannerCardProcess();
  3556. // 隨插即充
  3557. autoStartCharging(gunIndex);
  3558. if (isDetectPlugin() && pSysInfo->SystemPage != _LCM_LINK_ERROR) {
  3559. // 卡號驗證成功後,等待充電槍插入充電車
  3560. if (pDcChargingInfo->RemoteStartFlag == YES) {
  3561. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3562. pDcChargingInfo->IsAvailable) {
  3563. log_info("-------- Remote Start(%d) --------", gunIndex);
  3564. pDcChargingInfo->RemoteStartFlag = NO;
  3565. pDcChargingInfo->isRemoteStart = YES; //DS60-120
  3566. ChangeGunSelectByIndex(gunIndex);
  3567. //AddPlugInTimes(gunIndex);
  3568. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  3569. strcpy((char *)pDcChargingInfo->StartUserId, "");
  3570. ClearDetectPluginFlag();
  3571. continue;
  3572. }
  3573. } else if (pSysInfo->OrderCharging == NO_DEFINE) {
  3574. if (pDcChargingInfo->ConnectorPlugIn == YES &&
  3575. pDcChargingInfo->IsAvailable &&
  3576. pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  3577. //waitSelectGunPlugIt(gunIndex)
  3578. (waitRightGunPlugIt(gunIndex) == PASS ||
  3579. waitLeftGunPlugIt(gunIndex) == PASS)
  3580. ) {
  3581. log_info("-------- RFID/Auto Start(%d) --------", gunIndex);
  3582. ChangeGunSelectByIndex(gunIndex);
  3583. //AddPlugInTimes(gunIndex);
  3584. pDcChargingInfo->isRemoteStart = NO;
  3585. strcpy((char *)pDcChargingInfo->StartUserId, (char *)pSysConfig->UserId);
  3586. log_info("index = %d, CardNumber = %s ",
  3587. gunIndex,
  3588. pDcChargingInfo->StartUserId);
  3589. strcpy((char *)pSysConfig->UserId, "");
  3590. // 當前操作的槍號,進入 Preparing
  3591. setChargerMode(gunIndex, MODE_REASSIGN_CHECK);
  3592. ClearDetectPluginFlag();
  3593. continue;
  3594. }
  3595. }
  3596. if (!GetIsCardScan() && pSysInfo->SystemPage != _LCM_LINK_ERROR) {
  3597. pSysInfo->SystemPage = _LCM_WAIT_PLUGIN;
  3598. }
  3599. }
  3600. break;
  3601. case S_REASSIGN_CHECK: {
  3602. if (isModeChange(gunIndex)) {
  3603. log_info("============================= S_REASSIGN_CHECK(%x) ============================= ", gunIndex);
  3604. if (pSysInfo->OrderCharging != NO_DEFINE) {
  3605. pSysInfo->OrderCharging = NO_DEFINE;
  3606. }
  3607. StopSystemTimeoutDet();
  3608. }
  3609. setChargerMode(gunIndex, S_PREPARNING);
  3610. if (pSysInfo->CurGunSelected == gunIndex) {
  3611. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3612. }
  3613. }
  3614. break;
  3615. case S_PREPARNING:
  3616. if (isModeChange(gunIndex)) {
  3617. log_info("============================= S_PREPARNING(%x) ============================= ", gunIndex);
  3618. StopGunInfoTimeoutDet(gunIndex);
  3619. StartGunInfoTimeoutDet(gunIndex, Timeout_Preparing);
  3620. }
  3621. if (ShmPsuData->SystemPresentPsuQuantity > 0 &&
  3622. ShmPsuData->SystemAvailablePower > 10 &&
  3623. GetTimeoutValue(pDcChargingInfo->TimeoutTimer) >= 5000000) {
  3624. setChargerMode(gunIndex, MODE_PREPARE_FOR_EV);
  3625. }
  3626. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3627. if (pSysInfo->CurGunSelected == gunIndex) {
  3628. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3629. }
  3630. break;
  3631. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  3632. if (isModeChange(gunIndex)) {
  3633. log_info("============================= S_PREPARING_FOR_EV(%x) ============================= ", gunIndex);
  3634. log_info("Waitting for Gun locked.");
  3635. StopGunInfoTimeoutDet(gunIndex);
  3636. StartGunInfoTimeoutDet(gunIndex, Timeout_EvChargingDet);
  3637. }
  3638. if (pDcChargingInfo->Type == _Type_Chademo) {
  3639. if (isEvGunLocked_chademo(gunIndex) == YES) {
  3640. log_info("Gun Locked");
  3641. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  3642. }
  3643. } else if (pDcChargingInfo->Type == _Type_GB) {
  3644. if (isEvGunLocked_gb(gunIndex) == YES) {
  3645. log_info("Gun Locked");
  3646. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  3647. }
  3648. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3649. if (isEvGunLocked_ccs(gunIndex) == YES) {
  3650. log_info("Gun Locked");
  3651. setChargerMode(gunIndex, MODE_PREPARE_FOR_EVSE);
  3652. }
  3653. }
  3654. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3655. if (pSysInfo->CurGunSelected == gunIndex) {
  3656. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3657. }
  3658. break;
  3659. case S_PREPARING_FOR_EVSE: // Ground fault test, 等待 Relay Board 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  3660. if (isModeChange(gunIndex)) {
  3661. log_info("============================= S_PREPARING_FOR_EVSE(%x) ============================= ", gunIndex);
  3662. StopGunInfoTimeoutDet(gunIndex);
  3663. StartGunInfoTimeoutDet(gunIndex, Timeout_EvseChargingDet);
  3664. }
  3665. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  3666. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3667. if (pSysInfo->CurGunSelected == gunIndex) {
  3668. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3669. }
  3670. break;
  3671. case S_CCS_PRECHARGE_ST0:
  3672. if (isModeChange(gunIndex)) {
  3673. log_info("============================= CCS Precharge Processing 1(%x) ============================= ", gunIndex);
  3674. StopGunInfoTimeoutDet(gunIndex);
  3675. StartGunInfoTimeoutDet(gunIndex, Timeout_ForCcsPrechargeDet);
  3676. }
  3677. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3678. // GFD 錯誤停止
  3679. RecordAlarmCode(gunIndex, "012235");
  3680. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3681. }
  3682. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3683. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  3684. // 切換 D+ Relay to Precharge Relay
  3685. if (isPrechargeStatus_ccs(gunIndex) == 39 ||
  3686. isPrechargeStatus_ccs(gunIndex) == 40) {
  3687. if ((pDcChargingInfo->RelayKPK2Status == YES || pDcChargingInfo->PantographFlag == YES) &&
  3688. pDcChargingInfo->PrechargeStatus != PRECHARGE_READY)
  3689. //if (pDcChargingInfo->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  3690. {
  3691. //log_info("Send precharge ready 1..........%x, status = %d ",
  3692. // gunIndex,
  3693. // isPrechargeStatus_ccs(gunIndex));
  3694. if (isPrechargeStatus_ccs(gunIndex) == 39) {
  3695. log_info("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) ",
  3696. gunIndex,
  3697. isPrechargeStatus_ccs(gunIndex));
  3698. } else if (isPrechargeStatus_ccs(gunIndex) == 40) {
  3699. log_info("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) ",
  3700. gunIndex,
  3701. isPrechargeStatus_ccs(gunIndex));
  3702. }
  3703. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  3704. }
  3705. } else if (isPrechargeStatus_ccs(gunIndex) == 45 ||
  3706. isPrechargeStatus_ccs(gunIndex) == 46
  3707. ) {
  3708. setChargerMode(gunIndex, MODE_CCS_PRECHARGE_STEP1);
  3709. }
  3710. if (pSysInfo->CurGunSelected == gunIndex) {
  3711. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3712. }
  3713. break;
  3714. case S_CCS_PRECHARGE_ST1:
  3715. if (isModeChange(gunIndex)) {
  3716. log_info("============================= CCS Precharge Processing 2(%x) ============================= ", gunIndex);
  3717. }
  3718. if (pDcChargingInfo->GroundFaultStatus == GFD_FAIL) {
  3719. // GFD 錯誤停止
  3720. RecordAlarmCode(gunIndex, "012235");
  3721. ShmDcCommonData->ConnectErrList[gunIndex].GunBits.CCSGfdTrip = YES;
  3722. }
  3723. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3724. // 等待小板通知進入充電
  3725. // 切換 D+ Relay to Precharge Relay
  3726. if (pDcChargingInfo->RelayK1K2Status == YES || pDcChargingInfo->PantographFlag == YES) {
  3727. pDcChargingInfo->PrechargeStatus = PRECHARGE_READY;
  3728. setChargerMode(gunIndex, MODE_CHARGING);
  3729. }
  3730. if (pSysInfo->CurGunSelected == gunIndex) {
  3731. pSysInfo->SystemPage = _LCM_PRECHARGE;
  3732. }
  3733. break;
  3734. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  3735. if (isModeChange(gunIndex)) {
  3736. log_info("============================= S_CHARGING(%x) ============================= ", gunIndex);
  3737. StopGunInfoTimeoutDet(gunIndex);
  3738. ftime(&startChargingTime[gunIndex]);
  3739. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].ResponseIdTagInfo.Status, ""); //DS60-120 add
  3740. ChangeStartOrStopDateTime(YES, gunIndex);
  3741. pSysInfo->SystemPage = _LCM_DETAIL_VIEW;
  3742. StartGunInfoTimeoutDet(gunIndex,Timeout_DetailView);
  3743. }
  3744. if (ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf) {
  3745. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionConf = NO;
  3746. }
  3747. ftime(&endChargingTime[gunIndex]);
  3748. pDcChargingInfo->PresentChargedDuration = DiffTimeb(startChargingTime[gunIndex], endChargingTime[gunIndex]);
  3749. checkPileEndGfdResult(gunIndex, pDcChargingInfo->Type, pDcChargingInfo->SystemStatus);
  3750. checkEvBoardReqStop(pDcChargingInfo->SystemStatus, gunIndex);
  3751. // Check Stop Charging
  3752. AuthorizeStopCharging(gunIndex);
  3753. // LCM => Charging
  3754. /*
  3755. if (pSysInfo->SystemPage != _LCM_DETAIL_VIEW) {
  3756. pSysInfo->SystemPage = _LCM_VIEW;
  3757. }
  3758. */
  3759. break;
  3760. case S_ALARM:
  3761. if (isModeChange(gunIndex)) {
  3762. log_info("============================= S_ALARM(%x) ============================= ", gunIndex);
  3763. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3764. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  3765. }
  3766. UpdateErrorCodeToOcpp(gunIndex);
  3767. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  3768. OcppStopTransation(gunIndex);
  3769. }
  3770. TheEndCharging(gunIndex);
  3771. StopGunInfoTimeoutDet(gunIndex);
  3772. //pSysInfo->SystemPage = _LCM_ERROR;
  3773. }
  3774. pSysInfo->SystemPage = _LCM_MAINTAIN;
  3775. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton)
  3776. pSysInfo->SystemPage = _LCM_EMERGENCY;
  3777. if (pDcChargingInfo->ConnectorPlugIn == NO &&
  3778. GetTimeoutValue(pDcChargingInfo->TimeoutTimer) >= 10000000) {
  3779. setChargerMode(gunIndex, MODE_IDLE);
  3780. }
  3781. break;
  3782. case S_TERMINATING:
  3783. if (isModeChange(gunIndex)) {
  3784. log_info("============================= S_TERMINATING(%x) ============================= ", gunIndex);
  3785. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "") == EQUAL) {
  3786. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Local");
  3787. }
  3788. StopGunInfoTimeoutDet(gunIndex);
  3789. StartSystemTimeoutDet(Timeout_Terminating);
  3790. }
  3791. // For Precharging timeout
  3792. if (pDcChargingInfo->Type == _Type_Chademo) {
  3793. if (isEvStopCharging_chademo(gunIndex) == YES ||
  3794. isPrechargeStatus_chademo(gunIndex) <= 0) {
  3795. setChargerMode(gunIndex, MODE_COMPLETE);
  3796. }
  3797. } else if (pDcChargingInfo->Type == _Type_GB) {
  3798. if (isEvStopCharging_gb(gunIndex) == YES ||
  3799. isPrechargeStatus_gb(gunIndex) <= 0) {
  3800. setChargerMode(gunIndex, MODE_COMPLETE);
  3801. }
  3802. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  3803. if (isEvStopCharging_ccs(gunIndex) == YES &&
  3804. (isPrechargeStatus_ccs(gunIndex) >= 53 ||
  3805. isPrechargeStatus_ccs(gunIndex) == 0 ||
  3806. isPrechargeStatus_ccs(gunIndex) == 13 ||
  3807. isPrechargeStatus_ccs(gunIndex) == 14)
  3808. ) {
  3809. setChargerMode(gunIndex, MODE_COMPLETE);
  3810. }
  3811. }
  3812. //if (pSysInfo->SystemPage == _LCM_ERROR) {
  3813. if (pSysInfo->SystemPage == _LCM_MAINTAIN) {
  3814. break;
  3815. }
  3816. if (pSysInfo->CurGunSelected == gunIndex) {
  3817. pSysInfo->SystemPage = _LCM_STOPPING;
  3818. }
  3819. break;
  3820. case S_COMPLETE:
  3821. if (isModeChange(gunIndex)) {
  3822. log_info ("============================= S_COMPLETE(%x) ============================= ", gunIndex);
  3823. if (strcmp((char *)pDcChargingInfo->StartDateTime, "") != EQUAL) {
  3824. OcppStopTransation(gunIndex);
  3825. }
  3826. TheEndCharging(gunIndex);
  3827. StopSystemTimeoutDet();
  3828. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  3829. if (pDcChargingInfo->ConnectorPlugIn)
  3830. pSysInfo->SystemPage = _LCM_WAIT_PLUGOUT;
  3831. else
  3832. pSysInfo->SystemPage = _LCM_SUMMARY;
  3833. }
  3834. //if (pSysInfo->SystemPage == _LCM_ERROR) {
  3835. if (pSysInfo->SystemPage == _LCM_MAINTAIN) {
  3836. StopSystemTimeoutDet();
  3837. break;
  3838. }
  3839. if (pSysInfo->CurGunSelected == gunIndex) {
  3840. if (pDcChargingInfo->ConnectorPlugIn) {
  3841. if (pSysInfo->SystemPage != _LCM_SUMMARY)
  3842. pSysInfo->SystemPage = _LCM_WAIT_PLUGOUT;
  3843. } else {
  3844. if (pSysInfo->SystemPage == _LCM_WAIT_PLUGOUT) {
  3845. StopSystemTimeoutDet();
  3846. pSysInfo->SystemPage = _LCM_SUMMARY;
  3847. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  3848. }
  3849. }
  3850. }
  3851. break;
  3852. case S_MAINTAIN:
  3853. if (isModeChange(gunIndex)) {
  3854. log_info("============================= S_MAINTAIN(%x) ============================= ", gunIndex);
  3855. if (pSysInfo->FirmwareUpdate == YES)
  3856. continue;
  3857. if (pDcChargingInfo->IsAvailable == NO )
  3858. continue;
  3859. if (pSysWarning->Level != WARN_LV_ER) {
  3860. if (!DisplaySelfTestFailReason()) { //DS60-120 add
  3861. log_info("Soft reboot for retry self-tets. ");
  3862. sleep(3);
  3863. system("killall OcppBackend &");
  3864. KillAllTask();
  3865. system("/usr/bin/run_evse_restart.sh");
  3866. }
  3867. }
  3868. sleep(3);
  3869. if (pSysWarning->Level == WARN_LV_ER) { //DS60-120 add
  3870. KillTaskExceptPrimary();
  3871. } else {
  3872. KillTask();
  3873. }
  3874. if (pSysInfo->SelfTestSeq == _STEST_FAIL)
  3875. StopProcessingLoop();
  3876. }
  3877. break;
  3878. }//switch
  3879. }//for
  3880. //if (pSysInfo->SystemPage != _LCM_VIEW)
  3881. //ChangeLcmByIndex(pSysInfo->SystemPage);
  3882. TryFeedWatchdog();
  3883. usleep(WHILE_LOOP_TIME);
  3884. }
  3885. return FAIL;
  3886. }