battery: Relocate abnormal battery health handling logic 81/311981/3
authorYunhee Seo <yuni.seo@samsung.com>
Fri, 31 May 2024 07:46:49 +0000 (16:46 +0900)
committerYunhee Seo <yuni.seo@samsung.com>
Tue, 4 Jun 2024 01:50:51 +0000 (10:50 +0900)
Move the logic used specifically for deviced mobile plugin to mobile plugin backend.
Abnormal battery health signal subscription was only used in deviced mobile plugin.
Thus, the signal subscription and handling part is moved to here.

Change-Id: Ibc026567f6079784a3f3d30fb6570073319d9f46
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
src/battery/battery.c

index 2f4098ae466d491b442b03f6eb32b8ea96877e58..87f6937ac7f975fc6f08581221515f7fb340eb30 100644 (file)
 #include <stdlib.h>
 #include <errno.h>
 
+#include <libsyscommon/libgdbus.h>
+#include <libsyscommon/log.h>
+#include <libsyscommon/notifier.h>
+#include <libsyscommon/resource-manager.h>
+#include <libsyscommon/resource-type.h>
 #include <system/syscommon-plugin-common-interface.h>
 #include <system/syscommon-plugin-deviced-battery.h>
 #include <system/syscommon-plugin-deviced-battery-interface.h>
+#include <system/syscommon-plugin-deviced-common-interface.h>
+#include <system/syscommon-plugin-deviced-display-interface.h>
 
 #define EXPORT __attribute__ ((visibility("default")))
 
+#define SIGNAL_CHARGEERR_RESPONSE "ChargeErrResponse"
+
+static guint abnormal_timer;
+
+static void health_timer_reset(void)
+{
+       abnormal_timer = 0;
+}
+
+static gboolean health_timer_cb(void *data)
+{
+       int ret = 0;
+       enum syscommon_deviced_battery_health_type battery_health;
+       char *battery_health_s = NULL;
+       bool battery_do_not_disturb = false;
+
+       health_timer_reset();
+
+       ret = syscommon_resman_get_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_BATTERY),
+                       DEVICED_BATTERY_ATTR_INT_STATUS_HEALTH, (int32_t *) &battery_health);
+       if (ret < 0) {
+               _E("Failed to get battery health status.");
+               return G_SOURCE_REMOVE;
+       }
+
+       if (battery_health != SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW && battery_health != SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH)
+               return G_SOURCE_REMOVE;
+
+       battery_health_s = calloc(1, SYSCOMMON_RESMAN_BUFF_MAX);
+       if (battery_health_s) {
+               battery_health_s[SYSCOMMON_RESMAN_BUFF_MAX - 1] = '\0';
+               ret = syscommon_resman_get_resource_attr_string(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_BATTERY),
+                               DEVICED_BATTERY_ATTR_STRING_STATUS_HEALTH, battery_health_s);
+               if (ret == 0) {
+                       CRITICAL_LOG("Popup: Battery health status is not good, %s.", battery_health_s);
+               }
+               free(battery_health_s);
+       }
+
+       syscommon_notifier_emit_notify(DEVICED_NOTIFIER_BATTERY_HEALTH, (void *)&battery_health);
+       ret = syscommon_resman_get_resource_attr_bool(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_BATTERY),
+                       DEVICED_BATTERY_ATTR_BOOL_DO_NOT_DISTURB, &battery_do_not_disturb);
+       if (ret == 0 && !battery_do_not_disturb)
+               syscommon_resman_set_resource_attr_uint64_2(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
+                       DEVICED_DISPLAY_ATTR_TUPLE2_CURRENT_STATE_WITH_OPTION, DEVICED_EVENT_MISC_POPUP, SYSCOMMON_DEVICED_DISPLAY_STATE_DIM);
+
+       syscommon_resman_set_resource_attr_uint64_3(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
+               DEVICED_DISPLAY_ATTR_TUPLE3_UNLOCK_WITH_OPTION, DEVICED_EVENT_MISC_POPUP,
+               SYSCOMMON_DEVICED_DISPLAY_STATE_OFF, DEVICED_DISPLAY_LOCK_SLEEP_MARGIN);
+
+       syscommon_resman_set_resource_attr_uint64_4(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
+               DEVICED_DISPLAY_ATTR_TUPLE4_LOCK_WITH_OPTION, DEVICED_EVENT_MISC_POPUP,
+               SYSCOMMON_DEVICED_DISPLAY_STATE_DIM, DEVICED_DISPLAY_STATE_STAY_CUR_STATE, 0);
+
+       if (battery_health == SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW) {
+               syscommon_resman_set_resource_attr_uint64_4(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
+                       DEVICED_DISPLAY_ATTR_TUPLE4_LOCK_WITH_OPTION, DEVICED_EVENT_BATTERY_HEALTH_OVERCOOL,
+                       SYSCOMMON_DEVICED_DISPLAY_STATE_OFF, DEVICED_DISPLAY_STATE_STAY_CUR_STATE, 60000);
+               syscommon_resman_set_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_BATTERY),
+                       DEVICED_BATTERY_ATTR_INT_LOW_BATTERY_POPUP, SYSCOMMON_DEVICED_BATTERY_OPT_ERR_TEMP_LOW);
+       } else if (battery_health == SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH) {
+               syscommon_resman_set_resource_attr_uint64_4(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
+                       DEVICED_DISPLAY_ATTR_TUPLE4_LOCK_WITH_OPTION, DEVICED_EVENT_BATTERY_HEALTH_OVERHEAT,
+                       SYSCOMMON_DEVICED_DISPLAY_STATE_OFF, DEVICED_DISPLAY_STATE_STAY_CUR_STATE, 60000);
+               syscommon_resman_set_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_BATTERY),
+                       DEVICED_BATTERY_ATTR_INT_LOW_BATTERY_POPUP, SYSCOMMON_DEVICED_BATTERY_OPT_ERR_TEMP_HIGH);
+       }
+
+       return G_SOURCE_REMOVE;
+}
+
+static void abnormal_popup_dbus_signal_handler(GDBusConnection  *conn,
+               const gchar      *sender,
+               const gchar      *path,
+               const gchar      *iface,
+               const gchar      *name,
+               GVariant         *param,
+               gpointer          user_data)
+
+{
+       int ret = 0;
+       enum syscommon_deviced_battery_health_type battery_health;
+
+       ret = syscommon_resman_get_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_BATTERY),
+                       DEVICED_BATTERY_ATTR_INT_STATUS_HEALTH, (int32_t *) &battery_health);
+       if (ret < 0) {
+               _E("Failed to get battery health status.");
+               return;
+       }
+
+       if (battery_health == SYSCOMMON_DEVICED_BATTERY_HEALTH_GOOD)
+               return;
+
+       _I("Restart health timer.");
+       abnormal_timer = g_timeout_add_seconds(DEVICED_BATTERY_ABNORMAL_CHECK_TIMER_INTERVAL,
+                                               health_timer_cb, NULL);
+       if (abnormal_timer == 0)
+               _W("Failed to add abnormal check timer.");
+}
+
 static bool is_possible_to_notify_battery_full(void)
 {
        return true;
@@ -31,6 +138,8 @@ static bool is_possible_to_notify_battery_full(void)
 
 static int deviced_plugin_battery_init(void **data)
 {
+       int ret = 0;
+
        syscommon_plugin_backend_deviced_battery_funcs *funcs = NULL;
 
        funcs = calloc(1, sizeof(*funcs));
@@ -42,6 +151,11 @@ static int deviced_plugin_battery_init(void **data)
 
        *data = (void *)funcs;
 
+       ret = gdbus_signal_subscribe(NULL, DEVICED_PATH_SYSNOTI,
+               DEVICED_INTERFACE_SYSNOTI, SIGNAL_CHARGEERR_RESPONSE, abnormal_popup_dbus_signal_handler, NULL, NULL);
+       if (ret <= 0)
+               _W("Failed to init dbus signal: %d", ret);
+
        return 0;
 }