簡體   English   中英

監聽 sysfs 電池事件

[英]Listening for sysfs battery events

我正在編寫一個檢查電池容量的守護進程。 這適用於運行 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;
}

我得到的唯一事件是我插入/拔出充電器時! 然后我認為我在筆記本電腦安裝中使用的狀態欄確實顯示了電池容量。 我查看了源代碼,他們正在使用inotify來監控電池的uevent 但是我到處都讀到我不應該將inotify用於sysfs :盡管如此,我還是嘗試了:

#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來poll()用於中斷的GPIO的指南建議尋找POLLPRI事件,而不是您展示的POLLIN在第一個實現中(請參閱man poll了解事件類型)。

更重要的是,您說連接/斷開充電器時會收到一個事件,您的意思是每次軟件執行都有一個事件嗎? 如果是這種情況,可能是因為您沒有清除中斷標志:在poll()命中后,在 sysfs 中需要int len = read(fds[0].fd, *buf, SIZE); 將中斷標記為已服務,以及lseek(fds[0].fd, 0, 0); 為了使下一個 read() 成功(有關代碼示例,請參見我的其他答案)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM