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 
34 namespace lely {
35 namespace io {
36 
38 class 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
80  start() noexcept {
81  io_can_net_start(*this);
82  }
83 
86  get_ctx() const noexcept {
87  return ContextBase(io_can_net_get_ctx(*this));
88  }
89 
92  get_executor() const noexcept {
93  return ev::Executor(io_can_net_get_exec(*this));
94  }
95 
97  Clock
98  get_clock() const noexcept {
99  return Clock(io_can_net_get_clock(*this));
100  }
101 
102  protected:
103  void
104  lock() final {
105  if (io_can_net_lock(*this) == -1) util::throw_errc("lock");
106  }
107 
108  void
109  unlock() final {
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
191  on_can_state(CanState new_state, CanState old_state) noexcept {
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_
io_can_net_set_on_queue_error_func
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
io_can_net_get_on_read_error_func
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
io_can_net_start
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
lely::util::BasicLockable
An abstract interface conforming to the BasicLockable concept.
Definition: mutex.hpp:34
lely::io::CanNet::~CanNet
virtual ~CanNet()
Definition: can_net.hpp:70
io_can_net_get_exec
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
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
lely::io::CanNet::set_time
void set_time()
Updates the CAN network time.
Definition: can_net.hpp:124
io_can_net_lock
int io_can_net_lock(io_can_net_t *net)
Locks the mutex protecting the CAN network interface.
Definition: can_net.c:649
io_can_net_on_can_error_func_t
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
io_can_net_set_on_can_state_func
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
lely::io::ContextBase
A refence to an I/O context. This class is a wrapper around #io_ctx_t*.
Definition: ctx.hpp:49
lely::error
The type of objects thrown as exceptions to report a system error with an associated error code.
Definition: exception.hpp:54
io_can_net_get_on_can_error_func
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
lely::io::CanNet::on_can_state
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
lely::io::CanNet::get_executor
ev::Executor get_executor() const noexcept
Definition: can_net.hpp:92
timer.hpp
lely::io::CanError
CanError
The error flags of a CAN bus, which are not mutually exclusive.
Definition: err.hpp:47
io_tqueue
Definition: tqueue.c:64
lely::io::CanState
CanState
The states of a CAN node, depending on the TX/RX error count.
Definition: err.hpp:33
io_can_net_get_on_write_error_func
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
io_can_net_get_on_can_state_func
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
lely::io::CanNet::get_clock
Clock get_clock() const noexcept
Definition: can_net.hpp:98
io_can_chan_t
const struct io_can_chan_vtbl *const io_can_chan_t
An abstract CAN channel.
Definition: can.h:59
lely::io::CanNet::lock
void lock() final
Blocks until a lock can be obtained for the current execution agent (thread, process,...
Definition: can_net.hpp:104
lely::io::CanNet::on_read_error
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
lely::io::CanNet::on_write_error
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
io_can_net_destroy
void io_can_net_destroy(io_can_net_t *net)
Destroys a CAN network interface.
Definition: can_net.c:413
lely::ev::Executor
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
can_net.h
lely::io::CanNet::on_can_error
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
lely::io::CanNet::CanNet
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
lely::io::CanNet::get_ctx
ContextBase get_ctx() const noexcept
Definition: can_net.hpp:86
can.hpp
io_can_net_get_clock
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
lely::io::CanNet::start
void start() noexcept
Definition: can_net.hpp:80
io_can_net_set_on_can_error_func
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
io_can_net_create
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
io_can_net_on_can_state_func_t
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
lely::io::Clock
An abstract clock. This class is a wrapper around #io_clock_t*.
Definition: clock.hpp:35
lely::io::CanNet::unlock
void unlock() final
Releases the lock held by the execution agent. Throws no exceptions.
Definition: can_net.hpp:109
lely::io::CanNet::on_queue_error
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
io_can_net_get_ctx
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
io_can_net_get_net
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
io_can_net
The implementation of a CAN network interface.
Definition: can_net.c:68
io_can_net_unlock
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_set_time
int io_can_net_set_time(io_can_net_t *net)
Updates the CAN network time.
Definition: can_net.c:686
io_timer_t
const struct io_timer_vtbl *const io_timer_t
An abstract timer.
Definition: timer.h:38
lely::io::CanNet::CanNet
CanNet(io_timer_t *timer, io_can_chan_t *chan, ::std::size_t txlen=0, int txtimeo=0)
Definition: can_net.hpp:62
lely::canopen::make_error_code
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
io_can_net_set_on_read_error_func
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
io_can_net_get_tqueue
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_can_net_on_error_func_t
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
lely::io::CanNet
A CAN network interface. This class is a wrapper around io_can_net_t*.
Definition: can_net.hpp:38
__can_net
A CAN network interface.
Definition: net.c:37
io_can_net_set_on_write_error_func
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
io_can_net_get_on_queue_error_func
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
mutex.hpp