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