Module_PrimaryComm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "PrimaryComm.h"
  31. #include <stdbool.h>
  32. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  33. #define PASS 1
  34. #define FAIL -1
  35. #define YES 1
  36. #define NO 0
  37. typedef unsigned char byte;
  38. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  39. struct StatusCodeData *ShmStatusCodeData;
  40. struct PrimaryMcuData *ShmPrimaryMcuData;
  41. void trim(char *s);
  42. int mystrcmp(char *p1,char *p2);
  43. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  44. void split(char **arr, char *str, const char *del);
  45. int Uart1Fd;
  46. char *priPortName = "/dev/ttyS1";
  47. Ver ver;
  48. Gpio_in gpio_in;
  49. Rtc rtc;
  50. struct timeval _flash_time;
  51. byte flash = NO;
  52. struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  53. unsigned char ChillerSwitch;
  54. unsigned int ChillerOnTime;
  55. void PRINTF_FUNC(char *string, ...);
  56. int StoreLogMsg(const char *fmt, ...);
  57. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  58. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  59. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  60. int StoreLogMsg(const char *fmt, ...)
  61. {
  62. char Buf[4096+256];
  63. char buffer[4096];
  64. va_list args;
  65. struct timeb SeqEndTime;
  66. struct tm *tm;
  67. va_start(args, fmt);
  68. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  69. va_end(args);
  70. memset(Buf,0,sizeof(Buf));
  71. ftime(&SeqEndTime);
  72. SeqEndTime.time = time(NULL);
  73. tm=localtime(&SeqEndTime.time);
  74. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  75. {
  76. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  77. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  78. printf("%s \n", Buf);
  79. }
  80. else
  81. {
  82. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  83. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  84. buffer,
  85. tm->tm_year+1900,tm->tm_mon+1);
  86. system(Buf);
  87. }
  88. return rc;
  89. }
  90. int DiffTimeb(struct timeb ST, struct timeb ET)
  91. {
  92. //return milli-second
  93. unsigned int StartTime,StopTime;
  94. StartTime=(unsigned int)ST.time;
  95. StopTime=(unsigned int)ET.time;
  96. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  97. }
  98. void PRINTF_FUNC(char *string, ...)
  99. {
  100. va_list args;
  101. char buffer[4096];
  102. va_start(args, string);
  103. vsnprintf(buffer, sizeof(buffer), string, args);
  104. va_end(args);
  105. DEBUG_INFO("%s \n", buffer);
  106. }
  107. //=================================
  108. // Common routine
  109. //=================================
  110. char* getTimeString(void)
  111. {
  112. char *result=malloc(21);
  113. time_t timep;
  114. struct tm *p;
  115. time(&timep);
  116. p=gmtime(&timep);
  117. 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);
  118. return result;
  119. }
  120. void trim(char *s)
  121. {
  122. int i=0, j, k, l=0;
  123. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  124. i++;
  125. j = strlen(s)-1;
  126. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  127. j--;
  128. if(i==0 && j==strlen(s)-1) { }
  129. else if(i==0) s[j+1] = '\0';
  130. else {
  131. for(k=i; k<=j; k++) s[l++] = s[k];
  132. s[l] = '\0';
  133. }
  134. }
  135. int mystrcmp(char *p1,char *p2)
  136. {
  137. while(*p1==*p2)
  138. {
  139. if(*p1=='\0' || *p2=='\0')
  140. break;
  141. p1++;
  142. p2++;
  143. }
  144. if(*p1=='\0' && *p2=='\0')
  145. return(PASS);
  146. else
  147. return(FAIL);
  148. }
  149. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  150. {
  151. strncpy(dest, src + start, cnt);
  152. dest[cnt] = 0;
  153. }
  154. void split(char **arr, char *str, const char *del)
  155. {
  156. char *s = strtok(str, del);
  157. while(s != NULL)
  158. {
  159. *arr++ = s;
  160. s = strtok(NULL, del);
  161. }
  162. }
  163. //==========================================
  164. // Init all share memory
  165. //==========================================
  166. int InitShareMemory()
  167. {
  168. int result = PASS;
  169. int MeterSMId;
  170. //creat ShmSysConfigAndInfo
  171. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  172. {
  173. #ifdef SystemLogMessage
  174. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  175. #endif
  176. result = FAIL;
  177. }
  178. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  179. {
  180. #ifdef SystemLogMessage
  181. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  182. #endif
  183. result = FAIL;
  184. }
  185. //creat ShmStatusCodeData
  186. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  187. {
  188. #ifdef SystemLogMessage
  189. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  190. #endif
  191. result = FAIL;
  192. }
  193. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  194. {
  195. #ifdef SystemLogMessage
  196. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  197. #endif
  198. result = FAIL;
  199. }
  200. //creat ShmStatusCodeData
  201. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  202. {
  203. #ifdef SystemLogMessage
  204. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  205. #endif
  206. result = FAIL;
  207. }
  208. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  209. {
  210. #ifdef ShmPrimaryMcuData
  211. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  212. #endif
  213. result = FAIL;
  214. }
  215. return result;
  216. }
  217. //================================================
  218. // Function
  219. //================================================
  220. void GetFwAndHwVersion()
  221. {
  222. if(Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  223. {
  224. //PRINTF_FUNC("Primary FW Rev = %s \n", ver.Version_FW);
  225. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  226. strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
  227. }
  228. if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  229. ;//PRINTF_FUNC("Primary HW Rev = %s \n", ver.Version_HW);
  230. }
  231. void GetInputGpioStatus()
  232. {
  233. //PRINTF_FUNC("GetInputGpioStatus \n");
  234. if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS)
  235. {
  236. #ifndef DD360
  237. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector;
  238. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  239. #else
  240. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  241. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  242. #endif
  243. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  244. ShmPrimaryMcuData->InputDet.bits.DoorOpen = ~gpio_in.Door_Open;
  245. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  246. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  247. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  248. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0]&0x01;
  249. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1]&0x01;
  250. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2]&0x01;
  251. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3]&0x01;
  252. /*printf(" gpio_in.Key[0]~ gpio_in.Key[3]=%d, %d, %d, %d\n",
  253. ShmPrimaryMcuData->InputDet.bits.Key0 , ShmPrimaryMcuData->InputDet.bits.Key1,
  254. ShmPrimaryMcuData->InputDet.bits.Key2,ShmPrimaryMcuData->InputDet.bits.Key3);
  255. printf("ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning=%d\n", ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning);
  256. printf("ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsLiquidChillerWaterLevelFault=%d\n", ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsLiquidChillerWaterLevelFault);
  257. */
  258. //PRINTF_FUNC("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
  259. //PRINTF_FUNC("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
  260. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  261. #ifndef DD360
  262. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES)
  263. DEBUG_ERROR("AC Mainbreaker occur. \n");
  264. #endif
  265. }
  266. }
  267. void SetOutputGpio(byte flash)
  268. {
  269. Gpio_out gpio;
  270. gpio.Button_LED[0] = flash;
  271. gpio.Button_LED[1] = flash;
  272. gpio.System_LED[0] = 0x00;
  273. gpio.System_LED[1] = 0x00;
  274. gpio.System_LED[2] = 0x00;
  275. gpio.System_LED[3] = 0x00;
  276. #ifdef DD360
  277. if((ChargingData[0]->PresentChargingCurrent)>=150)
  278. {
  279. ChillerSwitch=1;
  280. ChillerOnTime=time((time_t*)NULL);
  281. }
  282. else
  283. {
  284. if(ChillerSwitch==1)
  285. {
  286. if((ChargingData[0]->PresentChargingCurrent)>=100)
  287. {
  288. ChillerSwitch=1;
  289. ChillerOnTime=time((time_t*)NULL);
  290. }
  291. else
  292. {
  293. if((time((time_t*)NULL)-ChillerOnTime)>=600)
  294. {
  295. ChillerSwitch=0;
  296. }
  297. else
  298. {
  299. ChillerSwitch=1;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. ChillerSwitch=0;
  306. }
  307. }
  308. gpio.AC_Connector = ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  309. //printf("ChargingData[0]->PresentChargingCurrent=%f,ChargingData[1]->PresentChargingCurrent=%f,ChillerSwitch=%d\n",ChargingData[0]->PresentChargingCurrent,ChargingData[1]->PresentChargingCurrent,ChillerSwitch);
  310. #else
  311. gpio.AC_Connector = 0x00;
  312. #endif
  313. gpio.AC_Breaker = 0x00;
  314. if (Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio) == PASS)
  315. {
  316. //PRINTF_FUNC("SetOutputGpio sucessfully. %d \n", flash);
  317. }
  318. else
  319. {
  320. //PRINTF_FUNC("SetOutputGpio fail. \n");
  321. }
  322. }
  323. void SetRtcData()
  324. {
  325. struct timeb csuTime;
  326. struct tm *tmCSU;
  327. ftime(&csuTime);
  328. tmCSU = localtime(&csuTime.time);
  329. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  330. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  331. // tmCSU->tm_sec);
  332. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  333. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  334. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  335. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  336. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  337. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  338. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  339. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  340. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  341. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  342. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  343. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  344. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  345. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  346. if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS)
  347. {
  348. //PRINTF_FUNC("SetRtc sucessfully. \n");
  349. }
  350. else
  351. {
  352. //PRINTF_FUNC("SetRtc fail. \n");
  353. }
  354. }
  355. //================================================
  356. // Main process
  357. //================================================
  358. int InitComPort()
  359. {
  360. int fd;
  361. struct termios tios;
  362. fd = open(priPortName, O_RDWR);
  363. if(fd<=0)
  364. {
  365. #ifdef SystemLogMessage
  366. DEBUG_ERROR("open 407 Communication port NG \n");
  367. #endif
  368. return -1;
  369. }
  370. ioctl (fd, TCGETS, &tios);
  371. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  372. tios.c_lflag = 0;
  373. tios.c_iflag = 0;
  374. tios.c_oflag = 0;
  375. tios.c_cc[VMIN]=0;
  376. tios.c_cc[VTIME]=(unsigned char)1;
  377. tios.c_lflag=0;
  378. tcflush(fd, TCIFLUSH);
  379. ioctl (fd, TCSETS, &tios);
  380. return fd;
  381. }
  382. unsigned long GetTimeoutValue(struct timeval _sour_time)
  383. {
  384. struct timeval _end_time;
  385. gettimeofday(&_end_time, NULL);
  386. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  387. }
  388. int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  389. {
  390. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  391. {
  392. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  393. {
  394. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  395. return 1;
  396. }
  397. }
  398. for (byte index = 0; index < CCS_QUANTITY; index++)
  399. {
  400. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  401. {
  402. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  403. return 1;
  404. }
  405. }
  406. for (byte index = 0; index < GB_QUANTITY; index++)
  407. {
  408. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  409. {
  410. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  411. return 1;
  412. }
  413. }
  414. return 0;
  415. }
  416. int main(void)
  417. {
  418. if(InitShareMemory() == FAIL)
  419. {
  420. #ifdef SystemLogMessage
  421. DEBUG_ERROR("InitShareMemory NG\n");
  422. #endif
  423. if(ShmStatusCodeData!=NULL)
  424. {
  425. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  426. }
  427. sleep(5);
  428. return 0;
  429. }
  430. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  431. {
  432. if (!FindChargingInfoData(_index, &ChargingData[0]))
  433. {
  434. DEBUG_ERROR("FindChargingInfoData false \n");
  435. break;
  436. }
  437. }
  438. ChillerSwitch=0;
  439. Uart1Fd = InitComPort();
  440. //PRINTF_FUNC("407 Port id = %d \n", Uart1Fd);
  441. if(Uart1Fd < 0)
  442. {
  443. #ifdef SystemLogMessage
  444. DEBUG_ERROR("InitComPort (Uart1 : AM3352 - STM32) NG");
  445. #endif
  446. if (ShmStatusCodeData != NULL)
  447. {
  448. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  449. }
  450. sleep(5);
  451. return 0;
  452. }
  453. SetRtcData();
  454. gettimeofday(&_flash_time, NULL);
  455. for(;;)
  456. {
  457. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == 0x00)
  458. {
  459. if ((GetTimeoutValue(_flash_time) / 1000) > 1000)
  460. {
  461. if (flash == NO)
  462. flash = YES;
  463. else
  464. flash = NO;
  465. SetOutputGpio(flash);
  466. gettimeofday(&_flash_time, NULL);
  467. }
  468. }
  469. else
  470. {
  471. if ((GetTimeoutValue(_flash_time) / 1000) > 5000)
  472. {
  473. if (flash == NO)
  474. flash = YES;
  475. SetOutputGpio(flash);
  476. gettimeofday(&_flash_time, NULL);
  477. }
  478. }
  479. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  480. // 模組更新 FW 後,需重新做
  481. if(ShmPrimaryMcuData->SelfTest_Comp != PASS)
  482. {
  483. //PRINTF_FUNC("(407) Get Fw and Hw Ver. \n");
  484. GetFwAndHwVersion();
  485. sleep(1);
  486. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  487. }
  488. else
  489. {
  490. GetInputGpioStatus();
  491. }
  492. usleep(100000);
  493. }
  494. return FAIL;
  495. }