Update version (1.90.2)
[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(unsigned 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(%u)", 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_UINT32, &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(unsigned 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(%u), 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_UINT32, &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(%u), 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(unsigned 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_UINT32, &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(%u), 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(unsigned 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_UINT32, &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(%u), 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(unsigned 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(%u), 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_UINT32, &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(unsigned 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_UINT32, &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_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);
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         while(1) {
404                 DBusMessage* msg = NULL;
405                 msg = dbus_connection_pop_message(g_conn_listener);
406
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;
410                 }
411
412                 /* loop again if we haven't read a message */
413                 if (NULL == msg) {
414                         return ECORE_CALLBACK_RENEW;
415                 }
416
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);
419
420                 /* client event */
421                 if (dbus_message_is_method_call(msg, g_server_service_interface, STT_METHOD_HELLO))
422                         sttd_dbus_server_hello(g_conn_listener, msg);
423
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);
426
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);
429
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);
432
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);
435
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);
438
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);
441
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);
444
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);
447
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);
450
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);
453
454
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);
457
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);
460
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);
463
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);
466
467
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);
470
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);
473
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);
476
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);
479
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);
482
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);
485
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);
488
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);
491
492                 /* free the message */
493                 dbus_message_unref(msg);
494         }
495
496         return ECORE_CALLBACK_RENEW;
497 }
498
499 void __sttd_dbus_service_free()
500 {
501         if (NULL != g_server_service_name) {
502                 free(g_server_service_name);
503                 g_server_service_name = NULL;
504         }
505
506         if (NULL != g_server_service_object) {
507                 free(g_server_service_object);
508                 g_server_service_object = NULL;
509         }
510
511         if (NULL != g_server_service_interface) {
512                 free(g_server_service_interface);
513                 g_server_service_interface = NULL;
514         }
515 }
516
517 void __sttd_dbus_connection_free()
518 {
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;
523         }
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;
528         }
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;
533         }
534 }
535
536 int __sttd_get_buxtonkey()
537 {
538         /* request our name on the bus and check for errors */
539         int pid = getpid();
540         char engine_appid[2048] = {0, };
541         const char* engine_default = NULL;
542
543         struct buxton_client * bux_cli;
544         struct buxton_layer * bux_layer;
545         struct buxton_value * bux_val;
546
547         int ret = buxton_open(&bux_cli, NULL, NULL);
548         if (0 != ret) {
549                 SLOG(LOG_ERROR, TAG_STTD, "[DBUS-BUXTON2] Fail to open buxton client, ret(%d)", ret);
550                 return STTD_ERROR_OPERATION_FAILED;
551         }
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);
556                 bux_cli = NULL;
557                 return STTD_ERROR_OPERATION_FAILED;
558         }
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);
564                 bux_layer = NULL;
565                 bux_cli = NULL;
566                 return STTD_ERROR_OPERATION_FAILED;
567         } else {
568                 SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] layer: %s", buxton_layer_get_name(bux_layer));
569         }
570         ret = buxton_get_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_DEFAULT, &bux_val);
571         if (0 != ret) {
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);
576
577                 bux_cli = NULL;
578                 bux_layer = NULL;
579                 bux_val = NULL;
580                 return STTD_ERROR_OPERATION_FAILED;
581         }
582         ret = buxton_value_get_string(bux_val, &engine_default);
583         if (0 != ret) {
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);
588
589                 bux_cli = NULL;
590                 bux_layer = NULL;
591                 bux_val = NULL;
592                 return STTD_ERROR_OPERATION_FAILED;
593         }
594         SLOG(LOG_DEBUG, TAG_STTD, "[DBUS-BUXTON2] buxton_value_get_string: engine_default: %s", engine_default);
595
596         ret = aul_app_get_appid_bypid(pid, engine_appid, sizeof(engine_appid) - 1);
597
598         __sttd_dbus_service_free();
599
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);
610         } else {
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));
620         }
621
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);
627
628                 bux_cli = NULL;
629                 bux_layer = NULL;
630                 bux_val = NULL;
631
632                 __sttd_dbus_service_free();
633                 return STTD_ERROR_OPERATION_FAILED;
634         }
635
636         char* ret_char = strchr(g_server_service_interface, '-');
637         while (NULL != ret_char) {
638                 ret_char[0] = '_';
639                 ret_char = strchr(g_server_service_interface, '-');
640         }
641
642         SLOG(LOG_DEBUG, TAG_STTD, "[get_service_interface] service interface: %s", g_server_service_interface);
643
644         buxton_value_free(bux_val);
645         buxton_free_layer(bux_layer);
646         buxton_close(bux_cli);
647
648         bux_cli = NULL;
649         bux_layer = NULL;
650         bux_val = NULL;
651
652         SLOG(LOG_DEBUG, TAG_STTD, "[get_service_name] FINISH!! service name: %s", g_server_service_name);
653
654         return STTD_ERROR_NONE;
655
656 }
657
658 int __sttd_request_custom_dbus_name()
659 {
660         DBusError err;
661         dbus_error_init(&err);
662
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;
670         }
671
672         if (NULL == g_conn_custom) {
673                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] fail to get dbus connection");
674                 return STTD_ERROR_OPERATION_FAILED;
675         }
676
677         dbus_connection_set_exit_on_disconnect(g_conn_custom, false);
678
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;
684         }
685
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;
691         }
692
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;
699         }
700
701         dbus_connection_close(g_conn_custom);
702         dbus_connection_unref(g_conn_custom);
703         g_conn_custom = NULL;
704
705         SLOG(LOG_DEBUG, TAG_STTD, "==");
706         return 0;
707 }
708
709 int sttd_dbus_open_connection()
710 {
711         DBusError err;
712         dbus_error_init(&err);
713
714         int ret;
715
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);
721         }
722
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;
726         }
727
728         dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
729
730         /* connect to the bus and check for errors */
731         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
732
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);
736         }
737
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;
742         }
743
744         dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
745
746         /* Get buxton key */
747         ret = __sttd_get_buxtonkey();
748         if (0 != ret) {
749                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get buxton key");
750                 __sttd_dbus_service_free();
751                 __sttd_dbus_connection_free();
752                 return ret;
753         }
754
755         ret = __sttd_request_custom_dbus_name();
756         if (0 != ret) {
757                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to request custom dbus name");
758                 __sttd_dbus_service_free();
759                 __sttd_dbus_connection_free();
760                 return ret;
761         }
762
763         ret = dbus_bus_request_name(g_conn_listener, g_server_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
764
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;
770         }
771
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;
778         }
779
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);
783         }
784 */
785         /* add a rule for getting signal */
786         char rule[128];
787         snprintf(rule, 128, "type='signal',interface='%s'", g_server_service_interface);
788
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);
792
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;
799         }
800
801         int fd = 0;
802         dbus_connection_get_unix_fd(g_conn_listener, &fd);
803
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;
810         }
811
812         return 0;
813 }
814
815 int sttd_dbus_close_connection()
816 {
817         DBusError err;
818         dbus_error_init(&err);
819
820         if (NULL != g_dbus_fd_handler) {
821                 ecore_main_fd_handler_del(g_dbus_fd_handler);
822                 g_dbus_fd_handler = NULL;
823         }
824
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);
830                 }
831         }
832
833         __sttd_dbus_connection_free();
834         __sttd_dbus_service_free();
835
836         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Close dbus connection");
837
838         return 0;
839 }