2 * Copyright (c) 2013-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.
19 #include <dbus/dbus.h>
20 #include <dbus/dbus-glib-lowlevel.h>
22 #include "minicontrol-error.h"
23 #include "minicontrol-type.h"
24 #include "minicontrol-internal.h"
25 #include "minicontrol-log.h"
26 #include "minicontrol-handler.h"
28 #define MINICTRL_DBUS_PATH "/org/tizen/minicontrol"
29 #define MINICTRL_DBUS_INTERFACE "org.tizen.minicontrol.signal"
31 #define PROC_DBUS_OBJECT "/Org/Tizen/ResourceD/Process"
32 #define PROC_DBUS_INTERFACE "org.tizen.resourced.process"
33 #define PROC_DBUS_METHOD "ProcExclude"
34 #define PROC_DBUS_EXCLUDE "exclude"
35 #define PROC_DBUS_INCLUDE "include"
37 struct _minictrl_sig_handle {
39 void (*callback) (void *data, DBusMessage *msg);
44 int _minictrl_viewer_req_message_send(void)
46 DBusConnection *connection = NULL;
47 DBusMessage *message = NULL;
50 int ret = MINICONTROL_ERROR_NONE;
52 dbus_error_init(&err);
53 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
55 ERR("Fail to dbus_bus_get : %s", err.message);
56 ret = MINICONTROL_ERROR_DBUS;
57 goto release_n_return;
60 message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
61 MINICTRL_DBUS_INTERFACE,
62 MINICTRL_DBUS_SIG_RUNNING_REQ);
64 ERR("fail to create dbus message");
65 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
66 goto release_n_return;
69 dbus_ret = dbus_connection_send(connection, message, NULL);
71 ERR("fail to send dbus viewer req message");
72 ret = MINICONTROL_ERROR_DBUS;
73 goto release_n_return;
76 dbus_connection_flush(connection);
79 dbus_error_free(&err);
82 dbus_message_unref(message);
85 dbus_connection_unref(connection);
90 int _minictrl_provider_proc_send(int type)
93 DBusConnection* conn = NULL;
94 DBusMessage* msg = NULL;
97 dbus_uint32_t serial = 0;
99 if (type == MINICONTROL_DBUS_PROC_EXCLUDE)
100 typestr = PROC_DBUS_EXCLUDE;
101 else if (type == MINICONTROL_DBUS_PROC_INCLUDE)
102 typestr = PROC_DBUS_INCLUDE;
104 ERR("Check unsupported type : %d", type);
107 DBG("_minictrl_provider_proc_send : %d, %d", pid, type);
108 dbus_error_init(&err);
109 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
111 ERR("Fail to dbus_bus_get : %s", err.message);
112 ret = MINICONTROL_ERROR_DBUS;
113 goto release_n_return;
115 msg = dbus_message_new_signal(PROC_DBUS_OBJECT, // object name of the signal
116 PROC_DBUS_INTERFACE, // interface name of the signal
117 PROC_DBUS_METHOD); // name of the signal
119 ERR("ERR Could not create DBus Message");
120 goto release_n_return;
122 ret = dbus_message_append_args(msg,
123 DBUS_TYPE_STRING, &typestr,
124 DBUS_TYPE_INT32, &pid,
126 if (!dbus_connection_send(conn, msg, &serial))
127 ERR("ERR send DBus Message");
128 dbus_connection_flush(conn);
130 dbus_error_free(&err);
133 dbus_message_unref(msg);
136 dbus_connection_unref(conn);
142 int _minictrl_provider_message_send(const char *sig_name, const char *svr_name,
143 unsigned int witdh, unsigned int height,
144 minicontrol_priority_e priority,
145 minicontrol_h handler)
147 DBusConnection *connection = NULL;
148 DBusMessage *message = NULL;
150 dbus_bool_t dbus_ret;
151 int ret = MINICONTROL_ERROR_NONE;
152 int handler_raw_data_len = 0;
153 char *handler_raw_data = NULL;
156 ERR("sig_name is NULL, invaild parameter");
157 return MINICONTROL_ERROR_INVALID_PARAMETER;
161 ERR("svr_name is NULL, invaild parameter");
162 return MINICONTROL_ERROR_INVALID_PARAMETER;
165 if (handler != NULL) {
166 _minictrl_handler_get_raw_data(handler, &handler_raw_data, &handler_raw_data_len);
169 dbus_error_init(&err);
170 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
172 ERR("Fail to dbus_bus_get : %s", err.message);
173 ret = MINICONTROL_ERROR_DBUS;
174 goto release_n_return;
177 message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
178 MINICTRL_DBUS_INTERFACE,
182 ERR("fail to create dbus message");
183 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
184 goto release_n_return;
188 if (handler_raw_data != NULL && handler_raw_data_len > 0) {
189 dbus_ret = dbus_message_append_args(message,
190 DBUS_TYPE_STRING, &svr_name,
191 DBUS_TYPE_UINT32, &witdh,
192 DBUS_TYPE_UINT32, &height,
193 DBUS_TYPE_UINT32, &priority,
194 DBUS_TYPE_STRING, &handler_raw_data,
195 DBUS_TYPE_UINT32, &handler_raw_data_len,
198 dbus_ret = dbus_message_append_args(message,
199 DBUS_TYPE_STRING, &svr_name,
200 DBUS_TYPE_UINT32, &witdh,
201 DBUS_TYPE_UINT32, &height,
202 DBUS_TYPE_UINT32, &priority,
206 ERR("fail to append name to dbus message : %s", svr_name);
207 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
208 goto release_n_return;
211 dbus_ret = dbus_connection_send(connection, message, NULL);
213 ERR("fail to send dbus message : %s", svr_name);
214 ret = MINICONTROL_ERROR_DBUS;
215 goto release_n_return;
218 dbus_connection_flush(connection);
221 dbus_error_free(&err);
224 dbus_message_unref(message);
227 dbus_connection_unref(connection);
232 static DBusHandlerResult _minictrl_signal_filter(DBusConnection *conn,
233 DBusMessage *msg, void *user_data)
235 minictrl_sig_handle *handle = NULL;
236 const char *interface;
241 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245 dbus_error_init(&error);
247 interface = dbus_message_get_interface(msg);
249 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
251 if (strcmp(MINICTRL_DBUS_INTERFACE, interface))
252 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
254 ret = dbus_message_is_signal(msg, interface, handle->signal);
256 DBG("this msg is not signal");
257 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
260 if (handle->callback)
261 handle->callback(handle->user_data, msg);
263 return DBUS_HANDLER_RESULT_HANDLED;
267 minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
268 void (*callback) (void *data, DBusMessage *msg),
271 minictrl_sig_handle *handle = NULL;
273 DBusConnection *conn = NULL;
274 char rule[1024] = {'\0', };
277 ERR("signal is NULL");
286 handle = malloc(sizeof(minictrl_sig_handle));
288 ERR("fail to alloc handle");
292 dbus_error_init(&err);
293 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
295 ERR("fail to get bus : %s", err.message);
299 dbus_connection_setup_with_g_main(conn, NULL);
301 "path='%s',type='signal',interface='%s',member='%s'",
303 MINICTRL_DBUS_INTERFACE,
306 dbus_bus_add_match(conn, rule, &err);
307 if (dbus_error_is_set(&err)) {
308 ERR("fail to dbus_bus_remove_match : %s",
313 if (dbus_connection_add_filter(conn, _minictrl_signal_filter,
314 handle, NULL) == FALSE) {
315 ERR("fail to dbus_connection_add_filter : %s",
320 dbus_connection_set_exit_on_disconnect(conn, FALSE);
323 handle->callback = callback;
324 handle->user_data = data;
325 handle->signal = strdup(signal);
327 INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);
336 dbus_error_free(&err);
339 dbus_connection_close(conn);
344 void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
347 char rule[1024] = {'\0', };
350 ERR("handle is NULL");
354 dbus_error_init(&err);
356 dbus_connection_remove_filter(handle->conn,
357 _minictrl_signal_filter, handle);
360 "path='%s',type='signal',interface='%s',member='%s'",
362 MINICTRL_DBUS_INTERFACE,
365 dbus_bus_remove_match(handle->conn, rule, &err);
366 if (dbus_error_is_set(&err)) {
367 ERR("fail to dbus_bus_remove_match : %s", err.message);
368 dbus_error_free(&err);
371 dbus_connection_close(handle->conn);
373 free(handle->signal);