main.c 153 KB

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