resource: Add battery resource driver 56/271256/7
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 17 Feb 2022 04:12:49 +0000 (13:12 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 18 Feb 2022 07:52:00 +0000 (16:52 +0900)
Add battery resouce driver which read the Linux standard power_supply
interface to get the battery information.

[Detailed description of battery attributes]
- BATTERY_CAPACITY indicates the SoC (State of Charging)and unit is %.
- BATTERY_STATUS indicates the charging status.
- BATTERY_TEMPERATURE indicates the battery temperature.
- BATTERY_VOLTAGE_NOW indicates the voltage value now.
- BATTERY_CURRENT_NOW indicates the current value now.
- BATTERY_PRESENT indicates the connection status of battery.
- BATTERY_ONLINE indicates the connection status of charging cable.

Change-Id: I6011e155d3e401f82538abbccba7ba02e33d796b
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
CMakeLists.txt
lib/tmonitor/tmonitor.h
src/resource/resource-battery.c [new file with mode: 0644]

index 8cbd328..9cc70e7 100644 (file)
@@ -46,6 +46,7 @@ SET(SRCS
        src/resource/resource-memory.c
        src/resource/resource-display.c
        src/resource/resource-system.c
+       src/resource/resource-battery.c
        src/monitor/monitor.c
        src/monitor/monitor-thread.c
        src/monitor/monitor-command.c
index 890f0b3..d4d2fb1 100644 (file)
@@ -72,8 +72,12 @@ extern "C" {
 #define MEMORY_FAULT_AROUND_BYTES      BIT(3)
 
 #define BATTERY_CAPACITY               BIT(0)
-#define BATTERY_CHARGING_STATUS                BIT(1)
+#define BATTERY_STATUS                 BIT(1)
 #define BATTERY_TEMPERATURE            BIT(2)
+#define BATTERY_VOLTAGE_NOW            BIT(3)
+#define BATTERY_CURRENT_NOW            BIT(4)
+#define BATTERY_PRESENT                        BIT(5)
+#define BATTERY_ONLINE                 BIT(6)
 
 #define DISPLAY_FPS                    BIT(0)
 
diff --git a/src/resource/resource-battery.c b/src/resource/resource-battery.c
new file mode 100644 (file)
index 0000000..0e2364c
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * 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)