--- /dev/null
+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)
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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