c - 监听 sysfs 电池事件

标签 c linux inotify sysfs libudev

我正在编写一个检查电池容量的守护进程。这是用于运行 Linux 的太阳能嵌入式设备。我读过使用 sleep() 是个坏主意在守护进程中,因此我正在尝试使用事件。所以我写了一些 PoC,但我没有收到任何事件!正如他们向我推荐的那样,我的第一个实现使用 libudevpoll() :

#include <fcntl.h>
#include <libudev.h>
#include <poll.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
    struct udev *udev;
    struct udev_monitor *mon;
    struct pollfd fds[1];
    int fd;
    udev = udev_new();
    if (udev == NULL)
        return 1;
    mon = udev_monitor_new_from_netlink(udev, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
    udev_monitor_enable_receiving(mon);
    fd = udev_monitor_get_fd(mon);
    fds[0].fd = fd;
    fds[0].events = POLLIN;
    fds[0].revents = 0;
    if (poll(fds, 1, -1) > 0) {
        /* Never gets here! */
        struct udev_device *const dev = udev_monitor_receive_device(mon);
        if (dev != NULL) {
            puts(udev_device_get_sysname(dev));
            udev_device_unref(dev);
        }
        else
            fputs("udev_monitor_receive_device() failed\n", stderr);
    }
    udev_unref(udev);
    return 0;
}
我得到的唯一事件是当我插入/拔出充电器时!然后我认为我在笔记本电脑安装中使用的状态栏确实显示了电池容量。我看了source他们正在使用 inotify监控电池的uevent .但我到处都读到我不应该使用 inotifysysfs !尽管如此,我还是尝试了:
#include <stdio.h>
#include <sys/inotify.h>
#include <unistd.h>

#define BAT_PATH "/sys/class/power_supply/BAT0"

int main(void)
{
    struct inotify_event ev = {0};
    int wd, ret = 1;
    ssize_t len;
    const int fd = inotify_init1(IN_CLOEXEC);
    if (fd < 0) {
        perror("inotify_init() failed");
        return ret;
    }
    /* else */
    wd = inotify_add_watch(fd, BAT_PATH "/uevent", IN_ACCESS);
    if (wd < 0)
        goto end;
    /* else */
    len = read(fd, &ev, sizeof(ev));
    /* Again... never gets here. */
    if (len > 0 && (ev.mask & IN_ACCESS))
        puts("It worked!");
    inotify_rm_watch(fd, wd);
    ret = 0;
end:
    close(fd);
    return ret;
}
事实证明这也不起作用!它如何适用于我的状态栏但在我尝试时不起作用?我做错了什么吗?谢谢你。

最佳答案

关于你的第一个实现(会评论但没有足够的代表。因为我对 libudev 一无所知):我遵循的指南成功使用 sysfs 来轮询()用于中断的 GPIO 建议寻找 POLLPRI事件,而不是 POLLIN正如您在第一个实现中展示的那样(事件类型见 man poll)。

更重要的是,你说当你连接/断开充电器时你会得到一个事件,你的意思是每个软件执行一个事件吗?如果是这种情况,可能是因为您没有清除中断标志:在 poll() 之后命中,在 sysfs 中需要 int len = read(fds[0].fd, *buf, SIZE); 将中断标记为已服务,还有 lseek(fds[0].fd, 0, 0);为了使下一个 read() 成功(有关代码示例,请参阅我的其他答案 here)。

关于c - 监听 sysfs 电池事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67852156/

相关文章:

c++ - 为什么 pthread_getname_np() 在 Linux 的线程中失败?

linux - 在不创建额外进程的情况下在脚本中使用 inotifywait -m?

python - pyinotify.ThreadedNotifier,process_* 未调用

python - inotify_add_watch 失败并显示 errno ESUCCESS

c - 在管道命名程序中执行 exec() 函数

c - 在linux上的c中分配可执行ram

linux - 检查字符串是否包含星号 (*)

linux - 为什么我们不能使用 esp 寄存器直接引用内存地址?

编译器警告 : left shift of negative value

python - 打开文件(例如双击文件)的代码