Lely core libraries 2.3.4
can.hpp
Go to the documentation of this file.
1
24#ifndef LELY_IO2_USER_CAN_HPP_
25#define LELY_IO2_USER_CAN_HPP_
26
27#include <lely/io2/user/can.h>
28#include <lely/io2/can.hpp>
29#include <lely/util/chrono.hpp>
30
31#include <utility>
32
33namespace lely {
34namespace io {
35
38 public:
41 CanBusFlag flags = CanBusFlag::NONE, size_t rxlen = 0,
42 int txtime = 0, io_user_can_chan_write_t* func = nullptr,
43 void* arg = nullptr)
45 ctx, exec, static_cast<int>(flags), rxlen, txtime, func, arg)) {
46 if (!chan) util::throw_errc("UserCanChannel");
47 }
48
49 UserCanChannel(const UserCanChannel&) = delete;
50
51 UserCanChannel(UserCanChannel&& other) noexcept : CanChannelBase(other.chan) {
52 other.chan = nullptr;
53 other.dev = nullptr;
54 }
55
56 UserCanChannel& operator=(const UserCanChannel&) = delete;
57
59 operator=(UserCanChannel&& other) noexcept {
60 using ::std::swap;
61 swap(chan, other.chan);
62 swap(dev, other.dev);
63 return *this;
64 }
65
68
70 void
71 on_read(const can_msg* msg, const ::std::chrono::nanoseconds& d, int timeout,
72 ::std::error_code& ec) noexcept {
73 auto ts = util::to_timespec(d);
74 int errsv = get_errc();
75 set_errc(0);
76 if (!io_user_can_chan_on_msg(*this, msg, &ts, timeout))
77 ec.clear();
78 else
80 set_errc(errsv);
81 }
82
84 void
85 on_read(const can_msg* msg, const ::std::chrono::nanoseconds& d,
86 int timeout = -1) {
87 ::std::error_code ec;
88 on_read(msg, d, timeout, ec);
89 if (ec) throw ::std::system_error(ec, "on_read");
90 }
91
93 void
94 on_read(const can_msg* msg, int timeout, ::std::error_code& ec) noexcept {
95 int errsv = get_errc();
96 set_errc(0);
97 if (!io_user_can_chan_on_msg(*this, msg, nullptr, timeout))
98 ec.clear();
99 else
101 set_errc(errsv);
102 }
103
105 void
106 on_read(const can_msg* msg, int timeout = -1) {
107 ::std::error_code ec;
108 on_read(msg, timeout, ec);
109 if (ec) throw ::std::system_error(ec, "on_read");
110 }
111
113 void
114 on_read(const can_err* err, const ::std::chrono::nanoseconds& d, int timeout,
115 ::std::error_code& ec) noexcept {
116 auto ts = util::to_timespec(d);
117 int errsv = get_errc();
118 set_errc(0);
119 if (!io_user_can_chan_on_err(*this, err, &ts, timeout))
120 ec.clear();
121 else
123 set_errc(errsv);
124 }
125
127 void
128 on_read(const can_err* err, const ::std::chrono::nanoseconds& d,
129 int timeout = -1) {
130 ::std::error_code ec;
131 on_read(err, d, timeout, ec);
132 if (ec) throw ::std::system_error(ec, "on_read");
133 }
134
136 void
137 on_read(const can_err* err, int timeout, ::std::error_code& ec) noexcept {
138 int errsv = get_errc();
139 set_errc(0);
140 if (!io_user_can_chan_on_err(*this, err, nullptr, timeout))
141 ec.clear();
142 else
144 set_errc(errsv);
145 }
146
148 void
149 on_read(const can_err* err, int timeout = -1) {
150 ::std::error_code ec;
151 on_read(err, timeout, ec);
152 if (ec) throw ::std::system_error(ec, "on_read");
153 }
154};
155
156} // namespace io
157} // namespace lely
158
159#endif // !LELY_IO2_USER_CAN_HPP_
A reference to an abstract CAN channel.
Definition: can.hpp:430
A user-defined CAN channel.
Definition: can.hpp:37
void on_read(const can_msg *msg, const ::std::chrono::nanoseconds &d, int timeout=-1)
Definition: can.hpp:85
void on_read(const can_msg *msg, int timeout=-1)
Definition: can.hpp:106
void on_read(const can_err *err, int timeout=-1)
Definition: can.hpp:149
UserCanChannel(io_ctx_t *ctx, ev_exec_t *exec, CanBusFlag flags=CanBusFlag::NONE, size_t rxlen=0, int txtime=0, io_user_can_chan_write_t *func=nullptr, void *arg=nullptr)
Definition: can.hpp:40
void on_read(const can_err *err, const ::std::chrono::nanoseconds &d, int timeout=-1)
Definition: can.hpp:128
void on_read(const can_err *err, const ::std::chrono::nanoseconds &d, int timeout, ::std::error_code &ec) noexcept
Definition: can.hpp:114
void on_read(const can_msg *msg, int timeout, ::std::error_code &ec) noexcept
Definition: can.hpp:94
void on_read(const can_msg *msg, const ::std::chrono::nanoseconds &d, int timeout, ::std::error_code &ec) noexcept
Definition: can.hpp:71
void on_read(const can_err *err, int timeout, ::std::error_code &ec) noexcept
Definition: can.hpp:137
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition: errnum.c:932
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:944
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
This header file is part of the I/O library; it contains the user-defined CAN channel declarations.
io_can_chan_t * io_user_can_chan_create(io_ctx_t *ctx, ev_exec_t *exec, int flags, size_t rxlen, int txtimeo, io_user_can_chan_write_t *func, void *arg)
Creates a new user-defined CAN channel.
Definition: can.c:378
void io_user_can_chan_destroy(io_can_chan_t *chan)
Destroys a user-defined CAN channel.
Definition: can.c:407
int io_user_can_chan_on_msg(io_can_chan_t *chan, const struct can_msg *msg, const struct timespec *tp, int timeout)
Processes an incoming CAN frame.
Definition: can.c:416
int io_user_can_chan_on_err(io_can_chan_t *chan, const struct can_err *err, const struct timespec *tp, int timeout)
Processes an incoming CAN error frame.
Definition: can.c:441
int io_user_can_chan_write_t(const struct can_msg *msg, int timeout, void *arg)
The type of function invoked by a user-defined CAN channel when a CAN frame needs to be written.
Definition: can.h:50
This header file is part of the I/O library; it contains the C++ interface for the abstract CAN bus.
CanBusFlag
The CAN bus flags.
Definition: can.hpp:40
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
A CAN error frame.
Definition: err.h:28
A CAN or CAN FD format frame.
Definition: msg.h:87
Definition: ctx.c:38
This header file is part of the utilities library; it contains the time function declarations.