battery: disable module if battery.present is 0 64/275064/4
authorYoungjae Cho <y0.cho@samsung.com>
Mon, 16 May 2022 06:59:55 +0000 (15:59 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Wed, 8 Jun 2022 05:31:12 +0000 (14:31 +0900)
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 <y0.cho@samsung.com>
src/battery/power-supply.c

index 7d33f84..f5c57c4 100644 (file)
@@ -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);