device: Merge public api on Tizen 2.3 into tizen branch
[platform/core/api/device.git] / src / battery.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <vconf.h>
23
24 #include "battery.h"
25 #include "common.h"
26 #include "dbus.h"
27
28 #define METHOD_GET_PERCENT              "GetPercent"
29
30 int device_battery_get_percent(int *percent)
31 {
32         int ret;
33
34         if (!percent)
35                 return DEVICE_ERROR_INVALID_PARAMETER;
36
37         ret = dbus_method_sync(DEVICED_BUS_NAME,
38                         DEVICED_PATH_BATTERY, DEVICED_INTERFACE_BATTERY,
39                         METHOD_GET_PERCENT, NULL, NULL);
40         if (ret < 0)
41                 return errno_to_device_error(ret);
42
43         *percent = ret;
44         return DEVICE_ERROR_NONE;
45 }
46
47 int device_battery_is_charging(bool *charging)
48 {
49         int ret, val;
50
51         if (!charging)
52                 return DEVICE_ERROR_INVALID_PARAMETER;
53
54         ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &val);
55         if (ret < 0 || val < 0 || val > 1)
56                 return DEVICE_ERROR_OPERATION_FAILED;
57
58         *charging = val;
59         return DEVICE_ERROR_NONE;
60 }
61
62 int device_battery_get_level_status(device_battery_level_e *status)
63 {
64         int val, ret;
65
66         if (!status)
67                 return DEVICE_ERROR_INVALID_PARAMETER;
68
69         ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &val);
70         if (ret < 0)
71                 return DEVICE_ERROR_OPERATION_FAILED;
72
73         if (val == VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY)
74                 *status = DEVICE_BATTERY_LEVEL_EMPTY;
75         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL)
76                 *status = DEVICE_BATTERY_LEVEL_CRITICAL;
77         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_LOW)
78                 *status = DEVICE_BATTERY_LEVEL_LOW;
79         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
80                 *status = DEVICE_BATTERY_LEVEL_HIGH;
81         else if (val == VCONFKEY_SYSMAN_BAT_LEVEL_FULL)
82                 *status = DEVICE_BATTERY_LEVEL_FULL;
83         else
84                 return DEVICE_ERROR_OPERATION_FAILED;
85
86         return DEVICE_ERROR_NONE;
87 }