ReModule_PrimaryComm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 struct StChillerInfo {
  38. uint8_t ChillerSwitch;
  39. uint32_t ChillerOnTime;
  40. uint8_t Reserved[3];
  41. } ChillerInfo;
  42. typedef struct StLedConfig {
  43. //OutputDrvValue[0]
  44. uint8_t LeftButtonLed: 1; //bit 0, H: ON, L:OFF
  45. uint8_t RightButtonLed: 1; //bit 1, H: ON, L:OFF
  46. uint8_t GreenLED: 1; //bit 2, H: ON, L:OFF
  47. uint8_t YellowLED: 1; //bit 3, H: ON, L:OFF
  48. uint8_t RedLED: 1; //bit 4, H: ON, L:OFF
  49. uint8_t SystemLed4: 1; //bit 5, H: ON, L:OFF
  50. uint8_t AcContactor: 1; //bit 6, H: ON, L:OFF
  51. uint8_t Reserved: 1; //bit 7 reserved
  52. } LedConfig;
  53. typedef unsigned char byte;
  54. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  55. struct StatusCodeData *ShmStatusCodeData;
  56. struct PrimaryMcuData *ShmPrimaryMcuData;
  57. void trim(char *s);
  58. int mystrcmp(char *p1, char *p2);
  59. void substr(char *dest, const char *src, unsigned int start, unsigned int cnt);
  60. void split(char **arr, char *str, const char *del);
  61. int Uart1Fd;
  62. char *priPortName = "/dev/ttyS1";
  63. Ver ver;
  64. Gpio_in gpio_in;
  65. Rtc rtc;
  66. struct timeval _flash_time;
  67. byte flash = NO;
  68. byte gun_count; //DS60-120 add
  69. byte _curDeviceStatus[3] = {0, 0, 0}; //DS60-120 add
  70. byte _reCheckCount[3] = {0, 0, 0}; //DS60-120 add
  71. struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  72. #if defined DD360 ||defined DD360Audi || defined DD360ComBox
  73. unsigned char ChillerSwitch; //DS60-120 remove
  74. unsigned int ChillerOnTime; //DS60-120 remove
  75. #endif //defined DD360 || defined DD360Audi
  76. int StoreLogMsg(const char *fmt, ...);
  77. #define log_info(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  78. #define log_warn(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  79. #define log_error(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  80. int StoreLogMsg(const char *fmt, ...)
  81. {
  82. char Buf[4096 + 256];
  83. char buffer[4096];
  84. va_list args;
  85. struct timeb SeqEndTime;
  86. struct tm *tm;
  87. va_start(args, fmt);
  88. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  89. va_end(args);
  90. memset(Buf, 0, sizeof(Buf));
  91. ftime(&SeqEndTime);
  92. SeqEndTime.time = time(NULL);
  93. tm = localtime(&SeqEndTime.time);
  94. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES) {
  95. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  96. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  97. printf("%s \n", Buf);
  98. } else {
  99. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  100. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  101. buffer,
  102. tm->tm_year + 1900, tm->tm_mon + 1);
  103. system(Buf);
  104. }
  105. return rc;
  106. }
  107. int DiffTimeb(struct timeb ST, struct timeb ET)
  108. {
  109. //return milli-second
  110. unsigned int StartTime, StopTime;
  111. StartTime = (unsigned int)ST.time;
  112. StopTime = (unsigned int)ET.time;
  113. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  114. }
  115. //=================================
  116. // Common routine
  117. //=================================
  118. char *getTimeString(void)
  119. {
  120. char *result = malloc(21);
  121. time_t timep;
  122. struct tm *p;
  123. time(&timep);
  124. p = gmtime(&timep);
  125. 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);
  126. return result;
  127. }
  128. void trim(char *s)
  129. {
  130. int i = 0, j, k, l = 0;
  131. while ((s[i] == ' ') || (s[i] == '\t') || (s[i] == '\n')) {
  132. i++;
  133. }
  134. j = strlen(s) - 1;
  135. while ((s[j] == ' ') || (s[j] == '\t') || (s[j] == '\n')) {
  136. j--;
  137. }
  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. if (*p1 == '\0' || *p2 == '\0') {
  149. break;
  150. }
  151. p1++;
  152. p2++;
  153. }
  154. if (*p1 == '\0' && *p2 == '\0') {
  155. return (PASS);
  156. } else {
  157. return (FAIL);
  158. }
  159. }
  160. void substr(char *dest, const char *src, unsigned int start, unsigned int cnt)
  161. {
  162. strncpy(dest, src + start, cnt);
  163. dest[cnt] = 0;
  164. }
  165. void split(char **arr, char *str, const char *del)
  166. {
  167. char *s = strtok(str, del);
  168. while (s != NULL) {
  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. result = FAIL;
  183. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  184. result = FAIL;
  185. }
  186. //creat ShmStatusCodeData
  187. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  188. result = FAIL;
  189. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  190. result = FAIL;
  191. }
  192. //creat ShmStatusCodeData
  193. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  194. result = FAIL;
  195. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  196. result = FAIL;
  197. }
  198. return result;
  199. }
  200. //================================================
  201. // Function
  202. //================================================
  203. void GetFwAndHwVersion()
  204. {
  205. if (Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS) {
  206. //log_info("Primary FW Rev = %s \n", ver.Version_FW);
  207. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  208. strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
  209. }
  210. //if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  211. // ;//log_info("Primary HW Rev = %s \n", ver.Version_HW);
  212. }
  213. void GetInputGpioStatus()
  214. {
  215. uint8_t dispenserSwTmp = 0;
  216. static uint8_t dispenserSw = 0;
  217. //log_info("GetInputGpioStatus \n");
  218. if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS) {
  219. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  220. //DS60-120 add
  221. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != gpio_in.AC_Connector) {
  222. if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3) {
  223. _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = gpio_in.AC_Connector;
  224. ShmSysConfigAndInfo->SysInfo.AcContactorStatus =
  225. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec =
  226. gpio_in.AC_Connector;
  227. } else {
  228. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT]++;
  229. }
  230. } else {
  231. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] = 0;
  232. }
  233. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] != gpio_in.AC_MainBreaker) {
  234. if (_reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] >= 3) {
  235. _curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = gpio_in.AC_MainBreaker;
  236. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  237. } else {
  238. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER]++;
  239. }
  240. } else {
  241. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = 0;
  242. }
  243. //ShmSysConfigAndInfo->SysInfo.AcContactorStatus = ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector;
  244. //ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  245. #else
  246. if ((strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[7], "V", 1) == 0) ||
  247. (strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[9], "V", 1) == 0) ||
  248. (strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[7], "F", 1) == 0) ||
  249. (strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[9], "F", 1) == 0)
  250. ) {
  251. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  252. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  253. } else {
  254. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = gpio_in.AC_Connector;
  255. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsLiquidChillerWaterLevelFault = gpio_in.AC_MainBreaker;
  256. }
  257. #endif //!defined DD360 && !defined DD360Audi
  258. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  259. #if defined DD360 ||defined DD360Audi || defined DD360ComBox
  260. #if defined DD360ComBox
  261. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  262. #else
  263. ShmPrimaryMcuData->InputDet.bits.DoorOpen = ~gpio_in.Door_Open;
  264. #endif //
  265. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0] & 0x01;
  266. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1] & 0x01;
  267. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2] & 0x01;
  268. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3] & 0x01;
  269. //ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  270. #else
  271. ShmPrimaryMcuData->InputDet.bits.Key0 = gpio_in.Key[0] & 0x01;
  272. ShmPrimaryMcuData->InputDet.bits.Key1 = gpio_in.Key[1] & 0x01;
  273. ShmPrimaryMcuData->InputDet.bits.Key2 = gpio_in.Key[2] & 0x01;
  274. ShmPrimaryMcuData->InputDet.bits.Key3 = gpio_in.Key[3] & 0x01;
  275. //ShmPrimaryMcuData->InputDet.bits.EmergencyButton = ~gpio_in.Emergency_Btn;
  276. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  277. #endif //defined DD360 ||defined DD360Audi
  278. #if defined DD360ComBox
  279. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = ~gpio_in.Emergency_Btn;
  280. #else
  281. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  282. #endif //
  283. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key0);
  284. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key1 << 1);
  285. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key2 << 2);
  286. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  287. if (dispenserSwTmp != dispenserSw) {
  288. dispenserSw = dispenserSwTmp;
  289. log_info("Dispenser switch number = %d, bit = %d, %d, %d, %d\r\n",
  290. dispenserSw,
  291. ShmPrimaryMcuData->InputDet.bits.Key3,
  292. ShmPrimaryMcuData->InputDet.bits.Key2,
  293. ShmPrimaryMcuData->InputDet.bits.Key1,
  294. ShmPrimaryMcuData->InputDet.bits.Key0);
  295. }
  296. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  297. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  298. /*printf(" gpio_in.Key[0]~ gpio_in.Key[3]=%d, %d, %d, %d\n",
  299. ShmPrimaryMcuData->InputDet.bits.Key0 , ShmPrimaryMcuData->InputDet.bits.Key1,
  300. ShmPrimaryMcuData->InputDet.bits.Key2,ShmPrimaryMcuData->InputDet.bits.Key3);
  301. printf("ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning=%d\n", ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning);
  302. printf("ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsLiquidChillerWaterLevelFault=%d\n", ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsLiquidChillerWaterLevelFault);
  303. */
  304. //log_info("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
  305. //log_info("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
  306. //log_info("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  307. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  308. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES) {
  309. log_error("AC Mainbreaker occur. \n");
  310. }
  311. #endif //!defined DD360 && !defined DD360Audi
  312. }
  313. }
  314. static void checkChillerStatus(Gpio_out *gpio)
  315. {
  316. uint8_t gunIndex = 0;
  317. uint8_t chillerCount = 0;
  318. static ChillerInfo fChillerInfo[2] = {0};
  319. Gpio_out *pGpio = (Gpio_out *)gpio;
  320. if ((strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[7], "V", 1) == 0) ||
  321. (strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[7], "F", 1) == 0)) {
  322. chillerCount++;
  323. }
  324. if ((strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[9], "V", 1) == 0) ||
  325. (strncmp((char *)&ShmSysConfigAndInfo->SysConfig.ModelName[9], "F", 1) == 0)) {
  326. chillerCount++;
  327. }
  328. if (chillerCount == 0) {
  329. pGpio->AC_Connector = 0x00;
  330. return;
  331. }
  332. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++) {
  333. if ((ChargingData[gunIndex]->PresentChargingCurrent) >= 150) {
  334. fChillerInfo[gunIndex].ChillerSwitch = YES;
  335. fChillerInfo[gunIndex].ChillerOnTime = time((time_t *)NULL);
  336. } else {
  337. if (fChillerInfo[gunIndex].ChillerSwitch == YES) {
  338. if ((ChargingData[gunIndex]->PresentChargingCurrent) >= 100) {
  339. fChillerInfo[gunIndex].ChillerSwitch = YES;
  340. fChillerInfo[gunIndex].ChillerOnTime = time((time_t *)NULL);
  341. } else {
  342. if ((time((time_t *)NULL) - fChillerInfo[gunIndex].ChillerOnTime) >= 600) {
  343. fChillerInfo[gunIndex].ChillerSwitch = NO;
  344. } else {
  345. fChillerInfo[gunIndex].ChillerSwitch = YES;
  346. }
  347. }
  348. } else {
  349. fChillerInfo[gunIndex].ChillerSwitch = NO;
  350. }
  351. }
  352. }
  353. pGpio->AC_Connector = fChillerInfo[gunIndex].ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  354. }
  355. void SetOutputGpio(uint8_t outputValue)
  356. {
  357. Gpio_out gpio;
  358. LedConfig *pLedConfig = (LedConfig *)&outputValue;
  359. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == 0x00) {
  360. if (flash == NO) {
  361. flash = YES;
  362. } else {
  363. flash = NO;
  364. }
  365. } else {
  366. if (flash == NO) {
  367. flash = YES;
  368. }
  369. }
  370. pLedConfig->LeftButtonLed = flash;
  371. pLedConfig->RightButtonLed = flash;
  372. gpio.Button_LED[0] = pLedConfig->LeftButtonLed;
  373. gpio.Button_LED[1] = pLedConfig->RightButtonLed;
  374. gpio.System_LED[0] = pLedConfig->GreenLED;
  375. gpio.System_LED[1] = pLedConfig->YellowLED;
  376. gpio.System_LED[2] = pLedConfig->RedLED;
  377. gpio.System_LED[3] = 0x00;
  378. checkChillerStatus(&gpio);
  379. gpio.AC_Breaker = 0x00;
  380. Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio);
  381. }
  382. void SetRtcData()
  383. {
  384. struct timeb csuTime;
  385. struct tm *tmCSU;
  386. ftime(&csuTime);
  387. tmCSU = localtime(&csuTime.time);
  388. // log_info("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  389. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  390. // tmCSU->tm_sec);
  391. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  392. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  393. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  394. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  395. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  396. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  397. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  398. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  399. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  400. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  401. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  402. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  403. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  404. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  405. if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS) {
  406. //log_info("SetRtc sucessfully. \n");
  407. } else {
  408. //log_info("SetRtc fail. \n");
  409. }
  410. }
  411. void SetModelName()
  412. {
  413. if (Config_Model_Name(Uart1Fd, Addr.IoExtend, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS) {
  414. }
  415. }
  416. //================================================
  417. // Main process
  418. //================================================
  419. int InitComPort()
  420. {
  421. int fd;
  422. struct termios tios;
  423. fd = open(priPortName, O_RDWR);
  424. if (fd <= 0) {
  425. #ifdef SystemLogMessage
  426. log_error("open 407 Communication port NG \n");
  427. #endif
  428. return -1;
  429. }
  430. ioctl (fd, TCGETS, &tios);
  431. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  432. tios.c_lflag = 0;
  433. tios.c_iflag = 0;
  434. tios.c_oflag = 0;
  435. tios.c_cc[VMIN] = 0;
  436. tios.c_cc[VTIME] = (unsigned char)1;
  437. tios.c_lflag = 0;
  438. tcflush(fd, TCIFLUSH);
  439. ioctl (fd, TCSETS, &tios);
  440. return fd;
  441. }
  442. unsigned long GetTimeoutValue(struct timeval _sour_time)
  443. {
  444. struct timeval _end_time;
  445. gettimeofday(&_end_time, NULL);
  446. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  447. }
  448. int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  449. {
  450. for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
  451. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target) {
  452. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  453. return 1;
  454. }
  455. }
  456. for (byte index = 0; index < CCS_QUANTITY; index++) {
  457. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target) {
  458. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  459. return 1;
  460. }
  461. }
  462. for (byte index = 0; index < GB_QUANTITY; index++) {
  463. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target) {
  464. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  465. return 1;
  466. }
  467. }
  468. return 0;
  469. }
  470. void Initialization() //DS60-120 add
  471. {
  472. bool isPass = false;
  473. while (!isPass) {
  474. isPass = true;
  475. for (byte _index = 0; _index < gun_count; _index++) {
  476. if (!FindChargingInfoData(_index, &ChargingData[0])) {
  477. log_error("EvComm (main) : FindChargingInfoData false \n");
  478. isPass = false;
  479. break;
  480. }
  481. }
  482. sleep(1);
  483. }
  484. }
  485. int main(void)
  486. {
  487. if (InitShareMemory() == FAIL) {
  488. log_error("InitShareMemory NG\r\n");
  489. if (ShmStatusCodeData != NULL) {
  490. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  491. }
  492. sleep(5);
  493. return 0;
  494. }
  495. //DS60-120 remove
  496. //for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  497. // if (!FindChargingInfoData(_index, &ChargingData[0])) {
  498. // log_error("FindChargingInfoData false \n");
  499. // break;
  500. // }
  501. //}
  502. ChillerSwitch = 0;
  503. Uart1Fd = InitComPort();
  504. //log_info("407 Port id = %d \n", Uart1Fd);
  505. if (Uart1Fd < 0) {
  506. #ifdef SystemLogMessage
  507. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  508. #endif
  509. if (ShmStatusCodeData != NULL) {
  510. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  511. }
  512. sleep(5);
  513. return 0;
  514. }
  515. SetRtcData();
  516. SetModelName();//DS60-120 add
  517. gettimeofday(&_flash_time, NULL);
  518. //DS60-120 add
  519. gun_count = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  520. Initialization();
  521. byte _count = 0;
  522. for (;;) {
  523. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  524. // 模組更新 FW 後,需重新做
  525. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  526. //log_info("(407) Get Fw and Hw Ver. \n");
  527. GetFwAndHwVersion();
  528. sleep(1);
  529. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  530. } else {
  531. SetOutputGpio(ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  532. GetInputGpioStatus();
  533. }
  534. usleep(50000);
  535. }
  536. return FAIL;
  537. }