42#include <linux/sockios.h>
53#define ERRNOLOG(where, x...) \
63int socketcan_open(
const char *device,
int loopback)
66 struct sockaddr_can addr;
70 ERRNOLOG(
"socket", s);
74 ERRNOLOG(
"setsockopt(sndbuf)",
78 ERRNOLOG(
"setsockopt(loopback)",
80 s, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback,
sizeof(loopback)));
83 can_err_mask_t err_mask = CAN_ERR_TX_TIMEOUT | CAN_ERR_LOSTARB |
84 CAN_ERR_CRTL | CAN_ERR_PROT | CAN_ERR_TRX | CAN_ERR_ACK |
85 CAN_ERR_BUSOFF | CAN_ERR_BUSERROR | CAN_ERR_RESTARTED;
86 ERRNOLOG(
"setsockopt(filter)",
88 s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask,
sizeof(err_mask)));
89 strcpy(ifr.ifr_name, device);
91 ERRNOLOG(
"interface set",
::ioctl(s, SIOCGIFINDEX, &ifr));
93 addr.can_family = AF_CAN;
94 addr.can_ifindex = ifr.ifr_ifindex;
96 ERRNOLOG(
"bind",
bind(s, (
struct sockaddr *)&addr,
sizeof(addr)));
int ioctl(int fd, unsigned long int key,...)
Request and ioctl transaction.
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len)
Set the socket options.
int bind(int socket, const struct sockaddr *address, socklen_t address_len)
Bind a name to a socket.
int socket(int domain, int type, int protocol)
Create an unbound socket in a communications domain.
#define SOCK_RAW
Raw Socket.
#define SOL_SOCKET
socket option category