Fix cancel by error
[platform/core/uifw/stt.git] / client / stt_file.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 <dlfcn.h>
16 #include <dirent.h>
17 #include <dlog.h>
18 #include <Ecore.h>
19 #include <glib.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "stt_config_mgr.h"
27 #include "stt_defs.h"
28 #include "stt_engine.h"
29 #include "stt_file.h"
30 #include "stt_file_client.h"
31 #include "stt_network.h"
32
33
34 typedef struct _sttengine_info {
35         int     engine_id;
36
37         char*   engine_uuid;
38         char*   engine_path;
39         char*   engine_name;
40         char*   engine_setting_path;
41         bool    use_network;
42
43         bool    is_loaded;
44 } sttengine_info_s;
45
46 static GSList*  g_engine_list;
47
48
49 #define STT_FILE_CONFIG_HANDLE  100000
50
51
52 static const char* __stt_file_get_error_code(stt_file_error_e err)
53 {
54         switch (err) {
55         case STT_FILE_ERROR_NONE:                       return "STT_FILE_ERROR_NONE";
56         case STT_FILE_ERROR_OUT_OF_MEMORY:              return "STT_FILE_ERROR_OUT_OF_MEMORY";
57         case STT_FILE_ERROR_IO_ERROR:                   return "STT_FILE_ERROR_IO_ERROR";
58         case STT_FILE_ERROR_INVALID_PARAMETER:          return "STT_FILE_ERROR_INVALID_PARAMETER";
59         case STT_FILE_ERROR_OUT_OF_NETWORK:             return "STT_FILE_ERROR_OUT_OF_NETWORK";
60         case STT_FILE_ERROR_INVALID_STATE:              return "STT_FILE_ERROR_INVALID_STATE";
61         case STT_FILE_ERROR_INVALID_LANGUAGE:           return "STT_FILE_ERROR_INVALID_LANGUAGE";
62         case STT_FILE_ERROR_ENGINE_NOT_FOUND:           return "STT_FILE_ERROR_ENGINE_NOT_FOUND";
63         case STT_FILE_ERROR_OPERATION_FAILED:           return "STT_FILE_ERROR_OPERATION_FAILED";
64         case STT_FILE_ERROR_NOT_SUPPORTED_FEATURE:      return "STT_FILE_ERROR_NOT_SUPPORTED_FEATURE";
65         case STT_FILE_ERROR_NOT_AGREE_SERVICE:          return "STT_FILE_ERROR_NOT_AGREE_SERVICE";
66         default:
67                 return "Invalid error code";
68         }
69 }
70
71 void __stt_file_engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
72                       bool use_network, void* user_data)
73 {
74         sttengine_info_s* temp = (sttengine_info_s*)user_data;
75
76         temp->engine_uuid = g_strdup(engine_uuid);
77         temp->engine_name = g_strdup(engine_name);
78         temp->engine_setting_path = g_strdup(setting_ug_name);
79         temp->use_network = use_network;
80 }
81
82 static int __stt_file_get_engine_info(const char* filepath, sttengine_info_s** info)
83 {
84 #ifdef __UNUSED_CODES__
85         if (NULL == filepath || NULL == info) {
86                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Invalid Parameter");
87                 return STT_FILE_ERROR_INVALID_PARAMETER;
88         }
89
90         /* load engine */
91         char *error;
92         void* handle;
93
94         handle = dlopen(filepath, RTLD_LAZY);
95
96         if (!handle) {
97                 SECURE_SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Invalid engine : %s", filepath);
98                 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
99         }
100
101         /* link engine to stt-service */
102         dlsym(handle, "sttp_load_engine");
103         if ((error = dlerror()) != NULL) {
104                 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Invalid engine. Fail to open sttp_load_engine : %s", error);
105                 dlclose(handle);
106                 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
107         }
108
109         dlsym(handle, "sttp_unload_engine");
110         if ((error = dlerror()) != NULL) {
111                 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Invalid engine. Fail to open sttp_unload_engine : %s", error);
112                 dlclose(handle);
113                 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
114         }
115
116         int (*get_engine_info)(sttpe_engine_info_cb callback, void* user_data);
117
118         get_engine_info = (int (*)(sttpe_engine_info_cb, void*))dlsym(handle, "sttp_get_engine_info");
119         if ((error = dlerror()) != NULL || NULL == get_engine_info) {
120                 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent WARNING] Invalid engine. Fail to open sttp_get_engine_info : %s", error);
121                 dlclose(handle);
122                 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
123         }
124
125         sttengine_info_s* temp;
126         temp = (sttengine_info_s*)calloc(1, sizeof(sttengine_info_s));
127         if (NULL == temp) {
128                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to allocate memory");
129                 dlclose(handle);
130                 return STT_FILE_ERROR_OUT_OF_MEMORY;
131         }
132
133         /* get engine info */
134         if (0 != get_engine_info(__stt_file_engine_info_cb, (void*)temp)) {
135                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to get engine info from engine");
136                 dlclose(handle);
137                 free(temp);
138                 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
139         }
140
141         /* close engine */
142         dlclose(handle);
143
144         temp->engine_id = g_engine_id_count;
145         g_engine_id_count++;
146
147         temp->engine_path = g_strdup(filepath);
148         temp->is_loaded = false;
149
150         SLOG(LOG_DEBUG, TAG_STTFC, "----- Valid Engine");
151         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine id : %d", temp->engine_id);
152         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine uuid : %s", temp->engine_uuid);
153         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine name : %s", temp->engine_name);
154         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine path : %s", temp->engine_path);
155         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Engine setting path : %s", temp->engine_setting_path);
156         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "Use network : %s", temp->use_network ? "true" : "false");
157         SLOG(LOG_DEBUG, TAG_STTFC, "-----");
158         SLOG(LOG_DEBUG, TAG_STTFC, "  ");
159
160         *info = temp;
161 #endif
162
163         return STT_FILE_ERROR_NONE;
164 }
165
166 static bool __stt_file_is_engine(const char* filepath)
167 {
168         GSList *iter = NULL;
169         sttengine_info_s *engine = NULL;
170
171         if (0 < g_slist_length(g_engine_list)) {
172                 /* Get a first item */
173                 iter = g_slist_nth(g_engine_list, 0);
174
175                 while (NULL != iter) {
176                         /* Get handle data from list */
177                         engine = iter->data;
178
179                         if (0 == strcmp(engine->engine_path, filepath)) {
180                                 return true;
181                         }
182
183                         iter = g_slist_next(iter);
184                 }
185         }
186
187         return false;
188 }
189
190 void __stt_file_relseae_engine_info(void)
191 {
192         GSList *iter = NULL;
193
194         /* Release engine list */
195         sttengine_info_s *engine = NULL;
196
197         if (0 < g_slist_length(g_engine_list)) {
198                 /* Get a first item */
199                 iter = g_slist_nth(g_engine_list, 0);
200
201                 while (NULL != iter) {
202                         /* Get handle data from list */
203                         engine = iter->data;
204                         g_engine_list = g_slist_remove_link(g_engine_list, iter);
205
206                         /* Check engine unload */
207                         if (NULL != engine) {
208                                 if (engine->is_loaded) {
209                                         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[Engine Agent] Unload engine id(%d)", engine->engine_id);
210
211                                         if (0 != stt_engine_deinitialize())
212                                                 SECURE_SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Fail to deinitialize engine id(%d)", engine->engine_id);
213
214                                         if (0 != stt_engine_unload())
215                                                 SECURE_SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent] Fail to unload engine id(%d)", engine->engine_id);
216
217                                         engine->is_loaded = false;
218                                 }
219
220                                 if (NULL != engine->engine_uuid)        free(engine->engine_uuid);
221                                 if (NULL != engine->engine_path)        free(engine->engine_path);
222                                 if (NULL != engine->engine_name)        free(engine->engine_name);
223                                 if (NULL != engine->engine_setting_path)free(engine->engine_setting_path);
224
225                                 free(engine);
226                         }
227
228                         iter = g_slist_nth(g_engine_list, 0);
229                 }
230         }
231 }
232
233 static sttengine_info_s* __stt_file_get_engine_by_id(int engine_id)
234 {
235         GSList *iter = NULL;
236         sttengine_info_s *data = NULL;
237
238         iter = g_slist_nth(g_engine_list, 0);
239
240         while (NULL != iter) {
241
242                 data = iter->data;
243
244                 if (data->engine_id == engine_id)
245                         return data;
246
247                 iter = g_slist_next(iter);
248         }
249
250         return NULL;
251 }
252
253 int __stt_file_result_cb(stte_result_event_e event, const char* type, const char** data, int data_count,
254                  const char* msg, void* time_info, void *user_data)
255 {
256
257         SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Result event(%d) type(%s) msg(%s)", event, type, msg);
258
259         if (NULL != data) {
260                 int i = 0;
261                 for (i = 0; i < data_count; i++) {
262                         if (NULL != data[i]) {
263                                 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] [%d] %s", i, data[i]);
264                         }
265                 }
266         }
267
268         stt_file_client_s* client = stt_file_client_get();
269
270         /* check handle */
271         if (NULL == client) {
272                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to notify error : A handle is not valid");
273                 return STT_FILE_ERROR_OPERATION_FAILED;
274         }
275
276         if (NULL != time_info) {
277                 client->time_info = time_info;
278         }
279
280         if (NULL != client->recognition_result_cb) {
281                 stt_file_client_use_callback(client);
282                 client->recognition_result_cb(event, data, data_count, msg, client->recognition_result_user_data);
283                 stt_file_client_not_use_callback(client);
284                 SLOG(LOG_DEBUG, TAG_STTFC, "client recognition result callback called");
285         } else {
286                 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] User recognition result callback is NULL");
287         }
288
289         client->time_info = NULL;
290
291         if (STTE_RESULT_EVENT_FINAL_RESULT == event || STTE_RESULT_EVENT_ERROR == event) {
292                 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] State change : 'Ready'");
293
294                 client->before_state = client->current_state;
295                 client->current_state = STT_FILE_STATE_READY;
296
297                 if (NULL != client->state_changed_cb) {
298                         stt_file_client_use_callback(client);
299                         client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
300                         stt_file_client_not_use_callback(client);
301                         SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
302                 } else {
303                         SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
304                 }
305         }
306
307         return STT_FILE_ERROR_NONE;
308 }
309
310 void __stt_file_speech_status_cb(stte_speech_status_e status, void *user_data)
311 {
312         /* it seems to be no necessity yet */
313         SLOG(LOG_WARN, TAG_STTFC, "[WARNING] This callback should NOT be called.");
314         return;
315 }
316
317 void __stt_file_error_cb(stte_error_e error, const char* msg)
318 {
319         /* it seems to be no necessity yet */
320         SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Error callback is called");
321         return;
322 }
323
324 static int __stt_file_load_engine(sttengine_info_s* engine)
325 {
326         if (NULL == engine) {
327                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Input engine is NULL");
328                 return STT_FILE_ERROR_INVALID_PARAMETER;
329         }
330
331         if (0 != stt_engine_load(engine->engine_path, NULL)) {
332                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to load engine(%s)", engine->engine_path);
333                 return STT_FILE_ERROR_OPERATION_FAILED;
334         }
335
336         int ret = stt_engine_initialize(true);
337         if (0 != ret) {
338                 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine : id(%d) path(%s)", engine->engine_id, engine->engine_path);
339                 return STT_FILE_ERROR_OPERATION_FAILED;
340         }
341
342         engine->is_loaded = true;
343
344         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE Success] Load engine id(%d) path(%s)", engine->engine_id, engine->engine_path);
345
346         return STT_FILE_ERROR_NONE;
347 }
348
349 int stt_file_initialize(void)
350 {
351         SLOG(LOG_DEBUG, TAG_STTFC, "===== Initialize STT FILE");
352
353         stt_file_client_s* client = stt_file_client_get();
354         if (NULL != client) {
355                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Already initialized");
356                 return STT_FILE_ERROR_INVALID_STATE;
357         }
358
359         /* Get file name from default engine directory */
360         DIR *dp = NULL;
361         int ret = -1;
362         struct dirent *dirp = NULL;
363         dp  = opendir(STT_DEFAULT_ENGINE);
364         if (NULL != dp) {
365                 do {
366                         dirp = readdir(dp);
367
368                         if (NULL != dirp) {
369                                 if (!strcmp(".", dirp->d_name) || !strcmp("..", dirp->d_name))
370                                         continue;
371
372                                 sttengine_info_s* info = NULL;
373                                 char* filepath;
374                                 int filesize;
375
376                                 filesize = strlen(STT_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
377                                 filepath = (char*)calloc(filesize, sizeof(char));
378
379
380                                 if (NULL != filepath) {
381                                         snprintf(filepath, filesize, "%s/%s", STT_DEFAULT_ENGINE, dirp->d_name);
382                                 } else {
383                                         SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Memory not enough!!");
384                                         continue;
385                                 }
386
387                                 if (false == __stt_file_is_engine(filepath)) {
388                                         /* get its and update engine list */
389                                         if (0 == __stt_file_get_engine_info(filepath, &info)) {
390                                                 /* add engine info to g_engine_list */
391                                                 g_engine_list = g_slist_append(g_engine_list, info);
392                                         }
393                                 }
394
395                                 if (NULL != filepath)
396                                         free(filepath);
397                         }
398                 } while (NULL != dirp);
399
400                 closedir(dp);
401         } else {
402                 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent WARNING] Fail to open default directory");
403         }
404
405         if (0 >= g_slist_length(g_engine_list)) {
406                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Not found engine");
407                 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
408         }
409
410         ret = stt_file_client_new();
411         if (0 != ret) {
412                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to create client : %s", __stt_file_get_error_code(ret));
413                 return STT_FILE_ERROR_OPERATION_FAILED;
414         }
415
416         client = stt_file_client_get();
417         if (NULL == client) {
418                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
419                 return STT_FILE_ERROR_OPERATION_FAILED;
420         }
421
422         ret = stt_config_mgr_initialize(getpid() + STT_FILE_CONFIG_HANDLE);
423         if (0 != ret) {
424                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to init config manager : %s", __stt_file_get_error_code(ret));
425                 stt_file_client_destroy();
426                 return STT_FILE_ERROR_OPERATION_FAILED;
427         }
428
429         /* Get engine */
430         char* engine_id = NULL;
431         ret = stt_config_mgr_get_engine(&engine_id);
432         if (0 != ret) {
433                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to get engine id : %s", __stt_file_get_error_code(ret));
434                 stt_file_client_destroy();
435                 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
436                 __stt_file_relseae_engine_info();
437                 return STT_FILE_ERROR_OPERATION_FAILED;
438         }
439
440         SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Get engine id : %s", engine_id);
441
442         bool is_found = false;
443
444         sttengine_info_s* engine = NULL;
445         GSList *iter = NULL;
446         /* load engine */
447         if (0 < g_slist_length(g_engine_list)) {
448                 /* Get a first item */
449                 iter = g_slist_nth(g_engine_list, 0);
450
451                 while (NULL != iter) {
452                         /* Get handle data from list */
453                         engine = iter->data;
454
455                         if (0 == strcmp(engine->engine_uuid, engine_id)) {
456                                 is_found = true;
457                                 break;
458                         }
459
460                         iter = g_slist_next(iter);
461                 }
462         }
463
464         if (NULL != engine_id) {
465                 free(engine_id);
466                 engine_id = NULL;
467         }
468
469         if (false == is_found) {
470                 engine = NULL;
471                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Fail to find default engine");
472                 iter = g_slist_nth(g_engine_list, 0);
473                 if (NULL != iter)
474                         engine = iter->data;
475
476                 if (NULL == engine) {
477                         SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
478                         stt_file_client_destroy();
479                         stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
480                         __stt_file_relseae_engine_info();
481                         return STT_FILE_ERROR_OPERATION_FAILED;
482                 }
483                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Set default engine (%s)", engine->engine_name);
484         }
485
486         ret = __stt_file_load_engine(engine);
487         if (0 != ret) {
488                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
489                 stt_file_client_destroy();
490                 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
491                 __stt_file_relseae_engine_info();
492                 return STT_FILE_ERROR_OPERATION_FAILED;
493         }
494
495         client->current_engine_id = engine->engine_id;
496
497         stt_network_initialize();
498
499         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE Success] Initialize : pid(%d)", getpid());
500
501         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
502         SLOG(LOG_DEBUG, TAG_STTFC, " ");
503
504         return STT_FILE_ERROR_NONE;
505 }
506
507 int stt_file_deinitialize(void)
508 {
509         SLOG(LOG_DEBUG, TAG_STTFC, "===== Deinitialize STT FILE");
510
511         stt_file_client_s* client = stt_file_client_get();
512         if (NULL == client) {
513                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
514                 return STT_FILE_ERROR_INVALID_STATE;
515         }
516
517         /* check used callback */
518         if (0 != stt_file_client_get_use_callback(client)) {
519                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Client should NOT deinitialize STT FILE in callback function");
520                 return STT_FILE_ERROR_OPERATION_FAILED;
521         }
522
523         stt_network_finalize();
524
525         stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
526
527         /* check state */
528         switch (client->current_state) {
529         case STT_FILE_STATE_PROCESSING:
530                 /* Cancel file recognition */
531                 stt_engine_recognize_cancel_file();
532
533         case STT_FILE_STATE_READY:
534                 /* Unload engine */
535                 __stt_file_relseae_engine_info();
536
537                 /* Free resources */
538                 stt_file_client_destroy();
539                 break;
540
541         case STT_FILE_STATE_NONE:
542                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] NOT initialized");
543                 return 0;
544         default:
545                 break;
546         }
547
548         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
549         SLOG(LOG_DEBUG, TAG_STTFC, " ");
550
551         return STT_FILE_ERROR_NONE;
552 }
553
554 int stt_file_get_state(stt_file_state_e* state)
555 {
556         if (NULL == state) {
557                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
558                 return STT_FILE_ERROR_INVALID_PARAMETER;
559         }
560
561         stt_file_client_s* client = stt_file_client_get();
562
563         if (NULL == client) {
564                 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Not initialized");
565                 *state = STT_FILE_STATE_NONE;
566                 return STT_FILE_ERROR_NONE;
567         }
568
569         *state = client->current_state;
570
571         switch (*state) {
572         case STT_FILE_STATE_NONE:       SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'NONE'");          break;
573         case STT_FILE_STATE_READY:      SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Ready'");         break;
574         case STT_FILE_STATE_PROCESSING: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Processing'");    break;
575         default:                        SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid value");            break;
576         }
577
578         return STT_FILE_ERROR_NONE;
579 }
580
581 int stt_file_foreach_supported_engines(stt_file_supported_engine_cb callback, void* user_data)
582 {
583         SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported engine");
584
585         if (NULL == callback) {
586                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
587                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
588                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
589                 return STT_FILE_ERROR_INVALID_PARAMETER;
590         }
591
592         stt_file_client_s* client = stt_file_client_get();
593
594         /* check handle */
595         if (NULL == client) {
596                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
597                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
598                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
599                 return STT_FILE_ERROR_INVALID_PARAMETER;
600         }
601
602         if (client->current_state != STT_FILE_STATE_READY) {
603                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
604                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
605                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
606                 return STT_FILE_ERROR_INVALID_STATE;
607         }
608
609         GSList *iter = NULL;
610         sttengine_info_s *engine = NULL;
611
612         /* Get a first item */
613         iter = g_slist_nth(g_engine_list, 0);
614
615         while (NULL != iter) {
616                 /* Get handle data from list */
617                 engine = iter->data;
618
619                 if (NULL != engine) {
620                         if (false == callback(engine->engine_uuid, engine->engine_name, user_data)) {
621                                 break;
622                         }
623                 }
624                 iter = g_slist_next(iter);
625         }
626
627         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
628         SLOG(LOG_DEBUG, TAG_STTFC, " ");
629
630         return STT_FILE_ERROR_NONE;
631 }
632
633 int stt_file_get_engine(char** engine_id)
634 {
635         SLOG(LOG_DEBUG, TAG_STTFC, "===== Get current engine");
636
637         if (NULL == engine_id) {
638                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
639                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
640                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
641                 return STT_FILE_ERROR_INVALID_PARAMETER;
642         }
643
644         stt_file_client_s* client = stt_file_client_get();
645
646         /* check handle */
647         if (NULL == client) {
648                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
649                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
650                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
651                 return STT_FILE_ERROR_INVALID_PARAMETER;
652         }
653
654         if (client->current_state != STT_FILE_STATE_READY) {
655                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
656                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
657                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
658                 return STT_FILE_ERROR_INVALID_STATE;
659         }
660
661         sttengine_info_s* engine = NULL;
662         engine = __stt_file_get_engine_by_id(client->current_engine_id);
663         if (NULL == engine) {
664                 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
665                 return STT_FILE_ERROR_OPERATION_FAILED;
666         }
667
668         *engine_id = strdup(engine->engine_uuid);
669
670         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
671         SLOG(LOG_DEBUG, TAG_STTFC, " ");
672
673         return STT_FILE_ERROR_NONE;
674 }
675
676 int stt_file_set_engine(const char* engine_id)
677 {
678         SLOG(LOG_DEBUG, TAG_STTFC, "===== Set current engine");
679
680         if (NULL == engine_id) {
681                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
682                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
683                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
684                 return STT_FILE_ERROR_INVALID_PARAMETER;
685         }
686
687         stt_file_client_s* client = stt_file_client_get();
688
689         /* check handle */
690         if (NULL == client) {
691                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
692                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
693                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
694                 return STT_FILE_ERROR_INVALID_PARAMETER;
695         }
696
697         /* check state */
698         if (client->current_state != STT_FILE_STATE_READY) {
699                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
700                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
701                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
702                 return STT_FILE_ERROR_INVALID_STATE;
703         }
704
705         GSList *iter = NULL;
706         sttengine_info_s *engine = NULL;
707
708         int temp_old_engine = -1;
709
710         /* Get a first item */
711         iter = g_slist_nth(g_engine_list, 0);
712
713         while (NULL != iter) {
714                 /* Get handle data from list */
715                 engine = iter->data;
716
717                 if (0 == strcmp(engine->engine_uuid, engine_id)) {
718                         if (client->current_engine_id != engine->engine_id) {
719                                 temp_old_engine = client->current_engine_id;
720                         } else {
721                                 SLOG(LOG_DEBUG, TAG_STTFC, "Already loaded engine : %s", engine_id);
722                                 return 0;
723                         }
724
725                         break;
726                 }
727
728                 iter = g_slist_next(iter);
729                 engine = NULL;
730         }
731
732         if (NULL == engine) {
733                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Engine id is NOT valid");
734                 return STT_FILE_ERROR_INVALID_PARAMETER;
735         }
736
737         int ret = __stt_file_load_engine(engine);
738         if (0 != ret) {
739                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
740                 return STT_FILE_ERROR_OPERATION_FAILED;
741         }
742
743         client->current_engine_id = engine->engine_id;
744
745         if (-1 != temp_old_engine) {
746                 stt_engine_deinitialize();
747                 stt_engine_unload();
748         }
749
750         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
751         SLOG(LOG_DEBUG, TAG_STTFC, " ");
752
753         return 0;
754 }
755
756 bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
757 {
758         stt_file_client_s* client = stt_file_client_get();
759         if (NULL == client) {
760                 SLOG(LOG_ERROR, TAG_STTFC, "[WARNING] A handle is not valid");
761                 return false;
762         }
763
764         /* call callback function */
765         if (NULL != client->supported_lang_cb) {
766                 return client->supported_lang_cb(language, client->supported_lang_user_data);
767         } else {
768                 SLOG(LOG_WARN, TAG_STTFC, "No registered callback function of supported languages");
769         }
770
771         return false;
772 }
773
774 int stt_file_foreach_supported_languages(stt_file_supported_language_cb callback, void* user_data)
775 {
776         SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported Language");
777
778         if (NULL == callback) {
779                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
780                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
781                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
782                 return STT_FILE_ERROR_INVALID_PARAMETER;
783         }
784
785         stt_file_client_s* client = stt_file_client_get();
786
787         /* check handle */
788         if (NULL == client) {
789                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
790                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
791                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
792                 return STT_FILE_ERROR_INVALID_PARAMETER;
793         }
794
795         sttengine_info_s* engine = NULL;
796         engine = __stt_file_get_engine_by_id(client->current_engine_id);
797         if (NULL == engine) {
798                 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
799                 return STT_FILE_ERROR_OPERATION_FAILED;
800         }
801
802         client->supported_lang_cb = callback;
803         client->supported_lang_user_data = user_data;
804
805         int ret = -1;
806         ret = stt_config_mgr_get_language_list(engine->engine_uuid, __stt_config_supported_language_cb, NULL);
807         if (0 != ret) {
808                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get languages");
809         }
810
811         client->supported_lang_cb = NULL;
812         client->supported_lang_user_data = NULL;
813
814         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
815         SLOG(LOG_DEBUG, TAG_STTFC, " ");
816
817         return STT_FILE_ERROR_NONE;
818 }
819
820 int stt_file_start(const char* language, const char* type, const char* filepath,
821                 stt_file_audio_type_e audio_type, int sample_rate)
822 {
823         SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE START");
824
825         stt_file_client_s* client = stt_file_client_get();
826
827         /* check handle */
828         if (NULL == client) {
829                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
830                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
831                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
832                 return STT_FILE_ERROR_INVALID_STATE;
833         }
834
835         /* check state */
836         if (client->current_state != STT_FILE_STATE_READY) {
837                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
838                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
839                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
840                 return STT_FILE_ERROR_INVALID_STATE;
841         }
842
843         if (NULL == language || NULL == filepath) {
844                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
845                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
846                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
847                 return STT_FILE_ERROR_INVALID_PARAMETER;
848         }
849
850         /* check if engine use network */
851         sttengine_info_s* engine = NULL;
852         engine = __stt_file_get_engine_by_id(client->current_engine_id);
853         if (NULL == engine) {
854                 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
855                 return STT_FILE_ERROR_OPERATION_FAILED;
856         }
857
858         if (true == engine->use_network) {
859                 if (false == stt_network_is_connected()) {
860                         SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Disconnect network. Current engine needs to network connection.");
861                         return STT_FILE_ERROR_OUT_OF_NETWORK;
862                 }
863         }
864
865         SLOG(LOG_DEBUG, TAG_STTFC, "[START Info] Engine(%d) Lang(%s) Type(%s) Filepath(%s) Audio(%d) Sample rate(%d)",
866                 client->current_engine_id, language, type, filepath, audio_type, sample_rate);
867
868         int ret = -1;
869         ret = stt_engine_recognize_start_file(language, type, filepath, audio_type, sample_rate, NULL);
870         if (0 != ret) {
871                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to start file recognition");
872                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
873                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
874                 return STT_FILE_ERROR_OPERATION_FAILED;
875         }
876
877         client->before_state = client->current_state;
878         client->current_state = STT_FILE_STATE_PROCESSING;
879
880         if (NULL != client->state_changed_cb) {
881                 stt_file_client_use_callback(client);
882                 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
883                 stt_file_client_not_use_callback(client);
884                 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
885         } else {
886                 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
887         }
888
889         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
890         SLOG(LOG_DEBUG, TAG_STTFC, " ");
891
892         return STT_FILE_ERROR_NONE;
893 }
894
895 int stt_file_cancel(void)
896 {
897         SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE CANCEL");
898
899         stt_file_client_s* client = stt_file_client_get();
900
901         /* check handle */
902         if (NULL == client) {
903                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
904                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
905                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
906                 return STT_FILE_ERROR_INVALID_PARAMETER;
907         }
908
909         /* check state */
910         if (STT_FILE_STATE_PROCESSING != client->current_state) {
911                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid state : Current state(%d) is NOT 'Processing'", client->current_state);
912                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
913                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
914                 return STT_FILE_ERROR_INVALID_STATE;
915         }
916
917         int ret = -1;
918         ret = stt_engine_recognize_cancel_file();
919         if (0 != ret) {
920                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to cancel file recognition");
921                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
922                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
923                 return STT_FILE_ERROR_OPERATION_FAILED;
924         }
925
926         client->before_state = client->current_state;
927         client->current_state = STT_FILE_STATE_READY;
928
929         if (NULL != client->state_changed_cb) {
930                 stt_file_client_use_callback(client);
931                 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
932                 stt_file_client_not_use_callback(client);
933                 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
934         } else {
935                 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
936         }
937
938         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
939         SLOG(LOG_DEBUG, TAG_STTFC, " ");
940
941         return STT_FILE_ERROR_NONE;
942 }
943
944 bool __stt_file_result_time_cb(int index, stte_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
945 {
946         SLOG(LOG_DEBUG, TAG_STTFC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
947                 index, event, text, start_time, end_time);
948
949         stt_file_client_s* client = stt_file_client_get();
950
951         /* check handle */
952         if (NULL == client) {
953                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to notify error : A handle is not valid");
954                 return EINA_FALSE;
955         }
956
957         if (NULL != client->result_time_cb) {
958                 client->result_time_cb(index, (stt_file_result_time_event_e)event,
959                         text, start_time, end_time, client->result_time_user_data);
960         } else {
961                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Callback is NULL");
962                 return false;
963         }
964
965         return true;
966 }
967
968 int stt_file_foreach_detailed_result(stt_file_result_time_cb callback, void* user_data)
969 {
970         SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE FOREACH DETAILED RESULT");
971
972         if (NULL == callback) {
973                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
974                 return STT_FILE_ERROR_INVALID_PARAMETER;
975         }
976
977         stt_file_client_s* client = stt_file_client_get();
978
979         /* check handle */
980         if (NULL == client) {
981                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail : A handle is not valid");
982                 return STT_FILE_ERROR_INVALID_PARAMETER;
983         }
984
985         client->result_time_cb = callback;
986         client->result_time_user_data = user_data;
987
988         stt_engine_foreach_result_time(client->time_info, __stt_file_result_time_cb, NULL);
989
990         client->result_time_cb = NULL;
991         client->result_time_user_data = NULL;
992
993         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
994         SLOG(LOG_DEBUG, TAG_STTFC, " ");
995
996         return STT_FILE_ERROR_NONE;
997 }
998
999 int stt_file_set_recognition_result_cb(stt_file_recognition_result_cb callback, void* user_data)
1000 {
1001         if (NULL == callback)
1002                 return STT_FILE_ERROR_INVALID_PARAMETER;
1003
1004         stt_file_client_s* client = stt_file_client_get();
1005
1006         /* check handle */
1007         if (NULL == client) {
1008                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1009                 return STT_FILE_ERROR_INVALID_PARAMETER;
1010         }
1011
1012         if (STT_FILE_STATE_READY != client->current_state) {
1013                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1014                 return STT_FILE_ERROR_INVALID_STATE;
1015         }
1016
1017         int ret = stt_engine_set_recognition_result_cb(__stt_file_result_cb, NULL);
1018         if (0 != ret) {
1019                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1020         }
1021
1022         client->recognition_result_cb = callback;
1023         client->recognition_result_user_data = user_data;
1024
1025         return ret;
1026 }
1027
1028 int stt_file_unset_recognition_result_cb(void)
1029 {
1030         stt_file_client_s* client = stt_file_client_get();
1031
1032         /* check handle */
1033         if (NULL == client) {
1034                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1035                 return STT_FILE_ERROR_INVALID_PARAMETER;
1036         }
1037
1038         if (STT_FILE_STATE_READY != client->current_state) {
1039                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1040                 return STT_FILE_ERROR_INVALID_STATE;
1041         }
1042
1043         int ret = stt_engine_set_recognition_result_cb(NULL, NULL);
1044         if (0 != ret) {
1045                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1046         }
1047
1048         client->recognition_result_cb = NULL;
1049         client->recognition_result_user_data = NULL;
1050
1051         return ret;
1052 }
1053
1054 int stt_file_set_state_changed_cb(stt_file_state_changed_cb callback, void* user_data)
1055 {
1056         if (NULL == callback)
1057                 return STT_FILE_ERROR_INVALID_PARAMETER;
1058
1059         stt_file_client_s* client = stt_file_client_get();
1060
1061         /* check handle */
1062         if (NULL == client) {
1063                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1064                 return STT_FILE_ERROR_INVALID_PARAMETER;
1065         }
1066
1067         if (STT_FILE_STATE_READY != client->current_state) {
1068                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1069                 return STT_FILE_ERROR_INVALID_STATE;
1070         }
1071
1072         client->state_changed_cb = callback;
1073         client->state_changed_user_data = user_data;
1074
1075         return 0;
1076 }
1077
1078 int stt_file_unset_state_changed_cb(void)
1079 {
1080         stt_file_client_s* client = stt_file_client_get();
1081
1082         /* check handle */
1083         if (NULL == client) {
1084                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1085                 return STT_FILE_ERROR_INVALID_PARAMETER;
1086         }
1087
1088         if (STT_FILE_STATE_READY != client->current_state) {
1089                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1090                 return STT_FILE_ERROR_INVALID_STATE;
1091         }
1092
1093         client->state_changed_cb = NULL;
1094         client->state_changed_user_data = NULL;
1095
1096         return 0;
1097 }