Module_EvRxComm.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #include <linux/can.h>
  9. #include <linux/can/raw.h>
  10. #include <time.h>
  11. #include "../Config.h"
  12. #include "../Log/log.h"
  13. #include "../Define/define.h"
  14. #include "../ShareMemory/shmMem.h"
  15. #include "Ev_Comm.h"
  16. #include "Module_EvComm.h"
  17. //------------------------------------------------------------------------------
  18. static struct SysConfigData *pSysConfig = NULL;
  19. static struct SysInfoData *pSysInfo = NULL;
  20. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  21. static struct AlarmCodeData *pAlarmCode = NULL;
  22. static struct CHAdeMOData *ShmCHAdeMOData = NULL;
  23. static struct GBTData *ShmGBTData = NULL;
  24. static struct CcsData *ShmCcsData = NULL;
  25. static struct FanModuleData *ShmFanModuleData = NULL;
  26. static DcCommonInfo *ShmDcCommonData = NULL;
  27. #define TempArraySize 10
  28. uint8_t TempArray_1[TempArraySize]={0};
  29. uint8_t TempArray_2[TempArraySize]={0};
  30. uint8_t ptemp_1 = 0;
  31. uint8_t ptemp_2 = 0;
  32. bool firstcircule = true;
  33. float EvTargetVolt[2] = { 0 };
  34. float EvTargetCur[2] = { 0 };
  35. //------------------------------------------------------------------------------
  36. extern bool AbnormalStopAnalysis(uint8_t gun_index, uint8_t *errCode);
  37. //------------------------------------------------------------------------------
  38. /*static uint8_t getMaxConnectTempAndChiller(uint8_t headTemp1, uint8_t headTemp2,
  39. uint8_t chillerTemp1, uint8_t chillerTemp2)
  40. {
  41. uint8_t i = 0;
  42. uint8_t tempSource[4] = {headTemp1, headTemp2, chillerTemp1, chillerTemp2};
  43. uint8_t maxTemp = 0;
  44. if (headTemp1 == UNDEFINED_TEMP &&
  45. headTemp2 == UNDEFINED_TEMP &&
  46. chillerTemp1 == UNDEFINED_TEMP &&
  47. chillerTemp2 == UNDEFINED_TEMP) {
  48. return UNDEFINED_TEMP;
  49. }
  50. //先取得第一個非UNDEFINED_TEMP的值
  51. for (i = 0; i < (sizeof(tempSource) / sizeof(uint8_t)); i++) {
  52. if (tempSource[i] != UNDEFINED_TEMP) {
  53. maxTemp = tempSource[i];
  54. break;
  55. }
  56. }
  57. //找最大的溫度值
  58. for (i = 0; i < (sizeof(tempSource) / sizeof(uint8_t)); i++) {
  59. if (tempSource[i] != UNDEFINED_TEMP) {
  60. if (maxTemp < tempSource[i]) {
  61. maxTemp = tempSource[i];
  62. }
  63. }
  64. }
  65. return maxTemp;
  66. }
  67. */
  68. static uint8_t getAvageTemp(uint8_t value,uint8_t gun)
  69. {
  70. uint16_t avagetemp = 0;
  71. int i;
  72. uint8_t ptr = 0;
  73. uint8_t *pArray;
  74. if(gun == 0) {
  75. pArray = &TempArray_1;
  76. ptr = ptemp_1;
  77. } else {
  78. pArray = &TempArray_2;
  79. ptr = ptemp_2;
  80. }
  81. pArray[ptr] = value;
  82. for(i=0;i<TempArraySize;i++)
  83. avagetemp+=pArray[i];
  84. if (firstcircule) {
  85. avagetemp/=(ptr+1);
  86. } else {
  87. avagetemp/=TempArraySize;
  88. }
  89. ptr++;
  90. if(ptr >= TempArraySize){
  91. ptr = 0;
  92. firstcircule = false;
  93. }
  94. if(gun == 0 )
  95. ptemp_1 = ptr;
  96. else
  97. ptemp_2 = ptr;
  98. if(avagetemp > TEMP_BOUNDARY)
  99. return UNDEFINED_TEMP;
  100. return avagetemp;
  101. }
  102. static uint8_t getMaxConnectTemp(uint8_t headTemp1, uint8_t headTemp2)
  103. {
  104. uint8_t maxTemp = 0;
  105. if (headTemp1 > TEMP_BOUNDARY &&
  106. headTemp2 > TEMP_BOUNDARY) {
  107. return UNDEFINED_TEMP;
  108. }
  109. if (headTemp1 <= TEMP_BOUNDARY) {
  110. maxTemp = headTemp1;
  111. }
  112. if (headTemp2 <= TEMP_BOUNDARY) {
  113. if (headTemp2 > maxTemp) {
  114. maxTemp = headTemp2;
  115. }
  116. }
  117. return maxTemp;
  118. }
  119. static float ReadAdcVolt(uint8_t AdcChannel)
  120. {
  121. //AIN0=CCS GUN Temp 1
  122. //AIN1=CCS GUN Temp 2
  123. //AIN2=CCS_Proximity/2
  124. //AIN3=pilot voltage
  125. int fd = -1;
  126. uint8_t str[64] = {0};
  127. uint8_t AdcValue[8] = {'\0'};
  128. if (AdcChannel > 7) {
  129. return -1;
  130. }
  131. sprintf((char *)str, "/sys/bus/iio/devices/iio\:device0/in_voltage%d_raw", AdcChannel);
  132. fd = open((char *)str, O_RDONLY);
  133. read(fd, AdcValue, 4);
  134. close(fd);
  135. return (1.8 * atoi((char *)&AdcValue[0])) / 4095;
  136. //return (1.8 * atoi((char *)&AdcValue)) / 4095;
  137. }
  138. static void getChillerTemperature(ChillerTemp *chillerTemp)
  139. {
  140. uint8_t i = 0;
  141. float adcVoltage = 0.0;
  142. ChillerTemp *pChillerTemp = (ChillerTemp *)chillerTemp;
  143. for (i = 0; i < 4; i++) {
  144. adcVoltage = 0.0;
  145. adcVoltage = ReadAdcVolt(i);
  146. ShmDcCommonData->TempVolt[i] = adcVoltage;
  147. if ((adcVoltage <= 0.9) && (adcVoltage >= 0.8)) { //0 ~ -40
  148. pChillerTemp->Temp[i] = ((adcVoltage - 0.908) * 500) + 60;
  149. //log_info("1 adcVoltage = %f", (adcVoltage - 0.9) * 500);
  150. } else if ((adcVoltage <= 1.07) && (adcVoltage > 0.9)) {
  151. pChillerTemp->Temp[i] = ((adcVoltage - 0.91) * 705.88) + 60;
  152. //log_info("2 adcVoltage = %f", (adcVoltage - 0.9) * 500);
  153. } else {
  154. pChillerTemp->Temp[i] = UNDEFINED_TEMP;
  155. }
  156. /*CcsConnectorTemp1 = ReadAdcVolt(i);
  157. if ((CcsConnectorTemp1 <= 0.9) && (CcsConnectorTemp1 >= 0.8)) { //0 ~ -40
  158. CcsConnectorTemp1 = (CcsConnectorTemp1 - 0.9) * 500;
  159. } else if ((CcsConnectorTemp1 <= 1.07) && (CcsConnectorTemp1 > 0.9)) {
  160. CcsConnectorTemp1 = (CcsConnectorTemp1 - 0.9) * 705.88;
  161. } else {
  162. CcsConnectorTemp1 = 195; //not available
  163. }
  164. CcsConnectorTemp |= ((unsigned int)(CcsConnectorTemp1 + 60) & 0xFF) << (i * 8); //0x00(-60)~0xFE(194)
  165. */
  166. }
  167. }
  168. static void AddrAssignment(uint8_t *data)
  169. {
  170. uint8_t target_number[8];
  171. uint8_t index = 0x00;
  172. struct ChargingInfoData *pDcChargingInfo = NULL;
  173. memcpy(target_number, data, sizeof(target_number));
  174. index = *(data + 4);
  175. if (pSysConfig->TotalConnectorCount == 1) {
  176. index = 0x01;
  177. }
  178. //if (CheckUniqNumber(index)) {
  179. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(index - 1);
  180. //DS60-120 add
  181. if (pDcChargingInfo->Type == _Type_Chademo) {
  182. log_info("Set EV board info : (Chademo) TargetAddr = %d ", index);
  183. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  184. log_info("Set EV board info : (CCS) TargetAddr = %d ", index);
  185. } else if (pDcChargingInfo->Type == _Type_GB) {
  186. log_info("Set EV board info : (GB) TargetAddr = %d ", index);
  187. }
  188. //log_info("EV board id = %x ", index); //DS60-120 remove
  189. //log_info("target_number[0] = %x ", target_number[0]);
  190. //log_info("target_number[1] = %x ", target_number[1]);
  191. //log_info("target_number[2] = %x ", target_number[2]);
  192. //log_info("target_number[3] = %x ", target_number[3]);
  193. //log_info("target_number[4] = %x ", target_number[4]);
  194. log_info("SetTargetAddr = %d, type = %d ", index, pDcChargingInfo->Type);
  195. SetTargetAddr(target_number, index);
  196. //}
  197. }
  198. void CheckEvConnect(int gunIndex)
  199. {
  200. int isDisconnect = FALSE;
  201. int gunType = _Type_CCS_2;
  202. struct ChargingInfoData* pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  203. struct InfoCodeData* pInfoCode = (struct InfoCodeData*)GetShmInfoCodeData();
  204. struct ChargingInfoData* pDcChargingInfo_0 = (struct ChargingInfoData*)GetDcChargingInfoData(0);
  205. struct ChargingInfoData* pDcChargingInfo_1 = NULL;
  206. if (pSysConfig->TotalConnectorCount == 1)
  207. pDcChargingInfo_1 = (struct ChargingInfoData*)GetDcChargingInfoData(0);
  208. else
  209. pDcChargingInfo_1 = (struct ChargingInfoData*)GetDcChargingInfoData(1);
  210. if (pDcChargingInfo_0->Type == pDcChargingInfo_1->Type) {
  211. isDisconnect = ShmDcCommonData->pGunStatus[0].EVLoseFlag | ShmDcCommonData->pGunStatus[1].EVLoseFlag;
  212. gunType = pDcChargingInfo_0->Type;
  213. } else {
  214. isDisconnect = ShmDcCommonData->pGunStatus[gunIndex].EVLoseFlag;
  215. gunType = pDcChargingInfo->Type;
  216. }
  217. //log_info("ShmDcCommonData->EVDisconnectFlag[%d]:%d", gunIndex, ShmDcCommonData->EVDisconnectFlag[gunIndex]);
  218. if (isDisconnect) {
  219. switch (gunType) {
  220. case _Type_Chademo:
  221. pInfoCode->InfoEvents.bits.ChademoEvCommFail = YES;
  222. break;
  223. case _Type_CCS_2:
  224. pInfoCode->InfoEvents.bits.CcsEvCommFail = YES;
  225. break;
  226. case _Type_GB:
  227. pInfoCode->InfoEvents.bits.GbEvCommFail = YES;
  228. break;
  229. }
  230. } else {
  231. switch (gunType) {
  232. case _Type_Chademo:
  233. pInfoCode->InfoEvents.bits.ChademoEvCommFail = NO;
  234. break;
  235. case _Type_CCS_2:
  236. pInfoCode->InfoEvents.bits.CcsEvCommFail = NO;
  237. break;
  238. case _Type_GB:
  239. pInfoCode->InfoEvents.bits.GbEvCommFail = NO;
  240. break;
  241. }
  242. }
  243. }
  244. void CANReceiver(int fd)
  245. {
  246. pid_t canRecPid;
  247. canRecPid = fork();
  248. if (canRecPid < 0) {
  249. log_error("Create CAN Bus receive task failed");
  250. return;
  251. }
  252. if (canRecPid == 0) {
  253. int isContinue = 1;
  254. int nbytes;
  255. int intCmd;
  256. uint8_t _index = 0;
  257. uint8_t recvID = 0;
  258. uint8_t targetGun = 0x00;
  259. uint8_t gunTypeIndex = 0;
  260. uint8_t ver[16] = {0};
  261. uint8_t printChillerTemp = NO;
  262. uint8_t printConnTemp = NO;
  263. uint8_t chillerTemp[2] = {0, 0};
  264. uint8_t maxChillerTemp = 0;
  265. uint8_t lastChillerTemp[2] = {0};
  266. uint8_t maxConnTemp = 0;
  267. uint8_t lastConnTemp[2] = {0, 0};
  268. struct can_frame frame;
  269. ChillerTemp chiilerTemp = {0};
  270. struct ChargingInfoData *pDcChargingInfo = NULL;
  271. int len = 0;
  272. char _info[1024];
  273. int i;
  274. unsigned char GunStatus[2];
  275. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  276. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  277. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  278. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  279. ShmCHAdeMOData = (struct CHAdeMOData *)GetShmCHAdeMOData();
  280. ShmGBTData = (struct GBTData *)GetShmGBTData();
  281. ShmCcsData = (struct CcsData *)GetShmCcsData();
  282. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  283. ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();
  284. ShmDcCommonData->pGunStatus[0].EVLoseTimer = time((time_t*)NULL);
  285. ShmDcCommonData->pGunStatus[1].EVLoseTimer = time((time_t*)NULL);
  286. //log_info("Module_EvRXComm Child's PID is %d", getpid());
  287. while (isContinue) {
  288. memset(&frame, 0, sizeof(struct can_frame));
  289. for (_index = 0; _index < pSysConfig->TotalConnectorCount; _index++) {
  290. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(_index);
  291. // 檢查是否有收到EV小板訊號
  292. if (pSysInfo->SelfTestSeq == _STEST_COMPLETE) {
  293. if ((time((time_t*)NULL) - ShmDcCommonData->pGunStatus[_index].EVLoseTimer > 10) &&
  294. !ShmDcCommonData->pGunStatus[_index].EVLoseFlag && pSysInfo->SelfTestSeq == _STEST_COMPLETE &&
  295. pDcChargingInfo->SystemStatus != S_UPDATE && !ShmDcCommonData->debugflag) {
  296. ShmDcCommonData->pGunStatus[_index].EVLoseTimer = time((time_t*)NULL);
  297. ShmDcCommonData->pGunStatus[_index].EVLoseFlag = TRUE;
  298. system("/sbin/ip link set can0 down");
  299. sleep(1);
  300. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  301. system("/sbin/ip link set can0 up");
  302. }
  303. } else {
  304. ShmDcCommonData->pGunStatus[targetGun].EVLoseTimer = time((time_t*)NULL);
  305. ShmDcCommonData->pGunStatus[targetGun].EVLoseFlag = FALSE;
  306. }
  307. CheckEvConnect(_index);
  308. }
  309. nbytes = read(fd, &frame, sizeof(struct can_frame));
  310. if (nbytes <= 0) {
  311. usleep(10000);
  312. continue;
  313. }
  314. recvID = 0;
  315. targetGun = 0x00;
  316. intCmd = (int) (frame.can_id & CAN_EFF_MASK);
  317. if (intCmd == ADDRESS_REQ) {
  318. //ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V013S0;
  319. AddrAssignment(frame.data);
  320. continue;
  321. }
  322. intCmd = (int) (frame.can_id & CAN_EFF_MASK & 0xFFFFFF00);
  323. recvID = ((uint8_t) (frame.can_id & 0x000000FF)); // 0x01 or 0x02
  324. if (ShmDcCommonData->showCanPackage) {
  325. len = 0;
  326. len += sprintf(&_info[len], "CAN Dispenser <= EV Rx:\t[0x%X] ", frame.can_id);
  327. for (i = 0; i < nbytes; i++) {
  328. len += sprintf(&_info[len], "%X ", frame.data[i]);
  329. }
  330. len += sprintf(&_info[len], "\n");
  331. printf("%s", _info);
  332. }
  333. for (_index = 0; _index < pSysConfig->TotalConnectorCount; _index++) { // 假設有找到回應的 Index
  334. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(_index);
  335. //if (gun_count == 1 &&
  336. // _chargingData[_index]->Type == _Type_CCS_2 &&
  337. // ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0) {
  338. // target -= 1;
  339. //}
  340. if (pDcChargingInfo->Evboard_id == recvID) {
  341. targetGun = _index;
  342. break;
  343. }
  344. }
  345. if ((targetGun < 0) || (targetGun >= CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY)) {
  346. log_info("EvComm (CANReceiver) : Target index = %x is < 0 or > QUANTITY ", targetGun);
  347. usleep(10000);
  348. continue;
  349. }
  350. //else if (gun_count == 1 && targetGun == 0 && findIndex == 1 &&
  351. // ShmDcCommonData->CcsVersion == _CCS_VERSION_CHECK_TAG_V015S0) {
  352. // // 這樣的條件下~ 也是單槍 CCS 舊版本的狀況 : 因為舊版 CCS 不會 timeout, then send request id
  353. // ShmDcCommonData->CcsVersion = _CCS_VERSION_CHECK_TAG_V013S0;
  354. //}
  355. if (intCmd == 256) {
  356. log_info("EvComm command = 256");
  357. usleep(10000);
  358. continue;
  359. }
  360. // Reset Connect Timer
  361. ShmDcCommonData->pGunStatus[targetGun].EVLoseTimer = time((time_t*)NULL);
  362. ShmDcCommonData->pGunStatus[targetGun].EVLoseFlag = FALSE;
  363. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(targetGun);
  364. gunTypeIndex = pDcChargingInfo->type_index;
  365. switch (intCmd) {
  366. case NOTIFICATION_EV_STATUS:
  367. if (pDcChargingInfo->ConnectorPlugIn != frame.data[0]) {
  368. if (frame.data[0] == PLUG) {
  369. log_info("Conn %d, Plugin. ", targetGun);
  370. pDcChargingInfo->isEVCCIDVerify = false;
  371. #ifdef DD360Audi
  372. if (pSysConfig->isAuthrizeByEVCCID)
  373. pSysInfo->CurGunSelected = targetGun;
  374. #endif
  375. } else if (frame.data[0] == UNPLUG) {
  376. if (pDcChargingInfo->Type != _Type_CCS_2)
  377. log_info("Conn %d, Unplug. ", targetGun);
  378. strcpy( (char *) pDcChargingInfo->EVCCID, "");
  379. } else {
  380. log_info("Conn %d, None Check. (%d) ", targetGun, frame.data[0]);
  381. }
  382. if(pDcChargingInfo->RemoteStartFlag == YES) {
  383. pSysInfo->CurGunSelected = targetGun;
  384. }
  385. }
  386. pDcChargingInfo->PilotVoltage = frame.data[1];
  387. // CCS 小板確認Pilot Voltage != 0
  388. if (pDcChargingInfo->Type == _Type_CCS_2 && pDcChargingInfo->PilotVoltage != 0) {
  389. if (frame.data[0] == UNPLUG && pDcChargingInfo->ConnectorPlugIn != frame.data[0])
  390. log_info("Conn %d, Unplug. ", targetGun);
  391. pDcChargingInfo->ConnectorPlugIn = frame.data[0];
  392. } else {
  393. pDcChargingInfo->ConnectorPlugIn = frame.data[0];
  394. }
  395. //log_info("index = %d, ConnectorPlugIn = %x, data[0] = %x ",
  396. // targetGun,
  397. // pDcChargingInfo->ConnectorPlugIn,
  398. // frame.data[0]);
  399. //log_info("ConnectorPlugIn = %x ", (-120 + frame.data[1]) / 10);
  400. break;
  401. case ACK_EV_FW_VERSION:
  402. memset(ver, 0, sizeof(ver));
  403. if (pDcChargingInfo->Type == _Type_Chademo) {
  404. memcpy(ver, frame.data, frame.can_dlc);
  405. memcpy(ShmCHAdeMOData->evse[gunTypeIndex].version, ver, ARRAY_SIZE(ver));
  406. ShmCHAdeMOData->evse[gunTypeIndex].SelfTest_Comp = PASS;
  407. log_info("chademo ver. : %s", ShmCHAdeMOData->evse[gunTypeIndex].version);
  408. } else if (pDcChargingInfo->Type == _Type_GB) {
  409. memcpy(ver, frame.data, frame.can_dlc);
  410. memcpy(ShmGBTData->evse[gunTypeIndex].version, ver, ARRAY_SIZE(ver));
  411. ShmGBTData->evse[gunTypeIndex].SelfTest_Comp = PASS;
  412. log_info("gbt ver. : %s", ShmGBTData->evse[gunTypeIndex].version);
  413. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  414. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  415. memcpy(ver, frame.data, frame.can_dlc); //DS60-120 add
  416. //for (uint8_t _vCount = 0, _vPoint = 0; _vCount < frame.can_dlc; _vCount++) {//DS60-120 remove
  417. /*if (_vCount % 2 == 0 && _vCount != 0)
  418. {
  419. ver[_vCount + _vPoint] = 0x2E;
  420. _vPoint++;
  421. }*/
  422. //ver[_vCount + _vPoint] = frame.data[_vCount];
  423. //}
  424. memcpy(&ShmCcsData->V2GMessage_DIN70121[gunTypeIndex].version, ver, ARRAY_SIZE(ver));
  425. ShmCcsData->V2GMessage_DIN70121[gunTypeIndex].SelfTest_Comp = PASS;
  426. log_info("CCS FW = %s ", ShmCcsData->V2GMessage_DIN70121[gunTypeIndex].version);
  427. }
  428. }
  429. if (targetGun == 0) {
  430. memset(pSysInfo->Connector1FwRev,
  431. 0,
  432. sizeof(pSysInfo->Connector1FwRev));
  433. memcpy(pSysInfo->Connector1FwRev, ver, ARRAY_SIZE(ver));
  434. } else if (targetGun == 1) {
  435. memset(pSysInfo->Connector2FwRev,
  436. 0,
  437. sizeof(pSysInfo->Connector2FwRev));
  438. memcpy(pSysInfo->Connector2FwRev, ver, ARRAY_SIZE(ver));
  439. }
  440. break;
  441. case ACK_EV_HW_VERSION:
  442. //log_info("Get EV HW = %s ", frame.data);
  443. break;
  444. case ACK_GET_OUTPUT_REQ:
  445. //DS60-120 add
  446. if ((pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EV &&
  447. pDcChargingInfo->SystemStatus <= S_TERMINATING) ||
  448. pDcChargingInfo->SystemStatus == S_ALARM ||
  449. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  450. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)
  451. ) {
  452. if (pDcChargingInfo->EvBatteryStartSoc <= 0 &&
  453. pDcChargingInfo->SystemStatus >= S_PREPARING_FOR_EVSE) {
  454. pDcChargingInfo->EvBatteryStartSoc = frame.data[1];
  455. }
  456. // 進入充電時重新更新電池電量
  457. if (GunStatus[targetGun] != pDcChargingInfo->SystemStatus && pDcChargingInfo->SystemStatus == S_CHARGING) {
  458. log_info("Reset Gun%d SoC:%d", targetGun, frame.data[1]);
  459. pDcChargingInfo->EvBatterySoc = frame.data[1];
  460. GunStatus[targetGun] = pDcChargingInfo->SystemStatus;
  461. }
  462. if (frame.data[1] > pDcChargingInfo->EvBatterySoc || pDcChargingInfo->SystemStatus == S_CHARGING) {
  463. pDcChargingInfo->EvBatterySoc = frame.data[1];
  464. if (frame.data[1] > pDcChargingInfo->EvBatterySoc)
  465. log_info("Gun%d SOC:%d", targetGun,pDcChargingInfo->EvBatterySoc);
  466. }
  467. }
  468. //pDcChargingInfo->EvBatterySoc = frame.data[1]; //DS60-120 remove
  469. //Jerry add set voltage limit
  470. pDcChargingInfo->EvBatterytargetVoltage = (float)((frame.data[3] << 8) + frame.data[2]) / 10;
  471. if (pDcChargingInfo->EvBatterytargetVoltage > (GetMaxChargingVol(targetGun) * 0.1)) {
  472. pDcChargingInfo->EvBatterytargetVoltage = (GetMaxChargingVol(targetGun) * 0.1);
  473. }
  474. //printf("id = %d, EvBatterytargetVoltage = %.2f", targetGun, pDcChargingInfo->EvBatterytargetVoltage);
  475. //Jerry add set currency limit
  476. pDcChargingInfo->EvBatterytargetCurrent = (float)((frame.data[5] << 8) + frame.data[4]) / 10;
  477. if (pDcChargingInfo->EvBatterytargetCurrent > (GetMaxCharginigCur(targetGun) * 0.1)) {
  478. pDcChargingInfo->EvBatterytargetCurrent = (GetMaxCharginigCur(targetGun) * 0.1);
  479. }
  480. //printf("id = %d, EvBatterytargetCurrent = %.2f", targetGun, pDcChargingInfo->EvBatterytargetCurrent);
  481. pDcChargingInfo->RemainChargingDuration = ((short) frame.data[7] << 8) + (short) frame.data[6];
  482. //printf("RemainChargingDuration = %d", pDcChargingInfo->RemainChargingDuration);
  483. if (pDcChargingInfo->Type == _Type_Chademo) {
  484. if (ShmCHAdeMOData->ev[gunTypeIndex].PresentMsgFlowStatus != frame.data[0]) {
  485. log_info("Gun%d CHAdeMO board status = %d ", targetGun, ShmCHAdeMOData->ev[gunTypeIndex].PresentMsgFlowStatus);
  486. ShmCHAdeMOData->ev[gunTypeIndex].PresentMsgFlowStatus = frame.data[0];
  487. ShmDcCommonData->pGunInfo[targetGun].EVStatus = frame.data[0];
  488. }
  489. ShmCHAdeMOData->ev[gunTypeIndex].EvDetection = frame.data[0];
  490. ShmCHAdeMOData->ev[gunTypeIndex].SOC = pDcChargingInfo->EvBatterySoc;
  491. ShmCHAdeMOData->ev[gunTypeIndex].TargetBatteryVoltage = (pDcChargingInfo->EvBatterytargetVoltage * 10);
  492. ShmCHAdeMOData->ev[gunTypeIndex].ChargingCurrentRequest = (pDcChargingInfo->EvBatterytargetCurrent * 10);
  493. } else if (pDcChargingInfo->Type == _Type_GB) {
  494. if (ShmGBTData->ev[gunTypeIndex].PresentMsgFlowStatus != frame.data[0]) {
  495. log_info("Gun%d GB Board status = %d ", targetGun, ShmGBTData->ev[pDcChargingInfo->type_index].PresentMsgFlowStatus);
  496. ShmGBTData->ev[gunTypeIndex].PresentMsgFlowStatus = frame.data[0];
  497. ShmDcCommonData->pGunInfo[targetGun].EVStatus = frame.data[0];
  498. }
  499. ShmGBTData->ev[gunTypeIndex].EvDetection = frame.data[0];
  500. ShmGBTData->ev[gunTypeIndex].SOC = pDcChargingInfo->EvBatterySoc;
  501. ShmGBTData->ev[gunTypeIndex].TargetBatteryVoltage = (pDcChargingInfo->EvBatterytargetVoltage * 10);
  502. ShmGBTData->ev[gunTypeIndex].ChargingCurrentRequest = (pDcChargingInfo->EvBatterytargetCurrent * 10);
  503. } else if (pDcChargingInfo->Type == _Type_CCS_2) {
  504. if (ShmCcsData->V2GMessage_DIN70121[gunTypeIndex].PresentMsgFlowStatus != frame.data[0] && frame.data[0] != 0xFF) {
  505. log_info("Gun%d CCS board status = %d ", targetGun, ShmCcsData->V2GMessage_DIN70121[pDcChargingInfo->type_index].PresentMsgFlowStatus);
  506. ShmCcsData->V2GMessage_DIN70121[gunTypeIndex].PresentMsgFlowStatus = frame.data[0];
  507. ShmDcCommonData->pGunInfo[targetGun].EVStatus = frame.data[0];
  508. }
  509. }
  510. if (pDcChargingInfo->EvBatterytargetVoltage > (EvTargetVolt[targetGun] + 5) ||
  511. pDcChargingInfo->EvBatterytargetVoltage < (EvTargetVolt[targetGun] - 5) ||
  512. pDcChargingInfo->EvBatterytargetCurrent >(EvTargetCur[targetGun] + 2) ||
  513. pDcChargingInfo->EvBatterytargetCurrent < (EvTargetCur[targetGun] - 2)) {
  514. log_info("Gun%d TargetVoltage = %f , TargetCurrent = %f",
  515. targetGun, pDcChargingInfo->EvBatterytargetVoltage, pDcChargingInfo->EvBatterytargetCurrent);
  516. EvTargetVolt[targetGun] = pDcChargingInfo->EvBatterytargetVoltage;
  517. EvTargetCur[targetGun] = pDcChargingInfo->EvBatterytargetCurrent;
  518. }
  519. //log_info("EvBatterytargetVoltage = %f ", pDcChargingInfo->EvBatterytargetVoltage);
  520. //log_info("EvBatterytargetCurrent = %f ", pDcChargingInfo->EvBatterytargetCurrent);
  521. //log_info("BatteryVoltage = %d ",
  522. // ShmCHAdeMOData->ev[_chargingData[target]->type_index].TargetBatteryVoltage);
  523. //log_info("CurrentRequest = %d ",
  524. // ShmCHAdeMOData->ev[_chargingData[target]->type_index].ChargingCurrentRequest);
  525. break;
  526. case ACK_GET_EV_BATTERY_INFO:
  527. //_chargingData[target].EvACorDCcharging = frame.data[0];
  528. //_chargingData[target]->TotalBatteryCap = ((float) frame.data[4] << 8) + (short) frame.data[3];
  529. pDcChargingInfo->EvBatteryMaxVoltage = ((float)(((uint16_t)frame.data[4] << 8) + (uint16_t)frame.data[3])) / 10;
  530. //_chargingData[target]->EvBatteryMaxCurrent = ((float) frame.data[4] << 8) + (short) frame.data[3];
  531. //_chargingData[target].MaxiBatteryCurrent = ((short) frame.data[6] << 8) + (short) frame.data[5];
  532. if (pDcChargingInfo->Type == _Type_Chademo) {
  533. ShmCHAdeMOData->ev[gunTypeIndex].TotalBatteryCapacity = ((uint16_t)frame.data[2] << 8) + (uint16_t)frame.data[1];
  534. ShmCHAdeMOData->ev[gunTypeIndex].MaxiBatteryVoltage = pDcChargingInfo->EvBatteryMaxVoltage;
  535. //log_info("EvBatteryMaxVoltage = %f ", _chargingData[target]->EvBatteryMaxVoltage);
  536. //log_info("TotalBatteryCapacity = %d ", ShmCHAdeMOData->ev[_chargingData[target]->type_index].TotalBatteryCapacity);
  537. //log_info("MaxiBatteryVoltage = %d ", ShmCHAdeMOData->ev[_chargingData[target]->type_index].MaxiBatteryVoltage);
  538. } else if (pDcChargingInfo->Type == _Type_GB) {
  539. ShmGBTData->ev[gunTypeIndex].TotalBatteryCapacity = ((uint16_t)frame.data[2] << 8) + (uint16_t)frame.data[1];
  540. ShmGBTData->ev[gunTypeIndex].MaxiBatteryVoltage = pDcChargingInfo->EvBatteryMaxVoltage;
  541. }
  542. //else if (pDcChargingInfo->Type == _Type_CCS_2) {
  543. //}
  544. break;
  545. case ACK_GET_MISCELLANEOUS_INFO:
  546. pDcChargingInfo->GunLocked = frame.data[0];
  547. pDcChargingInfo->PilotVoltage = ((float)(-120 + frame.data[3])) / 10;
  548. if (pDcChargingInfo->Type == _Type_Chademo) {
  549. ShmCHAdeMOData->evse[gunTypeIndex].ConnectorTemperatureP = frame.data[1];
  550. ShmCHAdeMOData->evse[gunTypeIndex].ConnectorTemperatureN = frame.data[2];
  551. ShmCHAdeMOData->evse[gunTypeIndex].EvboardStatus = frame.data[7];
  552. } else if (pDcChargingInfo->Type == _Type_GB) {
  553. ShmGBTData->evse[gunTypeIndex].ConnectorTemperatureP = frame.data[1];
  554. ShmGBTData->evse[gunTypeIndex].ConnectorTemperatureN = frame.data[2];
  555. ShmGBTData->evse[gunTypeIndex].EvboardStatus = frame.data[7];
  556. }
  557. /*else if (pDcChargingInfo->Type == _Type_CCS_2) {
  558. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  559. //ShmCcsData->V2GMessage_DIN70121[gunTypeIndex]. .ConnectorTemperatureP = frame.data[1];
  560. //ShmCcsData->V2GMessage_DIN70121[gunTypeIndex]. .ConnectorTemperatureN = frame.data[2];
  561. }
  562. }*/
  563. ShmDcCommonData->ConnectorTemp[gunTypeIndex][0] = frame.data[1];
  564. ShmDcCommonData->ConnectorTemp[gunTypeIndex][1] = frame.data[2];
  565. if (ShmDcCommonData->TestTemperature == YES) { //ReadCmdline test
  566. break;
  567. }
  568. printChillerTemp = NO;
  569. printConnTemp = NO;
  570. if (((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x80) >> 7) == YES) {
  571. getChillerTemperature(&chiilerTemp);
  572. memcpy((char *)ShmDcCommonData->SystemTemp, (char *)chiilerTemp.Temp, sizeof(ChillerTemp));
  573. chillerTemp[0] = getMaxConnectTemp(chiilerTemp.Temp[0], chiilerTemp.Temp[1]);
  574. chillerTemp[1] = getMaxConnectTemp(chiilerTemp.Temp[2], chiilerTemp.Temp[3]);
  575. if ((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x7F) == 1) {
  576. // 單Chiller
  577. maxChillerTemp = getMaxConnectTemp(chillerTemp[0], chillerTemp[1]);
  578. } else {
  579. // 雙Chiller
  580. if (gunTypeIndex == 0) {
  581. maxChillerTemp = chillerTemp[0];
  582. } else {
  583. maxChillerTemp = chillerTemp[1];
  584. }
  585. }
  586. //if ((maxChillerTemp - 3) >= pDcChargingInfo->ChillerTemp) {
  587. // printChillerTemp = YES;
  588. //}
  589. if(maxChillerTemp > (lastChillerTemp[gunTypeIndex] + 2) || maxChillerTemp < (lastChillerTemp[gunTypeIndex] - 2))
  590. {
  591. lastChillerTemp[gunTypeIndex] = maxChillerTemp;
  592. printChillerTemp = YES;
  593. }
  594. pDcChargingInfo->ChillerTemp = maxChillerTemp;
  595. }
  596. if (ShmDcCommonData->ConnectorTemp[gunTypeIndex][0] != 0 && ShmDcCommonData->ConnectorTemp[gunTypeIndex][1] != 0) {
  597. maxConnTemp = getMaxConnectTemp(frame.data[1], frame.data[2]);
  598. //if ((maxConnTemp - 3) >= pDcChargingInfo->ConnectorTemp) {
  599. // printConnTemp = YES;
  600. //}
  601. maxConnTemp = getAvageTemp(maxConnTemp, targetGun);
  602. if (maxConnTemp > (lastConnTemp[targetGun] + 2) || maxConnTemp < (lastConnTemp[targetGun] - 2))
  603. {
  604. lastConnTemp[targetGun] = maxConnTemp;
  605. printConnTemp = YES;
  606. }
  607. pDcChargingInfo->ConnectorTemp = maxConnTemp;
  608. } else {
  609. //log_info("Connector%d Tmep is zero:[%d,%d]", gunTypeIndex, ShmDcCommonData->ConnectorTemp[gunTypeIndex][0],
  610. // ShmDcCommonData->ConnectorTemp[gunTypeIndex][1]);
  611. }
  612. //紀錄槍頭和水冷機溫度, 在系統狀態變化或溫度大於150
  613. if ((ShmDcCommonData->SystemModeChange[targetGun] == YES) ||
  614. (printConnTemp == YES) ||
  615. (printChillerTemp == YES) //&&
  616. //(((pDcChargingInfo->ConnectorTemp >= GUN_OTP_VALUE) &&
  617. // (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP)) ||
  618. // ((pDcChargingInfo->ChillerTemp >= GUN_OTP_VALUE) &&
  619. // (pDcChargingInfo->ChillerTemp != UNDEFINED_TEMP)))
  620. ) {
  621. ShmDcCommonData->SystemModeChange[targetGun] = NO;
  622. log_info("Conn %d max temp = %d, chiller = [%d %d], chiller2 = [%d %d]",
  623. targetGun,
  624. pDcChargingInfo->ConnectorTemp,
  625. chiilerTemp.Temp[0],
  626. chiilerTemp.Temp[1],
  627. chiilerTemp.Temp[2],
  628. chiilerTemp.Temp[3]);
  629. }
  630. if (((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x80) >> 7) == YES) {
  631. //沒有水冷槍
  632. break;
  633. }
  634. if ((ShmDcCommonData->ChillerValve.MultiChillerGun & 0x7F) == 1) {
  635. //單一水冷槍,不需要切換水冷機油閥
  636. //ShmFanModuleData-> ? = YES; //尚未定義
  637. break;
  638. }
  639. //紀錄槍頭溫度
  640. if (pDcChargingInfo->ConnectorTemp != UNDEFINED_TEMP) {
  641. if (targetGun == 0) {
  642. ShmDcCommonData->ChillerValve.LeftTemp = pDcChargingInfo->ConnectorTemp;
  643. } else if (targetGun == 1) {
  644. ShmDcCommonData->ChillerValve.RightTemp = pDcChargingInfo->ConnectorTemp;
  645. }
  646. }
  647. //有兩把水冷槍,判斷兩把槍頭溫度,將水冷機節流閥導向溫度高的那一把槍
  648. if (ShmDcCommonData->ChillerValve.LeftTemp > ShmDcCommonData->ChillerValve.RightTemp) {
  649. //ShmFanModuleData->? = YES; //尚未定義
  650. } else {
  651. //ShmFanModuleData->? = NO; //尚未定義
  652. }
  653. //log_info("EvboardStatus = %x ",
  654. // ShmCHAdeMOData->evse[gunTypeIndex].EvboardStatus);
  655. //log_info("ConnectorPlug locked = %x ",
  656. // frame.data[0]);
  657. //log_info("PilotVoltage = %x ", (-120 + frame.data[3]) / 10);
  658. break;
  659. case ACK_EVSE_ISOLATION_STATUS:
  660. break;
  661. case ACK_EVSE_PRECHAGE_INFO:
  662. pDcChargingInfo->PrechargeStatus = frame.data[0];
  663. break;
  664. case NOTIFICATION_EV_STOP:
  665. // 車端要求停止
  666. if ((pDcChargingInfo->SystemStatus >= S_REASSIGN_CHECK && //DS60-120 add
  667. pDcChargingInfo->SystemStatus <= S_TERMINATING) ||
  668. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  669. pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  670. // frame.data[0] : 0x01 => normal stop, 0x02 => ev emergency stop
  671. /*
  672. log_info("----------------------------- Gun%d NOTIFICATION_EV_STOP err level = %d -----------------------------",
  673. targetGun,
  674. frame.data[0]);
  675. */
  676. if (frame.data[0] == 0x02) {
  677. if (AbnormalStopAnalysis(targetGun, frame.data + 1) == true) {
  678. pDcChargingInfo->StopChargeFlag = YES;
  679. } else {
  680. pDcChargingInfo->NormalStopChargeFlag = YES;
  681. }
  682. } else {
  683. AbnormalStopAnalysis(targetGun, frame.data + 1);
  684. pDcChargingInfo->NormalStopChargeFlag = YES;
  685. }
  686. }
  687. break;
  688. case ACK_EVCCID_REQ:
  689. if (frame.can_dlc > 0 && strcmp ( (char *)pDcChargingInfo->EVCCID, "" ) == EQUAL &&
  690. pDcChargingInfo->Type == _Type_CCS_2)
  691. {
  692. {
  693. memset (
  694. ShmCcsData->V2GMessage_DIN70121 [pDcChargingInfo->type_index].SessionSetupRequest.EVCCID,
  695. 0,
  696. sizeof(ShmCcsData->V2GMessage_DIN70121 [pDcChargingInfo->type_index].SessionSetupRequest.EVCCID) );
  697. memcpy (
  698. ShmCcsData->V2GMessage_DIN70121 [pDcChargingInfo->type_index].SessionSetupRequest.EVCCID,
  699. frame.data, frame.can_dlc );
  700. }
  701. sprintf ( (char *) pDcChargingInfo->EVCCID, "%.2x%.2x%.2x%.2x%.2x%.2x", frame.data [0],
  702. frame.data [1], frame.data [2], frame.data [3], frame.data [4], frame.data [5] );
  703. pDcChargingInfo->EVCCID [17] = '\0';
  704. log_info( "Gun %d->EVCCID = %s ", targetGun, pDcChargingInfo->EVCCID );
  705. }
  706. break;
  707. case ACK_DOWNLOAD_FINISH:
  708. log_info("Gun %d image download finish",targetGun);
  709. break;
  710. default:
  711. log_info("EV board = %d, Ack none defined. intCmd = %d ", targetGun, intCmd);
  712. break;
  713. }//switch
  714. usleep(10000);
  715. }//while
  716. }
  717. }