co_usb
Loading...
Searching...
No Matches
driver_guard.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{
32 ~driver_guard () noexcept(false)
33 {
34 std::error_code ec;
35 attach(ec);
36 if (ec)
37 {
38 throw std::system_error{ec};
39 }
40 }
41
42 driver_guard(driver_guard const &) = delete;
44
45 driver_guard (driver_guard &&other) noexcept
46 : m_devh(other.m_devh), m_interface_num(other.m_interface_num)
47 {
48 other.m_devh = nullptr;
49 }
50
52 {
53 std::error_code ec;
54 if (this == &other)
55 {
56 return *this;
57 }
58 attach(ec);
59 if (ec)
60 {
61 throw std::system_error{ec};
62 }
63 m_devh = other.m_devh;
64 m_interface_num = other.m_interface_num;
65 other.m_devh = nullptr;
66 return *this;
67 }
68
74 auto attach (std::error_code &ec) noexcept -> void
75 {
76 if (!m_devh)
77 {
78 return;
79 }
80 int r = libusb_attach_kernel_driver(m_devh, m_interface_num);
81 if (r != LIBUSB_SUCCESS)
82 {
83 ec = make_usb_error_code(static_cast<usb_error>(r));
84 }
85 m_devh = nullptr;
86 }
87
88 auto devh () const noexcept -> libusb_device_handle *
89 {
90 return m_devh;
91 }
92
93 auto interface_num () const noexcept -> int
94 {
95 return m_interface_num;
96 }
97
98 explicit driver_guard (detail::DeviceHandleSource auto const &devh_src, int driver_guard_num)
99 : m_devh(detail::device_handle_of(devh_src)), m_interface_num(driver_guard_num)
100 {
101 }
102
103 private:
104 libusb_device_handle *m_devh{nullptr};
105 int m_interface_num;
106};
107static_assert(detail::DeviceHandleSource<driver_guard>, "Not a proper device handle source");
108
117template <detail::ErrorProtocol<driver_guard> ErrTy>
118auto detach_driver (detail::DeviceHandleSource auto const &devh_src, int interface_num,
119 ErrTy &&errp = as_exception()) ->
120 typename ErrTy::template return_type<driver_guard>
121{
122 libusb_device_handle *devh = detail::device_handle_of(devh_src);
123
124 int r = libusb_detach_kernel_driver(devh, interface_num);
125 if (r != LIBUSB_SUCCESS)
126 {
127 return errp.template with_error<driver_guard>(
128 make_usb_error_code(static_cast<usb_error>(r)));
129 }
130 return errp.template with_success<driver_guard>(devh, interface_num);
131}
132
140inline auto detach_driver (detail::DeviceHandleSource auto const &devh_src, int interface_num)
141 -> driver_guard
142{
143 return detach_driver(devh_src, interface_num, as_exception());
144}
145
146} // 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 detach_driver(detail::DeviceHandleSource auto const &devh_src, int interface_num, ErrTy &&errp=as_exception()) -> typename ErrTy::template return_type< driver_guard >
Detaches the kernel driver and returns a driver_guard or reports an error per error protocol implemen...
Definition driver_guard.hpp:118
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 detaching and reattaching kernel driver.
Definition driver_guard.hpp:31
auto devh() const noexcept -> libusb_device_handle *
Definition driver_guard.hpp:88
driver_guard(detail::DeviceHandleSource auto const &devh_src, int driver_guard_num)
Definition driver_guard.hpp:98
driver_guard & operator=(driver_guard &&other)
Definition driver_guard.hpp:51
~driver_guard() noexcept(false)
Definition driver_guard.hpp:32
auto interface_num() const noexcept -> int
Definition driver_guard.hpp:93
driver_guard(driver_guard &&other) noexcept
Definition driver_guard.hpp:45
driver_guard(driver_guard const &)=delete
driver_guard & operator=(driver_guard const &)=delete
auto attach(std::error_code &ec) noexcept -> void
Reattaches the detached kernel driver.
Definition driver_guard.hpp:74
USB error enumeration.