main.c 143 KB

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