main.c 223 KB

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