2#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
19static optional<CdcEps> get_cdc(
const usb_config_desc_t *config_desc, uint8_t intf_idx) {
20 int conf_offset, ep_offset;
23 eps.bulk_interface_number = 0xFF;
25 const auto *intf_desc = usb_parse_interface_descriptor(config_desc, intf_idx++, 0, &conf_offset);
27 ESP_LOGE(TAG,
"usb_parse_interface_descriptor failed");
30 ESP_LOGD(TAG,
"intf_desc: bInterfaceClass=%02X, bInterfaceSubClass=%02X, bInterfaceProtocol=%02X, bNumEndpoints=%d",
31 intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass, intf_desc->bInterfaceProtocol,
32 intf_desc->bNumEndpoints);
33 for (uint8_t i = 0; i != intf_desc->bNumEndpoints; i++) {
34 ep_offset = conf_offset;
35 const auto *ep = usb_parse_endpoint_descriptor_by_index(intf_desc, i, config_desc->wTotalLength, &ep_offset);
37 ESP_LOGE(TAG,
"Ran out of interfaces at %d before finding all endpoints", i);
40 ESP_LOGD(TAG,
"ep: bEndpointAddress=%02X, bmAttributes=%02X", ep->bEndpointAddress, ep->bmAttributes);
41 if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_INT) {
43 eps.interrupt_interface_number = intf_desc->bInterfaceNumber;
44 }
else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK && ep->bEndpointAddress & usb_host::USB_DIR_IN &&
45 (eps.bulk_interface_number == 0xFF || eps.bulk_interface_number == intf_desc->bInterfaceNumber)) {
47 eps.bulk_interface_number = intf_desc->bInterfaceNumber;
48 }
else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK && !(ep->bEndpointAddress & usb_host::USB_DIR_IN) &&
49 (eps.bulk_interface_number == 0xFF || eps.bulk_interface_number == intf_desc->bInterfaceNumber)) {
51 eps.bulk_interface_number = intf_desc->bInterfaceNumber;
53 ESP_LOGE(TAG,
"Unexpected endpoint attributes: %02X", ep->bmAttributes);
57 if (eps.in_ep !=
nullptr && eps.out_ep !=
nullptr && eps.notify_ep !=
nullptr)
63 const usb_config_desc_t *config_desc;
64 const usb_device_desc_t *device_desc;
66 std::vector<CdcEps> cdc_devs{};
69 if (usb_host_get_device_descriptor(dev_hdl, &device_desc) != ESP_OK) {
70 ESP_LOGE(TAG,
"get_device_descriptor failed");
73 if (usb_host_get_active_config_descriptor(dev_hdl, &config_desc) != ESP_OK) {
74 ESP_LOGE(TAG,
"get_active_config_descriptor failed");
77 if (device_desc->bDeviceClass == USB_CLASS_COMM || device_desc->bDeviceClass == USB_CLASS_VENDOR_SPEC) {
79 if (
auto eps = get_cdc(config_desc, 0)) {
80 ESP_LOGV(TAG,
"Found CDC-ACM device");
81 cdc_devs.push_back(*eps);
85 if (((device_desc->bDeviceClass == USB_CLASS_MISC) && (device_desc->bDeviceSubClass == USB_SUBCLASS_COMMON) &&
86 (device_desc->bDeviceProtocol == USB_DEVICE_PROTOCOL_IAD)) ||
87 ((device_desc->bDeviceClass == USB_CLASS_PER_INTERFACE) && (device_desc->bDeviceSubClass == USB_SUBCLASS_NULL) &&
88 (device_desc->bDeviceProtocol == USB_PROTOCOL_NULL))) {
90 const auto *this_desc =
reinterpret_cast<const usb_standard_desc_t *
>(config_desc);
92 this_desc = usb_parse_next_descriptor_of_type(this_desc, config_desc->wTotalLength,
93 USB_B_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION, &desc_offset);
96 const auto *iad_desc =
reinterpret_cast<const usb_iad_desc_t *
>(this_desc);
98 if (iad_desc->bFunctionClass == USB_CLASS_COMM && iad_desc->bFunctionSubClass == USB_CDC_SUBCLASS_ACM) {
99 ESP_LOGV(TAG,
"Found CDC-ACM device in composite device");
100 if (
auto eps = get_cdc(config_desc, iad_desc->bFirstInterface))
101 cdc_devs.push_back(*eps);
113 for (
size_t i = 0; i !=
len; i++) {
126 for (
size_t i = 0; i !=
len; i++) {
134 ESP_LOGV(TAG,
"Channel not initialised - write ignored");
142 ESP_LOGE(TAG,
"Buffer full - failed to write %d bytes",
len);
144 this->
parent_->start_output(
this);
156 ESP_LOGV(TAG,
"Channel not initialised - read ignored");
162 ESP_LOGV(TAG,
"underflow: requested %zu but returned %d, bytes",
len,
available);
166 for (
size_t i = 0; i !=
len; i++) {
169 this->
parent_->start_input(
this);
175 USBClient::dump_config();
179 " Baud Rate: %" PRIu32
" baud\n"
184 " Dummy receiver: %s",
185 channel->index_, channel->baud_rate_, channel->data_bits_, PARITY_NAMES[channel->parity_],
186 STOP_BITS_NAMES[channel->stop_bits_], YESNO(channel->debug_), YESNO(channel->dummy_receiver_));
195 ESP_LOGV(TAG,
"Transfer result: length: %u; status %X",
status.data_len,
status.error_code);
197 ESP_LOGE(TAG,
"Control transfer failed, status=%s", esp_err_to_name(
status.error_code));
200#ifdef USE_UART_DEBUGGER
208 for (
size_t i = 0; i !=
status.data_len; i++) {
217 this->
transfer_in(ep->bEndpointAddress, callback, ep->wMaxPacketSize);
228 ESP_LOGV(TAG,
"Output Transfer result: length: %u; status %X",
status.data_len,
status.error_code);
233 uint8_t data[ep->wMaxPacketSize];
236#ifdef USE_UART_DEBUGGER
241 ESP_LOGV(TAG,
"Output %d bytes started",
len);
248static void fix_mps(
const usb_ep_desc_t *ep) {
250 auto *ep_mutable =
const_cast<usb_ep_desc_t *
>(ep);
251 if (ep->wMaxPacketSize > 64) {
252 ESP_LOGW(TAG,
"Corrected MPS of EP %u from %u to 64", ep->bEndpointAddress, ep->wMaxPacketSize);
253 ep_mutable->wMaxPacketSize = 64;
259 if (cdc_devs.empty()) {
264 ESP_LOGD(TAG,
"Found %zu CDC-ACM devices", cdc_devs.size());
267 if (i == cdc_devs.size()) {
268 ESP_LOGE(TAG,
"No configuration found for channel %d", channel->index_);
272 channel->cdc_dev_ = cdc_devs[i++];
273 fix_mps(channel->cdc_dev_.in_ep);
274 fix_mps(channel->cdc_dev_.out_ep);
275 channel->initialised_ =
true;
277 usb_host_interface_claim(this->
handle_, this->
device_handle_, channel->cdc_dev_.bulk_interface_number, 0);
279 ESP_LOGE(TAG,
"usb_host_interface_claim failed: %s, channel=%d, intf=%d", esp_err_to_name(err), channel->index_,
280 channel->cdc_dev_.bulk_interface_number);
291 if (channel->cdc_dev_.in_ep !=
nullptr) {
292 usb_host_endpoint_halt(this->
device_handle_, channel->cdc_dev_.in_ep->bEndpointAddress);
293 usb_host_endpoint_flush(this->
device_handle_, channel->cdc_dev_.in_ep->bEndpointAddress);
295 if (channel->cdc_dev_.out_ep !=
nullptr) {
296 usb_host_endpoint_halt(this->
device_handle_, channel->cdc_dev_.out_ep->bEndpointAddress);
297 usb_host_endpoint_flush(this->
device_handle_, channel->cdc_dev_.out_ep->bEndpointAddress);
299 if (channel->cdc_dev_.notify_ep !=
nullptr) {
300 usb_host_endpoint_halt(this->
device_handle_, channel->cdc_dev_.notify_ep->bEndpointAddress);
301 usb_host_endpoint_flush(this->
device_handle_, channel->cdc_dev_.notify_ep->bEndpointAddress);
303 usb_host_interface_release(this->
handle_, this->
device_handle_, channel->cdc_dev_.bulk_interface_number);
304 channel->initialised_ =
false;
305 channel->input_started_ =
false;
306 channel->output_started_ =
false;
307 channel->input_buffer_.clear();
308 channel->output_buffer_.clear();
310 USBClient::on_disconnected();
315 if (!channel->initialised_)
317 channel->input_started_ =
false;
318 channel->output_started_ =
false;
void status_set_warning(const char *message=nullptr)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
void status_set_error(const char *message=nullptr)
USBUartComponent * parent_
static void log_hex(UARTDirection direction, std::vector< uint8_t > bytes, uint8_t separator)
Log the bytes as hex values, separated by the provided separator character.
usb_host_client_handle_t handle_
void transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length)
Performs a transfer input operation.
void transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length)
Performs an output transfer operation.
virtual void disconnect()
usb_device_handle_t device_handle_
size_t get_free_space() const
size_t get_available() const
bool peek_byte(uint8_t *data) override
void write_array(const uint8_t *data, size_t len) override
RingBuffer output_buffer_
bool read_array(uint8_t *data, size_t len) override
std::vector< USBUartChannel * > channels_
void start_output(USBUartChannel *channel)
void dump_config() override
void start_input(USBUartChannel *channel)
void on_disconnected() override
void on_connected() override
virtual std::vector< CdcEps > parse_descriptors(usb_device_handle_t dev_hdl)
virtual void enable_channels()
Providing packet encoding functions for exchanging data with a remote host.
const nullopt_t nullopt((nullopt_t::init()))
const usb_ep_desc_t * out_ep
const usb_ep_desc_t * in_ep