main.c 166 KB

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