main.c 166 KB

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