34e95cf4c3255b2eb9658efbbadc56b1af8fa50e
[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         }
536
537         SLOG(LOG_DEBUG, TAG_STTC, "===== engined_id(%s)", engine_id);
538
539         client->current_engine_id = strdup(engine_id);
540
541         SLOG(LOG_DEBUG, TAG_STTC, "=====");
542         SLOG(LOG_DEBUG, TAG_STTC, " ");
543
544         return 0;
545 }
546
547 int stt_set_credential(stt_h stt, const char* credential)
548 {
549         stt_client_s* client = NULL;
550         if (0 != __stt_get_feature_enabled()) {
551                 return STT_ERROR_NOT_SUPPORTED;
552         }
553         if (0 != __stt_check_privilege()) {
554                 return STT_ERROR_PERMISSION_DENIED;
555         }
556         if (0 != __stt_check_handle(stt, &client)) {
557                 return STT_ERROR_INVALID_PARAMETER;
558         }
559
560         SLOG(LOG_DEBUG, TAG_STTC, "===== Set credential");
561
562         if (NULL == credential) {
563                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
564                 return STT_ERROR_INVALID_PARAMETER;
565         }
566
567         /* check state */
568         if (client->current_state != STT_STATE_CREATED && client->current_state != STT_STATE_READY) {
569                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not CREATED or READY", client->current_state);
570                 return STT_ERROR_INVALID_STATE;
571         }
572
573         if (NULL != client->credential) {
574                 free(client->credential);
575                 client->credential = NULL;
576         }
577         client->credential = strdup(credential);
578
579         SLOG(LOG_DEBUG, TAG_STTC, "=====");
580         SLOG(LOG_DEBUG, TAG_STTC, " ");
581
582         return STT_ERROR_NONE;
583 }
584
585 int stt_set_private_data(stt_h stt, const char* key, const char* data)
586 {
587         stt_client_s* client = NULL;
588         if (0 != __stt_get_feature_enabled()) {
589                 return STT_ERROR_NOT_SUPPORTED;
590         }
591         if (0 != __stt_check_privilege()) {
592                 return STT_ERROR_PERMISSION_DENIED;
593         }
594         if (0 != __stt_check_handle(stt, &client)) {
595                 return STT_ERROR_INVALID_PARAMETER;
596         }
597
598         SLOG(LOG_DEBUG, TAG_STTC, "===== Set private data");
599
600         if (NULL == key || NULL == data) {
601                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
602                 return STT_ERROR_INVALID_PARAMETER;
603         }
604
605         /* check state */
606         if (STT_STATE_READY != client->current_state) {
607                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
608                 return STT_ERROR_INVALID_STATE;
609         }
610
611         int ret = -1;
612         int count = 0;
613         while (0 != ret) {
614                 ret = stt_dbus_request_set_private_data(client->uid, key, data);
615                 if (0 != ret) {
616                         if (STT_ERROR_TIMED_OUT != ret) {
617                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set private data : %s", __stt_get_error_code(ret));
618                                 return ret;
619                         } else {
620                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry : %s", __stt_get_error_code(ret));
621                                 usleep(10000);
622                                 count++;
623                                 if (STT_RETRY_COUNT == count) {
624                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
625                                         return ret;
626                                 }
627                         }
628                 }
629         }
630
631         SLOG(LOG_DEBUG, TAG_STTC, "=====");
632         SLOG(LOG_DEBUG, TAG_STTC, "");
633
634         return STT_ERROR_NONE;
635
636 }
637 int stt_get_private_data(stt_h stt, const char* key, char** data)
638 {
639         stt_client_s* client = NULL;
640         if (0 != __stt_get_feature_enabled()) {
641                 return STT_ERROR_NOT_SUPPORTED;
642         }
643         if (0 != __stt_check_privilege()) {
644                 return STT_ERROR_PERMISSION_DENIED;
645         }
646         if (0 != __stt_check_handle(stt, &client)) {
647                 return STT_ERROR_INVALID_PARAMETER;
648         }
649
650         SLOG(LOG_DEBUG, TAG_STTC, "===== Get private data");
651
652         if (NULL == key || NULL == data) {
653                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid parameter");
654                 return STT_ERROR_INVALID_PARAMETER;
655         }
656
657         /* check state */
658         if (STT_STATE_READY != client->current_state) {
659                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
660                 return STT_ERROR_INVALID_STATE;
661         }
662
663         int ret = -1;
664         int count = 0;
665         while (0 != ret) {
666                 ret = stt_dbus_request_get_private_data(client->uid, key, data);
667                 if (0 != ret) {
668                         if (STT_ERROR_TIMED_OUT != ret) {
669                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get private data : %s", __stt_get_error_code(ret));
670                                 return ret;
671                         } else {
672                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry : %s", __stt_get_error_code(ret));
673                                 usleep(10000);
674                                 count++;
675                                 if (STT_RETRY_COUNT == count) {
676                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
677                                         return ret;
678                                 }
679                         }
680                 }
681         }
682
683         if (0 == strncmp(*data, "NULL", strlen(*data))) {
684                 free(*data);
685                 *data = NULL;
686         }
687
688         SLOG(LOG_DEBUG, TAG_STTC, "=====");
689         SLOG(LOG_DEBUG, TAG_STTC, "");
690
691         return STT_ERROR_NONE;
692 }
693 static Eina_Bool __stt_connect_daemon(void *data)
694 {
695         stt_client_s* client = (stt_client_s*)data;
696
697         if (NULL == client) {
698                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available");
699                 g_connect_timer = NULL;
700                 return EINA_FALSE;
701         }
702
703         /* Send hello */
704         int ret = -1;
705         ret = stt_dbus_request_hello();
706
707         if (0 != ret) {
708                 if (STT_ERROR_INVALID_STATE == ret) {
709                         g_connect_timer = NULL;
710                         return EINA_FALSE;
711                 }
712                 return EINA_TRUE;
713         }
714
715         g_connect_timer = NULL;
716         SLOG(LOG_DEBUG, TAG_STTC, "===== Connect stt-service");
717
718         /* request initialization */
719         bool silence_supported = false;
720         bool credential_needed = false;
721
722         ret = stt_dbus_request_initialize(client->uid, &silence_supported, &credential_needed);
723
724         if (STT_ERROR_ENGINE_NOT_FOUND == ret) {
725                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to initialize : %s", __stt_get_error_code(ret));
726
727                 client->reason = STT_ERROR_ENGINE_NOT_FOUND;
728                 ecore_timer_add(0, __stt_notify_error, (void*)client);
729
730                 return EINA_FALSE;
731
732         } else if (STT_ERROR_NONE != ret) {
733                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] Fail to connection. Retry to connect");
734                 return EINA_TRUE;
735         } else {
736                 /* success to connect stt-service */
737                 client->silence_supported = silence_supported;
738                 client->credential_needed = credential_needed;
739                 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
740         }
741
742         if (NULL != client->current_engine_id) {
743                 ret = -1;
744                 int count = 0;
745                 silence_supported = false;
746                 credential_needed = false;
747                 SLOG(LOG_DEBUG, TAG_STTC, "[WARNING] current_engine_id(%s)", client->current_engine_id);
748
749                 while (0 != ret) {
750                         ret = stt_dbus_request_set_current_engine(client->uid, client->current_engine_id, &silence_supported, &credential_needed);
751                         if (0 != ret) {
752                                 if (STT_ERROR_TIMED_OUT != ret) {
753                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set current engine : %s", __stt_get_error_code(ret));
754                                         return ret;
755                                 } else {
756                                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
757                                         usleep(10000);
758                                         count++;
759                                         if (STT_RETRY_COUNT == count) {
760                                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
761                                                 return ret;
762                                         }
763                                 }
764                         } else {
765                                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current engine uuid = %s", client->current_engine_id);
766
767                                 /* success to change engine */
768                                 client->silence_supported = silence_supported;
769                                 SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
770                         }
771                 }
772         }
773
774         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] uid(%d)", client->uid);
775
776         client->before_state = client->current_state;
777         client->current_state = STT_STATE_READY;
778
779         if (NULL != client->state_changed_cb) {
780                 stt_client_use_callback(client);
781                 client->state_changed_cb(client->stt, client->before_state,
782                         client->current_state, client->state_changed_user_data);
783                 stt_client_not_use_callback(client);
784                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
785         } else {
786                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
787         }
788
789         SLOG(LOG_DEBUG, TAG_STTC, "=====");
790         SLOG(LOG_DEBUG, TAG_STTC, "  ");
791
792         return EINA_FALSE;
793 }
794
795 int stt_prepare(stt_h stt)
796 {
797         stt_client_s* client = NULL;
798         if (0 != __stt_get_feature_enabled()) {
799                 return STT_ERROR_NOT_SUPPORTED;
800         }
801         if (0 != __stt_check_privilege()) {
802                 return STT_ERROR_PERMISSION_DENIED;
803         }
804         if (0 != __stt_check_handle(stt, &client)) {
805                 return STT_ERROR_INVALID_PARAMETER;
806         }
807
808         /* check state */
809         if (client->current_state != STT_STATE_CREATED) {
810                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not 'CREATED'", client->current_state);
811                 return STT_ERROR_INVALID_STATE;
812         }
813
814         g_connect_timer = ecore_timer_add(0, __stt_connect_daemon, (void*)client);
815
816         SLOG(LOG_DEBUG, TAG_STTC, "=====");
817         SLOG(LOG_DEBUG, TAG_STTC, " ");
818
819         return STT_ERROR_NONE;
820 }
821
822 int stt_unprepare(stt_h stt)
823 {
824         stt_client_s* client = NULL;
825         if (0 != __stt_get_feature_enabled()) {
826                 return STT_ERROR_NOT_SUPPORTED;
827         }
828         if (0 != __stt_check_privilege()) {
829                 return STT_ERROR_PERMISSION_DENIED;
830         }
831         if (0 != __stt_check_handle(stt, &client)) {
832                 return STT_ERROR_INVALID_PARAMETER;
833         }
834
835         SLOG(LOG_DEBUG, TAG_STTC, "===== Unprepare STT");
836
837         /* check state */
838         if (client->current_state != STT_STATE_READY) {
839                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not 'READY'", client->current_state);
840                 return STT_ERROR_INVALID_STATE;
841         }
842
843         int ret = -1;
844         int count = 0;
845         while (0 != ret) {
846                 ret = stt_dbus_request_finalize(client->uid);
847                 if (0 != ret) {
848                         if (STT_ERROR_TIMED_OUT != ret) {
849                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request finalize : %s", __stt_get_error_code(ret));
850                                 break;
851                         } else {
852                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
853                                 usleep(10000);
854                                 count++;
855                                 if (STT_RETRY_COUNT == count) {
856                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
857                                         break;
858                                 }
859                         }
860                 }
861         }
862
863         client->internal_state = STT_INTERNAL_STATE_NONE;
864
865         client->before_state = client->current_state;
866         client->current_state = STT_STATE_CREATED;
867
868         if (NULL != client->state_changed_cb) {
869                 stt_client_use_callback(client);
870                 client->state_changed_cb(client->stt, client->before_state,
871                         client->current_state, client->state_changed_user_data);
872                 stt_client_not_use_callback(client);
873         } else {
874                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
875         }
876
877         if (g_connect_timer) {
878                 ecore_timer_del(g_connect_timer);
879                 g_connect_timer = NULL;
880         }
881
882         SLOG(LOG_DEBUG, TAG_STTC, "=====");
883         SLOG(LOG_DEBUG, TAG_STTC, " ");
884
885         return STT_ERROR_NONE;
886 }
887
888 bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
889 {
890         stt_h stt = (stt_h)user_data;
891
892         stt_client_s* client = stt_client_get(stt);
893         if (NULL == client) {
894                 SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
895                 return false;
896         }
897
898         /* call callback function */
899         if (NULL != client->supported_lang_cb) {
900                 return client->supported_lang_cb(stt, language, client->supported_lang_user_data);
901         } else {
902                 SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported languages");
903         }
904
905         return false;
906 }
907
908 int stt_foreach_supported_languages(stt_h stt, stt_supported_language_cb callback, void* user_data)
909 {
910         stt_client_s* client = NULL;
911         if (0 != __stt_get_feature_enabled()) {
912                 return STT_ERROR_NOT_SUPPORTED;
913         }
914         if (0 != __stt_check_privilege()) {
915                 return STT_ERROR_PERMISSION_DENIED;
916         }
917         if (0 != __stt_check_handle(stt, &client)) {
918                 return STT_ERROR_INVALID_PARAMETER;
919         }
920
921         SLOG(LOG_DEBUG, TAG_STTC, "===== Foreach Supported Language");
922
923         if (NULL == callback) {
924                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
925                 return STT_ERROR_INVALID_PARAMETER;
926         }
927
928         int ret;
929         char* current_engine_id = NULL;
930
931         if (NULL == client->current_engine_id) {
932                 ret = stt_config_mgr_get_engine(&current_engine_id);
933                 ret = __stt_convert_config_error_code(ret);
934                 if (0 != ret) {
935                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default engine id : %s", __stt_get_error_code(ret));
936                         return ret;
937                 }
938         } else {
939                 current_engine_id = strdup(client->current_engine_id);
940                 if (NULL == current_engine_id) {
941                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
942                         return STT_ERROR_OUT_OF_MEMORY;
943                 }
944         }
945
946         client->supported_lang_cb = callback;
947         client->supported_lang_user_data = user_data;
948
949         ret = stt_config_mgr_get_language_list(current_engine_id, __stt_config_supported_language_cb, client->stt);
950         ret = __stt_convert_config_error_code(ret);
951         if (0 != ret) {
952                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get languages : %s", __stt_get_error_code(ret));
953         }
954
955         if (NULL != current_engine_id) {
956                 free(current_engine_id);
957         }
958
959         client->supported_lang_cb = NULL;
960         client->supported_lang_user_data = NULL;
961
962         SLOG(LOG_DEBUG, TAG_STTC, "=====");
963         SLOG(LOG_DEBUG, TAG_STTC, " ");
964
965         return ret;
966 }
967
968 int stt_get_default_language(stt_h stt, char** language)
969 {
970         stt_client_s* client = NULL;
971         if (0 != __stt_get_feature_enabled()) {
972                 return STT_ERROR_NOT_SUPPORTED;
973         }
974         if (0 != __stt_check_privilege()) {
975                 return STT_ERROR_PERMISSION_DENIED;
976         }
977         if (0 != __stt_check_handle(stt, &client)) {
978                 return STT_ERROR_INVALID_PARAMETER;
979         }
980
981         SLOG(LOG_DEBUG, TAG_STTC, "===== Get Default Language");
982
983         if (NULL == language) {
984                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
985                 return STT_ERROR_INVALID_PARAMETER;
986         }
987
988         int ret = 0;
989         ret = stt_config_mgr_get_default_language(language);
990         ret = __stt_convert_config_error_code(ret);
991         if (0 != ret) {
992                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get default language : %s", __stt_get_error_code(ret));
993         } else {
994                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Current language = %s", *language);
995         }
996
997         SLOG(LOG_DEBUG, TAG_STTC, "=====");
998         SLOG(LOG_DEBUG, TAG_STTC, " ");
999
1000         return ret;
1001 }
1002
1003 int stt_get_state(stt_h stt, stt_state_e* state)
1004 {
1005         stt_client_s* client = NULL;
1006         if (0 != __stt_get_feature_enabled()) {
1007                 return STT_ERROR_NOT_SUPPORTED;
1008         }
1009         if (0 != __stt_check_privilege()) {
1010                 return STT_ERROR_PERMISSION_DENIED;
1011         }
1012         if (0 != __stt_check_handle(stt, &client)) {
1013                 return STT_ERROR_INVALID_PARAMETER;
1014         }
1015
1016         if (NULL == state) {
1017                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1018                 return STT_ERROR_INVALID_PARAMETER;
1019         }
1020
1021         *state = client->current_state;
1022
1023         switch (*state) {
1024         case STT_STATE_CREATED:         SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'CREATED'");        break;
1025         case STT_STATE_READY:           SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Ready'");          break;
1026         case STT_STATE_RECORDING:       SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Recording'");      break;
1027         case STT_STATE_PROCESSING:      SLOG(LOG_DEBUG, TAG_STTC, "Current state is 'Processing'");     break;
1028         default:                        SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid value");             break;
1029         }
1030
1031         return STT_ERROR_NONE;
1032 }
1033
1034 int stt_get_error_message(stt_h stt, char** err_msg)
1035 {
1036         stt_client_s* client = NULL;
1037         if (0 != __stt_get_feature_enabled()) {
1038                 return STT_ERROR_NOT_SUPPORTED;
1039         }
1040         if (0 != __stt_check_privilege()) {
1041                 return STT_ERROR_PERMISSION_DENIED;
1042         }
1043         if (0 != __stt_check_handle(stt, &client)) {
1044                 return STT_ERROR_INVALID_PARAMETER;
1045         }
1046
1047         if (NULL == err_msg) {
1048                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1049                 return STT_ERROR_INVALID_PARAMETER;
1050         }
1051
1052         if (false == g_err_callback_status) {
1053                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] This callback should be called during an err_callback");
1054                 return STT_ERROR_OPERATION_FAILED;
1055         }
1056
1057         if (NULL != client->err_msg) {
1058                 *err_msg = strdup(client->err_msg);
1059                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Error msg (%s)", *err_msg);
1060         } else {
1061                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Error msg (NULL)");
1062         }
1063
1064         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1065         SLOG(LOG_DEBUG, TAG_STTC, " ");
1066
1067         return STT_ERROR_NONE;
1068 }
1069
1070 int stt_is_recognition_type_supported(stt_h stt, const char* type, bool* support)
1071 {
1072         stt_client_s* client = NULL;
1073         if (0 != __stt_get_feature_enabled()) {
1074                 return STT_ERROR_NOT_SUPPORTED;
1075         }
1076         if (0 != __stt_check_privilege()) {
1077                 return STT_ERROR_PERMISSION_DENIED;
1078         }
1079         if (0 != __stt_check_handle(stt, &client)) {
1080                 return STT_ERROR_INVALID_PARAMETER;
1081         }
1082
1083         if (NULL == type || NULL == support) {
1084                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1085                 return STT_ERROR_INVALID_PARAMETER;
1086         }
1087
1088         /* check state */
1089         if (client->current_state != STT_STATE_READY) {
1090                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1091                 return STT_ERROR_INVALID_STATE;
1092         }
1093
1094         int ret = -1;
1095         int count = 0;
1096         while (0 != ret) {
1097                 ret = stt_dbus_request_is_recognition_type_supported(client->uid, type, support);
1098                 if (0 != ret) {
1099                         if (STT_ERROR_TIMED_OUT != ret) {
1100                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get recognition type supported : %s", __stt_get_error_code(ret));
1101                                 return ret;
1102                         } else {
1103                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1104                                 usleep(10000);
1105                                 count++;
1106                                 if (STT_RETRY_COUNT == count) {
1107                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1108                                         return ret;
1109                                 }
1110                         }
1111                 } else {
1112                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] recognition type is %s", *support ? "true " : "false");
1113                         break;
1114                 }
1115         }
1116
1117         return STT_ERROR_NONE;
1118 }
1119
1120 int stt_set_silence_detection(stt_h stt, stt_option_silence_detection_e type)
1121 {
1122         stt_client_s* client = NULL;
1123         if (0 != __stt_get_feature_enabled()) {
1124                 return STT_ERROR_NOT_SUPPORTED;
1125         }
1126         if (0 != __stt_check_privilege()) {
1127                 return STT_ERROR_PERMISSION_DENIED;
1128         }
1129         if (0 != __stt_check_handle(stt, &client)) {
1130                 return STT_ERROR_INVALID_PARAMETER;
1131         }
1132
1133         /* check state */
1134         if (client->current_state != STT_STATE_READY) {
1135                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1136                 return STT_ERROR_INVALID_STATE;
1137         }
1138
1139         if (true == client->silence_supported) {
1140                 if (type >= STT_OPTION_SILENCE_DETECTION_FALSE && type <= STT_OPTION_SILENCE_DETECTION_AUTO) {
1141                         client->silence = type;
1142                 } else {
1143                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Type is invalid");
1144                         return STT_ERROR_INVALID_PARAMETER;
1145                 }
1146         } else {
1147                 return STT_ERROR_NOT_SUPPORTED_FEATURE;
1148         }
1149
1150         return STT_ERROR_NONE;
1151 }
1152
1153 int stt_set_start_sound(stt_h stt, const char* filename)
1154 {
1155         stt_client_s* client = NULL;
1156         if (0 != __stt_get_feature_enabled()) {
1157                 return STT_ERROR_NOT_SUPPORTED;
1158         }
1159         if (0 != __stt_check_privilege()) {
1160                 return STT_ERROR_PERMISSION_DENIED;
1161         }
1162         if (0 != __stt_check_handle(stt, &client)) {
1163                 return STT_ERROR_INVALID_PARAMETER;
1164         }
1165
1166         SLOG(LOG_DEBUG, TAG_STTC, "===== STT SET START SOUND");
1167
1168         if (NULL == filename) {
1169                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1170                 return STT_ERROR_INVALID_PARAMETER;
1171         }
1172
1173         if (0 != access(filename, F_OK)) {
1174                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist");
1175                 return STT_ERROR_INVALID_PARAMETER;
1176         }
1177
1178         /* check state */
1179         if (client->current_state != STT_STATE_READY) {
1180                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1181                 return STT_ERROR_INVALID_STATE;
1182         }
1183
1184         int ret = -1;
1185         int count = 0;
1186         while (0 != ret) {
1187                 ret = stt_dbus_request_set_start_sound(client->uid, filename);
1188                 if (0 != ret) {
1189                         if (STT_ERROR_TIMED_OUT != ret) {
1190                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set start sound : %s", __stt_get_error_code(ret));
1191                                 return ret;
1192                         } else {
1193                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1194                                 usleep(10000);
1195                                 count++;
1196                                 if (STT_RETRY_COUNT == count) {
1197                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1198                                         return ret;
1199                                 }
1200                         }
1201                 } else {
1202                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Set start sound : %s", filename);
1203                         break;
1204                 }
1205         }
1206
1207         return STT_ERROR_NONE;
1208 }
1209
1210 int stt_unset_start_sound(stt_h stt)
1211 {
1212         stt_client_s* client = NULL;
1213         if (0 != __stt_get_feature_enabled()) {
1214                 return STT_ERROR_NOT_SUPPORTED;
1215         }
1216         if (0 != __stt_check_privilege()) {
1217                 return STT_ERROR_PERMISSION_DENIED;
1218         }
1219         if (0 != __stt_check_handle(stt, &client)) {
1220                 return STT_ERROR_INVALID_PARAMETER;
1221         }
1222
1223         SLOG(LOG_DEBUG, TAG_STTC, "===== STT UNSET START SOUND");
1224
1225         /* check state */
1226         if (client->current_state != STT_STATE_READY) {
1227                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1228                 return STT_ERROR_INVALID_STATE;
1229         }
1230
1231         int ret = -1;
1232         int count = 0;
1233         while (0 != ret) {
1234                 ret = stt_dbus_request_unset_start_sound(client->uid);
1235                 if (0 != ret) {
1236                         if (STT_ERROR_TIMED_OUT != ret) {
1237                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset start sound : %s", __stt_get_error_code(ret));
1238                                 return ret;
1239                         } else {
1240                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1241                                 usleep(10000);
1242                                 count++;
1243                                 if (STT_RETRY_COUNT == count) {
1244                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1245                                         return ret;
1246                                 }
1247                         }
1248                 } else {
1249                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Unset start sound");
1250                         break;
1251                 }
1252         }
1253
1254         return STT_ERROR_NONE;
1255 }
1256
1257 int stt_set_stop_sound(stt_h stt, const char* filename)
1258 {
1259         stt_client_s* client = NULL;
1260         if (0 != __stt_get_feature_enabled()) {
1261                 return STT_ERROR_NOT_SUPPORTED;
1262         }
1263         if (0 != __stt_check_privilege()) {
1264                 return STT_ERROR_PERMISSION_DENIED;
1265         }
1266         if (0 != __stt_check_handle(stt, &client)) {
1267                 return STT_ERROR_INVALID_PARAMETER;
1268         }
1269
1270         SLOG(LOG_DEBUG, TAG_STTC, "===== STT SET STOP SOUND");
1271
1272         if (NULL == filename) {
1273                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1274                 return STT_ERROR_INVALID_PARAMETER;
1275         }
1276
1277         if (0 != access(filename, F_OK)) {
1278                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] File does not exist");
1279                 return STT_ERROR_INVALID_PARAMETER;
1280         }
1281
1282         /* check state */
1283         if (client->current_state != STT_STATE_READY) {
1284                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1285                 return STT_ERROR_INVALID_STATE;
1286         }
1287
1288         int ret = -1;
1289         int count = 0;
1290         while (0 != ret) {
1291                 ret = stt_dbus_request_set_stop_sound(client->uid, filename);
1292                 if (0 != ret) {
1293                         if (STT_ERROR_TIMED_OUT != ret) {
1294                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to set stop sound : %s", __stt_get_error_code(ret));
1295                                 return ret;
1296                         } else {
1297                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1298                                 usleep(10000);
1299                                 count++;
1300                                 if (STT_RETRY_COUNT == count) {
1301                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1302                                         return ret;
1303                                 }
1304                         }
1305                 } else {
1306                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Set stop sound : %s", filename);
1307                         break;
1308                 }
1309         }
1310
1311         return STT_ERROR_NONE;
1312 }
1313
1314 int stt_unset_stop_sound(stt_h stt)
1315 {
1316         stt_client_s* client = NULL;
1317         if (0 != __stt_get_feature_enabled()) {
1318                 return STT_ERROR_NOT_SUPPORTED;
1319         }
1320         if (0 != __stt_check_privilege()) {
1321                 return STT_ERROR_PERMISSION_DENIED;
1322         }
1323         if (0 != __stt_check_handle(stt, &client)) {
1324                 return STT_ERROR_INVALID_PARAMETER;
1325         }
1326
1327         SLOG(LOG_DEBUG, TAG_STTC, "===== STT UNSET STOP SOUND");
1328
1329         /* check state */
1330         if (client->current_state != STT_STATE_READY) {
1331                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1332                 return STT_ERROR_INVALID_STATE;
1333         }
1334
1335         int ret = -1;
1336         int count = 0;
1337         while (0 != ret) {
1338                 ret = stt_dbus_request_unset_stop_sound(client->uid);
1339                 if (0 != ret) {
1340                         if (STT_ERROR_TIMED_OUT != ret) {
1341                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to unset stop sound : %s", __stt_get_error_code(ret));
1342                                 return ret;
1343                         } else {
1344                                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] retry");
1345                                 usleep(10000);
1346                                 count++;
1347                                 if (STT_RETRY_COUNT == count) {
1348                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to request");
1349                                         return ret;
1350                                 }
1351                         }
1352                 } else {
1353                         SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Unset stop sound");
1354                         break;
1355                 }
1356         }
1357
1358         return STT_ERROR_NONE;
1359 }
1360
1361 int stt_start(stt_h stt, const char* language, const char* type)
1362 {
1363         stt_client_s* client = NULL;
1364         if (0 != __stt_get_feature_enabled()) {
1365                 return STT_ERROR_NOT_SUPPORTED;
1366         }
1367         if (0 != __stt_check_privilege()) {
1368                 return STT_ERROR_PERMISSION_DENIED;
1369         }
1370         if (0 != __stt_check_handle(stt, &client)) {
1371                 return STT_ERROR_INVALID_PARAMETER;
1372         }
1373
1374         SLOG(LOG_DEBUG, TAG_STTC, "===== STT START");
1375
1376         /* check state */
1377         if (client->current_state != STT_STATE_READY) {
1378                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
1379                 return STT_ERROR_INVALID_STATE;
1380         }
1381
1382         if (STT_INTERNAL_STATE_NONE != client->internal_state) {
1383                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
1384                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1385         }
1386
1387         int ret = -1;
1388         char appid[128] = {0, };
1389         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
1390
1391         if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
1392                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
1393         } else {
1394                 SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
1395         }
1396
1397         char* temp = NULL;
1398         if (NULL == language) {
1399                 temp = strdup("default");
1400         } else {
1401                 temp = strdup(language);
1402         }
1403
1404         if (true == client->credential_needed && NULL == client->credential) {
1405                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
1406                 return STT_ERROR_PERMISSION_DENIED;
1407         }
1408
1409         ret = stt_dbus_request_start(client->uid, temp, type, client->silence, appid, client->credential);
1410         if (0 != ret) {
1411                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : %s", __stt_get_error_code(ret));
1412         } else {
1413                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
1414                 client->internal_state = STT_INTERNAL_STATE_STARTING;
1415         }
1416
1417         if (NULL != temp)       free(temp);
1418
1419         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1420         SLOG(LOG_DEBUG, TAG_STTC, " ");
1421
1422         return ret;
1423 }
1424
1425 int stt_stop(stt_h stt)
1426 {
1427         stt_client_s* client = NULL;
1428         if (0 != __stt_get_feature_enabled()) {
1429                 return STT_ERROR_NOT_SUPPORTED;
1430         }
1431         if (0 != __stt_check_privilege()) {
1432                 return STT_ERROR_PERMISSION_DENIED;
1433         }
1434         if (0 != __stt_check_handle(stt, &client)) {
1435                 return STT_ERROR_INVALID_PARAMETER;
1436         }
1437
1438         SLOG(LOG_DEBUG, TAG_STTC, "===== STT STOP");
1439
1440         /* check state */
1441         if (client->current_state != STT_STATE_RECORDING) {
1442                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Current state(%d) is NOT RECORDING", client->current_state);
1443                 return STT_ERROR_INVALID_STATE;
1444         }
1445
1446         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1447                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1448                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1449         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1450                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1451                 return STT_ERROR_IN_PROGRESS_TO_READY;
1452         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1453                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1454                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1455         }
1456
1457         int ret = stt_dbus_request_stop(client->uid);
1458
1459         if (0 != ret) {
1460                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : %s", __stt_get_error_code(ret));
1461         } else {
1462                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Stop is successful but not done");
1463                 client->internal_state = STT_INTERNAL_STATE_STOPPING;
1464         }
1465
1466         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1467         SLOG(LOG_DEBUG, TAG_STTC, " ");
1468
1469         return ret;
1470 }
1471
1472
1473 int stt_cancel(stt_h stt)
1474 {
1475         stt_client_s* client = NULL;
1476         if (0 != __stt_get_feature_enabled()) {
1477                 return STT_ERROR_NOT_SUPPORTED;
1478         }
1479         if (0 != __stt_check_privilege()) {
1480                 return STT_ERROR_PERMISSION_DENIED;
1481         }
1482         if (0 != __stt_check_handle(stt, &client)) {
1483                 return STT_ERROR_INVALID_PARAMETER;
1484         }
1485
1486         SLOG(LOG_DEBUG, TAG_STTC, "===== STT CANCEL");
1487
1488         /* check state */
1489         if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
1490                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
1491                 return STT_ERROR_INVALID_STATE;
1492         }
1493
1494         if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
1495                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
1496                 return STT_ERROR_IN_PROGRESS_TO_RECORDING;
1497         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
1498                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
1499                 return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
1500         } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
1501                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
1502                 return STT_ERROR_IN_PROGRESS_TO_READY;
1503         }
1504
1505         int ret = stt_dbus_request_cancel(client->uid);
1506         if (0 != ret) {
1507                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel : %s", __stt_get_error_code(ret));
1508         } else {
1509                 SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Cancel is successful but not done");
1510                 client->internal_state = STT_INTERNAL_STATE_CANCELING;
1511         }
1512
1513         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1514         SLOG(LOG_DEBUG, TAG_STTC, " ");
1515
1516         return ret;
1517 }
1518
1519 int __stt_cb_set_volume(int uid, float volume)
1520 {
1521         stt_client_s* client = NULL;
1522
1523         client = stt_client_get_by_uid(uid);
1524         if (NULL == client) {
1525                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1526                 return STT_ERROR_INVALID_PARAMETER;
1527         }
1528
1529         if (STT_STATE_RECORDING != client->current_state) {
1530                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1531                 return STT_ERROR_INVALID_STATE;
1532         }
1533
1534         g_volume_db = volume;
1535         SLOG(LOG_DEBUG, TAG_STTC, "Set volume (%f)", g_volume_db);
1536
1537         return 0;
1538 }
1539
1540 int stt_get_recording_volume(stt_h stt, float* volume)
1541 {
1542         stt_client_s* client = NULL;
1543         if (0 != __stt_get_feature_enabled()) {
1544                 return STT_ERROR_NOT_SUPPORTED;
1545         }
1546         if (0 != __stt_check_privilege()) {
1547                 return STT_ERROR_PERMISSION_DENIED;
1548         }
1549         if (0 != __stt_check_handle(stt, &client)) {
1550                 return STT_ERROR_INVALID_PARAMETER;
1551         }
1552
1553         if (NULL == volume) {
1554                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1555                 return STT_ERROR_INVALID_PARAMETER;
1556         }
1557
1558         if (STT_STATE_RECORDING != client->current_state) {
1559                 SLOG(LOG_DEBUG, TAG_STTC, "[ERROR] Invalid state : NO 'Recording' state, cur(%d)", client->current_state);
1560                 return STT_ERROR_INVALID_STATE;
1561         }
1562
1563         *volume = g_volume_db;
1564
1565         return STT_ERROR_NONE;
1566 }
1567
1568 bool __stt_result_time_cb(int index, int event, const char* text, long start_time, long end_time, void* user_data)
1569 {
1570         stt_client_s* client = (stt_client_s*)user_data;
1571
1572         /* check handle */
1573         if (NULL == client) {
1574                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1575                 return EINA_FALSE;
1576         }
1577
1578         if (NULL != client->result_time_cb) {
1579                 SLOG(LOG_DEBUG, TAG_STTC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
1580                         index, event, text, start_time, end_time);
1581                 client->result_time_cb(client->stt, index, (stt_result_time_event_e)event,
1582                         text, start_time, end_time, client->result_time_user_data);
1583         } else {
1584                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Callback is NULL");
1585                 return false;
1586         }
1587
1588         return true;
1589 }
1590
1591 int stt_foreach_detailed_result(stt_h stt, stt_result_time_cb callback, void* user_data)
1592 {
1593         stt_client_s* client = NULL;
1594         if (0 != __stt_get_feature_enabled()) {
1595                 return STT_ERROR_NOT_SUPPORTED;
1596         }
1597         if (0 != __stt_check_privilege()) {
1598                 return STT_ERROR_PERMISSION_DENIED;
1599         }
1600         if (0 != __stt_check_handle(stt, &client)) {
1601                 return STT_ERROR_INVALID_PARAMETER;
1602         }
1603
1604         SLOG(LOG_DEBUG, TAG_STTC, "===== STT FOREACH DETAILED RESULT");
1605
1606         if (NULL == callback) {
1607                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
1608                 return STT_ERROR_INVALID_PARAMETER;
1609         }
1610
1611         client->result_time_cb = callback;
1612         client->result_time_user_data = user_data;
1613
1614         int ret = -1;
1615         ret = stt_config_mgr_foreach_time_info(__stt_result_time_cb, client);
1616         ret = __stt_convert_config_error_code(ret);
1617         if (0 != ret) {
1618                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to foreach time info : %s", __stt_get_error_code(ret));
1619         }
1620
1621         client->result_time_cb = NULL;
1622         client->result_time_user_data = NULL;
1623
1624         SLOG(LOG_DEBUG, TAG_STTC, "=====");
1625         SLOG(LOG_DEBUG, TAG_STTC, " ");
1626
1627         return ret;
1628 }
1629
1630 static Eina_Bool __stt_notify_error(void *data)
1631 {
1632         stt_client_s* client = (stt_client_s*)data;
1633
1634         SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error from sttd");
1635
1636         /* check handle */
1637         if (NULL == client) {
1638                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1639                 return EINA_FALSE;
1640         }
1641
1642         if (NULL == stt_client_get_by_uid(client->uid))
1643                 return EINA_FALSE;
1644
1645         if (NULL != client->error_cb) {
1646                 stt_client_use_callback(client);
1647                 g_err_callback_status = true;
1648                 client->error_cb(client->stt, client->reason, client->error_user_data);
1649                 g_err_callback_status = false;
1650                 stt_client_not_use_callback(client);
1651                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is called : reason [%d]", client->reason);
1652         } else {
1653                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1654         }
1655
1656         return EINA_FALSE;
1657 }
1658
1659 int __stt_cb_error(int uid, int reason, char* err_msg)
1660 {
1661         stt_client_s* client = stt_client_get_by_uid(uid);
1662         if (NULL == client) {
1663                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
1664                 return -1;
1665         }
1666
1667         client->reason = reason;
1668         client->internal_state = STT_INTERNAL_STATE_NONE;
1669         if (NULL != client->err_msg) {
1670                 free(client->err_msg);
1671                 client->err_msg = NULL;
1672         }
1673         client->err_msg = strdup(err_msg);
1674
1675         SLOG(LOG_INFO, TAG_STTC, "internal state is initialized to 0");
1676
1677         if (NULL != client->error_cb) {
1678                 ecore_timer_add(0, __stt_notify_error, client);
1679         } else {
1680                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
1681         }
1682
1683         if (STT_ERROR_SERVICE_RESET == reason) {
1684                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
1685
1686                 client->current_state = STT_STATE_CREATED;
1687                 if (0 != stt_prepare(client->stt)) {
1688                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
1689                 }
1690         }
1691
1692         return 0;
1693 }
1694
1695 static void __stt_notify_state_changed(void *data)
1696 {
1697         stt_client_s* client = (stt_client_s*)data;
1698
1699         /* check handle */
1700         if (NULL == client) {
1701                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1702                 return;
1703         }
1704
1705         if (NULL == stt_client_get_by_uid(client->uid)) {
1706                 return;
1707         }
1708
1709         if (STT_INTERNAL_STATE_STARTING == client->internal_state && STT_STATE_RECORDING == client->current_state) {
1710                 client->internal_state = STT_INTERNAL_STATE_NONE;
1711                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
1712         } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state && STT_STATE_PROCESSING == 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_CANCELING == client->internal_state && STT_STATE_READY == client->current_state) {
1716                 client->internal_state = STT_INTERNAL_STATE_NONE;
1717                 SLOG(LOG_DEBUG, TAG_STTC, "Internal state change to NONE");
1718         }
1719
1720         if (NULL != client->state_changed_cb) {
1721                 stt_client_use_callback(client);
1722                 client->state_changed_cb(client->stt, client->before_state,
1723                         client->current_state, client->state_changed_user_data);
1724                 stt_client_not_use_callback(client);
1725                 SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
1726         } else {
1727                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1728         }
1729
1730         return;
1731 }
1732
1733 static Eina_Bool __stt_notify_result(void *data)
1734 {
1735         stt_client_s* client = (stt_client_s*)data;
1736
1737         /* check handle */
1738         if (NULL == client) {
1739                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to notify error : A handle is not valid");
1740                 return EINA_FALSE;
1741         }
1742
1743         if (NULL == stt_client_get_by_uid(client->uid)) {
1744                 return EINA_FALSE;
1745         }
1746
1747         if (NULL != client->recognition_result_cb) {
1748                 stt_client_use_callback(client);
1749                 client->recognition_result_cb(client->stt, client->event, (const char**)client->data_list, client->data_count,
1750                         client->msg, client->recognition_result_user_data);
1751                 stt_client_not_use_callback(client);
1752                 SLOG(LOG_DEBUG, TAG_STTC, "client recognition result callback called");
1753         } else {
1754                 SLOG(LOG_WARN, TAG_STTC, "[WARNING] User recognition result callback is NULL");
1755         }
1756
1757         if (NULL != client->msg) {
1758                 free(client->msg);
1759                 client->msg = NULL;
1760         }
1761
1762         if (NULL != client->data_list) {
1763                 char **temp = NULL;
1764                 temp = client->data_list;
1765
1766                 int i = 0;
1767                 for (i = 0; i < client->data_count; i++) {
1768                         if (NULL != temp[i]) {
1769                                 free(temp[i]);
1770                                 temp[i] = NULL;
1771                         } else {
1772                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
1773                         }
1774                 }
1775                 free(client->data_list);
1776                 client->data_list = NULL;
1777         }
1778
1779         client->data_count = 0;
1780
1781         stt_config_mgr_remove_time_info_file();
1782
1783         if (STT_RESULT_EVENT_FINAL_RESULT == client->event || STT_RESULT_EVENT_ERROR == client->event) {
1784                 client->before_state = client->current_state;
1785                 client->current_state = STT_STATE_READY;
1786
1787                 if (NULL != client->state_changed_cb) {
1788                         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
1789                 } else {
1790                         SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
1791                 }
1792         }
1793
1794         return EINA_FALSE;
1795 }
1796
1797 int __stt_cb_result(int uid, int event, char** data, int data_count, const char* msg)
1798 {
1799         stt_client_s* client = NULL;
1800
1801         client = stt_client_get_by_uid(uid);
1802         if (NULL == client) {
1803                 SLOG(LOG_ERROR, TAG_STTC, "Handle is NOT valid");
1804                 return STT_ERROR_INVALID_PARAMETER;
1805         }
1806
1807         if (NULL != msg)
1808                 SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result Message = %s", msg);
1809
1810         int i = 0;
1811         for (i = 0; i < data_count; i++) {
1812                 if (NULL != data[i])
1813                         SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
1814         }
1815
1816         if (NULL != client->recognition_result_cb) {
1817                 client->event = event;
1818                 if (NULL != msg) {
1819                         client->msg = strdup(msg);
1820                 }
1821
1822                 client->data_count = data_count;
1823
1824                 if (data_count > 0) {
1825                         char **temp = NULL;
1826                         temp = (char**)calloc(data_count, sizeof(char*));
1827                         if (NULL == temp) {
1828                                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
1829                                 return STT_ERROR_OUT_OF_MEMORY;
1830                         }
1831
1832                         for (i = 0; i < data_count; i++) {
1833                                 if (NULL != data[i])
1834                                         temp[i] = strdup(data[i]);
1835                                 else
1836                                         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Result data is error");
1837                         }
1838
1839                         client->data_list = temp;
1840                 }
1841         } else {
1842                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] User result callback is null");
1843         }
1844
1845         ecore_timer_add(0, __stt_notify_result, client);
1846
1847         return STT_ERROR_NONE;
1848 }
1849
1850 int __stt_cb_set_state(int uid, int state)
1851 {
1852         stt_client_s* client = stt_client_get_by_uid(uid);
1853         if (NULL == client) {
1854                 SLOG(LOG_ERROR, TAG_STTC, "Handle not found");
1855                 return -1;
1856         }
1857
1858         stt_state_e state_from_daemon = (stt_state_e)state;
1859
1860         if (client->current_state == state_from_daemon) {
1861                 SLOG(LOG_DEBUG, TAG_STTC, "Current state has already been %d", client->current_state);
1862                 return 0;
1863         }
1864
1865         client->before_state = client->current_state;
1866         client->current_state = state_from_daemon;
1867
1868         ecore_main_loop_thread_safe_call_async(__stt_notify_state_changed, client);
1869         return 0;
1870 }
1871
1872 int stt_set_recognition_result_cb(stt_h stt, stt_recognition_result_cb callback, void* user_data)
1873 {
1874         stt_client_s* client = NULL;
1875         if (0 != __stt_get_feature_enabled()) {
1876                 return STT_ERROR_NOT_SUPPORTED;
1877         }
1878         if (0 != __stt_check_privilege()) {
1879                 return STT_ERROR_PERMISSION_DENIED;
1880         }
1881         if (0 != __stt_check_handle(stt, &client)) {
1882                 return STT_ERROR_INVALID_PARAMETER;
1883         }
1884
1885         if (callback == NULL)
1886                 return STT_ERROR_INVALID_PARAMETER;
1887
1888         if (STT_STATE_CREATED != client->current_state) {
1889                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1890                 return STT_ERROR_INVALID_STATE;
1891         }
1892
1893         client->recognition_result_cb = callback;
1894         client->recognition_result_user_data = user_data;
1895
1896         return 0;
1897 }
1898
1899 int stt_unset_recognition_result_cb(stt_h stt)
1900 {
1901         stt_client_s* client = NULL;
1902         if (0 != __stt_get_feature_enabled()) {
1903                 return STT_ERROR_NOT_SUPPORTED;
1904         }
1905         if (0 != __stt_check_privilege()) {
1906                 return STT_ERROR_PERMISSION_DENIED;
1907         }
1908         if (0 != __stt_check_handle(stt, &client)) {
1909                 return STT_ERROR_INVALID_PARAMETER;
1910         }
1911
1912         if (STT_STATE_CREATED != client->current_state) {
1913                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1914                 return STT_ERROR_INVALID_STATE;
1915         }
1916
1917         client->recognition_result_cb = NULL;
1918         client->recognition_result_user_data = NULL;
1919
1920         return 0;
1921 }
1922
1923 int stt_set_state_changed_cb(stt_h stt, stt_state_changed_cb callback, void* user_data)
1924 {
1925         stt_client_s* client = NULL;
1926         if (0 != __stt_get_feature_enabled()) {
1927                 return STT_ERROR_NOT_SUPPORTED;
1928         }
1929         if (0 != __stt_check_privilege()) {
1930                 return STT_ERROR_PERMISSION_DENIED;
1931         }
1932         if (0 != __stt_check_handle(stt, &client)) {
1933                 return STT_ERROR_INVALID_PARAMETER;
1934         }
1935
1936         if (NULL == callback)
1937                 return STT_ERROR_INVALID_PARAMETER;
1938
1939         if (STT_STATE_CREATED != client->current_state) {
1940                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1941                 return STT_ERROR_INVALID_STATE;
1942         }
1943
1944         client->state_changed_cb = callback;
1945         client->state_changed_user_data = user_data;
1946
1947         return 0;
1948 }
1949
1950 int stt_unset_state_changed_cb(stt_h stt)
1951 {
1952         stt_client_s* client = NULL;
1953         if (0 != __stt_get_feature_enabled()) {
1954                 return STT_ERROR_NOT_SUPPORTED;
1955         }
1956         if (0 != __stt_check_privilege()) {
1957                 return STT_ERROR_PERMISSION_DENIED;
1958         }
1959         if (0 != __stt_check_handle(stt, &client)) {
1960                 return STT_ERROR_INVALID_PARAMETER;
1961         }
1962
1963         if (STT_STATE_CREATED != client->current_state) {
1964                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1965                 return STT_ERROR_INVALID_STATE;
1966         }
1967
1968         client->state_changed_cb = NULL;
1969         client->state_changed_user_data = NULL;
1970
1971         return 0;
1972 }
1973
1974 int stt_set_error_cb(stt_h stt, stt_error_cb callback, void* user_data)
1975 {
1976         stt_client_s* client = NULL;
1977         if (0 != __stt_get_feature_enabled()) {
1978                 return STT_ERROR_NOT_SUPPORTED;
1979         }
1980         if (0 != __stt_check_privilege()) {
1981                 return STT_ERROR_PERMISSION_DENIED;
1982         }
1983         if (0 != __stt_check_handle(stt, &client)) {
1984                 return STT_ERROR_INVALID_PARAMETER;
1985         }
1986
1987         if (NULL == callback)
1988                 return STT_ERROR_INVALID_PARAMETER;
1989
1990         if (STT_STATE_CREATED != client->current_state) {
1991                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
1992                 return STT_ERROR_INVALID_STATE;
1993         }
1994
1995         client->error_cb = callback;
1996         client->error_user_data = user_data;
1997
1998         return 0;
1999 }
2000
2001 int stt_unset_error_cb(stt_h stt)
2002 {
2003         stt_client_s* client = NULL;
2004         if (0 != __stt_get_feature_enabled()) {
2005                 return STT_ERROR_NOT_SUPPORTED;
2006         }
2007         if (0 != __stt_check_privilege()) {
2008                 return STT_ERROR_PERMISSION_DENIED;
2009         }
2010         if (0 != __stt_check_handle(stt, &client)) {
2011                 return STT_ERROR_INVALID_PARAMETER;
2012         }
2013
2014         if (STT_STATE_CREATED != client->current_state) {
2015                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2016                 return STT_ERROR_INVALID_STATE;
2017         }
2018
2019         client->error_cb = NULL;
2020         client->error_user_data = NULL;
2021
2022         return 0;
2023 }
2024
2025 int stt_set_default_language_changed_cb(stt_h stt, stt_default_language_changed_cb callback, void* user_data)
2026 {
2027         stt_client_s* client = NULL;
2028         if (0 != __stt_get_feature_enabled()) {
2029                 return STT_ERROR_NOT_SUPPORTED;
2030         }
2031         if (0 != __stt_check_privilege()) {
2032                 return STT_ERROR_PERMISSION_DENIED;
2033         }
2034         if (0 != __stt_check_handle(stt, &client)) {
2035                 return STT_ERROR_INVALID_PARAMETER;
2036         }
2037
2038         if (NULL == callback)
2039                 return STT_ERROR_INVALID_PARAMETER;
2040
2041         if (STT_STATE_CREATED != client->current_state) {
2042                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2043                 return STT_ERROR_INVALID_STATE;
2044         }
2045
2046         client->default_lang_changed_cb = callback;
2047         client->default_lang_changed_user_data = user_data;
2048
2049         return 0;
2050 }
2051
2052 int stt_unset_default_language_changed_cb(stt_h stt)
2053 {
2054         stt_client_s* client = NULL;
2055         if (0 != __stt_get_feature_enabled()) {
2056                 return STT_ERROR_NOT_SUPPORTED;
2057         }
2058         if (0 != __stt_check_privilege()) {
2059                 return STT_ERROR_PERMISSION_DENIED;
2060         }
2061         if (0 != __stt_check_handle(stt, &client)) {
2062                 return STT_ERROR_INVALID_PARAMETER;
2063         }
2064
2065         if (STT_STATE_CREATED != client->current_state) {
2066                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2067                 return STT_ERROR_INVALID_STATE;
2068         }
2069
2070         client->default_lang_changed_cb = NULL;
2071         client->default_lang_changed_user_data = NULL;
2072
2073         return 0;
2074 }
2075
2076 int stt_set_engine_changed_cb(stt_h stt, stt_engine_changed_cb callback, void* user_data)
2077 {
2078         stt_client_s* client = NULL;
2079         if (0 != __stt_get_feature_enabled()) {
2080                 return STT_ERROR_NOT_SUPPORTED;
2081         }
2082         if (0 != __stt_check_privilege()) {
2083                 return STT_ERROR_PERMISSION_DENIED;
2084         }
2085         if (0 != __stt_check_handle(stt, &client)) {
2086                 return STT_ERROR_INVALID_PARAMETER;
2087         }
2088
2089         if (NULL == callback)
2090                 return STT_ERROR_INVALID_PARAMETER;
2091
2092         if (STT_STATE_CREATED != client->current_state) {
2093                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2094                 return STT_ERROR_INVALID_STATE;
2095         }
2096
2097         client->engine_changed_cb = callback;
2098         client->engine_changed_user_data = user_data;
2099
2100         return 0;
2101 }
2102
2103 int stt_unset_engine_changed_cb(stt_h stt)
2104 {
2105         stt_client_s* client = NULL;
2106         if (0 != __stt_get_feature_enabled()) {
2107                 return STT_ERROR_NOT_SUPPORTED;
2108         }
2109         if (0 != __stt_check_privilege()) {
2110                 return STT_ERROR_PERMISSION_DENIED;
2111         }
2112         if (0 != __stt_check_handle(stt, &client)) {
2113                 return STT_ERROR_INVALID_PARAMETER;
2114         }
2115
2116         if (STT_STATE_CREATED != client->current_state) {
2117                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Current state(%d) is not 'Created'", client->current_state);
2118                 return STT_ERROR_INVALID_STATE;
2119         }
2120
2121         client->engine_changed_cb = NULL;
2122         client->engine_changed_user_data = NULL;
2123
2124         return 0;
2125 }