Add engine update checker into __send_hello
[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         /* check whether engine is updating or not */
903         if (g_engine_update_status) {
904                 SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] cannot prepare due to engine update");
905                 __tts_cb_error(-1, TTS_ERROR_SERVICE_RESET, -1, "Daemon Reset");
906                 g_hello_timer = NULL;
907                 return EINA_FALSE;
908         }
909
910         /* Send hello */
911         int ret = tts_dbus_request_hello(client->uid);
912         if (0 != ret)  {
913                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request hello !!"); //LCOV_EXCL_LINE
914         } else {
915                 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Send Hello");
916         }
917
918         g_retry_cnt++;
919         if (TTS_HELLO_RETRY_COUNT == g_retry_cnt) {
920                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Stop to send hello, retry count reaches the limit");
921                 g_retry_cnt = 0;
922                 g_hello_timer = NULL;
923                 return EINA_FALSE;
924         }
925
926         if (!g_hello_timer) {
927                 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
928                 g_hello_timer = ecore_timer_add(0.5, __send_hello, tts);
929                 return EINA_FALSE;
930         }
931         return EINA_TRUE;
932 }
933
934 int tts_prepare(tts_h tts)
935 {
936         if (0 != __tts_get_feature_enabled()) {
937                 return TTS_ERROR_NOT_SUPPORTED;
938         }
939
940         SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
941
942         tts_client_s* client = tts_client_get(tts);
943
944         /* check handle */
945         if (NULL == client) {
946                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
947                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
948                 return TTS_ERROR_INVALID_PARAMETER;
949         }
950
951         /* check state */
952         if (client->current_state != TTS_STATE_CREATED) {
953                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
954                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
955                 return TTS_ERROR_INVALID_STATE;
956         }
957
958         if (NULL == g_hello_timer) {
959                 SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
960                 g_retry_cnt = 0;
961                 ecore_thread_main_loop_begin();
962                 ecore_timer_add(0.0, __send_hello, (void*)tts);
963                 ecore_thread_main_loop_end();
964         }
965
966         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
967
968         return TTS_ERROR_NONE;
969 }
970
971 //LCOV_EXCL_START
972 int tts_prepare_sync(tts_h tts)
973 {
974         if (0 != __tts_get_feature_enabled()) {
975                 return TTS_ERROR_NOT_SUPPORTED;
976         }
977
978         SLOG(LOG_INFO, TAG_TTSC, "@@@ Prepare TTS");
979
980         tts_client_s* client = tts_client_get(tts);
981
982         /* check handle */
983         if (NULL == client) {
984                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
985                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
986                 return TTS_ERROR_INVALID_PARAMETER;
987         }
988
989         /* check state */
990         if (client->current_state != TTS_STATE_CREATED) {
991                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'CREATED'");
992                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
993                 return TTS_ERROR_INVALID_STATE;
994         }
995
996         int cnt = 0;
997         while (EINA_TRUE == __tts_connect_daemon((void*)tts) && TTS_CONNECTION_RETRY_COUNT > cnt) {
998                 cnt++;
999         }
1000
1001         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1002
1003         if (TTS_CONNECTION_RETRY_COUNT == cnt) {
1004                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to connect daemon");
1005                 return TTS_ERROR_OPERATION_FAILED;
1006         }
1007
1008         return TTS_ERROR_NONE;
1009 }
1010 //LCOV_EXCL_STOP
1011
1012 int tts_unprepare(tts_h tts)
1013 {
1014         if (0 != __tts_get_feature_enabled()) {
1015                 return TTS_ERROR_NOT_SUPPORTED;
1016         }
1017
1018         SLOG(LOG_INFO, TAG_TTSC, "@@@ Unprepare TTS");
1019
1020         tts_client_s* client = tts_client_get(tts);
1021
1022         /* check handle */
1023         if (NULL == client) {
1024                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not available");
1025                 return TTS_ERROR_INVALID_PARAMETER;
1026         }
1027
1028         /* check state */
1029         if (client->current_state != TTS_STATE_READY) {
1030                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid State: Current state is not 'READY'");
1031                 return TTS_ERROR_INVALID_STATE;
1032         }
1033
1034         int ret = -1;
1035         int count = 0;
1036         int screen_reader = -1;
1037
1038         ret = vconf_get_bool(TTS_ACCESSIBILITY_KEY, &screen_reader);
1039         if (0 != ret) {
1040                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get screen reader");
1041         } else {
1042                 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);
1043                 g_screen_reader = (bool)screen_reader;
1044         }
1045
1046         bool is_prepared = false;
1047         if (!(false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode)) {
1048                 do {
1049                         ret = tts_dbus_request_finalize(client->uid);
1050                         if (0 != ret) {
1051                                 //LCOV_EXCL_START
1052                                 if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1053                                         client->current_state = TTS_STATE_CREATED;
1054                                         if (0 == tts_prepare_sync(tts)) {
1055                                                 is_prepared = true;
1056                                                 SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1057                                         }
1058                                 } else if (TTS_ERROR_TIMED_OUT != ret) {
1059                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1060                                         break;
1061                                 } else {
1062                                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize : %s", __tts_get_error_code(ret));
1063                                         usleep(10000);
1064                                         count++;
1065                                         if (TTS_RETRY_COUNT == count) {
1066                                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1067                                                 break;
1068                                         }
1069                                 }
1070                                 //LCOV_EXCL_STOP
1071                         }
1072                 } while (0 != ret);
1073         } else {
1074                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Do not request finalize : g_sr(%d) mode(%d)", g_screen_reader, client->mode);
1075         }
1076
1077         client->before_state = client->current_state;
1078         client->current_state = TTS_STATE_CREATED;
1079
1080         if (NULL != client->state_changed_cb) {
1081                 tts_client_use_callback(client);
1082                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1083                 tts_client_not_use_callback(client);
1084                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1085         }
1086
1087         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1088
1089         return TTS_ERROR_NONE;
1090 }
1091
1092 bool __tts_supported_voice_cb(const char* engine_id, const char* language, int type, void* user_data)
1093 {
1094         tts_h tts = (tts_h)user_data;
1095
1096         tts_client_s* client = tts_client_get(tts);
1097         if (NULL == client) {
1098                 SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
1099                 return false;
1100         }
1101
1102         /* call callback function */
1103         if (NULL != client->supported_voice_cb) {
1104                 return client->supported_voice_cb(tts, language, type, client->supported_voice_user_data);
1105         } else {
1106                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of supported voice");
1107         }
1108
1109         return false;
1110 }
1111
1112 int tts_foreach_supported_voices(tts_h tts, tts_supported_voice_cb callback, void* user_data)
1113 {
1114         if (0 != __tts_get_feature_enabled()) {
1115                 return TTS_ERROR_NOT_SUPPORTED;
1116         }
1117
1118         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Foreach supported voices");
1119
1120         if (NULL == tts || NULL == callback) {
1121                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1122                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1123                 return TTS_ERROR_INVALID_PARAMETER;
1124         }
1125
1126         tts_client_s* client = tts_client_get(tts);
1127
1128         /* check handle */
1129         if (NULL == client) {
1130                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1131                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1132                 return TTS_ERROR_INVALID_PARAMETER;
1133         }
1134
1135         int ret = 0;
1136         char* current_engine = NULL;
1137         ret = tts_config_mgr_get_engine(&current_engine);
1138         if (0 != ret) {
1139                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get current engine : %d", ret);
1140                 return __tts_convert_config_error_code(ret);
1141         }
1142
1143         client->supported_voice_cb = callback;
1144         client->supported_voice_user_data = user_data;
1145
1146         ret = tts_config_mgr_get_voice_list(current_engine, __tts_supported_voice_cb, client->tts);
1147
1148         if (NULL != current_engine) {
1149                 free(current_engine);
1150                 current_engine = NULL;
1151         }
1152
1153         client->supported_voice_cb = NULL;
1154         client->supported_voice_user_data = NULL;
1155
1156         if (0 != ret) {
1157                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Result : %d", ret);
1158                 ret = TTS_ERROR_OPERATION_FAILED;
1159         }
1160
1161         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1162
1163         return ret;
1164 }
1165
1166 int tts_get_default_voice(tts_h tts, char** lang, int* vctype)
1167 {
1168         if (0 != __tts_get_feature_enabled()) {
1169                 return TTS_ERROR_NOT_SUPPORTED;
1170         }
1171
1172         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get default voice");
1173
1174         if (NULL == tts) {
1175                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1176                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1177                 return TTS_ERROR_INVALID_PARAMETER;
1178
1179         }
1180         tts_client_s* client = tts_client_get(tts);
1181
1182         if (NULL == client) {
1183                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1184                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1185                 return TTS_ERROR_INVALID_PARAMETER;
1186         }
1187
1188         /* Request call remote method */
1189         int ret = 0;
1190         ret = tts_config_mgr_get_voice(lang, vctype);
1191         if (0 != ret) {
1192                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %d", ret);
1193                 return __tts_convert_config_error_code(ret);
1194         } else {
1195                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Default language(%s), type(%d)", *lang, *vctype);
1196         }
1197
1198         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1199
1200         return ret;
1201 }
1202
1203 int tts_get_max_text_size(tts_h tts, unsigned int* size)
1204 {
1205         if (0 != __tts_get_feature_enabled()) {
1206                 return TTS_ERROR_NOT_SUPPORTED;
1207         }
1208
1209         if (NULL == tts || NULL == size) {
1210                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Input parameter is null");
1211                 return TTS_ERROR_INVALID_PARAMETER;
1212         }
1213
1214         tts_client_s* client = tts_client_get(tts);
1215
1216         if (NULL == client) {
1217                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : A handle is not valid");
1218                 return TTS_ERROR_INVALID_PARAMETER;
1219         }
1220
1221         if (TTS_STATE_READY != client->current_state) {
1222                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get max text count : Current state is NOT 'READY'.");
1223                 return TTS_ERROR_INVALID_STATE;
1224         }
1225
1226         if (0 != tts_config_mgr_get_max_text_size(size)) {
1227                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
1228                 return TTS_ERROR_INVALID_PARAMETER;
1229         }
1230
1231         g_max_text_size = (int)*size;
1232
1233         SLOG(LOG_DEBUG, TAG_TTSC, "Get max text count : %d", *size);
1234         return TTS_ERROR_NONE;
1235 }
1236
1237 int tts_get_state(tts_h tts, tts_state_e* state)
1238 {
1239         if (0 != __tts_get_feature_enabled()) {
1240                 return TTS_ERROR_NOT_SUPPORTED;
1241         }
1242
1243         if (NULL == tts || NULL == state) {
1244                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : Input parameter is null");
1245                 return TTS_ERROR_INVALID_PARAMETER;
1246         }
1247
1248         tts_client_s* client = tts_client_get(tts);
1249
1250         if (NULL == client) {
1251                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1252                 return TTS_ERROR_INVALID_PARAMETER;
1253         }
1254
1255         *state = client->current_state;
1256
1257         switch (*state) {
1258         case TTS_STATE_CREATED: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Created'");        break;
1259         case TTS_STATE_READY:   SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Ready'");          break;
1260         case TTS_STATE_PLAYING: SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Playing'");        break;
1261         case TTS_STATE_PAUSED:  SLOG(LOG_DEBUG, TAG_TTSC, "Current state is 'Paused'");         break;
1262         default:                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid value");             break;
1263         }
1264
1265         return TTS_ERROR_NONE;
1266 }
1267
1268 int tts_get_speed_range(tts_h tts, int* min, int* normal, int* max)
1269 {
1270         if (0 != __tts_get_feature_enabled()) {
1271                 return TTS_ERROR_NOT_SUPPORTED;
1272         }
1273
1274         if (NULL == tts || NULL == min || NULL == normal || NULL == max) {
1275                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1276                 return TTS_ERROR_INVALID_PARAMETER;
1277         }
1278
1279         tts_client_s* client = tts_client_get(tts);
1280
1281         if (NULL == client) {
1282                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1283                 return TTS_ERROR_INVALID_PARAMETER;
1284         }
1285
1286         *min = TTS_SPEED_MIN;
1287         *normal = TTS_SPEED_NORMAL;
1288         *max = TTS_SPEED_MAX;
1289
1290         return TTS_ERROR_NONE;
1291 }
1292
1293 int tts_get_error_message(tts_h tts, char** err_msg)
1294 {
1295         if (0 != __tts_get_feature_enabled()) {
1296                 return TTS_ERROR_NOT_SUPPORTED;
1297         }
1298
1299         if (NULL == tts || NULL == err_msg) {
1300                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1301                 return TTS_ERROR_INVALID_PARAMETER;
1302         }
1303
1304         tts_client_s* client = tts_client_get(tts);
1305
1306         if (NULL == client) {
1307                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
1308                 return TTS_ERROR_INVALID_PARAMETER;
1309         }
1310
1311         if (NULL != client->err_msg) {
1312                 *err_msg = strdup(client->err_msg);
1313                 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (%s)", *err_msg);
1314         } else {
1315                 *err_msg = NULL;
1316                 SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Error msg (NULL)");
1317         }
1318
1319         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1320
1321         return TTS_ERROR_NONE;
1322 }
1323
1324 int tts_add_text(tts_h tts, const char* text, const char* language, int voice_type, int speed, int* utt_id)
1325 {
1326         SLOG(LOG_INFO, TAG_TTSC, "[DEBUG] Add text: text(%s), language(%s), type(%d)", (NULL == text) ? "NULL" : text, (NULL == language) ? "NULL" : language, voice_type);
1327
1328         if (0 != __tts_get_feature_enabled()) {
1329                 return TTS_ERROR_NOT_SUPPORTED;
1330         }
1331
1332         if (speed < 0) {
1333                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Speed should not be negative(%d)", speed);
1334                 return TTS_ERROR_INVALID_PARAMETER;
1335         }
1336
1337         if (voice_type < 0) {
1338                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Voice type should not be negative(%d)", voice_type);
1339                 return TTS_ERROR_INVALID_PARAMETER;
1340         }
1341
1342         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Add text");
1343
1344         if (NULL == tts || NULL == utt_id) {
1345                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
1346                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1347                 return TTS_ERROR_INVALID_PARAMETER;
1348         }
1349
1350         tts_client_s* client = tts_client_get(tts);
1351
1352         if (NULL == client) {
1353                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1354                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1355                 return TTS_ERROR_INVALID_PARAMETER;
1356         }
1357
1358         if (TTS_STATE_CREATED == client->current_state) {
1359                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1360                 return TTS_ERROR_INVALID_STATE;
1361         }
1362
1363         if (-1 == g_max_text_size) {
1364                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d", g_max_text_size);
1365                 if (0 != tts_config_mgr_get_max_text_size((unsigned int*)&g_max_text_size)) {
1366                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
1367                         return TTS_ERROR_INVALID_PARAMETER;
1368                 }
1369         }
1370
1371         if (0 == g_max_text_size) {
1372                 if (strlen(text) <= 0) {
1373                         SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Max Text Size is %d", g_max_text_size);
1374                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid. (max text size is unlimited.)");
1375                         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1376                         return TTS_ERROR_INVALID_PARAMETER;
1377                 }
1378         } else {
1379                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d", g_max_text_size);
1380                 if (g_max_text_size < strlen(text) || strlen(text) <= 0) {
1381                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid.");
1382                         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1383                         return TTS_ERROR_INVALID_PARAMETER;
1384                 }
1385         }
1386
1387         if (TTS_SPEED_AUTO > speed || TTS_SPEED_MAX < speed) {
1388                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] speed value(%d) is invalid.", speed);
1389                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1390                 return TTS_ERROR_INVALID_PARAMETER;
1391         }
1392
1393         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1394                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1395                 return TTS_ERROR_INVALID_STATE;
1396         }
1397
1398         if (true == client->credential_needed && NULL == client->credential) {
1399                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1400                 return TTS_ERROR_PERMISSION_DENIED;
1401         }
1402
1403         /* check valid utf8 */
1404         bool valid = false;
1405
1406         DBusError err;
1407         dbus_error_init(&err);
1408
1409         valid = dbus_validate_utf8(text, &err);
1410         if (dbus_error_is_set(&err)) {
1411                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Dbus Error(%s), text(%s)", err.message, text);
1412                 dbus_error_free(&err);
1413                 return TTS_ERROR_INVALID_PARAMETER;
1414         }
1415
1416         if (valid != true) {
1417                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Text is invalid - '%s'", text);
1418                 return TTS_ERROR_INVALID_PARAMETER;
1419         }
1420         SLOG(LOG_DEBUG, TAG_TTSC, "Text is valid - text is '%s'", text);
1421
1422         /* save texts for repetition */
1423         if (NULL != client->text_repeat) {
1424                 free(client->text_repeat);
1425                 client->text_repeat = NULL;
1426         }
1427
1428         client->text_repeat = strdup(text);
1429
1430         if (NULL != g_language) {
1431                 free(g_language);
1432                 g_language = NULL;
1433         }
1434         if (NULL == language)
1435                 g_language = NULL;
1436         else
1437                 g_language = strdup(language);
1438
1439         g_voice_type = voice_type;
1440         g_speed = speed;
1441
1442         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);
1443
1444         /* change default language value */
1445         char* temp = NULL;
1446
1447         if (NULL == language)
1448                 temp = strdup("default");
1449         else
1450                 temp = strdup(language);
1451
1452         client->current_utt_id++;
1453         if (client->current_utt_id == 10000) {
1454                 client->current_utt_id = 1;
1455         }
1456
1457         /* do request */
1458         int ret = -1;
1459         int count = 0;
1460         bool is_prepared = false;
1461         while (0 != ret) {
1462                 ret = tts_dbus_request_add_text(client->uid, text, temp, voice_type, speed, client->current_utt_id, client->credential);
1463                 if (0 != ret) {
1464                         //LCOV_EXCL_START
1465                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1466                                 client->current_state = TTS_STATE_CREATED;
1467                                 if (0 == tts_prepare_sync(tts)) {
1468                                         is_prepared = true;
1469                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1470                                 }
1471                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1472                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1473                                 break;
1474                         } else {
1475                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add text : %s", __tts_get_error_code(ret));
1476                                 usleep(10000);
1477                                 count++;
1478                                 if (TTS_RETRY_MIN_COUNT == count) {
1479                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1480                                         break;
1481                                 }
1482                         }
1483                         //LCOV_EXCL_STOP
1484                 } else {
1485                         *utt_id = client->current_utt_id;
1486                 }
1487         }
1488
1489         if (NULL != temp) {
1490                 free(temp);
1491                 temp = NULL;
1492         }
1493
1494         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1495
1496         return ret;
1497 }
1498
1499 //LCOV_EXCL_START
1500 static void __tts_play_async(void *data)
1501 {
1502         tts_h tts = (tts_h)data;
1503         tts_client_s* client = tts_client_get(tts);
1504
1505         /* check handle */
1506         if (NULL == client) {
1507                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1508                 return;
1509         }
1510
1511         int ret = -1;
1512         int count = 0;
1513         bool is_prepared = false;
1514         while (0 != ret) {
1515                 ret = tts_dbus_request_play(client->uid, client->credential);
1516                 if (0 != ret) {
1517                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1518                                 client->current_state = TTS_STATE_CREATED;
1519                                 if (0 == tts_prepare_sync(tts)) {
1520                                         is_prepared = true;
1521                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1522                                 }
1523                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1524                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1525                                 break;
1526                         } else {
1527                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1528                                 usleep(10000);
1529                                 count++;
1530                                 if (TTS_RETRY_COUNT == count) {
1531                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1532                                         break;
1533                                 }
1534                         }
1535                 }
1536         }
1537
1538         if (0 != ret) {
1539                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play tts : %s", __tts_get_error_code(ret));
1540
1541                 client->reason = ret;
1542                 client->utt_id = -1;
1543
1544                 ecore_timer_add(0, __tts_notify_error, client->tts);
1545                 return;
1546         }
1547
1548         client->before_state = client->current_state;
1549         client->current_state = TTS_STATE_PLAYING;
1550
1551         if (NULL != client->state_changed_cb) {
1552                 tts_client_use_callback(client);
1553                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1554                 tts_client_not_use_callback(client);
1555                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1556         }
1557
1558         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1559
1560         return;
1561 }
1562
1563 int tts_play_async(tts_h tts)
1564 {
1565         if (0 != __tts_get_feature_enabled()) {
1566                 return TTS_ERROR_NOT_SUPPORTED;
1567         }
1568
1569         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Play tts");
1570
1571         if (NULL == tts) {
1572                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1573                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1574                 return TTS_ERROR_INVALID_PARAMETER;
1575         }
1576
1577         tts_client_s* client = tts_client_get(tts);
1578
1579         if (NULL == client) {
1580                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1581                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1582                 return TTS_ERROR_INVALID_PARAMETER;
1583         }
1584
1585         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1586                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1587                 return TTS_ERROR_INVALID_STATE;
1588         }
1589
1590         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1591                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1592                 return TTS_ERROR_INVALID_STATE;
1593         }
1594
1595         if (true == client->credential_needed && NULL == client->credential) {
1596                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1597                 return TTS_ERROR_PERMISSION_DENIED;
1598         }
1599
1600         ecore_main_loop_thread_safe_call_async(__tts_play_async, (void*)tts);
1601
1602         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1603
1604         return TTS_ERROR_NONE;
1605 }
1606 //LCOV_EXCL_STOP
1607 int tts_play(tts_h tts)
1608 {
1609         if (0 != __tts_get_feature_enabled()) {
1610                 return TTS_ERROR_NOT_SUPPORTED;
1611         }
1612
1613         SLOG(LOG_INFO, TAG_TTSC, "@@@ Play tts");
1614
1615         if (NULL == tts) {
1616                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
1617                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1618                 return TTS_ERROR_INVALID_PARAMETER;
1619         }
1620
1621         tts_client_s* client = tts_client_get(tts);
1622
1623         if (NULL == client) {
1624                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
1625                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1626                 SLOG(LOG_DEBUG, TAG_TTSC, " ");
1627                 return TTS_ERROR_INVALID_PARAMETER;
1628         }
1629
1630         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
1631                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
1632                 return TTS_ERROR_INVALID_STATE;
1633         }
1634
1635         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1636                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1637                 return TTS_ERROR_INVALID_STATE;
1638         }
1639
1640         if (true == client->credential_needed && NULL == client->credential) {
1641                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
1642                 return TTS_ERROR_PERMISSION_DENIED;
1643         }
1644
1645         int ret = -1;
1646         int count = 0;
1647         bool is_prepared = false;
1648         while (0 != ret) {
1649                 ret = tts_dbus_request_play(client->uid, client->credential);
1650                 if (0 != ret) {
1651                         //LCOV_EXCL_START
1652                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1653                                 client->current_state = TTS_STATE_CREATED;
1654                                 if (0 == tts_prepare_sync(tts)) {
1655                                         is_prepared = true;
1656                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1657                                 }
1658                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1659                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1660                                 return ret;
1661                         } else {
1662                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
1663                                 usleep(10000);
1664                                 count++;
1665                                 if (TTS_RETRY_COUNT == count) {
1666                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1667                                         return ret;
1668                                 }
1669                         }
1670                         //LCOV_EXCL_STOP
1671                 }
1672         }
1673
1674         client->before_state = client->current_state;
1675         client->current_state = TTS_STATE_PLAYING;
1676
1677         if (NULL != client->state_changed_cb) {
1678                 tts_client_use_callback(client);
1679                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1680                 tts_client_not_use_callback(client);
1681                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1682         }
1683
1684         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1685
1686         return TTS_ERROR_NONE;
1687 }
1688 //LCOV_EXCL_START
1689 static void __tts_stop_async(void *data)
1690 {
1691         tts_h tts = (tts_h)data;
1692         tts_client_s* client = tts_client_get(tts);
1693
1694         /* check handle */
1695         if (NULL == client) {
1696                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1697                 return;
1698         }
1699
1700         int ret = -1;
1701         int count = 0;
1702         bool is_prepared = false;
1703         while (0 != ret) {
1704                 ret = tts_dbus_request_stop(client->uid);
1705                 if (0 != ret) {
1706                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1707                                 client->current_state = TTS_STATE_CREATED;
1708                                 if (0 == tts_prepare_sync(tts)) {
1709                                         is_prepared = true;
1710                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1711                                 }
1712                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1713                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1714                                 break;
1715                         } else {
1716                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1717                                 usleep(10000);
1718                                 count++;
1719                                 if (TTS_RETRY_COUNT == count) {
1720                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1721                                         break;
1722                                 }
1723                         }
1724                 }
1725         }
1726
1727         if (0 != ret) {
1728                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to stop tts : %s", __tts_get_error_code(ret));
1729
1730                 client->reason = ret;
1731                 client->utt_id = -1;
1732
1733                 ecore_timer_add(0, __tts_notify_error, client->tts);
1734                 return;
1735         }
1736
1737         client->before_state = client->current_state;
1738         client->current_state = TTS_STATE_READY;
1739
1740         if (NULL != client->state_changed_cb) {
1741                 tts_client_use_callback(client);
1742                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1743                 tts_client_not_use_callback(client);
1744                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1745         }
1746
1747         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1748
1749         return;
1750 }
1751
1752 int tts_stop_aync(tts_h tts)
1753 {
1754         if (0 != __tts_get_feature_enabled()) {
1755                 return TTS_ERROR_NOT_SUPPORTED;
1756         }
1757
1758         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Stop tts");
1759
1760         if (NULL == tts) {
1761                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1762                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1763                 return TTS_ERROR_INVALID_PARAMETER;
1764         }
1765
1766         tts_client_s* client = tts_client_get(tts);
1767
1768         if (NULL == client) {
1769                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1770                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1771                 return TTS_ERROR_INVALID_PARAMETER;
1772         }
1773
1774         if (TTS_STATE_CREATED == client->current_state) {
1775                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1776                 return TTS_ERROR_INVALID_STATE;
1777         }
1778
1779         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1780                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1781                 return TTS_ERROR_INVALID_STATE;
1782         }
1783
1784         ecore_main_loop_thread_safe_call_async(__tts_stop_async, (void*)tts);
1785
1786         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1787
1788         return TTS_ERROR_NONE;
1789 }
1790 //LCOV_EXCL_STOP
1791 int tts_stop(tts_h tts)
1792 {
1793         if (0 != __tts_get_feature_enabled()) {
1794                 return TTS_ERROR_NOT_SUPPORTED;
1795         }
1796
1797         SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop tts");
1798
1799         if (NULL == tts) {
1800                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1801                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1802                 return TTS_ERROR_INVALID_PARAMETER;
1803         }
1804
1805         tts_client_s* client = tts_client_get(tts);
1806
1807         if (NULL == client) {
1808                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1809                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1810                 return TTS_ERROR_INVALID_PARAMETER;
1811         }
1812
1813         if (TTS_STATE_CREATED == client->current_state) {
1814                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Current state is 'CREATED'.");
1815                 return TTS_ERROR_INVALID_STATE;
1816         }
1817
1818         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1819                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1820                 return TTS_ERROR_INVALID_STATE;
1821         }
1822
1823         int ret = -1;
1824         int count = 0;
1825         bool is_prepared = false;
1826         while (0 != ret) {
1827                 ret = tts_dbus_request_stop(client->uid);
1828                 if (0 != ret) {
1829                         //LCOV_EXCL_START
1830                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1831                                 client->current_state = TTS_STATE_CREATED;
1832                                 if (0 == tts_prepare_sync(tts)) {
1833                                         is_prepared = true;
1834                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1835                                 }
1836                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1837                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1838                                 return ret;
1839                         } else {
1840                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop : %s", __tts_get_error_code(ret));
1841                                 usleep(10000);
1842                                 count++;
1843                                 if (TTS_RETRY_COUNT == count) {
1844                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1845                                         return ret;
1846                                 }
1847                         }
1848                         //LCOV_EXCL_STOP
1849                 }
1850         }
1851
1852         client->before_state = client->current_state;
1853         client->current_state = TTS_STATE_READY;
1854
1855         if (NULL != client->state_changed_cb) {
1856                 tts_client_use_callback(client);
1857                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1858                 tts_client_not_use_callback(client);
1859                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1860         }
1861
1862         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1863
1864         return TTS_ERROR_NONE;
1865 }
1866 //LCOV_EXCL_START
1867 static void __tts_pause_async(void *data)
1868 {
1869         tts_h tts = (tts_h)data;
1870         tts_client_s* client = tts_client_get(tts);
1871
1872         /* check handle */
1873         if (NULL == client) {
1874                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1875                 return;
1876         }
1877
1878         int ret = -1;
1879         int count = 0;
1880         bool is_prepared = false;
1881         while (0 != ret) {
1882                 ret = tts_dbus_request_pause(client->uid);
1883                 if (0 != ret) {
1884                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
1885                                 client->current_state = TTS_STATE_CREATED;
1886                                 if (0 == tts_prepare_sync(tts)) {
1887                                         is_prepared = true;
1888                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
1889                                 }
1890                         } else if (TTS_ERROR_TIMED_OUT != ret) {
1891                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
1892                                 break;
1893                         } else {
1894                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
1895                                 usleep(10000);
1896                                 count++;
1897                                 if (TTS_RETRY_COUNT == count) {
1898                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
1899                                         break;
1900                                 }
1901                         }
1902                 }
1903         }
1904
1905         if (0 != ret) {
1906                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to pause tts : %s", __tts_get_error_code(ret));
1907
1908                 client->reason = ret;
1909                 client->utt_id = -1;
1910
1911                 ecore_timer_add(0, __tts_notify_error, client->tts);
1912                 return;
1913         }
1914
1915         client->before_state = client->current_state;
1916         client->current_state = TTS_STATE_PAUSED;
1917
1918         if (NULL != client->state_changed_cb) {
1919                 tts_client_use_callback(client);
1920                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
1921                 tts_client_not_use_callback(client);
1922                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
1923         }
1924
1925         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1926
1927         return;
1928 }
1929
1930 int tts_pause_async(tts_h tts)
1931 {
1932         if (0 != __tts_get_feature_enabled()) {
1933                 return TTS_ERROR_NOT_SUPPORTED;
1934         }
1935
1936         SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Pause tts");
1937
1938         if (NULL == tts) {
1939                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1940                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1941                 return TTS_ERROR_INVALID_PARAMETER;
1942         }
1943
1944         tts_client_s* client = tts_client_get(tts);
1945
1946         if (NULL == client) {
1947                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1948                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1949                 return TTS_ERROR_INVALID_PARAMETER;
1950         }
1951
1952         if (TTS_STATE_PLAYING != client->current_state) {
1953                 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1954                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1955                 return TTS_ERROR_INVALID_STATE;
1956         }
1957
1958         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1959                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
1960                 return TTS_ERROR_INVALID_STATE;
1961         }
1962
1963         ecore_main_loop_thread_safe_call_async(__tts_pause_async, (void*)tts);
1964
1965         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1966
1967         return TTS_ERROR_NONE;
1968 }
1969 //LCOV_EXCL_STOP
1970 int tts_pause(tts_h tts)
1971 {
1972         if (0 != __tts_get_feature_enabled()) {
1973                 return TTS_ERROR_NOT_SUPPORTED;
1974         }
1975
1976         SLOG(LOG_INFO, TAG_TTSC, "@@@ Pause tts");
1977
1978         if (NULL == tts) {
1979                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
1980                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1981                 return TTS_ERROR_INVALID_PARAMETER;
1982         }
1983
1984         tts_client_s* client = tts_client_get(tts);
1985
1986         if (NULL == client) {
1987                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
1988                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1989                 return TTS_ERROR_INVALID_PARAMETER;
1990         }
1991
1992         if (TTS_STATE_PLAYING != client->current_state) {
1993                 SLOG(LOG_ERROR, TAG_TTSC, "[Error] The Current state is NOT 'playing'. So this request should be not running.");
1994                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
1995                 return TTS_ERROR_INVALID_STATE;
1996         }
1997
1998         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
1999                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2000                 return TTS_ERROR_INVALID_STATE;
2001         }
2002
2003         int ret = -1;
2004         int count = 0;
2005         bool is_prepared = false;
2006         while (0 != ret) {
2007                 ret = tts_dbus_request_pause(client->uid);
2008                 if (0 != ret) {
2009                         //LCOV_EXCL_START
2010                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2011                                 client->current_state = TTS_STATE_CREATED;
2012                                 if (0 == tts_prepare_sync(tts)) {
2013                                         is_prepared = true;
2014                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2015                                 }
2016                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2017                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2018                                 return ret;
2019                         } else {
2020                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
2021                                 usleep(10000);
2022                                 count++;
2023                                 if (TTS_RETRY_COUNT == count) {
2024                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2025                                         return ret;
2026                                 }
2027                         }
2028                         //LCOV_EXCL_STOP
2029                 }
2030         }
2031
2032         client->before_state = client->current_state;
2033         client->current_state = TTS_STATE_PAUSED;
2034
2035         if (NULL != client->state_changed_cb) {
2036                 tts_client_use_callback(client);
2037                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2038                 tts_client_not_use_callback(client);
2039                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
2040         }
2041
2042         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2043
2044         return TTS_ERROR_NONE;
2045 }
2046
2047 int tts_set_private_data(tts_h tts, const char* key, const char* data)
2048 {
2049         if (0 != __tts_get_feature_enabled()) {
2050                 return TTS_ERROR_NOT_SUPPORTED;
2051         }
2052
2053         SLOG(LOG_INFO, TAG_TTSC, "@@@ Set private data, key(%s), data(%s)", key, data);
2054
2055         if (NULL == tts) {
2056                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
2057                 return TTS_ERROR_INVALID_PARAMETER;
2058         }
2059
2060         if (NULL == key || NULL == data) {
2061                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
2062                 return TTS_ERROR_INVALID_PARAMETER;
2063         }
2064
2065         tts_client_s* client = tts_client_get(tts);
2066
2067         if (NULL == client) {
2068                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
2069                 return TTS_ERROR_INVALID_PARAMETER;
2070         }
2071
2072         if (TTS_STATE_READY != client->current_state) {
2073                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
2074                 return TTS_ERROR_INVALID_STATE;
2075         }
2076
2077         if (true != client->internal && (0 == strcmp(key, "EnableServerTTS") || 0 == strcmp(key, "DisableServerTTS"))) {
2078                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] This is not an internal app");
2079                 return TTS_ERROR_INVALID_PARAMETER;
2080         }
2081
2082         int ret = -1;
2083         int count = 0;
2084         bool is_prepared = false;
2085         while (0 != ret) {
2086                 ret = tts_dbus_request_set_private_data(client->uid, key, data);
2087                 if (0 != ret) {
2088                         //LCOV_EXCL_START
2089                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2090                                 client->current_state = TTS_STATE_CREATED;
2091                                 if (0 == tts_prepare_sync(tts)) {
2092                                         is_prepared = true;
2093                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2094                                 }
2095                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2096                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2097                                 return ret;
2098                         } else {
2099                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
2100                                 usleep(10000);
2101                                 count++;
2102                                 if (TTS_RETRY_COUNT == count) {
2103                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2104                                         return ret;
2105                                 }
2106                         }
2107                         //LCOV_EXCL_STOP
2108                 }
2109         }
2110
2111         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2112
2113         return 0;
2114 }
2115
2116 int tts_get_private_data(tts_h tts, const char* key, char** data)
2117 {
2118         if (0 != __tts_get_feature_enabled()) {
2119                 return TTS_ERROR_NOT_SUPPORTED;
2120         }
2121
2122         SLOG(LOG_INFO, TAG_TTSC, "@@@ Get private data, key(%s)", key);
2123
2124         if (NULL == tts) {
2125                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
2126                 return TTS_ERROR_INVALID_PARAMETER;
2127         }
2128
2129         if (NULL == key || NULL == data) {
2130                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
2131                 return TTS_ERROR_INVALID_PARAMETER;
2132         }
2133
2134         tts_client_s* client = tts_client_get(tts);
2135
2136         if (NULL == client) {
2137                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
2138                 return TTS_ERROR_INVALID_PARAMETER;
2139         }
2140
2141         if (TTS_STATE_READY != client->current_state) {
2142                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid state : Current state is NOT 'READY'");
2143                 return TTS_ERROR_INVALID_STATE;
2144         }
2145
2146         int ret = -1;
2147         int count = 0;
2148         bool is_prepared = false;
2149         while (0 != ret) {
2150                 ret = tts_dbus_request_get_private_data(client->uid, key, data);
2151                 if (0 != ret) {
2152                         //LCOV_EXCL_START
2153                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2154                                 client->current_state = TTS_STATE_CREATED;
2155                                 if (0 == tts_prepare_sync(tts)) {
2156                                         is_prepared = true;
2157                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2158                                 }
2159                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2160                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2161                                 return ret;
2162                         } else {
2163                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry : %s", __tts_get_error_code(ret));
2164                                 usleep(10000);
2165                                 count++;
2166                                 if (TTS_RETRY_COUNT == count) {
2167                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2168                                         return ret;
2169                                 }
2170                         }
2171                         //LCOV_EXCL_STOP
2172                 }
2173         }
2174
2175         if (0 == strncmp(*data, "NULL", strlen(*data))) {
2176                 free(*data);
2177                 *data = NULL;
2178         }
2179
2180         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2181
2182         return 0;
2183 }
2184
2185 //LCOV_EXCL_START
2186 static Eina_Bool __tts_notify_error(void *data)
2187 {
2188         tts_h tts = (tts_h)data;
2189
2190         tts_client_s* client = tts_client_get(tts);
2191
2192         /* check handle */
2193         if (NULL == client) {
2194                 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify error msg : A handle is not valid");
2195                 return EINA_FALSE;
2196         }
2197
2198         SLOG(LOG_DEBUG, TAG_TTSC, "Error data : uttid(%d) reason(%s)", client->utt_id, __tts_get_error_code(client->reason));
2199
2200         if (NULL != client->error_cb) {
2201                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of error");
2202                 tts_client_use_callback(client);
2203                 g_err_callback_status = true;
2204                 client->error_cb(client->tts, client->utt_id, client->reason, client->error_user_data);
2205                 g_err_callback_status = false;
2206                 tts_client_not_use_callback(client);
2207         } else {
2208                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2209         }
2210
2211         return EINA_FALSE;
2212 }
2213
2214 static void __start_reprepare_thread(void* data, Ecore_Thread* thread)
2215 {
2216         SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] start reprepare thread. engine update status(%d)", g_engine_update_status);
2217
2218         tts_client_s* temp = (tts_client_s*)data;
2219         if (NULL == temp) {
2220                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] data is null");
2221                 return ;
2222         }
2223
2224         int cnt = 0;
2225         while (!g_engine_update_status && (cnt < 10)) {
2226                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] wait for starting update");
2227                 usleep(50000);
2228                 cnt++;
2229         }
2230
2231         SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] update status(%d)", g_engine_update_status);
2232
2233         while (g_engine_update_status && (NULL != g_pkgmgr)) {
2234 //              SLOG(LOG_WARN, TAG_TTSC, "[WARNING] wait for finishing update");
2235                 usleep(200000);
2236         }
2237
2238         SLOG(LOG_INFO, TAG_TTSC, "[INFO] finish updating. request to prepare");
2239
2240         if (0 != tts_prepare(temp->tts)) {
2241                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare");
2242         }
2243
2244         return ;
2245 }
2246
2247 static void __end_reprepare_thread(void* data, Ecore_Thread* thread)
2248 {
2249         SLOG(LOG_INFO, TAG_TTSC, "[INFO] end reprepare thread");
2250 }
2251
2252 int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
2253 {
2254         if (-1 == uid) {
2255                 GList* client_list = NULL;
2256                 client_list = tts_client_get_client_list();
2257
2258                 GList *iter = NULL;
2259                 tts_client_s *data = NULL;
2260
2261                 if (g_list_length(client_list) > 0) {
2262                         /* Get a first item */
2263                         iter = g_list_first(client_list);
2264
2265                         while (NULL != iter) {
2266                                 data = iter->data;
2267
2268                                 data->utt_id = utt_id;
2269                                 data->reason = reason;
2270                                 if (NULL != data->err_msg) {
2271                                         free(data->err_msg);
2272                                         data->err_msg = NULL;
2273                                 }
2274                                 if (NULL != err_msg)
2275                                         data->err_msg = strdup(err_msg);
2276
2277                                 /* call callback function */
2278                                 if (NULL != data->error_cb) {
2279                                         ecore_timer_add(0, __tts_notify_error, data->tts);
2280                                 } else {
2281                                         SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2282                                 }
2283
2284                                 if (TTS_ERROR_SERVICE_RESET == reason) {
2285                                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
2286
2287                                         data->current_state = TTS_STATE_CREATED;
2288                                         data->reason = 0;
2289                                         ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, data);
2290                                 }
2291
2292                                 /* Next item */
2293                                 iter = g_list_next(iter);
2294                         }
2295                 }
2296         } else {
2297                 tts_client_s* client = tts_client_get_by_uid(uid);
2298
2299                 if (NULL == client) {
2300                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2301                         return TTS_ERROR_INVALID_PARAMETER;
2302                 }
2303
2304                 client->utt_id = utt_id;
2305                 client->reason = reason;
2306                 if (NULL != client->err_msg) {
2307                         free(client->err_msg);
2308                         client->err_msg = NULL;
2309                 }
2310                 if (NULL != err_msg)
2311                         client->err_msg = strdup(err_msg);
2312
2313                 /* call callback function */
2314                 if (NULL != client->error_cb) {
2315                         ecore_timer_add(0, __tts_notify_error, client->tts);
2316                 } else {
2317                         SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
2318                 }
2319
2320                 if (TTS_ERROR_SERVICE_RESET == reason) {
2321                         SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
2322
2323                         client->current_state = TTS_STATE_CREATED;
2324                         client->reason = 0;
2325                         ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, client);
2326                 }
2327         }
2328
2329         return 0;
2330 }
2331
2332 static Eina_Bool __tts_notify_state_changed(void *data)
2333 {
2334         tts_h tts = (tts_h)data;
2335
2336         tts_client_s* client = tts_client_get(tts);
2337
2338         /* check handle */
2339         if (NULL == client) {
2340                 SLOG(LOG_WARN, TAG_TTSC, "Fail to notify state changed : A handle is not valid");
2341                 return EINA_FALSE;
2342         }
2343
2344         if (NULL != client->state_changed_cb) {
2345                 tts_client_use_callback(client);
2346                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2347                 tts_client_not_use_callback(client);
2348                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called : pre(%d) cur(%d)", client->before_state, client->current_state);
2349         } else {
2350                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
2351         }
2352
2353         return EINA_FALSE;
2354 }
2355
2356 int __tts_cb_set_state(int uid, int state)
2357 {
2358         tts_client_s* client = tts_client_get_by_uid(uid);
2359         if (NULL == client) {
2360                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] The handle is not valid");
2361                 return -1;
2362         }
2363
2364         tts_state_e state_from_daemon = (tts_state_e)state;
2365
2366         if (client->current_state == state_from_daemon) {
2367                 SLOG(LOG_DEBUG, TAG_TTSC, "Current state has already been %d", client->current_state);
2368                 return 0;
2369         }
2370
2371         if (NULL != client->state_changed_cb) {
2372                 if (NULL != g_check_state_timer) {
2373                         ecore_timer_del(g_check_state_timer);
2374                         g_check_state_timer = NULL;
2375                 }
2376                 g_check_state_timer = ecore_timer_add(0, __tts_notify_state_changed, client->tts);
2377         } else {
2378                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
2379         }
2380
2381         client->before_state = client->current_state;
2382         client->current_state = state_from_daemon;
2383
2384         return 0;
2385 }
2386 //LCOV_EXCL_STOP
2387
2388 int __tts_cb_utt_started(int uid, int utt_id)
2389 {
2390         tts_client_s* client = tts_client_get_by_uid(uid);
2391
2392         if (NULL == client) {
2393                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2394                 return TTS_ERROR_INVALID_PARAMETER;
2395         }
2396
2397         SLOG(LOG_INFO, TAG_TTSC, "utterance started : utt id(%d) ", utt_id);
2398
2399         client->utt_id = utt_id;
2400
2401         /* call callback function */
2402         if (NULL != client->utt_started_cb) {
2403                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance started ");
2404                 tts_client_use_callback(client);
2405                 client->utt_started_cb(client->tts, client->utt_id, client->utt_started_user_data);
2406                 tts_client_not_use_callback(client);
2407         } else {
2408                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance started ");
2409         }
2410
2411         return 0;
2412 }
2413
2414 int __tts_cb_utt_completed(int uid, int utt_id)
2415 {
2416         tts_client_s* client = tts_client_get_by_uid(uid);
2417
2418         if (NULL == client) {
2419                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
2420                 return TTS_ERROR_INVALID_PARAMETER;
2421         }
2422
2423         SLOG(LOG_INFO, TAG_TTSC, "utterance completed : uttid(%d) ", utt_id);
2424
2425         client->utt_id = utt_id;
2426
2427         /* call callback function */
2428         if (NULL != client->utt_completeted_cb) {
2429                 SLOG(LOG_DEBUG, TAG_TTSC, "Call callback function of utterance completed ");
2430                 tts_client_use_callback(client);
2431                 client->utt_completeted_cb(client->tts, client->utt_id, client->utt_completed_user_data);
2432                 tts_client_not_use_callback(client);
2433         } else {
2434                 SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of utterance completed ");
2435         }
2436
2437         return 0;
2438 }
2439
2440 int tts_set_state_changed_cb(tts_h tts, tts_state_changed_cb callback, void* user_data)
2441 {
2442         if (0 != __tts_get_feature_enabled()) {
2443                 return TTS_ERROR_NOT_SUPPORTED;
2444         }
2445
2446         if (NULL == tts || NULL == callback) {
2447                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Input parameter is null");
2448                 return TTS_ERROR_INVALID_PARAMETER;
2449         }
2450
2451         tts_client_s* client = tts_client_get(tts);
2452
2453         if (NULL == client) {
2454                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : A handle is not valid");
2455                 return TTS_ERROR_INVALID_PARAMETER;
2456         }
2457
2458         if (TTS_STATE_CREATED != client->current_state) {
2459                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set state changed cb : Current state is not 'Created'.");
2460                 return TTS_ERROR_INVALID_STATE;
2461         }
2462
2463         client->state_changed_cb = callback;
2464         client->state_changed_user_data = user_data;
2465
2466         SLOG(LOG_INFO, TAG_TTSC, "[SUCCESS] Set state changed cb");
2467
2468         return 0;
2469 }
2470
2471 int tts_unset_state_changed_cb(tts_h tts)
2472 {
2473         if (0 != __tts_get_feature_enabled()) {
2474                 return TTS_ERROR_NOT_SUPPORTED;
2475         }
2476
2477         if (NULL == tts) {
2478                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Input parameter is null");
2479                 return TTS_ERROR_INVALID_PARAMETER;
2480         }
2481
2482         tts_client_s* client = tts_client_get(tts);
2483
2484         if (NULL == client) {
2485                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : A handle is not valid");
2486                 return TTS_ERROR_INVALID_PARAMETER;
2487         }
2488
2489         if (TTS_STATE_CREATED != client->current_state) {
2490                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset state changed cb : Current state is not 'Created'.");
2491                 return TTS_ERROR_INVALID_STATE;
2492         }
2493
2494         client->state_changed_cb = NULL;
2495         client->state_changed_user_data = NULL;
2496
2497         if (NULL != g_check_state_timer) {
2498                 ecore_timer_del(g_check_state_timer);
2499                 g_check_state_timer = NULL;
2500         }
2501
2502         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset state changed cb");
2503
2504         return 0;
2505 }
2506
2507 int tts_set_utterance_started_cb(tts_h tts, tts_utterance_started_cb callback, void* user_data)
2508 {
2509         if (0 != __tts_get_feature_enabled()) {
2510                 return TTS_ERROR_NOT_SUPPORTED;
2511         }
2512
2513         if (NULL == tts || NULL == callback) {
2514                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Input parameter is null");
2515                 return TTS_ERROR_INVALID_PARAMETER;
2516         }
2517
2518         tts_client_s* client = tts_client_get(tts);
2519
2520         if (NULL == client) {
2521                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : A handle is not valid");
2522                 return TTS_ERROR_INVALID_PARAMETER;
2523         }
2524
2525         if (TTS_STATE_CREATED != client->current_state) {
2526                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Current state is not 'Created'.");
2527                 return TTS_ERROR_INVALID_STATE;
2528         }
2529
2530         client->utt_started_cb = callback;
2531         client->utt_started_user_data = user_data;
2532
2533         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt started cb");
2534
2535         return 0;
2536 }
2537
2538 int tts_unset_utterance_started_cb(tts_h tts)
2539 {
2540         if (0 != __tts_get_feature_enabled()) {
2541                 return TTS_ERROR_NOT_SUPPORTED;
2542         }
2543
2544         if (NULL == tts) {
2545                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Input parameter is null");
2546                 return TTS_ERROR_INVALID_PARAMETER;
2547         }
2548
2549         tts_client_s* client = tts_client_get(tts);
2550
2551         if (NULL == client) {
2552                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : A handle is not valid");
2553                 return TTS_ERROR_INVALID_PARAMETER;
2554         }
2555
2556         if (TTS_STATE_CREATED != client->current_state) {
2557                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt started cb : Current state is not 'Created'.");
2558                 return TTS_ERROR_INVALID_STATE;
2559         }
2560
2561         client->utt_started_cb = NULL;
2562         client->utt_started_user_data = NULL;
2563
2564         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt started cb");
2565
2566         return 0;
2567 }
2568
2569 int tts_set_utterance_completed_cb(tts_h tts, tts_utterance_completed_cb callback, void* user_data)
2570 {
2571         if (0 != __tts_get_feature_enabled()) {
2572                 return TTS_ERROR_NOT_SUPPORTED;
2573         }
2574
2575         if (NULL == tts || NULL == callback) {
2576                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Input parameter is null");
2577                 return TTS_ERROR_INVALID_PARAMETER;
2578         }
2579
2580         tts_client_s* client = tts_client_get(tts);
2581
2582         if (NULL == client) {
2583                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : A handle is not valid");
2584                 return TTS_ERROR_INVALID_PARAMETER;
2585         }
2586
2587         if (TTS_STATE_CREATED != client->current_state) {
2588                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt completed cb : Current state is not 'Created'.");
2589                 return TTS_ERROR_INVALID_STATE;
2590         }
2591
2592         client->utt_completeted_cb = callback;
2593         client->utt_completed_user_data = user_data;
2594
2595         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set utt completed cb");
2596
2597         return 0;
2598 }
2599
2600 int tts_unset_utterance_completed_cb(tts_h tts)
2601 {
2602         if (0 != __tts_get_feature_enabled()) {
2603                 return TTS_ERROR_NOT_SUPPORTED;
2604         }
2605
2606         if (NULL == tts) {
2607                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Input parameter is null");
2608                 return TTS_ERROR_INVALID_PARAMETER;
2609         }
2610
2611         tts_client_s* client = tts_client_get(tts);
2612
2613         if (NULL == client) {
2614                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : A handle is not valid");
2615                 return TTS_ERROR_INVALID_PARAMETER;
2616         }
2617
2618         if (TTS_STATE_CREATED != client->current_state) {
2619                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset utt completed cb : Current state is not 'Created'.");
2620                 return TTS_ERROR_INVALID_STATE;
2621         }
2622
2623         client->utt_completeted_cb = NULL;
2624         client->utt_completed_user_data = NULL;
2625
2626         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset utt completed cb");
2627         return 0;
2628 }
2629
2630 int tts_set_error_cb(tts_h tts, tts_error_cb callback, void* user_data)
2631 {
2632         if (0 != __tts_get_feature_enabled()) {
2633                 return TTS_ERROR_NOT_SUPPORTED;
2634         }
2635
2636         if (NULL == tts || NULL == callback) {
2637                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Input parameter is null");
2638                 return TTS_ERROR_INVALID_PARAMETER;
2639         }
2640
2641         tts_client_s* client = tts_client_get(tts);
2642
2643         if (NULL == client) {
2644                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : A handle is not valid");
2645                 return TTS_ERROR_INVALID_PARAMETER;
2646         }
2647
2648         if (TTS_STATE_CREATED != client->current_state) {
2649                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set error cb : Current state is not 'Created'.");
2650                 return TTS_ERROR_INVALID_STATE;
2651         }
2652
2653         client->error_cb = callback;
2654         client->error_user_data = user_data;
2655
2656         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set error cb");
2657
2658         return 0;
2659 }
2660
2661 int tts_unset_error_cb(tts_h tts)
2662 {
2663         if (0 != __tts_get_feature_enabled()) {
2664                 return TTS_ERROR_NOT_SUPPORTED;
2665         }
2666
2667         if (NULL == tts) {
2668                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Input parameter is null");
2669                 return TTS_ERROR_INVALID_PARAMETER;
2670         }
2671
2672         tts_client_s* client = tts_client_get(tts);
2673
2674         if (NULL == client) {
2675                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : A handle is not valid");
2676                 return TTS_ERROR_INVALID_PARAMETER;
2677         }
2678
2679         if (TTS_STATE_CREATED != client->current_state) {
2680                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset error cb : Current state is not 'Created'.");
2681                 return TTS_ERROR_INVALID_STATE;
2682         }
2683
2684         client->error_cb = NULL;
2685         client->error_user_data = NULL;
2686
2687         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset error cb");
2688
2689         return 0;
2690 }
2691
2692 int tts_set_default_voice_changed_cb(tts_h tts, tts_default_voice_changed_cb callback, void* user_data)
2693 {
2694         if (0 != __tts_get_feature_enabled()) {
2695                 return TTS_ERROR_NOT_SUPPORTED;
2696         }
2697
2698         if (NULL == tts || NULL == callback) {
2699                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Input parameter is null");
2700                 return TTS_ERROR_INVALID_PARAMETER;
2701         }
2702
2703         tts_client_s* client = tts_client_get(tts);
2704
2705         if (NULL == client) {
2706                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : A handle is not valid");
2707                 return TTS_ERROR_INVALID_PARAMETER;
2708         }
2709
2710         if (TTS_STATE_CREATED != client->current_state) {
2711                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set default voice changed cb : Current state is not 'Created'.");
2712                 return TTS_ERROR_INVALID_STATE;
2713         }
2714
2715         client->default_voice_changed_cb = callback;
2716         client->default_voice_changed_user_data = user_data;
2717
2718         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set default voice changed cb");
2719
2720         return 0;
2721 }
2722
2723 int tts_unset_default_voice_changed_cb(tts_h tts)
2724 {
2725         if (0 != __tts_get_feature_enabled()) {
2726                 return TTS_ERROR_NOT_SUPPORTED;
2727         }
2728
2729         if (NULL == tts) {
2730                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Input parameter is null");
2731                 return TTS_ERROR_INVALID_PARAMETER;
2732         }
2733
2734         tts_client_s* client = tts_client_get(tts);
2735
2736         if (NULL == client) {
2737                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : A handle is not valid");
2738                 return TTS_ERROR_INVALID_PARAMETER;
2739         }
2740
2741         if (TTS_STATE_CREATED != client->current_state) {
2742                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset default voice changed cb : Current state is not 'Created'.");
2743                 return TTS_ERROR_INVALID_STATE;
2744         }
2745
2746         client->default_voice_changed_cb = NULL;
2747         client->default_voice_changed_user_data = NULL;
2748
2749         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset default voice changed cb");
2750
2751         return 0;
2752 }
2753
2754 int tts_set_engine_changed_cb(tts_h tts, tts_engine_changed_cb callback, void* user_data)
2755 {
2756         if (0 != __tts_get_feature_enabled()) {
2757                 return TTS_ERROR_NOT_SUPPORTED;
2758         }
2759
2760         if (NULL == tts || NULL == callback) {
2761                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Input parameter is null");
2762                 return TTS_ERROR_INVALID_PARAMETER;
2763         }
2764
2765         tts_client_s* client = tts_client_get(tts);
2766
2767         if (NULL == client) {
2768                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : A handle is not valid");
2769                 return TTS_ERROR_INVALID_PARAMETER;
2770         }
2771
2772         if (TTS_STATE_CREATED != client->current_state) {
2773                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Current state is not 'Created'.");
2774                 return TTS_ERROR_INVALID_STATE;
2775         }
2776
2777         client->engine_changed_cb = callback;
2778         client->engine_changed_user_data = user_data;
2779
2780         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set engine changed cb");
2781
2782         return 0;
2783 }
2784
2785 int tts_unset_engine_changed_cb(tts_h tts)
2786 {
2787         if (0 != __tts_get_feature_enabled()) {
2788                 return TTS_ERROR_NOT_SUPPORTED;
2789         }
2790
2791         if (NULL == tts) {
2792                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Input parameter is null");
2793                 return TTS_ERROR_INVALID_PARAMETER;
2794         }
2795
2796         tts_client_s* client = tts_client_get(tts);
2797
2798         if (NULL == client) {
2799                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : A handle is not valid");
2800                 return TTS_ERROR_INVALID_PARAMETER;
2801         }
2802
2803         if (TTS_STATE_CREATED != client->current_state) {
2804                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Current state is not 'Created'.");
2805                 return TTS_ERROR_INVALID_STATE;
2806         }
2807
2808         client->engine_changed_cb = NULL;
2809         client->engine_changed_user_data = NULL;
2810
2811         SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset engine changed cb");
2812
2813         return 0;
2814 }
2815
2816 //LCOV_EXCL_START
2817 int tts_add_pcm(tts_h tts, int event, const void* data, unsigned int data_size, int audio_type, int rate)
2818 {
2819         if (0 != __tts_get_feature_enabled()) {
2820                 return TTS_ERROR_NOT_SUPPORTED;
2821         }
2822
2823         SLOG(LOG_INFO, TAG_TTSC, "@@@ Add pcm tts");
2824
2825         if (NULL == tts) {
2826                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2827                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2828                 return TTS_ERROR_INVALID_PARAMETER;
2829         }
2830
2831         tts_client_s* client = tts_client_get(tts);
2832
2833         if (NULL == client) {
2834                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2835                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2836                 return TTS_ERROR_INVALID_PARAMETER;
2837         }
2838
2839         if (TTS_STATE_CREATED == client->current_state) {
2840                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2841                 return TTS_ERROR_INVALID_STATE;
2842         }
2843
2844         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2845                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2846                 return TTS_ERROR_INVALID_STATE;
2847         }
2848
2849         int ret = -1;
2850         int count = 0;
2851         bool is_prepared = false;
2852         while (0 != ret) {
2853                 ret = tts_dbus_request_add_pcm(client->uid, event, data, data_size, audio_type, rate);
2854                 if (0 != ret) {
2855                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2856                                 client->current_state = TTS_STATE_CREATED;
2857                                 if (0 == tts_prepare_sync(tts)) {
2858                                         is_prepared = true;
2859                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2860                                 }
2861                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2862                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2863                                 return ret;
2864                         } else {
2865                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add pcm : %s", __tts_get_error_code(ret));
2866                                 usleep(10000);
2867                                 count++;
2868                                 if (TTS_RETRY_COUNT == count) {
2869                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2870                                         return ret;
2871                                 }
2872                         }
2873                 }
2874         }
2875
2876         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2877
2878         return TTS_ERROR_NONE;
2879 }
2880
2881 int tts_play_pcm(tts_h tts)
2882 {
2883         if (0 != __tts_get_feature_enabled()) {
2884                 return TTS_ERROR_NOT_SUPPORTED;
2885         }
2886
2887         SLOG(LOG_INFO, TAG_TTSC, "@@@ Play pcm tts");
2888
2889         if (NULL == tts) {
2890                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2891                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2892                 return TTS_ERROR_INVALID_PARAMETER;
2893         }
2894
2895         tts_client_s* client = tts_client_get(tts);
2896
2897         if (NULL == client) {
2898                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2899                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2900                 return TTS_ERROR_INVALID_PARAMETER;
2901         }
2902
2903         if (TTS_STATE_PLAYING == client->current_state || TTS_STATE_CREATED == client->current_state) {
2904                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2905                 return TTS_ERROR_INVALID_STATE;
2906         }
2907
2908         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2909                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2910                 return TTS_ERROR_INVALID_STATE;
2911         }
2912
2913         int ret = -1;
2914         int count = 0;
2915         bool is_prepared = false;
2916         while (0 != ret) {
2917                 ret = tts_dbus_request_play_pcm(client->uid);
2918                 if (0 != ret) {
2919                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2920                                 client->current_state = TTS_STATE_CREATED;
2921                                 if (0 == tts_prepare_sync(tts)) {
2922                                         is_prepared = true;
2923                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2924                                 }
2925                         } else if (TTS_ERROR_TIMED_OUT != ret) {
2926                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
2927                                 return ret;
2928                         } else {
2929                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play pcm : %s", __tts_get_error_code(ret));
2930                                 usleep(10000);
2931                                 count++;
2932                                 if (TTS_RETRY_COUNT == count) {
2933                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
2934                                         return ret;
2935                                 }
2936                         }
2937                 }
2938         }
2939
2940         client->before_state = client->current_state;
2941         client->current_state = TTS_STATE_PLAYING;
2942
2943         if (NULL != client->state_changed_cb) {
2944                 tts_client_use_callback(client);
2945                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
2946                 tts_client_not_use_callback(client);
2947                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
2948         }
2949
2950         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2951
2952         return TTS_ERROR_NONE;
2953 }
2954
2955 int tts_stop_pcm(tts_h tts)
2956 {
2957         if (0 != __tts_get_feature_enabled()) {
2958                 return TTS_ERROR_NOT_SUPPORTED;
2959         }
2960
2961         SLOG(LOG_INFO, TAG_TTSC, "@@@ Stop pcm tts");
2962
2963         if (NULL == tts) {
2964                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
2965                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2966                 return TTS_ERROR_INVALID_PARAMETER;
2967         }
2968
2969         tts_client_s* client = tts_client_get(tts);
2970
2971         if (NULL == client) {
2972                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
2973                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
2974                 return TTS_ERROR_INVALID_PARAMETER;
2975         }
2976
2977         if (TTS_STATE_CREATED == client->current_state) {
2978                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid.");
2979                 return TTS_ERROR_INVALID_STATE;
2980         }
2981
2982         if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
2983                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
2984                 return TTS_ERROR_INVALID_STATE;
2985         }
2986
2987         int ret = -1;
2988         int count = 0;
2989         bool is_prepared = false;
2990         while (0 != ret) {
2991                 ret = tts_dbus_request_stop_pcm(client->uid);
2992                 if (0 != ret) {
2993                         if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
2994                                 client->current_state = TTS_STATE_CREATED;
2995                                 if (0 == tts_prepare_sync(tts)) {
2996                                         is_prepared = true;
2997                                         SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
2998                                 }
2999                         } else if (TTS_ERROR_TIMED_OUT != ret) {
3000                                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
3001                                 return ret;
3002                         } else {
3003                                 SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop pcm : %s", __tts_get_error_code(ret));
3004                                 usleep(10000);
3005                                 count++;
3006                                 if (TTS_RETRY_COUNT == count) {
3007                                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
3008                                         return ret;
3009                                 }
3010                         }
3011                 }
3012         }
3013
3014         client->before_state = client->current_state;
3015         client->current_state = TTS_STATE_READY;
3016
3017         if (NULL != client->state_changed_cb) {
3018                 tts_client_use_callback(client);
3019                 client->state_changed_cb(client->tts, client->before_state, client->current_state, client->state_changed_user_data);
3020                 tts_client_not_use_callback(client);
3021                 SLOG(LOG_DEBUG, TAG_TTSC, "State changed callback is called");
3022         }
3023
3024         SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3025
3026         return TTS_ERROR_NONE;
3027 }
3028 //LCOV_EXCL_STOP
3029
3030 int tts_repeat(tts_h tts, char** text_repeat, int* utt_id)
3031 {
3032         if (0 != __tts_get_feature_enabled()) {
3033                 return TTS_ERROR_NOT_SUPPORTED;
3034         }
3035
3036         SLOG(LOG_INFO, TAG_TTSC, "@@@ Repeat TTS");
3037
3038         if (NULL == tts) {
3039                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null.");
3040                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3041                 return TTS_ERROR_INVALID_PARAMETER;
3042         }
3043
3044         if (NULL == text_repeat || NULL == utt_id) {
3045                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null.");
3046                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3047                 return TTS_ERROR_INVALID_PARAMETER;
3048         }
3049
3050         *text_repeat = NULL;
3051         *utt_id = -1;
3052
3053         tts_client_s* client = tts_client_get(tts);
3054
3055         if (NULL == client) {
3056                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid.");
3057                 SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
3058                 return TTS_ERROR_INVALID_PARAMETER;
3059         }
3060
3061         if (TTS_STATE_READY != client->current_state) {
3062                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid. (%d)", client->current_state);
3063                 return TTS_ERROR_INVALID_STATE;
3064         }
3065
3066         /* Clear the legacy and Add texts to be played repeatedly */
3067         int ret = -1;
3068         ret = tts_stop(tts);
3069         if (TTS_ERROR_NONE != ret) {
3070                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to clear the legacy");
3071                 return ret;
3072         }
3073
3074         if (NULL != client->text_repeat) {
3075                 char* tmp_text = strdup(client->text_repeat);
3076                 char* tmp_lang = NULL;
3077                 if (NULL != g_language) {
3078                         tmp_lang = strdup(g_language);
3079                 }
3080                 ret = tts_add_text(tts, tmp_text, tmp_lang, g_voice_type, g_speed, utt_id);
3081                 if (TTS_ERROR_NONE != ret) {
3082                         SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to add texts for repetition.");
3083                         if (NULL != tmp_text) {
3084                                 free(tmp_text);
3085                                 tmp_text = NULL;
3086                         }
3087                         if (NULL != tmp_lang) {
3088                                 free(tmp_lang);
3089                                 tmp_lang = NULL;
3090                         }
3091                         return ret;
3092                 }
3093                 *text_repeat = strdup(client->text_repeat);
3094                 SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] text to repeat(%s), utt_id(%d)", (*text_repeat) ? *text_repeat : "NULL", *utt_id);
3095                 if (NULL != tmp_text) {
3096                         free(tmp_text);
3097                         tmp_text = NULL;
3098                 }
3099                 if (NULL != tmp_lang) {
3100                         free(tmp_lang);
3101                         tmp_lang = NULL;
3102                 }
3103         } else {
3104                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] There is no previous added texts. Please add texts");
3105                 return TTS_ERROR_OPERATION_FAILED;
3106         }
3107
3108         /* Play added texts */
3109         ret = tts_play(tts);
3110         if (TTS_ERROR_NONE != ret) {
3111                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play texts for repetition.");
3112                 if (NULL != *text_repeat) {
3113                         free(*text_repeat);
3114                         *text_repeat = NULL;
3115                 }
3116                 *utt_id = -1;
3117                 return ret;
3118         }
3119
3120         return TTS_ERROR_NONE;
3121 }