如何获取 Linux 内核模块内的电池电量?
我正在尝试获取 Linux 内核模块内的电池电量(该模块是通过 modprobe 插入的)。理想情况下,我希望使用内核 API 调用来获取电池信息。我在网上搜索了解决方案,并且还探索了 Linux 内核源代码和 Michael Meskes 的程序“acpi”源代码以寻求想法。
这些是我认为可以使用的一些技术:
- 读取并解析
/proc/acpi/battery/BAT0/state
和/proc/acpi/battery/BAT0/info
- 从
/sys/class/power_supply/BAT0/charge_now
和charge_full
读取,不涉及解析。 - 如果我能弄清楚如何公开接口,我可以尝试使用来自 Linux 内核源 drivers/acpi/battery.c 的调用。我可能需要方法
acpi_battery_get_status
和acpi_battery_get_info
- 我还注意到在 drivers/acpi/sbs.c 内部有一个方法
acpi_battery_read
就在它的上方有一条评论说“驱动程序接口”。如果有人知道如何使用它,这可能是另一种方式。
我认为在内核模块内读取文件可能是一个坏主意,但我不确定这些文件如何映射到内核函数调用,所以可能没问题。
那么,你们能给我一些建议吗?
编辑:我在下面的答案中包含了我的解决方案。
I am trying to get the battery level inside a Linux kernel module (the module is inserted via modprobe). I would ideally like to use a kernel API call to get the battery information. I have searched on the web for solutions, and I have also explored Linux kernel source and the source of program "acpi" by Michael Meskes for ideas.
These are some of the techniques I think I can use:
- Read and parse
/proc/acpi/battery/BAT0/state
and/proc/acpi/battery/BAT0/info
- Read from
/sys/class/power_supply/BAT0/charge_now
andcharge_full
with no parsing involved. - I could try using the calls from Linux kernel source drivers/acpi/battery.c if I could figure out how to expose the interface. I would probably need the methods
acpi_battery_get_status
andacpi_battery_get_info
- I also noticed that inside drivers/acpi/sbs.c there's a method
acpi_battery_read
and right above it there is a comment saying "Driver Interface". This might be another way if anyone knows how to use this.
I assume that it is probably a bad idea to read files while inside a kernel module, but I am not exactly sure how those files map to kernel function calls, so it might be okay.
So, can you guys give me some suggestions/recommendations?
Edit: I included my solution in an answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个适合我的解决方案。首先确保 #include < linux/power_supply.h >
假设您知道电池的名称,此代码给出了如何获取当前电池容量的示例。
I have found a solution that works for me. First of all make sure to #include < linux/power_supply.h >
Assuming you know the name of the battery, this code gives an example of how to get current battery capacity.
查看battery.c,sbs.c,我认为您可以直接调用LKM中的接口API(acpi_battery_read,acpi_battery_get_state)。
到目前为止你尝试过吗?
Looking at the battery.c, sbs.c, I think you can call the interface API (acpi_battery_read, acpi_battery_get_state) in your LKM directly.
Did you try that so far?
我也有同样的困境! :-\
如果这是您正在做的特定于硬件的事情,您可以看看在您的特定笔记本电脑上是否可以检测到 SMBus 链路上的智能电池。如果可以的话,那么您可以从 LKM 中进行 i2c/SMBus 调用。大多数新系统(除了一些富士通笔记本电脑)都与嵌入式控制器通信,最终配置电池(我想是通过 SMBus)......
尝试安装“lm-sensors”并查看它是否检测到您的智能电池。如果是这样,您应该能够直接与电池对话(通常位于 i2c 地址 0xb)。
如果这不是您正在做的特定于硬件的事情,请忽略我所说的:)
I have the same dilemma! :-\
If this is a hardware specific thing you'r doing, you could see if on your particular laptop you can detect the smart battery on the SMBus link or not. If you can, then you can just do i2c/SMBus calls from within your LKM. Most new systems (except some Fujitsu laptops) talk to an embedded controller instead, which ends up configuring the battery (through SMBus i suppose)....
Try installing "lm-sensors" and see if it detects your smart battery. If so you should be able to talk directly to the battery (usually at i2c address 0xb).
If this isn't a hardware specific thing you're doing, then ignore what i said :)