main.c 174 KB

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