--- /dev/null
+/*
+ * PASS (Power Aware System Service) - Battery Resource Driver
+ *
+ * Copyright (c) 2022 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.
+ */
+
+/**
+ * @file resource-battery.c
+ * @brief TBD
+ * @ingroup TBD
+ */
+
+#include <glib.h>
+
+#include <util/common.h>
+#include <util/log.h>
+#include <util/resource.h>
+
+#include <tmonitor/tmonitor.h>
+
+static int battery_get_info(const struct resource *res,
+ const struct resource_attribute *attr,
+ void **data)
+{
+ char *path = NULL;
+ int ret, val = 0;
+
+ if (!res || !attr || !data)
+ return -EINVAL;
+
+ switch (attr->id) {
+ case BATTERY_CAPACITY:
+ path = "/sys/class/power_supply/battery/capacity";
+ break;
+ case BATTERY_TEMPERATURE:
+ path = "/sys/class/power_supply/battery/temp";
+ break;
+ case BATTERY_VOLTAGE_NOW:
+ path = "/sys/class/power_supply/battery/voltage_now";
+ break;
+ case BATTERY_CURRENT_NOW:
+ path = "/sys/class/power_supply/battery/current_now";
+ break;
+ case BATTERY_PRESENT:
+ path = "/sys/class/power_supply/battery/present";
+ break;
+ case BATTERY_ONLINE:
+ path = "/sys/class/power_supply/battery/online";
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = sysfs_get_int(path, &val);
+ if (ret < 0)
+ return ret;
+
+ *data = (void *)(intptr_t)val;
+
+ return 0;
+}
+
+static int battery_get_status(const struct resource *res,
+ const struct resource_attribute *attr,
+ void **data)
+{
+ char buf[BUFF_MAX];
+ int ret;
+
+ if (!res || !attr || !data)
+ return -EINVAL;
+
+ ret = sys_get_str("/sys/class/power_supply/battery/status", buf);
+ if (ret < 0)
+ return ret;
+
+ *data = strdup(buf);
+ if (!*data)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static const struct resource_attribute battery_attrs[] = {
+ {
+ .name = "BATTERY_CAPACITY",
+ .id = BATTERY_CAPACITY,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = battery_get_info,
+ }
+ }, {
+ .name = "BATTERY_STATUS",
+ .id = BATTERY_STATUS,
+ .type = DATA_TYPE_STRING,
+ .ops = {
+ .get = battery_get_status,
+ }
+ }, {
+ .name = "BATTERY_TEMPERATURE",
+ .id = BATTERY_TEMPERATURE,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = battery_get_info,
+ }
+ }, {
+ .name = "BATTERY_VOLTAGE_NOW",
+ .id = BATTERY_VOLTAGE_NOW,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = battery_get_info,
+ }
+ }, {
+ .name = "BATTERY_CURRENT_NOW",
+ .id = BATTERY_CURRENT_NOW,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = battery_get_info,
+ }
+ }, {
+ .name = "BATTERY_PRESENT",
+ .id = BATTERY_PRESENT,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = battery_get_info,
+ }
+ }, {
+ .name = "BATTERY_ONLINE",
+ .id = BATTERY_ONLINE,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = battery_get_info,
+ }
+ },
+};
+
+static const struct resource_driver battery_resource_driver = {
+ .name = "BATTERY",
+ .type = RESOURCE_TYPE_BATTERY,
+ .attrs = battery_attrs,
+ .num_attrs = ARRAY_SIZE(battery_attrs),
+ .flags = RESOURCE_DRIVER_NO_DEVICE,
+};
+RESOURCE_DRIVER_REGISTER(&battery_resource_driver)