1f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin/*
2f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * Copyright (C) 2011 The Android Open Source Project
3f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin *
4f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * Licensed under the Apache License, Version 2.0 (the "License");
5f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * you may not use this file except in compliance with the License.
6f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * You may obtain a copy of the License at
7f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin *
8f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin *      http://www.apache.org/licenses/LICENSE-2.0
9f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin *
10f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * Unless required by applicable law or agreed to in writing, software
11f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * distributed under the License is distributed on an "AS IS" BASIS,
12f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * See the License for the specific language governing permissions and
14f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * limitations under the License.
15f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin */
16f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
17f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin//#define DEBUG_UEVENTS
18f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define CHARGER_KLOG_LEVEL 6
19f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
20f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <dirent.h>
21f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <errno.h>
22f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <fcntl.h>
23f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <linux/input.h>
24f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <stdbool.h>
25f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <stdio.h>
26f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <stdlib.h>
27f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <string.h>
28f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <sys/poll.h>
29f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <sys/stat.h>
30f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <sys/types.h>
31f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <sys/un.h>
32f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <time.h>
33f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <unistd.h>
34f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
35b77d3d724945f38b39f3116e3aead4edcbd08a36Elliott Hughes#include <sys/socket.h>
36b77d3d724945f38b39f3116e3aead4edcbd08a36Elliott Hughes#include <linux/netlink.h>
37b77d3d724945f38b39f3116e3aead4edcbd08a36Elliott Hughes
38f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <cutils/android_reboot.h>
39f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <cutils/klog.h>
40f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <cutils/list.h>
41823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin#include <cutils/misc.h>
42f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include <cutils/uevent.h>
43f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
44f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#include "minui/minui.h"
45f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
46f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#ifndef max
47f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define max(a,b) ((a) > (b) ? (a) : (b))
48f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#endif
49f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
50f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#ifndef min
51f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define min(a,b) ((a) < (b) ? (a) : (b))
52f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#endif
53f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
540052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin#define ARRAY_SIZE(x)           (sizeof(x)/sizeof(x[0]))
550052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
56f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define MSEC_PER_SEC            (1000LL)
57f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define NSEC_PER_MSEC           (1000000LL)
58f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
590052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin#define BATTERY_UNKNOWN_TIME    (2 * MSEC_PER_SEC)
6092312a515b0b877deed5dfdbf9d7613e8d66fee5Dima Zavin#define POWER_ON_KEY_TIME       (2 * MSEC_PER_SEC)
61f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
62f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
631a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin#define BATTERY_FULL_THRESH     95
641a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin
65823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin#define LAST_KMSG_PATH          "/proc/last_kmsg"
66823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin#define LAST_KMSG_MAX_SZ        (32 * 1024)
67823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
68f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
69f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
70f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
71f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
72f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstruct key_state {
73f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    bool pending;
74f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    bool down;
75f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int64_t timestamp;
76f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin};
77f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
78f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstruct power_supply {
79f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct listnode list;
80f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    char name[256];
81f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    char type[32];
82f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    bool online;
83f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    bool valid;
840052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    char cap_path[PATH_MAX];
850052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin};
860052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
870052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavinstruct frame {
880052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    const char *name;
890052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    int disp_time;
900052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    int min_capacity;
919ec3f3e2e4ccb18ed85fa47015b0fa71985710c4Dima Zavin    bool level_only;
920052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
930052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    gr_surface surface;
940052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin};
950052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
960052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavinstruct animation {
970052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    bool run;
980052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
990052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    struct frame *frames;
1000052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    int cur_frame;
1010052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    int num_frames;
1020052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
1030052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    int cur_cycle;
1040052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    int num_cycles;
1050052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
1060052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    /* current capacity being animated */
1070052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    int capacity;
108f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin};
109f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
110f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstruct charger {
111f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int64_t next_screen_transition;
112f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int64_t next_key_check;
113f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int64_t next_pwr_check;
114f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
115f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct key_state keys[KEY_MAX + 1];
116f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int uevent_fd;
117f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
118f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct listnode supplies;
119f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int num_supplies;
120f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int num_supplies_online;
121f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
1220052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    struct animation *batt_anim;
1230052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    gr_surface surf_unknown;
1240052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
125f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct power_supply *battery;
126f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin};
127f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
128f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstruct uevent {
129f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    const char *action;
130f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    const char *path;
131f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    const char *subsystem;
132f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    const char *ps_name;
133f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    const char *ps_type;
134f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    const char *ps_online;
135f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin};
136f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
1370052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavinstatic struct frame batt_anim_frames[] = {
1380052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    {
1390052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .name = "charger/battery_0",
1400052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .disp_time = 750,
1410052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .min_capacity = 0,
1420052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    },
1430052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    {
1440052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .name = "charger/battery_1",
1450052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .disp_time = 750,
1460052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .min_capacity = 20,
1470052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    },
1480052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    {
1490052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .name = "charger/battery_2",
1500052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .disp_time = 750,
1510052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .min_capacity = 40,
1520052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    },
1530052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    {
1540052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .name = "charger/battery_3",
1550052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .disp_time = 750,
1560052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .min_capacity = 60,
1570052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    },
1580052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    {
1590052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .name = "charger/battery_4",
1600052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .disp_time = 750,
1610052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin        .min_capacity = 80,
1629ec3f3e2e4ccb18ed85fa47015b0fa71985710c4Dima Zavin        .level_only = true,
1630052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    },
1641a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    {
1651a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin        .name = "charger/battery_5",
1661a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin        .disp_time = 750,
1671a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin        .min_capacity = BATTERY_FULL_THRESH,
1681a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    },
1690052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin};
1700052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
1710052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavinstatic struct animation battery_animation = {
1720052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    .frames = batt_anim_frames,
1730052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    .num_frames = ARRAY_SIZE(batt_anim_frames),
1740052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    .num_cycles = 3,
1750052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin};
1760052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
1770052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavinstatic struct charger charger_state = {
1780052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    .batt_anim = &battery_animation,
1790052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin};
1800052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin
181f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic int char_width;
182f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic int char_height;
183f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
184f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin/* current time in milliseconds */
185f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic int64_t curr_time_ms(void)
186f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
187f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct timespec tm;
188f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    clock_gettime(CLOCK_MONOTONIC, &tm);
189f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
190f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
191f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
192f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic void clear_screen(void)
193f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
194f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    gr_color(0, 0, 0, 255);
195f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    gr_fill(0, 0, gr_fb_width(), gr_fb_height());
196f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin};
197f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
198823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin#define MAX_KLOG_WRITE_BUF_SZ 256
199823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
200823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavinstatic void dump_last_kmsg(void)
201823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin{
202823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    char *buf;
203823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    char *ptr;
204823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    unsigned sz = 0;
205823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    int len;
206823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
207823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    LOGI("\n");
208823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    LOGI("*************** LAST KMSG ***************\n");
209823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    LOGI("\n");
210823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    buf = load_file(LAST_KMSG_PATH, &sz);
211823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    if (!buf || !sz) {
212823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        LOGI("last_kmsg not found. Cold reset?\n");
213823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        goto out;
214823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    }
215823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
216823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    len = min(sz, LAST_KMSG_MAX_SZ);
217823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    ptr = buf + (sz - len);
218823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
219823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    while (len > 0) {
220823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        int cnt = min(len, MAX_KLOG_WRITE_BUF_SZ);
221823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        char yoink;
222823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        char *nl;
223823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
224823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        nl = memrchr(ptr, '\n', cnt - 1);
225823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        if (nl)
226823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin            cnt = nl - ptr + 1;
227823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
228823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        yoink = ptr[cnt];
229823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        ptr[cnt] = '\0';
230d11e1a0278c602c761ac37d843926e1eba5a63f1Dima Zavin        klog_write(6, "<6>%s", ptr);
231823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        ptr[cnt] = yoink;
232823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
233823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        len -= cnt;
234823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin        ptr += cnt;
235823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    }
236823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
237823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    free(buf);
238823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
239823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavinout:
240823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    LOGI("\n");
241823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    LOGI("************* END LAST KMSG *************\n");
242823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin    LOGI("\n");
243823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin}
244823ebc4d824ea70e5ec7d376adf2fbce50eb9cb2Dima Zavin
245f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic int read_file(const char *path, char *buf, size_t sz)
246f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
247f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int fd;
248f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    size_t cnt;
249f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
250f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    fd = open(path, O_RDONLY, 0);
251f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (fd < 0)
252f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        goto err;
253f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
254f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    cnt = read(fd, buf, sz - 1);
255f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (cnt <= 0)
256f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        goto err;
257f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    buf[cnt] = '\0';
258f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (buf[cnt - 1] == '\n') {
259f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        cnt--;
260f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        buf[cnt] = '\0';
261f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
262f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
263f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    close(fd);
264f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return cnt;
265f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
266f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinerr:
267f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (fd >= 0)
268f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        close(fd);
269f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return -1;
270f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
271f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
272f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic int read_file_int(const char *path, int *val)
273f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
274f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    char buf[32];
275f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int ret;
276f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int tmp;
277f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    char *end;
278f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
279f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    ret = read_file(path, buf, sizeof(buf));
280f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (ret < 0)
281f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        return -1;
282f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
283f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    tmp = strtol(buf, &end, 0);
284f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (end == buf ||
285f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
286f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        goto err;
287f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
288f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    *val = tmp;
289f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return 0;
290f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
291f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinerr:
292f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return -1;
293f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
294f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
2951a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavinstatic int get_battery_capacity(struct charger *charger)
2961a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin{
2971a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    int ret;
2981a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    int batt_cap = -1;
2991a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin
3001a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    if (!charger->battery)
3011a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin        return -1;
3021a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin
3031a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    ret = read_file_int(charger->battery->cap_path, &batt_cap);
3041a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    if (ret < 0 || batt_cap > 100) {
3051a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin        batt_cap = -1;
3061a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    }
3071a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin
3081a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin    return batt_cap;
3091a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin}
3101a5ca61b28b3269b625b7a7162b4f50734f7fea9Dima Zavin
311f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic struct power_supply *find_supply(struct charger *charger,
312f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                                        const char *name)
313f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
314f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct listnode *node;
315f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct power_supply *supply;
316f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
317f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    list_for_each(node, &charger->supplies) {
318f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        supply = node_to_item(node, struct power_supply, list);
319f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (!strncmp(name, supply->name, sizeof(supply->name)))
320f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            return supply;
321f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
322f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return NULL;
323f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
324f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
325f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic struct power_supply *add_supply(struct charger *charger,
326f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                                       const char *name, const char *type,
3270052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin                                       const char *path, bool online)
328f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
329f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct power_supply *supply;
330f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
331f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    supply = calloc(1, sizeof(struct power_supply));
332f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (!supply)
333f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        return NULL;
334f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
335f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    strlcpy(supply->name, name, sizeof(supply->name));
336f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    strlcpy(supply->type, type, sizeof(supply->type));
3370052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin    snprintf(supply->cap_path, sizeof(supply->cap_path),
3380052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin             "/sys/%s/capacity", path);
339f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    supply->online = online;
340f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    list_add_tail(&charger->supplies, &supply->list);
341f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    charger->num_supplies++;
342f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    LOGV("... added %s %s %d\n", supply->name, supply->type, online);
343f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return supply;
344f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
345f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
346f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic void remove_supply(struct charger *charger, struct power_supply *supply)
347f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
348f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (!supply)
349f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        return;
350f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    list_remove(&supply->list);
351f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    charger->num_supplies--;
352f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    free(supply);
353f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
354f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
355f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic void parse_uevent(const char *msg, struct uevent *uevent)
356f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
357f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    uevent->action = "";
358f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    uevent->path = "";
359f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    uevent->subsystem = "";
360f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    uevent->ps_name = "";
361f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    uevent->ps_online = "";
362f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    uevent->ps_type = "";
363f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
364f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    /* currently ignoring SEQNUM */
365f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    while (*msg) {
366f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#ifdef DEBUG_UEVENTS
367f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        LOGV("uevent str: %s\n", msg);
368f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#endif
369f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (!strncmp(msg, "ACTION=", 7)) {
370f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            msg += 7;
371f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            uevent->action = msg;
372f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        } else if (!strncmp(msg, "DEVPATH=", 8)) {
373f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            msg += 8;
374f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            uevent->path = msg;
375f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
376f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            msg += 10;
377f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            uevent->subsystem = msg;
378f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
379f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            msg += 18;
380f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            uevent->ps_name = msg;
381f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
382f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            msg += 20;
383f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            uevent->ps_online = msg;
384f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
385f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            msg += 18;
386f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            uevent->ps_type = msg;
387f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        }
388f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
389f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        /* advance to after the next \0 */
390f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        while (*msg++)
391f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            ;
392f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
393f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
394f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
395f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin         uevent->action, uevent->path, uevent->subsystem,
396f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin         uevent->ps_name, uevent->ps_type, uevent->ps_online);
397f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
398f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
399f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic void process_ps_uevent(struct charger *charger, struct uevent *uevent)
400f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
401f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int online;
402f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    char ps_type[32];
403f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct power_supply *supply = NULL;
404f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int i;
405f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    bool was_online = false;
406f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    bool battery = false;
407f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
408f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (uevent->ps_type[0] == '\0') {
409f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        char *path;
410f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        int ret;
411f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
412f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (uevent->path[0] == '\0')
413f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            return;
414f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        ret = asprintf(&path, "/sys/%s/type", uevent->path);
415f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (ret <= 0)
416f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            return;
417f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        ret = read_file(path, ps_type, sizeof(ps_type));
418f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        free(path);
419f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (ret < 0)
420f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            return;
421f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    } else {
422f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
423f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
424f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
425f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (!strncmp(ps_type, "Battery", 7))
426f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        battery = true;
427f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
428f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    online = atoi(uevent->ps_online);
429f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    supply = find_supply(charger, uevent->ps_name);
430f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (supply) {
431f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        was_online = supply->online;
432f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        supply->online = online;
433f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
434f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
435f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (!strcmp(uevent->action, "add")) {
436f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (!supply) {
4370052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin            supply = add_supply(charger, uevent->ps_name, ps_type, uevent->path,
4380052abdafd7179fc4a7458f09d2f95c74dda0021Dima Zavin                                online);
439f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            if (!supply) {
440f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
441f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                     uevent->ps_type, online);
442f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                return;
443f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            }
444f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            /* only pick up the first battery for now */
445f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            if (battery && !charger->battery)
446f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                charger->battery = supply;
447f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        } else {
448f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            LOGE("supply '%s' already exists..\n", uevent->ps_name);
449f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        }
450f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    } else if (!strcmp(uevent->action, "remove")) {
451f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (supply) {
452f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            if (charger->battery == supply)
453f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                charger->battery = NULL;
454f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            remove_supply(charger, supply);
455f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            supply = NULL;
456f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        }
457f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    } else if (!strcmp(uevent->action, "change")) {
458f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (!supply) {
459f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            LOGE("power supply '%s' not found ('%s' %d)\n",
460f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                 uevent->ps_name, ps_type, online);
461f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            return;
462f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        }
463f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    } else {
464f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        return;
465f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
466f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
467f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    /* allow battery to be managed in the supply list but make it not
468f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin     * contribute to online power supplies. */
469f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (!battery) {
470f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (was_online && !online)
471f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            charger->num_supplies_online--;
472f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        else if (supply && !was_online && online)
473f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            charger->num_supplies_online++;
474f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
475f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
476f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
477f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin         uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
478f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin         uevent->action, charger->num_supplies_online, charger->num_supplies);
479f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
480f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
481f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic void process_uevent(struct charger *charger, struct uevent *uevent)
482f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
483f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (!strcmp(uevent->subsystem, "power_supply"))
484f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        process_ps_uevent(charger, uevent);
485f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
486f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
487f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin#define UEVENT_MSG_LEN  1024
488f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic int handle_uevent_fd(struct charger *charger, int fd)
489f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
490f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    char msg[UEVENT_MSG_LEN+2];
491f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int n;
492f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
493f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (fd < 0)
494f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        return -1;
495f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
496f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    while (true) {
497f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        struct uevent uevent;
498f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
499f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
500f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (n <= 0)
501f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            break;
502f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        if (n >= UEVENT_MSG_LEN)   /* overflow -- discard */
503f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin            continue;
504f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
505f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        msg[n] = '\0';
506f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        msg[n+1] = '\0';
507f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
508f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        parse_uevent(msg, &uevent);
509f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        process_uevent(charger, &uevent);
510f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    }
511f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
512f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return 0;
513f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
514f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
515f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic int uevent_callback(int fd, short revents, void *data)
516f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
517f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct charger *charger = data;
518f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
519f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (!(revents & POLLIN))
520f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        return -1;
521f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    return handle_uevent_fd(charger, fd);
522f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin}
523f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
524f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin/* force the kernel to regenerate the change events for the existing
525f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin * devices, if valid */
526f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavinstatic void do_coldboot(struct charger *charger, DIR *d, const char *event,
527f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin                        bool follow_links, int max_depth)
528f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin{
529f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    struct dirent *de;
530f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    int dfd, fd;
531f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
532f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    dfd = dirfd(d);
533f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin
534f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    fd = openat(dfd, "uevent", O_WRONLY);
535f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin    if (fd >= 0) {
536f48b23688c1303212c65e470af9f9f3892e98f8eDima Zavin        write(fd, event, strlen(event));
537