5b745835c9eb3848ff359a99c584d17bee1a2f68
[platform/core/uifw/stt.git] / client / stt.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 <aul.h>
15 #include <cynara-client.h>
16 #include <cynara-error.h>
17 #include <cynara-session.h>
18 #include <dirent.h>
19 #include <Ecore.h>
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <system_info.h>
26 #include <unistd.h>
27 #include <buxton2.h>
28 #include <sound_manager.h>
29 #include <sound_manager_internal.h>
30
31 #include "stt.h"
32 #include "stt_client.h"
33 #include "stt_dbus.h"
34 #include "stt_config_mgr.h"
35 #include "stt_internal.h"
36 #include "stt_main.h"
37 #include "stt_dlog.h"
38
39
40 static void __stt_notify_state_changed(void *data);
41 static void __stt_notify_error(void *data);
42
43 static Ecore_Timer* g_connect_timer = NULL;
44 static float g_volume_db = 0;
45
46 static int g_feature_enabled = -1;
47
48 static pthread_mutex_t g_cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
49
50 static cynara *p_cynara = NULL;
51
52 static bool g_err_callback_status = false;
53
54 /* for changing volume on each sound stream */
55 static sound_stream_ducking_h g_media_stream_ducking;
56 static sound_stream_ducking_h g_system_stream_ducking;
57 static sound_stream_ducking_h g_notification_stream_ducking;
58 static sound_stream_ducking_h g_alarm_stream_ducking;
59
60 #define STT_BG_VOLUME_RATIO_FARFIELD    0.0
61 #define STT_BG_VOLUME_RATIO_NEARFIELD   0.7
62 #define SND_MGR_DUCKING_DURATION 500
63
64
65 static int __stt_get_feature_enabled()
66 {
67         if (0 == g_feature_enabled) {
68                 //LCOV_EXCL_START
69                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
70                 return STT_ERROR_NOT_SUPPORTED;
71                 //LCOV_EXCL_STOP
72         } else if (-1 == g_feature_enabled) {
73                 bool stt_supported = false;
74                 bool mic_supported = false;
75
76                 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(STT_FEATURE_PATH, &stt_supported)) {
77                         //LCOV_EXCL_START
78                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
79                         return STT_ERROR_NOT_SUPPORTED;
80                         //LCOV_EXCL_STOP
81                 }
82
83                 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(STT_MIC_FEATURE_PATH, &mic_supported)) {
84                         //LCOV_EXCL_START
85                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
86                         return STT_ERROR_NOT_SUPPORTED;
87                         //LCOV_EXCL_STOP
88                 }
89
90                 if (false == stt_supported || false == mic_supported) {
91                         //LCOV_EXCL_START
92                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
93                         g_feature_enabled = 0;
94                         return STT_ERROR_NOT_SUPPORTED;
95                         //LCOV_EXCL_STOP
96                 }
97
98                 g_feature_enabled = 1;
99         }
100
101         return STT_ERROR_NONE;
102 }
103
104 static int __check_privilege_initialize()
105 {
106         int ret = cynara_initialize(&p_cynara, NULL);
107         if (CYNARA_API_SUCCESS != ret)
108                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to initialize"); //LCOV_EXCL_LINE
109
110         return ret == CYNARA_API_SUCCESS;
111 }
112
113 static bool __check_privilege(const char* uid, const char * privilege)
114 {
115         FILE *fp = NULL;
116         char label_path[1024] = "/proc/self/attr/current";
117         char smack_label[1024] = {'\0',};
118
119         if (!p_cynara) {
120                 return false;
121         }
122
123         fp = fopen(label_path, "r");
124         if (fp != NULL) {
125                 if (0 >= fread(smack_label, 1, sizeof(smack_label), fp))
126                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread"); //LCOV_EXCL_LINE
127
128                 fclose(fp);
129         }
130
131         pid_t pid = getpid();
132         char *session = cynara_session_from_pid(pid);
133         int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
134         free(session);
135         session = NULL;
136
137         if (ret != CYNARA_API_ACCESS_ALLOWED) {
138                 SLOG(LOG_DEBUG, TAG_STTC, "[Client]cynara_check returned %d(Denied)", ret);
139                 return false;
140         }
141         return true;
142 }
143
144 static void __check_privilege_deinitialize()
145 {
146         if (p_cynara)
147                 cynara_finish(p_cynara);
148         p_cynara = NULL;
149 }
150
151 static int __stt_check_privilege()
152 {
153         pthread_mutex_lock(&g_cynara_mutex);
154
155         bool ret = true;
156         ret = __check_privilege_initialize();
157         if (false == ret) {
158                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed"); //LCOV_EXCL_LINE
159                 pthread_mutex_unlock(&g_cynara_mutex);
160                 return STT_ERROR_PERMISSION_DENIED;
161         }
162
163         char uid[16];
164         snprintf(uid, 16, "%d", getuid());
165         ret = true;
166         ret = __check_privilege(uid, STT_PRIVILEGE_RECORDER);
167         __check_privilege_deinitialize();
168         if (false == ret) {
169                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied"); //LCOV_EXCL_LINE
170                 pthread_mutex_unlock(&g_cynara_mutex);
171                 return STT_ERROR_PERMISSION_DENIED;
172         }
173
174         pthread_mutex_unlock(&g_cynara_mutex);
175         return STT_ERROR_NONE;
176 }
177
178 static int __stt_check_privilege_for_applaunch()
179 {
180         pthread_mutex_lock(&g_cynara_mutex);
181
182         bool ret = true;
183         ret = __check_privilege_initialize();
184         if (false == ret) {
185                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed (applaunch)"); //LCOV_EXCL_LINE
186                 pthread_mutex_unlock(&g_cynara_mutex);
187                 return STT_ERROR_PERMISSION_DENIED;
188         }
189
190         char uid[16];
191         snprintf(uid, 16, "%d", getuid());
192         ret = true;
193         ret = __check_privilege(uid, STT_PRIVILEGE_APPLAUNCH);
194         __check_privilege_deinitialize();
195         if (false == ret) {
196                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied : appmanager.launch"); //LCOV_EXCL_LINE
197                 pthread_mutex_unlock(&g_cynara_mutex);
198                 return STT_ERROR_PERMISSION_DENIED;
199         }
200
201         pthread_mutex_unlock(&g_cynara_mutex);
202         return STT_ERROR_NONE;
203 }
204
205 //LCOV_EXCL_START
206 static const char* __stt_get_error_code(stt_error_e err)
207 {
208         switch (err) {
209         case STT_ERROR_NONE:                    return "STT_ERROR_NONE";
210         case STT_ERROR_OUT_OF_MEMORY:           return "STT_ERROR_OUT_OF_MEMORY";
211         case STT_ERROR_IO_ERROR:                return "STT_ERROR_IO_ERROR";
212         case STT_ERROR_INVALID_PARAMETER:       return "STT_ERROR_INVALID_PARAMETER";
213         case STT_ERROR_TIMED_OUT:               return "STT_ERROR_TIMED_OUT";
214         case STT_ERROR_RECORDER_BUSY:           return "STT_ERROR_RECORDER_BUSY";
215         case STT_ERROR_OUT_OF_NETWORK:          return "STT_ERROR_OUT_OF_NETWORK";
216         case STT_ERROR_PERMISSION_DENIED:       return "STT_ERROR_PERMISSION_DENIED";
217         case STT_ERROR_NOT_SUPPORTED:           return "STT_ERROR_NOT_SUPPORTED";
218         case STT_ERROR_INVALID_STATE:           return "STT_ERROR_INVALID_STATE";
219         case STT_ERROR_INVALID_LANGUAGE:        return "STT_ERROR_INVALID_LANGUAGE";
220         case STT_ERROR_ENGINE_NOT_FOUND:        return "STT_ERROR_ENGINE_NOT_FOUND";
221         case STT_ERROR_OPERATION_FAILED:        return "STT_ERROR_OPERATION_FAILED";
222         case STT_ERROR_NOT_SUPPORTED_FEATURE:   return "STT_ERROR_NOT_SUPPORTED_FEATURE";
223         case STT_ERROR_SERVICE_RESET:           return "STT_ERROR_SERVICE_RESET";
224         default:
225                 return "Invalid error code";
226         }
227 }
228
229 static int __stt_convert_config_error_code(stt_config_error_e code)
230 {
231         if (code == STT_CONFIG_ERROR_NONE)                      return STT_ERROR_NONE;
232         if (code == STT_CONFIG_ERROR_OUT_OF_MEMORY)             return STT_ERROR_OUT_OF_MEMORY;
233         if (code == STT_CONFIG_ERROR_IO_ERROR)                  return STT_ERROR_IO_ERROR;
234         if (code == STT_CONFIG_ERROR_INVALID_PARAMETER)         return STT_ERROR_INVALID_PARAMETER;
235         if (code == STT_CONFIG_ERROR_PERMISSION_DENIED)         return STT_ERROR_PERMISSION_DENIED;
236         if (code == STT_CONFIG_ERROR_NOT_SUPPORTED)             return STT_ERROR_NOT_SUPPORTED;
237         if (code == STT_CONFIG_ERROR_INVALID_STATE)             return STT_ERROR_INVALID_STATE;
238         if (code == STT_CONFIG_ERROR_INVALID_LANGUAGE)          return STT_ERROR_INVALID_LANGUAGE;
239         if (code == STT_CONFIG_ERROR_ENGINE_NOT_FOUND)          return STT_ERROR_ENGINE_NOT_FOUND;
240         if (code == STT_CONFIG_ERROR_OPERATION_FAILED)          return STT_ERROR_OPERATION_FAILED;
241
242         return code;
243 }
244
245 void __stt_config_lang_changed_cb(const char* before_language, const char* current_language, void* user_data)
246 {
247         SLOG(LOG_DEBUG, TAG_STTC, "Language changed : Before lang(%s) Current lang(%s)",
248                 before_language, current_language);
249
250         if (0 == strcmp(before_language, current_language)) {
251                 return;
252         }
253
254         GList* client_list = NULL;
255         client_list = stt_client_get_client_list();
256
257         GList *iter = NULL;
258         stt_client_s *data = NULL;
259
260         if (g_list_length(client_list) > 0) {
261                 /* Get a first item */
262                 iter = g_list_first(client_list);
263
264                 while (NULL != iter) {
265                         data = iter->data;
266                         if (NULL != data->default_lang_changed_cb) {
267                                 SLOG(LOG_DEBUG, TAG_STTC, "Call default language changed callback : uid(%u)", data->uid);
268                                 data->default_lang_changed_cb(data->stt, before_language, current_language,
269                                         data->default_lang_changed_user_data);
270                         }
271
272                         /* Next item */
273                         iter = g_list_next(iter);
274                 }
275         }
276
277         return;
278 }
279
280 static Eina_Bool __reconnect_by_engine_changed(void *data)
281 {
282         stt_h stt = (stt_h)data;
283
284         stt_client_s* client = stt_client_get(stt);
285         if (NULL == client) {
286                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
287                 return EINA_FALSE;
288         }
289
290         if (STT_STATE_READY != client->current_state) {
291                 usleep(10000);
292                 return EINA_TRUE;
293         }
294
295         int ret = stt_unprepare(stt);
296         if (0 != ret) {
297                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
298         }
299         ret = stt_prepare(stt);
300         if (0 != ret) {
301                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
302         }
303
304         return EINA_FALSE;
305 }
306
307 void __stt_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, bool support_silence, bool need_credential, void* user_data)
308 {
309         stt_h stt = (stt_h)user_data;
310
311         stt_client_s* client = stt_client_get(stt);
312         if (NULL == client) {
313                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
314                 return;
315         }
316
317         if (NULL != engine_id)  SLOG(LOG_DEBUG, TAG_STTC, "Engine id(%s)", engine_id);
318         if (NULL != setting)    SLOG(LOG_DEBUG, TAG_STTC, "Engine setting(%s)", setting);
319         if (NULL != language)   SLOG(LOG_DEBUG, TAG_STTC, "Language(%s)", language);
320         SLOG(LOG_DEBUG, TAG_STTC, "Silence(%s), Credential(%s)", support_silence ? "on" : "off", need_credential ? "need" : "no need");
321
322         /* When the default engine is changed, please unload the old engine and load the new one. */
323         int ret = -1;
324
325         if (NULL == client->current_engine_id) {
326                 if (STT_STATE_RECORDING == client->current_state || STT_STATE_PROCESSING == client->current_state) {
327                         SLOG(LOG_INFO, TAG_STTC, "[INFO] stt cancel is invoked by engine_changed_cb, state(%d)", client->current_state);
328                         ret = stt_cancel(stt);
329                         if (0 != ret) {
330                                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] STT client cancelling...");
331                         }
332
333                         ecore_idler_add(__reconnect_by_engine_changed, (void*)stt);
334                 } else if (STT_STATE_READY == client->current_state) {
335                         ret = stt_unprepare(stt);
336                         if (0 != ret) {
337                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
338                         }
339                         ret = stt_prepare(stt);
340                         if (0 != ret) {
341                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
342                         }
343                 }
344         }
345
346         /* call callback function */
347         if (NULL != client->engine_changed_cb) {
348                 client->engine_changed_cb(stt, engine_id, language, support_silence, need_credential, client->engine_changed_user_data);
349         } else {
350                 SLOG(LOG_WARN, TAG_STTC, "No registered callback function for engine change");
351         }
352         return;
353 }
354
355 static const char* __stt_get_state_string(stt_state_e state)
356 {
357         switch (state) {
358         case STT_STATE_CREATED:                 return "STT_STATE_CREATED";
359         case STT_STATE_READY:           return "STT_STATE_READY";
360         case STT_STATE_RECORDING:               return "STT_STATE_RECORDING";
361         case STT_STATE_PROCESSING:      return "STT_STATE_PROCESSING";
362         default:
363                 return "Invalid State";
364         }
365 }
366 //LCOV_EXCL_STOP
367
368 static int __stt_check_handle(stt_h stt, stt_client_s** client)
369 {
370         RETVM_IF(NULL == stt, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input handle is null");
371
372         stt_client_s* temp = NULL;
373         temp = stt_client_get(stt);
374
375         /* check handle */
376         RETVM_IF(NULL == temp, STT_ERROR_INVALID_PARAMETER, "[ERROR] A handle is not available");
377         *client = temp;
378
379         return STT_ERROR_NONE;
380 }
381
382 static int __stt_check_precondition(stt_h stt, stt_client_s** client)
383 {
384         RETV_IF(0 != __stt_get_feature_enabled(), STT_ERROR_NOT_SUPPORTED);
385         RETV_IF(0 != __stt_check_privilege(), STT_ERROR_PERMISSION_DENIED);
386         RETV_IF(0 != __stt_check_handle(stt, client), STT_ERROR_INVALID_PARAMETER);
387         return STT_ERROR_NONE;
388 }
389
390 static int __stt_check_precondition_with_state(stt_h stt, stt_client_s** client, stt_state_e state)
391 {
392         int ret = __stt_check_precondition(stt, client);
393         if (STT_ERROR_NONE != ret)
394                 return ret;
395         const char* state_string = __stt_get_state_string(state);
396         RETVM_IF(state != (*client)->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not %s", (*client)->current_state, state_string);
397         return STT_ERROR_NONE;
398 }
399
400 int stt_create(stt_h* stt)
401 {
402         RETV_IF(0 != __stt_get_feature_enabled(), STT_ERROR_NOT_SUPPORTED);
403         RETV_IF(0 != __stt_check_privilege(), STT_ERROR_PERMISSION_DENIED);
404
405         SLOG(LOG_INFO, TAG_STTC, "===== Create STT");
406         RETVM_IF(NULL == stt, STT_ERROR_INVALID_PARAMETER, "[ERROR] A handle is null");
407
408         if (0 == stt_client_get_size()) {
409                 if (0 != stt_dbus_open_connection()) {
410                         //LCOV_EXCL_START
411                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to open connection");
412                         return STT_ERROR_OPERATION_FAILED;
413                         //LCOV_EXCL_STOP
414                 }
415         }
416
417         stt_h new_stt = NULL;
418         if (STT_ERROR_NONE != stt_client_new(&new_stt)) {
419                 //LCOV_EXCL_START
420                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client!");
421                 return STT_ERROR_OUT_OF_MEMORY;
422                 //LCOV_EXCL_STOP
423         }
424
425         stt_client_s* client = stt_client_get(new_stt);
426         if (NULL == client) {
427                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client"); //LCOV_EXCL_LINE
428                 stt_client_destroy(new_stt);
429                 return STT_ERROR_OPERATION_FAILED;
430         }
431
432         int ret = stt_config_mgr_initialize(client->uid);
433         ret = __stt_convert_config_error_code(ret);
434         if (0 != ret) {
435                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to init config manager : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
436                 stt_client_destroy(new_stt);
437                 return ret;
438         }
439
440         ret = stt_config_mgr_set_callback(client->uid, __stt_config_engine_changed_cb, __stt_config_lang_changed_cb, NULL, client->stt);
441         ret = __stt_convert_config_error_code(ret);
442         if (0 != ret) {
443                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set config changed : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
444                 stt_client_destroy(new_stt);
445                 return ret;
446         }
447
448         *stt = new_stt;
449         SLOG(LOG_INFO, TAG_STTC, "[Success] uid(%u)", client->uid);
450         SLOG(LOG_DEBUG, TAG_STTC, "=====");
451         return STT_ERROR_NONE;
452 }
453
454 int stt_destroy(stt_h stt)
455 {
456         stt_client_s* client = NULL;
457         int temp = __stt_check_precondition(stt, &client);
458         if (STT_ERROR_NONE != temp)
459                 return temp;
460         SLOG(LOG_INFO, TAG_STTC, "===== Destroy STT");
461
462         /* check used callback */
463         if (0 != stt_client_get_use_callback(client)) {
464                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Cannot destroy in Callback function");
465                 return STT_ERROR_OPERATION_FAILED;
466         }
467
468         stt_config_mgr_finalize(client->uid);
469
470         int ret = -1;
471
472         /* check state */
473         switch (client->current_state) {
474         case STT_STATE_PROCESSING:
475         case STT_STATE_RECORDING:
476         case STT_STATE_READY:
477                 ret = stt_dbus_request_finalize(client->uid);
478                 if (0 != ret) {
479                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
480                 }
481         case STT_STATE_CREATED:
482                 if (NULL != g_connect_timer) {
483                         SLOG(LOG_DEBUG, TAG_STTC, "Connect Timer is deleted");
484                         ecore_timer_del(g_connect_timer);
485                         g_connect_timer = NULL;
486                 }
487
488                 /* Free resources */
489                 stt_client_destroy(stt);
490                 break;
491         default:
492                 break;
493         }
494
495         if (0 == stt_client_get_size()) {
496                 if (0 != stt_dbus_close_connection()) {
497                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to close connection"); //LCOV_EXCL_LINE
498                 }
499         }
500
501         stt = NULL;
502
503         SLOG(LOG_INFO, TAG_STTC, "=====");
504         SLOG(LOG_DEBUG, TAG_STTC, " ");
505
506         return STT_ERROR_NONE;
507 }
508
509 static bool __stt_config_supported_engine_cb(const char* engine_id, const char* engine_name,
510                                              const char* setting, bool support_silence, void* user_data)
511 {
512         stt_h stt = (stt_h)user_data;
513
514         stt_client_s* client = stt_client_get(stt);
515         if (NULL == client) {
516                 //LCOV_EXCL_START
517                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
518                 return false;
519                 //LCOV_EXCL_STOP
520         }
521
522         /* call callback function */
523         if (NULL != client->supported_engine_cb) {
524                 return client->supported_engine_cb(stt, engine_id, engine_name, client->supported_engine_user_data);
525         } else {
526                 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported engine"); //LCOV_EXCL_LINE
527         }
528
529         return false;
530 }
531
532 int stt_foreach_supported_engines(stt_h stt, stt_supported_engine_cb callback, void* user_data)
533 {
534         stt_client_s* client = NULL;
535         int temp = __stt_check_precondition(stt, &client);
536         if (STT_ERROR_NONE != temp)
537                 return temp;
538         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
539         RETVM_IF(client->current_state != STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
540
541         SLOG(LOG_INFO, TAG_STTC, "===== Foreach Supported engine");
542
543         client->supported_engine_cb = callback;
544         client->supported_engine_user_data = user_data;
545
546         int ret = 0;
547         ret = stt_config_mgr_get_engine_list(__stt_config_supported_engine_cb, client->stt);
548         ret = __stt_convert_config_error_code(ret);
549         if (0 != ret) {
550                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get engines : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
551         }
552
553         client->supported_engine_cb = NULL;
554         client->supported_engine_user_data = NULL;
555
556         SLOG(LOG_INFO, TAG_STTC, "=====");
557         SLOG(LOG_DEBUG, TAG_STTC, " ");
558
559         return ret;
560 }
561
562 int stt_get_engine(stt_h stt, char** engine_id)
563 {
564         stt_client_s* client = NULL;
565         int temp = __stt_check_precondition(stt, &client);
566         if (STT_ERROR_NONE != temp)
567                 return temp;
568
569         RETVM_IF(NULL == engine_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
570         RETVM_IF(client->current_state != STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
571
572         SLOG(LOG_INFO, TAG_STTC, "===== Get current engine");
573
574         int ret = 0;
575
576         if (NULL != client->current_engine_id) {
577                 *engine_id = strdup(client->current_engine_id);
578                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
579         } else {
580
581                 ret = stt_config_mgr_get_engine(engine_id);
582                 ret = __stt_convert_config_error_code(ret);
583                 if (0 != ret) {
584                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request get current engine : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
585                 } else {
586                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
587                 }
588         }
589
590         SLOG(LOG_INFO, TAG_STTC, "=====");
591         SLOG(LOG_DEBUG, TAG_STTC, " ");
592
593         return ret;
594 }
595
596 int __stt_set_buxtonkey(const char* engine_id)
597 {
598         /* Set vconfkey */
599         struct buxton_client * bux_cli;
600         struct buxton_layer * bux_layer;
601         struct buxton_value * bux_val;
602
603         int ret = buxton_open(&bux_cli, NULL, NULL);
604         if (0 != ret) {
605                 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] Fail to open buxton client"); //LCOV_EXCL_LINE
606                 return STT_ERROR_OPERATION_FAILED;
607         }
608         SLOG(LOG_DEBUG, TAG_STTC, "[DBUS-BUXTON2] buxton_open: %d", ret);
609         bux_layer = buxton_create_layer("system");
610         if (NULL == bux_layer) {
611                 //LCOV_EXCL_START
612                 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] buxton_create_layer FAIL");
613                 buxton_close(bux_cli);
614                 bux_cli = NULL;
615                 return STT_ERROR_OPERATION_FAILED;
616                 //LCOV_EXCL_STOP
617         }
618         bux_val = buxton_value_create_string(engine_id);
619         if (NULL == bux_val) {
620                 //LCOV_EXCL_START
621                 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] buxton_value_create_string FAIL");
622                 buxton_free_layer(bux_layer);
623                 buxton_close(bux_cli);
624                 bux_layer = NULL;
625                 bux_cli = NULL;
626                 return STT_ERROR_OPERATION_FAILED;
627                 //LCOV_EXCL_STOP
628         }
629
630         ret = buxton_set_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_CUSTOM, bux_val);
631         if (0 != ret) {
632                 //LCOV_EXCL_START
633                 SLOG(LOG_ERROR, TAG_STTC, "[DBUS-BUXTON2] Fail to set value sync");
634                 buxton_value_free(bux_val);
635                 buxton_free_layer(bux_layer);
636                 buxton_close(bux_cli);
637
638                 bux_cli = NULL;
639                 bux_layer = NULL;
640                 bux_val = NULL;
641                 return STT_ERROR_OPERATION_FAILED;
642                 //LCOV_EXCL_STOP
643         }
644         SLOG(LOG_DEBUG, TAG_STTC, "[DBUS-BUXTON2] buxton_set_value_sync: %d, %s", ret, STT_ENGINE_DB_CUSTOM);
645
646         buxton_value_free(bux_val);
647         buxton_free_layer(bux_layer);
648         buxton_close(bux_cli);
649
650         bux_cli = NULL;
651         bux_layer = NULL;
652         bux_val = NULL;
653
654         return STT_ERROR_NONE;
655 }
656
657 int stt_set_engine(stt_h stt, const char* engine_id)
658 {
659         stt_client_s* client = NULL;
660         if (0 != __stt_get_feature_enabled()) {
661                 return STT_ERROR_NOT_SUPPORTED;
662         }
663         if (0 != __stt_check_privilege()) {
664                 return STT_ERROR_PERMISSION_DENIED;
665         }
666         if (0 != __stt_check_privilege_for_applaunch()) {
667                 return STT_ERROR_PERMISSION_DENIED;
668         }
669         if (0 != __stt_check_handle(stt, &client)) {
670                 return STT_ERROR_INVALID_PARAMETER;
671         }
672
673         SLOG(LOG_INFO, TAG_STTC, "===== Set current engine");
674
675         RETVM_IF(NULL == engine_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
676         RETVM_IF(client->current_state != STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
677
678         SLOG(LOG_INFO, TAG_STTC, "===== engined_id(%s)", engine_id);
679
680         free(client->current_engine_id);
681         client->current_engine_id = strdup(engine_id);
682
683         /* Set vconfkey */
684         int ret = __stt_set_buxtonkey(engine_id);
685         if (0 != ret) {
686                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] set buxtonkey Failed!!!"); //LCOV_EXCL_LINE
687                 return ret;
688         }
689
690         SLOG(LOG_INFO, TAG_STTC, "=====");
691         SLOG(LOG_DEBUG, TAG_STTC, " ");
692
693         return 0;
694 }
695
696 int stt_set_credential(stt_h stt, const char* credential)
697 {
698         stt_client_s* client = NULL;
699         int temp = __stt_check_precondition(stt, &client);
700         if (STT_ERROR_NONE != temp)
701                 return temp;
702
703         SLOG(LOG_INFO, TAG_STTC, "===== Set credential");
704
705         RETVM_IF(NULL == credential, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
706         RETVM_IF(client->current_state != STT_STATE_CREATED && client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE,
707                         "[ERROR] Invalid State: Current state(%d) is not CREATED or READY", client->current_state);
708
709         free(client->credential);
710         client->credential = strdup(credential);
711
712         SLOG(LOG_INFO, TAG_STTC, "=====");
713         SLOG(LOG_DEBUG, TAG_STTC, " ");
714
715         return STT_ERROR_NONE;
716 }
717
718 //LCOV_EXCL_START
719 static bool __stt_is_necessary_to_retry_dbus_request(int count, int return_value)
720 {
721         bool ret = true;
722         if (STT_ERROR_TIMED_OUT != return_value) {
723                 ret = false;
724         } else {
725                 SLOG(LOG_WARN, TAG_STTC, "[WARNING][%d] retry : %s", count, __stt_get_error_code(ret));
726                 usleep(10000);
727                 if (STT_RETRY_COUNT == count) {
728                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
729                         ret = false;
730                 }
731         }
732         return ret;
733 }
734 //LCOV_EXCL_STOP
735
736 int stt_set_private_data(stt_h stt, const char* key, const char* data)
737 {
738         stt_client_s* client = NULL;
739         int temp = __stt_check_precondition(stt, &client);
740         if (STT_ERROR_NONE != temp)
741                 return temp;
742
743         RETVM_IF(NULL == key || NULL == data, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter");
744         RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
745
746         SLOG(LOG_INFO, TAG_STTC, "===== Set private data");
747
748         if (true != client->internal && (0 == strcmp(key, "server") || 0 == strcmp(key, "rampcode") || 0 == strcmp(key, "epd"))) {
749                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This is not an internal app");
750                 return STT_ERROR_INVALID_PARAMETER;
751         }
752
753         int ret = -1;
754         int count = 0;
755         while (0 != ret) {
756                 ret = stt_dbus_request_set_private_data(client->uid, key, data);
757                 if (0 != ret) {
758                         //LCOV_EXCL_START
759                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
760                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data : %s", __stt_get_error_code(ret));
761                                 return ret;
762                         }
763                         count++;
764                         //LCOV_EXCL_STOP
765                 }
766         }
767
768         SLOG(LOG_INFO, TAG_STTC, "=====");
769         SLOG(LOG_DEBUG, TAG_STTC, "");
770
771         return STT_ERROR_NONE;
772 }
773
774 int stt_get_private_data(stt_h stt, const char* key, char** data)
775 {
776         stt_client_s* client = NULL;
777         int temp = __stt_check_precondition(stt, &client);
778         if (STT_ERROR_NONE != temp)
779                 return temp;
780
781         RETVM_IF(NULL == key || NULL == data, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter");
782         RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
783
784         SLOG(LOG_INFO, TAG_STTC, "===== Get private data");
785
786         int ret = -1;
787         int count = 0;
788         while (0 != ret) {
789                 ret = stt_dbus_request_get_private_data(client->uid, key, data);
790                 if (0 != ret) {
791                         //LCOV_EXCL_START
792                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
793                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get private data : %s", __stt_get_error_code(ret));
794                                 return ret;
795                         }
796                         count++;
797                         //LCOV_EXCL_STOP
798                 }
799         }
800
801         if (0 == strncmp(*data, "NULL", strlen(*data))) {
802                 free(*data);
803                 *data = NULL;
804         }
805
806         SLOG(LOG_INFO, TAG_STTC, "=====");
807         SLOG(LOG_DEBUG, TAG_STTC, "");
808
809         return STT_ERROR_NONE;
810 }
811
812 int stt_set_server_stt(stt_h stt, const char* key, char* user_data)
813 {
814         int ret = -1;
815         stt_client_s* client = NULL;
816
817         int temp = __stt_check_precondition(stt, &client);
818         if (STT_ERROR_NONE != temp)
819                 return temp;
820
821         SLOG(LOG_INFO, TAG_STTC, "===== Set STT server");
822
823         RETVM_IF(NULL == key || NULL == user_data, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter");
824
825         if (STT_STATE_READY != client->current_state) {
826                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] The current state is invalid (%d).", client->current_state);
827                 return STT_ERROR_INVALID_STATE;
828         }
829
830         client->internal = true;
831         ret = stt_set_private_data(stt, key, user_data);
832         if (0 != ret) {
833                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data, ret(%d), key(%s)", ret, key);
834                 return ret;
835         }
836
837         SLOG(LOG_INFO, TAG_STTC, "======");
838         SLOG(LOG_DEBUG, TAG_STTC, " ");
839
840         return ret;
841 }
842
843 static Eina_Bool __stt_connect_daemon(void *data)
844 {
845         stt_client_s* client = (stt_client_s*)data;
846         int ret = -1;
847
848         if (NULL == client) {
849                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available"); //LCOV_EXCL_LINE
850                 g_connect_timer = NULL;
851                 return EINA_FALSE;
852         }
853
854         if (0 == stt_client_get_size() || NULL == stt_client_get_by_uid(client->uid)) {
855                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Client has been already destroyed"); //LCOV_EXCL_LINE
856                 return EINA_FALSE;
857         }
858
859         /* Check and Set vconfkey of custom engine before sending hello */
860         if (NULL != client->current_engine_id && 0 == __stt_check_privilege_for_applaunch()) {
861                 /* Set vconfkey */
862                 ret = __stt_set_buxtonkey(client->current_engine_id);
863                 //LCOV_EXCL_START
864                 if (0 != ret) {
865                         SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] set buxtonkey Failed!!! (inside __stt_connect_daemon)");
866                         return EINA_TRUE;
867                 }
868                 //LCOV_EXCL_STOP
869         }
870
871         /* Send hello */
872         ret = stt_dbus_request_hello(client->uid);
873         if (0 != ret) {
874                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to send hello, ret(0x%x)", ret);
875                 return EINA_TRUE;
876         }
877
878         g_connect_timer = NULL;
879         SLOG(LOG_INFO, TAG_STTC, "===== Connect stt-service");
880
881         /* request initialization */
882         bool silence_supported = false;
883         bool credential_needed = false;
884
885         ret = stt_dbus_request_initialize(client->uid, &silence_supported, &credential_needed);
886
887         if (STT_ERROR_ENGINE_NOT_FOUND == ret) {
888                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to initialize : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
889
890                 client->reason = STT_ERROR_ENGINE_NOT_FOUND; //LCOV_EXCL_LINE
891                 ecore_main_loop_thread_safe_call_async(__stt_notify_error, (void*)client); //LCOV_EXCL_LINE
892
893                 return EINA_FALSE; //LCOV_EXCL_LINE
894         } else if (STT_ERROR_NONE != ret) {
895                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] Fail to connection. Retry to connect"); //LCOV_EXCL_LINE
896                 return EINA_TRUE;
897         } else {
898                 /* success to connect stt-service */
899                 client->silence_supported = silence_supported;
900                 client->credential_needed = credential_needed;
901                 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
902         }
903
904         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] uid(%u)", client->uid);
905
906         client->before_state = client->current_state;
907         client->current_state = STT_STATE_READY;
908
909         if (NULL != client->state_changed_cb) {
910                 stt_client_use_callback(client);
911                 client->state_changed_cb(client->stt, client->before_state,
912                         client->current_state, client->state_changed_user_data);
913                 stt_client_not_use_callback(client);
914                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
915         } else {
916                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
917         }
918
919         SLOG(LOG_INFO, TAG_STTC, "=====");
920         SLOG(LOG_DEBUG, TAG_STTC, "  ");
921
922         return EINA_FALSE;
923 }
924
925 int stt_prepare(stt_h stt)
926 {
927         stt_client_s* client = NULL;
928         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
929         if (STT_ERROR_NONE != temp)
930                 return temp;
931
932         SLOG(LOG_INFO, TAG_STTC, "===== Prepare STT");
933
934         ecore_thread_main_loop_begin();
935         g_connect_timer = ecore_timer_add(0.02, __stt_connect_daemon, (void*)client);
936         ecore_thread_main_loop_end();
937
938         SLOG(LOG_INFO, TAG_STTC, "=====");
939         SLOG(LOG_DEBUG, TAG_STTC, " ");
940
941         return STT_ERROR_NONE;
942 }
943
944 int stt_unprepare(stt_h stt)
945 {
946         stt_client_s* client = NULL;
947         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
948         if (STT_ERROR_NONE != temp)
949                 return temp;
950
951         SLOG(LOG_INFO, TAG_STTC, "===== Unprepare STT");
952
953         int ret = -1;
954         int count = 0;
955         while (0 != ret) {
956                 ret = stt_dbus_request_finalize(client->uid);
957                 if (0 != ret) {
958                         //LCOV_EXCL_START
959                         if (STT_ERROR_TIMED_OUT != ret) {
960                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret));
961                                 break;
962                         } else {
963                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
964                                 usleep(10000);
965                                 count++;
966                                 if (STT_RETRY_COUNT == count) {
967                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
968                                         break;
969                                 }
970                         }
971                         //LCOV_EXCL_STOP
972                 }
973         }
974
975         client->internal_state = STT_INTERNAL_STATE_NONE;
976
977         client->before_state = client->current_state;
978         client->current_state = STT_STATE_CREATED;
979
980         if (NULL != client->state_changed_cb) {
981                 stt_client_use_callback(client);
982                 client->state_changed_cb(client->stt, client->before_state,
983                         client->current_state, client->state_changed_user_data);
984                 stt_client_not_use_callback(client);
985         } else {
986                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
987         }
988
989         if (g_connect_timer) {
990                 ecore_timer_del(g_connect_timer);
991                 g_connect_timer = NULL;
992         }
993
994         SLOG(LOG_INFO, TAG_STTC, "=====");
995         SLOG(LOG_DEBUG, TAG_STTC, " ");
996
997         return STT_ERROR_NONE;
998 }
999
1000 static bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
1001 {
1002         stt_h stt = (stt_h)user_data;
1003
1004         stt_client_s* client = stt_client_get(stt);
1005         if (NULL == client) {
1006                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid"); //LCOV_EXCL_LINE
1007                 return false;
1008         }
1009
1010         SLOG(LOG_INFO, TAG_STTC, "===== supported language callback");
1011
1012         /* call callback function */
1013         if (NULL != client->supported_lang_cb) {
1014                 return client->supported_lang_cb(stt, language, client->supported_lang_user_data);
1015         } else {
1016                 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported languages"); //LCOV_EXCL_LINE
1017         }
1018
1019         return false;
1020 }
1021
1022 int stt_foreach_supported_languages(stt_h stt, stt_supported_language_cb callback, void* user_data)
1023 {
1024         stt_client_s* client = NULL;
1025         int temp = __stt_check_precondition(stt, &client);
1026         if (STT_ERROR_NONE != temp)
1027                 return temp;
1028
1029         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1030         SLOG(LOG_INFO, TAG_STTC, "===== Foreach Supported Language");
1031
1032         int ret;
1033         char* current_engine_id = NULL;
1034
1035         if (NULL == client->current_engine_id) {
1036                 ret = stt_config_mgr_get_engine(&current_engine_id);
1037                 if (NULL == current_engine_id) {
1038                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory or Engine id is NULL");
1039                         return STT_ERROR_OPERATION_FAILED;
1040                 }
1041                 ret = __stt_convert_config_error_code(ret);
1042                 if (0 != ret) {
1043                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default engine id : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1044                         free(current_engine_id);
1045                         current_engine_id = NULL;
1046                         return ret;
1047                 }
1048         } else {
1049                 current_engine_id = strdup(client->current_engine_id);
1050                 //LCOV_EXCL_START
1051                 if (NULL == current_engine_id) {
1052                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1053                         return STT_ERROR_OUT_OF_MEMORY;
1054                 }
1055                 //LCOV_EXCL_STOP
1056         }
1057
1058         client->supported_lang_cb = callback;
1059         client->supported_lang_user_data = user_data;
1060
1061         ret = stt_config_mgr_get_language_list(current_engine_id, __stt_config_supported_language_cb, client->stt);
1062         ret = __stt_convert_config_error_code(ret);
1063         if (0 != ret) {
1064                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get languages : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1065         }
1066
1067         free(current_engine_id);
1068         current_engine_id = NULL;
1069
1070         client->supported_lang_cb = NULL;
1071         client->supported_lang_user_data = NULL;
1072
1073         SLOG(LOG_INFO, TAG_STTC, "=====");
1074         SLOG(LOG_DEBUG, TAG_STTC, " ");
1075
1076         return ret;
1077 }
1078
1079 int stt_get_default_language(stt_h stt, char** language)
1080 {
1081         stt_client_s* client = NULL;
1082         int temp = __stt_check_precondition(stt, &client);
1083         if (STT_ERROR_NONE != temp)
1084                 return temp;
1085
1086         RETVM_IF(NULL == language, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1087         SLOG(LOG_INFO, TAG_STTC, "===== Get Default Language");
1088
1089         int ret = 0;
1090         ret = stt_config_mgr_get_default_language(language);
1091         ret = __stt_convert_config_error_code(ret);
1092         if (0 != ret) {
1093                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default language : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1094         } else {
1095                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current language = %s", *language);
1096         }
1097
1098         SLOG(LOG_INFO, TAG_STTC, "=====");
1099         SLOG(LOG_DEBUG, TAG_STTC, " ");
1100
1101         return ret;
1102 }
1103
1104 int stt_get_state(stt_h stt, stt_state_e* state)
1105 {
1106         stt_client_s* client = NULL;
1107         int temp = __stt_check_precondition(stt, &client);
1108         if (STT_ERROR_NONE != temp)
1109                 return temp;
1110
1111         RETVM_IF(NULL == state, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1112
1113         *state = client->current_state;
1114
1115         SLOG(LOG_INFO, TAG_STTC, "===== Get state(%d)", *state);
1116
1117         switch (*state) {
1118         case STT_STATE_CREATED:         SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'CREATED'");        break;
1119         case STT_STATE_READY:           SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Ready'");          break;
1120         case STT_STATE_RECORDING:       SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Recording'");      break;
1121         case STT_STATE_PROCESSING:      SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Processing'");     break;
1122         default:                        SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid value");             break; //LCOV_EXCL_LINE
1123         }
1124
1125         return STT_ERROR_NONE;
1126 }
1127
1128 int stt_get_error_message(stt_h stt, char** err_msg)
1129 {
1130         stt_client_s* client = NULL;
1131         int temp = __stt_check_precondition(stt, &client);
1132         if (STT_ERROR_NONE != temp)
1133                 return temp;
1134
1135         RETVM_IF(NULL == err_msg, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1136         RETVM_IF(false == g_err_callback_status, STT_ERROR_OPERATION_FAILED, "[ERROR] This callback should be called during an err_callback");
1137
1138         if (NULL != client->err_msg) {
1139                 *err_msg = strdup(client->err_msg);
1140                 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (%s)", *err_msg);
1141         } else {
1142                 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Error msg (NULL)"); //LCOV_EXCL_LINE
1143         }
1144
1145         SLOG(LOG_INFO, TAG_STTC, "=====");
1146         SLOG(LOG_DEBUG, TAG_STTC, " ");
1147
1148         return STT_ERROR_NONE;
1149 }
1150
1151 int stt_is_recognition_type_supported(stt_h stt, const char* type, bool* support)
1152 {
1153         stt_client_s* client = NULL;
1154         int temp = __stt_check_precondition(stt, &client);
1155         if (STT_ERROR_NONE != temp)
1156                 return temp;
1157
1158         RETVM_IF(NULL == type || NULL == support, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1159         RETVM_IF(client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1160
1161         int ret = -1;
1162         int count = 0;
1163         while (0 != ret) {
1164                 ret = stt_dbus_request_is_recognition_type_supported(client->uid, type, support);
1165                 if (0 != ret) {
1166                         //LCOV_EXCL_START
1167                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1168                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get recognition type supported : %s", __stt_get_error_code(ret));
1169                                 return ret;
1170                         }
1171                         count++;
1172                         //LCOV_EXCL_STOP
1173                 } else {
1174                         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] recognition type is %s", *support ? "true " : "false");
1175                         break;
1176                 }
1177         }
1178
1179         return STT_ERROR_NONE;
1180 }
1181
1182 int stt_get_audio_format(stt_h stt, stt_audio_type_e* type, int* rate, int* num_of_channels)
1183 {
1184         stt_client_s* client = NULL;
1185         int temp = __stt_check_precondition(stt, &client);
1186         if (STT_ERROR_NONE != temp)
1187                 return temp;
1188
1189         RETVM_IF(NULL == type || NULL == rate || NULL == num_of_channels, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1190         RETVM_IF(client->current_state == STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is CREATED", client->current_state);
1191
1192         int ret = STT_ERROR_OPERATION_FAILED;
1193         int count = 0;
1194         while (STT_ERROR_NONE != ret) {
1195                 ret = stt_dbus_request_get_audio_format(client->uid, type, rate, num_of_channels);
1196                 if (STT_ERROR_NONE != ret) {
1197                         //LCOV_EXCL_START
1198                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1199                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get audio format : %d/%s", ret, get_error_message(ret));
1200                                 return ret;
1201                         }
1202                         count++;
1203                         //LCOV_EXCL_STOP
1204                 } else {
1205                         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Audio format: type(%d), rate(%d), number of channels(%d)",
1206                                  *type, *rate, *num_of_channels);
1207                         break;
1208                 }
1209         }
1210
1211         return STT_ERROR_NONE;
1212 }
1213
1214 int stt_set_silence_detection(stt_h stt, stt_option_silence_detection_e type)
1215 {
1216         stt_client_s* client = NULL;
1217         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1218         if (STT_ERROR_NONE != temp)
1219                 return temp;
1220
1221         SLOG(LOG_INFO, TAG_STTC, "===== Set silence detection, supported(%d), type(%d)", client->silence_supported, type);
1222
1223         if (true == client->silence_supported) {
1224                 if (type >= STT_OPTION_SILENCE_DETECTION_FALSE && type <= STT_OPTION_SILENCE_DETECTION_AUTO) {
1225                         client->silence = type;
1226                 } else {
1227                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Type is invalid");
1228                         return STT_ERROR_INVALID_PARAMETER;
1229                 }
1230         } else {
1231                 return STT_ERROR_NOT_SUPPORTED_FEATURE;
1232         }
1233
1234         return STT_ERROR_NONE;
1235 }
1236
1237 int stt_set_start_sound(stt_h stt, const char* filename)
1238 {
1239
1240         stt_client_s* client = NULL;
1241         int temp = __stt_check_precondition(stt, &client);
1242         if (STT_ERROR_NONE != temp)
1243                 return temp;
1244
1245         RETVM_IF(NULL == filename, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1246         RETVM_IF(0 != access(filename, F_OK), STT_ERROR_INVALID_PARAMETER, "[ERROR] File does not exist");
1247         RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1248
1249         SLOG(LOG_INFO, TAG_STTC, "===== STT SET START SOUND");
1250
1251         int ret = -1;
1252         int count = 0;
1253         while (0 != ret) {
1254                 ret = stt_dbus_request_set_start_sound(client->uid, filename);
1255                 if (0 != ret) {
1256                         //LCOV_EXCL_START
1257                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1258                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set start sound : %s", __stt_get_error_code(ret));
1259                                 return ret;
1260                         }
1261                         count++;
1262                         //LCOV_EXCL_STOP
1263                 } else {
1264                         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set start sound : %s", filename);
1265                         break;
1266                 }
1267         }
1268
1269         return STT_ERROR_NONE;
1270 }
1271
1272 int stt_unset_start_sound(stt_h stt)
1273 {
1274         stt_client_s* client = NULL;
1275         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1276         if (STT_ERROR_NONE != temp)
1277                 return temp;
1278
1279         SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET START SOUND");
1280
1281         int ret = -1;
1282         int count = 0;
1283         while (0 != ret) {
1284                 ret = stt_dbus_request_unset_start_sound(client->uid);
1285                 if (0 != ret) {
1286                         //LCOV_EXCL_START
1287                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1288                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset start sound : %s", __stt_get_error_code(ret));
1289                                 return ret;
1290                         }
1291                         count++;
1292                         //LCOV_EXCL_STOP
1293                 } else {
1294                         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset start sound");
1295                         break;
1296                 }
1297         }
1298
1299         return STT_ERROR_NONE;
1300 }
1301
1302 int stt_set_stop_sound(stt_h stt, const char* filename)
1303 {
1304
1305         stt_client_s* client = NULL;
1306         int temp = __stt_check_precondition(stt, &client);
1307         if (STT_ERROR_NONE != temp)
1308                 return temp;
1309
1310         RETVM_IF(NULL == filename, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1311         RETVM_IF(0 != access(filename, F_OK), STT_ERROR_INVALID_PARAMETER, "[ERROR] File does not exist");
1312         RETVM_IF(STT_STATE_READY != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1313
1314         SLOG(LOG_INFO, TAG_STTC, "===== STT SET STOP SOUND");
1315
1316         int ret = -1;
1317         int count = 0;
1318         while (0 != ret) {
1319                 ret = stt_dbus_request_set_stop_sound(client->uid, filename);
1320                 if (0 != ret) {
1321                         //LCOV_EXCL_START
1322                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1323                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set stop sound : %s", __stt_get_error_code(ret));
1324                                 return ret;
1325                         }
1326                         count++;
1327                         //LCOV_EXCL_STOP
1328                 } else {
1329                         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Set stop sound : %s", filename);
1330                         break;
1331                 }
1332         }
1333
1334         return STT_ERROR_NONE;
1335 }
1336
1337 int stt_unset_stop_sound(stt_h stt)
1338 {
1339         stt_client_s* client = NULL;
1340         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1341         if (STT_ERROR_NONE != temp)
1342                 return temp;
1343
1344         SLOG(LOG_INFO, TAG_STTC, "===== STT UNSET STOP SOUND");
1345
1346         int ret = -1;
1347         int count = 0;
1348         while (0 != ret) {
1349                 ret = stt_dbus_request_unset_stop_sound(client->uid);
1350                 if (0 != ret) {
1351                         //LCOV_EXCL_START
1352                         if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
1353                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset stop sound : %s", __stt_get_error_code(ret));
1354                                 return ret;
1355                         }
1356                         count++;
1357                         //LCOV_EXCL_STOP
1358                 } else {
1359                         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Unset stop sound");
1360                         break;
1361                 }
1362         }
1363
1364         return STT_ERROR_NONE;
1365 }
1366
1367 int stt_start(stt_h stt, const char* language, const char* type)
1368 {
1369         stt_client_s* client = NULL;
1370         int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1371         if (STT_ERROR_NONE != tmp)
1372                 return tmp;
1373
1374         RETVM_IF(STT_INTERNAL_STATE_NONE != client->internal_state, STT_ERROR_IN_PROGRESS_TO_RECORDING, "[ERROR] Invalid State : Internal state is NOT none : %d", client->current_state);
1375         SLOG(LOG_INFO, TAG_STTC, "===== STT START");
1376
1377         int ret = -1;
1378         char appid[1024] = {0, };
1379         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
1380
1381         if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1382                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID"); //LCOV_EXCL_LINE
1383         } else {
1384                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1385         }
1386
1387         const char* temp = NULL;
1388         if (NULL == language) {
1389                 temp = "default";
1390         } else {
1391                 temp = language;
1392         }
1393
1394         RETVM_IF(NULL == temp, STT_ERROR_OUT_OF_MEMORY, "[ERROR] Fail to allocate memory");
1395
1396         if (true == client->credential_needed && NULL == client->credential) {
1397                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id); //LCOV_EXCL_LINE
1398                 return STT_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1399         }
1400
1401         client->internal_state = STT_INTERNAL_STATE_STARTING;
1402         ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential, client->audio_id);
1403         if (0 != ret) {
1404                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1405                 client->internal_state = STT_INTERNAL_STATE_NONE;
1406         } else {
1407                 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
1408                 client->is_streaming = false;
1409         }
1410
1411         SLOG(LOG_INFO, TAG_STTC, "=====");
1412         SLOG(LOG_DEBUG, TAG_STTC, " ");
1413
1414         return ret;
1415 }
1416
1417 int stt_stop(stt_h stt)
1418 {
1419         stt_client_s* client = NULL;
1420         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
1421         if (STT_ERROR_NONE != temp)
1422                 return temp;
1423
1424         SLOG(LOG_INFO, TAG_STTC, "===== STT STOP");
1425
1426         if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1427                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1428                 return STT_ERROR_IN_PROGRESS_TO_READY;
1429         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1430                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1431                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1432         }
1433
1434         if (client->is_streaming) {
1435                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is activated.");
1436                 return STT_ERROR_OPERATION_FAILED;
1437         }
1438
1439         client->internal_state = STT_INTERNAL_STATE_STOPPING;
1440         int ret = stt_dbus_request_stop(client->uid);
1441         if (0 != ret) {
1442                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1443                 client->internal_state = STT_INTERNAL_STATE_NONE;
1444         } else {
1445                 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1446         }
1447
1448         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1449         SLOG(LOG_DEBUG, TAG_STTC, " ");
1450
1451         return ret;
1452 }
1453
1454 int stt_start_audio_streaming(stt_h stt, const char* language, const char* type)
1455 {
1456         stt_client_s* client = NULL;
1457         int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
1458         if (STT_ERROR_NONE != tmp)
1459                 return tmp;
1460
1461         RETVM_IF(STT_INTERNAL_STATE_NONE != client->internal_state, STT_ERROR_IN_PROGRESS_TO_RECORDING, "[ERROR] Invalid State : Internal state is NOT none : %d", client->current_state);
1462         SLOG(LOG_INFO, TAG_STTC, "===== STT START AUDIO STREAMING");
1463
1464         int ret = STT_ERROR_NONE;
1465         char appid[1024] = {0, };
1466         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
1467
1468         if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1469                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID"); //LCOV_EXCL_LINE
1470         } else {
1471                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1472         }
1473
1474         const char* language_param = NULL == language ? "default" : language;
1475         if (true == client->credential_needed && NULL == client->credential) {
1476                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id); //LCOV_EXCL_LINE
1477                 return STT_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE
1478         }
1479
1480         client->internal_state = STT_INTERNAL_STATE_STARTING;
1481         ret = stt_dbus_request_start_audio_streaming(client->uid, language_param, type, client->silence, appid, client->credential, client->audio_id);
1482         if (0 != ret) {
1483                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
1484                 client->internal_state = STT_INTERNAL_STATE_NONE;
1485                 return ret;
1486         }
1487
1488         client->is_streaming = true;
1489         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start audio streaming is successful but not done");
1490         SLOG(LOG_INFO, TAG_STTC, "=====");
1491         return STT_ERROR_NONE;
1492 }
1493
1494 int stt_send_audio_streaming(stt_h stt, const char* data, size_t data_size)
1495 {
1496         stt_client_s* client = NULL;
1497         int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
1498         if (STT_ERROR_NONE != tmp)
1499                 return tmp;
1500
1501         SLOG(LOG_INFO, TAG_STTC, "===== STT SEND AUDIO STREAMING");
1502
1503         if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1504                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1505                 return STT_ERROR_IN_PROGRESS_TO_READY;
1506         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1507                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1508                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1509         }
1510
1511         if (false == client->is_streaming) {
1512                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is not activated.");
1513                 return STT_ERROR_OPERATION_FAILED;
1514         }
1515
1516         int ret = stt_dbus_request_send_audio_streaming(client->uid, data, data_size);
1517         if (STT_ERROR_NONE != ret) {
1518                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
1519                 return ret;
1520         }
1521
1522         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Send audio streaming is successful");
1523         SLOG(LOG_INFO, TAG_STTC, "=====");
1524         return STT_ERROR_NONE;
1525 }
1526
1527 int stt_stop_audio_streaming(stt_h stt)
1528 {
1529         stt_client_s* client = NULL;
1530         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
1531         if (STT_ERROR_NONE != temp)
1532                 return temp;
1533
1534         SLOG(LOG_INFO, TAG_STTC, "===== STT STOP AUDIO STREAMING");
1535
1536         if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1537                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1538                 return STT_ERROR_IN_PROGRESS_TO_READY;
1539         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1540                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1541                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1542         }
1543
1544         if (false == client->is_streaming) {
1545                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is not activated.");
1546                 return STT_ERROR_OPERATION_FAILED;
1547         }
1548
1549         client->internal_state = STT_INTERNAL_STATE_STOPPING;
1550         int ret = stt_dbus_request_stop_audio_streaming(client->uid);
1551         if (0 != ret) {
1552                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
1553                 client->internal_state = STT_INTERNAL_STATE_NONE;
1554                 return ret;
1555         }
1556
1557         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1558         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1559         return STT_ERROR_NONE;
1560 }
1561
1562 int stt_cancel(stt_h stt)
1563 {
1564         stt_client_s* client = NULL;
1565         int temp = __stt_check_precondition(stt, &client);
1566         if (STT_ERROR_NONE != temp)
1567                 return temp;
1568
1569         SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL");
1570
1571         /* check state */
1572         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1573                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state); //LCOV_EXCL_LINE
1574                 return STT_ERROR_INVALID_STATE;
1575         }
1576
1577         if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1578                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1579                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1580         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1581                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1582                 return STT_ERROR_IN_PROGRESS_TO_READY;
1583         }
1584
1585         client->internal_state = STT_INTERNAL_STATE_CANCELING;
1586         int ret = stt_dbus_request_cancel(client->uid);
1587         if (0 != ret) {
1588                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1589                 client->internal_state = STT_INTERNAL_STATE_NONE;
1590         } else {
1591                 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1592         }
1593
1594         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1595         SLOG(LOG_DEBUG, TAG_STTC, " ");
1596
1597         return ret;
1598 }
1599
1600 //LCOV_EXCL_START
1601 int __stt_cb_set_volume(unsigned int uid, float volume)
1602 {
1603         stt_client_s* client = NULL;
1604
1605         client = stt_client_get_by_uid(uid);
1606         if (NULL == client) {
1607                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1608                 return STT_ERROR_INVALID_PARAMETER;
1609         }
1610
1611         if (STT_STATE_RECORDING != client->current_state) {
1612                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1613                 return STT_ERROR_INVALID_STATE;
1614         }
1615
1616         g_volume_db = volume;
1617         SLOG(LOG_INFO, TAG_STTC, "Set volume (%f)", g_volume_db);
1618
1619         return 0;
1620 }
1621 //LCOV_EXCL_STOP
1622
1623 int stt_get_recording_volume(stt_h stt, float* volume)
1624 {
1625         stt_client_s* client = NULL;
1626         int temp = __stt_check_precondition(stt, &client);
1627         if (STT_ERROR_NONE != temp)
1628                 return temp;
1629
1630         RETVM_IF(NULL == volume, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1631         RETVM_IF(STT_STATE_RECORDING != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1632
1633         *volume = g_volume_db;
1634
1635         SLOG(LOG_INFO, TAG_STTC, "Get recording volume (%f)", *volume);
1636
1637         return STT_ERROR_NONE;
1638 }
1639
1640 //LCOV_EXCL_START
1641 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1642 {
1643         stt_client_s* client = (stt_client_s*)user_data;
1644
1645         /* check handle */
1646         if (NULL == client) {
1647                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1648                 return EINA_FALSE;
1649         }
1650
1651         if (NULL != client->result_time_cb) {
1652                 SLOG(LOG_INFO, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1653                         index, event, text, start_time, end_time);
1654                 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1655                         text, start_time, end_time, client->result_time_user_data);
1656         } else {
1657                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1658                 return false;
1659         }
1660
1661         return true;
1662 }
1663 //LCOV_EXCL_STOP
1664
1665 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1666 {
1667         stt_client_s* client = NULL;
1668         int temp = __stt_check_precondition(stt, &client);
1669         if (STT_ERROR_NONE != temp)
1670                 return temp;
1671
1672         SLOG(LOG_INFO, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1673         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1674
1675         client->result_time_cb = callback;
1676         client->result_time_user_data = user_data;
1677
1678         int ret = -1;
1679         ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1680         ret = __stt_convert_config_error_code(ret);
1681         if (0 != ret) {
1682                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret)); //LCOV_EXCL_LINE
1683         }
1684
1685         client->result_time_cb = NULL;
1686         client->result_time_user_data = NULL;
1687
1688         SLOG(LOG_INFO, TAG_STTC, "=====");
1689         SLOG(LOG_DEBUG, TAG_STTC, " ");
1690
1691         return ret;
1692 }
1693
1694 static void __stt_notify_error(void *data)
1695 {
1696         stt_client_s* client = (stt_client_s*)data;
1697
1698         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1699         RETM_IF(NULL == client, "[ERROR] Fail to notify error : A handle is not valid");
1700
1701         if (NULL == stt_client_get_by_uid(client->uid))
1702                 return;
1703
1704         if (NULL != client->error_cb) {
1705                 stt_client_use_callback(client);
1706                 g_err_callback_status = true;
1707                 client->error_cb(client->stt, client->reason, client->error_user_data);
1708                 g_err_callback_status = false;
1709                 stt_client_not_use_callback(client);
1710                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1711         } else {
1712                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null"); //LCOV_EXCL_LINE
1713         }
1714 }
1715
1716 int __stt_cb_error(unsigned int uid, int reason, char* err_msg)
1717 {
1718         if (-1 == uid) {
1719                 GList* client_list = NULL;
1720                 client_list = stt_client_get_client_list();
1721
1722                 GList *iter = NULL;
1723                 stt_client_s *data = NULL;
1724
1725                 if (g_list_length(client_list) > 0) {
1726                         /* Get a first item */
1727                         iter = g_list_first(client_list);
1728
1729                         while (NULL != iter) {
1730                                 data = iter->data;
1731
1732                                 data->reason = reason;
1733                                 data->internal_state = STT_INTERNAL_STATE_NONE;
1734
1735                                 free(data->err_msg);
1736                                 data->err_msg = NULL;
1737
1738                                 if (NULL != err_msg)
1739                                         data->err_msg = strdup(err_msg);
1740
1741                                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1742
1743                                 if (NULL != data->error_cb) {
1744                                         ecore_main_loop_thread_safe_call_async(__stt_notify_error, data);
1745                                 } else {
1746                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1747                                 }
1748
1749                                 if (STT_ERROR_SERVICE_RESET == reason) {
1750                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1751
1752                                         data->current_state = STT_STATE_CREATED;
1753                                         if (0 != stt_prepare(data->stt)) {
1754                                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1755                                         }
1756                                 }
1757
1758                                 /* Next item */
1759                                 iter = g_list_next(iter);
1760                         }
1761                 }
1762         } else {
1763                 //LCOV_EXCL_START
1764                 stt_client_s* client = stt_client_get_by_uid(uid);
1765                 if (NULL == client) {
1766                         SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
1767                         return -1;
1768                 }
1769
1770                 client->reason = reason;
1771                 client->internal_state = STT_INTERNAL_STATE_NONE;
1772
1773                 free(client->err_msg);
1774                 client->err_msg = NULL;
1775
1776                 if (NULL != err_msg)
1777                         client->err_msg = strdup(err_msg);
1778
1779                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1780
1781                 if (NULL != client->error_cb) {
1782                         ecore_main_loop_thread_safe_call_async(__stt_notify_error, client);
1783                 } else {
1784                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1785                 }
1786
1787                 if (STT_ERROR_SERVICE_RESET == reason) {
1788                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset"); //LCOV_EXCL_LINE
1789
1790                         client->current_state = STT_STATE_CREATED;
1791                         if (0 != stt_prepare(client->stt)) {
1792                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare"); //LCOV_EXCL_LINE
1793                         }
1794                 }
1795                 //LCOV_EXCL_STOP
1796         }
1797
1798         return 0;
1799 }
1800
1801 static void __stt_notify_state_changed(void *data)
1802 {
1803         stt_client_s* client = (stt_client_s*)data;
1804
1805         /* check handle */
1806         RETM_IF(NULL == client, "[ERROR] Fail to notify error : A handle is not valid");
1807         RET_IF(NULL == stt_client_get_by_uid(client->uid));
1808
1809         if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
1810                 client->internal_state = STT_INTERNAL_STATE_NONE;
1811                 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
1812         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == client->current_state) {
1813                 client->internal_state = STT_INTERNAL_STATE_NONE;
1814                 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
1815         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
1816                 client->internal_state = STT_INTERNAL_STATE_NONE;
1817                 SLOG(LOG_INFO, TAG_STTC, "Internal state change to NONE");
1818         }
1819
1820         if (NULL != client->state_changed_cb) {
1821                 stt_client_use_callback(client);
1822                 client->state_changed_cb(client->stt, client->before_state,
1823                         client->current_state, client->state_changed_user_data);
1824                 stt_client_not_use_callback(client);
1825                 SLOG(LOG_INFO, TAG_STTC, "State changed callback is called, State(%d)", client->current_state);
1826         } else {
1827                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null, State(%d)", client->current_state); //LCOV_EXCL_LINE
1828         }
1829
1830         return;
1831 }
1832
1833 int __stt_cb_result(unsigned int uid, int event, char** data, int data_count, const char* msg)
1834 {
1835         stt_client_s* client = stt_client_get_by_uid(uid);
1836         RETVM_IF(NULL == client, STT_ERROR_INVALID_PARAMETER, "Handle is NOT valid. uid(%u)", uid);
1837
1838         if (NULL != msg)
1839                 SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result Message = %s", msg);
1840
1841         for (int i = 0; i < data_count; i++) {
1842                 if (NULL != data[i])
1843                         SECURE_SLOG(LOG_INFO, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
1844         }
1845
1846         if (NULL != client->recognition_result_cb) {
1847                 stt_client_use_callback(client);
1848                 client->recognition_result_cb(client->stt, event, (const char**)data, data_count,
1849                         msg, client->recognition_result_user_data);
1850                 stt_client_not_use_callback(client);
1851                 SLOG(LOG_INFO, TAG_STTC, "client recognition result callback called");
1852         } else {
1853                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
1854         }
1855
1856         stt_config_mgr_remove_time_info_file();
1857
1858         if (STT_RESULT_EVENT_FINAL_RESULT == event || STT_RESULT_EVENT_ERROR == event) {
1859                 client->before_state = client->current_state;
1860                 client->current_state = STT_STATE_READY;
1861
1862                 if (NULL != client->state_changed_cb) {
1863                         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
1864                 } else {
1865                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null"); //LCOV_EXCL_LINE
1866                 }
1867         }
1868
1869         return STT_ERROR_NONE;
1870 }
1871
1872 int __stt_cb_set_state(unsigned int uid, int state)
1873 {
1874         stt_client_s* client = stt_client_get_by_uid(uid);
1875         if (NULL == client) {
1876                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
1877                 return -1;
1878         }
1879
1880         stt_state_e state_from_daemon = (stt_state_e)state;
1881
1882         if (client->current_state == state_from_daemon) {
1883                 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
1884                 return 0;
1885         }
1886
1887         client->before_state = client->current_state;
1888         client->current_state = state_from_daemon;
1889
1890         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
1891         return 0;
1892 }
1893
1894 //LCOV_EXCL_START
1895 static void __stt_notify_speech_status(void *data)
1896 {
1897         stt_client_s* client = (stt_client_s*)data;
1898
1899         /* check handle */
1900         RETM_IF(NULL == client, "[ERROR] Fail to notify speech status : A handle is not valid");
1901         RET_IF(NULL == stt_client_get_by_uid(client->uid));
1902
1903         if (NULL != client->speech_status_cb) {
1904                 stt_client_use_callback(client);
1905                 client->speech_status_cb(client->stt, client->speech_status, client->speech_status_user_data);
1906                 stt_client_not_use_callback(client);
1907                 SLOG(LOG_INFO, TAG_STTC, "Speech status callback is called"); //LCOV_EXCL_LINE
1908         } else {
1909                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Speech status callback is null"); //LCOV_EXCL_LINE
1910         }
1911
1912         return;
1913 }
1914
1915 int __stt_cb_speech_status(unsigned int uid, int status)
1916 {
1917         stt_client_s* client = stt_client_get_by_uid(uid);
1918         if (NULL == client) {
1919                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found"); //LCOV_EXCL_LINE
1920                 return -1;
1921         }
1922
1923         client->speech_status = status;
1924
1925         ecore_main_loop_thread_safe_call_async(__stt_notify_speech_status, client);
1926         return 0;
1927 }
1928 //LCOV_EXCL_STOP
1929
1930 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
1931 {
1932         stt_client_s* client = NULL;
1933         int temp = __stt_check_precondition(stt, &client);
1934         if (STT_ERROR_NONE != temp)
1935                 return temp;
1936
1937         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1938         RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1939
1940         SLOG(LOG_INFO, TAG_STTC, "[INFO] Set recognition result cb");
1941
1942         client->recognition_result_cb = callback;
1943         client->recognition_result_user_data = user_data;
1944
1945         return 0;
1946 }
1947
1948 int stt_unset_recognition_result_cb(stt_h stt)
1949 {
1950         stt_client_s* client = NULL;
1951         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
1952         if (STT_ERROR_NONE != temp)
1953                 return temp;
1954
1955         SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset recognition result cb");
1956
1957         client->recognition_result_cb = NULL;
1958         client->recognition_result_user_data = NULL;
1959
1960         return 0;
1961 }
1962
1963 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
1964 {
1965         stt_client_s* client = NULL;
1966         int temp = __stt_check_precondition(stt, &client);
1967         if (STT_ERROR_NONE != temp)
1968                 return temp;
1969
1970         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
1971         RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1972
1973         SLOG(LOG_INFO, TAG_STTC, "[INFO] Set state changed cb");
1974
1975         client->state_changed_cb = callback;
1976         client->state_changed_user_data = user_data;
1977
1978         return 0;
1979 }
1980
1981 int stt_unset_state_changed_cb(stt_h stt)
1982 {
1983         stt_client_s* client = NULL;
1984         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
1985         if (STT_ERROR_NONE != temp)
1986                 return temp;
1987
1988         SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset state changed cb");
1989
1990         client->state_changed_cb = NULL;
1991         client->state_changed_user_data = NULL;
1992
1993         return 0;
1994 }
1995
1996 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
1997 {
1998         stt_client_s* client = NULL;
1999         int temp = __stt_check_precondition(stt, &client);
2000         if (STT_ERROR_NONE != temp)
2001                 return temp;
2002
2003         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2004         RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2005
2006         SLOG(LOG_INFO, TAG_STTC, "[INFO] Set error cb");
2007
2008         client->error_cb = callback;
2009         client->error_user_data = user_data;
2010
2011         return 0;
2012 }
2013
2014 int stt_unset_error_cb(stt_h stt)
2015 {
2016         stt_client_s* client = NULL;
2017         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2018         if (STT_ERROR_NONE != temp)
2019                 return temp;
2020
2021         SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset error cb");
2022
2023         client->error_cb = NULL;
2024         client->error_user_data = NULL;
2025
2026         return 0;
2027 }
2028
2029 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2030 {
2031         stt_client_s* client = NULL;
2032         int temp = __stt_check_precondition(stt, &client);
2033         if (STT_ERROR_NONE != temp)
2034                 return temp;
2035
2036         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2037         RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2038
2039         SLOG(LOG_INFO, TAG_STTC, "[INFO] Set default language changed cb");
2040
2041         client->default_lang_changed_cb = callback;
2042         client->default_lang_changed_user_data = user_data;
2043
2044         return 0;
2045 }
2046
2047 int stt_unset_default_language_changed_cb(stt_h stt)
2048 {
2049         stt_client_s* client = NULL;
2050         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2051         if (STT_ERROR_NONE != temp)
2052                 return temp;
2053
2054         SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset default language changed cb");
2055
2056         client->default_lang_changed_cb = NULL;
2057         client->default_lang_changed_user_data = NULL;
2058
2059         return 0;
2060 }
2061
2062 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2063 {
2064         stt_client_s* client = NULL;
2065         int temp = __stt_check_precondition(stt, &client);
2066         if (STT_ERROR_NONE != temp)
2067                 return temp;
2068
2069         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2070         RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2071
2072         SLOG(LOG_INFO, TAG_STTC, "[INFO] Set engine changed cb");
2073
2074         client->engine_changed_cb = callback;
2075         client->engine_changed_user_data = user_data;
2076
2077         return 0;
2078 }
2079
2080 int stt_unset_engine_changed_cb(stt_h stt)
2081 {
2082         stt_client_s* client = NULL;
2083         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2084         if (STT_ERROR_NONE != temp)
2085                 return temp;
2086
2087         SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset engine changed cb");
2088
2089         client->engine_changed_cb = NULL;
2090         client->engine_changed_user_data = NULL;
2091
2092         return 0;
2093 }
2094
2095 int stt_set_speech_status_cb(stt_h stt, stt_speech_status_cb callback, void* user_data)
2096 {
2097         stt_client_s* client = NULL;
2098         int temp = __stt_check_precondition(stt, &client);
2099         if (STT_ERROR_NONE != temp)
2100                 return temp;
2101
2102         RETVM_IF(NULL == callback, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2103         RETVM_IF(STT_STATE_CREATED != client->current_state, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2104
2105         SLOG(LOG_INFO, TAG_STTC, "[INFO] Set speech status cb");
2106
2107         client->speech_status_cb = callback;
2108         client->speech_status_user_data = user_data;
2109
2110         return 0;
2111 }
2112
2113 int stt_unset_speech_status_cb(stt_h stt)
2114 {
2115         stt_client_s* client = NULL;
2116         int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_CREATED);
2117         if (STT_ERROR_NONE != temp)
2118                 return temp;
2119
2120         SLOG(LOG_INFO, TAG_STTC, "[INFO] Unset speech status cb");
2121
2122         client->speech_status_cb = NULL;
2123         client->speech_status_user_data = NULL;
2124
2125         return 0;
2126 }
2127
2128 //LCOV_EXCL_START
2129 int stt_start_file(stt_h stt, const char* language, const char* type, const char* filepath)
2130 {
2131         stt_client_s* client = NULL;
2132         int tmp = __stt_check_precondition(stt, &client);
2133         if (STT_ERROR_NONE != tmp)
2134                 return tmp;
2135
2136         RETVM_IF(NULL == filepath, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2137         RETVM_IF(client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
2138
2139         SLOG(LOG_INFO, TAG_STTC, "===== STT START FILE");
2140
2141         if (STT_INTERNAL_STATE_NONE != client->internal_state) {
2142                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
2143                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2144         }
2145
2146         int ret = -1;
2147         char appid[1024] = {0, };
2148         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
2149
2150         if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
2151                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
2152         } else {
2153                 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Current app id is %s", appid);
2154         }
2155
2156         char* temp = NULL;
2157         if (NULL == language) {
2158                 temp = strdup("default");
2159         } else {
2160                 temp = strdup(language);
2161         }
2162
2163         RETVM_IF(NULL == temp, STT_ERROR_OUT_OF_MEMORY, "[ERROR] Fail to allocate memory");
2164
2165         if (true == client->credential_needed && NULL == client->credential) {
2166                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
2167                 free(temp);
2168                 temp = NULL;
2169                 return STT_ERROR_PERMISSION_DENIED;
2170         }
2171
2172         client->internal_state = STT_INTERNAL_STATE_STARTING;
2173         ret = stt_dbus_request_start_file(client->uid, temp, type, client->silence, appid, client->credential, filepath);
2174         if (0 != ret) {
2175                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start file : %s", __stt_get_error_code(ret));
2176                 client->internal_state = STT_INTERNAL_STATE_NONE;
2177         } else {
2178                 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
2179         }
2180
2181         free(temp);
2182         temp = NULL;
2183
2184         SLOG(LOG_DEBUG, TAG_STTC, "=====");
2185         SLOG(LOG_DEBUG, TAG_STTC, " ");
2186
2187         return ret;
2188 }
2189
2190 int stt_cancel_file(stt_h stt)
2191 {
2192         stt_client_s* client = NULL;
2193         int temp = __stt_check_precondition(stt, &client);
2194         if (STT_ERROR_NONE != temp)
2195                 return temp;
2196
2197         SLOG(LOG_INFO, TAG_STTC, "===== STT CANCEL FILE");
2198
2199         /* check state */
2200         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
2201                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
2202                 return STT_ERROR_INVALID_STATE;
2203         }
2204
2205         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
2206                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
2207                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2208         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
2209                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
2210                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
2211         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
2212                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
2213                 return STT_ERROR_IN_PROGRESS_TO_READY;
2214         }
2215
2216         client->internal_state = STT_INTERNAL_STATE_CANCELING;
2217         int ret = stt_dbus_request_cancel_file(client->uid);
2218         if (0 != ret) {
2219                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel file : %s", __stt_get_error_code(ret));
2220                 client->internal_state = STT_INTERNAL_STATE_NONE;
2221         } else {
2222                 SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Cancel file is successful but not done");
2223         }
2224
2225         SLOG(LOG_DEBUG, TAG_STTC, "=====");
2226         SLOG(LOG_DEBUG, TAG_STTC, " ");
2227
2228         return ret;
2229 }
2230 //LCOV_EXCL_STOP
2231
2232 void __sound_stream_ducking_state_changed_cb(sound_stream_ducking_h stream_ducking, bool is_ducked, void *user_data)
2233 {
2234         SLOG(LOG_DEBUG, TAG_STTC, "@@@ ducking state changed cb");
2235         SLOG(LOG_DEBUG, TAG_STTC, "[Volume] is ducked : %d", is_ducked);
2236         // ducking_flag = true;
2237         return;
2238 }
2239
2240 static char* __get_ducking_stream(sound_stream_type_e stream_type)
2241 {
2242         if (SOUND_STREAM_TYPE_MEDIA == stream_type)
2243                 return "Media stream";
2244         else if (SOUND_STREAM_TYPE_SYSTEM == stream_type)
2245                 return "System stream";
2246         else if (SOUND_STREAM_TYPE_NOTIFICATION == stream_type)
2247                 return "Notification stream";
2248         else if (SOUND_STREAM_TYPE_ALARM == stream_type)
2249                 return "Alarm stream";
2250
2251         return "Non matched stream";
2252 }
2253
2254 static int __activate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h, double bg_volume_ratio)
2255 {
2256         bool is_ducked = false;
2257         int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2258         if (is_ducked) {
2259                 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already ducked", __get_ducking_stream(stream_type));
2260         } else {
2261                 ret = sound_manager_activate_ducking(stream_ducking_h, SND_MGR_DUCKING_DURATION, bg_volume_ratio);
2262                 if (SOUND_MANAGER_ERROR_NONE != ret) {
2263                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to activate ducking for %s", __get_ducking_stream(stream_type));
2264                 } else {
2265                         SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Activate ducking for %s", __get_ducking_stream(stream_type));
2266                 }
2267         }
2268         return ret;
2269 }
2270
2271 static void __change_background_volume(stt_system_volume_event_e volume_event)
2272 {
2273         double bg_volume_ratio = 0.0;
2274
2275         if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD == volume_event) {
2276                 bg_volume_ratio = STT_BG_VOLUME_RATIO_FARFIELD;
2277         } else if (STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD == volume_event) {
2278                 bg_volume_ratio = STT_BG_VOLUME_RATIO_NEARFIELD;
2279         }
2280
2281         SLOG(LOG_INFO, TAG_STTC, "[Volume] volume ratio(%lf)", bg_volume_ratio);
2282
2283         if (1.0 > bg_volume_ratio) {
2284                 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking, bg_volume_ratio);
2285                 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking, bg_volume_ratio);
2286                 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking, bg_volume_ratio);
2287                 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking, bg_volume_ratio);
2288         }
2289 }
2290
2291 static int __deactivate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h)
2292 {
2293         bool is_ducked = false;
2294         int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
2295         if (!is_ducked) {
2296                 SLOG(LOG_DEBUG, TAG_STTC, "[Volume] The %s is already recovered from ducking", __get_ducking_stream(stream_type));
2297         } else {
2298                 ret = sound_manager_deactivate_ducking(stream_ducking_h);
2299                 if (SOUND_MANAGER_ERROR_NONE != ret) {
2300                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to deactivate ducking for %s", __get_ducking_stream(stream_type));
2301                 } else {
2302                         SLOG(LOG_INFO, TAG_STTC, "[Volume SUCCESS] Deactivate ducking for %s", __get_ducking_stream(stream_type));
2303                 }
2304         }
2305         return ret;
2306 }
2307
2308 static void __recover_background_volume()
2309 {
2310         SLOG(LOG_INFO, TAG_STTC, "[Volume] background volume recover");
2311
2312         __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking);
2313         __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking);
2314         __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking);
2315         __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking);
2316 }
2317
2318 int __create_ducking_handle(void)
2319 {
2320         int ret = -1;
2321         if (NULL == g_media_stream_ducking) {
2322                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_MEDIA, __sound_stream_ducking_state_changed_cb, NULL, &g_media_stream_ducking);
2323                 if (SOUND_MANAGER_ERROR_NONE != ret)
2324                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for type media, ret(%d)", ret);
2325         } else {
2326                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2327         }
2328
2329         if (NULL == g_system_stream_ducking) {
2330                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_SYSTEM, __sound_stream_ducking_state_changed_cb, NULL, &g_system_stream_ducking);
2331                 if (SOUND_MANAGER_ERROR_NONE != ret)
2332                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for system type, ret(%d)", ret);
2333         } else {
2334                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2335         }
2336
2337         if (NULL == g_notification_stream_ducking) {
2338                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_NOTIFICATION, __sound_stream_ducking_state_changed_cb, NULL, &g_notification_stream_ducking);
2339                 if (SOUND_MANAGER_ERROR_NONE != ret)
2340                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for notification type, ret(%d)", ret);
2341         } else {
2342                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2343         }
2344
2345         if (NULL == g_alarm_stream_ducking) {
2346                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_ALARM, __sound_stream_ducking_state_changed_cb, NULL, &g_alarm_stream_ducking);
2347                 if (SOUND_MANAGER_ERROR_NONE != ret)
2348                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to create stream ducking for alarm type, ret(%d)", ret);
2349         } else {
2350                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2351         }
2352
2353         return ret;
2354 }
2355
2356 int stt_change_system_volume(stt_h stt, stt_system_volume_event_e volume_event)
2357 {
2358         SLOG(LOG_DEBUG, TAG_STTC, "[STT] Change system volume, volume_event(%d)", volume_event);
2359
2360         stt_client_s* client = NULL;
2361         int temp = __stt_check_precondition(stt, &client);
2362         if (STT_ERROR_NONE != temp)
2363                 return temp;
2364
2365         RETVM_IF((STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD > volume_event || STT_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD < volume_event),
2366                          STT_ERROR_INVALID_PARAMETER,
2367                          "[ERROR] Invalid volume event (%d)",
2368                          volume_event);
2369
2370         /* check state */
2371         if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2372                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2373                 return STT_ERROR_INVALID_STATE;
2374         }
2375
2376         /* change system volume */
2377         int ret = __create_ducking_handle();
2378         if (0 != ret) {
2379                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create volume handle");
2380         } else {
2381                 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to create volume handle");
2382         }
2383
2384         __change_background_volume(volume_event);
2385         return STT_ERROR_NONE;
2386 }
2387
2388 int __destroy_ducking_handle(void)
2389 {
2390         int ret = -1;
2391         if (g_media_stream_ducking) {
2392                 ret = sound_manager_destroy_stream_ducking(g_media_stream_ducking);
2393                 if (SOUND_MANAGER_ERROR_NONE != ret)
2394                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy media stream ducking, ret(%d)", ret);
2395                 g_media_stream_ducking = NULL;
2396         } else {
2397                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for media stream is already created");
2398         }
2399
2400         if (g_system_stream_ducking) {
2401                 ret = sound_manager_destroy_stream_ducking(g_system_stream_ducking);
2402                 if (SOUND_MANAGER_ERROR_NONE != ret)
2403                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy system stream ducking, ret(%d)", ret);
2404                 g_system_stream_ducking = NULL;
2405         } else {
2406                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for system stream is already created");
2407         }
2408
2409         if (g_notification_stream_ducking) {
2410                 ret = sound_manager_destroy_stream_ducking(g_notification_stream_ducking);
2411                 if (SOUND_MANAGER_ERROR_NONE != ret)
2412                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy notification stream ducking, ret(%d)", ret);
2413                 g_notification_stream_ducking = NULL;
2414         } else {
2415                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for notification stream is already created");
2416         }
2417
2418         if (g_alarm_stream_ducking) {
2419                 ret = sound_manager_destroy_stream_ducking(g_alarm_stream_ducking);
2420                 if (SOUND_MANAGER_ERROR_NONE != ret)
2421                         SLOG(LOG_WARN, TAG_STTC, "[Volume WARNING] Fail to destroy alarm stream ducking, ret(%d)", ret);
2422                 g_alarm_stream_ducking = NULL;
2423         } else {
2424                 SLOG(LOG_INFO, TAG_STTC, "[Volume INFO] Ducking handle for alarm stream is already created");
2425         }
2426         return ret;
2427 }
2428
2429 int stt_recover_system_volume(stt_h stt)
2430 {
2431         SLOG(LOG_DEBUG, TAG_STTC, "[STT] recover system volume");
2432
2433         stt_client_s* client = NULL;
2434         int temp = __stt_check_precondition(stt, &client);
2435         if (STT_ERROR_NONE != temp)
2436                 return temp;
2437
2438         /* check state */
2439         if (client->current_state != STT_STATE_READY && client->current_state != STT_STATE_CREATED) {
2440                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY nor CREATED", client->current_state);
2441                 return STT_ERROR_INVALID_STATE;
2442         }
2443
2444         /* recover volume */
2445         __recover_background_volume();
2446         int ret = __destroy_ducking_handle();
2447         if (0 != ret) {
2448                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to destroy volume handle");
2449         } else {
2450                 SLOG(LOG_INFO, TAG_STTC, "[DEBUG] Success to destroy volume handle");
2451         }
2452
2453         return STT_ERROR_NONE;
2454 }
2455
2456 int stt_set_audio_type(stt_h stt, const char *audio_id)
2457 {
2458         stt_client_s* client = NULL;
2459         int temp = __stt_check_precondition(stt, &client);
2460         if (STT_ERROR_NONE != temp)
2461                 return temp;
2462
2463         RETVM_IF(NULL == audio_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2464         RETVM_IF(STT_STATE_CREATED != client->current_state && client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created' or 'Ready'", client->current_state);
2465
2466         SLOG(LOG_INFO, TAG_STTC, "[INFO] Set audio type(%s)", audio_id);
2467
2468         free(client->audio_id);
2469         client->audio_id = strdup(audio_id);
2470
2471         return STT_ERROR_NONE;
2472 }
2473
2474 int stt_get_audio_type(stt_h stt, char **audio_id)
2475 {
2476         stt_client_s* client = NULL;
2477         int temp = __stt_check_precondition(stt, &client);
2478         if (STT_ERROR_NONE != temp)
2479                 return temp;
2480
2481         RETVM_IF(NULL == audio_id, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
2482         RETVM_IF(STT_STATE_CREATED != client->current_state && client->current_state != STT_STATE_READY, STT_ERROR_INVALID_STATE, "[ERROR] Current state(%d) is not 'Created' or 'Ready'", client->current_state);
2483
2484         *audio_id = strdup(client->audio_id);
2485         SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Get audio type(%s)", *audio_id);
2486
2487         return STT_ERROR_NONE;
2488 }