#include #include #include #include #include #include #include #include #include #include #include #include #include "JMmqtt.h" #include "JMremote.h" #include "JMcan.h" extern u8 carGear; static struct can_frame canPack={0,8,0,0,0,{0}}; static int canSocket=0; u32 canRecvPackIDs[CANRECVPACKCOUNT]={0x1f0,0x1f1,0x1f2,0x1f4,0x1f5,0x1f6,0x2f2,0x3f0,0x3f2,0x4f0,0x4f2,0x4f4,0x5f4,0x6f4,0x70a,0x18FEE500,0x18FEEE00,0x18FEEF00,0x0CF00300,0x0CF00400,0x0CFF2403,0x0CFF2303}; u32 canPackIDs[CANPACKCOUNT]={0x186,0x286,0x386,0x486}; u8 defaultCan[CANBUFSIZE]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff, 0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; u8 canSendBuf[CANBUFSIZE]={0}; u8 *manualSendBuf=defaultCan; u8 *sendBuf=NULL; POSTDATABUF_S postData={0}; u8 can306[8]={0}; int CAN_Open(char *name) { struct ifreq ifr; struct sockaddr_can addr; pxLog(INFO,"[CAN] CAN ready to open %s",name); if(canSocket>0) { close(canSocket); canSocket=0; } /* open socket */ if ((canSocket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { pxLog(ERROR,"[CAN] CAN create failed:%d,exit.",canSocket); return -1; } strncpy(ifr.ifr_name, name, IFNAMSIZ - 1); ifr.ifr_name[IFNAMSIZ - 1] = '\0'; ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name); if (!ifr.ifr_ifindex) { pxLog(ERROR,"[CAN] CAN if_nametoindex failed,exit."); close(canSocket); canSocket=0; return -1; } memset(&addr, 0, sizeof(addr)); addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; struct can_filter *rfilter= malloc(sizeof(struct can_filter) * (CANRECVPACKCOUNT+1)); if (!rfilter) { pxLog(ERROR,"[CAN] CAN set filter failed,exit."); close(canSocket); canSocket=0; return -1; } for(int x=0;xtm_mday || logfp==NULL) { dateLast=dateNow->tm_mday; if(logfp) { timeOrig=get_timeStamp(); fwrite(&timeOrig,8,1,logfp); fclose(logfp); } char fileName[64]={0}; sprintf(fileName,DATALOGPATH"%02d-%02d-%02d",dateNow->tm_year+1900,dateNow->tm_mon+1,dateNow->tm_mday); logfp = fopen(fileName, "a+"); if(logfp==NULL) return; fcntl(fileno(logfp), F_SETFD, FD_CLOEXEC); timeOrig=get_timeStamp(); static u8 startSign[4]={0xfc,0xfd,0xfe,0xff}; fwrite(startSign,4,1,logfp); fwrite(&timeOrig,8,1,logfp); memcpy(logBuf,&postData.status,1); u8 wheel=(s8)(*(double*)(&postData.wheelSet)); logBuf[1]=wheel; memcpy(logBuf+2,sendBuf,CANBUFSIZE); fwrite(logBuf,CANBUFSIZE+2,1,logfp); fflush(logfp); return; } u8 logTmp[CANBUFSIZE+7]={0}; int writeSize=5; if(memcmpset(&postData.status,logBuf,logTmp+writeSize,1)) { logTmp[4]|=1; writeSize++; } u8 wheel=(u8)postData.wheelSet; if(memcmpset(&wheel,logBuf+1,logTmp+writeSize,1)) { logTmp[4]|=2; writeSize++; } for(int x=0;x= 10) { logCount=0; canDataLog(); } for(int x=0;x=10) { recvTime=0; recvCount=CAN_dataRecv(); if(recvCount==-1) { sleep(1); break; } if(recvCount==0) { if(recvEmptyCount++>3) { //can接收超时 postData.status.canTimeout=1; } } else { recvEmptyCount=0; postData.status.canTimeout=0; } } usleep(9000); } } }