12#include <boost/capy/ex/execution_context.hpp>
13#include <boost/capy/ex/executor_ref.hpp>
19#include <system_error>
82 : boost::capy::execution_context::service{}, m_handler{}
94 libusb_exit(m_usb_ctx);
105 auto r = libusb_init(&ctx);
106 if (r != LIBUSB_SUCCESS)
112 libusb_exit(m_usb_ctx);
123 auto init_context (std::span<const libusb_init_option> options)
noexcept -> std::error_code
126 auto r = libusb_init_context(&ctx, options.data(), options.size());
127 if (r != LIBUSB_SUCCESS)
133 libusb_exit(m_usb_ctx);
159 requires(!std::same_as<HandlerTy, any_event_handler> &&
160 !std::same_as<HandlerTy, event_handler_ref> &&
161 std::constructible_from<HandlerTy, Args...>)
163 std::pmr::memory_resource *memres = std::pmr::get_default_resource())
166 if (m_handler.
valid())
168 m_stop.request_stop();
170 m_stop = std::stop_source{};
172 m_handler.
emplace<HandlerTy>(memres, std::forward<Args>(args)...);
182 return m_stop.get_token();
193 libusb_interrupt_event_handler(m_usb_ctx);
194 m_stop.request_stop();
212 return m_handler.
start(m_usb_ctx, m_stop.get_token());
222 m_stop.request_stop();
237 std::stop_source m_stop;
238 libusb_context *m_usb_ctx{
nullptr};
252 throw std::runtime_error{
"co_usb handler service is not present"};
Owning type-erased wrapper for EventHandler.
Concept defining an event handler for libusb.
Definition event_handler.hpp:42
Specification and implementation of EventHandler concept.
Type-erased reference to any event handler.
Definition event_handler.hpp:13
auto get_handler_service(boost::capy::executor_ref exec) -> handler_service &
Retrieve the handler_service from a given executor.
Definition handler_service.hpp:248
auto nothrow_get_handler_service(boost::capy::executor_ref exec) -> handler_service *
Retrieve the handler_service from a given executor.
Definition handler_service.hpp:263
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
Owning type-erased wrapper for EventHandler.
Definition any_event_handler.hpp:31
auto start(libusb_context *usb_ctx, std::stop_token stop) -> bool
Definition any_event_handler.hpp:112
auto emplace(std::pmr::memory_resource *memres, Args &&...args)
Definition any_event_handler.hpp:62
auto stop() -> void
Definition any_event_handler.hpp:139
auto valid() const noexcept -> bool
Definition any_event_handler.hpp:107
Execution context service that owns a libusb context and an event handler.
Definition handler_service.hpp:74
auto init_context() noexcept -> std::error_code
Initializes libusb context without options.
Definition handler_service.hpp:102
auto get_token() const noexcept -> std::stop_token
Get the stop token used for coordinating shutdown.
Definition handler_service.hpp:180
handler_service(boost::capy::execution_context &exec_ctx)
Construct the handler service.
Definition handler_service.hpp:81
~handler_service() override
Destructor that stops the handler and releases the libusb context.
Definition handler_service.hpp:91
auto shutdown() -> void override
Shutdown hook invoked by the base service.
Definition handler_service.hpp:220
auto init_context(std::span< const libusb_init_option > options) noexcept -> std::error_code
Initializes libusb context with given options.
Definition handler_service.hpp:123
auto emplace_handler(Args &&...args, std::pmr::memory_resource *memres=std::pmr::get_default_resource()) -> event_handler_ref
Emplace a concrete event handler and return a reference wrapper.
Definition handler_service.hpp:162
auto handler() -> event_handler_ref
Get a reference to the stored event handler.
Definition handler_service.hpp:201
auto usb_context() -> libusb_context *
Access the underlying libusb context.
Definition handler_service.hpp:230
auto request_stop()
Request stop for the event handler.
Definition handler_service.hpp:191
friend struct co_usb::context
Definition handler_service.hpp:75
auto start() -> bool
Start the stored event handler.
Definition handler_service.hpp:210
Type-erased reference to any event handler.
Definition event_handler_ref.hpp:34