Add battery-plugin library 00/240200/4 accepted/tizen_6.0_unified accepted/tizen_6.0_unified_hotfix tizen_6.0 tizen_6.0_hotfix accepted/tizen/6.0/unified/20201030.115609 accepted/tizen/6.0/unified/20201110.230833 accepted/tizen/6.0/unified/hotfix/20201103.003810 accepted/tizen/unified/20200806.062505 accepted/tizen/unified/20201106.130204 submit/tizen/20200805.062948 submit/tizen/20201103.015640 submit/tizen/20201104.011652 submit/tizen_6.0/20201029.205104 submit/tizen_6.0/20201109.030411 submit/tizen_6.0_hotfix/20201102.192504 submit/tizen_6.0_hotfix/20201103.114804 tizen_6.0.m2_release
authorYunmi Ha <yunmi.ha@samsung.com>
Tue, 4 Aug 2020 09:30:25 +0000 (18:30 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Tue, 4 Aug 2020 09:44:13 +0000 (18:44 +0900)
- Has 'battery_get_info_direct' function symbol.
- Load hwcommon library and get battery info direct.

Change-Id: I6e79215127275b4d2f1263fdeb93744976d819a3
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
CMakeLists.txt
battery-plugin/CMakeLists.txt [new file with mode: 0644]
battery-plugin/battery-plugin.c [new file with mode: 0644]
battery-plugin/battery-plugin.h [new file with mode: 0644]
packaging/libdevice-node.spec

index 4646bb0..2e46af6 100644 (file)
@@ -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 (file)
index 0000000..a054698
--- /dev/null
@@ -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 (file)
index 0000000..9770378
--- /dev/null
@@ -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 <stdio.h>
+#include <errno.h>
+
+#include <hw/common.h>
+#include <hw/battery.h>
+
+#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 (file)
index 0000000..c01bd77
--- /dev/null
@@ -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 <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define FEATURE_BATTERY_PLUGIN_DLOG
+#ifdef FEATURE_BATTERY_PLUGIN_DLOG
+#define LOG_TAG        "BATTERY_PLUGIN"
+#include <dlog.h>
+#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
index 809904f..d6bcb90 100644 (file)
@@ -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