[mobile] Change API launching an application : aul_launch_app() -> app_control_send_l...
[apps/native/starter.git] / src / dbus_util.c
1 /*
2  * Copyright (c) 2000 - 2015 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 #include <E_DBus.h>
18 #include <dbus/dbus-glib.h>
19 #include <dbus/dbus-glib-lowlevel.h>
20
21 #include "dbus_util.h"
22 #include "util.h"
23
24 #define DBUS_HOME_BUS_NAME "org.tizen.coreapps.home"
25 #define DBUS_HOME_RAISE_PATH "/Org/Tizen/Coreapps/home/raise"
26 #define DBUS_HOME_RAISE_INTERFACE DBUS_HOME_BUS_NAME".raise"
27 #define DBUS_HOME_RAISE_MEMBER "homeraise"
28
29 #define DBUS_REPLY_TIMEOUT (120 * 1000)
30
31 #define POWEROFF_BUS_NAME       "org.tizen.system.popup"
32 #define POWEROFF_OBJECT_PATH    "/Org/Tizen/System/Popup/Poweroff"
33 #define POWEROFF_INTERFACE_NAME POWEROFF_BUS_NAME".Poweroff"
34 #define METHOD_POWEROFF_NAME    "PopupLaunch"
35
36 #define CPU_BOOSTER_OBJECT_PATH DEVICED_OBJECT_PATH"/PmQos"
37 #define CPU_BOOSTER_INTERFACE   DEVICED_BUS_NAME".PmQos"
38 #define METHOD_CPU_BOOSTER              "AppLaunchHome"
39 #define DBUS_CPU_BOOSTER_SEC    200
40
41 #define METHOD_LOCK_PMQOS_NAME  "LockScreen"
42 #define DBUS_LOCK_PMQOS_SEC (2 * 1000)
43
44 static struct _info {
45         DBusConnection *connection;
46 } s_info = {
47         .connection = NULL,
48 };
49
50
51
52 static DBusConnection *_dbus_connection_get(void)
53 {
54         DBusError derror;
55         DBusConnection *connection = NULL;
56
57         if (s_info.connection) {
58                 return s_info.connection;
59         }
60
61         _W("no connection for dbus. get dbus connection");
62
63         dbus_error_init(&derror);
64         connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &derror);
65         if (!connection) {
66                 _E("Failed to get dbus connection:%s", derror.message);
67                 dbus_error_free(&derror);
68                 return NULL;
69         }
70         dbus_connection_setup_with_g_main(connection, NULL);
71         dbus_error_free(&derror);
72
73         s_info.connection = connection;
74
75         return s_info.connection;
76 }
77
78
79
80 static int _append_variant(DBusMessageIter *iter, const char *sig, char *param[])
81 {
82         char *ch;
83         int i;
84         int int_type;
85         uint64_t int64_type;
86
87         if (!sig || !param)
88                 return 0;
89
90         for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
91                 switch (*ch) {
92                 case 'i':
93                         int_type = atoi(param[i]);
94                         dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
95                         break;
96                 case 'u':
97                         int_type = atoi(param[i]);
98                         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
99                         break;
100                 case 't':
101                         int64_type = (uint64_t) atoi(param[i]);
102                         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
103                         break;
104                 case 's':
105                         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &param[i]);
106                         break;
107                 default:
108                         return -EINVAL;
109                 }
110         }
111
112         return 0;
113 }
114
115
116
117 static DBusMessage *_invoke_dbus_method_sync(const char *dest, const char *path,
118                 const char *interface, const char *method,
119                 const char *sig, char *param[])
120 {
121         DBusConnection *conn = NULL;
122         DBusMessage *msg;
123         DBusMessageIter iter;
124         DBusMessage *reply;
125         DBusError err;
126         int r;
127
128         conn = (DBusConnection *)_dbus_connection_get();
129         if (!conn) {
130                 _E("dbus_bus_get error");
131                 return NULL;
132         }
133
134         msg = dbus_message_new_method_call(dest, path, interface, method);
135         if (!msg) {
136                 _E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
137                 return NULL;
138         }
139
140         dbus_message_iter_init_append(msg, &iter);
141         r = _append_variant(&iter, sig, param);
142         if (r < 0) {
143                 _E("append_variant error(%d)", r);
144                 dbus_message_unref(msg);
145                 return NULL;
146         }
147
148         dbus_error_init(&err);
149
150         reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
151         dbus_message_unref(msg);
152         if (!reply) {
153                 _E("dbus_connection_send error(%s:%s)", err.name, err.message);
154                 dbus_error_free(&err);
155                 return NULL;
156         }
157
158         return reply;
159 }
160
161
162
163 static int _invoke_dbus_method_async(const char *dest, const char *path,
164                 const char *interface, const char *method,
165                 const char *sig, char *param[])
166 {
167         DBusConnection *conn;
168         DBusMessage *msg;
169         DBusMessageIter iter;
170         int r;
171
172         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
173         if (!conn) {
174                 _E("dbus_bus_get error");
175                 return 0;
176         }
177
178         msg = dbus_message_new_method_call(dest, path, interface, method);
179         if (!msg) {
180                 _E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
181                 return 0;
182         }
183
184         dbus_message_iter_init_append(msg, &iter);
185         r = _append_variant(&iter, sig, param);
186         if (r < 0) {
187                 _E("append_variant error(%d)", r);
188                 dbus_message_unref(msg);
189                 return 0;
190         }
191
192         r = dbus_connection_send(conn, msg, NULL);
193         dbus_message_unref(msg);
194         if (r != TRUE) {
195                 _E("dbus_connection_send error(%s:%s:%s-%s)", dest, path, interface, method);
196                 return 0;
197         }
198
199         _D("dbus_connection_send, ret=%d", r);
200         return 1;
201 }
202
203
204
205 #if 0
206 static int _dbus_message_send(const char *path, const char *interface, const char *member)
207 {
208         int ret = 0;
209         DBusMessage *msg = NULL;
210         DBusConnection *conn = NULL;
211
212         conn = (DBusConnection *)_dbus_connection_get();
213         if (!conn) {
214                 _E("dbus_bus_get error");
215                 return -1;
216         }
217
218         msg = dbus_message_new_signal(path, interface, member);
219         if (!msg) {
220                 _E("dbus_message_new_signal(%s:%s-%s)", path, interface, member);
221                 return -1;
222         }
223
224         ret = dbus_connection_send(conn, msg, NULL); //async call
225         dbus_message_unref(msg);
226         if (ret != TRUE) {
227                 _E("dbus_connection_send error(%s:%s-%s)", path, interface, member);
228                 return -ECOMM;
229         }
230         _D("dbus_connection_send, ret=%d", ret);
231         return 0;
232 }
233 #endif
234
235
236
237 int dbus_util_send_oomadj(int pid, int oom_adj_value)
238 {
239         DBusError err;
240         DBusMessage *msg;
241         char *pa[4];
242         char buf1[BUF_SIZE_16];
243         char buf2[BUF_SIZE_16];
244         int ret, val;
245
246         if(pid <= 0){
247                 _E("Pid is invalid");
248                 return -1;
249         }
250
251         snprintf(buf1, sizeof(buf1), "%d", pid);
252         snprintf(buf2, sizeof(buf2), "%d", oom_adj_value);
253
254         pa[0] = DEVICED_SET_METHOD;
255         pa[1] = "2";
256         pa[2] = buf1;
257         pa[3] = buf2;
258
259         msg = _invoke_dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH, DEVICED_INTERFACE, DEVICED_SET_METHOD, "siss", pa);
260         if (!msg)
261                 return -EBADMSG;
262
263         dbus_error_init(&err);
264
265         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
266         if (!ret) {
267                 _E("no message : [%s:%s]", err.name, err.message);
268                 val = -EBADMSG;
269         }
270
271         dbus_message_unref(msg);
272         dbus_error_free(&err);
273
274         _D("%s-%s : %d", DEVICED_INTERFACE, pa[0], val);
275         return val;
276 }
277
278
279
280 void dbus_util_send_cpu_booster_signal(void)
281 {
282         int ret = 0;
283         char *arr[1];
284         char val[BUF_SIZE_32];
285
286         snprintf(val, sizeof(val), "%d", DBUS_CPU_BOOSTER_SEC);
287         arr[0] = val;
288
289         ret = _invoke_dbus_method_async(DEVICED_BUS_NAME, CPU_BOOSTER_OBJECT_PATH, CPU_BOOSTER_INTERFACE,
290                         METHOD_CPU_BOOSTER, "i", arr);
291         ret_if(!ret);
292
293         _D("%s-%s", CPU_BOOSTER_INTERFACE, METHOD_CPU_BOOSTER);
294 }
295
296
297
298 void dbus_util_send_poweroff_signal(void)
299 {
300         int ret = 0;
301
302         ret = _invoke_dbus_method_async(POWEROFF_BUS_NAME, POWEROFF_OBJECT_PATH, POWEROFF_INTERFACE_NAME,
303                         METHOD_POWEROFF_NAME, NULL, NULL);
304         ret_if(!ret);
305
306         _D("%s-%s", POWEROFF_INTERFACE_NAME, METHOD_POWEROFF_NAME);
307 }
308
309
310
311 void dbus_util_send_lock_PmQos_signal(void)
312 {
313         int ret = 0;
314
315         char *arr[1];
316         char val[BUF_SIZE_32];
317
318         snprintf(val, sizeof(val), "%d", DBUS_LOCK_PMQOS_SEC);
319         arr[0] = val;
320
321         ret = _invoke_dbus_method_async(DEVICED_BUS_NAME, CPU_BOOSTER_OBJECT_PATH, CPU_BOOSTER_INTERFACE,
322                         METHOD_LOCK_PMQOS_NAME, "i", arr);
323         ret_if(!ret);
324
325         _D("%s-%s", CPU_BOOSTER_INTERFACE, METHOD_LOCK_PMQOS_NAME);
326 }
327
328
329
330 int dbus_util_receive_lcd_status(void (*changed_cb)(void *data, DBusMessage *msg), void *data)
331 {
332         E_DBus_Connection *conn;
333         E_DBus_Signal_Handler *handler;
334
335         e_dbus_init();
336
337         conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
338         if (conn == NULL) {
339                 _E("e_dbus_bus_get error");
340                 return 0;
341         }
342
343         handler = e_dbus_signal_handler_add(conn, NULL, DISPLAY_OBJECT_PATH,
344                                                                 DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_ON,
345                                                                 changed_cb, data);
346         if (handler == NULL) {
347                 _E("e_dbus_signal_handler_add error");
348                 return 0;
349         }
350
351         handler = e_dbus_signal_handler_add(conn, NULL, DISPLAY_OBJECT_PATH,
352                                                                 DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_OFF,
353                                                                 changed_cb, data);
354         if (handler == NULL) {
355                 _E("e_dbus_signal_handler_add error");
356                 return 0;
357         }
358
359         return 1;
360 }
361
362 char *dbus_util_msg_arg_get_str(DBusMessage *msg)
363 {
364         int ret = 0;
365         DBusError derror;
366         const char *state = NULL;
367         dbus_error_init(&derror);
368
369         ret = dbus_message_get_args(msg, &derror, DBUS_TYPE_STRING, &state, DBUS_TYPE_INVALID);
370         goto_if(!ret, ERROR);
371
372         dbus_error_free(&derror);
373
374         return strdup(state);
375
376 ERROR:
377         _E("Failed to get reply (%s:%s)", derror.name, derror.message);
378         dbus_error_free(&derror);
379
380         return NULL;
381 }
382
383
384