From 1c1758f2edc98c84d0687ddc5b034ff7640ae4ac Mon Sep 17 00:00:00 2001 From: Yunmi Ha Date: Tue, 4 Aug 2020 18:30:25 +0900 Subject: [PATCH] Add battery-plugin library - Has 'battery_get_info_direct' function symbol. - Load hwcommon library and get battery info direct. Change-Id: I6e79215127275b4d2f1263fdeb93744976d819a3 Signed-off-by: Yunmi Ha --- CMakeLists.txt | 3 + battery-plugin/CMakeLists.txt | 28 ++++++++++ battery-plugin/battery-plugin.c | 120 ++++++++++++++++++++++++++++++++++++++++ battery-plugin/battery-plugin.h | 45 +++++++++++++++ packaging/libdevice-node.spec | 1 + 5 files changed, 197 insertions(+) create mode 100644 battery-plugin/CMakeLists.txt create mode 100644 battery-plugin/battery-plugin.c create mode 100644 battery-plugin/battery-plugin.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4646bb0..2e46af6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,6 +69,9 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/mtp-responder-dummy/descs DESTINATION INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/mtp-responder-dummy/mtp-responder-dummy.socket DESTINATION /usr/lib/systemd/system) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/mtp-responder-dummy/mtp-responder-dummy.service DESTINATION /usr/lib/systemd/system) +ADD_SUBDIRECTORY(battery-plugin) +ADD_DEPENDENCIES(battery-plugin hwcommon) + IF(BUILD_GTESTS STREQUAL on) ADD_SUBDIRECTORY(unittest) ENDIF() diff --git a/battery-plugin/CMakeLists.txt b/battery-plugin/CMakeLists.txt new file mode 100644 index 0000000..a054698 --- /dev/null +++ b/battery-plugin/CMakeLists.txt @@ -0,0 +1,28 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(battery-plugin C) + +LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/..) + +SET(SRCS + battery-plugin.c) + +SET(PKG_MODULES + dlog +) + +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(pkgs REQUIRED ${PKG_MODULES}) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Werror -Wl,-zdefs -fPIC") + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +ADD_LIBRARY(${PROJECT_NAME} MODULE ${SRCS}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} "hwcommon -ldl") +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) diff --git a/battery-plugin/battery-plugin.c b/battery-plugin/battery-plugin.c new file mode 100644 index 0000000..9770378 --- /dev/null +++ b/battery-plugin/battery-plugin.c @@ -0,0 +1,120 @@ +/* + * battery-plugin + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include + +#include "battery-plugin.h" + +#define BATTERY_INFO_MAX 32 + +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; + int current_now; + int current_average; + int voltage_now; + int voltage_average; + int temperature; +}; + +static struct hw_info *hw = NULL; +static struct battery_device *battery_dev = NULL; + +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 __attribute__((visibility("default"))) battery_get_info_direct(void *battery_info) +{ + struct device_battery_info *info = battery_info; + int ret = 0; + + if (!info) + return -EINVAL; + + if (!battery_dev) { + ret = load_battery_dev(); + if (ret < 0) + return ret; + + if (!battery_dev) + return -ENODEV; + } + + if (!battery_dev->get_current_state) { + _E("get_current_state() is not supported by the Battery HAL."); + return -ENODEV; + } + + ret = battery_dev->get_current_state(battery_get_info, info); + if (ret < 0) { + _E("Failed to get battery info: %d", ret); + return -EPERM; + } + + return 0; +} diff --git a/battery-plugin/battery-plugin.h b/battery-plugin/battery-plugin.h new file mode 100644 index 0000000..c01bd77 --- /dev/null +++ b/battery-plugin/battery-plugin.h @@ -0,0 +1,45 @@ +/* + * battery-plugin + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef __DEV_BATTERY_PLUGIN_H__ +#define __DEV_BATTERY_PLUGIN_H__ + +#include +#include +#include +#include +#include +#include + +#define FEATURE_BATTERY_PLUGIN_DLOG +#ifdef FEATURE_BATTERY_PLUGIN_DLOG +#define LOG_TAG "BATTERY_PLUGIN" +#include +#define _I(fmt, args...) SLOGI(fmt, ##args) +#define _D(fmt, args...) SLOGD(fmt, ##args) +#define _E(fmt, args...) SLOGE(fmt, ##args) +#else +#define _I(x, ...) do { } while (0) +#define _D(x, ...) do { } while (0) +#define _E(x, ...) do { } while (0) +#endif + +int battery_get_info_direct(void *battery_info); + +#endif diff --git a/packaging/libdevice-node.spec b/packaging/libdevice-node.spec index 809904f..d6bcb90 100644 --- a/packaging/libdevice-node.spec +++ b/packaging/libdevice-node.spec @@ -54,6 +54,7 @@ make %{?jobs:-j%jobs} %manifest %{name}.manifest %license LICENSE.Apache-2.0 %{_libdir}/*.so.* +%{_libdir}/libbattery-plugin.so %{_unitdir}/mtp-responder-dummy.socket %{_unitdir}/mtp-responder-dummy.service /etc/mtp-responder-dummy/strs -- 2.7.4