main.c 159 KB

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