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