Initialize Tizen 2.3
[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 struct popup_data {
30         char *name;
31         char *key;
32         char *value;
33 };
34
35 static int lowbat_pid = 0;
36
37 static void lowbat_cb(void *data, DBusMessage *msg, DBusError *unused)
38 {
39         DBusError err;
40         int ret, id;
41
42         if (!msg)
43                 return;
44
45         dbus_error_init(&err);
46         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &id, DBUS_TYPE_INVALID);
47         if (!ret) {
48                 _E("no message [%s:%s]", err.name, err.message);
49                 dbus_error_free(&err);
50                 return;
51         }
52
53         lowbat_pid = id;
54         _I("Created popup : %d", lowbat_pid);
55 }
56
57 static int lowbat_launch(void *data)
58 {
59         char *param[2];
60         struct popup_data * key_data = (struct popup_data *)data;
61         int ret;
62
63         param[0] = key_data->key;
64         param[1] = key_data->value;
65
66         ret = dbus_method_async_with_reply(POPUP_BUS_NAME,
67                         POPUP_PATH_LOWBAT,
68                         POPUP_INTERFACE_LOWBAT,
69                         POPUP_METHOD_LAUNCH,
70                         "ss", param, lowbat_cb, -1, NULL);
71
72         if (strncmp(key_data->value, LOWBAT_WARNING, strlen(LOWBAT_WARNING)) &&
73             strncmp(key_data->value, LOWBAT_CRITICAL, strlen(LOWBAT_CRITICAL)) &&
74             strncmp(key_data->value, LOWBAT_POWEROFF, strlen(LOWBAT_POWEROFF)))
75                 _I("%s(%d)",key_data->name, ret);
76
77         return ret;
78 }
79
80 static int lowbat_terminate(void *data)
81 {
82         int pid;
83         int ret;
84         char *param[1];
85         char buf[PATH_MAX];
86
87         printf("\n");
88
89         if (lowbat_pid <= 0)
90                 return 0;
91
92         snprintf(buf, sizeof(buf), "%d", lowbat_pid);
93         param[0] = buf;
94
95         ret = dbus_method_sync(POPUP_BUS_NAME,
96                         POPUP_PATH_APP,
97                         POPUP_IFACE_APP,
98                         POPUP_METHOD_TERMINATE, "i", param);
99         if (ret < 0)
100                 _E("FAIL: dbus_method_sync(): %d %d", ret, param);
101         lowbat_pid = 0;
102         return 0;
103 }
104
105
106 static const struct apps_ops lowbat_ops = {
107         .name   = "lowbat-syspopup",
108         .launch = lowbat_launch,
109         .terminate = lowbat_terminate,
110 };
111
112 APPS_OPS_REGISTER(&lowbat_ops)