Giter VIP home page Giter VIP logo

can-tests's People

Contributors

ajneu avatar darbedar avatar grandwolf avatar hartkopp avatar marckleinebudde avatar olerem avatar rshanmu avatar snewz avatar thuermann avatar yegorich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

can-tests's Issues

socketCAN read() segmentation faults

Hi there,
Apologies if this is the right platform the issue on Linux CANSocket.

I have been experienced the segmentation faults on second frame read(). Yes, always at second frame. However, they are any frame as far as the program is concerned. I have tried to modified the code by introducing struct timeval, the timeout values. As soon as I do that, the read() throw -1 when no frames in the system buffer, or it supposes to wait till a frame is available.

here is how I open the CAN Socket. I have added many tests code to check the sanity of the CAN Socket. However, the segmentation remains. Very appreciate your comments.

m_interface = can_interface;
        m_socket_mode = mode;
        m_read_timeout_ms = read_timeout_ms;

        char buf[100];


        /* open socket */
        if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
        {
            perror("socketcan ");
            return STATUS_SOCKET_CREATE_ERROR;
        }


        int mtu, enable_canfd = 1; //Lim for time being not supporting CAN_FD

        struct ifreq ifr;

        strncpy(ifr.ifr_name, can_interface.c_str(), IFNAMSIZ - 1);
        ifr.ifr_name[IFNAMSIZ - 1] = '\0';


/*
 * retrieve index of the interface name
 */
        ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
        /*
         * index of the interface name
         */
        if (!ifr.ifr_ifindex) {
            perror("if_nametoindex");
            printf("Interface name %s",ifr.ifr_name);
            return STATUS_INTERFACE_NAME_TO_IDX_ERROR;
        }

        /* Check if it is required to set as FD CAN */
        if (mode == MODE_CANFD_MTU)
        {
        	/*
        	 * retrieve the interface index for the interface name (can0, can1, vcan0 etc) we wish to use.
        	 */

        	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
        		perror("SIOCGIFINDEX");
        		return STATUS_CAN_OP_ERROR;
        	}

        	 mtu = ifr.ifr_mtu;

        	if (mtu != CANFD_MTU) {

        		printf("CAN Classic  mode");
        		return STATUS_CANFD_NOT_SUPPORTED;
        	}

        	printf("CD CAN mode");
        	/* By default it is CAN classic.
        	 *interface is ok - try to switch the socket into CAN FD mode
        	 */

        	if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
        	           &enable_canfd, sizeof(enable_canfd)))
        	{

        	        return STATUS_ENABLE_FD_SUPPORT_ERROR;
        	}


        }

        	/*
        		const int timestamping_flags = (SOF_TIMESTAMPING_SOFTWARE | \
            		SOF_TIMESTAMPING_RX_SOFTWARE | \
            		SOF_TIMESTAMPING_RAW_HARDWARE);

        		if (setsockopt(m_socket, SOL_SOCKET, SO_TIMESTAMPING,
            		&timestamping_flags, sizeof(timestamping_flags)) < 0) {
            		perror("setsockopt SO_TIMESTAMPING is not supported by your Linux kernel");
        		}
        	 */

        /*
         * The owner - disable default receive filter on this RAW socket
         * This is obsolete as we do not read from the socket at all, but for
         * this reason we can remove the receive list in the Kernel to save a
         * little (really a very little!) CPU usage.
         *
         * */

        /*
         #define CAN_SFF_MASK 0x000007FFU / * Standard frame format (SFF) * /
 	 	 #define CAN_EFF_MASK 0x1FFFFFFFU / * Extended frame format (EFF) * /
 	 	 #define CAN_ERR_MASK 0x1FFFFFFFU / * Ignore EFF, RTR, ERR flags * /
         */

        /* 12 Nov 20 Lim - Re-open the default receive filter on this RAW socket */
        //setsockopt(m_socket, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);  // Disable frame reception
        /*
         * A filter matches, when  <received_can_id> & mask == can_id & mask
         */


#if 0
        struct can_filter rfilter[2];
        rfilter[0].can_id   = 0x128;
        rfilter[0].can_mask = CAN_SFF_MASK;
        rfilter[1].can_id   = 0x120; 	//x120-x123
        rfilter[1].can_mask = 0x1FC;  	//001-1111-1100 eg, 0x120-0x123
#else
        /* filter incoming frames */
        struct can_filter rfilter[1];
        rfilter[0].can_id   = CAN_SFF_MASK;
        rfilter[0].can_mask = 0x123;   	// can_mask = 0, all frames pass through
#endif
        setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

#if 0
        struct timeval timeout;

        timeout.tv_sec = 0;
        timeout.tv_usec = 1000; //m_read_timeout_ms *1000;

       // timeout.tv_usec = m_read_timeout_ms * 10;  // Not init'ing this can cause strange errors
       // if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO|SO_SNDTIMEO, (const char*)&timeout,sizeof(struct timeval)) < 0) {
        if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout,sizeof(timeout)) < 0) {
        	perror("Setting timeout failed");
        	return STATUS_SOCKET_CREATE_ERROR;
        }
        printf("\nSocketCAN init Timeout %d succeeded\n",timeout.tv_usec);
#endif

// LINUX
        struct sockaddr_can addr;

        memset(&addr, 0, sizeof(addr));
        addr.can_family = AF_CAN;
        addr.can_ifindex = ifr.ifr_ifindex;


        if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
            perror("Socket CAN2 bind");
            return STATUS_BIND_ERROR;
        }

        printf("SocketCAN  bind() succeeded\n");

        int error = 0;
        socklen_t len = sizeof (error);
        int retval = getsockopt (s, SOL_SOCKET, SO_ERROR, &error, &len);
        if (retval != 0) {
            /* there was a problem getting the error code */
            printf("Error getting socket error code: %s\n", strerror(retval));
            perror("Setting timeout failed");
            return STATUS_SOCKET_CREATE_ERROR;
        }
        printf("SocketCAN  getsockopt() succeeded\n");

        if (error != 0) {
           /* socket has a non zero error status */
        	printf("Socket error: %s\n", strerror(error));
            return STATUS_SOCKET_CREATE_ERROR;
        }
        printf("SocketCAN  error none\n");
        sprintf(buf,"%s \n", ifr.ifr_name);
        printf("SocketCAN for interface %s succeeded\n",buf);

        return STATUS_OK;

tst-raw-sendto

Hi All,
I am calling "tst-bcm-rx-sendto.c" from C code, it works fine but it runs in shell program . I have to use "ctr-c" to cancel it. Is there any file, which can call from C programe as intruppt to read can.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.