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.
18 #include <dbus/dbus.h>
19 #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"
27 #define MINICTRL_DBUS_PATH "/org/tizen/minicontrol"
28 #define MINICTRL_DBUS_INTERFACE "org.tizen.minicontrol.signal"
30 #define PROC_DBUS_OBJECT "/Org/Tizen/ResourceD/Process"
31 #define PROC_DBUS_INTERFACE "org.tizen.resourced.process"
32 #define PROC_DBUS_METHOD "ProcExclude"
33 #define PROC_DBUS_EXCLUDE "exclude"
34 #define PROC_DBUS_INCLUDE "include"
36 struct _minictrl_sig_handle {
38 void (*callback) (void *data, DBusMessage *msg);
43 int _minictrl_viewer_req_message_send(void)
45 DBusConnection *connection = NULL;
46 DBusMessage *message = NULL;
49 int ret = MINICONTROL_ERROR_NONE;
51 dbus_error_init(&err);
52 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
54 ERR("Fail to dbus_bus_get : %s", err.message);
55 ret = MINICONTROL_ERROR_IPC_FAILURE;
56 goto release_n_return;
59 message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
60 MINICTRL_DBUS_INTERFACE,
61 MINICTRL_DBUS_SIG_RUNNING_REQ);
63 ERR("fail to create dbus message");
64 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
65 goto release_n_return;
68 dbus_ret = dbus_connection_send(connection, message, NULL);
70 ERR("fail to send dbus viewer req message");
71 ret = MINICONTROL_ERROR_IPC_FAILURE;
72 goto release_n_return;
75 dbus_connection_flush(connection);
78 dbus_error_free(&err);
81 dbus_message_unref(message);
84 dbus_connection_unref(connection);
89 int _minictrl_provider_proc_send(int type)
92 DBusConnection* conn = NULL;
93 DBusMessage* msg = NULL;
96 dbus_uint32_t serial = 0;
98 if (type == MINICONTROL_DBUS_PROC_EXCLUDE)
99 typestr = PROC_DBUS_EXCLUDE;
100 else if (type == MINICONTROL_DBUS_PROC_INCLUDE)
101 typestr = PROC_DBUS_INCLUDE;
103 ERR("Check unsupported type : %d", type);
106 DBG("_minictrl_provider_proc_send : %d, %d", pid, type);
107 dbus_error_init(&err);
108 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
110 ERR("Fail to dbus_bus_get : %s", err.message);
111 ret = MINICONTROL_ERROR_IPC_FAILURE;
112 goto release_n_return;
114 msg = dbus_message_new_signal(PROC_DBUS_OBJECT, // object name of the signal
115 PROC_DBUS_INTERFACE, // interface name of the signal
116 PROC_DBUS_METHOD); // name of the signal
118 ERR("ERR Could not create DBus Message");
119 goto release_n_return;
121 ret = dbus_message_append_args(msg,
122 DBUS_TYPE_STRING, &typestr,
123 DBUS_TYPE_INT32, &pid,
125 if (!dbus_connection_send(conn, msg, &serial))
126 ERR("ERR send DBus Message");
127 dbus_connection_flush(conn);
129 dbus_error_free(&err);
132 dbus_message_unref(msg);
135 dbus_connection_unref(conn);
141 int _minictrl_send_event(const char *signal_name, const char *minicontrol_name, int event, bundle *signal_arg)
143 DBusConnection *connection = NULL;
144 DBusMessage *message = NULL;
146 dbus_bool_t dbus_ret;
147 bundle_raw *serialized_arg = NULL;
148 unsigned int serialized_arg_length = 0;
149 int ret = MINICONTROL_ERROR_NONE;
151 if (minicontrol_name == NULL) {
152 ERR("Invaild parameter");
153 return MINICONTROL_ERROR_INVALID_PARAMETER;
156 dbus_error_init(&err);
157 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
159 ERR("Fail to dbus_bus_get : %s", err.message);
160 ret = MINICONTROL_ERROR_IPC_FAILURE;
161 goto release_n_return;
164 message = dbus_message_new_signal(MINICTRL_DBUS_PATH, MINICTRL_DBUS_INTERFACE, signal_name);
167 ERR("fail to create dbus message");
168 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
169 goto release_n_return;
172 if (signal_arg != NULL) {
173 if (bundle_encode(signal_arg, &serialized_arg, (int*)&serialized_arg_length) != BUNDLE_ERROR_NONE) {
174 ERR("fail to serialize bundle argument");
175 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
176 goto release_n_return;
180 serialized_arg = (bundle_raw*)strdup("");
181 serialized_arg_length = 0;
184 dbus_ret = dbus_message_append_args(message,
185 DBUS_TYPE_STRING, &minicontrol_name,
186 DBUS_TYPE_INT32, &event,
187 DBUS_TYPE_STRING, &serialized_arg,
188 DBUS_TYPE_UINT32, &serialized_arg_length,
192 ERR("fail to append arguments to dbus message : [%s][%d]", minicontrol_name, event);
193 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
194 goto release_n_return;
197 dbus_ret = dbus_connection_send(connection, message, NULL);
200 ERR("fail to send dbus message : %s", minicontrol_name);
201 ret = MINICONTROL_ERROR_IPC_FAILURE;
202 goto release_n_return;
205 dbus_connection_flush(connection);
208 dbus_error_free(&err);
211 free(serialized_arg);
214 dbus_message_unref(message);
217 dbus_connection_unref(connection);
223 int _minictrl_provider_message_send(int event, const char *minicontrol_name, unsigned int witdh, unsigned int height, minicontrol_priority_e priority)
225 bundle *event_arg_bundle = NULL;
226 int ret = MINICONTROL_ERROR_NONE;
227 char bundle_value_buffer[BUNDLE_BUFFER_LENGTH] = { 0, };
229 event_arg_bundle = bundle_create();
231 if (event_arg_bundle == NULL) {
232 ERR("fail to create a bundle instance");
233 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
237 snprintf(bundle_value_buffer, BUNDLE_BUFFER_LENGTH, "%s", minicontrol_name);
239 bundle_add_str(event_arg_bundle, "minicontrol_name", bundle_value_buffer);
240 bundle_add_byte(event_arg_bundle, "width", (void*)&witdh, sizeof(int));
241 bundle_add_byte(event_arg_bundle, "height", (void*)&height, sizeof(int));
242 bundle_add_byte(event_arg_bundle, "priority", (void*)&priority, sizeof(int));
244 _minictrl_send_event(MINICTRL_DBUS_SIG_TO_VIEWER, minicontrol_name, event, event_arg_bundle);
247 if (event_arg_bundle)
248 bundle_free(event_arg_bundle);
253 static DBusHandlerResult _minictrl_signal_filter(DBusConnection *conn, DBusMessage *msg, void *user_data)
255 minictrl_sig_handle *handle = NULL;
256 const char *interface;
261 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
265 dbus_error_init(&error);
267 interface = dbus_message_get_interface(msg);
269 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
271 if (strcmp(MINICTRL_DBUS_INTERFACE, interface))
272 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
274 ret = dbus_message_is_signal(msg, interface, handle->signal);
276 DBG("this msg is not signal");
277 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
280 if (handle->callback)
281 handle->callback(handle->user_data, msg);
283 return DBUS_HANDLER_RESULT_HANDLED;
287 minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
288 void (*callback) (void *data, DBusMessage *msg),
291 minictrl_sig_handle *handle = NULL;
293 DBusConnection *conn = NULL;
294 char rule[1024] = {'\0', };
297 ERR("signal is NULL");
306 handle = malloc(sizeof(minictrl_sig_handle));
308 ERR("fail to alloc handle");
312 dbus_error_init(&err);
313 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
315 ERR("fail to get bus : %s", err.message);
319 dbus_connection_setup_with_g_main(conn, NULL);
321 "path='%s',type='signal',interface='%s',member='%s'",
323 MINICTRL_DBUS_INTERFACE,
326 dbus_bus_add_match(conn, rule, &err);
327 if (dbus_error_is_set(&err)) {
328 ERR("fail to dbus_bus_remove_match : %s",
333 if (dbus_connection_add_filter(conn, _minictrl_signal_filter,
334 handle, NULL) == FALSE) {
335 ERR("fail to dbus_connection_add_filter : %s",
340 dbus_connection_set_exit_on_disconnect(conn, FALSE);
343 handle->callback = callback;
344 handle->user_data = data;
345 handle->signal = strdup(signal);
347 INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);
356 dbus_error_free(&err);
359 dbus_connection_close(conn);
364 void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
367 char rule[1024] = {'\0', };
370 ERR("handle is NULL");
374 dbus_error_init(&err);
376 dbus_connection_remove_filter(handle->conn, _minictrl_signal_filter, handle);
379 "path='%s',type='signal',interface='%s',member='%s'",
381 MINICTRL_DBUS_INTERFACE,
384 dbus_bus_remove_match(handle->conn, rule, &err);
385 if (dbus_error_is_set(&err)) {
386 ERR("fail to dbus_bus_remove_match : %s", err.message);
387 dbus_error_free(&err);
390 dbus_connection_close(handle->conn);
392 free(handle->signal);