Lely core libraries  2.2.5
can.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_LINUX_CAN_HPP_
25 #define LELY_IO2_LINUX_CAN_HPP_
26 
27 #include <lely/io2/linux/can.h>
28 #include <lely/io2/can.hpp>
29 
30 #include <utility>
31 
32 namespace lely {
33 namespace io {
34 
37  public:
39  CanController(const char* name, ::std::size_t txlen = 0)
41  if (!ctrl) util::throw_errc("CanController");
42  }
43 
45  CanController(unsigned int index, ::std::size_t txlen = 0)
47  if (!ctrl) util::throw_errc("CanController");
48  }
49 
50  CanController(const CanController&) = delete;
51 
52  CanController(CanController&& other) noexcept
53  : CanControllerBase(other.ctrl) {
54  other.ctrl = nullptr;
55  }
56 
57  CanController& operator=(const CanController&) = delete;
58 
60  operator=(CanController&& other) noexcept {
61  using ::std::swap;
62  swap(ctrl, other.ctrl);
63  return *this;
64  }
65 
68 
69  // @see io_can_ctrl_get_name()
70  const char*
71  get_name() const noexcept {
72  return io_can_ctrl_get_name(*this);
73  }
74 
75  // @see io_can_ctrl_get_index()
76  unsigned int
77  get_index() const noexcept {
78  return io_can_ctrl_get_index(*this);
79  }
80 
81  // @see io_can_ctrl_get_flags
83  get_flags() const noexcept {
84  return static_cast<CanBusFlag>(io_can_ctrl_get_flags(*this));
85  }
86 };
87 
89 class CanChannel : public CanChannelBase {
90  public:
92  CanChannel(io_poll_t* poll, ev_exec_t* exec, ::std::size_t rxlen = 0)
93  : CanChannelBase(io_can_chan_create(poll, exec, rxlen)) {
94  if (!chan) util::throw_errc("CanChannel");
95  }
96 
97  CanChannel(const CanChannel&) = delete;
98 
99  CanChannel(CanChannel&& other) noexcept : CanChannelBase(other.chan) {
100  other.chan = nullptr;
101  other.dev = nullptr;
102  }
103 
104  CanChannel& operator=(const CanChannel&) = delete;
105 
106  CanChannel&
107  operator=(CanChannel&& other) noexcept {
108  using ::std::swap;
109  swap(chan, other.chan);
110  swap(dev, other.dev);
111  return *this;
112  }
113 
116 
118  int
119  get_handle() const noexcept {
120  return io_can_chan_get_handle(*this);
121  }
122 
124  void
125  open(const io_can_ctrl_t* ctrl, CanBusFlag flags,
126  ::std::error_code& ec) noexcept {
127  int errsv = get_errc();
128  set_errc(0);
129  if (!io_can_chan_open(*this, ctrl, static_cast<int>(flags)))
130  ec.clear();
131  else
132  ec = util::make_error_code();
133  set_errc(errsv);
134  }
135 
137  void
138  open(const io_can_ctrl_t* ctrl, CanBusFlag flags = CanBusFlag::NONE) {
139  ::std::error_code ec;
140  open(ctrl, flags, ec);
141  if (ec) throw ::std::system_error(ec, "open");
142  }
143 
145  void
146  assign(int fd, ::std::error_code& ec) noexcept {
147  int errsv = get_errc();
148  set_errc(0);
149  if (!io_can_chan_assign(*this, fd))
150  ec.clear();
151  else
152  ec = util::make_error_code();
153  set_errc(errsv);
154  }
155 
157  void
158  assign(int fd) {
159  ::std::error_code ec;
160  assign(fd, ec);
161  if (ec) throw ::std::system_error(ec, "assign");
162  }
163 
165  int
166  release() noexcept {
167  return io_can_chan_release(*this);
168  }
169 
171  bool
172  is_open() const noexcept {
173  return io_can_chan_is_open(*this) != 0;
174  }
175 
177  void
178  close(::std::error_code& ec) noexcept {
179  int errsv = get_errc();
180  set_errc(0);
181  if (!io_can_chan_close(*this))
182  ec.clear();
183  else
184  ec = util::make_error_code();
185  set_errc(errsv);
186  }
187 
189  void
190  close() {
191  ::std::error_code ec;
192  close(ec);
193  if (ec) throw ::std::system_error(ec, "close");
194  }
195 };
196 
197 } // namespace io
198 } // namespace lely
199 
200 #endif // !LELY_IO2_LINUX_CAN_HPP_
int get_handle() const noexcept
Definition: can.hpp:119
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
int io_can_chan_close(io_can_chan_t *chan)
Closes the SocketCAN file descriptor associated with a CAN channel.
Definition: can_chan.c:571
int io_can_ctrl_get_flags(const io_can_ctrl_t *ctrl)
Returns the flags specifying which CAN bus features are enabled.
Definition: can_ctrl.c:190
void assign(int fd, ::std::error_code &ec) noexcept
Definition: can.hpp:146
An I/O polling interface.
Definition: poll.c:48
unsigned int io_can_ctrl_get_index(const io_can_ctrl_t *ctrl)
Returns the interface index of a CAN controller.
Definition: can_ctrl.c:182
void close(::std::error_code &ec) noexcept
Definition: can.hpp:178
const char * io_can_ctrl_get_name(const io_can_ctrl_t *ctrl)
Returns the interface name of a CAN controller.
Definition: can_ctrl.c:174
int io_can_chan_get_handle(const io_can_chan_t *chan)
Returns the SocketCAN file descriptor associated with a CAN channel, or -1 if the channel is closed...
Definition: can_chan.c:414
int io_can_chan_is_open(const io_can_chan_t *chan)
Returns 1 if the CAN channel is open and 0 if not.
Definition: can_chan.c:565
CanBusFlag
The CAN bus flags.
Definition: can.hpp:40
void io_can_ctrl_destroy(io_can_ctrl_t *ctrl)
Destroys a CAN controller.
Definition: can_ctrl.c:165
io_can_ctrl_t * io_can_ctrl_create_from_name(const char *name, size_t txlen)
Creates a new CAN controller from an interface name.
Definition: can_ctrl.c:129
CanController(unsigned int index, ::std::size_t txlen=0)
Definition: can.hpp:45
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:957
CanChannel(io_poll_t *poll, ev_exec_t *exec, ::std::size_t rxlen=0)
Definition: can.hpp:92
int io_can_chan_release(io_can_chan_t *chan)
Dissociates and returns the SocketCAN file descriptor from a CAN channel.
Definition: can_chan.c:557
io_can_chan_t * io_can_chan_create(io_poll_t *poll, ev_exec_t *exec, size_t rxlen)
Creates a new CAN channel.
Definition: can_chan.c:378
void open(const io_can_ctrl_t *ctrl, CanBusFlag flags, ::std::error_code &ec) noexcept
Definition: can.hpp:125
io_can_ctrl_t * io_can_ctrl_create_from_index(unsigned int index, size_t txlen)
Creates a new CAN controller from an interface index.
Definition: can_ctrl.c:138
void open(const io_can_ctrl_t *ctrl, CanBusFlag flags=CanBusFlag::NONE)
Definition: can.hpp:138
bool is_open() const noexcept
Definition: can.hpp:172
int io_can_chan_assign(io_can_chan_t *chan, int fd)
Assigns an existing SocketCAN file descriptor to a CAN channel.
Definition: can_chan.c:499
A CAN channel.
Definition: can.hpp:89
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function...
Definition: errnum.c:947
This header file is part of the I/O library; it contains the CAN bus declarations for Linux...
int release() noexcept
Definition: can.hpp:166
void assign(int fd)
Definition: can.hpp:158
CanController(const char *name, ::std::size_t txlen=0)
Definition: can.hpp:39
A CAN controller.
Definition: can.hpp:36
void io_can_chan_destroy(io_can_chan_t *chan)
Destroys a CAN channel.
Definition: can_chan.c:405
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
const struct io_can_ctrl_vtbl *const io_can_ctrl_t
An abstract CAN controller.
Definition: can.h:56
A reference to an abstract CAN controller.
Definition: can.hpp:285
Definition: buf.hpp:32
This header file is part of the I/O library; it contains the C++ interface for the abstract CAN bus...
int io_can_chan_open(io_can_chan_t *chan, const io_can_ctrl_t *ctrl, int flags)
Opens a CAN channel.
Definition: can_chan.c:429
A reference to an abstract CAN channel.
Definition: can.hpp:429