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