co_usb
Loading...
Searching...
No Matches
device_acceptor.hpp
Go to the documentation of this file.
1#pragma once
2
5#include "co_usb/error.hpp"
6#include <boost/capy/continuation.hpp>
7#include <boost/capy/ex/io_env.hpp>
8#include <boost/capy/io_task.hpp>
9#include <functional>
10#include <libusb-1.0/libusb.h>
11#include <map>
12#include <memory_resource>
13#include <mutex>
14#include <stop_token>
15
16namespace co_usb
17{
18
33{
34 explicit device_acceptor(libusb_context *ctx, std::pmr::memory_resource *memory_resource =
35 std::pmr::get_default_resource());
36
38
43
56 boost::capy::io_task<device_ref> accept(device_triplet triplet);
57
58 private:
59 struct acceptor_awaitable;
60 friend struct acceptor_awaitable;
61
62 // internal device entry
63 struct device_state_t
64 {
65 boost::capy::io_env const *env{nullptr};
66 boost::capy::continuation cont{};
67 device_ref dev{};
68 usb_error err;
69
70 // fires on cancellation to interrupt awaitables
71 std::optional<std::stop_callback<std::function<void()>>> opt_cb;
72 };
73
74 struct triplet_comparator
75 {
76 bool operator()(const device_triplet &lhs, const device_triplet &rhs) const;
77 };
78
79 using allocator_t =
80 std::pmr::polymorphic_allocator<std::pair<const device_triplet, device_state_t>>;
81 using map_t = std::map<device_triplet, device_state_t, triplet_comparator, allocator_t>;
82
83 libusb_context *m_ctx;
85 allocator_t m_allocator;
86 std::mutex m_mutex;
87 map_t m_dev_states;
88};
89
90} // namespace co_usb
Definition context.hpp:14
usb_error
Definition error.hpp:10
use_service
Definition context.hpp:17
Internal awaitable type for device_acceptor.
Definition device_acceptor.cpp:16
Accepts devices via hotplug.
Definition device_acceptor.hpp:33
device_acceptor(device_acceptor &&)=delete
~device_acceptor()
Definition device_acceptor.cpp:185
device_acceptor & operator=(const device_acceptor &)=delete
boost::capy::io_task< device_ref > accept(device_triplet triplet)
accepts a device by its triplet
Definition device_acceptor.cpp:180
device_acceptor & operator=(device_acceptor &&)=delete
device_acceptor(const device_acceptor &)=delete
Aggregate struct to pass to functions requiring device information.
Definition device_triplet.hpp:14