ReModule_PrimaryComm.c 21 KB

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