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