4980358e951a2cfa56e9b023b29916f02a5ffc9e
[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 static int _dbus_message_send(const char *path, const char *interface, const char *member)
206 {
207         int ret = 0;
208         DBusMessage *msg = NULL;
209         DBusConnection *conn = NULL;
210
211         conn = (DBusConnection *)_dbus_connection_get();
212         if (!conn) {
213                 _E("dbus_bus_get error");
214                 return -1;
215         }
216
217         msg = dbus_message_new_signal(path, interface, member);
218         if (!msg) {
219                 _E("dbus_message_new_signal(%s:%s-%s)", path, interface, member);
220                 return -1;
221         }
222
223         ret = dbus_connection_send(conn, msg, NULL); //async call
224         dbus_message_unref(msg);
225         if (ret != TRUE) {
226                 _E("dbus_connection_send error(%s:%s-%s)", path, interface, member);
227                 return -ECOMM;
228         }
229         _D("dbus_connection_send, ret=%d", ret);
230         return 0;
231 }
232
233
234
235 void dbus_util_send_home_raise_signal(void)
236 {
237         int ret = 0;
238
239         ret = _dbus_message_send(
240                         DBUS_HOME_RAISE_PATH,
241                         DBUS_HOME_RAISE_INTERFACE,
242                         DBUS_HOME_RAISE_MEMBER);
243         _E("Sending HOME RAISE signal, result:%d", ret);
244 }
245
246
247
248 int dbus_util_send_oomadj(int pid, int oom_adj_value)
249 {
250         DBusError err;
251         DBusMessage *msg;
252         char *pa[4];
253         char buf1[BUF_SIZE_16];
254         char buf2[BUF_SIZE_16];
255         int ret, val;
256
257         if(pid <= 0){
258                 _E("Pid is invalid");
259                 return -1;
260         }
261
262         snprintf(buf1, sizeof(buf1), "%d", pid);
263         snprintf(buf2, sizeof(buf2), "%d", oom_adj_value);
264
265         pa[0] = DEVICED_SET_METHOD;
266         pa[1] = "2";
267         pa[2] = buf1;
268         pa[3] = buf2;
269
270         msg = _invoke_dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH, DEVICED_INTERFACE, DEVICED_SET_METHOD, "siss", pa);
271         if (!msg)
272                 return -EBADMSG;
273
274         dbus_error_init(&err);
275
276         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
277         if (!ret) {
278                 _E("no message : [%s:%s]", err.name, err.message);
279                 val = -EBADMSG;
280         }
281
282         dbus_message_unref(msg);
283         dbus_error_free(&err);
284
285         _D("%s-%s : %d", DEVICED_INTERFACE, pa[0], val);
286         return val;
287 }
288
289
290
291 void dbus_util_send_cpu_booster_signal(void)
292 {
293         int ret = 0;
294         char *arr[1];
295         char val[BUF_SIZE_32];
296
297         snprintf(val, sizeof(val), "%d", DBUS_CPU_BOOSTER_SEC);
298         arr[0] = val;
299
300         ret = _invoke_dbus_method_async(DEVICED_BUS_NAME, CPU_BOOSTER_OBJECT_PATH, CPU_BOOSTER_INTERFACE,
301                         METHOD_CPU_BOOSTER, "i", arr);
302         ret_if(!ret);
303
304         _D("%s-%s", CPU_BOOSTER_INTERFACE, METHOD_CPU_BOOSTER);
305 }
306
307
308
309 void dbus_util_send_poweroff_signal(void)
310 {
311         int ret = 0;
312
313         ret = _invoke_dbus_method_async(POWEROFF_BUS_NAME, POWEROFF_OBJECT_PATH, POWEROFF_INTERFACE_NAME,
314                         METHOD_POWEROFF_NAME, NULL, NULL);
315         ret_if(!ret);
316
317         _D("%s-%s", POWEROFF_INTERFACE_NAME, METHOD_POWEROFF_NAME);
318 }
319
320
321
322 void dbus_util_send_lock_PmQos_signal(void)
323 {
324         int ret = 0;
325
326         char *arr[1];
327         char val[BUF_SIZE_32];
328
329         snprintf(val, sizeof(val), "%d", DBUS_LOCK_PMQOS_SEC);
330         arr[0] = val;
331
332         ret = _invoke_dbus_method_async(DEVICED_BUS_NAME, CPU_BOOSTER_OBJECT_PATH, CPU_BOOSTER_INTERFACE,
333                         METHOD_LOCK_PMQOS_NAME, "i", arr);
334         ret_if(!ret);
335
336         _D("%s-%s", CPU_BOOSTER_INTERFACE, METHOD_LOCK_PMQOS_NAME);
337 }
338
339
340
341 int dbus_util_receive_lcd_status(void (*changed_cb)(void *data, DBusMessage *msg), void *data)
342 {
343         E_DBus_Connection *conn;
344         E_DBus_Signal_Handler *handler;
345
346         e_dbus_init();
347
348         conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
349         if (conn == NULL) {
350                 _E("e_dbus_bus_get error");
351                 return 0;
352         }
353
354         handler = e_dbus_signal_handler_add(conn, NULL, DISPLAY_OBJECT_PATH,
355                                                                 DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_ON,
356                                                                 changed_cb, data);
357         if (handler == NULL) {
358                 _E("e_dbus_signal_handler_add error");
359                 return 0;
360         }
361
362         handler = e_dbus_signal_handler_add(conn, NULL, DISPLAY_OBJECT_PATH,
363                                                                 DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_OFF,
364                                                                 changed_cb, data);
365         if (handler == NULL) {
366                 _E("e_dbus_signal_handler_add error");
367                 return 0;
368         }
369
370         return 1;
371 }
372
373 char *dbus_util_msg_arg_get_str(DBusMessage *msg)
374 {
375         int ret = 0;
376         DBusError derror;
377         const char *state = NULL;
378         dbus_error_init(&derror);
379
380         ret = dbus_message_get_args(msg, &derror, DBUS_TYPE_STRING, &state, DBUS_TYPE_INVALID);
381         goto_if(!ret, ERROR);
382
383         dbus_error_free(&derror);
384
385         return strdup(state);
386
387 ERROR:
388         _E("Failed to get reply (%s:%s)", derror.name, derror.message);
389         dbus_error_free(&derror);
390
391         return NULL;
392 }
393
394
395