main.c 185 KB

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