1c78f22da672637ce201125444864eb7537f9f33
[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 #include <package-manager.h>
24 #include <pthread.h>
25
26 #include "tts.h"
27 #include "tts_client.h"
28 #include "tts_config_mgr.h"
29 #include "tts_dbus.h"
30 #include "tts_main.h"
31
32 #include "tts_internal.h"
33
34 static bool g_screen_reader;
35
36 static int g_feature_enabled = -1;
37
38 static bool g_err_callback_status = false;
39
40 static int g_max_text_size = -1;
41
42 static Ecore_Timer* g_check_state_timer = NULL;
43
44 static Ecore_Timer* g_hello_timer;
45
46 /* for repetition */
47 static char* g_language = NULL;
48
49 static int g_voice_type = -1;
50
51 static int g_speed = -1;
52 static int g_retry_cnt = 0;
53
54 /* for checking engine update */
55 static pkgmgr_client* g_pkgmgr = NULL;
56 static char* g_engine_name = NULL;
57 static int g_engine_update_status = 0;
58 static pthread_mutex_t g_pkgmgr_mutex = PTHREAD_MUTEX_INITIALIZER;
59
60
61 /* Function definition */
62 static Eina_Bool __tts_notify_state_changed(void *data);
63 static Eina_Bool __tts_notify_error(void *data);
64 int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg);
65
66
67 const char* tts_tag()
68 {
69         return "ttsc";
70 }
71
72 static int __tts_get_feature_enabled()
73 {
74         if (0 == g_feature_enabled) {
75                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] TTS feature NOT supported");
76                 return TTS_ERROR_NOT_SUPPORTED;
77         } else if (-1 == g_feature_enabled) {
78                 bool tts_supported = false;
79                 if (0 == system_info_get_platform_bool(TTS_FEATURE_PATH, &tts_supported)) {
80                         if (false == tts_supported) {
81                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] TTS feature NOT supported");
82                                 g_feature_enabled = 0;
83                                 return TTS_ERROR_NOT_SUPPORTED;
84                         }
85
86                         g_feature_enabled = 1;
87                 } else {
88                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get feature value");
89                         return TTS_ERROR_NOT_SUPPORTED;
90                 }
91         }
92
93         return 0;
94 }
95
96 static const char* __tts_get_error_code(tts_error_e err)
97 {
98         switch (err) {
99         case TTS_ERROR_NONE:                    return "TTS_ERROR_NONE";
100         case TTS_ERROR_OUT_OF_MEMORY:           return "TTS_ERROR_OUT_OF_MEMORY";
101         case TTS_ERROR_IO_ERROR:                return "TTS_ERROR_IO_ERROR";
102         case TTS_ERROR_INVALID_PARAMETER:       return "TTS_ERROR_INVALID_PARAMETER";
103         case TTS_ERROR_OUT_OF_NETWORK:          return "TTS_ERROR_OUT_OF_NETWORK";
104         case TTS_ERROR_TIMED_OUT:               return "TTS_ERROR_TIMED_OUT";
105         case TTS_ERROR_PERMISSION_DENIED:       return "TTS_ERROR_PERMISSION_DENIED";
106         case TTS_ERROR_NOT_SUPPORTED:           return "TTS_ERROR_NOT_SUPPORTED";
107         case TTS_ERROR_INVALID_STATE:           return "TTS_ERROR_INVALID_STATE";
108         case TTS_ERROR_INVALID_VOICE:           return "TTS_ERROR_INVALID_VOICE";
109         case TTS_ERROR_ENGINE_NOT_FOUND:        return "TTS_ERROR_ENGINE_NOT_FOUND";
110         case TTS_ERROR_OPERATION_FAILED:        return "TTS_ERROR_OPERATION_FAILED";
111         case TTS_ERROR_AUDIO_POLICY_BLOCKED:    return "TTS_ERROR_AUDIO_POLICY_BLOCKED";
112         case TTS_ERROR_NOT_SUPPORTED_FEATURE:   return "TTS_ERROR_NOT_SUPPORTED_FEATURE";
113         case TTS_ERROR_SERVICE_RESET:           return "TTS_ERROR_SERVICE_RESET";
114         default:
115                 return "Invalid error code";
116         }
117         return NULL;
118 }
119
120 static int __tts_convert_config_error_code(tts_config_error_e code)
121 {
122         if (code == TTS_CONFIG_ERROR_NONE)                      return TTS_ERROR_NONE;
123         if (code == TTS_CONFIG_ERROR_OUT_OF_MEMORY)             return TTS_ERROR_OUT_OF_MEMORY;
124         if (code == TTS_CONFIG_ERROR_IO_ERROR)                  return TTS_ERROR_IO_ERROR;
125         if (code == TTS_CONFIG_ERROR_INVALID_PARAMETER)         return TTS_ERROR_INVALID_PARAMETER;
126         if (code == TTS_CONFIG_ERROR_INVALID_STATE)             return TTS_ERROR_INVALID_STATE;
127         if (code == TTS_CONFIG_ERROR_INVALID_VOICE)             return TTS_ERROR_INVALID_VOICE;
128         if (code == TTS_CONFIG_ERROR_ENGINE_NOT_FOUND)          return TTS_ERROR_ENGINE_NOT_FOUND;
129         if (code == TTS_CONFIG_ERROR_OPERATION_FAILED)          return TTS_ERROR_OPERATION_FAILED;
130         if (code == TTS_CONFIG_ERROR_NOT_SUPPORTED_FEATURE)     return TTS_ERROR_NOT_SUPPORTED_FEATURE;
131
132         return code;
133 }
134
135 //LCOV_EXCL_START
136 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)
137 {
138         SLOG(LOG_DEBUG, TAG_TTSC, "Voice changed : Before lang(%s) type(%d) , Current lang(%s), type(%d)",
139                 before_lang, before_voice_type, language, voice_type);
140
141         GList* client_list = NULL;
142         client_list = tts_client_get_client_list();
143
144         GList *iter = NULL;
145         tts_client_s *data = NULL;
146
147         if (g_list_length(client_list) > 0) {
148                 /* Get a first item */
149                 iter = g_list_first(client_list);
150
151                 while (NULL != iter) {
152                         data = iter->data;
153                         if (NULL != data->default_voice_changed_cb) {
154                                 SLOG(LOG_DEBUG, TAG_TTSC, "Call default voice changed callback : uid(%d)", data->uid);
155                                 data->default_voice_changed_cb(data->tts, before_lang, before_voice_type, 
156                                         language, voice_type, data->default_voice_changed_user_data);
157                         }
158
159                         /* Check whether language is changed or not. If it is changed, make 'text_repeat' NULL */
160                         if (0 != strncmp(before_lang, language, strlen(before_lang))) {
161                                 if (NULL != data->text_repeat) {
162                                         free(data->text_repeat);
163                                         data->text_repeat = NULL;
164                                 }
165                         }
166
167                         /* Next item */
168                         iter = g_list_next(iter);
169                 }
170         }
171
172         return;
173 }
174
175 static Eina_Bool __reconnect_by_engine_changed(void* data)
176 {
177         tts_h tts = (tts_h)data;
178
179         tts_client_s* client = tts_client_get(tts);
180         if (NULL == client) {
181                 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
182                 return EINA_FALSE;
183         }
184
185         if (TTS_STATE_READY != client->current_state) {
186                 usleep(10000);
187                 return EINA_TRUE;
188         }
189
190         int ret = tts_unprepare(tts);
191         if (0 != ret) {
192                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
193         }
194         ret = tts_prepare(tts);
195         if (0 != ret) {
196                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
197         }
198
199         return EINA_FALSE;
200 }
201
202 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)
203 {
204         tts_h tts = (tts_h)user_data;
205
206         tts_client_s* client = tts_client_get(tts);
207         if (NULL == client) {
208                 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
209                 return;
210         }
211
212         if (NULL != engine_id)  SLOG(LOG_DEBUG, TAG_TTSC, "Engine id(%s)", engine_id);
213         if (NULL != setting)    SLOG(LOG_DEBUG, TAG_TTSC, "Engine setting(%s)", setting);
214         if (NULL != language)   SLOG(LOG_DEBUG, TAG_TTSC, "Language(%s)", language);
215         SLOG(LOG_DEBUG, TAG_TTSC, "Voice type(%d), Auto voice(%s), Credential(%s)", voice_type, auto_voice ? "on" : "off", need_credential ? "need" : "no need");
216
217         /* When the default engine is changed, please unload the old engine and load the new one. */
218         int ret = -1;
219
220         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_PAUSED == client->current_state) {
221                 ret = tts_stop(tts);
222                 if (0 != ret) {
223                         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] TTS client stopping...");
224                 }
225
226                 ecore_idler_add(__reconnect_by_engine_changed, (void*)tts);
227         } else if (TTS_STATE_READY == client->current_state) {
228                 ret = tts_unprepare(tts);
229                 if (0 != ret) {
230                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare for setting a new engine... (%d)", ret);
231                 }
232                 ret = tts_prepare(tts);
233                 if (0 != ret) {
234                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
235                 }
236         }
237
238         /* call callback function */
239         if (NULL != client->engine_changed_cb) {
240                 client->engine_changed_cb(tts, engine_id, language, voice_type, need_credential, client->engine_changed_user_data);
241         } else {
242                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function for changed engine");
243         }
244         return;
245 }
246 //LCOV_EXCL_STOP
247
248 void __tts_unset_all_callbacks(tts_h tts)
249 {
250         SLOG(LOG_INFO, TAG_TTSC, "@@@ unset all callbacks");
251
252         tts_unset_state_changed_cb(tts);
253         tts_unset_utterance_started_cb(tts);
254         tts_unset_utterance_completed_cb(tts);
255         tts_unset_error_cb(tts);
256         tts_unset_default_voice_changed_cb(tts);
257         tts_unset_engine_changed_cb(tts);
258
259         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
260 }
261
262 static int __pkgmgr_status_cb(uid_t target_uid, int req_id, const char *type, const char *pkgname, const char *key, const char *val, const void *pmsg, void *data)
263 {
264         SLOG(LOG_INFO, TAG_TTSC, "[INFO] pkgmgr status cb is invoked. pkgname(%s), type(%s), key(%s), val(%s)", pkgname, type, key, val);
265
266         if (0 != strncmp(g_engine_name, pkgname, strlen(g_engine_name))) {
267                 SLOG(LOG_DEBUG, TAG_TTSC, "[WARN] this is not tts engine");
268                 return 0;
269         } else {
270                 if (key && 0 == strncmp(key, "start", strlen(key))) {
271                         if (val && (0 == strncmp(val, "update", strlen(val) || 0 == strncmp(val, "uninstall", strlen(val))))) {
272                                 SLOG(LOG_INFO, TAG_TTSC, "[INFO] start to install.");
273                                 g_engine_update_status = 1;
274                         }
275                 } else if (key && 0 == strncmp(key, "end", strlen(key))) {
276                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] finish to install");
277                         g_engine_update_status = 0;
278                 }
279         }
280
281         return 0;
282 }
283
284 static void __create_pkgmgr_thread(void* data, Ecore_Thread* thread)
285 {
286         SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] create pkgmgr thread");
287
288         pthread_mutex_lock(&g_pkgmgr_mutex);
289
290         while (!g_pkgmgr) {
291                 g_pkgmgr = pkgmgr_client_new(PC_LISTENING);
292                 if (NULL == g_pkgmgr) {
293                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create pkgmgr handle");
294                 } else {
295                         if (pkgmgr_client_listen_status(g_pkgmgr, __pkgmgr_status_cb, NULL) < 0) {
296                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to listen pkgmgr status. remove and recreate client");
297                                 pkgmgr_client_free(g_pkgmgr);
298                                 g_pkgmgr = NULL;
299                                 continue;
300                         } else {
301                                 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] Succeed to register pkgmgr cb");
302                         }
303                 }
304                 usleep(10000);
305         }
306
307         pthread_mutex_unlock(&g_pkgmgr_mutex);
308
309         return ;
310 }
311
312 static void __finish_pkgmgr_thread(void* data, Ecore_Thread* thread)
313 {
314         SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] Finish pkgmgr thread");
315 }
316
317 static void __pkgmgr_thread(void* data)
318 {
319         SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] call pkgmgr_thread");
320
321         ecore_thread_run(__create_pkgmgr_thread, __finish_pkgmgr_thread, NULL, NULL);
322
323         return ;
324 }
325
326 int tts_create(tts_h* tts)
327 {
328         if (0 != __tts_get_feature_enabled()) {
329                 return TTS_ERROR_NOT_SUPPORTED;
330         }
331
332         SLOG(LOG_INFO, TAG_TTSC, "@@@ Create TTS");
333
334         /* check param */
335         if (NULL == tts) {
336                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
337                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
338                 return TTS_ERROR_INVALID_PARAMETER;
339         }
340
341         if (0 == tts_client_get_size()) {
342                 if (0 != tts_dbus_open_connection()) {
343                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to open dbus connection");
344                         return TTS_ERROR_OPERATION_FAILED;
345                 }
346         }
347
348         if (0 != tts_client_new(tts)) {
349                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create client!!!!!");
350                 return TTS_ERROR_OUT_OF_MEMORY;
351         }
352
353         tts_client_s* client = tts_client_get(*tts);
354         if (NULL == client) {
355                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client");
356                 return TTS_ERROR_OPERATION_FAILED;
357         }
358
359         int ret = tts_config_mgr_initialize(client->uid);
360         if (0 != ret) {
361                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to init config manager : %d", ret);
362                 tts_client_destroy(*tts);
363                 return __tts_convert_config_error_code(ret);
364         }
365
366         ret = tts_config_mgr_set_callback(client->uid, _tts_config_engine_changed_cb, __tts_config_voice_changed_cb, NULL, NULL, NULL, client->tts);
367         if (0 != ret) {
368                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set config changed : %d", ret);
369                 tts_client_destroy(*tts);
370                 return __tts_convert_config_error_code(ret);
371         }
372
373         ecore_main_loop_thread_safe_call_async(__pkgmgr_thread, NULL);
374
375         SLOG(LOG_INFO, TAG_TTSC, "[INFO] call ecore thread for creating pkgmgr thread");
376
377         g_engine_name = vconf_get_str(TTS_ENGINE_DB_DEFAULT);
378         if (NULL == g_engine_name) {
379                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get engine name");
380         } else {
381                 SLOG(LOG_ERROR, TAG_TTSC, "[INFO] Engine name(%s)", g_engine_name);
382         }
383
384         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
385         SLOG(LOG_DEBUG, TAG_TTSC, " ");
386
387         return TTS_ERROR_NONE;
388 }
389
390 int tts_destroy(tts_h tts)
391 {
392         if (0 != __tts_get_feature_enabled()) {
393                 return TTS_ERROR_NOT_SUPPORTED;
394         }
395
396         SLOG(LOG_INFO, TAG_TTSC, "@@@ Destroy TTS");
397
398         if (NULL == tts) {
399                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
400                 return TTS_ERROR_INVALID_PARAMETER;
401         }
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 valid");
408                 return TTS_ERROR_INVALID_PARAMETER;
409         }
410
411         /* check used callback */
412         if (0 != tts_client_get_use_callback(client)) {
413                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Cannot destroy in Callback function");
414                 return TTS_ERROR_OPERATION_FAILED;
415         }
416
417         pthread_mutex_lock(&g_pkgmgr_mutex);
418         if (g_pkgmgr) {
419                 pkgmgr_client_remove_listen_status(g_pkgmgr);
420                 pkgmgr_client_free(g_pkgmgr);
421                 g_pkgmgr = NULL;
422         }
423         pthread_mutex_unlock(&g_pkgmgr_mutex);
424
425         tts_config_mgr_finalize(client->uid);
426
427         int ret = -1;
428         int count = 0;
429         int screen_reader = -1;
430
431         /* check state */
432         switch (client->current_state) {
433         case TTS_STATE_PAUSED:
434         case TTS_STATE_PLAYING:
435         case TTS_STATE_READY:
436                 ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
437                 if (0 != ret) {
438                         SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get screen reader");
439                 } else {
440                         SLOG(LOG_INFO, tts_tag(), "[INFO] Success to get screen reader(%d), g_screen_reader(%s), client->mode(%d)", screen_reader, (true == g_screen_reader) ? "True" : "False", client->mode);
441                         g_screen_reader = (bool)screen_reader;
442                 }
443                 if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
444                         do {
445                                 ret = tts_dbus_request_finalize(client->uid);
446                                 if (0 != ret) {
447                                         //LCOV_EXCL_START
448                                         if (TTS_ERROR_TIMED_OUT != ret) {
449                                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
450                                                 break;
451                                         } else {
452                                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize");
453                                                 usleep(10000);
454                                                 count++;
455                                                 if (TTS_RETRY_COUNT == count) {
456                                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
457                                                         break;
458                                                 }
459                                         }
460                                         //LCOV_EXCL_STOP
461                                 }
462                         } while (0 != ret);
463                 } else {
464                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize : g_sr(%d) mode(%d)", g_screen_reader, client->mode);
465                 }
466
467                 client->before_state = client->current_state;
468                 client->current_state = TTS_STATE_CREATED;
469
470         case TTS_STATE_CREATED:
471                 if (NULL != client->conn_timer) {
472                         SLOG(LOG_DEBUG, TAG_TTSC, "Connect Timer is deleted");
473                         ecore_timer_del(client->conn_timer);
474                         client->conn_timer = NULL;
475                 }
476                 /* Unset registered callbacks */
477                 __tts_unset_all_callbacks(tts);
478
479                 /* Free resources */
480                 tts_client_destroy(tts);
481                 break;
482
483         default:
484                 break;
485         }
486
487         if (0 == tts_client_get_size()) {
488                 if (0 != tts_dbus_close_connection()) {
489                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to close connection");
490                 }
491         }
492
493         if (NULL != g_language) {
494                 free(g_language);
495                 g_language = NULL;
496         }
497
498         /* Delete state timer before destroying handle */
499         if (NULL != g_check_state_timer) {
500                 ecore_timer_del(g_check_state_timer);
501                 g_check_state_timer = NULL;
502         }
503
504         tts = NULL;
505
506         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
507
508         return TTS_ERROR_NONE;
509 }
510
511 //LCOV_EXCL_START
512 void __tts_screen_reader_changed_cb(bool value)
513 {
514         g_screen_reader = value;
515 }
516 //LCOV_EXCL_STOP
517
518 int tts_set_mode(tts_h tts, tts_mode_e mode)
519 {
520         if (0 != __tts_get_feature_enabled()) {
521                 return TTS_ERROR_NOT_SUPPORTED;
522         }
523
524         SLOG(LOG_INFO, TAG_TTSC, "@@@ Set TTS mode(%d)", mode);
525
526         tts_client_s* client = tts_client_get(tts);
527
528         /* check handle */
529         if (NULL == client) {
530                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
531                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
532                 return TTS_ERROR_INVALID_PARAMETER;
533         }
534
535         /* check state */
536         if (client->current_state != TTS_STATE_CREATED) {
537                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
538                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
539                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
540                 return TTS_ERROR_INVALID_STATE;
541         }
542
543         if (TTS_MODE_DEFAULT <= mode && mode <= TTS_MODE_INTERRUPT) {
544                 client->mode = mode;
545         } else {
546                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] mode is not valid : %d", mode);
547                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
548                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
549                 return TTS_ERROR_INVALID_PARAMETER;
550         }
551
552         if (TTS_MODE_SCREEN_READER == mode) {
553                 int ret;
554                 int screen_reader;
555                 ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
556                 if (0 != ret) {
557                         SLOG(LOG_ERROR, tts_tag(), "[Config ERROR] Fail to get screen reader");
558                         return TTS_ERROR_OPERATION_FAILED;
559                 }
560                 g_screen_reader = (bool)screen_reader;
561                 tts_config_set_screen_reader_callback(client->uid, __tts_screen_reader_changed_cb);
562         } else {
563                 tts_config_unset_screen_reader_callback(client->uid);
564         }
565
566         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
567
568         return TTS_ERROR_NONE;
569 }
570
571 int tts_get_mode(tts_h tts, tts_mode_e* mode)
572 {
573         if (0 != __tts_get_feature_enabled()) {
574                 return TTS_ERROR_NOT_SUPPORTED;
575         }
576
577         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get TTS mode");
578
579         tts_client_s* client = tts_client_get(tts);
580
581         /* check handle */
582         if (NULL == client) {
583                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
584                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
585                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
586                 return TTS_ERROR_INVALID_PARAMETER;
587         }
588
589         /* check state */
590         if (client->current_state != TTS_STATE_CREATED) {
591                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
592                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
593                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
594                 return TTS_ERROR_INVALID_STATE;
595         }
596
597         if (NULL == mode) {
598                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter(mode) is NULL");
599                 return TTS_ERROR_INVALID_PARAMETER;
600         }
601
602         *mode = client->mode;
603
604         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
605         SLOG(LOG_DEBUG, TAG_TTSC, " ");
606
607         return TTS_ERROR_NONE;
608 }
609
610 int tts_set_credential(tts_h tts, const char* credential)
611 {
612         if (0 != __tts_get_feature_enabled()) {
613                 return TTS_ERROR_NOT_SUPPORTED;
614         }
615
616         if (NULL == tts || NULL == credential) {
617                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
618                 return TTS_ERROR_INVALID_PARAMETER;
619         }
620
621         tts_client_s* client = tts_client_get(tts);
622
623         if (NULL == client) {
624                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
625                 return TTS_ERROR_INVALID_PARAMETER;
626         }
627
628         if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
629                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
630                 return TTS_ERROR_INVALID_STATE;
631         }
632
633         if (NULL != client->credential) {
634                 free(client->credential);
635                 client->credential = NULL;
636         }
637         client->credential = strdup(credential);
638
639         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
640
641         return TTS_ERROR_NONE;
642 }
643
644 //LCOV_EXCL_START
645 int tts_set_server_tts(tts_h tts, const char* credential)
646 {
647         if (0 != __tts_get_feature_enabled()) {
648                 return TTS_ERROR_NOT_SUPPORTED;
649         }
650
651         if (NULL == tts) {
652                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
653                 return TTS_ERROR_INVALID_PARAMETER;
654         }
655
656         tts_client_s* client = tts_client_get(tts);
657
658         if (NULL == client) {
659                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
660                 return TTS_ERROR_INVALID_PARAMETER;
661         }
662
663         if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
664                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
665                 return TTS_ERROR_INVALID_STATE;
666         }
667
668         if (NULL != client->credential) {
669                 free(client->credential);
670                 client->credential = NULL;
671         }
672
673         client->internal = true;
674
675         char* key = NULL;
676         if (NULL != credential) {
677                 key = strdup("EnableServerTTS");
678                 client->credential = strdup(credential);
679                 if (NULL == client->credential) {
680                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
681                         if (NULL != key) {
682                                 free(key);
683                                 key = NULL;
684                         }
685                         return TTS_ERROR_OUT_OF_MEMORY;
686                 }
687         } else {
688                 key = strdup("DisableServerTTS");
689         }
690
691         if (NULL == key) {
692                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
693                 return TTS_ERROR_OUT_OF_MEMORY;
694         }
695
696         int pid = getpid();
697         char* appid = NULL;
698         int ret = app_manager_get_app_id(pid, &appid);
699         if (0 != ret || NULL == appid) {
700                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
701                 free(key);
702                 key = NULL;
703                 if (NULL != appid) {
704                         free(appid);
705                         appid = NULL;
706                 }
707                 return TTS_ERROR_OPERATION_FAILED;
708         }
709
710         ret = tts_set_private_data(tts, key, appid);
711         if (0 != ret) {
712                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set private data, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
713                 free(key);
714                 key = NULL;
715                 free(appid);
716                 appid = NULL;
717                 return ret;
718         }
719
720         free(appid);
721         appid = NULL;
722         free(key);
723         key = NULL;
724
725         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
726
727         return TTS_ERROR_NONE;
728 }
729 // LCOV_EXCL_STOP
730
731 static Eina_Bool __tts_connect_daemon(void *data)
732 {
733         tts_h tts = (tts_h)data;
734         tts_client_s* client = tts_client_get(tts);
735
736         /* check handle */
737         if (NULL == client) {
738                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
739                 return EINA_FALSE;
740         }
741
742         /* check whether engine is updating or not */
743         if (g_engine_update_status) {
744                 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] cannot prepare due to engine update");
745                 __tts_cb_error(-1, TTS_ERROR_SERVICE_RESET, -1, "Daemon Reset");
746
747                 return EINA_FALSE;
748         }
749
750         /* Send hello */
751         if (0 != tts_dbus_request_hello_sync(client->uid)) {
752                 return EINA_TRUE;
753         }
754
755         SLOG(LOG_INFO, TAG_TTSC, "@@@ Connect daemon");
756
757         /* do request initialize */
758         int ret = -1;
759         bool credential_needed = false;
760
761         ret = tts_dbus_request_initialize(client->uid, &credential_needed);
762
763         if (TTS_ERROR_ENGINE_NOT_FOUND == ret) {
764                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
765
766                 client->reason = TTS_ERROR_ENGINE_NOT_FOUND;
767                 client->utt_id = -1;
768
769                 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
770                 if (client->conn_timer) {
771                         ecore_timer_del(client->conn_timer);
772                         client->conn_timer = NULL;
773                 }
774                 return EINA_FALSE;
775
776         } else if (TTS_ERROR_PERMISSION_DENIED == ret) {
777                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
778
779                 client->reason = TTS_ERROR_PERMISSION_DENIED;
780                 client->utt_id = -1;
781
782                 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
783                 if (client->conn_timer) {
784                         ecore_timer_del(client->conn_timer);
785                         client->conn_timer = NULL;
786                 }
787                 return EINA_FALSE;
788
789         } else if (TTS_ERROR_NONE != ret) {
790                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Fail to connection. Retry to connect : %s", __tts_get_error_code(ret));
791                 return EINA_TRUE;
792
793         } else {
794                 /* success to connect tts-daemon */
795                 client->credential_needed = credential_needed;
796                 SLOG(LOG_ERROR, TAG_TTSC, "Supported options : credential(%s)", credential_needed ? "need" : "no need");
797         }
798
799         if (client->conn_timer) {
800                 ecore_timer_del(client->conn_timer);
801                 client->conn_timer = NULL;
802         }
803
804         client = tts_client_get(tts);
805         /* check handle */
806         if (NULL == client) {
807                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
808                 return EINA_FALSE;
809         }
810
811         client->before_state = client->current_state;
812         client->current_state = TTS_STATE_READY;
813
814         if (NULL != client->state_changed_cb) {
815                 tts_client_use_callback(client);
816                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
817                 tts_client_not_use_callback(client);
818         } else {
819                 SLOG(LOG_WARN, TAG_TTSC, "State changed callback is NULL");
820         }
821
822         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
823
824         return EINA_FALSE;
825 }
826
827 int __tts_cb_hello(int uid, int ret, int credential_needed)
828 {
829         tts_client_s* client = tts_client_get_by_uid(uid);
830         if (NULL == client) {
831                 SLOG(LOG_ERROR, TAG_TTSC, "Fail to get TTS client or ignore this uid(%d)", uid);
832                 return TTS_ERROR_OPERATION_FAILED;
833         }
834
835         if (g_hello_timer) {
836                 ecore_timer_del(g_hello_timer);
837                 g_hello_timer = NULL;
838         }
839
840         if (TTS_STATE_READY == client->current_state) {
841                 SLOG(LOG_INFO, TAG_TTSC, "[INFO] tts client is already READY");
842                 return TTS_ERROR_NONE;
843         }
844
845         if (TTS_ERROR_ENGINE_NOT_FOUND == ret) {
846                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
847
848                 client->reason = TTS_ERROR_ENGINE_NOT_FOUND;
849                 client->utt_id = -1;
850
851                 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
852                 return TTS_ERROR_OPERATION_FAILED;
853
854         } else if (TTS_ERROR_PERMISSION_DENIED == ret) {
855                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
856
857                 client->reason = TTS_ERROR_PERMISSION_DENIED;
858                 client->utt_id = -1;
859
860                 ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
861                 return TTS_ERROR_PERMISSION_DENIED;
862
863         } else if (TTS_ERROR_NONE != ret) {
864                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Fail to connection. Retry to connect : %s", __tts_get_error_code(ret));
865                 return TTS_ERROR_OPERATION_FAILED;
866
867         } else {
868                 /* success to connect tts-daemon */
869                 client->credential_needed = credential_needed;
870                 SLOG(LOG_ERROR, TAG_TTSC, "Supported options : credential(%s)", credential_needed ? "need" : "no need");
871         }
872
873         client->before_state = client->current_state;
874         client->current_state = TTS_STATE_READY;
875
876         ecore_timer_add(0.0, __tts_notify_state_changed, client->tts);
877
878         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
879         return TTS_ERROR_NONE;
880 }
881
882 static Eina_Bool __send_hello(void *data)
883 {
884         tts_h tts = (tts_h)data;
885         tts_client_s* client = tts_client_get(tts);
886
887         /* check handle */
888         if (NULL == client) {
889                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
890                 g_hello_timer = NULL;
891                 return EINA_FALSE;
892         }
893
894         /* check state */
895         if (client->current_state == TTS_STATE_READY) {
896                 SLOG(LOG_ERROR, TAG_TTSC, "[INFO] TTS client has been already connected to tts service"); //LCOV_EXCL_LINE
897                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
898                 g_hello_timer = NULL;
899                 return EINA_FALSE;
900         }
901
902         /* Send hello */
903         int ret = tts_dbus_request_hello(client->uid);
904         if (0 != ret)  {
905                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request hello !!"); //LCOV_EXCL_LINE
906         } else {
907                 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Send Hello");
908         }
909
910         g_retry_cnt++;
911         if (TTS_HELLO_RETRY_COUNT == g_retry_cnt) {
912                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Stop to send hello, retry count reaches the limit");
913                 g_retry_cnt = 0;
914                 return EINA_FALSE;
915         }
916
917         if (!g_hello_timer) {
918                 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
919                 g_hello_timer = ecore_timer_add(0.5, __send_hello, tts);
920                 return EINA_FALSE;
921         }
922         return EINA_TRUE;
923 }
924
925 int tts_prepare(tts_h tts)
926 {
927         if (0 != __tts_get_feature_enabled()) {
928                 return TTS_ERROR_NOT_SUPPORTED;
929         }
930
931         SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
932
933         tts_client_s* client = tts_client_get(tts);
934
935         /* check handle */
936         if (NULL == client) {
937                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
938                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
939                 return TTS_ERROR_INVALID_PARAMETER;
940         }
941
942         /* check state */
943         if (client->current_state != TTS_STATE_CREATED) {
944                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
945                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
946                 return TTS_ERROR_INVALID_STATE;
947         }
948
949         if (NULL == g_hello_timer) {
950                 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
951                 g_retry_cnt = 0;
952                 ecore_thread_main_loop_begin();
953                 ecore_timer_add(0.0, __send_hello, (void*)tts);
954                 ecore_thread_main_loop_end();
955         }
956
957         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
958
959         return TTS_ERROR_NONE;
960 }
961
962 //LCOV_EXCL_START
963 int tts_prepare_sync(tts_h tts)
964 {
965         if (0 != __tts_get_feature_enabled()) {
966                 return TTS_ERROR_NOT_SUPPORTED;
967         }
968
969         SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
970
971         tts_client_s* client = tts_client_get(tts);
972
973         /* check handle */
974         if (NULL == client) {
975                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
976                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
977                 return TTS_ERROR_INVALID_PARAMETER;
978         }
979
980         /* check state */
981         if (client->current_state != TTS_STATE_CREATED) {
982                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
983                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
984                 return TTS_ERROR_INVALID_STATE;
985         }
986
987         int cnt = 0;
988         while (EINA_TRUE == __tts_connect_daemon((void*)tts) && TTS_CONNECTION_RETRY_COUNT > cnt) {
989                 cnt++;
990         }
991
992         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
993
994         if (TTS_CONNECTION_RETRY_COUNT == cnt) {
995                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to connect daemon");
996                 return TTS_ERROR_OPERATION_FAILED;
997         }
998
999         return TTS_ERROR_NONE;
1000 }
1001 //LCOV_EXCL_STOP
1002
1003 int tts_unprepare(tts_h tts)
1004 {
1005         if (0 != __tts_get_feature_enabled()) {
1006                 return TTS_ERROR_NOT_SUPPORTED;
1007         }
1008
1009         SLOG(LOG_INFO, TAG_TTSC, "@@@ Unprepare TTS");
1010
1011         tts_client_s* client = tts_client_get(tts);
1012
1013         /* check handle */
1014         if (NULL == client) {
1015                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
1016                 return TTS_ERROR_INVALID_PARAMETER;
1017         }
1018
1019         /* check state */
1020         if (client->current_state != TTS_STATE_READY) {
1021                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'READY'");
1022                 return TTS_ERROR_INVALID_STATE;
1023         }
1024
1025         int ret = -1;
1026         int count = 0;
1027         int screen_reader = -1;
1028
1029         ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
1030         if (0 != ret) {
1031                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get screen reader");
1032         } else {
1033                 SLOG(LOG_INFO, tts_tag(), "[INFO] Success to get screen reader(%d), g_screen_reader(%s), client->mode(%d)", screen_reader, (true == g_screen_reader) ? "True" : "False", client->mode);
1034                 g_screen_reader = (bool)screen_reader;
1035         }
1036
1037         bool is_prepared = false;
1038         if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
1039                 do {
1040                         ret = tts_dbus_request_finalize(client->uid);
1041                         if (0 != ret) {
1042                                 //LCOV_EXCL_START
1043                                 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1044                                         client->current_state = TTS_STATE_CREATED;
1045                                         if (0 == tts_prepare_sync(tts)) {
1046                                                 is_prepared = true;
1047                                                 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1048                                         }
1049                                 } else if (TTS_ERROR_TIMED_OUT != ret) {
1050                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1051                                         break;
1052                                 } else {
1053                                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize : %s", __tts_get_error_code(ret));
1054                                         usleep(10000);
1055                                         count++;
1056                                         if (TTS_RETRY_COUNT == count) {
1057                                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1058                                                 break;
1059                                         }
1060                                 }
1061                                 //LCOV_EXCL_STOP
1062                         }
1063                 } while (0 != ret);
1064         } else {
1065                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize : g_sr(%d) mode(%d)", g_screen_reader, client->mode);
1066         }
1067
1068         client->before_state = client->current_state;
1069         client->current_state = TTS_STATE_CREATED;
1070
1071         if (NULL != client->state_changed_cb) {
1072                 tts_client_use_callback(client);
1073                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1074                 tts_client_not_use_callback(client);
1075                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1076         }
1077
1078         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1079
1080         return TTS_ERROR_NONE;
1081 }
1082
1083 bool __tts_supported_voice_cb(const char* engine_id, const char* language, int type, void* user_data)
1084 {
1085         tts_h tts = (tts_h)user_data;
1086
1087         tts_client_s* client = tts_client_get(tts);
1088         if (NULL == client) {
1089                 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
1090                 return false;
1091         }
1092
1093         /* call callback function */
1094         if (NULL != client->supported_voice_cb) {
1095                 return client->supported_voice_cb(tts, language, type, client->supported_voice_user_data);
1096         } else {
1097                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of supported voice");
1098         }
1099
1100         return false;
1101 }
1102
1103 int tts_foreach_supported_voices(tts_h tts, tts_supported_voice_cb callback, void* user_data)
1104 {
1105         if (0 != __tts_get_feature_enabled()) {
1106                 return TTS_ERROR_NOT_SUPPORTED;
1107         }
1108
1109         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Foreach supported voices");
1110
1111         if (NULL == tts || NULL == callback) {
1112                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1113                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1114                 return TTS_ERROR_INVALID_PARAMETER;
1115         }
1116
1117         tts_client_s* client = tts_client_get(tts);
1118
1119         /* check handle */
1120         if (NULL == client) {
1121                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1122                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1123                 return TTS_ERROR_INVALID_PARAMETER;
1124         }
1125
1126         int ret = 0;
1127         char* current_engine = NULL;
1128         ret = tts_config_mgr_get_engine(&current_engine);
1129         if (0 != ret) {
1130                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get current engine : %d", ret);
1131                 return __tts_convert_config_error_code(ret);
1132         }
1133
1134         client->supported_voice_cb = callback;
1135         client->supported_voice_user_data = user_data;
1136
1137         ret = tts_config_mgr_get_voice_list(current_engine, __tts_supported_voice_cb, client->tts);
1138
1139         if (NULL != current_engine) {
1140                 free(current_engine);
1141                 current_engine = NULL;
1142         }
1143
1144         client->supported_voice_cb = NULL;
1145         client->supported_voice_user_data = NULL;
1146
1147         if (0 != ret) {
1148                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Result : %d", ret);
1149                 ret = TTS_ERROR_OPERATION_FAILED;
1150         }
1151
1152         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1153
1154         return ret;
1155 }
1156
1157 int tts_get_default_voice(tts_h tts, char** lang, int* vctype)
1158 {
1159         if (0 != __tts_get_feature_enabled()) {
1160                 return TTS_ERROR_NOT_SUPPORTED;
1161         }
1162
1163         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get default voice");
1164
1165         if (NULL == tts) {
1166                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1167                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1168                 return TTS_ERROR_INVALID_PARAMETER;
1169
1170         }
1171         tts_client_s* client = tts_client_get(tts);
1172
1173         if (NULL == client) {
1174                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1175                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1176                 return TTS_ERROR_INVALID_PARAMETER;
1177         }
1178
1179         /* Request call remote method */
1180         int ret = 0;
1181         ret = tts_config_mgr_get_voice(lang, vctype);
1182         if (0 != ret) {
1183                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %d", ret);
1184                 return __tts_convert_config_error_code(ret);
1185         } else {
1186                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Default language(%s), type(%d)", *lang, *vctype);
1187         }
1188
1189         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1190
1191         return ret;
1192 }
1193
1194 int tts_get_max_text_size(tts_h tts, unsigned int* size)
1195 {
1196         if (0 != __tts_get_feature_enabled()) {
1197                 return TTS_ERROR_NOT_SUPPORTED;
1198         }
1199
1200         if (NULL == tts || NULL == size) {
1201                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Input parameter is null");
1202                 return TTS_ERROR_INVALID_PARAMETER;
1203         }
1204
1205         tts_client_s* client = tts_client_get(tts);
1206
1207         if (NULL == client) {
1208                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : A handle is not valid");
1209                 return TTS_ERROR_INVALID_PARAMETER;
1210         }
1211
1212         if (TTS_STATE_READY != client->current_state) {
1213                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Current state is NOT 'READY'.");
1214                 return TTS_ERROR_INVALID_STATE;
1215         }
1216
1217         if (0 != tts_config_mgr_get_max_text_size(size)) {
1218                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
1219                 return TTS_ERROR_INVALID_PARAMETER;
1220         }
1221
1222         g_max_text_size = (int)*size;
1223
1224         SLOG(LOG_DEBUG, TAG_TTSC, "Get max text count : %d", *size);
1225         return TTS_ERROR_NONE;
1226 }
1227
1228 int tts_get_state(tts_h tts, tts_state_e* state)
1229 {
1230         if (0 != __tts_get_feature_enabled()) {
1231                 return TTS_ERROR_NOT_SUPPORTED;
1232         }
1233
1234         if (NULL == tts || NULL == state) {
1235                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : Input parameter is null");
1236                 return TTS_ERROR_INVALID_PARAMETER;
1237         }
1238
1239         tts_client_s* client = tts_client_get(tts);
1240
1241         if (NULL == client) {
1242                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1243                 return TTS_ERROR_INVALID_PARAMETER;
1244         }
1245
1246         *state = client->current_state;
1247
1248         switch (*state) {
1249         case TTS_STATE_CREATED: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Created'");        break;
1250         case TTS_STATE_READY:   SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Ready'");          break;
1251         case TTS_STATE_PLAYING: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Playing'");        break;
1252         case TTS_STATE_PAUSED:  SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Paused'");         break;
1253         default:                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid value");             break;
1254         }
1255
1256         return TTS_ERROR_NONE;
1257 }
1258
1259 int tts_get_speed_range(tts_h tts, int* min, int* normal, int* max)
1260 {
1261         if (0 != __tts_get_feature_enabled()) {
1262                 return TTS_ERROR_NOT_SUPPORTED;
1263         }
1264
1265         if (NULL == tts || NULL == min || NULL == normal || NULL == max) {
1266                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1267                 return TTS_ERROR_INVALID_PARAMETER;
1268         }
1269
1270         tts_client_s* client = tts_client_get(tts);
1271
1272         if (NULL == client) {
1273                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1274                 return TTS_ERROR_INVALID_PARAMETER;
1275         }
1276
1277         *min = TTS_SPEED_MIN;
1278         *normal = TTS_SPEED_NORMAL;
1279         *max = TTS_SPEED_MAX;
1280
1281         return TTS_ERROR_NONE;
1282 }
1283
1284 int tts_get_error_message(tts_h tts, char** err_msg)
1285 {
1286         if (0 != __tts_get_feature_enabled()) {
1287                 return TTS_ERROR_NOT_SUPPORTED;
1288         }
1289
1290         if (NULL == tts || NULL == err_msg) {
1291                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1292                 return TTS_ERROR_INVALID_PARAMETER;
1293         }
1294
1295         tts_client_s* client = tts_client_get(tts);
1296
1297         if (NULL == client) {
1298                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1299                 return TTS_ERROR_INVALID_PARAMETER;
1300         }
1301
1302         if (NULL != client->err_msg) {
1303                 *err_msg = strdup(client->err_msg);
1304                 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (%s)", *err_msg);
1305         } else {
1306                 *err_msg = NULL;
1307                 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (NULL)");
1308         }
1309
1310         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1311
1312         return TTS_ERROR_NONE;
1313 }
1314
1315 int tts_add_text(tts_h tts, const char* text, const char* language, int voice_type, int speed, int* utt_id)
1316 {
1317         SLOG(LOG_INFO, TAG_TTSC, "[DEBUG] Add text: text(%s), language(%s), type(%d)", (NULL == text) ? "NULL" : text, (NULL == language) ? "NULL" : language, voice_type);
1318
1319         if (0 != __tts_get_feature_enabled()) {
1320                 return TTS_ERROR_NOT_SUPPORTED;
1321         }
1322
1323         if (speed < 0) {
1324                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Speed should not be negative(%d)", speed);
1325                 return TTS_ERROR_INVALID_PARAMETER;
1326         }
1327
1328         if (voice_type < 0) {
1329                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Voice type should not be negative(%d)", voice_type);
1330                 return TTS_ERROR_INVALID_PARAMETER;
1331         }
1332
1333         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Add text");
1334
1335         if (NULL == tts || NULL == utt_id) {
1336                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1337                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1338                 return TTS_ERROR_INVALID_PARAMETER;
1339         }
1340
1341         tts_client_s* client = tts_client_get(tts);
1342
1343         if (NULL == client) {
1344                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1345                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1346                 return TTS_ERROR_INVALID_PARAMETER;
1347         }
1348
1349         if (TTS_STATE_CREATED == client->current_state) {
1350                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1351                 return TTS_ERROR_INVALID_STATE;
1352         }
1353
1354         if (-1 == g_max_text_size) {
1355                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d", g_max_text_size);
1356                 if (0 != tts_config_mgr_get_max_text_size((unsigned int*)&g_max_text_size)) {
1357                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
1358                         return TTS_ERROR_INVALID_PARAMETER;
1359                 }
1360         }
1361
1362         if (0 == g_max_text_size) {
1363                 if (strlen(text) <= 0) {
1364                         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Max Text Size is %d", g_max_text_size);
1365                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid. (max text size is unlimited.)");
1366                         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1367                         return TTS_ERROR_INVALID_PARAMETER;
1368                 }
1369         } else {
1370                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d", g_max_text_size);
1371                 if (g_max_text_size < strlen(text) || strlen(text) <= 0) {
1372                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid.");
1373                         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1374                         return TTS_ERROR_INVALID_PARAMETER;
1375                 }
1376         }
1377
1378         if (TTS_SPEED_AUTO > speed || TTS_SPEED_MAX < speed) {
1379                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] speed value(%d) is invalid.", speed);
1380                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1381                 return TTS_ERROR_INVALID_PARAMETER;
1382         }
1383
1384         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1385                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1386                 return TTS_ERROR_INVALID_STATE;
1387         }
1388
1389         if (true == client->credential_needed && NULL == client->credential) {
1390                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1391                 return TTS_ERROR_PERMISSION_DENIED;
1392         }
1393
1394         /* check valid utf8 */
1395         bool valid = false;
1396
1397         DBusError err;
1398         dbus_error_init(&err);
1399
1400         valid = dbus_validate_utf8(text, &err);
1401         if (dbus_error_is_set(&err)) {
1402                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Dbus Error(%s), text(%s)", err.message, text);
1403                 dbus_error_free(&err);
1404                 return TTS_ERROR_INVALID_PARAMETER;
1405         }
1406
1407         if (valid != true) {
1408                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Text is invalid - '%s'", text);
1409                 return TTS_ERROR_INVALID_PARAMETER;
1410         }
1411         SLOG(LOG_DEBUG, TAG_TTSC, "Text is valid - text is '%s'", text);
1412
1413         /* save texts for repetition */
1414         if (NULL != client->text_repeat) {
1415                 free(client->text_repeat);
1416                 client->text_repeat = NULL;
1417         }
1418
1419         client->text_repeat = strdup(text);
1420
1421         if (NULL != g_language) {
1422                 free(g_language);
1423                 g_language = NULL;
1424         }
1425         if (NULL == language)
1426                 g_language = NULL;
1427         else
1428                 g_language = strdup(language);
1429
1430         g_voice_type = voice_type;
1431         g_speed = speed;
1432
1433         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] repeat: text(%s), language(%s), voice type(%d), speed(%d)", client->text_repeat, (g_language) ? g_language : "NULL", g_voice_type, g_speed);
1434
1435         /* change default language value */
1436         char* temp = NULL;
1437
1438         if (NULL == language)
1439                 temp = strdup("default");
1440         else
1441                 temp = strdup(language);
1442
1443         client->current_utt_id++;
1444         if (client->current_utt_id == 10000) {
1445                 client->current_utt_id = 1;
1446         }
1447
1448         /* do request */
1449         int ret = -1;
1450         int count = 0;
1451         bool is_prepared = false;
1452         while (0 != ret) {
1453                 ret = tts_dbus_request_add_text(client->uid, text, temp, voice_type, speed, client->current_utt_id, client->credential);
1454                 if (0 != ret) {
1455                         //LCOV_EXCL_START
1456                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1457                                 client->current_state = TTS_STATE_CREATED;
1458                                 if (0 == tts_prepare_sync(tts)) {
1459                                         is_prepared = true;
1460                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1461                                 }
1462                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1463                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1464                                 break;
1465                         } else {
1466                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add text : %s", __tts_get_error_code(ret));
1467                                 usleep(10000);
1468                                 count++;
1469                                 if (TTS_RETRY_MIN_COUNT == count) {
1470                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1471                                         break;
1472                                 }
1473                         }
1474                         //LCOV_EXCL_STOP
1475                 } else {
1476                         *utt_id = client->current_utt_id;
1477                 }
1478         }
1479
1480         if (NULL != temp) {
1481                 free(temp);
1482                 temp = NULL;
1483         }
1484
1485         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1486
1487         return ret;
1488 }
1489
1490 //LCOV_EXCL_START
1491 static void __tts_play_async(void *data)
1492 {
1493         tts_h tts = (tts_h)data;
1494         tts_client_s* client = tts_client_get(tts);
1495
1496         /* check handle */
1497         if (NULL == client) {
1498                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1499                 return;
1500         }
1501
1502         int ret = -1;
1503         int count = 0;
1504         bool is_prepared = false;
1505         while (0 != ret) {
1506                 ret = tts_dbus_request_play(client->uid, client->credential);
1507                 if (0 != ret) {
1508                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1509                                 client->current_state = TTS_STATE_CREATED;
1510                                 if (0 == tts_prepare_sync(tts)) {
1511                                         is_prepared = true;
1512                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1513                                 }
1514                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1515                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1516                                 break;
1517                         } else {
1518                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1519                                 usleep(10000);
1520                                 count++;
1521                                 if (TTS_RETRY_COUNT == count) {
1522                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1523                                         break;
1524                                 }
1525                         }
1526                 }
1527         }
1528
1529         if (0 != ret) {
1530                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play tts : %s", __tts_get_error_code(ret));
1531
1532                 client->reason = ret;
1533                 client->utt_id = -1;
1534
1535                 ecore_timer_add(0, __tts_notify_error, client->tts);
1536                 return;
1537         }
1538
1539         client->before_state = client->current_state;
1540         client->current_state = TTS_STATE_PLAYING;
1541
1542         if (NULL != client->state_changed_cb) {
1543                 tts_client_use_callback(client);
1544                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1545                 tts_client_not_use_callback(client);
1546                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1547         }
1548
1549         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1550
1551         return;
1552 }
1553
1554 int tts_play_async(tts_h tts)
1555 {
1556         if (0 != __tts_get_feature_enabled()) {
1557                 return TTS_ERROR_NOT_SUPPORTED;
1558         }
1559
1560         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Play tts");
1561
1562         if (NULL == tts) {
1563                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1564                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1565                 return TTS_ERROR_INVALID_PARAMETER;
1566         }
1567
1568         tts_client_s* client = tts_client_get(tts);
1569
1570         if (NULL == client) {
1571                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1572                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1573                 return TTS_ERROR_INVALID_PARAMETER;
1574         }
1575
1576         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1577                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1578                 return TTS_ERROR_INVALID_STATE;
1579         }
1580
1581         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1582                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1583                 return TTS_ERROR_INVALID_STATE;
1584         }
1585
1586         if (true == client->credential_needed && NULL == client->credential) {
1587                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1588                 return TTS_ERROR_PERMISSION_DENIED;
1589         }
1590
1591         ecore_main_loop_thread_safe_call_async(__tts_play_async, (void*)tts);
1592
1593         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1594
1595         return TTS_ERROR_NONE;
1596 }
1597 //LCOV_EXCL_STOP
1598 int tts_play(tts_h tts)
1599 {
1600         if (0 != __tts_get_feature_enabled()) {
1601                 return TTS_ERROR_NOT_SUPPORTED;
1602         }
1603
1604         SLOG(LOG_INFO, TAG_TTSC, "@@@ Play tts");
1605
1606         if (NULL == tts) {
1607                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1608                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1609                 return TTS_ERROR_INVALID_PARAMETER;
1610         }
1611
1612         tts_client_s* client = tts_client_get(tts);
1613
1614         if (NULL == client) {
1615                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1616                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1617                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1618                 return TTS_ERROR_INVALID_PARAMETER;
1619         }
1620
1621         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1622                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1623                 return TTS_ERROR_INVALID_STATE;
1624         }
1625
1626         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1627                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1628                 return TTS_ERROR_INVALID_STATE;
1629         }
1630
1631         if (true == client->credential_needed && NULL == client->credential) {
1632                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1633                 return TTS_ERROR_PERMISSION_DENIED;
1634         }
1635
1636         int ret = -1;
1637         int count = 0;
1638         bool is_prepared = false;
1639         while (0 != ret) {
1640                 ret = tts_dbus_request_play(client->uid, client->credential);
1641                 if (0 != ret) {
1642                         //LCOV_EXCL_START
1643                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1644                                 client->current_state = TTS_STATE_CREATED;
1645                                 if (0 == tts_prepare_sync(tts)) {
1646                                         is_prepared = true;
1647                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1648                                 }
1649                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1650                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1651                                 return ret;
1652                         } else {
1653                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1654                                 usleep(10000);
1655                                 count++;
1656                                 if (TTS_RETRY_COUNT == count) {
1657                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1658                                         return ret;
1659                                 }
1660                         }
1661                         //LCOV_EXCL_STOP
1662                 }
1663         }
1664
1665         client->before_state = client->current_state;
1666         client->current_state = TTS_STATE_PLAYING;
1667
1668         if (NULL != client->state_changed_cb) {
1669                 tts_client_use_callback(client);
1670                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1671                 tts_client_not_use_callback(client);
1672                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1673         }
1674
1675         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1676
1677         return TTS_ERROR_NONE;
1678 }
1679 //LCOV_EXCL_START
1680 static void __tts_stop_async(void *data)
1681 {
1682         tts_h tts = (tts_h)data;
1683         tts_client_s* client = tts_client_get(tts);
1684
1685         /* check handle */
1686         if (NULL == client) {
1687                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1688                 return;
1689         }
1690
1691         int ret = -1;
1692         int count = 0;
1693         bool is_prepared = false;
1694         while (0 != ret) {
1695                 ret = tts_dbus_request_stop(client->uid);
1696                 if (0 != ret) {
1697                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1698                                 client->current_state = TTS_STATE_CREATED;
1699                                 if (0 == tts_prepare_sync(tts)) {
1700                                         is_prepared = true;
1701                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1702                                 }
1703                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1704                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1705                                 break;
1706                         } else {
1707                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1708                                 usleep(10000);
1709                                 count++;
1710                                 if (TTS_RETRY_COUNT == count) {
1711                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1712                                         break;
1713                                 }
1714                         }
1715                 }
1716         }
1717
1718         if (0 != ret) {
1719                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to stop tts : %s", __tts_get_error_code(ret));
1720
1721                 client->reason = ret;
1722                 client->utt_id = -1;
1723
1724                 ecore_timer_add(0, __tts_notify_error, client->tts);
1725                 return;
1726         }
1727
1728         client->before_state = client->current_state;
1729         client->current_state = TTS_STATE_READY;
1730
1731         if (NULL != client->state_changed_cb) {
1732                 tts_client_use_callback(client);
1733                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1734                 tts_client_not_use_callback(client);
1735                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1736         }
1737
1738         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1739
1740         return;
1741 }
1742
1743 int tts_stop_aync(tts_h tts)
1744 {
1745         if (0 != __tts_get_feature_enabled()) {
1746                 return TTS_ERROR_NOT_SUPPORTED;
1747         }
1748
1749         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Stop tts");
1750
1751         if (NULL == tts) {
1752                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1753                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1754                 return TTS_ERROR_INVALID_PARAMETER;
1755         }
1756
1757         tts_client_s* client = tts_client_get(tts);
1758
1759         if (NULL == client) {
1760                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1761                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1762                 return TTS_ERROR_INVALID_PARAMETER;
1763         }
1764
1765         if (TTS_STATE_CREATED == client->current_state) {
1766                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1767                 return TTS_ERROR_INVALID_STATE;
1768         }
1769
1770         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1771                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1772                 return TTS_ERROR_INVALID_STATE;
1773         }
1774
1775         ecore_main_loop_thread_safe_call_async(__tts_stop_async, (void*)tts);
1776
1777         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1778
1779         return TTS_ERROR_NONE;
1780 }
1781 //LCOV_EXCL_STOP
1782 int tts_stop(tts_h tts)
1783 {
1784         if (0 != __tts_get_feature_enabled()) {
1785                 return TTS_ERROR_NOT_SUPPORTED;
1786         }
1787
1788         SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop tts");
1789
1790         if (NULL == tts) {
1791                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1792                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1793                 return TTS_ERROR_INVALID_PARAMETER;
1794         }
1795
1796         tts_client_s* client = tts_client_get(tts);
1797
1798         if (NULL == client) {
1799                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1800                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1801                 return TTS_ERROR_INVALID_PARAMETER;
1802         }
1803
1804         if (TTS_STATE_CREATED == client->current_state) {
1805                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1806                 return TTS_ERROR_INVALID_STATE;
1807         }
1808
1809         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1810                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1811                 return TTS_ERROR_INVALID_STATE;
1812         }
1813
1814         int ret = -1;
1815         int count = 0;
1816         bool is_prepared = false;
1817         while (0 != ret) {
1818                 ret = tts_dbus_request_stop(client->uid);
1819                 if (0 != ret) {
1820                         //LCOV_EXCL_START
1821                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1822                                 client->current_state = TTS_STATE_CREATED;
1823                                 if (0 == tts_prepare_sync(tts)) {
1824                                         is_prepared = true;
1825                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1826                                 }
1827                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1828                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1829                                 return ret;
1830                         } else {
1831                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1832                                 usleep(10000);
1833                                 count++;
1834                                 if (TTS_RETRY_COUNT == count) {
1835                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1836                                         return ret;
1837                                 }
1838                         }
1839                         //LCOV_EXCL_STOP
1840                 }
1841         }
1842
1843         client->before_state = client->current_state;
1844         client->current_state = TTS_STATE_READY;
1845
1846         if (NULL != client->state_changed_cb) {
1847                 tts_client_use_callback(client);
1848                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1849                 tts_client_not_use_callback(client);
1850                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1851         }
1852
1853         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1854
1855         return TTS_ERROR_NONE;
1856 }
1857 //LCOV_EXCL_START
1858 static void __tts_pause_async(void *data)
1859 {
1860         tts_h tts = (tts_h)data;
1861         tts_client_s* client = tts_client_get(tts);
1862
1863         /* check handle */
1864         if (NULL == client) {
1865                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1866                 return;
1867         }
1868
1869         int ret = -1;
1870         int count = 0;
1871         bool is_prepared = false;
1872         while (0 != ret) {
1873                 ret = tts_dbus_request_pause(client->uid);
1874                 if (0 != ret) {
1875                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1876                                 client->current_state = TTS_STATE_CREATED;
1877                                 if (0 == tts_prepare_sync(tts)) {
1878                                         is_prepared = true;
1879                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1880                                 }
1881                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1882                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1883                                 break;
1884                         } else {
1885                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
1886                                 usleep(10000);
1887                                 count++;
1888                                 if (TTS_RETRY_COUNT == count) {
1889                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1890                                         break;
1891                                 }
1892                         }
1893                 }
1894         }
1895
1896         if (0 != ret) {
1897                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to pause tts : %s", __tts_get_error_code(ret));
1898
1899                 client->reason = ret;
1900                 client->utt_id = -1;
1901
1902                 ecore_timer_add(0, __tts_notify_error, client->tts);
1903                 return;
1904         }
1905
1906         client->before_state = client->current_state;
1907         client->current_state = TTS_STATE_PAUSED;
1908
1909         if (NULL != client->state_changed_cb) {
1910                 tts_client_use_callback(client);
1911                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1912                 tts_client_not_use_callback(client);
1913                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1914         }
1915
1916         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1917
1918         return;
1919 }
1920
1921 int tts_pause_async(tts_h tts)
1922 {
1923         if (0 != __tts_get_feature_enabled()) {
1924                 return TTS_ERROR_NOT_SUPPORTED;
1925         }
1926
1927         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Pause tts");
1928
1929         if (NULL == tts) {
1930                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1931                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
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] A handle is not valid");
1939                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1940                 return TTS_ERROR_INVALID_PARAMETER;
1941         }
1942
1943         if (TTS_STATE_PLAYING != client->current_state) {
1944                 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1945                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1946                 return TTS_ERROR_INVALID_STATE;
1947         }
1948
1949         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1950                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1951                 return TTS_ERROR_INVALID_STATE;
1952         }
1953
1954         ecore_main_loop_thread_safe_call_async(__tts_pause_async, (void*)tts);
1955
1956         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1957
1958         return TTS_ERROR_NONE;
1959 }
1960 //LCOV_EXCL_STOP
1961 int tts_pause(tts_h tts)
1962 {
1963         if (0 != __tts_get_feature_enabled()) {
1964                 return TTS_ERROR_NOT_SUPPORTED;
1965         }
1966
1967         SLOG(LOG_INFO, TAG_TTSC, "@@@ Pause tts");
1968
1969         if (NULL == tts) {
1970                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1971                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1972                 return TTS_ERROR_INVALID_PARAMETER;
1973         }
1974
1975         tts_client_s* client = tts_client_get(tts);
1976
1977         if (NULL == client) {
1978                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1979                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1980                 return TTS_ERROR_INVALID_PARAMETER;
1981         }
1982
1983         if (TTS_STATE_PLAYING != client->current_state) {
1984                 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1985                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1986                 return TTS_ERROR_INVALID_STATE;
1987         }
1988
1989         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1990                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1991                 return TTS_ERROR_INVALID_STATE;
1992         }
1993
1994         int ret = -1;
1995         int count = 0;
1996         bool is_prepared = false;
1997         while (0 != ret) {
1998                 ret = tts_dbus_request_pause(client->uid);
1999                 if (0 != ret) {
2000                         //LCOV_EXCL_START
2001                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2002                                 client->current_state = TTS_STATE_CREATED;
2003                                 if (0 == tts_prepare_sync(tts)) {
2004                                         is_prepared = true;
2005                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2006                                 }
2007                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2008                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2009                                 return ret;
2010                         } else {
2011                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
2012                                 usleep(10000);
2013                                 count++;
2014                                 if (TTS_RETRY_COUNT == count) {
2015                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2016                                         return ret;
2017                                 }
2018                         }
2019                         //LCOV_EXCL_STOP
2020                 }
2021         }
2022
2023         client->before_state = client->current_state;
2024         client->current_state = TTS_STATE_PAUSED;
2025
2026         if (NULL != client->state_changed_cb) {
2027                 tts_client_use_callback(client);
2028                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2029                 tts_client_not_use_callback(client);
2030                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
2031         }
2032
2033         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2034
2035         return TTS_ERROR_NONE;
2036 }
2037
2038 int tts_set_private_data(tts_h tts, const char* key, const char* data)
2039 {
2040         if (0 != __tts_get_feature_enabled()) {
2041                 return TTS_ERROR_NOT_SUPPORTED;
2042         }
2043
2044         SLOG(LOG_INFO, TAG_TTSC, "@@@ Set private data, key(%s), data(%s)", key, data);
2045
2046         if (NULL == tts) {
2047                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
2048                 return TTS_ERROR_INVALID_PARAMETER;
2049         }
2050
2051         if (NULL == key || NULL == data) {
2052                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
2053                 return TTS_ERROR_INVALID_PARAMETER;
2054         }
2055
2056         tts_client_s* client = tts_client_get(tts);
2057
2058         if (NULL == client) {
2059                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
2060                 return TTS_ERROR_INVALID_PARAMETER;
2061         }
2062
2063         if (TTS_STATE_READY != client->current_state) {
2064                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
2065                 return TTS_ERROR_INVALID_STATE;
2066         }
2067
2068         if (true != client->internal && (0 == strcmp(key, "EnableServerTTS") || 0 == strcmp(key, "DisableServerTTS"))) {
2069                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] This is not an internal app");
2070                 return TTS_ERROR_INVALID_PARAMETER;
2071         }
2072
2073         int ret = -1;
2074         int count = 0;
2075         bool is_prepared = false;
2076         while (0 != ret) {
2077                 ret = tts_dbus_request_set_private_data(client->uid, key, data);
2078                 if (0 != ret) {
2079                         //LCOV_EXCL_START
2080                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2081                                 client->current_state = TTS_STATE_CREATED;
2082                                 if (0 == tts_prepare_sync(tts)) {
2083                                         is_prepared = true;
2084                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2085                                 }
2086                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2087                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2088                                 return ret;
2089                         } else {
2090                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
2091                                 usleep(10000);
2092                                 count++;
2093                                 if (TTS_RETRY_COUNT == count) {
2094                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2095                                         return ret;
2096                                 }
2097                         }
2098                         //LCOV_EXCL_STOP
2099                 }
2100         }
2101
2102         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2103
2104         return 0;
2105 }
2106
2107 int tts_get_private_data(tts_h tts, const char* key, char** data)
2108 {
2109         if (0 != __tts_get_feature_enabled()) {
2110                 return TTS_ERROR_NOT_SUPPORTED;
2111         }
2112
2113         SLOG(LOG_INFO, TAG_TTSC, "@@@ Get private data, key(%s)", key);
2114
2115         if (NULL == tts) {
2116                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
2117                 return TTS_ERROR_INVALID_PARAMETER;
2118         }
2119
2120         if (NULL == key || NULL == data) {
2121                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
2122                 return TTS_ERROR_INVALID_PARAMETER;
2123         }
2124
2125         tts_client_s* client = tts_client_get(tts);
2126
2127         if (NULL == client) {
2128                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
2129                 return TTS_ERROR_INVALID_PARAMETER;
2130         }
2131
2132         if (TTS_STATE_READY != client->current_state) {
2133                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
2134                 return TTS_ERROR_INVALID_STATE;
2135         }
2136
2137         int ret = -1;
2138         int count = 0;
2139         bool is_prepared = false;
2140         while (0 != ret) {
2141                 ret = tts_dbus_request_get_private_data(client->uid, key, data);
2142                 if (0 != ret) {
2143                         //LCOV_EXCL_START
2144                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2145                                 client->current_state = TTS_STATE_CREATED;
2146                                 if (0 == tts_prepare_sync(tts)) {
2147                                         is_prepared = true;
2148                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2149                                 }
2150                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2151                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2152                                 return ret;
2153                         } else {
2154                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
2155                                 usleep(10000);
2156                                 count++;
2157                                 if (TTS_RETRY_COUNT == count) {
2158                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2159                                         return ret;
2160                                 }
2161                         }
2162                         //LCOV_EXCL_STOP
2163                 }
2164         }
2165
2166         if (0 == strncmp(*data, "NULL", strlen(*data))) {
2167                 free(*data);
2168                 *data = NULL;
2169         }
2170
2171         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2172
2173         return 0;
2174 }
2175
2176 //LCOV_EXCL_START
2177 static Eina_Bool __tts_notify_error(void *data)
2178 {
2179         tts_h tts = (tts_h)data;
2180
2181         tts_client_s* client = tts_client_get(tts);
2182
2183         /* check handle */
2184         if (NULL == client) {
2185                 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify error msg : A handle is not valid");
2186                 return EINA_FALSE;
2187         }
2188
2189         SLOG(LOG_DEBUG, TAG_TTSC, "Error data : uttid(%d) reason(%s)", client->utt_id, __tts_get_error_code(client->reason));
2190
2191         if (NULL != client->error_cb) {
2192                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of error");
2193                 tts_client_use_callback(client);
2194                 g_err_callback_status = true;
2195                 client->error_cb(client->tts, client->utt_id, client->reason, client->error_user_data);
2196                 g_err_callback_status = false;
2197                 tts_client_not_use_callback(client);
2198         } else {
2199                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2200         }
2201
2202         return EINA_FALSE;
2203 }
2204
2205 static void __start_reprepare_thread(void* data, Ecore_Thread* thread)
2206 {
2207         SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] start reprepare thread. engine update status(%d)", g_engine_update_status);
2208
2209         tts_client_s* temp = (tts_client_s*)data;
2210         if (NULL == temp) {
2211                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] data is null");
2212                 return ;
2213         }
2214
2215         int cnt = 0;
2216         while (!g_engine_update_status && (cnt < 10)) {
2217                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] wait for starting update");
2218                 usleep(50000);
2219                 cnt++;
2220         }
2221
2222         SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] update status(%d)", g_engine_update_status);
2223
2224         while (g_engine_update_status && (NULL != g_pkgmgr)) {
2225 //              SLOG(LOG_WARN, TAG_TTSC, "[WARNING] wait for finishing update");
2226                 usleep(200000);
2227         }
2228
2229         SLOG(LOG_INFO, TAG_TTSC, "[INFO] finish updating. request to prepare");
2230
2231         if (0 != tts_prepare(temp->tts)) {
2232                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare");
2233         }
2234
2235         return ;
2236 }
2237
2238 static void __end_reprepare_thread(void* data, Ecore_Thread* thread)
2239 {
2240         SLOG(LOG_INFO, TAG_TTSC, "[INFO] end reprepare thread");
2241 }
2242
2243 int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
2244 {
2245         if (-1 == uid) {
2246                 GList* client_list = NULL;
2247                 client_list = tts_client_get_client_list();
2248
2249                 GList *iter = NULL;
2250                 tts_client_s *data = NULL;
2251
2252                 if (g_list_length(client_list) > 0) {
2253                         /* Get a first item */
2254                         iter = g_list_first(client_list);
2255
2256                         while (NULL != iter) {
2257                                 data = iter->data;
2258
2259                                 data->utt_id = utt_id;
2260                                 data->reason = reason;
2261                                 if (NULL != data->err_msg) {
2262                                         free(data->err_msg);
2263                                         data->err_msg = NULL;
2264                                 }
2265                                 if (NULL != err_msg)
2266                                         data->err_msg = strdup(err_msg);
2267
2268                                 /* call callback function */
2269                                 if (NULL != data->error_cb) {
2270                                         ecore_timer_add(0, __tts_notify_error, data->tts);
2271                                 } else {
2272                                         SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2273                                 }
2274
2275                                 if (TTS_ERROR_SERVICE_RESET == reason) {
2276                                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
2277
2278                                         data->current_state = TTS_STATE_CREATED;
2279                                         data->reason = 0;
2280                                         ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, data);
2281                                 }
2282
2283                                 /* Next item */
2284                                 iter = g_list_next(iter);
2285                         }
2286                 }
2287         } else {
2288                 tts_client_s* client = tts_client_get_by_uid(uid);
2289
2290                 if (NULL == client) {
2291                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2292                         return TTS_ERROR_INVALID_PARAMETER;
2293                 }
2294
2295                 client->utt_id = utt_id;
2296                 client->reason = reason;
2297                 if (NULL != client->err_msg) {
2298                         free(client->err_msg);
2299                         client->err_msg = NULL;
2300                 }
2301                 if (NULL != err_msg)
2302                         client->err_msg = strdup(err_msg);
2303
2304                 /* call callback function */
2305                 if (NULL != client->error_cb) {
2306                         ecore_timer_add(0, __tts_notify_error, client->tts);
2307                 } else {
2308                         SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2309                 }
2310
2311                 if (TTS_ERROR_SERVICE_RESET == reason) {
2312                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
2313
2314                         client->current_state = TTS_STATE_CREATED;
2315                         client->reason = 0;
2316                         ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, client);
2317                 }
2318         }
2319
2320         return 0;
2321 }
2322
2323 static Eina_Bool __tts_notify_state_changed(void *data)
2324 {
2325         tts_h tts = (tts_h)data;
2326
2327         tts_client_s* client = tts_client_get(tts);
2328
2329         /* check handle */
2330         if (NULL == client) {
2331                 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify state changed : A handle is not valid");
2332                 return EINA_FALSE;
2333         }
2334
2335         if (NULL != client->state_changed_cb) {
2336                 tts_client_use_callback(client);
2337                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2338                 tts_client_not_use_callback(client);
2339                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called : pre(%d) cur(%d)", client->before_state, client->current_state);
2340         } else {
2341                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
2342         }
2343
2344         return EINA_FALSE;
2345 }
2346
2347 int __tts_cb_set_state(int uid, int state)
2348 {
2349         tts_client_s* client = tts_client_get_by_uid(uid);
2350         if (NULL == client) {
2351                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] The handle is not valid");
2352                 return -1;
2353         }
2354
2355         tts_state_e state_from_daemon = (tts_state_e)state;
2356
2357         if (client->current_state == state_from_daemon) {
2358                 SLOG(LOG_DEBUG, TAG_TTSC, "Current state has already been %d", client->current_state);
2359                 return 0;
2360         }
2361
2362         if (NULL != client->state_changed_cb) {
2363                 if (NULL != g_check_state_timer) {
2364                         ecore_timer_del(g_check_state_timer);
2365                         g_check_state_timer = NULL;
2366                 }
2367                 g_check_state_timer = ecore_timer_add(0, __tts_notify_state_changed, client->tts);
2368         } else {
2369                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
2370         }
2371
2372         client->before_state = client->current_state;
2373         client->current_state = state_from_daemon;
2374
2375         return 0;
2376 }
2377 //LCOV_EXCL_STOP
2378
2379 int __tts_cb_utt_started(int uid, int utt_id)
2380 {
2381         tts_client_s* client = tts_client_get_by_uid(uid);
2382
2383         if (NULL == client) {
2384                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2385                 return TTS_ERROR_INVALID_PARAMETER;
2386         }
2387
2388         SLOG(LOG_INFO, TAG_TTSC, "utterance started : utt id(%d) ", utt_id);
2389
2390         client->utt_id = utt_id;
2391
2392         /* call callback function */
2393         if (NULL != client->utt_started_cb) {
2394                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance started ");
2395                 tts_client_use_callback(client);
2396                 client->utt_started_cb(client->tts, client->utt_id, client->utt_started_user_data);
2397                 tts_client_not_use_callback(client);
2398         } else {
2399                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance started ");
2400         }
2401
2402         return 0;
2403 }
2404
2405 int __tts_cb_utt_completed(int uid, int utt_id)
2406 {
2407         tts_client_s* client = tts_client_get_by_uid(uid);
2408
2409         if (NULL == client) {
2410                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2411                 return TTS_ERROR_INVALID_PARAMETER;
2412         }
2413
2414         SLOG(LOG_INFO, TAG_TTSC, "utterance completed : uttid(%d) ", utt_id);
2415
2416         client->utt_id = utt_id;
2417
2418         /* call callback function */
2419         if (NULL != client->utt_completeted_cb) {
2420                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance completed ");
2421                 tts_client_use_callback(client);
2422                 client->utt_completeted_cb(client->tts, client->utt_id, client->utt_completed_user_data);
2423                 tts_client_not_use_callback(client);
2424         } else {
2425                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance completed ");
2426         }
2427
2428         return 0;
2429 }
2430
2431 int tts_set_state_changed_cb(tts_h tts, tts_state_changed_cb callback, void* user_data)
2432 {
2433         if (0 != __tts_get_feature_enabled()) {
2434                 return TTS_ERROR_NOT_SUPPORTED;
2435         }
2436
2437         if (NULL == tts || NULL == callback) {
2438                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Input parameter is null");
2439                 return TTS_ERROR_INVALID_PARAMETER;
2440         }
2441
2442         tts_client_s* client = tts_client_get(tts);
2443
2444         if (NULL == client) {
2445                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : A handle is not valid");
2446                 return TTS_ERROR_INVALID_PARAMETER;
2447         }
2448
2449         if (TTS_STATE_CREATED != client->current_state) {
2450                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Current state is not 'Created'.");
2451                 return TTS_ERROR_INVALID_STATE;
2452         }
2453
2454         client->state_changed_cb = callback;
2455         client->state_changed_user_data = user_data;
2456
2457         SLOG(LOG_INFO, TAG_TTSC, "[SUCCESS] Set state changed cb");
2458
2459         return 0;
2460 }
2461
2462 int tts_unset_state_changed_cb(tts_h tts)
2463 {
2464         if (0 != __tts_get_feature_enabled()) {
2465                 return TTS_ERROR_NOT_SUPPORTED;
2466         }
2467
2468         if (NULL == tts) {
2469                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Input parameter is null");
2470                 return TTS_ERROR_INVALID_PARAMETER;
2471         }
2472
2473         tts_client_s* client = tts_client_get(tts);
2474
2475         if (NULL == client) {
2476                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : A handle is not valid");
2477                 return TTS_ERROR_INVALID_PARAMETER;
2478         }
2479
2480         if (TTS_STATE_CREATED != client->current_state) {
2481                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Current state is not 'Created'.");
2482                 return TTS_ERROR_INVALID_STATE;
2483         }
2484
2485         client->state_changed_cb = NULL;
2486         client->state_changed_user_data = NULL;
2487
2488         if (NULL != g_check_state_timer) {
2489                 ecore_timer_del(g_check_state_timer);
2490                 g_check_state_timer = NULL;
2491         }
2492
2493         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset state changed cb");
2494
2495         return 0;
2496 }
2497
2498 int tts_set_utterance_started_cb(tts_h tts, tts_utterance_started_cb callback, void* user_data)
2499 {
2500         if (0 != __tts_get_feature_enabled()) {
2501                 return TTS_ERROR_NOT_SUPPORTED;
2502         }
2503
2504         if (NULL == tts || NULL == callback) {
2505                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Input parameter is null");
2506                 return TTS_ERROR_INVALID_PARAMETER;
2507         }
2508
2509         tts_client_s* client = tts_client_get(tts);
2510
2511         if (NULL == client) {
2512                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : A handle is not valid");
2513                 return TTS_ERROR_INVALID_PARAMETER;
2514         }
2515
2516         if (TTS_STATE_CREATED != client->current_state) {
2517                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Current state is not 'Created'.");
2518                 return TTS_ERROR_INVALID_STATE;
2519         }
2520
2521         client->utt_started_cb = callback;
2522         client->utt_started_user_data = user_data;
2523
2524         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt started cb");
2525
2526         return 0;
2527 }
2528
2529 int tts_unset_utterance_started_cb(tts_h tts)
2530 {
2531         if (0 != __tts_get_feature_enabled()) {
2532                 return TTS_ERROR_NOT_SUPPORTED;
2533         }
2534
2535         if (NULL == tts) {
2536                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Input parameter is null");
2537                 return TTS_ERROR_INVALID_PARAMETER;
2538         }
2539
2540         tts_client_s* client = tts_client_get(tts);
2541
2542         if (NULL == client) {
2543                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : A handle is not valid");
2544                 return TTS_ERROR_INVALID_PARAMETER;
2545         }
2546
2547         if (TTS_STATE_CREATED != client->current_state) {
2548                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Current state is not 'Created'.");
2549                 return TTS_ERROR_INVALID_STATE;
2550         }
2551
2552         client->utt_started_cb = NULL;
2553         client->utt_started_user_data = NULL;
2554
2555         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt started cb");
2556
2557         return 0;
2558 }
2559
2560 int tts_set_utterance_completed_cb(tts_h tts, tts_utterance_completed_cb callback, void* user_data)
2561 {
2562         if (0 != __tts_get_feature_enabled()) {
2563                 return TTS_ERROR_NOT_SUPPORTED;
2564         }
2565
2566         if (NULL == tts || NULL == callback) {
2567                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Input parameter is null");
2568                 return TTS_ERROR_INVALID_PARAMETER;
2569         }
2570
2571         tts_client_s* client = tts_client_get(tts);
2572
2573         if (NULL == client) {
2574                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : A handle is not valid");
2575                 return TTS_ERROR_INVALID_PARAMETER;
2576         }
2577
2578         if (TTS_STATE_CREATED != client->current_state) {
2579                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Current state is not 'Created'.");
2580                 return TTS_ERROR_INVALID_STATE;
2581         }
2582
2583         client->utt_completeted_cb = callback;
2584         client->utt_completed_user_data = user_data;
2585
2586         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt completed cb");
2587
2588         return 0;
2589 }
2590
2591 int tts_unset_utterance_completed_cb(tts_h tts)
2592 {
2593         if (0 != __tts_get_feature_enabled()) {
2594                 return TTS_ERROR_NOT_SUPPORTED;
2595         }
2596
2597         if (NULL == tts) {
2598                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Input parameter is null");
2599                 return TTS_ERROR_INVALID_PARAMETER;
2600         }
2601
2602         tts_client_s* client = tts_client_get(tts);
2603
2604         if (NULL == client) {
2605                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : A handle is not valid");
2606                 return TTS_ERROR_INVALID_PARAMETER;
2607         }
2608
2609         if (TTS_STATE_CREATED != client->current_state) {
2610                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Current state is not 'Created'.");
2611                 return TTS_ERROR_INVALID_STATE;
2612         }
2613
2614         client->utt_completeted_cb = NULL;
2615         client->utt_completed_user_data = NULL;
2616
2617         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt completed cb");
2618         return 0;
2619 }
2620
2621 int tts_set_error_cb(tts_h tts, tts_error_cb callback, void* user_data)
2622 {
2623         if (0 != __tts_get_feature_enabled()) {
2624                 return TTS_ERROR_NOT_SUPPORTED;
2625         }
2626
2627         if (NULL == tts || NULL == callback) {
2628                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Input parameter is null");
2629                 return TTS_ERROR_INVALID_PARAMETER;
2630         }
2631
2632         tts_client_s* client = tts_client_get(tts);
2633
2634         if (NULL == client) {
2635                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : A handle is not valid");
2636                 return TTS_ERROR_INVALID_PARAMETER;
2637         }
2638
2639         if (TTS_STATE_CREATED != client->current_state) {
2640                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Current state is not 'Created'.");
2641                 return TTS_ERROR_INVALID_STATE;
2642         }
2643
2644         client->error_cb = callback;
2645         client->error_user_data = user_data;
2646
2647         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set error cb");
2648
2649         return 0;
2650 }
2651
2652 int tts_unset_error_cb(tts_h tts)
2653 {
2654         if (0 != __tts_get_feature_enabled()) {
2655                 return TTS_ERROR_NOT_SUPPORTED;
2656         }
2657
2658         if (NULL == tts) {
2659                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Input parameter is null");
2660                 return TTS_ERROR_INVALID_PARAMETER;
2661         }
2662
2663         tts_client_s* client = tts_client_get(tts);
2664
2665         if (NULL == client) {
2666                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : A handle is not valid");
2667                 return TTS_ERROR_INVALID_PARAMETER;
2668         }
2669
2670         if (TTS_STATE_CREATED != client->current_state) {
2671                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Current state is not 'Created'.");
2672                 return TTS_ERROR_INVALID_STATE;
2673         }
2674
2675         client->error_cb = NULL;
2676         client->error_user_data = NULL;
2677
2678         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset error cb");
2679
2680         return 0;
2681 }
2682
2683 int tts_set_default_voice_changed_cb(tts_h tts, tts_default_voice_changed_cb callback, void* user_data)
2684 {
2685         if (0 != __tts_get_feature_enabled()) {
2686                 return TTS_ERROR_NOT_SUPPORTED;
2687         }
2688
2689         if (NULL == tts || NULL == callback) {
2690                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Input parameter is null");
2691                 return TTS_ERROR_INVALID_PARAMETER;
2692         }
2693
2694         tts_client_s* client = tts_client_get(tts);
2695
2696         if (NULL == client) {
2697                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : A handle is not valid");
2698                 return TTS_ERROR_INVALID_PARAMETER;
2699         }
2700
2701         if (TTS_STATE_CREATED != client->current_state) {
2702                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Current state is not 'Created'.");
2703                 return TTS_ERROR_INVALID_STATE;
2704         }
2705
2706         client->default_voice_changed_cb = callback;
2707         client->default_voice_changed_user_data = user_data;
2708
2709         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set default voice changed cb");
2710
2711         return 0;
2712 }
2713
2714 int tts_unset_default_voice_changed_cb(tts_h tts)
2715 {
2716         if (0 != __tts_get_feature_enabled()) {
2717                 return TTS_ERROR_NOT_SUPPORTED;
2718         }
2719
2720         if (NULL == tts) {
2721                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Input parameter is null");
2722                 return TTS_ERROR_INVALID_PARAMETER;
2723         }
2724
2725         tts_client_s* client = tts_client_get(tts);
2726
2727         if (NULL == client) {
2728                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : A handle is not valid");
2729                 return TTS_ERROR_INVALID_PARAMETER;
2730         }
2731
2732         if (TTS_STATE_CREATED != client->current_state) {
2733                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Current state is not 'Created'.");
2734                 return TTS_ERROR_INVALID_STATE;
2735         }
2736
2737         client->default_voice_changed_cb = NULL;
2738         client->default_voice_changed_user_data = NULL;
2739
2740         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset default voice changed cb");
2741
2742         return 0;
2743 }
2744
2745 int tts_set_engine_changed_cb(tts_h tts, tts_engine_changed_cb callback, void* user_data)
2746 {
2747         if (0 != __tts_get_feature_enabled()) {
2748                 return TTS_ERROR_NOT_SUPPORTED;
2749         }
2750
2751         if (NULL == tts || NULL == callback) {
2752                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Input parameter is null");
2753                 return TTS_ERROR_INVALID_PARAMETER;
2754         }
2755
2756         tts_client_s* client = tts_client_get(tts);
2757
2758         if (NULL == client) {
2759                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : A handle is not valid");
2760                 return TTS_ERROR_INVALID_PARAMETER;
2761         }
2762
2763         if (TTS_STATE_CREATED != client->current_state) {
2764                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Current state is not 'Created'.");
2765                 return TTS_ERROR_INVALID_STATE;
2766         }
2767
2768         client->engine_changed_cb = callback;
2769         client->engine_changed_user_data = user_data;
2770
2771         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set engine changed cb");
2772
2773         return 0;
2774 }
2775
2776 int tts_unset_engine_changed_cb(tts_h tts)
2777 {
2778         if (0 != __tts_get_feature_enabled()) {
2779                 return TTS_ERROR_NOT_SUPPORTED;
2780         }
2781
2782         if (NULL == tts) {
2783                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Input parameter is null");
2784                 return TTS_ERROR_INVALID_PARAMETER;
2785         }
2786
2787         tts_client_s* client = tts_client_get(tts);
2788
2789         if (NULL == client) {
2790                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : A handle is not valid");
2791                 return TTS_ERROR_INVALID_PARAMETER;
2792         }
2793
2794         if (TTS_STATE_CREATED != client->current_state) {
2795                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Current state is not 'Created'.");
2796                 return TTS_ERROR_INVALID_STATE;
2797         }
2798
2799         client->engine_changed_cb = NULL;
2800         client->engine_changed_user_data = NULL;
2801
2802         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset engine changed cb");
2803
2804         return 0;
2805 }
2806
2807 //LCOV_EXCL_START
2808 int tts_add_pcm(tts_h tts, int event, const void* data, unsigned int data_size, int audio_type, int rate)
2809 {
2810         if (0 != __tts_get_feature_enabled()) {
2811                 return TTS_ERROR_NOT_SUPPORTED;
2812         }
2813
2814         SLOG(LOG_INFO, TAG_TTSC, "@@@ Add pcm tts");
2815
2816         if (NULL == tts) {
2817                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2818                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2819                 return TTS_ERROR_INVALID_PARAMETER;
2820         }
2821
2822         tts_client_s* client = tts_client_get(tts);
2823
2824         if (NULL == client) {
2825                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2826                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2827                 return TTS_ERROR_INVALID_PARAMETER;
2828         }
2829
2830         if (TTS_STATE_CREATED == client->current_state) {
2831                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2832                 return TTS_ERROR_INVALID_STATE;
2833         }
2834
2835         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2836                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2837                 return TTS_ERROR_INVALID_STATE;
2838         }
2839
2840         int ret = -1;
2841         int count = 0;
2842         bool is_prepared = false;
2843         while (0 != ret) {
2844                 ret = tts_dbus_request_add_pcm(client->uid, event, data, data_size, audio_type, rate);
2845                 if (0 != ret) {
2846                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2847                                 client->current_state = TTS_STATE_CREATED;
2848                                 if (0 == tts_prepare_sync(tts)) {
2849                                         is_prepared = true;
2850                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2851                                 }
2852                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2853                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2854                                 return ret;
2855                         } else {
2856                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add pcm : %s", __tts_get_error_code(ret));
2857                                 usleep(10000);
2858                                 count++;
2859                                 if (TTS_RETRY_COUNT == count) {
2860                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2861                                         return ret;
2862                                 }
2863                         }
2864                 }
2865         }
2866
2867         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2868
2869         return TTS_ERROR_NONE;
2870 }
2871
2872 int tts_play_pcm(tts_h tts)
2873 {
2874         if (0 != __tts_get_feature_enabled()) {
2875                 return TTS_ERROR_NOT_SUPPORTED;
2876         }
2877
2878         SLOG(LOG_INFO, TAG_TTSC, "@@@ Play pcm tts");
2879
2880         if (NULL == tts) {
2881                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2882                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2883                 return TTS_ERROR_INVALID_PARAMETER;
2884         }
2885
2886         tts_client_s* client = tts_client_get(tts);
2887
2888         if (NULL == client) {
2889                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2890                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2891                 return TTS_ERROR_INVALID_PARAMETER;
2892         }
2893
2894         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
2895                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2896                 return TTS_ERROR_INVALID_STATE;
2897         }
2898
2899         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2900                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2901                 return TTS_ERROR_INVALID_STATE;
2902         }
2903
2904         int ret = -1;
2905         int count = 0;
2906         bool is_prepared = false;
2907         while (0 != ret) {
2908                 ret = tts_dbus_request_play_pcm(client->uid);
2909                 if (0 != ret) {
2910                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2911                                 client->current_state = TTS_STATE_CREATED;
2912                                 if (0 == tts_prepare_sync(tts)) {
2913                                         is_prepared = true;
2914                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2915                                 }
2916                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2917                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2918                                 return ret;
2919                         } else {
2920                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play pcm : %s", __tts_get_error_code(ret));
2921                                 usleep(10000);
2922                                 count++;
2923                                 if (TTS_RETRY_COUNT == count) {
2924                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2925                                         return ret;
2926                                 }
2927                         }
2928                 }
2929         }
2930
2931         client->before_state = client->current_state;
2932         client->current_state = TTS_STATE_PLAYING;
2933
2934         if (NULL != client->state_changed_cb) {
2935                 tts_client_use_callback(client);
2936                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2937                 tts_client_not_use_callback(client);
2938                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
2939         }
2940
2941         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2942
2943         return TTS_ERROR_NONE;
2944 }
2945
2946 int tts_stop_pcm(tts_h tts)
2947 {
2948         if (0 != __tts_get_feature_enabled()) {
2949                 return TTS_ERROR_NOT_SUPPORTED;
2950         }
2951
2952         SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop pcm tts");
2953
2954         if (NULL == tts) {
2955                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2956                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2957                 return TTS_ERROR_INVALID_PARAMETER;
2958         }
2959
2960         tts_client_s* client = tts_client_get(tts);
2961
2962         if (NULL == client) {
2963                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2964                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2965                 return TTS_ERROR_INVALID_PARAMETER;
2966         }
2967
2968         if (TTS_STATE_CREATED == client->current_state) {
2969                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2970                 return TTS_ERROR_INVALID_STATE;
2971         }
2972
2973         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2974                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2975                 return TTS_ERROR_INVALID_STATE;
2976         }
2977
2978         int ret = -1;
2979         int count = 0;
2980         bool is_prepared = false;
2981         while (0 != ret) {
2982                 ret = tts_dbus_request_stop_pcm(client->uid);
2983                 if (0 != ret) {
2984                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2985                                 client->current_state = TTS_STATE_CREATED;
2986                                 if (0 == tts_prepare_sync(tts)) {
2987                                         is_prepared = true;
2988                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2989                                 }
2990                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2991                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2992                                 return ret;
2993                         } else {
2994                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop pcm : %s", __tts_get_error_code(ret));
2995                                 usleep(10000);
2996                                 count++;
2997                                 if (TTS_RETRY_COUNT == count) {
2998                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2999                                         return ret;
3000                                 }
3001                         }
3002                 }
3003         }
3004
3005         client->before_state = client->current_state;
3006         client->current_state = TTS_STATE_READY;
3007
3008         if (NULL != client->state_changed_cb) {
3009                 tts_client_use_callback(client);
3010                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
3011                 tts_client_not_use_callback(client);
3012                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
3013         }
3014
3015         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3016
3017         return TTS_ERROR_NONE;
3018 }
3019 //LCOV_EXCL_STOP
3020
3021 int tts_repeat(tts_h tts, char** text_repeat, int* utt_id)
3022 {
3023         if (0 != __tts_get_feature_enabled()) {
3024                 return TTS_ERROR_NOT_SUPPORTED;
3025         }
3026
3027         SLOG(LOG_INFO, TAG_TTSC, "@@@ Repeat TTS");
3028
3029         if (NULL == tts) {
3030                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
3031                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3032                 return TTS_ERROR_INVALID_PARAMETER;
3033         }
3034
3035         if (NULL == text_repeat || NULL == utt_id) {
3036                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null.");
3037                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3038                 return TTS_ERROR_INVALID_PARAMETER;
3039         }
3040
3041         *text_repeat = NULL;
3042         *utt_id = -1;
3043
3044         tts_client_s* client = tts_client_get(tts);
3045
3046         if (NULL == client) {
3047                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
3048                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3049                 return TTS_ERROR_INVALID_PARAMETER;
3050         }
3051
3052         if (TTS_STATE_READY != client->current_state) {
3053                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid. (%d)", client->current_state);
3054                 return TTS_ERROR_INVALID_STATE;
3055         }
3056
3057         /* Clear the legacy and Add texts to be played repeatedly */
3058         int ret = -1;
3059         ret = tts_stop(tts);
3060         if (TTS_ERROR_NONE != ret) {
3061                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to clear the legacy");
3062                 return ret;
3063         }
3064
3065         if (NULL != client->text_repeat) {
3066                 char* tmp_text = strdup(client->text_repeat);
3067                 char* tmp_lang = NULL;
3068                 if (NULL != g_language) {
3069                         tmp_lang = strdup(g_language);
3070                 }
3071                 ret = tts_add_text(tts, tmp_text, tmp_lang, g_voice_type, g_speed, utt_id);
3072                 if (TTS_ERROR_NONE != ret) {
3073                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to add texts for repetition.");
3074                         if (NULL != tmp_text) {
3075                                 free(tmp_text);
3076                                 tmp_text = NULL;
3077                         }
3078                         if (NULL != tmp_lang) {
3079                                 free(tmp_lang);
3080                                 tmp_lang = NULL;
3081                         }
3082                         return ret;
3083                 }
3084                 *text_repeat = strdup(client->text_repeat);
3085                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] text to repeat(%s), utt_id(%d)", (*text_repeat) ? *text_repeat : "NULL", *utt_id);
3086                 if (NULL != tmp_text) {
3087                         free(tmp_text);
3088                         tmp_text = NULL;
3089                 }
3090                 if (NULL != tmp_lang) {
3091                         free(tmp_lang);
3092                         tmp_lang = NULL;
3093                 }
3094         } else {
3095                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] There is no previous added texts. Please add texts");
3096                 return TTS_ERROR_OPERATION_FAILED;
3097         }
3098
3099         /* Play added texts */
3100         ret = tts_play(tts);
3101         if (TTS_ERROR_NONE != ret) {
3102                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play texts for repetition.");
3103                 if (NULL != *text_repeat) {
3104                         free(*text_repeat);
3105                         *text_repeat = NULL;
3106                 }
3107                 *utt_id = -1;
3108                 return ret;
3109         }
3110
3111         return TTS_ERROR_NONE;
3112 }