main.c 193 KB

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