co_usb
Loading...
Searching...
No Matches
interface.hpp
Go to the documentation of this file.
1
6#pragma once
7
12#include <libusb.h>
13#include <system_error>
14
15namespace co_usb
16{
17
31{
39 ~interface () noexcept(false)
40 {
41 std::error_code ec;
42 release(ec);
43 if (ec)
44 {
45 throw std::system_error{ec};
46 }
47 }
48
49 interface(interface const &) = delete;
50 interface &operator=(interface const &) = delete;
51
52 interface (interface &&other) noexcept
53 : m_devh(other.m_devh), m_interface_num(other.m_interface_num)
54 {
55 other.m_devh = nullptr;
56 }
57
58 interface &operator=(interface &&other)
59 {
60 std::error_code ec;
61 if (this == &other)
62 {
63 return *this;
64 }
65 release(ec);
66 if (ec)
67 {
68 throw std::system_error{ec};
69 }
70 m_devh = other.m_devh;
71 m_interface_num = other.m_interface_num;
72 other.m_devh = nullptr;
73 return *this;
74 }
75
81 auto release (std::error_code &ec) noexcept -> void
82 {
83 if (!m_devh)
84 {
85 return;
86 }
87 int r = libusb_release_interface(m_devh, m_interface_num);
88 if (r != LIBUSB_SUCCESS)
89 {
90 ec = make_usb_error_code(static_cast<usb_error>(r));
91 }
92 m_devh = nullptr;
93 }
94
95 auto devh () const noexcept -> libusb_device_handle *
96 {
97 return m_devh;
98 }
99
100 auto interface_num () const noexcept -> int
101 {
102 return m_interface_num;
103 }
104
105 explicit interface (detail::DeviceHandleSource auto const &devh_src, int interface_num)
106 : m_devh(detail::device_handle_of(devh_src)), m_interface_num(interface_num)
107 {
108 }
109
110 private:
111 libusb_device_handle *m_devh{nullptr};
112 int m_interface_num;
113};
114static_assert(detail::DeviceHandleSource<interface>, "Not a proper device handle source");
115
124template <detail::ErrorProtocol<interface> ErrTy>
125auto claim_interface (detail::DeviceHandleSource auto const &devh_src, int interface_num,
126 ErrTy &&errp = as_exception()) ->
127 typename ErrTy::template return_type<interface>
128{
129
130 libusb_device_handle *devh = detail::device_handle_of(devh_src);
131
132 int r = libusb_claim_interface(devh, interface_num);
133 if (r != LIBUSB_SUCCESS)
134 {
135 return errp.template with_error<interface>(make_usb_error_code(static_cast<usb_error>(r)));
136 }
137 return errp.template with_success<interface>(devh, interface_num);
138}
139
147inline auto claim_interface (detail::DeviceHandleSource auto const &devh_src, int interface_num)
148 -> interface
149{
150 return claim_interface(devh_src, interface_num, as_exception());
151}
152
153} // namespace co_usb
A type which can provide a co_usb::detail::DeviceHandlePointer.
Definition device_handle_source.hpp:42
Utilities for creating error protocols.
auto device_handle_of(Ty &&devh) -> libusb_device_handle *
Obtains a libusb_device_handle * from a co_usb::detail::DeviceHandlePointer.
Definition device_handle_source.hpp:57
auto claim_interface(detail::DeviceHandleSource auto const &devh_src, int interface_num, ErrTy &&errp=as_exception()) -> typename ErrTy::template return_type< interface >
Claims the interface and returns a interface or reports an error per error protocol implementation.
Definition interface.hpp:125
Definition flag_type.hpp:6
usb_error
USB error enumeration.
Definition usb_error.hpp:20
std::error_code make_usb_error_code(usb_error e) noexcept
Definition usb_error.hpp:85
constexpr auto as_exception() noexcept -> detail::as_exception_t
Definition error_protocol.hpp:13
RAII wrapper for an interface.
Definition interface.hpp:31
~interface() noexcept(false)
Releases the interface.
Definition interface.hpp:39
interface & operator=(interface &&other)
Definition interface.hpp:58
interface & operator=(interface const &)=delete
interface(interface const &)=delete
auto interface_num() const noexcept -> int
Definition interface.hpp:100
auto release(std::error_code &ec) noexcept -> void
Releases the interface.
Definition interface.hpp:81
auto devh() const noexcept -> libusb_device_handle *
Definition interface.hpp:95
USB error enumeration.