Zephyr Project API  3.2.0
A Scalable Open Source RTOS
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 zframe->id_type = (sframe->can_id & BIT(31)) >> 31;
40 zframe->rtr = (sframe->can_id & BIT(30)) >> 30;
41 zframe->id = sframe->can_id & BIT_MASK(29);
42 zframe->dlc = can_bytes_to_dlc(sframe->len);
43 zframe->fd = !!(sframe->flags & CANFD_FDF);
44 memcpy(zframe->data, sframe->data, MIN(sizeof(sframe->data), sizeof(zframe->data)));
45}
46
53static inline void socketcan_from_can_frame(const struct can_frame *zframe,
54 struct socketcan_frame *sframe)
55{
56 sframe->can_id = (zframe->id_type << 31) | (zframe->rtr << 30) | zframe->id;
57 sframe->len = can_dlc_to_bytes(zframe->dlc);
58 if (zframe->fd) {
59 sframe->flags = CANFD_FDF;
60 }
61 memcpy(sframe->data, zframe->data, MIN(sizeof(zframe->data), sizeof(sframe->data)));
62}
63
70static inline void socketcan_to_can_filter(const struct socketcan_filter *sfilter,
71 struct can_filter *zfilter)
72{
73 zfilter->id_type = (sfilter->can_id & BIT(31)) >> 31;
74 zfilter->rtr = (sfilter->can_id & BIT(30)) >> 30;
75 zfilter->id = sfilter->can_id & BIT_MASK(29);
76 zfilter->rtr_mask = (sfilter->can_mask & BIT(30)) >> 30;
77 zfilter->id_mask = sfilter->can_mask & BIT_MASK(29);
78}
79
86static inline void socketcan_from_can_filter(const struct can_filter *zfilter,
87 struct socketcan_filter *sfilter)
88{
89 sfilter->can_id = (zfilter->id_type << 31) |
90 (zfilter->rtr << 30) | zfilter->id;
91 sfilter->can_mask = (zfilter->rtr_mask << 30) |
92 (zfilter->id_type << 31) | zfilter->id_mask;
93}
94
99#ifdef __cplusplus
100}
101#endif
102
103#endif /* ZEPHYR_INCLUDE_NET_SOCKETCAN_H_ */
#define BIT_MASK(n)
Definition: adc.h:14
static uint8_t can_bytes_to_dlc(uint8_t num_bytes)
Convert from number of bytes to Data Length Code (DLC)
Definition: can.h:1339
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:1324
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:70
#define CANFD_FDF
Definition: socketcan.h:55
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:53
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
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:86
#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:267
SocketCAN definitions.
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
CAN filter structure.
Definition: can.h:189
uint32_t rtr_mask
Definition: can.h:210
uint32_t id
Definition: can.h:191
uint32_t id_type
Definition: can.h:198
uint32_t id_mask
Definition: can.h:202
uint32_t rtr
Definition: can.h:196
CAN frame structure.
Definition: can.h:148
uint8_t dlc
Definition: can.h:158
uint32_t fd
Definition: can.h:152
uint32_t rtr
Definition: can.h:154
uint32_t id
Definition: can.h:150
uint32_t id_type
Definition: can.h:156
uint8_t data[CAN_MAX_DLEN]
Definition: can.h:181
CAN filter for Linux SocketCAN compatibility.
Definition: socketcan.h:122
socketcan_id_t can_mask
Definition: socketcan.h:126
socketcan_id_t can_id
Definition: socketcan.h:124
CAN frame for Linux SocketCAN compatibility.
Definition: socketcan.h:101
socketcan_id_t can_id
Definition: socketcan.h:103
uint8_t data[8U]
Definition: socketcan.h:114
uint8_t len
Definition: socketcan.h:105
uint8_t flags
Definition: socketcan.h:107