22#ifndef LELY_IO2_INTERN_LINUX_CAN_ATTR_H_
23#define LELY_IO2_INTERN_LINUX_CAN_ATTR_H_
36#if !LELY_NO_CANFD && !defined(CANFD_MTU)
37#error CAN FD not supported.
39#define can_state can_state_
40#define CAN_STATE_STOPPED CAN_STATE_STOPPED_
41#define CAN_STATE_SLEEPING CAN_STATE_SLEEPING_
42#include <linux/can/netlink.h>
43#undef CAN_STATE_SLEEPING
44#undef CAN_STATE_STOPPED
46#include <linux/if_arp.h>
60#define IO_CAN_ATTR_INIT \
62 CAN_STATE_ACTIVE, 0, 0 \
65#define IO_CAN_ATTR_INIT \
67 CAN_STATE_ACTIVE, 0, 0, 0 \
75static int io_can_attr_get(
struct io_can_attr *attr,
unsigned int ifindex);
76static int io_can_attr_parse(
struct nlmsghdr *nlh,
size_t len,
void *arg);
79io_can_attr_get(
struct io_can_attr *attr,
unsigned int ifindex)
84 if (rtnl_open(&rth) == -1) {
89 if (rtnl_send_getlink_request(&rth, AF_UNSPEC, ARPHRD_CAN, ifindex)
92 goto error_rtnl_send_getlink_request;
95 if (rtnl_recv_type(&rth, RTM_NEWLINK, &io_can_attr_parse, attr) == -1) {
97 goto error_rtnl_recv_type;
104error_rtnl_send_getlink_request:
112io_can_attr_parse(
struct nlmsghdr *nlh,
size_t len,
void *arg)
115 assert(NLMSG_OK(nlh, len));
116 assert(nlh->nlmsg_type == RTM_NEWLINK);
121 struct ifinfomsg *ifi = NLMSG_DATA(nlh);
122 if (ifi->ifi_type != ARPHRD_CAN) {
129 rta_find(IFLA_RTA(ifi), IFLA_PAYLOAD(nlh), IFLA_MTU);
130 if (mtu && RTA_PAYLOAD(mtu) >=
sizeof(
unsigned int)) {
131 unsigned int *data = RTA_DATA(mtu);
132 if (*data == CANFD_MTU)
135 attr->flags &= ~IO_CAN_BUS_FLAG_FDF;
139 struct rtattr *linkinfo = rta_find(
140 IFLA_RTA(ifi), IFLA_PAYLOAD(nlh), IFLA_LINKINFO);
146 struct rtattr *info_data = rta_find(RTA_DATA(linkinfo),
147 RTA_PAYLOAD(linkinfo), IFLA_INFO_DATA);
151 rta = rta_find(RTA_DATA(info_data), RTA_PAYLOAD(info_data),
153 if (rta && RTA_PAYLOAD(rta) >=
sizeof(
int)) {
154 int *data = RTA_DATA(rta);
156 case CAN_STATE_ERROR_ACTIVE:
157 case CAN_STATE_ERROR_WARNING:
160 case CAN_STATE_ERROR_PASSIVE:
163 case CAN_STATE_BUS_OFF:
166 case CAN_STATE_STOPPED_:
167 attr->state = CAN_STATE_STOPPED;
169 case CAN_STATE_SLEEPING_:
170 attr->state = CAN_STATE_SLEEPING;
176 rta = rta_find(RTA_DATA(info_data), RTA_PAYLOAD(info_data),
178 if (rta && RTA_PAYLOAD(rta) >=
sizeof(
struct can_ctrlmode)) {
179 struct can_ctrlmode *data = RTA_DATA(rta);
180 if (data->flags & CAN_CTRLMODE_FD)
183 attr->flags &= ~IO_CAN_BUS_FLAG_BRS;
187 rta = rta_find(RTA_DATA(info_data), RTA_PAYLOAD(info_data),
189 if (rta && RTA_PAYLOAD(rta) >=
sizeof(
struct can_bittiming)) {
190 struct can_bittiming *data = RTA_DATA(rta);
191 attr->nominal = data->bitrate;
195 rta = rta_find(RTA_DATA(info_data), RTA_PAYLOAD(info_data),
196 IFLA_CAN_DATA_BITTIMING);
197 if (rta && RTA_PAYLOAD(rta) >=
sizeof(
struct can_bittiming)) {
198 struct can_bittiming *data = RTA_DATA(rta);
199 attr->data = data->bitrate;
@ CAN_STATE_BUSOFF
The bus off state (TX/RX error count >= 256).
@ CAN_STATE_PASSIVE
The error passive state (TX/RX error count < 256).
@ CAN_STATE_ACTIVE
The error active state (TX/RX error count < 128).
This header file is part of the I/O library; it contains the abstract CAN bus interface.
@ IO_CAN_BUS_FLAG_BRS
Bit Rate Switch support is enabled.
@ IO_CAN_BUS_FLAG_FDF
FD Format (formerly Extended Data Length) support is enabled.
This header file is part of the I/O library; it contains the CAN bus declarations for Linux.
This is the internal header file of the rtnetlink functions.
This is the internal header file of the Windows-specific I/O declarations.