Lely core libraries  2.2.5
vcan.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_VCAN_HPP_
25 #define LELY_IO2_VCAN_HPP_
26 
27 #include <lely/io2/can.hpp>
28 #include <lely/io2/clock.hpp>
29 #include <lely/io2/vcan.h>
30 
31 #include <utility>
32 
33 namespace lely {
34 namespace io {
35 
38  public:
40  VirtualCanController(io_clock_t* clock, CanBusFlag flags = CanBusFlag::MASK,
41  int nominal = 0, int data = 0,
42  CanState state = CanState::ACTIVE)
43  : CanControllerBase(io_vcan_ctrl_create(clock, static_cast<int>(flags),
44  nominal, data,
45  static_cast<int>(state))) {
46  if (!ctrl) util::throw_errc("VirtualCanController");
47  }
48 
50 
52  : CanControllerBase(other.ctrl) {
53  other.ctrl = nullptr;
54  }
55 
56  VirtualCanController& operator=(const VirtualCanController&) = delete;
57 
59  operator=(VirtualCanController&& other) noexcept {
60  using ::std::swap;
61  swap(ctrl, other.ctrl);
62  return *this;
63  }
64 
67 
69  void
71  io_vcan_ctrl_set_state(*this, static_cast<int>(state));
72  }
73 
75  void
76  write(const can_msg& msg, int timeout, ::std::error_code& ec) noexcept {
77  int errsv = get_errc();
78  set_errc(0);
79  if (!io_vcan_ctrl_write_msg(*this, &msg, timeout))
80  ec.clear();
81  else
82  ec = util::make_error_code();
83  set_errc(errsv);
84  }
85 
87  void
88  write(const can_msg& msg, int timeout = -1) {
89  ::std::error_code ec;
90  write(msg, timeout, ec);
91  if (ec) throw ::std::system_error(ec, "write");
92  }
93 
95  void
96  write(const can_err& err, int timeout, ::std::error_code& ec) noexcept {
97  int errsv = get_errc();
98  set_errc(0);
99  if (!io_vcan_ctrl_write_err(*this, &err, timeout))
100  ec.clear();
101  else
102  ec = util::make_error_code();
103  set_errc(errsv);
104  }
105 
107  void
108  write(const can_err& err, int timeout = -1) {
109  ::std::error_code ec;
110  write(err, timeout, ec);
111  if (ec) throw ::std::system_error(ec, "write");
112  }
113 };
114 
117  public:
119  VirtualCanChannel(io_ctx_t* ctx, ev_exec_t* exec, size_t rxlen = 0)
120  : CanChannelBase(io_vcan_chan_create(ctx, exec, rxlen)) {
121  if (!chan) util::throw_errc("VirtualCanChannel");
122  }
123 
124  VirtualCanChannel(const VirtualCanChannel&) = delete;
125 
126  VirtualCanChannel(VirtualCanChannel&& other) noexcept
127  : CanChannelBase(other.chan) {
128  other.chan = nullptr;
129  other.dev = nullptr;
130  }
131 
132  VirtualCanChannel& operator=(const VirtualCanChannel&) = delete;
133 
135  operator=(VirtualCanChannel&& other) noexcept {
136  using ::std::swap;
137  swap(chan, other.chan);
138  swap(dev, other.dev);
139  return *this;
140  }
141 
144 
147  get_ctrl() const noexcept {
149  }
150 
152  void
153  open(const io_can_ctrl_t* ctrl) noexcept {
154  io_vcan_chan_open(*this, ctrl);
155  }
156 
158  bool
159  is_open() const noexcept {
160  return io_vcan_chan_is_open(*this) != 0;
161  }
162 
164  void
165  close() noexcept {
166  io_vcan_chan_close(*this);
167  }
168 };
169 
170 } // namespace io
171 } // namespace lely
172 
173 #endif // !LELY_IO2_VCAN_HPP_
A reference to an abstract CAN channel.
Definition: can.hpp:429
A reference to an abstract CAN controller.
Definition: can.hpp:285
A virtual CAN channel.
Definition: vcan.hpp:116
void close() noexcept
Definition: vcan.hpp:165
void open(const io_can_ctrl_t *ctrl) noexcept
Definition: vcan.hpp:153
VirtualCanChannel(io_ctx_t *ctx, ev_exec_t *exec, size_t rxlen=0)
Definition: vcan.hpp:119
bool is_open() const noexcept
Definition: vcan.hpp:159
CanControllerBase get_ctrl() const noexcept
Definition: vcan.hpp:147
A virtual CAN controller.
Definition: vcan.hpp:37
void write(const can_msg &msg, int timeout, ::std::error_code &ec) noexcept
Definition: vcan.hpp:76
VirtualCanController(io_clock_t *clock, CanBusFlag flags=CanBusFlag::MASK, int nominal=0, int data=0, CanState state=CanState::ACTIVE)
Definition: vcan.hpp:40
void write(const can_err &err, int timeout=-1)
Definition: vcan.hpp:108
void write(const can_msg &msg, int timeout=-1)
Definition: vcan.hpp:88
void write(const can_err &err, int timeout, ::std::error_code &ec) noexcept
Definition: vcan.hpp:96
void set_state(CanState state)
Definition: vcan.hpp:70
const struct io_clock_vtbl *const io_clock_t
An abstract clock.
Definition: clock.h:36
This header file is part of the I/O library; it contains the C++ interface for the abstract clock.
CanState
The states of a CAN node, depending on the TX/RX error count.
Definition: err.hpp:33
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function.
Definition: errnum.c:947
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:957
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
const struct io_can_ctrl_vtbl *const io_can_ctrl_t
An abstract CAN controller.
Definition: can.h:56
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:35
This header file is part of the I/O library; it contains the virtual CAN bus declarations.
void io_vcan_chan_destroy(io_can_chan_t *chan)
Destroys a virtual CAN channel.
Definition: vcan.c:583
void io_vcan_ctrl_set_state(io_can_ctrl_t *ctrl, int state)
Sets the state of a virtual CAN bus: one of CAN_STATE_ACTIVE, CAN_STATE_PASSIVE, CAN_STATE_BUSOFF,...
Definition: vcan.c:379
void io_vcan_chan_close(io_can_chan_t *chan)
Closes a virtual CAN channel.
Definition: vcan.c:622
int io_vcan_ctrl_write_err(io_can_ctrl_t *ctrl, const struct can_err *err, int timeout)
Writes a CAN error frame to a all virtual CAN channels registered with a virtual CAN controller.
Definition: vcan.c:415
void io_vcan_chan_open(io_can_chan_t *chan, io_can_ctrl_t *ctrl)
Opens a virtual CAN channel by registering it with the specified virtual CAN controller.
Definition: vcan.c:608
int io_vcan_chan_is_open(const io_can_chan_t *chan)
Returns 1 if the CAN channel is open and 0 if not.
Definition: vcan.c:616
io_can_ctrl_t * io_vcan_chan_get_ctrl(const io_can_chan_t *chan)
Returns a pointer to the virtual CAN controller with which a virtual CAN channel is registered,...
Definition: vcan.c:592
int io_vcan_ctrl_write_msg(io_can_ctrl_t *ctrl, const struct can_msg *msg, int timeout)
Writes a CAN frame to a all virtual CAN channels registered with a virtual CAN controller.
Definition: vcan.c:408
io_can_chan_t * io_vcan_chan_create(io_ctx_t *ctx, ev_exec_t *exec, size_t rxlen)
Creates a new virtual CAN channel.
Definition: vcan.c:556
io_can_ctrl_t * io_vcan_ctrl_create(io_clock_t *clock, int flags, int nominal, int data, int state)
Creates a new virtual CAN controller.
Definition: vcan.c:341
void io_vcan_ctrl_destroy(io_can_ctrl_t *ctrl)
Destroys a virtual CAN controller.
Definition: vcan.c:370