main.c 162 KB

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