From fc4c94ac1822bf85e3887e21e36a104f4c51a07a Mon Sep 17 00:00:00 2001 From: Yunmi Ha Date: Tue, 4 Aug 2020 18:27:02 +0900 Subject: [PATCH] Remove hwcommon dependency - Load 'battery-plugin.so' library dynamically and call 'battery_get_info_direct' func. Change-Id: I81ede014a7cf4180f02d198bf5638626df0fbe49 Signed-off-by: Yunmi Ha --- CMakeLists.txt | 2 +- packaging/capi-system-device.spec | 1 - src/battery.c | 83 ++++++++++++++------------------------- 3 files changed, 31 insertions(+), 55 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f87602..00c20d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}") diff --git a/packaging/capi-system-device.spec b/packaging/capi-system-device.spec index df4582b..090c36f 100644 --- a/packaging/capi-system-device.spec +++ b/packaging/capi-system-device.spec @@ -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 diff --git a/src/battery.c b/src/battery.c index 16e2178..7d1d28c 100644 --- a/src/battery.c +++ b/src/battery.c @@ -20,8 +20,8 @@ #include #include #include -#include -#include +#define _GNU_SOURCE +#include #include "battery.h" #include "battery-internal.h" @@ -34,8 +34,15 @@ #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; } } -- 2.7.4