0

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:

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.

Is it possible to change the mask after the bind and before receive?

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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
lior.i
  • 573
  • 1
  • 7
  • 20
  • 1
    "*I saw a similar question but it didn't answer mine*" - because it is not even remotely close to your question. `setsockopt()` is used for a lot of different options. Some can be set/changed after `bind()`, and some can't. You are going to have to refer to the can-bus documentation to see whether the `CAN_RAW_FILTER` option can be changed after `bind()` or not. – Remy Lebeau Aug 30 '22 at 13:54

1 Answers1

0

Thanks @RamyLebeau for the SocketCAN referral.

From the documentation:

RAW protocol sockets with can_filters (SOCK_RAW)

Using CAN_RAW sockets is extensively comparable to the commonly
known access to CAN character devices. To meet the new possibilities
provided by the multi user SocketCAN approach, some reasonable
defaults are set at RAW socket binding time:

  • 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)
  • 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.

lior.i
  • 573
  • 1
  • 7
  • 20