Lely core libraries  2.3.4
ixxat.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_WIN32_IXXAT_HPP_
25 #define LELY_IO2_WIN32_IXXAT_HPP_
26 
27 #include <lely/io2/win32/ixxat.h>
28 #include <lely/io2/can.hpp>
29 
30 #include <utility>
31 
32 #include <windows.h>
33 
34 namespace lely {
35 namespace io {
36 
38 class IxxatGuard {
39  public:
40  IxxatGuard() {
41  if (io_ixxat_init() == -1) util::throw_errc("IxxatGuard");
42  }
43 
44  IxxatGuard(const IxxatGuard&) = delete;
45  IxxatGuard& operator=(const IxxatGuard&) = delete;
46 
47  ~IxxatGuard() noexcept { io_ixxat_fini(); }
48 };
49 
52  public:
54  IxxatController(UINT32 dwIndex, UINT32 dwCanNo, CanBusFlag flags, int nominal,
55  int data = 0)
57  dwIndex, dwCanNo, static_cast<int>(flags), nominal, data)) {
58  if (!ctrl) util::throw_errc("IxxatController");
59  }
60 
62  IxxatController(const LUID& rLuid, UINT32 dwCanNo, CanBusFlag flags,
63  int nominal, int data = 0)
65  &rLuid, dwCanNo, static_cast<int>(flags), nominal, data)) {
66  if (!ctrl) util::throw_errc("IxxatController");
67  }
68 
69  IxxatController(const IxxatController&) = delete;
70 
71  IxxatController(IxxatController&& other) noexcept
72  : CanControllerBase(other.ctrl) {
73  other.ctrl = nullptr;
74  }
75 
76  IxxatController& operator=(const IxxatController&) = delete;
77 
79  operator=(IxxatController&& other) noexcept {
80  using ::std::swap;
81  swap(ctrl, other.ctrl);
82  return *this;
83  }
84 
87 
89  HANDLE
90  get_handle() const noexcept { return io_ixxat_ctrl_get_handle(*this); }
91 };
92 
94 class IxxatChannel : public CanChannelBase {
95  public:
97  IxxatChannel(io_ctx_t* ctx, ev_exec_t* exec, int rxtimeo = 0, int txtimeo = 0)
98  : CanChannelBase(io_ixxat_chan_create(ctx, exec, rxtimeo, txtimeo)) {
99  if (!chan) util::throw_errc("IxxatChannel");
100  }
101 
102  IxxatChannel(const IxxatChannel&) = delete;
103 
104  IxxatChannel(IxxatChannel&& other) noexcept : CanChannelBase(other.chan) {
105  other.chan = nullptr;
106  other.dev = nullptr;
107  }
108 
109  IxxatChannel& operator=(const IxxatChannel&) = delete;
110 
111  IxxatChannel&
112  operator=(IxxatChannel&& other) noexcept {
113  using ::std::swap;
114  swap(chan, other.chan);
115  swap(dev, other.dev);
116  return *this;
117  }
118 
121 
123  HANDLE
124  get_handle() const noexcept { return io_ixxat_chan_get_handle(*this); }
125 
127  void
128  open(const io_can_ctrl_t* ctrl, UINT16 wRxFifoSize, UINT16 wTxFifoSize,
129  ::std::error_code& ec) noexcept {
130  DWORD dwErrCode = GetLastError();
131  SetLastError(0);
132  if (!io_ixxat_chan_open(*this, ctrl, wRxFifoSize, wTxFifoSize))
133  ec.clear();
134  else
135  ec = util::make_error_code();
136  SetLastError(dwErrCode);
137  }
138 
140  void
141  open(const io_can_ctrl_t* ctrl, UINT16 wRxFifoSize = 0,
142  UINT16 wTxFifoSize = 0) {
143  ::std::error_code ec;
144  open(ctrl, wRxFifoSize, wTxFifoSize, ec);
145  if (ec) throw ::std::system_error(ec, "open");
146  }
147 
149  void
150  assign(HANDLE hCanChn, UINT32 dwTscClkFreq, UINT32 dwTscDivisor,
151  ::std::error_code& ec) noexcept {
152  DWORD dwErrCode = GetLastError();
153  SetLastError(0);
154  if (!io_ixxat_chan_assign(*this, hCanChn, dwTscClkFreq, dwTscDivisor))
155  ec.clear();
156  else
157  ec = util::make_error_code();
158  SetLastError(dwErrCode);
159  }
160 
162  void
163  assign(HANDLE hCanChn, UINT32 dwTscClkFreq = 0, UINT32 dwTscDivisor = 0) {
164  ::std::error_code ec;
165  assign(hCanChn, dwTscClkFreq, dwTscDivisor, ec);
166  if (ec) throw ::std::system_error(ec, "assign");
167  }
168 
170  HANDLE
171  release() noexcept { return io_ixxat_chan_release(*this); }
172 
174  bool
175  is_open() const noexcept {
176  return io_ixxat_chan_is_open(*this) != 0;
177  }
178 
180  void
181  close(::std::error_code& ec) noexcept {
182  DWORD dwErrCode = GetLastError();
183  SetLastError(0);
184  if (!io_ixxat_chan_close(*this))
185  ec.clear();
186  else
187  ec = util::make_error_code();
188  SetLastError(dwErrCode);
189  }
190 
192  void
193  close() {
194  ::std::error_code ec;
195  close(ec);
196  if (ec) throw ::std::system_error(ec, "close");
197  }
198 };
199 
200 } // namespace io
201 } // namespace lely
202 
203 #endif // !LELY_IO2_WIN32_IXXAT_HPP_
lely::io::IxxatGuard
A RAII-style wrapper around io_ixxat_init() and io_ixxat_fini().
Definition: ixxat.hpp:38
io_ixxat_ctrl_get_handle
HANDLE io_ixxat_ctrl_get_handle(const io_can_ctrl_t *ctrl)
Returns the native handle of the CAN controller.
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
lely::io::IxxatController::IxxatController
IxxatController(UINT32 dwIndex, UINT32 dwCanNo, CanBusFlag flags, int nominal, int data=0)
Definition: ixxat.hpp:54
io_ixxat_fini
void io_ixxat_fini(void)
Frees the "vcinpl.dll" or "vcinpl2.dll" library and terminates the availability of the IXXAT function...
io_ixxat_chan_create
io_can_chan_t * io_ixxat_chan_create(io_ctx_t *ctx, ev_exec_t *exec, int rxtimeo, int txtimeo)
Creates a new IXXAT CAN channel.
lely::io::IxxatChannel::close
void close(::std::error_code &ec) noexcept
Definition: ixxat.hpp:181
lely::io::IxxatController::get_handle
HANDLE get_handle() const noexcept
Definition: ixxat.hpp:90
lely::io::IxxatChannel::assign
void assign(HANDLE hCanChn, UINT32 dwTscClkFreq=0, UINT32 dwTscDivisor=0)
Definition: ixxat.hpp:163
lely::io::CanChannelBase
A reference to an abstract CAN channel.
Definition: can.hpp:430
io_ixxat_chan_is_open
int io_ixxat_chan_is_open(const io_can_chan_t *chan)
Returns 1 is the CAN channel is open and 0 if not.
lely::io::IxxatChannel::~IxxatChannel
~IxxatChannel()
Definition: ixxat.hpp:120
lely::io::CanBusFlag
CanBusFlag
The CAN bus flags.
Definition: can.hpp:40
io_ixxat_chan_get_handle
HANDLE io_ixxat_chan_get_handle(const io_can_chan_t *chan)
Returns the native handle of the CAN channel, or NULL if the channel is closed.
io_ixxat_chan_destroy
void io_ixxat_chan_destroy(io_can_chan_t *chan)
Destroys an IXXAT CAN channel.
io_ixxat_chan_release
HANDLE io_ixxat_chan_release(io_can_chan_t *chan)
Dissociates and returns the native handle from a CAN channel.
lely::io::IxxatChannel::assign
void assign(HANDLE hCanChn, UINT32 dwTscClkFreq, UINT32 dwTscDivisor, ::std::error_code &ec) noexcept
Definition: ixxat.hpp:150
lely::io::IxxatChannel::close
void close()
Definition: ixxat.hpp:193
io_ixxat_ctrl_create_from_luid
io_can_ctrl_t * io_ixxat_ctrl_create_from_luid(const LUID *lpLuid, UINT32 dwCanNo, int flags, int nominal, int data)
Creates a new IXXAT CAN controller from a locally unique identifier (LUID).
io_ixxat_ctrl_create_from_index
io_can_ctrl_t * io_ixxat_ctrl_create_from_index(UINT32 dwIndex, UINT32 dwCanNo, int flags, int nominal, int data)
Creates a new IXXAT CAN controller from a device index.
can.hpp
lely::io::IxxatChannel::IxxatChannel
IxxatChannel(io_ctx_t *ctx, ev_exec_t *exec, int rxtimeo=0, int txtimeo=0)
Definition: ixxat.hpp:97
lely::io::IxxatController::~IxxatController
~IxxatController()
Definition: ixxat.hpp:86
lely::io::IxxatController::IxxatController
IxxatController(const LUID &rLuid, UINT32 dwCanNo, CanBusFlag flags, int nominal, int data=0)
Definition: ixxat.hpp:62
lely::io::IxxatChannel::release
HANDLE release() noexcept
Definition: ixxat.hpp:171
io_ixxat_ctrl_destroy
void io_ixxat_ctrl_destroy(io_can_ctrl_t *ctrl)
Destroys an IXXAT CAN controller.
lely::io::CanControllerBase
A reference to an abstract CAN controller.
Definition: can.hpp:286
ixxat.h
io_ctx
Definition: ctx.c:38
lely::io::IxxatChannel::open
void open(const io_can_ctrl_t *ctrl, UINT16 wRxFifoSize=0, UINT16 wTxFifoSize=0)
Definition: ixxat.hpp:141
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::IxxatController
An IXXAT CAN controller.
Definition: ixxat.hpp:51
io_ixxat_init
int io_ixxat_init(void)
Loads the "vcinpl.dll" or "vcinpl2.dll" library and makes the IXXAT functions available for use.
lely::io::IxxatChannel::is_open
bool is_open() const noexcept
Definition: ixxat.hpp:175
lely::io::IxxatChannel::open
void open(const io_can_ctrl_t *ctrl, UINT16 wRxFifoSize, UINT16 wTxFifoSize, ::std::error_code &ec) noexcept
Definition: ixxat.hpp:128
io_ixxat_chan_open
int io_ixxat_chan_open(io_can_chan_t *chan, const io_can_ctrl_t *ctrl, UINT16 wRxFifoSize, UINT16 wTxFifoSize)
Opens a CAN channel.
lely::io::IxxatChannel
An IXXAT CAN channel.
Definition: ixxat.hpp:94
io_ixxat_chan_assign
int io_ixxat_chan_assign(io_can_chan_t *chan, HANDLE hCanChn, UINT32 dwTscClkFreq, UINT32 dwTscDivisor)
Assigns an existing handle to a CAN channel, and activates the channel if necessary.
io_can_ctrl_t
const struct io_can_ctrl_vtbl *const io_can_ctrl_t
An abstract CAN controller.
Definition: can.h:56
lely::io::IxxatChannel::get_handle
HANDLE get_handle() const noexcept
Definition: ixxat.hpp:124
io_ixxat_chan_close
int io_ixxat_chan_close(io_can_chan_t *chan)
Closes a CAN channel.