main.c 78 KB

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