From c1186415d46f0e7d7f4f9c263e5b915eaf6f1904 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 16 May 2022 15:59:55 +0900 Subject: [PATCH] battery: disable module if battery.present is 0 The actual behavior of no battery remains same as before. It is the main purpose of this patch that cleaning up handlings and loggins. Change-Id: Iaae8ac1ce6a438f0a2984d7ea6a3e6953ff8a51f Signed-off-by: Youngjae Cho --- src/battery/power-supply.c | 76 +++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/src/battery/power-supply.c b/src/battery/power-supply.c index 7d33f84..f5c57c4 100644 --- a/src/battery/power-supply.c +++ b/src/battery/power-supply.c @@ -110,6 +110,7 @@ static void update_health(enum battery_noti_status status); static bool battery_dev_available = false; static int load_uevent(struct parse_result *result, void *user_data); static int event_handler_state_changed(void *data); +static void power_supply_exit(void *data); inline struct battery_status *get_var_battery_status(void) { @@ -924,7 +925,7 @@ static int battery_state(struct battery_info *info) return 1; } -static void battery_invalidate_vconf(int value) +static void battery_disable_module(int value) { int retval; static const struct invalidate_list { @@ -950,6 +951,8 @@ static void battery_invalidate_vconf(int value) if (list[i].member) *(list[i].member) = value; } + + power_supply_exit(NULL); } static void battery_changed(struct battery_info *info, void *data) @@ -1014,36 +1017,25 @@ static void battery_changed(struct battery_info *info, void *data) if (battery.present) { process_power_supply(&battery.capacity); } else { - _D("Battery disconnected"); - battery_invalidate_vconf(-ENODEV); + CRITICAL_LOG_E("Battery disconnected. Disable the battery module."); + battery_disable_module(-ENODEV); } } -static void power_supply_status_init(void) +static gboolean power_supply_update_during_booting(void *data) { - int r; + int retval; - r = hal_device_battery_get_current_state(battery_changed, NULL); - if (battery_dev_available && (r != -ENODEV)) { - if (r < 0 || battery.capacity < 0) { - _E("Failed to get battery capacity (capa: %d, ret: %d)", battery.capacity, r); - return; - } - } else { - r = config_parse(POWER_SUPPLY_UEVENT, load_uevent, &battery); - if (r < 0) { - _E("Failed to load %s, %d Use default value.", POWER_SUPPLY_UEVENT, r); - return; - } - battery.health = HEALTH_GOOD; - battery.present = PRESENT_NORMAL; - process_power_supply(&battery.capacity); + if (!battery_dev_available) + return G_SOURCE_REMOVE; + + retval = hal_device_battery_get_current_state(battery_changed, NULL); + if (retval == -ENODEV) { + CRITICAL_LOG_E("There is no battery detected. Disable the battery module."); + battery_disable_module(retval); + return G_SOURCE_REMOVE; } -} -static gboolean power_supply_update(void *data) -{ - power_supply_status_init(); return G_SOURCE_CONTINUE; } @@ -1051,31 +1043,13 @@ static void power_supply_timer_start(void) { _D("Battery init timer during booting."); power_timer = g_timeout_add(BATTERY_CHECK_TIMER_INTERVAL, - power_supply_update, NULL); + power_supply_update_during_booting, NULL); if (power_timer == 0) _E("Failed to add battery init timer during booting."); else _I("Started battery init timer during booting."); } -static void power_supply_timer_stop(void) -{ - int retval; - - if (power_timer == 0) - return; - _I("Stop battery init timer during booting."); - g_source_remove(power_timer); - power_timer = 0; - - /* check battery has been initialized until booting done */ - retval = hal_device_battery_get_current_state(battery_changed, NULL); - if (retval < 0) { - _E("Failed to initialize battery state"); - battery_invalidate_vconf(retval); - } -} - static GVariant *dbus_get_charger_status(GDBusConnection *conn, const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) @@ -1412,6 +1386,7 @@ static const dbus_interface_u dbus_interface = { static int delayed_init_done(void *data) { static int done; + int retval; device_notifier_state_e state = DEVICE_NOTIFIER_STATE_START; if (data == NULL) @@ -1420,7 +1395,19 @@ static int delayed_init_done(void *data) if (done == 0) return done; - power_supply_timer_stop(); + if (power_timer) { + g_source_remove(power_timer); + power_timer = 0; + } + + /* check battery has been initialized */ + retval = hal_device_battery_get_current_state(battery_changed, NULL); + if (retval == -ENODEV) { + CRITICAL_LOG_E("Failed to initialize battery state, %d. Disable the battery module.", retval); + battery_disable_module(retval); + return done; + } + event_handler_state_changed((void *)&state); _I("booting done %s(%d) %s(%d) %s(%d) %s(%d) %s(%d) %s(%d) %s(%d : %s)", @@ -1627,6 +1614,7 @@ static void power_supply_exit(void *data) { device_notifier_state_e state = DEVICE_NOTIFIER_STATE_STOP; + unregister_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done); unregister_notifier(DEVICE_NOTIFIER_EVENT_HANDLER, event_handler_state_changed); event_handler_state_changed((void *)&state); -- 2.7.4