lior.i
lior.i

Reputation: 747

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:

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);

Upvotes: 0

Views: 311

Answers (1)

lior.i
lior.i

Reputation: 747

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.

Upvotes: 0

Related Questions