Lely core libraries  2.3.4
co_can_rt.hpp
Go to the documentation of this file.
1 
25 #ifndef LELY_IO2_CO_CAN_RT_HPP_
26 #define LELY_IO2_CO_CAN_RT_HPP_
27 
28 #include <lely/io2/can_rt.hpp>
29 #include <lely/util/coroutine.hpp>
30 
31 namespace lely {
32 namespace io {
33 
39  public:
41  explicit CoCanRouterReadFrame(uint_least32_t id,
42  CanFlag flags = CanFlag::NONE) noexcept
44  id, static_cast<uint_least8_t>(flags), [](ev_task* task) noexcept {
45  auto read_msg = io_can_rt_read_msg_from_task(task);
46  auto msg = read_msg->r.msg;
47  ::std::error_code ec;
48  if (!msg) ec = util::make_error_code(read_msg->r.errc);
49  auto self = static_cast<CoCanRouterReadFrame*>(read_msg);
50  (*self)(msg, ec);
51  }) {}
52 
53  virtual ~CoCanRouterReadFrame() = default;
54 
55  operator ev_task&() & noexcept { return task; }
56 
58  ev::Executor
59  get_executor() const noexcept {
60  return ev::Executor(task.exec);
61  }
62 
72  virtual void operator()(const can_msg* msg,
73  ::std::error_code ec) noexcept = 0;
74 };
75 
81  public:
83  explicit CoCanRouterReadError() noexcept
85  auto read_err = io_can_rt_read_err_from_task(task);
86  auto err = read_err->r.err;
87  ::std::error_code ec;
88  if (!err) ec = util::make_error_code(read_err->r.errc);
89  auto self = static_cast<CoCanRouterReadError*>(read_err);
90  (*self)(err, ec);
91  }) {}
92 
93  virtual ~CoCanRouterReadError() = default;
94 
95  operator ev_task&() & noexcept { return task; }
96 
98  ev::Executor
99  get_executor() const noexcept {
100  return ev::Executor(task.exec);
101  }
102 
112  virtual void operator()(const can_err* err,
113  ::std::error_code ec) noexcept = 0;
114 };
115 
116 } // namespace io
117 } // namespace lely
118 
119 #endif // !LELY_IO2_CO_CAN_RT_HPP_
lely::io::CoCanRouterReadFrame::operator()
virtual void operator()(const can_msg *msg, ::std::error_code ec) noexcept=0
The coroutine to be executed once the read operation completes (or is canceled).
can_msg
A CAN or CAN FD format frame.
Definition: msg.h:87
lely::io::CoCanRouterReadError::operator()
virtual void operator()(const can_err *err, ::std::error_code ec) noexcept=0
The coroutine to be executed once the read operation completes (or is canceled).
coroutine.hpp
can_err
A CAN error frame.
Definition: err.h:28
io_can_rt_read_err_from_task
struct io_can_rt_read_err * io_can_rt_read_err_from_task(struct ev_task *task)
Obtains a pointer to a CAN error frame read operation from a pointer to its completion task.
Definition: can_rt.c:522
lely::io::CanFlag
CanFlag
The error flags of a CAN bus, which are not mutually exclusive.
Definition: msg.hpp:33
lely::ev::Executor
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
lely::util::Coroutine
The parent class for function objects used as stackless coroutines.
Definition: coroutine.hpp:48
lely::io::CoCanRouterReadError
A CAN error frame read operation suitable for use with a CAN frame router, with a stackless coroutine...
Definition: co_can_rt.hpp:80
can_rt.hpp
lely::io::CoCanRouterReadFrame
A CAN frame read operation suitable for use with a CAN frame router, with a stackless coroutine as th...
Definition: co_can_rt.hpp:38
IO_CAN_RT_READ_ERR_INIT
#define IO_CAN_RT_READ_ERR_INIT(func)
The static initializer for io_can_rt_read_err.
Definition: can_rt.h:111
lely::io::CoCanRouterReadError::get_executor
ev::Executor get_executor() const noexcept
Returns the executor to which the completion task is (to be) submitted.
Definition: co_can_rt.hpp:99
io_can_rt_read_msg_from_task
struct io_can_rt_read_msg * io_can_rt_read_msg_from_task(struct ev_task *task)
Obtains a pointer to a CAN frame read operation from a pointer to its completion task.
Definition: can_rt.c:516
lely::io::CoCanRouterReadError::CoCanRouterReadError
CoCanRouterReadError() noexcept
Constructs a CAN error frame read operation.
Definition: co_can_rt.hpp:83
io_can_rt_read_err::task
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the read operation.
Definition: can_rt.h:105
ev_task
An executable task.
Definition: task.h:41
IO_CAN_RT_READ_MSG_INIT
#define IO_CAN_RT_READ_MSG_INIT(id, flags, func)
The static initializer for io_can_rt_read_msg.
Definition: can_rt.h:77
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
lely::io::CoCanRouterReadFrame::get_executor
ev::Executor get_executor() const noexcept
Returns the executor to which the completion task is (to be) submitted.
Definition: co_can_rt.hpp:59
io_can_rt_read_msg::task
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the read operation.
Definition: can_rt.h:69
io_can_rt_read_msg
A CAN frame read operation suitable for use with a CAN frame router.
Definition: can_rt.h:52
ev_task::exec
ev_exec_t * exec
A pointer to the executor to which the task is (to be) submitted.
Definition: task.h:43
io_can_rt_read_err
A CAN error frame read operation suitable for use with a CAN frame router.
Definition: can_rt.h:100
io_can_rt_read_msg::flags
uint_least8_t flags
The flags of the CAN frame to be received (any combination of CAN_FLAG_IDE, CAN_FLAG_RTR,...
Definition: can_rt.h:64
lely::io::CoCanRouterReadFrame::CoCanRouterReadFrame
CoCanRouterReadFrame(uint_least32_t id, CanFlag flags=CanFlag::NONE) noexcept
Constructs a CAN frame read operation.
Definition: co_can_rt.hpp:41