Implement set_engine for setting TTS default engine
[platform/core/uifw/tts.git] / client / tts.c
1 /*
2 *  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #include <app_manager.h>
15 #include <dirent.h>
16 #include <Ecore.h>
17 #include <iconv.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <system_info.h>
22 #include <vconf.h>
23
24 #include "tts.h"
25 #include "tts_client.h"
26 #include "tts_config_mgr.h"
27 #include "tts_dbus.h"
28 #include "tts_main.h"
29
30
31 static bool g_screen_reader;
32
33 static int g_feature_enabled = -1;
34
35 static bool g_err_callback_status = false;
36
37 /* Function definition */
38 static Eina_Bool __tts_notify_state_changed(void *data);
39 static Eina_Bool __tts_notify_error(void *data);
40
41 const char* tts_tag()
42 {
43         return "ttsc";
44 }
45
46 static int __tts_get_feature_enabled()
47 {
48         if (0 == g_feature_enabled) {
49                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] TTS feature NOT supported");
50                 return TTS_ERROR_NOT_SUPPORTED;
51         } else if (-1 == g_feature_enabled) {
52                 bool tts_supported = false;
53                 if (0 == system_info_get_platform_bool(TTS_FEATURE_PATH, &tts_supported)) {
54                         if (false == tts_supported) {
55                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] TTS feature NOT supported");
56                                 g_feature_enabled = 0;
57                                 return TTS_ERROR_NOT_SUPPORTED;
58                         }
59
60                         g_feature_enabled = 1;
61                 } else {
62                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get feature value");
63                         return TTS_ERROR_NOT_SUPPORTED;
64                 }
65         }
66
67         return 0;
68 }
69
70 static const char* __tts_get_error_code(tts_error_e err)
71 {
72         switch (err) {
73         case TTS_ERROR_NONE:                    return "TTS_ERROR_NONE";
74         case TTS_ERROR_OUT_OF_MEMORY:           return "TTS_ERROR_OUT_OF_MEMORY";
75         case TTS_ERROR_IO_ERROR:                return "TTS_ERROR_IO_ERROR";
76         case TTS_ERROR_INVALID_PARAMETER:       return "TTS_ERROR_INVALID_PARAMETER";
77         case TTS_ERROR_OUT_OF_NETWORK:          return "TTS_ERROR_OUT_OF_NETWORK";
78         case TTS_ERROR_TIMED_OUT:               return "TTS_ERROR_TIMED_OUT";
79         case TTS_ERROR_PERMISSION_DENIED:       return "TTS_ERROR_PERMISSION_DENIED";
80         case TTS_ERROR_NOT_SUPPORTED:           return "TTS_ERROR_NOT_SUPPORTED";
81         case TTS_ERROR_INVALID_STATE:           return "TTS_ERROR_INVALID_STATE";
82         case TTS_ERROR_INVALID_VOICE:           return "TTS_ERROR_INVALID_VOICE";
83         case TTS_ERROR_ENGINE_NOT_FOUND:        return "TTS_ERROR_ENGINE_NOT_FOUND";
84         case TTS_ERROR_OPERATION_FAILED:        return "TTS_ERROR_OPERATION_FAILED";
85         case TTS_ERROR_AUDIO_POLICY_BLOCKED:    return "TTS_ERROR_AUDIO_POLICY_BLOCKED";
86         case TTS_ERROR_NOT_SUPPORTED_FEATURE:   return "TTS_ERROR_NOT_SUPPORTED_FEATURE";
87         case TTS_ERROR_SERVICE_RESET:           return "TTS_ERROR_SERVICE_RESET";
88         default:
89                 return "Invalid error code";
90         }
91         return NULL;
92 }
93
94 static int __tts_convert_config_error_code(tts_config_error_e code)
95 {
96         if (code == TTS_CONFIG_ERROR_NONE)                      return TTS_ERROR_NONE;
97         if (code == TTS_CONFIG_ERROR_OUT_OF_MEMORY)             return TTS_ERROR_OUT_OF_MEMORY;
98         if (code == TTS_CONFIG_ERROR_IO_ERROR)                  return TTS_ERROR_IO_ERROR;
99         if (code == TTS_CONFIG_ERROR_INVALID_PARAMETER)         return TTS_ERROR_INVALID_PARAMETER;
100         if (code == TTS_CONFIG_ERROR_INVALID_STATE)             return TTS_ERROR_INVALID_STATE;
101         if (code == TTS_CONFIG_ERROR_INVALID_VOICE)             return TTS_ERROR_INVALID_VOICE;
102         if (code == TTS_CONFIG_ERROR_ENGINE_NOT_FOUND)          return TTS_ERROR_ENGINE_NOT_FOUND;
103         if (code == TTS_CONFIG_ERROR_OPERATION_FAILED)          return TTS_ERROR_OPERATION_FAILED;
104         if (code == TTS_CONFIG_ERROR_NOT_SUPPORTED_FEATURE)     return TTS_ERROR_NOT_SUPPORTED_FEATURE;
105
106         return code;
107 }
108
109 void __tts_config_voice_changed_cb(const char* before_lang, int before_voice_type, const char* language, int voice_type, bool auto_voice, void* user_data)
110 {
111         SLOG(LOG_DEBUG, TAG_TTSC, "Voice changed : Before lang(%s) type(%d) , Current lang(%s), type(%d)",
112                 before_lang, before_voice_type, language, voice_type);
113
114         GList* client_list = NULL;
115         client_list = tts_client_get_client_list();
116
117         GList *iter = NULL;
118         tts_client_s *data = NULL;
119
120         if (g_list_length(client_list) > 0) {
121                 /* Get a first item */
122                 iter = g_list_first(client_list);
123
124                 while (NULL != iter) {
125                         data = iter->data;
126                         if (NULL != data->default_voice_changed_cb) {
127                                 SLOG(LOG_DEBUG, TAG_TTSC, "Call default voice changed callback : uid(%d)", data->uid);
128                                 data->default_voice_changed_cb(data->tts, before_lang, before_voice_type, 
129                                         language, voice_type, data->default_voice_changed_user_data);
130                         }
131
132                         /* Next item */
133                         iter = g_list_next(iter);
134                 }
135         }
136
137         return;
138 }
139
140 void _tts_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, int voice_type, bool auto_voice, bool need_credential, void* user_data)
141 {
142         tts_h tts = (tts_h)user_data;
143
144         tts_client_s* client = tts_client_get(tts);
145         if (NULL == client) {
146                 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
147                 return;
148         }
149
150         if (NULL != engine_id)  SLOG(LOG_DEBUG, TAG_TTSC, "Engine id(%s)", engine_id);
151         if (NULL != setting)    SLOG(LOG_DEBUG, TAG_TTSC, "Engine setting(%s)", setting);
152         if (NULL != language)   SLOG(LOG_DEBUG, TAG_TTSC, "Language(%s)", language);
153         SLOG(LOG_DEBUG, TAG_TTSC, "Voice type(%d), Auto voice(%s), Credential(%s)", voice_type, auto_voice ? "on" : "off", need_credential ? "need" : "no need");
154
155         /* When the default engine is changed, please unload the old engine and load the new one. */
156         int ret = -1;
157
158         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_PAUSED == client->current_state) {
159                 ret = tts_stop(tts);
160                 if (0 != ret) {
161                         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] TTS client stopping...");
162                 }
163                 ret = tts_unprepare(tts);
164                 if (0 != ret) {
165                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare for setting a new engine... (%d)", ret);
166                 }
167                 ret = tts_prepare(tts);
168                 if (0 != ret) {
169                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
170                 }
171         } else if (TTS_STATE_READY == client->current_state) {
172                 ret = tts_unprepare(tts);
173                 if (0 != ret) {
174                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare for setting a new engine... (%d)", ret);
175                 }
176                 ret = tts_prepare(tts);
177                 if (0 != ret) {
178                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
179                 }
180         }
181
182         /* call callback function */
183         if (NULL != client->engine_changed_cb) {
184                 client->engine_changed_cb(tts, engine_id, language, voice_type, need_credential, client->engine_changed_user_data);
185         } else {
186                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function for changed engine");
187         }
188         return;
189 }
190
191 int tts_create(tts_h* tts)
192 {
193         if (0 != __tts_get_feature_enabled()) {
194                 return TTS_ERROR_NOT_SUPPORTED;
195         }
196
197         SLOG(LOG_DEBUG, TAG_TTSC, "===== Create TTS");
198
199         /* check param */
200         if (NULL == tts) {
201                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
202                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
203                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
204                 return TTS_ERROR_INVALID_PARAMETER;
205         }
206
207         if (0 == tts_client_get_size()) {
208                 if (0 != tts_dbus_open_connection()) {
209                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to open dbus connection");
210                         return TTS_ERROR_OPERATION_FAILED;
211                 }
212         }
213
214         if (0 != tts_client_new(tts)) {
215                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create client!!!!!");
216                 return TTS_ERROR_OUT_OF_MEMORY;
217         }
218
219         tts_client_s* client = tts_client_get(*tts);
220         if (NULL == client) {
221                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client");
222                 return TTS_ERROR_OPERATION_FAILED;
223         }
224
225         int ret = tts_config_mgr_initialize(client->uid);
226         if (0 != ret) {
227                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to init config manager : %d", ret);
228                 tts_client_destroy(*tts);
229                 return __tts_convert_config_error_code(ret);
230         }
231
232         ret = tts_config_mgr_set_callback(client->uid, _tts_config_engine_changed_cb, __tts_config_voice_changed_cb, NULL, NULL, NULL);
233         if (0 != ret) {
234                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set config changed : %d", ret);
235                 tts_client_destroy(*tts);
236                 return __tts_convert_config_error_code(ret);
237         }
238
239         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
240         SLOG(LOG_DEBUG, TAG_TTSC, " ");
241
242         return TTS_ERROR_NONE;
243 }
244
245 int tts_destroy(tts_h tts)
246 {
247         if (0 != __tts_get_feature_enabled()) {
248                 return TTS_ERROR_NOT_SUPPORTED;
249         }
250
251         SLOG(LOG_DEBUG, TAG_TTSC, "===== Destroy TTS");
252
253         if (NULL == tts) {
254                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
255                 return TTS_ERROR_INVALID_PARAMETER;
256         }
257
258         tts_client_s* client = tts_client_get(tts);
259
260         /* check handle */
261         if (NULL == client) {
262                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
263                 return TTS_ERROR_INVALID_PARAMETER;
264         }
265
266         /* check used callback */
267         if (0 != tts_client_get_use_callback(client)) {
268                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Cannot destroy in Callback function");
269                 return TTS_ERROR_OPERATION_FAILED;
270         }
271
272         tts_config_mgr_finalize(client->uid);
273
274         int ret = -1;
275         int count = 0;
276
277         /* check state */
278         switch (client->current_state) {
279         case TTS_STATE_PAUSED:
280         case TTS_STATE_PLAYING:
281         case TTS_STATE_READY:
282                 if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
283                         while (0 != ret) {
284                                 ret = tts_dbus_request_finalize(client->uid);
285                                 if (0 != ret) {
286                                         if (TTS_ERROR_TIMED_OUT != ret) {
287                                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
288                                                 break;
289                                         } else {
290                                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize");
291                                                 usleep(10000);
292                                                 count++;
293                                                 if (TTS_RETRY_COUNT == count) {
294                                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
295                                                         break;
296                                                 }
297                                         }
298                                 }
299                         }
300                 } else {
301                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize : g_sr(%d) mode(%d)", g_screen_reader, client->mode);
302                 }
303
304                 client->before_state = client->current_state;
305                 client->current_state = TTS_STATE_CREATED;
306
307         case TTS_STATE_CREATED:
308                 if (NULL != client->conn_timer) {
309                         SLOG(LOG_DEBUG, TAG_TTSC, "Connect Timer is deleted");
310                         ecore_timer_del(client->conn_timer);
311                         client->conn_timer = NULL;
312                 }
313                 /* Free resources */
314                 tts_client_destroy(tts);
315                 break;
316
317         default:
318                 break;
319         }
320
321         if (0 == tts_client_get_size()) {
322                 if (0 != tts_dbus_close_connection()) {
323                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to close connection");
324                 }
325         }
326
327         tts = NULL;
328
329         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
330         SLOG(LOG_DEBUG, TAG_TTSC, " ");
331
332         return TTS_ERROR_NONE;
333 }
334
335 void __tts_screen_reader_changed_cb(bool value)
336 {
337         g_screen_reader = value;
338 }
339
340 int tts_set_mode(tts_h tts, tts_mode_e mode)
341 {
342         if (0 != __tts_get_feature_enabled()) {
343                 return TTS_ERROR_NOT_SUPPORTED;
344         }
345
346         SLOG(LOG_DEBUG, TAG_TTSC, "===== Set TTS mode");
347
348         tts_client_s* client = tts_client_get(tts);
349
350         /* check handle */
351         if (NULL == client) {
352                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
353                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
354                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
355                 return TTS_ERROR_INVALID_PARAMETER;
356         }
357
358         /* check state */
359         if (client->current_state != TTS_STATE_CREATED) {
360                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
361                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
362                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
363                 return TTS_ERROR_INVALID_STATE;
364         }
365
366         if (TTS_MODE_DEFAULT <= mode && mode <= TTS_MODE_SCREEN_READER) {
367                 client->mode = mode;
368         } else {
369                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] mode is not valid : %d", mode);
370                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
371                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
372                 return TTS_ERROR_INVALID_PARAMETER;
373         }
374
375         if (TTS_MODE_SCREEN_READER == mode) {
376                 int ret;
377                 int screen_reader;
378                 ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
379                 if (0 != ret) {
380                         SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get screen reader");
381                         return TTS_ERROR_OPERATION_FAILED;
382                 }
383                 g_screen_reader = (bool)screen_reader;
384                 tts_config_set_screen_reader_callback(client->uid, __tts_screen_reader_changed_cb);
385         } else {
386                 tts_config_unset_screen_reader_callback(client->uid);
387         }
388
389         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
390         SLOG(LOG_DEBUG, TAG_TTSC, " ");
391
392         return TTS_ERROR_NONE;
393 }
394
395 int tts_get_mode(tts_h tts, tts_mode_e* mode)
396 {
397         if (0 != __tts_get_feature_enabled()) {
398                 return TTS_ERROR_NOT_SUPPORTED;
399         }
400
401         SLOG(LOG_DEBUG, TAG_TTSC, "===== Get TTS mode");
402
403         tts_client_s* client = tts_client_get(tts);
404
405         /* check handle */
406         if (NULL == client) {
407                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
408                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
409                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
410                 return TTS_ERROR_INVALID_PARAMETER;
411         }
412
413         /* check state */
414         if (client->current_state != TTS_STATE_CREATED) {
415                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
416                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
417                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
418                 return TTS_ERROR_INVALID_STATE;
419         }
420
421         if (NULL == mode) {
422                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter(mode) is NULL");
423                 return TTS_ERROR_INVALID_PARAMETER;
424         }
425
426         *mode = client->mode;
427
428         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
429         SLOG(LOG_DEBUG, TAG_TTSC, " ");
430
431         return TTS_ERROR_NONE;
432 }
433
434 int tts_set_credential(tts_h tts, const char* credential)
435 {
436         if (0 != __tts_get_feature_enabled()) {
437                 return TTS_ERROR_NOT_SUPPORTED;
438         }
439
440         if (NULL == tts || NULL == credential) {
441                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
442                 return TTS_ERROR_INVALID_PARAMETER;
443         }
444
445         tts_client_s* client = tts_client_get(tts);
446
447         if (NULL == client) {
448                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
449                 return TTS_ERROR_INVALID_PARAMETER;
450         }
451
452         if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
453                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
454                 return TTS_ERROR_INVALID_STATE;
455         }
456
457         if (NULL != client->credential) {
458                 free(client->credential);
459                 client->credential = NULL;
460         }
461         client->credential = strdup(credential);
462
463         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
464         SLOG(LOG_DEBUG, TAG_TTSC, " ");
465
466         return TTS_ERROR_NONE;
467 }
468
469 int tts_set_server_tts(tts_h tts, const char* credential)
470 {
471         if (0 != __tts_get_feature_enabled()) {
472                 return TTS_ERROR_NOT_SUPPORTED;
473         }
474
475         if (NULL == tts) {
476                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
477                 return TTS_ERROR_INVALID_PARAMETER;
478         }
479
480         tts_client_s* client = tts_client_get(tts);
481
482         if (NULL == client) {
483                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
484                 return TTS_ERROR_INVALID_PARAMETER;
485         }
486
487         if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
488                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
489                 return TTS_ERROR_INVALID_STATE;
490         }
491
492         if (NULL != client->credential) {
493                 free(client->credential);
494                 client->credential = NULL;
495         }
496
497         client->internal = true;
498
499         char* key = NULL;
500         if (NULL != credential) {
501                 key = strdup("EnableServerTTS");
502                 client->credential = strdup(credential);
503                 if (NULL == client->credential) {
504                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
505                         return TTS_ERROR_OUT_OF_MEMORY;
506                 }
507         } else {
508                 key = strdup("DisableServerTTS");
509         }
510
511         if (NULL == key) {
512                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
513                 return TTS_ERROR_OUT_OF_MEMORY;
514         }
515
516         int pid = getpid();
517         char* appid = NULL;
518         int ret = app_manager_get_app_id(pid, &appid);
519         if (0 != ret) {
520                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
521                 free(key);
522                 key = NULL;
523                 return TTS_ERROR_OPERATION_FAILED;
524         }
525
526         ret = tts_set_private_data(tts, key, appid);
527         if (0 != ret) {
528                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set private data, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
529                 free(key);
530                 key = NULL;
531                 return ret;
532         }
533
534         free(appid);
535         appid = NULL;
536         free(key);
537         key = NULL;
538
539         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
540         SLOG(LOG_DEBUG, TAG_TTSC, " ");
541
542         return TTS_ERROR_NONE;
543 }
544
545 static Eina_Bool __tts_connect_daemon(void *data)
546 {
547         tts_h tts = (tts_h)data;
548         tts_client_s* client = tts_client_get(tts);
549
550         /* check handle */
551         if (NULL == client) {
552                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
553                 return EINA_FALSE;
554         }
555
556         /* Send hello */
557         if (0 != tts_dbus_request_hello(client->uid)) {
558                 return EINA_TRUE;
559         }
560
561         SLOG(LOG_DEBUG, TAG_TTSC, "===== Connect daemon");
562
563         /* do request initialize */
564         int ret = -1;
565         bool credential_needed = false;
566
567         ret = tts_dbus_request_initialize(client->uid, &credential_needed);
568
569         if (TTS_ERROR_ENGINE_NOT_FOUND == ret) {
570                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
571
572                 client->reason = TTS_ERROR_ENGINE_NOT_FOUND;
573                 client->utt_id = -1;
574
575                 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
576                 client->conn_timer = NULL;
577                 return EINA_FALSE;
578
579         } else if (TTS_ERROR_NONE != ret) {
580                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Fail to connection. Retry to connect : %s", __tts_get_error_code(ret));
581                 return EINA_TRUE;
582
583         } else {
584                 /* success to connect tts-daemon */
585                 client->credential_needed = credential_needed;
586                 SLOG(LOG_ERROR, TAG_TTSC, "Supported options : credential(%s)", credential_needed ? "need" : "no need");
587         }
588
589         client->conn_timer = NULL;
590
591         client = tts_client_get(tts);
592         /* check handle */
593         if (NULL == client) {
594                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
595                 return EINA_FALSE;
596         }
597
598         client->before_state = client->current_state;
599         client->current_state = TTS_STATE_READY;
600
601         if (NULL != client->state_changed_cb) {
602                 tts_client_use_callback(client);
603                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
604                 tts_client_not_use_callback(client);
605         } else {
606                 SLOG(LOG_WARN, TAG_TTSC, "State changed callback is NULL");
607         }
608
609         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
610         SLOG(LOG_DEBUG, TAG_TTSC, " ");
611
612         return EINA_FALSE;
613 }
614
615 int tts_prepare(tts_h tts)
616 {
617         if (0 != __tts_get_feature_enabled()) {
618                 return TTS_ERROR_NOT_SUPPORTED;
619         }
620
621         SLOG(LOG_DEBUG, TAG_TTSC, "===== Prepare TTS");
622
623         tts_client_s* client = tts_client_get(tts);
624
625         /* check handle */
626         if (NULL == client) {
627                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
628                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
629                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
630                 return TTS_ERROR_INVALID_PARAMETER;
631         }
632
633         /* check state */
634         if (client->current_state != TTS_STATE_CREATED) {
635                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
636                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
637                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
638                 return TTS_ERROR_INVALID_STATE;
639         }
640
641         client->conn_timer = ecore_timer_add(0, __tts_connect_daemon, (void*)tts);
642
643         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
644         SLOG(LOG_DEBUG, TAG_TTSC, " ");
645
646         return TTS_ERROR_NONE;
647 }
648
649 int tts_unprepare(tts_h tts)
650 {
651         if (0 != __tts_get_feature_enabled()) {
652                 return TTS_ERROR_NOT_SUPPORTED;
653         }
654
655         SLOG(LOG_DEBUG, TAG_TTSC, "===== Unprepare TTS");
656
657         tts_client_s* client = tts_client_get(tts);
658
659         /* check handle */
660         if (NULL == client) {
661                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
662                 return TTS_ERROR_INVALID_PARAMETER;
663         }
664
665         /* check state */
666         if (client->current_state != TTS_STATE_READY) {
667                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'READY'");
668                 return TTS_ERROR_INVALID_STATE;
669         }
670
671         int ret = -1;
672         int count = 0;
673         if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
674                 while (0 != ret) {
675                         ret = tts_dbus_request_finalize(client->uid);
676                         if (0 != ret) {
677                                 if (TTS_ERROR_TIMED_OUT != ret) {
678                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
679                                         break;
680                                 } else {
681                                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize : %s", __tts_get_error_code(ret));
682                                         usleep(10000);
683                                         count++;
684                                         if (TTS_RETRY_COUNT == count) {
685                                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
686                                                 break;
687                                         }
688                                 }
689                         }
690                 }
691         } else {
692                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize : g_sr(%d) mode(%d)", g_screen_reader, client->mode);
693         }
694
695         client->before_state = client->current_state;
696         client->current_state = TTS_STATE_CREATED;
697
698         if (NULL != client->state_changed_cb) {
699                 tts_client_use_callback(client);
700                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
701                 tts_client_not_use_callback(client);
702                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
703         }
704
705         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
706         SLOG(LOG_DEBUG, TAG_TTSC, " ");
707
708         return TTS_ERROR_NONE;
709 }
710
711 bool __tts_supported_voice_cb(const char* engine_id, const char* language, int type, void* user_data)
712 {
713         tts_h tts = (tts_h)user_data;
714
715         tts_client_s* client = tts_client_get(tts);
716         if (NULL == client) {
717                 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
718                 return false;
719         }
720
721         /* call callback function */
722         if (NULL != client->supported_voice_cb) {
723                 return client->supported_voice_cb(tts, language, type, client->supported_voice_user_data);
724         } else {
725                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of supported voice");
726         }
727
728         return false;
729 }
730
731 int tts_foreach_supported_voices(tts_h tts, tts_supported_voice_cb callback, void* user_data)
732 {
733         if (0 != __tts_get_feature_enabled()) {
734                 return TTS_ERROR_NOT_SUPPORTED;
735         }
736
737         SLOG(LOG_DEBUG, TAG_TTSC, "===== Foreach supported voices");
738
739         if (NULL == tts || NULL == callback) {
740                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
741                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
742                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
743                 return TTS_ERROR_INVALID_PARAMETER;
744         }
745
746         tts_client_s* client = tts_client_get(tts);
747
748         /* check handle */
749         if (NULL == client) {
750                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
751                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
752                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
753                 return TTS_ERROR_INVALID_PARAMETER;
754         }
755
756         int ret = 0;
757         char* current_engine = NULL;
758         ret = tts_config_mgr_get_engine(&current_engine);
759         if (0 != ret) {
760                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get current engine : %d", ret);
761                 return __tts_convert_config_error_code(ret);
762         }
763
764         client->supported_voice_cb = callback;
765         client->supported_voice_user_data = user_data;
766
767         ret = tts_config_mgr_get_voice_list(current_engine, __tts_supported_voice_cb, client->tts);
768
769         if (NULL != current_engine)
770                 free(current_engine);
771
772         client->supported_voice_cb = NULL;
773         client->supported_voice_user_data = NULL;
774
775         if (0 != ret) {
776                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Result : %d", ret);
777                 ret = TTS_ERROR_OPERATION_FAILED;
778         }
779
780         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
781         SLOG(LOG_DEBUG, TAG_TTSC, " ");
782
783         return ret;
784 }
785
786 int tts_get_default_voice(tts_h tts, char** lang, int* vctype)
787 {
788         if (0 != __tts_get_feature_enabled()) {
789                 return TTS_ERROR_NOT_SUPPORTED;
790         }
791
792         SLOG(LOG_DEBUG, TAG_TTSC, "===== Get default voice");
793
794         if (NULL == tts) {
795                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
796                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
797                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
798                 return TTS_ERROR_INVALID_PARAMETER;
799
800         }
801         tts_client_s* client = tts_client_get(tts);
802
803         if (NULL == client) {
804                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
805                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
806                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
807                 return TTS_ERROR_INVALID_PARAMETER;
808         }
809
810         /* Request call remote method */
811         int ret = 0;
812         ret = tts_config_mgr_get_voice(lang, vctype);
813         if (0 != ret) {
814                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %d", ret);
815                 return __tts_convert_config_error_code(ret);
816         } else {
817                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Default language(%s), type(%d)", *lang, *vctype);
818         }
819
820         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
821         SLOG(LOG_DEBUG, TAG_TTSC, " ");
822
823         return ret;
824 }
825
826 int tts_get_max_text_size(tts_h tts, unsigned int* size)
827 {
828         if (0 != __tts_get_feature_enabled()) {
829                 return TTS_ERROR_NOT_SUPPORTED;
830         }
831
832         if (NULL == tts || NULL == size) {
833                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Input parameter is null");
834                 return TTS_ERROR_INVALID_PARAMETER;
835         }
836
837         tts_client_s* client = tts_client_get(tts);
838
839         if (NULL == client) {
840                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : A handle is not valid");
841                 return TTS_ERROR_INVALID_PARAMETER;
842         }
843
844         if (TTS_STATE_READY != client->current_state) {
845                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Current state is NOT 'READY'.");
846                 return TTS_ERROR_INVALID_STATE;
847         }
848
849         *size = TTS_MAX_TEXT_SIZE;
850
851         SLOG(LOG_DEBUG, TAG_TTSC, "Get max text count : %d", *size);
852         return TTS_ERROR_NONE;
853 }
854
855 int tts_get_state(tts_h tts, tts_state_e* state)
856 {
857         if (0 != __tts_get_feature_enabled()) {
858                 return TTS_ERROR_NOT_SUPPORTED;
859         }
860
861         if (NULL == tts || NULL == state) {
862                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : Input parameter is null");
863                 return TTS_ERROR_INVALID_PARAMETER;
864         }
865
866         tts_client_s* client = tts_client_get(tts);
867
868         if (NULL == client) {
869                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
870                 return TTS_ERROR_INVALID_PARAMETER;
871         }
872
873         *state = client->current_state;
874
875         switch (*state) {
876         case TTS_STATE_CREATED: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Created'");        break;
877         case TTS_STATE_READY:   SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Ready'");          break;
878         case TTS_STATE_PLAYING: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Playing'");        break;
879         case TTS_STATE_PAUSED:  SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Paused'");         break;
880         default:                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid value");             break;
881         }
882
883         return TTS_ERROR_NONE;
884 }
885
886 int tts_get_speed_range(tts_h tts, int* min, int* normal, int* max)
887 {
888         if (0 != __tts_get_feature_enabled()) {
889                 return TTS_ERROR_NOT_SUPPORTED;
890         }
891
892         if (NULL == tts || NULL == min || NULL == normal || NULL == max) {
893                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
894                 return TTS_ERROR_INVALID_PARAMETER;
895         }
896
897         tts_client_s* client = tts_client_get(tts);
898
899         if (NULL == client) {
900                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
901                 return TTS_ERROR_INVALID_PARAMETER;
902         }
903
904         *min = TTS_SPEED_MIN;
905         *normal = TTS_SPEED_NORMAL;
906         *max = TTS_SPEED_MAX;
907
908         return TTS_ERROR_NONE;
909 }
910
911 int tts_get_error_message(tts_h tts, char** err_msg)
912 {
913         if (0 != __tts_get_feature_enabled()) {
914                 return TTS_ERROR_NOT_SUPPORTED;
915         }
916
917         if (NULL == tts || NULL == err_msg) {
918                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
919                 return TTS_ERROR_INVALID_PARAMETER;
920         }
921
922         tts_client_s* client = tts_client_get(tts);
923
924         if (NULL == client) {
925                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
926                 return TTS_ERROR_INVALID_PARAMETER;
927         }
928
929         if (NULL != client->err_msg) {
930                 *err_msg = strdup(client->err_msg);
931                 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (%s)", *err_msg);
932         } else {
933                 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (NULL)");
934         }
935
936         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
937         SLOG(LOG_DEBUG, TAG_TTSC, " ");
938
939         return TTS_ERROR_NONE;
940 }
941
942 int tts_add_text(tts_h tts, const char* text, const char* language, int voice_type, int speed, int* utt_id)
943 {
944         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Add text: text(%s), language(%s), type(%d)", (NULL == text) ? "NULL" : text, (NULL == language) ? "NULL" : language, voice_type);
945
946         if (0 != __tts_get_feature_enabled()) {
947                 return TTS_ERROR_NOT_SUPPORTED;
948         }
949
950         if (speed < 0) {
951                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Speed should not be negative(%d)", speed);
952                 return TTS_ERROR_INVALID_PARAMETER;
953         }
954
955         if (voice_type < 0) {
956                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Voice type should not be negative(%d)", voice_type);
957                 return TTS_ERROR_INVALID_PARAMETER;
958         }
959
960         SLOG(LOG_DEBUG, TAG_TTSC, "===== Add text");
961
962         if (NULL == tts || NULL == utt_id) {
963                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
964                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
965                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
966                 return TTS_ERROR_INVALID_PARAMETER;
967         }
968
969         tts_client_s* client = tts_client_get(tts);
970
971         if (NULL == client) {
972                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
973                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
974                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
975                 return TTS_ERROR_INVALID_PARAMETER;
976         }
977
978         if (TTS_STATE_CREATED == client->current_state) {
979                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
980                 return TTS_ERROR_INVALID_STATE;
981         }
982
983         if (TTS_MAX_TEXT_SIZE < strlen(text) || strlen(text) <= 0) {
984                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid.");
985                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
986                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
987                 return TTS_ERROR_INVALID_PARAMETER;
988         }
989
990         if (TTS_SPEED_AUTO > speed || TTS_SPEED_MAX < speed) {
991                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] speed value(%d) is invalid.", speed);
992                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
993                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
994                 return TTS_ERROR_INVALID_PARAMETER;
995         }
996
997         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
998                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
999                 return TTS_ERROR_INVALID_STATE;
1000         }
1001
1002         if (true == client->credential_needed && NULL == client->credential) {
1003                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1004                 return TTS_ERROR_PERMISSION_DENIED;
1005         }
1006
1007         /* check valid utf8 */
1008         iconv_t *ict;
1009         ict = iconv_open("utf-8", "");
1010         if ((iconv_t)-1 == ict) {
1011                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to init for text check");
1012                 return TTS_ERROR_OPERATION_FAILED;
1013         }
1014
1015         size_t len = strlen(text);
1016         char *in_tmp = NULL;
1017         char in_buf[TTS_MAX_TEXT_SIZE];
1018         char *out_tmp = NULL;
1019         char out_buf[TTS_MAX_TEXT_SIZE];
1020         size_t len_tmp = sizeof(out_buf);
1021
1022         memset(in_buf, 0, TTS_MAX_TEXT_SIZE);
1023         snprintf(in_buf, TTS_MAX_TEXT_SIZE, "%s", text);
1024         in_tmp = in_buf;
1025
1026         memset(out_buf, 0, TTS_MAX_TEXT_SIZE);
1027         out_tmp = out_buf;
1028
1029         size_t st;
1030         st = iconv(ict, &in_tmp, &len, &out_tmp, &len_tmp);
1031         if ((size_t)-1 == st) {
1032                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Text is invalid - '%s'", in_buf);
1033                 iconv_close(ict);
1034                 return TTS_ERROR_INVALID_PARAMETER;
1035         }
1036         iconv_close(ict);
1037         SLOG(LOG_DEBUG, TAG_TTSC, "Text is valid - Converted text is '%s'", out_buf);
1038
1039         /* change default language value */
1040         char* temp = NULL;
1041
1042         if (NULL == language)
1043                 temp = strdup("default");
1044         else
1045                 temp = strdup(language);
1046
1047         client->current_utt_id++;
1048         if (client->current_utt_id == 10000) {
1049                 client->current_utt_id = 1;
1050         }
1051
1052         /* do request */
1053         int ret = -1;
1054         int count = 0;
1055         while (0 != ret) {
1056                 ret = tts_dbus_request_add_text(client->uid, out_buf, temp, voice_type, speed, client->current_utt_id, client->credential);
1057                 if (0 != ret) {
1058                         if (TTS_ERROR_TIMED_OUT != ret) {
1059                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1060                                 break;
1061                         } else {
1062                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add text : %s", __tts_get_error_code(ret));
1063                                 usleep(10000);
1064                                 count++;
1065                                 if (TTS_RETRY_MIN_COUNT == count) {
1066                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1067                                         break;
1068                                 }
1069                         }
1070                 } else {
1071                         *utt_id = client->current_utt_id;
1072                 }
1073         }
1074
1075         if (NULL != temp)       free(temp);
1076
1077         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1078         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1079
1080         return ret;
1081 }
1082
1083 static void __tts_play_async(void *data)
1084 {
1085         tts_h tts = (tts_h)data;
1086         tts_client_s* client = tts_client_get(tts);
1087
1088         /* check handle */
1089         if (NULL == client) {
1090                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1091                 return;
1092         }
1093
1094         int ret = -1;
1095         int count = 0;
1096         while (0 != ret) {
1097                 ret = tts_dbus_request_play(client->uid, client->credential);
1098                 if (0 != ret) {
1099                         if (TTS_ERROR_TIMED_OUT != ret) {
1100                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1101                                 break;
1102                         } else {
1103                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1104                                 usleep(10000);
1105                                 count++;
1106                                 if (TTS_RETRY_COUNT == count) {
1107                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1108                                         break;
1109                                 }
1110                         }
1111                 }
1112         }
1113
1114         if (0 != ret) {
1115                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play tts : %s", __tts_get_error_code(ret));
1116
1117                 client->reason = ret;
1118                 client->utt_id = -1;
1119
1120                 ecore_timer_add(0, __tts_notify_error, client->tts);
1121                 return;
1122         }
1123
1124         client->before_state = client->current_state;
1125         client->current_state = TTS_STATE_PLAYING;
1126
1127         if (NULL != client->state_changed_cb) {
1128                 tts_client_use_callback(client);
1129                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1130                 tts_client_not_use_callback(client);
1131                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1132         }
1133
1134         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1135         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1136
1137         return;
1138 }
1139
1140 int tts_play_async(tts_h tts)
1141 {
1142         if (0 != __tts_get_feature_enabled()) {
1143                 return TTS_ERROR_NOT_SUPPORTED;
1144         }
1145
1146         SLOG(LOG_DEBUG, TAG_TTSC, "===== Play tts");
1147
1148         if (NULL == tts) {
1149                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1150                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1151                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1152                 return TTS_ERROR_INVALID_PARAMETER;
1153         }
1154
1155         tts_client_s* client = tts_client_get(tts);
1156
1157         if (NULL == client) {
1158                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1159                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1160                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1161                 return TTS_ERROR_INVALID_PARAMETER;
1162         }
1163
1164         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1165                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1166                 return TTS_ERROR_INVALID_STATE;
1167         }
1168
1169         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1170                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1171                 return TTS_ERROR_INVALID_STATE;
1172         }
1173
1174         if (true == client->credential_needed && NULL == client->credential) {
1175                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1176                 return TTS_ERROR_PERMISSION_DENIED;
1177         }
1178
1179         ecore_main_loop_thread_safe_call_async(__tts_play_async, (void*)tts);
1180
1181         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1182         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1183
1184         return TTS_ERROR_NONE;
1185 }
1186
1187 int tts_play(tts_h tts)
1188 {
1189         if (0 != __tts_get_feature_enabled()) {
1190                 return TTS_ERROR_NOT_SUPPORTED;
1191         }
1192
1193         SLOG(LOG_DEBUG, TAG_TTSC, "===== Play tts");
1194
1195         if (NULL == tts) {
1196                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1197                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1198                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1199                 return TTS_ERROR_INVALID_PARAMETER;
1200         }
1201
1202         tts_client_s* client = tts_client_get(tts);
1203
1204         if (NULL == client) {
1205                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1206                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1207                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1208                 return TTS_ERROR_INVALID_PARAMETER;
1209         }
1210
1211         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1212                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1213                 return TTS_ERROR_INVALID_STATE;
1214         }
1215
1216         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1217                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1218                 return TTS_ERROR_INVALID_STATE;
1219         }
1220
1221         if (true == client->credential_needed && NULL == client->credential) {
1222                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1223                 return TTS_ERROR_PERMISSION_DENIED;
1224         }
1225
1226         int ret = -1;
1227         int count = 0;
1228         while (0 != ret) {
1229                 ret = tts_dbus_request_play(client->uid, client->credential);
1230                 if (0 != ret) {
1231                         if (TTS_ERROR_TIMED_OUT != ret) {
1232                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1233                                 return ret;
1234                         } else {
1235                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1236                                 usleep(10000);
1237                                 count++;
1238                                 if (TTS_RETRY_COUNT == count) {
1239                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1240                                         return ret;
1241                                 }
1242                         }
1243                 }
1244         }
1245
1246         client->before_state = client->current_state;
1247         client->current_state = TTS_STATE_PLAYING;
1248
1249         if (NULL != client->state_changed_cb) {
1250                 tts_client_use_callback(client);
1251                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1252                 tts_client_not_use_callback(client);
1253                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1254         }
1255
1256         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1257         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1258
1259         return TTS_ERROR_NONE;
1260 }
1261
1262 static void __tts_stop_async(void *data)
1263 {
1264         tts_h tts = (tts_h)data;
1265         tts_client_s* client = tts_client_get(tts);
1266
1267         /* check handle */
1268         if (NULL == client) {
1269                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1270                 return;
1271         }
1272
1273         int ret = -1;
1274         int count = 0;
1275         while (0 != ret) {
1276                 ret = tts_dbus_request_stop(client->uid);
1277                 if (0 != ret) {
1278                         if (TTS_ERROR_TIMED_OUT != ret) {
1279                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1280                                 break;
1281                         } else {
1282                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1283                                 usleep(10000);
1284                                 count++;
1285                                 if (TTS_RETRY_COUNT == count) {
1286                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1287                                         break;
1288                                 }
1289                         }
1290                 }
1291         }
1292
1293         if (0 != ret) {
1294                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to stop tts : %s", __tts_get_error_code(ret));
1295
1296                 client->reason = ret;
1297                 client->utt_id = -1;
1298
1299                 ecore_timer_add(0, __tts_notify_error, client->tts);
1300                 return;
1301         }
1302
1303         client->before_state = client->current_state;
1304         client->current_state = TTS_STATE_READY;
1305
1306         if (NULL != client->state_changed_cb) {
1307                 tts_client_use_callback(client);
1308                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1309                 tts_client_not_use_callback(client);
1310                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1311         }
1312
1313         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1314         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1315
1316         return;
1317 }
1318
1319 int tts_stop_aync(tts_h tts)
1320 {
1321         if (0 != __tts_get_feature_enabled()) {
1322                 return TTS_ERROR_NOT_SUPPORTED;
1323         }
1324
1325         SLOG(LOG_DEBUG, TAG_TTSC, "===== Stop tts");
1326
1327         if (NULL == tts) {
1328                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1329                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1330                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1331                 return TTS_ERROR_INVALID_PARAMETER;
1332         }
1333
1334         tts_client_s* client = tts_client_get(tts);
1335
1336         if (NULL == client) {
1337                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1338                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1339                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1340                 return TTS_ERROR_INVALID_PARAMETER;
1341         }
1342
1343         if (TTS_STATE_CREATED == client->current_state) {
1344                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1345                 return TTS_ERROR_INVALID_STATE;
1346         }
1347
1348         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1349                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1350                 return TTS_ERROR_INVALID_STATE;
1351         }
1352
1353         ecore_main_loop_thread_safe_call_async(__tts_stop_async, (void*)tts);
1354
1355         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1356         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1357
1358         return TTS_ERROR_NONE;
1359 }
1360
1361 int tts_stop(tts_h tts)
1362 {
1363         if (0 != __tts_get_feature_enabled()) {
1364                 return TTS_ERROR_NOT_SUPPORTED;
1365         }
1366
1367         SLOG(LOG_DEBUG, TAG_TTSC, "===== Stop tts");
1368
1369         if (NULL == tts) {
1370                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1371                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1372                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1373                 return TTS_ERROR_INVALID_PARAMETER;
1374         }
1375
1376         tts_client_s* client = tts_client_get(tts);
1377
1378         if (NULL == client) {
1379                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1380                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1381                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1382                 return TTS_ERROR_INVALID_PARAMETER;
1383         }
1384
1385         if (TTS_STATE_CREATED == client->current_state) {
1386                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1387                 return TTS_ERROR_INVALID_STATE;
1388         }
1389
1390         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1391                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1392                 return TTS_ERROR_INVALID_STATE;
1393         }
1394
1395         int ret = -1;
1396         int count = 0;
1397         while (0 != ret) {
1398                 ret = tts_dbus_request_stop(client->uid);
1399                 if (0 != ret) {
1400                         if (TTS_ERROR_TIMED_OUT != ret) {
1401                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1402                                 return ret;
1403                         } else {
1404                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1405                                 usleep(10000);
1406                                 count++;
1407                                 if (TTS_RETRY_COUNT == count) {
1408                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1409                                         return ret;
1410                                 }
1411                         }
1412                 }
1413         }
1414
1415         client->before_state = client->current_state;
1416         client->current_state = TTS_STATE_READY;
1417
1418         if (NULL != client->state_changed_cb) {
1419                 tts_client_use_callback(client);
1420                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1421                 tts_client_not_use_callback(client);
1422                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1423         }
1424
1425         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1426         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1427
1428         return TTS_ERROR_NONE;
1429 }
1430
1431 static void __tts_pause_async(void *data)
1432 {
1433         tts_h tts = (tts_h)data;
1434         tts_client_s* client = tts_client_get(tts);
1435
1436         /* check handle */
1437         if (NULL == client) {
1438                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1439                 return;
1440         }
1441
1442         int ret = -1;
1443         int count = 0;
1444         while (0 != ret) {
1445                 ret = tts_dbus_request_pause(client->uid);
1446                 if (0 != ret) {
1447                         if (TTS_ERROR_TIMED_OUT != ret) {
1448                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1449                                 break;
1450                         } else {
1451                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
1452                                 usleep(10000);
1453                                 count++;
1454                                 if (TTS_RETRY_COUNT == count) {
1455                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1456                                         break;
1457                                 }
1458                         }
1459                 }
1460         }
1461
1462         if (0 != ret) {
1463                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to pause tts : %s", __tts_get_error_code(ret));
1464
1465                 client->reason = ret;
1466                 client->utt_id = -1;
1467
1468                 ecore_timer_add(0, __tts_notify_error, client->tts);
1469                 return;
1470         }
1471
1472         client->before_state = client->current_state;
1473         client->current_state = TTS_STATE_PAUSED;
1474
1475         if (NULL != client->state_changed_cb) {
1476                 tts_client_use_callback(client);
1477                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1478                 tts_client_not_use_callback(client);
1479                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1480         }
1481
1482         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1483         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1484
1485         return;
1486 }
1487
1488 int tts_pause_async(tts_h tts)
1489 {
1490         if (0 != __tts_get_feature_enabled()) {
1491                 return TTS_ERROR_NOT_SUPPORTED;
1492         }
1493
1494         SLOG(LOG_DEBUG, TAG_TTSC, "===== Pause tts");
1495
1496         if (NULL == tts) {
1497                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1498                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1499                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1500                 return TTS_ERROR_INVALID_PARAMETER;
1501         }
1502
1503         tts_client_s* client = tts_client_get(tts);
1504
1505         if (NULL == client) {
1506                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1507                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1508                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1509                 return TTS_ERROR_INVALID_PARAMETER;
1510         }
1511
1512         if (TTS_STATE_PLAYING != client->current_state) {
1513                 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1514                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1515                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1516                 return TTS_ERROR_INVALID_STATE;
1517         }
1518
1519         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1520                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1521                 return TTS_ERROR_INVALID_STATE;
1522         }
1523
1524         ecore_main_loop_thread_safe_call_async(__tts_pause_async, (void*)tts);
1525
1526         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1527         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1528
1529         return TTS_ERROR_NONE;
1530 }
1531
1532 int tts_pause(tts_h tts)
1533 {
1534         if (0 != __tts_get_feature_enabled()) {
1535                 return TTS_ERROR_NOT_SUPPORTED;
1536         }
1537
1538         SLOG(LOG_DEBUG, TAG_TTSC, "===== Pause tts");
1539
1540         if (NULL == tts) {
1541                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1542                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1543                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1544                 return TTS_ERROR_INVALID_PARAMETER;
1545         }
1546
1547         tts_client_s* client = tts_client_get(tts);
1548
1549         if (NULL == client) {
1550                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1551                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1552                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1553                 return TTS_ERROR_INVALID_PARAMETER;
1554         }
1555
1556         if (TTS_STATE_PLAYING != client->current_state) {
1557                 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1558                 SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1559                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1560                 return TTS_ERROR_INVALID_STATE;
1561         }
1562
1563         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1564                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1565                 return TTS_ERROR_INVALID_STATE;
1566         }
1567
1568         int ret = -1;
1569         int count = 0;
1570         while (0 != ret) {
1571                 ret = tts_dbus_request_pause(client->uid);
1572                 if (0 != ret) {
1573                         if (TTS_ERROR_TIMED_OUT != ret) {
1574                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1575                                 return ret;
1576                         } else {
1577                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
1578                                 usleep(10000);
1579                                 count++;
1580                                 if (TTS_RETRY_COUNT == count) {
1581                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1582                                         return ret;
1583                                 }
1584                         }
1585                 }
1586         }
1587
1588         client->before_state = client->current_state;
1589         client->current_state = TTS_STATE_PAUSED;
1590
1591         if (NULL != client->state_changed_cb) {
1592                 tts_client_use_callback(client);
1593                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1594                 tts_client_not_use_callback(client);
1595                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1596         }
1597
1598         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1599         SLOG(LOG_DEBUG, TAG_TTSC, " ");
1600
1601         return TTS_ERROR_NONE;
1602 }
1603
1604 int tts_set_private_data(tts_h tts, const char* key, const char* data)
1605 {
1606         if (0 != __tts_get_feature_enabled()) {
1607                 return TTS_ERROR_NOT_SUPPORTED;
1608         }
1609
1610         SLOG(LOG_DEBUG, TAG_TTSC, "===== Set private data");
1611
1612         if (NULL == tts) {
1613                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1614                 return TTS_ERROR_INVALID_PARAMETER;
1615         }
1616
1617         if (NULL == key || NULL == data) {
1618                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
1619                 return TTS_ERROR_INVALID_PARAMETER;
1620         }
1621
1622         tts_client_s* client = tts_client_get(tts);
1623
1624         if (NULL == client) {
1625                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1626                 return TTS_ERROR_INVALID_PARAMETER;
1627         }
1628
1629         if (TTS_STATE_READY != client->current_state) {
1630                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
1631                 return TTS_ERROR_INVALID_STATE;
1632         }
1633
1634         if (true != client->internal && (0 == strcmp(key, "EnableServerTTS") || 0 == strcmp(key, "DisableServerTTS"))) {
1635                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] This is not an internal app");
1636                 return TTS_ERROR_INVALID_PARAMETER;
1637         }
1638
1639         int ret = -1;
1640         int count = 0;
1641         while (0 != ret) {
1642                 ret = tts_dbus_request_set_private_data(client->uid, key, data);
1643                 if (0 != ret) {
1644                         if (TTS_ERROR_TIMED_OUT != ret) {
1645                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1646                                 return ret;
1647                         } else {
1648                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
1649                                 usleep(10000);
1650                                 count++;
1651                                 if (TTS_RETRY_COUNT == count) {
1652                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1653                                         return ret;
1654                                 }
1655                         }
1656                 }
1657         }
1658
1659         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1660         SLOG(LOG_DEBUG, TAG_TTSC, "");
1661
1662         return 0;
1663 }
1664
1665 int tts_get_private_data(tts_h tts, const char* key, char** data)
1666 {
1667         if (0 != __tts_get_feature_enabled()) {
1668                 return TTS_ERROR_NOT_SUPPORTED;
1669         }
1670
1671         SLOG(LOG_DEBUG, TAG_TTSC, "===== Get private data");
1672
1673         if (NULL == tts) {
1674                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1675                 return TTS_ERROR_INVALID_PARAMETER;
1676         }
1677
1678         if (NULL == key || NULL == data) {
1679                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
1680                 return TTS_ERROR_INVALID_PARAMETER;
1681         }
1682
1683         tts_client_s* client = tts_client_get(tts);
1684
1685         if (NULL == client) {
1686                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1687                 return TTS_ERROR_INVALID_PARAMETER;
1688         }
1689
1690         if (TTS_STATE_READY != client->current_state) {
1691                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
1692                 return TTS_ERROR_INVALID_STATE;
1693         }
1694
1695         int ret = -1;
1696         int count = 0;
1697         while (0 != ret) {
1698                 ret = tts_dbus_request_get_private_data(client->uid, key, data);
1699                 if (0 != ret) {
1700                         if (TTS_ERROR_TIMED_OUT != ret) {
1701                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1702                                 return ret;
1703                         } else {
1704                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
1705                                 usleep(10000);
1706                                 count++;
1707                                 if (TTS_RETRY_COUNT == count) {
1708                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1709                                         return ret;
1710                                 }
1711                         }
1712                 }
1713         }
1714
1715         if (0 == strncmp(*data, "NULL", strlen(*data))) {
1716                 free(*data);
1717                 *data = NULL;
1718         }
1719
1720         SLOG(LOG_DEBUG, TAG_TTSC, "=====");
1721         SLOG(LOG_DEBUG, TAG_TTSC, "");
1722
1723         return 0;
1724 }
1725
1726 static Eina_Bool __tts_notify_error(void *data)
1727 {
1728         tts_h tts = (tts_h)data;
1729
1730         tts_client_s* client = tts_client_get(tts);
1731
1732         /* check handle */
1733         if (NULL == client) {
1734                 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify error msg : A handle is not valid");
1735                 return EINA_FALSE;
1736         }
1737
1738         SLOG(LOG_DEBUG, TAG_TTSC, "Error data : uttid(%d) reason(%s)", client->utt_id, __tts_get_error_code(client->reason));
1739
1740         if (NULL != client->error_cb) {
1741                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of error");
1742                 tts_client_use_callback(client);
1743                 g_err_callback_status = true;
1744                 client->error_cb(client->tts, client->utt_id, client->reason, client->error_user_data);
1745                 g_err_callback_status = false;
1746                 tts_client_not_use_callback(client);
1747         } else {
1748                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
1749         }
1750
1751         return EINA_FALSE;
1752 }
1753
1754 int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
1755 {
1756         tts_client_s* client = tts_client_get_by_uid(uid);
1757
1758         if (NULL == client) {
1759                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
1760                 return TTS_ERROR_INVALID_PARAMETER;
1761         }
1762
1763         client->utt_id = utt_id;
1764         client->reason = reason;
1765         if (NULL != client->err_msg) {
1766                 free(client->err_msg);
1767                 client->err_msg = NULL;
1768         }
1769         client->err_msg = strdup(err_msg);
1770
1771         /* call callback function */
1772         if (NULL != client->error_cb) {
1773                 ecore_timer_add(0, __tts_notify_error, client->tts);
1774         } else {
1775                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
1776         }
1777
1778         if (TTS_ERROR_SERVICE_RESET == reason) {
1779                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
1780
1781                 client->current_state = TTS_STATE_CREATED;
1782                 if (0 != tts_prepare(client->tts)) {
1783                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare");
1784                 }
1785         }
1786
1787         return 0;
1788 }
1789
1790 static Eina_Bool __tts_notify_state_changed(void *data)
1791 {
1792         tts_h tts = (tts_h)data;
1793
1794         tts_client_s* client = tts_client_get(tts);
1795
1796         /* check handle */
1797         if (NULL == client) {
1798                 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify state changed : A handle is not valid");
1799                 return EINA_FALSE;
1800         }
1801
1802         if (NULL != client->state_changed_cb) {
1803                 tts_client_use_callback(client);
1804                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1805                 tts_client_not_use_callback(client);
1806                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called : pre(%d) cur(%d)", client->before_state, client->current_state);
1807         } else {
1808                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
1809         }
1810
1811         return EINA_FALSE;
1812 }
1813
1814 int __tts_cb_set_state(int uid, int state)
1815 {
1816         tts_client_s* client = tts_client_get_by_uid(uid);
1817         if (NULL == client) {
1818                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] The handle is not valid");
1819                 return -1;
1820         }
1821
1822         tts_state_e state_from_daemon = (tts_state_e)state;
1823
1824         if (client->current_state == state_from_daemon) {
1825                 SLOG(LOG_DEBUG, TAG_TTSC, "Current state has already been %d", client->current_state);
1826                 return 0;
1827         }
1828
1829         if (NULL != client->state_changed_cb) {
1830                 ecore_timer_add(0, __tts_notify_state_changed, client->tts);
1831         } else {
1832                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
1833         }
1834
1835         client->before_state = client->current_state;
1836         client->current_state = state_from_daemon;
1837
1838         return 0;
1839 }
1840
1841 int __tts_cb_utt_started(int uid, int utt_id)
1842 {
1843         tts_client_s* client = tts_client_get_by_uid(uid);
1844
1845         if (NULL == client) {
1846                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
1847                 return TTS_ERROR_INVALID_PARAMETER;
1848         }
1849
1850         SLOG(LOG_DEBUG, TAG_TTSC, "utterance started : utt id(%d) ", utt_id);
1851
1852         client->utt_id = utt_id;
1853
1854         /* call callback function */
1855         if (NULL != client->utt_started_cb) {
1856                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance started ");
1857                 tts_client_use_callback(client);
1858                 client->utt_started_cb(client->tts, client->utt_id, client->utt_started_user_data);
1859                 tts_client_not_use_callback(client);
1860         } else {
1861                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance started ");
1862         }
1863
1864         return 0;
1865 }
1866
1867 int __tts_cb_utt_completed(int uid, int utt_id)
1868 {
1869         tts_client_s* client = tts_client_get_by_uid(uid);
1870
1871         if (NULL == client) {
1872                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
1873                 return TTS_ERROR_INVALID_PARAMETER;
1874         }
1875
1876         SLOG(LOG_DEBUG, TAG_TTSC, "utterance completed : uttid(%d) ", utt_id);
1877
1878         client->utt_id = utt_id;
1879
1880         /* call callback function */
1881         if (NULL != client->utt_completeted_cb) {
1882                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance completed ");
1883                 tts_client_use_callback(client);
1884                 client->utt_completeted_cb(client->tts, client->utt_id, client->utt_completed_user_data);
1885                 tts_client_not_use_callback(client);
1886         } else {
1887                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance completed ");
1888         }
1889
1890         return 0;
1891 }
1892
1893 int tts_set_state_changed_cb(tts_h tts, tts_state_changed_cb callback, void* user_data)
1894 {
1895         if (0 != __tts_get_feature_enabled()) {
1896                 return TTS_ERROR_NOT_SUPPORTED;
1897         }
1898
1899         if (NULL == tts || NULL == callback) {
1900                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Input parameter is null");
1901                 return TTS_ERROR_INVALID_PARAMETER;
1902         }
1903
1904         tts_client_s* client = tts_client_get(tts);
1905
1906         if (NULL == client) {
1907                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : A handle is not valid");
1908                 return TTS_ERROR_INVALID_PARAMETER;
1909         }
1910
1911         if (TTS_STATE_CREATED != client->current_state) {
1912                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Current state is not 'Created'.");
1913                 return TTS_ERROR_INVALID_STATE;
1914         }
1915
1916         client->state_changed_cb = callback;
1917         client->state_changed_user_data = user_data;
1918
1919         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set state changed cb");
1920
1921         return 0;
1922 }
1923
1924 int tts_unset_state_changed_cb(tts_h tts)
1925 {
1926         if (0 != __tts_get_feature_enabled()) {
1927                 return TTS_ERROR_NOT_SUPPORTED;
1928         }
1929
1930         if (NULL == tts) {
1931                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Input parameter is null");
1932                 return TTS_ERROR_INVALID_PARAMETER;
1933         }
1934
1935         tts_client_s* client = tts_client_get(tts);
1936
1937         if (NULL == client) {
1938                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : A handle is not valid");
1939                 return TTS_ERROR_INVALID_PARAMETER;
1940         }
1941
1942         if (TTS_STATE_CREATED != client->current_state) {
1943                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Current state is not 'Created'.");
1944                 return TTS_ERROR_INVALID_STATE;
1945         }
1946
1947         client->state_changed_cb = NULL;
1948         client->state_changed_user_data = NULL;
1949
1950         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset state changed cb");
1951
1952         return 0;
1953 }
1954
1955 int tts_set_utterance_started_cb(tts_h tts, tts_utterance_started_cb callback, void* user_data)
1956 {
1957         if (0 != __tts_get_feature_enabled()) {
1958                 return TTS_ERROR_NOT_SUPPORTED;
1959         }
1960
1961         if (NULL == tts || NULL == callback) {
1962                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Input parameter is null");
1963                 return TTS_ERROR_INVALID_PARAMETER;
1964         }
1965
1966         tts_client_s* client = tts_client_get(tts);
1967
1968         if (NULL == client) {
1969                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : A handle is not valid");
1970                 return TTS_ERROR_INVALID_PARAMETER;
1971         }
1972
1973         if (TTS_STATE_CREATED != client->current_state) {
1974                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Current state is not 'Created'.");
1975                 return TTS_ERROR_INVALID_STATE;
1976         }
1977
1978         client->utt_started_cb = callback;
1979         client->utt_started_user_data = user_data;
1980
1981         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt started cb");
1982
1983         return 0;
1984 }
1985
1986 int tts_unset_utterance_started_cb(tts_h tts)
1987 {
1988         if (0 != __tts_get_feature_enabled()) {
1989                 return TTS_ERROR_NOT_SUPPORTED;
1990         }
1991
1992         if (NULL == tts) {
1993                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Input parameter is null");
1994                 return TTS_ERROR_INVALID_PARAMETER;
1995         }
1996
1997         tts_client_s* client = tts_client_get(tts);
1998
1999         if (NULL == client) {
2000                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : A handle is not valid");
2001                 return TTS_ERROR_INVALID_PARAMETER;
2002         }
2003
2004         if (TTS_STATE_CREATED != client->current_state) {
2005                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Current state is not 'Created'.");
2006                 return TTS_ERROR_INVALID_STATE;
2007         }
2008
2009         client->utt_started_cb = NULL;
2010         client->utt_started_user_data = NULL;
2011
2012         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt started cb");
2013
2014         return 0;
2015 }
2016
2017 int tts_set_utterance_completed_cb(tts_h tts, tts_utterance_completed_cb callback, void* user_data)
2018 {
2019         if (0 != __tts_get_feature_enabled()) {
2020                 return TTS_ERROR_NOT_SUPPORTED;
2021         }
2022
2023         if (NULL == tts || NULL == callback) {
2024                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Input parameter is null");
2025                 return TTS_ERROR_INVALID_PARAMETER;
2026         }
2027
2028         tts_client_s* client = tts_client_get(tts);
2029
2030         if (NULL == client) {
2031                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : A handle is not valid");
2032                 return TTS_ERROR_INVALID_PARAMETER;
2033         }
2034
2035         if (TTS_STATE_CREATED != client->current_state) {
2036                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Current state is not 'Created'.");
2037                 return TTS_ERROR_INVALID_STATE;
2038         }
2039
2040         client->utt_completeted_cb = callback;
2041         client->utt_completed_user_data = user_data;
2042
2043         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt completed cb");
2044
2045         return 0;
2046 }
2047
2048 int tts_unset_utterance_completed_cb(tts_h tts)
2049 {
2050         if (0 != __tts_get_feature_enabled()) {
2051                 return TTS_ERROR_NOT_SUPPORTED;
2052         }
2053
2054         if (NULL == tts) {
2055                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Input parameter is null");
2056                 return TTS_ERROR_INVALID_PARAMETER;
2057         }
2058
2059         tts_client_s* client = tts_client_get(tts);
2060
2061         if (NULL == client) {
2062                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : A handle is not valid");
2063                 return TTS_ERROR_INVALID_PARAMETER;
2064         }
2065
2066         if (TTS_STATE_CREATED != client->current_state) {
2067                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Current state is not 'Created'.");
2068                 return TTS_ERROR_INVALID_STATE;
2069         }
2070
2071         client->utt_completeted_cb = NULL;
2072         client->utt_completed_user_data = NULL;
2073
2074         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt completed cb");
2075         return 0;
2076 }
2077
2078 int tts_set_error_cb(tts_h tts, tts_error_cb callback, void* user_data)
2079 {
2080         if (0 != __tts_get_feature_enabled()) {
2081                 return TTS_ERROR_NOT_SUPPORTED;
2082         }
2083
2084         if (NULL == tts || NULL == callback) {
2085                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Input parameter is null");
2086                 return TTS_ERROR_INVALID_PARAMETER;
2087         }
2088
2089         tts_client_s* client = tts_client_get(tts);
2090
2091         if (NULL == client) {
2092                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : A handle is not valid");
2093                 return TTS_ERROR_INVALID_PARAMETER;
2094         }
2095
2096         if (TTS_STATE_CREATED != client->current_state) {
2097                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Current state is not 'Created'.");
2098                 return TTS_ERROR_INVALID_STATE;
2099         }
2100
2101         client->error_cb = callback;
2102         client->error_user_data = user_data;
2103
2104         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set error cb");
2105
2106         return 0;
2107 }
2108
2109 int tts_unset_error_cb(tts_h tts)
2110 {
2111         if (0 != __tts_get_feature_enabled()) {
2112                 return TTS_ERROR_NOT_SUPPORTED;
2113         }
2114
2115         if (NULL == tts) {
2116                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Input parameter is null");
2117                 return TTS_ERROR_INVALID_PARAMETER;
2118         }
2119
2120         tts_client_s* client = tts_client_get(tts);
2121
2122         if (NULL == client) {
2123                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : A handle is not valid");
2124                 return TTS_ERROR_INVALID_PARAMETER;
2125         }
2126
2127         if (TTS_STATE_CREATED != client->current_state) {
2128                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Current state is not 'Created'.");
2129                 return TTS_ERROR_INVALID_STATE;
2130         }
2131
2132         client->error_cb = NULL;
2133         client->error_user_data = NULL;
2134
2135         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset error cb");
2136
2137         return 0;
2138 }
2139
2140 int tts_set_default_voice_changed_cb(tts_h tts, tts_default_voice_changed_cb callback, void* user_data)
2141 {
2142         if (0 != __tts_get_feature_enabled()) {
2143                 return TTS_ERROR_NOT_SUPPORTED;
2144         }
2145
2146         if (NULL == tts || NULL == callback) {
2147                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Input parameter is null");
2148                 return TTS_ERROR_INVALID_PARAMETER;
2149         }
2150
2151         tts_client_s* client = tts_client_get(tts);
2152
2153         if (NULL == client) {
2154                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : A handle is not valid");
2155                 return TTS_ERROR_INVALID_PARAMETER;
2156         }
2157
2158         if (TTS_STATE_CREATED != client->current_state) {
2159                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Current state is not 'Created'.");
2160                 return TTS_ERROR_INVALID_STATE;
2161         }
2162
2163         client->default_voice_changed_cb = callback;
2164         client->default_voice_changed_user_data = user_data;
2165
2166         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set default voice changed cb");
2167
2168         return 0;
2169 }
2170
2171 int tts_unset_default_voice_changed_cb(tts_h tts)
2172 {
2173         if (0 != __tts_get_feature_enabled()) {
2174                 return TTS_ERROR_NOT_SUPPORTED;
2175         }
2176
2177         if (NULL == tts) {
2178                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Input parameter is null");
2179                 return TTS_ERROR_INVALID_PARAMETER;
2180         }
2181
2182         tts_client_s* client = tts_client_get(tts);
2183
2184         if (NULL == client) {
2185                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : A handle is not valid");
2186                 return TTS_ERROR_INVALID_PARAMETER;
2187         }
2188
2189         if (TTS_STATE_CREATED != client->current_state) {
2190                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Current state is not 'Created'.");
2191                 return TTS_ERROR_INVALID_STATE;
2192         }
2193
2194         client->default_voice_changed_cb = NULL;
2195         client->default_voice_changed_user_data = NULL;
2196
2197         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset default voice changed cb");
2198
2199         return 0;
2200 }
2201
2202 int tts_set_engine_changed_cb(tts_h tts, tts_engine_changed_cb callback, void* user_data)
2203 {
2204         if (0 != __tts_get_feature_enabled()) {
2205                 return TTS_ERROR_NOT_SUPPORTED;
2206         }
2207
2208         if (NULL == tts || NULL == callback) {
2209                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Input parameter is null");
2210                 return TTS_ERROR_INVALID_PARAMETER;
2211         }
2212
2213         tts_client_s* client = tts_client_get(tts);
2214
2215         if (NULL == client) {
2216                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : A handle is not valid");
2217                 return TTS_ERROR_INVALID_PARAMETER;
2218         }
2219
2220         if (TTS_STATE_CREATED != client->current_state) {
2221                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Current state is not 'Created'.");
2222                 return TTS_ERROR_INVALID_STATE;
2223         }
2224
2225         client->engine_changed_cb = callback;
2226         client->engine_changed_user_data = user_data;
2227
2228         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set engine changed cb");
2229
2230         return 0;
2231 }
2232
2233 int tts_unset_engine_changed_cb(tts_h tts)
2234 {
2235         if (0 != __tts_get_feature_enabled()) {
2236                 return TTS_ERROR_NOT_SUPPORTED;
2237         }
2238
2239         if (NULL == tts) {
2240                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Input parameter is null");
2241                 return TTS_ERROR_INVALID_PARAMETER;
2242         }
2243
2244         tts_client_s* client = tts_client_get(tts);
2245
2246         if (NULL == client) {
2247                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : A handle is not valid");
2248                 return TTS_ERROR_INVALID_PARAMETER;
2249         }
2250
2251         if (TTS_STATE_CREATED != client->current_state) {
2252                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Current state is not 'Created'.");
2253                 return TTS_ERROR_INVALID_STATE;
2254         }
2255
2256         client->engine_changed_cb = NULL;
2257         client->engine_changed_user_data = NULL;
2258
2259         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset engine changed cb");
2260
2261         return 0;
2262 }
2263