main.c 167 KB

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