简体   繁体   English

我可以在bind()之后更改canbus套接字上的掩码吗?

[英]Can I change the mask on a canbus socket after bind()?

I saw a similar question but it didn't answer mine.我看到了一个类似的问题,但它没有回答我的问题。

I'm using canbus communication in my program with a mask , here is an example, taken from can-utils :我在我的程序中使用带有maskcanbus通信,这是一个来自can-utils的示例:

struct can_filter {
    canid_t can_id;
    canid_t can_mask;
};

struct can_filter *rfilter;
setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER, rfilter, numfilter * sizeof(struct can_filter));

/* try to switch the socket into CAN FD mode */
setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));

bind(s[i], (struct sockaddr *) &addr, sizeof(addr))

. . . . . .

int ret = select(s[currmax - 1] + 1, &rdfs, NULL, NULL, timeout);
int nbytes = recvmsg(s[i], &msg, 0);

In this example, I set the mask and than bind so every time I will receive a message from the socket , it will be with the same mask .在这个例子中,我设置了mask ,然后bind ,所以每次我会receive来自socket的消息时,它都会使用相同的mask

Is it possible to change the mask after the bind and before receive ?是否可以在bind之后和receive之前更改mask

Example:例子:

/* try to switch the socket into CAN FD mode */
setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));

bind(s[i], (struct sockaddr *) &addr, sizeof(addr))

. . . . . .

setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER, rfilter, numfilter * sizeof(struct can_filter));
int ret = select(s[currmax - 1] + 1, &rdfs, NULL, NULL, timeout);
int nbytes = recvmsg(s[i], &msg, 0);

Thanks @RamyLebeau for the SocketCAN referral .感谢@RamyLebeau 的SocketCAN 推荐

From the documentation:从文档中:

RAW protocol sockets with can_filters (SOCK_RAW)带有 can_filters (SOCK_RAW) 的 RAW 协议 sockets

Using CAN_RAW sockets is extensively comparable to the commonly使用 CAN_RAW sockets 与常用的
known access to CAN character devices.对 CAN 字符设备的已知访问。 To meet the new possibilities迎接新的可能
provided by the multi user SocketCAN approach, some reasonable由多用户SocketCAN提供的方法,一些合理的
defaults are set at RAW socket binding time:默认设置在 RAW 套接字绑定时间:

  • The filters are set to exactly one filter receiving everything过滤器设置为一个过滤器接收所有内容
  • The socket only receives valid data frames (=> no error message frames)套接字只接收有效的数据帧(=> 没有错误消息帧)
  • The loopback of sent CAN frames is enabled (see chapter 3.2)已发送 CAN 帧的环回已启用(参见第 3.2 节)
  • The socket does not receive its own sent frames (in loopback mode)套接字不接收自己发送的帧(在环回模式下)

These default settings may be changed before or after binding the socket

I can change the mask before or after binding the socket and before every can message receive .canbinding socket之前或之后以及在每条消息receive之前更改mask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM