跳转至

ADR-049:跨平台 WiFi 接口检测与优雅降级

字段 值
状态 提议中
日期 2026-03-06
决策者 ruv
依赖于 ADR-013(功能级感知)、ADR-025(macOS CoreWLAN)
问题 #148

背景

用户在 Linux 无线 proc 文件系统不可用的环境中运行 WiFi DensePose 时,报告出现 RuntimeError: Cannot read /proc/net/wireless:

  • Docker 容器 运行在 macOS/Windows 上(检测到 Linux 内核,但没有无线子系统)
  • WSL2 未启用 USB WiFi 直通
  • 没有 WiFi 硬件的无头 Linux 服务器
  • 不支持 wireless-extensions 的嵌入式 Linux 板卡

当前架构有两层防护:

  1. ws_server.py(第 345-355 行)在实例化 LinuxWifiCollector 之前会检查 os.path.exists("/proc/net/wireless"),如果缺失则回退到 SimulatedCollector。
  2. rssi_collector.py 中 LinuxWifiCollector._validate_interface()(第 178-196 行)在 /proc/net/wireless 缺失或接口未列出时抛出硬 RuntimeError。

然而,仍存在以下缺口:

  • 直接使用:任何直接实例化 LinuxWifiCollector 的代码(在 ws_server.py 之外)都会遇到未防护的 RuntimeError,且没有回退机制。
  • 错误信息:RuntimeError 消息提示用户“改用 SimulatedCollector”,但没有说明如何操作。
  • 无自动检测:采集器选择逻辑在 ws_server.py 和 install.sh 中重复出现,且没有共享的平台检测工具。
  • 部分 /proc/net/wireless:该文件可能存在(例如内核模块已加载),但不包含任何接口,从而产生令人困惑的 “interface not found” 错误,而不是干净的回退。

决策

1. 平台感知采集器工厂

在 rssi_collector.py 中引入 create_collector() 工厂函数,封装平台检测和回退链:

def create_collector(
    preferred: str = "auto",
    interface: str = "wlan0",
    sample_rate_hz: float = 10.0,
) -> BaseCollector:
    """
    Create the best available WiFi collector for the current platform.

    Resolution order (when preferred="auto"):
      1. ESP32 CSI (if UDP port 5005 is receiving frames)
      2. Platform-native WiFi:
         - Linux: LinuxWifiCollector (requires /proc/net/wireless + active interface)
         - Windows: WindowsWifiCollector (netsh wlan)
         - macOS: MacosWifiCollector (CoreWLAN)
      3. SimulatedCollector (always available)

    Raises nothing — always returns a usable collector.
    """

2. LinuxWifiCollector 中的软验证

将 _validate_interface() 中的硬 RuntimeError 替换为一个类方法,该方法返回可用性状态而不抛出异常:

@classmethod
def is_available(cls, interface: str = "wlan0") -> tuple[bool, str]:
    """Check if Linux WiFi collection is possible. Returns (available, reason)."""
    if not os.path.exists("/proc/net/wireless"):
        return False, "/proc/net/wireless not found (Docker, WSL, or no wireless subsystem)"
    with open("/proc/net/wireless") as f:
        content = f.read()
    if interface not in content:
        names = cls._parse_interface_names(content)
        return False, f"Interface '{interface}' not in /proc/net/wireless. Available: {names}"
    return True, "ok"

现有的 _validate_interface() 继续为需要快速失败行为的直接调用者抛出 RuntimeError,但 create_collector() 使用 is_available() 进行无异常探测。

3. 结构化回退日志

当自动检测跳过某个采集器时,以 WARNING 级别记录日志,并提供可操作的上下文:

WiFi collector: LinuxWifiCollector unavailable (/proc/net/wireless not found — likely Docker/WSL).
WiFi collector: Falling back to SimulatedCollector. For real sensing, connect ESP32 nodes via UDP:5005.

4. 整合平台检测

从 ws_server.py 和 install.sh 中移除重复的平台检测逻辑。两者都应使用 create_collector()(Python)或共享的 detect_wifi_platform() shell 函数。

影响

积极影响

  • 零崩溃启动:create_collector("auto") 从不抛出异常——Docker、WSL 和无头用户会自动获得 SimulatedCollector,并附带清晰的日志消息。
  • 单一检测路径:平台逻辑集中在一处(rssi_collector.py),减少 ws_server.py、install.sh 和未来入口点之间的漂移。
  • 更好的 DX:错误消息解释采集器为什么不可用以及该怎么做(连接 ESP32、安装 WiFi 驱动等)。

消极影响

  • SimulatedCollector 可能掩盖硬件问题:拥有真实 WiFi 硬件但检测失败的用户可能在不知情的情况下运行在模拟数据上。通过 WARNING 级别日志缓解。
  • 对直接 LinuxWifiCollector 调用者的破坏性变更:将 _validate_interface() 抛出的 RuntimeError 作为信号捕获的代码需要迁移到 is_available() 或 create_collector()。这是一个小变更——目前没有已知的对外使用者。

中性影响

  • 对于现有直接调用者,_validate_interface() 的行为保持不变——这是增量式变更。

实现说明

  1. 在 archive/v1/src/sensing/rssi_collector.py 中添加 create_collector() 和 BaseCollector.is_available()
  2. 重构 ws_server.py 中的 _init_collector(),使其调用 create_collector()
  3. 更新 install.sh 中的 detect_wifi_hardware(),使用共享检测逻辑
  4. 为每个平台路径添加单元测试(模拟 /proc/net/wireless 存在/不存在)
  5. 在 issue #148 上评论修复方案

参考资料

  • 问题 #148:RuntimeError: Cannot read /proc/net/wireless
  • ADR-013:消费级设备上的功能级感知
  • ADR-025:macOS CoreWLAN WiFi 感知
  • Linux /proc/net/wireless 文档

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 wi/RuView