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(unsigned 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(%u)", 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_UINT32, &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(unsigned 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(%u), 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_UINT32, &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(%u), volume(%f)", uid, volume);
149 dbus_connection_flush(g_conn_sender);
152 dbus_message_unref(msg);
157 int sttdc_send_set_state(unsigned 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_UINT32, &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(%u), state(%d)", uid, state);
193 dbus_connection_flush(g_conn_sender);
196 dbus_message_unref(msg);
201 int sttdc_send_speech_status(unsigned 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_UINT32, &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(%u), status(%d)", uid, status);
237 dbus_connection_flush(g_conn_sender);
240 dbus_message_unref(msg);
245 int sttdc_send_result(unsigned 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(%u), 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_UINT32, &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(unsigned 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_UINT32, &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_ERROR, TAG_STTD, "<<<< Send error message : uid(%u), 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);
404 DBusMessage* msg = NULL;
405 msg = dbus_connection_pop_message(g_conn_listener);
407 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
408 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Connection is disconnected");
409 return ECORE_CALLBACK_RENEW;
412 /* loop again if we haven't read a message */
414 return ECORE_CALLBACK_RENEW;
417 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,
418 (NULL == g_server_service_object) ? "NULL" : g_server_service_object, (NULL == g_server_service_interface) ? "NULL" : g_server_service_interface);
421 if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_HELLO))
422 sttd_dbus_server_hello(g_conn_listener, msg);
424 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_INITIALIZE))
425 sttd_dbus_server_initialize(g_conn_listener, msg);
427 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_FINALIZE))
428 sttd_dbus_server_finalize(g_conn_listener, msg);
430 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_CURRENT_ENGINE))
431 sttd_dbus_server_set_current_engine(g_conn_listener, msg);
433 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_CHECK_APP_AGREED))
434 sttd_dbus_server_check_app_agreed(g_conn_listener, msg);
436 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_GET_SUPPORT_LANGS))
437 sttd_dbus_server_get_support_lang(g_conn_listener, msg);
439 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_GET_CURRENT_LANG))
440 sttd_dbus_server_get_default_lang(g_conn_listener, msg);
442 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_PRIVATE_DATA))
443 sttd_dbus_server_set_private_data(g_conn_listener, msg);
445 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_GET_PRIVATE_DATA))
446 sttd_dbus_server_get_private_data(g_conn_listener, msg);
448 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_IS_TYPE_SUPPORTED))
449 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_GET_AUDIO_FORMAT))
452 sttd_dbus_server_get_audio_format(g_conn_listener, msg);
455 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_START_SOUND))
456 sttd_dbus_server_set_start_sound(g_conn_listener, msg);
458 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_UNSET_START_SOUND))
459 sttd_dbus_server_unset_start_sound(g_conn_listener, msg);
461 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SET_STOP_SOUND))
462 sttd_dbus_server_set_stop_sound(g_conn_listener, msg);
464 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_UNSET_STOP_SOUND))
465 sttd_dbus_server_unset_stop_sound(g_conn_listener, msg);
468 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_START))
469 sttd_dbus_server_start(g_conn_listener, msg);
471 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_STOP))
472 sttd_dbus_server_stop(g_conn_listener, msg);
474 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_START_AUDIO_STREAMING))
475 sttd_dbus_server_start_audio_streaming(g_conn_listener, msg);
477 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_SEND_AUDIO_STREAMING))
478 sttd_dbus_server_send_audio_streaming(g_conn_listener, msg);
480 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_STOP_AUDIO_STREAMING))
481 sttd_dbus_server_stop_audio_streaming(g_conn_listener, msg);
483 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_CANCEL))
484 sttd_dbus_server_cancel(g_conn_listener, msg);
486 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_START_FILE))
487 sttd_dbus_server_start_file(g_conn_listener, msg);
489 else if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_CANCEL_FILE))
490 sttd_dbus_server_cancel_file(g_conn_listener, msg);
492 /* free the message */
493 dbus_message_unref(msg);
496 return ECORE_CALLBACK_RENEW;
499 void __sttd_dbus_service_free()
501 if (NULL != g_server_service_name) {
502 free(g_server_service_name);
503 g_server_service_name = NULL;
506 if (NULL != g_server_service_object) {
507 free(g_server_service_object);
508 g_server_service_object = NULL;
511 if (NULL != g_server_service_interface) {
512 free(g_server_service_interface);
513 g_server_service_interface = NULL;
517 void __sttd_dbus_connection_free()
519 if (NULL != g_conn_listener) {
520 dbus_connection_close(g_conn_listener);
521 dbus_connection_unref(g_conn_listener);
522 g_conn_listener = NULL;
524 if (NULL != g_conn_sender) {
525 dbus_connection_close(g_conn_sender);
526 dbus_connection_unref(g_conn_sender);
527 g_conn_sender = NULL;
529 if (NULL != g_conn_custom) {
530 dbus_connection_close(g_conn_custom);
531 dbus_connection_unref(g_conn_custom);
532 g_conn_custom = NULL;
536 int __sttd_get_buxtonkey()
538 /* request our name on the bus and check for errors */
540 char engine_appid[2048] = {0, };
541 const char* engine_default = NULL;
543 struct buxton_client * bux_cli;
544 struct buxton_layer * bux_layer;
545 struct buxton_value * bux_val;
547 int ret = buxton_open(&bux_cli, NULL, NULL);
549 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to open buxton client, ret(%d)", ret);
550 return STTD_ERROR_OPERATION_FAILED;
552 bux_layer = buxton_create_layer("system");
553 if (NULL == bux_layer) {
554 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] buxton_create_layer FAIL");
555 buxton_close(bux_cli);
557 return STTD_ERROR_OPERATION_FAILED;
559 bux_val = buxton_value_create_string("stt-engine-default");
560 if (NULL == bux_val) {
561 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] buxton_value_create_string FAIL");
562 buxton_free_layer(bux_layer);
563 buxton_close(bux_cli);
566 return STTD_ERROR_OPERATION_FAILED;
568 SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] layer: %s", buxton_layer_get_name(bux_layer));
570 ret = buxton_get_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_DEFAULT, &bux_val);
572 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to get value sync, ret(%d)", ret);
573 buxton_value_free(bux_val);
574 buxton_free_layer(bux_layer);
575 buxton_close(bux_cli);
580 return STTD_ERROR_OPERATION_FAILED;
582 ret = buxton_value_get_string(bux_val, &engine_default);
584 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to get value string, ret(%d)", ret);
585 buxton_value_free(bux_val);
586 buxton_free_layer(bux_layer);
587 buxton_close(bux_cli);
592 return STTD_ERROR_OPERATION_FAILED;
594 SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] buxton_value_get_string: engine_default: %s", engine_default);
596 ret = aul_app_get_appid_bypid(pid, engine_appid, sizeof(engine_appid) - 1);
598 __sttd_dbus_service_free();
600 if (NULL == engine_default || 0 == strncmp(engine_appid, engine_default, strlen(engine_appid))) {
601 g_server_service_name = (char*)calloc(strlen(STT_SERVER_SERVICE_NAME) + 1, sizeof(char));
602 if (g_server_service_name)
603 snprintf(g_server_service_name, strlen(STT_SERVER_SERVICE_NAME) + 1, "%s", STT_SERVER_SERVICE_NAME);
604 g_server_service_object = (char*)calloc(strlen(STT_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
605 if (g_server_service_object)
606 snprintf(g_server_service_object, strlen(STT_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", STT_SERVER_SERVICE_OBJECT_PATH);
607 g_server_service_interface = (char*)calloc(strlen(STT_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
608 if (g_server_service_interface)
609 snprintf(g_server_service_interface, strlen(STT_SERVER_SERVICE_INTERFACE) + 1, "%s", STT_SERVER_SERVICE_INTERFACE);
611 g_server_service_name = (char*)calloc(strlen(STT_SERVER_SERVICE_NAME) + strlen(engine_appid) - 8, sizeof(char));
612 if (g_server_service_name)
613 snprintf(g_server_service_name, strlen(STT_SERVER_SERVICE_NAME) + strlen(engine_appid) - 8, "%s%s%s", STT_SERVER_SERVICE_NAME, ".", (engine_appid + 10));
614 g_server_service_object = (char*)calloc(strlen(STT_SERVER_SERVICE_OBJECT_PATH) + strlen(engine_appid) - 8, sizeof(char));
615 if (g_server_service_object)
616 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));
617 g_server_service_interface = (char*)calloc(strlen(STT_SERVER_SERVICE_INTERFACE) + strlen(engine_appid) - 8, sizeof(char));
618 if (g_server_service_interface)
619 snprintf(g_server_service_interface, strlen(STT_SERVER_SERVICE_INTERFACE) + strlen(engine_appid) - 8, "%s%s%s", STT_SERVER_SERVICE_INTERFACE, ".", (engine_appid + 10));
622 if (NULL == g_server_service_name || NULL == g_server_service_object || NULL == g_server_service_interface) {
623 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] dbus info invalid");
624 buxton_value_free(bux_val);
625 buxton_free_layer(bux_layer);
626 buxton_close(bux_cli);
632 __sttd_dbus_service_free();
633 return STTD_ERROR_OPERATION_FAILED;
636 char* ret_char = strchr(g_server_service_interface, '-');
637 while (NULL != ret_char) {
639 ret_char = strchr(g_server_service_interface, '-');
642 SLOG(LOG_DEBUG, TAG_STTD, "[get_service_interface] service interface: %s", g_server_service_interface);
644 buxton_value_free(bux_val);
645 buxton_free_layer(bux_layer);
646 buxton_close(bux_cli);
652 SLOG(LOG_DEBUG, TAG_STTD, "[get_service_name] FINISH!! service name: %s", g_server_service_name);
654 return STTD_ERROR_NONE;
658 int __sttd_request_custom_dbus_name()
661 dbus_error_init(&err);
663 /* Create connection for sender */
664 g_conn_custom = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
665 if (dbus_error_is_set(&err)) {
666 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
667 dbus_error_free(&err);
668 __sttd_dbus_connection_free();
669 return STTD_ERROR_OPERATION_FAILED;
672 if (NULL == g_conn_custom) {
673 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] fail to get dbus connection");
674 return STTD_ERROR_OPERATION_FAILED;
677 dbus_connection_set_exit_on_disconnect(g_conn_custom, false);
679 int ret = dbus_bus_request_name(g_conn_custom, STT_SERVER_CUSTOM_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
680 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
681 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
682 __sttd_dbus_connection_free();
683 return STTD_ERROR_OPERATION_FAILED;
686 if (dbus_error_is_set(&err)) {
687 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
688 dbus_error_free(&err);
689 __sttd_dbus_connection_free();
690 return STTD_ERROR_OPERATION_FAILED;
693 dbus_bus_release_name(g_conn_custom, STT_SERVER_CUSTOM_SERVICE_NAME, &err);
694 if (dbus_error_is_set(&err)) {
695 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
696 dbus_error_free(&err);
697 __sttd_dbus_connection_free();
698 return STTD_ERROR_OPERATION_FAILED;
701 dbus_connection_close(g_conn_custom);
702 dbus_connection_unref(g_conn_custom);
703 g_conn_custom = NULL;
705 SLOG(LOG_DEBUG, TAG_STTD, "==");
709 int sttd_dbus_open_connection()
712 dbus_error_init(&err);
716 /* Create connection for sender */
717 g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
718 if (dbus_error_is_set(&err)) {
719 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
720 dbus_error_free(&err);
723 if (NULL == g_conn_sender) {
724 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection sender");
725 return STTD_ERROR_OPERATION_FAILED;
728 dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
730 /* connect to the bus and check for errors */
731 g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
733 if (dbus_error_is_set(&err)) {
734 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
735 dbus_error_free(&err);
738 if (NULL == g_conn_listener) {
739 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection");
740 __sttd_dbus_connection_free();
741 return STTD_ERROR_OPERATION_FAILED;
744 dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
747 ret = __sttd_get_buxtonkey();
749 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get buxton key");
750 __sttd_dbus_service_free();
751 __sttd_dbus_connection_free();
755 ret = __sttd_request_custom_dbus_name();
757 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to request custom dbus name");
758 __sttd_dbus_service_free();
759 __sttd_dbus_connection_free();
763 ret = dbus_bus_request_name(g_conn_listener, g_server_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
765 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
766 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
767 __sttd_dbus_service_free();
768 __sttd_dbus_connection_free();
769 return STTD_ERROR_OPERATION_FAILED;
772 if (dbus_error_is_set(&err)) {
773 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
774 dbus_error_free(&err);
775 __sttd_dbus_service_free();
776 __sttd_dbus_connection_free();
777 return STTD_ERROR_OPERATION_FAILED;
780 /* Flush messages which are received before fd event handler registration */
781 /* while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(g_conn_listener)) {
782 listener_event_callback(NULL, NULL);
785 /* add a rule for getting signal */
787 snprintf(rule, 128, "type='signal',interface='%s'", g_server_service_interface);
789 /* add a rule for which messages we want to see */
790 dbus_bus_add_match(g_conn_listener, rule, &err); /* see signals from the given interface */
791 dbus_connection_flush(g_conn_listener);
793 if (dbus_error_is_set(&err)) {
794 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
795 dbus_error_free(&err);
796 __sttd_dbus_service_free();
797 __sttd_dbus_connection_free();
798 return STTD_ERROR_OPERATION_FAILED;
802 dbus_connection_get_unix_fd(g_conn_listener, &fd);
804 g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
805 if (NULL == g_dbus_fd_handler) {
806 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get fd handler");
807 __sttd_dbus_service_free();
808 __sttd_dbus_connection_free();
809 return STTD_ERROR_OPERATION_FAILED;
815 int sttd_dbus_close_connection()
818 dbus_error_init(&err);
820 if (NULL != g_dbus_fd_handler) {
821 ecore_main_fd_handler_del(g_dbus_fd_handler);
822 g_dbus_fd_handler = NULL;
825 if (NULL != g_conn_listener) {
826 dbus_bus_release_name(g_conn_listener, g_server_service_name, &err);
827 if (dbus_error_is_set(&err)) {
828 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
829 dbus_error_free(&err);
833 __sttd_dbus_connection_free();
834 __sttd_dbus_service_free();
836 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Close dbus connection");