battery: Add attribute getter for battery health status 50/312850/1
authorYunhee Seo <yuni.seo@samsung.com>
Wed, 29 May 2024 01:08:28 +0000 (10:08 +0900)
committerYunhee Seo <yuni.seo@samsung.com>
Mon, 17 Jun 2024 02:22:53 +0000 (11:22 +0900)
New attribute:
 - id: DEVICED_BATTERY_ATTR_INT_STATUS_HEALTH
 - type: SYSCOMMON_RESMAN_DATA_TYPE_INT
 - setter: X
 - getter: O

New attribute:
 - id: DEVICED_BATTERY_ATTR_STRING_STATUS_HEALTH
 - type: SYSCOMMON_RESMAN_DATA_TYPE_STRING
 - setter: X
 - getter: O

To support above attributes getter, below functions are added.
- int power_supply_get_battery_status_health(int *battery_health);
- int power_supply_get_battery_status_health_string(char **battery_health_s);

In below description, "core" means deviced battery module side.

Before deviding mobile plugin battery module, battery resource driver is needed.
Because struct battery_status was used directly receiving and using the address
of the structure in the core battery module.
However, above method of used is very dependent on the deviced
and is not a desirable method, struct pointer should be removed from plugin side.
Through battery resource driver, plugin can use core side battery attributes without dependency.
In order to add these attributes, new file resource-battery.c is added.
As applying this, enumeration for health_type is moved to libsyscommon
for common usage between plugins and core side.

Change-Id: Id7a897564a5b32c45a05f9056434a75fd27cf59c
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
plugins/mobile/battery/battery-notification.c
plugins/wearable/battery/battery-notification.c
src/battery/lowbat-handler.c
src/battery/power-supply.c
src/battery/power-supply.h
src/battery/resource-battery.c [new file with mode: 0644]
src/display/display-misc.c

index 6f4a7cf31b32b27879dcd9f29a6ae226516ebff5..013258f1756ffca10c310247aac7be5c5ac4a8c5 100644 (file)
@@ -23,6 +23,7 @@
 #include <libsyscommon/resource-manager.h>
 #include <system/syscommon-plugin-deviced-common-interface.h>
 #include <system/syscommon-plugin-deviced-display-interface.h>
+#include <system/syscommon-plugin-deviced-battery-interface.h>
 
 #include <libsyscommon/log.h>
 #include "battery.h"
@@ -43,7 +44,6 @@
 #define REMOVE_POPUP        "remove_battery_popups"
 
 static struct display_plugin *disp_plgn;
-static struct battery_status *battery;
 static guint abnormal_timer;
 
 static void health_timer_reset(void)
@@ -53,19 +53,40 @@ static void health_timer_reset(void)
 
 static gboolean health_timer_cb(void *data)
 {
+       int ret = 0;
+       enum syscommon_deviced_battery_health_type battery_health;
+       char *battery_health_s = NULL;
+
        health_timer_reset();
 
-       if (battery->health != HEALTH_LOW && battery->health != HEALTH_HIGH)
+       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;
 
-       CRITICAL_LOG("Popup: Battery health status is not good, %s.", battery->health_s);
-       syscommon_notifier_emit_notify(DEVICED_NOTIFIER_BATTERY_HEALTH, (void *)&battery->health);
+       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);
        battery_pm_change_internal(DEVICED_EVENT_MISC_POPUP, LCD_DIM);
        display_lock_request_unlock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_OFF, PM_SLEEP_MARGIN);
        display_lock_request_lock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_DIM, STAY_CUR_STATE, 0);
-       if (battery->health == HEALTH_LOW)
+       if (battery_health == SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW)
                battery_charge_err_low_act(NULL);
-       else if (battery->health == HEALTH_HIGH)
+       else if (battery_health == SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH)
                battery_charge_err_high_act(NULL);
        return G_SOURCE_REMOVE;
 }
@@ -79,7 +100,17 @@ static void abnormal_popup_dbus_signal_handler(GDBusConnection  *conn,
                gpointer          user_data)
 
 {
-       if (battery->health == HEALTH_GOOD)
+       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.");
@@ -117,8 +148,4 @@ static void __CONSTRUCTOR__ initialize(void)
        disp_plgn = get_var_display_plugin();
        if (!disp_plgn)
                _E("Failed to get display plugin variable.");
-
-       battery = get_var_battery_status();
-       if (!battery)
-               _E("Failed to get battery status structure.");
 }
index ed6bb939817a65c7597882a768794a778ec05be3..16f1de6f97bf8c1f479c63cbb87de3e1d87eb621 100644 (file)
@@ -19,6 +19,7 @@
 #include <time.h>
 #include <glib.h>
 #include <system/syscommon-plugin-deviced-common-interface.h>
+#include <system/syscommon-plugin-deviced-battery-interface.h>
 
 #include "core.h"
 #include <libsyscommon/log.h>
@@ -34,7 +35,7 @@ static void launch_health_popup_by_display_state(int display_state)
 {
        static enum syscommon_deviced_display_state old = SYSCOMMON_DEVICED_DISPLAY_STATE_START;
 
-       if (battery->health != HEALTH_LOW && battery->health != HEALTH_HIGH) {
+       if (battery->health != SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW && battery->health != SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH) {
                old = display_state;
                return;
        }
index ed08fb316131d19a49a1836c424a9a865d9ec625..967f8eaa1c30a5c97ed870a6c427213594400e3f 100644 (file)
@@ -28,6 +28,8 @@
 #include <libsyscommon/libgdbus.h>
 #include <libsyscommon/list.h>
 #include <libsyscommon/common.h>
+#include <libsyscommon/resource-manager.h>
+#include <system/syscommon-plugin-deviced-battery.h>
 #include <system/syscommon-plugin-deviced-common-interface.h>
 
 #include "lowbat-handler.h"
index 071083856a29a280c867418835374b92016bfa55..9ed51b7aa57714c3573b1d8a9d24b4f12f450b62 100644 (file)
@@ -362,6 +362,24 @@ int power_supply_broadcast(char *sig, int status)
                return ret;
 }
 
+int power_supply_get_battery_status_health(int *battery_health)
+{
+       if (!battery_health)
+               return -EINVAL;
+
+       *battery_health = battery.health;
+       return 0;
+}
+
+int power_supply_get_battery_status_health_string(char **battery_health_s)
+{
+       if (!battery_health_s)
+               return -EINVAL;
+
+       *battery_health_s = battery.health_s;
+       return 0;
+}
+
 static void noti_batt_full(void)
 {
        static int bat_full_noti;
@@ -468,9 +486,9 @@ static void launch_health_popup(void)
        battery_pm_change_internal(DEVICED_EVENT_MISC_POPUP, LCD_DIM);
        display_lock_request_unlock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_OFF, PM_SLEEP_MARGIN);
        display_lock_request_lock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_DIM, STAY_CUR_STATE, LCD_DIM_TIME_IN_BATTERY_HEALTH);
-       if (battery.health == HEALTH_LOW)
+       if (battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW)
                battery_charge_err_low_act(NULL);
-       else if (battery.health == HEALTH_HIGH)
+       else if (battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH)
                battery_charge_err_high_act(NULL);
 
        launching_health_popup = false;
@@ -522,9 +540,9 @@ void relaunch_health_popup(void)
 
        display_lock_request_unlock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_OFF, PM_SLEEP_MARGIN);
        display_lock_request_lock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_DIM, STAY_CUR_STATE, LCD_DIM_TIME_IN_BATTERY_HEALTH);
-       if (battery.health == HEALTH_LOW)
+       if (battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW)
                battery_charge_err_low_act(NULL);
-       else if (battery.health == HEALTH_HIGH)
+       else if (battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH)
                battery_charge_err_high_act(NULL);
 
        abnormal_health_popup_timer = g_timeout_add_seconds(ABNORMAL_CHECK_TIMER_INTERVAL, health_popup_cb, NULL);
@@ -534,11 +552,11 @@ void relaunch_health_popup(void)
 
 static void check_abnormal_status(void)
 {
-       if (old_battery.health != HEALTH_LOW && old_battery.health != HEALTH_HIGH &&
-           (battery.health == HEALTH_LOW || battery.health == HEALTH_HIGH))
+       if (old_battery.health != SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW && old_battery.health != SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH &&
+           (battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW || battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH))
                update_health(DEVICE_NOTI_ON);
-       else if ((old_battery.health == HEALTH_LOW || old_battery.health == HEALTH_HIGH) &&
-               battery.health != HEALTH_LOW && battery.health != HEALTH_HIGH)
+       else if ((old_battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW || old_battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH) &&
+               battery.health != SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW && battery.health != SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH)
                update_health(DEVICE_NOTI_OFF);
 
        if (old_battery.present != PRESENT_ABNORMAL && battery.present == PRESENT_ABNORMAL)
@@ -546,9 +564,9 @@ static void check_abnormal_status(void)
        else if (battery.present != PRESENT_ABNORMAL && old_battery.present == PRESENT_ABNORMAL)
                update_present(DEVICE_NOTI_OFF);
 
-       if (old_battery.health != HEALTH_OVP && battery.health == HEALTH_OVP) {
+       if (old_battery.health != SYSCOMMON_DEVICED_BATTERY_HEALTH_OVP && battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_OVP) {
                syscommon_plugin_deviced_battery_update_health_ovp_state(DEVICED_BATTERY_NOTI_ON);
-       } else if (battery.health != HEALTH_OVP && old_battery.health == HEALTH_OVP) {
+       } else if (battery.health != SYSCOMMON_DEVICED_BATTERY_HEALTH_OVP && old_battery.health == SYSCOMMON_DEVICED_BATTERY_HEALTH_OVP) {
                syscommon_plugin_deviced_battery_update_health_ovp_state(DEVICED_BATTERY_NOTI_OFF);
        }
 }
@@ -595,7 +613,7 @@ static void check_health_status(const char *env_value)
        int len;
 
        if (env_value == NULL) {
-               battery.health = HEALTH_GOOD;
+               battery.health = SYSCOMMON_DEVICED_BATTERY_HEALTH_GOOD;
                return;
        }
 
@@ -603,15 +621,15 @@ static void check_health_status(const char *env_value)
 
        len = strlen(env_value);
        if (strncmp(env_value, OVERHEAT_NAME, len) == 0)
-               battery.health = HEALTH_HIGH;
+               battery.health = SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH;
        else if (strncmp(env_value, TEMPCOLD_NAME, len) == 0)
-               battery.health = HEALTH_LOW;
+               battery.health = SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW;
        else if (strncmp(env_value, OVERVOLT_NAME, len) == 0)
-               battery.health = HEALTH_OVP;
+               battery.health = SYSCOMMON_DEVICED_BATTERY_HEALTH_OVP;
        else if (strncmp(env_value, GOOD_NAME, len) == 0)
-               battery.health = HEALTH_GOOD;
+               battery.health = SYSCOMMON_DEVICED_BATTERY_HEALTH_GOOD;
        else
-               battery.health = HEALTH_NO_OPT;
+               battery.health = SYSCOMMON_DEVICED_BATTERY_HEALTH_NO_OPT;
 }
 
 static void check_power_source(const char *env_value)
@@ -944,7 +962,7 @@ static void battery_changed(hal_device_battery_info_s *info, void *data)
        }
 
        if (ret_val == 0) {
-               battery.health = HEALTH_GOOD;
+               battery.health = SYSCOMMON_DEVICED_BATTERY_HEALTH_GOOD;
                battery.present = PRESENT_NORMAL;
        }
 
index 078047aa96de72dd8e6209dccc8a88988c1d71c0..6a7384c40a09a451544d0cea0673f63cc435c686 100644 (file)
@@ -49,15 +49,6 @@ enum charge_now_type {
        CHARGER_CHARGING,
 };
 
-enum health_type {
-       HEALTH_NO_OPT = -1,
-       HEALTH_GOOD,
-       HEALTH_LOW,
-       HEALTH_DEAD,
-       HEALTH_HIGH,
-       HEALTH_OVP,
-};
-
 enum present_type {
        PRESENT_ABNORMAL,
        PRESENT_NORMAL,
@@ -116,6 +107,8 @@ int power_supply_broadcast(char *sig, int status);
 int battery_pm_change_internal(int pid, int s_bits);
 void relaunch_health_popup(void);
 int get_charging_status(int *val);
+int power_supply_get_battery_status_health(int *battery_health);
+int power_supply_get_battery_status_health_string(char **battery_health_s);
 
 #define CHARGER_STATUS_SIGNAL      "ChargerStatus"
 #define CHARGE_NOW_SIGNAL          "ChargeNow"
diff --git a/src/battery/resource-battery.c b/src/battery/resource-battery.c
new file mode 100644 (file)
index 0000000..5f106bc
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <libsyscommon/resource-manager.h>
+#include <libsyscommon/resource-type.h>
+#include <system/syscommon-plugin-deviced-battery-interface.h>
+#include <system/syscommon-plugin-deviced-common-interface.h>
+
+#include "power-supply.h"
+#include "shared/common.h"
+#include "core/log.h"
+
+typedef union {
+       int32_t i32;
+       int64_t i64;
+       uint32_t u32;
+       uint64_t u64;
+       double d;
+       void* p;
+       char *s;
+       bool b;
+} resource_attr_data_t;
+
+static int get_battery_attr_data(int resource_id,
+       const struct syscommon_resman_resource_attribute *attr, void *data)
+{
+       int ret = 0;
+       resource_attr_data_t attr_data = { 0, };
+
+       if (!data)
+               return -EINVAL;
+
+       switch (attr->id) {
+       case DEVICED_BATTERY_ATTR_INT_STATUS_HEALTH:
+               ret = power_supply_get_battery_status_health(&attr_data.i32);
+               break;
+       case DEVICED_BATTERY_ATTR_STRING_STATUS_HEALTH:
+               ret = power_supply_get_battery_status_health_string(&attr_data.s);
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+
+       if (ret < 0)
+               return ret;
+
+       switch (attr->type) {
+       case SYSCOMMON_RESMAN_DATA_TYPE_INT:
+               *(int32_t *) data = attr_data.i32;
+               break;
+       case SYSCOMMON_RESMAN_DATA_TYPE_INT64:
+               *(int64_t *) data = attr_data.i64;
+               break;
+       case SYSCOMMON_RESMAN_DATA_TYPE_UINT:
+               *(uint32_t *) data = attr_data.u32;
+               break;
+       case SYSCOMMON_RESMAN_DATA_TYPE_UINT64:
+               *(uint64_t *) data = attr_data.u64;
+               break;
+       case SYSCOMMON_RESMAN_DATA_TYPE_DOUBLE:
+               *(double *) data = attr_data.d;
+               break;
+       case SYSCOMMON_RESMAN_DATA_TYPE_STRING:
+               strncpy(data, attr_data.s, SYSCOMMON_RESMAN_BUFF_MAX);
+               break;
+       case SYSCOMMON_RESMAN_DATA_TYPE_PTR:
+               *(void **) data = attr_data.p;
+               break;
+       case SYSCOMMON_RESMAN_DATA_TYPE_BOOLEAN:
+               *(bool *) data = attr_data.b;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static const struct syscommon_resman_resource_attribute battery_attrs[] = {
+       {
+               .name = "DEVICED_BATTERY_ATTR_INT_STATUS_HEALTH",
+               .id = DEVICED_BATTERY_ATTR_INT_STATUS_HEALTH,
+               .type = SYSCOMMON_RESMAN_DATA_TYPE_INT,
+               .flag = SYSCOMMON_RESMAN_RESOURCE_FLAG_PUBLIC,
+               .ops = {
+                       .get = get_battery_attr_data,
+                       .is_supported = syscommon_resman_resource_attr_supported_always,
+               },
+       }, {
+               .name = "DEVICED_BATTERY_ATTR_STRING_STATUS_HEALTH",
+               .id = DEVICED_BATTERY_ATTR_STRING_STATUS_HEALTH,
+               .type = SYSCOMMON_RESMAN_DATA_TYPE_STRING,
+               .flag = SYSCOMMON_RESMAN_RESOURCE_FLAG_PUBLIC,
+               .ops = {
+                       .get = get_battery_attr_data,
+                       .is_supported = syscommon_resman_resource_attr_supported_always,
+               },
+       },
+};
+
+static const struct syscommon_resman_resource_driver deviced_battery_driver = {
+       .name           = "battery",
+       .type           = DEVICED_RESOURCE_TYPE_BATTERY,
+       .flag           = SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_COUNT_ONLY_ONE,
+       .attrs          = battery_attrs,
+       .num_attrs      = ARRAY_SIZE(battery_attrs),
+};
+SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(&deviced_battery_driver);
\ No newline at end of file
index ed10b3f0e488cb1ceca7cec81675419a365854c2..4d8b6085369d2da9194f9c245e0784578275caf8 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <vconf-keys.h>
 #include <system/syscommon-plugin-deviced-common-interface.h>
+#include <system/syscommon-plugin-deviced-battery-interface.h>
 
 #include "battery/power-supply.h"
 #include "shared/device-notifier.h"
@@ -117,10 +118,10 @@ static int changed_battery_health(void *data)
 
        _I("battery health change %d", health);
 
-       if (health == HEALTH_GOOD) {
+       if (health == SYSCOMMON_DEVICED_BATTERY_HEALTH_GOOD) {
                clear_pm_status_flag(BATTERY_FLAG);
                clear_pm_status_flag(DIMSTAY_FLAG);
-       } else if (health == HEALTH_LOW || health == HEALTH_HIGH || health == HEALTH_OVP) {
+       } else if (health == SYSCOMMON_DEVICED_BATTERY_HEALTH_LOW || health == SYSCOMMON_DEVICED_BATTERY_HEALTH_HIGH || health == SYSCOMMON_DEVICED_BATTERY_HEALTH_OVP) {
                set_pm_status_flag(BATTERY_FLAG);
                set_pm_status_flag(DIMSTAY_FLAG);
        }