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.

425 lines
11 KiB

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