tizen 2.3 release
[framework/system/deviced.git] / src / apps / lowbat.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 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
20 #include "core/log.h"
21 #include "core/common.h"
22 #include "apps.h"
23 #include "core/edbus-handler.h"
24
25 #define LOWBAT_WARNING  "warning"
26 #define LOWBAT_CRITICAL  "critical"
27 #define LOWBAT_POWEROFF "poweroff"
28
29 #define DBUS_POPUP_PATH_LOWBAT          POPUP_OBJECT_PATH"/Battery"
30 #define DBUS_POPUP_INTERFACE_LOWBAT     POPUP_INTERFACE_NAME".Battery"
31
32 #define METHOD_LOWBAT_POPUP             "BatteryLow"
33 #define METHOD_LOWBAT_POPUP_DISABLE     "LowBatteryDisable"
34 #define METHOD_LOWBAT_POPUP_ENABLE      "LowBatteryEnable"
35
36 struct popup_data {
37         char *name;
38         char *key;
39         char *value;
40 };
41
42 static int lowbat_pid = 0;
43 static int lowbat_popup = APPS_ENABLE;
44
45 static void lowbat_cb(void *data, DBusMessage *msg, DBusError *unused)
46 {
47         DBusError err;
48         int ret, id;
49
50         if (!msg)
51                 return;
52
53         dbus_error_init(&err);
54         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &id, DBUS_TYPE_INVALID);
55         if (!ret) {
56                 _E("no message [%s:%s]", err.name, err.message);
57                 dbus_error_free(&err);
58                 return;
59         }
60
61         lowbat_pid = id;
62         _I("Created popup : %d", lowbat_pid);
63 }
64
65 static int lowbat_launch(void *data)
66 {
67         char *param[2];
68         char *dest, *path, *iface, *method;
69         struct popup_data * key_data = (struct popup_data *)data;
70         int ret;
71
72         if (lowbat_popup == APPS_DISABLE) {
73                 _D("skip lowbat popup control");
74                 return 0;
75         }
76
77         param[0] = key_data->key;
78         param[1] = key_data->value;
79         if (strncmp(key_data->value, "lowbattery_warning", 18) == 0 ||
80             strncmp(key_data->value, "lowbattery_critical", 19) == 0) {
81                 dest = POPUP_BUS_NAME;
82                 path = DBUS_POPUP_PATH_LOWBAT;
83                 iface = DBUS_POPUP_INTERFACE_LOWBAT;
84                 method = METHOD_LOWBAT_POPUP;
85         } else {
86                 dest = POPUP_BUS_NAME;
87                 path = POPUP_PATH_LOWBAT;
88                 iface = POPUP_INTERFACE_LOWBAT;
89                 method = POPUP_METHOD_LAUNCH;
90         }
91         ret = dbus_method_async_with_reply(dest, path, iface, method,
92                         "ss", param, lowbat_cb, -1, NULL);
93
94         if (strncmp(key_data->value, LOWBAT_WARNING, strlen(LOWBAT_WARNING)) &&
95             strncmp(key_data->value, LOWBAT_CRITICAL, strlen(LOWBAT_CRITICAL)) &&
96             strncmp(key_data->value, LOWBAT_POWEROFF, strlen(LOWBAT_POWEROFF)))
97                 _I("%s(%d)",key_data->name, ret);
98
99         return ret;
100 }
101
102 static int lowbat_terminate(void *data)
103 {
104         int pid;
105         int ret;
106         char *param[1];
107         char buf[PATH_MAX];
108
109         printf("\n");
110
111         if (lowbat_pid <= 0)
112                 return 0;
113
114         snprintf(buf, sizeof(buf), "%d", lowbat_pid);
115         param[0] = buf;
116
117         ret = dbus_method_sync(POPUP_BUS_NAME,
118                         POPUP_PATH_APP,
119                         POPUP_IFACE_APP,
120                         POPUP_METHOD_TERMINATE, "i", param);
121         if (ret < 0)
122                 _E("FAIL: dbus_method_sync(): %d %d", ret, param);
123         lowbat_pid = 0;
124         return 0;
125 }
126
127 static DBusMessage *dbus_disable_popup(E_DBus_Object *obj, DBusMessage *msg)
128 {
129         DBusMessageIter iter;
130         DBusMessage *reply;
131
132         lowbat_popup = APPS_DISABLE;
133         reply = dbus_message_new_method_return(msg);
134         dbus_message_iter_init_append(reply, &iter);
135         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &lowbat_popup);
136         return reply;
137 }
138
139 static DBusMessage *dbus_enable_popup(E_DBus_Object *obj, DBusMessage *msg)
140 {
141         DBusMessageIter iter;
142         DBusMessage *reply;
143
144         lowbat_popup = APPS_ENABLE;
145         reply = dbus_message_new_method_return(msg);
146         dbus_message_iter_init_append(reply, &iter);
147         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &lowbat_popup);
148         return reply;
149 }
150
151 static const struct edbus_method edbus_methods[] = {
152         { METHOD_LOWBAT_POPUP_DISABLE,  NULL, "i", dbus_disable_popup },
153         { METHOD_LOWBAT_POPUP_ENABLE,   NULL, "i", dbus_enable_popup },
154 };
155
156 static void lowbat_init(void)
157 {
158         int ret;
159
160         ret = register_edbus_method(DEVICED_PATH_APPS, edbus_methods, ARRAY_SIZE(edbus_methods));
161         if (ret < 0)
162                 _E("fail to init edbus method(%d)", ret);
163 }
164
165 static const struct apps_ops lowbat_ops = {
166         .name      = "lowbat-syspopup",
167         .init      = lowbat_init,
168         .launch    = lowbat_launch,
169         .terminate = lowbat_terminate,
170 };
171
172 APPS_OPS_REGISTER(&lowbat_ops)