main.c 174 KB

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