Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
socketcan_utils.h
Go to the documentation of this file.
1
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>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
36static 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
48 if ((zframe->flags & CAN_FRAME_RTR) == 0U) {
49 memcpy(zframe->data, sframe->data,
50 MIN(sframe->len, MIN(sizeof(sframe->data), sizeof(zframe->data))));
51 }
52}
53
60static inline void socketcan_from_can_frame(const struct can_frame *zframe,
61 struct socketcan_frame *sframe)
62{
63 memset(sframe, 0, sizeof(*sframe));
64
65 sframe->can_id = zframe->id;
66 sframe->can_id |= (zframe->flags & CAN_FRAME_IDE) != 0 ? BIT(31) : 0;
67 sframe->can_id |= (zframe->flags & CAN_FRAME_RTR) != 0 ? BIT(30) : 0;
68 sframe->len = can_dlc_to_bytes(zframe->dlc);
69
70 if ((zframe->flags & CAN_FRAME_FDF) != 0) {
71 sframe->flags |= CANFD_FDF;
72 }
73
74 if ((zframe->flags & CAN_FRAME_BRS) != 0) {
75 sframe->flags |= CANFD_BRS;
76 }
77
78 if ((zframe->flags & CAN_FRAME_RTR) == 0U) {
79 memcpy(sframe->data, zframe->data,
80 MIN(sframe->len, MIN(sizeof(zframe->data), sizeof(sframe->data))));
81 }
82}
83
90static inline void socketcan_to_can_filter(const struct socketcan_filter *sfilter,
91 struct can_filter *zfilter)
92{
93 memset(zfilter, 0, sizeof(*zfilter));
94
95 zfilter->flags |= (sfilter->can_id & BIT(31)) != 0 ? CAN_FILTER_IDE : 0;
96 zfilter->id = sfilter->can_id & BIT_MASK(29);
97 zfilter->mask = sfilter->can_mask & BIT_MASK(29);
98}
99
106static inline void socketcan_from_can_filter(const struct can_filter *zfilter,
107 struct socketcan_filter *sfilter)
108{
109 memset(sfilter, 0, sizeof(*sfilter));
110
111 sfilter->can_id = zfilter->id;
112 sfilter->can_id |= (zfilter->flags & CAN_FILTER_IDE) != 0 ? BIT(31) : 0;
113
114 sfilter->can_mask = zfilter->mask;
115 sfilter->can_mask |= (zfilter->flags & CAN_FILTER_IDE) != 0 ? BIT(31) : 0;
116
117 if (!IS_ENABLED(CONFIG_CAN_ACCEPT_RTR)) {
118 sfilter->can_mask |= BIT(30);
119 }
120}
121
126#ifdef __cplusplus
127}
128#endif
129
130#endif /* ZEPHYR_INCLUDE_NET_SOCKETCAN_H_ */
Controller Area Network (CAN) driver API.
#define CAN_FRAME_BRS
Frame uses CAN FD Baud Rate Switch (BRS).
Definition: can.h:160
#define CAN_FRAME_FDF
Frame uses CAN FD format (FDF)
Definition: can.h:157
static uint8_t can_bytes_to_dlc(uint8_t num_bytes)
Convert from number of bytes to Data Length Code (DLC)
Definition: can.h:1753
#define CAN_FRAME_IDE
Frame uses extended (29-bit) CAN ID.
Definition: can.h:151
static uint8_t can_dlc_to_bytes(uint8_t dlc)
Convert from Data Length Code (DLC) to the number of data bytes.
Definition: can.h:1738
#define CAN_FRAME_RTR
Frame is a Remote Transmission Request (RTR)
Definition: can.h:154
#define CAN_FILTER_IDE
Filter matches frames with extended (29-bit) CAN IDs.
Definition: can.h:211
static void socketcan_to_can_filter(const struct socketcan_filter *sfilter, struct can_filter *zfilter)
Translate a socketcan_filter struct to a can_filter struct.
Definition: socketcan_utils.h:90
#define CANFD_FDF
Mark CAN FD for dual use of struct canfd_frame.
Definition: socketcan.h:64
static void socketcan_from_can_frame(const struct can_frame *zframe, struct socketcan_frame *sframe)
Translate a can_frame struct to a socketcan_frame struct.
Definition: socketcan_utils.h:60
static void socketcan_to_can_frame(const struct socketcan_frame *sframe, struct can_frame *zframe)
Translate a socketcan_frame struct to a can_frame struct.
Definition: socketcan_utils.h:36
#define CANFD_BRS
Bit rate switch (second bitrate for payload data)
Definition: socketcan.h:62
static void socketcan_from_can_filter(const struct can_filter *zfilter, struct socketcan_filter *sfilter)
Translate a can_filter struct to a socketcan_filter struct.
Definition: socketcan_utils.h:106
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:124
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition: util_macro.h:44
#define MIN(a, b)
Obtain the minimum of two values.
Definition: util.h:391
#define BIT_MASK(n)
Bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0.
Definition: util_macro.h:68
SocketCAN definitions.
void * memset(void *buf, int c, size_t n)
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
CAN filter structure.
Definition: can.h:218
uint32_t mask
CAN identifier matching mask.
Definition: can.h:224
uint8_t flags
Flags.
Definition: can.h:226
uint32_t id
CAN identifier to match.
Definition: can.h:220
CAN frame structure.
Definition: can.h:172
uint8_t dlc
Data Length Code (DLC) indicating data length in bytes.
Definition: can.h:176
uint8_t flags
Flags.
Definition: can.h:178
uint32_t id
Standard (11-bit) or extended (29-bit) CAN identifier.
Definition: can.h:174
uint8_t data[CAN_MAX_DLEN]
Payload data accessed as unsigned 8 bit values.
Definition: can.h:197
CAN filter for Linux SocketCAN compatibility.
Definition: socketcan.h:131
socketcan_id_t can_mask
The mask applied to can_id for matching.
Definition: socketcan.h:135
socketcan_id_t can_id
The CAN identifier to match.
Definition: socketcan.h:133
CAN frame for Linux SocketCAN compatibility.
Definition: socketcan.h:110
socketcan_id_t can_id
32-bit CAN ID + EFF/RTR/ERR flags.
Definition: socketcan.h:112
uint8_t data[8U]
The payload data.
Definition: socketcan.h:123
uint8_t len
Frame payload length in bytes.
Definition: socketcan.h:114
uint8_t flags
Additional flags for CAN FD.
Definition: socketcan.h:116