Skip to content

Conversation

@MDLZCOOL
Copy link
Contributor

@MDLZCOOL MDLZCOOL commented Jan 5, 2026

Includes:

  1. Add CONFIG_USBHOST_SERIAL_CMD in Kconfig to allow disable/enable the usbh_serial command.
  2. Wrap the usbh_serial command and g_serial_testbuffer with the macro to save resources when not needed.

Result:

  • Flash: Reduced by ~0.9KB (text) + potential stdlib removal (snprintf/atoi).
  • RAM: Reduced by 512 Bytes (NoCache Data).

Summary by CodeRabbit

  • New Features

    • Added a config option USBHOST_SERIAL_CMD to enable the USB host serial shell command (disabled by default; depends on USBHOST_SERIAL).
  • Behavior Change

    • The usbh_serial shell command is only exposed when USBHOST_SERIAL_CMD is enabled.
  • Tests / Platform

    • One test/platform path enables USBHOST_SERIAL_CMD by default for CI/testing.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 5, 2026

📝 Walkthrough

Walkthrough

Adds a new Kconfig symbol to gate the USB host serial shell command and conditions compilation and export of the command and its help function on that symbol across Kconfig variants, headers, source, platform registration, and a test config header.

Changes

Cohort / File(s) Summary
Kconfig variants
Kconfig, Kconfig.rtt, Kconfig.rttpkg
Added boolean config USBHOST_SERIAL_CMD / CONFIG_USBHOST_SERIAL_CMD with prompt "Enable usbh_serial command for shell", default n, and depends on USBHOST_SERIAL (or CONFIG_USBHOST_SERIAL in variants).
Serial implementation
class/serial/usbh_serial.c, class/serial/usbh_serial.h
Guarded usbh_serial declaration/definition and added usbh_serial_help() under #ifdef CONFIG_USBHOST_SERIAL_CMD, making the command/help compile only when enabled.
Platform export
platform/rtthread/usb_msh.c
Tightened export guard: MSH_CMD_EXPORT(usbh_serial, ...) now requires both CONFIG_USBHOST_SERIAL and CONFIG_USBHOST_SERIAL_CMD.
Test config header
tests/hpmicro/inc/usb_config.h
Added conditional macro to define CONFIG_USBHOST_SERIAL_CMD for CI/test builds when not already defined.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐇 I nibble at flags and hop through code,
A tiny switch where shell commands go.
Guarded in silence until you say "yes",
Then out I scurry, helping with the mess.
Hooray for tiny options and tidy shows!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Optimize memory usage and add shell cmd switch' is only partially related to the changeset. While the PR does add a shell command switch (the CONFIG_USBHOST_SERIAL_CMD option), the primary change is adding a configuration option to conditionally compile the usbh_serial command. The title mentions 'optimize memory usage' which is an indirect benefit, not the main change. A more direct title would focus on the config option itself rather than the secondary memory optimization benefit.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 53cedab and 762df4c.

📒 Files selected for processing (7)
  • Kconfig
  • Kconfig.rtt
  • Kconfig.rttpkg
  • class/serial/usbh_serial.c
  • class/serial/usbh_serial.h
  • platform/rtthread/usb_msh.c
  • tests/hpmicro/inc/usb_config.h
🚧 Files skipped from review as they are similar to previous changes (3)
  • platform/rtthread/usb_msh.c
  • Kconfig.rttpkg
  • Kconfig
🧰 Additional context used
🧬 Code graph analysis (1)
class/serial/usbh_serial.h (1)
class/serial/usbh_serial.c (1)
  • usbh_serial (637-705)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build_bouffalolab
  • GitHub Check: build_espressif (latest)
🔇 Additional comments (5)
tests/hpmicro/inc/usb_config.h (1)

271-274: Past issue resolved - LGTM!

The previous review identified that the condition was checking the wrong macro (CONFIG_USBHOST_BLUETOOTH_RX_SIZE), but this has been correctly fixed. The current code properly checks CONFIG_USBHOST_SERIAL_CMD before defining it, allowing the macro to be optionally overridden while providing a sensible default for CI/CD tests.

class/serial/usbh_serial.h (1)

176-178: LGTM! Consistent conditional declaration.

The function prototype is correctly guarded with CONFIG_USBHOST_SERIAL_CMD, matching the implementation in usbh_serial.c and the Kconfig options. This ensures the command API is only exposed when the feature is enabled, contributing to the reported memory savings.

Kconfig.rtt (1)

456-460: Past issue resolved - LGTM!

The previous review identified an incorrect dependency reference (USBHOST_SERIAL instead of CONFIG_USBHOST_SERIAL), but this has been properly fixed. The current configuration correctly depends on CONFIG_USBHOST_SERIAL (line 460), ensuring the shell command option is only available when the serial host driver is enabled. The default of n appropriately makes this an opt-in feature for memory-constrained deployments.

class/serial/usbh_serial.c (2)

621-633: LGTM! Well-structured help function.

The new usbh_serial_help() function provides clear usage instructions and is appropriately guarded by CONFIG_USBHOST_SERIAL_CMD. Encapsulating the help text in a dedicated function improves code organization and reusability.


635-706: LGTM! Achieves the memory optimization goals.

The conditional compilation successfully gates:

  • The 512-byte g_serial_testbuffer (accounting for the RAM reduction)
  • The usbh_serial() command implementation with its snprintf/atoi stdlib usage (accounting for the flash reduction)

This implementation cleanly achieves the PR's memory optimization objectives while maintaining code clarity.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Fix all issues with AI Agents 🤖
In @Kconfig.rtt:
- Around line 456-460: The dependency for CONFIG_USBHOST_SERIAL_CMD incorrectly
references USBHOST_SERIAL; update the depends on line to use the actual defined
symbol CONFIG_USBHOST_SERIAL so the dependency is evaluated correctly (i.e.,
change the dependency from USBHOST_SERIAL to CONFIG_USBHOST_SERIAL for
CONFIG_USBHOST_SERIAL_CMD).

In @Kconfig.rttpkg:
- Around line 455-459: The dependency for CONFIG_USBHOST_SERIAL_CMD is
incorrect: change the depends on symbol from USBHOST_SERIAL to
CONFIG_USBHOST_SERIAL so it matches the actual config symbol
(CONFIG_USBHOST_SERIAL) defined elsewhere; update the depends on line in the
CONFIG_USBHOST_SERIAL_CMD stanza accordingly to restore the correct dependency.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
class/serial/usbh_serial.c (1)

681-682: Remove redundant memcpy before snprintf.

The memcpy on line 681 is immediately overwritten by the snprintf on line 682, making it redundant. The snprintf already copies argv[3] and appends "\r\n", so the memcpy serves no purpose.

🔎 Proposed fix
-    memcpy(g_serial_testbuffer, argv[3], MIN(strlen(argv[3]), sizeof(g_serial_testbuffer)));
     uint32_t len = snprintf((char *)g_serial_testbuffer, sizeof(g_serial_testbuffer), "%s\r\n", argv[3]);
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 44d92be and 527c968.

📒 Files selected for processing (6)
  • Kconfig
  • Kconfig.rtt
  • Kconfig.rttpkg
  • class/serial/usbh_serial.c
  • class/serial/usbh_serial.h
  • platform/rtthread/usb_msh.c
🧰 Additional context used
🧬 Code graph analysis (1)
class/serial/usbh_serial.h (1)
class/serial/usbh_serial.c (1)
  • usbh_serial (637-705)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build_espressif (latest)
  • GitHub Check: build_bouffalolab
🔇 Additional comments (6)
Kconfig (1)

439-443: LGTM! Configuration option properly structured.

The new USBHOST_SERIAL_CMD option is correctly defined with appropriate dependency on USBHOST_SERIAL and defaults to disabled for memory optimization.

class/serial/usbh_serial.h (1)

176-178: LGTM! API visibility correctly gated.

The function prototype is properly conditionally compiled based on CONFIG_USBHOST_SERIAL_CMD, ensuring the API is only exposed when the feature is enabled.

platform/rtthread/usb_msh.c (1)

48-51: LGTM! Command export correctly gated.

The MSH command export now correctly requires both CONFIG_USBHOST_SERIAL and CONFIG_USBHOST_SERIAL_CMD to be enabled, properly implementing the conditional feature exposure.

class/serial/usbh_serial.c (3)

621-633: LGTM! Clean conditional compilation.

The conditional compilation guard and help function are well-implemented. The help text clearly documents the command-line interface.


635-635: LGTM! Proper buffer declaration.

The test buffer is correctly declared with appropriate memory attributes for USB transfers.


637-705: Command implementation is solid (aside from the redundant memcpy).

The shell command implementation correctly handles multiple options and maintains a persistent connection using a static variable. The validity checks on lines 647-651 appropriately handle device disconnection scenarios.

@MDLZCOOL MDLZCOOL force-pushed the usbh_serial_cmd_onoff branch from 527c968 to 53cedab Compare January 5, 2026 15:52
Includes:
1. Add `CONFIG_USBHOST_SERIAL_CMD` in Kconfig to allow disable/enable the `usbh_serial` command.
2. Wrap the `usbh_serial` command and `g_serial_testbuffer` with the macro to save resources when not needed.

Result:
- Flash: Reduced by ~0.9KB (text) + potential stdlib removal (snprintf/atoi).
- RAM: Reduced by 512 Bytes (NoCache Data).
@MDLZCOOL MDLZCOOL force-pushed the usbh_serial_cmd_onoff branch from 53cedab to 762df4c Compare January 5, 2026 15:55
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Fix all issues with AI Agents 🤖
In @tests/hpmicro/inc/usb_config.h:
- Around line 271-274: The preprocessor check currently tests
CONFIG_USBHOST_BLUETOOTH_RX_SIZE (already defined earlier), making the block
unreachable so CONFIG_USBHOST_SERIAL_CMD is never set; update the condition to
check CONFIG_USBHOST_SERIAL_RX_SIZE instead (or remove the #ifndef so
CONFIG_USBHOST_SERIAL_CMD is always defined for CI/CD) so the macro
CONFIG_USBHOST_SERIAL_CMD gets defined as intended.
🧹 Nitpick comments (1)
class/serial/usbh_serial.c (1)

681-682: Remove redundant memcpy before snprintf.

Line 681 copies argv[3] into g_serial_testbuffer, but line 682 immediately overwrites the buffer with snprintf. The memcpy is unnecessary and should be removed.

🔎 Proposed fix
     } else if (strncmp(argv[2], "-w", 2) == 0 && argc >= 4) {
-        memcpy(g_serial_testbuffer, argv[3], MIN(strlen(argv[3]), sizeof(g_serial_testbuffer)));
         uint32_t len = snprintf((char *)g_serial_testbuffer, sizeof(g_serial_testbuffer), "%s\r\n", argv[3]);
         ret = usbh_serial_write(serial, g_serial_testbuffer, len);
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 527c968 and 53cedab.

📒 Files selected for processing (7)
  • Kconfig
  • Kconfig.rtt
  • Kconfig.rttpkg
  • class/serial/usbh_serial.c
  • class/serial/usbh_serial.h
  • platform/rtthread/usb_msh.c
  • tests/hpmicro/inc/usb_config.h
🚧 Files skipped from review as they are similar to previous changes (4)
  • platform/rtthread/usb_msh.c
  • Kconfig
  • class/serial/usbh_serial.h
  • Kconfig.rtt
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build_bouffalolab
  • GitHub Check: build_espressif (latest)
🔇 Additional comments (2)
Kconfig.rttpkg (1)

455-460: LGTM! Dependency is correct.

The dependency on CONFIG_USBHOST_SERIAL correctly references the symbol defined at line 412. The Kconfig syntax and structure look appropriate for gating the shell command feature.

class/serial/usbh_serial.c (1)

621-706: LGTM! Command gating is properly implemented.

The shell command and its help function are correctly wrapped with CONFIG_USBHOST_SERIAL_CMD, achieving the stated goal of making this feature optional to save flash and RAM.

@sakumisu
Copy link
Collaborator

sakumisu commented Jan 6, 2026

感觉不是特别有必要。没调用的话是不占用内存的

@MDLZCOOL
Copy link
Contributor Author

MDLZCOOL commented Jan 6, 2026

@sakumisu 同意,这个问题主要影响开启了 whole_archive 的特定构建系统,而这些平台通常 Flash 资源比较充裕,因此这个 Kconfig 开关的必要性确实不大。我先关闭这个 PR 了。

@MDLZCOOL MDLZCOOL closed this Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants