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