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