Fix unused codes
[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 void __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;
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;
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 entry;
363         struct dirent *dirp = NULL;
364         dp  = opendir(STT_DEFAULT_ENGINE);
365         if (NULL != dp) {
366                 do {
367                         ret = readdir_r(dp, &entry, &dirp);
368                         if (0 != ret) {
369                                 SLOG(LOG_ERROR, TAG_STTFC, "[File ERROR] Fail to read directory");
370                                 break;
371                         }
372
373                         if (NULL != dirp) {
374                                 sttengine_info_s* info = NULL;
375                                 char* filepath;
376                                 int filesize;
377
378                                 filesize = strlen(STT_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
379                                 filepath = (char*)calloc(filesize, sizeof(char));
380
381
382                                 if (NULL != filepath) {
383                                         snprintf(filepath, filesize, "%s/%s", STT_DEFAULT_ENGINE, dirp->d_name);
384                                 } else {
385                                         SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Memory not enough!!");
386                                         continue;
387                                 }
388
389                                 if (false == __stt_file_is_engine(filepath)) {
390                                         /* get its and update engine list */
391                                         if (0 == __stt_file_get_engine_info(filepath, &info)) {
392                                                 /* add engine info to g_engine_list */
393                                                 g_engine_list = g_slist_append(g_engine_list, info);
394                                         }
395                                 }
396
397                                 if (NULL != filepath)
398                                         free(filepath);
399                         }
400                 } while (NULL != dirp);
401
402                 closedir(dp);
403         } else {
404                 SLOG(LOG_WARN, TAG_STTFC, "[Engine Agent WARNING] Fail to open default directory");
405         }
406
407         if (0 >= g_slist_length(g_engine_list)) {
408                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Not found engine");
409                 return STT_FILE_ERROR_ENGINE_NOT_FOUND;
410         }
411
412         ret = stt_file_client_new();
413         if (0 != ret) {
414                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to create client : %s", __stt_file_get_error_code(ret));
415                 return STT_FILE_ERROR_OPERATION_FAILED;
416         }
417
418         client = stt_file_client_get();
419         if (NULL == client) {
420                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
421                 return STT_FILE_ERROR_OPERATION_FAILED;
422         }
423
424         ret = stt_config_mgr_initialize(getpid() + STT_FILE_CONFIG_HANDLE);
425         if (0 != ret) {
426                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to init config manager : %s", __stt_file_get_error_code(ret));
427                 stt_file_client_destroy();
428                 return STT_FILE_ERROR_OPERATION_FAILED;
429         }
430
431         /* Get engine */
432         char* engine_id = NULL;
433         ret = stt_config_mgr_get_engine(&engine_id);
434         if (0 != ret) {
435                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Fail to get engine id : %s", __stt_file_get_error_code(ret));
436                 stt_file_client_destroy();
437                 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
438                 __stt_file_relseae_engine_info();
439                 return STT_FILE_ERROR_OPERATION_FAILED;
440         }
441
442         SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Get engine id : %s", engine_id);
443
444         bool is_found = false;
445
446         sttengine_info_s* engine = NULL;
447         GSList *iter = NULL;
448         /* load engine */
449         if (0 < g_slist_length(g_engine_list)) {
450                 /* Get a first item */
451                 iter = g_slist_nth(g_engine_list, 0);
452
453                 while (NULL != iter) {
454                         /* Get handle data from list */
455                         engine = iter->data;
456
457                         if (0 == strcmp(engine->engine_uuid, engine_id)) {
458                                 is_found = true;
459                                 break;
460                         }
461
462                         iter = g_slist_next(iter);
463                 }
464         }
465
466         if (NULL != engine_id) {
467                 free(engine_id);
468                 engine_id = NULL;
469         }
470
471         if (false == is_found) {
472                 engine = NULL;
473                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Fail to find default engine");
474                 iter = g_slist_nth(g_engine_list, 0);
475                 if (NULL != iter)
476                         engine = iter->data;
477
478                 if (NULL == engine) {
479                         SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
480                         stt_file_client_destroy();
481                         stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
482                         __stt_file_relseae_engine_info();
483                         return STT_FILE_ERROR_OPERATION_FAILED;
484                 }
485                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] Set default engine (%s)", engine->engine_name);
486         }
487
488         ret = __stt_file_load_engine(engine);
489         if (0 != ret) {
490                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
491                 stt_file_client_destroy();
492                 stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
493                 __stt_file_relseae_engine_info();
494                 return STT_FILE_ERROR_OPERATION_FAILED;
495         }
496
497         client->current_engine_id = engine->engine_id;
498
499         stt_network_initialize();
500
501         SECURE_SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE Success] Initialize : pid(%d)", getpid());
502
503         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
504         SLOG(LOG_DEBUG, TAG_STTFC, " ");
505
506         return STT_FILE_ERROR_NONE;
507 }
508
509 int stt_file_deinitialize(void)
510 {
511         SLOG(LOG_DEBUG, TAG_STTFC, "===== Deinitialize STT FILE");
512
513         stt_file_client_s* client = stt_file_client_get();
514         if (NULL == client) {
515                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Not initialized");
516                 return STT_FILE_ERROR_INVALID_STATE;
517         }
518
519         /* check used callback */
520         if (0 != stt_file_client_get_use_callback(client)) {
521                 SLOG(LOG_ERROR, TAG_STTFC, "[STT FILE ERROR] Client should NOT deinitialize STT FILE in callback function");
522                 return STT_FILE_ERROR_OPERATION_FAILED;
523         }
524
525         stt_network_finalize();
526
527         stt_config_mgr_finalize(getpid() + STT_FILE_CONFIG_HANDLE);
528
529         /* check state */
530         switch (client->current_state) {
531         case STT_FILE_STATE_PROCESSING:
532                 /* Cancel file recognition */
533                 stt_engine_recognize_cancel_file();
534
535         case STT_FILE_STATE_READY:
536                 /* Unload engine */
537                 __stt_file_relseae_engine_info();
538
539                 /* Free resources */
540                 stt_file_client_destroy();
541                 break;
542
543         case STT_FILE_STATE_NONE:
544                 SLOG(LOG_WARN, TAG_STTFC, "[STT FILE WARNING] NOT initialized");
545                 return 0;
546         default:
547                 break;
548         }
549
550         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
551         SLOG(LOG_DEBUG, TAG_STTFC, " ");
552
553         return STT_FILE_ERROR_NONE;
554 }
555
556 int stt_file_get_state(stt_file_state_e* state)
557 {
558         if (NULL == state) {
559                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
560                 return STT_FILE_ERROR_INVALID_PARAMETER;
561         }
562
563         stt_file_client_s* client = stt_file_client_get();
564
565         if (NULL == client) {
566                 SLOG(LOG_DEBUG, TAG_STTFC, "[STT FILE] Not initialized");
567                 *state = STT_FILE_STATE_NONE;
568                 return STT_FILE_ERROR_NONE;
569         }
570
571         *state = client->current_state;
572
573         switch (*state) {
574         case STT_FILE_STATE_NONE:       SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'NONE'");          break;
575         case STT_FILE_STATE_READY:      SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Ready'");         break;
576         case STT_FILE_STATE_PROCESSING: SLOG(LOG_DEBUG, TAG_STTFC, "Current state is 'Processing'");    break;
577         default:                        SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid value");            break;
578         }
579
580         return STT_FILE_ERROR_NONE;
581 }
582
583 int stt_file_foreach_supported_engines(stt_file_supported_engine_cb callback, void* user_data)
584 {
585         SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported engine");
586
587         if (NULL == callback) {
588                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
589                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
590                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
591                 return STT_FILE_ERROR_INVALID_PARAMETER;
592         }
593
594         stt_file_client_s* client = stt_file_client_get();
595
596         /* check handle */
597         if (NULL == client) {
598                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
599                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
600                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
601                 return STT_FILE_ERROR_INVALID_PARAMETER;
602         }
603
604         if (client->current_state != STT_FILE_STATE_READY) {
605                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
606                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
607                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
608                 return STT_FILE_ERROR_INVALID_STATE;
609         }
610
611         GSList *iter = NULL;
612         sttengine_info_s *engine = NULL;
613
614         /* Get a first item */
615         iter = g_slist_nth(g_engine_list, 0);
616
617         while (NULL != iter) {
618                 /* Get handle data from list */
619                 engine = iter->data;
620
621                 if (NULL != engine) {
622                         if (false == callback(engine->engine_uuid, engine->engine_name, user_data)) {
623                                 break;
624                         }
625                 }
626                 iter = g_slist_next(iter);
627         }
628
629         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
630         SLOG(LOG_DEBUG, TAG_STTFC, " ");
631
632         return STT_FILE_ERROR_NONE;
633 }
634
635 int stt_file_get_engine(char** engine_id)
636 {
637         SLOG(LOG_DEBUG, TAG_STTFC, "===== Get current engine");
638
639         if (NULL == engine_id) {
640                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
641                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
642                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
643                 return STT_FILE_ERROR_INVALID_PARAMETER;
644         }
645
646         stt_file_client_s* client = stt_file_client_get();
647
648         /* check handle */
649         if (NULL == client) {
650                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
651                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
652                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
653                 return STT_FILE_ERROR_INVALID_PARAMETER;
654         }
655
656         if (client->current_state != STT_FILE_STATE_READY) {
657                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
658                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
659                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
660                 return STT_FILE_ERROR_INVALID_STATE;
661         }
662
663         sttengine_info_s* engine = NULL;
664         engine = __stt_file_get_engine_by_id(client->current_engine_id);
665         if (NULL == engine) {
666                 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
667                 return STT_FILE_ERROR_OPERATION_FAILED;
668         }
669
670         *engine_id = strdup(engine->engine_uuid);
671
672         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
673         SLOG(LOG_DEBUG, TAG_STTFC, " ");
674
675         return STT_FILE_ERROR_NONE;
676 }
677
678 int stt_file_set_engine(const char* engine_id)
679 {
680         SLOG(LOG_DEBUG, TAG_STTFC, "===== Set current engine");
681
682         if (NULL == engine_id) {
683                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
684                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
685                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
686                 return STT_FILE_ERROR_INVALID_PARAMETER;
687         }
688
689         stt_file_client_s* client = stt_file_client_get();
690
691         /* check handle */
692         if (NULL == client) {
693                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
694                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
695                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
696                 return STT_FILE_ERROR_INVALID_PARAMETER;
697         }
698
699         /* check state */
700         if (client->current_state != STT_FILE_STATE_READY) {
701                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", client->current_state);
702                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
703                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
704                 return STT_FILE_ERROR_INVALID_STATE;
705         }
706
707         GSList *iter = NULL;
708         sttengine_info_s *engine = NULL;
709
710         int temp_old_engine = -1;
711
712         /* Get a first item */
713         iter = g_slist_nth(g_engine_list, 0);
714
715         while (NULL != iter) {
716                 /* Get handle data from list */
717                 engine = iter->data;
718
719                 if (0 == strcmp(engine->engine_uuid, engine_id)) {
720                         if (client->current_engine_id != engine->engine_id) {
721                                 temp_old_engine = client->current_engine_id;
722                         } else {
723                                 SLOG(LOG_DEBUG, TAG_STTFC, "Already loaded engine : %s", engine_id);
724                                 return 0;
725                         }
726
727                         break;
728                 }
729
730                 iter = g_slist_next(iter);
731                 engine = NULL;
732         }
733
734         if (NULL == engine) {
735                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Engine id is NOT valid");
736                 return STT_FILE_ERROR_INVALID_PARAMETER;
737         }
738
739         int ret = __stt_file_load_engine(engine);
740         if (0 != ret) {
741                 SLOG(LOG_ERROR, TAG_STTFC, "[Engine Agent ERROR] Fail to initialize engine");
742                 return STT_FILE_ERROR_OPERATION_FAILED;
743         }
744
745         client->current_engine_id = engine->engine_id;
746
747         if (-1 != temp_old_engine) {
748                 stt_engine_deinitialize();
749                 stt_engine_unload();
750         }
751
752         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
753         SLOG(LOG_DEBUG, TAG_STTFC, " ");
754
755         return 0;
756 }
757
758 bool __stt_config_supported_language_cb(const char* engine_id, const char* language, void* user_data)
759 {
760         stt_file_client_s* client = stt_file_client_get();
761         if (NULL == client) {
762                 SLOG(LOG_ERROR, TAG_STTFC, "[WARNING] A handle is not valid");
763                 return false;
764         }
765
766         /* call callback function */
767         if (NULL != client->supported_lang_cb) {
768                 return client->supported_lang_cb(language, client->supported_lang_user_data);
769         } else {
770                 SLOG(LOG_WARN, TAG_STTFC, "No registered callback function of supported languages");
771         }
772
773         return false;
774 }
775
776 int stt_file_foreach_supported_languages(stt_file_supported_language_cb callback, void* user_data)
777 {
778         SLOG(LOG_DEBUG, TAG_STTFC, "===== Foreach Supported Language");
779
780         if (NULL == callback) {
781                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
782                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
783                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
784                 return STT_FILE_ERROR_INVALID_PARAMETER;
785         }
786
787         stt_file_client_s* client = stt_file_client_get();
788
789         /* check handle */
790         if (NULL == client) {
791                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
792                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
793                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
794                 return STT_FILE_ERROR_INVALID_PARAMETER;
795         }
796
797         sttengine_info_s* engine = NULL;
798         engine = __stt_file_get_engine_by_id(client->current_engine_id);
799         if (NULL == engine) {
800                 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
801                 return STT_FILE_ERROR_OPERATION_FAILED;
802         }
803
804         client->supported_lang_cb = callback;
805         client->supported_lang_user_data = user_data;
806
807         int ret = -1;
808         ret = stt_config_mgr_get_language_list(engine->engine_uuid, __stt_config_supported_language_cb, NULL);
809         if (0 != ret) {
810                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get languages");
811         }
812
813         client->supported_lang_cb = NULL;
814         client->supported_lang_user_data = NULL;
815
816         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
817         SLOG(LOG_DEBUG, TAG_STTFC, " ");
818
819         return STT_FILE_ERROR_NONE;
820 }
821
822 int stt_file_start(const char* language, const char* type, const char* filepath,
823                 stt_file_audio_type_e audio_type, int sample_rate)
824 {
825         SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE START");
826
827         stt_file_client_s* client = stt_file_client_get();
828
829         /* check handle */
830         if (NULL == client) {
831                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
832                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
833                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
834                 return STT_FILE_ERROR_INVALID_STATE;
835         }
836
837         /* check state */
838         if (client->current_state != STT_FILE_STATE_READY) {
839                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
840                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
841                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
842                 return STT_FILE_ERROR_INVALID_STATE;
843         }
844
845         if (NULL == language || NULL == filepath) {
846                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
847                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
848                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
849                 return STT_FILE_ERROR_INVALID_PARAMETER;
850         }
851
852         /* check if engine use network */
853         sttengine_info_s* engine = NULL;
854         engine = __stt_file_get_engine_by_id(client->current_engine_id);
855         if (NULL == engine) {
856                 SECURE_SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to get engine info(%d)", client->current_engine_id);
857                 return STT_FILE_ERROR_OPERATION_FAILED;
858         }
859
860         if (true == engine->use_network) {
861                 if (false == stt_network_is_connected()) {
862                         SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Disconnect network. Current engine needs to network connection.");
863                         return STT_FILE_ERROR_OUT_OF_NETWORK;
864                 }
865         }
866
867         SLOG(LOG_DEBUG, TAG_STTFC, "[START Info] Engine(%d) Lang(%s) Type(%s) Filepath(%s) Audio(%d) Sample rate(%d)",
868                 client->current_engine_id, language, type, filepath, audio_type, sample_rate);
869
870         int ret = -1;
871         ret = stt_engine_recognize_start_file(language, type, filepath, audio_type, sample_rate, NULL);
872         if (0 != ret) {
873                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to start file recognition");
874                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
875                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
876                 return STT_FILE_ERROR_OPERATION_FAILED;
877         }
878
879         client->before_state = client->current_state;
880         client->current_state = STT_FILE_STATE_PROCESSING;
881
882         if (NULL != client->state_changed_cb) {
883                 stt_file_client_use_callback(client);
884                 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
885                 stt_file_client_not_use_callback(client);
886                 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
887         } else {
888                 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
889         }
890
891         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
892         SLOG(LOG_DEBUG, TAG_STTFC, " ");
893
894         return STT_FILE_ERROR_NONE;
895 }
896
897 int stt_file_cancel(void)
898 {
899         SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE CANCEL");
900
901         stt_file_client_s* client = stt_file_client_get();
902
903         /* check handle */
904         if (NULL == client) {
905                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
906                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
907                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
908                 return STT_FILE_ERROR_INVALID_PARAMETER;
909         }
910
911         /* check state */
912         if (STT_FILE_STATE_PROCESSING != client->current_state) {
913                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Invalid state : Current state(%d) is NOT 'Processing'", client->current_state);
914                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
915                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
916                 return STT_FILE_ERROR_INVALID_STATE;
917         }
918
919         int ret = -1;
920         ret = stt_engine_recognize_cancel_file();
921         if (0 != ret) {
922                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to cancel file recognition");
923                 SLOG(LOG_DEBUG, TAG_STTFC, "=====");
924                 SLOG(LOG_DEBUG, TAG_STTFC, " ");
925                 return STT_FILE_ERROR_OPERATION_FAILED;
926         }
927
928         client->before_state = client->current_state;
929         client->current_state = STT_FILE_STATE_READY;
930
931         if (NULL != client->state_changed_cb) {
932                 stt_file_client_use_callback(client);
933                 client->state_changed_cb(client->before_state, client->current_state, client->state_changed_user_data);
934                 stt_file_client_not_use_callback(client);
935                 SLOG(LOG_DEBUG, TAG_STTFC, "State changed callback is called");
936         } else {
937                 SLOG(LOG_WARN, TAG_STTFC, "[WARNING] State changed callback is null");
938         }
939
940         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
941         SLOG(LOG_DEBUG, TAG_STTFC, " ");
942
943         return STT_FILE_ERROR_NONE;
944 }
945
946 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)
947 {
948         SLOG(LOG_DEBUG, TAG_STTFC, "(%d) event(%d) text(%s) start(%ld) end(%ld)",
949                 index, event, text, start_time, end_time);
950
951         stt_file_client_s* client = stt_file_client_get();
952
953         /* check handle */
954         if (NULL == client) {
955                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to notify error : A handle is not valid");
956                 return EINA_FALSE;
957         }
958
959         if (NULL != client->result_time_cb) {
960                 client->result_time_cb(index, (stt_file_result_time_event_e)event,
961                         text, start_time, end_time, client->result_time_user_data);
962         } else {
963                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Callback is NULL");
964                 return false;
965         }
966
967         return true;
968 }
969
970 int stt_file_foreach_detailed_result(stt_file_result_time_cb callback, void* user_data)
971 {
972         SLOG(LOG_DEBUG, TAG_STTFC, "===== STT FILE FOREACH DETAILED RESULT");
973
974         if (NULL == callback) {
975                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Input parameter is NULL");
976                 return STT_FILE_ERROR_INVALID_PARAMETER;
977         }
978
979         stt_file_client_s* client = stt_file_client_get();
980
981         /* check handle */
982         if (NULL == client) {
983                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail : A handle is not valid");
984                 return STT_FILE_ERROR_INVALID_PARAMETER;
985         }
986
987         client->result_time_cb = callback;
988         client->result_time_user_data = user_data;
989
990         stt_engine_foreach_result_time(client->time_info, __stt_file_result_time_cb, NULL);
991
992         client->result_time_cb = NULL;
993         client->result_time_user_data = NULL;
994
995         SLOG(LOG_DEBUG, TAG_STTFC, "=====");
996         SLOG(LOG_DEBUG, TAG_STTFC, " ");
997
998         return STT_FILE_ERROR_NONE;
999 }
1000
1001 int stt_file_set_recognition_result_cb(stt_file_recognition_result_cb callback, void* user_data)
1002 {
1003         if (NULL == callback)
1004                 return STT_FILE_ERROR_INVALID_PARAMETER;
1005
1006         stt_file_client_s* client = stt_file_client_get();
1007
1008         /* check handle */
1009         if (NULL == client) {
1010                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1011                 return STT_FILE_ERROR_INVALID_PARAMETER;
1012         }
1013
1014         if (STT_FILE_STATE_READY != client->current_state) {
1015                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1016                 return STT_FILE_ERROR_INVALID_STATE;
1017         }
1018
1019         int ret = stt_engine_set_recognition_result_cb(__stt_file_result_cb, NULL);
1020         if (0 != ret) {
1021                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1022         }
1023
1024         client->recognition_result_cb = callback;
1025         client->recognition_result_user_data = user_data;
1026
1027         return ret;
1028 }
1029
1030 int stt_file_unset_recognition_result_cb(void)
1031 {
1032         stt_file_client_s* client = stt_file_client_get();
1033
1034         /* check handle */
1035         if (NULL == client) {
1036                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1037                 return STT_FILE_ERROR_INVALID_PARAMETER;
1038         }
1039
1040         if (STT_FILE_STATE_READY != client->current_state) {
1041                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1042                 return STT_FILE_ERROR_INVALID_STATE;
1043         }
1044
1045         int ret = stt_engine_set_recognition_result_cb(NULL, NULL);
1046         if (0 != ret) {
1047                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Fail to set recognition result cb");
1048         }
1049
1050         client->recognition_result_cb = NULL;
1051         client->recognition_result_user_data = NULL;
1052
1053         return ret;
1054 }
1055
1056 int stt_file_set_state_changed_cb(stt_file_state_changed_cb callback, void* user_data)
1057 {
1058         if (NULL == callback)
1059                 return STT_FILE_ERROR_INVALID_PARAMETER;
1060
1061         stt_file_client_s* client = stt_file_client_get();
1062
1063         /* check handle */
1064         if (NULL == client) {
1065                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1066                 return STT_FILE_ERROR_INVALID_PARAMETER;
1067         }
1068
1069         if (STT_FILE_STATE_READY != client->current_state) {
1070                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1071                 return STT_FILE_ERROR_INVALID_STATE;
1072         }
1073
1074         client->state_changed_cb = callback;
1075         client->state_changed_user_data = user_data;
1076
1077         return 0;
1078 }
1079
1080 int stt_file_unset_state_changed_cb(void)
1081 {
1082         stt_file_client_s* client = stt_file_client_get();
1083
1084         /* check handle */
1085         if (NULL == client) {
1086                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] A handle is not available");
1087                 return STT_FILE_ERROR_INVALID_PARAMETER;
1088         }
1089
1090         if (STT_FILE_STATE_READY != client->current_state) {
1091                 SLOG(LOG_ERROR, TAG_STTFC, "[ERROR] Current state(%d) is not 'ready'", client->current_state);
1092                 return STT_FILE_ERROR_INVALID_STATE;
1093         }
1094
1095         client->state_changed_cb = NULL;
1096         client->state_changed_user_data = NULL;
1097
1098         return 0;
1099 }