Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
canbus.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
10
11#ifndef ZEPHYR_INCLUDE_NET_CANBUS_H_
12#define ZEPHYR_INCLUDE_NET_CANBUS_H_
13
14#include <zephyr/types.h>
15#include <zephyr/net/net_ip.h>
16#include <zephyr/net/net_if.h>
18#include <zephyr/drivers/can.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
27struct canbus_api {
32 struct net_if_api iface_api;
33
35 int (*send)(const struct device *dev, struct net_pkt *pkt);
36
38 void (*close)(const struct device *dev, int filter_id);
39
41 int (*setsockopt)(const struct device *dev, void *obj, int level,
42 int optname,
43 const void *optval, net_socklen_t optlen);
44
46 int (*getsockopt)(const struct device *dev, void *obj, int level,
47 int optname,
48 const void *optval, net_socklen_t *optlen);
49};
50
51/* Make sure that the network interface API is properly setup inside
52 * CANBUS API struct (it is the first one).
53 */
54BUILD_ASSERT(offsetof(struct canbus_api, iface_api) == 0);
55
57
58struct z_net_canbus_context {
59 struct net_if *iface;
60};
61
62struct z_net_canbus_config {
63 const struct device *can_dev;
64};
65
66int z_net_canbus_init(const struct device *dev);
67
68extern const struct canbus_api z_net_canbus_api;
69
70/*
71 * Network interface of a CAN controller, defined by CAN_DEVICE_DT_DEFINE() next to the
72 * controller's device so that only controllers with a driver in the build get one.
73 */
74#define Z_NET_CANBUS_DEVICE_DT_DEFINE(node_id) \
75 static struct z_net_canbus_context z_net_canbus_ctx_##node_id; \
76 static const struct z_net_canbus_config z_net_canbus_cfg_##node_id = { \
77 .can_dev = DEVICE_DT_GET(node_id), \
78 }; \
79 NET_DEVICE_INIT(net_canbus_##node_id, DEVICE_DT_NAME(node_id), z_net_canbus_init, NULL, \
80 &z_net_canbus_ctx_##node_id, &z_net_canbus_cfg_##node_id, \
81 CONFIG_NET_CANBUS_INIT_PRIORITY, &z_net_canbus_api, CANBUS_RAW_L2, \
82 NET_L2_GET_CTX_TYPE(CANBUS_RAW_L2), CAN_MTU);
83
85
86#ifdef __cplusplus
87}
88#endif
89
90#endif /* ZEPHYR_INCLUDE_NET_CANBUS_H_ */
Header file for Controller Area Network (CAN) controller driver API.
uint32_t net_socklen_t
Length of a socket address.
Definition net_ip.h:172
Public API for network interface.
IPv6 and IPv4 definitions.
SocketCAN definitions.
CAN L2 network driver API.
Definition canbus.h:27
int(* setsockopt)(const struct device *dev, void *obj, int level, int optname, const void *optval, net_socklen_t optlen)
Set socket CAN option.
Definition canbus.h:41
int(* send)(const struct device *dev, struct net_pkt *pkt)
Send a CAN packet by socket.
Definition canbus.h:35
void(* close)(const struct device *dev, int filter_id)
Close the related CAN socket.
Definition canbus.h:38
int(* getsockopt)(const struct device *dev, void *obj, int level, int optname, const void *optval, net_socklen_t *optlen)
Get socket CAN option.
Definition canbus.h:46
struct net_if_api iface_api
The net_if_api must be placed in first position in this struct so that we are compatible with network...
Definition canbus.h:32
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
Network Interface structure.
Definition net_if.h:857
Network packet.
Definition net_pkt.h:119