SimpleKernel 1.17.0
Loading...
Searching...
No Matches
device_manager.cpp
Go to the documentation of this file.
1
5#include "device_manager.hpp"
6
7#include <cassert>
8
9#include "kernel_log.hpp"
10
12 LockGuard guard(lock_);
13
14 size_t probed = 0;
15 size_t no_driver_count = 0;
16 for (size_t i = 0; i < device_count_; ++i) {
17 auto& node = devices_[i];
18
19 const auto* drv = registry_.FindDriver(node);
20 if (drv == nullptr) {
21 ++no_driver_count;
22 continue;
23 }
24
25 if (!drv->match(node)) {
26 continue;
27 }
28
29 if (node.bound) {
30 continue;
31 }
32 node.bound = true;
33
34 drv->probe(node).or_else([&](auto&& err) {
35 klog::Err("DeviceManager: probe '{}' failed: {}", node.name,
36 err.message());
37 node.bound = false;
38 return Expected<void>{};
39 });
40
41 ++probed;
42 }
43
44 klog::Info("DeviceManager: probed {} device(s), {} skipped (no driver)",
45 probed, no_driver_count);
46 return {};
47}
48
50 assert(name != nullptr && "FindDevice: name must not be null");
51 LockGuard guard(lock_);
52 const auto it = name_index_.find(name);
53 if (it != name_index_.end()) {
54 return &devices_[it->second];
55 }
56 return std::unexpected(Error(ErrorCode::kDeviceNotFound));
57}
58
60 size_t max) -> size_t {
61 assert((out != nullptr || max == 0) &&
62 "FindDevicesByType: out must not be null when max > 0");
63 LockGuard guard(lock_);
64 size_t found = 0;
65 for (size_t i = 0; i < device_count_ && found < max; ++i) {
66 if (devices_[i].type == type) {
67 out[found++] = &devices_[i];
68 }
69 }
70 return found;
71}
auto FindDevicesByType(DeviceType type, DeviceNode **out, size_t max) -> size_t
按类型枚举设备。
auto FindDevice(const char *name) -> Expected< DeviceNode * >
根据名称查找设备。
auto ProbeAll() -> Expected< void >
匹配已注册的驱动程序并探测所有未绑定的设备。
RAII 风格的锁守卫模板类
Definition spinlock.hpp:131
DeviceType
设备分类
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
单个设备的硬件资源描述。
错误类型,用于 std::expected
Definition expected.hpp:343