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 SocketCAN library
27  * @since 1.14
28  * @version 0.8.0
29  * @ingroup networking
30  * @{
31  */
32 
33 /** Protocols of the protocol family PF_CAN */
34 #define CAN_RAW 1
35 
36 /** @cond INTERNAL_HIDDEN */
37 
38 /* SocketCAN options */
39 #define SOL_CAN_BASE 100
40 #define SOL_CAN_RAW (SOL_CAN_BASE + CAN_RAW)
41 
42 enum {
43 	CAN_RAW_FILTER = 1,
44 };
45 
46 /** @endcond */
47 
48 /* SocketCAN MTU size compatible with Linux */
49 #ifdef CONFIG_CAN_FD_MODE
50 /** SocketCAN max data length */
51 #define SOCKETCAN_MAX_DLEN 64U
52 /** CAN FD frame MTU */
53 #define CANFD_MTU (sizeof(struct socketcan_frame))
54 /** CAN frame MTU */
55 #define CAN_MTU (CANFD_MTU - 56U)
56 #else /* CONFIG_CAN_FD_MODE */
57 /** SocketCAN max data length */
58 #define SOCKETCAN_MAX_DLEN 8U
59 /** CAN frame MTU */
60 #define CAN_MTU (sizeof(struct socketcan_frame))
61 #endif /* !CONFIG_CAN_FD_MODE */
62 
63 /* CAN FD specific flags from Linux Kernel (include/uapi/linux/can.h) */
64 #define CANFD_BRS 0x01 /**< Bit rate switch (second bitrate for payload data) */
65 #define CANFD_ESI 0x02 /**< Error state indicator of the transmitting node */
66 #define CANFD_FDF 0x04 /**< Mark CAN FD for dual use of struct canfd_frame */
67 
68 /**
69  * struct sockaddr_can - The sockaddr structure for CAN sockets
70  *
71  */
72 struct sockaddr_can {
73 	sa_family_t can_family;   /**< Address family */
74 	int         can_ifindex;  /**< SocketCAN network interface index */
75 };
76 
77 /**
78  * @name Linux SocketCAN compatibility
79  *
80  * The following structures and functions provide compatibility with the CAN
81  * frame and CAN filter formats used by Linux SocketCAN.
82  *
83  * @{
84  */
85 
86 /**
87  * CAN Identifier structure for Linux SocketCAN compatibility.
88  *
89  * The fields in this type are:
90  *
91  * @code{.text}
92  *
93  * +------+--------------------------------------------------------------+
94  * | Bits | Description                                                  |
95  * +======+==============================================================+
96  * | 0-28 | CAN identifier (11/29 bit)                                   |
97  * +------+--------------------------------------------------------------+
98  * |  29  | Error message frame flag (0 = data frame, 1 = error message) |
99  * +------+--------------------------------------------------------------+
100  * |  30  | Remote transmission request flag (1 = RTR frame)             |
101  * +------+--------------------------------------------------------------+
102  * |  31  | Frame format flag (0 = standard 11 bit, 1 = extended 29 bit) |
103  * +------+--------------------------------------------------------------+
104  *
105  * @endcode
106  */
107 typedef uint32_t socketcan_id_t;
108 
109 /**
110  * @brief CAN frame for Linux SocketCAN compatibility.
111  */
112 struct socketcan_frame {
113 	/** 32-bit CAN ID + EFF/RTR/ERR flags. */
114 	socketcan_id_t can_id;
115 	/** Frame payload length in bytes. */
116 	uint8_t len;
117 	/** Additional flags for CAN FD. */
118 	uint8_t flags;
119 	/** @cond INTERNAL_HIDDEN */
120 	uint8_t res0;  /* reserved/padding. */
121 	uint8_t res1;  /* reserved/padding. */
122 	/** @endcond */
123 
124 	/** The payload data. */
125 	uint8_t data[SOCKETCAN_MAX_DLEN];
126 };
127 
128 /**
129  * @brief CAN filter for Linux SocketCAN compatibility.
130  *
131  * A filter is considered a match when `received_can_id & mask == can_id & can_mask`.
132  */
133 struct socketcan_filter {
134 	/** The CAN identifier to match. */
135 	socketcan_id_t can_id;
136 	/** The mask applied to @a can_id for matching. */
137 	socketcan_id_t can_mask;
138 	/** Additional flags for FD frame filter. */
139 	uint8_t flags;
140 };
141 
142 /** @} */
143 
144 /**
145  * @}
146  */
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 
152 #endif /* ZEPHYR_INCLUDE_NET_SOCKETCAN_H_ */
153