main.c 88 KB

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