SocketCAN Filter problems : setsockopt

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

SocketCAN Filter problems : setsockopt

Neptis
This post was updated on .
Hi,

I am currently working with SocketCAN, my application is simple, I just want to filter frame from the Can-bus to save information.

I am learning with the help  of <a href="http://">http://svn.berlios.de/wsvn/socketcan/trunk/kernel/2.6/Documentation/networking/can.txt 
I am sending on the CAN bus extended frame (29bits of id) like : 0000F004

By using the code : nbytes = read(s, &frame, sizeof(struct can_frame));

I am able to receive data 8000F004 (8 corresponding to the EFF (extended) flag).

Now, here is my problem, I am trying to add a filter for the extended id : 0x0000F004

I am using the setsockopt function as shown in the previoux link. But I have no reception...

Find at the end of my post the code currently used.

Do you have any ideas of where my problem can come from ?

Update (from my previous message) :  with Standard ID the Filter works correctly...

Thank you for your help

Neptis (a socketCAN beginner !)

CODE

#include "stdio.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>

#include <linux/can.h>
#include <linux/can/raw.h>
#include <string.h>

/* At time of writing, these constants are not defined in the headers */
#ifndef PF_CAN
#define PF_CAN 29
#endif

#ifndef AF_CAN
#define AF_CAN PF_CAN
#endif

/*
* Programme de test
*/

int main(int argc, char** argv) {

  //Create a frame
   struct can_frame frame;
   //Filter
   struct can_filter rfilter[1];
   //Interface
   struct ifreq ifr;
   //Number of te
   int bytes_read;
  /* Create the socket */
   int skt = socket( PF_CAN, SOCK_RAW, CAN_RAW );
   //Sock adresse
   struct sockaddr_can addr;

   /* Locate the interface you wish to use */
   strcpy(ifr.ifr_name, "slc0");
   ioctl(skt, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
                                  * with that device's index */

   /* Select that CAN interface, and bind the socket to it. */
   addr.can_family = AF_CAN;
   addr.can_ifindex = ifr.ifr_ifindex;
   bind( skt, (struct sockaddr*)&addr, sizeof(addr) );

   //Add new filter on can-id 0xF004
   rfilter[0].can_id   = 0x0000F004;
   rfilter[0].can_mask = CAN_EFF_MASK;
   setsockopt(skt, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

  while(1)
  {
    bytes_read = read( skt, &frame, sizeof(frame));
    if(bytes_read<0){printf("reception pb\n");}
    printf("%x\n",frame.can_id);
  }
  return(0);
}
Reply | Threaded
Open this post in threaded view
|

Re: SocketCAN Filter problems : setsockopt

Neptis
After long search, I am still in the same point...

The filtering is working perfectly for Standard frame but are not working for extended frame...

I am pretty sure there is something I miss... I really need your help in this case.

Thank you so much by advance.

Neptis
Reply | Threaded
Open this post in threaded view
|

Re: SocketCAN Filter problems : setsockopt

shirazee@gmail.com
This post has NOT been accepted by the mailing list yet.
In reply to this post by Neptis
After 6 years, I'm not sure you are still having this problem or not but as I searched and find this page, I thought it is a good idea to share my knowledge here with all.

I Extended frame, the first bit is always 1 and as it is extended, you should consider it.
For example, if you want to send 00EA00F9 as can_id, you should change it to 80EA00F9 and then sent. Similarly, when you are getting messages, you should do it in vice verse.