Skip to content

Conversation

@finger563
Copy link
Contributor

@finger563 finger563 commented Nov 14, 2025

Description

  • Update cli component to not error if compiled with support for USB-CDC
  • Add espp::Cli::configure_stdin_stdout_usb_cdc method to configure stdin and stdout to use USB-CDC if available
  • Add sdkconfig defaults file for using usb-cdc on ESP32-S2
  • Add dependency to esp_vfs_console component, which is required for console output support on esp32-s2

Motivation and Context

  • Ensures that cli component can be used with USB-CDC support enabled
  • Allows using cli component on ESP32-S2

How has this been tested?

  • Build and run cli/example on ESP32-S2 with USB-CDC enabled

Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):

CleanShot 2025-11-14 at 10 40 49

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update
  • Hardware (schematic, board, system design) change
  • Software change

Checklist:

  • My change requires a change to the documentation.
  • I have added / updated the documentation related to this change via either README or WIKI

Software

  • I have added tests to cover my changes.
  • I have updated the .github/workflows/build.yml file to add my new test to the automated cloud build github action.
  • All new and existing tests passed.
  • My code follows the code style of this project.

Copilot AI review requested due to automatic review settings November 14, 2025 16:32
@finger563 finger563 self-assigned this Nov 14, 2025
@finger563 finger563 added enhancement New feature or request cli labels Nov 14, 2025
@github-actions
Copy link

✅Static analysis result - no issues found! ✅

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds USB CDC support to the cli component to enable its use on ESP32-S2 and other chips with native USB support.

Key Changes:

  • Added configure_stdin_stdout_usb_cdc() method for USB CDC console configuration
  • Removed the error directive that prevented compilation with CONFIG_ESP_CONSOLE_USB_CDC
  • Added esp_vfs_console component dependency for USB CDC functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
components/cli/src/cli.cpp Initializes the static console_ member variable for storing file handles
components/cli/include/cli.hpp Adds new USB CDC configuration method and removes incompatibility error
components/cli/example/sdkconfig.defaults.esp32s2 Configures ESP32-S2 example to use USB CDC console
components/cli/CMakeLists.txt Adds required esp_vfs_console component dependency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// _IONBF = no buffering
// disable buffering on stdin
setvbuf(stdin, nullptr, _IONBF, 0);

Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Missing a final fflush/fsync sequence before setting configured_ = true. The configure_stdin_stdout_uart() (lines 146-147) and configure_stdin_stdout_usb_serial_jtag() (lines 180-181) functions both call fflush(stdout) and fsync(fileno(stdout)) at the end to ensure all output is written before completing configuration. Consider adding this for consistency:

fflush(stdout);
fsync(fileno(stdout));

configured_ = true;
Suggested change
fflush(stdout);
fsync(fileno(stdout));

Copilot uses AI. Check for mistakes.
Comment on lines +203 to +209
// redirect stdin, stdout, stderr to the USB CDC interface
console_.in = freopen(dev_name.data(), "r", stdin);
console_.out = freopen(dev_name.data(), "w", stdout);
console_.err = freopen(dev_name.data(), "w", stderr);

esp_vfs_dev_cdcacm_register();

Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The VFS device should be registered before calling freopen on it. Currently, freopen is called on lines 204-206 before esp_vfs_dev_cdcacm_register() on line 208. This is inconsistent with the configure_stdin_stdout_vfs() function (lines 261 then 264-266) which registers the VFS first, then calls freopen. The order should be:

  1. Register the VFS device with esp_vfs_dev_cdcacm_register()
  2. Then call freopen to redirect stdin/stdout/stderr
Suggested change
// redirect stdin, stdout, stderr to the USB CDC interface
console_.in = freopen(dev_name.data(), "r", stdin);
console_.out = freopen(dev_name.data(), "w", stdout);
console_.err = freopen(dev_name.data(), "w", stderr);
esp_vfs_dev_cdcacm_register();
// Register the VFS device before redirecting streams
esp_vfs_dev_cdcacm_register();
// redirect stdin, stdout, stderr to the USB CDC interface
console_.in = freopen(dev_name.data(), "r", stdin);
console_.out = freopen(dev_name.data(), "w", stdout);
console_.err = freopen(dev_name.data(), "w", stderr);

Copilot uses AI. Check for mistakes.
fcntl(fileno(stdout), F_SETFL, 0);
fcntl(fileno(stdin), F_SETFL, 0);

// Initialize VFS & UART so we can use std::cout/cin
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "Initialize VFS & UART" is misleading as this function configures USB CDC, not UART. It should say "Initialize VFS & USB CDC" to be accurate.

Suggested change
// Initialize VFS & UART so we can use std::cout/cin
// Initialize VFS & USB CDC so we can use std::cout/cin

Copilot uses AI. Check for mistakes.
// _IOLBF = line buffering
// _IONBF = no buffering
// disable buffering on stdin
setvbuf(stdin, nullptr, _IONBF, 0);
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setvbuf call should be placed before the VFS registration and freopen calls, similar to the pattern in configure_stdin_stdout_vfs() (line 258) and configure_stdin_stdout_uart() (line 121). The current placement after freopen may not properly configure stdin's buffering mode. Move this call to after the fflush/fsync operations (around line 199-200).

Copilot uses AI. Check for mistakes.
// redirect stdin, stdout, stderr to the USB CDC interface
console_.in = freopen(dev_name.data(), "r", stdin);
console_.out = freopen(dev_name.data(), "w", stdout);
console_.err = freopen(dev_name.data(), "w", stderr);
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The freopen calls can return NULL on failure, but these return values are not checked. While configure_stdin_stdout_vfs() also doesn't check these (lines 264-266), it would be good practice to verify that the operations succeeded before proceeding. Consider adding checks like:

if (!console_.in || !console_.out || !console_.err) {
  // Handle error
}
Suggested change
console_.err = freopen(dev_name.data(), "w", stderr);
console_.err = freopen(dev_name.data(), "w", stderr);
// Check for errors in freopen calls
if (!console_.in || !console_.out || !console_.err) {
// Handle error: could not redirect stdio to USB CDC
return;
}

Copilot uses AI. Check for mistakes.
@finger563 finger563 merged commit 3329fa8 into main Nov 14, 2025
106 of 108 checks passed
@finger563 finger563 deleted the feat/cli-usb-cdc branch November 14, 2025 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants