b082568226ab546ba49d5a81d0c74ef99ce42d5e
[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         client->internal_state = STT_INTERNAL_STATE_STARTING;
1596         ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential);
1597         if (0 != ret) {
1598                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret));
1599                 client->internal_state = STT_INTERNAL_STATE_NONE;
1600         } else {
1601                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
1602         }
1603
1604         if (NULL != temp)       free(temp);
1605
1606         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1607         SLOG(LOG_DEBUG, TAG_STTC, " ");
1608
1609         return ret;
1610 }
1611
1612 int stt_stop(stt_h stt)
1613 {
1614         stt_client_s* client = NULL;
1615         if (0 != __stt_get_feature_enabled()) {
1616                 return STT_ERROR_NOT_SUPPORTED;
1617         }
1618         if (0 != __stt_check_privilege()) {
1619                 return STT_ERROR_PERMISSION_DENIED;
1620         }
1621         if (0 != __stt_check_handle(stt, &client)) {
1622                 return STT_ERROR_INVALID_PARAMETER;
1623         }
1624
1625         SLOG(LOG_DEBUG, TAG_STTC, "===== STT STOP");
1626
1627         /* check state */
1628         if (client->current_state != STT_STATE_RECORDING) {
1629                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Current state(%d) is NOT RECORDING", client->current_state);
1630                 return STT_ERROR_INVALID_STATE;
1631         }
1632
1633         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1634                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1635                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1636         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1637                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1638                 return STT_ERROR_IN_PROGRESS_TO_READY;
1639         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1640                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1641                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1642         }
1643
1644         client->internal_state = STT_INTERNAL_STATE_STOPPING;
1645         int ret = stt_dbus_request_stop(client->uid);
1646         if (0 != ret) {
1647                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret));
1648                 client->internal_state = STT_INTERNAL_STATE_NONE;
1649         } else {
1650                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1651         }
1652
1653         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1654         SLOG(LOG_DEBUG, TAG_STTC, " ");
1655
1656         return ret;
1657 }
1658
1659
1660 int stt_cancel(stt_h stt)
1661 {
1662         stt_client_s* client = NULL;
1663         if (0 != __stt_get_feature_enabled()) {
1664                 return STT_ERROR_NOT_SUPPORTED;
1665         }
1666         if (0 != __stt_check_privilege()) {
1667                 return STT_ERROR_PERMISSION_DENIED;
1668         }
1669         if (0 != __stt_check_handle(stt, &client)) {
1670                 return STT_ERROR_INVALID_PARAMETER;
1671         }
1672
1673         SLOG(LOG_DEBUG, TAG_STTC, "===== STT CANCEL");
1674
1675         /* check state */
1676         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1677                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
1678                 return STT_ERROR_INVALID_STATE;
1679         }
1680
1681         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1682                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1683                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1684         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1685                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1686                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1687         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1688                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1689                 return STT_ERROR_IN_PROGRESS_TO_READY;
1690         }
1691
1692         client->internal_state = STT_INTERNAL_STATE_CANCELING;
1693         int ret = stt_dbus_request_cancel(client->uid);
1694         if (0 != ret) {
1695                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret));
1696                 client->internal_state = STT_INTERNAL_STATE_NONE;
1697         } else {
1698                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1699         }
1700
1701         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1702         SLOG(LOG_DEBUG, TAG_STTC, " ");
1703
1704         return ret;
1705 }
1706
1707 int __stt_cb_set_volume(int uid, float volume)
1708 {
1709         stt_client_s* client = NULL;
1710
1711         client = stt_client_get_by_uid(uid);
1712         if (NULL == client) {
1713                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1714                 return STT_ERROR_INVALID_PARAMETER;
1715         }
1716
1717         if (STT_STATE_RECORDING != client->current_state) {
1718                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1719                 return STT_ERROR_INVALID_STATE;
1720         }
1721
1722         g_volume_db = volume;
1723         SLOG(LOG_DEBUG, TAG_STTC, "Set volume (%f)", g_volume_db);
1724
1725         return 0;
1726 }
1727
1728 int stt_get_recording_volume(stt_h stt, float* volume)
1729 {
1730         stt_client_s* client = NULL;
1731         if (0 != __stt_get_feature_enabled()) {
1732                 return STT_ERROR_NOT_SUPPORTED;
1733         }
1734         if (0 != __stt_check_privilege()) {
1735                 return STT_ERROR_PERMISSION_DENIED;
1736         }
1737         if (0 != __stt_check_handle(stt, &client)) {
1738                 return STT_ERROR_INVALID_PARAMETER;
1739         }
1740
1741         if (NULL == volume) {
1742                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1743                 return STT_ERROR_INVALID_PARAMETER;
1744         }
1745
1746         if (STT_STATE_RECORDING != client->current_state) {
1747                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1748                 return STT_ERROR_INVALID_STATE;
1749         }
1750
1751         *volume = g_volume_db;
1752
1753         return STT_ERROR_NONE;
1754 }
1755
1756 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1757 {
1758         stt_client_s* client = (stt_client_s*)user_data;
1759
1760         /* check handle */
1761         if (NULL == client) {
1762                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1763                 return EINA_FALSE;
1764         }
1765
1766         if (NULL != client->result_time_cb) {
1767                 SLOG(LOG_DEBUG, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1768                         index, event, text, start_time, end_time);
1769                 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1770                         text, start_time, end_time, client->result_time_user_data);
1771         } else {
1772                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1773                 return false;
1774         }
1775
1776         return true;
1777 }
1778
1779 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1780 {
1781         stt_client_s* client = NULL;
1782         if (0 != __stt_get_feature_enabled()) {
1783                 return STT_ERROR_NOT_SUPPORTED;
1784         }
1785         if (0 != __stt_check_privilege()) {
1786                 return STT_ERROR_PERMISSION_DENIED;
1787         }
1788         if (0 != __stt_check_handle(stt, &client)) {
1789                 return STT_ERROR_INVALID_PARAMETER;
1790         }
1791
1792         SLOG(LOG_DEBUG, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1793
1794         if (NULL == callback) {
1795                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1796                 return STT_ERROR_INVALID_PARAMETER;
1797         }
1798
1799         client->result_time_cb = callback;
1800         client->result_time_user_data = user_data;
1801
1802         int ret = -1;
1803         ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1804         ret = __stt_convert_config_error_code(ret);
1805         if (0 != ret) {
1806                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret));
1807         }
1808
1809         client->result_time_cb = NULL;
1810         client->result_time_user_data = NULL;
1811
1812         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1813         SLOG(LOG_DEBUG, TAG_STTC, " ");
1814
1815         return ret;
1816 }
1817
1818 static Eina_Bool __stt_notify_error(void *data)
1819 {
1820         stt_client_s* client = (stt_client_s*)data;
1821
1822         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1823
1824         /* check handle */
1825         if (NULL == client) {
1826                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1827                 return EINA_FALSE;
1828         }
1829
1830         if (NULL == stt_client_get_by_uid(client->uid))
1831                 return EINA_FALSE;
1832
1833         if (NULL != client->error_cb) {
1834                 stt_client_use_callback(client);
1835                 g_err_callback_status = true;
1836                 client->error_cb(client->stt, client->reason, client->error_user_data);
1837                 g_err_callback_status = false;
1838                 stt_client_not_use_callback(client);
1839                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1840         } else {
1841                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1842         }
1843
1844         return EINA_FALSE;
1845 }
1846
1847 int __stt_cb_error(int uid, int reason, char* err_msg)
1848 {
1849         if (-1 == uid) {
1850                 GList* client_list = NULL;
1851                 client_list = stt_client_get_client_list();
1852
1853                 GList *iter = NULL;
1854                 stt_client_s *data = NULL;
1855
1856                 if (g_list_length(client_list) > 0) {
1857                         /* Get a first item */
1858                         iter = g_list_first(client_list);
1859
1860                         while (NULL != iter) {
1861                                 data = iter->data;
1862
1863                                 data->reason = reason;
1864                                 data->internal_state = STT_INTERNAL_STATE_NONE;
1865                                 if (NULL != data->err_msg) {
1866                                         free(data->err_msg);
1867                                         data->err_msg = NULL;
1868                                 }
1869                                 if (NULL != err_msg)
1870                                         data->err_msg = strdup(err_msg);
1871
1872                                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1873
1874                                 if (NULL != data->error_cb) {
1875                                         ecore_timer_add(0, __stt_notify_error, data);
1876                                 } else {
1877                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1878                                 }
1879
1880                                 if (STT_ERROR_SERVICE_RESET == reason) {
1881                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1882
1883                                         data->current_state = STT_STATE_CREATED;
1884                                         if (0 != stt_prepare(data->stt)) {
1885                                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1886                                         }
1887                                 }
1888
1889                                 /* Next item */
1890                                 iter = g_list_next(iter);
1891                         }
1892                 }
1893         } else {
1894                 stt_client_s* client = stt_client_get_by_uid(uid);
1895                 if (NULL == client) {
1896                         SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
1897                         return -1;
1898                 }
1899
1900                 client->reason = reason;
1901                 client->internal_state = STT_INTERNAL_STATE_NONE;
1902                 if (NULL != client->err_msg) {
1903                         free(client->err_msg);
1904                         client->err_msg = NULL;
1905                 }
1906                 if (NULL != err_msg)
1907                         client->err_msg = strdup(err_msg);
1908
1909                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1910
1911                 if (NULL != client->error_cb) {
1912                         ecore_timer_add(0, __stt_notify_error, client);
1913                 } else {
1914                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1915                 }
1916
1917                 if (STT_ERROR_SERVICE_RESET == reason) {
1918                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1919
1920                         client->current_state = STT_STATE_CREATED;
1921                         if (0 != stt_prepare(client->stt)) {
1922                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1923                         }
1924                 }
1925         }
1926
1927         return 0;
1928 }
1929
1930 static void __stt_notify_state_changed(void *data)
1931 {
1932         stt_client_s* client = (stt_client_s*)data;
1933
1934         /* check handle */
1935         if (NULL == client) {
1936                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1937                 return;
1938         }
1939
1940         if (NULL == stt_client_get_by_uid(client->uid)) {
1941                 return;
1942         }
1943
1944         if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
1945                 client->internal_state = STT_INTERNAL_STATE_NONE;
1946                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
1947         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == client->current_state) {
1948                 client->internal_state = STT_INTERNAL_STATE_NONE;
1949                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
1950         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
1951                 client->internal_state = STT_INTERNAL_STATE_NONE;
1952                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
1953         }
1954
1955         if (NULL != client->state_changed_cb) {
1956                 stt_client_use_callback(client);
1957                 client->state_changed_cb(client->stt, client->before_state,
1958                         client->current_state, client->state_changed_user_data);
1959                 stt_client_not_use_callback(client);
1960                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
1961         } else {
1962                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1963         }
1964
1965         return;
1966 }
1967
1968 static Eina_Bool __stt_notify_result(void *data)
1969 {
1970         stt_client_s* client = (stt_client_s*)data;
1971
1972         /* check handle */
1973         if (NULL == client) {
1974                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1975                 return EINA_FALSE;
1976         }
1977
1978         if (NULL == stt_client_get_by_uid(client->uid)) {
1979                 return EINA_FALSE;
1980         }
1981
1982         if (NULL != client->recognition_result_cb) {
1983                 stt_client_use_callback(client);
1984                 client->recognition_result_cb(client->stt, client->event, (const char**)client->data_list, client->data_count,
1985                         client->msg, client->recognition_result_user_data);
1986                 stt_client_not_use_callback(client);
1987                 SLOG(LOG_DEBUG, TAG_STTC, "client recognition result callback called");
1988         } else {
1989                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
1990         }
1991
1992         if (NULL != client->msg) {
1993                 free(client->msg);
1994                 client->msg = NULL;
1995         }
1996
1997         if (NULL != client->data_list) {
1998                 char **temp = NULL;
1999                 temp = client->data_list;
2000
2001                 int i = 0;
2002                 for (i = 0; i < client->data_count; i++) {
2003                         if (NULL != temp[i]) {
2004                                 free(temp[i]);
2005                                 temp[i] = NULL;
2006                         } else {
2007                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
2008                         }
2009                 }
2010                 free(client->data_list);
2011                 client->data_list = NULL;
2012         }
2013
2014         client->data_count = 0;
2015
2016         stt_config_mgr_remove_time_info_file();
2017
2018         if (STT_RESULT_EVENT_FINAL_RESULT == client->event || STT_RESULT_EVENT_ERROR == client->event) {
2019                 client->before_state = client->current_state;
2020                 client->current_state = STT_STATE_READY;
2021
2022                 if (NULL != client->state_changed_cb) {
2023                         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2024                 } else {
2025                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
2026                 }
2027         }
2028
2029         return EINA_FALSE;
2030 }
2031
2032 int __stt_cb_result(int uid, int event, char** data, int data_count, const char* msg)
2033 {
2034         stt_client_s* client = NULL;
2035
2036         client = stt_client_get_by_uid(uid);
2037         if (NULL == client) {
2038                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
2039                 return STT_ERROR_INVALID_PARAMETER;
2040         }
2041
2042         if (NULL != msg)
2043                 SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result Message = %s", msg);
2044
2045         int i = 0;
2046         for (i = 0; i < data_count; i++) {
2047                 if (NULL != data[i])
2048                         SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
2049         }
2050
2051         if (NULL != client->recognition_result_cb) {
2052                 client->event = event;
2053                 if (NULL != msg) {
2054                         client->msg = strdup(msg);
2055                 }
2056
2057                 client->data_count = data_count;
2058
2059                 if (data_count > 0) {
2060                         char **temp = NULL;
2061                         temp = (char**)calloc(data_count, sizeof(char*));
2062                         if (NULL == temp) {
2063                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
2064                                 return STT_ERROR_OUT_OF_MEMORY;
2065                         }
2066
2067                         for (i = 0; i < data_count; i++) {
2068                                 if (NULL != data[i])
2069                                         temp[i] = strdup(data[i]);
2070                                 else
2071                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
2072                         }
2073
2074                         client->data_list = temp;
2075                 }
2076         } else {
2077                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] User result callback is null");
2078         }
2079
2080         ecore_timer_add(0, __stt_notify_result, client);
2081
2082         return STT_ERROR_NONE;
2083 }
2084
2085 int __stt_cb_set_state(int uid, int state)
2086 {
2087         stt_client_s* client = stt_client_get_by_uid(uid);
2088         if (NULL == client) {
2089                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
2090                 return -1;
2091         }
2092
2093         stt_state_e state_from_daemon = (stt_state_e)state;
2094
2095         if (client->current_state == state_from_daemon) {
2096                 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
2097                 return 0;
2098         }
2099
2100         client->before_state = client->current_state;
2101         client->current_state = state_from_daemon;
2102
2103         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2104         return 0;
2105 }
2106
2107 static void __stt_notify_speech_status(void *data)
2108 {
2109         stt_client_s* client = (stt_client_s*)data;
2110
2111         /* check handle */
2112         if (NULL == client) {
2113                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify speech status : A handle is not valid");
2114                 return;
2115         }
2116
2117         if (NULL == stt_client_get_by_uid(client->uid)) {
2118                 return;
2119         }
2120
2121         if (NULL != client->speech_status_cb) {
2122                 stt_client_use_callback(client);
2123                 client->speech_status_cb(client->stt, client->speech_status, client->speech_status_user_data);
2124                 stt_client_not_use_callback(client);
2125                 SLOG(LOG_DEBUG, TAG_STTC, "Speech status callback is called");
2126         } else {
2127                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Speech status callback is null");
2128         }
2129
2130         return;
2131 }
2132
2133 int __stt_cb_speech_status(int uid, int status)
2134 {
2135         stt_client_s* client = stt_client_get_by_uid(uid);
2136         if (NULL == client) {
2137                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
2138                 return -1;
2139         }
2140
2141         client->speech_status = status;
2142
2143         ecore_main_loop_thread_safe_call_async(__stt_notify_speech_status, client);
2144         return 0;
2145 }
2146
2147 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
2148 {
2149         stt_client_s* client = NULL;
2150         if (0 != __stt_get_feature_enabled()) {
2151                 return STT_ERROR_NOT_SUPPORTED;
2152         }
2153         if (0 != __stt_check_privilege()) {
2154                 return STT_ERROR_PERMISSION_DENIED;
2155         }
2156         if (0 != __stt_check_handle(stt, &client)) {
2157                 return STT_ERROR_INVALID_PARAMETER;
2158         }
2159
2160         if (callback == NULL)
2161                 return STT_ERROR_INVALID_PARAMETER;
2162
2163         if (STT_STATE_CREATED != client->current_state) {
2164                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2165                 return STT_ERROR_INVALID_STATE;
2166         }
2167
2168         client->recognition_result_cb = callback;
2169         client->recognition_result_user_data = user_data;
2170
2171         return 0;
2172 }
2173
2174 int stt_unset_recognition_result_cb(stt_h stt)
2175 {
2176         stt_client_s* client = NULL;
2177         if (0 != __stt_get_feature_enabled()) {
2178                 return STT_ERROR_NOT_SUPPORTED;
2179         }
2180         if (0 != __stt_check_privilege()) {
2181                 return STT_ERROR_PERMISSION_DENIED;
2182         }
2183         if (0 != __stt_check_handle(stt, &client)) {
2184                 return STT_ERROR_INVALID_PARAMETER;
2185         }
2186
2187         if (STT_STATE_CREATED != client->current_state) {
2188                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2189                 return STT_ERROR_INVALID_STATE;
2190         }
2191
2192         client->recognition_result_cb = NULL;
2193         client->recognition_result_user_data = NULL;
2194
2195         return 0;
2196 }
2197
2198 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
2199 {
2200         stt_client_s* client = NULL;
2201         if (0 != __stt_get_feature_enabled()) {
2202                 return STT_ERROR_NOT_SUPPORTED;
2203         }
2204         if (0 != __stt_check_privilege()) {
2205                 return STT_ERROR_PERMISSION_DENIED;
2206         }
2207         if (0 != __stt_check_handle(stt, &client)) {
2208                 return STT_ERROR_INVALID_PARAMETER;
2209         }
2210
2211         if (NULL == callback)
2212                 return STT_ERROR_INVALID_PARAMETER;
2213
2214         if (STT_STATE_CREATED != client->current_state) {
2215                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2216                 return STT_ERROR_INVALID_STATE;
2217         }
2218
2219         client->state_changed_cb = callback;
2220         client->state_changed_user_data = user_data;
2221
2222         return 0;
2223 }
2224
2225 int stt_unset_state_changed_cb(stt_h stt)
2226 {
2227         stt_client_s* client = NULL;
2228         if (0 != __stt_get_feature_enabled()) {
2229                 return STT_ERROR_NOT_SUPPORTED;
2230         }
2231         if (0 != __stt_check_privilege()) {
2232                 return STT_ERROR_PERMISSION_DENIED;
2233         }
2234         if (0 != __stt_check_handle(stt, &client)) {
2235                 return STT_ERROR_INVALID_PARAMETER;
2236         }
2237
2238         if (STT_STATE_CREATED != client->current_state) {
2239                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2240                 return STT_ERROR_INVALID_STATE;
2241         }
2242
2243         client->state_changed_cb = NULL;
2244         client->state_changed_user_data = NULL;
2245
2246         return 0;
2247 }
2248
2249 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
2250 {
2251         stt_client_s* client = NULL;
2252         if (0 != __stt_get_feature_enabled()) {
2253                 return STT_ERROR_NOT_SUPPORTED;
2254         }
2255         if (0 != __stt_check_privilege()) {
2256                 return STT_ERROR_PERMISSION_DENIED;
2257         }
2258         if (0 != __stt_check_handle(stt, &client)) {
2259                 return STT_ERROR_INVALID_PARAMETER;
2260         }
2261
2262         if (NULL == callback)
2263                 return STT_ERROR_INVALID_PARAMETER;
2264
2265         if (STT_STATE_CREATED != client->current_state) {
2266                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2267                 return STT_ERROR_INVALID_STATE;
2268         }
2269
2270         client->error_cb = callback;
2271         client->error_user_data = user_data;
2272
2273         return 0;
2274 }
2275
2276 int stt_unset_error_cb(stt_h stt)
2277 {
2278         stt_client_s* client = NULL;
2279         if (0 != __stt_get_feature_enabled()) {
2280                 return STT_ERROR_NOT_SUPPORTED;
2281         }
2282         if (0 != __stt_check_privilege()) {
2283                 return STT_ERROR_PERMISSION_DENIED;
2284         }
2285         if (0 != __stt_check_handle(stt, &client)) {
2286                 return STT_ERROR_INVALID_PARAMETER;
2287         }
2288
2289         if (STT_STATE_CREATED != client->current_state) {
2290                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2291                 return STT_ERROR_INVALID_STATE;
2292         }
2293
2294         client->error_cb = NULL;
2295         client->error_user_data = NULL;
2296
2297         return 0;
2298 }
2299
2300 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2301 {
2302         stt_client_s* client = NULL;
2303         if (0 != __stt_get_feature_enabled()) {
2304                 return STT_ERROR_NOT_SUPPORTED;
2305         }
2306         if (0 != __stt_check_privilege()) {
2307                 return STT_ERROR_PERMISSION_DENIED;
2308         }
2309         if (0 != __stt_check_handle(stt, &client)) {
2310                 return STT_ERROR_INVALID_PARAMETER;
2311         }
2312
2313         if (NULL == callback)
2314                 return STT_ERROR_INVALID_PARAMETER;
2315
2316         if (STT_STATE_CREATED != client->current_state) {
2317                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2318                 return STT_ERROR_INVALID_STATE;
2319         }
2320
2321         client->default_lang_changed_cb = callback;
2322         client->default_lang_changed_user_data = user_data;
2323
2324         return 0;
2325 }
2326
2327 int stt_unset_default_language_changed_cb(stt_h stt)
2328 {
2329         stt_client_s* client = NULL;
2330         if (0 != __stt_get_feature_enabled()) {
2331                 return STT_ERROR_NOT_SUPPORTED;
2332         }
2333         if (0 != __stt_check_privilege()) {
2334                 return STT_ERROR_PERMISSION_DENIED;
2335         }
2336         if (0 != __stt_check_handle(stt, &client)) {
2337                 return STT_ERROR_INVALID_PARAMETER;
2338         }
2339
2340         if (STT_STATE_CREATED != client->current_state) {
2341                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2342                 return STT_ERROR_INVALID_STATE;
2343         }
2344
2345         client->default_lang_changed_cb = NULL;
2346         client->default_lang_changed_user_data = NULL;
2347
2348         return 0;
2349 }
2350
2351 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2352 {
2353         stt_client_s* client = NULL;
2354         if (0 != __stt_get_feature_enabled()) {
2355                 return STT_ERROR_NOT_SUPPORTED;
2356         }
2357         if (0 != __stt_check_privilege()) {
2358                 return STT_ERROR_PERMISSION_DENIED;
2359         }
2360         if (0 != __stt_check_handle(stt, &client)) {
2361                 return STT_ERROR_INVALID_PARAMETER;
2362         }
2363
2364         if (NULL == callback)
2365                 return STT_ERROR_INVALID_PARAMETER;
2366
2367         if (STT_STATE_CREATED != client->current_state) {
2368                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2369                 return STT_ERROR_INVALID_STATE;
2370         }
2371
2372         client->engine_changed_cb = callback;
2373         client->engine_changed_user_data = user_data;
2374
2375         return 0;
2376 }
2377
2378 int stt_unset_engine_changed_cb(stt_h stt)
2379 {
2380         stt_client_s* client = NULL;
2381         if (0 != __stt_get_feature_enabled()) {
2382                 return STT_ERROR_NOT_SUPPORTED;
2383         }
2384         if (0 != __stt_check_privilege()) {
2385                 return STT_ERROR_PERMISSION_DENIED;
2386         }
2387         if (0 != __stt_check_handle(stt, &client)) {
2388                 return STT_ERROR_INVALID_PARAMETER;
2389         }
2390
2391         if (STT_STATE_CREATED != client->current_state) {
2392                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2393                 return STT_ERROR_INVALID_STATE;
2394         }
2395
2396         client->engine_changed_cb = NULL;
2397         client->engine_changed_user_data = NULL;
2398
2399         return 0;
2400 }
2401
2402 int stt_set_speech_status_cb(stt_h stt, stt_speech_status_cb callback, void* user_data)
2403 {
2404         stt_client_s* client = NULL;
2405         if (0 != __stt_get_feature_enabled()) {
2406                 return STT_ERROR_NOT_SUPPORTED;
2407         }
2408         if (0 != __stt_check_privilege()) {
2409                 return STT_ERROR_PERMISSION_DENIED;
2410         }
2411         if (0 != __stt_check_handle(stt, &client)) {
2412                 return STT_ERROR_INVALID_PARAMETER;
2413         }
2414
2415         if (NULL == callback)
2416                 return STT_ERROR_INVALID_PARAMETER;
2417
2418         if (STT_STATE_CREATED != client->current_state) {
2419                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2420                 return STT_ERROR_INVALID_STATE;
2421         }
2422
2423         client->speech_status_cb = callback;
2424         client->speech_status_user_data = user_data;
2425
2426         return 0;
2427 }
2428
2429 int stt_unset_speech_status_cb(stt_h stt)
2430 {
2431         stt_client_s* client = NULL;
2432         if (0 != __stt_get_feature_enabled()) {
2433                 return STT_ERROR_NOT_SUPPORTED;
2434         }
2435         if (0 != __stt_check_privilege()) {
2436                 return STT_ERROR_PERMISSION_DENIED;
2437         }
2438         if (0 != __stt_check_handle(stt, &client)) {
2439                 return STT_ERROR_INVALID_PARAMETER;
2440         }
2441
2442         if (STT_STATE_CREATED != client->current_state) {
2443                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2444                 return STT_ERROR_INVALID_STATE;
2445         }
2446
2447         client->speech_status_cb = NULL;
2448         client->speech_status_user_data = NULL;
2449
2450         return 0;
2451 }