* @brief 设备管理器 — 管理所有设备节点和驱动程序。 * * @pre 调用任何方法前,内存子系统必须已完成初始化 * @post ProbeAll() 执行完成后,已绑定的设备可供使用 */
| 21 | * @post ProbeAll() 执行完成后,已绑定的设备可供使用 |
| 22 | */ |
| 23 | class DeviceManager { |
| 24 | public: |
| 25 | /** |
| 26 | * @brief 注册总线并立即枚举其设备。 |
| 27 | * |
| 28 | * @tparam B 总线类型(必须满足 Bus 概念约束) |
| 29 | * @param bus 总线实例 |
| 30 | * @return Expected<void> 成功返回 void,失败返回错误 |
| 31 | */ |
| 32 | template <Bus B> |
| 33 | auto RegisterBus(B& bus) -> Expected<void> { |
| 34 | LockGuard guard(lock_); |
| 35 | |
| 36 | if (device_count_ >= kMaxDevices) { |
| 37 | return std::unexpected(Error(ErrorCode::kOutOfMemory)); |
| 38 | } |
| 39 | |
| 40 | size_t remaining = kMaxDevices - device_count_; |
| 41 | auto result = bus.Enumerate(devices_ + device_count_, remaining); |
| 42 | if (!result.has_value()) { |
| 43 | klog::Err("DeviceManager: bus '{}' enumeration failed: {}", B::GetName(), |
| 44 | result.error().message()); |
| 45 | return std::unexpected(result.error()); |
| 46 | } |
| 47 | |
| 48 | size_t count = result.value(); |
| 49 | for (size_t i = 0; i < count; ++i) { |
| 50 | devices_[device_count_ + i].dev_id = next_dev_id_++; |
| 51 | if (!name_index_.full()) { |
| 52 | name_index_.insert( |
| 53 | {devices_[device_count_ + i].name, device_count_ + i}); |
| 54 | } |
| 55 | } |
| 56 | device_count_ += count; |
| 57 | |
| 58 | klog::Info("DeviceManager: bus '{}' enumerated {} device(s)", B::GetName(), |
| 59 | count); |
| 60 | return {}; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @brief 匹配已注册的驱动程序并探测所有未绑定的设备。 |
| 65 | * |
| 66 | * @return Expected<void> 成功返回 void,失败返回错误 |
| 67 | */ |
| 68 | [[nodiscard]] auto ProbeAll() -> Expected<void>; |
| 69 | |
| 70 | /** |
| 71 | * @brief 根据名称查找设备。 |
| 72 | * |
| 73 | * @note 只读路径 — 枚举完成后 device_count_ 和 devices_ 稳定, |
| 74 | * 并发调用者可安全使用。 |
| 75 | * @param name 设备名称 |
| 76 | * @return Expected<DeviceNode*> 设备指针,或 kDeviceNotFound |
| 77 | */ |
| 78 | [[nodiscard]] auto FindDevice(const char* name) -> Expected<DeviceNode*>; |
| 79 | |
| 80 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected