Merge "Add stte errors" into tizen
[platform/core/uifw/stt.git] / server / sttd_dbus.c
1 /*
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.
12 */
13
14
15 #include <dbus/dbus.h>
16 #include <Ecore.h>
17
18 #include "sttd_main.h"
19 #include "sttd_dbus.h"
20 #include "sttd_client_data.h"
21 #include "sttd_dbus_server.h"
22 #include "stt_defs.h"
23 #include <aul.h>
24 #include <vconf.h>
25 #include <buxton2.h>
26
27 static DBusConnection* g_conn_sender = NULL;
28 static DBusConnection* g_conn_listener = NULL;
29 static DBusConnection* g_conn_custom = NULL;
30
31 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
32
33 static int g_waiting_time = 3000;
34
35 static int g_internal_result_id = 0;
36
37 static char* g_server_service_name = NULL;
38 static char* g_server_service_object = NULL;
39 static char* g_server_service_interface = NULL;
40
41 void __sttd_dbus_service_free();
42
43 int sttdc_send_hello(int uid)
44 {
45         int pid = sttd_client_get_pid(uid);
46         if (0 > pid) {
47                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
48                 return STTD_ERROR_INVALID_PARAMETER;
49         }
50
51         char service_name[64];
52         memset(service_name, 0, 64);
53         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
54
55         char target_if_name[128];
56         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
57
58         DBusMessage* msg = NULL;
59
60         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send hello message : uid(%d)", uid);
61
62         msg = dbus_message_new_method_call(
63                 service_name,
64                 STT_CLIENT_SERVICE_OBJECT_PATH,
65                 target_if_name,
66                 STTD_METHOD_HELLO);
67
68         if (NULL == msg) {
69                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
70                 return STTD_ERROR_OUT_OF_MEMORY;
71         }
72
73         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
74
75         DBusError err;
76         dbus_error_init(&err);
77         int result = -1;
78
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);
88                                 return 0;
89                         }
90                 }
91                 dbus_error_free(&err);
92         }
93
94         if (NULL != result_msg) {
95                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
96
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);
100                 }
101
102                 dbus_message_unref(result_msg);
103         } else {
104                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Result message is NULL. Client is not available");
105         }
106
107         return result;
108 }
109
110 int sttdc_send_set_volume(int uid, float volume)
111 {
112         int pid = sttd_client_get_pid(uid);
113
114         if (0 > pid) {
115                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
116                 return -1;
117         }
118
119         char service_name[64];
120         memset(service_name, 0, 64);
121         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
122
123         char target_if_name[128];
124         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
125
126         DBusMessage* msg;
127
128         /* SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send set volume : uid(%d), volume(%f) volume size(%d)", uid, volume, sizeof(float)); */
129
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 */
134
135         if (NULL == msg) {
136                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
137                 return -1;
138         }
139
140         dbus_message_append_args(msg,
141                 DBUS_TYPE_INT32, &uid,
142                 DBUS_TYPE_INT32, &volume,
143                 DBUS_TYPE_INVALID);
144
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 !");
147         } else {
148                 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send set volume : uid(%d), volume(%f)", uid, volume);
149                 dbus_connection_flush(g_conn_sender);
150         }
151
152         dbus_message_unref(msg);
153
154         return 0;
155 }
156
157 int sttdc_send_set_state(int uid, int state)
158 {
159         int pid = sttd_client_get_pid(uid);
160
161         if (0 > pid) {
162                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
163                 return -1;
164         }
165
166         char service_name[64];
167         memset(service_name, 0, 64);
168         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
169
170         char target_if_name[128];
171         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
172
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 */
178
179         if (NULL == msg) {
180                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
181                 return -1;
182         }
183
184         dbus_message_append_args(msg,
185                 DBUS_TYPE_INT32, &uid,
186                 DBUS_TYPE_INT32, &state,
187                 DBUS_TYPE_INVALID);
188
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 !");
191         } else {
192                 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send change state message : uid(%d), state(%d)", uid, state);
193                 dbus_connection_flush(g_conn_sender);
194         }
195
196         dbus_message_unref(msg);
197
198         return 0;
199 }
200
201 int sttdc_send_speech_status(int uid, int status)
202 {
203         int pid = sttd_client_get_pid(uid);
204
205         if (0 > pid) {
206                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
207                 return -1;
208         }
209
210         char service_name[64];
211         memset(service_name, 0, 64);
212         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
213
214         char target_if_name[128];
215         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
216
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 */
222
223         if (NULL == msg) {
224                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
225                 return -1;
226         }
227
228         dbus_message_append_args(msg,
229                 DBUS_TYPE_INT32, &uid,
230                 DBUS_TYPE_INT32, &status,
231                 DBUS_TYPE_INVALID);
232
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 !");
235         } else {
236                 SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send speech status message : uid(%d), status(%d)", uid, status);
237                 dbus_connection_flush(g_conn_sender);
238         }
239
240         dbus_message_unref(msg);
241
242         return 0;
243 }
244
245 int sttdc_send_result(int uid, int event, const char** data, int data_count, const char* result_msg)
246 {
247         int pid = sttd_client_get_pid(uid);
248         if (0 > pid) {
249                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
250                 return STTD_ERROR_INVALID_PARAMETER;
251         }
252
253         char service_name[64];
254         memset(service_name, 0, 64);
255         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
256
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);
260
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);
264
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 */
269
270         if (NULL == msg) {
271                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
272                 return STTD_ERROR_OUT_OF_MEMORY;
273         }
274
275         DBusMessageIter args;
276         dbus_message_iter_init_append(msg, &args);
277
278         /* Append uid & type */
279         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &uid);
280
281         char* msg_temp;
282         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &event);
283
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");
289                 free(msg_temp);
290         } else {
291                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result message(%s)", result_msg);
292                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(result_msg));
293         }
294
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);
299                 return -1;
300         }
301
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);
305                 return -1;
306         }
307
308         g_internal_result_id++;
309
310         if (10000 == g_internal_result_id) {
311                 g_internal_result_id = 1;
312         }
313
314         int i;
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]);
319
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;
324                         }
325                 } else {
326                         int reason = (int)STTD_ERROR_OPERATION_FAILED;
327
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");
330
331                                 /* clean client data */
332                                 sttd_client_delete(uid);
333                         }
334
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;
338                 }
339         }
340
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 !");
343         }
344
345         dbus_connection_flush(g_conn_sender);
346         dbus_message_unref(msg);
347
348         return 0;
349 }
350
351 int sttdc_send_error_signal(int uid, int reason, const char *err_msg)
352 {
353         int pid = sttd_client_get_pid(uid);
354         if (0 > pid) {
355                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
356                 return STTD_ERROR_INVALID_PARAMETER;
357         }
358
359         char service_name[64];
360         memset(service_name, 0, 64);
361         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
362
363         char target_if_name[128];
364         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
365
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 */
371
372         if (NULL == msg) {
373                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
374                 return STTD_ERROR_OUT_OF_MEMORY;
375         }
376
377         dbus_message_append_args(msg,
378                 DBUS_TYPE_INT32, &uid,
379                 DBUS_TYPE_INT32, &reason,
380                 DBUS_TYPE_STRING, &err_msg,
381                 DBUS_TYPE_INVALID);
382
383         if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
384                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] <<<< error message : Out Of Memory !");
385         } else {
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);
388         }
389
390         dbus_message_unref(msg);
391
392         return 0;
393 }
394
395 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
396 {
397         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] listener_event_callback is called!!!");
398
399         if (NULL == g_conn_listener)    return ECORE_CALLBACK_RENEW;
400
401         dbus_connection_read_write_dispatch(g_conn_listener, 50);
402
403         DBusMessage* msg = NULL;
404         msg = dbus_connection_pop_message(g_conn_listener);
405
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;
409         }
410
411         /* loop again if we haven't read a message */
412         if (NULL == msg) {
413                 return ECORE_CALLBACK_RENEW;
414         }
415
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);
418
419         /* client event */
420         if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_HELLO))
421                 sttd_dbus_server_hello(g_conn_listener, msg);
422
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);
425
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);
428
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);
431
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);
434
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);
437
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);
440
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);
443
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);
446
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);
449
450
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);
453
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);
456
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);
459
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);
462
463
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);
466
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);
469
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);
472
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);
475
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);
478
479         /* free the message */
480         dbus_message_unref(msg);
481
482         return ECORE_CALLBACK_RENEW;
483 }
484
485 void __sttd_dbus_service_free()
486 {
487         if (NULL != g_server_service_name) {
488                 free(g_server_service_name);
489                 g_server_service_name = NULL;
490         }
491
492         if (NULL != g_server_service_object) {
493                 free(g_server_service_object);
494                 g_server_service_object = NULL;
495         }
496
497         if (NULL != g_server_service_interface) {
498                 free(g_server_service_interface);
499                 g_server_service_interface = NULL;
500         }
501 }
502
503 void __sttd_dbus_connection_free()
504 {
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;
509         }
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;
514         }
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;
519         }
520 }
521
522 int __sttd_get_buxtonkey()
523 {
524         /* request our name on the bus and check for errors */
525         int pid = getpid();
526         char engine_appid[2048] = {0, };
527         const char* engine_default = NULL;
528
529         struct buxton_client * bux_cli;
530         struct buxton_layer * bux_layer;
531         struct buxton_value * bux_val;
532
533         int ret = buxton_open(&bux_cli, NULL, NULL);
534         if (0 != ret) {
535                 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to open buxton client, ret(%d)", ret);
536                 return STTD_ERROR_OPERATION_FAILED;
537         }
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);
542                 bux_cli = NULL;
543                 return STTD_ERROR_OPERATION_FAILED;
544         }
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);
550                 bux_layer = NULL;
551                 bux_cli = NULL;
552                 return STTD_ERROR_OPERATION_FAILED;
553         } else {
554                 SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] layer: %s", buxton_layer_get_name(bux_layer));
555         }
556         ret = buxton_get_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_DEFAULT, &bux_val);
557         if (0 != ret) {
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);
562
563                 bux_cli = NULL;
564                 bux_layer = NULL;
565                 bux_val = NULL;
566                 return STTD_ERROR_OPERATION_FAILED;
567         }
568         ret = buxton_value_get_string(bux_val, &engine_default);
569         if (0 != ret) {
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);
574
575                 bux_cli = NULL;
576                 bux_layer = NULL;
577                 bux_val = NULL;
578                 return STTD_ERROR_OPERATION_FAILED;
579         }
580         SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] buxton_value_get_string: engine_default: %s", engine_default);
581
582         ret = aul_app_get_appid_bypid(pid, engine_appid, sizeof(engine_appid) - 1);
583
584         __sttd_dbus_service_free();
585
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);
596         } else {
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));
606         }
607
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);
613
614                 bux_cli = NULL;
615                 bux_layer = NULL;
616                 bux_val = NULL;
617
618                 __sttd_dbus_service_free();
619                 return STTD_ERROR_OPERATION_FAILED;
620         }
621
622         char* ret_char = strchr(g_server_service_interface, '-');
623         while (NULL != ret_char) {
624                 ret_char[0] = '_';
625                 ret_char = strchr(g_server_service_interface, '-');
626         }
627
628         SLOG(LOG_DEBUG, TAG_STTD, "[get_service_interface] service interface: %s", g_server_service_interface);
629
630         buxton_value_free(bux_val);
631         buxton_free_layer(bux_layer);
632         buxton_close(bux_cli);
633
634         bux_cli = NULL;
635         bux_layer = NULL;
636         bux_val = NULL;
637
638         SLOG(LOG_DEBUG, TAG_STTD, "[get_service_name] FINISH!! service name: %s", g_server_service_name);
639
640         return STTD_ERROR_NONE;
641
642 }
643
644 int __sttd_request_custom_dbus_name()
645 {
646         DBusError err;
647         dbus_error_init(&err);
648
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;
656         }
657
658         if (NULL == g_conn_custom) {
659                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] fail to get dbus connection");
660                 return STTD_ERROR_OPERATION_FAILED;
661         }
662
663         dbus_connection_set_exit_on_disconnect(g_conn_custom, false);
664
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;
670         }
671
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;
677         }
678
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;
685         }
686
687         dbus_connection_close(g_conn_custom);
688         dbus_connection_unref(g_conn_custom);
689         g_conn_custom = NULL;
690
691         SLOG(LOG_DEBUG, TAG_STTD, "==");
692         return 0;
693 }
694
695 int sttd_dbus_open_connection()
696 {
697         DBusError err;
698         dbus_error_init(&err);
699
700         int ret;
701
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);
707         }
708
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;
712         }
713
714         dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
715
716         /* connect to the bus and check for errors */
717         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
718
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);
722         }
723
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;
728         }
729
730         dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
731
732         /* Get buxton key */
733         ret = __sttd_get_buxtonkey();
734         if (0 != ret) {
735                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get buxton key");
736                 __sttd_dbus_service_free();
737                 __sttd_dbus_connection_free();
738                 return ret;
739         }
740
741         ret = __sttd_request_custom_dbus_name();
742         if (0 != ret) {
743                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to request custom dbus name");
744                 __sttd_dbus_service_free();
745                 __sttd_dbus_connection_free();
746                 return ret;
747         }
748
749         ret = dbus_bus_request_name(g_conn_listener, g_server_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
750
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;
756         }
757
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;
764         }
765
766         /* add a rule for getting signal */
767         char rule[128];
768         snprintf(rule, 128, "type='signal',interface='%s'", g_server_service_interface);
769
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);
773
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;
780         }
781
782         int fd = 0;
783         dbus_connection_get_unix_fd(g_conn_listener, &fd);
784
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;
791         }
792
793         return 0;
794 }
795
796 int sttd_dbus_close_connection()
797 {
798         DBusError err;
799         dbus_error_init(&err);
800
801         if (NULL != g_dbus_fd_handler) {
802                 ecore_main_fd_handler_del(g_dbus_fd_handler);
803                 g_dbus_fd_handler = NULL;
804         }
805
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);
811                 }
812         }
813
814         __sttd_dbus_connection_free();
815         __sttd_dbus_service_free();
816
817         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Close dbus connection");
818
819         return 0;
820 }