Module_PrimaryComm.c 20 KB

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