CsuComm.c 21 KB

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