From: Yunmi Ha Date: Fri, 17 Jul 2020 08:52:05 +0000 (+0900) Subject: battery: Add 'device_battery_get_info_direct' internal API X-Git-Tag: submit/tizen/20200721.112435^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F87%2F238787%2F4;p=platform%2Fcore%2Fapi%2Fdevice.git battery: Add 'device_battery_get_info_direct' internal API - Change battery_info structure to device_battery_info - Get battery info directly from HAL Change-Id: Id4892bedea89b9e35932f98c72c926512dca3d2e Signed-off-by: Yunmi Ha --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 75181f5..3f87602 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ SET(PKG_MODULES capi-system-info gio-2.0 tracker + hwcommon ) INCLUDE(FindPkgConfig) diff --git a/include/battery-internal.h b/include/battery-internal.h index 804b5dd..19b8768 100644 --- a/include/battery-internal.h +++ b/include/battery-internal.h @@ -32,12 +32,12 @@ extern "C" { * @addtogroup CAPI_SYSTEM_DEVICE_BATTERY_MODULE * @{ */ -#define BATTER_INFO_MAX 32 +#define BATTERY_INFO_MAX 32 -struct battery_info { - char status[BATTER_INFO_MAX]; - char health[BATTER_INFO_MAX]; - char power_source[BATTER_INFO_MAX]; +struct device_battery_info { + char status[BATTERY_INFO_MAX]; + char health[BATTERY_INFO_MAX]; + char power_source[BATTERY_INFO_MAX]; int online; int present; int capacity; @@ -61,7 +61,22 @@ struct battery_info { * @retval #DEVICE_ERROR_OPERATION_FAILED Operation failed * @retval #DEVICE_ERROR_NOT_SUPPORTED Not supported device */ -int device_battery_get_info(struct battery_info *info); +int device_battery_get_info(struct device_battery_info *info); + +/** + * @brief Gets the battery status.. + * @since_tizen 6.0 + * @param[out] info battery status information.\n + * The current_average is INT_MAX if battery hw does not provide current average.\n + * The voltage_average is INT_MAX if battery hw does not provide voltage average. + * @return @c 0 on success, + * otherwise a negative error value + * @retval #DEVICE_ERROR_NONE Successful + * @retval #DEVICE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #DEVICE_ERROR_OPERATION_FAILED Operation failed + * @retval #DEVICE_ERROR_NOT_SUPPORTED Not supported device + */ +int device_battery_get_info_direct(struct device_battery_info *info); /** * @} diff --git a/packaging/capi-system-device.spec b/packaging/capi-system-device.spec index 090c36f..df4582b 100644 --- a/packaging/capi-system-device.spec +++ b/packaging/capi-system-device.spec @@ -13,6 +13,7 @@ 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 de0d687..16e2178 100644 --- a/src/battery.c +++ b/src/battery.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include "battery.h" #include "battery-internal.h" @@ -32,6 +34,9 @@ #define BATTERY_FEATURE "http://tizen.org/feature/battery" +static struct hw_info *hw = NULL; +static struct battery_device *battery_dev = NULL; + static int is_battery_supported(void) { int ret; @@ -135,7 +140,7 @@ int device_battery_get_level_status(device_battery_level_e *status) return DEVICE_ERROR_NONE; } -int device_battery_get_info(struct battery_info *info) +int device_battery_get_info(struct device_battery_info *info) { int ret; GVariant *output = NULL; @@ -203,9 +208,87 @@ out: return ret; } +//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; + } + } + + 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; + } + + 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; + + if (!info) + return DEVICE_ERROR_INVALID_PARAMETER; + + ret = is_battery_supported(); + 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."); + return DEVICE_ERROR_OPERATION_FAILED; + } + + ret = battery_dev->get_current_state(battery_get_info, info); + if (ret < 0) { + _E("Failed to get battery info: %d", ret); + return DEVICE_ERROR_OPERATION_FAILED; + } + + return DEVICE_ERROR_NONE; +} +//LCOV_EXCL_STOP + int device_battery_get_health(device_battery_health_e *health) { - struct battery_info info; + struct device_battery_info info; int ret; size_t len; @@ -241,7 +324,7 @@ int device_battery_get_health(device_battery_health_e *health) int device_battery_get_power_source(device_battery_power_source_e *source) { - struct battery_info info; + struct device_battery_info info; int ret; size_t len; @@ -273,7 +356,7 @@ int device_battery_get_power_source(device_battery_power_source_e *source) int device_battery_get_property(device_battery_property_e property, int *val) { - struct battery_info info = {0, }; + struct device_battery_info info = {0, }; int ret; if (!val) @@ -318,7 +401,7 @@ int device_battery_get_property(device_battery_property_e property, int *val) int device_battery_get_status(device_battery_status_e *status) { - struct battery_info info; + struct device_battery_info info; int ret; size_t len; @@ -349,3 +432,11 @@ int device_battery_get_status(device_battery_status_e *status) return DEVICE_ERROR_NONE; } + +void __attribute__ ((destructor)) battery_finalize(void) +{ + if (battery_dev) { + if (hw && hw->close) + hw->close((struct hw_common *)battery_dev); + } +}