Module_PrimaryComm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 <stdbool.h>
  16. #include <unistd.h>
  17. #include <stdarg.h>
  18. #include <stdio.h> /*標準輸入輸出定義*/
  19. #include <stdlib.h> /*標準函數庫定義*/
  20. #include <unistd.h> /*Unix 標準函數定義*/
  21. #include <fcntl.h> /*檔控制定義*/
  22. #include <termios.h> /*PPSIX 終端控制定義*/
  23. #include <errno.h> /*錯誤號定義*/
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include <ctype.h>
  28. #include <ifaddrs.h>
  29. #include <math.h>
  30. #include "../Log/log.h"
  31. #include "../Define/define.h"
  32. #include "../Config.h"
  33. #include "../ShareMemory/shmMem.h"
  34. #include "PrimaryComm.h"
  35. #include "Module_PrimaryComm.h"
  36. //------------------------------------------------------------------------------
  37. //struct SysConfigAndInfo *ShmSysConfigAndInfo;
  38. //struct StatusCodeData *ShmStatusCodeData;
  39. static struct SysConfigData *pSysConfig = NULL;
  40. static struct SysInfoData *pSysInfo = NULL;
  41. static struct AlarmCodeData *pAlarmCode = NULL;
  42. static struct FaultCodeData *pFaultCode = NULL;
  43. static struct PrimaryMcuData *ShmPrimaryMcuData;
  44. const char *priPortName = "/dev/ttyS1";
  45. uint8_t gun_count; //DS60-120 add
  46. uint8_t EmgBtn_count = 0;
  47. uint8_t Door_count = 0;
  48. uint8_t EmgBtn_flag = 0;
  49. uint8_t Door_flag = 0;
  50. //struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  51. //------------------------------------------------------------------------------
  52. /*int StoreLogMsg(const char *fmt, ...)
  53. {
  54. char Buf[4096 + 256];
  55. char buffer[4096];
  56. va_list args;
  57. struct timeb SeqEndTime;
  58. struct tm *tm;
  59. va_start(args, fmt);
  60. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  61. va_end(args);
  62. memset(Buf, 0, sizeof(Buf));
  63. ftime(&SeqEndTime);
  64. SeqEndTime.time = time(NULL);
  65. tm = localtime(&SeqEndTime.time);
  66. if (pSysConfig->SwitchDebugFlag == YES) {
  67. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  68. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  69. printf("%s \n", Buf);
  70. } else {
  71. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  72. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  73. buffer,
  74. tm->tm_year + 1900, tm->tm_mon + 1);
  75. system(Buf);
  76. }
  77. return rc;
  78. }
  79. */
  80. #if 0 //non use
  81. int DiffTimeb(struct timeb ST, struct timeb ET)
  82. {
  83. //return milli-second
  84. unsigned int StartTime, StopTime;
  85. StartTime = (unsigned int)ST.time;
  86. StopTime = (unsigned int)ET.time;
  87. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  88. }
  89. //=================================
  90. // Common routine
  91. //=================================
  92. char *getTimeString(void)
  93. {
  94. char *result = malloc(21);
  95. time_t timep;
  96. struct tm *p;
  97. time(&timep);
  98. p = gmtime(&timep);
  99. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]",
  100. (1900 + p->tm_year),
  101. (1 + p->tm_mon),
  102. p->tm_mday,
  103. p->tm_hour,
  104. p->tm_hour,
  105. p->tm_sec);
  106. return result;
  107. }
  108. #endif //0
  109. //==========================================
  110. // Init all share memory
  111. //==========================================
  112. /*int InitShareMemory()
  113. {
  114. int result = PASS;
  115. int MeterSMId;
  116. //creat ShmSysConfigAndInfo
  117. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  118. result = FAIL;
  119. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  120. result = FAIL;
  121. }
  122. //creat ShmStatusCodeData
  123. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  124. result = FAIL;
  125. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  126. result = FAIL;
  127. }
  128. //creat ShmStatusCodeData
  129. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  130. result = FAIL;
  131. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  132. result = FAIL;
  133. }
  134. return result;
  135. }
  136. */
  137. //================================================
  138. // Function
  139. //================================================
  140. void GetFwAndHwVersion(int fd)
  141. {
  142. Ver ver = {0};
  143. if (Query_FW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS) {
  144. //log_info("Primary FW Rev = %s \n", ver.Version_FW);
  145. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  146. strcpy((char *) pSysInfo->CsuPrimFwRev, ver.Version_FW);
  147. }
  148. //if (Query_HW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS)
  149. // ;//log_info("Primary HW Rev = %s \n", ver.Version_HW);
  150. }
  151. void GetInputGpioStatus(int fd)
  152. {
  153. uint8_t dispenserSwTmp = 0;
  154. Gpio_in gpio_in = {0};
  155. static uint8_t dispenserSw = 0;
  156. //log_info("GetInputGpioStatus \n");
  157. if (Query_Gpio_Input(fd, OP_ADDR_IO_EXTEND, &gpio_in) != PASS) {
  158. return;
  159. }
  160. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  161. //log_info("======== gpio_in.Emergency_Btn:%d =============\n",gpio_in.Emergency_Btn);
  162. //log_info("======== gpio_in.Door_Open:%d =============\n",gpio_in.Door_Open);
  163. #if defined DD360ComBox
  164. if (gpio_in.Emergency_Btn == 0 && (EmgBtn_flag == gpio_in.Emergency_Btn))
  165. #else
  166. if (gpio_in.Emergency_Btn && (EmgBtn_flag != gpio_in.Emergency_Btn))
  167. #endif //defined DD360ComBox
  168. {
  169. EmgBtn_count++;
  170. if (EmgBtn_count > SensorTrigCount) {
  171. EmgBtn_flag = 1;
  172. EmgBtn_count = 0; // Avoid Overflow
  173. }
  174. #ifdef DD360ComBox
  175. } else if ( gpio_in.Emergency_Btn && EmgBtn_flag ) {
  176. #else
  177. } else if (EmgBtn_flag != gpio_in.Emergency_Btn ) {
  178. #endif
  179. EmgBtn_count++;
  180. if (EmgBtn_count > SensorTrigCount) {
  181. EmgBtn_flag = 0;
  182. EmgBtn_count = 0;
  183. }
  184. }
  185. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = EmgBtn_flag;
  186. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key0);
  187. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key1 << 1);
  188. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key2 << 2);
  189. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  190. if (dispenserSwTmp != dispenserSw) {
  191. dispenserSw = dispenserSwTmp;
  192. log_info("Dispenser switch number = %d, bit = %d, %d, %d, %d\r\n",
  193. dispenserSw,
  194. ShmPrimaryMcuData->InputDet.bits.Key3,
  195. ShmPrimaryMcuData->InputDet.bits.Key2,
  196. ShmPrimaryMcuData->InputDet.bits.Key1,
  197. ShmPrimaryMcuData->InputDet.bits.Key0);
  198. }
  199. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  200. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  201. #if defined DD360 || defined DD360Audi || defined DD360ComBox
  202. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  203. (strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  204. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0) ||
  205. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)
  206. ) {
  207. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  208. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  209. } else {
  210. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = gpio_in.AC_Connector;
  211. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = gpio_in.AC_MainBreaker;
  212. }
  213. #if defined DD360ComBox
  214. if (gpio_in.Door_Open && (Door_flag != gpio_in.Door_Open))
  215. #else
  216. if (gpio_in.Door_Open == 0 && (Door_flag == gpio_in.Door_Open))
  217. #endif //defined DD360ComBox
  218. {
  219. Door_count++;
  220. if (Door_count > SensorTrigCount) {
  221. Door_flag = 1;
  222. Door_count = 0; // Avoid Overflow
  223. }
  224. #ifdef DD360ComBox
  225. } else if (gpio_in.Door_Open == 0 && Door_flag) {
  226. #else
  227. } else if (gpio_in.Door_Open && Door_flag) {
  228. #endif
  229. Door_count++;
  230. if (Door_count > SensorTrigCount) {
  231. Door_flag = 0;
  232. Door_count = 0;
  233. }
  234. }
  235. ShmPrimaryMcuData->InputDet.bits.DoorOpen = Door_flag;
  236. /*
  237. log_info("Emergency Button Count = %d , Emergency flag = %d\n",
  238. EmgBtn_count,EmgBtn_flag);
  239. log_info("Door Sensor Count = %d , Door Sensor flag = %d\n",
  240. Door_count,Door_flag);
  241. */
  242. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0] & 0x01;
  243. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1] & 0x01;
  244. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2] & 0x01;
  245. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3] & 0x01;
  246. return;
  247. #endif //defined DD360 || defined DD360Audi || defined DD360ComBox
  248. static uint8_t _curDeviceStatus[3] = {0};
  249. static uint8_t _reCheckCount[3] = {0};
  250. //DS60-120 add
  251. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != gpio_in.AC_Connector) {
  252. if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3) {
  253. _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = gpio_in.AC_Connector;
  254. pSysInfo->AcContactorStatus =
  255. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec =
  256. gpio_in.AC_Connector;
  257. } else {
  258. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT]++;
  259. }
  260. } else {
  261. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] = 0;
  262. }
  263. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] != gpio_in.AC_MainBreaker) {
  264. if (_reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] >= 3) {
  265. _curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = gpio_in.AC_MainBreaker;
  266. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  267. } else {
  268. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER]++;
  269. }
  270. } else {
  271. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = 0;
  272. }
  273. //pSysInfo->AcContactorStatus = ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector;
  274. //ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  275. ShmPrimaryMcuData->InputDet.bits.Key0 = gpio_in.Key[0] & 0x01;
  276. ShmPrimaryMcuData->InputDet.bits.Key1 = gpio_in.Key[1] & 0x01;
  277. ShmPrimaryMcuData->InputDet.bits.Key2 = gpio_in.Key[2] & 0x01;
  278. ShmPrimaryMcuData->InputDet.bits.Key3 = gpio_in.Key[3] & 0x01;
  279. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  280. /*printf(" gpio_in.Key[0]~ gpio_in.Key[3]=%d, %d, %d, %d\n",
  281. ShmPrimaryMcuData->InputDet.bits.Key0 , ShmPrimaryMcuData->InputDet.bits.Key1,
  282. ShmPrimaryMcuData->InputDet.bits.Key2,ShmPrimaryMcuData->InputDet.bits.Key3);
  283. printf("pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning=%d\n", pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning);
  284. printf("pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault=%d\n", pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault);
  285. */
  286. //log_info("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
  287. //log_info("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
  288. //log_info("pSysInfo->AcContactorStatus = %d \n", pSysInfo->AcContactorStatus);
  289. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES) {
  290. log_error("AC Mainbreaker occur. \n");
  291. }
  292. }
  293. static void checkChillerStatus(Gpio_out *gpio)
  294. {
  295. uint8_t gunIndex = 0;
  296. uint8_t chillerCount = 0;
  297. struct ChargingInfoData *pDcChargingInfo = NULL;
  298. static ChillerInfo fChillerInfo[2] = {0}, *pChillerInfo = NULL;
  299. static ChillerInfo _chiller;
  300. Gpio_out *pGpio = (Gpio_out *)gpio;
  301. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  302. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)) {
  303. chillerCount++;
  304. }
  305. if ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  306. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)) {
  307. chillerCount++;
  308. }
  309. if (chillerCount == 0) {
  310. pGpio->AC_Connector = 0x00;
  311. return;
  312. }
  313. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++) {
  314. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  315. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  316. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  317. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  318. {
  319. pChillerInfo->ChillerSwitch = YES;
  320. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  321. }
  322. else
  323. {
  324. if(pChillerInfo->ChillerSwitch == YES)
  325. {
  326. //10分鐘後停止
  327. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600)
  328. {
  329. pChillerInfo->ChillerSwitch = NO;
  330. }
  331. }
  332. else
  333. {
  334. pChillerInfo->ChillerSwitch = NO;
  335. }
  336. }
  337. #if 0
  338. if ((pDcChargingInfo->PresentChargingCurrent) >= 150) { //當前電壓於150A,打開水冷機
  339. pChillerInfo->ChillerSwitch = YES;
  340. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  341. } else {
  342. if (pChillerInfo->ChillerSwitch == YES) {
  343. if ((pDcChargingInfo->PresentChargingCurrent) >= 100) { //判斷如果還是大於100A不變動
  344. pChillerInfo->ChillerSwitch = YES;
  345. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  346. } else {
  347. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600) { //5分鐘後停止
  348. pChillerInfo->ChillerSwitch = NO;
  349. } else {
  350. pChillerInfo->ChillerSwitch = YES;
  351. }
  352. }
  353. } else {
  354. pChillerInfo->ChillerSwitch = NO;
  355. }
  356. }
  357. #endif
  358. }
  359. uint8_t _chillerNeedOn = NO;
  360. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++)
  361. {
  362. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  363. if(pChillerInfo->ChillerSwitch == YES)
  364. {
  365. _chillerNeedOn = YES;
  366. }
  367. }
  368. if(_chiller.ChillerSwitch != _chillerNeedOn)
  369. {
  370. log_info("Chiller Need Turn %s\n", _chillerNeedOn == YES ? "ON" : "OFF");
  371. }
  372. _chiller.ChillerSwitch = _chillerNeedOn;
  373. pGpio->AC_Connector = _chiller.ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  374. }
  375. void SetOutputGpio(int fd, uint8_t outputValue)
  376. {
  377. Gpio_out gpio;
  378. LedConfig *pLedConfig = (LedConfig *)&outputValue;
  379. static uint8_t flash = NO;
  380. if (strcmp((char *)pSysInfo->LcmHwRev, " ") == 0x00) {
  381. if (flash == NO) {
  382. flash = YES;
  383. } else {
  384. flash = NO;
  385. }
  386. } else {
  387. if (flash == NO) {
  388. flash = YES;
  389. }
  390. }
  391. pLedConfig->LeftButtonLed = flash;
  392. pLedConfig->RightButtonLed = flash;
  393. gpio.Button_LED[0] = pLedConfig->LeftButtonLed;
  394. gpio.Button_LED[1] = pLedConfig->RightButtonLed;
  395. gpio.System_LED[0] = pLedConfig->GreenLED;
  396. gpio.System_LED[1] = pLedConfig->YellowLED;
  397. gpio.System_LED[2] = pLedConfig->RedLED;
  398. gpio.System_LED[3] = 0x00;
  399. checkChillerStatus(&gpio);
  400. gpio.AC_Breaker = 0x00;
  401. Config_Gpio_Output(fd, OP_ADDR_IO_EXTEND, &gpio);
  402. }
  403. void SetRtcData(int fd)
  404. {
  405. Rtc rtc = {0};
  406. struct timeb csuTime;
  407. struct tm *tmCSU;
  408. ftime(&csuTime);
  409. tmCSU = localtime(&csuTime.time);
  410. //log_info("Time : %04d-%02d-%02d %02d:%02d:%02d \n",
  411. // tmCSU->tm_year + 1900,
  412. // tmCSU->tm_mon + 1,
  413. // tmCSU->tm_mday,
  414. // tmCSU->tm_hour,
  415. // tmCSU->tm_min,
  416. // tmCSU->tm_sec);
  417. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  418. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  419. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  420. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  421. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  422. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  423. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  424. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  425. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  426. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  427. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  428. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  429. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  430. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  431. if (Config_Rtc_Data(fd, OP_ADDR_IO_EXTEND, &rtc) == PASS) {
  432. //log_info("SetRtc sucessfully. \n");
  433. } else {
  434. //log_info("SetRtc fail. \n");
  435. }
  436. }
  437. void SetModelName(int fd)
  438. {
  439. if (Config_Model_Name(fd, OP_ADDR_IO_EXTEND, pSysConfig->ModelName) == PASS) {
  440. }
  441. }
  442. //================================================
  443. // Main process
  444. //================================================
  445. int InitComPort()
  446. {
  447. int fd;
  448. struct termios tios;
  449. fd = open(priPortName, O_RDWR);
  450. if (fd <= 0) {
  451. #ifdef SystemLogMessage
  452. log_error("open 407 Communication port NG \n");
  453. #endif
  454. return -1;
  455. }
  456. ioctl (fd, TCGETS, &tios);
  457. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  458. tios.c_lflag = 0;
  459. tios.c_iflag = 0;
  460. tios.c_oflag = 0;
  461. tios.c_cc[VMIN] = 0;
  462. tios.c_cc[VTIME] = (uint8_t)1;
  463. tios.c_lflag = 0;
  464. tcflush(fd, TCIFLUSH);
  465. ioctl (fd, TCSETS, &tios);
  466. return fd;
  467. }
  468. unsigned long GetTimeoutValue(struct timeval _sour_time)
  469. {
  470. struct timeval _end_time;
  471. gettimeofday(&_end_time, NULL);
  472. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  473. }
  474. //int FindChargingInfoData(uint8_t target, struct ChargingInfoData **chargingData)
  475. //{
  476. // for (uint8_t index = 0; index < CHAdeMO_QUANTITY; index++) {
  477. // if (pSysInfo->ChademoChargingData[index].Index == target) {
  478. // chargingData[target] = &pSysInfo->ChademoChargingData[index];
  479. // return 1;
  480. // }
  481. // }
  482. //
  483. // for (uint8_t index = 0; index < CCS_QUANTITY; index++) {
  484. // if (pSysInfo->CcsChargingData[index].Index == target) {
  485. // chargingData[target] = &pSysInfo->CcsChargingData[index];
  486. // return 1;
  487. // }
  488. // }
  489. //
  490. // for (uint8_t index = 0; index < GB_QUANTITY; index++) {
  491. // if (pSysInfo->GbChargingData[index].Index == target) {
  492. // chargingData[target] = &pSysInfo->GbChargingData[index];
  493. // return 1;
  494. // }
  495. // }
  496. //
  497. // return 0;
  498. //}
  499. //void Initialization() //DS60-120 add
  500. //{
  501. // bool isPass = false;
  502. // while (!isPass) {
  503. // isPass = true;
  504. // for (uint8_t _index = 0; _index < gun_count; _index++) {
  505. // if (!FindChargingInfoData(_index, &ChargingData[0])) {
  506. // log_error("EvComm (main) : FindChargingInfoData false \n");
  507. // isPass = false;
  508. // break;
  509. // }
  510. // }
  511. // sleep(1);
  512. // }
  513. //}
  514. static bool IsPrimaryProcessNeedPause(void)
  515. {
  516. bool _pause = false;
  517. static bool isPause = false;
  518. struct ChargingInfoData *pDcChargingInfo = NULL;
  519. for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++)
  520. {
  521. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  522. if(pDcChargingInfo->SystemStatus == S_UPDATE)
  523. {
  524. _pause = true;
  525. }
  526. }
  527. if(isPause != _pause)
  528. {
  529. log_info("Primary Process Now Is %s \n", _pause == true ? "Paused" : "Continued");
  530. }
  531. isPause = _pause;
  532. return _pause;
  533. }
  534. int main(void)
  535. {
  536. int Uart1Fd = -1;
  537. //if (InitShareMemory() == FAIL) {
  538. // log_error("InitShareMemory NG\r\n");
  539. // if (ShmStatusCodeData != NULL) {
  540. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  541. // }
  542. // sleep(5);
  543. // return 0;
  544. //}
  545. if (CreateAllCsuShareMemory() == FAIL) {
  546. log_error("create share memory error\r\n");
  547. return FAIL;
  548. }
  549. MappingGunChargingInfo("Primary Task");
  550. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  551. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  552. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  553. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  554. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  555. Uart1Fd = InitComPort();
  556. //log_info("407 Port id = %d \n", Uart1Fd);
  557. if (Uart1Fd < 0) {
  558. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  559. if (pAlarmCode != NULL) {
  560. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  561. }
  562. sleep(5);
  563. return 0;
  564. }
  565. SetRtcData(Uart1Fd);
  566. SetModelName(Uart1Fd);
  567. gun_count = pSysConfig->TotalConnectorCount;
  568. //Initialization();
  569. for (;;) {
  570. if(IsPrimaryProcessNeedPause() == true)
  571. {
  572. sleep(1);
  573. continue;
  574. }
  575. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  576. // 模組更新 FW 後,需重新做
  577. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  578. //log_info("(407) Get Fw and Hw Ver. \n");
  579. GetFwAndHwVersion(Uart1Fd);
  580. sleep(1);
  581. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  582. } else {
  583. SetOutputGpio(Uart1Fd, ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  584. GetInputGpioStatus(Uart1Fd);
  585. }
  586. usleep(50000);
  587. }
  588. return FAIL;
  589. }