1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-26 07:02:21 +01:00
This commit is contained in:
J. Nick Koston
2025-09-23 21:58:01 -05:00
parent c08c0c111a
commit d5ad9dc0fb
2 changed files with 12 additions and 12 deletions

View File

@@ -307,6 +307,14 @@ void USBClient::on_removed(usb_device_handle_t handle) {
} }
} }
// Helper to queue transfer cleanup to main loop
static void queue_transfer_cleanup(TransferRequest *trq, EventType type) {
UsbEvent event;
event.type = type;
event.data.transfer.trq = trq;
xQueueSend(trq->client->get_event_queue(), &event, portMAX_DELAY);
}
// CALLBACK CONTEXT: USB task (called from usb_host_client_handle_events in USB task) // CALLBACK CONTEXT: USB task (called from usb_host_client_handle_events in USB task)
static void control_callback(const usb_transfer_t *xfer) { static void control_callback(const usb_transfer_t *xfer) {
auto *trq = static_cast<TransferRequest *>(xfer->context); auto *trq = static_cast<TransferRequest *>(xfer->context);
@@ -322,10 +330,7 @@ static void control_callback(const usb_transfer_t *xfer) {
} }
// Queue cleanup to main loop // Queue cleanup to main loop
UsbEvent event; queue_transfer_cleanup(trq, EVENT_CONTROL_COMPLETE);
event.type = EVENT_CONTROL_COMPLETE;
event.data.transfer.trq = trq;
xQueueSend(trq->client->get_event_queue(), &event, portMAX_DELAY);
} }
TransferRequest *USBClient::get_trq_() { TransferRequest *USBClient::get_trq_() {
@@ -402,11 +407,7 @@ static void transfer_callback(usb_transfer_t *xfer) {
} }
// Queue cleanup to main loop // Queue cleanup to main loop
UsbEvent event; queue_transfer_cleanup(trq, EVENT_TRANSFER_COMPLETE);
event.type = EVENT_TRANSFER_COMPLETE;
event.data.transfer.trq = trq;
event.data.transfer.callback_executed = true;
xQueueSend(trq->client->get_event_queue(), &event, portMAX_DELAY);
} }
/** /**
* Performs a transfer input operation. * Performs a transfer input operation.

View File

@@ -266,12 +266,11 @@ void USBUartComponent::start_output(USBUartChannel *channel) {
return; return;
} }
const auto *ep = channel->cdc_dev_.out_ep; const auto *ep = channel->cdc_dev_.out_ep;
// CALLBACK CONTEXT: This lambda is stored in TransferRequest and will be executed // CALLBACK CONTEXT: This lambda is executed in USB task via transfer_callback
// in MAIN LOOP after being queued by transfer_callback in USB task
auto callback = [this, channel](const usb_host::TransferStatus &status) { auto callback = [this, channel](const usb_host::TransferStatus &status) {
ESP_LOGV(TAG, "Output Transfer result: length: %u; status %X", status.data_len, status.error_code); ESP_LOGV(TAG, "Output Transfer result: length: %u; status %X", status.data_len, status.error_code);
channel->output_started_ = false; channel->output_started_ = false;
// DEFERRED CONTEXT: Main loop (restart output in main loop) // Defer restart to main loop (defer is thread-safe)
this->defer([this, channel] { this->start_output(channel); }); this->defer([this, channel] { this->start_output(channel); });
}; };
channel->output_started_ = true; channel->output_started_ = true;