Apply next HAL architecture (device_battery_get_info_direct) 73/251873/1
authorYunmi Ha <yunmi.ha@samsung.com>
Wed, 20 Jan 2021 07:41:29 +0000 (16:41 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Wed, 20 Jan 2021 07:41:29 +0000 (16:41 +0900)
Change-Id: I8501f3f3ffbcce78f76a0fd4b99d3901a30c5bee
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
CMakeLists.txt
packaging/capi-system-device.spec
src/battery.c

index 1c7a067..b10ff46 100644 (file)
@@ -18,6 +18,8 @@ SET(PKG_MODULES
                gio-2.0
                tracker
                libsyscommon
+               hal-api-common
+               hal-api-device
 )
 
 INCLUDE(FindPkgConfig)
index 8700293..5b55136 100644 (file)
@@ -14,6 +14,8 @@ BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(tracker)
 BuildRequires:  pkgconfig(libsyscommon)
+BuildRequires:  pkgconfig(hal-api-common)
+BuildRequires:  pkgconfig(hal-api-device)
 %if 0%{?gcov:1}
 BuildRequires:  lcov
 %endif
index 8983bb9..5d14920 100644 (file)
 #include <string.h>
 #include <errno.h>
 #include <vconf.h>
-#define _GNU_SOURCE
-#include <dlfcn.h>
+#include <hal/device/hal-battery.h>
 #include <libsyscommon/dbus-system.h>
+#include <system_info.h>
 
 #include "battery.h"
 #include "battery-internal.h"
 #include "common.h"
-#include <system_info.h>
 
 #define METHOD_GET_PERCENT             "GetPercent"
 #define METHOD_GET_INFO         "GetBatteryInfo"
 
 #define BATTERY_FEATURE              "http://tizen.org/feature/battery"
 
-#ifndef LIBPATH
-#error LIBPATH is not defined.
-#endif
-
-#define BATTERY_PLUGIN         LIBPATH"/libbattery-plugin.so"
-#define GET_INFO_FUNC          "battery_get_info_direct"
-
-static void *handle;
-static int (*plugin_battery_get_info)(void *);
+static bool hal_battery_loaded = false;
 
 static int is_battery_supported(void)
 {
@@ -213,30 +204,24 @@ out:
        return ret;
 }
 
-static int load_battery_plugin()
+static void battery_get_info(struct battery_info *info, void *data)
 {
-       if (plugin_battery_get_info)
-               return 0;
-
-       if (!handle) {
-               handle = dlopen(BATTERY_PLUGIN, RTLD_NOW);
-               if (!handle) {
-//LCOV_EXCL_START System Error
-                       _E("Failed to open '%s' : %s", BATTERY_PLUGIN, dlerror());
-                       return -1;
-//LCOV_EXCL_STOP
-               }
-       }
-
-       plugin_battery_get_info = (int (*)(void *))dlsym(handle, GET_INFO_FUNC);
-       if (!plugin_battery_get_info) {
-//LCOV_EXCL_START System Error
-               _E("Failed to get symbol '%s' : %s", GET_INFO_FUNC, dlerror());
-               return -1;
-//LCOV_EXCL_STOP
-       }
-
-       return 0;
+       struct device_battery_info *bat = data;
+
+    if (!info || !bat)
+        return;
+
+    snprintf(bat->status, sizeof(bat->status), "%s", info->status);
+    snprintf(bat->health, sizeof(bat->health), "%s", info->health);
+    snprintf(bat->power_source, sizeof(bat->power_source), "%s", info->power_source);
+    bat->online = info->online;
+    bat->present = info->present;
+    bat->capacity = info->capacity;
+    bat->current_now = info->current_now;
+    bat->current_average = info->current_average;
+    bat->temperature = info->temperature;
+    bat->voltage_now = info->voltage_now;
+    bat->voltage_average = info->voltage_average;
 }
 
 int device_battery_get_info_direct(struct device_battery_info *info)
@@ -250,10 +235,15 @@ int device_battery_get_info_direct(struct device_battery_info *info)
        if (!ret)
                return DEVICE_ERROR_NOT_SUPPORTED;
 
-       if (load_battery_plugin() < 0)
-               return DEVICE_ERROR_OPERATION_FAILED;
+       if (!hal_battery_loaded) {
+               ret = hal_device_battery_get_backend();
+               if (ret < 0)
+                       return DEVICE_ERROR_OPERATION_FAILED;
+
+               hal_battery_loaded = true;
+       }
 
-       ret = plugin_battery_get_info(info);
+       ret = hal_device_battery_get_current_state(battery_get_info, info);
        if (ret < 0) {
 //LCOV_EXCL_START System Error
                _E("Failed to get battery info: %d", ret);
@@ -421,8 +411,6 @@ int device_battery_get_status(device_battery_status_e *status)
 
 void __attribute__ ((destructor)) battery_finalize(void)
 {
-       if (handle) {
-               dlclose(handle);
-               handle = NULL;
-       }
+       hal_device_battery_put_backend();
+       hal_battery_loaded = false;
 }