main.c 108 KB

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