nvcan.cpp 8.2 KB

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