1 /** @file
2  * @brief SocketCAN definitions.
3  *
4  * Definitions for SocketCAN support.
5  */
6 
7 /*
8  * Copyright (c) 2019 Intel Corporation
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_NET_SOCKETCAN_H_
14 #define ZEPHYR_INCLUDE_NET_SOCKETCAN_H_
15 
16 #include <zephyr/types.h>
17 #include <zephyr/net/net_ip.h>
18 #include <zephyr/net/net_if.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25  * @brief SocketCAN library
26  * @defgroup socket_can Network Core Library
27  * @ingroup networking
28  * @{
29  */
30 
31 /* Protocols of the protocol family PF_CAN */
32 #define CAN_RAW 1
33 
34 /* SocketCAN options */
35 #define SOL_CAN_BASE 100
36 #define SOL_CAN_RAW (SOL_CAN_BASE + CAN_RAW)
37 
38 enum {
39 	CAN_RAW_FILTER = 1,
40 };
41 
42 /* SocketCAN MTU size compatible with Linux */
43 #ifdef CONFIG_CAN_FD_MODE
44 #define SOCKETCAN_MAX_DLEN 64U
45 #define CANFD_MTU (sizeof(struct socketcan_frame))
46 #define CAN_MTU (CANFD_MTU - 56U)
47 #else /* CONFIG_CAN_FD_MODE */
48 #define SOCKETCAN_MAX_DLEN 8U
49 #define CAN_MTU (sizeof(struct socketcan_frame))
50 #endif /* !CONFIG_CAN_FD_MODE */
51 
52 /* CAN-FD specific flags from Linux Kernel (include/uapi/linux/can.h) */
53 #define CANFD_BRS 0x01 /* bit rate switch (second bitrate for payload data) */
54 #define CANFD_ESI 0x02 /* error state indicator of the transmitting node */
55 #define CANFD_FDF 0x04 /* mark CAN FD for dual use of struct canfd_frame */
56 
57 /**
58  * struct sockaddr_can - The sockaddr structure for CAN sockets
59  *
60  */
61 struct sockaddr_can {
62 	sa_family_t can_family;
63 	int         can_ifindex;
64 };
65 
66 /**
67  * @name Linux SocketCAN compatibility
68  *
69  * The following structures and functions provide compatibility with the CAN
70  * frame and CAN filter formats used by Linux SocketCAN.
71  *
72  * @{
73  */
74 
75 /**
76  * CAN Identifier structure for Linux SocketCAN compatibility.
77  *
78  * The fields in this type are:
79  *
80  * @code{.text}
81  *
82  * +------+--------------------------------------------------------------+
83  * | Bits | Description                                                  |
84  * +======+==============================================================+
85  * | 0-28 | CAN identifier (11/29 bit)                                   |
86  * +------+--------------------------------------------------------------+
87  * |  29  | Error message frame flag (0 = data frame, 1 = error message) |
88  * +------+--------------------------------------------------------------+
89  * |  30  | Remote transmission request flag (1 = RTR frame)             |
90  * +------+--------------------------------------------------------------+
91  * |  31  | Frame format flag (0 = standard 11 bit, 1 = extended 29 bit) |
92  * +------+--------------------------------------------------------------+
93  *
94  * @endcode
95  */
96 typedef uint32_t socketcan_id_t;
97 
98 /**
99  * @brief CAN frame for Linux SocketCAN compatibility.
100  */
101 struct socketcan_frame {
102 	/** 32-bit CAN ID + EFF/RTR/ERR flags. */
103 	socketcan_id_t can_id;
104 	/** Frame payload length in bytes. */
105 	uint8_t len;
106 	/** Additional flags for CAN FD. */
107 	uint8_t flags;
108 	/** @cond INTERNAL_HIDDEN */
109 	uint8_t res0;  /* reserved/padding. */
110 	uint8_t res1;  /* reserved/padding. */
111 	/** @endcond */
112 
113 	/** The payload data. */
114 	uint8_t data[SOCKETCAN_MAX_DLEN];
115 };
116 
117 /**
118  * @brief CAN filter for Linux SocketCAN compatibility.
119  *
120  * A filter is considered a match when `received_can_id & mask == can_id & can_mask`.
121  */
122 struct socketcan_filter {
123 	/** The CAN identifier to match. */
124 	socketcan_id_t can_id;
125 	/** The mask applied to @a can_id for matching. */
126 	socketcan_id_t can_mask;
127 	/** Additional flags for FD frame filter. */
128 	uint8_t flags;
129 };
130 
131 /** @} */
132 
133 /**
134  * @}
135  */
136 
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 #endif /* ZEPHYR_INCLUDE_NET_SOCKETCAN_H_ */
142