2 * PASS (Power Aware System Service) - Battery Resource Driver
4 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * @file resource-battery.c
27 #include <util/common.h>
29 #include <util/resource.h>
31 #include <tmonitor/tmonitor.h>
33 static int battery_get_info(const struct resource *res,
34 const struct resource_attribute *attr,
39 int *val = (int *)data;
41 if (!res || !attr || !data)
45 case BATTERY_ATTR_CAPACITY:
46 path = "/sys/class/power_supply/battery/capacity";
48 case BATTERY_ATTR_TEMPERATURE:
49 path = "/sys/class/power_supply/battery/temp";
51 case BATTERY_ATTR_VOLTAGE_NOW:
52 path = "/sys/class/power_supply/battery/voltage_now";
54 case BATTERY_ATTR_CURRENT_NOW:
55 path = "/sys/class/power_supply/battery/current_now";
57 case BATTERY_ATTR_PRESENT:
58 path = "/sys/class/power_supply/battery/present";
60 case BATTERY_ATTR_ONLINE:
61 path = "/sys/class/power_supply/battery/online";
67 ret = sysfs_get_int(path, val);
74 static int battery_get_status(const struct resource *res,
75 const struct resource_attribute *attr,
78 char *buf = (char *)data;
81 if (!res || !attr || !data)
84 ret = sys_get_str("/sys/class/power_supply/battery/status", buf);
91 static const struct resource_attribute battery_attrs[] = {
93 .name = "BATTERY_ATTR_CAPACITY",
94 .id = BATTERY_ATTR_CAPACITY,
95 .type = DATA_TYPE_INT,
97 .get = battery_get_info,
100 .name = "BATTERY_ATTR_STATUS",
101 .id = BATTERY_ATTR_STATUS,
102 .type = DATA_TYPE_STRING,
104 .get = battery_get_status,
107 .name = "BATTERY_ATTR_TEMPERATURE",
108 .id = BATTERY_ATTR_TEMPERATURE,
109 .type = DATA_TYPE_INT,
111 .get = battery_get_info,
114 .name = "BATTERY_ATTR_VOLTAGE_NOW",
115 .id = BATTERY_ATTR_VOLTAGE_NOW,
116 .type = DATA_TYPE_INT,
118 .get = battery_get_info,
121 .name = "BATTERY_ATTR_CURRENT_NOW",
122 .id = BATTERY_ATTR_CURRENT_NOW,
123 .type = DATA_TYPE_INT,
125 .get = battery_get_info,
128 .name = "BATTERY_ATTR_PRESENT",
129 .id = BATTERY_ATTR_PRESENT,
130 .type = DATA_TYPE_INT,
132 .get = battery_get_info,
135 .name = "BATTERY_ATTR_ONLINE",
136 .id = BATTERY_ATTR_ONLINE,
137 .type = DATA_TYPE_INT,
139 .get = battery_get_info,
144 static const struct resource_driver battery_resource_driver = {
146 .type = RESOURCE_TYPE_BATTERY,
147 .attrs = battery_attrs,
148 .num_attrs = ARRAY_SIZE(battery_attrs),
150 RESOURCE_DRIVER_REGISTER(&battery_resource_driver)