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"
27 static DBusConnection* g_conn_sender = NULL;
28 static DBusConnection* g_conn_listener = NULL;
29 static DBusConnection* g_conn_custom = NULL;
31 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
33 static int g_waiting_time = 3000;
35 static int g_internal_result_id = 0;
37 static char* g_server_service_name = NULL;
38 static char* g_server_service_object = NULL;
39 static char* g_server_service_interface = NULL;
41 void __sttd_dbus_service_free();
43 int sttdc_send_hello(int uid)
45 int pid = sttd_client_get_pid(uid);
47 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
48 return STTD_ERROR_INVALID_PARAMETER;
51 char service_name[64];
52 memset(service_name, 0, 64);
53 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
55 char target_if_name[128];
56 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
58 DBusMessage* msg = NULL;
60 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send hello message : uid(%d)", uid);
62 msg = dbus_message_new_method_call(
64 STT_CLIENT_SERVICE_OBJECT_PATH,
69 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
70 return STTD_ERROR_OUT_OF_MEMORY;
73 dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
76 dbus_error_init(&err);
79 DBusMessage* result_msg = NULL;
80 result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
81 dbus_message_unref(msg);
82 if (dbus_error_is_set(&err)) {
83 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Send error (%s)", err.message);
84 if (NULL != err.name) {
85 if (!strcmp(err.name, DBUS_ERROR_SERVICE_UNKNOWN)) {
86 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Unknown service. Client is not available");
87 dbus_error_free(&err);
91 dbus_error_free(&err);
94 if (NULL != result_msg) {
95 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
97 if (dbus_error_is_set(&err)) {
98 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] Get arguments error (%s)", err.message);
99 dbus_error_free(&err);
102 dbus_message_unref(result_msg);
104 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Result message is NULL. Client is not available");
110 int sttdc_send_set_volume(int uid, float volume)
112 int pid = sttd_client_get_pid(uid);
115 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
119 char service_name[64];
120 memset(service_name, 0, 64);
121 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
123 char target_if_name[128];
124 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
128 /* SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send set volume : uid(%d), volume(%f) volume size(%d)", uid, volume, sizeof(float)); */
130 msg = dbus_message_new_signal(
131 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
132 target_if_name, /* interface name of the signal */
133 STTD_METHOD_SET_VOLUME); /* name of the signal */
136 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
140 dbus_message_append_args(msg,
141 DBUS_TYPE_INT32, &uid,
142 DBUS_TYPE_INT32, &volume,
145 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
146 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !");
148 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send set volume : uid(%d), volume(%f)", uid, volume);
149 dbus_connection_flush(g_conn_sender);
152 dbus_message_unref(msg);
157 int sttdc_send_set_state(int uid, int state)
159 int pid = sttd_client_get_pid(uid);
162 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
166 char service_name[64];
167 memset(service_name, 0, 64);
168 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
170 char target_if_name[128];
171 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
173 DBusMessage* msg = NULL;
174 msg = dbus_message_new_signal(
175 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
176 target_if_name, /* interface name of the signal */
177 STTD_METHOD_SET_STATE); /* name of the signal */
180 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
184 dbus_message_append_args(msg,
185 DBUS_TYPE_INT32, &uid,
186 DBUS_TYPE_INT32, &state,
189 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
190 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send change state message : Out Of Memory !");
192 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send change state message : uid(%d), state(%d)", uid, state);
193 dbus_connection_flush(g_conn_sender);
196 dbus_message_unref(msg);
201 int sttdc_send_speech_status(int uid, int status)
203 int pid = sttd_client_get_pid(uid);
206 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
210 char service_name[64];
211 memset(service_name, 0, 64);
212 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
214 char target_if_name[128];
215 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
217 DBusMessage* msg = NULL;
218 msg = dbus_message_new_signal(
219 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
220 target_if_name, /* interface name of the signal */
221 STTD_METHOD_SPEECH_STATUS); /* name of the signal */
224 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
228 dbus_message_append_args(msg,
229 DBUS_TYPE_INT32, &uid,
230 DBUS_TYPE_INT32, &status,
233 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
234 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send speech status message : Out Of Memory !");
236 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send speech status message : uid(%d), status(%d)", uid, status);
237 dbus_connection_flush(g_conn_sender);
240 dbus_message_unref(msg);
245 int sttdc_send_result(int uid, int event, const char** data, int data_count, const char* result_msg)
247 int pid = sttd_client_get_pid(uid);
249 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
250 return STTD_ERROR_INVALID_PARAMETER;
253 char service_name[64];
254 memset(service_name, 0, 64);
255 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
257 char target_if_name[128];
258 memset(target_if_name, 0, 128);
259 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
261 DBusMessage* msg = NULL;
262 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] send result signal : uid(%d), event(%d), result count(%d) result id(%d)",
263 uid, event, data_count, g_internal_result_id);
265 msg = dbus_message_new_signal(
266 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
267 target_if_name, /* interface name of the signal */
268 STTD_METHOD_RESULT); /* name of the signal */
271 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
272 return STTD_ERROR_OUT_OF_MEMORY;
275 DBusMessageIter args;
276 dbus_message_iter_init_append(msg, &args);
278 /* Append uid & type */
279 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &uid);
282 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &event);
284 /* Append result msg */
285 if (NULL == result_msg) {
286 msg_temp = strdup("None");
287 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &msg_temp);
288 SLOG(LOG_WARN, TAG_STTD, "[Dbus] result message is NULL");
291 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result message(%s)", result_msg);
292 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(result_msg));
295 /* Append result size */
296 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(data_count))) {
297 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result size");
298 dbus_message_unref(msg);
302 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(g_internal_result_id))) {
303 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append internal result id");
304 dbus_message_unref(msg);
308 g_internal_result_id++;
310 if (10000 == g_internal_result_id) {
311 g_internal_result_id = 1;
315 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result size (%d)", data_count);
316 for (i = 0; i < data_count; i++) {
317 if (NULL != data[i]) {
318 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result (%d, %s)", i, data[i]);
320 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &data[i])) {
321 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result data");
322 dbus_message_unref(msg);
323 return STTD_ERROR_OPERATION_FAILED;
326 int reason = (int)STTD_ERROR_OPERATION_FAILED;
328 if (0 != sttdc_send_error_signal(uid, reason, "Fail to get recognition result from engine")) {
329 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send error info. Remove client data");
331 /* clean client data */
332 sttd_client_delete(uid);
335 dbus_message_unref(msg);
336 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Result from engine is NULL(%d)", i);
337 return STTD_ERROR_OPERATION_FAILED;
341 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
342 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !");
345 dbus_connection_flush(g_conn_sender);
346 dbus_message_unref(msg);
351 int sttdc_send_error_signal(int uid, int reason, const char *err_msg)
353 int pid = sttd_client_get_pid(uid);
355 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
356 return STTD_ERROR_INVALID_PARAMETER;
359 char service_name[64];
360 memset(service_name, 0, 64);
361 snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
363 char target_if_name[128];
364 snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
366 DBusMessage* msg = NULL;
367 msg = dbus_message_new_signal(
368 STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
369 target_if_name, /* interface name of the signal */
370 STTD_METHOD_ERROR); /* name of the signal */
373 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
374 return STTD_ERROR_OUT_OF_MEMORY;
377 dbus_message_append_args(msg,
378 DBUS_TYPE_INT32, &uid,
379 DBUS_TYPE_INT32, &reason,
380 DBUS_TYPE_STRING, &err_msg,
383 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
384 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] <<<< error message : Out Of Memory !");
386 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send error message : uid(%d), reason(%d), err_msg(%s)", uid, reason, (NULL == err_msg) ? "NULL" : err_msg);
387 dbus_connection_flush(g_conn_sender);
390 dbus_message_unref(msg);
395 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
397 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] listener_event_callback is called!!!");
399 if (NULL == g_conn_listener) return ECORE_CALLBACK_RENEW;
401 dbus_connection_read_write_dispatch(g_conn_listener, 50);
403 DBusMessage* msg = NULL;
404 msg = dbus_connection_pop_message(g_conn_listener);
406 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
407 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Connection is disconnected");
408 return ECORE_CALLBACK_RENEW;
411 /* loop again if we haven't read a message */
413 return ECORE_CALLBACK_RENEW;
416 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus info] service name: %s, service object: %s, service interface: %s", (NULL == g_server_service_name) ? "NULL" : g_server_service_name,
417 (NULL == g_server_service_object) ? "NULL" : g_server_service_object, (NULL == g_server_service_interface) ? "NULL" : g_server_service_interface);
420 if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_HELLO))
421 sttd_dbus_server_hello(g_conn_listener, msg);
423 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_INITIALIZE))
424 sttd_dbus_server_initialize(g_conn_listener, msg);
426 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_FINALIZE))
427 sttd_dbus_server_finalize(g_conn_listener, msg);
429 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_CURRENT_ENGINE))
430 sttd_dbus_server_set_current_engine(g_conn_listener, msg);
432 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_CHECK_APP_AGREED))
433 sttd_dbus_server_check_app_agreed(g_conn_listener, msg);
435 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_GET_SUPPORT_LANGS))
436 sttd_dbus_server_get_support_lang(g_conn_listener, msg);
438 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_GET_CURRENT_LANG))
439 sttd_dbus_server_get_default_lang(g_conn_listener, msg);
441 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_PRIVATE_DATA))
442 sttd_dbus_server_set_private_data(g_conn_listener, msg);
444 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_GET_PRIVATE_DATA))
445 sttd_dbus_server_get_private_data(g_conn_listener, msg);
447 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_IS_TYPE_SUPPORTED))
448 sttd_dbus_server_is_recognition_type_supported(g_conn_listener, msg);
451 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_START_SOUND))
452 sttd_dbus_server_set_start_sound(g_conn_listener, msg);
454 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_UNSET_START_SOUND))
455 sttd_dbus_server_unset_start_sound(g_conn_listener, msg);
457 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_STOP_SOUND))
458 sttd_dbus_server_set_stop_sound(g_conn_listener, msg);
460 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_UNSET_STOP_SOUND))
461 sttd_dbus_server_unset_stop_sound(g_conn_listener, msg);
464 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_START))
465 sttd_dbus_server_start(g_conn_listener, msg);
467 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_STOP))
468 sttd_dbus_server_stop(g_conn_listener, msg);
470 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_CANCEL))
471 sttd_dbus_server_cancel(g_conn_listener, msg);
473 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_START_FILE))
474 sttd_dbus_server_start_file(g_conn_listener, msg);
476 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_CANCEL_FILE))
477 sttd_dbus_server_cancel_file(g_conn_listener, msg);
479 /* free the message */
480 dbus_message_unref(msg);
482 return ECORE_CALLBACK_RENEW;
485 void __sttd_dbus_service_free()
487 if (NULL != g_server_service_name) {
488 free(g_server_service_name);
489 g_server_service_name = NULL;
492 if (NULL != g_server_service_object) {
493 free(g_server_service_object);
494 g_server_service_object = NULL;
497 if (NULL != g_server_service_interface) {
498 free(g_server_service_interface);
499 g_server_service_interface = NULL;
503 void __sttd_dbus_connection_free()
505 if (NULL != g_conn_listener) {
506 dbus_connection_close(g_conn_listener);
507 dbus_connection_unref(g_conn_listener);
508 g_conn_listener = NULL;
510 if (NULL != g_conn_sender) {
511 dbus_connection_close(g_conn_sender);
512 dbus_connection_unref(g_conn_sender);
513 g_conn_sender = NULL;
515 if (NULL != g_conn_custom) {
516 dbus_connection_close(g_conn_custom);
517 dbus_connection_unref(g_conn_custom);
518 g_conn_custom = NULL;
522 int __sttd_get_buxtonkey()
524 /* request our name on the bus and check for errors */
526 char engine_appid[2048] = {0, };
527 const char* engine_default = NULL;
529 struct buxton_client * bux_cli;
530 struct buxton_layer * bux_layer;
531 struct buxton_value * bux_val;
533 int ret = buxton_open(&bux_cli, NULL, NULL);
535 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to open buxton client, ret(%d)", ret);
536 return STTD_ERROR_OPERATION_FAILED;
538 bux_layer = buxton_create_layer("system");
539 if (NULL == bux_layer) {
540 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] buxton_create_layer FAIL");
541 buxton_close(bux_cli);
543 return STTD_ERROR_OPERATION_FAILED;
545 bux_val = buxton_value_create_string("stt-engine-default");
546 if (NULL == bux_val) {
547 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] buxton_value_create_string FAIL");
548 buxton_free_layer(bux_layer);
549 buxton_close(bux_cli);
552 return STTD_ERROR_OPERATION_FAILED;
554 SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] layer: %s", buxton_layer_get_name(bux_layer));
556 ret = buxton_get_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_DEFAULT, &bux_val);
558 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to get value sync, ret(%d)", ret);
559 buxton_value_free(bux_val);
560 buxton_free_layer(bux_layer);
561 buxton_close(bux_cli);
566 return STTD_ERROR_OPERATION_FAILED;
568 ret = buxton_value_get_string(bux_val, &engine_default);
570 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to get value string, ret(%d)", ret);
571 buxton_value_free(bux_val);
572 buxton_free_layer(bux_layer);
573 buxton_close(bux_cli);
578 return STTD_ERROR_OPERATION_FAILED;
580 SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] buxton_value_get_string: engine_default: %s", engine_default);
582 ret = aul_app_get_appid_bypid(pid, engine_appid, sizeof(engine_appid) - 1);
584 __sttd_dbus_service_free();
586 if (NULL == engine_default || 0 == strncmp(engine_appid, engine_default, strlen(engine_appid))) {
587 g_server_service_name = (char*)calloc(strlen(STT_SERVER_SERVICE_NAME) + 1, sizeof(char));
588 if (g_server_service_name)
589 snprintf(g_server_service_name, strlen(STT_SERVER_SERVICE_NAME) + 1, "%s", STT_SERVER_SERVICE_NAME);
590 g_server_service_object = (char*)calloc(strlen(STT_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
591 if (g_server_service_object)
592 snprintf(g_server_service_object, strlen(STT_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", STT_SERVER_SERVICE_OBJECT_PATH);
593 g_server_service_interface = (char*)calloc(strlen(STT_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
594 if (g_server_service_interface)
595 snprintf(g_server_service_interface, strlen(STT_SERVER_SERVICE_INTERFACE) + 1, "%s", STT_SERVER_SERVICE_INTERFACE);
597 g_server_service_name = (char*)calloc(strlen(STT_SERVER_SERVICE_NAME) + strlen(engine_appid) - 8, sizeof(char));
598 if (g_server_service_name)
599 snprintf(g_server_service_name, strlen(STT_SERVER_SERVICE_NAME) + strlen(engine_appid) - 8, "%s%s%s", STT_SERVER_SERVICE_NAME, ".", (engine_appid + 10));
600 g_server_service_object = (char*)calloc(strlen(STT_SERVER_SERVICE_OBJECT_PATH) + strlen(engine_appid) - 8, sizeof(char));
601 if (g_server_service_object)
602 snprintf(g_server_service_object, strlen(STT_SERVER_SERVICE_OBJECT_PATH) + strlen(engine_appid) - 8, "%s%s%s", STT_SERVER_SERVICE_OBJECT_PATH, "/", (engine_appid + 10));
603 g_server_service_interface = (char*)calloc(strlen(STT_SERVER_SERVICE_INTERFACE) + strlen(engine_appid) - 8, sizeof(char));
604 if (g_server_service_interface)
605 snprintf(g_server_service_interface, strlen(STT_SERVER_SERVICE_INTERFACE) + strlen(engine_appid) - 8, "%s%s%s", STT_SERVER_SERVICE_INTERFACE, ".", (engine_appid + 10));
608 if (NULL == g_server_service_name || NULL == g_server_service_object || NULL == g_server_service_interface) {
609 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] dbus info invalid");
610 buxton_value_free(bux_val);
611 buxton_free_layer(bux_layer);
612 buxton_close(bux_cli);
618 __sttd_dbus_service_free();
619 return STTD_ERROR_OPERATION_FAILED;
622 char* ret_char = strchr(g_server_service_interface, '-');
623 while (NULL != ret_char) {
625 ret_char = strchr(g_server_service_interface, '-');
628 SLOG(LOG_DEBUG, TAG_STTD, "[get_service_interface] service interface: %s", g_server_service_interface);
630 buxton_value_free(bux_val);
631 buxton_free_layer(bux_layer);
632 buxton_close(bux_cli);
638 SLOG(LOG_DEBUG, TAG_STTD, "[get_service_name] FINISH!! service name: %s", g_server_service_name);
640 return STTD_ERROR_NONE;
644 int __sttd_request_custom_dbus_name()
647 dbus_error_init(&err);
649 /* Create connection for sender */
650 g_conn_custom = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
651 if (dbus_error_is_set(&err)) {
652 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
653 dbus_error_free(&err);
654 __sttd_dbus_connection_free();
655 return STTD_ERROR_OPERATION_FAILED;
658 if (NULL == g_conn_custom) {
659 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] fail to get dbus connection");
660 return STTD_ERROR_OPERATION_FAILED;
663 dbus_connection_set_exit_on_disconnect(g_conn_custom, false);
665 int ret = dbus_bus_request_name(g_conn_custom, STT_SERVER_CUSTOM_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
666 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
667 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
668 __sttd_dbus_connection_free();
669 return STTD_ERROR_OPERATION_FAILED;
672 if (dbus_error_is_set(&err)) {
673 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
674 dbus_error_free(&err);
675 __sttd_dbus_connection_free();
676 return STTD_ERROR_OPERATION_FAILED;
679 dbus_bus_release_name(g_conn_custom, STT_SERVER_CUSTOM_SERVICE_NAME, &err);
680 if (dbus_error_is_set(&err)) {
681 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
682 dbus_error_free(&err);
683 __sttd_dbus_connection_free();
684 return STTD_ERROR_OPERATION_FAILED;
687 dbus_connection_close(g_conn_custom);
688 dbus_connection_unref(g_conn_custom);
689 g_conn_custom = NULL;
691 SLOG(LOG_DEBUG, TAG_STTD, "==");
695 int sttd_dbus_open_connection()
698 dbus_error_init(&err);
702 /* Create connection for sender */
703 g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
704 if (dbus_error_is_set(&err)) {
705 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
706 dbus_error_free(&err);
709 if (NULL == g_conn_sender) {
710 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection sender");
711 return STTD_ERROR_OPERATION_FAILED;
714 dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
716 /* connect to the bus and check for errors */
717 g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
719 if (dbus_error_is_set(&err)) {
720 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
721 dbus_error_free(&err);
724 if (NULL == g_conn_listener) {
725 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection");
726 __sttd_dbus_connection_free();
727 return STTD_ERROR_OPERATION_FAILED;
730 dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
733 ret = __sttd_get_buxtonkey();
735 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get buxton key");
736 __sttd_dbus_service_free();
737 __sttd_dbus_connection_free();
741 ret = __sttd_request_custom_dbus_name();
743 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to request custom dbus name");
744 __sttd_dbus_service_free();
745 __sttd_dbus_connection_free();
749 ret = dbus_bus_request_name(g_conn_listener, g_server_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
751 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
752 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
753 __sttd_dbus_service_free();
754 __sttd_dbus_connection_free();
755 return STTD_ERROR_OPERATION_FAILED;
758 if (dbus_error_is_set(&err)) {
759 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
760 dbus_error_free(&err);
761 __sttd_dbus_service_free();
762 __sttd_dbus_connection_free();
763 return STTD_ERROR_OPERATION_FAILED;
766 /* add a rule for getting signal */
768 snprintf(rule, 128, "type='signal',interface='%s'", g_server_service_interface);
770 /* add a rule for which messages we want to see */
771 dbus_bus_add_match(g_conn_listener, rule, &err); /* see signals from the given interface */
772 dbus_connection_flush(g_conn_listener);
774 if (dbus_error_is_set(&err)) {
775 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
776 dbus_error_free(&err);
777 __sttd_dbus_service_free();
778 __sttd_dbus_connection_free();
779 return STTD_ERROR_OPERATION_FAILED;
783 dbus_connection_get_unix_fd(g_conn_listener, &fd);
785 g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
786 if (NULL == g_dbus_fd_handler) {
787 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get fd handler");
788 __sttd_dbus_service_free();
789 __sttd_dbus_connection_free();
790 return STTD_ERROR_OPERATION_FAILED;
796 int sttd_dbus_close_connection()
799 dbus_error_init(&err);
801 if (NULL != g_dbus_fd_handler) {
802 ecore_main_fd_handler_del(g_dbus_fd_handler);
803 g_dbus_fd_handler = NULL;
806 if (NULL != g_conn_listener) {
807 dbus_bus_release_name(g_conn_listener, g_server_service_name, &err);
808 if (dbus_error_is_set(&err)) {
809 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
810 dbus_error_free(&err);
814 __sttd_dbus_connection_free();
815 __sttd_dbus_service_free();
817 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Close dbus connection");