Module_PrimaryComm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.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 <string.h>
  23. #include <time.h>
  24. #include <ctype.h>
  25. #include <ifaddrs.h>
  26. #include <math.h>
  27. #include "../../define.h"
  28. #include "PrimaryComm.h"
  29. #include <stdbool.h>
  30. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  31. #define PASS 1
  32. #define FAIL -1
  33. #define YES 1
  34. #define NO 0
  35. #define EQUAL 0
  36. typedef unsigned char byte;
  37. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  38. struct StatusCodeData *ShmStatusCodeData;
  39. struct PrimaryMcuData *ShmPrimaryMcuData;
  40. struct MeterInformation *ShmCsuMeterData;
  41. struct DcCommonInformation *ShmDcCommonData;
  42. void trim(char *s);
  43. int mystrcmp(char *p1,char *p2);
  44. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  45. void split(char **arr, char *str, const char *del);
  46. int Uart1Fd;
  47. char *priPortName = "/dev/ttyS1";
  48. Ver ver;
  49. Gpio_in gpio_in;
  50. Rtc rtc;
  51. struct timespec _flash_time;
  52. byte flash = NO;
  53. byte gun_count;
  54. byte _curDeviceStatus[3] = {0, 0, 0};
  55. byte _reCheckCount[3] = {0, 0, 0};
  56. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  57. void PRINTF_FUNC(char *string, ...);
  58. int StoreLogMsg(const char *fmt, ...);
  59. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  60. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  61. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  62. int StoreLogMsg(const char *fmt, ...)
  63. {
  64. char Buf[4096+256];
  65. char buffer[4096];
  66. va_list args;
  67. struct timeb SeqEndTime;
  68. struct tm *tm;
  69. va_start(args, fmt);
  70. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  71. va_end(args);
  72. memset(Buf,0,sizeof(Buf));
  73. ftime(&SeqEndTime);
  74. SeqEndTime.time = time(NULL);
  75. tm=localtime(&SeqEndTime.time);
  76. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  77. {
  78. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  79. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  80. printf("%s \n", Buf);
  81. }
  82. else
  83. {
  84. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s",
  85. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  86. buffer,
  87. tm->tm_year+1900,tm->tm_mon+1,
  88. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  89. system(Buf);
  90. }
  91. return rc;
  92. }
  93. int DiffTimeb(struct timeb ST, struct timeb ET)
  94. {
  95. //return milli-second
  96. unsigned int StartTime,StopTime;
  97. StartTime=(unsigned int)ST.time;
  98. StopTime=(unsigned int)ET.time;
  99. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  100. }
  101. void PRINTF_FUNC(char *string, ...)
  102. {
  103. va_list args;
  104. char buffer[4096];
  105. va_start(args, string);
  106. vsnprintf(buffer, sizeof(buffer), string, args);
  107. va_end(args);
  108. DEBUG_INFO("%s ", buffer);
  109. }
  110. //=================================
  111. // Common routine
  112. //=================================
  113. char* getTimeString(void)
  114. {
  115. char *result=malloc(21);
  116. time_t timep;
  117. struct tm *p;
  118. time(&timep);
  119. p=gmtime(&timep);
  120. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
  121. return result;
  122. }
  123. void trim(char *s)
  124. {
  125. int i=0, j, k, l=0;
  126. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  127. i++;
  128. j = strlen(s)-1;
  129. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  130. j--;
  131. if(i==0 && j==strlen(s)-1) { }
  132. else if(i==0) s[j+1] = '\0';
  133. else {
  134. for(k=i; k<=j; k++) s[l++] = s[k];
  135. s[l] = '\0';
  136. }
  137. }
  138. int mystrcmp(char *p1,char *p2)
  139. {
  140. while(*p1==*p2)
  141. {
  142. if(*p1=='\0' || *p2=='\0')
  143. break;
  144. p1++;
  145. p2++;
  146. }
  147. if(*p1=='\0' && *p2=='\0')
  148. return(PASS);
  149. else
  150. return(FAIL);
  151. }
  152. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  153. {
  154. strncpy(dest, src + start, cnt);
  155. dest[cnt] = 0;
  156. }
  157. void split(char **arr, char *str, const char *del)
  158. {
  159. char *s = strtok(str, del);
  160. while(s != NULL)
  161. {
  162. *arr++ = s;
  163. s = strtok(NULL, del);
  164. }
  165. }
  166. //==========================================
  167. // Init all share memory
  168. //==========================================
  169. int InitShareMemory()
  170. {
  171. int result = PASS;
  172. int MeterSMId;
  173. //creat ShmSysConfigAndInfo
  174. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  175. {
  176. #ifdef SystemLogMessage
  177. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  178. #endif
  179. result = FAIL;
  180. }
  181. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  182. {
  183. #ifdef SystemLogMessage
  184. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  185. #endif
  186. result = FAIL;
  187. }
  188. //creat ShmStatusCodeData
  189. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  190. {
  191. #ifdef SystemLogMessage
  192. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  193. #endif
  194. result = FAIL;
  195. }
  196. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  197. {
  198. #ifdef SystemLogMessage
  199. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  200. #endif
  201. result = FAIL;
  202. }
  203. //creat ShmStatusCodeData
  204. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  205. {
  206. #ifdef SystemLogMessage
  207. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  208. #endif
  209. result = FAIL;
  210. }
  211. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  212. {
  213. #ifdef ShmPrimaryMcuData
  214. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  215. #endif
  216. result = FAIL;
  217. }
  218. if ((MeterSMId = shmget(ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777)) < 0)
  219. {
  220. #ifdef SystemLogMessage
  221. DEBUG_ERROR("shmget ShmCsuMeterKey NG \n");
  222. #endif
  223. result = FAIL;
  224. }
  225. else if ((ShmCsuMeterData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  226. {
  227. #ifdef SystemLogMessage
  228. DEBUG_ERROR("shmat ShmCsuMeterData NG \n");
  229. #endif
  230. result = FAIL;
  231. }
  232. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  233. {
  234. #ifdef SystemLogMessage
  235. DEBUG_ERROR("shmget ShmCommonKey NG \n");
  236. #endif
  237. result = FAIL;
  238. }
  239. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  240. {
  241. #ifdef SystemLogMessage
  242. DEBUG_ERROR("shmat ShmCommonKey NG \n");
  243. #endif
  244. result = FAIL;
  245. }
  246. return result;
  247. }
  248. //================================================
  249. // Function
  250. //================================================
  251. void GetFwAndHwVersion()
  252. {
  253. if(Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  254. {
  255. //PRINTF_FUNC("s1 = %s \n", ver.Version_FW);
  256. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  257. strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
  258. }
  259. if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  260. {
  261. //PRINTF_FUNC("s2 = %s \n", ver.Version_HW);
  262. }
  263. }
  264. void GetInputGpioStatus()
  265. {
  266. if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS)
  267. {
  268. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != gpio_in.AC_Connector ||
  269. _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != ShmDcCommonData->psuKeepCommunication)
  270. {
  271. if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3)
  272. {
  273. if (ShmDcCommonData->psuKeepCommunication == YES ||
  274. gpio_in.AC_Connector == YES)
  275. {
  276. ShmSysConfigAndInfo->SysInfo.AcContactorStatus =
  277. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = YES;
  278. _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = YES;
  279. }
  280. else
  281. {
  282. ShmSysConfigAndInfo->SysInfo.AcContactorStatus =
  283. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = NO;
  284. _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = NO;
  285. }
  286. }
  287. else
  288. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT]++;
  289. }
  290. else
  291. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] = 0;
  292. if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3 &&
  293. ShmDcCommonData->psuKeepCommunication == YES &&
  294. gpio_in.AC_Connector == NO)
  295. {
  296. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  297. }
  298. else
  299. {
  300. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  301. }
  302. // -------------------------------------------------------------------
  303. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] != gpio_in.AC_MainBreaker)
  304. {
  305. if (_reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] >= 3)
  306. {
  307. _curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = gpio_in.AC_MainBreaker;
  308. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  309. }
  310. else
  311. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER]++;
  312. }
  313. else
  314. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = 0;
  315. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  316. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  317. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  318. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  319. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  320. //PRINTF_FUNC("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
  321. //PRINTF_FUNC("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
  322. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  323. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES)
  324. DEBUG_ERROR("AC Mainbreaker occur. \n");
  325. }
  326. }
  327. void GetMeterConsumption(byte index)
  328. {
  329. if (Query_Meter_value(Uart1Fd, Addr.IoExtend, &ShmCsuMeterData->_meter[index], &ShmCsuMeterData->isWorking, index) == PASS)
  330. {
  331. // meter value (Resolution) : 10
  332. if (!ShmCsuMeterData->isWorking)
  333. {
  334. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterCommTimeout = YES;
  335. return;
  336. }
  337. if (ShmCsuMeterData->_meter[index].isCalculation == YES)
  338. {
  339. // printf("Meter%d, Comsumption = %.1f, curMeterValue = %.1f \n",
  340. // index,
  341. // ShmCsuMeterData->_meter[index].newMeterValue,
  342. // ShmCsuMeterData->_meter[index].curMeterValue);
  343. if (ShmCsuMeterData->_meter[index].curMeterValue == 0)
  344. ShmCsuMeterData->_meter[index].curMeterValue = ShmCsuMeterData->_meter[index].newMeterValue;
  345. else
  346. {
  347. // 最大充時,兩個都會累加,但只輸出給一槍使用
  348. ShmCsuMeterData->_meter[index]._chargingValue += ShmCsuMeterData->_meter[index].newMeterValue - ShmCsuMeterData->_meter[index].curMeterValue;
  349. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX ||
  350. (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  351. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY_A_TO_M))
  352. {
  353. for (byte subIndex = 0; subIndex < gun_count; subIndex++)
  354. {
  355. if (_chargingData[subIndex]->SystemStatus == SYS_MODE_CHARGING)
  356. {
  357. ShmCsuMeterData->_meter[subIndex]._curTotalCharging += ShmCsuMeterData->_meter[index].newMeterValue - ShmCsuMeterData->_meter[index].curMeterValue;
  358. break;
  359. }
  360. }
  361. }
  362. else
  363. {
  364. ShmCsuMeterData->_meter[index]._curTotalCharging += ShmCsuMeterData->_meter[index].newMeterValue - ShmCsuMeterData->_meter[index].curMeterValue;
  365. }
  366. ShmCsuMeterData->_meter[index].curMeterValue = ShmCsuMeterData->_meter[index].newMeterValue;
  367. }
  368. }
  369. }
  370. }
  371. void SetOutputGpio(byte flash)
  372. {
  373. Gpio_out gpio;
  374. // 藍燈
  375. gpio.Button_LED[0] = flash;
  376. // 綠燈
  377. gpio.Button_LED[1] = flash;
  378. //gpio.Button_LED[1] = 0x00;
  379. gpio.System_LED[0] = 0x00;
  380. gpio.System_LED[1] = 0x00;
  381. gpio.System_LED[2] = 0x00;
  382. gpio.System_LED[3] = 0x00;
  383. gpio.AC_Connector = 0x00;
  384. gpio.AC_Breaker = 0x00;
  385. if (Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio) == PASS)
  386. {
  387. //PRINTF_FUNC("SetOutputGpio sucessfully. %d \n", flash);
  388. }
  389. else
  390. {
  391. //PRINTF_FUNC("SetOutputGpio fail. \n");
  392. }
  393. }
  394. void SetRtcData()
  395. {
  396. struct timeb csuTime;
  397. struct tm *tmCSU;
  398. ftime(&csuTime);
  399. tmCSU = localtime(&csuTime.time);
  400. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  401. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  402. // tmCSU->tm_sec);
  403. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  404. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  405. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  406. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  407. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  408. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  409. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  410. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  411. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  412. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  413. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  414. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  415. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  416. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  417. if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS)
  418. {
  419. //PRINTF_FUNC("SetRtc sucessfully. \n");
  420. }
  421. else
  422. {
  423. //PRINTF_FUNC("SetRtc fail. \n");
  424. }
  425. }
  426. void SetModelName()
  427. {
  428. if (Config_Model_Name(Uart1Fd, Addr.IoExtend, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  429. {
  430. }
  431. }
  432. //================================================
  433. // Main process
  434. //================================================
  435. int InitComPort()
  436. {
  437. int fd;
  438. struct termios tios;
  439. fd = open(priPortName, O_RDWR);
  440. if(fd<=0)
  441. {
  442. #ifdef SystemLogMessage
  443. DEBUG_ERROR("open 407 Communication port NG \n");
  444. #endif
  445. return -1;
  446. }
  447. ioctl (fd, TCGETS, &tios);
  448. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  449. tios.c_lflag = 0;
  450. tios.c_iflag = 0;
  451. tios.c_oflag = 0;
  452. tios.c_cc[VMIN]=0;
  453. tios.c_cc[VTIME]=(unsigned char)1;
  454. tios.c_lflag=0;
  455. tcflush(fd, TCIFLUSH);
  456. ioctl (fd, TCSETS, &tios);
  457. return fd;
  458. }
  459. int GetTimeoutValue(struct timespec *startTime)
  460. {
  461. struct timespec endTime;
  462. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  463. return endTime.tv_sec - startTime->tv_sec;
  464. }
  465. void GetTimespecFunc(struct timespec *time)
  466. {
  467. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  468. }
  469. //================================================
  470. //================================================
  471. // CANBUS receive task
  472. //================================================
  473. //================================================
  474. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  475. {
  476. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  477. {
  478. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  479. {
  480. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  481. return true;
  482. }
  483. }
  484. for (byte index = 0; index < CCS_QUANTITY; index++)
  485. {
  486. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  487. {
  488. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  489. return true;
  490. }
  491. }
  492. for (byte index = 0; index < GB_QUANTITY; index++)
  493. {
  494. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  495. {
  496. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  497. return true;
  498. }
  499. }
  500. return false;
  501. }
  502. void Initialization()
  503. {
  504. bool isPass = false;
  505. while(!isPass)
  506. {
  507. isPass = true;
  508. for (byte _index = 0; _index < gun_count; _index++)
  509. {
  510. if (!FindChargingInfoData(_index, &_chargingData[0]))
  511. {
  512. DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
  513. isPass = false;
  514. break;
  515. }
  516. }
  517. sleep(1);
  518. }
  519. }
  520. int main(void)
  521. {
  522. if(InitShareMemory() == FAIL)
  523. {
  524. #ifdef SystemLogMessage
  525. DEBUG_ERROR("InitShareMemory NG\n");
  526. #endif
  527. if(ShmStatusCodeData!=NULL)
  528. {
  529. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  530. }
  531. sleep(5);
  532. return 0;
  533. }
  534. Uart1Fd = InitComPort();
  535. if(Uart1Fd < 0)
  536. {
  537. #ifdef SystemLogMessage
  538. DEBUG_ERROR("InitComPort (Uart1 : AM3352 - STM32) NG");
  539. #endif
  540. if (ShmStatusCodeData != NULL)
  541. {
  542. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  543. }
  544. sleep(5);
  545. return 0;
  546. }
  547. SetRtcData();
  548. SetModelName();
  549. GetTimespecFunc(&_flash_time);
  550. gun_count = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  551. Initialization();
  552. byte _count = 0;
  553. for(;;)
  554. {
  555. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == EQUAL)
  556. {
  557. if (GetTimeoutValue(&_flash_time) > 1)
  558. {
  559. if (flash == NO)
  560. flash = YES;
  561. else
  562. flash = NO;
  563. SetOutputGpio(flash);
  564. GetTimespecFunc(&_flash_time);
  565. }
  566. }
  567. else
  568. {
  569. if (GetTimeoutValue(&_flash_time) > 5)
  570. {
  571. if (flash == NO)
  572. flash = YES;
  573. SetOutputGpio(flash);
  574. GetTimespecFunc(&_flash_time);
  575. }
  576. }
  577. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  578. // 模組更新 FW 後,需重新做
  579. if(ShmPrimaryMcuData->SelfTest_Comp != PASS)
  580. {
  581. PRINTF_FUNC("(407) Get Fw and Hw Ver. \n");
  582. GetFwAndHwVersion();
  583. sleep(1);
  584. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  585. }
  586. else
  587. {
  588. GetInputGpioStatus();
  589. if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  590. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
  591. {
  592. GetMeterConsumption(_count);
  593. _count++;
  594. if (_count >= gun_count)
  595. _count = 0;
  596. }
  597. }
  598. usleep(100000);
  599. }
  600. return FAIL;
  601. }