main.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961
  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_3 3
  21. #define TMR_IDX_4 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 30000
  29. #define TIMEOUT_SPEC_HANDSHAKING_LED 185000
  30. #define TIMEOUT_SPEC_LOGPPRINTOUT 20000
  31. #define MtdBlockSize 0x600000
  32. //==========================
  33. // Declare method
  34. //==========================
  35. void trim(char *s);
  36. int mystrcmp(char *p1,char *p2);
  37. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  38. void split(char **arr, char *str, const char *del);
  39. int isReachableInternet();
  40. int InitRfidPort(void);
  41. int GetCardSerialNumber();
  42. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  43. void setRelay(unsigned char gun_index,unsigned char isOn);
  44. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  45. //==========================
  46. // Declare RFID module type
  47. //==========================
  48. #define MODULE_EWT 0
  49. int wtdFd = -1;
  50. int rfidFd = -1;
  51. char* rfidPortName = "/dev/ttyS2";
  52. RFID rfid;
  53. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  54. //==========================
  55. // Declare share memory
  56. //==========================
  57. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  58. struct StatusCodeData *ShmStatusCodeData;
  59. struct PsuData *ShmPsuData;
  60. struct CHAdeMOData *ShmCHAdeMOData;
  61. struct CcsData *ShmCcsData;
  62. struct PrimaryMcuData *ShmPrimaryMcuData;
  63. struct FanModuleData *ShmFanModuleData;
  64. struct RelayModuleData *ShmRelayModuleData;
  65. struct OCPP16Data *ShmOCPP16Data;
  66. struct Charger *ShmCharger;
  67. struct timeb startTime[AC_QUANTITY][10];
  68. struct timeb startChargingTime[AC_QUANTITY];
  69. struct timeb endChargingTime[AC_QUANTITY];
  70. sqlite3 *localDb;
  71. //=================================
  72. // Common routine
  73. //=================================
  74. void trim(char *s)
  75. {
  76. int i=0, j, k, l=0;
  77. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  78. i++;
  79. j = strlen(s)-1;
  80. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  81. j--;
  82. if(i==0 && j==strlen(s)-1) { }
  83. else if(i==0) s[j+1] = '\0';
  84. else {
  85. for(k=i; k<=j; k++) s[l++] = s[k];
  86. s[l] = '\0';
  87. }
  88. }
  89. int mystrcmp(char *p1,char *p2)
  90. {
  91. while(*p1==*p2)
  92. {
  93. if(*p1=='\0' || *p2=='\0')
  94. break;
  95. p1++;
  96. p2++;
  97. }
  98. if(*p1=='\0' && *p2=='\0')
  99. return(PASS);
  100. else
  101. return(FAIL);
  102. }
  103. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  104. {
  105. strncpy(dest, src + start, cnt);
  106. dest[cnt] = 0;
  107. }
  108. void split(char **arr, char *str, const char *del)
  109. {
  110. char *s = strtok(str, del);
  111. while(s != NULL)
  112. {
  113. *arr++ = s;
  114. s = strtok(NULL, del);
  115. }
  116. }
  117. int StoreLogMsg(const char *fmt, ...)
  118. {
  119. char Buf[4096+256];
  120. char buffer[4096];
  121. time_t CurrentTime;
  122. struct tm *tm;
  123. va_list args;
  124. va_start(args, fmt);
  125. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  126. va_end(args);
  127. memset(Buf,0,sizeof(Buf));
  128. CurrentTime = time(NULL);
  129. tm=localtime(&CurrentTime);
  130. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  131. {
  132. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  133. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  134. buffer,
  135. tm->tm_year+1900,tm->tm_mon+1,
  136. ShmSysConfigAndInfo->SysConfig.ModelName,
  137. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  138. }
  139. else
  140. {
  141. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  142. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  143. buffer,
  144. tm->tm_year+1900,tm->tm_mon+1);
  145. }
  146. #ifdef SystemLogMessage
  147. system(Buf);
  148. #endif
  149. #ifdef ConsloePrintLog
  150. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  151. #endif
  152. return rc;
  153. }
  154. int DiffTimebWithNow(struct timeb ST)
  155. {
  156. //return milli-second
  157. struct timeb ET;
  158. unsigned int StartTime,StopTime;
  159. ftime(&ET);
  160. StartTime=(unsigned int)ST.time;
  161. StopTime=(unsigned int)ET.time;
  162. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  163. }
  164. int DiffTimeb(struct timeb ST, struct timeb ET)
  165. {
  166. //return milli-second
  167. unsigned int StartTime,StopTime;
  168. StartTime=(unsigned int)ST.time;
  169. StopTime=(unsigned int)ET.time;
  170. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  171. }
  172. void getDateTimeString(char* result)
  173. {
  174. time_t CurrentTime;
  175. struct tm *tm;
  176. CurrentTime = time(NULL);
  177. tm=localtime(&CurrentTime);
  178. 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);
  179. }
  180. //======================================================
  181. // Check interface status
  182. //======================================================
  183. int isInterfaceUp(const char *interface)
  184. {
  185. int result = FAIL;
  186. FILE *fp;
  187. char cmd[256];
  188. char buf[512];
  189. strcpy(cmd, "ifconfig");;
  190. fp = popen(cmd, "r");
  191. if(fp != NULL)
  192. {
  193. while(fgets(buf, sizeof(buf), fp) != NULL)
  194. {
  195. if(strstr(buf, interface) > 0)
  196. {
  197. result = PASS;
  198. }
  199. }
  200. }
  201. pclose(fp);
  202. return result;
  203. }
  204. //======================================================
  205. // Create all share memory
  206. //======================================================
  207. int CreatShareMemory()
  208. {
  209. int result = PASS;
  210. int MeterSMId;
  211. //creat ShmSysConfigAndInfo
  212. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  213. {
  214. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\r\n");
  215. result = FAIL;
  216. }
  217. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  218. {
  219. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\r\n");
  220. result = FAIL;
  221. }
  222. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  223. //creat ShmStatusCodeData
  224. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  225. {
  226. DEBUG_ERROR("shmget ShmStatusCodeData NG\r\n");
  227. result = FAIL;
  228. }
  229. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  230. {
  231. DEBUG_ERROR("shmat ShmStatusCodeData NG\r\n");
  232. result = FAIL;
  233. }
  234. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  235. //creat ShmPsuData
  236. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  237. {
  238. DEBUG_ERROR("shmget ShmPsuData NG\r\n");
  239. result = FAIL;
  240. }
  241. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  242. {
  243. DEBUG_ERROR("shmat ShmPsuData NG\r\n");
  244. result = FAIL;
  245. }
  246. memset(ShmPsuData,0,sizeof(struct PsuData));
  247. //creat ShmCHAdeMOData
  248. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  249. {
  250. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\r\n");
  251. result = FAIL;
  252. }
  253. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  254. {
  255. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\r\n");
  256. result = FAIL;
  257. }
  258. memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
  259. //creat ShmCcsData
  260. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  261. {
  262. DEBUG_ERROR("shmget ShmCcsData NG\r\n");
  263. result = FAIL;
  264. }
  265. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  266. {
  267. DEBUG_ERROR("shmat ShmCcsData NG\r\n");
  268. result = FAIL;
  269. }
  270. memset(ShmCcsData,0,sizeof(struct CcsData));
  271. //creat ShmPrimaryMcuData
  272. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  273. {
  274. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\r\n");
  275. result = FAIL;
  276. }
  277. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  278. {
  279. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\r\n");
  280. result = FAIL;
  281. }
  282. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  283. /*
  284. //creat ShmFanModuleData
  285. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  286. {
  287. DEBUG_ERROR("shmget ShmFanModuleData NG\r\n");
  288. result = FAIL;
  289. }
  290. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  291. {
  292. DEBUG_ERROR("shmat ShmFanModuleData NG\r\n");
  293. result = FAIL;
  294. }
  295. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  296. //creat ShmRelayModuleData
  297. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  298. {
  299. DEBUG_ERROR("shmget ShmRelayModuleData NG\r\n");
  300. result = FAIL;
  301. }
  302. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  303. {
  304. DEBUG_ERROR("shmat ShmRelayModuleData NG\r\n");
  305. result = FAIL;
  306. }
  307. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));*/
  308. //creat ShmOCPP16Data
  309. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  310. {
  311. DEBUG_ERROR("shmget ShmOCPP16Data NG\r\n");
  312. result = FAIL;
  313. }
  314. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  315. {
  316. DEBUG_ERROR("shmat ShmOCPP16Data NG\r\n");
  317. result = FAIL;
  318. }
  319. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  320. //creat ShmCharger
  321. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  322. {
  323. DEBUG_ERROR("shmget ShmCharger NG\r\n");
  324. result = FAIL;
  325. }
  326. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  327. {
  328. DEBUG_ERROR("shmat ShmCharger NG\r\n");
  329. result = FAIL;
  330. }
  331. memset(ShmCharger,0,sizeof(struct Charger));
  332. return result;
  333. }
  334. //===============================================
  335. // SQLite3 related routine
  336. //===============================================
  337. int DB_Open(sqlite3 *db)
  338. {
  339. int result = PASS;
  340. char* errMsg = NULL;
  341. char* createSql="CREATE TABLE IF NOT EXISTS charging_record("
  342. "idx integer primary key AUTOINCREMENT, "
  343. "reservationId text, "
  344. "transactionId text, "
  345. "startMethod text, "
  346. "userId text, "
  347. "dateTimeStart text, "
  348. "dateTimeStop text,"
  349. "socStart text, "
  350. "socStop text, "
  351. "chargeEnergy text, "
  352. "stopReason text"
  353. ");";
  354. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  355. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  356. {
  357. result = FAIL;
  358. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  359. sqlite3_close(db);
  360. }
  361. else
  362. {
  363. DEBUG_INFO( "Local charging record database open successfully.\r\n");
  364. if (sqlite3_exec(db, createSql, 0, 0, &errMsg) != SQLITE_OK)
  365. {
  366. result = FAIL;
  367. DEBUG_INFO( "Create local charging record table error message: %s\n", errMsg);
  368. }
  369. else
  370. {
  371. DEBUG_INFO( "Opened local charging record table successfully\n");
  372. }
  373. sqlite3_close(db);
  374. }
  375. return result;
  376. }
  377. int DB_Insert_Record(sqlite3 *db, int gun_index)
  378. {
  379. int result = PASS;
  380. char* errMsg = NULL;
  381. char insertSql[1024];
  382. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  383. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  384. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  385. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  386. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  387. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  388. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  389. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  390. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  391. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  392. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  393. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  394. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  395. {
  396. result = FAIL;
  397. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  398. sqlite3_close(db);
  399. }
  400. else
  401. {
  402. DEBUG_INFO( "Local charging record database open successfully.\r\n");
  403. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  404. {
  405. result = FAIL;
  406. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  407. }
  408. else
  409. {
  410. DEBUG_INFO( "Insert local charging record successfully\n");
  411. }
  412. sqlite3_close(db);
  413. }
  414. return result;
  415. }
  416. //======================================================
  417. // Peripheral initial
  418. //======================================================
  419. int InitWatchDog()
  420. {
  421. int fd;
  422. system("/usr/bin/fuser -k /dev/watchdog");
  423. sleep(1);
  424. system("echo V > /dev/watchdog");
  425. sleep(1);
  426. fd=open("/dev/watchdog", O_RDWR);
  427. if(fd<=0)
  428. {
  429. DEBUG_ERROR("System watch dog initial fail.\r\n");
  430. }
  431. return fd;
  432. }
  433. void InitGPIO()
  434. {
  435. /*****************0~3, 4 bank, bank x 32+ num*********************/
  436. /***************************************************************/
  437. /*************** INPUT PIN ***************************************/
  438. /***************************************************************/
  439. /***************************************************************/
  440. /*************** OUTPUT PIN ************************************/
  441. /***************************************************************/
  442. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  443. system("echo 116 > /sys/class/gpio/export");
  444. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  445. system("echo 0 > /sys/class/gpio/gpio116/value");
  446. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  447. system("echo 19 > /sys/class/gpio/export");
  448. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  449. system("echo 1 > /sys/class/gpio/gpio19/value");
  450. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  451. system("echo 65 > /sys/class/gpio/export");
  452. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  453. system("echo 0 > /sys/class/gpio/gpio65/value");
  454. /*4G/Wifi RST:GPIO3_14 => H:ON; L:OFF*/
  455. system("echo 110 > /sys/class/gpio/export");
  456. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  457. system("echo 1 > /sys/class/gpio/gpio110/value");
  458. sleep(3);
  459. system("echo 0 > /sys/class/gpio/gpio110/value");
  460. DEBUG_INFO("Initial GPIO OK\r\n");
  461. }
  462. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  463. {
  464. int fd,wrd;
  465. unsigned char *buf;
  466. unsigned int ChkSum,ChkSumOrg;
  467. if((buf=malloc(MtdBlockSize))==NULL)
  468. {
  469. DEBUG_ERROR("malloc buffer NG,rebooting..\r\n");
  470. if(ShmStatusCodeData!=NULL)
  471. {
  472. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  473. }
  474. sleep(5);
  475. system("reboot -f");
  476. sleep(5);
  477. system("reboot -f");
  478. }
  479. memset(buf, 0, MtdBlockSize);
  480. //================================================
  481. // Load configuration from mtdblock10
  482. //================================================
  483. fd = open("/dev/mtdblock10", O_RDWR);
  484. if (fd < 0)
  485. {
  486. free(buf);
  487. DEBUG_ERROR("open mtdblock10 NG,rebooting..\r\n");
  488. if(ShmStatusCodeData!=NULL)
  489. {
  490. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  491. }
  492. sleep(5);
  493. system("reboot -f");
  494. sleep(5);
  495. system("reboot -f");
  496. }
  497. wrd=read(fd, buf, MtdBlockSize);
  498. close(fd);
  499. if(wrd<MtdBlockSize)
  500. {
  501. free(buf);
  502. DEBUG_ERROR("read SysConfigData data NG,rebooting..\r\n");
  503. if(ShmStatusCodeData!=NULL)
  504. {
  505. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  506. }
  507. sleep(5);
  508. system("reboot -f");
  509. sleep(5);
  510. system("reboot -f");
  511. }
  512. ChkSum=0;
  513. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  514. {
  515. ChkSum+=buf[wrd];
  516. }
  517. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  518. //================================================
  519. // Load configuration from mtdblock11
  520. //================================================
  521. if(ChkSum!=ChkSumOrg)
  522. {
  523. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\r\n");
  524. fd = open("/dev/mtdblock11", O_RDWR);
  525. if (fd < 0)
  526. {
  527. free(buf);
  528. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\r\n");
  529. if(ShmStatusCodeData!=NULL)
  530. {
  531. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  532. }
  533. sleep(5);
  534. system("reboot -f");
  535. sleep(5);
  536. system("reboot -f");
  537. }
  538. memset(buf, 0, MtdBlockSize);
  539. wrd=read(fd, buf,MtdBlockSize);
  540. close(fd);
  541. if(wrd<MtdBlockSize)
  542. {
  543. free(buf);
  544. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\r\n");
  545. if(ShmStatusCodeData!=NULL)
  546. {
  547. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  548. }
  549. sleep(5);
  550. system("reboot -f");
  551. sleep(5);
  552. system("reboot -f");
  553. }
  554. ChkSum=0;
  555. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  556. {
  557. ChkSum+=buf[wrd];
  558. }
  559. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  560. //================================================
  561. // Load configuration from mtdblock12 (Factory default)
  562. //================================================
  563. if(ChkSum!=ChkSumOrg)
  564. {
  565. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\r\n");
  566. fd = open("/dev/mtdblock12", O_RDWR);
  567. if (fd < 0)
  568. {
  569. free(buf);
  570. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  571. if(ShmStatusCodeData!=NULL)
  572. {
  573. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  574. }
  575. sleep(5);
  576. system("reboot -f");
  577. sleep(5);
  578. system("reboot -f");
  579. }
  580. memset(buf, 0, MtdBlockSize);
  581. wrd=read(fd, buf,MtdBlockSize);
  582. close(fd);
  583. if(wrd<MtdBlockSize)
  584. {
  585. free(buf);
  586. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\r\n");
  587. if(ShmStatusCodeData!=NULL)
  588. {
  589. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  590. }
  591. sleep(5);
  592. system("reboot -f");
  593. sleep(5);
  594. system("reboot -f");
  595. }
  596. ChkSum=0;
  597. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  598. {
  599. ChkSum+=buf[wrd];
  600. }
  601. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  602. if(ChkSum!=ChkSumOrg)
  603. {
  604. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\r\n");
  605. free(buf);
  606. system("cd /root;./Module_FactoryConfig -m");
  607. system("sync");
  608. sleep(5);
  609. system("reboot -f");
  610. sleep(5);
  611. system("reboot -f");
  612. return FAIL;
  613. }
  614. }
  615. }
  616. //load OK
  617. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  618. free(buf);
  619. DEBUG_INFO("Load SysConfigData OK\r\n");
  620. return PASS;
  621. }
  622. void InitEthernet()
  623. {
  624. pid_t pid;
  625. uint8_t cnt_pingDNS_Fail;
  626. char tmpbuf[256];
  627. //unsigned int address;
  628. //Init Eth0 for internet
  629. if(isInterfaceUp("eth0")==PASS)
  630. {
  631. memset(tmpbuf,0,256);
  632. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  633. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  634. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  635. system(tmpbuf);
  636. memset(tmpbuf,0,256);
  637. sprintf(tmpbuf,"route add default gw %s eth0 ",
  638. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  639. system(tmpbuf);
  640. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up");
  641. system("ifconfig lo up");
  642. }
  643. if(isInterfaceUp("eth1")==PASS)
  644. {
  645. //Init Eth1 for administrator tool
  646. memset(tmpbuf,0,256);
  647. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  648. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  649. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  650. system(tmpbuf);
  651. }
  652. //Run DHCP client if enabled
  653. system("killall udhcpc");
  654. system("rm -rf /etc/resolv.conf");
  655. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  656. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  657. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  658. system("/sbin/udhcpc -i eth0 -s /root/dhcp_script/eth0.script > /dev/null &");
  659. //check internet status
  660. pid = fork();
  661. // Upgrade system id to /etc/hostname
  662. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  663. system(tmpbuf);
  664. if(pid == 0)
  665. {
  666. for(;;)
  667. {
  668. if(isReachableInternet() == PASS)
  669. {
  670. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  671. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  672. cnt_pingDNS_Fail = 0;
  673. }
  674. else
  675. {
  676. if(++cnt_pingDNS_Fail > 3)
  677. {
  678. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  679. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  680. }
  681. }
  682. sleep(5);
  683. }
  684. }
  685. DEBUG_INFO("Initial Ethernet OK\r\n");
  686. }
  687. int SpawnTask()
  688. {
  689. system ("pkill Module_4g");
  690. system ("pkill Module_Wifi");
  691. system ("pkill Module_EventLogging");
  692. system ("pkill OcppBackend");
  693. system ("pkill Module_AlarmDetect");
  694. system ("pkill Module_InternalComm");
  695. system ("pkill Module_Speaker");
  696. system ("pkill Module_ProduceUtils");
  697. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  698. {
  699. system("/root/Module_4g &");
  700. }
  701. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  702. {
  703. system("/root/Module_Wifi &");
  704. }
  705. system("/root/Module_EventLogging &");
  706. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  707. {
  708. system("/root/OcppBackend &");
  709. }
  710. system ("/root/Module_AlarmDetect &");
  711. system ("/root/Module_InternalComm &");
  712. system ("/root/Module_Speaker &");
  713. system ("/root/Module_ProduceUtils &");
  714. return PASS;
  715. }
  716. int StoreUsrConfigData(struct SysConfigData *UsrData)
  717. {
  718. int result = PASS;
  719. int fd,wrd;
  720. unsigned int i,Chk;
  721. unsigned char *ptr, *BufTmp;
  722. Chk=0;
  723. ptr=(unsigned char *)UsrData;
  724. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  725. {
  726. memset(BufTmp,0,MtdBlockSize);
  727. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  728. for(i=0;i<MtdBlockSize-4;i++)
  729. Chk+=*(BufTmp+i);
  730. memcpy( BufTmp+MtdBlockSize-4,&Chk,4);
  731. fd = open("/dev/mtdblock10", O_RDWR);
  732. if (fd>0)
  733. {
  734. wrd=write(fd, BufTmp, MtdBlockSize);
  735. close(fd);
  736. if(wrd>=MtdBlockSize)
  737. {
  738. fd = open("/dev/mtdblock11", O_RDWR);
  739. if (fd>0)
  740. {
  741. wrd=write(fd, BufTmp, MtdBlockSize);
  742. close(fd);
  743. if(wrd<MtdBlockSize)
  744. {
  745. DEBUG_ERROR("write /dev/mtdblock11(backup) NG\r\n");
  746. result = FAIL;
  747. }
  748. }
  749. else
  750. {
  751. DEBUG_ERROR("open /dev/mtdblock11(backup) NG\r\n");
  752. result = FAIL;
  753. }
  754. }
  755. else
  756. {
  757. DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
  758. result = FAIL;
  759. }
  760. }
  761. else
  762. {
  763. DEBUG_ERROR("open /dev/mtdblock10 NG\r\n");
  764. result = FAIL;
  765. }
  766. }
  767. else
  768. {
  769. DEBUG_ERROR("alloc BlockSize NG\r\n");
  770. result = FAIL;
  771. }
  772. if(BufTmp!=NULL)
  773. free(BufTmp);
  774. return result;
  775. }
  776. int Initialization()
  777. {
  778. int result = PASS;
  779. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  780. InitGPIO();
  781. InitEthernet();
  782. if(DB_Open(localDb) != PASS)
  783. result = FAIL;
  784. rfidFd = InitRfidPort();
  785. wtdFd = InitWatchDog();
  786. if((wtdFd < 0) || (rfidFd < 0))
  787. result = FAIL;
  788. if(result == PASS)
  789. DEBUG_INFO("Initialization OK.\r\n");
  790. else
  791. DEBUG_INFO("Initialization Fail.\r\n");
  792. return result;
  793. }
  794. //=====================================================
  795. // Common routine
  796. //=====================================================
  797. char* getSystemModeName(unsigned char mode)
  798. {
  799. char* result;
  800. switch(mode)
  801. {
  802. case SYS_MODE_BOOTING:
  803. result = "booting";
  804. break;
  805. case SYS_MODE_IDLE:
  806. result = "idle";
  807. break;
  808. case SYS_MODE_AUTHORIZING:
  809. result = "authorizing";
  810. break;
  811. case SYS_MODE_PREPARING:
  812. result = "preparing";
  813. break;
  814. case SYS_MODE_CHARGING:
  815. result = "charging";
  816. break;
  817. case SYS_MODE_TERMINATING:
  818. result = "terminating";
  819. break;
  820. case SYS_MODE_ALARM:
  821. result = "alarm";
  822. break;
  823. case SYS_MODE_FAULT:
  824. result = "fault";
  825. break;
  826. case SYS_MODE_MAINTAIN:
  827. result = "maintain";
  828. break;
  829. case SYS_MODE_RESERVATION:
  830. result = "reservation";
  831. break;
  832. case SYS_MODE_BOOKING:
  833. result = "booking";
  834. break;
  835. case SYS_MODE_DEBUG:
  836. result = "debug";
  837. break;
  838. case SYS_MODE_UPDATE:
  839. result = "upgrade";
  840. break;
  841. default:
  842. result = "unknown";
  843. break;
  844. }
  845. return result;
  846. }
  847. void setChargerMode(unsigned char gun_index, unsigned char mode)
  848. {
  849. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  850. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  851. DEBUG_INFO("Gun-%02d mode switch from %s to %s\r\n", gun_index, getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus), getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus));
  852. }
  853. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  854. {
  855. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  856. }
  857. unsigned char isModeChange(unsigned char gun_index)
  858. {
  859. unsigned char result = NO;
  860. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  861. {
  862. result = YES;
  863. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  864. }
  865. return result;
  866. }
  867. //===============================================
  868. // Get firmware version
  869. //===============================================
  870. void get_firmware_version(unsigned char gun_index)
  871. {
  872. FILE *fp;
  873. char cmd[512];
  874. char buf[512];
  875. // Get CSU hardware version
  876. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  877. // Get CSU boot loader version
  878. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  879. // Get CSU kernel version
  880. sprintf(cmd, "/bin/uname -r");
  881. fp = popen(cmd, "r");
  882. if(fp == NULL)
  883. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  884. else
  885. {
  886. while(fgets(buf, sizeof(buf), fp) != NULL)
  887. {
  888. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  889. }
  890. }
  891. // Get MCU firmware version
  892. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  893. // Get CSU root file system version
  894. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.11.60.6007.BT");
  895. // Get AC connector type from model name
  896. for(uint8_t idx=0;idx<3;idx++)
  897. {
  898. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  899. {
  900. case '1':
  901. // J1772 Plug
  902. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  903. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  904. break;
  905. case '2':
  906. // J1772 Socket
  907. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  908. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  909. break;
  910. case '3':
  911. // CE Plug
  912. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  913. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  914. break;
  915. case '4':
  916. // CE Socket
  917. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  918. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  919. break;
  920. case '5':
  921. // GB Plug
  922. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  923. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  924. break;
  925. case '6':
  926. // GB Socket
  927. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  928. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  929. break;
  930. }
  931. }
  932. // Get network option from model name
  933. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  934. {
  935. case 'B':
  936. case 'U':
  937. //Blue tooth
  938. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  939. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  940. break;
  941. case 'W':
  942. // WIFI
  943. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  944. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  945. break;
  946. case 'T':
  947. // 3G/4G
  948. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  949. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  950. break;
  951. default:
  952. // LAN
  953. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  954. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  955. break;
  956. }
  957. // Get rating power from model name
  958. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  959. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  960. // Get vender code from model name
  961. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  962. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  963. DEBUG_INFO("====================================================================\r\n");
  964. DEBUG_INFO("Model: %s\r\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  965. DEBUG_INFO("CSU hardware version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  966. DEBUG_INFO("CSU boot loader version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  967. DEBUG_INFO("CSU kernel version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  968. DEBUG_INFO("CSU root file system version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  969. DEBUG_INFO("CSU MCU-%2d firmware version: %s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  970. DEBUG_INFO("====================================================================\r\n");
  971. }
  972. //===============================================
  973. // Upgrade firmware
  974. //===============================================
  975. int upgrade_check()
  976. {
  977. int result = PASS;
  978. int fd;
  979. DIR *dir;
  980. struct dirent *file;
  981. char cmd[512];
  982. long int MaxLen=48*1024*1024;
  983. if ((dir = opendir ("/mnt")) != NULL)
  984. {
  985. /* print all the files and directories within directory */
  986. while ((file = readdir (dir)) != NULL)
  987. {
  988. if(strlen(file->d_name)>2)
  989. {
  990. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  991. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  992. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  993. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  994. {
  995. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  996. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  997. read(fd, ptr, MaxLen);
  998. close(fd);
  999. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1000. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1001. DEBUG_INFO("New firmware type: %X\r\n", ShmCharger->fwUpgradeInfo.fwType);
  1002. DEBUG_INFO("New firmware model name: %s, %s\r\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  1003. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  1004. {
  1005. switch(ShmCharger->fwUpgradeInfo.fwType)
  1006. {
  1007. case CSU_BOOTLOADER:
  1008. case CSU_KERNEL_CONFIGURATION:
  1009. case CSU_KERNEL_IMAGE:
  1010. case CSU_ROOT_FILE_SYSTEM:
  1011. case CSU_USER_CONFIGURATION:
  1012. case CSU_PRIMARY_CONTROLLER:
  1013. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  1014. result = FAIL;
  1015. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1016. system(cmd);
  1017. break;
  1018. case AC_WALLMOUNT_CONTROLLER:
  1019. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1020. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  1021. break;
  1022. default:
  1023. result = FAIL;
  1024. DEBUG_WARN("Image file is unknown type.\r\n");
  1025. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1026. system(cmd);
  1027. break;
  1028. }
  1029. }
  1030. else
  1031. {
  1032. result = FAIL;
  1033. DEBUG_ERROR("Model name and Firmware type error.\r\n");
  1034. }
  1035. free(ptr);
  1036. }
  1037. else
  1038. {
  1039. result = FAIL;
  1040. DEBUG_ERROR("New firmware open error.\r\n");
  1041. }
  1042. }
  1043. else
  1044. {
  1045. result = FAIL;
  1046. DEBUG_ERROR("File name error.\r\n");
  1047. }
  1048. }
  1049. closedir (dir);
  1050. }
  1051. else
  1052. {
  1053. result = FAIL;
  1054. DEBUG_ERROR("/mnt does not valid.\r\n");
  1055. }
  1056. return result;
  1057. }
  1058. //===============================================
  1059. // Check RFID is match with start user
  1060. //===============================================
  1061. int isMatchStartUser(unsigned char gun_index)
  1062. {
  1063. uint8_t tmpUser[32];
  1064. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  1065. {
  1066. // Big endian
  1067. switch(rfid.snType)
  1068. {
  1069. case RFID_SN_TYPE_6BYTE:
  1070. 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]);
  1071. break;
  1072. case RFID_SN_TYPE_7BYTE:
  1073. 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]);
  1074. break;
  1075. case RFID_SN_TYPE_10BYTE:
  1076. 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]);
  1077. break;
  1078. case RFID_SN_TYPE_4BYTE:
  1079. default:
  1080. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  1081. break;
  1082. }
  1083. }
  1084. else
  1085. {
  1086. // Little endian
  1087. switch(rfid.snType)
  1088. {
  1089. case RFID_SN_TYPE_6BYTE:
  1090. 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]);
  1091. break;
  1092. case RFID_SN_TYPE_7BYTE:
  1093. 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]);
  1094. break;
  1095. case RFID_SN_TYPE_10BYTE:
  1096. 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]);
  1097. break;
  1098. case RFID_SN_TYPE_4BYTE:
  1099. default:
  1100. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1101. break;
  1102. }
  1103. }
  1104. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  1105. }
  1106. //===============================================
  1107. // Read RFID Serial Number
  1108. //===============================================
  1109. int GetCardSerialNumber()
  1110. {
  1111. int isSuccess = FAIL;
  1112. int module_type = MODULE_EWT;
  1113. if(getRequestCardSN(rfidFd,module_type,&rfid))
  1114. {
  1115. if(rfid.cardType == ISO14443A)
  1116. {
  1117. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  1118. {
  1119. isSuccess = PASS;
  1120. }
  1121. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  1122. {
  1123. isSuccess = PASS;
  1124. }
  1125. }
  1126. else if(rfid.cardType == IS014443B)
  1127. {
  1128. isSuccess = PASS;
  1129. }
  1130. else if(rfid.cardType == FELICA)
  1131. {
  1132. isSuccess = PASS;
  1133. }
  1134. else
  1135. {}
  1136. }
  1137. else
  1138. {}
  1139. return isSuccess;
  1140. }
  1141. //===============================================
  1142. // Check PH RFID
  1143. //===============================================
  1144. int isValidPHCard()
  1145. {
  1146. int isSuccess = FAIL;
  1147. int module_type = MODULE_EWT;
  1148. uint8_t data_read[16];
  1149. uint32_t crc_result;
  1150. uint8_t sn_len;
  1151. uint8_t *buf;
  1152. if(getBlockRead(rfidFd,module_type,2,0,data_read))
  1153. {
  1154. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  1155. {
  1156. // SN length: 4
  1157. sn_len = 4;
  1158. }
  1159. else if(rfid.snType == RFID_SN_TYPE_7BYTE)
  1160. {
  1161. // SN length: 7
  1162. sn_len = 7;
  1163. }
  1164. else
  1165. {
  1166. // SN length: 10
  1167. sn_len = 10;
  1168. }
  1169. buf = malloc(25 + sn_len);
  1170. memcpy(buf, ShmSysConfigAndInfo->SysConfig.SystemId, 25);
  1171. memcpy(&buf[25], rfid.currentCard, sn_len);
  1172. crc_result = crc32(buf, 25 + sn_len);
  1173. if(((data_read[0] << 8) | (data_read[1] << 0) | (data_read[2] << 24) | (data_read[3] << 16)) == ~crc_result)
  1174. {
  1175. isSuccess = PASS;
  1176. }
  1177. free(buf);
  1178. }
  1179. else
  1180. {
  1181. }
  1182. sethaltCard(rfidFd,module_type);
  1183. return isSuccess;
  1184. }
  1185. //===============================================
  1186. // Set led motion
  1187. //===============================================
  1188. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  1189. {
  1190. switch(led_mode)
  1191. {
  1192. case LED_ACTION_INIT:
  1193. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  1194. break;
  1195. case LED_ACTION_IDLE:
  1196. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  1197. break;
  1198. case LED_ACTION_AUTHED:
  1199. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  1200. break;
  1201. case LED_ACTION_CONNECTED:
  1202. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  1203. break;
  1204. case LED_ACTION_CHARGING:
  1205. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  1206. break;
  1207. case LED_ACTION_STOP:
  1208. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  1209. break;
  1210. case LED_ACTION_ALARM:
  1211. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  1212. break;
  1213. case LED_ACTION_MAINTAIN:
  1214. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  1215. break;
  1216. case LED_ACTION_RFID_PASS:
  1217. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  1218. break;
  1219. case LED_ACTION_RFID_FAIL:
  1220. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  1221. break;
  1222. case LED_ACTION_BLE_CONNECT:
  1223. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  1224. break;
  1225. case LED_ACTION_BLE_DISABLE:
  1226. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  1227. break;
  1228. case LED_ACTION_DEBUG:
  1229. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  1230. break;
  1231. case LED_ACTION_ALL_OFF:
  1232. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  1233. break;
  1234. case LED_RELAY_ON:
  1235. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  1236. break;
  1237. case LED_RELAY_OFF:
  1238. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  1239. break;
  1240. case LED_ACTION_HANDSHAKE_FAIL:
  1241. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  1242. break;
  1243. case LED_ACTION_INTERNET_DISCONNECT:
  1244. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  1245. break;
  1246. }
  1247. }
  1248. //===============================================
  1249. // Relay on/off request set
  1250. //===============================================
  1251. void setRelay(unsigned char gun_index,unsigned char isOn)
  1252. {
  1253. if(isOn == ON)
  1254. {
  1255. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  1256. {
  1257. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  1258. DEBUG_INFO("Gun-%d Output relay sts: ON \r\n",gun_index);
  1259. }
  1260. }
  1261. else
  1262. {
  1263. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  1264. {
  1265. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  1266. DEBUG_INFO("Gun-%d Output relay sts: OFF \r\n",gun_index);
  1267. }
  1268. }
  1269. }
  1270. //===============================================
  1271. // Relay on/off request get
  1272. //===============================================
  1273. int getRelay(unsigned char gun_index)
  1274. {
  1275. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  1276. }
  1277. //===============================================
  1278. // Set speaker on/off request
  1279. //===============================================
  1280. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  1281. {
  1282. if(isOn == ON)
  1283. {
  1284. ShmCharger->isSpeakerOn = ON;
  1285. ShmCharger->speaker_type = speaker_mode;
  1286. }
  1287. }
  1288. //===============================================
  1289. // Initialization RFID communication port
  1290. //===============================================
  1291. int InitRfidPort()
  1292. {
  1293. int uartO2 = open(rfidPortName, O_RDWR);
  1294. struct termios tios;
  1295. if (uartO2 != FAIL)
  1296. {
  1297. ioctl (uartO2, TCGETS, &tios);
  1298. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1299. tios.c_lflag = 0;
  1300. tios.c_iflag = 0;
  1301. tios.c_oflag = 0;
  1302. tios.c_cc[VMIN] = 0;
  1303. tios.c_cc[VTIME] = (unsigned char) 1;
  1304. tios.c_lflag = 0;
  1305. tcflush(uartO2, TCIFLUSH);
  1306. ioctl(uartO2, TCSETS, &tios);
  1307. }
  1308. if (uartO2 < 0)
  1309. {
  1310. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1311. }
  1312. return uartO2;
  1313. }
  1314. //===============================================
  1315. // Check internet access status
  1316. //===============================================
  1317. int isReachableInternet()
  1318. {
  1319. int result = FAIL;
  1320. FILE *fp;
  1321. char cmd[256];
  1322. char buf[512];
  1323. //char tmp[512];
  1324. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1325. {
  1326. strcpy(cmd, "ping -c 1 -w 3 ");
  1327. strcat(cmd, valid_Internet[idx]);
  1328. fp = popen(cmd, "r");
  1329. if(fp != NULL)
  1330. {
  1331. while(fgets(buf, sizeof(buf), fp) != NULL)
  1332. {
  1333. if(strstr(buf, "transmitted") > 0)
  1334. {
  1335. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1336. if(strstr(buf,"100%") != NULL)
  1337. {
  1338. }
  1339. else
  1340. {
  1341. result = PASS;
  1342. }
  1343. //DEBUG_INFO("%s",buf);
  1344. //DEBUG_INFO("%s\n",tmp);
  1345. }
  1346. }
  1347. }
  1348. pclose(fp);
  1349. }
  1350. return result;
  1351. }
  1352. //===============================================
  1353. // Check reservation date is expired
  1354. //===============================================
  1355. int isReservationExpired(unsigned char gun_index)
  1356. {
  1357. int result = NO;
  1358. struct tm expiredDate;
  1359. struct timeb expiredTime;
  1360. 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)
  1361. {
  1362. expiredDate.tm_year -= 1900;
  1363. expiredDate.tm_mon -= 1;
  1364. expiredTime.time = mktime(&expiredDate);
  1365. if(DiffTimebWithNow(expiredTime)>=0)
  1366. {
  1367. result = YES;
  1368. }
  1369. }
  1370. else
  1371. {
  1372. DEBUG_WARN("Expired date parsing error.\r\n");
  1373. DEBUG_INFO("Date expired.\r\n");
  1374. result = YES;
  1375. }
  1376. return result;
  1377. }
  1378. //===============================================
  1379. // Check charging profile related date routine
  1380. //===============================================
  1381. int isProfileValid(uint8_t gun_index)
  1382. {
  1383. int result = NO;
  1384. struct tm tmFrom, tmTo;
  1385. struct timeb tbFrom, tbTo;
  1386. 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) &&
  1387. (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))
  1388. {
  1389. tmFrom.tm_year -= 1900;
  1390. tmFrom.tm_mon -= 1;
  1391. tbFrom.time = mktime(&tmFrom);
  1392. tmTo.tm_year -= 1900;
  1393. tmTo.tm_mon -= 1;
  1394. tbTo.time = mktime(&tmTo);
  1395. DEBUG_INFO("Valid from compare Now: %d\r\n", DiffTimebWithNow(tbFrom));
  1396. DEBUG_INFO("Valid to compare Now: %d\r\n", DiffTimebWithNow(tbTo));
  1397. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  1398. {
  1399. result = YES;
  1400. }
  1401. }
  1402. else
  1403. {
  1404. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\r\n");
  1405. }
  1406. return result;
  1407. }
  1408. int getScheduleStart(int gun_index)
  1409. {
  1410. int result = -1;
  1411. struct tm tmScheduleStart;;
  1412. struct timeb tbScheduleStart;
  1413. 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))
  1414. {
  1415. tmScheduleStart.tm_year -= 1900;
  1416. tmScheduleStart.tm_mon -= 1;
  1417. tbScheduleStart.time = mktime(&tmScheduleStart);
  1418. result = DiffTimebWithNow(tbScheduleStart)/1000;
  1419. DEBUG_INFO("Schedule start compare Now(seconds): %d\r\n", result);
  1420. }
  1421. else
  1422. {
  1423. DEBUG_WARN("Schedule start date parsing error.\r\n");
  1424. }
  1425. return result;
  1426. }
  1427. int getStartSinceToday()
  1428. {
  1429. int result = -1;
  1430. time_t t;
  1431. struct tm *tmStartToday;
  1432. struct timeb tbStartToday;
  1433. t=time(NULL);
  1434. tmStartToday=localtime(&t);
  1435. tmStartToday->tm_hour = 0;
  1436. tmStartToday->tm_min = 0;
  1437. tmStartToday->tm_sec = 0;
  1438. tbStartToday.time = mktime(tmStartToday);
  1439. result = DiffTimebWithNow(tbStartToday)/1000;
  1440. DEBUG_INFO("Start today compare Now(seconds): %d\r\n", result);
  1441. return result;
  1442. }
  1443. int getStartSinceWeek()
  1444. {
  1445. int result = -1;
  1446. time_t t;
  1447. struct tm *tmStartWeek;
  1448. struct timeb tbStartWeek;
  1449. t=time(NULL);
  1450. tmStartWeek=localtime(&t);
  1451. tmStartWeek->tm_wday = 0;
  1452. tmStartWeek->tm_hour = 0;
  1453. tmStartWeek->tm_min = 0;
  1454. tmStartWeek->tm_sec = 0;
  1455. tbStartWeek.time = mktime(tmStartWeek);
  1456. result = DiffTimebWithNow(tbStartWeek)/1000;
  1457. DEBUG_INFO("Start week compare Now(seconds): %d\r\n", result);
  1458. return result;
  1459. }
  1460. //===============================================
  1461. // Valid from local white list
  1462. //===============================================
  1463. int isValidLocalWhiteCard()
  1464. {
  1465. uint8_t result = FAIL;
  1466. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  1467. {
  1468. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  1469. {
  1470. result = PASS;
  1471. }
  1472. }
  1473. return result;
  1474. }
  1475. //==========================================
  1476. // Get Ethernet MAC address
  1477. //==========================================
  1478. int getEth0MacAddress()
  1479. {
  1480. int result = PASS;
  1481. FILE *fp;
  1482. char cmd[256];
  1483. char buf[512];
  1484. char tmp[512];
  1485. strcpy(cmd, "ifconfig");;
  1486. fp = popen(cmd, "r");
  1487. if(fp != NULL)
  1488. {
  1489. while(fgets(buf, sizeof(buf), fp) != NULL)
  1490. {
  1491. if(strstr(buf, "eth0") > 0)
  1492. {
  1493. result = PASS;
  1494. }
  1495. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  1496. {
  1497. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  1498. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  1499. }
  1500. }
  1501. }
  1502. pclose(fp);
  1503. return result;
  1504. }
  1505. //==========================================
  1506. // Check task processing
  1507. //==========================================
  1508. void checkTask()
  1509. {
  1510. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1511. {
  1512. if(system("pidof -s Module_4g > /dev/null") != 0)
  1513. {
  1514. DEBUG_INFO("Module_4g not running, restart it.\r\n");
  1515. system("/root/Module_4g &");
  1516. }
  1517. }
  1518. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1519. {
  1520. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  1521. {
  1522. DEBUG_INFO("Module_Wifi not running, restart it.\r\n");
  1523. system("/root/Module_Wifi &");
  1524. }
  1525. }
  1526. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  1527. {
  1528. DEBUG_INFO("Module_EventLogging not running, restart it.\r\n");
  1529. system("/root/Module_EventLogging &");
  1530. }
  1531. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1532. {
  1533. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1534. {
  1535. DEBUG_INFO("OcppBackend not running, restart it.\r\n");
  1536. system("/root/OcppBackend &");
  1537. }
  1538. }
  1539. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  1540. {
  1541. DEBUG_INFO("Module_AlarmDetect not running, restart it.\r\n");
  1542. system("/root/Module_AlarmDetect &");
  1543. }
  1544. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  1545. {
  1546. DEBUG_INFO("Module_InternalComm not running, restart it.\r\n");
  1547. system("/root/Module_InternalComm &");
  1548. }
  1549. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  1550. {
  1551. DEBUG_INFO("Module_Speaker not running, restart it.\r\n");
  1552. system("/root/Module_Speaker &");
  1553. }
  1554. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  1555. {
  1556. DEBUG_INFO("Module_ProduceUtils not running, restart it.\r\n");
  1557. system ("/root/Module_ProduceUtils &");
  1558. }
  1559. }
  1560. //===============================================
  1561. // Check Connection Timeout
  1562. //===============================================
  1563. void checkConnectionTimeout()
  1564. {
  1565. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1566. {
  1567. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1568. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \r\n", TIMEOUT_SPEC_HANDSHAKING/1000);
  1569. }
  1570. else
  1571. {
  1572. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  1573. {
  1574. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1575. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  1576. {
  1577. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1578. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\r\n.");
  1579. }
  1580. else
  1581. {
  1582. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  1583. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\r\n.");
  1584. }
  1585. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \r\n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1586. }
  1587. else
  1588. {
  1589. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1590. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\r\n.");
  1591. }
  1592. //DEBUG_INFO("Present timeout spec : %d \r\n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  1593. }
  1594. }
  1595. //===============================================
  1596. // Main process
  1597. //===============================================
  1598. int main(void)
  1599. {
  1600. //Create all share memory
  1601. if(CreatShareMemory()==0)
  1602. {
  1603. DEBUG_ERROR("CreatShareMemory NG\r\n");
  1604. if(ShmStatusCodeData!=NULL)
  1605. {
  1606. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1607. }
  1608. sleep(5);
  1609. system("reboot -f");
  1610. sleep(5);
  1611. system("reboot -f");
  1612. }
  1613. else
  1614. {
  1615. DEBUG_INFO("CreatShareMemory OK\r\n");
  1616. }
  1617. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1618. {
  1619. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  1620. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  1621. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  1622. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  1623. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  1624. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  1625. }
  1626. // Main loop
  1627. for(;;)
  1628. {
  1629. //==========================================
  1630. // Synchronize share memory from OCPP struct
  1631. //==========================================
  1632. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ShmOCPP16Data->OcppConnStatus;
  1633. //==========================================
  1634. // Ethernet Mac address
  1635. //==========================================
  1636. getEth0MacAddress();
  1637. //==========================================
  1638. // Check task processing
  1639. //==========================================
  1640. checkTask();
  1641. //==========================================
  1642. // Check timeout spec
  1643. //==========================================
  1644. checkConnectionTimeout();
  1645. //==========================================
  1646. // Something need run in Idle mode
  1647. //==========================================
  1648. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  1649. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  1650. {
  1651. //======================================
  1652. // Check restore factory setting request
  1653. //======================================
  1654. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  1655. {
  1656. system("cd /root;./Module_FactoryConfig -m");
  1657. system("sync");
  1658. sleep(5);
  1659. system("reboot -f");
  1660. sleep(5);
  1661. system("reboot -f");
  1662. }
  1663. //======================================
  1664. // Check upgrade firmware request
  1665. //======================================
  1666. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  1667. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  1668. {
  1669. ShmCharger->isUpdateSuccess = FAIL;
  1670. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1671. {
  1672. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  1673. setChargerMode(gun_index, SYS_MODE_UPDATE);
  1674. }
  1675. }
  1676. }
  1677. //==========================================
  1678. // Check remote reset request
  1679. //==========================================
  1680. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  1681. {
  1682. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING)) &&
  1683. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING)):true))
  1684. {
  1685. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  1686. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  1687. {
  1688. system("sync");
  1689. sleep(5);
  1690. system("reboot -f");
  1691. sleep(5);
  1692. system("reboot -f");
  1693. }
  1694. else
  1695. {
  1696. system("/usr/bin/run_evse_restart.sh");
  1697. }
  1698. }
  1699. }
  1700. //==========================================
  1701. // Check RFID
  1702. //==========================================
  1703. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  1704. {
  1705. if(GetCardSerialNumber()!= FAIL)
  1706. {
  1707. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  1708. }
  1709. }
  1710. //==========================================
  1711. // Connector loop
  1712. //==========================================
  1713. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1714. {
  1715. // Synchronize current rating value from MCU
  1716. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1717. // Assign connector location index for OCPP
  1718. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  1719. // Synchronize present charging power
  1720. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  1721. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  1722. else
  1723. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  1724. // The system switch to Booting mode
  1725. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == 0XFF)
  1726. {
  1727. setChargerMode(gun_index, SYS_MODE_BOOTING);
  1728. }
  1729. // Check initialization "PASS" or "FAIL"
  1730. if(ShmCharger->gun_info[gun_index].isInitialPass == YES)
  1731. {
  1732. // Alarm event check
  1733. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1734. {
  1735. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  1736. {
  1737. setChargerMode(gun_index, SYS_MODE_ALARM);
  1738. }
  1739. }
  1740. else
  1741. {
  1742. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM)
  1743. {
  1744. }
  1745. }
  1746. }
  1747. // Reservation request check
  1748. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  1749. {
  1750. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  1751. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  1752. {
  1753. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  1754. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  1755. }
  1756. DEBUG_INFO("Reservation request on connector-%d.\r\n", gun_index);
  1757. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  1758. }
  1759. // Change availability check
  1760. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  1761. {
  1762. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  1763. {
  1764. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  1765. {
  1766. setChargerMode(gun_index, SYS_MODE_IDLE);
  1767. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  1768. }
  1769. }
  1770. else
  1771. {
  1772. if(isMode(gun_index, SYS_MODE_IDLE))
  1773. {
  1774. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  1775. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  1776. }
  1777. }
  1778. }
  1779. // Charging profile preparation
  1780. if(!ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf)
  1781. {
  1782. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
  1783. }
  1784. else
  1785. {
  1786. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = OFF;
  1787. }
  1788. // Connector process
  1789. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  1790. {
  1791. case SYS_MODE_BOOTING:
  1792. if(isModeChange(gun_index))
  1793. {
  1794. setLedMotion(gun_index,LED_ACTION_INIT);
  1795. //CSU Initialization & task spawn
  1796. if((Initialization() != PASS) ||
  1797. (SpawnTask() != PASS) )
  1798. {
  1799. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  1800. }
  1801. }
  1802. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  1803. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  1804. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  1805. {
  1806. ShmCharger->gun_selectd = 0;
  1807. // Firmware version
  1808. get_firmware_version(gun_index);
  1809. // OCPP BootNotification info set
  1810. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  1811. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1812. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1813. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  1814. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "AC");
  1815. DEBUG_INFO("==========================================\r\n");
  1816. DEBUG_INFO("System ID: %s\r\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  1817. DEBUG_INFO("==========================================\r\n");
  1818. DEBUG_INFO("========== OCPP BootNotification information ==========\r\n");
  1819. DEBUG_INFO("OCPP OcppServerURL: %s\r\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  1820. DEBUG_INFO("OCPP ChargeBoxId: %s\r\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1821. DEBUG_INFO("OCPP ChargePointVendor: %s\r\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1822. DEBUG_INFO("OCPP CpFwVersion: %s\r\n",ShmOCPP16Data->BootNotification.CpFwVersion);
  1823. DEBUG_INFO("OCPP CpMeterSerialNumber: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterSerialNumber);
  1824. DEBUG_INFO("OCPP CpMeterType: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterType);
  1825. DEBUG_INFO("=======================================================\r\n");
  1826. DEBUG_INFO("========== Set Wifi information ==========\r\n");
  1827. DEBUG_INFO("Wifi mode: %d\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  1828. DEBUG_INFO("Wifi SSID: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  1829. DEBUG_INFO("Wifi password: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  1830. DEBUG_INFO("==========================================\r\n");
  1831. // Flag for initialization system success
  1832. ShmCharger->gun_info[gun_index].isInitialPass = YES;
  1833. // Set max current to rating current
  1834. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1835. // If rotate switch equal zero, the system needs to change Debug mode
  1836. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  1837. setChargerMode(gun_index, SYS_MODE_DEBUG);
  1838. else
  1839. setChargerMode(gun_index, SYS_MODE_IDLE);
  1840. }
  1841. break;
  1842. case SYS_MODE_IDLE:
  1843. if(isModeChange(gun_index))
  1844. {
  1845. setLedMotion(gun_index,LED_ACTION_IDLE);
  1846. setRelay(gun_index,OFF);
  1847. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  1848. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  1849. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  1850. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  1851. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  1852. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  1853. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  1854. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  1855. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  1856. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  1857. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1858. // Response StopTransactionConf
  1859. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf == ON)
  1860. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = OFF;
  1861. ShmCharger->gun_info[gun_index].isHandshakeTimeOn = OFF;
  1862. }
  1863. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_FREE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  1864. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_FREE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  1865. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  1866. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  1867. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  1868. {
  1869. // Clean User id & Card Number
  1870. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1871. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  1872. {
  1873. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  1874. DEBUG_INFO("Start Method : RFID...\r\n");
  1875. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  1876. {
  1877. // Big endian
  1878. switch(rfid.snType)
  1879. {
  1880. case RFID_SN_TYPE_6BYTE:
  1881. 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]);
  1882. break;
  1883. case RFID_SN_TYPE_7BYTE:
  1884. 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]);
  1885. break;
  1886. case RFID_SN_TYPE_10BYTE:
  1887. 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]);
  1888. break;
  1889. case RFID_SN_TYPE_4BYTE:
  1890. default:
  1891. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  1892. break;
  1893. }
  1894. }
  1895. else
  1896. {
  1897. // Little endian
  1898. switch(rfid.snType)
  1899. {
  1900. case RFID_SN_TYPE_6BYTE:
  1901. 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]);
  1902. break;
  1903. case RFID_SN_TYPE_7BYTE:
  1904. 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]);
  1905. break;
  1906. case RFID_SN_TYPE_10BYTE:
  1907. 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]);
  1908. break;
  1909. case RFID_SN_TYPE_4BYTE:
  1910. default:
  1911. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1912. break;
  1913. }
  1914. }
  1915. DEBUG_INFO("Start request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  1916. }
  1917. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  1918. {
  1919. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  1920. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  1921. DEBUG_INFO("Start Method : BACKEND...\r\n");
  1922. }
  1923. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  1924. {
  1925. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  1926. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  1927. DEBUG_INFO("Start Method : BLE...\r\n");
  1928. }
  1929. else
  1930. {
  1931. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  1932. DEBUG_INFO("Start Method : FREE..\r\n");
  1933. }
  1934. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  1935. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  1936. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  1937. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  1938. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  1939. }
  1940. else
  1941. {}
  1942. break;
  1943. case SYS_MODE_AUTHORIZING:
  1944. if(isModeChange(gun_index))
  1945. {
  1946. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  1947. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  1948. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  1949. {
  1950. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  1951. {
  1952. case AUTH_MODE_BACKEND_OCPP:
  1953. if(ShmOCPP16Data->OcppConnStatus)
  1954. {
  1955. // On line
  1956. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  1957. }
  1958. else
  1959. {
  1960. // Off line
  1961. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  1962. {
  1963. case OFF_POLICY_LOCALLIST:
  1964. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  1965. break;
  1966. case OFF_POLICY_PH_RFID:
  1967. break;
  1968. case OFF_POLICY_FREE:
  1969. break;
  1970. case OFF_POLICY_NOCHARGE:
  1971. default:
  1972. break;
  1973. }
  1974. }
  1975. break;
  1976. case AUTH_MODE_FREE:
  1977. default:
  1978. break;
  1979. }
  1980. }
  1981. }
  1982. if(ShmCharger->gun_info[gun_index].isHandshakeTimeOn == OFF)
  1983. {
  1984. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  1985. {
  1986. // Authorization timeout process.
  1987. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  1988. setChargerMode(gun_index, SYS_MODE_IDLE);
  1989. DEBUG_WARN("Authorize timeout!\r\n");
  1990. }
  1991. else
  1992. {
  1993. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  1994. {
  1995. case START_METHOD_RFID:
  1996. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  1997. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_FREE) ||
  1998. !ShmOCPP16Data->OcppConnStatus ||
  1999. (((isValidLocalWhiteCard() == PASS) || (isValidPHCard() == PASS)) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  2000. {
  2001. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2002. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_FREE) ||
  2003. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2004. (((isValidLocalWhiteCard() == PASS)||(isValidPHCard() == PASS)) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  2005. {
  2006. DEBUG_INFO("Authorize pass.\r\n");
  2007. //setSpeaker(ON,SPEAKER_SHORT);
  2008. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2009. sleep(3);
  2010. //setChargerMode(gun_index, SYS_MODE_PREPARING);
  2011. setLedMotion(gun_index,LED_ACTION_AUTHED);
  2012. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2013. ShmCharger->gun_info[gun_index].isHandshakeTimeOn = ON;
  2014. }
  2015. else
  2016. {
  2017. DEBUG_INFO("Authorize fail.\r\n");
  2018. //setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2019. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2020. sleep(3);
  2021. setChargerMode(gun_index, SYS_MODE_IDLE);
  2022. }
  2023. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2024. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2025. }
  2026. break;
  2027. case START_METHOD_BACKEND:
  2028. case START_METHOD_BLE:
  2029. case START_METHOD_FREE:
  2030. default:
  2031. //setSpeaker(ON,SPEAKER_SHORT);
  2032. //setChargerMode(gun_index, SYS_MODE_PREPARING);
  2033. setLedMotion(gun_index,LED_ACTION_AUTHED);
  2034. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2035. ShmCharger->gun_info[gun_index].isHandshakeTimeOn = ON;
  2036. break;
  2037. }
  2038. }
  2039. }
  2040. else
  2041. {
  2042. // If control pilot detect Bx, skip watch dog time out.
  2043. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  2044. {
  2045. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2046. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  2047. }
  2048. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))&& (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  2049. {
  2050. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2051. }
  2052. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  2053. {
  2054. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2055. sleep(5);
  2056. //if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  2057. //{
  2058. DEBUG_INFO("Handshaking timeout...");
  2059. //setRelay(gun_index, OFF);
  2060. setChargerMode(gun_index, SYS_MODE_IDLE);
  2061. //}
  2062. }
  2063. }
  2064. break;
  2065. case SYS_MODE_PREPARING:
  2066. if(isModeChange(gun_index))
  2067. {
  2068. //ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2069. setRelay(gun_index, ON);
  2070. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  2071. }
  2072. // If control pilot detect Bx, skip watch dog time out.
  2073. /*if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)
  2074. {
  2075. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2076. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  2077. }*/
  2078. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  2079. {
  2080. DEBUG_INFO("Charging gun is plugged before.\r\n");
  2081. //setRelay(gun_index, OFF);
  2082. setChargerMode(gun_index, SYS_MODE_IDLE);
  2083. }
  2084. /*if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  2085. {
  2086. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2087. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  2088. {
  2089. DEBUG_INFO("Handshaking timeout...");
  2090. setChargerMode(gun_index, SYS_MODE_IDLE);
  2091. }
  2092. }
  2093. else*/ if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  2094. {
  2095. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].OutputEnergy = 0;
  2096. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2097. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  2098. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  2099. ShmOCPP16Data->StartTransaction[gun_index].MeterStart = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2100. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2101. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  2102. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = ON;
  2103. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2104. }
  2105. break;
  2106. case SYS_MODE_CHARGING:
  2107. if(isModeChange(gun_index))
  2108. {
  2109. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2110. ftime(&startChargingTime[gun_index]);
  2111. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2112. }
  2113. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2114. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2115. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)||
  2116. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  2117. ShmOCPP16Data->MsMsg.bits.ResetReq)
  2118. {
  2119. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2120. }
  2121. else
  2122. {
  2123. setLedMotion(gun_index,LED_ACTION_CHARGING);
  2124. ShmOCPP16Data->StopTransaction[gun_index].MeterStop = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start)/100;
  2125. ftime(&endChargingTime[gun_index]);
  2126. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2127. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start))/100;
  2128. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].OutputEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start))/100;
  2129. // Response StartTransactionConf
  2130. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
  2131. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = OFF;
  2132. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  2133. if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId>0) &&
  2134. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2135. {
  2136. DEBUG_INFO("Profile ID found: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2137. DEBUG_INFO("Valid from: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2138. DEBUG_INFO("Valid to: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2139. DEBUG_INFO("Start schedule: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2140. DEBUG_INFO("Profile kind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2141. DEBUG_INFO("RecurrencyKind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2142. DEBUG_INFO("Profile purpose: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2143. DEBUG_INFO("Transaction ID: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2144. DEBUG_INFO("ChargingRateUnit: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2145. // Checking profile kind
  2146. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2147. {
  2148. // Absolute profile
  2149. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2150. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2151. {
  2152. // Checking limitation
  2153. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2154. {
  2155. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2156. (((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)))
  2157. {
  2158. 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);
  2159. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2160. }
  2161. }
  2162. }
  2163. }
  2164. else if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Relative") == PASS))
  2165. {
  2166. // Relative profile
  2167. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2168. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2169. {
  2170. // Checking limitation
  2171. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2172. {
  2173. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2174. (((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)))
  2175. {
  2176. 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);
  2177. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2178. }
  2179. }
  2180. }
  2181. }
  2182. else
  2183. {
  2184. // Recurring profile
  2185. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2186. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2187. {
  2188. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind, "Daily") == PASS))
  2189. {
  2190. // Checking limitation
  2191. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2192. {
  2193. if((getStartSinceToday() > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2194. (((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)))
  2195. {
  2196. 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);
  2197. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2198. }
  2199. }
  2200. }
  2201. else
  2202. {
  2203. // Checking limitation
  2204. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2205. {
  2206. if((getStartSinceWeek() > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2207. (((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)))
  2208. {
  2209. 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);
  2210. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2211. }
  2212. }
  2213. }
  2214. }
  2215. }
  2216. }
  2217. // Determine max charging current to MCU
  2218. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2219. {
  2220. 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);
  2221. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2222. {
  2223. 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);
  2224. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2225. }
  2226. }
  2227. else
  2228. {
  2229. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2230. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2231. {
  2232. 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);
  2233. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2234. }
  2235. }
  2236. // Debug information
  2237. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2238. {
  2239. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2240. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  2241. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  2242. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2243. }
  2244. if(ShmOCPP16Data->OcppConnStatus)
  2245. {
  2246. // On-line max condition check
  2247. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  2248. {
  2249. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2250. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  2251. }
  2252. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  2253. {
  2254. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2255. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  2256. }
  2257. else
  2258. {
  2259. setRelay(gun_index, ON);
  2260. }
  2261. }
  2262. else
  2263. {
  2264. // Off-line max condition check
  2265. if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)
  2266. {
  2267. 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))))
  2268. {
  2269. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2270. DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  2271. }
  2272. 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))))
  2273. {
  2274. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2275. DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  2276. }
  2277. else
  2278. {
  2279. setRelay(gun_index, ON);
  2280. }
  2281. }
  2282. else
  2283. {
  2284. setRelay(gun_index, OFF);
  2285. DEBUG_INFO("Connector-%d can not charging in off line\r\n", gun_index);
  2286. }
  2287. }
  2288. }
  2289. break;
  2290. case SYS_MODE_TERMINATING:
  2291. if(isModeChange(gun_index))
  2292. {
  2293. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  2294. {
  2295. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2296. }
  2297. setLedMotion(gun_index, LED_ACTION_STOP);
  2298. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  2299. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  2300. }
  2301. // If RFID SN different with start user, it need to authorize ID
  2302. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  2303. {
  2304. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  2305. {
  2306. // Request authorization
  2307. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2308. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2309. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2310. {
  2311. // Big endian
  2312. switch(rfid.snType)
  2313. {
  2314. case RFID_SN_TYPE_6BYTE:
  2315. 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]);
  2316. break;
  2317. case RFID_SN_TYPE_7BYTE:
  2318. 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]);
  2319. break;
  2320. case RFID_SN_TYPE_10BYTE:
  2321. 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]);
  2322. break;
  2323. case RFID_SN_TYPE_4BYTE:
  2324. default:
  2325. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2326. break;
  2327. }
  2328. }
  2329. else
  2330. {
  2331. // Little endian
  2332. switch(rfid.snType)
  2333. {
  2334. case RFID_SN_TYPE_6BYTE:
  2335. 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]);
  2336. break;
  2337. case RFID_SN_TYPE_7BYTE:
  2338. 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]);
  2339. break;
  2340. case RFID_SN_TYPE_10BYTE:
  2341. 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]);
  2342. break;
  2343. case RFID_SN_TYPE_4BYTE:
  2344. default:
  2345. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2346. break;
  2347. }
  2348. }
  2349. DEBUG_INFO("End request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2350. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  2351. {
  2352. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2353. {
  2354. case AUTH_MODE_BACKEND_OCPP:
  2355. if(ShmOCPP16Data->OcppConnStatus)
  2356. {
  2357. // On line
  2358. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2359. }
  2360. else
  2361. {
  2362. // Off line
  2363. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2364. {
  2365. case OFF_POLICY_LOCALLIST:
  2366. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2367. break;
  2368. case OFF_POLICY_PH_RFID:
  2369. break;
  2370. case OFF_POLICY_FREE:
  2371. break;
  2372. case OFF_POLICY_NOCHARGE:
  2373. default:
  2374. break;
  2375. }
  2376. }
  2377. break;
  2378. case AUTH_MODE_FREE:
  2379. default:
  2380. break;
  2381. }
  2382. }
  2383. }
  2384. else
  2385. {
  2386. if((ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  2387. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_FREE) ||
  2388. !ShmOCPP16Data->OcppConnStatus ) &&
  2389. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod != START_METHOD_BLE))
  2390. {
  2391. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2392. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_FREE) ||
  2393. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2394. {
  2395. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  2396. }
  2397. else
  2398. {
  2399. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2400. }
  2401. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2402. }
  2403. }
  2404. }
  2405. // End authorize pass
  2406. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) && (ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2407. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  2408. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE) && (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)) ||
  2409. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON) ||
  2410. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) ||
  2411. ShmOCPP16Data->MsMsg.bits.ResetReq)
  2412. {
  2413. //if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2414. //setSpeaker(ON,SPEAKER_SHORT);
  2415. setRelay(gun_index, OFF);
  2416. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2417. {
  2418. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_A) )
  2419. {
  2420. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2421. sleep(3);
  2422. }
  2423. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2424. {
  2425. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  2426. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  2427. else
  2428. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  2429. }
  2430. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  2431. {
  2432. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  2433. }
  2434. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  2435. {
  2436. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  2437. }
  2438. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2439. {
  2440. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  2441. }
  2442. else
  2443. {
  2444. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  2445. }
  2446. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  2447. ShmOCPP16Data->StopTransaction[gun_index].MeterStop = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2448. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  2449. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2450. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  2451. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  2452. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  2453. DB_Insert_Record(localDb, gun_index);
  2454. setChargerMode(gun_index, SYS_MODE_IDLE);
  2455. }
  2456. }
  2457. else
  2458. {
  2459. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  2460. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  2461. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != ON) &&
  2462. !ShmOCPP16Data->MsMsg.bits.ResetReq &&
  2463. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  2464. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  2465. !(!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)))) &&
  2466. !(!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)))))
  2467. {
  2468. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2469. }
  2470. }
  2471. break;
  2472. case SYS_MODE_ALARM:
  2473. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  2474. {
  2475. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  2476. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  2477. {
  2478. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  2479. }
  2480. else
  2481. {
  2482. setChargerMode(gun_index, SYS_MODE_IDLE);
  2483. }
  2484. }
  2485. setLedMotion(gun_index,LED_ACTION_ALARM);
  2486. break;
  2487. case SYS_MODE_FAULT:
  2488. if(isModeChange(gun_index))
  2489. {}
  2490. break;
  2491. case SYS_MODE_MAINTAIN:
  2492. if(isModeChange(gun_index))
  2493. {
  2494. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  2495. }
  2496. break;
  2497. case SYS_MODE_UPDATE:
  2498. if(isModeChange(gun_index))
  2499. {
  2500. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  2501. }
  2502. //======================================
  2503. // Check local upgrade firmware request
  2504. //======================================
  2505. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  2506. {
  2507. DEBUG_INFO("Firmware local upgrading...\r\n");
  2508. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  2509. if(upgrade_check())
  2510. {
  2511. ShmCharger->isUpdateSuccess = PASS;
  2512. }
  2513. else
  2514. {
  2515. ShmCharger->isUpdateSuccess = FAIL;
  2516. }
  2517. }
  2518. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  2519. {
  2520. //======================================
  2521. // Check remote upgrade firmware request
  2522. //======================================
  2523. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  2524. {
  2525. DEBUG_INFO("Firmware remote upgraded fail...\r\n");
  2526. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2527. ShmCharger->isUpdateSuccess = FAIL;
  2528. }
  2529. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  2530. {
  2531. DEBUG_INFO("Firmware remote upgrading...\r\n");
  2532. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  2533. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2534. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  2535. if(upgrade_check())
  2536. {
  2537. ShmCharger->isUpdateSuccess = PASS;
  2538. }
  2539. else
  2540. {
  2541. ShmCharger->isUpdateSuccess = FAIL;
  2542. }
  2543. }
  2544. else
  2545. {}
  2546. }
  2547. else
  2548. {
  2549. //======================================
  2550. // Upgrade complete reboot system
  2551. //======================================
  2552. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  2553. {
  2554. if(ShmCharger->isUpdateSuccess == PASS)
  2555. {
  2556. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  2557. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2558. DEBUG_WARN("Firmware upgrade success.\r\n");
  2559. sleep(5);
  2560. DEBUG_INFO("Firmware upgraded, reboot...\r\n");
  2561. system("reboot -f");
  2562. sleep(5);
  2563. system("reboot -f");
  2564. }
  2565. else
  2566. {
  2567. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  2568. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  2569. DEBUG_WARN("Firmware upgrade fail.\r\n");
  2570. system("/usr/bin/run_evse_restart.sh");
  2571. }
  2572. }
  2573. }
  2574. break;
  2575. case SYS_MODE_RESERVATION:
  2576. if(isModeChange(gun_index))
  2577. {
  2578. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  2579. }
  2580. if(isReservationExpired(gun_index))
  2581. {
  2582. DEBUG_INFO("Reservation: Time's up...\r\n");
  2583. setChargerMode(gun_index, SYS_MODE_IDLE);
  2584. }
  2585. else if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq)
  2586. {
  2587. DEBUG_INFO("Reservation: Cancel reservation...\r\n");
  2588. setChargerMode(gun_index, SYS_MODE_IDLE);
  2589. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = OFF;
  2590. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  2591. }
  2592. break;
  2593. case SYS_MODE_BOOKING:
  2594. if(isModeChange(gun_index))
  2595. {}
  2596. break;
  2597. case SYS_MODE_DEBUG:
  2598. if(isModeChange(gun_index))
  2599. {
  2600. setLedMotion(gun_index,LED_ACTION_DEBUG);
  2601. }
  2602. break;
  2603. }
  2604. }
  2605. // System watch dog reset
  2606. write(wtdFd, "a", 1);
  2607. usleep(50000);
  2608. }
  2609. return FAIL;
  2610. }