main.c 171 KB

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