Module_PrimaryComm.c 18 KB

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