summaryrefslogtreecommitdiff
path: root/src/minicontrol-internal.c
blob: 196df034839b0bf69e2c0abd7f6550680a104c53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright (c) 2013 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <bundle.h>

#include "minicontrol-error.h"
#include "minicontrol-type.h"
#include "minicontrol-internal.h"
#include "minicontrol-log.h"

#define MINICTRL_DBUS_PATH "/org/tizen/minicontrol"
#define MINICTRL_DBUS_INTERFACE "org.tizen.minicontrol.signal"

#define PROC_DBUS_OBJECT	"/Org/Tizen/ResourceD/Process"
#define PROC_DBUS_INTERFACE	"org.tizen.resourced.process"
#define PROC_DBUS_METHOD	"ProcExclude"
#define PROC_DBUS_EXCLUDE	"exclude"
#define PROC_DBUS_INCLUDE	"include"

struct _minictrl_sig_handle {
	DBusConnection *conn;
	void (*callback) (void *data, DBusMessage *msg);
	void *user_data;
	char *signal;
};

int _minictrl_viewer_req_message_send(void)
{
	DBusConnection *connection = NULL;
	DBusMessage *message = NULL;
	DBusError err;
	dbus_bool_t dbus_ret;
	int ret = MINICONTROL_ERROR_NONE;

	dbus_error_init(&err);
	connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
	if (!connection) {
		ERR("Fail to dbus_bus_get : %s", err.message);
		ret = MINICONTROL_ERROR_IPC_FAILURE;
		goto release_n_return;
	}

	message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
				MINICTRL_DBUS_INTERFACE,
				MINICTRL_DBUS_SIG_RUNNING_REQ);
	if (!message) {
		ERR("fail to create dbus message");
		ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
		goto release_n_return;
	}

	dbus_ret = dbus_connection_send(connection, message, NULL);
	if (!dbus_ret) {
		ERR("fail to send dbus viewer req message");
		ret = MINICONTROL_ERROR_IPC_FAILURE;
		goto release_n_return;
	}

	dbus_connection_flush(connection);

release_n_return:
	dbus_error_free(&err);

	if (message)
		dbus_message_unref(message);

	if (connection)
		dbus_connection_unref(connection);

	return ret;
}

int _minictrl_provider_proc_send(int type)
{
	DBusError err;
	DBusConnection *conn = NULL;
	DBusMessage *msg = NULL;
	int ret = -1;
	int pid = getpid();
	dbus_uint32_t serial = 0;
	char *typestr;
	if (type == MINICONTROL_DBUS_PROC_EXCLUDE)
		typestr = PROC_DBUS_EXCLUDE;
	else if  (type == MINICONTROL_DBUS_PROC_INCLUDE)
		typestr = PROC_DBUS_INCLUDE;
	else {
		ERR("Check unsupported type : %d", type);
		return ret;
	}
	DBG("_minictrl_provider_proc_send : %d, %d", pid, type);
	dbus_error_init(&err);
	conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
	if (!conn) {
		ERR("Fail to dbus_bus_get : %s", err.message);
		ret = MINICONTROL_ERROR_IPC_FAILURE;
		goto release_n_return;
	}
	msg = dbus_message_new_signal(PROC_DBUS_OBJECT, /* object name of the signal */
			    PROC_DBUS_INTERFACE, /* interface name of the signal */
			    PROC_DBUS_METHOD); /* name of the signal */
	if (!msg) {
		ERR("ERR Could not create DBus Message");
		goto release_n_return;
	}
	ret = dbus_message_append_args(msg,
			    DBUS_TYPE_STRING, &typestr,
			    DBUS_TYPE_INT32, &pid,
			    DBUS_TYPE_INVALID);
	if (!dbus_connection_send(conn, msg, &serial))
		ERR("ERR send DBus Message");
	dbus_connection_flush(conn);
release_n_return:
	dbus_error_free(&err);

	if (msg)
		dbus_message_unref(msg);

	if (conn)
		dbus_connection_unref(conn);

	return ret;
}

int _minictrl_send_event(const char *signal_name, const char *minicontrol_name, int event, bundle *signal_arg)
{
	DBusConnection *connection = NULL;
	DBusMessage *message = NULL;
	DBusError err;
	dbus_bool_t dbus_ret;
	bundle_raw *serialized_arg = NULL;
	unsigned int serialized_arg_length = 0;
	int ret = MINICONTROL_ERROR_NONE;

	if (minicontrol_name == NULL) {
		ERR("Invaild parameter");
		return MINICONTROL_ERROR_INVALID_PARAMETER;
	}

	dbus_error_init(&err);
	connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
	if (!connection) {
		ERR("Fail to dbus_bus_get : %s", err.message);
		ret = MINICONTROL_ERROR_IPC_FAILURE;
		goto release_n_return;
	}

	message = dbus_message_new_signal(MINICTRL_DBUS_PATH, MINICTRL_DBUS_INTERFACE, signal_name);

	if (!message) {
		ERR("fail to create dbus message");
		ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
		goto release_n_return;
	}

	if (signal_arg != NULL) {
		if (bundle_encode(signal_arg, &serialized_arg, (int *)&serialized_arg_length) != BUNDLE_ERROR_NONE) {
			ERR("fail to serialize bundle argument");
			ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
			goto release_n_return;
		}
	} else {
		serialized_arg = (bundle_raw *)strdup("");
		serialized_arg_length = 0;
	}

	dbus_ret = dbus_message_append_args(message,
			DBUS_TYPE_STRING, &minicontrol_name,
			DBUS_TYPE_INT32,  &event,
			DBUS_TYPE_STRING, &serialized_arg,
			DBUS_TYPE_UINT32,  &serialized_arg_length,
			DBUS_TYPE_INVALID);

	if (!dbus_ret) {
		ERR("fail to append arguments to dbus message : [%s][%d]", minicontrol_name, event);
		ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
		goto release_n_return;
	}

	dbus_ret = dbus_connection_send(connection, message, NULL);

	if (!dbus_ret) {
		ERR("fail to send dbus message : %s", minicontrol_name);
		ret = MINICONTROL_ERROR_IPC_FAILURE;
		goto release_n_return;
	}

	dbus_connection_flush(connection);

release_n_return:
	dbus_error_free(&err);

	if (serialized_arg)
		free(serialized_arg);

	if (message)
		dbus_message_unref(message);

	if (connection)
		dbus_connection_unref(connection);

	return ret;
}


int _minictrl_provider_message_send(int event, const char *minicontrol_name, unsigned int witdh, unsigned int height, minicontrol_priority_e priority)
{
	bundle *event_arg_bundle = NULL;
	int ret = MINICONTROL_ERROR_NONE;
	char bundle_value_buffer[BUNDLE_BUFFER_LENGTH] = { 0, };

	event_arg_bundle = bundle_create();

	if (event_arg_bundle == NULL) {
		ERR("fail to create a bundle instance");
		ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
		goto out;
	}

	snprintf(bundle_value_buffer, BUNDLE_BUFFER_LENGTH, "%s", minicontrol_name);

	bundle_add_str(event_arg_bundle, "minicontrol_name", bundle_value_buffer);
	bundle_add_byte(event_arg_bundle, "width", (void *)&witdh, sizeof(int));
	bundle_add_byte(event_arg_bundle, "height", (void *)&height, sizeof(int));
	bundle_add_byte(event_arg_bundle, "priority", (void *)&priority, sizeof(int));

	_minictrl_send_event(MINICTRL_DBUS_SIG_TO_VIEWER, minicontrol_name, event, event_arg_bundle);

out:
	if (event_arg_bundle)
		bundle_free(event_arg_bundle);

	return ret;
}

static DBusHandlerResult _minictrl_signal_filter(DBusConnection *conn, DBusMessage *msg, void *user_data)
{
	minictrl_sig_handle *handle = NULL;
	const char *interface;
	DBusError error;
	dbus_bool_t ret;

	if (!user_data)
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

	handle = user_data;

	dbus_error_init(&error);

	interface = dbus_message_get_interface(msg);
	if (!interface)
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

	if (strcmp(MINICTRL_DBUS_INTERFACE, interface))
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

	ret = dbus_message_is_signal(msg, interface, handle->signal);
	if (!ret) {
		DBG("this msg is not signal");
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
	}

	if (handle->callback)
		handle->callback(handle->user_data, msg);

	return DBUS_HANDLER_RESULT_HANDLED;
}


minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
				void (*callback) (void *data, DBusMessage *msg),
				void *data)
{
	minictrl_sig_handle *handle = NULL;
	DBusError err;
	DBusConnection *conn = NULL;
	char rule[1024] = {'\0', };

	if (!signal) {
		ERR("signal is NULL");
		return NULL;
	}

	if (!callback) {
		ERR("call is NULL");
		return NULL;
	}

	handle = malloc(sizeof(minictrl_sig_handle));
	if (!handle) {
		ERR("fail to alloc handle");
		return NULL;
	}

	dbus_error_init(&err);
	conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
	if (!conn) {
		ERR("fail to get bus : %s", err.message);
		goto error_n_return;
	}

	dbus_connection_setup_with_g_main(conn, NULL);
	snprintf(rule, 1024,
		"path='%s',type='signal',interface='%s',member='%s'",
		MINICTRL_DBUS_PATH,
		MINICTRL_DBUS_INTERFACE,
		signal);

	dbus_bus_add_match(conn, rule, &err);
	if (dbus_error_is_set(&err)) {
		ERR("fail to dbus_bus_remove_match : %s",
				err.message);
		goto error_n_return;
	}

	if (dbus_connection_add_filter(conn, _minictrl_signal_filter,
					handle, NULL) == FALSE) {
		ERR("fail to dbus_connection_add_filter : %s",
				err.message);
		goto error_n_return;
	}

	dbus_connection_set_exit_on_disconnect(conn, FALSE);

	handle->conn = conn;
	handle->callback = callback;
	handle->user_data = data;
	handle->signal = strdup(signal);

	INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);

	return handle;


error_n_return:
	if (handle)
		free(handle);

	dbus_error_free(&err);

	if (conn)
		dbus_connection_close(conn);

	return NULL;
}

void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
{
	DBusError err;
	char rule[1024] = {'\0', };

	if (!handle) {
		ERR("handle is NULL");
		return;
	}

	dbus_error_init(&err);

	dbus_connection_remove_filter(handle->conn, _minictrl_signal_filter, handle);

	snprintf(rule, 1024,
		"path='%s',type='signal',interface='%s',member='%s'",
		MINICTRL_DBUS_PATH,
		MINICTRL_DBUS_INTERFACE,
		handle->signal);

	dbus_bus_remove_match(handle->conn, rule, &err);
	if (dbus_error_is_set(&err)) {
		ERR("fail to dbus_bus_remove_match : %s", err.message);
		dbus_error_free(&err);
	}

	dbus_connection_close(handle->conn);

	free(handle->signal);
	free(handle);

	return;
}