CsuComm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. CsuComm.c
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <linux/termios.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/sockios.h>
  19. #include <linux/socket.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <sys/time.h>
  23. #include <sys/timeb.h>
  24. #include <math.h>//for pow
  25. #include <net/if.h>
  26. #include <linux/can.h>
  27. #include <linux/can/raw.h>
  28. #include <unistd.h>
  29. #include "define.h"
  30. #include "CsuComm.h"
  31. //#define Debug
  32. //Protocol format : Dir
  33. #define SendDirection 0x08000000
  34. #define RecvDirection 0x00000000
  35. //Protocol format : Message ID
  36. #define MsgAddressRequest 0x00000100
  37. #define MsgAddressAssign 0x00000200
  38. #define MsgStatusNotification 0x00000300
  39. #define MsgGetFwVersion 0x00000400
  40. #define MsgGetHwVersion 0x00000500
  41. #define MsgChargingPermission 0x00000600
  42. #define MsgPresentOutputPower 0x00000700
  43. #define MsgPresentOutputCapacity 0x00000800
  44. #define MsgGetOutputRequirement 0x00000900
  45. #define MsgGetEvBatteryInfo 0x00000A00
  46. #define MsgEvStopEvent 0x00000B00
  47. #define MsgEvseStopEvent 0x00000C00
  48. #define MsgGetMiscInfo 0x00000D00
  49. #define MsgDownloadRequest 0x00000E00
  50. #define MsgStartBlockTransfer 0x00000F00
  51. #define MsgDataTransfer 0x00001000
  52. #define MsgDownloadFinish 0x00001100
  53. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  54. struct StatusCodeData *ShmStatusCodeData;
  55. struct CcsData *ShmCcsData;
  56. struct InternalComm *ShmInternalComm;
  57. pid_t CANReceiverPid;
  58. int CanFd;
  59. #ifdef SystemLogMessage
  60. int StoreLogMsg(unsigned char *DataString)
  61. {
  62. unsigned char Buf[256];
  63. time_t CurrentTime;
  64. struct tm *tm;
  65. memset(Buf, 0, sizeof(Buf));
  66. CurrentTime = time(NULL);
  67. tm = localtime(&CurrentTime);
  68. sprintf(Buf, "echo \"%04d.%02d.%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  69. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  70. DataString,
  71. tm->tm_year + 1900, tm->tm_mon + 1);
  72. system(Buf);
  73. #ifdef Debug
  74. printf("%s \n", DataString);
  75. #endif
  76. }
  77. #endif
  78. int DiffTimeb(struct timeb ST, struct timeb ET)
  79. {
  80. //return milli-second
  81. unsigned int StartTime, StopTime;
  82. StartTime = (unsigned int)ST.time;
  83. StopTime = (unsigned int)ET.time;
  84. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  85. }
  86. /**************************************************************************************/
  87. /**************************Init all share memory *********************************/
  88. /**************************************************************************************/
  89. int InitShareMemory()
  90. {
  91. int MeterSMId;
  92. //creat ShmSysConfigAndInfo
  93. if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  94. {
  95. #ifdef SystemLogMessage
  96. StoreLogMsg("[CsuComm]InitShareMemory:shmget ShmSysConfigAndInfo NG");
  97. #endif
  98. return 0;
  99. }
  100. else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  101. {
  102. #ifdef SystemLogMessage
  103. StoreLogMsg("[CsuComm]InitShareMemory:shmat ShmSysConfigAndInfo NG");
  104. #endif
  105. return 0;
  106. }
  107. //creat ShmStatusCodeData
  108. if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  109. {
  110. #ifdef SystemLogMessage
  111. StoreLogMsg("[CsuComm]InitShareMemory:shmget ShmStatusCodeData NG");
  112. #endif
  113. return 0;
  114. }
  115. else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  116. {
  117. #ifdef SystemLogMessage
  118. StoreLogMsg("[CsuComm]InitShareMemory:shmat ShmStatusCodeData NG");
  119. #endif
  120. return 0;
  121. }
  122. //creat ShmCcsData
  123. if((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), 0777)) < 0)
  124. {
  125. #ifdef SystemLogMessage
  126. StoreLogMsg("[CsuComm]InitShareMemory:shmget ShmCcsData NG");
  127. #endif
  128. return 0;
  129. }
  130. else if((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  131. {
  132. #ifdef SystemLogMessage
  133. StoreLogMsg("[CsuComm]InitShareMemory:shmat ShmCcsData NG");
  134. #endif
  135. return 0;
  136. }
  137. //creat ShmInternalComm
  138. if((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
  139. {
  140. #ifdef SystemLogMessage
  141. StoreLogMsg("[CsuComm]InitShareMemory:shmget ShmInternalComm NG");
  142. #endif
  143. return 0;
  144. }
  145. else if((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  146. {
  147. #ifdef SystemLogMessage
  148. StoreLogMsg("[CsuComm]InitShareMemory:shmat ShmInternalComm NG");
  149. #endif
  150. return 0;
  151. }
  152. memset(ShmInternalComm, 0, sizeof(struct InternalComm));
  153. return 1;
  154. }
  155. int InitCanBus()
  156. {
  157. int s0, nbytes;
  158. struct timeval tv;
  159. struct ifreq ifr0;
  160. struct sockaddr_can addr0;
  161. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  162. system("/sbin/ip link set can0 up");
  163. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  164. tv.tv_sec = 0;
  165. tv.tv_usec = 10000;
  166. if(setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  167. {
  168. #ifdef SystemLogMessage
  169. StoreLogMsg("[CsuComm]InitCanBus:Set SO_RCVTIMEO NG");
  170. #endif
  171. }
  172. nbytes = 40960;
  173. if(setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  174. {
  175. #ifdef SystemLogMessage
  176. StoreLogMsg("[CsuComm]InitCanBus:Set SO_RCVBUF NG");
  177. #endif
  178. }
  179. nbytes = 40960;
  180. if(setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  181. {
  182. #ifdef SystemLogMessage
  183. StoreLogMsg("[CsuComm]InitCanBus:Set SO_SNDBUF NG");
  184. #endif
  185. }
  186. strcpy(ifr0.ifr_name, "can0" );
  187. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  188. addr0.can_family = AF_CAN;
  189. addr0.can_ifindex = ifr0.ifr_ifindex;
  190. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  191. return s0;
  192. }
  193. int SendMsg(int Fd, unsigned int MsgId, unsigned char SlaveAddress, unsigned char DataLength, unsigned char *SendData)
  194. {
  195. struct can_frame frame;
  196. struct timeb StartTime, EndTime;
  197. unsigned int tmp = 0;
  198. int nbytes;
  199. memset(&frame, 0, sizeof(struct can_frame));
  200. frame.can_id = 0x80000000 | SendDirection | MsgId | SlaveAddress;
  201. frame.can_dlc = DataLength;
  202. memcpy(frame.data, SendData, DataLength);
  203. nbytes = write(Fd, &frame, sizeof(struct can_frame));
  204. #ifdef Debug
  205. printf("[CsuComm]SendMsg => Send to ID=0x%x, nbytes=0x%x, DataLength=0x%x, Data=0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x\n",
  206. frame.can_id, nbytes, frame.can_dlc,
  207. frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
  208. #endif
  209. return nbytes;
  210. }
  211. int SendFwVersion(int Fd)
  212. {
  213. int nbytes;
  214. unsigned char Buffer[4];
  215. memset(Buffer, 0, sizeof(Buffer));
  216. nbytes = FirmwareVersion;
  217. memcpy(Buffer, &nbytes, sizeof(int));
  218. nbytes = SendMsg(Fd, MsgGetFwVersion, ShmInternalComm->SlaveAddress, sizeof(int), Buffer);
  219. return nbytes;
  220. }
  221. int SendHwVersion(int Fd)
  222. {
  223. int nbytes;
  224. unsigned char Buffer[4];
  225. memset(Buffer, 0, sizeof(Buffer));
  226. nbytes = FirmwareVersion;
  227. memcpy(Buffer, &nbytes, sizeof(int));
  228. nbytes = SendMsg(Fd, MsgGetFwVersion, ShmInternalComm->SlaveAddress, sizeof(int), Buffer);
  229. return nbytes;
  230. }
  231. int SendStatusNotification(int Fd)
  232. {
  233. int nbytes;
  234. unsigned char Buffer[8];
  235. memset(Buffer, 0, sizeof(Buffer));
  236. Buffer[0] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].ConnectorPlugIn;
  237. Buffer[1] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].PilotState;
  238. if(strlen(ShmStatusCodeData->PresentStatusCode[0]) > 0)
  239. {
  240. memcpy(Buffer + 2, ShmStatusCodeData->PresentStatusCode[0], 6);
  241. }
  242. nbytes = SendMsg(Fd, MsgStatusNotification, ShmInternalComm->SlaveAddress, 8, Buffer);
  243. return nbytes;
  244. }
  245. float DIN70121PhyValDecode(struct PhysicalValueType_DIN70121 PhysicalData)
  246. {
  247. short DataValue;
  248. int DataMultiplier;
  249. float Rtn;
  250. DataValue = PhysicalData.Value;
  251. DataMultiplier = PhysicalData.Multiplier;
  252. switch(PhysicalData.Unit)
  253. {
  254. case h_DIN70121:
  255. Rtn = (DataValue * (10 ^ DataMultiplier) * 60 * 60);
  256. return Rtn;
  257. case m_DIN70121:
  258. Rtn = (DataValue * (10 ^ DataMultiplier) * 60);
  259. return Rtn;
  260. case s_DIN70121:
  261. case A_DIN70121:
  262. case V_DIN70121:
  263. Rtn = (DataValue * (10 ^ DataMultiplier));
  264. return Rtn;
  265. case Ah_DIN70121:
  266. Rtn = (DataValue * (10 ^ DataMultiplier));
  267. return Rtn;
  268. case VA_DIN70121:
  269. case W_DIN70121:
  270. Rtn = (DataValue * (10 ^ DataMultiplier) / 1000);
  271. return Rtn; //kW
  272. case Wh_DIN70121:
  273. Rtn = (DataValue * (10 ^ DataMultiplier) / 1000);
  274. return Rtn; //kWh
  275. }
  276. }
  277. float ISO151182014PhyValDecode(struct PhysicalValueType_ISO15118_2014 PhysicalData)
  278. {
  279. short DataValue;
  280. int DataMultiplier;
  281. float Rtn;
  282. DataValue = PhysicalData.Value;
  283. DataMultiplier = PhysicalData.Multiplier;
  284. switch(PhysicalData.Unit)
  285. {
  286. case h_ISO15118_2014:
  287. Rtn = (DataValue * (10 ^ DataMultiplier) * 60 * 60);
  288. return Rtn;
  289. case m_ISO15118_2014:
  290. Rtn = (DataValue * (10 ^ DataMultiplier) * 60);
  291. return Rtn;
  292. case s_ISO15118_2014:
  293. case A_ISO15118_2014:
  294. case V_ISO15118_2014:
  295. Rtn = (DataValue * (10 ^ DataMultiplier));
  296. return Rtn;
  297. case W_ISO15118_2014:
  298. Rtn = (DataValue * (10 ^ DataMultiplier) / 1000);
  299. return Rtn; //kW
  300. case Wh_ISO15118_2014:
  301. Rtn = (DataValue * (10 ^ DataMultiplier) / 1000);
  302. return Rtn; //kWh
  303. }
  304. }
  305. int SendOutputReq(int Fd)
  306. {
  307. int nbytes;
  308. unsigned char Buffer[8];
  309. unsigned short TmpValue;
  310. memset(Buffer, 0, sizeof(Buffer));
  311. if(ShmCcsData->CommProtocol == 1)
  312. {
  313. //DIN70121
  314. Buffer[0] = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  315. Buffer[1] = ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.DC_EVStatus.EVRESSSOC;
  316. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.EVTargetVoltage) * 10;
  317. memcpy(Buffer + 2, &TmpValue, 2);
  318. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.EVTargetCurrent) * 10;
  319. memcpy(Buffer + 4, &TmpValue, 2);
  320. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.RemainingTimeToFullSoC);
  321. memcpy(Buffer + 6, &TmpValue, 2);
  322. }
  323. else if(ShmCcsData->CommProtocol == 2)
  324. {
  325. //ISO15118_2014
  326. Buffer[0] = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  327. Buffer[1] = ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.DC_EVStatus.EVRESSSOC;
  328. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.EVTargetVoltage) * 10;
  329. memcpy(Buffer + 2, &TmpValue, 2);
  330. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.EVTargetCurrent) * 10;
  331. memcpy(Buffer + 4, &TmpValue, 2);
  332. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.RemainingTimeToFullSoC);
  333. memcpy(Buffer + 6, &TmpValue, 2);
  334. }
  335. nbytes = SendMsg(Fd, MsgStatusNotification, ShmInternalComm->SlaveAddress, 8, Buffer);
  336. return nbytes;
  337. }
  338. int SendBatteryInfo(int Fd)
  339. {
  340. int nbytes;
  341. unsigned char Buffer[8];
  342. unsigned short TmpValue;
  343. memset(Buffer, 0, sizeof(Buffer));
  344. if((ShmCcsData->CommProtocol == 2) && (ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.RequestedEnergyTransferMode <= 1))
  345. {
  346. Buffer[0] = 1;//AC
  347. }
  348. else
  349. {
  350. Buffer[0] = 0;//DC
  351. }
  352. if(ShmCcsData->CommProtocol == 1)
  353. {
  354. //DIN70121
  355. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVEnergyCapacity) * 10;
  356. memcpy(Buffer + 1, &TmpValue, 2);
  357. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumVoltageLimit) * 10;
  358. memcpy(Buffer + 3, &TmpValue, 2);
  359. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumCurrentLimit) * 10;
  360. memcpy(Buffer + 5, &TmpValue, 2);
  361. }
  362. else if(ShmCcsData->CommProtocol == 2)
  363. {
  364. //ISO15118_2014
  365. if(Buffer[0] == 0)
  366. {
  367. //DC
  368. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVEnergyCapacity) * 10;
  369. memcpy(Buffer + 1, &TmpValue, 2);
  370. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumVoltageLimit) * 10;
  371. memcpy(Buffer + 3, &TmpValue, 2);
  372. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumCurrentLimit) * 10;
  373. memcpy(Buffer + 5, &TmpValue, 2);
  374. }
  375. else
  376. {
  377. //AC
  378. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.AC_EVChargeParameter.EAmount) * 10;
  379. memcpy(Buffer + 1, &TmpValue, 2);
  380. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.AC_EVChargeParameter.EVMaxVoltage) * 10;
  381. memcpy(Buffer + 3, &TmpValue, 2);
  382. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.AC_EVChargeParameter.EVMaxCurrent) * 10;
  383. memcpy(Buffer + 5, &TmpValue, 2);
  384. }
  385. }
  386. nbytes = SendMsg(Fd, MsgStatusNotification, ShmInternalComm->SlaveAddress, 7, Buffer);
  387. return nbytes;
  388. }
  389. int SendStopEvent(int Fd)
  390. {
  391. int nbytes;
  392. unsigned char Buffer[8];
  393. memset(Buffer, 0, sizeof(Buffer));
  394. Buffer[0] = 0x01;
  395. if(strlen(ShmStatusCodeData->PresentStatusCode[0]) > 0)
  396. {
  397. memcpy(Buffer + 2, ShmStatusCodeData->PresentStatusCode[0], 6);
  398. }
  399. nbytes = SendMsg(Fd, MsgStatusNotification, ShmInternalComm->SlaveAddress, 7, Buffer);
  400. return nbytes;
  401. }
  402. int SendMiscInfo(int Fd)
  403. {
  404. int nbytes;
  405. unsigned char Buffer[8];
  406. unsigned short TmpValue;
  407. memset(Buffer, 0, sizeof(Buffer));
  408. Buffer[0] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].ConnectorPlugIn;
  409. TmpValue = ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp;
  410. memcpy(Buffer + 1, &TmpValue, 2);
  411. Buffer[3] = (unsigned char)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].PilotVoltage * 10);
  412. Buffer[4] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].PilotState;
  413. nbytes = SendMsg(Fd, MsgStatusNotification, ShmInternalComm->SlaveAddress, 7, Buffer);
  414. return nbytes;
  415. }
  416. /**************************************************************/
  417. /************** Receiving Task*******************************/
  418. /*************************************************************/
  419. void CANReceiver(int fd)
  420. {
  421. pid_t tmp = 0;
  422. struct can_frame frame;
  423. int nbytes;
  424. unsigned char FanspeedGetTime = 0;
  425. if(CANReceiverPid == 0)
  426. {
  427. tmp = fork();
  428. if(tmp > 0)
  429. {
  430. CANReceiverPid = tmp;
  431. {
  432. unsigned char buf[64];
  433. memset(buf, 0, sizeof(buf));
  434. sprintf(buf, "renice -20 -p %d", tmp);
  435. system(buf);
  436. }
  437. return;
  438. }
  439. }
  440. while(1)
  441. {
  442. memset(&frame, 0, sizeof(struct can_frame));
  443. nbytes = read(fd, &frame, sizeof(struct can_frame));
  444. if((frame.can_id == 0) || (frame.can_id & 0x08000000) || (((frame.can_id & 0x000000FF) != ShmInternalComm->SlaveAddress) && ((frame.can_id & 0x000000FF) != 0)))
  445. {
  446. continue;
  447. }
  448. switch(frame.can_id & 0x0000FF00)//Message ID
  449. {
  450. case MsgGetFwVersion:
  451. ShmInternalComm->InternalCommUnion.bits.FwVersion = 1;
  452. break;
  453. case MsgGetHwVersion:
  454. ShmInternalComm->InternalCommUnion.bits.HwVersion = 1;
  455. break;
  456. case MsgChargingPermission:
  457. ShmInternalComm->ChargingPermission = frame.data[0];
  458. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].AvailableChargingPower = (float)((unsigned int)frame.data[2] << 8 | frame.data[1]) / 10;
  459. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].AvailableChargingCurrent = (float)((unsigned int)frame.data[4] << 8 | frame.data[3]) / 10;
  460. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].MaximumChargingVoltage = (float)((unsigned int)frame.data[6] << 8 | frame.data[5]) / 10;
  461. break;
  462. case MsgPresentOutputPower:
  463. if(ShmInternalComm->SlaveAddress == 1)
  464. {
  465. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].PresentChargingVoltage = (float)((unsigned int)frame.data[1] << 8 | frame.data[0]) / 10;
  466. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].PresentChargingCurrent = (float)((unsigned int)frame.data[3] << 8 | frame.data[2]) / 10;
  467. }
  468. else
  469. {
  470. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].PresentChargingVoltage = (float)((unsigned int)frame.data[5] << 8 | frame.data[4]) / 10;
  471. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].PresentChargingCurrent = (float)((unsigned int)frame.data[7] << 8 | frame.data[6]) / 10;
  472. }
  473. break;
  474. case MsgPresentOutputCapacity:
  475. if(ShmInternalComm->SlaveAddress == 1)
  476. {
  477. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].AvailableChargingPower = (float)((unsigned int)frame.data[1] << 8 | frame.data[0]) / 10;
  478. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].AvailableChargingCurrent = (float)((unsigned int)frame.data[3] << 8 | frame.data[2]) / 10;
  479. }
  480. else
  481. {
  482. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].AvailableChargingPower = (float)((unsigned int)frame.data[5] << 8 | frame.data[4]) / 10;
  483. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].AvailableChargingCurrent = (float)((unsigned int)frame.data[7] << 8 | frame.data[6]) / 10;
  484. }
  485. break;
  486. case MsgGetOutputRequirement:
  487. ShmInternalComm->InternalCommUnion.bits.OutputRequirement = 1;
  488. break;
  489. case MsgGetEvBatteryInfo:
  490. ShmInternalComm->InternalCommUnion.bits.EvBatteryInfo = 1;
  491. break;
  492. case MsgEvseStopEvent:
  493. break;
  494. case MsgGetMiscInfo:
  495. ShmInternalComm->InternalCommUnion.bits.MiscInfo = 1;
  496. break;
  497. case MsgDownloadRequest:
  498. break;
  499. case MsgStartBlockTransfer:
  500. break;
  501. case MsgDataTransfer:
  502. break;
  503. case MsgDownloadFinish:
  504. break;
  505. }
  506. }
  507. }
  508. /**************************************************************/
  509. /************** main function***********************************/
  510. /*************************************************************/
  511. int main(int argc, char *argv[])
  512. {
  513. int CanFd;
  514. struct can_frame frame;
  515. struct timeb StartTime, EndTime;
  516. unsigned int TmpValue;
  517. unsigned char Buffer[8];
  518. //Initialization
  519. if(InitShareMemory() == 0)
  520. {
  521. #ifdef SystemLogMessage
  522. StoreLogMsg("[CsuComm]main:InitShareMemory NG");
  523. #endif
  524. if(ShmStatusCodeData != NULL)
  525. {
  526. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  527. }
  528. sleep(5);
  529. return 0;
  530. }
  531. CanFd = InitCanBus();
  532. CANReceiverPid = 0;
  533. ShmInternalComm->SlaveAddress = 0xFF;
  534. //Address Request
  535. while(ShmInternalComm->SlaveAddress == 0xFF)
  536. {
  537. ftime(&StartTime);
  538. ftime(&EndTime);
  539. srandom(StartTime.millitm);
  540. TmpValue = random();
  541. #ifdef Debug
  542. printf("[CsuComm]main => TmpValue=%d\n", TmpValue);
  543. #endif
  544. memset(Buffer, 0, sizeof(Buffer));
  545. memcpy(Buffer, &TmpValue, sizeof(int));
  546. SendMsg(CanFd, MsgAddressRequest, ShmInternalComm->SlaveAddress, 4, Buffer);
  547. while(DiffTimeb(StartTime, EndTime) < 100)//resend interval: 100ms
  548. {
  549. unsigned int TmpValue2 = 0;
  550. ftime(&EndTime);
  551. memset(&frame, 0, sizeof(struct can_frame));
  552. read(CanFd, &frame, sizeof(struct can_frame));
  553. if((frame.can_id == 0) || (frame.can_id & 0x08000000) || ((frame.can_id & 0x0000FF00) != MsgAddressAssign) || (frame.can_dlc != 4))
  554. {
  555. continue;
  556. }
  557. memcpy(&TmpValue2, frame.data, sizeof(int));
  558. if(TmpValue2 == TmpValue)
  559. {
  560. ShmInternalComm->SlaveAddress = frame.can_id & 0x000000FF;
  561. #ifdef SystemLogMessage
  562. {
  563. unsigned char Buffer[128];
  564. memset(Buffer, 0, sizeof(Buffer));
  565. sprintf(Buffer, "[CsuComm]main: ShmCcsData->SlaveAddres=%d", ShmInternalComm->SlaveAddress);
  566. StoreLogMsg(Buffer);
  567. }
  568. #endif
  569. #ifdef Debug
  570. printf("[CsuComm]main => ShmCcsData->SlaveAddres=0x%x\n", ShmCcsData->SlaveAddres);
  571. #endif
  572. break;
  573. }
  574. }
  575. }
  576. CANReceiver(CanFd);
  577. //main loop
  578. ftime(&StartTime);
  579. while(1)
  580. {
  581. ftime(&EndTime);
  582. if(DiffTimeb(StartTime, EndTime) >= 1000)
  583. {
  584. SendStatusNotification(CanFd);
  585. ftime(&StartTime);
  586. }
  587. if(ShmInternalComm->InternalCommUnion.bits.FwVersion == 1)
  588. {
  589. SendFwVersion(CanFd);
  590. ShmInternalComm->InternalCommUnion.bits.FwVersion = 0;
  591. }
  592. if(ShmInternalComm->InternalCommUnion.bits.HwVersion == 1)
  593. {
  594. SendHwVersion(CanFd);
  595. ShmInternalComm->InternalCommUnion.bits.HwVersion = 0;
  596. }
  597. if(ShmInternalComm->InternalCommUnion.bits.OutputRequirement == 1)
  598. {
  599. SendOutputReq(CanFd);
  600. ShmInternalComm->InternalCommUnion.bits.OutputRequirement = 0;
  601. }
  602. if(ShmInternalComm->InternalCommUnion.bits.EvBatteryInfo == 1)
  603. {
  604. SendBatteryInfo(CanFd);
  605. ShmInternalComm->InternalCommUnion.bits.EvBatteryInfo = 0;
  606. }
  607. if(ShmInternalComm->InternalCommUnion.bits.MiscInfo == 1)
  608. {
  609. SendMiscInfo(CanFd);
  610. ShmInternalComm->InternalCommUnion.bits.MiscInfo = 0;
  611. }
  612. }
  613. EndProcess:
  614. if(CANReceiverPid > 0)
  615. {
  616. char Buf[32];
  617. memset(Buf, 0, 32);
  618. sprintf(Buf, "kill %d", CANReceiverPid);
  619. system(Buf);
  620. }
  621. close(CanFd);
  622. system("/sbin/ip link set can0 down");
  623. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  624. system("/sbin/ip link set can0 up");
  625. system("/sbin/ip link set can0 down");
  626. system("killall CsuComm");
  627. }