2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #include <libsyscommon/dbus-system.h>
27 #include "hal-battery.h"
29 #include <system_info.h>
31 #define METHOD_GET_PERCENT "GetPercent"
32 #define METHOD_GET_INFO "GetBatteryInfo"
34 #define BATTERY_FEATURE "http://tizen.org/feature/battery"
37 #error LIBPATH is not defined.
40 #define BATTERY_PLUGIN LIBPATH"/libbattery-plugin.so"
41 #define GET_INFO_FUNC "battery_get_info_direct"
44 static int (*plugin_battery_get_info)(void *);
46 static int is_battery_supported(void)
51 ret = system_info_get_platform_bool(BATTERY_FEATURE, &battery_avail);
53 //LCOV_EXCL_START System Error
54 _E("Failed to get value of battery feature");
57 } else if (ret == 0 && !battery_avail) {
58 //LCOV_EXCL_START System Error
59 _D("Battery is not supported");
67 int device_battery_get_percent(int *percent)
72 return DEVICE_ERROR_INVALID_PARAMETER;
74 ret = is_battery_supported();
76 return DEVICE_ERROR_NOT_SUPPORTED;
78 ret = dbus_handle_method_sync_var(DEVICED_BUS_NAME,
79 DEVICED_PATH_BATTERY, DEVICED_INTERFACE_BATTERY,
80 METHOD_GET_PERCENT, NULL);
81 /* regard not suppoted as disconnected */
85 return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
88 return DEVICE_ERROR_NONE;
91 int device_battery_is_charging(bool *charging)
96 return DEVICE_ERROR_INVALID_PARAMETER;
98 ret = is_battery_supported();
100 return DEVICE_ERROR_NOT_SUPPORTED;
102 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &val);
103 /* regard not supported as disconnected */
105 val = 0; //LCOV_EXCL_LINE System Error
106 else if (ret < 0 || val < 0 || val > 1)
107 return DEVICE_ERROR_OPERATION_FAILED;
110 return DEVICE_ERROR_NONE;
113 int device_battery_get_level_status(device_battery_level_e *status)
118 return DEVICE_ERROR_INVALID_PARAMETER;
120 ret = is_battery_supported();
122 return DEVICE_ERROR_NOT_SUPPORTED;
124 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &val);
126 return DEVICE_ERROR_OPERATION_FAILED;
128 if (val == VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY)
129 *status = DEVICE_BATTERY_LEVEL_EMPTY;
130 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL)
131 *status = DEVICE_BATTERY_LEVEL_CRITICAL;
132 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_LOW)
133 *status = DEVICE_BATTERY_LEVEL_LOW;
134 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
135 *status = DEVICE_BATTERY_LEVEL_HIGH;
136 else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_FULL)
137 *status = DEVICE_BATTERY_LEVEL_FULL;
138 //LCOV_EXCL_START System Error
139 /* regard not supported as disconnected */
140 else if (val == -ENOTSUP)
141 *status = DEVICE_BATTERY_LEVEL_EMPTY;
144 return DEVICE_ERROR_OPERATION_FAILED;
146 return DEVICE_ERROR_NONE;
149 static int load_battery_plugin()
151 if (plugin_battery_get_info)
155 handle = dlopen(BATTERY_PLUGIN, RTLD_NOW);
157 //LCOV_EXCL_START System Error
158 _E("Failed to open '%s' : %s", BATTERY_PLUGIN, dlerror());
164 plugin_battery_get_info = (int (*)(void *))dlsym(handle, GET_INFO_FUNC);
165 if (!plugin_battery_get_info) {
166 //LCOV_EXCL_START System Error
167 _E("Failed to get symbol '%s' : %s", GET_INFO_FUNC, dlerror());
175 int device_battery_get_info_direct(struct device_battery_info *info)
180 return DEVICE_ERROR_INVALID_PARAMETER;
182 ret = is_battery_supported();
184 return DEVICE_ERROR_NOT_SUPPORTED;
186 if (load_battery_plugin() < 0)
187 return DEVICE_ERROR_OPERATION_FAILED;
189 ret = plugin_battery_get_info(info);
191 //LCOV_EXCL_START System Error
192 _E("Failed to get battery info: %d", ret);
193 return DEVICE_ERROR_OPERATION_FAILED;
197 return DEVICE_ERROR_NONE;
200 int device_battery_get_info(struct device_battery_info *info)
203 GVariant *output = NULL;
206 char *power_source = NULL;
217 return DEVICE_ERROR_INVALID_PARAMETER;
219 ret = is_battery_supported();
221 return DEVICE_ERROR_NOT_SUPPORTED;
223 output = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
224 DEVICED_PATH_BATTERY, DEVICED_INTERFACE_BATTERY,
225 METHOD_GET_INFO, NULL);
228 return DEVICE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE System Error
230 g_variant_get(output, "(isssiiiiiiii)", &ret,
231 &status, &health, &power_source,
232 &online, &present, &capacity,
233 ¤t_now, ¤t_average, &voltage_now,
234 &voltage_average, &temperature);
237 //LCOV_EXCL_START System Error
238 ret = errno_to_device_error(ret);
243 snprintf(info->status, sizeof(info->status), "%s", status);
244 snprintf(info->health, sizeof(info->health), "%s", health);
245 snprintf(info->power_source, sizeof(info->power_source), "%s", power_source);
246 info->online = online;
247 info->present = present;
248 info->capacity = capacity;
249 info->current_now = current_now;
250 info->current_average = current_average;
251 info->voltage_now = voltage_now;
252 info->voltage_average = voltage_average;
253 info->temperature = temperature;
255 ret = DEVICE_ERROR_NONE;
260 g_free(power_source);
261 g_variant_unref(output);
266 int device_battery_get_health(device_battery_health_e *health)
268 struct device_battery_info info;
273 return DEVICE_ERROR_INVALID_PARAMETER;
275 ret = is_battery_supported();
277 return DEVICE_ERROR_NOT_SUPPORTED;
279 ret = device_battery_get_info(&info);
280 if (ret != DEVICE_ERROR_NONE) {
281 //LCOV_EXCL_START System Error
282 _E("Failed to get battery info (%d)", ret);
287 len = strlen(info.health);
288 if (!strncmp(info.health, "Good", len))
289 *health = DEVICE_BATTERY_HEALTH_GOOD;
290 else if (!strncmp(info.health, "Cold", len))
291 *health = DEVICE_BATTERY_HEALTH_COLD;
292 else if (!strncmp(info.health, "Dead", len))
293 *health = DEVICE_BATTERY_HEALTH_DEAD;
294 else if (!strncmp(info.health, "Overheat", len))
295 *health = DEVICE_BATTERY_HEALTH_OVER_HEAT;
296 else if (!strncmp(info.health, "Over voltage", len))
297 *health = DEVICE_BATTERY_HEALTH_OVER_VOLTAGE;
299 return DEVICE_ERROR_OPERATION_FAILED;
301 return DEVICE_ERROR_NONE;
304 int device_battery_get_power_source(device_battery_power_source_e *source)
306 struct device_battery_info info;
311 return DEVICE_ERROR_INVALID_PARAMETER;
313 ret = is_battery_supported();
315 return DEVICE_ERROR_NOT_SUPPORTED;
317 ret = device_battery_get_info(&info);
318 if (ret != DEVICE_ERROR_NONE) {
319 //LCOV_EXCL_START System Error
320 _E("Failed to get battery info (%d)", ret);
325 len = strlen(info.power_source);
326 if (!strncmp(info.power_source, "ac", len))
327 *source = DEVICE_BATTERY_POWER_SOURCE_AC;
328 else if (!strncmp(info.power_source, "usb", len))
329 *source = DEVICE_BATTERY_POWER_SOURCE_USB;
330 else if (!strncmp(info.power_source, "wireless", len))
331 *source = DEVICE_BATTERY_POWER_SOURCE_WIRELESS;
333 *source = DEVICE_BATTERY_POWER_SOURCE_NONE;
335 return DEVICE_ERROR_NONE;
338 int device_battery_get_property(device_battery_property_e property, int *val)
340 struct device_battery_info info = {0, };
344 return DEVICE_ERROR_INVALID_PARAMETER;
346 ret = is_battery_supported();
348 return DEVICE_ERROR_NOT_SUPPORTED;
350 ret = device_battery_get_info(&info);
351 if (ret != DEVICE_ERROR_NONE) {
352 //LCOV_EXCL_START System Error
353 _E("Failed to get battery info (%d)", ret);
359 case DEVICE_BATTERY_PROPERTY_CAPACITY:
360 *val = info.capacity;
362 case DEVICE_BATTERY_PROPERTY_CURRENT_NOW:
363 *val = info.current_now;
365 case DEVICE_BATTERY_PROPERTY_CURRENT_AVERAGE:
366 *val = info.current_average;
368 case DEVICE_BATTERY_PROPERTY_VOLTAGE_NOW:
369 *val = info.voltage_now;
371 case DEVICE_BATTERY_PROPERTY_VOLTAGE_AVERAGE:
372 *val = info.voltage_average;
374 case DEVICE_BATTERY_PROPERTY_TEMPERATURE:
375 *val = info.temperature;
379 return DEVICE_ERROR_INVALID_PARAMETER;
382 return DEVICE_ERROR_NONE;
385 int device_battery_get_status(device_battery_status_e *status)
387 struct device_battery_info info;
392 return DEVICE_ERROR_INVALID_PARAMETER;
394 ret = is_battery_supported();
396 return DEVICE_ERROR_NOT_SUPPORTED;
398 ret = device_battery_get_info(&info);
399 if (ret != DEVICE_ERROR_NONE) {
400 //LCOV_EXCL_START System Error
401 _E("Failed to get battery info (%d)", ret);
406 len = strlen(info.status);
407 if (!strncmp(info.status, "Charging", len))
408 *status = DEVICE_BATTERY_STATUS_CHARGING;
409 else if (!strncmp(info.status, "Discharging", len))
410 *status = DEVICE_BATTERY_STATUS_DISCHARGING;
411 else if (!strncmp(info.status, "Full", len))
412 *status = DEVICE_BATTERY_STATUS_FULL;
413 else if (!strncmp(info.status, "Not charging", len))
414 *status = DEVICE_BATTERY_STATUS_NOT_CHARGING;
416 return DEVICE_ERROR_OPERATION_FAILED;
418 return DEVICE_ERROR_NONE;
421 void __attribute__ ((destructor)) battery_finalize(void)