#include <string.h>
#include <errno.h>
#include <vconf.h>
-#include <hw/common.h>
-#include <hw/battery.h>
+#define _GNU_SOURCE
+#include <dlfcn.h>
#include "battery.h"
#include "battery-internal.h"
#define BATTERY_FEATURE "http://tizen.org/feature/battery"
-static struct hw_info *hw = NULL;
-static struct battery_device *battery_dev = NULL;
+#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 int is_battery_supported(void)
{
}
//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;
+static int load_battery_plugin()
+{
+ if (plugin_battery_get_info)
+ return 0;
+
+ if (!handle) {
+ handle = dlopen(BATTERY_PLUGIN, RTLD_NOW);
+ if (!handle) {
+ _E("Failed to open '%s' : %s", BATTERY_PLUGIN, dlerror());
+ return -1;
}
}
- 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;
+ plugin_battery_get_info = (int (*)(void *))dlsym(handle, GET_INFO_FUNC);
+ if (!plugin_battery_get_info) {
+ _E("Failed to get symbol '%s' : %s", GET_INFO_FUNC, dlerror());
+ return -1;
}
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 (!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.");
+ if (load_battery_plugin() < 0)
return DEVICE_ERROR_OPERATION_FAILED;
- }
- ret = battery_dev->get_current_state(battery_get_info, info);
+ ret = plugin_battery_get_info(info);
if (ret < 0) {
_E("Failed to get battery info: %d", ret);
return DEVICE_ERROR_OPERATION_FAILED;
void __attribute__ ((destructor)) battery_finalize(void)
{
- if (battery_dev) {
- if (hw && hw->close)
- hw->close((struct hw_common *)battery_dev);
+ if (handle) {
+ dlclose(handle);
+ handle = NULL;
}
}