main.c 168 KB

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