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