* 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
* Re: Receive CAN messages - New User
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
1 sibling, 0 replies; 3+ messages in thread
From: Tim Felty @ 2016-06-14 17:22 UTC (permalink / raw)
Cc: linux-can
Sorry,
I was converting to C from C++ and made some transcription errors.
In main:
It should be nbytes = read(s, &frame, sizeof(struct can_frame));
and sleep(someTime) is just an example for waiting.
In addReceiver: return false should be return 0;
linux-can-owner@vger.kernel.org wrote on 06/14/2016 11:49:34 AM:
> ______________________________________________________________________
> 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);
> }
> }
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-can" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at https://urldefense.proofpoint.com/v2/url?
>
u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=CwIBAg&c=p0oa49nxxGtbbM2qgM-
> GB4r4m9OlGg-
>
sEp8sXylY2aQ&r=VuANltNjLNwJQmGUza63viDz3uCLJcIq5CxaNaRbWn0&m=dgDHT6InvAmo7oAJLN0Drk_Z5o16wYmhMD9bHRBuKJA&s=l0dWQXRf-
> hoFgMRFBlC1b-6EQbabDYC_aUyAnH5UcLo&e=
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Receive CAN messages - New User
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
1 sibling, 0 replies; 3+ messages in thread
From: Oliver Hartkopp @ 2016-06-14 17:53 UTC (permalink / raw)
To: Tim Felty, linux-can
On 06/14/2016 06:49 PM, Tim Felty wrote:
> 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;
CAN_EFF_FLAG ??
Please check whether setting the flags and masks in rfilter.can_id or
rfilter.can_mask enables the filter you really want.
You might want to play with
candump can0,<can_id>:<can_mask>
to check the filters in their functionality.
In
cat /proc/net/can/rcvlist_*
you can see the enabled filters.
Btw. as you seem to use PGNs the j1939 implementation from Kurt van
Dijck might be worth looking at.
Regards,
Oliver
> }
> 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);
> }
> }
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-can" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ 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).