2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dbus/dbus-glib.h>
19 #include <dbus/dbus-glib-lowlevel.h>
21 #include "dbus_util.h"
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"
29 #define DBUS_REPLY_TIMEOUT (120 * 1000)
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"
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
41 #define METHOD_LOCK_PMQOS_NAME "LockScreen"
42 #define DBUS_LOCK_PMQOS_SEC (2 * 1000)
45 DBusConnection *connection;
52 static DBusConnection *_dbus_connection_get(void)
55 DBusConnection *connection = NULL;
57 if (s_info.connection) {
58 return s_info.connection;
61 _W("no connection for dbus. get dbus connection");
63 dbus_error_init(&derror);
64 connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &derror);
66 _E("Failed to get dbus connection:%s", derror.message);
67 dbus_error_free(&derror);
70 dbus_connection_setup_with_g_main(connection, NULL);
71 dbus_error_free(&derror);
73 s_info.connection = connection;
75 return s_info.connection;
80 static int _append_variant(DBusMessageIter *iter, const char *sig, char *param[])
90 for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
93 int_type = atoi(param[i]);
94 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
97 int_type = atoi(param[i]);
98 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
101 int64_type = (uint64_t) atoi(param[i]);
102 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
105 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, ¶m[i]);
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[])
121 DBusConnection *conn = NULL;
123 DBusMessageIter iter;
128 conn = (DBusConnection *)_dbus_connection_get();
130 _E("dbus_bus_get error");
134 msg = dbus_message_new_method_call(dest, path, interface, method);
136 _E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
140 dbus_message_iter_init_append(msg, &iter);
141 r = _append_variant(&iter, sig, param);
143 _E("append_variant error(%d)", r);
144 dbus_message_unref(msg);
148 dbus_error_init(&err);
150 reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
151 dbus_message_unref(msg);
153 _E("dbus_connection_send error(%s:%s)", err.name, err.message);
154 dbus_error_free(&err);
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[])
167 DBusConnection *conn;
169 DBusMessageIter iter;
172 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
174 _E("dbus_bus_get error");
178 msg = dbus_message_new_method_call(dest, path, interface, method);
180 _E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
184 dbus_message_iter_init_append(msg, &iter);
185 r = _append_variant(&iter, sig, param);
187 _E("append_variant error(%d)", r);
188 dbus_message_unref(msg);
192 r = dbus_connection_send(conn, msg, NULL);
193 dbus_message_unref(msg);
195 _E("dbus_connection_send error(%s:%s:%s-%s)", dest, path, interface, method);
199 _D("dbus_connection_send, ret=%d", r);
205 static int _dbus_message_send(const char *path, const char *interface, const char *member)
208 DBusMessage *msg = NULL;
209 DBusConnection *conn = NULL;
211 conn = (DBusConnection *)_dbus_connection_get();
213 _E("dbus_bus_get error");
217 msg = dbus_message_new_signal(path, interface, member);
219 _E("dbus_message_new_signal(%s:%s-%s)", path, interface, member);
223 ret = dbus_connection_send(conn, msg, NULL); //async call
224 dbus_message_unref(msg);
226 _E("dbus_connection_send error(%s:%s-%s)", path, interface, member);
229 _D("dbus_connection_send, ret=%d", ret);
235 void dbus_util_send_home_raise_signal(void)
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);
248 int dbus_util_send_oomadj(int pid, int oom_adj_value)
253 char buf1[BUF_SIZE_16];
254 char buf2[BUF_SIZE_16];
258 _E("Pid is invalid");
262 snprintf(buf1, sizeof(buf1), "%d", pid);
263 snprintf(buf2, sizeof(buf2), "%d", oom_adj_value);
265 pa[0] = DEVICED_SET_METHOD;
270 msg = _invoke_dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH, DEVICED_INTERFACE, DEVICED_SET_METHOD, "siss", pa);
274 dbus_error_init(&err);
276 ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
278 _E("no message : [%s:%s]", err.name, err.message);
282 dbus_message_unref(msg);
283 dbus_error_free(&err);
285 _D("%s-%s : %d", DEVICED_INTERFACE, pa[0], val);
291 void dbus_util_send_cpu_booster_signal(void)
295 char val[BUF_SIZE_32];
297 snprintf(val, sizeof(val), "%d", DBUS_CPU_BOOSTER_SEC);
300 ret = _invoke_dbus_method_async(DEVICED_BUS_NAME, CPU_BOOSTER_OBJECT_PATH, CPU_BOOSTER_INTERFACE,
301 METHOD_CPU_BOOSTER, "i", arr);
304 _D("%s-%s", CPU_BOOSTER_INTERFACE, METHOD_CPU_BOOSTER);
309 void dbus_util_send_poweroff_signal(void)
313 ret = _invoke_dbus_method_async(POWEROFF_BUS_NAME, POWEROFF_OBJECT_PATH, POWEROFF_INTERFACE_NAME,
314 METHOD_POWEROFF_NAME, NULL, NULL);
317 _D("%s-%s", POWEROFF_INTERFACE_NAME, METHOD_POWEROFF_NAME);
322 void dbus_util_send_lock_PmQos_signal(void)
327 char val[BUF_SIZE_32];
329 snprintf(val, sizeof(val), "%d", DBUS_LOCK_PMQOS_SEC);
332 ret = _invoke_dbus_method_async(DEVICED_BUS_NAME, CPU_BOOSTER_OBJECT_PATH, CPU_BOOSTER_INTERFACE,
333 METHOD_LOCK_PMQOS_NAME, "i", arr);
336 _D("%s-%s", CPU_BOOSTER_INTERFACE, METHOD_LOCK_PMQOS_NAME);
341 int dbus_util_receive_lcd_status(void (*changed_cb)(void *data, DBusMessage *msg), void *data)
343 E_DBus_Connection *conn;
344 E_DBus_Signal_Handler *handler;
348 conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
350 _E("e_dbus_bus_get error");
354 handler = e_dbus_signal_handler_add(conn, NULL, DISPLAY_OBJECT_PATH,
355 DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_ON,
357 if (handler == NULL) {
358 _E("e_dbus_signal_handler_add error");
362 handler = e_dbus_signal_handler_add(conn, NULL, DISPLAY_OBJECT_PATH,
363 DEVICED_INTERFACE_DISPLAY, MEMBER_LCD_OFF,
365 if (handler == NULL) {
366 _E("e_dbus_signal_handler_add error");
373 char *dbus_util_msg_arg_get_str(DBusMessage *msg)
377 const char *state = NULL;
378 dbus_error_init(&derror);
380 ret = dbus_message_get_args(msg, &derror, DBUS_TYPE_STRING, &state, DBUS_TYPE_INVALID);
381 goto_if(!ret, ERROR);
383 dbus_error_free(&derror);
385 return strdup(state);
388 _E("Failed to get reply (%s:%s)", derror.name, derror.message);
389 dbus_error_free(&derror);