Tizen 2.1 base
[platform/core/uifw/stt.git] / server / sttd_dbus.c
1 /*
2 * Copyright (c) 2012, 2013 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
24 static DBusConnection* g_conn;
25 static int g_waiting_time = 3000;
26
27 int sttdc_send_hello(int uid)
28 {
29         int pid = sttd_client_get_pid(uid);
30
31         if (0 > pid) {
32                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
33                 return -1;
34         }
35
36         char service_name[64];
37         memset(service_name, 0, 64);
38         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
39
40         char target_if_name[128];
41         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
42
43         DBusMessage* msg;
44
45         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send hello message : uid(%d)", uid);
46
47         msg = dbus_message_new_method_call(
48                 service_name, 
49                 STT_CLIENT_SERVICE_OBJECT_PATH, 
50                 target_if_name, 
51                 STTD_METHOD_HELLO);
52
53         if (NULL == msg) { 
54                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message"); 
55                 return -1;
56         }
57
58         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
59
60         DBusError err;
61         dbus_error_init(&err);
62
63         DBusMessage* result_msg;
64         int result = -1;
65
66         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
67         dbus_message_unref(msg);
68
69         if (NULL != result_msg) {
70                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
71
72                 if (dbus_error_is_set(&err)) { 
73                         SLOG(LOG_ERROR, TAG_STTD, "[Dbus] Get arguments error (%s)\n", err.message);
74                         dbus_error_free(&err); 
75                         result = -1;
76                 }
77
78                 dbus_message_unref(result_msg);
79         } else {
80                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Result message is NULL. Client is not available");
81                 result = 0;
82         }
83
84         return result;
85 }
86
87 int sttdc_send_get_state(int uid, int* state)
88 {
89         int pid = sttd_client_get_pid(uid);
90
91         if (0 > pid) {
92                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
93                 return -1;
94         }
95
96         char service_name[64];
97         memset(service_name, 0, 64);
98         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
99
100         char target_if_name[128];
101         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
102
103         DBusMessage* msg;
104
105         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send get state message : uid(%d)", uid);
106
107         msg = dbus_message_new_method_call(
108                 service_name, 
109                 STT_CLIENT_SERVICE_OBJECT_PATH, 
110                 target_if_name, 
111                 STTD_METHOD_GET_STATE);
112
113         if (NULL == msg) { 
114                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message"); 
115                 return -1;
116         }
117
118         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
119
120         DBusError err;
121         dbus_error_init(&err);
122
123         DBusMessage* result_msg;
124         int tmp = -1;
125         int result = 0;
126
127         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
128         dbus_message_unref(msg);
129
130         if (NULL != result_msg) {
131                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &tmp, DBUS_TYPE_INVALID);
132
133                 if (dbus_error_is_set(&err)) { 
134                         SLOG(LOG_ERROR, TAG_STTD, "[Dbus] Get arguments error (%s)\n", err.message);
135                         dbus_error_free(&err); 
136                         result = -1;
137                 } else {
138                         *state = tmp;
139                         result = 0;
140                 }
141
142                 dbus_message_unref(result_msg);
143         } else {
144                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] Result message is NULL. Client is not available");
145                 result = -1;
146         }
147         
148         return result;
149 }
150
151 int sttdc_send_result(int uid, const char* type, const char** data, int data_count, const char* result_msg)
152 {
153         int pid = sttd_client_get_pid(uid);
154
155         if (0 > pid) {
156                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid" );
157                 return -1;
158         }
159
160         char service_name[64];
161         memset(service_name, 0, 64);
162         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
163
164         char target_if_name[128];
165         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
166
167         DBusMessage* msg;
168         
169         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] send result signal : uid(%d), type(%s), result count(%d)", uid, type, data_count);
170
171         msg = dbus_message_new_method_call(
172                 service_name, 
173                 STT_CLIENT_SERVICE_OBJECT_PATH, 
174                 target_if_name, 
175                 STTD_METHOD_RESULT);
176
177         if (NULL == msg) { 
178                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message"); 
179                 return -1;
180         }
181
182         DBusMessageIter args;
183         dbus_message_iter_init_append(msg, &args);
184
185         /* Append uid & type */
186         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &uid);
187
188         char* msg_temp;
189         if (NULL == type) {
190                 msg_temp = strdup("None");
191                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(msg_temp));
192                 SLOG(LOG_WARN, TAG_STTD, "[Dbus] result type is NULL"); 
193                 free(msg_temp);
194         } else {
195                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(type));
196                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result type(%s)", type ); 
197         }       
198
199         /* Append result msg */
200         if (NULL == result_msg) {
201                 msg_temp = strdup("None");
202                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &msg_temp);
203                 SLOG(LOG_WARN, TAG_STTD, "[Dbus] result message is NULL"); 
204                 free(msg_temp);
205         } else {
206                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result message(%s)", result_msg ); 
207                 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &(result_msg));
208         }
209         
210         /* Append result size */
211         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(data_count))) {
212                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result size"); 
213                 return -1;
214         }
215
216         int i;
217
218         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result size (%d)", data_count); 
219         for (i=0 ; i<data_count ; i++) {
220                 if (NULL != data[i]) {
221                         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] result (%d, %s)", i, data[i] ); 
222
223                         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &data[i])) {
224                                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result data");
225                                 return -1;
226                         }
227                 } else {
228                         int reason = (int)STTD_ERROR_OPERATION_FAILED;
229
230                         if (0 != sttdc_send_error_signal(uid, reason, "Fail to get recognition result from engine")) {
231                                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send error info. Remove client data"); 
232
233                                 /* clean client data */
234                                 sttd_client_delete(uid);
235                         }
236
237                         SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Result from engine is NULL(%d) %s", i); 
238
239                         return -1;
240                 }
241         }
242         
243         if (!dbus_connection_send(g_conn, msg, NULL)) {
244                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !"); 
245                 return -1;
246         }
247
248         dbus_connection_flush(g_conn);
249         dbus_message_unref(msg);
250
251         return 0;
252 }
253
254 int sttdc_send_partial_result(int uid, const char* data)
255 {
256         int pid = sttd_client_get_pid(uid);
257
258         if (0 > pid) {
259                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid" );
260                 return -1;
261         }
262
263         if (NULL == data) {
264                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Input data is NULL" );
265                 return -1;
266         }
267
268         char service_name[64];
269         memset(service_name, 0, 64);
270         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
271
272         char target_if_name[128];
273         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
274
275         DBusMessage* msg;
276
277         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] send result signal : uid(%d), result(%s)", uid, data);
278  
279         msg = dbus_message_new_method_call(
280                 service_name, 
281                 STT_CLIENT_SERVICE_OBJECT_PATH, 
282                 target_if_name, 
283                 STTD_METHOD_PARTIAL_RESULT);
284
285         if (NULL == msg) { 
286                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message"); 
287                 return -1;
288         }
289
290         DBusMessageIter args;
291         dbus_message_iter_init_append(msg, &args);
292
293         /* Append uid & type */
294         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &uid);
295
296         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Partial result (%s)", data ); 
297
298         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, data)) {
299                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus] response message : Fail to append result data");
300                 return -1;
301         }
302         
303         if (!dbus_connection_send(g_conn, msg, NULL)) {
304                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message : Out Of Memory !"); 
305                 return -1;
306         }
307
308         dbus_connection_flush(g_conn);
309         dbus_message_unref(msg);
310
311         return 0;
312 }
313
314 int sttdc_send_error_signal(int uid, int reason, char *err_msg)
315 {
316         if (NULL == err_msg) {
317                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Input parameter is NULL"); 
318                 return STTD_ERROR_INVALID_PARAMETER;
319         }
320
321         int pid = sttd_client_get_pid(uid);
322
323         if (0 > pid) {
324                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid" );
325                 return -1;
326         }
327
328         char service_name[64];
329         memset(service_name, 0, 64);
330         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
331
332         char target_if_name[128];
333         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
334
335         DBusMessage* msg;
336         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] send error signal : reason(%d), Error Msg(%s)", reason, err_msg);
337
338         msg = dbus_message_new_method_call(
339                 service_name, 
340                 STT_CLIENT_SERVICE_OBJECT_PATH, 
341                 target_if_name, 
342                 STTD_METHOD_ERROR);
343
344         if (NULL == msg) { 
345                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message"); 
346                 return -1;
347         }
348
349         dbus_message_append_args(msg, 
350                 DBUS_TYPE_INT32, &uid, 
351                 DBUS_TYPE_INT32, &reason, 
352                 DBUS_TYPE_STRING, &err_msg,
353                 DBUS_TYPE_INVALID);
354         
355         DBusError err;
356         dbus_error_init(&err);
357
358         DBusMessage* result_msg;
359
360         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
361         dbus_message_unref(msg);
362
363         if (NULL != result_msg) {
364                 dbus_message_unref(result_msg);
365         } else {
366                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Result message is NULL.");
367         }
368
369         return 0;
370 }
371
372 int sttdc_send_set_state(int uid, int state)
373 {
374         int pid = sttd_client_get_pid(uid);
375
376         if (0 > pid) {
377                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
378                 return -1;
379         }
380
381         char service_name[64];
382         memset(service_name, 0, 64);
383         snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
384
385         char target_if_name[128];
386         snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
387
388         DBusMessage* msg;
389
390         SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Send change state message : uid(%d), state(%d)", uid, state);
391
392         msg = dbus_message_new_method_call(
393                 service_name, 
394                 STT_CLIENT_SERVICE_OBJECT_PATH, 
395                 target_if_name, 
396                 STTD_METHOD_SET_STATE);
397
398         if (NULL == msg) { 
399                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message"); 
400                 return -1;
401         }
402
403         dbus_message_append_args(msg, 
404                 DBUS_TYPE_INT32, &uid, 
405                 DBUS_TYPE_INT32, &state, 
406                 DBUS_TYPE_INVALID);
407
408         DBusError err;
409         dbus_error_init(&err);
410
411         DBusMessage* result_msg;
412         int result = -1;
413
414         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, g_waiting_time, &err);
415         dbus_message_unref(msg);
416
417         if (NULL != result_msg) {
418                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
419
420                 if (dbus_error_is_set(&err)) { 
421                         SLOG(LOG_ERROR, TAG_STTD, "[Dbus] Get arguments error (%s)\n", err.message);
422                         dbus_error_free(&err); 
423                         result = -1;
424                 }
425
426                 dbus_message_unref(result_msg);
427         } else {
428                 SLOG(LOG_DEBUG, TAG_STTD, "[Dbus] Result message is NULL. Client is not available");
429         }
430
431         return result;
432 }
433
434 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
435 {
436         DBusConnection* conn = (DBusConnection*)data;
437         DBusMessage* msg = NULL;
438
439         if (NULL == conn)
440                 return ECORE_CALLBACK_RENEW;
441
442         dbus_connection_read_write_dispatch(conn, 50);
443         
444         msg = dbus_connection_pop_message(conn);
445
446         /* loop again if we haven't read a message */
447         if (NULL == msg || NULL == conn) { 
448                 return ECORE_CALLBACK_RENEW;
449         }
450
451
452         /* daemon internal event */
453         if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STTD_METHOD_STOP_BY_DAEMON))
454                 sttd_dbus_server_stop_by_daemon(msg);
455
456         /* client event */
457         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_HELLO))
458                 sttd_dbus_server_hello(conn, msg);
459
460         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_INITIALIZE))
461                 sttd_dbus_server_initialize(conn, msg);
462         
463         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_FINALIZE))
464                 sttd_dbus_server_finalize(conn, msg);
465         
466         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_SUPPORT_LANGS))
467                 sttd_dbus_server_get_support_lang(conn, msg);
468
469         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_CURRENT_LANG))
470                 sttd_dbus_server_get_default_lang(conn, msg);
471
472         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_IS_PARTIAL_SUPPORTED))
473                 sttd_dbus_server_is_partial_result_supported(conn, msg);
474
475         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_GET_AUDIO_VOLUME))
476                 sttd_dbus_server_get_audio_volume(conn, msg);
477
478         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_START)) 
479                 sttd_dbus_server_start(conn, msg);
480         
481         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_STOP)) 
482                 sttd_dbus_server_stop(conn, msg);
483
484         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_METHOD_CANCEL)) 
485                 sttd_dbus_server_cancel(conn, msg);
486
487
488         /* setting event */
489         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_HELLO))
490                 sttd_dbus_server_hello(conn, msg);
491
492         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_INITIALIZE))
493                 sttd_dbus_server_setting_initialize(conn, msg);
494
495         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_FINALIZE))
496                 sttd_dbus_server_setting_finalize(conn, msg);
497
498         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_ENGINE_LIST))
499                 sttd_dbus_server_setting_get_engine_list(conn, msg);
500
501         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_ENGINE))
502                 sttd_dbus_server_setting_get_engine(conn, msg);
503
504         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_SET_ENGINE))
505                 sttd_dbus_server_setting_set_engine(conn, msg);
506
507         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_LANG_LIST))
508                 sttd_dbus_server_setting_get_language_list(conn, msg);
509
510         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_DEFAULT_LANG))
511                 sttd_dbus_server_setting_get_default_language(conn, msg);
512
513         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_SET_DEFAULT_LANG))
514                 sttd_dbus_server_setting_set_default_language(conn, msg);
515
516
517         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_PROFANITY))
518                 sttd_dbus_server_setting_get_profanity_filter(conn, msg);
519
520         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_SET_PROFANITY))
521                 sttd_dbus_server_setting_set_profanity_filter(conn, msg);
522
523         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_PUNCTUATION))
524                 sttd_dbus_server_setting_get_punctuation_override(conn, msg);
525
526         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_SET_PUNCTUATION))
527                 sttd_dbus_server_setting_set_punctuation_override(conn, msg);
528
529         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_SILENCE))
530                 sttd_dbus_server_setting_get_silence_detection(conn, msg);
531
532         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_SET_SILENCE))
533                 sttd_dbus_server_setting_set_silence_detection(conn, msg);
534
535
536         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_GET_ENGINE_SETTING))
537                 sttd_dbus_server_setting_get_engine_setting(conn, msg);
538
539         else if (dbus_message_is_method_call(msg, STT_SERVER_SERVICE_INTERFACE, STT_SETTING_METHOD_SET_ENGINE_SETTING))
540                 sttd_dbus_server_setting_set_engine_setting(conn, msg);
541
542
543         /* free the message */
544         dbus_message_unref(msg);
545
546         return ECORE_CALLBACK_RENEW;
547 }
548
549 int sttd_dbus_open_connection()
550 {
551         DBusError err;
552         dbus_error_init(&err);
553
554         int ret;
555
556         /* connect to the bus and check for errors */
557         g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
558
559         if (dbus_error_is_set(&err)) { 
560                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
561                 dbus_error_free(&err); 
562         }
563
564         if (NULL == g_conn) { 
565                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection" );
566                 return -1;
567         }
568
569         /* request our name on the bus and check for errors */
570         ret = dbus_bus_request_name(g_conn, STT_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
571
572         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
573                 printf("Fail to be primary owner in dbus request.");
574                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
575                 return -1;
576         }
577
578         if (dbus_error_is_set(&err)) { 
579                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
580                 dbus_error_free(&err); 
581                 return -1;
582         }
583
584         /* add a rule for getting signal */
585         char rule[128];
586         snprintf(rule, 128, "type='signal',interface='%s'", STT_SERVER_SERVICE_INTERFACE);
587
588         /* add a rule for which messages we want to see */
589         dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
590         dbus_connection_flush(g_conn);
591
592         if (dbus_error_is_set(&err)) { 
593                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
594                 return -1; 
595         }
596
597         int fd = 0;
598         dbus_connection_get_unix_fd(g_conn, &fd);
599
600         if (!ecore_init()) {
601                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] ecore_init()");
602                 return -1;
603         }
604
605         Ecore_Fd_Handler* fd_handler;
606         fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
607
608         if (NULL == fd_handler) {
609                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get fd handler");
610                 return -1;
611         }
612
613         return 0;
614 }
615
616 int sttd_dbus_close_connection()
617 {
618         DBusError err;
619         dbus_error_init(&err);
620
621         dbus_bus_release_name (g_conn, STT_SERVER_SERVICE_NAME, &err);
622
623         if (dbus_error_is_set(&err)) {
624                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message); 
625                 dbus_error_free(&err); 
626                 return -1;
627         }
628
629         return 0;
630 }
631
632 int sttd_send_stop_recognition_by_daemon(int uid)
633 {
634         DBusMessage* msg;
635
636         msg = dbus_message_new_method_call(
637                 STT_SERVER_SERVICE_NAME, 
638                 STT_SERVER_SERVICE_OBJECT_PATH, 
639                 STT_SERVER_SERVICE_INTERFACE, 
640                 STTD_METHOD_STOP_BY_DAEMON);
641
642         if (NULL == msg) { 
643                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] >>>> Fail to make message for 'stop by daemon'"); 
644                 return -1;
645         } 
646
647         dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);        
648
649         if (!dbus_connection_send(g_conn, msg, NULL)) {
650                 SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send message for 'stop by daemon'"); 
651         }
652
653         dbus_connection_flush(g_conn);
654         dbus_message_unref(msg);
655
656         return 0;
657 }