co_usb
Loading...
Searching...
No Matches
device_handle.hpp
Go to the documentation of this file.
1
6#pragma once
7
14#include <boost/capy/ex/executor_ref.hpp>
15#include <libusb.h>
16#include <memory>
17#include <version>
18
19namespace co_usb
20{
21
35{
36
37 device_handle(const device_handle &) = delete;
41
42 explicit device_handle (libusb_device_handle *devh) : m_storage(devh, libusb_close)
43 {
44 }
45
46 auto get () const noexcept -> libusb_device_handle *
47 {
48 return m_storage.get();
49 }
50
51 auto reset () noexcept
52 {
53 m_storage.reset();
54 }
55
56 auto release () noexcept -> libusb_device_handle *
57 {
58 return m_storage.release();
59 }
60
61 auto close () noexcept
62 {
63 m_storage.reset();
64 }
65
66 private:
67 std::unique_ptr<libusb_device_handle, decltype(&libusb_close)> m_storage;
68};
69
78template <detail::ErrorProtocol<device_handle> ErrTy>
79auto open_device (boost::capy::executor_ref exec, device_triplet triplet, ErrTy &&errp) ->
80 typename ErrTy::template return_type<device_handle>
81{
82 libusb_context *ctx = ::co_usb::ev::detail::get_handler_service(exec).usb_context();
83 libusb_device_handle *devh = libusb_open_device_with_vid_pid(ctx, triplet.vid, triplet.pid);
84 if (!devh)
85 {
86 return errp.template with_error<device_handle>(make_usb_error_code(usb_error::unknown));
87 }
88 return errp.template with_success<device_handle>(devh);
89}
90
99inline auto open_device (boost::capy::executor_ref exec, device_triplet triplet)
100{
101 return open_device(exec, triplet, as_exception());
102}
103
112template <detail::ErrorProtocol<device_handle> ErrTy>
113auto open_device (device_ref dev_ref, ErrTy &&errp) ->
114 typename ErrTy::template return_type<device_handle>
115{
116 libusb_device_handle *devh;
117 int r = libusb_open(dev_ref.get(), &devh);
118 if (r != LIBUSB_SUCCESS)
119 {
120 return errp.template with_error<device_handle>(
121 make_usb_error_code(static_cast<usb_error>(r)));
122 }
123 return errp.template with_success<device_handle>(devh);
124}
125
133inline auto open_device (device_ref dev_ref)
134{
135 return open_device(dev_ref, as_exception());
136}
137
138} // namespace co_usb
Wrapper for nullable libusb_device that increments ref count on ctor and decrements on dtor.
An aggregate struct to pass to functions requiring device information.
Utilities for creating error protocols.
auto open_device(boost::capy::executor_ref exec, device_triplet triplet, ErrTy &&errp) -> typename ErrTy::template return_type< device_handle >
Opens a device by triplet and returns a device_handle or reports an error per error protocol implemen...
Definition device_handle.hpp:79
Capy execution_context service wrapping a libusb event handler.
auto get_handler_service(boost::capy::executor_ref exec) -> handler_service &
Retrieve the handler_service from a given executor.
Definition handler_service.hpp:248
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 around libusb_device_handle*.
Definition device_handle.hpp:35
auto release() noexcept -> libusb_device_handle *
Definition device_handle.hpp:56
device_handle(const device_handle &)=delete
device_handle & operator=(const device_handle &)=delete
device_handle(libusb_device_handle *devh)
Definition device_handle.hpp:42
device_handle & operator=(device_handle &&)=default
device_handle(device_handle &&)=default
auto close() noexcept
Definition device_handle.hpp:61
auto reset() noexcept
Definition device_handle.hpp:51
auto get() const noexcept -> libusb_device_handle *
Definition device_handle.hpp:46
Wrapper for nullable libusb_device that increments ref count on ctor and decrements on dtor.
Definition device_ref.hpp:25
An aggregate struct to pass to functions requiring device information.
Definition device_triplet.hpp:21
USB error enumeration.