main.c 65 KB

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