Remove hwcommon dependency 99/240199/3 accepted/tizen/unified/20200806.062501 submit/tizen/20200805.062948
authorYunmi Ha <yunmi.ha@samsung.com>
Tue, 4 Aug 2020 09:27:02 +0000 (18:27 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Wed, 5 Aug 2020 04:17:29 +0000 (13:17 +0900)
- Load 'battery-plugin.so' library dynamically
and call 'battery_get_info_direct' func.

Change-Id: I81ede014a7cf4180f02d198bf5638626df0fbe49
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
CMakeLists.txt
packaging/capi-system-device.spec
src/battery.c

index 3f87602..00c20d0 100644 (file)
@@ -17,7 +17,6 @@ SET(PKG_MODULES
                capi-system-info
                gio-2.0
                tracker
-               hwcommon
 )
 
 INCLUDE(FindPkgConfig)
@@ -35,6 +34,7 @@ ENDIF("${ARCH}" STREQUAL "arm")
 
 ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
 ADD_DEFINITIONS("-DFEATURE_DEVICE_DLOG")
+ADD_DEFINITIONS("-DLIBPATH=\"${LIB_INSTALL_DIR}\"")
 
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=${LIB_INSTALL_DIR}")
 
index df4582b..090c36f 100644 (file)
@@ -13,7 +13,6 @@ BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(tracker)
-BuildRequires:  pkgconfig(hwcommon)
 %if 0%{?gcov:1}
 BuildRequires:  lcov
 %endif
index 16e2178..7d1d28c 100644 (file)
@@ -20,8 +20,8 @@
 #include <string.h>
 #include <errno.h>
 #include <vconf.h>
-#include <hw/common.h>
-#include <hw/battery.h>
+#define _GNU_SOURCE
+#include <dlfcn.h>
 
 #include "battery.h"
 #include "battery-internal.h"
 
 #define BATTERY_FEATURE              "http://tizen.org/feature/battery"
 
-static struct hw_info *hw = NULL;
-static struct battery_device *battery_dev = NULL;
+#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 int is_battery_supported(void)
 {
@@ -209,51 +216,29 @@ out:
 }
 
 //LCOV_EXCL_START unused function
-static int load_battery_dev()
-{
-       int ret = 0;
 
-       if (!hw) {
-               ret = hw_get_info(BATTERY_HARDWARE_DEVICE_ID, (const struct hw_info **)&hw);
-               if (ret < 0) {
-                       return -ENODEV;
+static int load_battery_plugin()
+{
+       if (plugin_battery_get_info)
+               return 0;
+
+       if (!handle) {
+               handle = dlopen(BATTERY_PLUGIN, RTLD_NOW);
+               if (!handle) {
+                       _E("Failed to open '%s' : %s", BATTERY_PLUGIN, dlerror());
+                       return -1;
                }
        }
 
-       if (!hw->open) {
-               _E("Failed to open battery device: open(NULL)");
-               return -ENODEV;
-       }
-
-       ret = hw->open(hw, NULL, (struct hw_common**)&battery_dev);
-       if (ret < 0) {
-               _E("Failed to get battery device structure: %d", ret);
-               return ret;
+       plugin_battery_get_info = (int (*)(void *))dlsym(handle, GET_INFO_FUNC);
+       if (!plugin_battery_get_info) {
+               _E("Failed to get symbol '%s' : %s", GET_INFO_FUNC, dlerror());
+               return -1;
        }
 
        return 0;
 }
 
-static void battery_get_info(struct battery_info *info, void *data)
-{
-       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)
 {
        int ret;
@@ -265,18 +250,10 @@ int device_battery_get_info_direct(struct device_battery_info *info)
        if (!ret)
                return DEVICE_ERROR_NOT_SUPPORTED;
 
-       if (!battery_dev) {
-               ret = load_battery_dev();
-               if (ret < 0)
-                       return DEVICE_ERROR_OPERATION_FAILED;
-       }
-
-       if (!battery_dev->get_current_state) {
-               _E("get_current_state() is not supported by the Battery HAL.");
+       if (load_battery_plugin() < 0)
                return DEVICE_ERROR_OPERATION_FAILED;
-       }
 
-       ret = battery_dev->get_current_state(battery_get_info, info);
+       ret = plugin_battery_get_info(info);
        if (ret < 0) {
                _E("Failed to get battery info: %d", ret);
                return DEVICE_ERROR_OPERATION_FAILED;
@@ -435,8 +412,8 @@ int device_battery_get_status(device_battery_status_e *status)
 
 void __attribute__ ((destructor)) battery_finalize(void)
 {
-       if (battery_dev) {
-               if (hw && hw->close)
-                       hw->close((struct hw_common *)battery_dev);
+       if (handle) {
+               dlclose(handle);
+               handle = NULL;
        }
 }