main.c 211 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850
  1. #include "define.h"
  2. #include "main.h"
  3. //==========================
  4. // System basic sample constant
  5. //==========================
  6. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  7. #define PASS 1
  8. #define FAIL -1
  9. #define YES 1
  10. #define NO 0
  11. #define ON 1
  12. #define OFF 0
  13. #define BUFFER_SIZE 128
  14. //==========================
  15. // Timer interval
  16. //==========================
  17. #define TMR_IDX_HANDSHAKING 0
  18. #define TMR_IDX_AUTH 1
  19. #define TMR_IDX_LOGPPRINTOUT 2
  20. #define TMR_IDX_PROFILE_PREPARE 3
  21. #define TMR_IDX_PWN_CHANGE 4
  22. #define TMR_IDX_CHECK_TASK 5
  23. #define TMR_IDX_RESET_4G_WIFI_POWER 6
  24. #define TMR_IDX_7 7
  25. #define TMR_IDX_8 8
  26. #define TMR_IDX_9 9
  27. #define TIMEOUT_SPEC_HANDSHAKING 180
  28. #define TIMEOUT_SPEC_AUTH 15
  29. #define TIMEOUT_SPEC_HANDSHAKING_LED 185
  30. #define TIMEOUT_SPEC_LOGPPRINTOUT 30
  31. #define TIMEOUT_SPEC_PROFILE_PREPARE 60
  32. #define TIMEOUT_SPEC_PWN_CHANGE 5
  33. #define TIMEOUT_SPEC_RESET_4G_WIFI_POWER 300
  34. #define SPEC_TEMPERATURE_WARN 71
  35. #define SPEC_TEMPERATURE_RECOVER 63
  36. #define MtdBlockSize 0x300000
  37. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  38. #define DB_FILE_OCPP_LOCAL "/Storage/OCPP/charger.db"
  39. //==========================
  40. // Declare method
  41. //==========================
  42. void trim(char *s);
  43. int mystrcmp(char *p1,char *p2);
  44. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  45. void split(char **arr, char *str, const char *del);
  46. int isReachableInternet();
  47. int isRouteFail();
  48. int InitRfidPort(void);
  49. int GetCardSerialNumber();
  50. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  51. void setRelay(unsigned char gun_index,unsigned char isOn);
  52. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  53. //==========================
  54. // Declare RFID module type
  55. //==========================
  56. #define MODULE_EWT 0
  57. int wtdFd = -1;
  58. int rfidFd = -1;
  59. char* rfidPortName = "/dev/ttyS2";
  60. RFID rfid;
  61. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  62. int isRfidReadable;
  63. //==========================
  64. // Declare share memory
  65. //==========================
  66. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  67. struct StatusCodeData *ShmStatusCodeData;
  68. struct PsuData *ShmPsuData;
  69. struct CHAdeMOData *ShmCHAdeMOData;
  70. struct CcsData *ShmCcsData;
  71. struct PrimaryMcuData *ShmPrimaryMcuData;
  72. struct FanModuleData *ShmFanModuleData;
  73. struct RelayModuleData *ShmRelayModuleData;
  74. struct OCPP16Data *ShmOCPP16Data;
  75. struct OCPP20Data *ShmOCPP20Data;
  76. struct OCPP16Data *ShmOCPP16DataPH;
  77. struct Charger *ShmCharger;
  78. struct timespec startTime[AC_QUANTITY][10];
  79. struct timespec startChargingTime[AC_QUANTITY];
  80. struct timespec endChargingTime[AC_QUANTITY];
  81. sqlite3 *localDb;
  82. sqlite3 *ocppDb;
  83. struct SysConfigData SysConfigOrg;
  84. ParsingRatedCur modelnameInfo={0};
  85. struct PreviousData
  86. {
  87. uint16_t targetCurrent;
  88. uint16_t current_limit;
  89. uint16_t primaryMcuCp_Pwn_Duty;
  90. }previousData[4];
  91. //=================================
  92. // Common routine
  93. //=================================
  94. void trim(char *s)
  95. {
  96. int i=0, j, k, l=0;
  97. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  98. i++;
  99. j = strlen(s)-1;
  100. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  101. j--;
  102. if(i==0 && j==strlen(s)-1) { }
  103. else if(i==0) s[j+1] = '\0';
  104. else {
  105. for(k=i; k<=j; k++) s[l++] = s[k];
  106. s[l] = '\0';
  107. }
  108. }
  109. int mystrcmp(char *p1,char *p2)
  110. {
  111. while(*p1==*p2)
  112. {
  113. if(*p1=='\0' || *p2=='\0')
  114. break;
  115. p1++;
  116. p2++;
  117. }
  118. if(*p1=='\0' && *p2=='\0')
  119. return(PASS);
  120. else
  121. return(FAIL);
  122. }
  123. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  124. {
  125. strncpy(dest, src + start, cnt);
  126. dest[cnt] = 0;
  127. }
  128. void split(char **arr, char *str, const char *del)
  129. {
  130. char *s = strtok(str, del);
  131. while(s != NULL)
  132. {
  133. *arr++ = s;
  134. s = strtok(NULL, del);
  135. }
  136. }
  137. int StoreLogMsg(const char *fmt, ...)
  138. {
  139. char Buf[4096+256];
  140. char buffer[4096];
  141. time_t CurrentTime;
  142. struct tm *tm;
  143. struct timeval tv;
  144. va_list args;
  145. va_start(args, fmt);
  146. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  147. va_end(args);
  148. memset(Buf,0,sizeof(Buf));
  149. CurrentTime = time(NULL);
  150. tm=localtime(&CurrentTime);
  151. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  152. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  153. {
  154. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  155. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  156. buffer,
  157. tm->tm_year+1900,tm->tm_mon+1,
  158. ShmSysConfigAndInfo->SysConfig.ModelName,
  159. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  160. }
  161. else
  162. {
  163. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  164. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  165. buffer,
  166. tm->tm_year+1900,tm->tm_mon+1);
  167. }
  168. #ifdef SystemLogMessage
  169. system(Buf);
  170. #endif
  171. #ifdef ConsloePrintLog
  172. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  173. #endif
  174. return rc;
  175. }
  176. void refreshStartTimer(struct timespec *timer)
  177. {
  178. clock_gettime(CLOCK_MONOTONIC, timer);
  179. }
  180. int getDiffSecNow(struct timespec timer)
  181. {
  182. struct timespec timerNow;
  183. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  184. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  185. }
  186. int getDiffSecBetween(struct timespec start, struct timespec end)
  187. {
  188. return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
  189. }
  190. long long DiffTimebWithNow(struct timeb ST)
  191. {
  192. //return milli-second
  193. struct timeb ET;
  194. long long StartTime,StopTime;
  195. ftime(&ET);
  196. StartTime=(long long)ST.time;
  197. StopTime=(long long)ET.time;
  198. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  199. }
  200. long long DiffTimeb(struct timeb ST, struct timeb ET)
  201. {
  202. //return milli-second
  203. long long StartTime,StopTime;
  204. StartTime=(long long)ST.time;
  205. StopTime=(long long)ET.time;
  206. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  207. }
  208. int DiffTimebWithNowSec(struct timeb ST)
  209. {
  210. //return milli-second
  211. struct timeb ET;
  212. unsigned int StartTime,StopTime;
  213. ftime(&ET);
  214. StartTime=(unsigned int)ST.time;
  215. StopTime=(unsigned int)ET.time;
  216. return (StopTime-StartTime);
  217. }
  218. int isOvertNow(uint8_t *start)
  219. {
  220. int result = YES;
  221. struct ParsingResult
  222. {
  223. int result;
  224. int scanedElement;
  225. int year;
  226. int month;
  227. int mday;
  228. int hour;
  229. int min;
  230. int sec;
  231. int tz_hour;
  232. int tz_min;
  233. float minSec;
  234. }parsingResult;
  235. struct tm tmStart;
  236. struct timeb tbStart;
  237. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  238. if(strstr((char*)start, ".") != NULL)
  239. {
  240. // Original data with mini second
  241. if(strstr((char*)start, "Z") != NULL)
  242. {
  243. // Original data with Z
  244. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  245. &parsingResult.year,
  246. &parsingResult.month,
  247. &parsingResult.mday,
  248. &parsingResult.hour,
  249. &parsingResult.min,
  250. &parsingResult.sec,
  251. &parsingResult.minSec);
  252. }
  253. else
  254. {
  255. // Original data without Z
  256. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  257. &parsingResult.year,
  258. &parsingResult.month,
  259. &parsingResult.mday,
  260. &parsingResult.hour,
  261. &parsingResult.min,
  262. &parsingResult.sec,
  263. &parsingResult.minSec,
  264. &parsingResult.tz_hour,
  265. &parsingResult.tz_min);
  266. }
  267. }
  268. else
  269. {
  270. // Original data without mini second
  271. if(strstr((char*)start, "Z") != NULL)
  272. {
  273. // Original data with Z
  274. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  275. &parsingResult.year,
  276. &parsingResult.month,
  277. &parsingResult.mday,
  278. &parsingResult.hour,
  279. &parsingResult.min,
  280. &parsingResult.sec);
  281. }
  282. else
  283. {
  284. // Original data without Z
  285. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  286. &parsingResult.year,
  287. &parsingResult.month,
  288. &parsingResult.mday,
  289. &parsingResult.hour,
  290. &parsingResult.min,
  291. &parsingResult.sec,
  292. &parsingResult.tz_hour,
  293. &parsingResult.tz_min);
  294. }
  295. }
  296. if(parsingResult.scanedElement >= 6)
  297. {
  298. tmStart.tm_year = parsingResult.year - 1900;
  299. tmStart.tm_mon = parsingResult.month - 1;
  300. tmStart.tm_mday = parsingResult.mday;
  301. tmStart.tm_hour = parsingResult.hour;
  302. tmStart.tm_min = parsingResult.min;
  303. tmStart.tm_sec = parsingResult.sec;
  304. tmStart.tm_gmtoff = 0;
  305. tbStart.time = mktime(&tmStart);
  306. tbStart.timezone = 0;
  307. tbStart.millitm = 0;
  308. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  309. if(DiffTimebWithNowSec(tbStart) <= 0)
  310. result = NO;
  311. else
  312. result = YES;
  313. }
  314. else
  315. {
  316. DEBUG_WARN("Start date parsing error.\n");
  317. }
  318. return result;
  319. }
  320. void getDateTimeString(char* result)
  321. {
  322. time_t CurrentTime;
  323. struct tm *tm;
  324. CurrentTime = time(NULL);
  325. tm=localtime(&CurrentTime);
  326. sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  327. }
  328. int getCurrentYear()
  329. {
  330. int result = 0;
  331. time_t CurrentTime;
  332. struct tm *tm;
  333. CurrentTime = time(NULL);
  334. tm=localtime(&CurrentTime);
  335. result = (tm->tm_year + 1900);
  336. return result;
  337. }
  338. unsigned long long getAvailableMemory()
  339. {
  340. long pages = sysconf(_SC_AVPHYS_PAGES);
  341. long page_size = sysconf(_SC_PAGE_SIZE);
  342. return pages * page_size;
  343. }
  344. unsigned int isKernelSupportNAT()
  345. {
  346. unsigned int result = NO;
  347. unsigned int version = 0;
  348. FILE *fp;
  349. char cmd[256];
  350. char buf[512];
  351. // Get IP address & net mask
  352. strcpy(cmd, "uname -v");
  353. fp = popen(cmd, "r");
  354. if(fp != NULL)
  355. {
  356. if(fgets(buf, sizeof(buf), fp) != NULL)
  357. {
  358. sscanf(buf, "#%d", &version);
  359. //DEBUG_INFO("Kernel version: %d\n", version);
  360. if(version >= 30)
  361. result = YES;
  362. }
  363. }
  364. pclose(fp);
  365. return result;
  366. }
  367. int getEth0MacAddress()
  368. {
  369. int result = PASS;
  370. FILE *fp;
  371. char cmd[256];
  372. char buf[512];
  373. char tmp[512];
  374. strcpy(cmd, "ifconfig eth0");
  375. fp = popen(cmd, "r");
  376. if(fp != NULL)
  377. {
  378. while(fgets(buf, sizeof(buf), fp) != NULL)
  379. {
  380. if(strstr(buf, "eth0") > 0)
  381. {
  382. result = PASS;
  383. }
  384. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  385. {
  386. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  387. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  388. }
  389. }
  390. }
  391. pclose(fp);
  392. return result;
  393. }
  394. int isUap0up(void)
  395. {
  396. int result = FAIL;
  397. FILE *fp;
  398. char cmd[256];
  399. char buf[512];
  400. strcpy(cmd, "ifconfig uap0");;
  401. fp = popen(cmd, "r");
  402. if(fp != NULL)
  403. {
  404. while(fgets(buf, sizeof(buf), fp) != NULL)
  405. {
  406. if(strstr(buf, "uap0") > 0)
  407. {
  408. result = PASS;
  409. }
  410. }
  411. }
  412. pclose(fp);
  413. return result;
  414. }
  415. //======================================================
  416. // OCPP routine
  417. //======================================================
  418. void ocpp_process_start()
  419. {
  420. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  421. system("/root/OcppBackend &");
  422. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  423. system("/root/OcppBackend20 &");
  424. }
  425. uint8_t ocpp_get_isRemoteStartWait()
  426. {
  427. uint8_t result = OFF;
  428. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  429. {
  430. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  431. }
  432. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  433. {
  434. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  435. }
  436. return result;
  437. }
  438. uint8_t ocpp_get_connection_status()
  439. {
  440. uint8_t result = OFF;
  441. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  442. {
  443. result = ShmOCPP16Data->OcppConnStatus;
  444. }
  445. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  446. {
  447. result = ShmOCPP20Data->OcppConnStatus;
  448. }
  449. return result;
  450. }
  451. uint16_t ocpp_get_connection_timeout()
  452. {
  453. uint16_t result = TIMEOUT_SPEC_HANDSHAKING;
  454. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  455. {
  456. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  457. {
  458. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  459. }
  460. }
  461. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  462. {
  463. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  464. {
  465. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  466. }
  467. }
  468. return result;
  469. }
  470. uint8_t ocpp_get_update_firmware_req()
  471. {
  472. uint8_t result = NO;
  473. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  474. {
  475. result = ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq || ShmOCPP16Data->MsMsg.bits.SignedUpdateFirmwareReq;
  476. }
  477. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  478. {
  479. result = ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq;
  480. }
  481. return result;
  482. }
  483. uint8_t ocpp_get_reset_req()
  484. {
  485. uint8_t result = NO;
  486. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  487. {
  488. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  489. }
  490. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  491. {
  492. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  493. {
  494. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  495. }
  496. else
  497. {
  498. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_CHARGING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_TERMINATING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_COMPLETE)) &&
  499. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus != SYS_MODE_CHARGING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus != SYS_MODE_TERMINATING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus != SYS_MODE_COMPLETE)):TRUE))
  500. {
  501. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  502. }
  503. }
  504. }
  505. return result;
  506. }
  507. void ocpp_boot_info_sync()
  508. {
  509. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  510. {
  511. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  512. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  513. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  514. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  515. switch(ShmSysConfigAndInfo->SysConfig.ModelName[3])
  516. {
  517. case 'M':
  518. case 'Z':
  519. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "MID");
  520. break;
  521. case 'P':
  522. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "PTB");
  523. break;
  524. case 'I':
  525. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "TIC");
  526. break;
  527. case 'U':
  528. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "UL");
  529. break;
  530. default:
  531. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "N/A");
  532. break;
  533. }
  534. }
  535. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  536. {
  537. memcpy((char*)ShmOCPP20Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  538. memcpy((char*)ShmOCPP20Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  539. sprintf((char*)ShmOCPP20Data->BootNotification.chargingStation.firmwareVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  540. }
  541. }
  542. void ocpp_set_remotestart(uint8_t gun_index, uint8_t status)
  543. {
  544. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  545. {
  546. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq != status)
  547. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = status;
  548. }
  549. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  550. {
  551. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq != status)
  552. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = status;
  553. }
  554. }
  555. void ocpp_set_remotestop(uint8_t gun_index, uint8_t status)
  556. {
  557. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  558. {
  559. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != status)
  560. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = status;
  561. }
  562. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  563. {
  564. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq != status)
  565. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = status;
  566. }
  567. }
  568. uint8_t ocpp_get_remotestart(uint8_t gun_index)
  569. {
  570. uint8_t result = OFF;
  571. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  572. {
  573. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  574. }
  575. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  576. {
  577. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  578. }
  579. return result;
  580. }
  581. void ocpp_copy_userid_from_remotestart(uint8_t gun_index)
  582. {
  583. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  584. {
  585. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  586. DEBUG_INFO("==========================================\n");
  587. DEBUG_INFO("Copy IdTag into UserId. \n");
  588. DEBUG_INFO("IdTag : [%s] ---> UserId : [%s] . \n", ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ShmSysConfigAndInfo->SysConfig.UserId);
  589. DEBUG_INFO("==========================================\n");
  590. }
  591. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  592. {
  593. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  594. DEBUG_INFO("==========================================\n");
  595. DEBUG_INFO("Copy idToken into UserId. \n");
  596. DEBUG_INFO("idToken : [%s] ---> UserId : [%s]. \n", ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ShmSysConfigAndInfo->SysConfig.UserId);
  597. DEBUG_INFO("==========================================\n");
  598. }
  599. }
  600. uint8_t ocpp_get_remotestop(uint8_t gun_index)
  601. {
  602. uint8_t result = OFF;
  603. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  604. {
  605. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq;
  606. }
  607. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  608. {
  609. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq;
  610. }
  611. return result;
  612. }
  613. void ocpp_set_auth_conf(uint8_t status)
  614. {
  615. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  616. {
  617. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf != status)
  618. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = status;
  619. }
  620. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  621. {
  622. if(ShmOCPP20Data->SpMsg.bits.AuthorizeConf != status)
  623. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = status;
  624. }
  625. }
  626. void ocpp_set_auth_req(uint8_t status, ...)
  627. {
  628. va_list args;
  629. if(status == ON)
  630. ocpp_set_auth_conf(OFF);
  631. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  632. {
  633. if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq != status)
  634. {
  635. if(status == ON)
  636. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00, ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  637. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = status;
  638. }
  639. }
  640. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  641. {
  642. if(ShmOCPP20Data->SpMsg.bits.AuthorizeReq != status)
  643. {
  644. if(status == ON)
  645. {
  646. memset(&ShmOCPP20Data->Authorize.Response_idTokenInfo, 0x00, sizeof(struct IdTokenInfoType));
  647. va_start(args, status);
  648. sprintf((char*)ShmOCPP20Data->Authorize.idToken.type, "%s", va_arg(args, char*));
  649. va_end(args);
  650. }
  651. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = status;
  652. }
  653. }
  654. }
  655. uint8_t ocpp_get_auth_req()
  656. {
  657. uint8_t result = OFF;
  658. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  659. {
  660. result = ShmOCPP16Data->SpMsg.bits.AuthorizeReq;
  661. }
  662. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  663. {
  664. result = ShmOCPP20Data->SpMsg.bits.AuthorizeReq;
  665. }
  666. return result;
  667. }
  668. uint8_t ocpp_get_auth_conf()
  669. {
  670. uint8_t result = OFF;
  671. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  672. {
  673. result = ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  674. }
  675. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  676. {
  677. result = ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  678. }
  679. return result;
  680. }
  681. uint8_t ocpp_compare_reserve_id_with_user(uint8_t gun_index)
  682. {
  683. uint8_t result = OFF;
  684. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  685. {
  686. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  687. result = ON;
  688. }
  689. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  690. {
  691. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  692. result = ON;
  693. }
  694. return result;
  695. }
  696. uint8_t ocpp_compare_reserve_id_with_remote_user(uint8_t gun_index)
  697. {
  698. uint8_t result = OFF;
  699. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  700. {
  701. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  702. {
  703. result = ON;
  704. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  705. }
  706. }
  707. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  708. {
  709. if(strcmp((char*)ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  710. {
  711. result = ON;
  712. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  713. }
  714. }
  715. return result;
  716. }
  717. uint8_t ocpp_get_auth_result(uint8_t gun_index)
  718. {
  719. uint8_t result = OFF;
  720. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  721. {
  722. case SYS_MODE_AUTHORIZING:
  723. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  724. {
  725. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1) && (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0)))
  726. {
  727. result = PASS;
  728. }
  729. else if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId != -1) && (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0)))
  730. {
  731. if(ocpp_compare_reserve_id_with_user(gun_index) || ocpp_compare_reserve_id_with_remote_user(gun_index))
  732. {
  733. result = PASS;
  734. }
  735. else
  736. {
  737. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag)==0))
  738. result = PASS;
  739. }
  740. }
  741. DEBUG_INFO("============ [SYS_MODE_AUTHORIZING] ==============\n");
  742. DEBUG_INFO("Authorize response status: [ %s ].\n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  743. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId != -1))
  744. {
  745. DEBUG_INFO("Gun-[%d] Reservation id: [ %d ].\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId);
  746. DEBUG_INFO("Authorize response ParentIdTag: [ %s ].\n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  747. DEBUG_INFO("Gun-[%d] Reserve ParentIdTag: [ %s ].\n", gun_index, ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag);
  748. }
  749. DEBUG_INFO("==================================================\n");
  750. }
  751. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  752. {
  753. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1) && ((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) && (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_MasterPassGroupId].variableAttribute[0].value) != 0))) ||
  754. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId != -1) && ((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) && (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].groupIdToken.idToken)==0))))
  755. result = PASS;
  756. }
  757. break;
  758. case SYS_MODE_CHARGING:
  759. case SYS_MODE_TERMINATING:
  760. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  761. {
  762. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  763. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0) &&
  764. (strlen((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag)>0))
  765. result = PASS;
  766. DEBUG_INFO("==== [SYS_MODE_CHARGING/SYS_MODE_TERMINATING] ====\n");
  767. DEBUG_INFO("Authorize response status: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  768. DEBUG_INFO("Authorize response ParentIdTag: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  769. DEBUG_INFO("Gun-[%d] StartTransaction response ParentIdTag: [ %s ]. \n", gun_index, ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag);
  770. DEBUG_INFO("==================================================\n");
  771. }
  772. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  773. {
  774. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
  775. ((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken) == 0) ||
  776. (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_MasterPassGroupId].variableAttribute[0].value) == 0)))
  777. result = PASS;
  778. }
  779. break;
  780. case SYS_MODE_RESERVATION:
  781. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  782. {
  783. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  784. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag)==0))
  785. result = PASS;
  786. DEBUG_INFO("============ [SYS_MODE_RESERVATION] ==============\n");
  787. DEBUG_INFO("Authorize response status: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  788. DEBUG_INFO("Authorize response ParentIdTag: [ %s ]. \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  789. DEBUG_INFO("Gun-[%d] Reserve ParentIdTag: [ %s ]. \n", gun_index, ShmOCPP16Data->ReserveNow[gun_index].ParentIdTag);
  790. DEBUG_INFO("==================================================\n");
  791. }
  792. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  793. {
  794. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
  795. (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].groupIdToken.idToken)==0))
  796. result = PASS;
  797. }
  798. break;
  799. default:
  800. break;
  801. }
  802. return result;
  803. }
  804. uint8_t ocpp_get_unlocker_req(uint8_t gun_index)
  805. {
  806. uint8_t result = OFF;
  807. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  808. {
  809. result = ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor;
  810. }
  811. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  812. {
  813. result = ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor;
  814. }
  815. return result;
  816. }
  817. void ocpp_set_unlocker_req(uint8_t gun_index, uint8_t status)
  818. {
  819. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  820. {
  821. if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor != status)
  822. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = status;
  823. }
  824. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  825. {
  826. if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor != status)
  827. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = status;
  828. }
  829. }
  830. void ocpp_set_starttransaction_req(uint8_t gun_index, uint8_t status)
  831. {
  832. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  833. {
  834. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq != status)
  835. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = status;
  836. }
  837. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  838. {
  839. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  840. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  841. }
  842. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  843. {
  844. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq != status)
  845. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = status;
  846. }
  847. }
  848. void ocpp_set_starttransaction_conf(uint8_t gun_index, uint8_t status)
  849. {
  850. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  851. {
  852. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf != status)
  853. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = status;
  854. }
  855. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  856. {
  857. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  858. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  859. }
  860. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  861. {
  862. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf != status)
  863. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = status;
  864. }
  865. }
  866. void ocpp_set_stoptransaction_req(uint8_t gun_index, uint8_t status)
  867. {
  868. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  869. {
  870. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq != status)
  871. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = status;
  872. }
  873. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  874. {
  875. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  876. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  877. }
  878. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  879. {
  880. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq != status)
  881. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = status;
  882. }
  883. }
  884. void ocpp_set_stoptransaction_conf(uint8_t gun_index, uint8_t status)
  885. {
  886. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  887. {
  888. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf != status)
  889. {
  890. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = status;
  891. }
  892. }
  893. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  894. {
  895. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  896. {
  897. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  898. }
  899. }
  900. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  901. {
  902. if(ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf != status)
  903. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf = status;
  904. }
  905. }
  906. void ocpp_copy_userid_to_starttransaction(uint8_t gun_index)
  907. {
  908. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  909. {
  910. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  911. }
  912. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  913. {
  914. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  915. }
  916. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  917. {
  918. memcpy((char*)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16DataPH->StartTransaction[gun_index].IdTag));
  919. }
  920. }
  921. uint8_t ocpp_get_starttransaction_result(uint8_t gun_index)
  922. {
  923. uint8_t result = PASS;
  924. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  925. {
  926. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  927. {
  928. if((strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Blocked")==0) ||
  929. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Expired")==0) ||
  930. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid")==0))
  931. result = NO;
  932. }
  933. }
  934. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  935. {
  936. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "true"))
  937. {
  938. if((strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Blocked")==0) ||
  939. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Expired")==0) ||
  940. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Invalid")==0) ||
  941. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NoCredit")==0) ||
  942. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAllowedTypeEVSE")==0) ||
  943. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisLocation")==0) ||
  944. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisTime")==0) ||
  945. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Unknown")==0))
  946. result = NO;
  947. }
  948. }
  949. return result;
  950. }
  951. uint8_t ocpp_get_maxcharging_profileId()
  952. {
  953. uint8_t result = 0;
  954. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  955. {
  956. result = ShmOCPP16Data->MaxChargingProfile.ChargingProfileId;
  957. }
  958. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  959. {
  960. result = ShmOCPP20Data->MaxChargingProfile.id;
  961. }
  962. return result;
  963. }
  964. uint8_t ocpp_get_smartcharging_profileId(uint8_t gun_index)
  965. {
  966. uint8_t result = 0;
  967. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  968. {
  969. result = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId;
  970. }
  971. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  972. {
  973. result = ShmOCPP20Data->SmartChargingProfile[gun_index].id;
  974. }
  975. return result;
  976. }
  977. void ocpp_reset_smartcharging_profileId(uint8_t gun_index)
  978. {
  979. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  980. {
  981. ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
  982. }
  983. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  984. {
  985. ShmOCPP20Data->SmartChargingProfile[gun_index].id = 0;
  986. }
  987. }
  988. uint8_t ocpp_get_profile_req(uint8_t gun_index)
  989. {
  990. uint8_t result = OFF;
  991. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  992. {
  993. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  994. }
  995. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  996. {
  997. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  998. }
  999. return result;
  1000. }
  1001. void ocpp_set_profile_req(uint8_t gun_index, uint8_t status)
  1002. {
  1003. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1004. {
  1005. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  1006. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  1007. }
  1008. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1009. {
  1010. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  1011. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  1012. }
  1013. }
  1014. uint8_t ocpp_get_profile_conf(uint8_t gun_index)
  1015. {
  1016. uint8_t result = OFF;
  1017. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1018. {
  1019. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  1020. }
  1021. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1022. {
  1023. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  1024. }
  1025. return result;
  1026. }
  1027. void ocpp_set_profile_conf(uint8_t gun_index, uint8_t status)
  1028. {
  1029. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1030. {
  1031. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  1032. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  1033. }
  1034. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1035. {
  1036. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  1037. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  1038. }
  1039. }
  1040. uint8_t ocpp_get_StopTransactionOnEVSideDisconnect()
  1041. {
  1042. uint8_t result = OFF;
  1043. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1044. {
  1045. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)
  1046. result = ON;
  1047. }
  1048. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1049. {
  1050. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnEVSideDisconnect].variableAttribute[0].value, "true") == 0)
  1051. result = ON;
  1052. }
  1053. return result;
  1054. }
  1055. uint8_t ocpp_get_cancelreservation_req(uint8_t gun_index)
  1056. {
  1057. uint8_t result = OFF;
  1058. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1059. {
  1060. result = ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq;
  1061. }
  1062. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1063. {
  1064. result = ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq;
  1065. }
  1066. return result;
  1067. }
  1068. void ocpp_set_cancelreservation_req(uint8_t gun_index, uint8_t status)
  1069. {
  1070. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1071. {
  1072. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1073. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1074. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1075. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1076. }
  1077. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1078. {
  1079. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1080. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1081. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1082. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1083. }
  1084. }
  1085. uint8_t ocpp_isAuthorizeRemoteStart()
  1086. {
  1087. uint8_t result = NO;
  1088. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1089. {
  1090. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  1091. {
  1092. result = YES;
  1093. }
  1094. }
  1095. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1096. {
  1097. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "true"))
  1098. {
  1099. result = YES;
  1100. }
  1101. }
  1102. return result;
  1103. }
  1104. uint8_t ocpp_get_freevend_idtag(uint8_t * userId)
  1105. {
  1106. uint8_t result = NO;
  1107. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1108. {
  1109. strcpy((char*)userId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  1110. result = YES;
  1111. }
  1112. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1113. {
  1114. strcpy((char*)userId, (char*)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value);
  1115. result = YES;
  1116. }
  1117. return result;
  1118. }
  1119. //======================================================
  1120. // Check interface status
  1121. //======================================================
  1122. int isInterfaceUp(const char *interface)
  1123. {
  1124. int result = FAIL;
  1125. FILE *fp;
  1126. char cmd[256];
  1127. char buf[512];
  1128. strcpy(cmd, "ifconfig");;
  1129. fp = popen(cmd, "r");
  1130. if(fp != NULL)
  1131. {
  1132. while(fgets(buf, sizeof(buf), fp) != NULL)
  1133. {
  1134. if(strstr(buf, interface) > 0)
  1135. {
  1136. result = PASS;
  1137. }
  1138. }
  1139. }
  1140. pclose(fp);
  1141. return result;
  1142. }
  1143. //======================================================
  1144. // Create all share memory
  1145. //======================================================
  1146. int CreatShareMemory()
  1147. {
  1148. int result = PASS;
  1149. int MeterSMId;
  1150. //creat ShmSysConfigAndInfo
  1151. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  1152. {
  1153. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1154. result = FAIL;
  1155. }
  1156. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1157. {
  1158. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  1159. result = FAIL;
  1160. }
  1161. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  1162. //creat ShmStatusCodeData
  1163. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  1164. {
  1165. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1166. result = FAIL;
  1167. }
  1168. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1169. {
  1170. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1171. result = FAIL;
  1172. }
  1173. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  1174. //creat ShmPsuData
  1175. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  1176. {
  1177. DEBUG_ERROR("shmget ShmPsuData NG\n");
  1178. result = FAIL;
  1179. }
  1180. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1181. {
  1182. DEBUG_ERROR("shmat ShmPsuData NG\n");
  1183. result = FAIL;
  1184. }
  1185. memset(ShmPsuData,0,sizeof(struct PsuData));
  1186. //creat ShmCHAdeMOData
  1187. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  1188. {
  1189. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\n");
  1190. result = FAIL;
  1191. }
  1192. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1193. {
  1194. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\n");
  1195. result = FAIL;
  1196. }
  1197. memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
  1198. //creat ShmCcsData
  1199. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  1200. {
  1201. DEBUG_ERROR("shmget ShmCcsData NG\n");
  1202. result = FAIL;
  1203. }
  1204. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1205. {
  1206. DEBUG_ERROR("shmat ShmCcsData NG\n");
  1207. result = FAIL;
  1208. }
  1209. memset(ShmCcsData,0,sizeof(struct CcsData));
  1210. //creat ShmPrimaryMcuData
  1211. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  1212. {
  1213. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1214. result = FAIL;
  1215. }
  1216. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1217. {
  1218. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1219. result = FAIL;
  1220. }
  1221. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  1222. /*
  1223. //creat ShmFanModuleData
  1224. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  1225. {
  1226. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  1227. result = FAIL;
  1228. }
  1229. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1230. {
  1231. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  1232. result = FAIL;
  1233. }
  1234. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  1235. //creat ShmRelayModuleData
  1236. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  1237. {
  1238. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  1239. result = FAIL;
  1240. }
  1241. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1242. {
  1243. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  1244. result = FAIL;
  1245. }
  1246. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));
  1247. */
  1248. //creat ShmOCPP16Data
  1249. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  1250. {
  1251. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  1252. result = FAIL;
  1253. }
  1254. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1255. {
  1256. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  1257. result = FAIL;
  1258. }
  1259. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  1260. //creat ShmOCPP20Data
  1261. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  1262. {
  1263. DEBUG_ERROR("shmget OCPP20Data NG\n");
  1264. result = FAIL;
  1265. }
  1266. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1267. {
  1268. DEBUG_ERROR("shmat OCPP20Data NG\n");
  1269. result = FAIL;
  1270. }
  1271. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  1272. //creat ShmCharger
  1273. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  1274. {
  1275. DEBUG_ERROR("shmget ShmCharger NG\n");
  1276. result = FAIL;
  1277. }
  1278. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1279. {
  1280. DEBUG_ERROR("shmat ShmCharger NG\n");
  1281. result = FAIL;
  1282. }
  1283. memset(ShmCharger,0,sizeof(struct Charger));
  1284. //creat ShmOCPP16DataPH
  1285. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT|0777)) < 0)
  1286. {
  1287. DEBUG_ERROR("shmget ShmOCPP16DataPH NG\n");
  1288. result = FAIL;
  1289. }
  1290. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1291. {
  1292. DEBUG_ERROR("shmat ShmOCPP16DataPH NG\n");
  1293. result = FAIL;
  1294. }
  1295. else
  1296. {}
  1297. return result;
  1298. }
  1299. //===============================================
  1300. // SQLite3 related routine
  1301. //===============================================
  1302. int DB_Open(sqlite3 *db)
  1303. {
  1304. int result = PASS;
  1305. char* errMsg = NULL;
  1306. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  1307. "idx integer primary key AUTOINCREMENT, "
  1308. "reservationId text, "
  1309. "transactionId text, "
  1310. "startMethod text, "
  1311. "userId text, "
  1312. "dateTimeStart text, "
  1313. "dateTimeStop text,"
  1314. "socStart text, "
  1315. "socStop text, "
  1316. "chargeEnergy text, "
  1317. "stopReason text"
  1318. ");";
  1319. char* createRecordBufSql="CREATE TABLE IF NOT EXISTS charging_record_buffer("
  1320. "reservationId text, "
  1321. "transactionId text, "
  1322. "startMethod text, "
  1323. "userId text, "
  1324. "dateTimeStart text, "
  1325. "dateTimeStop text,"
  1326. "socStart text, "
  1327. "socStop text, "
  1328. "chargeEnergy text, "
  1329. "stopReason text"
  1330. ");";
  1331. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  1332. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  1333. "`item` TEXT NOT NULL, "
  1334. "`connector` INTEGER NOT NULL, "
  1335. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  1336. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1337. if(sqlite3_open(DB_FILE, &db))
  1338. {
  1339. result = FAIL;
  1340. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  1341. sqlite3_close(db);
  1342. }
  1343. else
  1344. {
  1345. DEBUG_INFO( "Local charging record database open successfully.\n");
  1346. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  1347. {
  1348. result = FAIL;
  1349. DEBUG_ERROR( "Create local charging record table error message: %s\n", errMsg);
  1350. }
  1351. else
  1352. {
  1353. DEBUG_INFO( "Opened local charging record table successfully\n");
  1354. }
  1355. if (sqlite3_exec(db, createRecordBufSql, 0, 0, &errMsg) != SQLITE_OK)
  1356. {
  1357. result = FAIL;
  1358. DEBUG_ERROR( "Create local charging record buffer table error message: %s\n", errMsg);
  1359. }
  1360. else
  1361. {
  1362. DEBUG_INFO( "Opened local charging record buffer table successfully\n");
  1363. }
  1364. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  1365. {
  1366. result = FAIL;
  1367. DEBUG_ERROR( "Create local config table error message: %s\n", errMsg);
  1368. }
  1369. else
  1370. {
  1371. DEBUG_INFO( "Opened local config table successfully\n");
  1372. }
  1373. sqlite3_close(db);
  1374. }
  1375. return result;
  1376. }
  1377. int DB_Open_OCPP20(sqlite3 *db)
  1378. {
  1379. int result = PASS;
  1380. char* errMsg = NULL;
  1381. char *sqlOcppVariable = "create table if not exists ocpp20_variable (idx integer primary key,"
  1382. "componentName text NOT NULL ,"
  1383. "componentInstance text, "
  1384. "variableName text NOT NULL, "
  1385. "variableInstance text, "
  1386. "variableCharacteristicsDataType integer, "
  1387. "variableCharacteristicsUnit text, "
  1388. "variableCharacteristicsMaxLimit real,"
  1389. "variableAttributesType text, "
  1390. "variableAttributesTypeMutability text, "
  1391. "variableAttributesTypeValue text, "
  1392. "variableCharacteristicsMinLimit real,"
  1393. "unique(componentName, componentInstance,variableName, variableInstance) on conflict replace);";
  1394. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1395. if(sqlite3_open(DB_FILE_OCPP_LOCAL, &db))
  1396. {
  1397. result = FAIL;
  1398. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  1399. sqlite3_close(db);
  1400. }
  1401. else
  1402. {
  1403. DEBUG_INFO( "OCPP local database open successfully.\n");
  1404. if(sqlite3_exec(db, sqlOcppVariable, 0, 0, &errMsg) != SQLITE_OK)
  1405. {
  1406. result = FAIL;
  1407. DEBUG_ERROR( "Create OCPP 2.0 variable table error message: %s\n", errMsg);
  1408. }
  1409. else
  1410. {
  1411. DEBUG_INFO( "Create OCPP 2.0 variable table successfully\n");
  1412. }
  1413. sqlite3_close(db);
  1414. }
  1415. return result;
  1416. }
  1417. int DB_variableClear(sqlite3 *db)
  1418. {
  1419. int result = PASS;
  1420. //char * sqlcleanLocalList = "delete from ocpp20_variable";
  1421. char * sqlcleanLocalList = "drop table ocpp20_variable";
  1422. char *errMsg = 0;
  1423. if(sqlite3_open(DB_FILE_OCPP_LOCAL, &db))
  1424. {
  1425. result = FAIL;
  1426. DEBUG_ERROR( "Can't open database for ocpp 2.0 variable: %s\n", sqlite3_errmsg(db));
  1427. sqlite3_close(db);
  1428. }
  1429. else
  1430. {
  1431. DEBUG_INFO("Command: &s\n", sqlcleanLocalList);
  1432. if(sqlite3_exec(db, sqlcleanLocalList, 0, 0, &errMsg) != SQLITE_OK)
  1433. {
  1434. DEBUG_INFO("%s\n", errMsg);
  1435. DEBUG_INFO("Drop table failed.\n");
  1436. result = FAIL;
  1437. }
  1438. DEBUG_INFO("Drop table succeed.\n");
  1439. }
  1440. return result;
  1441. }
  1442. int DB_Insert_Record(sqlite3 *db, int gun_index)
  1443. {
  1444. int result = PASS;
  1445. char* errMsg = NULL;
  1446. char sqlStr[1024];
  1447. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1448. {
  1449. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  1450. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  1451. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1452. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1453. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1454. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1455. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1456. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1457. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1458. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1459. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1460. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  1461. }
  1462. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1463. {
  1464. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  1465. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  1466. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1467. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1468. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1469. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1470. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1471. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1472. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1473. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1474. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1475. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  1476. }
  1477. if(sqlite3_open(DB_FILE, &db))
  1478. {
  1479. result = FAIL;
  1480. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1481. sqlite3_close(db);
  1482. }
  1483. else
  1484. {
  1485. DEBUG_INFO( "Local charging record database open successfully.\n");
  1486. // Delete buffer
  1487. if(sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1488. {
  1489. result = FAIL;
  1490. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1491. }
  1492. else
  1493. {
  1494. DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1495. }
  1496. // Insert charging record
  1497. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1498. {
  1499. result = FAIL;
  1500. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  1501. }
  1502. else
  1503. {
  1504. DEBUG_INFO( "Insert local charging record successfully\n");
  1505. }
  1506. sprintf(sqlStr, "delete from charging_record where (idx < (select idx from charging_record order by idx desc limit 1)-2000) and (dateTimeStop < '%04d.01.01 00:00:00');", (getCurrentYear()-3));
  1507. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1508. {
  1509. result = FAIL;
  1510. DEBUG_INFO( "delete local charging error message: %s\n", errMsg);
  1511. }
  1512. else
  1513. {
  1514. DEBUG_INFO( "delete local charging record successfully\n");
  1515. }
  1516. sqlite3_close(db);
  1517. }
  1518. return result;
  1519. }
  1520. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
  1521. {
  1522. uint8_t result = false;
  1523. char* errMsg = NULL;
  1524. char sqlStr[1024];
  1525. srand(time(NULL));
  1526. if(sqlite3_open(DB_FILE, &db))
  1527. {
  1528. result = FAIL;
  1529. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1530. sqlite3_close(db);
  1531. }
  1532. else
  1533. {
  1534. DEBUG_INFO( "Local charging record database open successfully.\n");
  1535. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
  1536. DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1537. if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1538. {
  1539. result = FAIL;
  1540. DEBUG_INFO( "update config error message: %s\n", errMsg);
  1541. }
  1542. else
  1543. {
  1544. DEBUG_INFO("update connector-%d config item isOperactive to %d\n", gun_index, isOperactive);
  1545. }
  1546. sqlite3_close(db);
  1547. }
  1548. return result;
  1549. }
  1550. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  1551. {
  1552. uint8_t result = true;
  1553. char* errMsg = NULL;
  1554. char sqlStr[1024];
  1555. char **rs;
  1556. int rows, cols;
  1557. sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
  1558. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1559. if(sqlite3_open(DB_FILE, &db))
  1560. {
  1561. result = FAIL;
  1562. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1563. sqlite3_close(db);
  1564. }
  1565. else
  1566. {
  1567. DEBUG_INFO( "Local config query database open successfully.\n");
  1568. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1569. if(rows>0)
  1570. {
  1571. for(int idxRow=1;idxRow<=rows;idxRow++)
  1572. {
  1573. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  1574. {
  1575. result = false;
  1576. }
  1577. DEBUG_INFO("Query connector-%d isOperactive: %s\n", gun_index, rs[(idxRow*cols)+3]);
  1578. }
  1579. }
  1580. else
  1581. {
  1582. DEBUG_INFO("Query connector-%d fail, default value as operactive.\n", gun_index);
  1583. }
  1584. sqlite3_free_table(rs);
  1585. sqlite3_close(db);
  1586. }
  1587. return result;
  1588. }
  1589. //======================================================
  1590. // Peripheral initial
  1591. //======================================================
  1592. int InitWatchDog()
  1593. {
  1594. int fd;
  1595. int timeout = 180;
  1596. system("/usr/bin/fuser -k /dev/watchdog");
  1597. sleep(1);
  1598. system("echo V > /dev/watchdog");
  1599. sleep(1);
  1600. fd=open("/dev/watchdog", O_RDWR);
  1601. if(fd<=0)
  1602. {
  1603. DEBUG_ERROR("System watch dog initial fail.\n");
  1604. }
  1605. ioctl(fd, _IOWR('W', 6, int), &timeout);
  1606. return fd;
  1607. }
  1608. void InitGPIO()
  1609. {
  1610. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1611. /***************************************************************/
  1612. /*************** INPUT PIN ***************************************/
  1613. /***************************************************************/
  1614. /***************************************************************/
  1615. /*************** OUTPUT PIN ************************************/
  1616. /***************************************************************/
  1617. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  1618. system("echo 116 > /sys/class/gpio/export");
  1619. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1620. system("echo 0 > /sys/class/gpio/gpio116/value");
  1621. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  1622. system("echo 19 > /sys/class/gpio/export");
  1623. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1624. system("echo 1 > /sys/class/gpio/gpio19/value");
  1625. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  1626. system("echo 65 > /sys/class/gpio/export");
  1627. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  1628. system("echo 0 > /sys/class/gpio/gpio65/value");
  1629. /*(FOR ACCSU-01-RW) 4G POWER RST:GPIO3_14 => H:ON; L:OFF*/
  1630. system("echo 110 > /sys/class/gpio/export");
  1631. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  1632. system("echo 1 > /sys/class/gpio/gpio110/value");
  1633. sleep(3);
  1634. system("echo 0 > /sys/class/gpio/gpio110/value");
  1635. /*(FOR ACCSU3P5-01-RW) 4G POWER RST:GPIO3_18 => H:ON; L:OFF*/
  1636. system("echo 114 > /sys/class/gpio/export");
  1637. system("echo \"out\" > /sys/class/gpio/gpio114/direction");
  1638. system("echo 1 > /sys/class/gpio/gpio114/value");
  1639. sleep(3);
  1640. system("echo 0 > /sys/class/gpio/gpio114/value");
  1641. DEBUG_INFO("Initial GPIO OK\n");
  1642. }
  1643. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1644. {
  1645. int fd,wrd;
  1646. unsigned char *buf;
  1647. unsigned int ChkSum,ChkSumOrg;
  1648. if((buf=malloc(MtdBlockSize))==NULL)
  1649. {
  1650. DEBUG_ERROR("malloc buffer NG,rebooting..\n");
  1651. if(ShmStatusCodeData!=NULL)
  1652. {
  1653. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1654. }
  1655. sleep(5);
  1656. system("reboot -f");
  1657. sleep(5);
  1658. system("reboot -f");
  1659. }
  1660. memset(buf, 0, MtdBlockSize);
  1661. //================================================
  1662. // Load configuration from mtdblock10
  1663. //================================================
  1664. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1665. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1666. if (fd < 0)
  1667. {
  1668. free(buf);
  1669. DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
  1670. if(ShmStatusCodeData!=NULL)
  1671. {
  1672. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1673. }
  1674. sleep(5);
  1675. system("reboot -f");
  1676. sleep(5);
  1677. system("reboot -f");
  1678. }
  1679. wrd=read(fd, buf, MtdBlockSize);
  1680. close(fd);
  1681. if(wrd<MtdBlockSize)
  1682. {
  1683. free(buf);
  1684. DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
  1685. if(ShmStatusCodeData!=NULL)
  1686. {
  1687. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1688. }
  1689. sleep(5);
  1690. system("reboot -f");
  1691. sleep(5);
  1692. system("reboot -f");
  1693. }
  1694. ChkSum=0;
  1695. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1696. {
  1697. ChkSum+=buf[wrd];
  1698. }
  1699. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1700. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1701. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1702. //================================================
  1703. // Load configuration from mtdblock11
  1704. //================================================
  1705. if(ChkSum!=ChkSumOrg)
  1706. {
  1707. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1708. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1709. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1710. if (fd < 0)
  1711. {
  1712. free(buf);
  1713. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  1714. if(ShmStatusCodeData!=NULL)
  1715. {
  1716. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1717. }
  1718. sleep(5);
  1719. system("reboot -f");
  1720. sleep(5);
  1721. system("reboot -f");
  1722. }
  1723. memset(buf, 0, MtdBlockSize);
  1724. wrd=read(fd, buf,MtdBlockSize);
  1725. close(fd);
  1726. if(wrd<MtdBlockSize)
  1727. {
  1728. free(buf);
  1729. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  1730. if(ShmStatusCodeData!=NULL)
  1731. {
  1732. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1733. }
  1734. sleep(5);
  1735. system("reboot -f");
  1736. sleep(5);
  1737. system("reboot -f");
  1738. }
  1739. ChkSum=0;
  1740. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1741. {
  1742. ChkSum+=buf[wrd];
  1743. }
  1744. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1745. //================================================
  1746. // Load configuration from mtdblock12 (Factory default)
  1747. //================================================
  1748. if(ChkSum!=ChkSumOrg)
  1749. {
  1750. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  1751. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1752. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1753. if (fd < 0)
  1754. {
  1755. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  1756. free(buf);
  1757. if(ShmStatusCodeData!=NULL)
  1758. {
  1759. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1760. }
  1761. sleep(5);
  1762. system("reboot -f");
  1763. sleep(5);
  1764. system("reboot -f");
  1765. }
  1766. memset(buf, 0, MtdBlockSize);
  1767. wrd=read(fd, buf,MtdBlockSize);
  1768. close(fd);
  1769. if(wrd<MtdBlockSize)
  1770. {
  1771. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  1772. free(buf);
  1773. if(ShmStatusCodeData!=NULL)
  1774. {
  1775. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1776. }
  1777. sleep(5);
  1778. system("reboot -f");
  1779. sleep(5);
  1780. system("reboot -f");
  1781. }
  1782. ChkSum=0;
  1783. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1784. {
  1785. ChkSum+=buf[wrd];
  1786. }
  1787. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1788. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1789. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1790. if(ChkSum!=ChkSumOrg)
  1791. {
  1792. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  1793. free(buf);
  1794. system("cd /root;./Module_FactoryConfig -m");
  1795. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1796. system("sync");
  1797. sleep(5);
  1798. system("reboot -f");
  1799. sleep(5);
  1800. system("reboot -f");
  1801. return FAIL;
  1802. }
  1803. }
  1804. }
  1805. //load OK
  1806. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1807. free(buf);
  1808. system("rm -f /mnt/EvseConfig.bin");
  1809. // SysConfig in flash is empty (0xffffffff)
  1810. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1811. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1812. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1813. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1814. {
  1815. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1816. {
  1817. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1818. }
  1819. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1820. {
  1821. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1822. }
  1823. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1824. {
  1825. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1826. }
  1827. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1828. {
  1829. DEBUG_INFO("Ethernet dhcp config is null.\n");
  1830. }
  1831. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  1832. {
  1833. DEBUG_INFO("Model name over length.\n");
  1834. }
  1835. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  1836. {
  1837. DEBUG_INFO("Model serial number over length.\n");
  1838. }
  1839. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  1840. {
  1841. DEBUG_INFO("SystemId over length.\n");
  1842. }
  1843. system("cd /root;./Module_FactoryConfig -m");
  1844. sleep(3);
  1845. system("/usr/bin/run_evse_restart.sh");
  1846. }
  1847. DEBUG_INFO("Load SysConfigData OK\n");
  1848. RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
  1849. return PASS;
  1850. }
  1851. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1852. {
  1853. int result = PASS;
  1854. int fd,wrd;
  1855. unsigned int i,Chk;
  1856. unsigned char *ptr, *BufTmp;
  1857. Chk=0;
  1858. ptr=(unsigned char *)UsrData;
  1859. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1860. {
  1861. memset(BufTmp,0,MtdBlockSize);
  1862. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1863. for(i=0;i<MtdBlockSize-4;i++)
  1864. Chk+=*(BufTmp+i);
  1865. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  1866. // Output configuration to file.
  1867. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  1868. if (fd < 0)
  1869. {
  1870. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  1871. free(BufTmp);
  1872. return 0;
  1873. }
  1874. wrd=write(fd, BufTmp, MtdBlockSize);
  1875. close(fd);
  1876. if(wrd<MtdBlockSize)
  1877. {
  1878. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  1879. free(BufTmp);
  1880. return 0;
  1881. }
  1882. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  1883. DEBUG_INFO("Erase /dev/mtd10.\n");
  1884. runShellCmd("flash_erase /dev/mtd10 0 0");
  1885. DEBUG_INFO("Write /dev/mtd10.\n");
  1886. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  1887. DEBUG_INFO("Erase /dev/mtd11.\n");
  1888. runShellCmd("flash_erase /dev/mtd11 0 0");
  1889. DEBUG_INFO("Write /dev/mtd11.\n");
  1890. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  1891. system("rm -f /mnt/EvseConfig.bin");
  1892. DEBUG_INFO("EvseConfig write to flash OK\n");
  1893. }
  1894. else
  1895. {
  1896. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1897. result = FAIL;
  1898. }
  1899. if(BufTmp!=NULL)
  1900. free(BufTmp);
  1901. return result;
  1902. }
  1903. void InitEthernet()
  1904. {
  1905. pid_t pid;
  1906. uint8_t cnt_pingDNS_Fail;
  1907. char tmpbuf[256];
  1908. unsigned int natInterface = 0;
  1909. // Detele bridge interface
  1910. system("/sbin/ifconfig uap0 down");
  1911. system("/sbin/ifconfig br0 down");
  1912. system("/usr/sbin/brctl delbr br0");
  1913. //Init Eth0 for internet
  1914. memset(tmpbuf,0,256);
  1915. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1916. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1917. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1918. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1919. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1920. system(tmpbuf);
  1921. memset(tmpbuf,0,256);
  1922. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1923. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1924. system(tmpbuf);
  1925. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  1926. system("ifconfig lo up &");
  1927. system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  1928. if(isInterfaceUp("eth1")==PASS)
  1929. {
  1930. //Init Eth1 for administrator tool
  1931. memset(tmpbuf,0,256);
  1932. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up &",
  1933. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  1934. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  1935. system(tmpbuf);
  1936. }
  1937. //Run DHCP client if enabled
  1938. system("killall udhcpc");
  1939. system("rm -rf /etc/resolv.conf");
  1940. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  1941. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1942. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  1943. {
  1944. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1945. system(tmpbuf);
  1946. }
  1947. // Upgrade system id to /etc/hostname
  1948. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1949. system(tmpbuf);
  1950. // Ethernet MAC address
  1951. getEth0MacAddress();
  1952. //check internet status
  1953. pid = fork();
  1954. if(pid == 0)
  1955. {
  1956. for(;;)
  1957. {
  1958. if(isRouteFail())
  1959. {
  1960. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  1961. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1962. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1963. {
  1964. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1965. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1966. system(tmpbuf);
  1967. }
  1968. else
  1969. {
  1970. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1971. memset(tmpbuf,0,256);
  1972. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1973. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1974. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1975. system(tmpbuf);
  1976. memset(tmpbuf,0,256);
  1977. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1978. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1979. system(tmpbuf);
  1980. }
  1981. }
  1982. if(isReachableInternet() == PASS)
  1983. {
  1984. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  1985. cnt_pingDNS_Fail = 0;
  1986. }
  1987. else
  1988. {
  1989. if(cnt_pingDNS_Fail >= 3)
  1990. {
  1991. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  1992. if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  1993. {
  1994. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  1995. {
  1996. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1997. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1998. system(tmpbuf);
  1999. }
  2000. else
  2001. {
  2002. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2003. memset(tmpbuf,0,256);
  2004. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2005. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2006. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2007. system(tmpbuf);
  2008. memset(tmpbuf,0,256);
  2009. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2010. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2011. system(tmpbuf);
  2012. }
  2013. cnt_pingDNS_Fail = 0;
  2014. }
  2015. }
  2016. else
  2017. {
  2018. cnt_pingDNS_Fail++;
  2019. }
  2020. }
  2021. // Network status
  2022. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2023. {
  2024. case 'E':
  2025. if((ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet) &&
  2026. (ShmOCPP16Data->OcppConnStatus != PASS))
  2027. {
  2028. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2029. }
  2030. else
  2031. {
  2032. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2033. }
  2034. break;
  2035. case 'W':
  2036. if((ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet) &&
  2037. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  2038. (ShmOCPP16Data->OcppConnStatus != PASS))
  2039. {
  2040. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2041. }
  2042. else
  2043. {
  2044. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2045. }
  2046. break;
  2047. case 'T':
  2048. if((ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet) &&
  2049. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  2050. (ShmOCPP16Data->OcppConnStatus != PASS))
  2051. {
  2052. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2053. }
  2054. else
  2055. {
  2056. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2057. }
  2058. break;
  2059. case 'D':
  2060. if((ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet) &&
  2061. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  2062. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  2063. (ShmOCPP16Data->OcppConnStatus != PASS))
  2064. {
  2065. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2066. }
  2067. else
  2068. {
  2069. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2070. }
  2071. break;
  2072. }
  2073. //============================================================
  2074. // Priority for internet 0 : First / 1 : Second / 2: Third
  2075. //============================================================
  2076. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  2077. {
  2078. system("/sbin/ifmetric eth0 0");
  2079. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2080. {
  2081. system("/sbin/ifmetric mlan0 1");
  2082. }
  2083. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2084. {
  2085. system("/sbin/ifmetric ppp0 2");
  2086. }
  2087. if(isKernelSupportNAT() == YES)
  2088. {
  2089. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2090. {
  2091. if(natInterface != 1)
  2092. {
  2093. system("/sbin/iptables -t nat -F");
  2094. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
  2095. natInterface = 1;
  2096. }
  2097. }
  2098. else
  2099. {
  2100. system("/sbin/iptables -t nat -F");
  2101. }
  2102. }
  2103. }
  2104. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  2105. {
  2106. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2107. {
  2108. system("/sbin/ifmetric eth0 1");
  2109. system("/sbin/ifmetric mlan0 0");
  2110. }
  2111. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2112. {
  2113. system("/sbin/ifmetric ppp0 2");
  2114. }
  2115. }
  2116. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  2117. {
  2118. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2119. {
  2120. system("/sbin/ifmetric mlan0 2");
  2121. }
  2122. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2123. {
  2124. system("/sbin/ifmetric eth0 1");
  2125. system("/sbin/ifmetric ppp0 0");
  2126. }
  2127. if(isKernelSupportNAT() == YES)
  2128. {
  2129. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2130. {
  2131. if(natInterface != 2)
  2132. {
  2133. system("/sbin/iptables -t nat -F");
  2134. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
  2135. natInterface = 2;
  2136. }
  2137. }
  2138. else
  2139. {
  2140. system("/sbin/iptables -t nat -F");
  2141. }
  2142. }
  2143. }
  2144. else
  2145. {
  2146. if(isKernelSupportNAT() == YES)
  2147. {
  2148. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2149. {
  2150. system("/sbin/iptables -t nat -F");
  2151. natInterface = 0;
  2152. }
  2153. }
  2154. }
  2155. // Check is need to reset WIFI/4G module power
  2156. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus) &&
  2157. ((system("pidof -s Module_Wifi > /dev/null") != 0) ||
  2158. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) ||
  2159. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == ON)) ||
  2160. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi == ON))) &&
  2161. ((system("pidof -s Module_4g > /dev/null") != 0) ||
  2162. (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) ||
  2163. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == ON))))
  2164. {
  2165. if(getDiffSecNow(startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]) > TIMEOUT_SPEC_RESET_4G_WIFI_POWER)
  2166. {
  2167. if((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != 0) || (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled != 0))
  2168. {
  2169. DEBUG_WARN("WIFI/4G module reset.\n");
  2170. // FOR ACCSU-01-RW RESET 4G
  2171. system("echo 1 > /sys/class/gpio/gpio110/value");
  2172. // FOR ACCSU3P5-01-RW RESET 4G
  2173. system("echo 1 > /sys/class/gpio/gpio114/value");
  2174. sleep(3);
  2175. // FOR ACCSU-01-RW RESET 4G
  2176. system("echo 0 > /sys/class/gpio/gpio110/value");
  2177. // FOR ACCSU3P5-01-RW RESET 4G
  2178. system("echo 0 > /sys/class/gpio/gpio114/value");
  2179. }
  2180. refreshStartTimer(&startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]);
  2181. }
  2182. }
  2183. else
  2184. {
  2185. refreshStartTimer(&startTime[0][TMR_IDX_RESET_4G_WIFI_POWER]);
  2186. }
  2187. // Bridge ethernet to uap0 to get dynamic ip address
  2188. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing &&
  2189. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) &&
  2190. (isUap0up() == PASS) &&
  2191. (access("/sys/class/net/br0/address", F_OK) == -1))
  2192. {
  2193. sleep(10);
  2194. system("/usr/sbin/brctl addbr br0");
  2195. system("/usr/sbin/brctl addif br0 eth0");
  2196. system("/usr/sbin/brctl addif br0 uap0");
  2197. system("/sbin/ifconfig br0 192.168.10.200 up");
  2198. system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
  2199. system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
  2200. system("kill udhcpd");
  2201. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  2202. DEBUG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.\n");
  2203. }
  2204. // Change uap0 to low priority
  2205. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing &&
  2206. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) &&
  2207. (isUap0up() == PASS))
  2208. {
  2209. system("/sbin/ifmetric uap0 1");
  2210. }
  2211. sleep(5);
  2212. }
  2213. }
  2214. DEBUG_INFO("Initial Ethernet OK\n");
  2215. }
  2216. int SpawnTask()
  2217. {
  2218. system ("pkill Module_");
  2219. system ("pkill OcppBackend");
  2220. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2221. {
  2222. system("/root/Module_4g &");
  2223. }
  2224. else if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2225. {
  2226. system("/root/Module_Wifi &");
  2227. }
  2228. system ("/root/Module_EventLogging &");
  2229. system ("/root/Module_AlarmDetect &");
  2230. system ("/root/Module_InternalComm &");
  2231. system ("/root/Module_Speaker &");
  2232. system ("/root/Module_ProduceUtils &");
  2233. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2234. {
  2235. ocpp_process_start();
  2236. }
  2237. else
  2238. {
  2239. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  2240. ocpp_process_start();
  2241. }
  2242. return PASS;
  2243. }
  2244. int Initialization()
  2245. {
  2246. int result = PASS;
  2247. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2248. InitGPIO();
  2249. InitEthernet();
  2250. if(DB_Open(localDb) != PASS)
  2251. {
  2252. DEBUG_ERROR("Local database initial fail.\n");
  2253. result = FAIL;
  2254. }
  2255. if(DB_Open_OCPP20(ocppDb) != PASS)
  2256. {
  2257. DEBUG_ERROR("OCPP 2.0 configuration variable database initial fail.\n");
  2258. result = FAIL;
  2259. }
  2260. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  2261. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2262. if((rfidFd = InitRfidPort()) == FAIL)
  2263. {
  2264. DEBUG_ERROR("RFID port initial fail.\n");
  2265. result = FAIL;
  2266. }
  2267. if((wtdFd = InitWatchDog()) == FAIL)
  2268. {
  2269. DEBUG_ERROR("Watchdog initial fail.\n");
  2270. result = FAIL;
  2271. }
  2272. if(result == PASS)
  2273. DEBUG_INFO("Initialization OK.\n");
  2274. else
  2275. DEBUG_INFO("Initialization Fail.\n");
  2276. return result;
  2277. }
  2278. //=====================================================
  2279. // Common routine
  2280. //=====================================================
  2281. char* getSystemModeName(unsigned char mode)
  2282. {
  2283. char* result;
  2284. switch(mode)
  2285. {
  2286. case SYS_MODE_BOOTING:
  2287. result = "booting";
  2288. break;
  2289. case SYS_MODE_IDLE:
  2290. result = "idle";
  2291. break;
  2292. case SYS_MODE_AUTHORIZING:
  2293. result = "authorizing";
  2294. break;
  2295. case SYS_MODE_PREPARING:
  2296. result = "preparing";
  2297. break;
  2298. case SYS_MODE_CHARGING:
  2299. result = "charging";
  2300. break;
  2301. case SYS_MODE_TERMINATING:
  2302. result = "terminating";
  2303. break;
  2304. case SYS_MODE_COMPLETE:
  2305. result = "complete";
  2306. break;
  2307. case SYS_MODE_ALARM:
  2308. result = "alarm";
  2309. break;
  2310. case SYS_MODE_FAULT:
  2311. result = "fault";
  2312. break;
  2313. case SYS_MODE_MAINTAIN:
  2314. result = "maintain";
  2315. break;
  2316. case SYS_MODE_RESERVATION:
  2317. result = "reservation";
  2318. break;
  2319. case SYS_MODE_BOOKING:
  2320. result = "booking";
  2321. break;
  2322. case SYS_MODE_DEBUG:
  2323. result = "debug";
  2324. break;
  2325. case SYS_MODE_UPDATE:
  2326. result = "upgrade";
  2327. break;
  2328. default:
  2329. result = "unknown";
  2330. break;
  2331. }
  2332. return result;
  2333. }
  2334. void setChargerMode(unsigned char gun_index, unsigned char mode)
  2335. {
  2336. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2337. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  2338. DEBUG_INFO("Gun-%02d mode switch from %s to %s\n", gun_index, getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus), getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus));
  2339. }
  2340. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  2341. {
  2342. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  2343. }
  2344. unsigned char isModeChange(unsigned char gun_index)
  2345. {
  2346. unsigned char result = NO;
  2347. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  2348. {
  2349. result = YES;
  2350. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2351. }
  2352. return result;
  2353. }
  2354. void gpio_set_value(unsigned int gpio, unsigned int value)
  2355. {
  2356. int fd;
  2357. char buf[256];
  2358. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2359. fd = open(buf, O_WRONLY);
  2360. if (fd < 0)
  2361. {
  2362. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  2363. return;
  2364. }
  2365. if (value)
  2366. write(fd, "1", 2);
  2367. else
  2368. write(fd, "0", 2);
  2369. close(fd);
  2370. }
  2371. int gpio_get_value(unsigned int gpio)
  2372. {
  2373. int fd;
  2374. char buf[256];
  2375. char ch;
  2376. int8_t result = FAIL;
  2377. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2378. fd = open(buf, O_RDONLY);
  2379. if (fd < 0)
  2380. {
  2381. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  2382. return result;
  2383. }
  2384. read(fd, &ch, 1);
  2385. if (ch != '0')
  2386. result = 1;
  2387. else
  2388. result = 0;
  2389. close(fd);
  2390. return result;
  2391. }
  2392. //===============================================
  2393. // Get firmware version
  2394. //===============================================
  2395. void get_firmware_version(unsigned char gun_index)
  2396. {
  2397. FILE *fp;
  2398. char cmd[512];
  2399. char buf[512];
  2400. // Get CSU hardware version
  2401. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  2402. // Get CSU boot loader version
  2403. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2404. // Get CSU kernel version
  2405. sprintf(cmd, "/bin/uname -r");
  2406. fp = popen(cmd, "r");
  2407. if(fp == NULL)
  2408. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2409. else
  2410. {
  2411. while(fgets(buf, sizeof(buf), fp) != NULL)
  2412. {
  2413. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  2414. }
  2415. }
  2416. // Get MCU firmware version
  2417. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2418. // Get CSU root file system version
  2419. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "V0.72.00.0000.00");
  2420. // Get AC connector type from model name
  2421. for(uint8_t idx=0;idx<3;idx++)
  2422. {
  2423. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2424. {
  2425. case '1':
  2426. // J1772 Plug
  2427. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2428. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2429. break;
  2430. case '2':
  2431. // J1772 Socket
  2432. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2433. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2434. break;
  2435. case '3':
  2436. // CE Plug
  2437. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2438. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2439. break;
  2440. case '4':
  2441. // CE Socket
  2442. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2443. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2444. break;
  2445. case '5':
  2446. // GB Plug
  2447. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2448. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2449. break;
  2450. case '6':
  2451. // GB Socket
  2452. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2453. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2454. break;
  2455. }
  2456. }
  2457. // Get network option from model name
  2458. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2459. {
  2460. case 'B':
  2461. case 'U':
  2462. //Blue tooth
  2463. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2464. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2465. break;
  2466. case 'W':
  2467. // WIFI
  2468. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2469. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2470. break;
  2471. case 'T':
  2472. // 3G/4G
  2473. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2474. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2475. break;
  2476. case 'D':
  2477. // WIFI + 4G
  2478. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2479. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2480. break;
  2481. default:
  2482. // LAN
  2483. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2484. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2485. break;
  2486. }
  2487. // Get rating power from model name
  2488. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2489. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2490. // Get vender code from model name
  2491. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2492. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2493. DEBUG_INFO("========================================\n");
  2494. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2495. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2496. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2497. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2498. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2499. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2500. DEBUG_INFO("========================================\n");
  2501. }
  2502. //===============================================
  2503. // Upgrade firmware
  2504. //===============================================
  2505. int upgrade_check()
  2506. {
  2507. int result = PASS;
  2508. int fd;
  2509. DIR *dir;
  2510. struct dirent *file;
  2511. char cmd[512];
  2512. long int MaxLen=48*1024*1024;
  2513. if ((dir = opendir ("/mnt")) != NULL)
  2514. {
  2515. /* print all the files and directories within directory */
  2516. while ((file = readdir (dir)) != NULL)
  2517. {
  2518. if(strlen(file->d_name)>2)
  2519. {
  2520. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2521. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2522. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2523. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2524. {
  2525. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2526. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2527. read(fd, ptr, MaxLen);
  2528. close(fd);
  2529. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2530. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2531. free(ptr);
  2532. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2533. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2534. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  2535. {
  2536. switch(ShmCharger->fwUpgradeInfo.fwType)
  2537. {
  2538. case CSU_BOOTLOADER:
  2539. case CSU_KERNEL_CONFIGURATION:
  2540. case CSU_KERNEL_IMAGE:
  2541. case CSU_ROOT_FILE_SYSTEM:
  2542. case CSU_USER_CONFIGURATION:
  2543. case CSU_PRIMARY_CONTROLLER:
  2544. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2545. {
  2546. result = FAIL;
  2547. }
  2548. else
  2549. {
  2550. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2551. {
  2552. DEBUG_INFO("Restore model name & serial number.\n");
  2553. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2554. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2555. {
  2556. DEBUG_INFO("Re-load configuration fail.\n");
  2557. result = FAIL;
  2558. }
  2559. else
  2560. {
  2561. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  2562. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2563. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  2564. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2565. {
  2566. DEBUG_INFO("Re-write configuration fail.\n");
  2567. result = FAIL;
  2568. }
  2569. else
  2570. DEBUG_INFO("Re-write configuration OK.\n");
  2571. }
  2572. }
  2573. }
  2574. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2575. system(cmd);
  2576. break;
  2577. case AC_WALLMOUNT_CONTROLLER:
  2578. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2579. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2580. sleep(10);
  2581. break;
  2582. default:
  2583. result = FAIL;
  2584. DEBUG_WARN("Image file is unknown type.\n");
  2585. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2586. system(cmd);
  2587. break;
  2588. }
  2589. }
  2590. else
  2591. {
  2592. result = FAIL;
  2593. DEBUG_ERROR("Model name and Firmware type error.\n");
  2594. }
  2595. }
  2596. else
  2597. {
  2598. result = FAIL;
  2599. DEBUG_ERROR("New firmware open error.\n");
  2600. }
  2601. }
  2602. else
  2603. {
  2604. if(strlen(file->d_name) >= 3)
  2605. {
  2606. result = FAIL;
  2607. DEBUG_ERROR("File name error.\n");
  2608. }
  2609. else
  2610. {
  2611. DEBUG_ERROR("Searching file.\n");
  2612. }
  2613. }
  2614. }
  2615. closedir (dir);
  2616. }
  2617. else
  2618. {
  2619. result = FAIL;
  2620. DEBUG_ERROR("/mnt does not valid.\n");
  2621. }
  2622. return result;
  2623. }
  2624. //===============================================
  2625. // Check RFID is match with start user
  2626. //===============================================
  2627. int isMatchStartUser(unsigned char gun_index)
  2628. {
  2629. uint8_t tmpUser[32];
  2630. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2631. {
  2632. // Big endian
  2633. switch(rfid.snType)
  2634. {
  2635. case RFID_SN_TYPE_8BYTE:
  2636. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7]);
  2637. break;
  2638. case RFID_SN_TYPE_7BYTE:
  2639. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2640. break;
  2641. case RFID_SN_TYPE_10BYTE:
  2642. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2643. break;
  2644. case RFID_SN_TYPE_4BYTE:
  2645. default:
  2646. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2647. break;
  2648. }
  2649. }
  2650. else
  2651. {
  2652. // Little endian
  2653. switch(rfid.snType)
  2654. {
  2655. case RFID_SN_TYPE_8BYTE:
  2656. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2657. break;
  2658. case RFID_SN_TYPE_7BYTE:
  2659. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2660. break;
  2661. case RFID_SN_TYPE_10BYTE:
  2662. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2663. break;
  2664. case RFID_SN_TYPE_4BYTE:
  2665. default:
  2666. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2667. break;
  2668. }
  2669. }
  2670. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2671. }
  2672. //===============================================
  2673. // Read RFID Serial Number
  2674. //===============================================
  2675. int GetCardSerialNumber()
  2676. {
  2677. int isSuccess = FAIL;
  2678. int module_type = MODULE_EWT;
  2679. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2680. {
  2681. if(rfid.cardType == ISO14443A)
  2682. {
  2683. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2684. {
  2685. isSuccess = PASS;
  2686. }
  2687. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2688. {
  2689. isSuccess = PASS;
  2690. }
  2691. sethaltCard(rfidFd,module_type);
  2692. }
  2693. else if(rfid.cardType == IS014443B)
  2694. {
  2695. isSuccess = PASS;
  2696. }
  2697. else if(rfid.cardType == FELICA)
  2698. {
  2699. isSuccess = PASS;
  2700. }
  2701. else
  2702. {}
  2703. }
  2704. else
  2705. {}
  2706. return isSuccess;
  2707. }
  2708. //===============================================
  2709. // Set led motion
  2710. //===============================================
  2711. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2712. {
  2713. switch(led_mode)
  2714. {
  2715. case LED_ACTION_INIT:
  2716. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2717. break;
  2718. case LED_ACTION_IDLE:
  2719. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2720. break;
  2721. case LED_ACTION_AUTHED:
  2722. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2723. break;
  2724. case LED_ACTION_CONNECTED:
  2725. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2726. break;
  2727. case LED_ACTION_CHARGING:
  2728. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2729. break;
  2730. case LED_ACTION_STOP:
  2731. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2732. break;
  2733. case LED_ACTION_ALARM:
  2734. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2735. break;
  2736. case LED_ACTION_MAINTAIN:
  2737. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2738. break;
  2739. case LED_ACTION_RFID_PASS:
  2740. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2741. break;
  2742. case LED_ACTION_RFID_FAIL:
  2743. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2744. break;
  2745. case LED_ACTION_BLE_CONNECT:
  2746. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  2747. break;
  2748. case LED_ACTION_BLE_DISABLE:
  2749. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  2750. break;
  2751. case LED_ACTION_DEBUG:
  2752. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  2753. break;
  2754. case LED_ACTION_ALL_OFF:
  2755. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  2756. break;
  2757. case LED_RELAY_ON:
  2758. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  2759. break;
  2760. case LED_RELAY_OFF:
  2761. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  2762. break;
  2763. case LED_ACTION_HANDSHAKE_FAIL:
  2764. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  2765. break;
  2766. case LED_ACTION_INTERNET_DISCONNECT:
  2767. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  2768. break;
  2769. }
  2770. }
  2771. //===============================================
  2772. // Request on/off set (PWM)
  2773. //===============================================
  2774. void setRequest(unsigned char gun_index,unsigned char isOn)
  2775. {
  2776. if(isOn == ON)
  2777. {
  2778. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  2779. {
  2780. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  2781. DEBUG_INFO("Gun-[%d] permission request: ON \n",gun_index);
  2782. }
  2783. }
  2784. else
  2785. {
  2786. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2787. {
  2788. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  2789. DEBUG_INFO("Gun-[%d] permission request: OFF \n",gun_index);
  2790. }
  2791. }
  2792. }
  2793. //===============================================
  2794. // Request on/off get (PWM)
  2795. //===============================================
  2796. int getRequest(unsigned char gun_index)
  2797. {
  2798. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  2799. }
  2800. //===============================================
  2801. // Relay on/off set (Relay)
  2802. //===============================================
  2803. void setRelay(unsigned char gun_index,unsigned char isOn)
  2804. {
  2805. if(isOn == ON)
  2806. {
  2807. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  2808. {
  2809. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  2810. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  2811. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  2812. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  2813. DEBUG_INFO("Gun-[%d] Output relay status: ON. \n",gun_index);
  2814. }
  2815. }
  2816. else
  2817. {
  2818. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  2819. {
  2820. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  2821. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  2822. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  2823. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  2824. DEBUG_INFO("Gun-[%d] Output relay status: OFF. \n",gun_index);
  2825. }
  2826. }
  2827. }
  2828. //===============================================
  2829. // Relay on/off get (Relay)
  2830. //===============================================
  2831. int getRelay(unsigned char gun_index)
  2832. {
  2833. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  2834. }
  2835. //===============================================
  2836. // Set speaker on/off request
  2837. //===============================================
  2838. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  2839. {
  2840. if(isOn == ON)
  2841. {
  2842. ShmCharger->isSpeakerOn = ON;
  2843. ShmCharger->speaker_type = speaker_mode;
  2844. }
  2845. }
  2846. //===============================================
  2847. // Initialization RFID communication port
  2848. //===============================================
  2849. int InitRfidPort()
  2850. {
  2851. int uartO2 = open(rfidPortName, O_RDWR);
  2852. struct termios tios;
  2853. if (uartO2 != FAIL)
  2854. {
  2855. ioctl (uartO2, TCGETS, &tios);
  2856. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  2857. tios.c_lflag = 0;
  2858. tios.c_iflag = 0;
  2859. tios.c_oflag = 0;
  2860. tios.c_cc[VMIN] = 0;
  2861. tios.c_cc[VTIME] = (unsigned char) 1;
  2862. tios.c_lflag = 0;
  2863. tcflush(uartO2, TCIFLUSH);
  2864. ioctl(uartO2, TCSETS, &tios);
  2865. }
  2866. if (uartO2 < 0)
  2867. {
  2868. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  2869. }
  2870. return uartO2;
  2871. }
  2872. //===============================================
  2873. // Check internet access status
  2874. //===============================================
  2875. int isReachableInternet()
  2876. {
  2877. int result = FAIL;
  2878. FILE *fp;
  2879. char cmd[256];
  2880. char buf[512];
  2881. char tmp[512];
  2882. // Get ip address & net mask
  2883. strcpy(cmd, "ifconfig eth0");
  2884. fp = popen(cmd, "r");
  2885. if(fp != NULL)
  2886. {
  2887. while(fgets(buf, sizeof(buf), fp) != NULL)
  2888. {
  2889. if (strstr(buf, "inet addr:") > 0)
  2890. {
  2891. sscanf(buf, "%*s%s", tmp);
  2892. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  2893. sscanf(buf, "%*s%*s%*s%s", tmp);
  2894. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp, strspn(tmp, "Mask:"), strlen(tmp)-strspn(tmp, "Mask:"));
  2895. }
  2896. }
  2897. }
  2898. pclose(fp);
  2899. memset(buf, 0x00, sizeof(buf));
  2900. // Get gateway
  2901. fp = popen("ip route", "r");
  2902. if(fp == NULL)
  2903. result = FAIL;
  2904. else
  2905. {
  2906. while(fgets(buf, sizeof(buf), fp) != NULL)
  2907. {
  2908. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  2909. break;
  2910. }
  2911. if(strstr(buf, "default") != NULL)
  2912. {
  2913. sscanf(buf, "%*s%*s%s", tmp);
  2914. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  2915. }
  2916. }
  2917. pclose(fp);
  2918. memset(buf, 0x00, sizeof(buf));
  2919. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  2920. {
  2921. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  2922. fp = popen(cmd, "r");
  2923. if(fp != NULL)
  2924. {
  2925. while(fgets(buf, sizeof(buf), fp) != NULL)
  2926. {
  2927. if(strstr(buf, "transmitted") > 0)
  2928. {
  2929. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  2930. if(strstr(buf,"100%") != NULL)
  2931. {
  2932. }
  2933. else
  2934. {
  2935. result = PASS;
  2936. }
  2937. //DEBUG_INFO("%s",buf);
  2938. //DEBUG_INFO("%s\n",tmp);
  2939. }
  2940. }
  2941. }
  2942. pclose(fp);
  2943. }
  2944. return result;
  2945. }
  2946. //===============================================
  2947. // Check route for eth0
  2948. //===============================================
  2949. int isRouteFail()
  2950. {
  2951. int result = YES;
  2952. FILE *fp;
  2953. char buf[512];
  2954. fp = popen("route -n", "r");
  2955. if(fp != NULL)
  2956. {
  2957. while(fgets(buf, sizeof(buf), fp) != NULL)
  2958. {
  2959. if(strstr(buf, "eth0") != NULL)
  2960. result = NO;
  2961. }
  2962. }
  2963. pclose(fp);
  2964. return result;
  2965. }
  2966. //===============================================
  2967. // Check reservation date is expired
  2968. //===============================================
  2969. int isReservationExpired(unsigned char gun_index)
  2970. {
  2971. int result = NO;
  2972. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2973. {
  2974. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  2975. result = YES;
  2976. }
  2977. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2978. {
  2979. if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  2980. result = YES;
  2981. }
  2982. return result;
  2983. }
  2984. //===============================================
  2985. // Check charging profile related date routine
  2986. //===============================================
  2987. int isProfileValid(uint8_t gun_index)
  2988. {
  2989. int result = NO;
  2990. struct tm tmFrom, tmTo;
  2991. struct timeb tbFrom, tbTo;
  2992. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2993. {
  2994. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  2995. (sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  2996. {
  2997. tmFrom.tm_year -= 1900;
  2998. tmFrom.tm_mon -= 1;
  2999. tbFrom.time = mktime(&tmFrom);
  3000. tmTo.tm_year -= 1900;
  3001. tmTo.tm_mon -= 1;
  3002. tbTo.time = mktime(&tmTo);
  3003. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3004. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3005. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3006. {
  3007. result = YES;
  3008. }
  3009. }
  3010. else
  3011. {
  3012. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3013. }
  3014. }
  3015. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3016. {
  3017. if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  3018. (sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  3019. {
  3020. tmFrom.tm_year -= 1900;
  3021. tmFrom.tm_mon -= 1;
  3022. tbFrom.time = mktime(&tmFrom);
  3023. tmTo.tm_year -= 1900;
  3024. tmTo.tm_mon -= 1;
  3025. tbTo.time = mktime(&tmTo);
  3026. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3027. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3028. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3029. {
  3030. result = YES;
  3031. }
  3032. }
  3033. else
  3034. {
  3035. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3036. }
  3037. }
  3038. return result;
  3039. }
  3040. int getMaxScheduleStart()
  3041. {
  3042. int result = -1;
  3043. struct tm tmScheduleStart;;
  3044. struct timeb tbScheduleStart;
  3045. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3046. {
  3047. if((sscanf((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule, "%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))
  3048. {
  3049. tmScheduleStart.tm_year -= 1900;
  3050. tmScheduleStart.tm_mon -= 1;
  3051. tbScheduleStart.time = mktime(&tmScheduleStart);
  3052. tbScheduleStart.millitm = 0;
  3053. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3054. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3055. }
  3056. else
  3057. {
  3058. DEBUG_WARN("Max schedule start date parsing error.\n");
  3059. }
  3060. }
  3061. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3062. {
  3063. if((sscanf((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule, "%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))
  3064. {
  3065. tmScheduleStart.tm_year -= 1900;
  3066. tmScheduleStart.tm_mon -= 1;
  3067. tbScheduleStart.time = mktime(&tmScheduleStart);
  3068. tbScheduleStart.millitm = 0;
  3069. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3070. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3071. }
  3072. else
  3073. {
  3074. DEBUG_WARN("Max schedule start date parsing error.\n");
  3075. }
  3076. }
  3077. return result;
  3078. }
  3079. int getScheduleStart(int gun_index)
  3080. {
  3081. int result = -1;
  3082. struct tm tmScheduleStart;;
  3083. struct timeb tbScheduleStart;
  3084. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3085. {
  3086. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%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))
  3087. {
  3088. tmScheduleStart.tm_year -= 1900;
  3089. tmScheduleStart.tm_mon -= 1;
  3090. tbScheduleStart.time = mktime(&tmScheduleStart);
  3091. tbScheduleStart.millitm = 0;
  3092. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3093. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3094. }
  3095. else
  3096. {
  3097. DEBUG_WARN("Schedule start date parsing error.\n");
  3098. }
  3099. }
  3100. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3101. {
  3102. if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule, "%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))
  3103. {
  3104. tmScheduleStart.tm_year -= 1900;
  3105. tmScheduleStart.tm_mon -= 1;
  3106. tbScheduleStart.time = mktime(&tmScheduleStart);
  3107. tbScheduleStart.millitm = 0;
  3108. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3109. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3110. }
  3111. else
  3112. {
  3113. DEBUG_WARN("Schedule start date parsing error.\n");
  3114. }
  3115. }
  3116. return result;
  3117. }
  3118. int getStartSinceToday()
  3119. {
  3120. int result = -1;
  3121. time_t t;
  3122. struct tm *tmStartToday;
  3123. struct timeb tbStartToday;
  3124. t=time(NULL);
  3125. tmStartToday=localtime(&t);
  3126. tmStartToday->tm_hour = 0;
  3127. tmStartToday->tm_min = 0;
  3128. tmStartToday->tm_sec = 0;
  3129. tbStartToday.time = mktime(tmStartToday);
  3130. result = DiffTimebWithNow(tbStartToday)/1000;
  3131. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  3132. return result;
  3133. }
  3134. int getStartSinceWeek()
  3135. {
  3136. int result = -1;
  3137. time_t t;
  3138. struct tm *tmStartWeek;
  3139. struct timeb tbStartWeek;
  3140. t=time(NULL);
  3141. tmStartWeek=localtime(&t);
  3142. t-=86400*tmStartWeek->tm_wday;
  3143. tmStartWeek=localtime(&t);
  3144. tmStartWeek->tm_hour = 0;
  3145. tmStartWeek->tm_min = 0;
  3146. tmStartWeek->tm_sec = 0;
  3147. tbStartWeek.time = mktime(tmStartWeek);
  3148. result = DiffTimebWithNow(tbStartWeek)/1000;
  3149. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  3150. return result;
  3151. }
  3152. //===============================================
  3153. // Valid from local white list
  3154. //===============================================
  3155. int isValidLocalWhiteCard()
  3156. {
  3157. uint8_t result = FAIL;
  3158. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  3159. {
  3160. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  3161. {
  3162. result = PASS;
  3163. }
  3164. }
  3165. return result;
  3166. }
  3167. //==========================================
  3168. // Check routine
  3169. //==========================================
  3170. void checkTask()
  3171. {
  3172. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3173. {
  3174. if(system("pidof -s Module_4g > /dev/null") != 0)
  3175. {
  3176. DEBUG_INFO("Module_4g not running, restart it.\n");
  3177. system("/root/Module_4g &");
  3178. }
  3179. }
  3180. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3181. {
  3182. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  3183. {
  3184. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  3185. system("/root/Module_Wifi &");
  3186. }
  3187. }
  3188. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  3189. {
  3190. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  3191. system("/root/Module_EventLogging &");
  3192. }
  3193. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  3194. {
  3195. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3196. {
  3197. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  3198. {
  3199. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  3200. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3201. system("pkill OcppBackend");
  3202. sleep(3);
  3203. ocpp_process_start();
  3204. }
  3205. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3206. {
  3207. DEBUG_INFO("OcppBackend not running, restart it.\n");
  3208. ocpp_process_start();
  3209. }
  3210. }
  3211. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3212. {
  3213. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  3214. {
  3215. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  3216. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3217. system("pkill OcppBackend20");
  3218. sleep(3);
  3219. ocpp_process_start();
  3220. }
  3221. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3222. {
  3223. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  3224. ocpp_process_start();
  3225. }
  3226. }
  3227. }
  3228. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  3229. {
  3230. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3231. {
  3232. DEBUG_INFO("OcppBackendPH not running, restart it.\n");
  3233. system("/root/OcppBackendPH &");
  3234. }
  3235. }
  3236. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  3237. {
  3238. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  3239. system("/root/Module_AlarmDetect &");
  3240. }
  3241. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  3242. {
  3243. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  3244. system("/root/Module_InternalComm &");
  3245. }
  3246. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  3247. {
  3248. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  3249. system("/root/Module_Speaker &");
  3250. }
  3251. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  3252. {
  3253. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  3254. system ("/root/Module_ProduceUtils &");
  3255. }
  3256. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  3257. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  3258. {
  3259. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  3260. system ("/root/Module_PowerSharing &");
  3261. }
  3262. if((system("pidof -s Module_Firewall > /dev/null") != 0) &&
  3263. ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
  3264. {
  3265. DEBUG_INFO("Module_Firewall not running, restart it.\n");
  3266. system ("/root/Module_Firewall &");
  3267. }
  3268. }
  3269. void checkConnectionTimeout()
  3270. {
  3271. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3272. {
  3273. if(ShmCharger->timeoutSpec.Present_Timeout_Spec != TIMEOUT_SPEC_HANDSHAKING)
  3274. {
  3275. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3276. DEBUG_INFO("Configure connection timeout following by default setting: [ %d ]. \n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  3277. }
  3278. }
  3279. else
  3280. {
  3281. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  3282. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  3283. {
  3284. if(ShmCharger->timeoutSpec.Present_Timeout_Spec != TIMEOUT_SPEC_HANDSHAKING)
  3285. {
  3286. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3287. DEBUG_INFO("Configure connection timeout following by OCPP: [ %d ]. \n", ShmCharger->timeoutSpec.Setting_Timeout_Spec);
  3288. DEBUG_INFO("Connection timeout can not be zero. Changing to the default setting: [ %d ]. \n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  3289. }
  3290. }
  3291. else
  3292. {
  3293. if(ShmCharger->timeoutSpec.Present_Timeout_Spec != ShmCharger->timeoutSpec.Setting_Timeout_Spec)
  3294. {
  3295. ShmCharger->timeoutSpec.Present_Timeout_Spec = ShmCharger->timeoutSpec.Setting_Timeout_Spec;
  3296. DEBUG_INFO("Configure connection timeout following by OCPP: [ %d ]. \n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  3297. }
  3298. }
  3299. }
  3300. }
  3301. void checkReset()
  3302. {
  3303. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3304. {
  3305. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3306. {
  3307. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3308. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
  3309. {
  3310. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3311. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3312. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3313. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3314. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3315. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3316. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3317. {
  3318. system("sync");
  3319. sleep(10);
  3320. system("reboot -f");
  3321. sleep(10);
  3322. system("reboot -f");
  3323. }
  3324. else
  3325. {
  3326. sleep(10);
  3327. close(wtdFd);
  3328. system("/usr/bin/run_evse_restart.sh");
  3329. }
  3330. }
  3331. }
  3332. }
  3333. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3334. {
  3335. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3336. {
  3337. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3338. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(1, SYS_MODE_COMPLETE)):TRUE))
  3339. {
  3340. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3341. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  3342. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3343. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3344. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3345. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3346. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3347. {
  3348. system("sync");
  3349. sleep(10);
  3350. system("reboot -f");
  3351. sleep(10);
  3352. system("reboot -f");
  3353. }
  3354. else
  3355. {
  3356. sleep(10);
  3357. close(wtdFd);
  3358. system("/usr/bin/run_evse_restart.sh");
  3359. }
  3360. }
  3361. }
  3362. }
  3363. }
  3364. void checkReservation(uint8_t gun_index)
  3365. {
  3366. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3367. {
  3368. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3369. {
  3370. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3371. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3372. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3373. {
  3374. sleep(5);
  3375. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3376. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3377. }
  3378. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  3379. }
  3380. }
  3381. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3382. {
  3383. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3384. {
  3385. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3386. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3387. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3388. {
  3389. sleep(5);
  3390. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3391. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3392. }
  3393. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  3394. }
  3395. }
  3396. }
  3397. void checkUnlocker(uint8_t gun_index)
  3398. {
  3399. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3400. {
  3401. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3402. {
  3403. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3404. if(ShmSysConfigAndInfo->SysConfig.ModelName[9-gun_index] == '4')
  3405. {
  3406. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "Unlocked");
  3407. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  3408. }
  3409. else
  3410. {
  3411. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  3412. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  3413. }
  3414. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  3415. }
  3416. }
  3417. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3418. {
  3419. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3420. {
  3421. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3422. if(ShmSysConfigAndInfo->SysConfig.ModelName[9-gun_index] == '4')
  3423. {
  3424. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING)
  3425. {
  3426. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "OngoingAuthorizedTransaction");
  3427. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = OFF;
  3428. }
  3429. else
  3430. {
  3431. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "Unlocked");
  3432. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  3433. }
  3434. }
  3435. else
  3436. {
  3437. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "UnlockFailed");
  3438. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = OFF;
  3439. }
  3440. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  3441. }
  3442. }
  3443. }
  3444. void checkAvailability(uint8_t gun_index)
  3445. {
  3446. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3447. {
  3448. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3449. {
  3450. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  3451. {
  3452. DB_Update_Operactive(localDb, gun_index, true);
  3453. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3454. }
  3455. else
  3456. {
  3457. DB_Update_Operactive(localDb, gun_index, false);
  3458. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3459. }
  3460. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3461. }
  3462. }
  3463. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3464. {
  3465. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3466. {
  3467. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  3468. {
  3469. DB_Update_Operactive(localDb, gun_index, true);
  3470. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3471. }
  3472. else
  3473. {
  3474. DB_Update_Operactive(localDb, gun_index, false);
  3475. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3476. }
  3477. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3478. }
  3479. }
  3480. }
  3481. void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
  3482. {
  3483. uint16_t MaxChargingProfileChargingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3484. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3485. {
  3486. //========================================
  3487. // Check TxProfile and TxDefaultProfile
  3488. //========================================
  3489. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3490. {
  3491. // Checking profile kind
  3492. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  3493. {
  3494. // Checking limitation
  3495. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3496. {
  3497. if((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3498. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3499. )
  3500. {
  3501. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  3502. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3503. }
  3504. else
  3505. break;
  3506. }
  3507. }
  3508. }
  3509. else
  3510. {
  3511. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3512. }
  3513. //========================================
  3514. // Check ChargingPointMaxProfile
  3515. //========================================
  3516. if((ocpp_get_maxcharging_profileId() > 0))
  3517. {
  3518. // Absolute profile
  3519. if((mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == PASS))
  3520. {
  3521. // Checking limitation
  3522. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3523. {
  3524. if((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3525. ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3526. )
  3527. {
  3528. MaxChargingProfileChargingCurrent = (mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  3529. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3530. }
  3531. else
  3532. break;
  3533. }
  3534. }
  3535. }
  3536. // Compare target current is over max current for EVSE limit
  3537. if(gun_index == 0)
  3538. {
  3539. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3540. {
  3541. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3542. }
  3543. }
  3544. else
  3545. {
  3546. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3547. {
  3548. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3549. }
  3550. }
  3551. }
  3552. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3553. {
  3554. //========================================
  3555. // Check TxProfile and TxDefaultProfile
  3556. //========================================
  3557. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3558. {
  3559. // Checking profile kind
  3560. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  3561. {
  3562. // Checking limitation
  3563. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3564. {
  3565. if((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3566. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3567. )
  3568. {
  3569. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
  3570. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3571. }
  3572. else
  3573. break;
  3574. }
  3575. }
  3576. }
  3577. else
  3578. {
  3579. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3580. }
  3581. //========================================
  3582. // Check ChargingPointMaxProfile
  3583. //========================================
  3584. if((ocpp_get_maxcharging_profileId() > 0))
  3585. {
  3586. // Absolute profile
  3587. if((mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, "Absolute") == PASS))
  3588. {
  3589. // Checking limitation
  3590. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3591. {
  3592. if((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3593. ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3594. )
  3595. {
  3596. MaxChargingProfileChargingCurrent = (mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
  3597. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3598. }
  3599. else
  3600. break;
  3601. }
  3602. }
  3603. }
  3604. // Compare target current is over max current for EVSE limit
  3605. if(gun_index == 0)
  3606. {
  3607. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3608. {
  3609. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3610. }
  3611. }
  3612. else
  3613. {
  3614. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3615. {
  3616. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3617. }
  3618. }
  3619. }
  3620. // Charging session target current check if OCPP disconnect and power sharing server connected
  3621. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  3622. {
  3623. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  3624. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index]>ShmCharger->gun_info[gun_index].primaryMcuState.rating_current?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index];
  3625. else
  3626. {
  3627. if(ShmCharger->gun_info[gun_index].targetCurrent != 0)
  3628. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  3629. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  3630. }
  3631. }
  3632. switch(system_mode)
  3633. {
  3634. case SYS_MODE_IDLE:
  3635. case SYS_MODE_PREPARING:
  3636. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3637. {
  3638. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3639. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3640. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3641. }
  3642. else
  3643. {
  3644. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3645. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3646. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3647. }
  3648. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3649. break;
  3650. case SYS_MODE_CHARGING:
  3651. case SYS_MODE_TERMINATING:
  3652. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3653. {
  3654. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3655. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  3656. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  3657. {
  3658. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3659. {
  3660. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3661. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3662. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3663. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3664. }
  3665. else
  3666. {}
  3667. }
  3668. }
  3669. else
  3670. {
  3671. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3672. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  3673. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  3674. {
  3675. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3676. {
  3677. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3678. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3679. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3680. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3681. }
  3682. else
  3683. {}
  3684. }
  3685. }
  3686. if(ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  3687. {
  3688. DEBUG_INFO("====================================================\n");
  3689. DEBUG_INFO("MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  3690. DEBUG_INFO("gun-%02d targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  3691. DEBUG_INFO("====================================================\n");
  3692. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  3693. }
  3694. if(previousData[gun_index].primaryMcuCp_Pwn_Duty != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  3695. {
  3696. DEBUG_INFO("====================================================\n");
  3697. DEBUG_INFO("gun-%02d primaryMcuCp_Pwn_Duty: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  3698. DEBUG_INFO("====================================================\n");
  3699. previousData[gun_index].primaryMcuCp_Pwn_Duty = ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current;
  3700. }
  3701. if(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  3702. {
  3703. DEBUG_INFO("====================================================\n");
  3704. DEBUG_INFO("gun-%02d mcu current_limit: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  3705. DEBUG_INFO("====================================================\n");
  3706. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  3707. }
  3708. break;
  3709. }
  3710. }
  3711. void checkStopReason(uint8_t gun_index)
  3712. {
  3713. sleep(2);
  3714. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3715. {
  3716. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3717. if(!ocpp_get_starttransaction_result(gun_index))
  3718. {
  3719. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  3720. }
  3721. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3722. {
  3723. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  3724. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3725. }
  3726. else if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)) && (strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") != NULL))
  3727. {
  3728. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3729. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3730. //memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3731. }
  3732. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3733. {
  3734. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3735. {
  3736. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3737. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3738. }
  3739. else
  3740. {
  3741. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3742. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3743. }
  3744. }
  3745. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3746. {
  3747. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3748. if(!isMatchStartUser(gun_index))
  3749. {
  3750. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3751. }
  3752. else
  3753. {
  3754. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3755. }
  3756. DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  3757. }
  3758. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3759. {
  3760. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3761. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3762. }
  3763. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3764. {
  3765. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3766. }
  3767. else
  3768. {
  3769. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3770. }
  3771. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3772. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3773. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  3774. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3775. }
  3776. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3777. {
  3778. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3779. if(!ocpp_get_starttransaction_result(gun_index))
  3780. {
  3781. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  3782. }
  3783. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3784. {
  3785. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  3786. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3787. }
  3788. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  3789. {
  3790. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  3791. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3792. }
  3793. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  3794. {
  3795. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  3796. }
  3797. else if((ShmOCPP20Data->MsMsg.bits.ResetReq) && (strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0))
  3798. {
  3799. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  3800. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3801. }
  3802. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3803. {
  3804. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  3805. if(!isMatchStartUser(gun_index))
  3806. {
  3807. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  3808. }
  3809. else
  3810. {
  3811. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3812. }
  3813. DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  3814. }
  3815. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_OVER_CURRENT))
  3816. {
  3817. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  3818. }
  3819. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  3820. {
  3821. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  3822. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  3823. }
  3824. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  3825. {
  3826. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  3827. }
  3828. else
  3829. {
  3830. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  3831. }
  3832. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3833. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3834. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  3835. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  3836. }
  3837. // Maintain server
  3838. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0)
  3839. {
  3840. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  3841. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].IdTag, ShmOCPP16Data->StopTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3842. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3843. }
  3844. }
  3845. void checkRemoteUpgradeStatus()
  3846. {
  3847. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3848. {
  3849. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3850. {
  3851. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3852. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3853. ShmCharger->isUpdateSuccess = NO;
  3854. }
  3855. else if((strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0))
  3856. {
  3857. DEBUG_INFO("Firmware remote upgrading...\n");
  3858. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3859. sleep(1);
  3860. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3861. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3862. int result = upgrade_check();
  3863. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3864. if(result == PASS)
  3865. {
  3866. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3867. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3868. ShmCharger->isUpdateSuccess = YES;
  3869. DEBUG_INFO("Remote update success...\n");
  3870. }
  3871. else
  3872. {
  3873. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3874. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3875. ShmCharger->isUpdateSuccess = NO;
  3876. DEBUG_INFO("Remote update unsuccess...\n");
  3877. }
  3878. }
  3879. else if((strcmp((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "SignatureVerified")==0))
  3880. {
  3881. DEBUG_INFO("Signature firmware remote upgrading...\n");
  3882. ShmOCPP16Data->SignedFirmwareStatusNotification.requestId = ShmOCPP16Data->SignedUpdateFirmware.requestId;
  3883. sprintf((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installing");
  3884. sleep(1);
  3885. ShmOCPP16Data->SpMsg.bits.SignedFirmwareStatusNotificationReq = ON;
  3886. ShmOCPP16Data->MsMsg.bits.SignedUpdateFirmwareReq = OFF;
  3887. if(upgrade_check() == PASS)
  3888. {
  3889. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3890. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3891. ShmCharger->isUpdateSuccess = YES;
  3892. DEBUG_INFO("Remote update success.\n");
  3893. }
  3894. else
  3895. {
  3896. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3897. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3898. ShmCharger->isUpdateSuccess = NO;
  3899. DEBUG_INFO("Remote update fail.\n");
  3900. }
  3901. }
  3902. else
  3903. {}
  3904. }
  3905. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3906. {
  3907. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  3908. {
  3909. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3910. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3911. ShmCharger->isUpdateSuccess = NO;
  3912. }
  3913. else if((strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0))
  3914. {
  3915. DEBUG_INFO("Firmware remote upgrading...\n");
  3916. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3917. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3918. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3919. int result = upgrade_check();
  3920. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3921. if(result == PASS)
  3922. {
  3923. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3924. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3925. ShmCharger->isUpdateSuccess = YES;
  3926. DEBUG_INFO("Remote update success...\n");
  3927. }
  3928. else
  3929. {
  3930. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3931. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3932. ShmCharger->isUpdateSuccess = NO;
  3933. DEBUG_INFO("Remote update unsuccess...\n");
  3934. }
  3935. }
  3936. else
  3937. {}
  3938. }
  3939. }
  3940. void getUserId()
  3941. {
  3942. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3943. {
  3944. // Big endian
  3945. switch(rfid.snType)
  3946. {
  3947. case RFID_SN_TYPE_8BYTE:
  3948. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7]);
  3949. break;
  3950. case RFID_SN_TYPE_7BYTE:
  3951. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  3952. break;
  3953. case RFID_SN_TYPE_10BYTE:
  3954. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  3955. break;
  3956. case RFID_SN_TYPE_4BYTE:
  3957. default:
  3958. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3959. break;
  3960. }
  3961. }
  3962. else
  3963. {
  3964. // Little endian
  3965. switch(rfid.snType)
  3966. {
  3967. case RFID_SN_TYPE_8BYTE:
  3968. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3969. break;
  3970. case RFID_SN_TYPE_7BYTE:
  3971. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3972. break;
  3973. case RFID_SN_TYPE_10BYTE:
  3974. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3975. break;
  3976. case RFID_SN_TYPE_4BYTE:
  3977. default:
  3978. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3979. break;
  3980. }
  3981. }
  3982. }
  3983. //===============================================
  3984. // Main process
  3985. //===============================================
  3986. int main(void)
  3987. {
  3988. //==================================================
  3989. // Create all share memory
  3990. //==================================================
  3991. if(CreatShareMemory()==0)
  3992. {
  3993. DEBUG_ERROR("CreatShareMemory NG\n");
  3994. if(ShmStatusCodeData!=NULL)
  3995. {
  3996. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3997. }
  3998. sleep(5);
  3999. system("reboot -f");
  4000. sleep(5);
  4001. system("reboot -f");
  4002. }
  4003. else
  4004. {
  4005. DEBUG_INFO("CreatShareMemory OK\n");
  4006. }
  4007. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4008. {
  4009. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  4010. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  4011. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  4012. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  4013. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  4014. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  4015. }
  4016. //==================================================
  4017. // Main loop
  4018. //==================================================
  4019. for(;;)
  4020. {
  4021. //==============================================
  4022. // Synchronize share memory from OCPP struct
  4023. //==============================================
  4024. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  4025. //==============================================
  4026. // Period check for 10 seconds
  4027. //==============================================
  4028. if((getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10) || (getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  4029. {
  4030. //==============================================
  4031. // Check task processing
  4032. //==============================================
  4033. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4034. checkTask();
  4035. //==============================================
  4036. // Check connection timeout specification
  4037. //==============================================
  4038. checkConnectionTimeout();
  4039. refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
  4040. }
  4041. //==============================================
  4042. // Something need run in Idle mode
  4043. //==============================================
  4044. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_DEBUG)) &&
  4045. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_DEBUG)):true))
  4046. {
  4047. //======================================
  4048. // Check restore factory setting request
  4049. //======================================
  4050. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4051. {
  4052. // Set led to initial
  4053. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4054. {
  4055. setLedMotion(gun_index,LED_ACTION_INIT);
  4056. }
  4057. sleep(5);
  4058. system("cd /root;./Module_FactoryConfig -m");
  4059. // OCPP 1.6 Configuration key
  4060. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4061. // OCPP 2.O Configuration variable
  4062. DB_variableClear(ocppDb);
  4063. DEBUG_INFO("Default configuration to factory setting. \n");
  4064. system("sync");
  4065. sleep(5);
  4066. system("reboot -f");
  4067. sleep(5);
  4068. system("reboot -f");
  4069. }
  4070. //======================================
  4071. // Check upgrade firmware request
  4072. //======================================
  4073. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  4074. ocpp_get_update_firmware_req())
  4075. {
  4076. // If available memory too small, free memory cache first
  4077. if(getAvailableMemory() < (200*1024*1024))
  4078. {
  4079. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  4080. system("echo 3 > /proc/sys/vm/drop_caches");
  4081. }
  4082. ShmCharger->isUpdateSuccess = NO;
  4083. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4084. {
  4085. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4086. setChargerMode(gun_index, SYS_MODE_UPDATE);
  4087. }
  4088. }
  4089. }
  4090. //==============================================
  4091. // Check remote reset request
  4092. //==============================================
  4093. checkReset();
  4094. //==============================================
  4095. // Check RFID authorization
  4096. //==============================================
  4097. if((!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq) && (ShmSysConfigAndInfo->SysConfig.AuthorisationMode != AUTH_MODE_DISABLE))
  4098. {
  4099. if(GetCardSerialNumber()!= FAIL)
  4100. {
  4101. if(isRfidReadable)
  4102. {
  4103. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  4104. isRfidReadable = NO;
  4105. }
  4106. }
  4107. else
  4108. {
  4109. isRfidReadable = YES;
  4110. }
  4111. }
  4112. //==============================================
  4113. // Check Local Power Sharing Communication
  4114. //==============================================
  4115. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  4116. {
  4117. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  4118. {
  4119. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
  4120. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  4121. }
  4122. else
  4123. {
  4124. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
  4125. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
  4126. }
  4127. }
  4128. //==============================================
  4129. // Connector loop
  4130. //==============================================
  4131. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4132. {
  4133. // Synchronize current rating value from MCU
  4134. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4135. // Assign connector location index for OCPP
  4136. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  4137. // Synchronize present charging power
  4138. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4139. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  4140. else
  4141. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4142. // Check initialization "PASS" or "FAIL"
  4143. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4144. {
  4145. // Alarm event check
  4146. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  4147. {
  4148. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  4149. {
  4150. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  4151. {
  4152. setChargerMode(gun_index, SYS_MODE_ALARM);
  4153. }
  4154. }
  4155. }
  4156. }
  4157. //==========================================
  4158. // Reservation request check
  4159. //==========================================
  4160. checkReservation(gun_index);
  4161. //==========================================
  4162. // Change availability check
  4163. //==========================================
  4164. checkAvailability(gun_index);
  4165. if(ShmCharger->gun_info[gun_index].isOperactive)
  4166. {
  4167. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  4168. {
  4169. setChargerMode(gun_index, SYS_MODE_IDLE);
  4170. }
  4171. }
  4172. else
  4173. {
  4174. if(isMode(gun_index, SYS_MODE_IDLE))
  4175. {
  4176. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  4177. }
  4178. }
  4179. //==========================================
  4180. // Unlock Connector signal check
  4181. //==========================================
  4182. checkUnlocker(gun_index);
  4183. //==========================================
  4184. // Synchronized total power consumption
  4185. //==========================================
  4186. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  4187. //==========================================
  4188. // Connector process
  4189. //==========================================
  4190. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  4191. {
  4192. case SYS_MODE_BOOTING:
  4193. if(isModeChange(gun_index))
  4194. {
  4195. setLedMotion(gun_index,LED_ACTION_INIT);
  4196. //CSU Initialization & task spawn
  4197. if((Initialization() != PASS) ||
  4198. (SpawnTask() != PASS))
  4199. {
  4200. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  4201. }
  4202. // Firmware version
  4203. get_firmware_version(gun_index);
  4204. }
  4205. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  4206. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  4207. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  4208. {
  4209. ShmCharger->gun_selectd = 0;
  4210. // Firmware version
  4211. get_firmware_version(gun_index);
  4212. // Change availability check
  4213. checkAvailability(gun_index);
  4214. // OCPP BootNotification info set
  4215. DEBUG_INFO("==========================================\n");
  4216. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  4217. DEBUG_INFO("==========================================\n");
  4218. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  4219. ocpp_boot_info_sync();
  4220. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  4221. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  4222. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  4223. DEBUG_INFO("==========================================\n");
  4224. DEBUG_INFO("========== Set Wifi information ==========\n");
  4225. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  4226. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  4227. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  4228. DEBUG_INFO("==========================================\n");
  4229. // Set max current to rating current
  4230. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4231. // Default Ethernet / Wifi / 4G to 1:disconnected
  4232. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4233. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4234. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4235. // If Web Server OPCC URL is empty kill Module_OcppBackend
  4236. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4237. {
  4238. DEBUG_INFO("[SYS_MODE_BOOTING] URL is empty kill Module_OcppBackend...\n");
  4239. system("pkill OcppBackend");
  4240. }
  4241. else
  4242. {
  4243. DEBUG_INFO("[SYS_MODE_BOOTING] Initialization: Kill OcppBackend task. \n");
  4244. system ("pkill OcppBackend");
  4245. }
  4246. // If rotate switch equal zero, the system needs to change Debug mode
  4247. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  4248. {
  4249. setChargerMode(gun_index, SYS_MODE_DEBUG);
  4250. }
  4251. else
  4252. {
  4253. // Check is operactive
  4254. if(ShmCharger->gun_info[gun_index].isOperactive)
  4255. {
  4256. setChargerMode(gun_index, SYS_MODE_IDLE);
  4257. }
  4258. else
  4259. {
  4260. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  4261. }
  4262. }
  4263. DEBUG_INFO("[SYS_MODE_BOOTING] System memory power consumption: %.4f \n", (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0));
  4264. DEBUG_INFO("[SYS_MODE_BOOTING] Share memory power consumption: %.4f \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption);
  4265. }
  4266. break;
  4267. case SYS_MODE_IDLE:
  4268. if(isModeChange(gun_index))
  4269. {
  4270. setLedMotion(gun_index,LED_ACTION_IDLE);
  4271. setRelay(gun_index,OFF);
  4272. setRequest(gun_index,OFF);
  4273. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  4274. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4275. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4276. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4277. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4278. ocpp_set_remotestart(gun_index, OFF);
  4279. ocpp_set_remotestop(gun_index, OFF);
  4280. ocpp_set_auth_conf(OFF);
  4281. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  4282. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  4283. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  4284. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  4285. ocpp_set_unlocker_req(gun_index, OFF);
  4286. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  4287. // Response StopTransactionConf
  4288. ocpp_set_stoptransaction_conf(gun_index, OFF);
  4289. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4290. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4291. previousData[gun_index].primaryMcuCp_Pwn_Duty = 0;
  4292. previousData[gun_index].targetCurrent = 0;
  4293. previousData[gun_index].current_limit = 0;
  4294. }
  4295. if(ocpp_get_isRemoteStartWait())
  4296. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  4297. else
  4298. setLedMotion(gun_index,LED_ACTION_IDLE);
  4299. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  4300. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  4301. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4302. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  4303. (ocpp_get_remotestart(gun_index) == ON) ||
  4304. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  4305. {
  4306. // Clean User id & Card Number
  4307. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4308. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4309. {
  4310. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4311. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  4312. DEBUG_INFO("[SYS_MODE_IDLE] Start Method: RFID...\n");
  4313. getUserId();
  4314. DEBUG_INFO("[SYS_MODE_IDLE] Start request UserId: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4315. }
  4316. else if(ocpp_get_remotestart(gun_index))
  4317. {
  4318. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4319. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  4320. ocpp_copy_userid_from_remotestart(gun_index);
  4321. DEBUG_INFO("[SYS_MODE_IDLE] Start Method: BACKEND...\n");
  4322. }
  4323. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  4324. {
  4325. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  4326. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Local;
  4327. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  4328. DEBUG_INFO("[SYS_MODE_IDLE] Start Method: BLE...\n");
  4329. }
  4330. else
  4331. {
  4332. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  4333. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_NoAuthorization;
  4334. //memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  4335. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  4336. DEBUG_INFO("[SYS_MODE_IDLE] Start Method: FREE...\n");
  4337. }
  4338. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4339. ocpp_set_remotestart(gun_index, OFF);
  4340. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4341. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  4342. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4343. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  4344. // Get target current
  4345. ocpp_set_profile_req(gun_index, ON);
  4346. sleep(1);
  4347. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4348. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4349. }
  4350. else
  4351. {}
  4352. break;
  4353. case SYS_MODE_AUTHORIZING:
  4354. if(isModeChange(gun_index))
  4355. {
  4356. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  4357. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  4358. {
  4359. DEBUG_INFO("[SYS_MODE_AUTHORIZING] Rfid start request authorize.\n");
  4360. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4361. {
  4362. case AUTH_MODE_ENABLE:
  4363. if(ocpp_get_connection_status())
  4364. {
  4365. // On line
  4366. ocpp_set_auth_req(ON, "ISO14443");
  4367. }
  4368. else
  4369. {
  4370. // Off line
  4371. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4372. {
  4373. case OFF_POLICY_LOCALLIST:
  4374. ocpp_set_auth_req(ON, "ISO14443");
  4375. break;
  4376. case OFF_POLICY_PH_RFID:
  4377. break;
  4378. case OFF_POLICY_FREE:
  4379. break;
  4380. case OFF_POLICY_NOCHARGE:
  4381. default:
  4382. break;
  4383. }
  4384. }
  4385. break;
  4386. case AUTH_MODE_DISABLE:
  4387. default:
  4388. break;
  4389. }
  4390. }
  4391. else if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
  4392. {
  4393. DEBUG_INFO("[SYS_MODE_AUTHORIZING] Remote start request authorize.\n");
  4394. ocpp_set_auth_req(ON, "ISO14443");
  4395. }
  4396. }
  4397. if(getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  4398. {
  4399. // Authorization timeout process.
  4400. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4401. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4402. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4403. sleep(3);
  4404. setChargerMode(gun_index, SYS_MODE_IDLE);
  4405. DEBUG_WARN("[SYS_MODE_AUTHORIZING] Authorize timeout !!!\n");
  4406. }
  4407. else
  4408. {
  4409. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  4410. {
  4411. case START_METHOD_RFID:
  4412. if((ocpp_get_auth_conf()) ||
  4413. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  4414. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > 2)))
  4415. {
  4416. if(ocpp_get_auth_result(gun_index) ||
  4417. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  4418. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4419. {
  4420. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4421. DEBUG_INFO("[SYS_MODE_AUTHORIZING] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  4422. DEBUG_INFO("[SYS_MODE_AUTHORIZING] Authorize pass.\n");
  4423. setSpeaker(ON,SPEAKER_SHORT);
  4424. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4425. sleep(3);
  4426. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4427. }
  4428. else
  4429. {
  4430. DEBUG_INFO("[SYS_MODE_AUTHORIZING] Authorize fail.\n");
  4431. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4432. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4433. sleep(3);
  4434. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1)
  4435. setChargerMode(gun_index, SYS_MODE_IDLE);
  4436. else
  4437. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  4438. }
  4439. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4440. ocpp_set_auth_conf(OFF);
  4441. }
  4442. break;
  4443. case START_METHOD_BACKEND:
  4444. if(ocpp_isAuthorizeRemoteStart())
  4445. {
  4446. if(ocpp_get_auth_conf())
  4447. {
  4448. if(ocpp_get_auth_result(gun_index))
  4449. {
  4450. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4451. DEBUG_INFO("[SYS_MODE_AUTHORIZING] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  4452. DEBUG_INFO("[SYS_MODE_AUTHORIZING] Authorize pass.\n");
  4453. setSpeaker(ON,SPEAKER_SHORT);
  4454. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4455. sleep(3);
  4456. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4457. }
  4458. else
  4459. {
  4460. DEBUG_INFO("[SYS_MODE_AUTHORIZING] Authorize fail.\n");
  4461. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4462. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4463. sleep(3);
  4464. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId == -1)
  4465. setChargerMode(gun_index, SYS_MODE_IDLE);
  4466. else
  4467. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  4468. }
  4469. ocpp_set_auth_conf(OFF);
  4470. }
  4471. }
  4472. else
  4473. {
  4474. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4475. DEBUG_INFO("[SYS_MODE_AUTHORIZING] StartUserId: %s \n",ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  4476. setSpeaker(ON,SPEAKER_SHORT);
  4477. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4478. }
  4479. break;
  4480. case START_METHOD_BLE:
  4481. case START_METHOD_FREE:
  4482. default:
  4483. setSpeaker(ON,SPEAKER_SHORT);
  4484. setChargerMode(gun_index, SYS_MODE_PREPARING);
  4485. break;
  4486. }
  4487. }
  4488. break;
  4489. case SYS_MODE_PREPARING:
  4490. if(isModeChange(gun_index))
  4491. {
  4492. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  4493. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  4494. ocpp_set_profile_req(gun_index, ON);
  4495. }
  4496. // Unplug the connector and the system change to Idle
  4497. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  4498. {
  4499. DEBUG_INFO("[SYS_MODE_PREPARING] Unplug the connector.\n");
  4500. setChargerMode(gun_index, SYS_MODE_IDLE);
  4501. }
  4502. // Use RFID card to stop handshaking
  4503. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  4504. {
  4505. DEBUG_INFO("[SYS_MODE_PREPARING] Use RFID card to stop handshaking.\n");
  4506. setSpeaker(ON,SPEAKER_SHORT);
  4507. setChargerMode(gun_index, SYS_MODE_IDLE);
  4508. }
  4509. else
  4510. {
  4511. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4512. }
  4513. // Handshaking timeout and the system back to idle
  4514. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  4515. {
  4516. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4517. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5))
  4518. {
  4519. DEBUG_INFO("[SYS_MODE_PREPARING] Handshaking timeout...\n");
  4520. setChargerMode(gun_index, SYS_MODE_IDLE);
  4521. }
  4522. }
  4523. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  4524. {
  4525. setRequest(gun_index,ON);
  4526. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  4527. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  4528. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4529. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4530. // Set relay on when the system is state C
  4531. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) && ((ShmCharger->gun_info[gun_index].targetCurrent != 0)))
  4532. setRelay(gun_index, ON);
  4533. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  4534. {
  4535. ocpp_set_unlocker_req(gun_index, OFF);
  4536. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  4537. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  4538. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  4539. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  4540. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4541. ocpp_copy_userid_to_starttransaction(gun_index);
  4542. ocpp_set_starttransaction_req(gun_index, ON);
  4543. refreshStartTimer(&startChargingTime[gun_index]);
  4544. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4545. }
  4546. }
  4547. break;
  4548. case SYS_MODE_CHARGING:
  4549. if(isModeChange(gun_index))
  4550. {
  4551. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4552. //ftime(&startChargingTime[gun_index]);
  4553. refreshStartTimer(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4554. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4555. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4556. startTime[gun_index][TMR_IDX_AUTH].tv_sec -= TIMEOUT_SPEC_AUTH;
  4557. ocpp_set_auth_req(OFF);
  4558. ocpp_reset_smartcharging_profileId(gun_index);
  4559. ocpp_set_profile_req(gun_index, ON);
  4560. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4561. }
  4562. // If unplug the connector during a session, the system must stop charging and stop session.
  4563. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4564. {
  4565. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  4566. {
  4567. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  4568. DEBUG_INFO("[SYS_MODE_CHARGING] The connector was unplugged.\n");
  4569. }
  4570. }
  4571. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES) ||
  4572. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4573. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4574. ocpp_get_remotestop(gun_index) ||
  4575. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4576. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  4577. ocpp_get_reset_req() ||
  4578. ocpp_get_unlocker_req(gun_index) ||
  4579. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  4580. {
  4581. if(ShmCharger->gun_info[gun_index].rfidReq)
  4582. {
  4583. // If RFID SN different with start user, it need to authorize ID
  4584. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4585. {
  4586. if((getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4587. {
  4588. // Request authorization
  4589. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  4590. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4591. getUserId();
  4592. DEBUG_INFO("[SYS_MODE_CHARGING] End request UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4593. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4594. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4595. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4596. {
  4597. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4598. {
  4599. case AUTH_MODE_ENABLE:
  4600. if(ocpp_get_connection_status())
  4601. {
  4602. // On line
  4603. ocpp_set_auth_req(ON, "ISO14443");
  4604. }
  4605. else
  4606. {
  4607. // Off line
  4608. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4609. {
  4610. case OFF_POLICY_LOCALLIST:
  4611. ocpp_set_auth_req(ON, "ISO14443");
  4612. break;
  4613. case OFF_POLICY_PH_RFID:
  4614. break;
  4615. case OFF_POLICY_FREE:
  4616. break;
  4617. case OFF_POLICY_NOCHARGE:
  4618. default:
  4619. break;
  4620. }
  4621. }
  4622. break;
  4623. case AUTH_MODE_DISABLE:
  4624. default:
  4625. break;
  4626. }
  4627. }
  4628. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4629. {
  4630. DEBUG_INFO("[SYS_MODE_CHARGING] Should be remote stop charger... \n");
  4631. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4632. }
  4633. }
  4634. else
  4635. {
  4636. if(ocpp_get_auth_conf() ||
  4637. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4638. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > 2)) ||
  4639. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4640. {
  4641. if(ocpp_get_auth_result(gun_index) ||
  4642. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4643. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4644. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4645. {
  4646. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4647. }
  4648. else
  4649. {
  4650. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4651. ocpp_set_auth_req(OFF);
  4652. DEBUG_INFO("[SYS_MODE_CHARGING] Authorize fail.\n");
  4653. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4654. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4655. sleep(3);
  4656. }
  4657. startTime[gun_index][TMR_IDX_AUTH].tv_sec -= TIMEOUT_SPEC_AUTH;
  4658. ocpp_set_auth_conf(OFF);
  4659. }
  4660. }
  4661. }
  4662. else
  4663. {}
  4664. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  4665. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  4666. {
  4667. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4668. }
  4669. else
  4670. {}
  4671. }
  4672. else
  4673. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4674. }
  4675. else
  4676. {
  4677. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  4678. refreshStartTimer(&endChargingTime[gun_index]);
  4679. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  4680. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start)/100.0;
  4681. // Response StartTransactionConf
  4682. ocpp_set_starttransaction_conf(gun_index, OFF);
  4683. // Charging profile preparation
  4684. if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  4685. {
  4686. if(!ocpp_get_profile_conf(gun_index))
  4687. {
  4688. ocpp_set_profile_req(gun_index, ON);
  4689. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4690. }
  4691. else
  4692. {
  4693. ocpp_set_profile_conf(gun_index, OFF);
  4694. }
  4695. }
  4696. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  4697. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4698. // If temperature warning derating PWM duty 20%
  4699. if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp >= SPEC_TEMPERATURE_WARN)
  4700. {
  4701. ShmCharger->gun_info[gun_index].tempCoefficient = 0.8;
  4702. }
  4703. else if(ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp <= SPEC_TEMPERATURE_RECOVER)
  4704. {
  4705. ShmCharger->gun_info[gun_index].tempCoefficient = 1.0;
  4706. }
  4707. ShmCharger->gun_info[gun_index].targetCurrent = (ShmCharger->gun_info[gun_index].targetCurrent==0?ShmCharger->gun_info[gun_index].targetCurrent:(((ShmCharger->gun_info[gun_index].targetCurrent*ShmCharger->gun_info[gun_index].tempCoefficient)>=6) ? (ShmCharger->gun_info[gun_index].targetCurrent*ShmCharger->gun_info[gun_index].tempCoefficient):6));
  4708. // Debug information
  4709. if((getDiffSecNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  4710. {
  4711. /*
  4712. DEBUG_INFO("=============================================================\n");
  4713. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4714. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  4715. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4716. DEBUG_INFO("=============================================================\n");
  4717. */
  4718. refreshStartTimer(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4719. }
  4720. if(ocpp_get_connection_status())
  4721. {
  4722. // On-line max condition check
  4723. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  4724. {
  4725. setRelay(gun_index, OFF);
  4726. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4727. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4728. DEBUG_INFO("[SYS_MODE_CHARGING] Connector-%d charging duration(%d) already over max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  4729. }
  4730. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  4731. {
  4732. setRelay(gun_index, OFF);
  4733. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4734. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4735. DEBUG_INFO("[SYS_MODE_CHARGING] Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  4736. }
  4737. else
  4738. {
  4739. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4740. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  4741. {
  4742. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4743. setRelay(gun_index, ON);
  4744. }
  4745. else
  4746. {
  4747. setLedMotion(gun_index, LED_ACTION_STOP);
  4748. setRelay(gun_index, OFF);
  4749. }
  4750. }
  4751. }
  4752. else
  4753. {
  4754. // Off-line max condition check
  4755. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4756. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  4757. {
  4758. if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
  4759. {
  4760. setRelay(gun_index, OFF);
  4761. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4762. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4763. DEBUG_INFO("[SYS_MODE_CHARGING] Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  4764. }
  4765. else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  4766. {
  4767. setRelay(gun_index, OFF);
  4768. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  4769. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  4770. DEBUG_INFO("[SYS_MODE_CHARGING] Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  4771. }
  4772. else
  4773. {
  4774. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  4775. if(ShmCharger->gun_info[gun_index].targetCurrent > 0)
  4776. {
  4777. setLedMotion(gun_index,LED_ACTION_CHARGING);
  4778. setRelay(gun_index, ON);
  4779. }
  4780. else
  4781. {
  4782. setLedMotion(gun_index, LED_ACTION_STOP);
  4783. setRelay(gun_index, OFF);
  4784. }
  4785. }
  4786. }
  4787. else
  4788. {
  4789. setRelay(gun_index, OFF);
  4790. DEBUG_INFO("[SYS_MODE_CHARGING] Connector-%d can not charging in off line\n", gun_index);
  4791. }
  4792. }
  4793. }
  4794. break;
  4795. case SYS_MODE_TERMINATING:
  4796. if(isModeChange(gun_index))
  4797. {
  4798. setLedMotion(gun_index, LED_ACTION_STOP);
  4799. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  4800. startTime[gun_index][TMR_IDX_AUTH].tv_sec -= TIMEOUT_SPEC_AUTH;
  4801. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  4802. setRelay(gun_index, OFF);
  4803. sleep(1);
  4804. }
  4805. refreshStartTimer(&endChargingTime[gun_index]);
  4806. //if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  4807. //{
  4808. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  4809. //}
  4810. // If unplug the connector during a session, the system must stop charging and stop session.
  4811. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  4812. {
  4813. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  4814. {
  4815. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  4816. DEBUG_INFO("[SYS_MODE_TERMINATING] The connector was unplugged.\n");
  4817. }
  4818. }
  4819. // End authorize pass
  4820. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4821. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  4822. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  4823. ocpp_get_remotestop(gun_index) ||
  4824. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  4825. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  4826. ocpp_get_reset_req() ||
  4827. ocpp_get_unlocker_req(gun_index) ||
  4828. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  4829. {
  4830. if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
  4831. {
  4832. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  4833. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  4834. {
  4835. DEBUG_INFO("[SYS_MODE_TERMINATING] Authorize pass.\n");
  4836. setSpeaker(ON,SPEAKER_SHORT);
  4837. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4838. sleep(3);
  4839. }
  4840. }
  4841. else
  4842. {}
  4843. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 100;
  4844. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4845. setLedMotion(gun_index, LED_ACTION_STOP);
  4846. checkStopReason(gun_index);
  4847. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  4848. }
  4849. else
  4850. {
  4851. // If the charger stops charging, the led should be stoped blink
  4852. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  4853. {
  4854. setLedMotion(gun_index, LED_ACTION_STOP);
  4855. }
  4856. if(ShmCharger->gun_info[gun_index].rfidReq)
  4857. {
  4858. // If RFID SN different with start user, it need to authorize ID
  4859. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  4860. {
  4861. if((getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ocpp_get_auth_req())
  4862. {
  4863. // Request authorization
  4864. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  4865. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4866. getUserId();
  4867. DEBUG_INFO("[SYS_MODE_TERMINATING] End request UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  4868. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  4869. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  4870. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  4871. {
  4872. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  4873. {
  4874. case AUTH_MODE_ENABLE:
  4875. if(ocpp_get_connection_status())
  4876. {
  4877. // On line
  4878. ocpp_set_auth_req(ON, "ISO14443");
  4879. }
  4880. else
  4881. {
  4882. // Off line
  4883. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  4884. {
  4885. case OFF_POLICY_LOCALLIST:
  4886. ocpp_set_auth_req(ON, "ISO14443");
  4887. break;
  4888. case OFF_POLICY_PH_RFID:
  4889. break;
  4890. case OFF_POLICY_FREE:
  4891. break;
  4892. case OFF_POLICY_NOCHARGE:
  4893. default:
  4894. break;
  4895. }
  4896. }
  4897. break;
  4898. case AUTH_MODE_DISABLE:
  4899. default:
  4900. break;
  4901. }
  4902. }
  4903. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  4904. {
  4905. DEBUG_INFO("[SYS_MODE_TERMINATING] Should be remote stop charger... \n");
  4906. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4907. }
  4908. }
  4909. else
  4910. {
  4911. if(ocpp_get_auth_conf() ||
  4912. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4913. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > 2)) ||
  4914. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4915. {
  4916. if(ocpp_get_auth_result(gun_index) ||
  4917. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  4918. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  4919. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  4920. {
  4921. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  4922. }
  4923. else
  4924. {
  4925. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4926. ocpp_set_auth_req(OFF);
  4927. DEBUG_INFO("[SYS_MODE_TERMINATING] Authorize fail... \n");
  4928. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4929. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4930. sleep(3);
  4931. }
  4932. startTime[gun_index][TMR_IDX_AUTH].tv_sec -= TIMEOUT_SPEC_AUTH;
  4933. ocpp_set_auth_conf(OFF);
  4934. }
  4935. }
  4936. }
  4937. }
  4938. // Charging profile preparation
  4939. if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  4940. {
  4941. if(!ocpp_get_profile_conf(gun_index))
  4942. {
  4943. ocpp_set_profile_req(gun_index, ON);
  4944. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  4945. }
  4946. else
  4947. {
  4948. ocpp_set_profile_conf(gun_index, OFF);
  4949. }
  4950. }
  4951. // Check target current if charging profile limit > 0
  4952. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4953. setRelay(gun_index, OFF);
  4954. // Debug information
  4955. if((getDiffSecNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) < 0))
  4956. {
  4957. /*
  4958. DEBUG_INFO("=============================================================\n");
  4959. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4960. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  4961. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4962. DEBUG_INFO("=============================================================\n");
  4963. */
  4964. refreshStartTimer(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  4965. }
  4966. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES) &&
  4967. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  4968. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  4969. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  4970. !ocpp_get_remotestop(gun_index) &&
  4971. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  4972. !ocpp_get_reset_req() &&
  4973. ocpp_get_starttransaction_result(gun_index) &&
  4974. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  4975. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  4976. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))) &&
  4977. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))))
  4978. {
  4979. sleep(1);
  4980. setChargerMode(gun_index, SYS_MODE_CHARGING);
  4981. }
  4982. }
  4983. break;
  4984. case SYS_MODE_COMPLETE:
  4985. if(isModeChange(gun_index))
  4986. {
  4987. setLedMotion(gun_index, LED_ACTION_STOP);
  4988. sleep(6);
  4989. setRequest(gun_index, OFF);
  4990. setRelay(gun_index, OFF);
  4991. sleep(2);
  4992. }
  4993. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || ocpp_get_reset_req())
  4994. {
  4995. sleep(2);
  4996. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4997. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  4998. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4999. ocpp_set_remotestop(gun_index, OFF);
  5000. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  5001. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5002. DB_Insert_Record(localDb, gun_index);
  5003. setChargerMode(gun_index, SYS_MODE_IDLE);
  5004. }
  5005. break;
  5006. case SYS_MODE_ALARM:
  5007. setLedMotion(gun_index,LED_ACTION_ALARM);
  5008. setRelay(gun_index, OFF);
  5009. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
  5010. {
  5011. setRequest(gun_index,OFF);
  5012. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  5013. {
  5014. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  5015. DEBUG_INFO("[SYS_MODE_ALARM] The connector was unplugged.\n");
  5016. }
  5017. }
  5018. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  5019. {
  5020. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  5021. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  5022. {
  5023. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  5024. }
  5025. else
  5026. {
  5027. setChargerMode(gun_index, SYS_MODE_IDLE);
  5028. }
  5029. }
  5030. break;
  5031. case SYS_MODE_FAULT:
  5032. if(isModeChange(gun_index))
  5033. {}
  5034. break;
  5035. case SYS_MODE_MAINTAIN:
  5036. if(isModeChange(gun_index))
  5037. {
  5038. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5039. }
  5040. break;
  5041. case SYS_MODE_UPDATE:
  5042. if(isModeChange(gun_index))
  5043. {
  5044. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5045. }
  5046. //======================================
  5047. // Check local upgrade firmware request
  5048. //======================================
  5049. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  5050. {
  5051. DEBUG_INFO("[SYS_MODE_UPDATE] Firmware local upgrading...\n");
  5052. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  5053. int value = upgrade_check();
  5054. DEBUG_INFO("[SYS_MODE_UPDATE] Local update Value: %s...\n",((value == PASS)?"Pass": "Fail"));
  5055. if(value == PASS)
  5056. {
  5057. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  5058. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  5059. ShmCharger->isUpdateSuccess = YES;
  5060. DEBUG_INFO("[SYS_MODE_UPDATE] Local update success...\n");
  5061. }
  5062. else
  5063. {
  5064. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  5065. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  5066. ShmCharger->isUpdateSuccess = NO;
  5067. DEBUG_INFO("[SYS_MODE_UPDATE] Local update unsuccess...\n");
  5068. }
  5069. }
  5070. else if(ocpp_get_update_firmware_req())
  5071. {
  5072. //======================================
  5073. // Check remote upgrade firmware request
  5074. //======================================
  5075. checkRemoteUpgradeStatus();
  5076. }
  5077. else
  5078. {
  5079. //======================================
  5080. // Upgrade complete reboot system
  5081. //======================================
  5082. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  5083. {
  5084. if(ShmCharger->isUpdateSuccess == YES)
  5085. {
  5086. if((strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing")==0))
  5087. {
  5088. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5089. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5090. }
  5091. else if((strcmp((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installing")==0))
  5092. {
  5093. ShmOCPP16Data->SignedFirmwareStatusNotification.requestId = ShmOCPP16Data->SignedUpdateFirmware.requestId;
  5094. sprintf((char*)ShmOCPP16Data->SignedFirmwareStatusNotification.status, "Installed");
  5095. ShmOCPP16Data->SpMsg.bits.SignedFirmwareStatusNotificationReq = ON;
  5096. }
  5097. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5098. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5099. DEBUG_WARN("[SYS_MODE_UPDATE] Firmware upgrade success.\n");
  5100. sleep(5);
  5101. DEBUG_INFO("[SYS_MODE_UPDATE] Firmware upgraded, reboot...\n");
  5102. system("reboot -f");
  5103. sleep(5);
  5104. system("reboot -f");
  5105. }
  5106. else
  5107. {
  5108. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  5109. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5110. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  5111. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5112. DEBUG_WARN("[SYS_MODE_UPDATE] Firmware upgrade fail.\n");
  5113. sleep(5);
  5114. system("rm -rvf /mnt/* ");
  5115. close(wtdFd);
  5116. system("/usr/bin/run_evse_restart.sh");
  5117. }
  5118. }
  5119. }
  5120. break;
  5121. case SYS_MODE_RESERVATION:
  5122. if(isModeChange(gun_index))
  5123. {
  5124. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5125. }
  5126. if(isReservationExpired(gun_index))
  5127. {
  5128. DEBUG_INFO("[SYS_MODE_RESERVATION] Time's up...\n");
  5129. setChargerMode(gun_index, SYS_MODE_IDLE);
  5130. }
  5131. else if(ocpp_get_cancelreservation_req(gun_index))
  5132. {
  5133. DEBUG_INFO("[SYS_MODE_RESERVATION] Cancel reservation...\n");
  5134. setChargerMode(gun_index, SYS_MODE_IDLE);
  5135. ocpp_set_cancelreservation_req(gun_index, OFF);
  5136. }
  5137. else
  5138. {
  5139. // Check is there RFID or back end request start
  5140. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5141. ocpp_get_remotestart(gun_index))
  5142. {
  5143. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  5144. {
  5145. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5146. getUserId();
  5147. if(ocpp_compare_reserve_id_with_user(gun_index) ||
  5148. ocpp_get_auth_result(gun_index))
  5149. {
  5150. DEBUG_INFO("[SYS_MODE_RESERVATION] Start Method in reservation: RFID...\n");
  5151. DEBUG_INFO("[SYS_MODE_RESERVATION] Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  5152. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  5153. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  5154. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5155. }
  5156. else
  5157. {
  5158. DEBUG_INFO("[SYS_MODE_RESERVATION] It's not reserve user id.\n");
  5159. DEBUG_INFO("[SYS_MODE_RESERVATION] Start request User Id: %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  5160. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  5161. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  5162. //setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5163. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5164. }
  5165. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5166. }
  5167. else if(ocpp_get_remotestart(gun_index))
  5168. {
  5169. if(ocpp_compare_reserve_id_with_remote_user(gun_index) || ocpp_isAuthorizeRemoteStart())
  5170. {
  5171. DEBUG_INFO("[SYS_MODE_RESERVATION] Start Method in reservation: BACKEND...\n");
  5172. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  5173. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  5174. if(ocpp_isAuthorizeRemoteStart())
  5175. ocpp_copy_userid_from_remotestart(gun_index);
  5176. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5177. }
  5178. ocpp_set_remotestart(gun_index, OFF);
  5179. }
  5180. }
  5181. }
  5182. break;
  5183. case SYS_MODE_BOOKING:
  5184. if(isModeChange(gun_index))
  5185. {}
  5186. break;
  5187. case SYS_MODE_DEBUG:
  5188. if(isModeChange(gun_index))
  5189. {
  5190. setLedMotion(gun_index,LED_ACTION_DEBUG);
  5191. }
  5192. break;
  5193. }
  5194. }
  5195. // System watch dog reset
  5196. write(wtdFd, "a", 1);
  5197. usleep(50000);
  5198. }
  5199. return FAIL;
  5200. }