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