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

查看:333
本文介绍了如何获得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,在它的正上方有一条注释,内容为"Driver Interface".如果有人知道如何使用它,这可能是另一种方式.
  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\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);
}

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

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