4 * Copyright (c) 2012 - 2013 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.
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"
33 * get_capacity - get battery capacity value
34 * using device_get_property
36 static int get_capacity(void)
40 ret = device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CAPACITY, &val);
42 _E("fail to get battery capacity value");
48 static int update_battery(void *data)
50 static int capacity = -1;
51 static int charge_now = -1;
53 if (battery.capacity != capacity) {
54 _D("capacity is changed %d -> %d", capacity, battery.capacity);
55 capacity = battery.capacity;
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;
65 static DBusMessage *edbus_get_percent(E_DBus_Object *obj, DBusMessage *msg)
71 ret = battery.capacity;
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);
79 static DBusMessage *edbus_get_percent_raw(E_DBus_Object *obj, DBusMessage *msg)
85 ret = device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CAPACITY_RAW, &val);
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);
101 static DBusMessage *edbus_is_full(E_DBus_Object *obj, DBusMessage *msg)
103 DBusMessageIter iter;
107 ret = battery.charge_full;
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);
115 static DBusMessage *edbus_get_health(E_DBus_Object *obj, DBusMessage *msg)
117 DBusMessageIter iter;
121 ret = battery.health;
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);
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 */
137 /* battery ops init funcion */
138 static void battery_init(void *data)
142 /* init dbus interface */
143 ret = register_edbus_method(DEVICED_PATH_BATTERY,
144 edbus_methods, ARRAY_SIZE(edbus_methods));
146 _E("fail to init edbus method(%d)", ret);
148 /* get initial battery capacity */
149 battery.capacity = get_capacity();
150 battery.charge_now = POWER_SUPPLY_STATUS_UNKNOWN;
152 /* register notifier */
153 register_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, update_battery);
156 /* battery ops exit funcion */
157 static void battery_exit(void *data)
159 /* unregister notifier */
160 unregister_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, update_battery);
163 static const struct device_ops battery_device_ops = {
164 .priority = DEVICE_PRIORITY_NORMAL,
166 .init = battery_init,
167 .exit = battery_exit,
170 DEVICE_OPS_REGISTER(&battery_device_ops)