main.c 210 KB

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