ReModule_PrimaryComm.c 24 KB

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