如何获取 Linux 内核模块内的电池电量? [英] How can I obtain battery level inside a Linux kernel module?

查看:38
本文介绍了如何获取 Linux 内核模块内的电池电量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 Linux 内核模块内的电池电量(该模块通过 modprobe 插入).理想情况下,我想使用内核 API 调用来获取电池信息.我在网上搜索了解决方案,我还探索了 Linux 内核源代码和 Michael Meskes 编写的程序acpi"的源代码.

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. 读取并解析/proc/acpi/battery/BAT0/state/proc/acpi/battery/BAT0/info
  2. /sys/class/power_supply/BAT0/charge_nowcharge_full 读取,不涉及解析.
  3. 如果我能弄清楚如何公开接口,我可以尝试使用来自 Linux 内核源驱动程序/acpi/battery.c 的调用.我可能需要方法 acpi_battery_get_statusacpi_battery_get_info
  4. 我还注意到在drivers/acpi/sbs.c 中有一个方法acpi_battery_read,在它的正上方有一条注释,上面写着驱动程序接口".如果有人知道如何使用它,这可能是另一种方式.
  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?

我在下面的答案中包含了我的解决方案.

推荐答案

我找到了适合我的解决方案.首先确保#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.

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
",chargenow.intval);
}
result = psy->get_property(psy,POWER_SUPPLY_PROP_CHARGE_FULL,&chargefull);
if(!result) {
    printk(KERN_INFO "The charge level is %d
",chargefull.intval);
}

这篇关于如何获取 Linux 内核模块内的电池电量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆