main.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/time.h>
  4. #include <sys/timeb.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include <ifaddrs.h>
  27. #include <math.h>
  28. #include "../../define.h"
  29. #include "Config.h"
  30. #include <stdbool.h>
  31. #include <dirent.h>
  32. #include "timeout.h"
  33. #define Debug
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define BUFFER_SIZE 128
  38. #define YES 1
  39. #define NO 0
  40. #define NORMAL 0
  41. #define ABNORMAL 1
  42. #define EQUAL 0
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  47. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  48. int whileLoopTime = 10000; // 10 ms
  49. bool IsAuthorizingMode();
  50. void ClearAuthorizedFlag();
  51. bool isDetectPlugin();
  52. void ClearDetectPluginFlag();
  53. int mystrcmp(unsigned char *p1, unsigned char *p2);
  54. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  55. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  56. void ChargingTerminalProcess(byte gunIndex);
  57. void ChkPrimaryStatus();
  58. void StartSystemTimeoutDet(unsigned char flag);
  59. void StopSystemTimeoutDet();
  60. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  61. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  62. int StoreLogMsg_1(const char *fmt, ...);
  63. unsigned long GetTimeoutValue(struct timeval _sour_time);
  64. #define DEBUG_INFO_1(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  65. #define DEBUG_WARN_1(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  66. #define DEBUG_ERROR_1(format, args...) StoreLogMsg_1("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  67. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  68. struct StatusCodeData *ShmStatusCodeData;
  69. struct PsuData *ShmPsuData;
  70. struct CHAdeMOData *ShmCHAdeMOData;
  71. struct CcsData *ShmCcsData;
  72. struct PrimaryMcuData *ShmPrimaryMcuData;
  73. struct FanModuleData *ShmFanModuleData;
  74. struct RelayModuleData *ShmRelayModuleData;
  75. struct OCPP16Data *ShmOCPP16Data;
  76. struct ChargingInfoData *chargingInfo[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  77. struct timeb startChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  78. struct timeb endChargingTime[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  79. byte _gunCount = 0;
  80. // for initial index to check EV board type is correct
  81. byte _gunIndex = 0;
  82. byte _chademoIndex = 0;
  83. byte _ccsIndex = 0;
  84. byte _gb_Index = 0;
  85. byte bd0_1_status = 0;
  86. byte bd0_2_status = 0;
  87. byte bd1_1_status = 0;
  88. byte bd1_2_status = 0;
  89. bool isCardScan = false;
  90. int rfidFd = -1;
  91. char* rfidPortName = "/dev/ttyS2";
  92. byte autoReturnTimeoutFlag = 0x00;
  93. //================================================
  94. // initial can-bus
  95. //================================================
  96. int InitCanBus()
  97. {
  98. int s0,nbytes;
  99. struct timeval tv;
  100. struct ifreq ifr0;
  101. struct sockaddr_can addr0;
  102. system("/sbin/ip link set can0 down");
  103. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  104. system("/sbin/ip link set can0 up");
  105. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  106. tv.tv_sec = 0;
  107. tv.tv_usec = 10000;
  108. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  109. {
  110. #ifdef SystemLogMessage
  111. DEBUG_ERROR_1("Set SO_RCVTIMEO NG");
  112. #endif
  113. }
  114. nbytes=40960;
  115. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  116. {
  117. #ifdef SystemLogMessage
  118. DEBUG_ERROR_1("Set SO_RCVBUF NG");
  119. #endif
  120. }
  121. nbytes=40960;
  122. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  123. {
  124. #ifdef SystemLogMessage
  125. DEBUG_ERROR_1("Set SO_SNDBUF NG");
  126. #endif
  127. }
  128. strcpy(ifr0.ifr_name, "can0" );
  129. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  130. addr0.can_family = AF_CAN;
  131. addr0.can_ifindex = ifr0.ifr_ifindex;
  132. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  133. return s0;
  134. }
  135. //================================================
  136. // initial uart port
  137. //================================================
  138. char *priPortName = "/dev/ttyS1";
  139. int InitComPort(byte id)
  140. {
  141. int fd;
  142. struct termios tios;
  143. if (id == 0x04)
  144. fd = open(priPortName, O_RDWR);
  145. if(fd<=0)
  146. {
  147. #ifdef SystemLogMessage
  148. DEBUG_ERROR_1("open 407 Communication port NG \n");
  149. #endif
  150. return -1;
  151. }
  152. ioctl (fd, TCGETS, &tios);
  153. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  154. tios.c_lflag = 0;
  155. tios.c_iflag = 0;
  156. tios.c_oflag = 0;
  157. tios.c_cc[VMIN]=0;
  158. tios.c_cc[VTIME]=(unsigned char)1;
  159. tios.c_lflag=0;
  160. tcflush(fd, TCIFLUSH);
  161. ioctl (fd, TCSETS, &tios);
  162. return fd;
  163. }
  164. //=================================
  165. // Common routine
  166. //=================================
  167. int StoreLogMsg_1(const char *fmt, ...)
  168. {
  169. char Buf[4096+256];
  170. char buffer[4096];
  171. time_t CurrentTime;
  172. struct tm *tm;
  173. va_list args;
  174. va_start(args, fmt);
  175. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  176. va_end(args);
  177. memset(Buf,0,sizeof(Buf));
  178. CurrentTime = time(NULL);
  179. tm=localtime(&CurrentTime);
  180. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  181. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  182. buffer,
  183. tm->tm_year+1900,tm->tm_mon+1);
  184. system(Buf);
  185. return rc;
  186. }
  187. unsigned long GetTimeoutValue(struct timeval _sour_time)
  188. {
  189. struct timeval _end_time;
  190. gettimeofday(&_end_time, NULL);
  191. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  192. }
  193. int mystrcmp(unsigned char *p1, unsigned char *p2)
  194. {
  195. while(*p1==*p2)
  196. {
  197. if(*p1=='\0' || *p2=='\0')
  198. break;
  199. p1++;
  200. p2++;
  201. }
  202. if(*p1=='\0' && *p2=='\0')
  203. return(PASS);
  204. else
  205. return(FAIL);
  206. }
  207. int DiffTimeb(struct timeb ST, struct timeb ET)
  208. {
  209. //return milli-second
  210. unsigned int StartTime,StopTime;
  211. StartTime=(unsigned int)ST.time;
  212. StopTime=(unsigned int)ET.time;
  213. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  214. return (StopTime-StartTime);
  215. }
  216. void setChargerMode(byte gun_index, byte mode)
  217. {
  218. chargingInfo[gun_index]->SystemStatus = mode;
  219. }
  220. //==========================================
  221. // Check interface status
  222. //==========================================
  223. int isInterfaceUp(const char *interface)
  224. {
  225. int result = FAIL;
  226. FILE *fp;
  227. char cmd[256];
  228. char buf[512];
  229. strcpy(cmd, "ifconfig");
  230. fp = popen(cmd, "r");
  231. if(fp != NULL)
  232. {
  233. while(fgets(buf, sizeof(buf), fp) != NULL)
  234. {
  235. if(strstr(buf, interface) > 0)
  236. {
  237. result = PASS;
  238. }
  239. }
  240. }
  241. pclose(fp);
  242. return result;
  243. }
  244. //=================================
  245. // Create all share memory
  246. //=================================
  247. int CreateShareMemory()
  248. {
  249. int MeterSMId;
  250. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  251. {
  252. #ifdef SystemLogMessage
  253. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  254. #endif
  255. return 0;
  256. }
  257. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  258. {
  259. #ifdef SystemLogMessage
  260. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  261. #endif
  262. return 0;
  263. }
  264. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  265. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  266. {
  267. #ifdef SystemLogMessage
  268. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  269. #endif
  270. return 0;
  271. }
  272. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  273. {
  274. #ifdef SystemLogMessage
  275. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  276. #endif
  277. return 0;
  278. }
  279. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  280. //creat ShmPsuData
  281. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  282. {
  283. #ifdef SystemLogMessage
  284. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  285. #endif
  286. return 0;
  287. }
  288. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  289. {
  290. #ifdef SystemLogMessage
  291. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  292. #endif
  293. return 0;
  294. }
  295. memset(ShmPsuData, 0, sizeof(struct PsuData));
  296. if(CHAdeMO_QUANTITY > 0)
  297. {
  298. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  299. {
  300. #ifdef SystemLogMessage
  301. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  302. #endif
  303. return 0;
  304. }
  305. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  306. {
  307. #ifdef SystemLogMessage
  308. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  309. #endif
  310. return 0;
  311. }
  312. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  313. }
  314. //creat ShmCcsData
  315. if(CCS_QUANTITY > 0)
  316. {
  317. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  318. {
  319. #ifdef SystemLogMessage
  320. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  321. #endif
  322. return 0;
  323. }
  324. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  325. {
  326. #ifdef SystemLogMessage
  327. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  328. #endif
  329. return 0;
  330. }
  331. memset(ShmCcsData, 0, sizeof(struct CcsData));
  332. }
  333. //creat ShmPrimaryMcuData
  334. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  335. {
  336. #ifdef SystemLogMessage
  337. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  338. #endif
  339. return 0;
  340. }
  341. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  342. {
  343. #ifdef SystemLogMessage
  344. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  345. #endif
  346. return 0;
  347. }
  348. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  349. //creat ShmFanModuleData
  350. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  351. {
  352. #ifdef SystemLogMessage
  353. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  354. #endif
  355. return 0;
  356. }
  357. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  358. {
  359. #ifdef SystemLogMessage
  360. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  361. #endif
  362. return 0;
  363. }
  364. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  365. //creat ShmRelayModuleData
  366. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  367. {
  368. #ifdef SystemLogMessage
  369. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmRelayModuleData NG \n");
  370. #endif
  371. return 0;
  372. }
  373. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  374. {
  375. #ifdef SystemLogMessage
  376. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmRelayModuleData NG \n");
  377. #endif
  378. return 0;
  379. }
  380. memset(ShmRelayModuleData, 0, sizeof(struct RelayModuleData));
  381. //creat ShmOCPP16Data
  382. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  383. {
  384. #ifdef SystemLogMessage
  385. DEBUG_ERROR_1("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  386. #endif
  387. return 0;
  388. }
  389. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  390. {
  391. #ifdef SystemLogMessage
  392. DEBUG_ERROR_1("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  393. #endif
  394. return 0;
  395. }
  396. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  397. return 1;
  398. }
  399. //=================================
  400. // LCM Page
  401. //=================================
  402. void ChangeLcmByIndex(byte page_index)
  403. {
  404. if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level != 2 ||
  405. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  406. {
  407. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  408. }
  409. }
  410. //======================================================
  411. // Peripheral initial
  412. //======================================================
  413. void InitGPIO()
  414. {
  415. /*****************0~3, 4 bank, bank x 32+ num*********************/
  416. /***************************************************************/
  417. /*************** GPIO 0 ***************************************/
  418. /***************************************************************/
  419. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  420. system("echo 22 > /sys/class/gpio/export");
  421. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  422. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  423. system("echo 23 > /sys/class/gpio/export");
  424. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  425. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  426. system("echo 26 > /sys/class/gpio/export");
  427. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  428. system("echo 1 > /sys/class/gpio/gpio26/value");
  429. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  430. system("echo 27 > /sys/class/gpio/export");
  431. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  432. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  433. system("echo 29 > /sys/class/gpio/export");
  434. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  435. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  436. system("echo 19 > /sys/class/gpio/export");
  437. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  438. system("echo 0 > /sys/class/gpio/gpio19/value");
  439. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  440. system("echo 20 > /sys/class/gpio/export");
  441. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  442. /***************************************************************/
  443. /*************** GPIO 1 ***************************************/
  444. /***************************************************************/
  445. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  446. system("echo 44 > /sys/class/gpio/export");
  447. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  448. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  449. system("echo 45 > /sys/class/gpio/export");
  450. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  451. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  452. system("echo 46 > /sys/class/gpio/export");
  453. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  454. system("echo 0 > /sys/class/gpio/gpio46/value");
  455. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  456. system("echo 47 > /sys/class/gpio/export");
  457. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  458. /***************************************************************/
  459. /*************** GPIO 2 ***************************************/
  460. /***************************************************************/
  461. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  462. system("echo 89 > /sys/class/gpio/export");
  463. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  464. system("echo 1 > /sys/class/gpio/gpio89/value");
  465. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  466. system("echo 87 > /sys/class/gpio/export");
  467. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  468. system("echo 0 > /sys/class/gpio/gpio87/value");
  469. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/
  470. system("echo 88 > /sys/class/gpio/export");
  471. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  472. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/
  473. system("echo 86 > /sys/class/gpio/export");
  474. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  475. /***************************************************************/
  476. /*************** GPIO 3 ***************************************/
  477. /***************************************************************/
  478. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  479. system("echo 111 > /sys/class/gpio/export");
  480. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  481. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  482. system("echo 114 > /sys/class/gpio/export");
  483. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  484. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  485. system("echo 113 > /sys/class/gpio/export");
  486. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  487. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  488. system("echo 110 > /sys/class/gpio/export");
  489. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  490. system("echo 0 > /sys/class/gpio/gpio110/value");
  491. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/
  492. system("echo 115 > /sys/class/gpio/export");
  493. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  494. system("echo 0 > /sys/class/gpio/gpio115/value");
  495. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  496. system("echo 112 > /sys/class/gpio/export");
  497. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  498. system("echo 0 > /sys/class/gpio/gpio112/value");
  499. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/
  500. system("echo 116 > /sys/class/gpio/export");
  501. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  502. system("echo 0 > /sys/class/gpio/gpio116/value");
  503. #ifdef SystemLogMessage
  504. DEBUG_ERROR_1("[main]InitGPIO: Initial GPIO OK");
  505. #endif
  506. }
  507. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  508. {
  509. int fd,wrd;
  510. struct SysConfigData *buf;
  511. byte *PtrBuf;
  512. unsigned int ChkSum, ChkSumOrg;
  513. if ((buf = malloc(sizeof(struct SysConfigData))) == NULL)
  514. {
  515. #ifdef SystemLogMessage
  516. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo:malloc buffer NG,rebooting..");
  517. #endif
  518. if (ShmStatusCodeData != NULL)
  519. {
  520. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  521. }
  522. sleep(5);
  523. system("reboot -f");
  524. sleep(5);
  525. system("reboot -f");
  526. }
  527. memset(buf, 0, sizeof(struct SysConfigData));
  528. fd = open("/dev/mtdblock10", O_RDWR);
  529. if (fd < 0)
  530. {
  531. free(buf);
  532. #ifdef SystemLogMessage
  533. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo:open mtdblock10 NG,rebooting..");
  534. #endif
  535. if (ShmStatusCodeData != NULL)
  536. {
  537. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  538. }
  539. sleep(5);
  540. system("reboot -f");
  541. sleep(5);
  542. system("reboot -f");
  543. }
  544. wrd = read(fd, buf, sizeof(struct SysConfigData));
  545. close(fd);
  546. if (wrd != (sizeof(struct SysConfigData)))
  547. {
  548. free(buf);
  549. #ifdef SystemLogMessage
  550. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: read SysConfigData data NG,rebooting..");
  551. #endif
  552. if (ShmStatusCodeData != NULL)
  553. {
  554. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  555. }
  556. sleep(5);
  557. system("reboot -f");
  558. sleep(5);
  559. system("reboot -f");
  560. }
  561. PtrBuf = (byte *) buf;
  562. ChkSum = 0;
  563. for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  564. {
  565. ChkSum += PtrBuf[wrd];
  566. }
  567. ChkSumOrg = buf->Checksum;
  568. if (ChkSum != ChkSumOrg)
  569. {
  570. #ifdef SystemLogMessage
  571. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: Primary SysConfigData checksum NG, read backup");
  572. #endif
  573. fd = open("/dev/mtdblock11", O_RDWR);
  574. if (fd < 0)
  575. {
  576. free(buf);
  577. #ifdef SystemLogMessage
  578. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: open mtdblock11 (backup) NG,rebooting..");
  579. #endif
  580. if (ShmStatusCodeData != NULL)
  581. {
  582. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  583. }
  584. sleep(5);
  585. system("reboot -f");
  586. sleep(5);
  587. system("reboot -f");
  588. }
  589. memset(buf, 0, sizeof(struct SysConfigData));
  590. wrd = read(fd, buf, sizeof(struct SysConfigData));
  591. close(fd);
  592. if (wrd != sizeof(struct SysConfigData))
  593. {
  594. free(buf);
  595. #ifdef SystemLogMessage
  596. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: read backup SysConfigData data NG,rebooting..");
  597. #endif
  598. if (ShmStatusCodeData != NULL)
  599. {
  600. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  601. }
  602. sleep(5);
  603. system("reboot -f");
  604. sleep(5);
  605. system("reboot -f");
  606. }
  607. PtrBuf = (byte *) buf;
  608. ChkSum = 0;
  609. for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  610. {
  611. ChkSum += PtrBuf[wrd];
  612. }
  613. ChkSumOrg = buf->Checksum;
  614. if (ChkSum != ChkSumOrg)
  615. {
  616. #ifdef SystemLogMessage
  617. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: backup SysConfigData checksum NG, read Factory default");
  618. #endif
  619. fd = open("/dev/mtdblock12", O_RDWR);
  620. if (fd < 0)
  621. {
  622. free(buf);
  623. #ifdef SystemLogMessage
  624. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: open mtdblock12 (Factory default) NG,rebooting..");
  625. #endif
  626. if (ShmStatusCodeData != NULL)
  627. {
  628. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  629. }
  630. sleep(5);
  631. system("reboot -f");
  632. sleep(5);
  633. system("reboot -f");
  634. }
  635. memset(buf, 0, sizeof(struct SysConfigData));
  636. wrd = read(fd, buf, sizeof(struct SysConfigData));
  637. close(fd);
  638. if (wrd != sizeof(struct SysConfigData))
  639. {
  640. free(buf);
  641. #ifdef SystemLogMessage
  642. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: read factory default SysConfigData data NG,rebooting..");
  643. #endif
  644. if (ShmStatusCodeData != NULL)
  645. {
  646. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  647. }
  648. sleep(5);
  649. system("reboot -f");
  650. sleep(5);
  651. system("reboot -f");
  652. }
  653. PtrBuf = (byte *) buf;
  654. ChkSum = 0;
  655. for (wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  656. {
  657. ChkSum += PtrBuf[wrd];
  658. }
  659. ChkSumOrg = buf->Checksum;
  660. if (ChkSum != ChkSumOrg)
  661. {
  662. #ifdef SystemLogMessage
  663. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: factory default SysConfigData checksum NG, restore factory default");
  664. #endif
  665. goto DefaultShm;
  666. }
  667. }
  668. }
  669. //load OK
  670. memcpy((struct SysConfigData *) ptr, (struct SysConfigData *) buf, sizeof(struct SysConfigData));
  671. free(buf);
  672. #ifdef SystemLogMessage
  673. DEBUG_ERROR_1("[main]LoadSysConfigAndInfo: Load SysConfigData OK");
  674. #endif
  675. return 1;
  676. DefaultShm: system("cd /root;./FactoryConfig");
  677. system("sync");
  678. sleep(5);
  679. system("reboot -f");
  680. sleep(5);
  681. system("reboot -f");
  682. return FAIL;
  683. }
  684. void InitEthernet()
  685. {
  686. char tmpbuf[256];
  687. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  688. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  689. sleep(2);
  690. //Init Eth0 for internet
  691. return;
  692. memset(tmpbuf,0,256);
  693. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  694. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  695. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  696. system(tmpbuf);
  697. memset(tmpbuf,0,256);
  698. sprintf(tmpbuf,"route add default gw %s eth0 ",
  699. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  700. system(tmpbuf);
  701. //Init Eth1 for administrator tool
  702. memset(tmpbuf,0,256);
  703. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  704. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  705. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  706. system(tmpbuf);
  707. //Run DHCP client if enabled
  708. system("killall udhcpc");
  709. system("rm -rf /etc/resolv.conf");
  710. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  711. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  712. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  713. system("/sbin/udhcpc -i eth0 -s /root/simple.script > /dev/null &");
  714. #ifdef SystemLogMessage
  715. DEBUG_ERROR_1("[main]InitEthernet: Initial Ethernet OK");
  716. #endif
  717. }
  718. int InitialRfidPort()
  719. {
  720. int uartO2 = open(rfidPortName, O_RDWR);
  721. struct termios tios;
  722. if (uartO2 != FAIL)
  723. {
  724. ioctl (uartO2, TCGETS, &tios);
  725. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  726. tios.c_lflag = 0;
  727. tios.c_iflag = 0;
  728. tios.c_oflag = 0;
  729. tios.c_cc[VMIN] = 0;
  730. tios.c_cc[VTIME] = (unsigned char) 1;
  731. tios.c_lflag = 0;
  732. tcflush(uartO2, TCIFLUSH);
  733. ioctl(uartO2, TCSETS, &tios);
  734. }
  735. if (uartO2 < 0)
  736. {
  737. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  738. }
  739. return uartO2;
  740. }
  741. int Initialization()
  742. {
  743. //InitGPIO();
  744. //LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  745. //InitEthernet();
  746. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  747. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  748. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  749. // 初始化卡號驗證的 Flag
  750. ClearAuthorizedFlag();
  751. // 初始化插槍驗證的 Flag
  752. ClearDetectPluginFlag();
  753. // UART 2 for Rfid
  754. rfidFd = InitialRfidPort();
  755. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  756. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  757. ShmPrimaryMcuData->SelfTest_Comp = NO;
  758. ShmRelayModuleData->SelfTest_Comp = NO;
  759. ShmFanModuleData->SelfTest_Comp = NO;
  760. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  761. for (byte count = 0; count < _gunCount; count++)
  762. {
  763. if (chargingInfo[count]->Type == _Type_Chademo)
  764. {
  765. ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  766. }
  767. else if (chargingInfo[count]->Type == _Type_CCS)
  768. {
  769. if (ShmCcsData->CommProtocol == 0x01)
  770. {
  771. ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  772. }
  773. }
  774. }
  775. #ifdef SystemLogMessage
  776. printf("Initialization OK \n");
  777. #endif
  778. return PASS;
  779. }
  780. void SelfTestRun()
  781. {
  782. bool evInitFlag = false;
  783. StartSystemTimeoutDet(Timeout_SelftestChk);
  784. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  785. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  786. {
  787. ChkPrimaryStatus();
  788. if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 2)
  789. {
  790. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  791. return;
  792. }
  793. if (_gunCount > 0)
  794. {
  795. if (ShmPsuData->Work_Step == _NO_WORKING)
  796. {
  797. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  798. break;
  799. }
  800. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  801. {
  802. case _STEST_VERSION:
  803. {
  804. if (strlen((char *)ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev) != 0 ||
  805. ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev[0] != '\0')
  806. {
  807. //printf("RB pass \n");
  808. ShmRelayModuleData->SelfTest_Comp = YES;
  809. }
  810. //if (strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  811. // ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  812. {
  813. //printf("Fan pass \n");
  814. ShmFanModuleData->SelfTest_Comp = YES;
  815. }
  816. if (strlen((char *)ShmPrimaryMcuData->version) != 0 ||
  817. ShmPrimaryMcuData->version[0] != '\0')
  818. {
  819. //printf("407 pass \n");
  820. ShmPrimaryMcuData->SelfTest_Comp = YES;
  821. }
  822. // EV 小板
  823. if (!evInitFlag)
  824. {
  825. evInitFlag = YES;
  826. for (byte index = 0; index < _gunCount; index++)
  827. {
  828. if (chargingInfo[index]->Type == _Type_Chademo)
  829. {
  830. if (strlen((char *)ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version) != 0 ||
  831. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].version[0] != '\0')
  832. {
  833. //printf("chademo pass \n");
  834. ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  835. }
  836. else
  837. {
  838. printf("chademo fw lose...... \n");
  839. evInitFlag = NO;
  840. }
  841. }
  842. else if (chargingInfo[index]->Type == _Type_CCS)
  843. {
  844. if (ShmCcsData->CommProtocol == 0x01)
  845. {
  846. if (strlen((char *)ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version) != 0 ||
  847. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version[0] != '\0')
  848. {
  849. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  850. }
  851. else
  852. {
  853. printf("ccs fw lose \n");
  854. evInitFlag = NO;
  855. }
  856. }
  857. }
  858. }
  859. }
  860. if (ShmFanModuleData->SelfTest_Comp &&
  861. ShmRelayModuleData->SelfTest_Comp &&
  862. ShmPrimaryMcuData->SelfTest_Comp &&
  863. evInitFlag)
  864. {
  865. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  866. }
  867. }
  868. break;
  869. case _STEST_AC_CONTACTOR:
  870. {
  871. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  872. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  873. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  874. {
  875. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  876. printf("Communication board pass. \n");
  877. }
  878. }
  879. break;
  880. case _STEST_PSU_DETECT:
  881. {
  882. // 此測試主要測試 PSU 對應是否為正確的火線上電壓
  883. // 如果沒有 PSU 模組請 bypass
  884. if (ShmPsuData->Work_Step == _TEST_POWER_STEP || ShmPsuData->Work_Step == _TEST_COMPLETE)
  885. {
  886. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  887. }
  888. }
  889. break;
  890. case _STEST_PSU_CAP:
  891. {
  892. // 此測試是要確認當前總輸出能力
  893. // 如果沒有 PSU 模組請 bypass
  894. if (ShmPsuData->Work_Step == _TEST_COMPLETE)
  895. {
  896. sleep(1);
  897. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  898. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  899. }
  900. }
  901. break;
  902. }
  903. }
  904. else
  905. break;
  906. usleep(100000);
  907. }
  908. }
  909. int SpawnTask()
  910. {
  911. system("/root/Module_EventLogging &");
  912. system("/root/Module_PrimaryComm &");
  913. system("/root/Module_EvComm &");
  914. system("/root/Module_LcmControl &");
  915. system("/root/Module_InternalComm &");
  916. system("/root/Module_PsuComm &");
  917. //system("/root/OcppBackend &");
  918. //system("/root/Module_4g &");
  919. //system("/root/Module_Wifi &");
  920. //system("/root/Module_PsuComm &");
  921. //system("/root/InfyPowerPsu_Comm &");
  922. // 加入參數
  923. // char str[64];
  924. // memset(str, '\0', sizeof(65));
  925. // sprintf(str, "/root/Module_EvComm %x &", (CHAdeMO_QUANTITY + CCS_QUANTITY));
  926. // printf("%s \n", str);
  927. // system(str);
  928. return PASS;
  929. }
  930. int StoreUsrConfigData(struct SysConfigData *UsrData)
  931. {
  932. int fd,wrd;
  933. unsigned int i, Chk;
  934. byte *ptr;
  935. Chk = 0;
  936. ptr = (byte *) UsrData;
  937. for (i = 0; i < sizeof(struct SysConfigData) - 4; i++)
  938. {
  939. Chk += *(ptr + i);
  940. }
  941. UsrData->Checksum = Chk;
  942. fd = open("/dev/mtdblock10", O_RDWR);
  943. if (fd < 0)
  944. {
  945. #ifdef SystemLogMessage
  946. DEBUG_ERROR_1("[main]StoreUsrConfigData: open /dev/mtdblock10 NG");
  947. #endif
  948. return 0;
  949. }
  950. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  951. close(fd);
  952. if (wrd != (sizeof(struct SysConfigData)))
  953. {
  954. #ifdef SystemLogMessage
  955. DEBUG_ERROR_1("[main]StoreUsrConfigData: write /dev/mtdblock10 NG");
  956. #endif
  957. return 0;
  958. }
  959. fd = open("/dev/mtdblock11", O_RDWR);
  960. if (fd < 0)
  961. {
  962. #ifdef SystemLogMessage
  963. DEBUG_ERROR_1("[main]StoreUsrConfigData: open /dev/mtdblock11(backup) NG");
  964. #endif
  965. return 0;
  966. }
  967. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  968. close(fd);
  969. if (wrd != (sizeof(struct SysConfigData)))
  970. {
  971. #ifdef SystemLogMessage
  972. DEBUG_ERROR_1("[main]StoreUsrConfigData: write /dev/mtdblock11(backup) NG");
  973. #endif
  974. return 0;
  975. }
  976. return 1;
  977. }
  978. //===============================================
  979. // Common Detect Chk - Stop Charging ?
  980. //===============================================
  981. bool isEvBoardStopChargeFlag(byte gunIndex)
  982. {
  983. return chargingInfo[gunIndex]->StopChargeFlag;
  984. }
  985. //===============================================
  986. // Common Detect Chk - Chademo
  987. //===============================================
  988. bool isEvGunLocked_chademo(byte gunIndex)
  989. {
  990. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  991. }
  992. bool isEvContactorWelding_chademo(byte gunIndex)
  993. {
  994. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  995. }
  996. bool isEvStopReq_chademo(byte gunIndex)
  997. {
  998. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  999. }
  1000. bool isEvStopCharging_chademo(byte gunIndex)
  1001. {
  1002. if (isEvGunLocked_chademo(gunIndex) == NO)
  1003. {
  1004. // 無鎖槍 = 停止
  1005. printf("gun locked none. \n");
  1006. return YES;
  1007. }
  1008. return NO;
  1009. }
  1010. byte isPrechargeStatus_chademo(byte gunIndex)
  1011. {
  1012. byte result = 0x00;
  1013. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1014. return result;
  1015. }
  1016. //===============================================
  1017. // Common Detect Chk - CCS
  1018. //===============================================
  1019. bool isEvGunLocked_ccs(byte gunIndex)
  1020. {
  1021. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1022. }
  1023. byte isPrechargeStatus_ccs(byte gunIndex)
  1024. {
  1025. byte result = 0x00;
  1026. if (ShmCcsData->CommProtocol == 0x01)
  1027. {
  1028. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1029. }
  1030. return result;
  1031. }
  1032. bool isEvStopCharging_ccs(byte gunIndex)
  1033. {
  1034. if (isEvGunLocked_ccs(gunIndex) == NO)
  1035. {
  1036. // 無鎖槍 = 停止
  1037. printf("gun locked none. \n");
  1038. return YES;
  1039. }
  1040. return NO;
  1041. }
  1042. //===============================================
  1043. // Callback
  1044. //===============================================
  1045. void _AutoReturnTimeout()
  1046. {
  1047. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1048. {
  1049. ClearDetectPluginFlag();
  1050. }
  1051. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1052. //
  1053. // for (byte i = 0; i < CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY; i++)
  1054. // {
  1055. // if (chargingInfo[i]->SystemStatus > S_IDLE)
  1056. // {
  1057. // switch(chargingInfo[i]->SystemStatus)
  1058. // {
  1059. // case S_PREPARNING:
  1060. // case S_PREPARING_FOR_EV:
  1061. // case S_PREPARING_FOR_EVSE:
  1062. // {
  1063. // ChangeLcmByIndex(i, _LCM_PRE_CHARGE);
  1064. // }
  1065. // break;
  1066. // case S_CHARGING:
  1067. // {
  1068. // ChangeLcmByIndex(i, _LCM_CHARGING);
  1069. // }
  1070. // break;
  1071. // case S_TERMINATING:
  1072. // {
  1073. // ChangeLcmByIndex(i, _LCM_COMPLETE);
  1074. // }
  1075. // break;
  1076. // }
  1077. // return;
  1078. // }
  1079. // }
  1080. // if (!IsAuthorizingMode())
  1081. // ChangeLcmByIndex(255, _LCM_IDLE);
  1082. }
  1083. void _SelfTestTimeout()
  1084. {
  1085. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  1086. {
  1087. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  1088. {
  1089. setChargerMode(gun_index, MODE_ALARM);
  1090. }
  1091. ShmPsuData->Work_Step = _NO_WORKING;
  1092. }
  1093. }
  1094. void _AuthorizedTimeout()
  1095. {
  1096. if(IsAuthorizingMode())
  1097. {
  1098. printf("_AuthorizedTimeout \n");
  1099. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1100. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1101. ClearAuthorizedFlag();
  1102. }
  1103. }
  1104. void _DetectPlugInTimeout()
  1105. {
  1106. if(isDetectPlugin())
  1107. {
  1108. printf("_DetectPlugInTimeout \n");
  1109. ClearDetectPluginFlag();
  1110. }
  1111. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1112. }
  1113. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1114. {
  1115. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1116. {
  1117. if(!isEvGunLocked_chademo(gunIndex))
  1118. {
  1119. printf("_DetectEvChargingEnableTimeout (chademo) \n");
  1120. ChargingTerminalProcess(gunIndex);
  1121. _AutoReturnTimeout();
  1122. }
  1123. }
  1124. else if (chargingInfo[gunIndex]->Type == _Type_CCS)
  1125. {
  1126. if(!isEvGunLocked_ccs(gunIndex))
  1127. {
  1128. printf("_DetectEvChargingEnableTimeout (ccs) \n");
  1129. ChargingTerminalProcess(gunIndex);
  1130. _AutoReturnTimeout();
  1131. }
  1132. }
  1133. }
  1134. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1135. {
  1136. printf("_DetectEvseChargingEnableTimeout (GFD timeout) \n");
  1137. if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  1138. {
  1139. setChargerMode(gunIndex, MODE_IDLE);
  1140. _AutoReturnTimeout();
  1141. }
  1142. }
  1143. void _PrepareTimeout(byte gunIndex)
  1144. {
  1145. printf("_PrechargeTimeout \n");
  1146. setChargerMode(gunIndex, MODE_IDLE);
  1147. _AutoReturnTimeout();
  1148. }
  1149. void _CompleteTimeout(byte gunIndex)
  1150. {
  1151. printf("_CompleteTimeout ====> %d \n", gunIndex);
  1152. setChargerMode(gunIndex, MODE_IDLE);
  1153. }
  1154. void _CcsPrechargeTimeout(byte gunIndex)
  1155. {
  1156. printf("_CcsPrechargeTimeout \n");
  1157. setChargerMode(gunIndex, MODE_IDLE);
  1158. }
  1159. //===============================================
  1160. // 取得卡號與卡號驗證
  1161. //===============================================
  1162. bool canStartCharging()
  1163. {
  1164. char buf2[16] = "";
  1165. memset(buf2, 0, ARRAY_SIZE(buf2));
  1166. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  1167. {
  1168. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1169. }
  1170. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1171. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1172. if(strstr(buf2, "Accepted") != 0)
  1173. return true;
  1174. else
  1175. {
  1176. }
  1177. return false;
  1178. }
  1179. void AuthorizingStart()
  1180. {
  1181. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1182. }
  1183. void ClearAuthorizedFlag()
  1184. {
  1185. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1186. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1187. }
  1188. bool isAuthorizedComplete()
  1189. {
  1190. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1191. return false;
  1192. return true;
  1193. }
  1194. bool IsAuthorizingMode()
  1195. {
  1196. if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq == NO)
  1197. return false;
  1198. return true;
  1199. }
  1200. byte GetCardNumber()
  1201. {
  1202. byte card_number[16];
  1203. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  1204. {
  1205. if(getRequestCardSN(rfidFd, 0, card_number))
  1206. {
  1207. //Get Card Number
  1208. byte len = card_number[0];
  1209. char buf2[32] = "";
  1210. memcpy(buf2, (card_number + 1), len);
  1211. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x0, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1212. for (byte index = 0; index < len; index++)
  1213. {
  1214. sprintf((char *)ShmSysConfigAndInfo->SysConfig.UserId + (index * 2), "%02X", buf2[index]);
  1215. }
  1216. printf("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  1217. return PASS;
  1218. }
  1219. }
  1220. return FAIL;
  1221. }
  1222. //===============================================
  1223. // 掃描插槍狀況
  1224. //===============================================
  1225. void ClearDetectPluginFlag()
  1226. {
  1227. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1228. }
  1229. void DetectPluginStart()
  1230. {
  1231. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1232. }
  1233. bool isDetectPlugin()
  1234. {
  1235. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  1236. return YES;
  1237. return NO;
  1238. }
  1239. //===============================================
  1240. // EmergencyStop and Charging Stop
  1241. //===============================================
  1242. void ChargingTerminalProcess(byte gunIndex)
  1243. {
  1244. setChargerMode(gunIndex, MODE_TERMINATING);
  1245. }
  1246. void StopChargingProcessByString(byte level, byte gun_index, char *string)
  1247. {
  1248. if (strlen((char *)ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index]) == 0 ||
  1249. level > ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level)
  1250. {
  1251. memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], string, 7);
  1252. ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = level;
  1253. }
  1254. }
  1255. void ReleaseChargingProcessByString(byte gun_index, char *code)
  1256. {
  1257. memcpy(&ShmSysConfigAndInfo->SysStopChargingAlarmCode.StopCode[gun_index][0], "", 7);
  1258. ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level = 0;
  1259. }
  1260. // 各小板的停止充電處理函式
  1261. void EmcOccureByString(byte index, char *code)
  1262. {
  1263. bool isStopCharger = false;
  1264. if (strncmp(code, "012251", 6) == 0 || strncmp(code, "012252", 6) == 0 ||
  1265. strncmp(code, "012237", 6) == 0 || strncmp(code, "012238", 6) == 0)
  1266. {
  1267. isStopCharger = true;
  1268. }
  1269. if (isStopCharger)
  1270. {
  1271. for (byte gun = 0; gun < _gunCount; gun++)
  1272. {
  1273. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  1274. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1275. {
  1276. ChargingTerminalProcess(gun);
  1277. }
  1278. StopChargingProcessByString(2, gun, code);
  1279. }
  1280. }
  1281. else
  1282. {
  1283. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  1284. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1285. {
  1286. ChargingTerminalProcess(index);
  1287. }
  1288. StopChargingProcessByString(2, index, code);
  1289. }
  1290. }
  1291. void ReleaseEmsOccureByString(byte index, char *code)
  1292. {
  1293. bool isReleaseCharger = false;
  1294. bool isTrigger = false;
  1295. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  1296. {
  1297. isTrigger = true;
  1298. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  1299. isReleaseCharger = true;
  1300. }
  1301. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  1302. {
  1303. isTrigger = true;
  1304. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  1305. isReleaseCharger = true;
  1306. }
  1307. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  1308. {
  1309. isTrigger = true;
  1310. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  1311. isReleaseCharger = true;
  1312. }
  1313. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  1314. {
  1315. isTrigger = true;
  1316. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  1317. isReleaseCharger = true;
  1318. }
  1319. else if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1320. {
  1321. isTrigger = true;
  1322. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  1323. }
  1324. if (isTrigger)
  1325. {
  1326. if (isReleaseCharger)
  1327. {
  1328. for (byte gun = 0; gun < _gunCount; gun++)
  1329. {
  1330. ReleaseChargingProcessByString(gun, code);
  1331. }
  1332. }
  1333. else
  1334. {
  1335. ReleaseChargingProcessByString(index, code);
  1336. }
  1337. }
  1338. }
  1339. //===============================================
  1340. // 確認硬體 (按鈕) 狀態
  1341. //===============================================
  1342. bool leftBtnPush = false;
  1343. bool rightBtnPush = false;
  1344. void ChkPrimaryStatus()
  1345. {
  1346. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  1347. {
  1348. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  1349. EmcOccureByString(0, "012251");
  1350. }
  1351. else
  1352. ReleaseEmsOccureByString(0, "012251");
  1353. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  1354. {
  1355. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  1356. EmcOccureByString(0, "012238");
  1357. }
  1358. else
  1359. ReleaseEmsOccureByString(0, "012238");
  1360. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  1361. {
  1362. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  1363. EmcOccureByString(0, "012237");
  1364. }
  1365. else
  1366. ReleaseEmsOccureByString(0, "012237");
  1367. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  1368. {
  1369. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  1370. EmcOccureByString(0, "012252");
  1371. }
  1372. else
  1373. ReleaseEmsOccureByString(0, "012252");
  1374. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  1375. {
  1376. if(!leftBtnPush)
  1377. {
  1378. printf("left btn down............................... \n");
  1379. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  1380. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1381. {
  1382. case S_IDLE:
  1383. {
  1384. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  1385. }
  1386. break;
  1387. case S_CHARGING:
  1388. {
  1389. // 停止充電
  1390. printf("To Stop = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1391. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 9;
  1392. //ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1393. }
  1394. break;
  1395. case S_COMPLETE:
  1396. {
  1397. // 回 IDLE
  1398. printf("Back to IDLE = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1399. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 1;
  1400. }
  1401. break;
  1402. }
  1403. }
  1404. leftBtnPush = true;
  1405. // 左邊的選槍按鈕,只有在雙槍都在充電時候才有用 : 30KW 以下該按鈕無作用
  1406. }
  1407. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  1408. {
  1409. if(leftBtnPush)
  1410. printf("left btn up............................... \n");
  1411. leftBtnPush = false;
  1412. }
  1413. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  1414. {
  1415. if(!rightBtnPush)
  1416. {
  1417. printf("right btn down............................... \n");
  1418. if (_gunCount > 1)
  1419. {
  1420. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 1;
  1421. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1422. {
  1423. case S_IDLE:
  1424. {
  1425. //ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  1426. }
  1427. break;
  1428. case S_CHARGING:
  1429. {
  1430. // 停止充電
  1431. printf("To Stop = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1432. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 9;
  1433. //ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1434. }
  1435. break;
  1436. case S_COMPLETE:
  1437. {
  1438. // 回 IDLE
  1439. printf("Back to IDLE = %d --------- \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1440. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = 1;
  1441. }
  1442. break;
  1443. }
  1444. }
  1445. }
  1446. rightBtnPush = true;
  1447. // 右邊的按鈕,只作用在當前頁面,當前頁面如果在可以回 Home 與 停止充電的狀態為可用
  1448. // switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  1449. // {
  1450. // case S_IDLE: {}break;
  1451. // case S_AUTHORIZING: {}break;
  1452. // case S_PREPARING_FOR_EV: {}break;
  1453. // case S_PREPARING_FOR_EVSE: {}break;
  1454. // case S_CCS_PRECHARGE_ST0: {}break;
  1455. // case S_CCS_PRECHARGE_ST1: {}break;
  1456. // case S_CHARGING:
  1457. // {
  1458. // // 停止充電
  1459. // printf("Stop --------------------------------------------------- \n");
  1460. // //ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  1461. // }
  1462. // break;
  1463. // case S_COMPLETE:
  1464. // {
  1465. // //setChargerMode(ShmSysConfigAndInfo->SysInfo.CurGunSelected, MODE_IDLE);
  1466. // }
  1467. // break;
  1468. // }
  1469. }
  1470. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  1471. {
  1472. if(rightBtnPush)
  1473. printf("right btn up............................... \n");
  1474. rightBtnPush = false;
  1475. }
  1476. }
  1477. //===============================================
  1478. // 確認各小板偵測的錯誤狀況
  1479. //===============================================
  1480. void CheckErrorOccurStatus(byte index)
  1481. {
  1482. // 小板
  1483. if (chargingInfo[index]->Type == _Type_Chademo)
  1484. {
  1485. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  1486. EmcOccureByString(index, "023730");
  1487. else if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  1488. EmcOccureByString(index, "011012");
  1489. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1490. EmcOccureByString(index, "012234");
  1491. }
  1492. else if (chargingInfo[index]->Type == _Type_CCS)
  1493. {
  1494. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  1495. EmcOccureByString(index, "011014");
  1496. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1497. EmcOccureByString(index, "012235");
  1498. }
  1499. }
  1500. //===============================================
  1501. // 確認 GPIO 狀態
  1502. //===============================================
  1503. int gpio_get_value(unsigned int gpio, unsigned int *value)
  1504. {
  1505. int fd;
  1506. char buf[MAX_BUF];
  1507. char ch;
  1508. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  1509. fd = open(buf, O_RDONLY);
  1510. if (fd < 0) {
  1511. perror("gpio/get-value");
  1512. return fd;
  1513. }
  1514. read(fd, &ch, 1);
  1515. if (ch != '0') {
  1516. *value = 1;
  1517. } else {
  1518. *value = 0;
  1519. }
  1520. close(fd);
  1521. return 0;
  1522. }
  1523. void CheckGunTypeFromHw()
  1524. {
  1525. int pinIn[4] = { 22, 23, 44, 45 };
  1526. unsigned int gpioValue = 0;
  1527. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  1528. gpio_get_value(pinIn[i], &gpioValue);
  1529. {
  1530. switch (pinIn[i])
  1531. {
  1532. case 22:
  1533. bd1_1_status = gpioValue;
  1534. break;
  1535. case 23:
  1536. bd1_2_status = gpioValue;
  1537. break;
  1538. case 44:
  1539. bd0_1_status = gpioValue;
  1540. break;
  1541. case 45:
  1542. bd0_2_status = gpioValue;
  1543. break;
  1544. }
  1545. }
  1546. }
  1547. }
  1548. void CheckGpioInStatus()
  1549. {
  1550. int pinIn[2] = { 27, 47 };
  1551. unsigned int gpioValue = 0;
  1552. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  1553. {
  1554. gpio_get_value(pinIn[i], &gpioValue);
  1555. if (gpioValue == 0x01)
  1556. {
  1557. switch(pinIn[i])
  1558. {
  1559. // 小板緊急停止
  1560. case 47:
  1561. {
  1562. for(int i = 0; i < _gunCount; i++)
  1563. {
  1564. if (chargingInfo[i]->slotsIndex == 1)
  1565. {
  1566. if (chargingInfo[i]->Type == _Type_Chademo)
  1567. EmcOccureByString(i, "023730");
  1568. else if (chargingInfo[i]->Type == _Type_CCS)
  1569. EmcOccureByString(i, "013627");
  1570. break;
  1571. }
  1572. }
  1573. }
  1574. break;
  1575. case 27:
  1576. {
  1577. for(int i = 0; i < _gunCount; i++)
  1578. {
  1579. if (chargingInfo[i]->slotsIndex == 3)
  1580. {
  1581. if (chargingInfo[i]->Type == _Type_Chademo)
  1582. EmcOccureByString(i, "023730");
  1583. else if (chargingInfo[i]->Type == _Type_CCS)
  1584. EmcOccureByString(i, "013627");
  1585. break;
  1586. }
  1587. }
  1588. }
  1589. break;
  1590. }
  1591. }
  1592. else
  1593. {
  1594. switch (pinIn[i])
  1595. {
  1596. // 小板解除緊急停止
  1597. case 47:
  1598. {
  1599. for(int i = 0; i < _gunCount; i++)
  1600. {
  1601. if (chargingInfo[i]->slotsIndex == 1)
  1602. {
  1603. if (chargingInfo[i]->Type == _Type_Chademo)
  1604. ReleaseEmsOccureByString(i, "023730");
  1605. else if (chargingInfo[i]->Type == _Type_CCS)
  1606. ReleaseEmsOccureByString(i, "013627");
  1607. break;
  1608. }
  1609. }
  1610. }
  1611. break;
  1612. case 27:
  1613. {
  1614. for (int i = 0; i < _gunCount; i++)
  1615. {
  1616. if (chargingInfo[i]->slotsIndex == 3)
  1617. {
  1618. if (chargingInfo[i]->Type == _Type_Chademo)
  1619. ReleaseEmsOccureByString(i, "023730");
  1620. else if (chargingInfo[i]->Type == _Type_CCS)
  1621. ReleaseEmsOccureByString(i, "013627");
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. break;
  1627. }
  1628. }
  1629. }
  1630. }
  1631. //===============================================
  1632. // Main process
  1633. //===============================================
  1634. // 檢查 Byte 中某個 Bit 的值
  1635. // _byte : 欲改變的 byte
  1636. // _bit : 該 byte 的第幾個 bit
  1637. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  1638. {
  1639. return ( _byte & mask_table[_bit] ) != 0x00;
  1640. }
  1641. // 設定 Byte 中某個 Bit的值
  1642. // _byte : 欲改變的 byte
  1643. // _bit : 該 byte 的第幾個 bit
  1644. // value : 修改的值為 0 or 1
  1645. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  1646. {
  1647. if(value == 1)
  1648. *_byte |= (1 << _bit);
  1649. else if (value == 0)
  1650. *_byte ^= (1 << _bit);
  1651. }
  1652. void UserScanFunction()
  1653. {
  1654. bool idleReq = false;
  1655. unsigned char stopReq = 255;
  1656. // 當前非驗證的狀態
  1657. if(!IsAuthorizingMode())
  1658. {
  1659. // 先判斷現在是否可以提供刷卡
  1660. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  1661. // 2. 停止充電
  1662. for (byte i = 0; i < _gunCount; i++)
  1663. {
  1664. // 二擇一
  1665. if (chargingInfo[i]->SystemStatus == S_CHARGING)
  1666. {
  1667. stopReq = i;
  1668. }
  1669. else if (chargingInfo[i]->SystemStatus == S_IDLE)
  1670. {
  1671. idleReq = true;
  1672. }
  1673. }
  1674. // 有閒置的槍號,即可接受刷卡
  1675. if (idleReq || stopReq < _gunCount)
  1676. {
  1677. // 取卡號,假設 : 刷卡過了
  1678. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  1679. {
  1680. if (stopReq < _gunCount)
  1681. {
  1682. char value[32];
  1683. memcpy(value, (unsigned char *)chargingInfo[stopReq]->CardNumber, ARRAY_SIZE(chargingInfo[stopReq]->CardNumber));
  1684. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  1685. {
  1686. ChargingTerminalProcess(stopReq);
  1687. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1688. return;
  1689. }
  1690. }
  1691. if (idleReq)
  1692. {
  1693. // LCM => Authorizing
  1694. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  1695. // 進入確認卡號狀態
  1696. AuthorizingStart();
  1697. // authorizing timer
  1698. StartSystemTimeoutDet(Timeout_Authorizing);
  1699. autoReturnTimeoutFlag = NO;
  1700. }
  1701. }
  1702. }
  1703. }
  1704. else
  1705. {
  1706. // 確認驗證卡號完成沒
  1707. if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  1708. {
  1709. StopSystemTimeoutDet();
  1710. // 判斷後台回覆狀態
  1711. if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  1712. {
  1713. // LCM => Authorize complete
  1714. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  1715. // 通過認證,開始確認當前要進入充電的槍號
  1716. DetectPluginStart();
  1717. }
  1718. else
  1719. {
  1720. // LCM => Authorize fail
  1721. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1722. }
  1723. ClearAuthorizedFlag();
  1724. }
  1725. }
  1726. }
  1727. unsigned char isModeChange(unsigned char gun_index)
  1728. {
  1729. unsigned char result = NO;
  1730. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  1731. {
  1732. result = YES;
  1733. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  1734. }
  1735. return result;
  1736. }
  1737. void ScannerCardProcess()
  1738. {
  1739. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 0)
  1740. {
  1741. isCardScan = true;
  1742. // 處理刷卡及驗證卡號的動作
  1743. UserScanFunction();
  1744. }
  1745. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  1746. {
  1747. StartSystemTimeoutDet(Timeout_VerifyFail);
  1748. isCardScan = false;
  1749. }
  1750. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  1751. {
  1752. StartSystemTimeoutDet(Timeout_VerifyComp);
  1753. }
  1754. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1755. {
  1756. StartSystemTimeoutDet(Timeout_WaitPlug);
  1757. }
  1758. else
  1759. isCardScan = false;
  1760. }
  1761. void AddGunInfoByConnector(byte typeValue, byte slots)
  1762. {
  1763. switch (typeValue)
  1764. {
  1765. case '0': // none
  1766. break;
  1767. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  1768. break;
  1769. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  1770. break;
  1771. case '3': // IEC 62196-2 Type 2 Plug
  1772. break;
  1773. case '4': // IEC 62196-2 Type 2 Socket
  1774. break;
  1775. case '5': // GB/T AC Plug
  1776. break;
  1777. case '6': // GB/T AC Socket
  1778. break;
  1779. case 'J': // CHAdeMO
  1780. {
  1781. if (CHAdeMO_QUANTITY > _chademoIndex)
  1782. {
  1783. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  1784. chargingInfo[_gunIndex]->Index = _gunIndex;
  1785. chargingInfo[_gunIndex]->slotsIndex = slots;
  1786. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  1787. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  1788. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  1789. _chademoIndex++;
  1790. _gunIndex++;
  1791. }
  1792. }
  1793. break;
  1794. case 'U': // CCS1 combo
  1795. break;
  1796. case 'E': // CCS2 combo
  1797. {
  1798. if (CCS_QUANTITY > _ccsIndex)
  1799. {
  1800. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  1801. chargingInfo[_gunIndex]->Index = _gunIndex;
  1802. chargingInfo[_gunIndex]->slotsIndex = slots;
  1803. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  1804. chargingInfo[_gunIndex]->Type = _Type_CCS;
  1805. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  1806. // 現階段預設為走 DIN70121
  1807. ShmCcsData->CommProtocol = 0x01;
  1808. _ccsIndex++;
  1809. _gunIndex++;
  1810. }
  1811. }
  1812. break;
  1813. case 'G': // GBT DC
  1814. break;
  1815. case 'D': // GBT DC x 2
  1816. break;
  1817. }
  1818. }
  1819. bool CheckConnectorTypeStatus()
  1820. {
  1821. bool result = true;
  1822. printf("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  1823. bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  1824. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  1825. {
  1826. byte slots = 1;
  1827. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  1828. {
  1829. AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots);
  1830. slots++;
  1831. }
  1832. _gunCount = _gunIndex;
  1833. printf("_gunCount = %d \n", _gunCount);
  1834. if (_gunCount == 0)
  1835. result = false;
  1836. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  1837. for (byte gunIndex = 0; gunIndex < _gunCount; gunIndex++)
  1838. {
  1839. if (bd0_1_status == 0 && bd0_2_status == 1)
  1840. {
  1841. // 與硬體相同 type : Chademo
  1842. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1843. {
  1844. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1845. }
  1846. }
  1847. else if (bd0_1_status == 1 && bd0_2_status == 0)
  1848. {
  1849. // 與硬體相同 type : CCS
  1850. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  1851. {
  1852. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1853. }
  1854. }
  1855. if (bd1_1_status == 0 && bd1_2_status == 1)
  1856. {
  1857. // 與硬體相同 type : Chademo
  1858. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1859. {
  1860. chargingInfo[gunIndex]->Evboard_id = 0x02;
  1861. }
  1862. if (_gunCount == 1)
  1863. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1864. }
  1865. else if (bd1_1_status == 1 && bd1_2_status == 0)
  1866. {
  1867. // 與硬體相同 type : CCS
  1868. if (chargingInfo[gunIndex]->Type == _Type_CCS)
  1869. {
  1870. chargingInfo[gunIndex]->Evboard_id = 0x02;
  1871. }
  1872. if (_gunCount == 1)
  1873. chargingInfo[gunIndex]->Evboard_id = 0x01;
  1874. }
  1875. printf("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  1876. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  1877. result = false;
  1878. }
  1879. }
  1880. else
  1881. {
  1882. // Module Name 不正確 - 告警
  1883. result = false;
  1884. }
  1885. return result;
  1886. }
  1887. void KillTask()
  1888. {
  1889. ChangeLcmByIndex(_LCM_FIX);
  1890. system("killall Module_EventLogging");
  1891. system("killall Module_PrimaryComm");
  1892. system("killall Module_EvComm");
  1893. system("killall Module_LcmControl");
  1894. system("killall Module_InternalComm");
  1895. system("killall Module_PsuComm");
  1896. }
  1897. char CheckUpdateProcess()
  1898. {
  1899. DIR *d;
  1900. struct dirent *dir;
  1901. d = opendir("/mnt/");
  1902. if (d)
  1903. {
  1904. long int MaxLen=48*1024*1024, ImageLen = 0;
  1905. while ((dir = readdir(d)) != NULL)
  1906. {
  1907. char *new_str;
  1908. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  1909. new_str[0] = '\0';
  1910. strcat(new_str, "/mnt/");
  1911. strcat(new_str, dir->d_name);
  1912. int fd = open(new_str, O_RDONLY);
  1913. if (fd < 0)
  1914. {
  1915. return FAIL;
  1916. }
  1917. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1918. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1919. //get the image length
  1920. ImageLen = read(fd, ptr, MaxLen);
  1921. if (ImageLen > 20)
  1922. {
  1923. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  1924. printf("Typed...%x \r\n", Type);
  1925. switch (Type)
  1926. {
  1927. case 0x10000001:
  1928. case 0x10000002:
  1929. case 0x10000003:
  1930. case 0x10000004:
  1931. case 0x10000005:
  1932. case 0x10000006:
  1933. {
  1934. // CSU_PRIMARY_CONTROLLER : 0x10000006
  1935. int fd = InitComPort(0x04);
  1936. if (Upgrade_UART(fd, Type, 0x04, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  1937. return PASS;
  1938. else
  1939. return FAIL;
  1940. close(fd);
  1941. }
  1942. break;
  1943. case 0x1000000B:
  1944. {
  1945. // CHAdeMO_BOARD : 0x1000000B
  1946. for(byte index = 0; index < _gunCount; index++)
  1947. {
  1948. if (chargingInfo[index]->Type == _Type_Chademo)
  1949. {
  1950. int CanFd = InitCanBus();
  1951. if (CanFd > 0)
  1952. {
  1953. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  1954. {
  1955. printf("Upgrad OK. \n");
  1956. return PASS;
  1957. }
  1958. else
  1959. {
  1960. printf("Upgrad Fail. \n");
  1961. return FAIL;
  1962. }
  1963. }
  1964. else
  1965. {
  1966. printf("Upgrad FD fail. \n");
  1967. return FAIL;
  1968. }
  1969. }
  1970. }
  1971. break;
  1972. }
  1973. }
  1974. }
  1975. free(new_str);
  1976. free(ptr);
  1977. }
  1978. }
  1979. free(dir);
  1980. closedir(d);
  1981. return FAIL;
  1982. }
  1983. void CreateRfidFork()
  1984. {
  1985. pid_t rfidRecPid;
  1986. rfidRecPid = fork();
  1987. if (rfidRecPid == 0)
  1988. {
  1989. while(true)
  1990. {
  1991. // 刷卡判斷
  1992. GetCardNumber();
  1993. usleep(100000);
  1994. }
  1995. }
  1996. }
  1997. void StartSystemTimeoutDet(unsigned char flag)
  1998. {
  1999. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  2000. {
  2001. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  2002. }
  2003. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  2004. }
  2005. void StopSystemTimeoutDet()
  2006. {
  2007. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  2008. }
  2009. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  2010. {
  2011. if (gunIndex < _gunCount)
  2012. {
  2013. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  2014. {
  2015. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  2016. }
  2017. chargingInfo[gunIndex]->TimeoutFlag = flag;
  2018. }
  2019. }
  2020. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  2021. {
  2022. if (gunIndex < _gunCount)
  2023. {
  2024. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  2025. }
  2026. }
  2027. void CreateTimeoutFork()
  2028. {
  2029. pid_t timeoutPid;
  2030. timeoutPid = fork();
  2031. if (timeoutPid == 0)
  2032. {
  2033. while(true)
  2034. {
  2035. // 系統
  2036. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  2037. {
  2038. case Timeout_SelftestChk:
  2039. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 60000000)
  2040. {
  2041. _SelfTestTimeout();
  2042. StopSystemTimeoutDet();
  2043. }
  2044. break;
  2045. case Timeout_Authorizing:
  2046. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 60000000)
  2047. {
  2048. _AuthorizedTimeout();
  2049. StopSystemTimeoutDet();
  2050. }
  2051. break;
  2052. case Timeout_VerifyFail:
  2053. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2054. {
  2055. _AutoReturnTimeout();
  2056. StopSystemTimeoutDet();
  2057. }
  2058. break;
  2059. case Timeout_VerifyComp:
  2060. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 3000000)
  2061. {
  2062. _AutoReturnTimeout();
  2063. StopSystemTimeoutDet();
  2064. }
  2065. break;
  2066. case Timeout_WaitPlug:
  2067. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) >= 40000000)
  2068. {
  2069. _DetectPlugInTimeout();
  2070. StopSystemTimeoutDet();
  2071. }
  2072. break;
  2073. }
  2074. // 各槍
  2075. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2076. {
  2077. switch(chargingInfo[gun_index]->TimeoutFlag)
  2078. {
  2079. case Timeout_Preparing:
  2080. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 30000000)
  2081. {
  2082. _PrepareTimeout(gun_index);
  2083. StopGunInfoTimeoutDet(gun_index);
  2084. }
  2085. break;
  2086. case Timeout_EvChargingDet:
  2087. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 120000000)
  2088. {
  2089. _DetectEvChargingEnableTimeout(gun_index);
  2090. StopGunInfoTimeoutDet(gun_index);
  2091. }
  2092. break;
  2093. case Timeout_EvseChargingDet:
  2094. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2095. {
  2096. _DetectEvseChargingEnableTimeout(gun_index);
  2097. StopGunInfoTimeoutDet(gun_index);
  2098. }
  2099. break;
  2100. case Timeout_WaitforCompleteDet:
  2101. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  2102. {
  2103. _CompleteTimeout(gun_index);
  2104. StopGunInfoTimeoutDet(gun_index);
  2105. }
  2106. break;
  2107. case Timeout_ForCcsPrechargeDet:
  2108. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 60000000)
  2109. {
  2110. _CcsPrechargeTimeout(gun_index);
  2111. StopGunInfoTimeoutDet(gun_index);
  2112. }
  2113. break;
  2114. }
  2115. }
  2116. usleep(100000);
  2117. }
  2118. }
  2119. }
  2120. int main(void)
  2121. {
  2122. InitGPIO();
  2123. InitEthernet();
  2124. sleep(1);
  2125. system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  2126. sleep(1);
  2127. system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up");
  2128. // //echo 1 > /sys/class/gpio/gpio110/value
  2129. //
  2130. // return 0;
  2131. if(CreateShareMemory() == 0)
  2132. {
  2133. #ifdef SystemLogMessage
  2134. DEBUG_ERROR_1("CreatShareMemory NG \n");
  2135. #endif
  2136. if(ShmStatusCodeData!=NULL)
  2137. {
  2138. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  2139. }
  2140. return 0;
  2141. sleep(5);
  2142. system("reboot -f");
  2143. sleep(5);
  2144. system("reboot -f");
  2145. }
  2146. while(1)
  2147. {
  2148. printf("TelcomApn = %s \n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
  2149. printf("TeleConfigData TelcomIpAddress = %s \n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  2150. sleep(1);
  2151. }
  2152. // while(1)
  2153. // {
  2154. // printf("sys id = %s \n", ShmSysConfigAndInfo->SysConfig.SystemId);
  2155. // printf("sys date = %s \n", ShmSysConfigAndInfo->SysConfig.SystemDateTime);
  2156. // printf("Ac Phase Count = %d \n", ShmSysConfigAndInfo->SysConfig.AcPhaseCount);
  2157. // printf("Factory Config = %d \n", ShmSysConfigAndInfo->SysInfo.FactoryConfiguration);
  2158. // printf("Author mode = %d \n", ShmSysConfigAndInfo->SysConfig.AuthorisationMode);
  2159. // printf("Default lang = %d \n", ShmSysConfigAndInfo->SysConfig.DefaultLanguage);
  2160. // printf("Rfid CardNum Endian = %d \n", ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian);
  2161. //
  2162. // printf("MaxChargingEnergy = %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy);
  2163. // printf("MaxChargingPower = %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingPower);
  2164. // printf("MaxChargingCurrent = %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2165. // printf("MaxChargingDuration = %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingDuration);
  2166. // printf("PhaseLossPolicy = %d \n", ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy);
  2167. // printf("CcsAuthentication = %d \n", ShmSysConfigAndInfo->SysConfig.AuthorisationMode);
  2168. // printf("AcCcsChargingModel = %d \n", ShmSysConfigAndInfo->SysConfig.DefaultLanguage);
  2169. //
  2170. // printf("FtpServer = %s \n", ShmSysConfigAndInfo->SysConfig.FtpServer);
  2171. // printf("Eth0DhcpClient = %d \n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient);
  2172. // printf("Eth0IpAddress = %d \n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient);
  2173. // printf("Eth0SubmaskAddress = %d \n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient);
  2174. // printf("Eth0GatewayAddress = %d \n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient);
  2175. // printf("Eth1DhcpClient = %d \n", ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient);
  2176. // printf("Eth1IpAddress = %d \n", ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient);
  2177. // printf("Eth1SubmaskAddress = %d \n", ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient);
  2178. // printf("Eth1GatewayAddress = %d \n", ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthDhcpClient);
  2179. // printf("WifiMode = %d \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  2180. // printf("WifiSsid = %s \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  2181. // printf("WifiPassword = %s \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  2182. // printf("WifiDhcpServer = %d \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpServer);
  2183. // printf("WifiDhcpClient = %d \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient);
  2184. // printf("WifiIpAddress = %s \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  2185. // printf("WifiSubmaskAddress = %s \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  2186. // printf("WifiGatewayAddress = %s \n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  2187. // printf("TelcomApn = %d \n", ShmSysConfigAndInfo->SysConfig.DefaultLanguage);
  2188. // printf("TelcomChapPapId = %s \n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  2189. // printf("TelcomChapPapPwd = %s \n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  2190. // printf("TelcomIpAddress = %s \n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  2191. //
  2192. // printf("BackendConnTimeout = %d \n", ShmSysConfigAndInfo->SysConfig.BackendConnTimeout);
  2193. // printf("OfflinePolicy = %d \n", ShmSysConfigAndInfo->SysConfig.OfflinePolicy);
  2194. // printf("OfflineMaxChargeEnergy = %d \n", ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy);
  2195. // printf("OfflineMaxChargeDuration = %d \n", ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration);
  2196. // printf("OcppServerURL = %s \n", ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  2197. // printf("ChargeBoxId = %s \n", ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  2198. //
  2199. // sleep(3);
  2200. // }
  2201. printf("\n");
  2202. printf("CheckGunTypeFromHw....... \n");
  2203. CheckGunTypeFromHw();
  2204. char *moduleName = "DSYE601J0ET2PH";
  2205. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, moduleName, strlen(moduleName));
  2206. if (!CheckConnectorTypeStatus())
  2207. {
  2208. // Module Name 與硬體對應不正確
  2209. printf("Module Name & HW info none match. \n");
  2210. DEBUG_ERROR_1("Module Name & HW info none match. \n");
  2211. sleep(5);
  2212. return 0;
  2213. }
  2214. printf("Module Name & HW info correct. Initialize.......\n");
  2215. Initialization();
  2216. printf("Spawn all Task. \n");
  2217. SpawnTask();
  2218. ChangeLcmByIndex(_LCM_INIT);
  2219. CreateTimeoutFork();
  2220. printf("Self test. \n");
  2221. SelfTestRun();
  2222. StopSystemTimeoutDet();
  2223. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  2224. {
  2225. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2226. {
  2227. setChargerMode(gun_index, MODE_ALARM);
  2228. }
  2229. ChangeLcmByIndex(_LCM_FIX);
  2230. return FAIL;
  2231. }
  2232. else
  2233. {
  2234. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2235. {
  2236. setChargerMode(gun_index, MODE_IDLE);
  2237. }
  2238. }
  2239. ChangeLcmByIndex(_LCM_IDLE);
  2240. sleep(1);
  2241. //***** 須新增的偵測 *****//
  2242. // 1. Thernal - 控制風扇轉速
  2243. // 2. ouput fuse - 控制風扇轉速
  2244. CreateRfidFork();
  2245. // Main loop
  2246. printf("Main Loop. \n");
  2247. for (;;)
  2248. {
  2249. ChkPrimaryStatus();
  2250. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_IDLE)
  2251. {
  2252. //printf("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  2253. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  2254. {
  2255. KillTask();
  2256. return FAIL;
  2257. if (CheckUpdateProcess() == PASS)
  2258. {
  2259. printf("update complete. \n");
  2260. }
  2261. else
  2262. {
  2263. printf("update fail. \n");
  2264. }
  2265. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  2266. sleep(5);
  2267. system("reboot -f");
  2268. }
  2269. }
  2270. // 讀卡邏輯
  2271. ScannerCardProcess();
  2272. for (byte gun_index = 0; gun_index < _gunCount; gun_index++)
  2273. {
  2274. CheckGpioInStatus();
  2275. CheckErrorOccurStatus(gun_index);
  2276. switch(chargingInfo[gun_index]->SystemStatus)
  2277. {
  2278. case S_IDLE:
  2279. {
  2280. if (isModeChange(gun_index))
  2281. {
  2282. printf("S_IDLE================================== %x \n", gun_index);
  2283. chargingInfo[gun_index]->RemainChargingDuration = 0;
  2284. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  2285. }
  2286. if (ShmSysConfigAndInfo->SysStopChargingAlarmCode.Level == 2)
  2287. {
  2288. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  2289. ClearDetectPluginFlag();
  2290. }
  2291. else
  2292. {
  2293. // 判斷是否有啟用檢查插槍
  2294. if(isDetectPlugin())
  2295. {
  2296. // 卡號驗證成功後,等待充電槍插入充電車
  2297. if (chargingInfo[gun_index]->ConnectorPlugIn == YES)
  2298. {
  2299. ShmSysConfigAndInfo->SysInfo.CurGunSelected = gun_index;
  2300. strcpy((char *)chargingInfo[gun_index]->CardNumber, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  2301. // 當前操作的槍號,進入 Preparing
  2302. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  2303. ClearDetectPluginFlag();
  2304. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2305. }
  2306. else if (!isCardScan)
  2307. {
  2308. // LCM => Waiting for plugging
  2309. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  2310. }
  2311. }
  2312. else
  2313. {
  2314. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2315. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  2316. }
  2317. }
  2318. }
  2319. break;
  2320. case S_REASSIGN_CHECK:
  2321. {
  2322. if (isModeChange(gun_index))
  2323. {
  2324. printf("S_REASSIGN_CHECK================================== %x \n", gun_index);
  2325. StopSystemTimeoutDet();
  2326. }
  2327. bool isRessign = false;
  2328. if (_gunCount > 1)
  2329. {
  2330. for (byte index = 0; index < _gunCount; index++)
  2331. {
  2332. // 有其他槍已經分配好 psu 模塊
  2333. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  2334. chargingInfo[index]->SystemStatus >= S_PREPARNING)
  2335. {
  2336. printf("=============Smart Charging============= Step 1 \n");
  2337. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE;
  2338. isRessign = true;
  2339. break;
  2340. }
  2341. }
  2342. }
  2343. if (isRessign)
  2344. setChargerMode(gun_index, MODE_REASSIGN);
  2345. else
  2346. setChargerMode(gun_index, MODE_PRECHARGE);
  2347. }
  2348. break;
  2349. case S_REASSIGN:
  2350. {
  2351. if (isModeChange(gun_index))
  2352. {
  2353. printf("S_REASSIGN================================== %x \n", gun_index);
  2354. }
  2355. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  2356. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  2357. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  2358. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  2359. setChargerMode(gun_index, MODE_PRECHARGE);
  2360. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY &&
  2361. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  2362. {
  2363. printf("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  2364. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_COMP;
  2365. }
  2366. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2367. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  2368. }
  2369. break;
  2370. case S_PREPARNING:
  2371. {
  2372. if (isModeChange(gun_index))
  2373. {
  2374. printf("S_PREPARNING================================== %x \n", gun_index);
  2375. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  2376. StopGunInfoTimeoutDet(gun_index);
  2377. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  2378. }
  2379. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  2380. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  2381. // Precharge 三個流程 : 1 Precharge, 2 Preparing for ev, 3 Preparing for evse
  2382. // Precharge : AC Contactor <Relay board>, Relay k1 k2 <Relay board>, PSU AddressAssignment, PSU GroupAvailablePower
  2383. // Preparing for ev : 車端通訊流程
  2384. // Preparing for evse : PSU (output 500V, 2A), GFD Test <Relay board>
  2385. //ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES &&
  2386. if (((ShmPsuData->SystemPresentPsuQuantity > 0 &&
  2387. ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 &&
  2388. ShmPsuData->PsuGroup[gun_index].GroupAvailablePower > 10) &&
  2389. chargingInfo[gun_index]->AvailableChargingPower > 10))
  2390. {
  2391. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  2392. }
  2393. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2394. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  2395. }
  2396. break;
  2397. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  2398. {
  2399. if (isModeChange(gun_index))
  2400. {
  2401. printf("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  2402. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2403. StopGunInfoTimeoutDet(gun_index);
  2404. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  2405. }
  2406. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  2407. {
  2408. // 檢查車端的槍鎖是否為鎖上
  2409. if (isEvGunLocked_chademo(gun_index) == YES)
  2410. {
  2411. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  2412. }
  2413. }
  2414. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  2415. {
  2416. // 檢查車端的 charging enable 是否為 1
  2417. if (isEvGunLocked_ccs(gun_index) == YES)
  2418. {
  2419. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  2420. }
  2421. }
  2422. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2423. {
  2424. // 板端要求停止
  2425. ChargingTerminalProcess(gun_index);
  2426. }
  2427. // LCM => Pre-charging
  2428. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2429. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  2430. }
  2431. break;
  2432. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  2433. {
  2434. if (isModeChange(gun_index))
  2435. {
  2436. printf("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  2437. StopGunInfoTimeoutDet(gun_index);
  2438. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  2439. }
  2440. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  2441. {
  2442. // 檢查樁端的 GFD 結果
  2443. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  2444. {
  2445. // 當前操作的槍號,進入 Charging
  2446. setChargerMode(gun_index, MODE_CHARGING);
  2447. }
  2448. }
  2449. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  2450. {
  2451. // 檢查樁端的 GFD 結果
  2452. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS)
  2453. {
  2454. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  2455. }
  2456. }
  2457. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2458. {
  2459. // 板端要求停止
  2460. ChargingTerminalProcess(gun_index);
  2461. }
  2462. // LCM => Pre-charging
  2463. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2464. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  2465. }
  2466. break;
  2467. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  2468. {
  2469. if (isModeChange(gun_index))
  2470. {
  2471. printf("S_CHARGING================================== %x \n", gun_index);
  2472. StopGunInfoTimeoutDet(gun_index);
  2473. ftime(&startChargingTime[gun_index]);
  2474. }
  2475. ftime(&endChargingTime[gun_index]);
  2476. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  2477. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2478. {
  2479. // 板端要求停止
  2480. ChargingTerminalProcess(gun_index);
  2481. }
  2482. // LCM => Charging
  2483. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2484. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  2485. }
  2486. break;
  2487. case S_TERMINATING:
  2488. {
  2489. if (isModeChange(gun_index))
  2490. {
  2491. printf ("terminating......................... %x \n", gun_index);
  2492. StopGunInfoTimeoutDet(gun_index);
  2493. }
  2494. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  2495. {
  2496. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  2497. if (isEvStopCharging_chademo(gun_index) == YES)
  2498. {
  2499. setChargerMode(gun_index, MODE_COMPLETE);
  2500. }
  2501. }
  2502. else if (chargingInfo[gun_index]->Type == _Type_CCS)
  2503. {
  2504. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  2505. if (isEvStopCharging_ccs(gun_index) == YES)
  2506. {
  2507. setChargerMode(gun_index, MODE_COMPLETE);
  2508. }
  2509. }
  2510. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2511. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  2512. }
  2513. break;
  2514. case S_COMPLETE:
  2515. {
  2516. if (isModeChange(gun_index))
  2517. {
  2518. printf ("complete......................... %x \n", gun_index);
  2519. ftime(&endChargingTime[gun_index]);
  2520. if (chargingInfo[gun_index]->RemainChargingDuration != 0)
  2521. chargingInfo[gun_index]->RemainChargingDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  2522. strcpy((char *)chargingInfo[gun_index]->CardNumber, "");
  2523. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2524. StopGunInfoTimeoutDet(gun_index);
  2525. // 測試智能分配,暫時先不要自動回 Idle
  2526. //StartGunInfoTimeoutDet(gun_index, Timeout_WaitforCompleteDet);
  2527. }
  2528. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  2529. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  2530. }
  2531. break;
  2532. case S_CCS_PRECHARGE_ST0:
  2533. {
  2534. if (isModeChange(gun_index))
  2535. {
  2536. printf("CCS Precharge Processing 1....................%x \n", gun_index);
  2537. StopGunInfoTimeoutDet(gun_index);
  2538. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  2539. }
  2540. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2541. {
  2542. // 板端要求停止
  2543. ChargingTerminalProcess(gun_index);
  2544. }
  2545. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  2546. // 切換 D+ Relay to Precharge Relay
  2547. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  2548. {
  2549. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  2550. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  2551. {
  2552. printf("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  2553. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  2554. }
  2555. }
  2556. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  2557. {
  2558. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  2559. }
  2560. break;
  2561. }
  2562. case S_CCS_PRECHARGE_ST1:
  2563. {
  2564. if (isModeChange(gun_index))
  2565. {
  2566. printf("CCS Precharge Processing 2....................%x \n", gun_index);
  2567. }
  2568. if (isEvBoardStopChargeFlag(gun_index) == YES)
  2569. {
  2570. // 板端要求停止
  2571. ChargingTerminalProcess(gun_index);
  2572. }
  2573. // 等待小板通知進入充電
  2574. // 切換 D+ Relay to Precharge Relay
  2575. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  2576. {
  2577. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  2578. setChargerMode(gun_index, MODE_CHARGING);
  2579. }
  2580. break;
  2581. }
  2582. }
  2583. }
  2584. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  2585. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  2586. else
  2587. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  2588. usleep(whileLoopTime);
  2589. }
  2590. return FAIL;
  2591. }