main.c 172 KB

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