Skip to content

[components][dfs]Avoid colliding with POSIX by using the shifted 'ctr… - #11881

Merged
Rbb666 merged 1 commit into
RT-Thread:masterfrom
CXSforHPU:fix_5073
Nov 18, 2026
Merged

Rbb666 merged 1 commit into
RT-Thread:masterfrom
CXSforHPU:fix_5073

Conversation

@CXSforHPU

Copy link
Copy Markdown
Contributor

… nullness

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

关联issue:#5073

POSIX 值 原 RT 值
F_DUPFD 0 — —
F_GETFD 1 RT_DEVICE_CTRL_RESUME 0x01
F_SETFD 2 RT_DEVICE_CTRL_SUSPEND 0x02
F_GETFL 3 RT_DEVICE_CTRL_CONFIG 0x03
F_SETFL 4 RT_DEVICE_CTRL_CLOSE 0x04

F_GETFL (=3) 在数值上与 RT_DEVICE_CTRL_CONFIG (=3) 撞码,F_SETFL (=4) 与 RT_DEVICE_CTRL_CLOSE (=4) 撞码。两套 cmd 共享同一个 int 参数,POSIX 路径下(fcntl → dfs_file_ioctl / dfs_file_fcntl → fops->ioctl)会被 driver 误解释。

你的解决方案是什么 (what is your solution)

1. include/rtdef.h:把通用 cmd 编码从"低字节 index"换成"高字节 class + 低字节 index"。

引入 RT_DEVICE_CTRL_BASE(Type) = (RT_Device_Class_##Type + 1) * 0x100(include/rtdef.h:1321)。通用 RT_DEVICE_CTRL_* 9 个宏改用 BASE(Unknown) + 1..9(include/rtdef.h:1332-1340),实测落在 0x2001..0x2009,与 POSIX 0–4 完全错开。

RT_DEVICE_CTRL_MASK=0x1f 跟 BASE 宏不兼容(BASE 把 class 放高字节,MASK 只看低 5 位),且全仓 grep 零 caller,删。拆成两个 mask:

#define RT_DEVICE_CTRL_CLASS_MASK     0xff00   /* 高字节:class id */
#define RT_DEVICE_CTRL_INDEX_MASK     0x00ff   /* 低字节:类内 index */

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix_5073
    Enter PR branch fix_5073 in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix_5073 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix_5073 branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/drivers/include/drivers/dev_pin.h
  • components/drivers/include/drivers/dev_serial_v2.h
  • components/utilities/rt-link/inc/rtlink_dev.h
  • components/utilities/rt-link/src/rtlink_dev.c

📊 Current Review Status (Last Updated: 2026-11-17 17:11 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

at32f415-start

  • ROM: .text +24 B (+0.0%, 65,548 B / 262,144 B, total: 25% used)

esp32-c3

  • iram0_2_seg: .flash.text +24 B (+0.0%, 421,904 B / 8,388,576 B, total: 5% used)

gd32105r-start

  • CODE: .text +36 B (+0.1%, 69,472 B / 262,144 B, total: 27% used)

hc32f334

  • FLASH: .text +28 B (+0.0%, 99,720 B / 131,072 B, total: 76% used)

hpmicro-hpm5100evk

  • ILM: .fast +32 B (+0.1%, 42,704 B / 262,144 B, total: 16% used)
  • XPI0: .fast +32 B, .text -104 B (-0.1%, 102,908 B / 1,048,576 B, total: 10% used)

hpmicro-hpm5301evklite

  • XPI0: .text -136 B (-0.2%, 76,652 B / 1,048,576 B, total: 7% used)

infineon-psoc6

  • flash: .text +28 B (+0.0%, 116,320 B / 262,144 B, total: 44% used)

loongson-ls1cdev

  • Code: .text -1,112 B (-0.3%, 362,868 B)

nuvoton-m487

  • CODE: .text +32 B (+0.0%, 356,984 B / 524,288 B, total: 68% used)

raspberry-pico-rp2040

  • FLASH: .rodata -8 B, .text +24 B (+0.0%, 114,288 B / 2,097,152 B, total: 5% used)

renesas-ra2l1

  • FLASH: .text -120 B (-0.1%, 98,392 B / 262,144 B, total: 38% used)

simulator

  • Code: .rodata -32 B, .text +12 B (-0.0%, 2,676,664 B)

stm32f407-rt-spark

  • CODE: .text -36 B (-0.0%, 86,132 B / 1,048,576 B, total: 8% used)

stm32l475-atk-pandora-llvm

  • ROM: .text +36 B (+0.0%, 82,840 B / 524,288 B, total: 16% used)

wch-ch32v208w-r0

  • FLASH: .text +24 B (+0.0%, 160,000 B / 491,520 B, total: 33% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@Rbb666 Rbb666 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

当前两项 CI 有明确失败,请修正后再合并:

  • clang-format 22.1.8:dev_serial_v2.h:192-194、rtdef.h:1322-1323、1343-1351 的尾注对齐不符合仓库格式。
  • Doxygen:rtdef.h:1328 的 RT_Device_Class_##Type 被解析成对 Type 的显式链接,报 explicit link request to 'Type' could not be resolved。建议使用 Doxygen 代码块展示 token-paste 表达式。

日志:https://github.com/RT-Thread/rt-thread/actions/runs/41269911785/job/124740372130
https://github.com/RT-Thread/rt-thread/actions/runs/41269911855/job/124740372369

@Rbb666 Rbb666 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

确认两处由通用控制命令重编号引入的功能回归:RT-Link 的配置命令与阻塞接收标志冲突,以及 EFM32 控制回调的 8 位命令参数截断。请同步修复相关调用方后再合并,具体触发条件和建议见两条行内意见。

已提取 PR 中的原始控制函数,以最小依赖桩进行 GCC 前后对照验证;未进行真机测试。

English: Two functional regressions were confirmed: a collision with RT-Link's blocking-receive flag and command truncation in EFM32 control callbacks. Please update the affected consumers before merging. Both were reproduced with the original control functions in minimal GCC harnesses; no hardware testing was performed.

Comment thread include/rtdef.h
{
RT_DEVICE_CTRL_RESUME = RT_DEVICE_CTRL_BASE(Unknown) + 1, /**< resume device */
RT_DEVICE_CTRL_SUSPEND, /**< suspend device */
RT_DEVICE_CTRL_CONFIG, /**< configure device */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] 修复 RT-Link 的命令/标志冲突 / Fix the RT-Link command/flag collision

中文:RT_DEVICE_CTRL_CONFIG 改为 0x2003 后,与 RT_LINK_RX_BLOCKING = 0x2000 重叠。现有 rt_link_dev_control() 分别使用 cmd & RT_DEVICE_CTRL_CONFIG 和 cmd & RT_LINK_RX_BLOCKING 判断:调用 rt_device_control(dev, RT_LINK_RX_BLOCKING, RT_NULL) 会误入配置分支,返回 -RT_EINVAL;调用 rt_device_control(dev, RT_DEVICE_CTRL_CONFIG, &cfg) 又会意外清除已有的 rx_indicate,使接收回调失效并切换为阻塞接收。请同步调整 RT-Link 的命令与标志判定,避免编号重叠。

English: The new CONFIG value (0x2003) overlaps RT_LINK_RX_BLOCKING (0x2000). Since rt_link_dev_control() tests both with bitwise AND, a blocking-mode request with NULL arguments now fails with -RT_EINVAL, while an ordinary configuration request clears an existing receive callback and switches reception to blocking mode. Please update RT-Link's command/flag handling alongside this renumbering.

GCC 对照复现 / GCC reproduction:

Before: RX_BLOCKING + NULL -> ret=0, callback cleared
After:  RX_BLOCKING + NULL -> ret=-22, callback retained
Before: CONFIG + valid config -> ret=0, callback retained
After:  CONFIG + valid config -> ret=0, callback cleared

Comment thread include/rtdef.h
Comment on lines +1346 to +1347
RT_DEVICE_CTRL_RESUME = RT_DEVICE_CTRL_BASE(Unknown) + 1, /**< resume device */
RT_DEVICE_CTRL_SUSPEND, /**< suspend device */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] 同步扩宽 EFM32 控制回调的命令参数 / Widen EFM32 control callback parameters

中文:EFM32 的 ADC、USART、LEUART、I²C、比较器和定时器控制回调仍使用 rt_uint8_t cmd,例如 rt_hs_timer_control()。新 RESUME/SUSPEND = 0x2001/0x2002 无法用 8 位表示,被截断后不能匹配对应的 switch 分支,操作仍可能返回成功但硬件状态不变。bsp/efm32/dev_misc.c:69、102 还通过 RESUME 启动温度/VDD 的 ADC 转换,更新后无法启动新转换。请将受影响回调的命令参数同步改为与设备接口一致的 int。

English: Several EFM32 control callbacks still accept rt_uint8_t cmd. The new RESUME/SUSPEND values cannot fit in eight bits, making their switch cases unreachable. Calls can report success without changing peripheral state; the temperature/VDD path also relies on RESUME to start an ADC conversion. Please change the affected callbacks to use int cmd, matching the device interface.

提取原始定时器控制函数的 GCC 对照结果 / GCC result using the extracted timer control function:

Before: resume=0x1    return=0 enabled=1 suspended=0
After:  resume=0x2001 return=0 enabled=0 suspended=1

GCC also reports that the case label values exceed the command type's range.

@CXSforHPU

CXSforHPU commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

本次 CI 失败位于 others_ft32_mm32_acm32 作业的 Bsp Scons Compile 步骤,具体为 hk32/hk32f030c8-mini BSP 的 devices.soft_i2c 配置。

所有源文件编译完成后,链接阶段因 ROM 超出容量 4 字节而失败:

LINK rtthread.elf
ld: rtthread.elf section `.fini' will not fit in region `ROM'
ld: region `ROM' overflowed by 4 bytes

Memory region         Used Size  Region Size  %age Used
             ROM:       65540 B        64 KB    100.01%
             RAM:        4060 B        10 KB     39.65%

collect2: error: ld returned 1 exit status
scons: *** [rtthread.elf] Error 1

目前可确认的是该配置的固件体积超过 64 KB Flash 限制;是否由本 PR 的控制命令重编号引起,仍需在相同工具链与配置下对比基线和 PR 的 ELF/map 文件确认。该失败发生于 HK32F030C8(Cortex-M0),不是此前修复控制回调参数的 EFM32。

CI 日志:https://github.com/RT-Thread/rt-thread/actions/runs/43793773973/job/133058008395?pr=11881

@CXSforHPU

Copy link
Copy Markdown
Contributor Author

本次 CI 失败位于 others_ft32_mm32_acm32 作业的 Bsp Scons Compile 步骤,具体为 hk32/hk32f030c8-mini BSP 的 devices.soft_i2c 配置。

所有源文件编译完成后,链接阶段因 ROM 超出容量 4 字节而失败:

LINK rtthread.elf
ld: rtthread.elf section `.fini' will not fit in region `ROM'
ld: region `ROM' overflowed by 4 bytes

Memory region         Used Size  Region Size  %age Used
             ROM:       65540 B        64 KB    100.01%
             RAM:        4060 B        10 KB     39.65%

collect2: error: ld returned 1 exit status
scons: *** [rtthread.elf] Error 1

目前可确认的是该配置的固件体积超过 64 KB Flash 限制;是否由本 PR 的控制命令重编号引起,仍需在相同工具链与配置下对比基线和 PR 的 ELF/map 文件确认。该失败发生于 HK32F030C8(Cortex-M0),不是此前修复控制回调参数的 EFM32。

CI 日志:https://github.com/RT-Thread/rt-thread/actions/runs/43793773973/job/133058008395?pr=11881

通过修改 rtconfig.py的cflags

    if BUILD == 'debug':
        CFLAGS += ' -O0 -gdwarf-2 -g'
        AFLAGS += ' -gdwarf-2'
    else:
        CFLAGS += ' -O2'

更改为:

    if BUILD == 'debug':
        CFLAGS += ' -Og -gdwarf-2 -g'
        AFLAGS += ' -gdwarf-2'
    else:
        CFLAGS += ' -O2'

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

【P1】Char class 命令号冲突

RT_HW_SERIAL_CTRL_GETC 现在定义为 RT_DEVICE_CTRL_BASE(Char) + 1,与 RT_DEVICE_CTRL_CHAR_STREAM 的数值完全相同(均为 0x0101):

两者都会经 rt_device_control() 分派到同一设备的 control 回调。若驱动在同一 switch 中同时处理,会产生 duplicate case 编译错误;若只处理其中一个,则会把另一个命令误判。当前树内尚无 RT_DEVICE_CTRL_CHAR_STREAM 使用点,所以未立即暴露,但本 PR 的目的正是消除命令号冲突,建议给 RT_HW_SERIAL_CTRL_* 分配独立索引区间(例如从 BASE(Char) + 0x40 起),并在头文件注明保留区间。

其余改动(Unknown class 基址、rtlink 掩码、PIN_CONFIG 偏移、RT_DEVICE_CTRL_MASK 已无引用)核对一致。

…dev command collisions: class in high byte, index low byte.
@CXSforHPU

Copy link
Copy Markdown
Contributor Author

【P1】Char class 命令号冲突

RT_HW_SERIAL_CTRL_GETC 现在定义为 RT_DEVICE_CTRL_BASE(Char) + 1,与 RT_DEVICE_CTRL_CHAR_STREAM 的数值完全相同(均为 0x0101):

两者都会经 rt_device_control() 分派到同一设备的 control 回调。若驱动在同一 switch 中同时处理,会产生 duplicate case 编译错误;若只处理其中一个,则会把另一个命令误判。当前树内尚无 RT_DEVICE_CTRL_CHAR_STREAM 使用点,所以未立即暴露,但本 PR 的目的正是消除命令号冲突,建议给 RT_HW_SERIAL_CTRL_* 分配独立索引区间(例如从 BASE(Char) + 0x40 起),并在头文件注明保留区间。

其余改动(Unknown class 基址、rtlink 掩码、PIN_CONFIG 偏移、RT_DEVICE_CTRL_MASK 已无引用)核对一致。

已修复该 P1 冲突。

修改文件:components/drivers/include/drivers/dev_serial_v2.h:190
变更内容:

RT_HW_SERIAL_CTRL_GETC 从 RT_DEVICE_CTRL_BASE(Char) + 1 调整为 + 0x40,数值由 0x0101 变为 0x0140。
RT_HW_SERIAL_CTRL_PUTC 和 RT_HW_SERIAL_CTRL_GET_DMA_PING_BUF 顺延为 0x0141、0x0142。
增加注释,说明 Char 类 0x40-0x4f 区间保留给硬件串口控制命令。
已确认不与 RT_DEVICE_CTRL_CHAR_STREAM 的 0x0101 或 RPMSG 的 0x0153-0x0155 冲突。

@Rbb666
Rbb666 requested a review from BernardXiong November 17, 2026 10:01

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

复核 4be939ac:P1 已修复。RT_HW_SERIAL_CTRL_GETC/PUTC/GET_DMA_PING_BUF 改为 BASE(Char)+0x40/0x41/0x42(0x0140–0x0142),与 RT_DEVICE_CTRL_CHAR_STREAM(0x0101)不再重叠,并在 dev_serial_v2.h 注明 0x40–0x4f 为硬件串口保留区间。

同时核对了 general commands 迁至 BASE(Unknown)+1..9(Unknown=31,基址 0x2000)、PIN_CONFIG_* 基址、rt-link 高字节掩码,以及 efm32 控制回调 int cmd 签名;树内未发现 RT_DEVICE_CTRL_MASK 残留或窄类型控制回调,CI 全绿。

非阻塞建议:PIN_CONFIG_* 现从 BASE(Pin)+1(0x1A01)开始,已超出仍在使用的 PIN_CONFIG_END(0x7f) / PIN_CONFIG_MAX(0xff) 边界定义;当前树内没有按范围校验的消费点,建议后续统一这两个宏或补充注释,避免外部 pinctrl 代码误用。

@Rbb666
Rbb666 merged commit 629a797 into RT-Thread:master Nov 18, 2026
117 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants