2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
15 #include <dbus/dbus.h>
18 #include "sttd_main.h"
19 #include "sttd_dbus.h"
20 #include "sttd_client_data.h"
21 #include "sttd_dbus_server.h"
24 static DBusConnection* g_conn_sender = NULL;
25 static DBusConnection* g_conn_listener = NULL;
27 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
29 static int g_waiting_time = 3000;
31 static int g_internal_result_id = 0;
33 int sttdc_send_hello(int uid)
35 int pid = sttd_client_get_pid(uid);
37 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
38 return STTD_ERROR_INVALID_PARAMETER;
41 char service_name[64];
42 memset(service_name, 0, 64);
43 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
45 char target_if_name[128];
46 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
48 DBusMessage* msg = NULL;
50 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send hello message : uid(%d)", uid);
52 msg = dbus_message_new_method_call(
54 STT_CLIENT_SERVICE_OBJECT_PATH,
59 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
60 return STTD_ERROR_OUT_OF_MEMORY;
63 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
66 dbus_error_init(&err);
69 DBusMessage* result_msg = NULL;
70 result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
71 dbus_message_unref(msg);
72 if (dbus_error_is_set(&err)) {
73 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Send error (%s)", err.message);
74 if (NULL != err.name) {
75 if (!strcmp(err.name, DBUS_ERROR_SERVICE_UNKNOWN)) {
76 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Unknown service. Client is not available");
77 dbus_error_free(&err);
81 dbus_error_free(&err);
84 if (NULL != result_msg) {
85 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
87 if (dbus_error_is_set(&err)) {
88 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] Get arguments error (%s)", err.message);
89 dbus_error_free(&err);
92 dbus_message_unref(result_msg);
94 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Result message is NULL. Client is not available");
100 int sttdc_send_set_volume(int uid, float volume)
102 int pid = sttd_client_get_pid(uid);
105 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
109 char service_name[64];
110 memset(service_name, 0, 64);
111 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
113 char target_if_name[128];
114 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
118 /* SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send set volume : uid(%d), volume(%f) volume size(%d)", uid, volume, sizeof(float)); */
120 msg = dbus_message_new_signal(
121 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
122 target_if_name, /* interface name of the signal */
123 STTD_METHOD_SET_VOLUME); /* name of the signal */
126 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
130 dbus_message_append_args(msg,
131 DBUS_TYPE_INT32, &uid,
132 DBUS_TYPE_INT32, &volume,
135 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
136 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !");
138 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send set volume : uid(%d), volume(%f)", uid, volume);
139 dbus_connection_flush(g_conn_sender);
142 dbus_message_unref(msg);
147 int sttdc_send_set_state(int uid, int state)
149 int pid = sttd_client_get_pid(uid);
152 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
156 char service_name[64];
157 memset(service_name, 0, 64);
158 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
160 char target_if_name[128];
161 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
163 DBusMessage* msg = NULL;
164 msg = dbus_message_new_signal(
165 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
166 target_if_name, /* interface name of the signal */
167 STTD_METHOD_SET_STATE); /* name of the signal */
170 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
174 dbus_message_append_args(msg,
175 DBUS_TYPE_INT32, &uid,
176 DBUS_TYPE_INT32, &state,
179 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
180 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send change state message : Out Of Memory !");
182 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send change state message : uid(%d), state(%d)", uid, state);
183 dbus_connection_flush(g_conn_sender);
186 dbus_message_unref(msg);
191 int sttdc_send_result(int uid, int event, const char** data, int data_count, const char* result_msg)
193 int pid = sttd_client_get_pid(uid);
195 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
196 return STTD_ERROR_INVALID_PARAMETER;
199 char service_name[64];
200 memset(service_name, 0, 64);
201 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
203 char target_if_name[128];
204 memset(target_if_name, 0, 128);
205 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
207 DBusMessage* msg = NULL;
208 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] send result signal : uid(%d), event(%d), result count(%d) result id(%d)",
209 uid, event, data_count, g_internal_result_id);
211 msg = dbus_message_new_signal(
212 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
213 target_if_name, /* interface name of the signal */
214 STTD_METHOD_RESULT); /* name of the signal */
217 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
218 return STTD_ERROR_OUT_OF_MEMORY;
221 DBusMessageIter args;
222 dbus_message_iter_init_append(msg, &args);
224 /* Append uid & type */
225 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &uid);
228 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &event);
230 /* Append result msg */
231 if (NULL == result_msg) {
232 msg_temp = strdup("None");
233 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &msg_temp);
234 SLOG(LOG_WARN, TAG_STTD, "[Dbus] result message is NULL");
237 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result message(%s)", result_msg);
238 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(result_msg));
241 /* Append result size */
242 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(data_count))) {
243 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result size");
244 dbus_message_unref(msg);
248 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(g_internal_result_id))) {
249 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append internal result id");
250 dbus_message_unref(msg);
254 g_internal_result_id++;
256 if (10000 == g_internal_result_id) {
257 g_internal_result_id = 1;
261 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result size (%d)", data_count);
262 for (i = 0; i < data_count; i++) {
263 if (NULL != data[i]) {
264 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result (%d, %s)", i, data[i]);
266 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &data[i])) {
267 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result data");
268 dbus_message_unref(msg);
269 return STTD_ERROR_OPERATION_FAILED;
272 int reason = (int)STTD_ERROR_OPERATION_FAILED;
274 if (0 != sttdc_send_error_signal(uid, reason, "Fail to get recognition result from engine")) {
275 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send error info. Remove client data");
277 /* clean client data */
278 sttd_client_delete(uid);
281 dbus_message_unref(msg);
282 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Result from engine is NULL(%d)", i);
283 return STTD_ERROR_OPERATION_FAILED;
287 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
288 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !");
291 dbus_connection_flush(g_conn_sender);
292 dbus_message_unref(msg);
297 int sttdc_send_error_signal(int uid, int reason, const char *err_msg)
299 if (NULL == err_msg) {
300 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Input parameter is NULL");
301 return STTD_ERROR_INVALID_PARAMETER;
304 int pid = sttd_client_get_pid(uid);
306 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
307 return STTD_ERROR_INVALID_PARAMETER;
310 char service_name[64];
311 memset(service_name, 0, 64);
312 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
314 char target_if_name[128];
315 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
317 DBusMessage* msg = NULL;
318 msg = dbus_message_new_signal(
319 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
320 target_if_name, /* interface name of the signal */
321 STTD_METHOD_ERROR); /* name of the signal */
324 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
325 return STTD_ERROR_OUT_OF_MEMORY;
328 dbus_message_append_args(msg,
329 DBUS_TYPE_INT32, &uid,
330 DBUS_TYPE_INT32, &reason,
331 DBUS_TYPE_STRING, &err_msg,
334 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
335 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] <<<< error message : Out Of Memory !");
337 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send error message : uid(%d), reason(%d), err_msg(%s)", uid, reason, (NULL == err_msg) ? "NULL" : err_msg);
338 dbus_connection_flush(g_conn_sender);
341 dbus_message_unref(msg);
346 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
348 if (NULL == g_conn_listener) return ECORE_CALLBACK_RENEW;
350 dbus_connection_read_write_dispatch(g_conn_listener, 50);
352 DBusMessage* msg = NULL;
353 msg = dbus_connection_pop_message(g_conn_listener);
355 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
356 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Connection is disconnected");
357 return ECORE_CALLBACK_RENEW;
360 /* loop again if we haven't read a message */
362 return ECORE_CALLBACK_RENEW;
366 if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_HELLO))
367 sttd_dbus_server_hello(g_conn_listener, msg);
369 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_INITIALIZE))
370 sttd_dbus_server_initialize(g_conn_listener, msg);
372 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_FINALIZE))
373 sttd_dbus_server_finalize(g_conn_listener, msg);
375 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_CURRENT_ENGINE))
376 sttd_dbus_server_set_current_engine(g_conn_listener, msg);
378 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_CHECK_APP_AGREED))
379 sttd_dbus_server_check_app_agreed(g_conn_listener, msg);
381 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_SUPPORT_LANGS))
382 sttd_dbus_server_get_support_lang(g_conn_listener, msg);
384 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_CURRENT_LANG))
385 sttd_dbus_server_get_default_lang(g_conn_listener, msg);
387 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_PRIVATE_DATA))
388 sttd_dbus_server_set_private_data(g_conn_listener, msg);
390 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_PRIVATE_DATA))
391 sttd_dbus_server_get_private_data(g_conn_listener, msg);
393 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_IS_TYPE_SUPPORTED))
394 sttd_dbus_server_is_recognition_type_supported(g_conn_listener, msg);
397 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_START_SOUND))
398 sttd_dbus_server_set_start_sound(g_conn_listener, msg);
400 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_UNSET_START_SOUND))
401 sttd_dbus_server_unset_start_sound(g_conn_listener, msg);
403 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_SET_STOP_SOUND))
404 sttd_dbus_server_set_stop_sound(g_conn_listener, msg);
406 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_UNSET_STOP_SOUND))
407 sttd_dbus_server_unset_stop_sound(g_conn_listener, msg);
410 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_START))
411 sttd_dbus_server_start(g_conn_listener, msg);
413 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_STOP))
414 sttd_dbus_server_stop(g_conn_listener, msg);
416 else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_CANCEL))
417 sttd_dbus_server_cancel(g_conn_listener, msg);
420 /* free the message */
421 dbus_message_unref(msg);
423 return ECORE_CALLBACK_RENEW;
426 int sttd_dbus_open_connection()
429 dbus_error_init(&err);
433 /* Create connection for sender */
434 g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
435 if (dbus_error_is_set(&err)) {
436 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
437 dbus_error_free(&err);
440 if (NULL == g_conn_sender) {
441 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection sender");
442 return STTD_ERROR_OPERATION_FAILED;
445 /* connect to the bus and check for errors */
446 g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
448 if (dbus_error_is_set(&err)) {
449 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
450 dbus_error_free(&err);
453 if (NULL == g_conn_listener) {
454 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection");
455 return STTD_ERROR_OPERATION_FAILED;
458 /* request our name on the bus and check for errors */
459 ret = dbus_bus_request_name(g_conn_listener, STT_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
461 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
462 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
463 return STTD_ERROR_OPERATION_FAILED;
466 if (dbus_error_is_set(&err)) {
467 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
468 dbus_error_free(&err);
469 return STTD_ERROR_OPERATION_FAILED;
472 /* add a rule for getting signal */
474 snprintf(rule, 128, "type='signal',interface='%s'", STT_SERVER_SERVICE_INTERFACE);
476 /* add a rule for which messages we want to see */
477 dbus_bus_add_match(g_conn_listener, rule, &err); /* see signals from the given interface */
478 dbus_connection_flush(g_conn_listener);
480 if (dbus_error_is_set(&err)) {
481 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
482 dbus_error_free(&err);
483 return STTD_ERROR_OPERATION_FAILED;
487 dbus_connection_get_unix_fd(g_conn_listener, &fd);
489 g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
490 if (NULL == g_dbus_fd_handler) {
491 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get fd handler");
492 return STTD_ERROR_OPERATION_FAILED;
498 int sttd_dbus_close_connection()
501 dbus_error_init(&err);
503 if (NULL != g_dbus_fd_handler) {
504 ecore_main_fd_handler_del(g_dbus_fd_handler);
505 g_dbus_fd_handler = NULL;
508 dbus_bus_release_name(g_conn_listener, STT_SERVER_SERVICE_NAME, &err);
510 if (dbus_error_is_set(&err)) {
511 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
512 dbus_error_free(&err);
515 dbus_connection_close(g_conn_listener);
516 dbus_connection_close(g_conn_sender);
518 dbus_connection_unref(g_conn_sender);
519 dbus_connection_unref(g_conn_listener);
521 g_conn_listener = NULL;
522 g_conn_sender = NULL;
524 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Close dbus connection");