main.c 156 KB

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