Veethree D7 SDK
Loading...
Searching...
No Matches
can_socket_interface.h
Go to the documentation of this file.
1
5#ifndef CAN_SOCKET_INTERFACE
6#define CAN_SOCKET_INTERFACE
7
8#include "can_adapter.h"
9#include "can_common.h"
10#include <chrono>
11#include <functional>
12#include <system_error>
13#include <vector>
14
15namespace vsdk::comms {
20
21class can_socket;
22
27 std::errc recv_err{};
28 std::optional<std::errc> callback_err
29 = std::nullopt;
30};
31
41 std::optional<can_adapter> adapter
42 = std::nullopt;
43 std::optional<can_msg_frame> frame
44 = std::nullopt;
45};
46
50using can_socket_recv_callback = std::function<std::errc(can_msg_frame)>;
51
58class can_socket_interface { // NOLINT(*-special-member-functions)
59public:
60 virtual ~can_socket_interface() noexcept = default;
61
65#if __linux__
66 using native_handle = int;
70 virtual native_handle handle() = 0;
71#elif !defined(_WIN32) // Windows will be supported later
72#error "Unsupported OS"
73#endif
74
78 virtual std::errc open() = 0;
83 virtual std::errc close() = 0;
87 virtual bool is_open() = 0;
92 virtual std::errc bind(const can_adapter& adapter) = 0;
93
97 virtual std::optional<can_adapter> adapter() const = 0;
98
103 virtual std::errc send(const can_msg_frame& frame) = 0;
116 can_msg_frame& frame, std::chrono::milliseconds timeout = std::chrono::milliseconds::zero())
117 = 0;
118
122 virtual std::errc set_nonblocking() = 0;
126 virtual std::errc set_blocking() = 0;
130 virtual std::errc enable_error_frames() = 0;
134 virtual std::errc disable_error_frames() = 0;
143 virtual void clear_receive_callback() = 0;
147 virtual bool has_receive_callback() = 0;
148
157 virtual select_result_can select_receive_timeout(const std::vector<can_socket>& sockets,
158 std::chrono::milliseconds timeout) const
159 = 0;
160};
161
162
163} // namespace vsdk::comms
164
165#endif // CAN_SOCKET_INTERFACE
CAN Adapter.
Common CAN structures.
The CAN interface setup class.
Definition can_adapter.h:47
The can_socket_interface lists the expected interface for a CAN socket implementation that can be use...
Definition can_socket_interface.h:58
virtual std::optional< can_adapter > adapter() const =0
Get the adapter this socket is bound to, if any.
virtual std::errc enable_error_frames()=0
Enable receiving CAN Error Frames on this socket.
virtual std::errc send(const can_msg_frame &frame)=0
Send a CAN frame on this socket.
virtual std::errc bind(const can_adapter &adapter)=0
Bind this socket to an adapter.
virtual select_result_can select_receive_timeout(const std::vector< can_socket > &sockets, std::chrono::milliseconds timeout) const =0
Listen on multiple sockets at the same time for a CAN frame. Returns after receiving a single CAN fra...
virtual bool is_open()=0
Check if the socket is open and bound to an interface.
virtual std::errc set_nonblocking()=0
Make reads and writes on this socket non-blocking.
virtual void set_receive_callback(const can_socket_recv_callback &cb)=0
Set a callback to be run when a CAN frame is received.
virtual bool has_receive_callback()=0
Does the socket have a receive callback set.
virtual void clear_receive_callback()=0
Remove the receive callback.
virtual std::errc open()=0
Native handle for a socket.
virtual receive_result receive(can_msg_frame &frame, std::chrono::milliseconds timeout=std::chrono::milliseconds::zero())=0
Receive a CAN frame on this socket.
virtual std::errc close()=0
Close this socket.
virtual std::errc disable_error_frames()=0
Disable receiving CAN Error Frames on this socket.
virtual std::errc set_blocking()=0
Make reads and writes on this socket blocking.
Convenience wrapper class around a CAN socket.
Definition can_socket.h:22
std::function< std::errc(can_msg_frame)> can_socket_recv_callback
A callback function to be called when a message is successfully ready from a socket.
Definition can_socket_interface.h:50
Any entities that belong to the comms library.
Definition can_adapter.h:11
CAN frame structure.
Definition can_common.h:32
Result to receiving a message on a socket.
Definition can_socket_interface.h:26
std::errc recv_err
The error code for receving the CAN frame.
Definition can_socket_interface.h:27
std::optional< std::errc > callback_err
The error code returned by the callback (if there is one)
Definition can_socket_interface.h:29
Result to receive a message on multiple sockets at once.
Definition can_socket_interface.h:39
std::optional< can_msg_frame > frame
The CAN frame that was read. std::nullopt if an error occurred while reading.
Definition can_socket_interface.h:44
std::optional< can_adapter > adapter
The CAN adapter on which a frame was received. std::nullopt if an error occurred while reading.
Definition can_socket_interface.h:42
receive_result result
The result of the entire operation.
Definition can_socket_interface.h:40