main.c 155 KB

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