co_usb
Loading...
Searching...
No Matches
endpoint.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstdint>
5#include <libusb-1.0/libusb.h>
6#include <optional>
7#include <stdexcept>
8
9namespace co_usb
10{
11
13{
14 control = 0,
15 bulk,
19};
20
21enum class ep_direction
22{
23 out = 0x00,
24 in = 0x80,
25 both = 0xFF,
26};
27
38template <ep_direction Direction> struct endpoint
39{
49 requires(Direction != ep_direction::both)
50 {
51 if constexpr (Direction == ep_direction::out)
52 {
53 return {static_cast<uint8_t>(ep & ~LIBUSB_ENDPOINT_IN), iface.dev()};
54 }
55 else if constexpr (Direction == ep_direction::in)
56 {
57 return {static_cast<uint8_t>(ep | LIBUSB_ENDPOINT_IN), iface.dev()};
58 }
59 return {ep, iface.dev()};
60 }
61
63 {
64 return {ep, devh};
65 }
66
73 requires(Direction != ep_direction::both)
74 {
75 if constexpr (Direction == ep_direction::out)
76 {
78 {
79 throw std::invalid_argument{"Cannot use IN endpoint for OUT"};
80 }
81 }
82 else if constexpr (Direction == ep_direction::in)
83 {
84 if (!(ep & LIBUSB_ENDPOINT_IN))
85 {
86 throw std::invalid_argument{"Cannot use OUT endpoint for IN"};
87 }
88 }
89 return {ep, devh};
90 }
91
94 {
95 return m_ep;
96 }
97
100 {
101 return m_devh;
102 }
103
112 template <ep_direction ToDirection>
113 std::optional<endpoint<ToDirection>> as () const noexcept
115 {
116 if constexpr (ToDirection == ep_direction::in)
117 {
118 if (m_ep & LIBUSB_ENDPOINT_IN)
119 {
120 return endpoint<ep_direction::in>::make_unsafe(m_ep, m_devh);
121 }
122 return std::nullopt;
123 }
124 else if constexpr (ToDirection == ep_direction::out)
125 {
126 if (m_ep & LIBUSB_ENDPOINT_IN)
127 {
128 return std::nullopt;
129 }
130 return endpoint<ep_direction::out>::make_unsafe(m_ep, m_devh);
131 }
132 }
133
134 private:
135 template <ep_direction ToDirection>
137
139 {
140 }
141
142 private:
143 uint8_t m_ep;
144 libusb_device_handle *m_devh;
145};
146
150endpoint<ep_direction::in> ep_in(uint8_t ep, const interface &iface) noexcept;
151
155endpoint<ep_direction::out> ep_out(uint8_t ep, const interface &iface) noexcept;
156
160endpoint<ep_direction::both> ep_any(uint8_t ep, const interface &iface) noexcept;
161
162} // namespace co_usb
Definition context.hpp:14
ep_direction
Definition endpoint.hpp:22
endpoint< ep_direction::out > ep_out(uint8_t ep, const interface &iface) noexcept
Wrapper around endpoint<co_usb::ep_direction::out>::make_safe.
Definition endpoint.cpp:9
endpoint< ep_direction::both > ep_any(uint8_t ep, const interface &iface) noexcept
Creates an endpoint with an unknown direction.
Definition endpoint.cpp:15
use_service
Definition context.hpp:17
endpoint< ep_direction::in > ep_in(uint8_t ep, const interface &iface) noexcept
Wrapper around endpoint<co_usb::ep_direction::in>::make_safe.
Definition endpoint.cpp:3
endpoint_type
Definition endpoint.hpp:13
USB endpoint templated on its direction.
Definition endpoint.hpp:39
uint8_t addr() const noexcept
Definition endpoint.hpp:92
static endpoint< Direction > make_unsafe(uint8_t ep, libusb_device_handle *devh) noexcept
Definition endpoint.hpp:62
friend endpoint< ToDirection > endpoint_cast(endpoint< ep_direction::both > ep) noexcept
static endpoint< Direction > make_safe(uint8_t ep, const interface &iface) noexcept
Makes an endpoint and completes address to proper value.
Definition endpoint.hpp:48
std::optional< endpoint< ToDirection > > as() const noexcept
Safe cast from an endpoint with an unknown direction to an endpoint with a concrete direction.
Definition endpoint.hpp:113
static endpoint< Direction > make_throwing(uint8_t ep, libusb_device_handle *devh)
Creates an endpoint or throws if the address doesn't match expected direction.
Definition endpoint.hpp:72
auto * dev() const noexcept
Definition endpoint.hpp:98
RAII wrapper than claims interface on ctor and releases on dtor.
Definition interface.hpp:18