main.c 195 KB

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