merge with master
[platform/core/uifw/tts.git] / client / tts_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 <Ecore.h>
16 #include "tts_main.h"
17 #include "tts_dbus.h"
18 #include "tts_defs.h"
19 #include "tts_client.h"
20
21 #define INIT_WAITING_TIME 5000
22 #define WAITING_TIME 1000
23
24 static Ecore_Fd_Handler* g_fd_handler = NULL;
25
26 static DBusConnection* g_conn = NULL;
27
28
29 extern int __tts_cb_error(int uid, tts_error_e reason, int utt_id);
30
31 extern int __tts_cb_set_state(int uid, int state);
32
33 extern int __tts_cb_utt_started(int uid, int utt_id);
34
35 extern int __tts_cb_utt_completed(int uid, int utt_id);
36
37
38 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
39 {
40         DBusConnection* conn = (DBusConnection*)data;
41
42         if (NULL == conn)       return ECORE_CALLBACK_RENEW;
43
44         dbus_connection_read_write_dispatch(conn, 50);
45
46         DBusMessage* msg = NULL;
47         msg = dbus_connection_pop_message(conn);
48
49         /* loop again if we haven't read a message */
50         if (NULL == msg) { 
51                 return ECORE_CALLBACK_RENEW;
52         }
53
54         DBusError err;
55         dbus_error_init(&err);
56
57         DBusMessage *reply = NULL; 
58         
59         char if_name[64];
60         snprintf(if_name, 64, "%s%d", TTS_CLIENT_SERVICE_INTERFACE, getpid());
61
62         /* check if the message is a signal from the correct interface and with the correct name */
63         if (dbus_message_is_method_call(msg, if_name, TTSD_METHOD_HELLO)) {
64                 SLOG(LOG_DEBUG, TAG_TTSC, "===== Get Hello");
65                 int uid = 0;
66                 int response = -1;
67
68                 dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
69
70                 if (uid > 0) {
71                         SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts get hello : uid(%d) \n", uid);
72
73                         /* check uid */
74                         tts_client_s* client = tts_client_get_by_uid(uid);
75                         if (NULL != client) 
76                                 response = 1;
77                         else 
78                                 response = 0;
79                 } else {
80                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts get hello : invalid uid \n");
81                 }
82
83                 reply = dbus_message_new_method_return(msg);
84
85                 if (NULL != reply) {
86                         dbus_message_append_args(reply, DBUS_TYPE_INT32, &response, DBUS_TYPE_INVALID);
87
88                         if (!dbus_connection_send(conn, reply, NULL))
89                                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> tts get hello : fail to send reply");
90                         else 
91                                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> tts get hello : result(%d)", response);
92
93                         dbus_connection_flush(conn);
94                         dbus_message_unref(reply); 
95                 } else {
96                         SLOG(LOG_ERROR, TAG_TTSC, ">>>> tts get hello : fail to create reply message");
97                 }
98
99                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
100                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
101         } /* TTSD_METHOD_HELLO */
102
103         else if (dbus_message_is_method_call(msg, if_name, TTSD_METHOD_UTTERANCE_STARTED)) {
104                 SLOG(LOG_DEBUG, TAG_TTSC, "===== Get utterance started");
105                 int uid, uttid;
106                 dbus_message_get_args(msg, &err,
107                         DBUS_TYPE_INT32, &uid,
108                         DBUS_TYPE_INT32, &uttid,
109                         DBUS_TYPE_INVALID);
110
111                 if (dbus_error_is_set(&err)) { 
112                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< Get Utterance started - Get arguments error (%s)\n", err.message);
113                         dbus_error_free(&err); 
114                 } else {
115                         SLOG(LOG_DEBUG, TAG_TTSC, "<<<< Get Utterance started message : uid(%d), uttid(%d) \n", uid, uttid);
116                         __tts_cb_utt_started(uid, uttid);
117                 }
118
119                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
120                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
121         }/* TTS_SIGNAL_UTTERANCE_STARTED */
122
123         else if (dbus_message_is_method_call(msg, if_name, TTSD_METHOD_UTTERANCE_COMPLETED)) {
124                 SLOG(LOG_DEBUG, TAG_TTSC, "===== Get utterance completed");
125                 int uid, uttid;
126                 dbus_message_get_args(msg, &err,
127                         DBUS_TYPE_INT32, &uid,
128                         DBUS_TYPE_INT32, &uttid,
129                         DBUS_TYPE_INVALID);
130
131                 if (dbus_error_is_set(&err)) { 
132                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< Get Utterance completed - Get arguments error (%s)\n", err.message);
133                         dbus_error_free(&err); 
134                 } else {
135                         SLOG(LOG_DEBUG, TAG_TTSC, "<<<< Get Utterance completed message : uid(%d), uttid(%d) \n", uid, uttid);
136                         __tts_cb_utt_completed(uid, uttid);
137                 }
138
139                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
140                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
141         }/* TTS_SIGNAL_UTTERANCE_COMPLETED */
142
143         else if (dbus_message_is_method_call(msg, if_name, TTSD_METHOD_SET_STATE)) {
144                 SLOG(LOG_DEBUG, TAG_TTSC, "===== Get state changed callback");
145                 int uid;
146                 int state;
147                 dbus_message_get_args(msg, &err,
148                         DBUS_TYPE_INT32, &uid,
149                         DBUS_TYPE_INT32, &state,
150                         DBUS_TYPE_INVALID);
151
152                 if (dbus_error_is_set(&err)) { 
153                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< Get state change - Get arguments error (%s)", err.message);
154                         dbus_error_free(&err); 
155                 } else {
156                         SLOG(LOG_DEBUG, TAG_TTSC, "<<<< Get state change : uid(%d) , state(%d)", uid, state);
157                         __tts_cb_set_state(uid, state);
158                 }
159
160                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
161                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
162         } /* TTSD_METHOD_SET_STATE */
163
164         else if (dbus_message_is_method_call(msg, if_name, TTSD_METHOD_ERROR)) {
165                 SLOG(LOG_DEBUG, TAG_TTSC, "===== Get error callback");
166
167                 int uid;
168                 int uttid;
169                 int reason;
170
171                 dbus_message_get_args(msg, &err,
172                         DBUS_TYPE_INT32, &uid,
173                         DBUS_TYPE_INT32, &uttid,
174                         DBUS_TYPE_INT32, &reason,
175                         DBUS_TYPE_INVALID);
176
177                 if (dbus_error_is_set(&err)) { 
178                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< Get Error signal - Get arguments error (%s)\n", err.message);
179                         dbus_error_free(&err); 
180                 } else {
181                         SLOG(LOG_DEBUG, TAG_TTSC, "<<<< Get Error signal : uid(%d), error(%d), uttid(%d)\n", uid, reason, uttid);
182                         __tts_cb_error(uid, reason, uttid);
183                 }
184
185                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
186                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
187         }/* TTS_SIGNAL_ERROR */
188
189         /* free the message */
190         dbus_message_unref(msg);
191
192         return ECORE_CALLBACK_PASS_ON;
193 }
194
195
196 int tts_dbus_open_connection()
197 {
198         if (NULL != g_conn) {
199                 SLOG(LOG_WARN, TAG_TTSC, "already existed connection ");
200                 return 0;
201         }
202
203         DBusError err;
204         int ret;
205
206         /* initialise the error value */
207         dbus_error_init(&err);
208
209         /* connect to the DBUS system bus, and check for errors */
210         g_conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
211
212         if (dbus_error_is_set(&err)) { 
213                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Dbus Connection Error (%s)\n", err.message); 
214                 dbus_error_free(&err); 
215         }
216
217         if (NULL == g_conn) {
218                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] fail to get dbus connection \n");
219                 return TTS_ERROR_OPERATION_FAILED; 
220         }
221
222         dbus_connection_set_exit_on_disconnect(g_conn, false);
223
224         int pid = getpid();
225
226         char service_name[64];
227         memset(service_name, 0, 64);
228         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
229
230         SLOG(LOG_DEBUG, TAG_TTSC, "Service name is %s\n", service_name);
231
232         /* register our name on the bus, and check for errors */
233         ret = dbus_bus_request_name(g_conn, service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
234
235         if (dbus_error_is_set(&err)) {
236                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Name Error (%s)\n", err.message); 
237                 dbus_error_free(&err); 
238         }
239
240         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
241                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to open connection : Service name has already been existed. \n");
242                 return TTS_ERROR_OPERATION_FAILED;
243         }
244
245         char rule[128];
246         snprintf(rule, 128, "type='signal',interface='%s%d'", TTS_CLIENT_SERVICE_INTERFACE, pid);
247
248         /* add a rule for which messages we want to see */
249         dbus_bus_add_match(g_conn, rule, &err); 
250         dbus_connection_flush(g_conn);
251
252         if (dbus_error_is_set(&err)) { 
253                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Match Error (%s)\n", err.message);
254                 return TTS_ERROR_OPERATION_FAILED; 
255         }
256
257         int fd = 0;
258         dbus_connection_get_unix_fd(g_conn, &fd);
259
260         g_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
261
262         if (NULL == g_fd_handler) { 
263                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get fd handler from ecore \n");
264                 return TTS_ERROR_OPERATION_FAILED;
265         }
266
267         return 0;
268 }
269
270
271 int tts_dbus_close_connection()
272 {
273         DBusError err;
274         dbus_error_init(&err);
275
276         ecore_main_fd_handler_del(g_fd_handler);
277
278         int pid = getpid();
279
280         char service_name[64];
281         memset(service_name, 0, 64);
282         snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
283
284         dbus_bus_release_name (g_conn, service_name, &err);
285
286         dbus_connection_close(g_conn);
287         
288         g_fd_handler = NULL;
289         g_conn = NULL;
290
291         return 0;
292 }
293
294
295 int tts_dbus_reconnect()
296 {
297         bool connected = dbus_connection_get_is_connected(g_conn);
298         SLOG(LOG_DEBUG, "[DBUS] %s\n", connected ? "Connected" : "Not connected");
299
300         if (false == connected) {
301                 tts_dbus_close_connection();
302
303                 if(0 != tts_dbus_open_connection()) {
304                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to reconnect");
305                         return -1;
306                 } 
307
308                 SLOG(LOG_DEBUG, TAG_TTSC, "[DBUS] Reconnect");
309         }
310         
311         return 0;
312 }
313
314 DBusMessage* __tts_dbus_make_message(int uid, const char* method)
315 {
316         if (NULL == method) {
317                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input method is NULL"); 
318                 return NULL;
319         }
320
321         tts_client_s* client = tts_client_get_by_uid(uid);
322
323         /* check handle */
324         if (NULL == client) {
325                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] uid is not available");
326                 return NULL;
327         }
328
329         DBusMessage* msg;
330
331         if (TTS_MODE_DEFAULT == client->mode) {
332                 msg = dbus_message_new_method_call(
333                         TTS_SERVER_SERVICE_NAME, 
334                         TTS_SERVER_SERVICE_OBJECT_PATH, 
335                         TTS_SERVER_SERVICE_INTERFACE, 
336                         method);
337         } else if (TTS_MODE_NOTIFICATION == client->mode) {
338                 msg = dbus_message_new_method_call(
339                         TTS_NOTI_SERVER_SERVICE_NAME, 
340                         TTS_NOTI_SERVER_SERVICE_OBJECT_PATH, 
341                         TTS_NOTI_SERVER_SERVICE_INTERFACE, 
342                         method);
343         } else if (TTS_MODE_SCREEN_READER == client->mode) {
344                 msg = dbus_message_new_method_call(
345                         TTS_SR_SERVER_SERVICE_NAME, 
346                         TTS_SR_SERVER_SERVICE_OBJECT_PATH, 
347                         TTS_SR_SERVER_SERVICE_INTERFACE, 
348                         method);
349         } else {
350                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input mode is not available"); 
351                 return NULL;
352         }
353
354         return msg;
355 }
356
357 int tts_dbus_request_hello(int uid)
358 {
359         DBusMessage* msg;
360
361         msg = __tts_dbus_make_message(uid, TTS_METHOD_HELLO);
362
363         if (NULL == msg) { 
364                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts hello : Fail to make message \n"); 
365                 return TTS_ERROR_OPERATION_FAILED;
366         } 
367
368         DBusError err;
369         dbus_error_init(&err);
370
371         DBusMessage* result_msg = NULL;
372         int result = 0;
373
374         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, WAITING_TIME, &err);
375
376         dbus_message_unref(msg);
377
378         if (NULL != result_msg) {
379                 dbus_message_unref(result_msg);
380
381                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts hello");
382                 result = 0;
383         } else {
384                 result = TTS_ERROR_OPERATION_FAILED;
385         }
386
387         return result;
388 }
389
390 int tts_dbus_request_initialize(int uid)
391 {
392         DBusMessage* msg;
393         DBusError err;
394         dbus_error_init(&err);
395
396         msg = __tts_dbus_make_message(uid, TTS_METHOD_INITIALIZE);
397
398         if (NULL == msg) { 
399                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts initialize : Fail to make message \n");
400                 if (dbus_error_is_set(&err))  
401                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
402         
403                 return TTS_ERROR_OPERATION_FAILED;
404         } else {
405                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts initialize : uid(%d)", uid);
406         }
407
408         int pid = getpid();
409         if (true != dbus_message_append_args(msg, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
410                 dbus_message_unref(msg);
411                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
412
413                 return TTS_ERROR_OPERATION_FAILED;
414         }
415
416         DBusMessage* result_msg;
417         int result = TTS_ERROR_OPERATION_FAILED;
418
419         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, INIT_WAITING_TIME, &err);
420         dbus_message_unref(msg);
421
422         if (dbus_error_is_set(&err))  
423                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
424
425         if (NULL != result_msg) {
426                 dbus_message_get_args(result_msg, &err,
427                                   DBUS_TYPE_INT32, &result,
428                                   DBUS_TYPE_INVALID);
429
430                 if (dbus_error_is_set(&err)) { 
431                         SLOG(LOG_ERROR, TAG_TTSC, "Get arguments error (%s)\n", err.message);
432                         dbus_error_free(&err); 
433                         result = TTS_ERROR_OPERATION_FAILED;
434                 }
435
436                 dbus_message_unref(result_msg);
437         } else {
438                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL ");
439                 if (dbus_error_is_set(&err))  
440                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
441                 tts_dbus_reconnect();
442         }
443
444         if (0 == result) {
445                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts initialize : result = %d \n", result);
446         } else {
447                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts initialize : result = %d \n", result);
448         }
449         
450         return result;
451 }
452
453
454 int tts_dbus_request_finalize(int uid)
455 {
456         DBusMessage* msg;
457         DBusError err;
458         dbus_error_init(&err);
459
460         msg = __tts_dbus_make_message(uid, TTS_METHOD_FINALIZE);
461
462         if (NULL == msg) { 
463                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts finalize : Fail to make message"); 
464                 if (dbus_error_is_set(&err))  
465                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
466
467                 return TTS_ERROR_OPERATION_FAILED;
468         } else {
469                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts finalize : uid(%d)", uid);
470         }
471
472         if (true != dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
473                 dbus_message_unref(msg);
474                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
475
476                 return TTS_ERROR_OPERATION_FAILED;
477         }
478
479         DBusMessage* result_msg;
480         int result = TTS_ERROR_OPERATION_FAILED;
481
482         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, WAITING_TIME, &err);
483         dbus_message_unref(msg);
484
485         if (dbus_error_is_set(&err))  
486                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
487
488         if (NULL != result_msg) {
489                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
490
491                 if (dbus_error_is_set(&err)) { 
492                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< Get arguments error (%s)\n", err.message);
493                         dbus_error_free(&err); 
494                         result = TTS_ERROR_OPERATION_FAILED;
495                 }
496
497                 dbus_message_unref(result_msg);
498         } else {
499                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL ");
500                 if (dbus_error_is_set(&err))  
501                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
502                 tts_dbus_reconnect();
503         }
504
505         if (0 == result) {
506                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts finalize : result = %d \n", result);
507         } else {
508                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts finalize : result = %d \n", result);
509         }
510
511         return result;
512 }
513
514 int tts_dbus_request_get_support_voice(int uid, tts_h tts, tts_supported_voice_cb callback, void* user_data)
515 {
516         DBusMessage* msg;
517         DBusError err;
518         dbus_error_init(&err);
519
520         msg = __tts_dbus_make_message(uid, TTS_METHOD_GET_SUPPORT_VOICES);
521
522         if (NULL == msg) { 
523                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts get supported voices : Fail to make message"); 
524                 if (dbus_error_is_set(&err))  
525                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
526
527                 return TTS_ERROR_OPERATION_FAILED;
528         } else {
529                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts get supported voices : uid(%d)", uid);
530         }
531
532         if (true != dbus_message_append_args( msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
533                 dbus_message_unref(msg);
534                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
535
536                 return TTS_ERROR_OPERATION_FAILED;
537         }
538
539         DBusMessage* result_msg;
540         DBusMessageIter args;
541         int result = TTS_ERROR_OPERATION_FAILED;
542
543         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, WAITING_TIME, &err );
544         dbus_message_unref(msg);
545
546         if (dbus_error_is_set(&err)) {
547                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
548                 printf("result message : %p\n", result_msg);
549         }
550
551         if (NULL != result_msg) {
552                 if (dbus_message_iter_init(result_msg, &args)) {
553                         /* Get result */
554                         if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&args)) {
555                                 dbus_message_iter_get_basic(&args, &result);
556                                 dbus_message_iter_next(&args);
557                         }
558
559                         if (0 == result) {
560                                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts get supported voices : result = %d \n", result);
561
562                                 int size ; 
563                                 char* temp_char;
564                                 int temp_int;
565
566                                 /* Get voice size */
567                                 if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&args)) {
568                                         dbus_message_iter_get_basic(&args, &size);
569                                         dbus_message_iter_next(&args);
570                                 }
571
572                                 if (0 >= size) {
573                                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts size of voice error : size = %d \n", size);
574                                 } else {
575                                         int i=0;
576                                         for (i=0 ; i<size ; i++) {
577                                                 dbus_message_iter_get_basic(&args, &(temp_char) );
578                                                 dbus_message_iter_next(&args);
579                                                 dbus_message_iter_get_basic(&args, &(temp_int) );
580                                                 dbus_message_iter_next(&args);
581                                                 
582                                                 if (true != callback(tts, temp_char, (tts_voice_type_e)temp_int, user_data)) {
583                                                         break;
584                                                 }
585                                         }
586                                 }
587                         } else {
588                                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts get supported voices : result = %d \n", result);
589                         }
590                 } else  {
591                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts get supported voices : result message is invalid \n");
592                         result = TTS_ERROR_OPERATION_FAILED;
593                 }
594                 dbus_message_unref(result_msg);
595         } else {
596                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL");
597                 if (dbus_error_is_set(&err))  
598                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
599                 tts_dbus_reconnect();
600         }
601
602         return result;
603 }
604
605 int tts_dbus_request_get_default_voice(int uid , char** lang, tts_voice_type_e* vctype)
606 {
607         if (NULL == lang || NULL == vctype) {
608                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
609                 return TTS_ERROR_INVALID_PARAMETER;
610         }
611
612         DBusMessage* msg;
613         DBusError err;
614         dbus_error_init(&err);
615
616         msg = __tts_dbus_make_message(uid, TTS_METHOD_GET_CURRENT_VOICE);
617
618         if (NULL == msg) { 
619                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts get default voice : Fail to make message"); 
620                 if (dbus_error_is_set(&err))  
621                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
622
623                 return TTS_ERROR_OPERATION_FAILED;
624         } else {
625                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts get default voice : uid(%d)", uid);
626         }
627
628         if (true != dbus_message_append_args( msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
629                 dbus_message_unref(msg);
630                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
631
632                 return TTS_ERROR_OPERATION_FAILED;
633         }
634
635         DBusMessage* result_msg;
636         int result = TTS_ERROR_OPERATION_FAILED;
637         char* temp_lang;
638         int voice_type;
639
640         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, WAITING_TIME, &err);
641         dbus_message_unref(msg);
642
643         if (dbus_error_is_set(&err)) {
644                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
645                 printf("result message : %p\n", result_msg);
646         }
647
648         if (NULL != result_msg) {
649                 dbus_message_get_args(result_msg, &err,
650                         DBUS_TYPE_INT32, &result,
651                         DBUS_TYPE_STRING, &temp_lang,
652                         DBUS_TYPE_INT32, &voice_type,
653                         DBUS_TYPE_INVALID);
654
655                 if (dbus_error_is_set(&err)) { 
656                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< Get arguments error (%s)\n", err.message);
657                         dbus_error_free(&err); 
658                         result = TTS_ERROR_OPERATION_FAILED;
659                 } 
660                 dbus_message_unref(result_msg);
661         } else {
662                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL ");
663                 if (dbus_error_is_set(&err))  
664                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
665                 tts_dbus_reconnect();
666         }
667
668         if (0 == result) {
669                 *lang = strdup(temp_lang);
670                 *vctype = (tts_voice_type_e)voice_type;
671
672                 if (NULL == *lang) {
673                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts get default voice : Out of memory \n");
674                         result = TTS_ERROR_OUT_OF_MEMORY;
675                 } else {
676                         SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts get default voice : result(%d), lang(%s), vctype(%d) \n", result, *lang, *vctype);
677                 }
678         } else {
679                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts get default voice : result(%d) \n", result);
680         }
681
682         return result;
683 }
684
685
686 int tts_dbus_request_add_text(int uid, const char* text, const char* lang, int vctype, int speed, int uttid)
687 {
688         if (NULL == text || NULL == lang) {
689                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
690                 return TTS_ERROR_INVALID_PARAMETER;
691         }
692
693         DBusMessage* msg;
694         DBusError err;
695         dbus_error_init(&err);
696
697         msg = __tts_dbus_make_message(uid, TTS_METHOD_ADD_QUEUE);
698
699         if (NULL == msg) { 
700                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts add text : Fail to make message"); 
701                 if (dbus_error_is_set(&err))  
702                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
703
704                 return TTS_ERROR_OPERATION_FAILED;
705         } else {
706                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts add text : uid(%d), text(%s), lang(%s), type(%d), speed(%d), id(%d)", 
707                         uid, text, lang, vctype, speed, uttid);
708         }
709
710         if (true != dbus_message_append_args( msg, 
711                 DBUS_TYPE_INT32, &uid,
712                 DBUS_TYPE_STRING, &text,
713                 DBUS_TYPE_STRING, &lang,
714                 DBUS_TYPE_INT32, &vctype,
715                 DBUS_TYPE_INT32, &speed,
716                 DBUS_TYPE_INT32, &uttid,
717                 DBUS_TYPE_INVALID)) {
718                 dbus_message_unref(msg);
719                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
720
721                 return TTS_ERROR_OPERATION_FAILED;
722         }
723
724         DBusMessage* result_msg;
725         int result = TTS_ERROR_OPERATION_FAILED;
726
727         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, 5000, &err);
728         dbus_message_unref(msg);
729
730         if (dbus_error_is_set(&err))  
731                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
732
733         if (NULL != result_msg) {
734                 dbus_message_get_args(result_msg, &err,
735                         DBUS_TYPE_INT32, &result,
736                         DBUS_TYPE_INVALID);
737
738                 if (dbus_error_is_set(&err)) { 
739                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts add text : Get arguments error (%s)\n", err.message);
740                         dbus_error_free(&err); 
741                         result = TTS_ERROR_OPERATION_FAILED;
742                 }
743                 dbus_message_unref(result_msg);
744         } else {
745                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL ");
746                 if (dbus_error_is_set(&err))  
747                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
748                 tts_dbus_reconnect();
749         }
750
751         if (0 == result) {
752                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts add text : result(%d) \n", result);
753         } else {
754                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts add text : result(%d) \n", result);
755         }       
756
757         return result;
758 }
759
760 int tts_dbus_request_play(int uid) 
761 {
762         DBusMessage* msg;
763         DBusError err;
764         dbus_error_init(&err);
765
766         msg = __tts_dbus_make_message(uid, TTS_METHOD_PLAY);
767
768         if (NULL == msg) { 
769                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts play : Fail to make message"); 
770                 if (dbus_error_is_set(&err))  
771                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
772
773                 return TTS_ERROR_OPERATION_FAILED;
774         } else {
775                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts play : uid(%d)", uid);
776         }
777         
778         if (true != dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
779                 dbus_message_unref(msg);
780                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
781
782                 return TTS_ERROR_OPERATION_FAILED;
783         }
784
785         DBusMessage* result_msg;
786         int result = TTS_ERROR_OPERATION_FAILED;
787
788         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, 5000, &err);
789         dbus_message_unref(msg);
790
791         if (dbus_error_is_set(&err))  
792                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
793
794         if (NULL != result_msg) {
795                 dbus_message_get_args(result_msg, &err,
796                         DBUS_TYPE_INT32, &result,
797                         DBUS_TYPE_INVALID);
798
799                 if (dbus_error_is_set(&err)) { 
800                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts play : Get arguments error (%s)\n", err.message);
801                         dbus_error_free(&err); 
802                         result = TTS_ERROR_OPERATION_FAILED;
803                 }
804                 dbus_message_unref(result_msg);
805         } else {
806                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL ");
807                 if (dbus_error_is_set(&err))  
808                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
809                 tts_dbus_reconnect();
810         }
811
812         if (0 == result) {
813                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts play : result(%d) \n", result);
814         } else {
815                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts play : result(%d) \n", result);
816         }
817         
818         return result;
819 }
820
821
822 int tts_dbus_request_stop(int uid)
823 {
824         DBusMessage* msg;
825         DBusError err;
826         dbus_error_init(&err);
827
828         msg = __tts_dbus_make_message(uid, TTS_METHOD_STOP);
829
830         if (NULL == msg) { 
831                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts stop : Fail to make message"); 
832                 if (dbus_error_is_set(&err))  
833                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
834
835                 return TTS_ERROR_OPERATION_FAILED;
836         } else {
837                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts stop : uid(%d)", uid);
838         }
839
840         DBusMessage* result_msg;
841         int result = TTS_ERROR_OPERATION_FAILED;
842
843         if (true != dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
844                 dbus_message_unref(msg);
845                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
846
847                 return TTS_ERROR_OPERATION_FAILED;
848         }
849
850         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, 5000, &err);
851         dbus_message_unref(msg);
852
853         if (dbus_error_is_set(&err))  
854                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
855
856         if (NULL != result_msg) {
857                 dbus_message_get_args(result_msg, &err,
858                         DBUS_TYPE_INT32, &result,
859                         DBUS_TYPE_INVALID);
860
861                 if (dbus_error_is_set(&err)) { 
862                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts stop : Get arguments error (%s)\n", err.message);
863                         dbus_error_free(&err); 
864                         result = TTS_ERROR_OPERATION_FAILED;
865                 }
866                 dbus_message_unref(result_msg);
867         } else {
868                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL ");
869                 if (dbus_error_is_set(&err))  
870                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
871                 tts_dbus_reconnect();
872         }
873
874         if (0 == result) {
875                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts stop : result(%d) \n", result);
876         } else {
877                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts stop : result(%d) \n", result);
878         }
879
880         return result;
881 }
882
883 int tts_dbus_request_pause(int uid)
884 {
885         DBusMessage* msg;
886         DBusError err;
887         dbus_error_init(&err);
888
889         msg = __tts_dbus_make_message(uid, TTS_METHOD_PAUSE);
890
891         if (NULL == msg) { 
892                 SLOG(LOG_ERROR, TAG_TTSC, ">>>> Request tts pause : Fail to make message"); 
893                 if (dbus_error_is_set(&err))  
894                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
895
896                 return TTS_ERROR_OPERATION_FAILED;
897         } else {
898                 SLOG(LOG_DEBUG, TAG_TTSC, ">>>> Request tts pause : uid(%d)", uid);
899         }
900
901         DBusMessage* result_msg;
902         int result = TTS_ERROR_OPERATION_FAILED;
903
904         if (true != dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
905                 dbus_message_unref(msg);
906                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to append args"); 
907
908                 return TTS_ERROR_OPERATION_FAILED;
909         }
910
911         result_msg = dbus_connection_send_with_reply_and_block(g_conn, msg, 5000, &err);
912         dbus_message_unref(msg);
913
914         if (dbus_error_is_set(&err))  
915                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
916
917         if (NULL != result_msg) {
918                 dbus_message_get_args(result_msg, &err,
919                         DBUS_TYPE_INT32, &result,
920                         DBUS_TYPE_INVALID);
921
922                 if (dbus_error_is_set(&err)) { 
923                         SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts pause : Get arguments error (%s)\n", err.message);
924                         dbus_error_free(&err); 
925                         result = TTS_ERROR_OPERATION_FAILED;
926                 }
927                 dbus_message_unref(result_msg);
928         } else {
929                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< Result message is NULL ");
930                 if (dbus_error_is_set(&err))  
931                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] %s", err.message);
932                 tts_dbus_reconnect();
933         }
934
935         if (0 == result) {
936                 SLOG(LOG_DEBUG, TAG_TTSC, "<<<< tts pause : result(%d) \n", result);
937         } else {
938                 SLOG(LOG_ERROR, TAG_TTSC, "<<<< tts pause : result(%d) \n", result);
939         }
940
941         return result;
942 }