From 66f096d7b89d94506a10610cc5d2a803f33670d5 Mon Sep 17 00:00:00 2001 From: Yunmi Ha Date: Wed, 20 Jan 2021 16:41:29 +0900 Subject: [PATCH] Apply next HAL architecture (device_battery_get_info_direct) Change-Id: I8501f3f3ffbcce78f76a0fd4b99d3901a30c5bee Signed-off-by: Yunmi Ha --- CMakeLists.txt | 2 ++ packaging/capi-system-device.spec | 2 ++ src/battery.c | 72 ++++++++++++++++----------------------- 3 files changed, 34 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c7a067..b10ff46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,8 @@ SET(PKG_MODULES gio-2.0 tracker libsyscommon + hal-api-common + hal-api-device ) INCLUDE(FindPkgConfig) diff --git a/packaging/capi-system-device.spec b/packaging/capi-system-device.spec index 8700293..5b55136 100644 --- a/packaging/capi-system-device.spec +++ b/packaging/capi-system-device.spec @@ -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 diff --git a/src/battery.c b/src/battery.c index 8983bb9..5d14920 100644 --- a/src/battery.c +++ b/src/battery.c @@ -20,29 +20,20 @@ #include #include #include -#define _GNU_SOURCE -#include +#include #include +#include #include "battery.h" #include "battery-internal.h" #include "common.h" -#include #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; } -- 2.7.4