co_usb
Loading...
Searching...
No Matches
oneshot_hotplug.hpp
Go to the documentation of this file.
1
6#pragma once
7
13#include <boost/capy/ex/this_coro.hpp>
14#include <boost/capy/io_result.hpp>
15#include <boost/capy/io_task.hpp>
16#include <libusb.h>
17#include <stop_token>
18
19namespace co_usb::hotplug
20{
21
32inline auto oneshot_hotplug (event_type events, flag_type flags, device_triplet triplet)
33 -> boost::capy::io_task<event_type, device_ref>
34{
35 auto exec = co_await boost::capy::this_coro::executor;
36 auto stop = co_await boost::capy::this_coro::stop_token;
40 auto ev_ref = srv.handler();
41 libusb_context *ctx = srv.usb_context();
42 libusb_hotplug_callback_handle handle{0};
43 std::stop_callback stop_cb{stop, [&] ()
44 {
45 if (handle == 0)
46 return;
47 libusb_hotplug_deregister_callback(ctx, handle);
48 op_res.ec = std::make_error_code(std::errc::operation_canceled);
49 res.io_env->executor.post(res.cont);
50 }};
51 co_return co_await detail::hotplug_awaitable(ctx, &handle, &op_res, &res, events.bits,
52 flags.bits, triplet.vid, triplet.pid,
53 triplet.dev_class);
54}
55
56} // namespace co_usb::hotplug
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.
Strongly typed wrappers libusb hotplug flag types (flags & events).
auto oneshot_hotplug(event_type events, flag_type flags, device_triplet triplet) -> boost::capy::io_task< event_type, device_ref >
Oneshot coroutine to get a one-time hotplug accept.
Definition oneshot_hotplug.hpp:32
Capy execution_context service wrapping a libusb event handler.
Low-level awaitable primitive for strapping hotplug callbacks to a coroutine ecosystem.
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 hotplug_awaitable.hpp:18
An aggregate struct to pass to functions requiring device information.
Definition device_triplet.hpp:21
Low-level awaitable primitive for strapping hotplug callbacks to a coroutine ecosystem.
Definition hotplug_awaitable.hpp:35