main.c 153 KB

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