1 /** @file
2  * @brief SocketCAN utilities.
3  *
4  * Utilities 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_UTILS_H_
14 #define ZEPHYR_INCLUDE_NET_SOCKETCAN_UTILS_H_
15 
16 #include <zephyr/drivers/can.h>
17 #include <zephyr/net/socketcan.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief SocketCAN utilities
25  * @defgroup socket_can Network Core Library
26  * @ingroup networking
27  * @{
28  */
29 
30 /**
31  * @brief Translate a @a socketcan_frame struct to a @a can_frame struct.
32  *
33  * @param sframe Pointer to sockecan_frame struct.
34  * @param zframe Pointer to can_frame struct.
35  */
socketcan_to_can_frame(const struct socketcan_frame * sframe,struct can_frame * zframe)36 static inline void socketcan_to_can_frame(const struct socketcan_frame *sframe,
37 					  struct can_frame *zframe)
38 {
39 	memset(zframe, 0, sizeof(*zframe));
40 
41 	zframe->flags |= (sframe->can_id & BIT(31)) != 0 ? CAN_FRAME_IDE : 0;
42 	zframe->flags |= (sframe->can_id & BIT(30)) != 0 ? CAN_FRAME_RTR : 0;
43 	zframe->flags |= (sframe->flags & CANFD_FDF) != 0 ? CAN_FRAME_FDF : 0;
44 	zframe->flags |= (sframe->flags & CANFD_BRS) != 0 ? CAN_FRAME_BRS : 0;
45 	zframe->id = sframe->can_id & BIT_MASK(29);
46 	zframe->dlc = can_bytes_to_dlc(sframe->len);
47 	memcpy(zframe->data, sframe->data, MIN(sizeof(sframe->data), sizeof(zframe->data)));
48 }
49 
50 /**
51  * @brief Translate a @a can_frame struct to a @a socketcan_frame struct.
52  *
53  * @param zframe Pointer to can_frame struct.
54  * @param sframe  Pointer to socketcan_frame struct.
55  */
socketcan_from_can_frame(const struct can_frame * zframe,struct socketcan_frame * sframe)56 static inline void socketcan_from_can_frame(const struct can_frame *zframe,
57 					    struct socketcan_frame *sframe)
58 {
59 	memset(sframe, 0, sizeof(*sframe));
60 
61 	sframe->can_id = zframe->id;
62 	sframe->can_id |= (zframe->flags & CAN_FRAME_IDE) != 0 ? BIT(31) : 0;
63 	sframe->can_id |= (zframe->flags & CAN_FRAME_RTR) != 0 ? BIT(30) : 0;
64 	sframe->len = can_dlc_to_bytes(zframe->dlc);
65 
66 	if ((zframe->flags & CAN_FRAME_FDF) != 0) {
67 		sframe->flags |= CANFD_FDF;
68 	}
69 
70 	if ((zframe->flags & CAN_FRAME_BRS) != 0) {
71 		sframe->flags |= CANFD_BRS;
72 	}
73 
74 	memcpy(sframe->data, zframe->data, MIN(sizeof(zframe->data), sizeof(sframe->data)));
75 }
76 
77 /**
78  * @brief Translate a @a socketcan_filter struct to a @a can_filter struct.
79  *
80  * @param sfilter Pointer to socketcan_filter struct.
81  * @param zfilter Pointer to can_filter struct.
82  */
socketcan_to_can_filter(const struct socketcan_filter * sfilter,struct can_filter * zfilter)83 static inline void socketcan_to_can_filter(const struct socketcan_filter *sfilter,
84 					   struct can_filter *zfilter)
85 {
86 	memset(zfilter, 0, sizeof(*zfilter));
87 
88 	zfilter->flags |= (sfilter->can_id & BIT(31)) != 0 ? CAN_FILTER_IDE : 0;
89 	zfilter->id = sfilter->can_id & BIT_MASK(29);
90 	zfilter->mask = sfilter->can_mask & BIT_MASK(29);
91 	zfilter->flags |= (sfilter->flags & CANFD_FDF) != 0 ? CAN_FILTER_FDF : 0;
92 
93 	if ((sfilter->can_mask & BIT(30)) == 0) {
94 		zfilter->flags |= CAN_FILTER_DATA | CAN_FILTER_RTR;
95 	} else if ((sfilter->can_id & BIT(30)) == 0) {
96 		zfilter->flags |= CAN_FILTER_DATA;
97 	} else {
98 		zfilter->flags |= CAN_FILTER_RTR;
99 	}
100 }
101 
102 /**
103  * @brief Translate a @a can_filter struct to a @a socketcan_filter struct.
104  *
105  * @param zfilter Pointer to can_filter struct.
106  * @param sfilter Pointer to socketcan_filter struct.
107  */
socketcan_from_can_filter(const struct can_filter * zfilter,struct socketcan_filter * sfilter)108 static inline void socketcan_from_can_filter(const struct can_filter *zfilter,
109 					     struct socketcan_filter *sfilter)
110 {
111 	memset(sfilter, 0, sizeof(*sfilter));
112 
113 	sfilter->can_id = zfilter->id;
114 	sfilter->can_id |= (zfilter->flags & CAN_FILTER_IDE) != 0 ? BIT(31) : 0;
115 	sfilter->can_id |= (zfilter->flags & CAN_FILTER_RTR) != 0 ? BIT(30) : 0;
116 
117 	sfilter->can_mask = zfilter->mask;
118 	sfilter->can_mask |= (zfilter->flags & CAN_FILTER_IDE) != 0 ? BIT(31) : 0;
119 
120 	if ((zfilter->flags & (CAN_FILTER_DATA | CAN_FILTER_RTR)) !=
121 		(CAN_FILTER_DATA | CAN_FILTER_RTR)) {
122 		sfilter->can_mask |= BIT(30);
123 	}
124 
125 	if ((zfilter->flags & CAN_FILTER_FDF) != 0) {
126 		sfilter->flags |= CANFD_FDF;
127 	}
128 }
129 
130 /**
131  * @}
132  */
133 
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 #endif /* ZEPHYR_INCLUDE_NET_SOCKETCAN_H_ */
139