nvcan.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include "nvcan.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <ctype.h>
  9. #include <libgen.h>
  10. #include <time.h>
  11. #include <errno.h>
  12. #include <sys/time.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/uio.h>
  17. #include <net/if.h>
  18. #include <linux/can.h>
  19. #include <linux/can/raw.h>
  20. #include <QDateTime>
  21. #include <iostream>
  22. /* for hardware timestamps - since Linux 2.6.30 */
  23. #ifndef SO_TIMESTAMPING
  24. #define SO_TIMESTAMPING 37
  25. #endif
  26. /* from #include <linux/net_tstamp.h> - since Linux 2.6.30 */
  27. #define SOF_TIMESTAMPING_SOFTWARE (1<<4)
  28. #define SOF_TIMESTAMPING_RX_SOFTWARE (1<<3)
  29. #define SOF_TIMESTAMPING_RAW_HARDWARE (1<<6)
  30. #define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */
  31. #define MAXIFNAMES 30 /* size of receive name index to omit ioctls */
  32. #define MAXCOL 6 /* number of different colors for colorized output */
  33. #define ANYDEV "any" /* name of interface to receive from any CAN interface */
  34. #define ANL "\r\n" /* newline in ASC mode */
  35. #define SILENT_INI 42 /* detect user setting on commandline */
  36. #define SILENT_OFF 0 /* no silent mode */
  37. #define SILENT_ANI 1 /* silent mode with animation */
  38. #define SILENT_ON 2 /* silent mode (completely silent) */
  39. #include <QTime>
  40. #define BUF_SIZE 1000
  41. std::string CANNAME[] = {"can0","can1"};
  42. nvcan::nvcan()
  43. {
  44. // qDebug("nvcan");
  45. // connect(this,SIGNAL(SIG_CANOPENSTATE(bool,int,const char*)),this,SLOT(onMsg(bool,int,const char*)));
  46. mfault = new iv::Ivfault("can_agx");
  47. mivlog = new iv::Ivlog("can_agx");
  48. mfault->SetFaultState(0,0,"Prepare Initialize.");
  49. }
  50. void nvcan::run()
  51. {
  52. int currmax = 2;
  53. fd_set rdfs;
  54. int s[MAXSOCK];
  55. int ret;
  56. struct sockaddr_can addr;
  57. char ctrlmsg[CMSG_SPACE(sizeof(struct timeval) + 3*sizeof(struct timespec) + sizeof(__u32))];
  58. struct iovec iov;
  59. struct msghdr msg;
  60. struct canfd_frame frame;
  61. int nbytes, i, maxdlen;
  62. struct ifreq ifr;
  63. struct timeval tv, last_tv;
  64. struct timeval timeout_config = { 0, 0 }, *timeout_current = 0;
  65. mfault->SetFaultState(0,0,"Initializing.");
  66. for(i=0;i<currmax;i++)
  67. {
  68. s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  69. if (s[i] < 0) {
  70. mfault->SetFaultState(2,1,"Create Socket Error.");
  71. emit SIG_CANOPENSTATE(false,-1,"Create Socket Error");
  72. return;
  73. }
  74. addr.can_family = AF_CAN;
  75. memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
  76. strncpy(ifr.ifr_name, CANNAME[i].data(), 5);
  77. if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
  78. mfault->SetFaultState(2,2,"SIOCGIFINDEX.");
  79. emit SIG_CANOPENSTATE(false,-2,"SIOCGIFINDEX");
  80. return;
  81. }
  82. addr.can_ifindex = ifr.ifr_ifindex;
  83. if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  84. mfault->SetFaultState(2,3,"bind error.");
  85. emit SIG_CANOPENSTATE(false,-3,"bind error");
  86. return;
  87. }
  88. }
  89. mbCANOpen = true;
  90. mivlog->verbose("open can succesfully.");
  91. mfault->SetFaultState(0,0,"CAN OK.");
  92. emit SIG_CANOPENSTATE(true,0,"open can card successfully");
  93. iov.iov_base = &frame;
  94. msg.msg_name = &addr;
  95. msg.msg_iov = &iov;
  96. msg.msg_iovlen = 1;
  97. msg.msg_control = &ctrlmsg;
  98. qint64 nLastRecv = QDateTime::currentMSecsSinceEpoch();
  99. int nRecvState = 0; // 0 Have Data 1 No Data;
  100. mbRunning = true;
  101. int nrecvcount = 0;
  102. while((!QThread::isInterruptionRequested())&&(mbCANOpen))
  103. {
  104. FD_ZERO(&rdfs);
  105. for (i=0; i<currmax; i++)
  106. FD_SET(s[i], &rdfs);
  107. if (timeout_current)
  108. *timeout_current = timeout_config;
  109. timeout_config.tv_sec= 0;
  110. timeout_config.tv_usec = 100;;
  111. timeout_current = &timeout_config;
  112. ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, timeout_current);
  113. if (ret < 0) {
  114. emit SIG_CANOPENSTATE(false,-4,"select error");
  115. mfault->SetFaultState(2,4,"select error.");
  116. mbCANOpen = false;
  117. continue;
  118. }
  119. for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
  120. if (FD_ISSET(s[i], &rdfs)) {
  121. nLastRecv = QDateTime::currentMSecsSinceEpoch();
  122. /* these settings may be modified by recvmsg() */
  123. iov.iov_len = sizeof(frame);
  124. msg.msg_namelen = sizeof(addr);
  125. msg.msg_controllen = sizeof(ctrlmsg);
  126. msg.msg_flags = 0;
  127. nbytes = recvmsg(s[i], &msg, 0);
  128. if (nbytes < 0) {
  129. // if ((errno == ENETDOWN) && !down_causes_exit) {
  130. if ((errno == ENETDOWN)) {
  131. mivlog->error("%s interface down", CANNAME[i].data());
  132. mfault->SetFaultState(1, 0, "interface down");
  133. emit SIG_CANOPENSTATE(false,-5,"can card down");
  134. fprintf(stderr, "%s: interface down\n", CANNAME[i].data());
  135. return;
  136. }
  137. continue;
  138. // perror("read");
  139. // return 1;
  140. }
  141. if ((size_t)nbytes == CAN_MTU)
  142. maxdlen = CAN_MAX_DLEN;
  143. else if ((size_t)nbytes == CANFD_MTU)
  144. maxdlen = CANFD_MAX_DLEN;
  145. else {
  146. mivlog->warn("read incomplete message");
  147. continue;
  148. }
  149. nrecvcount++;
  150. // qDebug("receive msg.");
  151. mMutex.lock();
  152. basecan_msg msg;
  153. msg.id = frame.can_id&0x1fffffff;
  154. if((frame.can_id&0x80000000)!= 0)msg.isExtern = true;
  155. else msg.isExtern = false;
  156. if((frame.can_id&0x40000000)!= 0)msg.isRemote = true;
  157. else msg.isRemote = false;
  158. msg.nLen = frame.len;
  159. if((frame.len<9)&&(frame.len>0))memcpy(msg.data,frame.data,frame.len);
  160. if(mMsgRecvBuf[i].size()<BUF_SIZE)
  161. {
  162. mMsgRecvBuf[i].push_back(msg);
  163. }
  164. mMutex.unlock();
  165. }
  166. }
  167. if((QDateTime::currentMSecsSinceEpoch() - nLastRecv)> 1000)
  168. {
  169. if(nRecvState == 0)
  170. {
  171. nRecvState = -1;
  172. mfault->SetFaultState(0,1,"More than 1 second not receive data.");
  173. }
  174. }
  175. else
  176. {
  177. if(nRecvState == -1)
  178. {
  179. nRecvState = 0;
  180. mfault->SetFaultState(0,0,"CAN OK.");
  181. }
  182. }
  183. struct canfd_frame framesend[2500];
  184. for(int nch =0;nch<currmax;nch++)
  185. {
  186. int nsend = 0;
  187. mMutex.lock();
  188. for(i=0;i<mMsgSendBuf[nch].size();i++)
  189. {
  190. if(i>=2500)break;
  191. memcpy(framesend[i].data,mMsgSendBuf[nch].at(i).data,8);
  192. framesend[i].can_id = mMsgSendBuf[nch].at(i).id;
  193. if(mMsgSendBuf[nch].at(i).isExtern)
  194. {
  195. framesend[i].can_id = framesend[i].can_id|0x80000000;
  196. }
  197. else
  198. {
  199. framesend[i].can_id = framesend[i].can_id&0x7ff;
  200. }
  201. if(mMsgSendBuf[nch].at(i).isRemote)
  202. {
  203. framesend[i].can_id= framesend[i].can_id|0x40000000;
  204. }
  205. framesend[i].len = mMsgSendBuf[nch].at(i).nLen;
  206. nsend++;
  207. }
  208. mMsgSendBuf[nch].clear();
  209. mMutex.unlock();
  210. if(nsend > 0)
  211. {
  212. for(i=0;i<nsend;i++)
  213. if (write(s[nch], &framesend[i],16) != 16) {
  214. mivlog->error("write error 1");
  215. perror("write error 1.");
  216. continue;
  217. }
  218. }
  219. }
  220. }
  221. for (i=0; i<currmax; i++)
  222. {
  223. close(s[i]);
  224. }
  225. qDebug("nvcan thread close.");
  226. mbRunning = false;
  227. }
  228. void nvcan::startdev()
  229. {
  230. start();
  231. }
  232. void nvcan::stopdev()
  233. {
  234. requestInterruption();
  235. QTime xTime;
  236. xTime.start();
  237. while(xTime.elapsed()<100)
  238. {
  239. if(mbRunning == false)
  240. {
  241. mfault->SetFaultState(1, 0, "can closed");
  242. mivlog->error("can is closed at %d",xTime.elapsed());
  243. qDebug("can is closed.");
  244. break;
  245. }
  246. }
  247. }
  248. int nvcan::GetMessage(const int nch,basecan_msg *pMsg, const int nCap)
  249. {
  250. if((nch>1)||(nch < 0))return -1;
  251. if(mMsgRecvBuf[nch].size() == 0)return 0;
  252. int nRtn;
  253. nRtn = nCap;
  254. mMutex.lock();
  255. if(nRtn > mMsgRecvBuf[nch].size())nRtn = mMsgRecvBuf[nch].size();
  256. int i;
  257. for(i=0;i<nRtn;i++)
  258. {
  259. memcpy(&pMsg[i],&(mMsgRecvBuf[nch].at(i)),sizeof(basecan_msg));
  260. }
  261. std::vector<basecan_msg>::iterator iter;
  262. iter = mMsgRecvBuf[nch].begin();
  263. for(i=0;i<nRtn;i++)
  264. {
  265. iter = mMsgRecvBuf[nch].erase(iter);
  266. }
  267. mMutex.unlock();
  268. return nRtn;
  269. }
  270. int nvcan::SetMessage(const int nch, basecan_msg *pMsg)
  271. {
  272. if((nch>1)||(nch < 0))return -1;
  273. if(mMsgSendBuf[nch].size() > BUF_SIZE)return -2;
  274. mMutex.lock();
  275. mMsgSendBuf[nch].push_back(*pMsg);
  276. mMutex.unlock();
  277. return 0;
  278. }
  279. void nvcan::onMsg(bool bCAN, int nR, const char *strres)
  280. {
  281. mivlog->verbose("msg is %s ",strres);
  282. }