co_usb
Loading...
Searching...
No Matches
hotplug_awaitable.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include "boost/capy/concept/io_awaitable.hpp"
9#include "boost/capy/continuation.hpp"
10#include "boost/capy/ex/io_env.hpp"
12#include "co_usb/usb_error.hpp"
14#include <boost/capy/io_result.hpp>
15#include <system_error>
16
18{
19
35{
36 struct op_result
37 {
38 std::error_code ec;
41 };
42
44 {
45 boost::capy::io_env const *io_env = nullptr;
46 boost::capy::continuation cont;
48 };
49
50 hotplug_awaitable (libusb_context *ctx, libusb_hotplug_callback_handle *handle,
52 int pid, int dev_class)
55 {
56 }
57
61 bool await_ready () noexcept
62 {
63 return false;
64 }
65
75 std::coroutine_handle<> await_suspend (std::coroutine_handle<> h,
76 boost::capy::io_env const *env)
77 {
78 if (env->stop_token.stop_requested())
79 {
80 op_res->ec = std::make_error_code(std::errc::operation_canceled);
81 return h;
82 }
83 res->cont = {h};
84 res->io_env = env;
85 res->op_res = op_res;
86 auto r = libusb_hotplug_register_callback(
88 [] (libusb_context *ctx, libusb_device *dev, libusb_hotplug_event ev,
89 void *user_data) -> int
90 {
91 resumption_t *res = (resumption_t *)user_data;
92 res->op_res->event = event_type(ev);
93 res->op_res->dev = device_ref{dev};
94 res->io_env->executor.post(res->cont);
95 return 1;
96 },
97 res, handle);
98 if (r != LIBUSB_SUCCESS)
99 {
100 op_res->ec = make_usb_error_code(static_cast<usb_error>(r));
101 return h;
102 }
103 return std::noop_coroutine();
104 }
105
106 boost::capy::io_result<event_type, device_ref> await_resume ()
107 {
108 if (op_res->ec)
109 {
110 return {op_res->ec, {}, {}};
111 }
112 return {{}, op_res->event, op_res->dev};
113 }
114
115 libusb_context *ctx;
116 libusb_hotplug_callback_handle *handle;
120 int flags;
121 int vid;
122 int pid;
124};
125
126static_assert(boost::capy::IoAwaitable<hotplug_awaitable>, "Not an IoAwaitable");
127
128} // namespace co_usb::hotplug::detail
Wrapper for nullable libusb_device that increments ref count on ctor and decrements on dtor.
Strongly typed wrappers libusb hotplug flag types (flags & events).
detail::flag_type< libusb_hotplug_event, struct hotplug_event_t > event_type
enum type for libusb_hotplug_event
Definition flags.hpp:19
Definition hotplug_awaitable.hpp:18
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
Wrapper for nullable libusb_device that increments ref count on ctor and decrements on dtor.
Definition device_ref.hpp:25
event_type event
Definition hotplug_awaitable.hpp:39
device_ref dev
Definition hotplug_awaitable.hpp:40
std::error_code ec
Definition hotplug_awaitable.hpp:38
op_result * op_res
Definition hotplug_awaitable.hpp:47
boost::capy::io_env const * io_env
Definition hotplug_awaitable.hpp:45
boost::capy::continuation cont
Definition hotplug_awaitable.hpp:46
Low-level awaitable primitive for strapping hotplug callbacks to a coroutine ecosystem.
Definition hotplug_awaitable.hpp:35
int events
Definition hotplug_awaitable.hpp:119
int dev_class
Definition hotplug_awaitable.hpp:123
op_result * op_res
Definition hotplug_awaitable.hpp:117
libusb_context * ctx
Definition hotplug_awaitable.hpp:115
resumption_t * res
Definition hotplug_awaitable.hpp:118
int vid
Definition hotplug_awaitable.hpp:121
hotplug_awaitable(libusb_context *ctx, libusb_hotplug_callback_handle *handle, op_result *op_res, resumption_t *res, int events, int flags, int vid, int pid, int dev_class)
Definition hotplug_awaitable.hpp:50
int flags
Definition hotplug_awaitable.hpp:120
bool await_ready() noexcept
Definition hotplug_awaitable.hpp:61
libusb_hotplug_callback_handle * handle
Definition hotplug_awaitable.hpp:116
std::coroutine_handle await_suspend(std::coroutine_handle<> h, boost::capy::io_env const *env)
suspends and registers the callback
Definition hotplug_awaitable.hpp:75
int pid
Definition hotplug_awaitable.hpp:122
boost::capy::io_result< event_type, device_ref > await_resume()
Definition hotplug_awaitable.hpp:106
USB error enumeration.