Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1089648b authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I707ea158,Ic1c489dd,I7484b772 into main

* changes:
  libhealthloop: Use designated initializers
  libhealthloop: Reduce the number of ScheduleBatteryUpdate() calls
  libhealthloop: Terminate KLOG messages with a newline
parents 9b012da1 7b7948b1
Loading
Loading
Loading
Loading
+37 −29
Original line number Original line Diff line number Diff line
@@ -43,6 +43,8 @@ namespace android {
namespace hardware {
namespace hardware {
namespace health {
namespace health {


static constexpr uint32_t kUeventMsgLen = 2048;

HealthLoop::HealthLoop() {
HealthLoop::HealthLoop() {
    InitHealthdConfig(&healthd_config_);
    InitHealthdConfig(&healthd_config_);
    awake_poll_interval_ = -1;
    awake_poll_interval_ = -1;
@@ -61,14 +63,13 @@ int HealthLoop::RegisterEvent(int fd, BoundFunction func, EventWakeup wakeup) {
                                          EventHandler{this, fd, std::move(func)}))
                                          EventHandler{this, fd, std::move(func)}))
                                  .get();
                                  .get();


    struct epoll_event ev;
    struct epoll_event ev = {

        .events = EPOLLIN,
    ev.events = EPOLLIN;
        .data.ptr = reinterpret_cast<void*>(event_handler),
    };


    if (wakeup == EVENT_WAKEUP_FD) ev.events |= EPOLLWAKEUP;
    if (wakeup == EVENT_WAKEUP_FD) ev.events |= EPOLLWAKEUP;


    ev.data.ptr = reinterpret_cast<void*>(event_handler);

    if (epoll_ctl(epollfd_, EPOLL_CTL_ADD, fd, &ev) == -1) {
    if (epoll_ctl(epollfd_, EPOLL_CTL_ADD, fd, &ev) == -1) {
        KLOG_ERROR(LOG_TAG, "epoll_ctl failed; errno=%d\n", errno);
        KLOG_ERROR(LOG_TAG, "epoll_ctl failed; errno=%d\n", errno);
        return -1;
        return -1;
@@ -121,32 +122,39 @@ void HealthLoop::PeriodicChores() {
    ScheduleBatteryUpdate();
    ScheduleBatteryUpdate();
}
}


#define UEVENT_MSG_LEN 2048
// Returns true if and only if the battery statistics should be updated.
void HealthLoop::UeventEvent(uint32_t /*epevents*/) {
bool HealthLoop::RecvUevents() {
    // No need to lock because uevent_fd_ is guaranteed to be initialized.
    bool update_stats = false;

    for (;;) {
    char msg[UEVENT_MSG_LEN + 2];
        char msg[kUeventMsgLen + 2];
    char* cp;
        int n = uevent_kernel_multicast_recv(uevent_fd_, msg, kUeventMsgLen);
    int n;
        if (n <= 0) return update_stats;

        if (n >= kUeventMsgLen) {
    n = uevent_kernel_multicast_recv(uevent_fd_, msg, UEVENT_MSG_LEN);
            // too long -- discard
    if (n <= 0) return;
            continue;
    if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
        }
        return;
        if (update_stats) {
            continue;
        }


        msg[n] = '\0';
        msg[n] = '\0';
        msg[n + 1] = '\0';
        msg[n + 1] = '\0';
    cp = msg;
        for (char* cp = msg; *cp;) {

            if (strcmp(cp, "SUBSYSTEM=power_supply") == 0) {
    while (*cp) {
                update_stats = true;
        if (!strcmp(cp, "SUBSYSTEM=power_supply")) {
            ScheduleBatteryUpdate();
                break;
                break;
            }
            }


            /* advance to after the next \0 */
            /* advance to after the next \0 */
        while (*cp++)
            while (*cp++) {
            ;
            }
        }
    }
}

void HealthLoop::UeventEvent(uint32_t /*epevents*/) {
    if (RecvUevents()) {
        ScheduleBatteryUpdate();
    }
    }
}
}


@@ -183,9 +191,9 @@ void HealthLoop::UeventInit(void) {
        std::string error_msg = attach_result.error().message();
        std::string error_msg = attach_result.error().message();
        error_msg +=
        error_msg +=
                ". This is expected in recovery mode and also for kernel versions before 5.10.";
                ". This is expected in recovery mode and also for kernel versions before 5.10.";
        KLOG_WARNING(LOG_TAG, "%s", error_msg.c_str());
        KLOG_WARNING(LOG_TAG, "%s\n", error_msg.c_str());
    } else {
    } else {
        KLOG_INFO(LOG_TAG, "Successfully attached the BPF filter to the uevent socket");
        KLOG_INFO(LOG_TAG, "Successfully attached the BPF filter to the uevent socket\n");
    }
    }


    if (RegisterEvent(uevent_fd_, &HealthLoop::UeventEvent, EVENT_WAKEUP_FD))
    if (RegisterEvent(uevent_fd_, &HealthLoop::UeventEvent, EVENT_WAKEUP_FD))
+1 −0
Original line number Original line Diff line number Diff line
@@ -93,6 +93,7 @@ class HealthLoop {
    void WakeAlarmInit();
    void WakeAlarmInit();
    void WakeAlarmEvent(uint32_t);
    void WakeAlarmEvent(uint32_t);
    void UeventInit();
    void UeventInit();
    bool RecvUevents();
    void UeventEvent(uint32_t);
    void UeventEvent(uint32_t);
    void WakeAlarmSetInterval(int interval);
    void WakeAlarmSetInterval(int interval);
    void PeriodicChores();
    void PeriodicChores();