4 * Copyright (c) 2016 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.
24 #include <linux/limits.h>
27 #include <hw/battery.h>
28 #include "../shared.h"
31 #define BATTERY_ROOT_PATH "/sys/class/power_supply"
33 static struct uevent_data {
34 BatteryUpdated updated_cb;
38 static int get_power_source(char **src)
45 ret = sys_get_int(BATTERY_ROOT_PATH"/"POWER_SOURCE_AC"/online", &val);
46 if (ret >= 0 && val > 0) {
47 *src = POWER_SOURCE_AC;
51 ret = sys_get_int(BATTERY_ROOT_PATH"/"POWER_SOURCE_USB"/online", &val);
52 if (ret >= 0 && val > 0) {
53 *src = POWER_SOURCE_USB;
57 ret = sys_get_int(BATTERY_ROOT_PATH"/"POWER_SOURCE_WIRELESS"/online", &val);
58 if (ret >= 0 && val > 0) {
59 *src = POWER_SOURCE_WIRELESS;
63 *src = POWER_SOURCE_NONE;
68 static void remove_not_string(char *str)
82 static void uevent_delivered(struct udev_device *dev)
84 struct battery_info info;
88 _I("POWER_SUPPLY uevent is delivered");
90 if (!udata.updated_cb) {
91 _E("POWER_SUPPLY callback is NULL");
95 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_NAME");
100 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_STATUS");
105 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_HEALTH");
110 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_ONLINE");
113 info.online = atoi(val);
115 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_PRESENT");
118 info.present = atoi(val);
120 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CAPACITY");
123 info.capacity = atoi(val);
125 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CURRENT_NOW");
128 info.current_now = atoi(val); /* uA */
130 val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CURRENT_AVG");
133 info.current_average = atoi(val); /* uA */
135 ret = get_power_source(&val);
138 info.power_source = val;
140 udata.updated_cb(&info, udata.data);
143 static struct uevent_handler uh = {
144 .subsystem = "power_supply",
145 .uevent_func = uevent_delivered,
148 static int battery_register_changed_event(
149 BatteryUpdated updated_cb, void *data)
153 ret = uevent_control_kernel_start();
155 _E("Failed to register uevent handler (%d)", ret);
159 ret = register_kernel_event_control(&uh);
161 _E("Failed to register kernel event control (%d)", ret);
163 if (udata.updated_cb == NULL) {
164 udata.updated_cb = updated_cb;
167 _E("update callback is already registered");
172 static void battery_unregister_changed_event(
173 BatteryUpdated updated_cb)
175 unregister_kernel_event_control(&uh);
176 uevent_control_kernel_stop();
177 udata.updated_cb = NULL;
181 static int battery_get_current_state(
182 BatteryUpdated updated_cb, void *data)
185 struct battery_info info;
194 info.name = BATTERY_HARDWARE_DEVICE_ID;
196 path = BATTERY_ROOT_PATH"/battery/status";
197 ret = sys_get_str(path, status, sizeof(status));
199 _E("Failed to get value of (%s, %d)", path, ret);
202 remove_not_string(status);
203 info.status = status;
205 path = BATTERY_ROOT_PATH"/battery/health";
206 ret = sys_get_str(path, health, sizeof(health));
208 _E("Failed to get value of (%s, %d)", path, ret);
211 remove_not_string(health);
212 info.health = health;
214 ret = get_power_source(&power_source);
216 _E("Failed to get power source (%d)", ret);
219 remove_not_string(power_source);
220 info.power_source = power_source;
222 path = BATTERY_ROOT_PATH"/battery/online";
223 ret = sys_get_int(path, &val);
225 _E("Failed to get value of (%s, %d)", path, ret);
230 path = BATTERY_ROOT_PATH"/battery/present";
231 ret = sys_get_int(path, &val);
233 _E("Failed to get value of (%s, %d)", path, ret);
238 path = BATTERY_ROOT_PATH"/battery/capacity";
239 ret = sys_get_int(path, &val);
241 _E("Failed to get value of (%s, %d)", path, ret);
246 path = BATTERY_ROOT_PATH"/battery/current_now";
247 ret = sys_get_int(path, &val);
249 _E("Failed to get value of (%s, %d)", path, ret);
252 info.current_now = val;
254 path = BATTERY_ROOT_PATH"/battery/current_avg";
255 ret = sys_get_int(path, &val);
257 _E("Failed to get value of (%s, %d)", path, ret);
260 info.current_average = val;
262 updated_cb(&info, data);
267 static int battery_open(struct hw_info *info,
268 const char *id, struct hw_common **common)
270 struct battery_device *battery_dev;
272 if (!info || !common)
275 battery_dev = calloc(1, sizeof(struct battery_device));
279 battery_dev->common.info = info;
280 battery_dev->register_changed_event
281 = battery_register_changed_event;
282 battery_dev->unregister_changed_event
283 = battery_unregister_changed_event;
284 battery_dev->get_current_state
285 = battery_get_current_state;
287 *common = (struct hw_common *)battery_dev;
291 static int battery_close(struct hw_common *common)
300 HARDWARE_MODULE_STRUCTURE = {
301 .magic = HARDWARE_INFO_TAG,
302 .hal_version = HARDWARE_INFO_VERSION,
303 .device_version = BATTERY_HARDWARE_DEVICE_VERSION,
304 .id = BATTERY_HARDWARE_DEVICE_ID,
306 .open = battery_open,
307 .close = battery_close,