32b2534228ea59368be856b3303cd4075f9a8a5b
[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 (sizeof(smack_label) != fread(smack_label, 1, sizeof(smack_label), fp))
109                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread");
110
111                 fclose(fp);
112         }
113
114         pid_t pid = getpid();
115         char *session = cynara_session_from_pid(pid);
116         int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
117         SLOG(LOG_DEBUG, TAG_STTC, "[Client]cynara_check returned %d(%s)", ret, (CYNARA_API_ACCESS_ALLOWED == ret) ? "Allowed" : "Denied");
118         if (session) {
119                 free(session);
120                 session = NULL;
121         }
122
123         if (ret != CYNARA_API_ACCESS_ALLOWED)
124                 return false;
125         return true;
126 }
127
128 static void __check_privilege_deinitialize()
129 {
130         if (p_cynara)
131                 cynara_finish(p_cynara);
132         p_cynara = NULL;
133 }
134
135 static int __stt_check_privilege()
136 {
137         char uid[16];
138
139         if (0 == g_privilege_allowed) {
140                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied");
141                 return STT_ERROR_PERMISSION_DENIED;
142         } else if (-1 == g_privilege_allowed) {
143                 if (false == __check_privilege_initialize()) {
144                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed");
145                         return STT_ERROR_PERMISSION_DENIED;
146                 }
147                 snprintf(uid, 16, "%d", getuid());
148                 if (false == __check_privilege(uid, STT_PRIVILEGE_RECORDER)) {
149                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied");
150                         g_privilege_allowed = 0;
151                         __check_privilege_deinitialize();
152                         return STT_ERROR_PERMISSION_DENIED;
153                 }
154                 __check_privilege_deinitialize();
155         }
156
157         g_privilege_allowed = 1;
158         return STT_ERROR_NONE;
159 }
160
161 static int __stt_check_privilege_for_applaunch()
162 {
163         char uid[16];
164
165         if (0 == g_privilege_applaunch_allowed) {
166                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission for applaunch is denied");
167                 return STT_ERROR_PERMISSION_DENIED;
168         } else if (-1 == g_privilege_applaunch_allowed) {
169                 if (false == __check_privilege_initialize()) {
170                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed (applaunch)");
171                         return STT_ERROR_PERMISSION_DENIED;
172                 }
173                 snprintf(uid, 16, "%d", getuid());
174                 if (false == __check_privilege(uid, STT_PRIVILEGE_APPLAUNCH)) {
175                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied : appmanager.launch");
176                         g_privilege_applaunch_allowed = 0;
177                         __check_privilege_deinitialize();
178                         return STT_ERROR_PERMISSION_DENIED;
179                 }
180                 __check_privilege_deinitialize();
181         }
182
183         g_privilege_applaunch_allowed = 1;
184         return STT_ERROR_NONE;
185 }
186
187 static const char* __stt_get_error_code(stt_error_e err)
188 {
189         switch (err) {
190         case STT_ERROR_NONE:                    return "STT_ERROR_NONE";
191         case STT_ERROR_OUT_OF_MEMORY:           return "STT_ERROR_OUT_OF_MEMORY";
192         case STT_ERROR_IO_ERROR:                return "STT_ERROR_IO_ERROR";
193         case STT_ERROR_INVALID_PARAMETER:       return "STT_ERROR_INVALID_PARAMETER";
194         case STT_ERROR_TIMED_OUT:               return "STT_ERROR_TIMED_OUT";
195         case STT_ERROR_RECORDER_BUSY:           return "STT_ERROR_RECORDER_BUSY";
196         case STT_ERROR_OUT_OF_NETWORK:          return "STT_ERROR_OUT_OF_NETWORK";
197         case STT_ERROR_PERMISSION_DENIED:       return "STT_ERROR_PERMISSION_DENIED";
198         case STT_ERROR_NOT_SUPPORTED:           return "STT_ERROR_NOT_SUPPORTED";
199         case STT_ERROR_INVALID_STATE:           return "STT_ERROR_INVALID_STATE";
200         case STT_ERROR_INVALID_LANGUAGE:        return "STT_ERROR_INVALID_LANGUAGE";
201         case STT_ERROR_ENGINE_NOT_FOUND:        return "STT_ERROR_ENGINE_NOT_FOUND";
202         case STT_ERROR_OPERATION_FAILED:        return "STT_ERROR_OPERATION_FAILED";
203         case STT_ERROR_NOT_SUPPORTED_FEATURE:   return "STT_ERROR_NOT_SUPPORTED_FEATURE";
204         case STT_ERROR_SERVICE_RESET:           return "STT_ERROR_SERVICE_RESET";
205         default:
206                 return "Invalid error code";
207         }
208 }
209
210 static int __stt_convert_config_error_code(stt_config_error_e code)
211 {
212         if (code == STT_CONFIG_ERROR_NONE)                      return STT_ERROR_NONE;
213         if (code == STT_CONFIG_ERROR_OUT_OF_MEMORY)             return STT_ERROR_OUT_OF_MEMORY;
214         if (code == STT_CONFIG_ERROR_IO_ERROR)                  return STT_ERROR_IO_ERROR;
215         if (code == STT_CONFIG_ERROR_INVALID_PARAMETER)         return STT_ERROR_INVALID_PARAMETER;
216         if (code == STT_CONFIG_ERROR_PERMISSION_DENIED)         return STT_ERROR_PERMISSION_DENIED;
217         if (code == STT_CONFIG_ERROR_NOT_SUPPORTED)             return STT_ERROR_NOT_SUPPORTED;
218         if (code == STT_CONFIG_ERROR_INVALID_STATE)             return STT_ERROR_INVALID_STATE;
219         if (code == STT_CONFIG_ERROR_INVALID_LANGUAGE)          return STT_ERROR_INVALID_LANGUAGE;
220         if (code == STT_CONFIG_ERROR_ENGINE_NOT_FOUND)          return STT_ERROR_ENGINE_NOT_FOUND;
221         if (code == STT_CONFIG_ERROR_OPERATION_FAILED)          return STT_ERROR_OPERATION_FAILED;
222
223         return code;
224 }
225
226 void __stt_config_lang_changed_cb(const char* before_language, const char* current_language, void* user_data)
227 {
228         SLOG(LOG_DEBUG, TAG_STTC, "Language changed : Before lang(%s) Current lang(%s)",
229                 before_language, current_language);
230
231         if (0 == strcmp(before_language, current_language)) {
232                 return;
233         }
234
235         GList* client_list = NULL;
236         client_list = stt_client_get_client_list();
237
238         GList *iter = NULL;
239         stt_client_s *data = NULL;
240
241         if (g_list_length(client_list) > 0) {
242                 /* Get a first item */
243                 iter = g_list_first(client_list);
244
245                 while (NULL != iter) {
246                         data = iter->data;
247                         if (NULL != data->default_lang_changed_cb) {
248                                 SLOG(LOG_DEBUG, TAG_STTC, "Call default language changed callback : uid(%d)", data->uid);
249                                 data->default_lang_changed_cb(data->stt, before_language, current_language,
250                                         data->default_lang_changed_user_data);
251                         }
252
253                         /* Next item */
254                         iter = g_list_next(iter);
255                 }
256         }
257
258         return;
259 }
260
261 static Eina_Bool __reconnect_by_engine_changed(void *data)
262 {
263         stt_h stt = (stt_h)data;
264
265         stt_client_s* client = stt_client_get(stt);
266         if (NULL == client) {
267                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
268                 return EINA_FALSE;
269         }
270
271         if (STT_STATE_READY != client->current_state) {
272                 usleep(10000);
273                 return EINA_TRUE;
274         }
275
276         int ret = stt_unprepare(stt);
277         if (0 != ret) {
278                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
279         }
280         ret = stt_prepare(stt);
281         if (0 != ret) {
282                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
283         }
284
285         return EINA_FALSE;
286 }
287
288 void __stt_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, bool support_silence, bool need_credential, void* user_data)
289 {
290         stt_h stt = (stt_h)user_data;
291
292         stt_client_s* client = stt_client_get(stt);
293         if (NULL == client) {
294                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
295                 return;
296         }
297
298         if (NULL != engine_id)  SLOG(LOG_DEBUG, TAG_STTC, "Engine id(%s)", engine_id);
299         if (NULL != setting)    SLOG(LOG_DEBUG, TAG_STTC, "Engine setting(%s)", setting);
300         if (NULL != language)   SLOG(LOG_DEBUG, TAG_STTC, "Language(%s)", language);
301         SLOG(LOG_DEBUG, TAG_STTC, "Silence(%s), Credential(%s)", support_silence ? "on" : "off", need_credential ? "need" : "no need");
302
303         /* When the default engine is changed, please unload the old engine and load the new one. */
304         int ret = -1;
305
306         if (NULL == client->current_engine_id) {
307                 if (STT_STATE_RECORDING == client->current_state || STT_STATE_PROCESSING == client->current_state) {
308                         ret = stt_cancel(stt);
309                         if (0 != ret) {
310                                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] STT client canceling...");
311                         }
312
313                         ecore_idler_add(__reconnect_by_engine_changed, (void*)stt);
314                 } else if (STT_STATE_READY == client->current_state) {
315                         ret = stt_unprepare(stt);
316                         if (0 != ret) {
317                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
318                         }
319                         ret = stt_prepare(stt);
320                         if (0 != ret) {
321                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare for setting a new engine... (%d)", ret);
322                         }
323                 }
324         }
325
326         /* call callback function */
327         if (NULL != client->engine_changed_cb) {
328                 client->engine_changed_cb(stt, engine_id, language, support_silence, need_credential, client->engine_changed_user_data);
329         } else {
330                 SLOG(LOG_WARN, TAG_STTC, "No registered callback function for engine change");
331         }
332         return;
333 }
334
335 static int __stt_check_handle(stt_h stt, stt_client_s** client)
336 {
337         if (NULL == stt) {
338                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input handle is null");
339                 return STT_ERROR_INVALID_PARAMETER;
340         }
341
342         stt_client_s* temp = NULL;
343         temp = stt_client_get(stt);
344
345         /* check handle */
346         if (NULL == temp) {
347                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available");
348                 return STT_ERROR_INVALID_PARAMETER;
349         }
350         *client = temp;
351
352         return STT_ERROR_NONE;
353 }
354
355 int stt_create(stt_h* stt)
356 {
357         if (0 != __stt_get_feature_enabled()) {
358                 return STT_ERROR_NOT_SUPPORTED;
359         }
360         if (0 != __stt_check_privilege()) {
361                 return STT_ERROR_PERMISSION_DENIED;
362         }
363
364         SLOG(LOG_DEBUG, TAG_STTC, "===== Create STT");
365
366         if (NULL == stt) {
367                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is null");
368                 return STT_ERROR_INVALID_PARAMETER;
369         }
370
371         if (0 == stt_client_get_size()) {
372                 if (0 != stt_dbus_open_connection()) {
373                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to open connection");
374                         return STT_ERROR_OPERATION_FAILED;
375                 }
376         }
377
378         if (0 != stt_client_new(stt)) {
379                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client!");
380                 return STT_ERROR_OUT_OF_MEMORY;
381         }
382
383         stt_client_s* client = stt_client_get(*stt);
384         if (NULL == client) {
385                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to create client");
386                 stt_client_destroy(*stt);
387                 return STT_ERROR_OPERATION_FAILED;
388         }
389
390         int ret = stt_config_mgr_initialize(client->uid);
391         ret = __stt_convert_config_error_code(ret);
392         if (0 != ret) {
393                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to init config manager : %s", __stt_get_error_code(ret));
394                 stt_client_destroy(*stt);
395                 return ret;
396         }
397
398         ret = stt_config_mgr_set_callback(client->uid, __stt_config_engine_changed_cb, __stt_config_lang_changed_cb, NULL, client->stt);
399         ret = __stt_convert_config_error_code(ret);
400         if (0 != ret) {
401                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set config changed : %s", __stt_get_error_code(ret));
402                 stt_client_destroy(*stt);
403                 return ret;
404         }
405
406         SLOG(LOG_DEBUG, TAG_STTC, "[Success] uid(%d)", (*stt)->handle);
407
408         SLOG(LOG_DEBUG, TAG_STTC, "=====");
409         SLOG(LOG_DEBUG, TAG_STTC, " ");
410
411         return STT_ERROR_NONE;
412 }
413
414 int stt_destroy(stt_h stt)
415 {
416         stt_client_s* client = NULL;
417         if (0 != __stt_get_feature_enabled()) {
418                 return STT_ERROR_NOT_SUPPORTED;
419         }
420         if (0 != __stt_check_privilege()) {
421                 return STT_ERROR_PERMISSION_DENIED;
422         }
423         if (0 != __stt_check_handle(stt, &client)) {
424                 return STT_ERROR_INVALID_PARAMETER;
425         }
426
427         SLOG(LOG_DEBUG, TAG_STTC, "===== Destroy STT");
428
429         /* check used callback */
430         if (0 != stt_client_get_use_callback(client)) {
431                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Cannot destroy in Callback function");
432                 return STT_ERROR_OPERATION_FAILED;
433         }
434
435         stt_config_mgr_finalize(client->uid);
436
437         int ret = -1;
438
439         /* check state */
440         switch (client->current_state) {
441         case STT_STATE_PROCESSING:
442         case STT_STATE_RECORDING:
443         case STT_STATE_READY:
444                 ret = stt_dbus_request_finalize(client->uid);
445                 if (0 != ret) {
446                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret));
447                 }
448         case STT_STATE_CREATED:
449                 if (NULL != g_connect_timer) {
450                         SLOG(LOG_DEBUG, TAG_STTC, "Connect Timer is deleted");
451                         ecore_timer_del(g_connect_timer);
452                         g_connect_timer = NULL;
453                 }
454
455                 /* Free resources */
456                 stt_client_destroy(stt);
457                 break;
458         default:
459                 break;
460         }
461
462         if (0 == stt_client_get_size()) {
463                 if (0 != stt_dbus_close_connection()) {
464                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to close connection");
465                 }
466         }
467
468         stt = NULL;
469
470         SLOG(LOG_DEBUG, TAG_STTC, "=====");
471         SLOG(LOG_DEBUG, TAG_STTC, " ");
472
473         return STT_ERROR_NONE;
474 }
475
476 bool __stt_config_supported_engine_cb(const char* engine_id, const char* engine_name,
477                                       const char* setting, bool support_silence, void* user_data)
478 {
479         stt_h stt = (stt_h)user_data;
480
481         stt_client_s* client = stt_client_get(stt);
482         if (NULL == client) {
483                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
484                 return false;
485         }
486
487         /* call callback function */
488         if (NULL != client->supported_engine_cb) {
489                 return client->supported_engine_cb(stt, engine_id, engine_name, client->supported_engine_user_data);
490         } else {
491                 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported engine");
492         }
493
494         return false;
495 }
496
497 int stt_foreach_supported_engines(stt_h stt, stt_supported_engine_cb callback, void* user_data)
498 {
499         stt_client_s* client = NULL;
500         if (0 != __stt_get_feature_enabled()) {
501                 return STT_ERROR_NOT_SUPPORTED;
502         }
503         if (0 != __stt_check_privilege()) {
504                 return STT_ERROR_PERMISSION_DENIED;
505         }
506         if (0 != __stt_check_handle(stt, &client)) {
507                 return STT_ERROR_INVALID_PARAMETER;
508         }
509
510         SLOG(LOG_DEBUG, TAG_STTC, "===== Foreach Supported engine");
511
512         if (NULL == callback) {
513                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
514                 return STT_ERROR_INVALID_PARAMETER;
515         }
516
517         if (client->current_state != STT_STATE_CREATED) {
518                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
519                 return STT_ERROR_INVALID_STATE;
520         }
521
522         client->supported_engine_cb = callback;
523         client->supported_engine_user_data = user_data;
524
525         int ret = 0;
526         ret = stt_config_mgr_get_engine_list(__stt_config_supported_engine_cb, client->stt);
527         ret = __stt_convert_config_error_code(ret);
528         if (0 != ret) {
529                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get engines : %s", __stt_get_error_code(ret));
530         }
531
532         client->supported_engine_cb = NULL;
533         client->supported_engine_user_data = NULL;
534
535         SLOG(LOG_DEBUG, TAG_STTC, "=====");
536         SLOG(LOG_DEBUG, TAG_STTC, " ");
537
538         return ret;
539 }
540
541 int stt_get_engine(stt_h stt, char** engine_id)
542 {
543         stt_client_s* client = NULL;
544         if (0 != __stt_get_feature_enabled()) {
545                 return STT_ERROR_NOT_SUPPORTED;
546         }
547         if (0 != __stt_check_privilege()) {
548                 return STT_ERROR_PERMISSION_DENIED;
549         }
550         if (0 != __stt_check_handle(stt, &client)) {
551                 return STT_ERROR_INVALID_PARAMETER;
552         }
553
554         SLOG(LOG_DEBUG, TAG_STTC, "===== Get current engine");
555
556         if (NULL == engine_id) {
557                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
558                 return STT_ERROR_INVALID_PARAMETER;
559         }
560
561         if (client->current_state != STT_STATE_CREATED) {
562                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
563                 return STT_ERROR_INVALID_STATE;
564         }
565
566         int ret = 0;
567
568         if (NULL != client->current_engine_id) {
569                 *engine_id = strdup(client->current_engine_id);
570                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
571         } else {
572
573                 ret = stt_config_mgr_get_engine(engine_id);
574                 ret = __stt_convert_config_error_code(ret);
575                 if (0 != ret) {
576                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request get current engine : %s", __stt_get_error_code(ret));
577                 } else {
578                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", *engine_id);
579                 }
580         }
581
582         SLOG(LOG_DEBUG, TAG_STTC, "=====");
583         SLOG(LOG_DEBUG, TAG_STTC, " ");
584
585         return ret;
586 }
587
588 int __stt_set_buxtonkey(const char* engine_id)
589 {
590         /* Set vconfkey */
591         struct buxton_client * bux_cli;
592         struct buxton_layer * bux_layer;
593         struct buxton_value * bux_val;
594
595         int ret = buxton_open(&bux_cli, NULL, NULL);
596         if (0 != ret) {
597                 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] Fail to open buxton client");
598                 return STT_ERROR_OPERATION_FAILED;
599         }
600         SLOG(LOG_DEBUG, stt_tag(), "[DBUS-BUXTON2] buxton_open: %d", ret);
601         bux_layer = buxton_create_layer("system");
602         if (NULL == bux_layer) {
603                 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] buxton_create_layer FAIL");
604                 buxton_close(bux_cli);
605                 bux_cli = NULL;
606                 return STT_ERROR_OPERATION_FAILED;
607         }
608         bux_val = buxton_value_create_string(engine_id);
609         if (NULL == bux_val) {
610                 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] buxton_value_create_string FAIL");
611                 buxton_free_layer(bux_layer);
612                 buxton_close(bux_cli);
613                 bux_layer = NULL;
614                 bux_cli = NULL;
615                 return STT_ERROR_OPERATION_FAILED;
616         }
617
618         ret = buxton_set_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_CUSTOM, bux_val);
619         if (0 != ret) {
620                 SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] Fail to set value sync");
621                 buxton_value_free(bux_val);
622                 buxton_free_layer(bux_layer);
623                 buxton_close(bux_cli);
624
625                 bux_cli = NULL;
626                 bux_layer = NULL;
627                 bux_val = NULL;
628                 return STT_ERROR_OPERATION_FAILED;
629         }
630         SLOG(LOG_DEBUG, stt_tag(), "[DBUS-BUXTON2] buxton_set_value_sync: %d, %s", ret, STT_ENGINE_DB_CUSTOM);
631
632         buxton_value_free(bux_val);
633         buxton_free_layer(bux_layer);
634         buxton_close(bux_cli);
635
636         bux_cli = NULL;
637         bux_layer = NULL;
638         bux_val = NULL;
639
640         return STT_ERROR_NONE;
641 }
642
643 int stt_set_engine(stt_h stt, const char* engine_id)
644 {
645         stt_client_s* client = NULL;
646         if (0 != __stt_get_feature_enabled()) {
647                 return STT_ERROR_NOT_SUPPORTED;
648         }
649         if (0 != __stt_check_privilege()) {
650                 return STT_ERROR_PERMISSION_DENIED;
651         }
652         if (0 != __stt_check_privilege_for_applaunch()) {
653                 return STT_ERROR_PERMISSION_DENIED;
654         }
655         if (0 != __stt_check_handle(stt, &client)) {
656                 return STT_ERROR_INVALID_PARAMETER;
657         }
658
659         SLOG(LOG_DEBUG, TAG_STTC, "===== Set current engine");
660
661         if (NULL == engine_id) {
662                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
663                 return STT_ERROR_INVALID_PARAMETER;
664         }
665
666         /* check state */
667         if (client->current_state != STT_STATE_CREATED) {
668                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED", client->current_state);
669                 return STT_ERROR_INVALID_STATE;
670         }
671
672         if (NULL != client->current_engine_id) {
673                 free(client->current_engine_id);
674                 client->current_engine_id = NULL;
675         }
676
677         SLOG(LOG_DEBUG, TAG_STTC, "===== engined_id(%s)", engine_id);
678
679         client->current_engine_id = strdup(engine_id);
680
681         /* Set vconfkey */
682         int ret = __stt_set_buxtonkey(engine_id);
683         if (0 != ret) {
684                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] set buxtonkey Failed!!!");
685                 return ret;
686         }
687
688         SLOG(LOG_DEBUG, TAG_STTC, "=====");
689         SLOG(LOG_DEBUG, TAG_STTC, " ");
690
691         return 0;
692 }
693
694 int stt_set_credential(stt_h stt, const char* credential)
695 {
696         stt_client_s* client = NULL;
697         if (0 != __stt_get_feature_enabled()) {
698                 return STT_ERROR_NOT_SUPPORTED;
699         }
700         if (0 != __stt_check_privilege()) {
701                 return STT_ERROR_PERMISSION_DENIED;
702         }
703         if (0 != __stt_check_handle(stt, &client)) {
704                 return STT_ERROR_INVALID_PARAMETER;
705         }
706
707         SLOG(LOG_DEBUG, TAG_STTC, "===== Set credential");
708
709         if (NULL == credential) {
710                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
711                 return STT_ERROR_INVALID_PARAMETER;
712         }
713
714         /* check state */
715         if (client->current_state != STT_STATE_CREATED && client->current_state != STT_STATE_READY) {
716                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED or READY", client->current_state);
717                 return STT_ERROR_INVALID_STATE;
718         }
719
720         if (NULL != client->credential) {
721                 free(client->credential);
722                 client->credential = NULL;
723         }
724         client->credential = strdup(credential);
725
726         SLOG(LOG_DEBUG, TAG_STTC, "=====");
727         SLOG(LOG_DEBUG, TAG_STTC, " ");
728
729         return STT_ERROR_NONE;
730 }
731
732 int stt_set_private_data(stt_h stt, const char* key, const char* data)
733 {
734         stt_client_s* client = NULL;
735         if (0 != __stt_get_feature_enabled()) {
736                 return STT_ERROR_NOT_SUPPORTED;
737         }
738         if (0 != __stt_check_privilege()) {
739                 return STT_ERROR_PERMISSION_DENIED;
740         }
741         if (0 != __stt_check_handle(stt, &client)) {
742                 return STT_ERROR_INVALID_PARAMETER;
743         }
744
745         SLOG(LOG_DEBUG, TAG_STTC, "===== Set private data");
746
747         if (NULL == key || NULL == data) {
748                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
749                 return STT_ERROR_INVALID_PARAMETER;
750         }
751
752         /* check state */
753         if (STT_STATE_READY != client->current_state) {
754                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
755                 return STT_ERROR_INVALID_STATE;
756         }
757
758         if (true != client->internal && (0 == strcmp(key, "server") || 0 == strcmp(key, "rampcode") || 0 == strcmp(key, "epd"))) {
759                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This is not an internal app");
760                 return STT_ERROR_INVALID_PARAMETER;
761         }
762
763         int ret = -1;
764         int count = 0;
765         while (0 != ret) {
766                 ret = stt_dbus_request_set_private_data(client->uid, key, data);
767                 if (0 != ret) {
768                         if (STT_ERROR_TIMED_OUT != ret) {
769                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data : %s", __stt_get_error_code(ret));
770                                 return ret;
771                         } else {
772                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry : %s", __stt_get_error_code(ret));
773                                 usleep(10000);
774                                 count++;
775                                 if (STT_RETRY_COUNT == count) {
776                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
777                                         return ret;
778                                 }
779                         }
780                 }
781         }
782
783         SLOG(LOG_DEBUG, TAG_STTC, "=====");
784         SLOG(LOG_DEBUG, TAG_STTC, "");
785
786         return STT_ERROR_NONE;
787
788 }
789 int stt_get_private_data(stt_h stt, const char* key, char** data)
790 {
791         stt_client_s* client = NULL;
792         if (0 != __stt_get_feature_enabled()) {
793                 return STT_ERROR_NOT_SUPPORTED;
794         }
795         if (0 != __stt_check_privilege()) {
796                 return STT_ERROR_PERMISSION_DENIED;
797         }
798         if (0 != __stt_check_handle(stt, &client)) {
799                 return STT_ERROR_INVALID_PARAMETER;
800         }
801
802         SLOG(LOG_DEBUG, TAG_STTC, "===== Get private data");
803
804         if (NULL == key || NULL == data) {
805                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
806                 return STT_ERROR_INVALID_PARAMETER;
807         }
808
809         /* check state */
810         if (STT_STATE_READY != client->current_state) {
811                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
812                 return STT_ERROR_INVALID_STATE;
813         }
814
815         int ret = -1;
816         int count = 0;
817         while (0 != ret) {
818                 ret = stt_dbus_request_get_private_data(client->uid, key, data);
819                 if (0 != ret) {
820                         if (STT_ERROR_TIMED_OUT != ret) {
821                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get private data : %s", __stt_get_error_code(ret));
822                                 return ret;
823                         } else {
824                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry : %s", __stt_get_error_code(ret));
825                                 usleep(10000);
826                                 count++;
827                                 if (STT_RETRY_COUNT == count) {
828                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
829                                         return ret;
830                                 }
831                         }
832                 }
833         }
834
835         if (0 == strncmp(*data, "NULL", strlen(*data))) {
836                 free(*data);
837                 *data = NULL;
838         }
839
840         SLOG(LOG_DEBUG, TAG_STTC, "=====");
841         SLOG(LOG_DEBUG, TAG_STTC, "");
842
843         return STT_ERROR_NONE;
844 }
845
846 int stt_set_server_stt(stt_h stt, const char* key, char* user_data)
847 {
848         int ret = -1;
849         stt_client_s* client = NULL;
850
851         if (0 != __stt_get_feature_enabled()) {
852                 return STT_ERROR_NOT_SUPPORTED;
853         }
854         if (0 != __stt_check_privilege()) {
855                 return STT_ERROR_PERMISSION_DENIED;
856         }
857         if (0 != __stt_check_handle(stt, &client)) {
858                 return STT_ERROR_INVALID_PARAMETER;
859         }
860
861         SLOG(LOG_DEBUG, TAG_STTC, "===== Set STT server");
862
863         if (NULL == key || NULL == user_data) {
864                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
865                 return STT_ERROR_INVALID_PARAMETER;
866         }
867
868         if (STT_STATE_CREATED != client->current_state && STT_STATE_READY != client->current_state) {
869                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] The current state is invalid (%d).", client->current_state);
870                 return STT_ERROR_INVALID_STATE;
871         }
872
873
874         client->internal = true;
875
876         char* private_key = NULL;
877         private_key = strdup(key);
878         if (NULL == private_key) {
879                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory(private_key)");
880                 return STT_ERROR_OUT_OF_MEMORY;
881         }
882
883         char* data = NULL;
884         data = strdup(user_data);
885         if (NULL == data) {
886                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory(data)");
887                 free(private_key);
888                 private_key = NULL;
889                 return STT_ERROR_OUT_OF_MEMORY;
890         }
891
892         ret = stt_set_private_data(stt, private_key, data);
893         if (0 != ret) {
894                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data, ret(%d), key(%s)", ret, private_key);
895         }
896
897         free(data);
898         data = NULL;
899         free(private_key);
900         private_key = NULL;
901
902         SLOG(LOG_DEBUG, TAG_STTC, "======");
903         SLOG(LOG_DEBUG, TAG_STTC, " ");
904
905         return ret;
906 }
907
908 static Eina_Bool __stt_connect_daemon(void *data)
909 {
910         stt_client_s* client = (stt_client_s*)data;
911         int ret = -1;
912
913         if (NULL == client) {
914                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available");
915                 g_connect_timer = NULL;
916                 return EINA_FALSE;
917         }
918
919         if (0 == stt_client_get_size() || NULL == stt_client_get_by_uid(client->uid)) {
920                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Client has been already destroyed");
921                 return EINA_FALSE;
922         }
923
924         /* Check and Set vconfkey of custom engine before sending hello */
925         if (1 == g_privilege_applaunch_allowed && NULL != client->current_engine_id) {
926                 /* Set vconfkey */
927                 ret = __stt_set_buxtonkey(client->current_engine_id);
928                 if (0 != ret) {
929                         SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] set buxtonkey Failed!!! (inside __stt_connect_daemon)");
930                         return EINA_TRUE;
931                 }
932         }
933
934         /* Send hello */
935         ret = stt_dbus_request_hello(client->uid);
936
937         if (0 != ret) {
938                 if (STT_ERROR_INVALID_STATE == ret) {
939                         g_connect_timer = NULL;
940                         return EINA_FALSE;
941                 }
942                 return EINA_TRUE;
943         }
944
945         g_connect_timer = NULL;
946         SLOG(LOG_DEBUG, TAG_STTC, "===== Connect stt-service");
947
948         /* request initialization */
949         bool silence_supported = false;
950         bool credential_needed = false;
951
952         ret = stt_dbus_request_initialize(client->uid, &silence_supported, &credential_needed);
953
954         if (STT_ERROR_ENGINE_NOT_FOUND == ret) {
955                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to initialize : %s", __stt_get_error_code(ret));
956
957                 client->reason = STT_ERROR_ENGINE_NOT_FOUND;
958                 ecore_main_loop_thread_safe_call_async(__stt_notify_error, (void*)client);
959
960                 return EINA_FALSE;
961
962         } else if (STT_ERROR_NONE != ret) {
963                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] Fail to connection. Retry to connect");
964                 return EINA_TRUE;
965         } else {
966                 /* success to connect stt-service */
967                 client->silence_supported = silence_supported;
968                 client->credential_needed = credential_needed;
969                 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
970         }
971
972 #ifdef __UNUSED_CODES__
973         if (NULL != client->current_engine_id) {
974                 ret = -1;
975                 int count = 0;
976                 silence_supported = false;
977                 credential_needed = false;
978                 SLOG(LOG_DEBUG, TAG_STTC, "[WARNING] current_engine_id(%s)", client->current_engine_id);
979
980                 while (0 != ret) {
981                         ret = stt_dbus_request_set_current_engine(client->uid, client->current_engine_id, &silence_supported, &credential_needed);
982                         if (0 != ret) {
983                                 if (STT_ERROR_TIMED_OUT != ret) {
984                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set current engine : %s", __stt_get_error_code(ret));
985                                         return ret;
986                                 } else {
987                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
988                                         usleep(10000);
989                                         count++;
990                                         if (STT_RETRY_COUNT == count) {
991                                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
992                                                 return ret;
993                                         }
994                                 }
995                         } else {
996                                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", client->current_engine_id);
997
998                                 /* success to change engine */
999                                 client->silence_supported = silence_supported;
1000                                 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
1001                         }
1002                 }
1003         }
1004 #endif
1005         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] uid(%d)", client->uid);
1006
1007         client->before_state = client->current_state;
1008         client->current_state = STT_STATE_READY;
1009
1010         if (NULL != client->state_changed_cb) {
1011                 stt_client_use_callback(client);
1012                 client->state_changed_cb(client->stt, client->before_state,
1013                         client->current_state, client->state_changed_user_data);
1014                 stt_client_not_use_callback(client);
1015                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
1016         } else {
1017                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1018         }
1019
1020         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1021         SLOG(LOG_DEBUG, TAG_STTC, "  ");
1022
1023         return EINA_FALSE;
1024 }
1025
1026 int stt_prepare(stt_h stt)
1027 {
1028         stt_client_s* client = NULL;
1029         if (0 != __stt_get_feature_enabled()) {
1030                 return STT_ERROR_NOT_SUPPORTED;
1031         }
1032         if (0 != __stt_check_privilege()) {
1033                 return STT_ERROR_PERMISSION_DENIED;
1034         }
1035         if (0 != __stt_check_handle(stt, &client)) {
1036                 return STT_ERROR_INVALID_PARAMETER;
1037         }
1038
1039         /* check state */
1040         if (client->current_state != STT_STATE_CREATED) {
1041                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not 'CREATED'", client->current_state);
1042                 return STT_ERROR_INVALID_STATE;
1043         }
1044
1045         g_connect_timer = ecore_timer_add(0.02, __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[1024] = {0, };
1621         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
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 (NULL == temp) {
1637                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1638                 return STT_ERROR_OUT_OF_MEMORY;
1639         }
1640
1641         if (true == client->credential_needed && NULL == client->credential) {
1642                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
1643                 free(temp);
1644                 temp = NULL;
1645                 return STT_ERROR_PERMISSION_DENIED;
1646         }
1647
1648         client->internal_state = STT_INTERNAL_STATE_STARTING;
1649         ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential);
1650         if (0 != ret) {
1651                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret));
1652                 client->internal_state = STT_INTERNAL_STATE_NONE;
1653         } else {
1654                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
1655         }
1656
1657         free(temp);
1658         temp = NULL;
1659
1660         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1661         SLOG(LOG_DEBUG, TAG_STTC, " ");
1662
1663         return ret;
1664 }
1665
1666 int stt_stop(stt_h stt)
1667 {
1668         stt_client_s* client = NULL;
1669         if (0 != __stt_get_feature_enabled()) {
1670                 return STT_ERROR_NOT_SUPPORTED;
1671         }
1672         if (0 != __stt_check_privilege()) {
1673                 return STT_ERROR_PERMISSION_DENIED;
1674         }
1675         if (0 != __stt_check_handle(stt, &client)) {
1676                 return STT_ERROR_INVALID_PARAMETER;
1677         }
1678
1679         SLOG(LOG_DEBUG, TAG_STTC, "===== STT STOP");
1680
1681         /* check state */
1682         if (client->current_state != STT_STATE_RECORDING) {
1683                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Current state(%d) is NOT RECORDING", client->current_state);
1684                 return STT_ERROR_INVALID_STATE;
1685         }
1686
1687         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1688                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1689                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1690         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1691                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1692                 return STT_ERROR_IN_PROGRESS_TO_READY;
1693         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1694                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1695                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1696         }
1697
1698         client->internal_state = STT_INTERNAL_STATE_STOPPING;
1699         int ret = stt_dbus_request_stop(client->uid);
1700         if (0 != ret) {
1701                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret));
1702                 client->internal_state = STT_INTERNAL_STATE_NONE;
1703         } else {
1704                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1705         }
1706
1707         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1708         SLOG(LOG_DEBUG, TAG_STTC, " ");
1709
1710         return ret;
1711 }
1712
1713
1714 int stt_cancel(stt_h stt)
1715 {
1716         stt_client_s* client = NULL;
1717         if (0 != __stt_get_feature_enabled()) {
1718                 return STT_ERROR_NOT_SUPPORTED;
1719         }
1720         if (0 != __stt_check_privilege()) {
1721                 return STT_ERROR_PERMISSION_DENIED;
1722         }
1723         if (0 != __stt_check_handle(stt, &client)) {
1724                 return STT_ERROR_INVALID_PARAMETER;
1725         }
1726
1727         SLOG(LOG_DEBUG, TAG_STTC, "===== STT CANCEL");
1728
1729         /* check state */
1730         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1731                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
1732                 return STT_ERROR_INVALID_STATE;
1733         }
1734
1735         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1736                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1737                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1738         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1739                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1740                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1741         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1742                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1743                 return STT_ERROR_IN_PROGRESS_TO_READY;
1744         }
1745
1746         client->internal_state = STT_INTERNAL_STATE_CANCELING;
1747         int ret = stt_dbus_request_cancel(client->uid);
1748         if (0 != ret) {
1749                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret));
1750                 client->internal_state = STT_INTERNAL_STATE_NONE;
1751         } else {
1752                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1753         }
1754
1755         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1756         SLOG(LOG_DEBUG, TAG_STTC, " ");
1757
1758         return ret;
1759 }
1760
1761 int __stt_cb_set_volume(int uid, float volume)
1762 {
1763         stt_client_s* client = NULL;
1764
1765         client = stt_client_get_by_uid(uid);
1766         if (NULL == client) {
1767                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1768                 return STT_ERROR_INVALID_PARAMETER;
1769         }
1770
1771         if (STT_STATE_RECORDING != client->current_state) {
1772                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1773                 return STT_ERROR_INVALID_STATE;
1774         }
1775
1776         g_volume_db = volume;
1777         SLOG(LOG_DEBUG, TAG_STTC, "Set volume (%f)", g_volume_db);
1778
1779         return 0;
1780 }
1781
1782 int stt_get_recording_volume(stt_h stt, float* volume)
1783 {
1784         stt_client_s* client = NULL;
1785         if (0 != __stt_get_feature_enabled()) {
1786                 return STT_ERROR_NOT_SUPPORTED;
1787         }
1788         if (0 != __stt_check_privilege()) {
1789                 return STT_ERROR_PERMISSION_DENIED;
1790         }
1791         if (0 != __stt_check_handle(stt, &client)) {
1792                 return STT_ERROR_INVALID_PARAMETER;
1793         }
1794
1795         if (NULL == volume) {
1796                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1797                 return STT_ERROR_INVALID_PARAMETER;
1798         }
1799
1800         if (STT_STATE_RECORDING != client->current_state) {
1801                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1802                 return STT_ERROR_INVALID_STATE;
1803         }
1804
1805         *volume = g_volume_db;
1806
1807         return STT_ERROR_NONE;
1808 }
1809
1810 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1811 {
1812         stt_client_s* client = (stt_client_s*)user_data;
1813
1814         /* check handle */
1815         if (NULL == client) {
1816                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1817                 return EINA_FALSE;
1818         }
1819
1820         if (NULL != client->result_time_cb) {
1821                 SLOG(LOG_DEBUG, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1822                         index, event, text, start_time, end_time);
1823                 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1824                         text, start_time, end_time, client->result_time_user_data);
1825         } else {
1826                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1827                 return false;
1828         }
1829
1830         return true;
1831 }
1832
1833 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1834 {
1835         stt_client_s* client = NULL;
1836         if (0 != __stt_get_feature_enabled()) {
1837                 return STT_ERROR_NOT_SUPPORTED;
1838         }
1839         if (0 != __stt_check_privilege()) {
1840                 return STT_ERROR_PERMISSION_DENIED;
1841         }
1842         if (0 != __stt_check_handle(stt, &client)) {
1843                 return STT_ERROR_INVALID_PARAMETER;
1844         }
1845
1846         SLOG(LOG_DEBUG, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1847
1848         if (NULL == callback) {
1849                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1850                 return STT_ERROR_INVALID_PARAMETER;
1851         }
1852
1853         client->result_time_cb = callback;
1854         client->result_time_user_data = user_data;
1855
1856         int ret = -1;
1857         ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1858         ret = __stt_convert_config_error_code(ret);
1859         if (0 != ret) {
1860                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret));
1861         }
1862
1863         client->result_time_cb = NULL;
1864         client->result_time_user_data = NULL;
1865
1866         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1867         SLOG(LOG_DEBUG, TAG_STTC, " ");
1868
1869         return ret;
1870 }
1871
1872 static void __stt_notify_error(void *data)
1873 {
1874         stt_client_s* client = (stt_client_s*)data;
1875
1876         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1877
1878         /* check handle */
1879         if (NULL == client) {
1880                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1881                 return;
1882         }
1883
1884         if (NULL == stt_client_get_by_uid(client->uid))
1885                 return;
1886
1887         if (NULL != client->error_cb) {
1888                 stt_client_use_callback(client);
1889                 g_err_callback_status = true;
1890                 client->error_cb(client->stt, client->reason, client->error_user_data);
1891                 g_err_callback_status = false;
1892                 stt_client_not_use_callback(client);
1893                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1894         } else {
1895                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1896         }
1897
1898         return;
1899 }
1900
1901 int __stt_cb_error(int uid, int reason, char* err_msg)
1902 {
1903         if (-1 == uid) {
1904                 GList* client_list = NULL;
1905                 client_list = stt_client_get_client_list();
1906
1907                 GList *iter = NULL;
1908                 stt_client_s *data = NULL;
1909
1910                 if (g_list_length(client_list) > 0) {
1911                         /* Get a first item */
1912                         iter = g_list_first(client_list);
1913
1914                         while (NULL != iter) {
1915                                 data = iter->data;
1916
1917                                 data->reason = reason;
1918                                 data->internal_state = STT_INTERNAL_STATE_NONE;
1919                                 if (NULL != data->err_msg) {
1920                                         free(data->err_msg);
1921                                         data->err_msg = NULL;
1922                                 }
1923                                 if (NULL != err_msg)
1924                                         data->err_msg = strdup(err_msg);
1925
1926                                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1927
1928                                 if (NULL != data->error_cb) {
1929                                         ecore_main_loop_thread_safe_call_async(__stt_notify_error, data);
1930                                 } else {
1931                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1932                                 }
1933
1934                                 if (STT_ERROR_SERVICE_RESET == reason) {
1935                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1936
1937                                         data->current_state = STT_STATE_CREATED;
1938                                         if (0 != stt_prepare(data->stt)) {
1939                                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1940                                         }
1941                                 }
1942
1943                                 /* Next item */
1944                                 iter = g_list_next(iter);
1945                         }
1946                 }
1947         } else {
1948                 stt_client_s* client = stt_client_get_by_uid(uid);
1949                 if (NULL == client) {
1950                         SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
1951                         return -1;
1952                 }
1953
1954                 client->reason = reason;
1955                 client->internal_state = STT_INTERNAL_STATE_NONE;
1956                 if (NULL != client->err_msg) {
1957                         free(client->err_msg);
1958                         client->err_msg = NULL;
1959                 }
1960                 if (NULL != err_msg)
1961                         client->err_msg = strdup(err_msg);
1962
1963                 SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1964
1965                 if (NULL != client->error_cb) {
1966                         ecore_main_loop_thread_safe_call_async(__stt_notify_error, client);
1967                 } else {
1968                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1969                 }
1970
1971                 if (STT_ERROR_SERVICE_RESET == reason) {
1972                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1973
1974                         client->current_state = STT_STATE_CREATED;
1975                         if (0 != stt_prepare(client->stt)) {
1976                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1977                         }
1978                 }
1979         }
1980
1981         return 0;
1982 }
1983
1984 static void __stt_notify_state_changed(void *data)
1985 {
1986         stt_client_s* client = (stt_client_s*)data;
1987
1988         /* check handle */
1989         if (NULL == client) {
1990                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1991                 return;
1992         }
1993
1994         if (NULL == stt_client_get_by_uid(client->uid)) {
1995                 return;
1996         }
1997
1998         if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
1999                 client->internal_state = STT_INTERNAL_STATE_NONE;
2000                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
2001         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == client->current_state) {
2002                 client->internal_state = STT_INTERNAL_STATE_NONE;
2003                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
2004         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
2005                 client->internal_state = STT_INTERNAL_STATE_NONE;
2006                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
2007         }
2008
2009         if (NULL != client->state_changed_cb) {
2010                 stt_client_use_callback(client);
2011                 client->state_changed_cb(client->stt, client->before_state,
2012                         client->current_state, client->state_changed_user_data);
2013                 stt_client_not_use_callback(client);
2014                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called, State(%d)", client->current_state);
2015         } else {
2016                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null, State(%d)", client->current_state);
2017         }
2018
2019         return;
2020 }
2021
2022 static Eina_Bool __stt_notify_result(void *data)
2023 {
2024         stt_client_s* client = (stt_client_s*)data;
2025
2026         /* check handle */
2027         if (NULL == client) {
2028                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
2029                 return EINA_FALSE;
2030         }
2031
2032         if (NULL == stt_client_get_by_uid(client->uid)) {
2033                 return EINA_FALSE;
2034         }
2035
2036         if (NULL != client->recognition_result_cb) {
2037                 stt_client_use_callback(client);
2038                 client->recognition_result_cb(client->stt, client->event, (const char**)client->data_list, client->data_count,
2039                         client->msg, client->recognition_result_user_data);
2040                 stt_client_not_use_callback(client);
2041                 SLOG(LOG_DEBUG, TAG_STTC, "client recognition result callback called");
2042         } else {
2043                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
2044         }
2045
2046         if (NULL != client->msg) {
2047                 free(client->msg);
2048                 client->msg = NULL;
2049         }
2050
2051         if (NULL != client->data_list) {
2052                 char **temp = NULL;
2053                 temp = client->data_list;
2054
2055                 int i = 0;
2056                 for (i = 0; i < client->data_count; i++) {
2057                         if (NULL != temp[i]) {
2058                                 free(temp[i]);
2059                                 temp[i] = NULL;
2060                         } else {
2061                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
2062                         }
2063                 }
2064                 free(client->data_list);
2065                 client->data_list = NULL;
2066         }
2067
2068         client->data_count = 0;
2069
2070         stt_config_mgr_remove_time_info_file();
2071
2072         if (STT_RESULT_EVENT_FINAL_RESULT == client->event || STT_RESULT_EVENT_ERROR == client->event) {
2073                 client->before_state = client->current_state;
2074                 client->current_state = STT_STATE_READY;
2075
2076                 if (NULL != client->state_changed_cb) {
2077                         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2078                 } else {
2079                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
2080                 }
2081         }
2082
2083         return EINA_FALSE;
2084 }
2085
2086 int __stt_cb_result(int uid, int event, char** data, int data_count, const char* msg)
2087 {
2088         stt_client_s* client = NULL;
2089
2090         client = stt_client_get_by_uid(uid);
2091         if (NULL == client) {
2092                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
2093                 return STT_ERROR_INVALID_PARAMETER;
2094         }
2095
2096         if (NULL != msg)
2097                 SECURE_SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result Message = %s", msg);
2098
2099         int i = 0;
2100         for (i = 0; i < data_count; i++) {
2101                 if (NULL != data[i])
2102                         SECURE_SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
2103         }
2104
2105         if (NULL != client->recognition_result_cb) {
2106                 client->event = event;
2107                 if (NULL != msg) {
2108                         client->msg = strdup(msg);
2109                 }
2110
2111                 client->data_count = data_count;
2112
2113                 if (data_count > 0) {
2114                         char **temp = NULL;
2115                         temp = (char**)calloc(data_count, sizeof(char*));
2116                         if (NULL == temp) {
2117                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
2118                                 return STT_ERROR_OUT_OF_MEMORY;
2119                         }
2120
2121                         for (i = 0; i < data_count; i++) {
2122                                 if (NULL != data[i])
2123                                         temp[i] = strdup(data[i]);
2124                                 else
2125                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
2126                         }
2127
2128                         client->data_list = temp;
2129                 }
2130         } else {
2131                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] User result callback is null");
2132         }
2133
2134         __stt_notify_result(client);
2135
2136         return STT_ERROR_NONE;
2137 }
2138
2139 int __stt_cb_set_state(int uid, int state)
2140 {
2141         stt_client_s* client = stt_client_get_by_uid(uid);
2142         if (NULL == client) {
2143                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
2144                 return -1;
2145         }
2146
2147         stt_state_e state_from_daemon = (stt_state_e)state;
2148
2149         if (client->current_state == state_from_daemon) {
2150                 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
2151                 return 0;
2152         }
2153
2154         client->before_state = client->current_state;
2155         client->current_state = state_from_daemon;
2156
2157         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
2158         return 0;
2159 }
2160
2161 static void __stt_notify_speech_status(void *data)
2162 {
2163         stt_client_s* client = (stt_client_s*)data;
2164
2165         /* check handle */
2166         if (NULL == client) {
2167                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify speech status : A handle is not valid");
2168                 return;
2169         }
2170
2171         if (NULL == stt_client_get_by_uid(client->uid)) {
2172                 return;
2173         }
2174
2175         if (NULL != client->speech_status_cb) {
2176                 stt_client_use_callback(client);
2177                 client->speech_status_cb(client->stt, client->speech_status, client->speech_status_user_data);
2178                 stt_client_not_use_callback(client);
2179                 SLOG(LOG_DEBUG, TAG_STTC, "Speech status callback is called");
2180         } else {
2181                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Speech status callback is null");
2182         }
2183
2184         return;
2185 }
2186
2187 int __stt_cb_speech_status(int uid, int status)
2188 {
2189         stt_client_s* client = stt_client_get_by_uid(uid);
2190         if (NULL == client) {
2191                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
2192                 return -1;
2193         }
2194
2195         client->speech_status = status;
2196
2197         ecore_main_loop_thread_safe_call_async(__stt_notify_speech_status, client);
2198         return 0;
2199 }
2200
2201 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
2202 {
2203         stt_client_s* client = NULL;
2204         if (0 != __stt_get_feature_enabled()) {
2205                 return STT_ERROR_NOT_SUPPORTED;
2206         }
2207         if (0 != __stt_check_privilege()) {
2208                 return STT_ERROR_PERMISSION_DENIED;
2209         }
2210         if (0 != __stt_check_handle(stt, &client)) {
2211                 return STT_ERROR_INVALID_PARAMETER;
2212         }
2213
2214         if (callback == NULL)
2215                 return STT_ERROR_INVALID_PARAMETER;
2216
2217         if (STT_STATE_CREATED != client->current_state) {
2218                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2219                 return STT_ERROR_INVALID_STATE;
2220         }
2221
2222         client->recognition_result_cb = callback;
2223         client->recognition_result_user_data = user_data;
2224
2225         return 0;
2226 }
2227
2228 int stt_unset_recognition_result_cb(stt_h stt)
2229 {
2230         stt_client_s* client = NULL;
2231         if (0 != __stt_get_feature_enabled()) {
2232                 return STT_ERROR_NOT_SUPPORTED;
2233         }
2234         if (0 != __stt_check_privilege()) {
2235                 return STT_ERROR_PERMISSION_DENIED;
2236         }
2237         if (0 != __stt_check_handle(stt, &client)) {
2238                 return STT_ERROR_INVALID_PARAMETER;
2239         }
2240
2241         if (STT_STATE_CREATED != client->current_state) {
2242                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2243                 return STT_ERROR_INVALID_STATE;
2244         }
2245
2246         client->recognition_result_cb = NULL;
2247         client->recognition_result_user_data = NULL;
2248
2249         return 0;
2250 }
2251
2252 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
2253 {
2254         stt_client_s* client = NULL;
2255         if (0 != __stt_get_feature_enabled()) {
2256                 return STT_ERROR_NOT_SUPPORTED;
2257         }
2258         if (0 != __stt_check_privilege()) {
2259                 return STT_ERROR_PERMISSION_DENIED;
2260         }
2261         if (0 != __stt_check_handle(stt, &client)) {
2262                 return STT_ERROR_INVALID_PARAMETER;
2263         }
2264
2265         if (NULL == callback)
2266                 return STT_ERROR_INVALID_PARAMETER;
2267
2268         if (STT_STATE_CREATED != client->current_state) {
2269                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2270                 return STT_ERROR_INVALID_STATE;
2271         }
2272
2273         client->state_changed_cb = callback;
2274         client->state_changed_user_data = user_data;
2275
2276         return 0;
2277 }
2278
2279 int stt_unset_state_changed_cb(stt_h stt)
2280 {
2281         stt_client_s* client = NULL;
2282         if (0 != __stt_get_feature_enabled()) {
2283                 return STT_ERROR_NOT_SUPPORTED;
2284         }
2285         if (0 != __stt_check_privilege()) {
2286                 return STT_ERROR_PERMISSION_DENIED;
2287         }
2288         if (0 != __stt_check_handle(stt, &client)) {
2289                 return STT_ERROR_INVALID_PARAMETER;
2290         }
2291
2292         if (STT_STATE_CREATED != client->current_state) {
2293                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2294                 return STT_ERROR_INVALID_STATE;
2295         }
2296
2297         client->state_changed_cb = NULL;
2298         client->state_changed_user_data = NULL;
2299
2300         return 0;
2301 }
2302
2303 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
2304 {
2305         stt_client_s* client = NULL;
2306         if (0 != __stt_get_feature_enabled()) {
2307                 return STT_ERROR_NOT_SUPPORTED;
2308         }
2309         if (0 != __stt_check_privilege()) {
2310                 return STT_ERROR_PERMISSION_DENIED;
2311         }
2312         if (0 != __stt_check_handle(stt, &client)) {
2313                 return STT_ERROR_INVALID_PARAMETER;
2314         }
2315
2316         if (NULL == callback)
2317                 return STT_ERROR_INVALID_PARAMETER;
2318
2319         if (STT_STATE_CREATED != client->current_state) {
2320                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2321                 return STT_ERROR_INVALID_STATE;
2322         }
2323
2324         client->error_cb = callback;
2325         client->error_user_data = user_data;
2326
2327         return 0;
2328 }
2329
2330 int stt_unset_error_cb(stt_h stt)
2331 {
2332         stt_client_s* client = NULL;
2333         if (0 != __stt_get_feature_enabled()) {
2334                 return STT_ERROR_NOT_SUPPORTED;
2335         }
2336         if (0 != __stt_check_privilege()) {
2337                 return STT_ERROR_PERMISSION_DENIED;
2338         }
2339         if (0 != __stt_check_handle(stt, &client)) {
2340                 return STT_ERROR_INVALID_PARAMETER;
2341         }
2342
2343         if (STT_STATE_CREATED != client->current_state) {
2344                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2345                 return STT_ERROR_INVALID_STATE;
2346         }
2347
2348         client->error_cb = NULL;
2349         client->error_user_data = NULL;
2350
2351         return 0;
2352 }
2353
2354 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2355 {
2356         stt_client_s* client = NULL;
2357         if (0 != __stt_get_feature_enabled()) {
2358                 return STT_ERROR_NOT_SUPPORTED;
2359         }
2360         if (0 != __stt_check_privilege()) {
2361                 return STT_ERROR_PERMISSION_DENIED;
2362         }
2363         if (0 != __stt_check_handle(stt, &client)) {
2364                 return STT_ERROR_INVALID_PARAMETER;
2365         }
2366
2367         if (NULL == callback)
2368                 return STT_ERROR_INVALID_PARAMETER;
2369
2370         if (STT_STATE_CREATED != client->current_state) {
2371                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2372                 return STT_ERROR_INVALID_STATE;
2373         }
2374
2375         client->default_lang_changed_cb = callback;
2376         client->default_lang_changed_user_data = user_data;
2377
2378         return 0;
2379 }
2380
2381 int stt_unset_default_language_changed_cb(stt_h stt)
2382 {
2383         stt_client_s* client = NULL;
2384         if (0 != __stt_get_feature_enabled()) {
2385                 return STT_ERROR_NOT_SUPPORTED;
2386         }
2387         if (0 != __stt_check_privilege()) {
2388                 return STT_ERROR_PERMISSION_DENIED;
2389         }
2390         if (0 != __stt_check_handle(stt, &client)) {
2391                 return STT_ERROR_INVALID_PARAMETER;
2392         }
2393
2394         if (STT_STATE_CREATED != client->current_state) {
2395                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2396                 return STT_ERROR_INVALID_STATE;
2397         }
2398
2399         client->default_lang_changed_cb = NULL;
2400         client->default_lang_changed_user_data = NULL;
2401
2402         return 0;
2403 }
2404
2405 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2406 {
2407         stt_client_s* client = NULL;
2408         if (0 != __stt_get_feature_enabled()) {
2409                 return STT_ERROR_NOT_SUPPORTED;
2410         }
2411         if (0 != __stt_check_privilege()) {
2412                 return STT_ERROR_PERMISSION_DENIED;
2413         }
2414         if (0 != __stt_check_handle(stt, &client)) {
2415                 return STT_ERROR_INVALID_PARAMETER;
2416         }
2417
2418         if (NULL == callback)
2419                 return STT_ERROR_INVALID_PARAMETER;
2420
2421         if (STT_STATE_CREATED != client->current_state) {
2422                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2423                 return STT_ERROR_INVALID_STATE;
2424         }
2425
2426         client->engine_changed_cb = callback;
2427         client->engine_changed_user_data = user_data;
2428
2429         return 0;
2430 }
2431
2432 int stt_unset_engine_changed_cb(stt_h stt)
2433 {
2434         stt_client_s* client = NULL;
2435         if (0 != __stt_get_feature_enabled()) {
2436                 return STT_ERROR_NOT_SUPPORTED;
2437         }
2438         if (0 != __stt_check_privilege()) {
2439                 return STT_ERROR_PERMISSION_DENIED;
2440         }
2441         if (0 != __stt_check_handle(stt, &client)) {
2442                 return STT_ERROR_INVALID_PARAMETER;
2443         }
2444
2445         if (STT_STATE_CREATED != client->current_state) {
2446                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2447                 return STT_ERROR_INVALID_STATE;
2448         }
2449
2450         client->engine_changed_cb = NULL;
2451         client->engine_changed_user_data = NULL;
2452
2453         return 0;
2454 }
2455
2456 int stt_set_speech_status_cb(stt_h stt, stt_speech_status_cb callback, void* user_data)
2457 {
2458         stt_client_s* client = NULL;
2459         if (0 != __stt_get_feature_enabled()) {
2460                 return STT_ERROR_NOT_SUPPORTED;
2461         }
2462         if (0 != __stt_check_privilege()) {
2463                 return STT_ERROR_PERMISSION_DENIED;
2464         }
2465         if (0 != __stt_check_handle(stt, &client)) {
2466                 return STT_ERROR_INVALID_PARAMETER;
2467         }
2468
2469         if (NULL == callback)
2470                 return STT_ERROR_INVALID_PARAMETER;
2471
2472         if (STT_STATE_CREATED != client->current_state) {
2473                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2474                 return STT_ERROR_INVALID_STATE;
2475         }
2476
2477         client->speech_status_cb = callback;
2478         client->speech_status_user_data = user_data;
2479
2480         return 0;
2481 }
2482
2483 int stt_unset_speech_status_cb(stt_h stt)
2484 {
2485         stt_client_s* client = NULL;
2486         if (0 != __stt_get_feature_enabled()) {
2487                 return STT_ERROR_NOT_SUPPORTED;
2488         }
2489         if (0 != __stt_check_privilege()) {
2490                 return STT_ERROR_PERMISSION_DENIED;
2491         }
2492         if (0 != __stt_check_handle(stt, &client)) {
2493                 return STT_ERROR_INVALID_PARAMETER;
2494         }
2495
2496         if (STT_STATE_CREATED != client->current_state) {
2497                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2498                 return STT_ERROR_INVALID_STATE;
2499         }
2500
2501         client->speech_status_cb = NULL;
2502         client->speech_status_user_data = NULL;
2503
2504         return 0;
2505 }
2506
2507 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)
2508 {
2509         stt_client_s* client = NULL;
2510         if (0 != __stt_get_feature_enabled()) {
2511                 return STT_ERROR_NOT_SUPPORTED;
2512         }
2513         if (0 != __stt_check_privilege()) {
2514                 return STT_ERROR_PERMISSION_DENIED;
2515         }
2516         if (0 != __stt_check_handle(stt, &client)) {
2517                 return STT_ERROR_INVALID_PARAMETER;
2518         }
2519         if (NULL == filepath) {
2520                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
2521                 return STT_ERROR_INVALID_PARAMETER;
2522         }
2523
2524         SLOG(LOG_DEBUG, TAG_STTC, "===== STT START FILE");
2525
2526         /* check state */
2527         if (client->current_state != STT_STATE_READY) {
2528                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
2529                 return STT_ERROR_INVALID_STATE;
2530         }
2531
2532         if (STT_INTERNAL_STATE_NONE != client->internal_state) {
2533                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
2534                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2535         }
2536
2537         int ret = -1;
2538         char appid[1024] = {0, };
2539         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
2540
2541         if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
2542                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
2543         } else {
2544                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
2545         }
2546
2547         char* temp = NULL;
2548         if (NULL == language) {
2549                 temp = strdup("default");
2550         } else {
2551                 temp = strdup(language);
2552         }
2553
2554         if (NULL == temp) {
2555                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
2556                 return STT_ERROR_OUT_OF_MEMORY;
2557         }
2558
2559         if (true == client->credential_needed && NULL == client->credential) {
2560                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
2561                 free(temp);
2562                 temp = NULL;
2563                 return STT_ERROR_PERMISSION_DENIED;
2564         }
2565
2566         client->internal_state = STT_INTERNAL_STATE_STARTING;
2567         ret = stt_dbus_request_start_file(client->uid, temp, type, client->silence, appid, client->credential, filepath, audio_type, sample_rate);
2568         if (0 != ret) {
2569                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start file : %s", __stt_get_error_code(ret));
2570                 client->internal_state = STT_INTERNAL_STATE_NONE;
2571         } else {
2572                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
2573         }
2574
2575         free(temp);
2576         temp = NULL;
2577
2578         SLOG(LOG_DEBUG, TAG_STTC, "=====");
2579         SLOG(LOG_DEBUG, TAG_STTC, " ");
2580
2581         return ret;
2582 }
2583
2584 int stt_cancel_file(stt_h stt)
2585 {
2586         stt_client_s* client = NULL;
2587         if (0 != __stt_get_feature_enabled()) {
2588                 return STT_ERROR_NOT_SUPPORTED;
2589         }
2590         if (0 != __stt_check_privilege()) {
2591                 return STT_ERROR_PERMISSION_DENIED;
2592         }
2593         if (0 != __stt_check_handle(stt, &client)) {
2594                 return STT_ERROR_INVALID_PARAMETER;
2595         }
2596
2597         SLOG(LOG_DEBUG, TAG_STTC, "===== STT CANCEL FILE");
2598
2599         /* check state */
2600         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
2601                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
2602                 return STT_ERROR_INVALID_STATE;
2603         }
2604
2605         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
2606                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
2607                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
2608         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
2609                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
2610                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
2611         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
2612                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
2613                 return STT_ERROR_IN_PROGRESS_TO_READY;
2614         }
2615
2616         client->internal_state = STT_INTERNAL_STATE_CANCELING;
2617         int ret = stt_dbus_request_cancel_file(client->uid);
2618         if (0 != ret) {
2619                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel file : %s", __stt_get_error_code(ret));
2620                 client->internal_state = STT_INTERNAL_STATE_NONE;
2621         } else {
2622                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Cancel file is successful but not done");
2623         }
2624
2625         SLOG(LOG_DEBUG, TAG_STTC, "=====");
2626         SLOG(LOG_DEBUG, TAG_STTC, " ");
2627
2628         return ret;
2629 }
2630