linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Receive CAN messages - New User
@ 2016-06-14 16:49 Tim Felty
  2016-06-14 17:22 ` Tim Felty
  2016-06-14 17:53 ` Oliver Hartkopp
  0 siblings, 2 replies; 3+ messages in thread
From: Tim Felty @ 2016-06-14 16:49 UTC (permalink / raw)
  To: linux-can

Everyone, 

I'm a new user of socketcan and have some questions on using it to receive 
messages. 

I'm trying to write some helper functions for other users to more easily 
send and receive messages on an embedded device. 

The basic flow is to call a function with the requested CAN ID, and ID 
type, and they are returned a socket with the appropriate filters set. 
That socket is then passed to another function with the CAN Frame that 
gets populated.  I'm new to sockets as well so I'm not sure how the 
filters work, or whether it is even possible to do this at all. The code 
is below.  Any comments or suggestions would be great. 

int getNewSocket(void) 
    { 
        int s; 
 
        struct sockaddr_can addr; 
        struct ifreq ifr; 

        if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) 
        { 
            return -1; 
        } 
 
        strcpy(ifr.ifr_name, device.c_str()); 
        ioctl(s, SIOCGIFINDEX, &ifr); 

        addr.can_family  = AF_CAN; 
        addr.can_ifindex = ifr.ifr_ifindex; 

        if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) 
        { 
            return -2; 
        } 
 
        return s; 
    } 

int addReceiver(unsigned int id, int is29bit, int isPGN) 
{ 
        //I have no idea if this is even close 
        unsigned int filterMask;         
        if(is29bit != 0) 
        { 
            filterMask = CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; 
        } 
        else if(isPGN != 0)  //basically just the PGN mask, receive no 
matter the source or priority 
        { 
            filterMask = CAN_EFF_MASK | CAN_RTR_FLAG | 0x00FFFF00U; 
        } 
        else //11bit 
        { 
            filterMask = CAN_EFF_FLAG | CAN_RTR_FLAG | CAN_SFF_MASK; 
        } 
 
        int s = getNewSocket(); 
        if(s < 0) 
        { 
            return false; 
        } 
 
        struct can_filter rfilter; 
        rfilter.can_id = id; 
        rfilter.can_mask = thisReceiver.filterMask; 
        setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, 
sizeof(rfilter)); 
 
        return s; 
    } 


int main() 
{ 
        int s =  addReceiver(0x0A,0, 0); //this is 11bit ID request for 
testing 
        
       struct can_frame frame; 
       int is29Bit = 0; 
       int isPGN = 0; 

       unsigned in canID = 0x0A;   //example ID 
       if(is29Bit != 0)  //if its 29bit then get he correct ID 
       { 
        canID |= canID | CAN_EFF_FLAG; 
       } 
       if(isPGN != 0) //get PGN from CAN ID 
       { 
        canID |= canID | 0x00FFFF00 ; 
       } 
 
       int nbytes; 
       while(1 > 0) 
       {             
              nbytes = read(canRecvObjects[objectID].socketID, &frame, 
sizeof(struct can_frame)); 
                // do something with the dlc 
                // do something with the dat 
               sleep(someTime); 
        } 
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-06-14 17:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-14 16:49 Receive CAN messages - New User Tim Felty
2016-06-14 17:22 ` Tim Felty
2016-06-14 17:53 ` Oliver Hartkopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).