Zephyr API 3.6.99
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
35static inline void socketcan_to_can_frame(const struct socketcan_frame *sframe,
36 struct can_frame *zframe)
37{
38 memset(zframe, 0, sizeof(*zframe));
39
40 zframe->flags |= (sframe->can_id & BIT(31)) != 0 ? CAN_FRAME_IDE : 0;
41 zframe->flags |= (sframe->can_id & BIT(30)) != 0 ? CAN_FRAME_RTR : 0;
42 zframe->flags |= (sframe->flags & CANFD_FDF) != 0 ? CAN_FRAME_FDF : 0;
43 zframe->flags |= (sframe->flags & CANFD_BRS) != 0 ? CAN_FRAME_BRS : 0;
44 zframe->id = sframe->can_id & BIT_MASK(29);
45 zframe->dlc = can_bytes_to_dlc(sframe->len);
46
47 if ((zframe->flags & CAN_FRAME_RTR) == 0U) {
48 memcpy(zframe->data, sframe->data,
49 MIN(sframe->len, MIN(sizeof(sframe->data), sizeof(zframe->data))));
50 }
51}
52
59static inline void socketcan_from_can_frame(const struct can_frame *zframe,
60 struct socketcan_frame *sframe)
61{
62 memset(sframe, 0, sizeof(*sframe));
63
64 sframe->can_id = zframe->id;
65 sframe->can_id |= (zframe->flags & CAN_FRAME_IDE) != 0 ? BIT(31) : 0;
66 sframe->can_id |= (zframe->flags & CAN_FRAME_RTR) != 0 ? BIT(30) : 0;
67 sframe->len = can_dlc_to_bytes(zframe->dlc);
68
69 if ((zframe->flags & CAN_FRAME_FDF) != 0) {
70 sframe->flags |= CANFD_FDF;
71 }
72
73 if ((zframe->flags & CAN_FRAME_BRS) != 0) {
74 sframe->flags |= CANFD_BRS;
75 }
76
77 if ((zframe->flags & CAN_FRAME_RTR) == 0U) {
78 memcpy(sframe->data, zframe->data,
79 MIN(sframe->len, MIN(sizeof(zframe->data), sizeof(sframe->data))));
80 }
81}
82
89static inline void socketcan_to_can_filter(const struct socketcan_filter *sfilter,
90 struct can_filter *zfilter)
91{
92 memset(zfilter, 0, sizeof(*zfilter));
93
94 zfilter->flags |= (sfilter->can_id & BIT(31)) != 0 ? CAN_FILTER_IDE : 0;
95 zfilter->id = sfilter->can_id & BIT_MASK(29);
96 zfilter->mask = sfilter->can_mask & BIT_MASK(29);
97}
98
105static inline void socketcan_from_can_filter(const struct can_filter *zfilter,
106 struct socketcan_filter *sfilter)
107{
108 memset(sfilter, 0, sizeof(*sfilter));
109
110 sfilter->can_id = zfilter->id;
111 sfilter->can_id |= (zfilter->flags & CAN_FILTER_IDE) != 0 ? BIT(31) : 0;
112
113 sfilter->can_mask = zfilter->mask;
114 sfilter->can_mask |= (zfilter->flags & CAN_FILTER_IDE) != 0 ? BIT(31) : 0;
115
116 if (!IS_ENABLED(CONFIG_CAN_ACCEPT_RTR)) {
117 sfilter->can_mask |= BIT(30);
118 }
119}
120
125#ifdef __cplusplus
126}
127#endif
128
129#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:1754
#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:1739
#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:89
#define CANFD_FDF
Mark CAN FD for dual use of struct canfd_frame.
Definition socketcan.h:66
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:59
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:35
#define CANFD_BRS
Bit rate switch (second bitrate for payload data)
Definition socketcan.h:64
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:105
#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:407
#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:133
socketcan_id_t can_mask
The mask applied to can_id for matching.
Definition socketcan.h:137
socketcan_id_t can_id
The CAN identifier to match.
Definition socketcan.h:135
CAN frame for Linux SocketCAN compatibility.
Definition socketcan.h:112
socketcan_id_t can_id
32-bit CAN ID + EFF/RTR/ERR flags.
Definition socketcan.h:114
uint8_t data[8U]
The payload data.
Definition socketcan.h:125
uint8_t len
Frame payload length in bytes.
Definition socketcan.h:116
uint8_t flags
Additional flags for CAN FD.
Definition socketcan.h:118