如何获取 Linux 内核模块内的电池电量?

发布于 2024-10-15 08:24:38 字数 787 浏览 3 评论 0原文

我正在尝试获取 Linux 内核模块内的电池电量(该模块是通过 modprobe 插入的)。理想情况下,我希望使用内核 API 调用来获取电池信息。我在网上搜索了解决方案,并且还探索了 Linux 内核源代码和 Michael Meskes 的程序“acpi”源代码以寻求想法。

这些是我认为可以使用的一些技术:

  1. 读取并解析 /proc/acpi/battery/BAT0/state/proc/acpi/battery/BAT0/info
  2. /sys/class/power_supply/BAT0/charge_nowcharge_full 读取,不涉及解析。
  3. 如果我能弄清楚如何公开接口,我可以尝试使用来自 Linux 内核源 drivers/acpi/battery.c 的调用。我可能需要方法 acpi_battery_get_statusacpi_battery_get_info
  4. 我还注意到在 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:

  1. Read and parse /proc/acpi/battery/BAT0/state and /proc/acpi/battery/BAT0/info
  2. Read from /sys/class/power_supply/BAT0/charge_now and charge_full with no parsing involved.
  3. 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 and acpi_battery_get_info
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天 2024-10-22 08:24:38

我找到了一个适合我的解决方案。首先确保 #include < linux/power_supply.h >

假设您知道电池的名称,此代码给出了如何获取当前电池容量的示例。

char name[]= "BAT0";
int result = 0;
struct power_supply *psy = power_supply_get_by_name(name);
union power_supply_propval chargenow, chargefull;
result = psy->get_property(psy,POWER_SUPPLY_PROP_CHARGE_NOW,&chargenow);
if(!result) {
    printk(KERN_INFO "The charge level is %d\n",chargenow.intval);
}
result = psy->get_property(psy,POWER_SUPPLY_PROP_CHARGE_FULL,&chargefull);
if(!result) {
    printk(KERN_INFO "The charge level is %d\n",chargefull.intval);
}

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.

char name[]= "BAT0";
int result = 0;
struct power_supply *psy = power_supply_get_by_name(name);
union power_supply_propval chargenow, chargefull;
result = psy->get_property(psy,POWER_SUPPLY_PROP_CHARGE_NOW,&chargenow);
if(!result) {
    printk(KERN_INFO "The charge level is %d\n",chargenow.intval);
}
result = psy->get_property(psy,POWER_SUPPLY_PROP_CHARGE_FULL,&chargefull);
if(!result) {
    printk(KERN_INFO "The charge level is %d\n",chargefull.intval);
}
疧_╮線 2024-10-22 08:24:38

查看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?

银河中√捞星星 2024-10-22 08:24:38

我也有同样的困境! :-\
如果这是您正在做的特定于硬件的事情,您可以看看在您的特定笔记本电脑上是否可以检测到 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文