main.c 183 KB

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