a08dacce3b5323abc906347e65e693d3301fd65c
[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 void __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 (0 >= fread(smack_label, 1, sizeof(smack_label), fp))
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         if (0 == stt_client_get_size() || NULL == stt_client_get_by_uid(client->uid)) {
920                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Client has been already destroyed");
921                 return EINA_FALSE;
922         }
923
924         /* Check and Set vconfkey of custom engine before sending hello */
925         if (1 == g_privilege_applaunch_allowed && NULL != client->current_engine_id) {
926                 /* Set vconfkey */
927                 ret = __stt_set_buxtonkey(client->current_engine_id);
928                 if (0 != ret) {
929                         SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] set buxtonkey Failed!!! (inside __stt_connect_daemon)");
930                         return EINA_TRUE;
931                 }
932         }
933
934         /* Send hello */
935         ret = stt_dbus_request_hello(client->uid);
936
937         if (0 != ret) {
938                 if (STT_ERROR_INVALID_STATE == ret) {
939                         g_connect_timer = NULL;
940                         return EINA_FALSE;
941                 }
942                 return EINA_TRUE;
943         }
944
945         g_connect_timer = NULL;
946         SLOG(LOG_DEBUG, TAG_STTC, "===== Connect stt-service");
947
948         /* request initialization */
949         bool silence_supported = false;
950         bool credential_needed = false;
951
952         ret = stt_dbus_request_initialize(client->uid, &silence_supported, &credential_needed);
953
954         if (STT_ERROR_ENGINE_NOT_FOUND == ret) {
955                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to initialize : %s", __stt_get_error_code(ret));
956
957                 client->reason = STT_ERROR_ENGINE_NOT_FOUND;
958                 ecore_main_loop_thread_safe_call_async(__stt_notify_error, (void*)client);
959
960                 return EINA_FALSE;
961
962         } else if (STT_ERROR_NONE != ret) {
963                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] Fail to connection. Retry to connect");
964                 return EINA_TRUE;
965         } else {
966                 /* success to connect stt-service */
967                 client->silence_supported = silence_supported;
968                 client->credential_needed = credential_needed;
969                 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
970         }
971
972 #ifdef __UNUSED_CODES__
973         if (NULL != client->current_engine_id) {
974                 ret = -1;
975                 int count = 0;
976                 silence_supported = false;
977                 credential_needed = false;
978                 SLOG(LOG_DEBUG, TAG_STTC, "[WARNING] current_engine_id(%s)", client->current_engine_id);
979
980                 while (0 != ret) {
981                         ret = stt_dbus_request_set_current_engine(client->uid, client->current_engine_id, &silence_supported, &credential_needed);
982                         if (0 != ret) {
983                                 if (STT_ERROR_TIMED_OUT != ret) {
984                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set current engine : %s", __stt_get_error_code(ret));
985                                         return ret;
986                                 } else {
987                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
988                                         usleep(10000);
989                                         count++;
990                                         if (STT_RETRY_COUNT == count) {
991                                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
992                                                 return ret;
993                                         }
994                                 }
995                         } else {
996                                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", client->current_engine_id);
997
998                                 /* success to change engine */
999                                 client->silence_supported = silence_supported;
1000                                 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
1001                         }
1002                 }
1003         }
1004 #endif
1005         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] uid(%d)", client->uid);
1006
1007         client->before_state = client->current_state;
1008         client->current_state = STT_STATE_READY;
1009
1010         if (NULL != client->state_changed_cb) {
1011                 stt_client_use_callback(client);
1012                 client->state_changed_cb(client->stt, client->before_state,
1013                         client->current_state, client->state_changed_user_data);
1014                 stt_client_not_use_callback(client);
1015                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
1016         } else {
1017                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1018         }
1019
1020         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1021         SLOG(LOG_DEBUG, TAG_STTC, "  ");
1022
1023         return EINA_FALSE;
1024 }
1025
1026 int stt_prepare(stt_h stt)
1027 {
1028         stt_client_s* client = NULL;
1029         if (0 != __stt_get_feature_enabled()) {
1030                 return STT_ERROR_NOT_SUPPORTED;
1031         }
1032         if (0 != __stt_check_privilege()) {
1033                 return STT_ERROR_PERMISSION_DENIED;
1034         }
1035         if (0 != __stt_check_handle(stt, &client)) {
1036                 return STT_ERROR_INVALID_PARAMETER;
1037         }
1038
1039         /* check state */
1040         if (client->current_state != STT_STATE_CREATED) {
1041                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not 'CREATED'", client->current_state);
1042                 return STT_ERROR_INVALID_STATE;
1043         }
1044
1045         ecore_thread_main_loop_begin();
1046         g_connect_timer = ecore_timer_add(0.02, __stt_connect_daemon, (void*)client);
1047         ecore_thread_main_loop_end();
1048
1049         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1050         SLOG(LOG_DEBUG, TAG_STTC, " ");
1051
1052         return STT_ERROR_NONE;
1053 }
1054
1055 int stt_unprepare(stt_h stt)
1056 {
1057         stt_client_s* client = NULL;
1058         if (0 != __stt_get_feature_enabled()) {
1059                 return STT_ERROR_NOT_SUPPORTED;
1060         }
1061         if (0 != __stt_check_privilege()) {
1062                 return STT_ERROR_PERMISSION_DENIED;
1063         }
1064         if (0 != __stt_check_handle(stt, &client)) {
1065                 return STT_ERROR_INVALID_PARAMETER;
1066         }
1067
1068         SLOG(LOG_DEBUG, TAG_STTC, "===== Unprepare STT");
1069
1070         /* check state */
1071         if (client->current_state != STT_STATE_READY) {
1072                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not 'READY'", client->current_state);
1073                 return STT_ERROR_INVALID_STATE;
1074         }
1075
1076         int ret = -1;
1077         int count = 0;
1078         while (0 != ret) {
1079                 ret = stt_dbus_request_finalize(client->uid);
1080                 if (0 != ret) {
1081                         if (STT_ERROR_TIMED_OUT != ret) {
1082                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret));
1083                                 break;
1084                         } else {
1085                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1086                                 usleep(10000);
1087                                 count++;
1088                                 if (STT_RETRY_COUNT == count) {
1089                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1090                                         break;
1091                                 }
1092                         }
1093                 }
1094         }
1095
1096         client->internal_state = STT_INTERNAL_STATE_NONE;
1097
1098         client->before_state = client->current_state;
1099         client->current_state = STT_STATE_CREATED;
1100
1101         if (NULL != client->state_changed_cb) {
1102                 stt_client_use_callback(client);
1103                 client->state_changed_cb(client->stt, client->before_state,
1104                         client->current_state, client->state_changed_user_data);
1105                 stt_client_not_use_callback(client);
1106         } else {
1107                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1108         }
1109
1110         if (g_connect_timer) {
1111                 ecore_timer_del(g_connect_timer);
1112                 g_connect_timer = NULL;
1113         }
1114
1115         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1116         SLOG(LOG_DEBUG, TAG_STTC, " ");
1117
1118         return STT_ERROR_NONE;
1119 }
1120
1121 bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
1122 {
1123         stt_h stt = (stt_h)user_data;
1124
1125         stt_client_s* client = stt_client_get(stt);
1126         if (NULL == client) {
1127                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
1128                 return false;
1129         }
1130
1131         /* call callback function */
1132         if (NULL != client->supported_lang_cb) {
1133                 return client->supported_lang_cb(stt, language, client->supported_lang_user_data);
1134         } else {
1135                 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported languages");
1136         }
1137
1138         return false;
1139 }
1140
1141 int stt_foreach_supported_languages(stt_h stt, stt_supported_language_cb callback, void* user_data)
1142 {
1143         stt_client_s* client = NULL;
1144         if (0 != __stt_get_feature_enabled()) {
1145                 return STT_ERROR_NOT_SUPPORTED;
1146         }
1147         if (0 != __stt_check_privilege()) {
1148                 return STT_ERROR_PERMISSION_DENIED;
1149         }
1150         if (0 != __stt_check_handle(stt, &client)) {
1151                 return STT_ERROR_INVALID_PARAMETER;
1152         }
1153
1154         SLOG(LOG_DEBUG, TAG_STTC, "===== Foreach Supported Language");
1155
1156         if (NULL == callback) {
1157                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1158                 return STT_ERROR_INVALID_PARAMETER;
1159         }
1160
1161         int ret;
1162         char* current_engine_id = NULL;
1163
1164         if (NULL == client->current_engine_id) {
1165                 ret = stt_config_mgr_get_engine(&current_engine_id);
1166                 ret = __stt_convert_config_error_code(ret);
1167                 if (0 != ret) {
1168                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default engine id : %s", __stt_get_error_code(ret));
1169                         return ret;
1170                 }
1171         } else {
1172                 current_engine_id = strdup(client->current_engine_id);
1173                 if (NULL == current_engine_id) {
1174                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1175                         return STT_ERROR_OUT_OF_MEMORY;
1176                 }
1177         }
1178
1179         client->supported_lang_cb = callback;
1180         client->supported_lang_user_data = user_data;
1181
1182         ret = stt_config_mgr_get_language_list(current_engine_id, __stt_config_supported_language_cb, client->stt);
1183         ret = __stt_convert_config_error_code(ret);
1184         if (0 != ret) {
1185                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get languages : %s", __stt_get_error_code(ret));
1186         }
1187
1188         if (NULL != current_engine_id) {
1189                 free(current_engine_id);
1190                 current_engine_id = NULL;
1191         }
1192
1193         client->supported_lang_cb = NULL;
1194         client->supported_lang_user_data = NULL;
1195
1196         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1197         SLOG(LOG_DEBUG, TAG_STTC, " ");
1198
1199         return ret;
1200 }
1201
1202 int stt_get_default_language(stt_h stt, char** language)
1203 {
1204         stt_client_s* client = NULL;
1205         if (0 != __stt_get_feature_enabled()) {
1206                 return STT_ERROR_NOT_SUPPORTED;
1207         }
1208         if (0 != __stt_check_privilege()) {
1209                 return STT_ERROR_PERMISSION_DENIED;
1210         }
1211         if (0 != __stt_check_handle(stt, &client)) {
1212                 return STT_ERROR_INVALID_PARAMETER;
1213         }
1214
1215         SLOG(LOG_DEBUG, TAG_STTC, "===== Get Default Language");
1216
1217         if (NULL == language) {
1218                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1219                 return STT_ERROR_INVALID_PARAMETER;
1220         }
1221
1222         int ret = 0;
1223         ret = stt_config_mgr_get_default_language(language);
1224         ret = __stt_convert_config_error_code(ret);
1225         if (0 != ret) {
1226                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default language : %s", __stt_get_error_code(ret));
1227         } else {
1228                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current language = %s", *language);
1229         }
1230
1231         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1232         SLOG(LOG_DEBUG, TAG_STTC, " ");
1233
1234         return ret;
1235 }
1236
1237 int stt_get_state(stt_h stt, stt_state_e* state)
1238 {
1239         stt_client_s* client = NULL;
1240         if (0 != __stt_get_feature_enabled()) {
1241                 return STT_ERROR_NOT_SUPPORTED;
1242         }
1243         if (0 != __stt_check_privilege()) {
1244                 return STT_ERROR_PERMISSION_DENIED;
1245         }
1246         if (0 != __stt_check_handle(stt, &client)) {
1247                 return STT_ERROR_INVALID_PARAMETER;
1248         }
1249
1250         if (NULL == state) {
1251                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1252                 return STT_ERROR_INVALID_PARAMETER;
1253         }
1254
1255         *state = client->current_state;
1256
1257         switch (*state) {
1258         case STT_STATE_CREATED:         SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'CREATED'");        break;
1259         case STT_STATE_READY:           SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Ready'");          break;
1260         case STT_STATE_RECORDING:       SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Recording'");      break;
1261         case STT_STATE_PROCESSING:      SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Processing'");     break;
1262         default:                        SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid value");             break;
1263         }
1264
1265         return STT_ERROR_NONE;
1266 }
1267
1268 int stt_get_error_message(stt_h stt, char** err_msg)
1269 {
1270         stt_client_s* client = NULL;
1271         if (0 != __stt_get_feature_enabled()) {
1272                 return STT_ERROR_NOT_SUPPORTED;
1273         }
1274         if (0 != __stt_check_privilege()) {
1275                 return STT_ERROR_PERMISSION_DENIED;
1276         }
1277         if (0 != __stt_check_handle(stt, &client)) {
1278                 return STT_ERROR_INVALID_PARAMETER;
1279         }
1280
1281         if (NULL == err_msg) {
1282                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1283                 return STT_ERROR_INVALID_PARAMETER;
1284         }
1285
1286         if (false == g_err_callback_status) {
1287                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This callback should be called during an err_callback");
1288                 return STT_ERROR_OPERATION_FAILED;
1289         }
1290
1291         if (NULL != client->err_msg) {
1292                 *err_msg = strdup(client->err_msg);
1293                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Error msg (%s)", *err_msg);
1294         } else {
1295                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Error msg (NULL)");
1296         }
1297
1298         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1299         SLOG(LOG_DEBUG, TAG_STTC, " ");
1300
1301         return STT_ERROR_NONE;
1302 }
1303
1304 int stt_is_recognition_type_supported(stt_h stt, const char* type, bool* support)
1305 {
1306         stt_client_s* client = NULL;
1307         if (0 != __stt_get_feature_enabled()) {
1308                 return STT_ERROR_NOT_SUPPORTED;
1309         }
1310         if (0 != __stt_check_privilege()) {
1311                 return STT_ERROR_PERMISSION_DENIED;
1312         }
1313         if (0 != __stt_check_handle(stt, &client)) {
1314                 return STT_ERROR_INVALID_PARAMETER;
1315         }
1316
1317         if (NULL == type || NULL == support) {
1318                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1319                 return STT_ERROR_INVALID_PARAMETER;
1320         }
1321
1322         /* check state */
1323         if (client->current_state != STT_STATE_READY) {
1324                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1325                 return STT_ERROR_INVALID_STATE;
1326         }
1327
1328         int ret = -1;
1329         int count = 0;
1330         while (0 != ret) {
1331                 ret = stt_dbus_request_is_recognition_type_supported(client->uid, type, support);
1332                 if (0 != ret) {
1333                         if (STT_ERROR_TIMED_OUT != ret) {
1334                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get recognition type supported : %s", __stt_get_error_code(ret));
1335                                 return ret;
1336                         } else {
1337                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1338                                 usleep(10000);
1339                                 count++;
1340                                 if (STT_RETRY_COUNT == count) {
1341                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1342                                         return ret;
1343                                 }
1344                         }
1345                 } else {
1346                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] recognition type is %s", *support ? "true " : "false");
1347                         break;
1348                 }
1349         }
1350
1351         return STT_ERROR_NONE;
1352 }
1353
1354 int stt_set_silence_detection(stt_h stt, stt_option_silence_detection_e type)
1355 {
1356         stt_client_s* client = NULL;
1357         if (0 != __stt_get_feature_enabled()) {
1358                 return STT_ERROR_NOT_SUPPORTED;
1359         }
1360         if (0 != __stt_check_privilege()) {
1361                 return STT_ERROR_PERMISSION_DENIED;
1362         }
1363         if (0 != __stt_check_handle(stt, &client)) {
1364                 return STT_ERROR_INVALID_PARAMETER;
1365         }
1366
1367         /* check state */
1368         if (client->current_state != STT_STATE_READY) {
1369                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1370                 return STT_ERROR_INVALID_STATE;
1371         }
1372
1373         if (true == client->silence_supported) {
1374                 if (type >= STT_OPTION_SILENCE_DETECTION_FALSE && type <= STT_OPTION_SILENCE_DETECTION_AUTO) {
1375                         client->silence = type;
1376                 } else {
1377                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Type is invalid");
1378                         return STT_ERROR_INVALID_PARAMETER;
1379                 }
1380         } else {
1381                 return STT_ERROR_NOT_SUPPORTED_FEATURE;
1382         }
1383
1384         return STT_ERROR_NONE;
1385 }
1386
1387 int stt_set_start_sound(stt_h stt, const char* filename)
1388 {
1389         stt_client_s* client = NULL;
1390         if (0 != __stt_get_feature_enabled()) {
1391                 return STT_ERROR_NOT_SUPPORTED;
1392         }
1393         if (0 != __stt_check_privilege()) {
1394                 return STT_ERROR_PERMISSION_DENIED;
1395         }
1396         if (0 != __stt_check_handle(stt, &client)) {
1397                 return STT_ERROR_INVALID_PARAMETER;
1398         }
1399
1400         SLOG(LOG_DEBUG, TAG_STTC, "===== STT SET START SOUND");
1401
1402         if (NULL == filename) {
1403                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1404                 return STT_ERROR_INVALID_PARAMETER;
1405         }
1406
1407         if (0 != access(filename, F_OK)) {
1408                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist");
1409                 return STT_ERROR_INVALID_PARAMETER;
1410         }
1411
1412         /* check state */
1413         if (client->current_state != STT_STATE_READY) {
1414                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1415                 return STT_ERROR_INVALID_STATE;
1416         }
1417
1418         int ret = -1;
1419         int count = 0;
1420         while (0 != ret) {
1421                 ret = stt_dbus_request_set_start_sound(client->uid, filename);
1422                 if (0 != ret) {
1423                         if (STT_ERROR_TIMED_OUT != ret) {
1424                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set start sound : %s", __stt_get_error_code(ret));
1425                                 return ret;
1426                         } else {
1427                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1428                                 usleep(10000);
1429                                 count++;
1430                                 if (STT_RETRY_COUNT == count) {
1431                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1432                                         return ret;
1433                                 }
1434                         }
1435                 } else {
1436                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Set start sound : %s", filename);
1437                         break;
1438                 }
1439         }
1440
1441         return STT_ERROR_NONE;
1442 }
1443
1444 int stt_unset_start_sound(stt_h stt)
1445 {
1446         stt_client_s* client = NULL;
1447         if (0 != __stt_get_feature_enabled()) {
1448                 return STT_ERROR_NOT_SUPPORTED;
1449         }
1450         if (0 != __stt_check_privilege()) {
1451                 return STT_ERROR_PERMISSION_DENIED;
1452         }
1453         if (0 != __stt_check_handle(stt, &client)) {
1454                 return STT_ERROR_INVALID_PARAMETER;
1455         }
1456
1457         SLOG(LOG_DEBUG, TAG_STTC, "===== STT UNSET START SOUND");
1458
1459         /* check state */
1460         if (client->current_state != STT_STATE_READY) {
1461                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1462                 return STT_ERROR_INVALID_STATE;
1463         }
1464
1465         int ret = -1;
1466         int count = 0;
1467         while (0 != ret) {
1468                 ret = stt_dbus_request_unset_start_sound(client->uid);
1469                 if (0 != ret) {
1470                         if (STT_ERROR_TIMED_OUT != ret) {
1471                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset start sound : %s", __stt_get_error_code(ret));
1472                                 return ret;
1473                         } else {
1474                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1475                                 usleep(10000);
1476                                 count++;
1477                                 if (STT_RETRY_COUNT == count) {
1478                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1479                                         return ret;
1480                                 }
1481                         }
1482                 } else {
1483                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Unset start sound");
1484                         break;
1485                 }
1486         }
1487
1488         return STT_ERROR_NONE;
1489 }
1490
1491 int stt_set_stop_sound(stt_h stt, const char* filename)
1492 {
1493         stt_client_s* client = NULL;
1494         if (0 != __stt_get_feature_enabled()) {
1495                 return STT_ERROR_NOT_SUPPORTED;
1496         }
1497         if (0 != __stt_check_privilege()) {
1498                 return STT_ERROR_PERMISSION_DENIED;
1499         }
1500         if (0 != __stt_check_handle(stt, &client)) {
1501                 return STT_ERROR_INVALID_PARAMETER;
1502         }
1503
1504         SLOG(LOG_DEBUG, TAG_STTC, "===== STT SET STOP SOUND");
1505
1506         if (NULL == filename) {
1507                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1508                 return STT_ERROR_INVALID_PARAMETER;
1509         }
1510
1511         if (0 != access(filename, F_OK)) {
1512                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist");
1513                 return STT_ERROR_INVALID_PARAMETER;
1514         }
1515
1516         /* check state */
1517         if (client->current_state != STT_STATE_READY) {
1518                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1519                 return STT_ERROR_INVALID_STATE;
1520         }
1521
1522         int ret = -1;
1523         int count = 0;
1524         while (0 != ret) {
1525                 ret = stt_dbus_request_set_stop_sound(client->uid, filename);
1526                 if (0 != ret) {
1527                         if (STT_ERROR_TIMED_OUT != ret) {
1528                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set stop sound : %s", __stt_get_error_code(ret));
1529                                 return ret;
1530                         } else {
1531                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1532                                 usleep(10000);
1533                                 count++;
1534                                 if (STT_RETRY_COUNT == count) {
1535                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1536                                         return ret;
1537                                 }
1538                         }
1539                 } else {
1540                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Set stop sound : %s", filename);
1541                         break;
1542                 }
1543         }
1544
1545         return STT_ERROR_NONE;
1546 }
1547
1548 int stt_unset_stop_sound(stt_h stt)
1549 {
1550         stt_client_s* client = NULL;
1551         if (0 != __stt_get_feature_enabled()) {
1552                 return STT_ERROR_NOT_SUPPORTED;
1553         }
1554         if (0 != __stt_check_privilege()) {
1555                 return STT_ERROR_PERMISSION_DENIED;
1556         }
1557         if (0 != __stt_check_handle(stt, &client)) {
1558                 return STT_ERROR_INVALID_PARAMETER;
1559         }
1560
1561         SLOG(LOG_DEBUG, TAG_STTC, "===== STT UNSET STOP SOUND");
1562
1563         /* check state */
1564         if (client->current_state != STT_STATE_READY) {
1565                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1566                 return STT_ERROR_INVALID_STATE;
1567         }
1568
1569         int ret = -1;
1570         int count = 0;
1571         while (0 != ret) {
1572                 ret = stt_dbus_request_unset_stop_sound(client->uid);
1573                 if (0 != ret) {
1574                         if (STT_ERROR_TIMED_OUT != ret) {
1575                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset stop sound : %s", __stt_get_error_code(ret));
1576                                 return ret;
1577                         } else {
1578                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1579                                 usleep(10000);
1580                                 count++;
1581                                 if (STT_RETRY_COUNT == count) {
1582                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1583                                         return ret;
1584                                 }
1585                         }
1586                 } else {
1587                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Unset stop sound");
1588                         break;
1589                 }
1590         }
1591
1592         return STT_ERROR_NONE;
1593 }
1594
1595 int stt_start(stt_h stt, const char* language, const char* type)
1596 {
1597         stt_client_s* client = NULL;
1598         if (0 != __stt_get_feature_enabled()) {
1599                 return STT_ERROR_NOT_SUPPORTED;
1600         }
1601         if (0 != __stt_check_privilege()) {
1602                 return STT_ERROR_PERMISSION_DENIED;
1603         }
1604         if (0 != __stt_check_handle(stt, &client)) {
1605                 return STT_ERROR_INVALID_PARAMETER;
1606         }
1607
1608         SLOG(LOG_DEBUG, TAG_STTC, "===== STT START");
1609
1610         /* check state */
1611         if (client->current_state != STT_STATE_READY) {
1612                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1613                 return STT_ERROR_INVALID_STATE;
1614         }
1615
1616         if (STT_INTERNAL_STATE_NONE != client->internal_state) {
1617                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
1618                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1619         }
1620
1621         int ret = -1;
1622         char appid[1024] = {0, };
1623         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
1624
1625         if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1626                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
1627         } else {
1628                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1629         }
1630
1631         char* temp = NULL;
1632         if (NULL == language) {
1633                 temp = strdup("default");
1634         } else {
1635                 temp = strdup(language);
1636         }
1637
1638         if (NULL == temp) {
1639                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1640                 return STT_ERROR_OUT_OF_MEMORY;
1641         }
1642
1643         if (true == client->credential_needed && NULL == client->credential) {
1644                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
1645                 free(temp);
1646                 temp = NULL;
1647                 return STT_ERROR_PERMISSION_DENIED;
1648         }
1649
1650         client->internal_state = STT_INTERNAL_STATE_STARTING;
1651         ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential);
1652         if (0 != ret) {
1653                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret));
1654                 client->internal_state = STT_INTERNAL_STATE_NONE;
1655         } else {
1656                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
1657         }
1658
1659         free(temp);
1660         temp = NULL;
1661
1662         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1663         SLOG(LOG_DEBUG, TAG_STTC, " ");
1664
1665         return ret;
1666 }
1667
1668 int stt_stop(stt_h stt)
1669 {
1670         stt_client_s* client = NULL;
1671         if (0 != __stt_get_feature_enabled()) {
1672                 return STT_ERROR_NOT_SUPPORTED;
1673         }
1674         if (0 != __stt_check_privilege()) {
1675                 return STT_ERROR_PERMISSION_DENIED;
1676         }
1677         if (0 != __stt_check_handle(stt, &client)) {
1678                 return STT_ERROR_INVALID_PARAMETER;
1679         }
1680
1681         SLOG(LOG_DEBUG, TAG_STTC, "===== STT STOP");
1682
1683         /* check state */
1684         if (client->current_state != STT_STATE_RECORDING) {
1685                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Current state(%d) is NOT RECORDING", client->current_state);
1686                 return STT_ERROR_INVALID_STATE;
1687         }
1688
1689         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1690                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1691                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1692         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1693                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1694                 return STT_ERROR_IN_PROGRESS_TO_READY;
1695         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1696                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1697                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1698         }
1699
1700         client->internal_state = STT_INTERNAL_STATE_STOPPING;
1701         int ret = stt_dbus_request_stop(client->uid);
1702         if (0 != ret) {
1703                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret));
1704                 client->internal_state = STT_INTERNAL_STATE_NONE;
1705         } else {
1706                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1707         }
1708
1709         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1710         SLOG(LOG_DEBUG, TAG_STTC, " ");
1711
1712         return ret;
1713 }
1714
1715
1716 int stt_cancel(stt_h stt)
1717 {
1718         stt_client_s* client = NULL;
1719         if (0 != __stt_get_feature_enabled()) {
1720                 return STT_ERROR_NOT_SUPPORTED;
1721         }
1722         if (0 != __stt_check_privilege()) {
1723                 return STT_ERROR_PERMISSION_DENIED;
1724         }
1725         if (0 != __stt_check_handle(stt, &client)) {
1726                 return STT_ERROR_INVALID_PARAMETER;
1727         }
1728
1729         SLOG(LOG_DEBUG, TAG_STTC, "===== STT CANCEL");
1730
1731         /* check state */
1732         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1733                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
1734                 return STT_ERROR_INVALID_STATE;
1735         }
1736
1737         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1738                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1739                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1740         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1741                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1742                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1743         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1744                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1745                 return STT_ERROR_IN_PROGRESS_TO_READY;
1746         }
1747
1748         client->internal_state = STT_INTERNAL_STATE_CANCELING;
1749         int ret = stt_dbus_request_cancel(client->uid);
1750         if (0 != ret) {
1751                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret));
1752                 client->internal_state = STT_INTERNAL_STATE_NONE;
1753         } else {
1754                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1755         }
1756
1757         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1758         SLOG(LOG_DEBUG, TAG_STTC, " ");
1759
1760         return ret;
1761 }
1762
1763 int __stt_cb_set_volume(int uid, float volume)
1764 {
1765         stt_client_s* client = NULL;
1766
1767         client = stt_client_get_by_uid(uid);
1768         if (NULL == client) {
1769                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1770                 return STT_ERROR_INVALID_PARAMETER;
1771         }
1772
1773         if (STT_STATE_RECORDING != client->current_state) {
1774                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1775                 return STT_ERROR_INVALID_STATE;
1776         }
1777
1778         g_volume_db = volume;
1779         SLOG(LOG_DEBUG, TAG_STTC, "Set volume (%f)", g_volume_db);
1780
1781         return 0;
1782 }
1783
1784 int stt_get_recording_volume(stt_h stt, float* volume)
1785 {
1786         stt_client_s* client = NULL;
1787         if (0 != __stt_get_feature_enabled()) {
1788                 return STT_ERROR_NOT_SUPPORTED;
1789         }
1790         if (0 != __stt_check_privilege()) {
1791                 return STT_ERROR_PERMISSION_DENIED;
1792         }
1793         if (0 != __stt_check_handle(stt, &client)) {
1794                 return STT_ERROR_INVALID_PARAMETER;
1795         }
1796
1797         if (NULL == volume) {
1798                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1799                 return STT_ERROR_INVALID_PARAMETER;
1800         }
1801
1802         if (STT_STATE_RECORDING != client->current_state) {
1803                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1804                 return STT_ERROR_INVALID_STATE;
1805         }
1806
1807         *volume = g_volume_db;
1808
1809         return STT_ERROR_NONE;
1810 }
1811
1812 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1813 {
1814         stt_client_s* client = (stt_client_s*)user_data;
1815
1816         /* check handle */
1817         if (NULL == client) {
1818                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1819                 return EINA_FALSE;
1820         }
1821
1822         if (NULL != client->result_time_cb) {
1823                 SLOG(LOG_DEBUG, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1824                         index, event, text, start_time, end_time);
1825                 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1826                         text, start_time, end_time, client->result_time_user_data);
1827         } else {
1828                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1829                 return false;
1830         }
1831
1832         return true;
1833 }
1834
1835 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1836 {
1837         stt_client_s* client = NULL;
1838         if (0 != __stt_get_feature_enabled()) {
1839                 return STT_ERROR_NOT_SUPPORTED;
1840         }
1841         if (0 != __stt_check_privilege()) {
1842                 return STT_ERROR_PERMISSION_DENIED;
1843         }
1844         if (0 != __stt_check_handle(stt, &client)) {
1845                 return STT_ERROR_INVALID_PARAMETER;
1846         }
1847
1848         SLOG(LOG_DEBUG, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1849
1850         if (NULL == callback) {
1851                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1852                 return STT_ERROR_INVALID_PARAMETER;
1853         }
1854
1855         client->result_time_cb = callback;
1856         client->result_time_user_data = user_data;
1857
1858         int ret = -1;
1859         ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1860         ret = __stt_convert_config_error_code(ret);
1861         if (0 != ret) {
1862                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret));
1863         }
1864
1865         client->result_time_cb = NULL;
1866         client->result_time_user_data = NULL;
1867
1868         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1869         SLOG(LOG_DEBUG, TAG_STTC, " ");
1870
1871         return ret;
1872 }
1873
1874 static void __stt_notify_error(void *data)
1875 {
1876         stt_client_s* client = (stt_client_s*)data;
1877
1878         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1879
1880         /* check handle */
1881         if (NULL == client) {
1882                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1883                 return;
1884         }
1885
1886         if (NULL == stt_client_get_by_uid(client->uid))
1887                 return;
1888
1889         if (NULL != client->error_cb) {
1890                 stt_client_use_callback(client);
1891                 g_err_callback_status = true;
1892                 client->error_cb(client->stt, client->reason, client->error_user_data);
1893                 g_err_callback_status = false;
1894                 stt_client_not_use_callback(client);
1895                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1896         } else {
1897                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1898         }
1899
1900         return;
1901 }
1902
1903 int __stt_cb_error(int uid, int reason, char* err_msg)
1904 {
1905         if (-1 == uid) {
1906                 GList* client_list = NULL;
1907                 client_list = stt_client_get_client_list();
1908
1909                 GList *iter = NULL;
1910                 stt_client_s *data = NULL;
1911
1912                 if (g_list_length(client_list) > 0) {
1913                         /* Get a first item */
1914                         iter = g_list_first(client_list);
1915
1916                         while (NULL != iter) {
1917                                 data = iter->data;
1918
1919                                 data->reason = reason;
1920                                 data->internal_state = STT_INTERNAL_STATE_NONE;
1921                                 if (NULL != data->err_msg) {
1922                                         free(data->err_msg);
1923                                         data->err_msg = NULL;
1924                                 }
1925                                 if (NULL != err_msg)
1926                                         data->err_msg = strdup(err_msg);
1927
1928                                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1929
1930                                 if (NULL != data->error_cb) {
1931                                         ecore_main_loop_thread_safe_call_async(__stt_notify_error, data);
1932                                 } else {
1933                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1934                                 }
1935
1936                                 if (STT_ERROR_SERVICE_RESET == reason) {
1937                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1938
1939                                         data->current_state = STT_STATE_CREATED;
1940                                         if (0 != stt_prepare(data->stt)) {
1941                                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1942                                         }
1943                                 }
1944
1945                                 /* Next item */
1946                                 iter = g_list_next(iter);
1947                         }
1948                 }
1949         } else {
1950                 stt_client_s* client = stt_client_get_by_uid(uid);
1951                 if (NULL == client) {
1952                         SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
1953                         return -1;
1954                 }
1955
1956                 client->reason = reason;
1957                 client->internal_state = STT_INTERNAL_STATE_NONE;
1958                 if (NULL != client->err_msg) {
1959                         free(client->err_msg);
1960                         client->err_msg = NULL;
1961                 }
1962                 if (NULL != err_msg)
1963                         client->err_msg = strdup(err_msg);
1964
1965                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1966
1967                 if (NULL != client->error_cb) {
1968                         ecore_main_loop_thread_safe_call_async(__stt_notify_error, client);
1969                 } else {
1970                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1971                 }
1972
1973                 if (STT_ERROR_SERVICE_RESET == reason) {
1974                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1975
1976                         client->current_state = STT_STATE_CREATED;
1977                         if (0 != stt_prepare(client->stt)) {
1978                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1979                         }
1980                 }
1981         }
1982
1983         return 0;
1984 }
1985
1986 static void __stt_notify_state_changed(void *data)
1987 {
1988         stt_client_s* client = (stt_client_s*)data;
1989
1990         /* check handle */
1991         if (NULL == client) {
1992                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1993                 return;
1994         }
1995
1996         if (NULL == stt_client_get_by_uid(client->uid)) {
1997                 return;
1998         }
1999
2000         if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
2001                 client->internal_state = STT_INTERNAL_STATE_NONE;
2002                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
2003         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == client->current_state) {
2004                 client->internal_state = STT_INTERNAL_STATE_NONE;
2005                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
2006         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
2007                 client->internal_state = STT_INTERNAL_STATE_NONE;
2008                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
2009         }
2010
2011         if (NULL != client->state_changed_cb) {
2012                 stt_client_use_callback(client);
2013                 client->state_changed_cb(client->stt, client->before_state,
2014                         client->current_state, client->state_changed_user_data);
2015                 stt_client_not_use_callback(client);
2016                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called, State(%d)", client->current_state);
2017         } else {
2018                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null, State(%d)", client->current_state);
2019         }
2020
2021         return;
2022 }
2023
2024 static Eina_Bool __stt_notify_result(void *data)
2025 {
2026         stt_client_s* client = (stt_client_s*)data;
2027
2028         /* check handle */
2029         if (NULL == client) {
2030                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
2031                 return EINA_FALSE;
2032         }
2033
2034         if (NULL == stt_client_get_by_uid(client->uid)) {
2035                 return EINA_FALSE;
2036         }
2037
2038         if (NULL != client->recognition_result_cb) {
2039                 stt_client_use_callback(client);
2040                 client->recognition_result_cb(client->stt, client->event, (const char**)client->data_list, client->data_count,
2041                         client->msg, client->recognition_result_user_data);
2042                 stt_client_not_use_callback(client);
2043                 SLOG(LOG_DEBUG, TAG_STTC, "client recognition result callback called");
2044         } else {
2045                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
2046         }
2047
2048         if (NULL != client->msg) {
2049                 free(client->msg);
2050                 client->msg = NULL;
2051         }
2052
2053         if (NULL != client->data_list) {
2054                 char **temp = NULL;
2055                 temp = client->data_list;
2056
2057                 int i = 0;
2058                 for (i = 0; i < client->data_count; i++) {
2059                         if (NULL != temp[i]) {
2060                                 free(temp[i]);
2061                                 temp[i] = NULL;
2062                         } else {
2063                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
2064                         }
2065                 }
2066                 free(client->data_list);
2067                 client->data_list = NULL;
2068         }
2069
2070         client->data_count = 0;
2071
2072         stt_config_mgr_remove_time_info_file();
2073
2074         if (STT_RESULT_EVENT_FINAL_RESULT == client->event || STT_RESULT_EVENT_ERROR == client->event) {
2075                 client->before_state = client->current_state;
2076                 client->current_state = STT_STATE_READY;
2077
2078                 if (NULL != client->state_changed_cb) {
2079                         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2080                 } else {
2081                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
2082                 }
2083         }
2084
2085         return EINA_FALSE;
2086 }
2087
2088 int __stt_cb_result(int uid, int event, char** data, int data_count, const char* msg)
2089 {
2090         stt_client_s* client = NULL;
2091
2092         client = stt_client_get_by_uid(uid);
2093         if (NULL == client) {
2094                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
2095                 return STT_ERROR_INVALID_PARAMETER;
2096         }
2097
2098         if (NULL != msg)
2099                 SECURE_SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result Message = %s", msg);
2100
2101         int i = 0;
2102         for (i = 0; i < data_count; i++) {
2103                 if (NULL != data[i])
2104                         SECURE_SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
2105         }
2106
2107         if (NULL != client->recognition_result_cb) {
2108                 client->event = event;
2109                 if (NULL != msg) {
2110                         client->msg = strdup(msg);
2111                 }
2112
2113                 client->data_count = data_count;
2114
2115                 if (data_count > 0) {
2116                         char **temp = NULL;
2117                         temp = (char**)calloc(data_count, sizeof(char*));
2118                         if (NULL == temp) {
2119                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
2120                                 return STT_ERROR_OUT_OF_MEMORY;
2121                         }
2122
2123                         for (i = 0; i < data_count; i++) {
2124                                 if (NULL != data[i])
2125                                         temp[i] = strdup(data[i]);
2126                                 else
2127                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
2128                         }
2129
2130                         client->data_list = temp;
2131                 }
2132         } else {
2133                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] User result callback is null");
2134         }
2135
2136         __stt_notify_result(client);
2137
2138         return STT_ERROR_NONE;
2139 }
2140
2141 int __stt_cb_set_state(int uid, int state)
2142 {
2143         stt_client_s* client = stt_client_get_by_uid(uid);
2144         if (NULL == client) {
2145                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
2146                 return -1;
2147         }
2148
2149         stt_state_e state_from_daemon = (stt_state_e)state;
2150
2151         if (client->current_state == state_from_daemon) {
2152                 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
2153                 return 0;
2154         }
2155
2156         client->before_state = client->current_state;
2157         client->current_state = state_from_daemon;
2158
2159         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2160         return 0;
2161 }
2162
2163 static void __stt_notify_speech_status(void *data)
2164 {
2165         stt_client_s* client = (stt_client_s*)data;
2166
2167         /* check handle */
2168         if (NULL == client) {
2169                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify speech status : A handle is not valid");
2170                 return;
2171         }
2172
2173         if (NULL == stt_client_get_by_uid(client->uid)) {
2174                 return;
2175         }
2176
2177         if (NULL != client->speech_status_cb) {
2178                 stt_client_use_callback(client);
2179                 client->speech_status_cb(client->stt, client->speech_status, client->speech_status_user_data);
2180                 stt_client_not_use_callback(client);
2181                 SLOG(LOG_DEBUG, TAG_STTC, "Speech status callback is called");
2182         } else {
2183                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Speech status callback is null");
2184         }
2185
2186         return;
2187 }
2188
2189 int __stt_cb_speech_status(int uid, int status)
2190 {
2191         stt_client_s* client = stt_client_get_by_uid(uid);
2192         if (NULL == client) {
2193                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
2194                 return -1;
2195         }
2196
2197         client->speech_status = status;
2198
2199         ecore_main_loop_thread_safe_call_async(__stt_notify_speech_status, client);
2200         return 0;
2201 }
2202
2203 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
2204 {
2205         stt_client_s* client = NULL;
2206         if (0 != __stt_get_feature_enabled()) {
2207                 return STT_ERROR_NOT_SUPPORTED;
2208         }
2209         if (0 != __stt_check_privilege()) {
2210                 return STT_ERROR_PERMISSION_DENIED;
2211         }
2212         if (0 != __stt_check_handle(stt, &client)) {
2213                 return STT_ERROR_INVALID_PARAMETER;
2214         }
2215
2216         if (callback == NULL)
2217                 return STT_ERROR_INVALID_PARAMETER;
2218
2219         if (STT_STATE_CREATED != client->current_state) {
2220                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2221                 return STT_ERROR_INVALID_STATE;
2222         }
2223
2224         client->recognition_result_cb = callback;
2225         client->recognition_result_user_data = user_data;
2226
2227         return 0;
2228 }
2229
2230 int stt_unset_recognition_result_cb(stt_h stt)
2231 {
2232         stt_client_s* client = NULL;
2233         if (0 != __stt_get_feature_enabled()) {
2234                 return STT_ERROR_NOT_SUPPORTED;
2235         }
2236         if (0 != __stt_check_privilege()) {
2237                 return STT_ERROR_PERMISSION_DENIED;
2238         }
2239         if (0 != __stt_check_handle(stt, &client)) {
2240                 return STT_ERROR_INVALID_PARAMETER;
2241         }
2242
2243         if (STT_STATE_CREATED != client->current_state) {
2244                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2245                 return STT_ERROR_INVALID_STATE;
2246         }
2247
2248         client->recognition_result_cb = NULL;
2249         client->recognition_result_user_data = NULL;
2250
2251         return 0;
2252 }
2253
2254 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
2255 {
2256         stt_client_s* client = NULL;
2257         if (0 != __stt_get_feature_enabled()) {
2258                 return STT_ERROR_NOT_SUPPORTED;
2259         }
2260         if (0 != __stt_check_privilege()) {
2261                 return STT_ERROR_PERMISSION_DENIED;
2262         }
2263         if (0 != __stt_check_handle(stt, &client)) {
2264                 return STT_ERROR_INVALID_PARAMETER;
2265         }
2266
2267         if (NULL == callback)
2268                 return STT_ERROR_INVALID_PARAMETER;
2269
2270         if (STT_STATE_CREATED != client->current_state) {
2271                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2272                 return STT_ERROR_INVALID_STATE;
2273         }
2274
2275         client->state_changed_cb = callback;
2276         client->state_changed_user_data = user_data;
2277
2278         return 0;
2279 }
2280
2281 int stt_unset_state_changed_cb(stt_h stt)
2282 {
2283         stt_client_s* client = NULL;
2284         if (0 != __stt_get_feature_enabled()) {
2285                 return STT_ERROR_NOT_SUPPORTED;
2286         }
2287         if (0 != __stt_check_privilege()) {
2288                 return STT_ERROR_PERMISSION_DENIED;
2289         }
2290         if (0 != __stt_check_handle(stt, &client)) {
2291                 return STT_ERROR_INVALID_PARAMETER;
2292         }
2293
2294         if (STT_STATE_CREATED != client->current_state) {
2295                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2296                 return STT_ERROR_INVALID_STATE;
2297         }
2298
2299         client->state_changed_cb = NULL;
2300         client->state_changed_user_data = NULL;
2301
2302         return 0;
2303 }
2304
2305 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
2306 {
2307         stt_client_s* client = NULL;
2308         if (0 != __stt_get_feature_enabled()) {
2309                 return STT_ERROR_NOT_SUPPORTED;
2310         }
2311         if (0 != __stt_check_privilege()) {
2312                 return STT_ERROR_PERMISSION_DENIED;
2313         }
2314         if (0 != __stt_check_handle(stt, &client)) {
2315                 return STT_ERROR_INVALID_PARAMETER;
2316         }
2317
2318         if (NULL == callback)
2319                 return STT_ERROR_INVALID_PARAMETER;
2320
2321         if (STT_STATE_CREATED != client->current_state) {
2322                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2323                 return STT_ERROR_INVALID_STATE;
2324         }
2325
2326         client->error_cb = callback;
2327         client->error_user_data = user_data;
2328
2329         return 0;
2330 }
2331
2332 int stt_unset_error_cb(stt_h stt)
2333 {
2334         stt_client_s* client = NULL;
2335         if (0 != __stt_get_feature_enabled()) {
2336                 return STT_ERROR_NOT_SUPPORTED;
2337         }
2338         if (0 != __stt_check_privilege()) {
2339                 return STT_ERROR_PERMISSION_DENIED;
2340         }
2341         if (0 != __stt_check_handle(stt, &client)) {
2342                 return STT_ERROR_INVALID_PARAMETER;
2343         }
2344
2345         if (STT_STATE_CREATED != client->current_state) {
2346                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2347                 return STT_ERROR_INVALID_STATE;
2348         }
2349
2350         client->error_cb = NULL;
2351         client->error_user_data = NULL;
2352
2353         return 0;
2354 }
2355
2356 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2357 {
2358         stt_client_s* client = NULL;
2359         if (0 != __stt_get_feature_enabled()) {
2360                 return STT_ERROR_NOT_SUPPORTED;
2361         }
2362         if (0 != __stt_check_privilege()) {
2363                 return STT_ERROR_PERMISSION_DENIED;
2364         }
2365         if (0 != __stt_check_handle(stt, &client)) {
2366                 return STT_ERROR_INVALID_PARAMETER;
2367         }
2368
2369         if (NULL == callback)
2370                 return STT_ERROR_INVALID_PARAMETER;
2371
2372         if (STT_STATE_CREATED != client->current_state) {
2373                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2374                 return STT_ERROR_INVALID_STATE;
2375         }
2376
2377         client->default_lang_changed_cb = callback;
2378         client->default_lang_changed_user_data = user_data;
2379
2380         return 0;
2381 }
2382
2383 int stt_unset_default_language_changed_cb(stt_h stt)
2384 {
2385         stt_client_s* client = NULL;
2386         if (0 != __stt_get_feature_enabled()) {
2387                 return STT_ERROR_NOT_SUPPORTED;
2388         }
2389         if (0 != __stt_check_privilege()) {
2390                 return STT_ERROR_PERMISSION_DENIED;
2391         }
2392         if (0 != __stt_check_handle(stt, &client)) {
2393                 return STT_ERROR_INVALID_PARAMETER;
2394         }
2395
2396         if (STT_STATE_CREATED != client->current_state) {
2397                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2398                 return STT_ERROR_INVALID_STATE;
2399         }
2400
2401         client->default_lang_changed_cb = NULL;
2402         client->default_lang_changed_user_data = NULL;
2403
2404         return 0;
2405 }
2406
2407 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2408 {
2409         stt_client_s* client = NULL;
2410         if (0 != __stt_get_feature_enabled()) {
2411                 return STT_ERROR_NOT_SUPPORTED;
2412         }
2413         if (0 != __stt_check_privilege()) {
2414                 return STT_ERROR_PERMISSION_DENIED;
2415         }
2416         if (0 != __stt_check_handle(stt, &client)) {
2417                 return STT_ERROR_INVALID_PARAMETER;
2418         }
2419
2420         if (NULL == callback)
2421                 return STT_ERROR_INVALID_PARAMETER;
2422
2423         if (STT_STATE_CREATED != client->current_state) {
2424                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2425                 return STT_ERROR_INVALID_STATE;
2426         }
2427
2428         client->engine_changed_cb = callback;
2429         client->engine_changed_user_data = user_data;
2430
2431         return 0;
2432 }
2433
2434 int stt_unset_engine_changed_cb(stt_h stt)
2435 {
2436         stt_client_s* client = NULL;
2437         if (0 != __stt_get_feature_enabled()) {
2438                 return STT_ERROR_NOT_SUPPORTED;
2439         }
2440         if (0 != __stt_check_privilege()) {
2441                 return STT_ERROR_PERMISSION_DENIED;
2442         }
2443         if (0 != __stt_check_handle(stt, &client)) {
2444                 return STT_ERROR_INVALID_PARAMETER;
2445         }
2446
2447         if (STT_STATE_CREATED != client->current_state) {
2448                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2449                 return STT_ERROR_INVALID_STATE;
2450         }
2451
2452         client->engine_changed_cb = NULL;
2453         client->engine_changed_user_data = NULL;
2454
2455         return 0;
2456 }
2457
2458 int stt_set_speech_status_cb(stt_h stt, stt_speech_status_cb callback, void* user_data)
2459 {
2460         stt_client_s* client = NULL;
2461         if (0 != __stt_get_feature_enabled()) {
2462                 return STT_ERROR_NOT_SUPPORTED;
2463         }
2464         if (0 != __stt_check_privilege()) {
2465                 return STT_ERROR_PERMISSION_DENIED;
2466         }
2467         if (0 != __stt_check_handle(stt, &client)) {
2468                 return STT_ERROR_INVALID_PARAMETER;
2469         }
2470
2471         if (NULL == callback)
2472                 return STT_ERROR_INVALID_PARAMETER;
2473
2474         if (STT_STATE_CREATED != client->current_state) {
2475                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2476                 return STT_ERROR_INVALID_STATE;
2477         }
2478
2479         client->speech_status_cb = callback;
2480         client->speech_status_user_data = user_data;
2481
2482         return 0;
2483 }
2484
2485 int stt_unset_speech_status_cb(stt_h stt)
2486 {
2487         stt_client_s* client = NULL;
2488         if (0 != __stt_get_feature_enabled()) {
2489                 return STT_ERROR_NOT_SUPPORTED;
2490         }
2491         if (0 != __stt_check_privilege()) {
2492                 return STT_ERROR_PERMISSION_DENIED;
2493         }
2494         if (0 != __stt_check_handle(stt, &client)) {
2495                 return STT_ERROR_INVALID_PARAMETER;
2496         }
2497
2498         if (STT_STATE_CREATED != client->current_state) {
2499                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2500                 return STT_ERROR_INVALID_STATE;
2501         }
2502
2503         client->speech_status_cb = NULL;
2504         client->speech_status_user_data = NULL;
2505
2506         return 0;
2507 }
2508
2509 int stt_start_file(stt_h stt, const char* language, const char* type, const char* filepath, stt_audio_type_e audio_type, int sample_rate)
2510 {
2511         stt_client_s* client = NULL;
2512         if (0 != __stt_get_feature_enabled()) {
2513                 return STT_ERROR_NOT_SUPPORTED;
2514         }
2515         if (0 != __stt_check_privilege()) {
2516                 return STT_ERROR_PERMISSION_DENIED;
2517         }
2518         if (0 != __stt_check_handle(stt, &client)) {
2519                 return STT_ERROR_INVALID_PARAMETER;
2520         }
2521         if (NULL == filepath) {
2522                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
2523                 return STT_ERROR_INVALID_PARAMETER;
2524         }
2525
2526         SLOG(LOG_DEBUG, TAG_STTC, "===== STT START FILE");
2527
2528         /* check state */
2529         if (client->current_state != STT_STATE_READY) {
2530                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
2531                 return STT_ERROR_INVALID_STATE;
2532         }
2533
2534         if (STT_INTERNAL_STATE_NONE != client->internal_state) {
2535                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
2536                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2537         }
2538
2539         int ret = -1;
2540         char appid[1024] = {0, };
2541         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
2542
2543         if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
2544                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
2545         } else {
2546                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
2547         }
2548
2549         char* temp = NULL;
2550         if (NULL == language) {
2551                 temp = strdup("default");
2552         } else {
2553                 temp = strdup(language);
2554         }
2555
2556         if (NULL == temp) {
2557                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
2558                 return STT_ERROR_OUT_OF_MEMORY;
2559         }
2560
2561         if (true == client->credential_needed && NULL == client->credential) {
2562                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
2563                 free(temp);
2564                 temp = NULL;
2565                 return STT_ERROR_PERMISSION_DENIED;
2566         }
2567
2568         client->internal_state = STT_INTERNAL_STATE_STARTING;
2569         ret = stt_dbus_request_start_file(client->uid, temp, type, client->silence, appid, client->credential, filepath, audio_type, sample_rate);
2570         if (0 != ret) {
2571                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start file : %s", __stt_get_error_code(ret));
2572                 client->internal_state = STT_INTERNAL_STATE_NONE;
2573         } else {
2574                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
2575         }
2576
2577         free(temp);
2578         temp = NULL;
2579
2580         SLOG(LOG_DEBUG, TAG_STTC, "=====");
2581         SLOG(LOG_DEBUG, TAG_STTC, " ");
2582
2583         return ret;
2584 }
2585
2586 int stt_cancel_file(stt_h stt)
2587 {
2588         stt_client_s* client = NULL;
2589         if (0 != __stt_get_feature_enabled()) {
2590                 return STT_ERROR_NOT_SUPPORTED;
2591         }
2592         if (0 != __stt_check_privilege()) {
2593                 return STT_ERROR_PERMISSION_DENIED;
2594         }
2595         if (0 != __stt_check_handle(stt, &client)) {
2596                 return STT_ERROR_INVALID_PARAMETER;
2597         }
2598
2599         SLOG(LOG_DEBUG, TAG_STTC, "===== STT CANCEL FILE");
2600
2601         /* check state */
2602         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
2603                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
2604                 return STT_ERROR_INVALID_STATE;
2605         }
2606
2607         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
2608                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
2609                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2610         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
2611                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
2612                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
2613         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
2614                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
2615                 return STT_ERROR_IN_PROGRESS_TO_READY;
2616         }
2617
2618         client->internal_state = STT_INTERNAL_STATE_CANCELING;
2619         int ret = stt_dbus_request_cancel_file(client->uid);
2620         if (0 != ret) {
2621                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel file : %s", __stt_get_error_code(ret));
2622                 client->internal_state = STT_INTERNAL_STATE_NONE;
2623         } else {
2624                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Cancel file is successful but not done");
2625         }
2626
2627         SLOG(LOG_DEBUG, TAG_STTC, "=====");
2628         SLOG(LOG_DEBUG, TAG_STTC, " ");
2629
2630         return ret;
2631 }
2632