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

Commit 19cc4a3e authored by Matthew Qin's avatar Matthew Qin Committed by Steve Kondik
Browse files

healthd: charger: Support alarm in power off charging mode

To implement this feature need to let the phone reboot some time
earlier than the alarm time.

CRs-fixed: 727939
Change-Id: I67832ec089eddd436a4daee7c6808ab2eaa69213
parent 1d4d2512
Loading
Loading
Loading
Loading
+179 −0
Original line number Diff line number Diff line
@@ -40,6 +40,10 @@
#include <cutils/uevent.h>
#include <cutils/properties.h>

#include <pthread.h>
#include <linux/android_alarm.h>
#include <linux/rtc.h>

#ifdef CHARGER_ENABLE_SUSPEND
#include <suspend/autosuspend.h>
#endif
@@ -205,6 +209,17 @@ static int char_width;
static int char_height;
static bool minui_inited;

enum alarm_time_type {
    ALARM_TIME,
    RTC_TIME,
};

/*
 * shouldn't be changed after
 * reading from alarm register
 */
static time_t alm_secs;

static int set_tricolor_led(int on, int color)
{
    int fd, i;
@@ -782,6 +797,168 @@ static void charger_event_handler(uint32_t /*epevents*/)
        ev_dispatch();
}

static int alarm_get_time(enum alarm_time_type time_type,
                          time_t *secs)
{
    struct tm tm;
    unsigned int cmd;
    int rc, fd = -1;

    if (!secs)
        goto err;

    fd = open("/dev/rtc0", O_RDWR);
    if (fd < 0) {
        LOGE("Can't open rtc devfs node\n");
        goto err;
    }

    switch (time_type) {
        case ALARM_TIME:
            cmd = RTC_ALM_READ;
            break;
        case RTC_TIME:
            cmd = RTC_RD_TIME;
            break;
        default:
            LOGE("Invalid time type\n");
            goto err;
    }

    rc = ioctl(fd, cmd, &tm);
    if (rc < 0) {
        LOGE("Unable to get time\n");
        goto err;
    }

    close(fd);

    *secs = mktime(&tm) + tm.tm_gmtoff;
    if (*secs < 0) {
        LOGE("Invalid seconds = %ld\n", *secs);
        goto err;
    }

    return 0;

err:
    if (fd >= 0)
        close(fd);
    return -1;
}

#define ERR_SECS	2
static int alarm_is_alm_expired()
{
    int rc;
    time_t rtc_secs;

    rc = alarm_get_time(RTC_TIME, &rtc_secs);
    if (rc < 0)
        return 0;

    return (alm_secs >= rtc_secs - ERR_SECS &&
            alm_secs <= rtc_secs + ERR_SECS) ? 1 : 0;
}

static int alarm_set_reboot_time_and_wait(time_t secs)
{
    int rc, fd;
    struct timespec ts;

    fd = open("/dev/alarm", O_RDWR);
    if (fd < 0) {
        LOGE("Can't open alarm devfs node\n");
        goto err;
    }

    /* get the elapsed realtime from boot time to now */
    rc = ioctl(fd, ANDROID_ALARM_GET_TIME(
                      ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts);
    if (rc < 0) {
        LOGE("Unable to get elapsed realtime\n");
        goto err;
    }

    /* calculate the elapsed time from boot time to reboot time */
    ts.tv_sec += secs;
    ts.tv_nsec = 0;

    rc = ioctl(fd, ANDROID_ALARM_SET(
                      ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts);
    if (rc < 0) {
        LOGE("Unable to set reboot time to %ld\n", secs);
        goto err;
    }

    do {
        rc = ioctl(fd, ANDROID_ALARM_WAIT);
    } while ((rc < 0 && errno == EINTR) || !alarm_is_alm_expired());

    if (rc <= 0) {
        LOGE("Unable to wait on alarm\n");
        goto err;
    }

    close(fd);
    return 0;

err:
    if (fd >= 0)
        close(fd);
    return -1;
}

void *alarm_thread(void *)
{
    time_t rtc_secs, rb_secs;
    int rc;

    /*
     * to support power off alarm, the time
     * stored in alarm register at latest
     * shutdown time should be some time
     * earlier than the actual alarm time
     * set by user
     */
    rc = alarm_get_time(ALARM_TIME, &alm_secs);
    if (rc < 0 || !alm_secs)
        goto err;

    rc = alarm_get_time(RTC_TIME, &rtc_secs);
    if (rc < 0)
        goto err;

    /*
     * calculate the reboot time after which
     * the phone will reboot
     */
    rb_secs = alm_secs - rtc_secs;
    if (rb_secs <= 0)
        goto err;

    rc = alarm_set_reboot_time_and_wait(rb_secs);
    if (rc < 0)
        goto err;

    LOGI("Exit from power off charging, reboot the phone!\n");
    android_reboot(ANDROID_RB_RESTART2, 0, (char *)"rtc");

err:
    LOGE("Exit from alarm thread\n");
    return NULL;
}

void alarm_thread_create()
{
    pthread_t tid;
    int rc;

    rc = pthread_create(&tid, NULL, alarm_thread, NULL);
    if (rc < 0)
        LOGE("Create alarm thread failed\n");
}

void healthd_mode_charger_init(struct healthd_config* /*config*/)
{
    int ret;
@@ -791,6 +968,8 @@ void healthd_mode_charger_init(struct healthd_config* /*config*/)

    dump_last_kmsg();

    alarm_thread_create();

    LOGI("--------------- STARTING CHARGER MODE ---------------\n");

    ret = ev_init(input_callback, charger);