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