Initialize Tizen 2.3
[framework/system/deviced.git] / src / battery / battery.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 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 #define USE_LOGD 1
20
21 #include <stdbool.h>
22 #include <assert.h>
23 #include <limits.h>
24 #include "core/log.h"
25 #include "core/devices.h"
26 #include "core/udev.h"
27 #include "device-node.h"
28 #include "core/device-handler.h"
29 #include "core/device-notifier.h"
30 #include "core/edbus-handler.h"
31
32 /*
33  * get_capacity - get battery capacity value
34  * using device_get_property
35  */
36 static int get_capacity(void)
37 {
38         int val, ret;
39
40         ret = device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CAPACITY, &val);
41         if (ret < 0) {
42                 _E("fail to get battery capacity value");
43                 val = ret;
44         }
45         return val;
46 }
47
48 static int update_battery(void *data)
49 {
50         static int capacity = -1;
51         static int charge_now = -1;
52
53         if (battery.capacity != capacity) {
54                 _D("capacity is changed %d -> %d", capacity, battery.capacity);
55                 capacity = battery.capacity;
56         }
57
58         if (battery.charge_now != charge_now) {
59                 _D("chaging status is changed %d -> %d", charge_now, battery.charge_now);
60                 charge_now = battery.charge_now;
61         }
62         return 0;
63 }
64
65 static DBusMessage *edbus_get_percent(E_DBus_Object *obj, DBusMessage *msg)
66 {
67         DBusMessageIter iter;
68         DBusMessage *reply;
69         int ret;
70
71         ret = battery.capacity;
72
73         reply = dbus_message_new_method_return(msg);
74         dbus_message_iter_init_append(reply, &iter);
75         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
76         return reply;
77 }
78
79 static DBusMessage *edbus_get_percent_raw(E_DBus_Object *obj, DBusMessage *msg)
80 {
81         DBusMessageIter iter;
82         DBusMessage *reply;
83         int ret, val;
84
85         ret = device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CAPACITY_RAW, &val);
86         if (ret < 0)
87                 goto exit;
88
89         if (val > 10000)
90                 val = 10000;
91
92         ret = val;
93
94 exit:
95         reply = dbus_message_new_method_return(msg);
96         dbus_message_iter_init_append(reply, &iter);
97         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
98         return reply;
99 }
100
101 static DBusMessage *edbus_is_full(E_DBus_Object *obj, DBusMessage *msg)
102 {
103         DBusMessageIter iter;
104         DBusMessage *reply;
105         int ret;
106
107         ret = battery.charge_full;
108
109         reply = dbus_message_new_method_return(msg);
110         dbus_message_iter_init_append(reply, &iter);
111         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
112         return reply;
113 }
114
115 static DBusMessage *edbus_get_health(E_DBus_Object *obj, DBusMessage *msg)
116 {
117         DBusMessageIter iter;
118         DBusMessage *reply;
119         int ret;
120
121         ret = battery.health;
122
123         reply = dbus_message_new_method_return(msg);
124         dbus_message_iter_init_append(reply, &iter);
125         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
126         return reply;
127 }
128
129 static const struct edbus_method edbus_methods[] = {
130         { "GetPercent",    NULL,   "i", edbus_get_percent },
131         { "GetPercentRaw", NULL,   "i", edbus_get_percent_raw },
132         { "IsFull",        NULL,   "i", edbus_is_full },
133         { "GetHealth",     NULL,   "i", edbus_get_health },
134         /* Add methods here */
135 };
136
137 /* battery ops init funcion */
138 static void battery_init(void *data)
139 {
140         int ret;
141
142         /* init dbus interface */
143         ret = register_edbus_method(DEVICED_PATH_BATTERY,
144                         edbus_methods, ARRAY_SIZE(edbus_methods));
145         if (ret < 0)
146                 _E("fail to init edbus method(%d)", ret);
147
148         /* get initial battery capacity */
149         battery.capacity = get_capacity();
150         battery.charge_now = POWER_SUPPLY_STATUS_UNKNOWN;
151
152         /* register notifier */
153         register_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, update_battery);
154 }
155
156 /* battery ops exit funcion */
157 static void battery_exit(void *data)
158 {
159         /* unregister notifier */
160         unregister_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, update_battery);
161 }
162
163 static const struct device_ops battery_device_ops = {
164         .priority = DEVICE_PRIORITY_NORMAL,
165         .name     = "battery",
166         .init     = battery_init,
167         .exit     = battery_exit,
168 };
169
170 DEVICE_OPS_REGISTER(&battery_device_ops)