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