main.c 215 KB

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