2#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32P4)
20static optional<CdcEps> get_cdc(
const usb_config_desc_t *config_desc, uint8_t intf_idx) {
21 int conf_offset, ep_offset;
24 eps.bulk_interface_number = 0xFF;
26 const auto *intf_desc = usb_parse_interface_descriptor(config_desc, intf_idx++, 0, &conf_offset);
28 ESP_LOGE(TAG,
"usb_parse_interface_descriptor failed");
31 ESP_LOGD(TAG,
"intf_desc: bInterfaceClass=%02X, bInterfaceSubClass=%02X, bInterfaceProtocol=%02X, bNumEndpoints=%d",
32 intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass, intf_desc->bInterfaceProtocol,
33 intf_desc->bNumEndpoints);
34 for (uint8_t i = 0; i != intf_desc->bNumEndpoints; i++) {
35 ep_offset = conf_offset;
36 const auto *ep = usb_parse_endpoint_descriptor_by_index(intf_desc, i, config_desc->wTotalLength, &ep_offset);
38 ESP_LOGE(TAG,
"Ran out of interfaces at %d before finding all endpoints", i);
41 ESP_LOGD(TAG,
"ep: bEndpointAddress=%02X, bmAttributes=%02X", ep->bEndpointAddress, ep->bmAttributes);
42 if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_INT) {
44 eps.interrupt_interface_number = intf_desc->bInterfaceNumber;
45 }
else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK && ep->bEndpointAddress & usb_host::USB_DIR_IN &&
46 (eps.bulk_interface_number == 0xFF || eps.bulk_interface_number == intf_desc->bInterfaceNumber)) {
48 eps.bulk_interface_number = intf_desc->bInterfaceNumber;
49 }
else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK && !(ep->bEndpointAddress & usb_host::USB_DIR_IN) &&
50 (eps.bulk_interface_number == 0xFF || eps.bulk_interface_number == intf_desc->bInterfaceNumber)) {
52 eps.bulk_interface_number = intf_desc->bInterfaceNumber;
54 ESP_LOGE(TAG,
"Unexpected endpoint attributes: %02X", ep->bmAttributes);
58 if (eps.in_ep !=
nullptr && eps.out_ep !=
nullptr && eps.notify_ep !=
nullptr)
64 const usb_config_desc_t *config_desc;
65 const usb_device_desc_t *device_desc;
67 std::vector<CdcEps> cdc_devs{};
70 if (usb_host_get_device_descriptor(dev_hdl, &device_desc) != ESP_OK) {
71 ESP_LOGE(TAG,
"get_device_descriptor failed");
74 if (usb_host_get_active_config_descriptor(dev_hdl, &config_desc) != ESP_OK) {
75 ESP_LOGE(TAG,
"get_active_config_descriptor failed");
78 if (device_desc->bDeviceClass == USB_CLASS_COMM || device_desc->bDeviceClass == USB_CLASS_VENDOR_SPEC) {
80 if (
auto eps = get_cdc(config_desc, 0)) {
81 ESP_LOGV(TAG,
"Found CDC-ACM device");
82 cdc_devs.push_back(*eps);
86 if (((device_desc->bDeviceClass == USB_CLASS_MISC) && (device_desc->bDeviceSubClass == USB_SUBCLASS_COMMON) &&
87 (device_desc->bDeviceProtocol == USB_DEVICE_PROTOCOL_IAD)) ||
88 ((device_desc->bDeviceClass == USB_CLASS_PER_INTERFACE) && (device_desc->bDeviceSubClass == USB_SUBCLASS_NULL) &&
89 (device_desc->bDeviceProtocol == USB_PROTOCOL_NULL))) {
91 const auto *this_desc =
reinterpret_cast<const usb_standard_desc_t *
>(config_desc);
93 this_desc = usb_parse_next_descriptor_of_type(this_desc, config_desc->wTotalLength,
94 USB_B_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION, &desc_offset);
97 const auto *iad_desc =
reinterpret_cast<const usb_iad_desc_t *
>(this_desc);
99 if (iad_desc->bFunctionClass == USB_CLASS_COMM && iad_desc->bFunctionSubClass == USB_CDC_SUBCLASS_ACM) {
100 ESP_LOGV(TAG,
"Found CDC-ACM device in composite device");
101 if (
auto eps = get_cdc(config_desc, iad_desc->bFirstInterface))
102 cdc_devs.push_back(*eps);
114 for (
size_t i = 0; i !=
len; i++) {
127 for (
size_t i = 0; i !=
len; i++) {
135 ESP_LOGV(TAG,
"Channel not initialised - write ignored");
143 ESP_LOGE(TAG,
"Buffer full - failed to write %d bytes",
len);
145 this->
parent_->start_output(
this);
157 ESP_LOGV(TAG,
"Channel not initialised - read ignored");
163 ESP_LOGV(TAG,
"underflow: requested %zu but returned %d, bytes",
len,
available);
167 for (
size_t i = 0; i !=
len; i++) {
170 this->
parent_->start_input(
this);
180 auto *channel = chunk->
channel;
182#ifdef USE_UART_DEBUGGER
183 if (channel->debug_) {
197 uint16_t dropped = this->
usb_data_queue_.get_and_reset_dropped_count();
199 ESP_LOGW(TAG,
"Dropped %u USB data chunks due to buffer overflow", dropped);
203 USBClient::dump_config();
207 " Baud Rate: %" PRIu32
" baud\n"
212 " Dummy receiver: %s",
213 channel->index_, channel->baud_rate_, channel->data_bits_, PARITY_NAMES[channel->parity_],
214 STOP_BITS_NAMES[channel->stop_bits_], YESNO(channel->debug_), YESNO(channel->dummy_receiver_));
233 auto started =
false;
234 if (!channel->
input_started_.compare_exchange_weak(started,
true))
239 ESP_LOGV(TAG,
"Transfer result: length: %u; status %X",
status.data_len,
status.error_code);
241 ESP_LOGE(TAG,
"Input transfer failed, status=%s", esp_err_to_name(
status.error_code));
250 if (chunk ==
nullptr) {
268#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
278 if (!this->
transfer_in(ep->bEndpointAddress, callback, ep->wMaxPacketSize)) {
295 ESP_LOGV(TAG,
"Output Transfer result: length: %u; status %X",
status.data_len,
status.error_code);
301 uint8_t data[ep->wMaxPacketSize];
304#ifdef USE_UART_DEBUGGER
309 ESP_LOGV(TAG,
"Output %d bytes started",
len);
316static void fix_mps(
const usb_ep_desc_t *ep) {
318 auto *ep_mutable =
const_cast<usb_ep_desc_t *
>(ep);
319 if (ep->wMaxPacketSize > 64) {
320 ESP_LOGW(TAG,
"Corrected MPS of EP 0x%02X from %u to 64",
static_cast<uint8_t
>(ep->bEndpointAddress & 0xFF),
322 ep_mutable->wMaxPacketSize = 64;
328 if (cdc_devs.empty()) {
333 ESP_LOGD(TAG,
"Found %zu CDC-ACM devices", cdc_devs.size());
336 if (i == cdc_devs.size()) {
337 ESP_LOGE(TAG,
"No configuration found for channel %d", channel->index_);
341 channel->cdc_dev_ = cdc_devs[i++];
342 fix_mps(channel->cdc_dev_.in_ep);
343 fix_mps(channel->cdc_dev_.out_ep);
344 channel->initialised_.store(
true);
346 usb_host_interface_claim(this->
handle_, this->
device_handle_, channel->cdc_dev_.bulk_interface_number, 0);
348 ESP_LOGE(TAG,
"usb_host_interface_claim failed: %s, channel=%d, intf=%d", esp_err_to_name(err), channel->index_,
349 channel->cdc_dev_.bulk_interface_number);
360 if (channel->cdc_dev_.in_ep !=
nullptr) {
361 usb_host_endpoint_halt(this->
device_handle_, channel->cdc_dev_.in_ep->bEndpointAddress);
362 usb_host_endpoint_flush(this->
device_handle_, channel->cdc_dev_.in_ep->bEndpointAddress);
364 if (channel->cdc_dev_.out_ep !=
nullptr) {
365 usb_host_endpoint_halt(this->
device_handle_, channel->cdc_dev_.out_ep->bEndpointAddress);
366 usb_host_endpoint_flush(this->
device_handle_, channel->cdc_dev_.out_ep->bEndpointAddress);
368 if (channel->cdc_dev_.notify_ep !=
nullptr) {
369 usb_host_endpoint_halt(this->
device_handle_, channel->cdc_dev_.notify_ep->bEndpointAddress);
370 usb_host_endpoint_flush(this->
device_handle_, channel->cdc_dev_.notify_ep->bEndpointAddress);
372 usb_host_interface_release(this->
handle_, this->
device_handle_, channel->cdc_dev_.bulk_interface_number);
374 channel->input_started_.store(
true);
375 channel->output_started_.store(
true);
376 channel->input_buffer_.clear();
377 channel->output_buffer_.clear();
378 channel->initialised_.store(
false);
380 USBClient::on_disconnected();
385 if (!channel->initialised_.load())
387 channel->input_started_.store(
false);
388 channel->output_started_.store(
false);
void wake_loop_threadsafe()
Wake the main event loop from a FreeRTOS task Thread-safe, can be called from task context to immedia...
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_
bool 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()
bool transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length)
Performs a transfer input operation.
usb_device_handle_t device_handle_
size_t get_free_space() const
size_t get_available() const
std::atomic< bool > input_started_
std::atomic< bool > initialised_
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::atomic< bool > output_started_
EventPool< UsbDataChunk, USB_DATA_QUEUE_SIZE > chunk_pool_
std::vector< USBUartChannel * > channels_
LockFreeQueue< UsbDataChunk, USB_DATA_QUEUE_SIZE > usb_data_queue_
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.
Application App
Global storage of Application pointer - only one Application can exist.
const nullopt_t nullopt((nullopt_t::init()))
const usb_ep_desc_t * out_ep
const usb_ep_desc_t * in_ep
uint8_t data[MAX_CHUNK_SIZE]