Lely core libraries 2.3.4
can_net.hpp
Go to the documentation of this file.
1
24#ifndef LELY_IO2_CAN_NET_HPP_
25#define LELY_IO2_CAN_NET_HPP_
26
27#include <lely/io2/can.hpp>
28#include <lely/io2/can_net.h>
29#include <lely/io2/timer.hpp>
30#include <lely/util/mutex.hpp>
31
32#include <utility>
33
34namespace lely {
35namespace io {
36
38class CanNet : protected util::BasicLockable {
39 public:
41 explicit CanNet(ev_exec_t* exec, io_timer_t* timer, io_can_chan_t* chan,
42 ::std::size_t txlen = 0, int txtimeo = 0)
43 : net_(io_can_net_create(exec, timer, chan, txlen, txtimeo)) {
44 io_can_net_get_on_read_error_func(*this, &on_read_error_func_,
45 &on_read_error_arg_);
46 io_can_net_set_on_read_error_func(*this, &CanNet::on_read_error_, this);
47 io_can_net_get_on_queue_error_func(*this, &on_queue_error_func_,
48 &on_queue_error_arg_);
49 io_can_net_set_on_queue_error_func(*this, &CanNet::on_queue_error_, this);
50 io_can_net_get_on_write_error_func(*this, &on_write_error_func_,
51 &on_write_error_arg_);
52 io_can_net_set_on_write_error_func(*this, &CanNet::on_write_error_, this);
53 io_can_net_get_on_can_state_func(*this, &on_can_state_func_,
54 &on_can_state_arg_);
55 io_can_net_set_on_can_state_func(*this, &CanNet::on_can_state_, this);
56 io_can_net_get_on_can_error_func(*this, &on_can_error_func_,
57 &on_can_error_arg_);
58 io_can_net_set_on_can_error_func(*this, &CanNet::on_can_error_, this);
59 }
60
62 explicit CanNet(io_timer_t* timer, io_can_chan_t* chan,
63 ::std::size_t txlen = 0, int txtimeo = 0)
64 : CanNet(nullptr, timer, chan, txlen, txtimeo) {}
65
66 CanNet(const CanNet&) = delete;
67 CanNet& operator=(const CanNet&) = delete;
68
70 virtual ~CanNet() { io_can_net_destroy(*this); }
71
72 operator io_can_net_t*() const noexcept { return net_; }
73
74 operator io_tqueue_t*() const noexcept {
75 return io_can_net_get_tqueue(*this);
76 }
77
79 void
81 io_can_net_start(*this);
82 }
83
89
95
97 Clock
99 return Clock(io_can_net_get_clock(*this));
100 }
101
102 protected:
103 void
105 if (io_can_net_lock(*this) == -1) util::throw_errc("lock");
106 }
107
108 void
110 if (io_can_net_unlock(*this) == -1) util::throw_errc("unlock");
111 }
112
113 operator __can_net*() const noexcept { return io_can_net_get_net(*this); }
114
123 void
125 if (io_can_net_set_time(*this) == -1) util::throw_errc("set_time");
126 }
127
139 virtual void
140 on_read_error(::std::error_code ec, ::std::size_t errcnt) noexcept {
141 // Invoke the default callback.
142 on_read_error_func_(ec.value(), errcnt, on_read_error_arg_);
143 }
144
156 virtual void
157 on_queue_error(::std::error_code ec, ::std::size_t errcnt) noexcept {
158 // Invoke the default callback.
159 on_queue_error_func_(ec.value(), errcnt, on_queue_error_arg_);
160 }
161
173 virtual void
174 on_write_error(::std::error_code ec, ::std::size_t errcnt) noexcept {
175 // Invoke the default callback.
176 on_write_error_func_(ec.value(), errcnt, on_write_error_arg_);
177 }
178
190 virtual void
192 // Invoke the default callback.
193 on_can_state_func_(static_cast<int>(new_state), static_cast<int>(old_state),
194 on_can_state_arg_);
195 }
196
207 virtual void
209 // Invoke the default callback.
210 on_can_error_func_(static_cast<int>(error), on_can_error_arg_);
211 }
212
213 private:
214 static void
215 on_read_error_(int errc, size_t errcnt, void* arg) noexcept {
216 static_cast<CanNet*>(arg)->on_read_error(util::make_error_code(errc),
217 errcnt);
218 }
219
220 static void
221 on_queue_error_(int errc, size_t errcnt, void* arg) noexcept {
222 static_cast<CanNet*>(arg)->on_queue_error(util::make_error_code(errc),
223 errcnt);
224 }
225
226 static void
227 on_write_error_(int errc, size_t errcnt, void* arg) noexcept {
228 static_cast<CanNet*>(arg)->on_write_error(util::make_error_code(errc),
229 errcnt);
230 }
231
232 static void
233 on_can_state_(int new_state, int old_state, void* arg) noexcept {
234 static_cast<CanNet*>(arg)->on_can_state(static_cast<CanState>(new_state),
235 static_cast<CanState>(old_state));
236 }
237
238 static void
239 on_can_error_(int error, void* arg) noexcept {
240 static_cast<CanNet*>(arg)->on_can_error(static_cast<CanError>(error));
241 }
242
243 io_can_net* net_{nullptr};
244 io_can_net_on_error_func_t* on_read_error_func_{nullptr};
245 void* on_read_error_arg_{nullptr};
246 io_can_net_on_error_func_t* on_queue_error_func_{nullptr};
247 void* on_queue_error_arg_{nullptr};
248 io_can_net_on_error_func_t* on_write_error_func_{nullptr};
249 void* on_write_error_arg_{nullptr};
250 io_can_net_on_can_state_func_t* on_can_state_func_{nullptr};
251 void* on_can_state_arg_{nullptr};
252 io_can_net_on_can_error_func_t* on_can_error_func_{nullptr};
253 void* on_can_error_arg_{nullptr};
254};
255
256} // namespace io
257} // namespace lely
258
259#endif // !LELY_IO2_CAN_NET_HPP_
This header file is part of the I/O library; it contains the CAN network interface declarations.
io_clock_t * io_can_net_get_clock(const io_can_net_t *net)
Returns a pointer to the clock used by the CAN network interface.
Definition can_net.c:463
void io_can_net_set_on_read_error_func(io_can_net_t *net, io_can_net_on_error_func_t *func, void *arg)
Sets the function invoked when a new CAN frame read error occurs, or when a read operation completes ...
Definition can_net.c:497
void io_can_net_set_on_can_state_func(io_can_net_t *net, io_can_net_on_can_state_func_t *func, void *arg)
Sets the function to be invoked when a CAN bus state change is detected.
Definition can_net.c:599
io_tqueue_t * io_can_net_get_tqueue(const io_can_net_t *net)
Returns a pointer to the internal timer queue of a CAN network interface.
Definition can_net.c:471
io_ctx_t * io_can_net_get_ctx(const io_can_net_t *net)
Returns a pointer to the I/O context with which the CAN network interface is registered.
Definition can_net.c:447
void io_can_net_get_on_read_error_func(const io_can_net_t *net, io_can_net_on_error_func_t **pfunc, void **parg)
Retrieves the function invoked when a new CAN frame read error occurs, or when a read operation compl...
Definition can_net.c:479
void io_can_net_start(io_can_net_t *net)
Starts a CAN network interface and begins processing CAN frames.
Definition can_net.c:422
int io_can_net_unlock(io_can_net_t *net)
Unlocks the mutex protecting the CAN network interface.
Definition can_net.c:663
io_can_net_t * io_can_net_create(ev_exec_t *exec, io_timer_t *timer, io_can_chan_t *chan, size_t txlen, int txtimeo)
Creates a new CAN network interface.
Definition can_net.c:384
void io_can_net_set_on_queue_error_func(io_can_net_t *net, io_can_net_on_error_func_t *func, void *arg)
Sets the function invoked when a CAN frame is dropped because the transmit queue is full,...
Definition can_net.c:531
void io_can_net_get_on_write_error_func(const io_can_net_t *net, io_can_net_on_error_func_t **pfunc, void **parg)
Retrieves the function invoked when a new CAN frame write error occurs, or when a write operation com...
Definition can_net.c:547
void io_can_net_get_on_queue_error_func(const io_can_net_t *net, io_can_net_on_error_func_t **pfunc, void **parg)
Retrieves the function invoked when a CAN frame is dropped because the transmit queue is full,...
Definition can_net.c:513
void io_can_net_get_on_can_state_func(const io_can_net_t *net, io_can_net_on_can_state_func_t **pfunc, void **parg)
Retrieves the function invoked when a CAN bus state change is detected.
Definition can_net.c:581
int io_can_net_set_time(io_can_net_t *net)
Updates the CAN network time.
Definition can_net.c:686
void io_can_net_destroy(io_can_net_t *net)
Destroys a CAN network interface.
Definition can_net.c:413
ev_exec_t * io_can_net_get_exec(const io_can_net_t *net)
Returns a pointer to the executor used by the CAN network interface to execute asynchronous tasks.
Definition can_net.c:455
void io_can_net_set_on_can_error_func(io_can_net_t *net, io_can_net_on_can_error_func_t *func, void *arg)
Sets the function to be invoked when a CAN bus error is detected.
Definition can_net.c:633
void io_can_net_get_on_can_error_func(const io_can_net_t *net, io_can_net_on_can_error_func_t **pfunc, void **parg)
Retrieves the function invoked when a CAN bus error is detected.
Definition can_net.c:615
void io_can_net_on_can_state_func_t(int new_state, int old_state, void *arg)
The type of function invoked when a CAN bus state change is detected by a CAN network interface.
Definition can_net.h:72
struct __can_net * io_can_net_get_net(const io_can_net_t *net)
Returns a pointer to the internal interface of a CAN network interface.
Definition can_net.c:678
void io_can_net_on_error_func_t(int errc, size_t errcnt, void *arg)
The type of function invoked when an error occurs during a CAN network interface operations,...
Definition can_net.h:54
int io_can_net_lock(io_can_net_t *net)
Locks the mutex protecting the CAN network interface.
Definition can_net.c:649
void io_can_net_on_can_error_func_t(int error, void *arg)
The type of function invoked when a CAN bus error is detected by an CAN network interface.
Definition can_net.h:87
void io_can_net_set_on_write_error_func(io_can_net_t *net, io_can_net_on_error_func_t *func, void *arg)
Sets the function invoked when a new CAN frame write error occurs, or when a write operation complete...
Definition can_net.c:565
A CANopen value.
Definition val.hpp:42
The type of objects thrown as exceptions to report a system error with an associated error code.
Definition exception.hpp:54
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition exec.hpp:38
A CAN network interface. This class is a wrapper around io_can_net_t*.
Definition can_net.hpp:38
Clock get_clock() const noexcept
Definition can_net.hpp:98
virtual void on_queue_error(::std::error_code ec, ::std::size_t errcnt) noexcept
The function invoked when a CAN frame is dropped because the transmit queue is full,...
Definition can_net.hpp:157
void unlock() final
Releases the lock held by the execution agent. Throws no exceptions.
Definition can_net.hpp:109
void lock() final
Blocks until a lock can be obtained for the current execution agent (thread, process,...
Definition can_net.hpp:104
virtual void on_read_error(::std::error_code ec, ::std::size_t errcnt) noexcept
The function invoked when a new CAN frame read error occurs, or when a read operation completes succe...
Definition can_net.hpp:140
void set_time()
Updates the CAN network time.
Definition can_net.hpp:124
virtual void on_can_error(CanError error) noexcept
The function invoked when an error is detected on the CAN bus.
Definition can_net.hpp:208
virtual ~CanNet()
Definition can_net.hpp:70
virtual void on_write_error(::std::error_code ec, ::std::size_t errcnt) noexcept
The function invoked when a new CAN frame write error occurs, or when a write operation completes suc...
Definition can_net.hpp:174
void start() noexcept
Definition can_net.hpp:80
CanNet(io_timer_t *timer, io_can_chan_t *chan, ::std::size_t txlen=0, int txtimeo=0)
Definition can_net.hpp:62
ev::Executor get_executor() const noexcept
Definition can_net.hpp:92
virtual void on_can_state(CanState new_state, CanState old_state) noexcept
The function invoked when a CAN bus state change is detected.
Definition can_net.hpp:191
CanNet(ev_exec_t *exec, io_timer_t *timer, io_can_chan_t *chan, ::std::size_t txlen=0, int txtimeo=0)
Definition can_net.hpp:41
ContextBase get_ctx() const noexcept
Definition can_net.hpp:86
An abstract clock. This class is a wrapper around #io_clock_t*.
Definition clock.hpp:35
A refence to an I/O context. This class is a wrapper around #io_ctx_t*.
Definition ctx.hpp:49
An abstract interface conforming to the BasicLockable concept.
Definition mutex.hpp:34
CanError
The error flags of a CAN bus, which are not mutually exclusive.
Definition err.hpp:47
CanState
The states of a CAN node, depending on the TX/RX error count.
Definition err.hpp:33
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition ev.h:29
const struct io_can_chan_vtbl *const io_can_chan_t
An abstract CAN channel.
Definition can.h:59
const struct io_timer_vtbl *const io_timer_t
An abstract timer.
Definition timer.h:38
This header file is part of the I/O library; it contains the C++ interface for the abstract CAN bus.
This header file is part of the utilities library; it contains the C++ mutual exclusion helper classe...
A CAN network interface.
Definition net.c:37
The implementation of a CAN network interface.
Definition can_net.c:68
This header file is part of the I/O library; it contains the C++ interface for the abstract timer.