d6e5b53f49c136ddfdff70cc80bba151b0d13b4a
[platform/core/system/pass.git] / src / resource / resource-battery.c
1 /*
2  * PASS (Power Aware System Service) - Battery Resource Driver
3  *
4  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  * @file        resource-battery.c
21  * @brief       TBD
22  * @ingroup     TBD
23  */
24
25 #include <glib.h>
26
27 #include <util/common.h>
28 #include <util/log.h>
29 #include <util/resource.h>
30
31 #include <tmonitor/tmonitor.h>
32
33 static int battery_get_info(const struct resource *res,
34                                 const struct resource_attribute *attr,
35                                 void *data)
36 {
37         char *path = NULL;
38         int ret;
39         int *val = (int *)data;
40
41         if (!res || !attr || !data)
42                 return -EINVAL;
43
44         switch (attr->id) {
45         case BATTERY_ATTR_CAPACITY:
46                 path = "/sys/class/power_supply/battery/capacity";
47                 break;
48         case BATTERY_ATTR_TEMPERATURE:
49                 path = "/sys/class/power_supply/battery/temp";
50                 break;
51         case BATTERY_ATTR_VOLTAGE_NOW:
52                 path = "/sys/class/power_supply/battery/voltage_now";
53                 break;
54         case BATTERY_ATTR_CURRENT_NOW:
55                 path = "/sys/class/power_supply/battery/current_now";
56                 break;
57         case BATTERY_ATTR_PRESENT:
58                 path = "/sys/class/power_supply/battery/present";
59                 break;
60         case BATTERY_ATTR_ONLINE:
61                 path = "/sys/class/power_supply/battery/online";
62                 break;
63         default:
64                 return -EINVAL;
65         }
66
67         ret = sysfs_get_int(path, val);
68         if (ret < 0)
69                 return ret;
70
71         return 0;
72 }
73
74 static int battery_get_status(const struct resource *res,
75                                 const struct resource_attribute *attr,
76                                 void *data)
77 {
78         char *buf = (char *)data;
79         int ret;
80
81         if (!res || !attr || !data)
82                 return -EINVAL;
83
84         ret = sys_get_str("/sys/class/power_supply/battery/status", buf);
85         if (ret < 0)
86                 return ret;
87
88         return 0;
89 }
90
91 static const struct resource_attribute battery_attrs[] = {
92         {
93                 .name   = "BATTERY_ATTR_CAPACITY",
94                 .id     = BATTERY_ATTR_CAPACITY,
95                 .type   = DATA_TYPE_INT,
96                 .ops    = {
97                         .get = battery_get_info,
98                 }
99         }, {
100                 .name   = "BATTERY_ATTR_STATUS",
101                 .id     = BATTERY_ATTR_STATUS,
102                 .type   = DATA_TYPE_STRING,
103                 .ops    = {
104                         .get = battery_get_status,
105                 }
106         }, {
107                 .name   = "BATTERY_ATTR_TEMPERATURE",
108                 .id     = BATTERY_ATTR_TEMPERATURE,
109                 .type   = DATA_TYPE_INT,
110                 .ops    = {
111                         .get = battery_get_info,
112                 }
113         }, {
114                 .name   = "BATTERY_ATTR_VOLTAGE_NOW",
115                 .id     = BATTERY_ATTR_VOLTAGE_NOW,
116                 .type   = DATA_TYPE_INT,
117                 .ops    = {
118                         .get = battery_get_info,
119                 }
120         }, {
121                 .name   = "BATTERY_ATTR_CURRENT_NOW",
122                 .id     = BATTERY_ATTR_CURRENT_NOW,
123                 .type   = DATA_TYPE_INT,
124                 .ops    = {
125                         .get = battery_get_info,
126                 }
127         }, {
128                 .name   = "BATTERY_ATTR_PRESENT",
129                 .id     = BATTERY_ATTR_PRESENT,
130                 .type   = DATA_TYPE_INT,
131                 .ops    = {
132                         .get = battery_get_info,
133                 }
134         }, {
135                 .name   = "BATTERY_ATTR_ONLINE",
136                 .id     = BATTERY_ATTR_ONLINE,
137                 .type   = DATA_TYPE_INT,
138                 .ops    = {
139                         .get = battery_get_info,
140                 }
141         },
142 };
143
144 static const struct resource_driver battery_resource_driver = {
145         .name           = "BATTERY",
146         .type           = RESOURCE_TYPE_BATTERY,
147         .attrs          = battery_attrs,
148         .num_attrs      = ARRAY_SIZE(battery_attrs),
149 };
150 RESOURCE_DRIVER_REGISTER(&battery_resource_driver)