You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "canbus.h"
  2. #define DEFAULT_GAP 200 /* ms */
  3. #define MODE_RANDOM 0
  4. #define MODE_INCREMENT 1
  5. #define MODE_FIX 2
  6. #define LOG( X ) m_pLog->Log( "canBus", this, X )
  7. #define LOG_ERROR( X ) m_pLog->Log( "canBus", this, _LOG_ERROR, X )
  8. canBus::canBus():transportSerial(){
  9. m_pLog = basicLogCollection::getInstance();
  10. m_bConnected = false;
  11. m_bInitComplete = false;
  12. m_bFulDuplexCAN = false;
  13. m_bLoopbackDisabled = true;//false;
  14. m_bExtendedFrame = false;
  15. m_lPollTimeout = 0;
  16. m_sDevice = NULL;
  17. m_iNodeID = 0;
  18. /* bitrate switch implies CAN FD */
  19. m_bBitrateSwitch = false;
  20. if(m_bBitrateSwitch) {
  21. m_bFulDuplexCAN = true;
  22. };
  23. };
  24. canBus::canBus(CSTR Device, int NodeID, int FilterID=0 ):transportSerial(){
  25. m_pLog = basicLogCollection::getInstance();
  26. m_bConnected = false;
  27. m_bInitComplete = false;
  28. setNodeID( NodeID );
  29. m_bFilterID = false;
  30. setFilterID( FilterID );
  31. m_sDevice = (CSTR) malloc( strlen(Device)+1);
  32. strcpy( m_sDevice, Device );
  33. m_bFulDuplexCAN = false;
  34. m_bLoopbackDisabled = false;
  35. m_bExtendedFrame = false;
  36. m_lPollTimeout = 0;
  37. /* bitrate switch implies CAN FD */
  38. m_bBitrateSwitch = false;
  39. if(m_bBitrateSwitch) {
  40. m_bFulDuplexCAN = true;
  41. };
  42. m_bInitComplete = HW_init();
  43. if( m_bInitComplete ) {
  44. #ifdef DEBUG
  45. char MsgBuffer[CLOG];
  46. sprintf( MsgBuffer,"Init %s [0x%X] OK", m_sDevice, m_iNodeID );
  47. LOG( MsgBuffer );
  48. LOG("Starting");
  49. #endif
  50. init();
  51. };
  52. };
  53. canBus::~canBus(){
  54. LOG("Destructor");
  55. if( close( m_iInFID ) == -1) {
  56. switch( errno ) {
  57. case EBADF: LOG_ERROR( "Error Close In[FD is not a valid descriptor]!" ); break;
  58. case EINVAL: LOG_ERROR( "Error Close In[An invalid value was specified in how]!" ); break;
  59. case ENOTCONN: LOG_ERROR( "Error Close In[The specified socket is not connected]!" ); break;
  60. case ENOTSOCK: LOG_ERROR( "Error Close In[FD is a file, not a socket]!" ); break;
  61. default: {
  62. char Buffer[128];
  63. sprintf( Buffer, "Error(%d) Close In[Unexpected]!", errno );
  64. LOG_ERROR( Buffer );
  65. };
  66. };
  67. };
  68. if( close( m_iOutFID ) == -1) {
  69. switch( errno ) {
  70. case EBADF: LOG_ERROR( "Error Close Out[FD is not a valid descriptor]!" ); break;
  71. case EINVAL: LOG_ERROR( "Error Close Out[An invalid value was specified in how]!" ); break;
  72. case ENOTCONN: LOG_ERROR( "Error Close Out[The specified socket is not connected]!" ); break;
  73. case ENOTSOCK: LOG_ERROR( "Error Close Out[FD is a file, not a socket]!" ); break;
  74. default: {
  75. char Buffer[128];
  76. sprintf( Buffer, "Error(%d) Close Out[Unexpected]!", errno );
  77. LOG_ERROR( Buffer );
  78. };
  79. };
  80. };
  81. //waitThreadEnd( true );
  82. LOG("Destructor Done");
  83. };
  84. bool canBus::setFilterID( int ID ){
  85. if( ID > 0 ){
  86. LOG("SET Filter ID");
  87. m_sFilterID.can_id = ID;
  88. m_sFilterID.can_mask = 0x07FF;
  89. m_bFilterID = true;
  90. };
  91. return m_bFilterID;
  92. };
  93. bool canBus::setNodeID( int ID ){
  94. if( ID > 0 ) {
  95. LOG("SET Node ID");
  96. m_iNodeID = ID;
  97. return true;
  98. };
  99. return false;
  100. };
  101. bool canBus::HW_init(){
  102. char mBuffer[CLOG];
  103. if (strlen( m_sDevice ) >= IFNAMSIZ) {
  104. sprintf( mBuffer, "Name of CAN device '%s' is too long!", m_sDevice );
  105. LOG_ERROR( mBuffer );
  106. return false;
  107. }
  108. if(( m_iOutFID = socket( PF_CAN, SOCK_RAW, CAN_RAW )) < 0) {
  109. LOG_ERROR("Error while opening socket(OUT)");
  110. return false;
  111. };
  112. strncpy(ifr.ifr_name, m_sDevice, IFNAMSIZ - 1);
  113. ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  114. ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
  115. if (!ifr.ifr_ifindex) {
  116. LOG_ERROR("if_nametoindex");
  117. return false;
  118. };
  119. if( ioctl( m_iOutFID, SIOCGIFINDEX, &ifr ) < 0) {
  120. LOG_ERROR("SIOCGIFINDEX");
  121. return false;
  122. }
  123. addr.can_family = AF_CAN;
  124. addr.can_ifindex = ifr.ifr_ifindex;
  125. sprintf( mBuffer,"%s at index %d as Output", m_sDevice, ifr.ifr_ifindex);
  126. LOG( mBuffer );
  127. /* disable default receive filter on this RAW socket */
  128. /* This is obsolete as we do not read from the socket at all, but for */
  129. /* this reason we can remove the receive list in the Kernel to save a */
  130. /* little (really a very little!) CPU usage. */
  131. setsockopt( m_iOutFID, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
  132. if( m_bLoopbackDisabled ){
  133. int loopback = 0;
  134. setsockopt( m_iOutFID, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback) );
  135. };
  136. if( m_bFulDuplexCAN ){
  137. int enable_canfd = 1;
  138. /* check if the frame fits into the CAN netdevice */
  139. if( ioctl( m_iOutFID, SIOCGIFMTU, &ifr ) < 0){
  140. LOG_ERROR("SIOCGIFMTU");
  141. return false;
  142. };
  143. if( ifr.ifr_mtu != CANFD_MTU ){
  144. LOG_ERROR("CAN interface is not CAN FD capable - sorry.");
  145. return false;
  146. };
  147. /* interface is ok - try to switch the socket into CAN FD mode */
  148. if( setsockopt( m_iOutFID, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd, sizeof(enable_canfd))){
  149. LOG_ERROR("error when enabling CAN FD support");
  150. return false;
  151. };
  152. } else{
  153. int canfd_on = 1;
  154. /* try to switch the socket into CAN FD mode */
  155. setsockopt( m_iOutFID, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
  156. };
  157. if( bind( m_iOutFID, (struct sockaddr *)&addr, sizeof(addr)) < 0 ){
  158. LOG_ERROR("Error in socket(OUT) bind");
  159. return false;
  160. }
  161. if( m_lPollTimeout ){
  162. m_sFDs.fd = m_iOutFID;
  163. m_sFDs.events = POLLOUT;
  164. }
  165. /*Receiver Part*/
  166. if(( m_iInFID = socket( PF_CAN, SOCK_RAW, CAN_RAW ))<0){
  167. LOG_ERROR("Error while opening socket(IN)");
  168. return false;
  169. };
  170. strncpy(ifr.ifr_name, m_sDevice, IFNAMSIZ - 1);
  171. ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  172. ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
  173. if (!ifr.ifr_ifindex) {
  174. LOG_ERROR("if_nametoindex");
  175. return false;
  176. };
  177. if( ioctl( m_iInFID, SIOCGIFINDEX, &ifr ) < 0) {
  178. LOG_ERROR("SIOCGIFINDEX");
  179. return false;
  180. }
  181. addr.can_family = AF_CAN;
  182. addr.can_ifindex = ifr.ifr_ifindex;
  183. sprintf( mBuffer,"%s at index %d as Input", m_sDevice, ifr.ifr_ifindex);
  184. LOG( mBuffer );
  185. {
  186. /* try to switch the socket into CAN FD mode */
  187. int canfd_on = 1;
  188. setsockopt( m_iInFID, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
  189. }
  190. if( m_bFilterID ){
  191. setsockopt( m_iInFID, SOL_CAN_RAW, CAN_RAW_FILTER, &m_sFilterID, sizeof(struct can_filter));
  192. };
  193. if( bind( m_iInFID, (struct sockaddr *)&addr, sizeof(addr)) < 0 ){
  194. LOG_ERROR("Error in socket(IN) bind");
  195. return false;
  196. }
  197. return true;
  198. };
  199. int canBus::sendBlock( unsigned char *Data, int DataSize ){
  200. struct canfd_frame frame;
  201. int mtu, maxdlen;
  202. LOG("SendBlock");
  203. memset( &frame, 0, sizeof( canfd_frame ));
  204. if( m_bFulDuplexCAN ){
  205. mtu = CANFD_MTU;
  206. maxdlen = CANFD_MAX_DLEN;
  207. if( m_bBitrateSwitch )
  208. frame.flags |= CANFD_BRS;
  209. if( DataSize > CAN_MAX_DLEN ) DataSize = CAN_MAX_DLEN;
  210. /* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
  211. DataSize = can_dlc2len(can_len2dlc(DataSize));
  212. } else {
  213. mtu = CAN_MTU;
  214. maxdlen = CAN_MAX_DLEN;
  215. /* sanitize CAN 2.0 frame length */
  216. if( DataSize > 8 ){
  217. DataSize = 8;
  218. };
  219. };
  220. frame.len = DataSize;
  221. //frame.can_dlc = can_len2dlc( DataSize );
  222. for(int index= 0; index < DataSize; index++) {
  223. frame.data[index] = Data[index];
  224. };
  225. /* set unused payload data to zero like the CAN driver does it on rx */
  226. //if (frame.len < maxdlen) memset(&frame.data[frame.len], 0, maxdlen - frame.len);
  227. frame.can_id = m_iNodeID;
  228. if (m_bExtendedFrame ) {
  229. frame.can_id &= CAN_EFF_MASK;
  230. frame.can_id |= CAN_EFF_FLAG;
  231. } else
  232. frame.can_id &= CAN_SFF_MASK;
  233. //if( rtr_frame && !m_bFulDuplexCAN )
  234. // frame.can_id |= CAN_RTR_FLAG;
  235. int RetVal;
  236. do{
  237. RetVal = write( m_iOutFID, &frame, mtu );
  238. if ( RetVal < 0) {
  239. if( errno != ENOBUFS ){
  240. LOG_ERROR("Error write to Socket");
  241. return -1;
  242. };
  243. /*if( !m_lPollTimeout ){
  244. LOG_ERROR("Error write to Socket");
  245. return -1;
  246. }
  247. if( m_lPollTimeout ){
  248. // wait for the write socket (with timeout)
  249. if( poll( &m_sFDs, 1, m_lPollTimeout ) < 0 ){
  250. LOG_ERROR("Error Poll");
  251. return -1;
  252. };
  253. };*/
  254. } else if( RetVal < mtu ){
  255. LOG_ERROR("write: incomplete CAN frame");
  256. return -1;
  257. };
  258. } while( ( RetVal < 0 ) && (errno == EINTR || errno == EWOULDBLOCK ) );
  259. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  260. char MsgBuffer[CLOG];
  261. sprintf( MsgBuffer, "Sent %d[%d] byte(s) of %d", RetVal, mtu, DataSize );
  262. LOG( MsgBuffer );
  263. #endif
  264. return RetVal;
  265. };
  266. int canBus::recptBlock( unsigned char *Buffer, int *BufferLength ){
  267. struct iovec iov;
  268. struct msghdr msg;
  269. struct cmsghdr *cmsg;
  270. char ctrlmsg[ CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32)) ];
  271. struct can_filter *rfilter;
  272. can_err_mask_t err_mask;
  273. struct canfd_frame frame;
  274. int RetVal, maxdlen;
  275. /* these settings are static and can be held out of the hot path */
  276. iov.iov_base = &frame;
  277. msg.msg_name = &addr;
  278. msg.msg_iov = &iov;
  279. msg.msg_iovlen = 1;
  280. msg.msg_control = &ctrlmsg;
  281. /* these settings may be modified by recvmsg() */
  282. iov.iov_len = sizeof( canfd_frame );
  283. msg.msg_namelen = sizeof( sockaddr_can );
  284. msg.msg_controllen = sizeof(ctrlmsg);
  285. msg.msg_flags = 0;
  286. do{
  287. //LOG("<[|]>");
  288. RetVal = recvmsg( m_iInFID, &msg, MSG_DONTWAIT );//0 );
  289. if( RetVal < 0){
  290. if ( errno == ENETDOWN) {
  291. LOG_ERROR( "interface down" );
  292. return -1;
  293. };
  294. };
  295. if( RetVal > 0 ) {
  296. if( (size_t)RetVal == CAN_MTU )
  297. maxdlen = CAN_MAX_DLEN;
  298. else if ((size_t)RetVal == CANFD_MTU )
  299. maxdlen = CANFD_MAX_DLEN;
  300. else {
  301. LOG_ERROR( "read: incomplete CAN frame");
  302. return -1;
  303. };
  304. }
  305. SLEEP(1);
  306. //LOG("<[-]>");
  307. } while (RetVal < 0 && errno == EINTR );
  308. #ifdef _DEBUG_SERIAL_COMMUNICATION_
  309. if( RetVal > -1 ){
  310. LOG("ReceivedBlock");
  311. char MsgBuffer[CLOG];
  312. if( RetVal > 1 || RetVal == 0 ) {
  313. sprintf( MsgBuffer, "Received %d bytes", RetVal );
  314. } else {
  315. sprintf( MsgBuffer, "Received 1 byte");
  316. };
  317. LOG( MsgBuffer );
  318. };
  319. #endif
  320. if( RetVal > 0) {
  321. *BufferLength = frame.len;
  322. for(int index = 0; index < frame.len; index++ ) {
  323. Buffer[index] = frame.data[index];
  324. };
  325. RetVal = *BufferLength;
  326. return RetVal;
  327. };
  328. *BufferLength = 0;
  329. if( RetVal == -1) return 0;
  330. if( RetVal == 0 ){
  331. errno = 0;
  332. return 0;
  333. };
  334. /* r < -1, should never happen. */
  335. //errno = EIO;
  336. return 0;
  337. };
  338. #undef LOG
  339. #undef LOG_ERROR
  340. /*
  341. int
  342. main(void)
  343. {
  344. int s;
  345. int nbytes;
  346. struct sockaddr_can addr;
  347. struct can_frame frame;
  348. struct ifreq ifr;
  349. const char *ifname = "vcan0";
  350. if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
  351. perror("Error while opening socket");
  352. return -1;
  353. }
  354. strcpy(ifr.ifr_name, ifname);
  355. ioctl(s, SIOCGIFINDEX, &ifr);
  356. addr.can_family = AF_CAN;
  357. addr.can_ifindex = ifr.ifr_ifindex;
  358. printf("%s at index %d\n", ifname, ifr.ifr_ifindex);
  359. if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  360. perror("Error in socket bind");
  361. return -2;
  362. }
  363. frame.can_id = 0x123;
  364. frame.can_dlc = 2;
  365. frame.data[0] = 0x11;
  366. frame.data[1] = 0x22;
  367. nbytes = write(s, &frame, sizeof(struct can_frame));
  368. printf("Wrote %d bytes\n", nbytes);
  369. return 0;
  370. }
  371. */