main.c 169 KB

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