main.c 210 KB

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